2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "GrLayerCache.h"
12 DECLARE_SKMESSAGEBUS_MESSAGE(GrPictureDeletedMessage);
15 void GrCachedLayer::validate(const GrTexture* backingTexture) const {
16 SkASSERT(SK_InvalidGenID != fKey.pictureID());
17 SkASSERT(fKey.start() > 0 && fKey.stop() > 0);
21 // If the layer is in some texture then it must occupy some rectangle
22 SkASSERT(!fRect.isEmpty());
23 if (!this->isAtlased()) {
24 // If it isn't atlased then the rectangle should start at the origin
25 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
28 SkASSERT(fRect.isEmpty());
29 SkASSERT(NULL == fPlot);
30 SkASSERT(!fLocked); // layers without a texture cannot be locked
34 // If a layer has a plot (i.e., is atlased) then it must point to
35 // the backing texture. Additionally, its rect should be non-empty.
36 SkASSERT(fTexture && backingTexture == fTexture);
37 SkASSERT(!fRect.isEmpty());
41 // If a layer is locked it must have a texture (though it need not be
42 // the atlas-backing texture) and occupy some space.
44 SkASSERT(!fRect.isEmpty());
48 class GrAutoValidateLayer : ::SkNoncopyable {
50 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
51 : fBackingTexture(backingTexture)
54 fLayer->validate(backingTexture);
57 ~GrAutoValidateLayer() {
59 fLayer->validate(fBackingTexture);
62 void setBackingTexture(GrTexture* backingTexture) {
63 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
64 fBackingTexture = backingTexture;
68 const GrTexture* fBackingTexture;
69 const GrCachedLayer* fLayer;
73 GrLayerCache::GrLayerCache(GrContext* context)
76 memset(fPlotLocks, 0, sizeof(fPlotLocks));
79 GrLayerCache::~GrLayerCache() {
81 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
82 for (; !iter.done(); ++iter) {
83 GrCachedLayer* layer = &(*iter);
88 // The atlas only lets go of its texture when the atlas is deleted.
92 void GrLayerCache::initAtlas() {
93 SkASSERT(NULL == fAtlas.get());
95 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
96 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
97 kRenderTarget_GrTextureFlagBit,
98 textureSize, kNumPlotsX, kNumPlotsY, false)));
101 void GrLayerCache::freeAll() {
103 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
104 for (; !iter.done(); ++iter) {
105 GrCachedLayer* layer = &(*iter);
111 // The atlas only lets go of its texture when the atlas is deleted.
113 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
114 // lazily allocates a replacement texture so reallocating a new
115 // atlas here won't disrupt a GrContext::abandonContext or freeGpuResources.
116 // TODO: Make GrLayerCache lazily allocate the atlas manager?
120 GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
122 const SkIPoint& offset,
124 const SkPaint* paint) {
125 SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0);
127 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, offset, ctm, paint));
128 fLayerHash.add(layer);
132 GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
134 const SkIPoint& offset,
135 const SkMatrix& ctm) {
136 SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0);
137 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, stop, offset, ctm));
140 GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
142 const SkIPoint& offset,
144 const SkPaint* paint) {
145 SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0);
146 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, stop, offset, ctm));
148 layer = this->createLayer(pictureID, start, stop, offset, ctm, paint);
154 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas) {
155 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
157 if (layer->locked()) {
158 // This layer is already locked
160 if (layer->isAtlased()) {
161 // It claims to be atlased
162 SkASSERT(!dontAtlas);
163 SkASSERT(layer->rect().width() == desc.fWidth);
164 SkASSERT(layer->rect().height() == desc.fHeight);
170 if (layer->isAtlased()) {
171 // Hooray it is still in the atlas - make sure it stays there
172 SkASSERT(!dontAtlas);
173 layer->setLocked(true);
174 fPlotLocks[layer->plot()->id()]++;
176 } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
177 // Not in the atlas - will it fit?
178 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
179 if (NULL == pictInfo) {
180 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
181 fPictureHash.add(pictInfo);
185 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
186 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
187 desc.fWidth, desc.fHeight,
189 // addToAtlas can allocate the backing texture
190 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
192 // The layer was successfully added to the atlas
193 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
194 SkToS16(desc.fWidth),
195 SkToS16(desc.fHeight));
196 layer->setTexture(fAtlas->getTexture(), bounds);
197 layer->setPlot(plot);
198 layer->setLocked(true);
199 fPlotLocks[layer->plot()->id()]++;
203 // The layer was rejected by the atlas (even though we know it is
204 // plausibly atlas-able). See if a plot can be purged and try again.
205 if (!this->purgePlot()) {
206 break; // We weren't able to purge any plots
211 // The texture wouldn't fit in the cache - give it it's own texture.
212 // This path always uses a new scratch texture and (thus) doesn't cache anything.
213 // This can yield a lot of re-rendering
214 SkAutoTUnref<GrTexture> tex(fContext->lockAndRefScratchTexture(desc,
215 GrContext::kApprox_ScratchTexMatch));
217 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
218 layer->setLocked(true);
222 void GrLayerCache::unlock(GrCachedLayer* layer) {
223 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
225 if (NULL == layer || !layer->locked()) {
226 // invalid or not locked
230 if (layer->isAtlased()) {
231 const int plotID = layer->plot()->id();
233 SkASSERT(fPlotLocks[plotID] > 0);
234 fPlotLocks[plotID]--;
235 // At this point we could aggressively clear out un-locked plots but
236 // by delaying we may be able to reuse some of the atlased layers later.
238 // This testing code aggressively removes the atlased layers. This
239 // can be used to separate the performance contribution of less
240 // render target pingponging from that due to the re-use of cached layers
241 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
244 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
246 layer->setPlot(NULL);
247 layer->setTexture(NULL, GrIRect16::MakeEmpty());
251 fContext->unlockScratchTexture(layer->texture());
252 layer->setTexture(NULL, GrIRect16::MakeEmpty());
255 layer->setLocked(false);
259 void GrLayerCache::validate() const {
260 int plotLocks[kNumPlotsX * kNumPlotsY];
261 memset(plotLocks, 0, sizeof(plotLocks));
263 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
264 for (; !iter.done(); ++iter) {
265 const GrCachedLayer* layer = &(*iter);
267 layer->validate(fAtlas->getTexture());
269 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
271 // In aggressive cleanup mode a picture info should only exist if
272 // it has some atlased layers
274 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
277 // If there is no picture info for this layer then all of its
278 // layers should be non-atlased.
279 SkASSERT(!layer->isAtlased());
284 SkASSERT(pictInfo->fPictureID == layer->pictureID());
286 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
288 if (layer->locked()) {
289 plotLocks[layer->plot()->id()]++;
294 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
295 SkASSERT(plotLocks[i] == fPlotLocks[i]);
299 class GrAutoValidateCache : ::SkNoncopyable {
301 explicit GrAutoValidateCache(GrLayerCache* cache)
305 ~GrAutoValidateCache() {
309 GrLayerCache* fCache;
313 void GrLayerCache::purge(uint32_t pictureID) {
315 SkDEBUGCODE(GrAutoValidateCache avc(this);)
317 // We need to find all the layers associated with 'picture' and remove them.
318 SkTDArray<GrCachedLayer*> toBeRemoved;
320 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
321 for (; !iter.done(); ++iter) {
322 if (pictureID == (*iter).pictureID()) {
323 *toBeRemoved.append() = &(*iter);
327 for (int i = 0; i < toBeRemoved.count(); ++i) {
328 this->unlock(toBeRemoved[i]);
329 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
330 SkDELETE(toBeRemoved[i]);
333 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
335 fPictureHash.remove(pictureID);
340 bool GrLayerCache::purgePlot() {
341 SkDEBUGCODE(GrAutoValidateCache avc(this);)
343 GrAtlas::PlotIter iter;
345 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
347 plot = iter.prev()) {
348 if (fPlotLocks[plot->id()] > 0) {
352 this->purgePlot(plot);
359 void GrLayerCache::purgePlot(GrPlot* plot) {
360 SkASSERT(0 == fPlotLocks[plot->id()]);
362 // We need to find all the layers in 'plot' and remove them.
363 SkTDArray<GrCachedLayer*> toBeRemoved;
365 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
366 for (; !iter.done(); ++iter) {
367 if (plot == (*iter).plot()) {
368 *toBeRemoved.append() = &(*iter);
372 for (int i = 0; i < toBeRemoved.count(); ++i) {
373 SkASSERT(!toBeRemoved[i]->locked());
375 GrPictureInfo* pictInfo = fPictureHash.find(toBeRemoved[i]->pictureID());
378 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
380 // Aggressively remove layers and, if now totally uncached, picture info
381 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
382 SkDELETE(toBeRemoved[i]);
384 if (pictInfo->fPlotUsage.isEmpty()) {
385 fPictureHash.remove(pictInfo->fPictureID);
393 void GrLayerCache::purgeAll() {
394 GrAtlas::PlotIter iter;
396 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
398 plot = iter.prev()) {
399 SkASSERT(0 == fPlotLocks[plot->id()]);
401 this->purgePlot(plot);
405 class GrPictureDeletionListener : public SkPicture::DeletionListener {
406 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
407 const GrPictureDeletedMessage message = { pictureID };
408 SkMessageBus<GrPictureDeletedMessage>::Post(message);
412 void GrLayerCache::trackPicture(const SkPicture* picture) {
413 if (NULL == fDeletionListener) {
414 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
417 picture->addDeletionListener(fDeletionListener);
420 void GrLayerCache::processDeletedPictures() {
421 SkTDArray<GrPictureDeletedMessage> deletedPictures;
422 fPictDeletionInbox.poll(&deletedPictures);
424 for (int i = 0; i < deletedPictures.count(); i++) {
425 this->purge(deletedPictures[i].pictureID);