// Make sure GrRecordReplaceDraw balances unbalanced saves DEF_TEST(RecordReplaceDraw_Unbalanced, r) { sk_sp<SkPicture> pic; { SkPictureRecorder recorder; SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); // We won't balance this, but GrRecordReplaceDraw will for us. canvas->save(); canvas->scale(2, 2); pic = recorder.finishRecordingAsPicture(); // we may have optimized everything away. If so, just return if (pic->approximateOpCount() == 0) { return; } } SkRecord rerecord; SkRecorder canvas(&rerecord, kWidth, kHeight); GrRecordReplaceDraw(pic.get(), &canvas, nullptr, SkMatrix::I(), nullptr/*callback*/); // ensure rerecord is balanced (in this case by checking that the count is odd) REPORTER_ASSERT(r, (rerecord.count() & 1) == 1); }
// Make sure the abort callback works DEF_TEST(RecordReplaceDraw_Abort, r) { sk_sp<SkPicture> pic; { // Record two commands. SkPictureRecorder recorder; SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), SkPaint()); canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight))); pic = recorder.finishRecordingAsPicture(); } SkRecord rerecord; SkRecorder canvas(&rerecord, kWidth, kHeight); JustOneDraw callback; GrRecordReplaceDraw(pic.get(), &canvas, nullptr, SkMatrix::I(), &callback); switch (rerecord.count()) { case 3: assert_type<SkRecords::Save>(r, rerecord, 0); assert_type<SkRecords::DrawRect>(r, rerecord, 1); assert_type<SkRecords::Restore>(r, rerecord, 2); break; case 1: assert_type<SkRecords::DrawRect>(r, rerecord, 0); break; default: REPORTER_ASSERT(r, false); } }
// Test out the layer replacement functionality with and w/o a BBH void test_replacements(skiatest::Reporter* r, GrContext* context, bool doReplace) { sk_sp<SkPicture> pic; { SkPictureRecorder recorder; SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); SkPaint paint; canvas->saveLayer(nullptr, &paint); canvas->clear(SK_ColorRED); canvas->restore(); canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar(kHeight / 2)), SkPaint()); pic = recorder.finishRecordingAsPicture(); } SkAutoTUnref<GrTexture> texture; SkPaint paint; GrLayerCache* layerCache = context->getLayerCache(); if (doReplace) { int key[1] = { 0 }; GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0, 2, SkIRect::MakeWH(kWidth, kHeight), SkIRect::MakeWH(kWidth, kHeight), SkMatrix::I(), key, 1, &paint); GrSurfaceDesc desc; desc.fConfig = kSkia8888_GrPixelConfig; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fWidth = kWidth; desc.fHeight = kHeight; desc.fSampleCnt = 0; // Giving the texture some initial data so the Gpu (specifically vulkan) does not complain // when reading from an uninitialized texture. SkAutoTMalloc<uint32_t> srcBuffer(kWidth*kHeight); memset(srcBuffer.get(), 0, kWidth*kHeight*sizeof(uint32_t)); texture.reset(context->textureProvider()->createTexture( desc, SkBudgeted::kNo, srcBuffer.get(), 0)); layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight), false); } SkRecord rerecord; SkRecorder canvas(&rerecord, kWidth, kHeight); GrRecordReplaceDraw(pic.get(), &canvas, layerCache, SkMatrix::I(), nullptr/*callback*/); int numLayers = count_instances_of_type<SkRecords::SaveLayer>(rerecord); if (doReplace) { REPORTER_ASSERT(r, 0 == numLayers); } else { REPORTER_ASSERT(r, 1 == numLayers); } }
// Test out the layer replacement functionality with and w/o a BBH void test_replacements(skiatest::Reporter* r, GrContext* context, bool doReplace) { SkAutoTUnref<const SkPicture> pic; { SkPictureRecorder recorder; SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); SkPaint paint; canvas->saveLayer(nullptr, &paint); canvas->clear(SK_ColorRED); canvas->restore(); canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar(kHeight / 2)), SkPaint()); pic.reset(recorder.endRecording()); } SkAutoTUnref<GrTexture> texture; SkPaint paint; GrLayerCache* layerCache = context->getLayerCache(); if (doReplace) { int key[1] = { 0 }; GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0, 2, SkIRect::MakeWH(kWidth, kHeight), SkIRect::MakeWH(kWidth, kHeight), SkMatrix::I(), key, 1, &paint); GrSurfaceDesc desc; desc.fConfig = kSkia8888_GrPixelConfig; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fWidth = kWidth; desc.fHeight = kHeight; desc.fSampleCnt = 0; texture.reset(context->textureProvider()->createTexture( desc, SkBudgeted::kNo, nullptr, 0)); layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight), false); } SkRecord rerecord; SkRecorder canvas(&rerecord, kWidth, kHeight); GrRecordReplaceDraw(pic, &canvas, layerCache, SkMatrix::I(), nullptr/*callback*/); int numLayers = count_instances_of_type<SkRecords::SaveLayer>(rerecord); if (doReplace) { REPORTER_ASSERT(r, 0 == numLayers); } else { REPORTER_ASSERT(r, 1 == numLayers); } }
// Make sure GrRecordReplaceDraw balances unbalanced saves DEF_TEST(RecordReplaceDraw_Unbalanced, r) { SkAutoTUnref<const SkPicture> pic; { SkPictureRecorder recorder; SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); // We won't balance this, but GrRecordReplaceDraw will for us. canvas->save(); canvas->scale(2, 2); pic.reset(recorder.endRecording()); } SkRecord rerecord; SkRecorder canvas(&rerecord, kWidth, kHeight); GrRecordReplaceDraw(pic, &canvas, nullptr, SkMatrix::I(), nullptr/*callback*/); // ensure rerecord is balanced (in this case by checking that the count is odd) REPORTER_ASSERT(r, (rerecord.count() & 1) == 1); }
void SkMultiPictureDraw::draw(bool flush) { AutoMPDReset mpdreset(this); #ifdef FORCE_SINGLE_THREAD_DRAWING_FOR_TESTING for (int i = 0; i < fThreadSafeDrawData.count(); ++i) { fThreadSafeDrawData[i].draw(); } #else sk_parallel_for(fThreadSafeDrawData.count(), [&](int i) { fThreadSafeDrawData[i].draw(); }); #endif // N.B. we could get going on any GPU work from this main thread while the CPU work runs. // But in practice, we've either got GPU work or CPU work, not both. const int count = fGPUDrawData.count(); if (0 == count) { return; } #if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU GrContext* context = fGPUDrawData[0].fCanvas->getGrContext(); SkASSERT(context); // Start by collecting all the layers that are going to be atlased and render // them (if necessary). Hoisting the free floating layers is deferred until // drawing the canvas that requires them. SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled; for (int i = 0; i < count; ++i) { const DrawData& data = fGPUDrawData[i]; // we only expect 1 context for all the canvases SkASSERT(data.fCanvas->getGrContext() == context); if (!data.fPaint) { SkRect clipBounds; if (!data.fCanvas->getClipBounds(&clipBounds)) { continue; } SkMatrix initialMatrix = data.fCanvas->getTotalMatrix(); initialMatrix.preConcat(data.fMatrix); GrRenderTarget* rt = data.fCanvas->internal_private_accessTopLayerRenderTarget(); SkASSERT(rt); // TODO: sorting the cacheable layers from smallest to largest // would improve the packing and reduce the number of swaps // TODO: another optimization would be to make a first pass to // lock any required layer that is already in the atlas GrLayerHoister::FindLayersToAtlas(context, data.fPicture, initialMatrix, clipBounds, &atlasedNeedRendering, &atlasedRecycled, rt->numColorSamples()); } } GrLayerHoister::DrawLayersToAtlas(context, atlasedNeedRendering); SkTDArray<GrHoistedLayer> needRendering, recycled; #endif for (int i = 0; i < count; ++i) { const DrawData& data = fGPUDrawData[i]; SkCanvas* canvas = data.fCanvas; const SkPicture* picture = data.fPicture; #if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU if (!data.fPaint) { SkRect clipBounds; if (!canvas->getClipBounds(&clipBounds)) { continue; } SkAutoCanvasMatrixPaint acmp(canvas, &data.fMatrix, data.fPaint, picture->cullRect()); const SkMatrix initialMatrix = canvas->getTotalMatrix(); GrRenderTarget* rt = data.fCanvas->internal_private_accessTopLayerRenderTarget(); SkASSERT(rt); // Find the layers required by this canvas. It will return atlased // layers in the 'recycled' list since they have already been drawn. GrLayerHoister::FindLayersToHoist(context, picture, initialMatrix, clipBounds, &needRendering, &recycled, rt->numColorSamples()); GrLayerHoister::DrawLayers(context, needRendering); // Render the entire picture using new layers GrRecordReplaceDraw(picture, canvas, context->getLayerCache(), initialMatrix, NULL); GrLayerHoister::UnlockLayers(context, needRendering); GrLayerHoister::UnlockLayers(context, recycled); needRendering.rewind(); recycled.rewind(); } else #endif { canvas->drawPicture(picture, &data.fMatrix, data.fPaint); } if (flush) { canvas->flush(); } } #if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU GrLayerHoister::UnlockLayers(context, atlasedNeedRendering); GrLayerHoister::UnlockLayers(context, atlasedRecycled); #if !GR_CACHE_HOISTED_LAYERS GrLayerHoister::PurgeCache(context); #endif #endif }