blob: 50f7abe830a2ad2140c61e28e88451451887209e [file] [log] [blame]
robertphillips@google.come930a072014-04-03 00:34:27 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrAtlas.h"
9#include "GrGpu.h"
10#include "GrLayerCache.h"
11
robertphillipsd771f6b2014-07-22 10:18:06 -070012DECLARE_SKMESSAGEBUS_MESSAGE(GrPictureDeletedMessage);
13
robertphillips21048b52014-07-15 19:46:35 -070014#ifdef SK_DEBUG
robertphillips261b8a92014-07-17 08:26:44 -070015void GrCachedLayer::validate(const GrTexture* backingTexture) const {
robertphillips0c423322014-07-31 11:02:38 -070016 SkASSERT(SK_InvalidGenID != fKey.pictureID());
robertphillips410dd052014-10-06 12:19:50 -070017 SkASSERT(fKey.start() >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -070018
robertphillips21048b52014-07-15 19:46:35 -070019
bsalomon49f085d2014-09-05 13:34:00 -070020 if (fTexture) {
robertphillips21048b52014-07-15 19:46:35 -070021 // 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);
26 }
27 } else {
28 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070029 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070030 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070031 }
32
bsalomon49f085d2014-09-05 13:34:00 -070033 if (fPlot) {
robertphillips261b8a92014-07-17 08:26:44 -070034 // 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.
bsalomon49f085d2014-09-05 13:34:00 -070036 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070037 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070038 }
robertphillips320c9232014-07-29 06:07:19 -070039
40 if (fLocked) {
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.
bsalomon49f085d2014-09-05 13:34:00 -070043 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070044 SkASSERT(!fRect.isEmpty());
45 }
robertphillips21048b52014-07-15 19:46:35 -070046}
47
48class GrAutoValidateLayer : ::SkNoncopyable {
49public:
50 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
51 : fBackingTexture(backingTexture)
52 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070053 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070054 fLayer->validate(backingTexture);
55 }
56 }
57 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070058 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070059 fLayer->validate(fBackingTexture);
60 }
61 }
robertphillips261b8a92014-07-17 08:26:44 -070062 void setBackingTexture(GrTexture* backingTexture) {
63 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
64 fBackingTexture = backingTexture;
65 }
robertphillips21048b52014-07-15 19:46:35 -070066
67private:
robertphillips261b8a92014-07-17 08:26:44 -070068 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070069 const GrCachedLayer* fLayer;
70};
71#endif
72
robertphillips4ec84da2014-06-24 13:10:43 -070073GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070074 : fContext(context) {
75 this->initAtlas();
robertphillips320c9232014-07-29 06:07:19 -070076 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000077}
78
79GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070080
robertphillips3d533ac2014-07-20 09:40:00 -070081 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
82 for (; !iter.done(); ++iter) {
83 GrCachedLayer* layer = &(*iter);
84 this->unlock(layer);
85 SkDELETE(layer);
86 }
robertphillips952841b2014-06-30 08:26:50 -070087
88 // The atlas only lets go of its texture when the atlas is deleted.
89 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000090}
91
robertphillips952841b2014-06-30 08:26:50 -070092void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -070093 SkASSERT(NULL == fAtlas.get());
robertphillips@google.come930a072014-04-03 00:34:27 +000094
robertphillips@google.come930a072014-04-03 00:34:27 +000095 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -070096 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
robertphillips952841b2014-06-30 08:26:50 -070097 kRenderTarget_GrTextureFlagBit,
robertphillips261b8a92014-07-17 08:26:44 -070098 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +000099}
100
101void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700102
robertphillips3d533ac2014-07-20 09:40:00 -0700103 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
104 for (; !iter.done(); ++iter) {
105 GrCachedLayer* layer = &(*iter);
106 this->unlock(layer);
107 SkDELETE(layer);
108 }
109 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700110
111 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700112 fAtlas.free();
robertphillips952841b2014-06-30 08:26:50 -0700113 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
114 // lazily allocates a replacement texture so reallocating a new
bsalomonc8dc1f72014-08-21 13:02:13 -0700115 // atlas here won't disrupt a GrContext::abandonContext or freeGpuResources.
robertphillips952841b2014-06-30 08:26:50 -0700116 // TODO: Make GrLayerCache lazily allocate the atlas manager?
117 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +0000118}
119
robertphillips6f294af2014-08-18 08:50:03 -0700120GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700121 int start, int stop,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700122 const SkMatrix& ctm,
123 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700124 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700125
robertphillipsed420592014-09-29 11:39:38 -0700126 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700127 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000128 return layer;
129}
130
robertphillips6f294af2014-08-18 08:50:03 -0700131GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
robertphillipsed420592014-09-29 11:39:38 -0700132 int start,
robertphillips0c423322014-07-31 11:02:38 -0700133 const SkMatrix& ctm) {
robertphillipsed420592014-09-29 11:39:38 -0700134 SkASSERT(pictureID != SK_InvalidGenID && start > 0);
135 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700136}
robertphillips@google.come930a072014-04-03 00:34:27 +0000137
robertphillips6f294af2014-08-18 08:50:03 -0700138GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700139 int start, int stop,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700140 const SkMatrix& ctm,
141 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700142 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillipsed420592014-09-29 11:39:38 -0700143 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000144 if (NULL == layer) {
robertphillipsed420592014-09-29 11:39:38 -0700145 layer = this->createLayer(pictureID, start, stop, ctm, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000146 }
robertphillips4ec84da2014-06-24 13:10:43 -0700147
robertphillips@google.come930a072014-04-03 00:34:27 +0000148 return layer;
149}
robertphillips4ec84da2014-06-24 13:10:43 -0700150
robertphillips6f294af2014-08-18 08:50:03 -0700151bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas) {
robertphillips21048b52014-07-15 19:46:35 -0700152 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700153
robertphillips320c9232014-07-29 06:07:19 -0700154 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700155 // This layer is already locked
robertphillips21048b52014-07-15 19:46:35 -0700156 if (layer->isAtlased()) {
robertphillips5c481662014-10-09 12:30:10 -0700157 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700158 SkASSERT(!dontAtlas);
robertphillips952841b2014-06-30 08:26:50 -0700159 SkASSERT(layer->rect().width() == desc.fWidth);
160 SkASSERT(layer->rect().height() == desc.fHeight);
161 }
robertphillips6f294af2014-08-18 08:50:03 -0700162 return false;
robertphillips952841b2014-06-30 08:26:50 -0700163 }
164
robertphillips320c9232014-07-29 06:07:19 -0700165 if (layer->isAtlased()) {
166 // Hooray it is still in the atlas - make sure it stays there
robertphillips6f294af2014-08-18 08:50:03 -0700167 SkASSERT(!dontAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700168 layer->setLocked(true);
robertphillips5c481662014-10-09 12:30:10 -0700169 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700170 return false;
171 } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
robertphillips320c9232014-07-29 06:07:19 -0700172 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700173 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700174 if (NULL == pictInfo) {
175 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700176 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700177 }
178
179 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700180 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
181 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
182 desc.fWidth, desc.fHeight,
183 NULL, &loc);
184 // addToAtlas can allocate the backing texture
185 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700186 if (plot) {
robertphillips320c9232014-07-29 06:07:19 -0700187 // The layer was successfully added to the atlas
188 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700189 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700190 SkToS16(desc.fHeight));
191 layer->setTexture(fAtlas->getTexture(), bounds);
192 layer->setPlot(plot);
193 layer->setLocked(true);
robertphillips5c481662014-10-09 12:30:10 -0700194 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700195 return true;
robertphillips320c9232014-07-29 06:07:19 -0700196 }
197
198 // The layer was rejected by the atlas (even though we know it is
199 // plausibly atlas-able). See if a plot can be purged and try again.
200 if (!this->purgePlot()) {
201 break; // We weren't able to purge any plots
202 }
robertphillips261b8a92014-07-17 08:26:44 -0700203 }
robertphillips952841b2014-06-30 08:26:50 -0700204 }
robertphillips952841b2014-06-30 08:26:50 -0700205
robertphillips21048b52014-07-15 19:46:35 -0700206 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700207 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700208 // This can yield a lot of re-rendering
robertphillipsed6f03e2014-07-30 07:31:35 -0700209 SkAutoTUnref<GrTexture> tex(fContext->lockAndRefScratchTexture(desc,
210 GrContext::kApprox_ScratchTexMatch));
211
212 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700213 layer->setLocked(true);
robertphillips6f294af2014-08-18 08:50:03 -0700214 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700215}
216
217void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700218 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
219
robertphillips5c481662014-10-09 12:30:10 -0700220 if (NULL == layer) {
robertphillips320c9232014-07-29 06:07:19 -0700221 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700222 return;
223 }
224
robertphillips21048b52014-07-15 19:46:35 -0700225 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700226 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700227
robertphillips5c481662014-10-09 12:30:10 -0700228 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700229 // At this point we could aggressively clear out un-locked plots but
230 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips6f294af2014-08-18 08:50:03 -0700231#if DISABLE_CACHING
robertphillips0c423322014-07-31 11:02:38 -0700232 // This testing code aggressively removes the atlased layers. This
233 // can be used to separate the performance contribution of less
234 // render target pingponging from that due to the re-use of cached layers
235 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700236 SkASSERT(pictInfo);
robertphillips0c423322014-07-31 11:02:38 -0700237
238 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
239
240 layer->setPlot(NULL);
241 layer->setTexture(NULL, GrIRect16::MakeEmpty());
242#endif
243
robertphillips21048b52014-07-15 19:46:35 -0700244 } else {
robertphillips952841b2014-06-30 08:26:50 -0700245 layer->setTexture(NULL, GrIRect16::MakeEmpty());
246 }
robertphillips320c9232014-07-29 06:07:19 -0700247
248 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700249}
250
robertphillips21048b52014-07-15 19:46:35 -0700251#ifdef SK_DEBUG
252void GrLayerCache::validate() const {
robertphillips3d533ac2014-07-20 09:40:00 -0700253 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
254 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700255 const GrCachedLayer* layer = &(*iter);
256
257 layer->validate(fAtlas->getTexture());
258
259 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700260 if (pictInfo) {
robertphillips320c9232014-07-29 06:07:19 -0700261 // In aggressive cleanup mode a picture info should only exist if
262 // it has some atlased layers
robertphillips6f294af2014-08-18 08:50:03 -0700263#if !DISABLE_CACHING
robertphillips320c9232014-07-29 06:07:19 -0700264 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
robertphillips6f294af2014-08-18 08:50:03 -0700265#endif
robertphillips320c9232014-07-29 06:07:19 -0700266 } else {
robertphillips5c481662014-10-09 12:30:10 -0700267 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700268 // layers should be non-atlased.
269 SkASSERT(!layer->isAtlased());
270 }
271
bsalomon49f085d2014-09-05 13:34:00 -0700272 if (layer->plot()) {
273 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700274 SkASSERT(pictInfo->fPictureID == layer->pictureID());
275
276 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
277
278 if (layer->locked()) {
robertphillips5c481662014-10-09 12:30:10 -0700279 SkASSERT(fPlotLocks[layer->plot()->id()] > 0);
robertphillips320c9232014-07-29 06:07:19 -0700280 }
281 }
282 }
robertphillips21048b52014-07-15 19:46:35 -0700283}
284
285class GrAutoValidateCache : ::SkNoncopyable {
286public:
287 explicit GrAutoValidateCache(GrLayerCache* cache)
288 : fCache(cache) {
289 fCache->validate();
290 }
291 ~GrAutoValidateCache() {
292 fCache->validate();
293 }
294private:
295 GrLayerCache* fCache;
296};
297#endif
298
robertphillipsd771f6b2014-07-22 10:18:06 -0700299void GrLayerCache::purge(uint32_t pictureID) {
300
robertphillips21048b52014-07-15 19:46:35 -0700301 SkDEBUGCODE(GrAutoValidateCache avc(this);)
302
robertphillips3d533ac2014-07-20 09:40:00 -0700303 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700304 SkTDArray<GrCachedLayer*> toBeRemoved;
305
robertphillips3d533ac2014-07-20 09:40:00 -0700306 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
307 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700308 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700309 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700310 }
311 }
312
313 for (int i = 0; i < toBeRemoved.count(); ++i) {
314 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700315 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700316 SkDELETE(toBeRemoved[i]);
317 }
robertphillips261b8a92014-07-17 08:26:44 -0700318
robertphillipsd771f6b2014-07-22 10:18:06 -0700319 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700320 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700321 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700322 SkDELETE(pictInfo);
323 }
robertphillips4ec84da2014-06-24 13:10:43 -0700324}
robertphillipsd771f6b2014-07-22 10:18:06 -0700325
robertphillips320c9232014-07-29 06:07:19 -0700326bool GrLayerCache::purgePlot() {
327 SkDEBUGCODE(GrAutoValidateCache avc(this);)
328
329 GrAtlas::PlotIter iter;
330 GrPlot* plot;
331 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700332 plot;
robertphillips320c9232014-07-29 06:07:19 -0700333 plot = iter.prev()) {
334 if (fPlotLocks[plot->id()] > 0) {
335 continue;
336 }
337
robertphillips6f294af2014-08-18 08:50:03 -0700338 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700339 return true;
340 }
341
342 return false;
343}
344
robertphillips6f294af2014-08-18 08:50:03 -0700345void GrLayerCache::purgePlot(GrPlot* plot) {
346 SkASSERT(0 == fPlotLocks[plot->id()]);
347
348 // We need to find all the layers in 'plot' and remove them.
349 SkTDArray<GrCachedLayer*> toBeRemoved;
350
351 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
352 for (; !iter.done(); ++iter) {
353 if (plot == (*iter).plot()) {
354 *toBeRemoved.append() = &(*iter);
355 }
356 }
357
358 for (int i = 0; i < toBeRemoved.count(); ++i) {
359 SkASSERT(!toBeRemoved[i]->locked());
360
robertphillips410dd052014-10-06 12:19:50 -0700361 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700362
robertphillips410dd052014-10-06 12:19:50 -0700363 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700364 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
365 SkDELETE(toBeRemoved[i]);
366
robertphillips410dd052014-10-06 12:19:50 -0700367 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
368 if (pictInfo) {
369 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
370
371 if (pictInfo->fPlotUsage.isEmpty()) {
372 fPictureHash.remove(pictInfo->fPictureID);
373 SkDELETE(pictInfo);
374 }
robertphillips6f294af2014-08-18 08:50:03 -0700375 }
376 }
377
378 plot->resetRects();
379}
380
381void GrLayerCache::purgeAll() {
382 GrAtlas::PlotIter iter;
383 GrPlot* plot;
384 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700385 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700386 plot = iter.prev()) {
387 SkASSERT(0 == fPlotLocks[plot->id()]);
388
389 this->purgePlot(plot);
390 }
391}
392
robertphillipsd771f6b2014-07-22 10:18:06 -0700393class GrPictureDeletionListener : public SkPicture::DeletionListener {
394 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
395 const GrPictureDeletedMessage message = { pictureID };
396 SkMessageBus<GrPictureDeletedMessage>::Post(message);
397 }
398};
399
400void GrLayerCache::trackPicture(const SkPicture* picture) {
401 if (NULL == fDeletionListener) {
402 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
403 }
404
405 picture->addDeletionListener(fDeletionListener);
406}
407
408void GrLayerCache::processDeletedPictures() {
409 SkTDArray<GrPictureDeletedMessage> deletedPictures;
410 fPictDeletionInbox.poll(&deletedPictures);
411
412 for (int i = 0; i < deletedPictures.count(); i++) {
413 this->purge(deletedPictures[i].pictureID);
414 }
415}
416