static void convert_layers_to_replacements(const SkTDArray<GrHoistedLayer>& layers, GrReplacements* replacements) { // TODO: just replace GrReplacements::ReplacementInfo with GrCachedLayer? for (int i = 0; i < layers.count(); ++i) { GrCachedLayer* layer = layers[i].fLayer; const SkPicture* picture = layers[i].fPicture; GrReplacements::ReplacementInfo* layerInfo = replacements->newReplacement(picture->uniqueID(), layer->start(), layers[i].fCTM); layerInfo->fStop = layer->stop(); layerInfo->fPos = layers[i].fOffset; SkBitmap bm; wrap_texture(layers[i].fLayer->texture(), !layers[i].fLayer->isAtlased() ? layers[i].fLayer->rect().width() : layers[i].fLayer->texture()->width(), !layers[i].fLayer->isAtlased() ? layers[i].fLayer->rect().height() : layers[i].fLayer->texture()->height(), &bm); layerInfo->fImage = SkImage::NewTexture(bm); layerInfo->fPaint = layers[i].fLayer->paint() ? SkNEW_ARGS(SkPaint, (*layers[i].fLayer->paint())) : NULL; layerInfo->fSrcRect = SkIRect::MakeXYWH(layers[i].fLayer->rect().fLeft, layers[i].fLayer->rect().fTop, layers[i].fLayer->rect().width(), layers[i].fLayer->rect().height()); } }
static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) { if (layer.pictureID() == key.pictureID()) { return layer.layerID() < key.layerID(); } return layer.pictureID() < key.pictureID(); }
GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) { GrCachedLayer* layer = fLayerPool.alloc(); SkASSERT(picture->uniqueID() != SK_InvalidGenID); layer->init(picture->uniqueID(), layerID); fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); return layer; }
// Create the layer information for the hoisted layer and secure the // required texture/render target resources. static void prepare_for_hoisting(GrLayerCache* layerCache, const SkPicture* topLevelPicture, const GrAccelData::SaveLayerInfo& info, const SkIRect& layerRect, SkTDArray<GrHoistedLayer>* needRendering, SkTDArray<GrHoistedLayer>* recycled, bool attemptToAtlas) { const SkPicture* pict = info.fPicture ? info.fPicture : topLevelPicture; SkMatrix combined = SkMatrix::Concat(info.fPreMat, info.fLocalMat); GrCachedLayer* layer = layerCache->findLayerOrCreate(pict->uniqueID(), info.fSaveLayerOpID, info.fRestoreOpID, layerRect, combined, info.fPaint); GrSurfaceDesc desc; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fWidth = layerRect.width(); desc.fHeight = layerRect.height(); desc.fConfig = kSkia8888_GrPixelConfig; // TODO: need to deal with sample count bool locked, needsRendering; if (attemptToAtlas) { locked = layerCache->tryToAtlas(layer, desc, &needsRendering); } else { locked = layerCache->lock(layer, desc, &needsRendering); } if (!locked) { // GPU resources could not be secured for the hoisting of this layer return; } if (attemptToAtlas) { SkASSERT(layer->isAtlased()); } GrHoistedLayer* hl; if (needsRendering) { if (!attemptToAtlas) { SkASSERT(!layer->isAtlased()); } hl = needRendering->append(); } else { hl = recycled->append(); } layerCache->addUse(layer); hl->fLayer = layer; hl->fPicture = pict; hl->fOffset = SkIPoint::Make(layerRect.fLeft, layerRect.fTop); hl->fLocalMat = info.fLocalMat; hl->fPreMat = info.fPreMat; }
// 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); } }
// Create the layer information for the hoisted layer and secure the // required texture/render target resources. static void prepare_for_hoisting(GrLayerCache* layerCache, const SkPicture* topLevelPicture, const GrAccelData::SaveLayerInfo& info, const SkIRect& layerRect, SkTDArray<GrHoistedLayer>* atlased, SkTDArray<GrHoistedLayer>* nonAtlased, SkTDArray<GrHoistedLayer>* recycled) { const SkPicture* pict = info.fPicture ? info.fPicture : topLevelPicture; GrCachedLayer* layer = layerCache->findLayerOrCreate(pict->uniqueID(), info.fSaveLayerOpID, info.fRestoreOpID, layerRect, info.fOriginXform, info.fPaint); GrTextureDesc desc; desc.fFlags = kRenderTarget_GrTextureFlagBit; desc.fWidth = layerRect.width(); desc.fHeight = layerRect.height(); desc.fConfig = kSkia8888_GrPixelConfig; // TODO: need to deal with sample count bool disallowAtlasing = info.fHasNestedLayers || info.fIsNested || (layer->paint() && layer->paint()->getImageFilter()); bool needsRendering = layerCache->lock(layer, desc, disallowAtlasing); if (NULL == layer->texture()) { // GPU resources could not be secured for the hoisting of this layer return; } GrHoistedLayer* hl; if (needsRendering) { if (layer->isAtlased()) { hl = atlased->append(); } else { hl = nonAtlased->append(); } } else { hl = recycled->append(); } layerCache->addUse(layer); hl->fLayer = layer; hl->fPicture = pict; hl->fOffset = SkIPoint::Make(layerRect.fLeft, layerRect.fTop); hl->fCTM = info.fOriginXform; }
// 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); } }
GrLayerCache::~GrLayerCache() { SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); for (; !iter.done(); ++iter) { GrCachedLayer* layer = &(*iter); SkASSERT(0 == layer->uses()); this->unlock(layer); SkDELETE(layer); } SkASSERT(0 == fPictureHash.count()); // The atlas only lets go of its texture when the atlas is deleted. fAtlas.free(); }
// Add several layers to the cache static void create_layers(skiatest::Reporter* reporter, GrLayerCache* cache, const SkPicture& picture, int numToAdd, int idOffset) { for (int i = 0; i < numToAdd; ++i) { GrCachedLayer* layer = cache->findLayerOrCreate(&picture, idOffset+i+1, idOffset+i+2, SkMatrix::I()); REPORTER_ASSERT(reporter, NULL != layer); GrCachedLayer* temp = cache->findLayer(&picture, idOffset+i+1, idOffset+i+2, SkMatrix::I()); REPORTER_ASSERT(reporter, temp == layer); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1); REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID()); REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1); REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2); REPORTER_ASSERT(reporter, layer->ctm() == SkMatrix::I()); REPORTER_ASSERT(reporter, NULL == layer->texture()); REPORTER_ASSERT(reporter, !layer->isAtlased()); } cache->trackPicture(&picture); }
// Add several layers to the cache static void create_layers(skiatest::Reporter* reporter, GrLayerCache* cache, const SkPicture& picture, unsigned numToAdd, unsigned idOffset) { for (unsigned i = 0; i < numToAdd; ++i) { unsigned indices[1] = { idOffset+i+1 }; GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(), idOffset+i+1, idOffset+i+2, SkIRect::MakeEmpty(), SkIRect::MakeEmpty(), SkMatrix::I(), indices, 1, NULL); REPORTER_ASSERT(reporter, layer); GrCachedLayer* temp = TestingAccess::Find(cache, picture.uniqueID(), SkMatrix::I(), indices, 1); REPORTER_ASSERT(reporter, temp == layer); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1); REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID()); REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1); REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2); REPORTER_ASSERT(reporter, NULL == layer->texture()); REPORTER_ASSERT(reporter, NULL == layer->paint()); REPORTER_ASSERT(reporter, !layer->isAtlased()); } }
void operator()(const SkRecords::SaveLayer& sl) { // For a saveLayer command, check if it can be replaced by a drawBitmap // call and, if so, draw it and then update the current op index accordingly. int startOffset; if (fOps.count()) { startOffset = fOps[fIndex]; } else { startOffset = fIndex; } fOpIndexStack.push(startOffset); GrCachedLayer* layer = fLayerCache->findLayer(fTopLevelPicture->uniqueID(), fInitialMatrix, fOpIndexStack.begin(), fOpIndexStack.count()); if (layer) { fNumReplaced++; draw_replacement_bitmap(layer, fCanvas); if (fPicture->bbh()) { while (fOps[fIndex] < layer->stop()) { ++fIndex; } SkASSERT(fOps[fIndex] == layer->stop()); } else { fIndex = layer->stop(); } fOpIndexStack.pop(); return; } // This is a fail for layer hoisting this->INHERITED::operator()(sl); fOpIndexStack.pop(); }
// Create the layer information for the hoisted layer and secure the // required texture/render target resources. static void prepare_for_hoisting(GrLayerCache* layerCache, const SkPicture* topLevelPicture, const SkMatrix& initialMat, const SkLayerInfo::BlockInfo& info, const SkIRect& srcIR, const SkIRect& dstIR, SkTDArray<GrHoistedLayer>* needRendering, SkTDArray<GrHoistedLayer>* recycled, bool attemptToAtlas, int numSamples) { const SkPicture* pict = info.fPicture ? info.fPicture : topLevelPicture; GrCachedLayer* layer = layerCache->findLayerOrCreate(topLevelPicture->uniqueID(), SkToInt(info.fSaveLayerOpID), SkToInt(info.fRestoreOpID), srcIR, dstIR, initialMat, info.fKey, info.fKeySize, info.fPaint); GrSurfaceDesc desc; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fWidth = srcIR.width(); desc.fHeight = srcIR.height(); desc.fConfig = kSkia8888_GrPixelConfig; desc.fSampleCnt = numSamples; bool locked, needsRendering; if (attemptToAtlas) { locked = layerCache->tryToAtlas(layer, desc, &needsRendering); } else { locked = layerCache->lock(layer, desc, &needsRendering); } if (!locked) { // GPU resources could not be secured for the hoisting of this layer return; } if (attemptToAtlas) { SkASSERT(layer->isAtlased()); } GrHoistedLayer* hl; if (needsRendering) { if (!attemptToAtlas) { SkASSERT(!layer->isAtlased()); } hl = needRendering->append(); } else { hl = recycled->append(); } layerCache->addUse(layer); hl->fLayer = layer; hl->fPicture = pict; hl->fLocalMat = info.fLocalMat; hl->fInitialMat = initialMat; hl->fPreMat = initialMat; hl->fPreMat.preConcat(info.fPreMat); }
// This test case exercises the public API of the GrLayerCache class. // In particular it checks its interaction with the resource cache (w.r.t. // locking & unlocking textures). // TODO: need to add checks on VRAM usage! DEF_GPUTEST(GpuLayerCache, reporter, factory) { static const int kInitialNumLayers = 5; for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { continue; } GrContext* context = factory->get(glCtxType); if (NULL == context) { continue; } SkPictureRecorder recorder; recorder.beginRecording(1, 1); SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); GrLayerCache cache(context); create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); for (int i = 0; i < kInitialNumLayers; ++i) { GrCachedLayer* layer = cache.findLayer(picture, i+1, i+2, SkMatrix::I()); REPORTER_ASSERT(reporter, NULL != layer); lock_layer(reporter, &cache, layer); // The first 4 layers should be in the atlas (and thus have non-empty // rects) if (i < 4) { REPORTER_ASSERT(reporter, layer->isAtlased()); } else { // The 5th layer couldn't fit in the atlas REPORTER_ASSERT(reporter, !layer->isAtlased()); } } // Unlock the textures for (int i = 0; i < kInitialNumLayers; ++i) { GrCachedLayer* layer = cache.findLayer(picture, i+1, i+2, SkMatrix::I()); REPORTER_ASSERT(reporter, NULL != layer); cache.unlock(layer); } for (int i = 0; i < kInitialNumLayers; ++i) { GrCachedLayer* layer = cache.findLayer(picture, i+1, i+2, SkMatrix::I()); REPORTER_ASSERT(reporter, NULL != layer); REPORTER_ASSERT(reporter, !layer->locked()); // The first 4 layers should still be in the atlas. if (i < 4) { REPORTER_ASSERT(reporter, NULL != layer->texture()); REPORTER_ASSERT(reporter, layer->isAtlased()); } else { // The final layer should be unlocked. REPORTER_ASSERT(reporter, NULL == layer->texture()); REPORTER_ASSERT(reporter, !layer->isAtlased()); } } { // Add an additional layer. Since all the layers are unlocked this // will force out the first atlased layer create_layers(reporter, &cache, *picture, 1, kInitialNumLayers); GrCachedLayer* layer = cache.findLayer(picture, kInitialNumLayers+1, kInitialNumLayers+2, SkMatrix::I()); REPORTER_ASSERT(reporter, NULL != layer); lock_layer(reporter, &cache, layer); cache.unlock(layer); } for (int i = 0; i < kInitialNumLayers+1; ++i) { GrCachedLayer* layer = cache.findLayer(picture, i+1, i+2, SkMatrix::I()); // 3 old layers plus the new one should be in the atlas. if (1 == i || 2 == i || 3 == i || 5 == i) { REPORTER_ASSERT(reporter, NULL != layer); REPORTER_ASSERT(reporter, !layer->locked()); REPORTER_ASSERT(reporter, NULL != layer->texture()); REPORTER_ASSERT(reporter, layer->isAtlased()); } else if (4 == i) { // The one that was never atlased should still be around REPORTER_ASSERT(reporter, NULL != layer); REPORTER_ASSERT(reporter, NULL == layer->texture()); REPORTER_ASSERT(reporter, !layer->isAtlased()); } else { // The one bumped out of the atlas (i.e., 0) should be gone REPORTER_ASSERT(reporter, NULL == layer); } } //-------------------------------------------------------------------- // Free them all SkGpuDevice-style. This will not free up the // atlas' texture but will eliminate all the layers. TestingAccess::Purge(&cache, picture->uniqueID()); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here //-------------------------------------------------------------------- // Test out the GrContext-style purge. This should remove all the layers // and the atlas. // Re-create the layers create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); // Free them again GrContext-style. This should free up everything. cache.freeAll(); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here //-------------------------------------------------------------------- // Test out the MessageBus-style purge. This will not free the atlas // but should eliminate the free-floating layers. create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); picture.reset(NULL); cache.processDeletedPictures(); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here } }
// This test case exercises the public API of the GrLayerCache class. // In particular it checks its interaction with the resource cache (w.r.t. // locking & unlocking textures). // TODO: need to add checks on VRAM usage! DEF_GPUTEST(GpuLayerCache, reporter, factory) { GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); if (NULL == context) { return; } SkPicture picture; GrLayerCache cache(context); create_layers(reporter, &cache, picture); // Lock the layers making them all 512x512 GrTextureDesc desc; desc.fWidth = 512; desc.fHeight = 512; desc.fConfig = kSkia8888_GrPixelConfig; for (int i = 0; i < kNumLayers; ++i) { GrCachedLayer* layer = cache.findLayer(&picture, i); REPORTER_ASSERT(reporter, NULL != layer); bool foundInCache = cache.lock(layer, desc); REPORTER_ASSERT(reporter, !foundInCache); foundInCache = cache.lock(layer, desc); REPORTER_ASSERT(reporter, foundInCache); REPORTER_ASSERT(reporter, NULL != layer->texture()); #if USE_ATLAS // The first 4 layers should be in the atlas (and thus have non-empty // rects) if (i < 4) { REPORTER_ASSERT(reporter, !layer->rect().isEmpty()); } else { #endif REPORTER_ASSERT(reporter, layer->rect().isEmpty()); #if USE_ATLAS } #endif } // Unlock the textures for (int i = 0; i < kNumLayers; ++i) { GrCachedLayer* layer = cache.findLayer(&picture, i); REPORTER_ASSERT(reporter, NULL != layer); cache.unlock(layer); } for (int i = 0; i < kNumLayers; ++i) { GrCachedLayer* layer = cache.findLayer(&picture, i); REPORTER_ASSERT(reporter, NULL != layer); #if USE_ATLAS // The first 4 layers should be in the atlas (and thus do not // currently unlock). The final layer should be unlocked. if (i < 4) { REPORTER_ASSERT(reporter, NULL != layer->texture()); REPORTER_ASSERT(reporter, !layer->rect().isEmpty()); } else { #endif REPORTER_ASSERT(reporter, NULL == layer->texture()); REPORTER_ASSERT(reporter, layer->rect().isEmpty()); #if USE_ATLAS } #endif } // Free them all SkGpuDevice-style. This will not free up the // atlas' texture but will eliminate all the layers. cache.purge(&picture); REPORTER_ASSERT(reporter, GetNumLayers::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here #if 0 // Re-create the layers create_layers(reporter, &cache, picture); // Free them again GrContext-style. This should free up everything. cache.freeAll(); REPORTER_ASSERT(reporter, GetNumLayers::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here #endif }
void GrLayerHoister::DrawLayers(const SkTDArray<GrHoistedLayer>& atlased, const SkTDArray<GrHoistedLayer>& nonAtlased, const SkTDArray<GrHoistedLayer>& recycled, GrReplacements* replacements) { // Render the atlased layers that require it if (atlased.count() > 0) { // All the atlased layers are rendered into the same GrTexture SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( atlased[0].fLayer->texture()->asRenderTarget(), NULL)); SkCanvas* atlasCanvas = surface->getCanvas(); SkPaint paint; paint.setColor(SK_ColorTRANSPARENT); paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); for (int i = 0; i < atlased.count(); ++i) { GrCachedLayer* layer = atlased[i].fLayer; const SkPicture* pict = atlased[i].fPicture; const SkIPoint offset = atlased[i].fOffset; atlasCanvas->save(); // Add a rect clip to make sure the rendering doesn't // extend beyond the boundaries of the atlased sub-rect SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), SkIntToScalar(layer->rect().fTop), SkIntToScalar(layer->rect().width()), SkIntToScalar(layer->rect().height())); atlasCanvas->clipRect(bound); // Since 'clear' doesn't respect the clip we need to draw a rect // TODO: ensure none of the atlased layers contain a clear call! atlasCanvas->drawRect(bound, paint); // info.fCTM maps the layer's top/left to the origin. // Since this layer is atlased, the top/left corner needs // to be offset to the correct location in the backing texture. SkMatrix initialCTM; initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); initialCTM.postTranslate(bound.fLeft, bound.fTop); atlasCanvas->translate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); atlasCanvas->translate(bound.fLeft, bound.fTop); atlasCanvas->concat(atlased[i].fCTM); SkRecordPartialDraw(*pict->fRecord.get(), atlasCanvas, bound, layer->start()+1, layer->stop(), initialCTM); atlasCanvas->restore(); } atlasCanvas->flush(); } // Render the non-atlased layers that require it for (int i = 0; i < nonAtlased.count(); ++i) { GrCachedLayer* layer = nonAtlased[i].fLayer; const SkPicture* pict = nonAtlased[i].fPicture; const SkIPoint offset = nonAtlased[i].fOffset; // Each non-atlased layer has its own GrTexture SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( layer->texture()->asRenderTarget(), NULL)); SkCanvas* layerCanvas = surface->getCanvas(); // Add a rect clip to make sure the rendering doesn't // extend beyond the boundaries of the atlased sub-rect SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), SkIntToScalar(layer->rect().fTop), SkIntToScalar(layer->rect().width()), SkIntToScalar(layer->rect().height())); layerCanvas->clipRect(bound); // TODO: still useful? layerCanvas->clear(SK_ColorTRANSPARENT); SkMatrix initialCTM; initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); layerCanvas->translate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); layerCanvas->concat(nonAtlased[i].fCTM); SkRecordPartialDraw(*pict->fRecord.get(), layerCanvas, bound, layer->start()+1, layer->stop(), initialCTM); layerCanvas->flush(); } convert_layers_to_replacements(atlased, replacements); convert_layers_to_replacements(nonAtlased, replacements); convert_layers_to_replacements(recycled, replacements); }
static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID(); }
// This test case exercises the public API of the GrLayerCache class. // In particular it checks its interaction with the resource cache (w.r.t. // locking & unlocking textures). // TODO: need to add checks on VRAM usage! DEF_GPUTEST(GpuLayerCache, reporter, factory) { static const unsigned kInitialNumLayers = 5; for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { continue; } GrContext* context = factory->get(glCtxType); if (NULL == context) { continue; } SkPictureRecorder recorder; SkCanvas* c = recorder.beginRecording(1, 1); // Draw something, anything, to prevent an empty-picture optimization, // which is a singleton and never purged. c->drawRect(SkRect::MakeWH(1,1), SkPaint()); SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); GrLayerCache cache(context); create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); for (unsigned i = 0; i < kInitialNumLayers; ++i) { unsigned indices[1] = { i + 1 }; GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(), indices, 1); REPORTER_ASSERT(reporter, layer); lock_layer(reporter, &cache, layer); // The first 4 layers should be in the atlas (and thus have non-empty // rects) if (i < 4) { REPORTER_ASSERT(reporter, layer->isAtlased()); } else { // The 5th layer couldn't fit in the atlas REPORTER_ASSERT(reporter, !layer->isAtlased()); } } // Unlock the textures for (unsigned i = 0; i < kInitialNumLayers; ++i) { unsigned indices[1] = { i+1 }; GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(), indices, 1); REPORTER_ASSERT(reporter, layer); cache.removeUse(layer); } for (unsigned i = 0; i < kInitialNumLayers; ++i) { unsigned indices[1] = { i+1 }; GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(), indices, 1); REPORTER_ASSERT(reporter, layer); // All the layers should be unlocked REPORTER_ASSERT(reporter, !layer->locked()); // When hoisted layers aren't cached they are aggressively removed // from the atlas #if GR_CACHE_HOISTED_LAYERS // The first 4 layers should still be in the atlas. if (i < 4) { REPORTER_ASSERT(reporter, layer->texture()); REPORTER_ASSERT(reporter, layer->isAtlased()); } else { #endif // The final layer should not be atlased. REPORTER_ASSERT(reporter, NULL == layer->texture()); REPORTER_ASSERT(reporter, !layer->isAtlased()); #if GR_CACHE_HOISTED_LAYERS } #endif } { unsigned indices[1] = { kInitialNumLayers+1 }; // Add an additional layer. Since all the layers are unlocked this // will force out the first atlased layer create_layers(reporter, &cache, *picture, 1, kInitialNumLayers); GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(), indices, 1); REPORTER_ASSERT(reporter, layer); lock_layer(reporter, &cache, layer); cache.removeUse(layer); } for (unsigned i = 0; i < kInitialNumLayers+1; ++i) { unsigned indices[1] = { i+1 }; GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(), indices, 1); #if GR_CACHE_HOISTED_LAYERS // 3 old layers plus the new one should be in the atlas. if (1 == i || 2 == i || 3 == i || 5 == i) { REPORTER_ASSERT(reporter, layer); REPORTER_ASSERT(reporter, !layer->locked()); REPORTER_ASSERT(reporter, layer->texture()); REPORTER_ASSERT(reporter, layer->isAtlased()); } else if (4 == i) { #endif // The one that was never atlased should still be around REPORTER_ASSERT(reporter, layer); REPORTER_ASSERT(reporter, NULL == layer->texture()); REPORTER_ASSERT(reporter, !layer->isAtlased()); #if GR_CACHE_HOISTED_LAYERS } else { // The one bumped out of the atlas (i.e., 0) should be gone REPORTER_ASSERT(reporter, NULL == layer); } #endif } //-------------------------------------------------------------------- // Free them all SkGpuDevice-style. This will not free up the // atlas' texture but will eliminate all the layers. TestingAccess::Purge(&cache, picture->uniqueID()); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here //-------------------------------------------------------------------- // Test out the GrContext-style purge. This should remove all the layers // and the atlas. // Re-create the layers create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); // Free them again GrContext-style. This should free up everything. cache.freeAll(); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here //-------------------------------------------------------------------- // Test out the MessageBus-style purge. This will not free the atlas // but should eliminate the free-floating layers. create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); picture.reset(NULL); cache.processDeletedPictures(); REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); // TODO: add VRAM/resource cache check here } }