blob: 570b2d6eafef15a8558532aa2f785f869786d463 [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
bsalomon49f085d2014-09-05 13:34:00 -070019 if (fTexture) {
robertphillips21048b52014-07-15 19:46:35 -070020 // If the layer is in some texture then it must occupy some rectangle
21 SkASSERT(!fRect.isEmpty());
22 if (!this->isAtlased()) {
23 // If it isn't atlased then the rectangle should start at the origin
24 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
25 }
26 } else {
27 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070028 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070029 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070030 }
31
bsalomon49f085d2014-09-05 13:34:00 -070032 if (fPlot) {
robertphillips261b8a92014-07-17 08:26:44 -070033 // If a layer has a plot (i.e., is atlased) then it must point to
34 // the backing texture. Additionally, its rect should be non-empty.
bsalomon49f085d2014-09-05 13:34:00 -070035 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070036 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070037 }
robertphillips320c9232014-07-29 06:07:19 -070038
39 if (fLocked) {
40 // If a layer is locked it must have a texture (though it need not be
41 // the atlas-backing texture) and occupy some space.
bsalomon49f085d2014-09-05 13:34:00 -070042 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070043 SkASSERT(!fRect.isEmpty());
44 }
robertphillips7bb9ed72014-10-10 11:38:29 -070045
46 // Unfortunately there is a brief time where a layer can be locked
47 // but not used, so we can only check the "used implies locked"
48 // invariant.
49 if (fUses > 0) {
50 SkASSERT(fLocked);
51 } else {
52 SkASSERT(0 == fUses);
53 }
robertphillips21048b52014-07-15 19:46:35 -070054}
55
56class GrAutoValidateLayer : ::SkNoncopyable {
57public:
58 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
59 : fBackingTexture(backingTexture)
60 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070061 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070062 fLayer->validate(backingTexture);
63 }
64 }
65 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070066 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070067 fLayer->validate(fBackingTexture);
68 }
69 }
robertphillips261b8a92014-07-17 08:26:44 -070070 void setBackingTexture(GrTexture* backingTexture) {
71 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
72 fBackingTexture = backingTexture;
73 }
robertphillips21048b52014-07-15 19:46:35 -070074
75private:
robertphillips261b8a92014-07-17 08:26:44 -070076 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070077 const GrCachedLayer* fLayer;
78};
79#endif
80
robertphillips4ec84da2014-06-24 13:10:43 -070081GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070082 : fContext(context) {
83 this->initAtlas();
robertphillips320c9232014-07-29 06:07:19 -070084 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000085}
86
87GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070088
robertphillips3d533ac2014-07-20 09:40:00 -070089 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
90 for (; !iter.done(); ++iter) {
91 GrCachedLayer* layer = &(*iter);
robertphillips7bb9ed72014-10-10 11:38:29 -070092 SkASSERT(0 == layer->uses());
robertphillips3d533ac2014-07-20 09:40:00 -070093 this->unlock(layer);
94 SkDELETE(layer);
95 }
robertphillips952841b2014-06-30 08:26:50 -070096
97 // The atlas only lets go of its texture when the atlas is deleted.
98 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000099}
100
robertphillips952841b2014-06-30 08:26:50 -0700101void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -0700102 SkASSERT(NULL == fAtlas.get());
robertphillips@google.come930a072014-04-03 00:34:27 +0000103
robertphillips@google.come930a072014-04-03 00:34:27 +0000104 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -0700105 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
robertphillips952841b2014-06-30 08:26:50 -0700106 kRenderTarget_GrTextureFlagBit,
robertphillips261b8a92014-07-17 08:26:44 -0700107 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000108}
109
110void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700111
robertphillips3d533ac2014-07-20 09:40:00 -0700112 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
113 for (; !iter.done(); ++iter) {
114 GrCachedLayer* layer = &(*iter);
115 this->unlock(layer);
116 SkDELETE(layer);
117 }
118 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700119
120 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700121 fAtlas.free();
robertphillips952841b2014-06-30 08:26:50 -0700122 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
123 // lazily allocates a replacement texture so reallocating a new
bsalomonc8dc1f72014-08-21 13:02:13 -0700124 // atlas here won't disrupt a GrContext::abandonContext or freeGpuResources.
robertphillips952841b2014-06-30 08:26:50 -0700125 // TODO: Make GrLayerCache lazily allocate the atlas manager?
126 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +0000127}
128
robertphillips6f294af2014-08-18 08:50:03 -0700129GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700130 int start, int stop,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700131 const SkMatrix& ctm,
132 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700133 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700134
robertphillipsed420592014-09-29 11:39:38 -0700135 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700136 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000137 return layer;
138}
139
robertphillips6f294af2014-08-18 08:50:03 -0700140GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
robertphillipsed420592014-09-29 11:39:38 -0700141 int start,
robertphillips0c423322014-07-31 11:02:38 -0700142 const SkMatrix& ctm) {
robertphillipsed420592014-09-29 11:39:38 -0700143 SkASSERT(pictureID != SK_InvalidGenID && start > 0);
144 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700145}
robertphillips@google.come930a072014-04-03 00:34:27 +0000146
robertphillips6f294af2014-08-18 08:50:03 -0700147GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700148 int start, int stop,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700149 const SkMatrix& ctm,
150 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700151 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillipsed420592014-09-29 11:39:38 -0700152 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000153 if (NULL == layer) {
robertphillipsed420592014-09-29 11:39:38 -0700154 layer = this->createLayer(pictureID, start, stop, ctm, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000155 }
robertphillips4ec84da2014-06-24 13:10:43 -0700156
robertphillips@google.come930a072014-04-03 00:34:27 +0000157 return layer;
158}
robertphillips4ec84da2014-06-24 13:10:43 -0700159
robertphillips6f294af2014-08-18 08:50:03 -0700160bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas) {
robertphillips21048b52014-07-15 19:46:35 -0700161 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700162
robertphillips320c9232014-07-29 06:07:19 -0700163 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700164 // This layer is already locked
robertphillipsa32c6bc2014-10-09 12:47:01 -0700165#ifdef SK_DEBUG
robertphillips21048b52014-07-15 19:46:35 -0700166 if (layer->isAtlased()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700167 // It claims to be atlased
robertphillips6f294af2014-08-18 08:50:03 -0700168 SkASSERT(!dontAtlas);
robertphillips952841b2014-06-30 08:26:50 -0700169 SkASSERT(layer->rect().width() == desc.fWidth);
170 SkASSERT(layer->rect().height() == desc.fHeight);
171 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700172#endif
robertphillips6f294af2014-08-18 08:50:03 -0700173 return false;
robertphillips952841b2014-06-30 08:26:50 -0700174 }
175
robertphillips320c9232014-07-29 06:07:19 -0700176 if (layer->isAtlased()) {
177 // Hooray it is still in the atlas - make sure it stays there
robertphillips6f294af2014-08-18 08:50:03 -0700178 SkASSERT(!dontAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700179 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700180 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700181 return false;
182 } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
robertphillips320c9232014-07-29 06:07:19 -0700183 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700184 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700185 if (NULL == pictInfo) {
186 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700187 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700188 }
189
190 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700191 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
192 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
193 desc.fWidth, desc.fHeight,
194 NULL, &loc);
195 // addToAtlas can allocate the backing texture
196 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700197 if (plot) {
robertphillips320c9232014-07-29 06:07:19 -0700198 // The layer was successfully added to the atlas
199 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700200 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700201 SkToS16(desc.fHeight));
202 layer->setTexture(fAtlas->getTexture(), bounds);
203 layer->setPlot(plot);
204 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700205 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700206 return true;
robertphillips320c9232014-07-29 06:07:19 -0700207 }
208
209 // The layer was rejected by the atlas (even though we know it is
210 // plausibly atlas-able). See if a plot can be purged and try again.
211 if (!this->purgePlot()) {
212 break; // We weren't able to purge any plots
213 }
robertphillips261b8a92014-07-17 08:26:44 -0700214 }
robertphillips952841b2014-06-30 08:26:50 -0700215 }
robertphillips952841b2014-06-30 08:26:50 -0700216
robertphillips21048b52014-07-15 19:46:35 -0700217 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700218 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700219 // This can yield a lot of re-rendering
robertphillipsed6f03e2014-07-30 07:31:35 -0700220 SkAutoTUnref<GrTexture> tex(fContext->lockAndRefScratchTexture(desc,
221 GrContext::kApprox_ScratchTexMatch));
222
223 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700224 layer->setLocked(true);
robertphillips6f294af2014-08-18 08:50:03 -0700225 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700226}
227
228void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700229 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
230
robertphillipsa32c6bc2014-10-09 12:47:01 -0700231 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700232 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700233 return;
234 }
235
robertphillips21048b52014-07-15 19:46:35 -0700236 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700237 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700238
robertphillips7bb9ed72014-10-10 11:38:29 -0700239 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700240 // At this point we could aggressively clear out un-locked plots but
241 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips6f294af2014-08-18 08:50:03 -0700242#if DISABLE_CACHING
robertphillips0c423322014-07-31 11:02:38 -0700243 // This testing code aggressively removes the atlased layers. This
244 // can be used to separate the performance contribution of less
245 // render target pingponging from that due to the re-use of cached layers
246 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700247 SkASSERT(pictInfo);
robertphillips0c423322014-07-31 11:02:38 -0700248
249 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
250
251 layer->setPlot(NULL);
252 layer->setTexture(NULL, GrIRect16::MakeEmpty());
253#endif
254
robertphillips21048b52014-07-15 19:46:35 -0700255 } else {
robertphillips952841b2014-06-30 08:26:50 -0700256 layer->setTexture(NULL, GrIRect16::MakeEmpty());
257 }
robertphillips320c9232014-07-29 06:07:19 -0700258
259 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700260}
261
robertphillips21048b52014-07-15 19:46:35 -0700262#ifdef SK_DEBUG
263void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700264 int plotLocks[kNumPlotsX * kNumPlotsY];
265 memset(plotLocks, 0, sizeof(plotLocks));
266
robertphillips3d533ac2014-07-20 09:40:00 -0700267 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
268 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700269 const GrCachedLayer* layer = &(*iter);
270
271 layer->validate(fAtlas->getTexture());
272
273 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700274 if (pictInfo) {
robertphillips320c9232014-07-29 06:07:19 -0700275 // In aggressive cleanup mode a picture info should only exist if
276 // it has some atlased layers
robertphillips6f294af2014-08-18 08:50:03 -0700277#if !DISABLE_CACHING
robertphillips320c9232014-07-29 06:07:19 -0700278 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
robertphillips6f294af2014-08-18 08:50:03 -0700279#endif
robertphillips320c9232014-07-29 06:07:19 -0700280 } else {
robertphillips7bb9ed72014-10-10 11:38:29 -0700281 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700282 // layers should be non-atlased.
283 SkASSERT(!layer->isAtlased());
284 }
285
bsalomon49f085d2014-09-05 13:34:00 -0700286 if (layer->plot()) {
287 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700288 SkASSERT(pictInfo->fPictureID == layer->pictureID());
289
290 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
291
292 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700293 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700294 }
295 }
296 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700297
298 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
299 SkASSERT(plotLocks[i] == fPlotLocks[i]);
300 }
robertphillips21048b52014-07-15 19:46:35 -0700301}
302
303class GrAutoValidateCache : ::SkNoncopyable {
304public:
305 explicit GrAutoValidateCache(GrLayerCache* cache)
306 : fCache(cache) {
307 fCache->validate();
308 }
309 ~GrAutoValidateCache() {
310 fCache->validate();
311 }
312private:
313 GrLayerCache* fCache;
314};
315#endif
316
robertphillipsd771f6b2014-07-22 10:18:06 -0700317void GrLayerCache::purge(uint32_t pictureID) {
318
robertphillips21048b52014-07-15 19:46:35 -0700319 SkDEBUGCODE(GrAutoValidateCache avc(this);)
320
robertphillips3d533ac2014-07-20 09:40:00 -0700321 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700322 SkTDArray<GrCachedLayer*> toBeRemoved;
323
robertphillips3d533ac2014-07-20 09:40:00 -0700324 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
325 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700326 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700327 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700328 }
329 }
330
331 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700332 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700333 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700334 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700335 SkDELETE(toBeRemoved[i]);
336 }
robertphillips261b8a92014-07-17 08:26:44 -0700337
robertphillipsd771f6b2014-07-22 10:18:06 -0700338 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700339 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700340 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700341 SkDELETE(pictInfo);
342 }
robertphillips4ec84da2014-06-24 13:10:43 -0700343}
robertphillipsd771f6b2014-07-22 10:18:06 -0700344
robertphillips320c9232014-07-29 06:07:19 -0700345bool GrLayerCache::purgePlot() {
346 SkDEBUGCODE(GrAutoValidateCache avc(this);)
347
348 GrAtlas::PlotIter iter;
349 GrPlot* plot;
350 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700351 plot;
robertphillips320c9232014-07-29 06:07:19 -0700352 plot = iter.prev()) {
353 if (fPlotLocks[plot->id()] > 0) {
354 continue;
355 }
356
robertphillips6f294af2014-08-18 08:50:03 -0700357 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700358 return true;
359 }
360
361 return false;
362}
363
robertphillips6f294af2014-08-18 08:50:03 -0700364void GrLayerCache::purgePlot(GrPlot* plot) {
365 SkASSERT(0 == fPlotLocks[plot->id()]);
366
367 // We need to find all the layers in 'plot' and remove them.
368 SkTDArray<GrCachedLayer*> toBeRemoved;
369
370 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
371 for (; !iter.done(); ++iter) {
372 if (plot == (*iter).plot()) {
373 *toBeRemoved.append() = &(*iter);
374 }
375 }
376
377 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700378 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700379 SkASSERT(!toBeRemoved[i]->locked());
380
robertphillips410dd052014-10-06 12:19:50 -0700381 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700382
robertphillips410dd052014-10-06 12:19:50 -0700383 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700384 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
385 SkDELETE(toBeRemoved[i]);
386
robertphillips410dd052014-10-06 12:19:50 -0700387 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
388 if (pictInfo) {
389 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
390
391 if (pictInfo->fPlotUsage.isEmpty()) {
392 fPictureHash.remove(pictInfo->fPictureID);
393 SkDELETE(pictInfo);
394 }
robertphillips6f294af2014-08-18 08:50:03 -0700395 }
396 }
397
398 plot->resetRects();
399}
400
401void GrLayerCache::purgeAll() {
402 GrAtlas::PlotIter iter;
403 GrPlot* plot;
404 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700405 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700406 plot = iter.prev()) {
407 SkASSERT(0 == fPlotLocks[plot->id()]);
408
409 this->purgePlot(plot);
410 }
411}
412
robertphillipsd771f6b2014-07-22 10:18:06 -0700413class GrPictureDeletionListener : public SkPicture::DeletionListener {
414 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
415 const GrPictureDeletedMessage message = { pictureID };
416 SkMessageBus<GrPictureDeletedMessage>::Post(message);
417 }
418};
419
420void GrLayerCache::trackPicture(const SkPicture* picture) {
421 if (NULL == fDeletionListener) {
422 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
423 }
424
425 picture->addDeletionListener(fDeletionListener);
426}
427
428void GrLayerCache::processDeletedPictures() {
429 SkTDArray<GrPictureDeletedMessage> deletedPictures;
430 fPictDeletionInbox.poll(&deletedPictures);
431
432 for (int i = 0; i < deletedPictures.count(); i++) {
433 this->purge(deletedPictures[i].pictureID);
434 }
435}
436