blob: 11a97e4c666e1a56f1291f451be7f687a2ac0f0b [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());
17 SkASSERT(fKey.start() > 0 && fKey.stop() > 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,
robertphillips4815fe52014-09-16 10:32:43 -0700122 const SkIPoint& offset,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700123 const SkMatrix& ctm,
124 const SkPaint* paint) {
robertphillips6f294af2014-08-18 08:50:03 -0700125 SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700126
robertphillips4aa6dfc2014-09-17 07:50:47 -0700127 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, offset, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700128 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000129 return layer;
130}
131
robertphillips6f294af2014-08-18 08:50:03 -0700132GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700133 int start, int stop,
robertphillips4815fe52014-09-16 10:32:43 -0700134 const SkIPoint& offset,
robertphillips0c423322014-07-31 11:02:38 -0700135 const SkMatrix& ctm) {
robertphillips6f294af2014-08-18 08:50:03 -0700136 SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0);
robertphillips4815fe52014-09-16 10:32:43 -0700137 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, stop, offset, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700138}
robertphillips@google.come930a072014-04-03 00:34:27 +0000139
robertphillips6f294af2014-08-18 08:50:03 -0700140GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700141 int start, int stop,
robertphillips4815fe52014-09-16 10:32:43 -0700142 const SkIPoint& offset,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700143 const SkMatrix& ctm,
144 const SkPaint* paint) {
robertphillips6f294af2014-08-18 08:50:03 -0700145 SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0);
robertphillips4815fe52014-09-16 10:32:43 -0700146 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, stop, offset, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000147 if (NULL == layer) {
robertphillips4aa6dfc2014-09-17 07:50:47 -0700148 layer = this->createLayer(pictureID, start, stop, offset, ctm, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000149 }
robertphillips4ec84da2014-06-24 13:10:43 -0700150
robertphillips@google.come930a072014-04-03 00:34:27 +0000151 return layer;
152}
robertphillips4ec84da2014-06-24 13:10:43 -0700153
robertphillips6f294af2014-08-18 08:50:03 -0700154bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas) {
robertphillips21048b52014-07-15 19:46:35 -0700155 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700156
robertphillips320c9232014-07-29 06:07:19 -0700157 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700158 // This layer is already locked
159#ifdef SK_DEBUG
robertphillips21048b52014-07-15 19:46:35 -0700160 if (layer->isAtlased()) {
robertphillips952841b2014-06-30 08:26:50 -0700161 // It claims to be atlased
robertphillips6f294af2014-08-18 08:50:03 -0700162 SkASSERT(!dontAtlas);
robertphillips952841b2014-06-30 08:26:50 -0700163 SkASSERT(layer->rect().width() == desc.fWidth);
164 SkASSERT(layer->rect().height() == desc.fHeight);
165 }
166#endif
robertphillips6f294af2014-08-18 08:50:03 -0700167 return false;
robertphillips952841b2014-06-30 08:26:50 -0700168 }
169
robertphillips320c9232014-07-29 06:07:19 -0700170 if (layer->isAtlased()) {
171 // Hooray it is still in the atlas - make sure it stays there
robertphillips6f294af2014-08-18 08:50:03 -0700172 SkASSERT(!dontAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700173 layer->setLocked(true);
174 fPlotLocks[layer->plot()->id()]++;
robertphillips6f294af2014-08-18 08:50:03 -0700175 return false;
176 } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
robertphillips320c9232014-07-29 06:07:19 -0700177 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700178 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700179 if (NULL == pictInfo) {
180 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700181 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700182 }
183
184 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700185 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,
188 NULL, &loc);
189 // addToAtlas can allocate the backing texture
190 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700191 if (plot) {
robertphillips320c9232014-07-29 06:07:19 -0700192 // The layer was successfully added to the atlas
193 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700194 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700195 SkToS16(desc.fHeight));
196 layer->setTexture(fAtlas->getTexture(), bounds);
197 layer->setPlot(plot);
198 layer->setLocked(true);
199 fPlotLocks[layer->plot()->id()]++;
robertphillips6f294af2014-08-18 08:50:03 -0700200 return true;
robertphillips320c9232014-07-29 06:07:19 -0700201 }
202
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
207 }
robertphillips261b8a92014-07-17 08:26:44 -0700208 }
robertphillips952841b2014-06-30 08:26:50 -0700209 }
robertphillips952841b2014-06-30 08:26:50 -0700210
robertphillips21048b52014-07-15 19:46:35 -0700211 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700212 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700213 // This can yield a lot of re-rendering
robertphillipsed6f03e2014-07-30 07:31:35 -0700214 SkAutoTUnref<GrTexture> tex(fContext->lockAndRefScratchTexture(desc,
215 GrContext::kApprox_ScratchTexMatch));
216
217 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700218 layer->setLocked(true);
robertphillips6f294af2014-08-18 08:50:03 -0700219 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700220}
221
222void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700223 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
224
robertphillips320c9232014-07-29 06:07:19 -0700225 if (NULL == layer || !layer->locked()) {
226 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700227 return;
228 }
229
robertphillips21048b52014-07-15 19:46:35 -0700230 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700231 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700232
robertphillips320c9232014-07-29 06:07:19 -0700233 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.
robertphillips6f294af2014-08-18 08:50:03 -0700237#if DISABLE_CACHING
robertphillips0c423322014-07-31 11:02:38 -0700238 // 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());
bsalomon49f085d2014-09-05 13:34:00 -0700242 SkASSERT(pictInfo);
robertphillips0c423322014-07-31 11:02:38 -0700243
244 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
245
246 layer->setPlot(NULL);
247 layer->setTexture(NULL, GrIRect16::MakeEmpty());
248#endif
249
robertphillips21048b52014-07-15 19:46:35 -0700250 } else {
robertphillips952841b2014-06-30 08:26:50 -0700251 fContext->unlockScratchTexture(layer->texture());
252 layer->setTexture(NULL, GrIRect16::MakeEmpty());
253 }
robertphillips320c9232014-07-29 06:07:19 -0700254
255 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700256}
257
robertphillips21048b52014-07-15 19:46:35 -0700258#ifdef SK_DEBUG
259void GrLayerCache::validate() const {
robertphillips320c9232014-07-29 06:07:19 -0700260 int plotLocks[kNumPlotsX * kNumPlotsY];
261 memset(plotLocks, 0, sizeof(plotLocks));
262
robertphillips3d533ac2014-07-20 09:40:00 -0700263 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
264 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700265 const GrCachedLayer* layer = &(*iter);
266
267 layer->validate(fAtlas->getTexture());
268
269 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700270 if (pictInfo) {
robertphillips320c9232014-07-29 06:07:19 -0700271 // In aggressive cleanup mode a picture info should only exist if
272 // it has some atlased layers
robertphillips6f294af2014-08-18 08:50:03 -0700273#if !DISABLE_CACHING
robertphillips320c9232014-07-29 06:07:19 -0700274 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
robertphillips6f294af2014-08-18 08:50:03 -0700275#endif
robertphillips320c9232014-07-29 06:07:19 -0700276 } else {
277 // If there is no picture info for this layer then all of its
278 // layers should be non-atlased.
279 SkASSERT(!layer->isAtlased());
280 }
281
bsalomon49f085d2014-09-05 13:34:00 -0700282 if (layer->plot()) {
283 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700284 SkASSERT(pictInfo->fPictureID == layer->pictureID());
285
286 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
287
288 if (layer->locked()) {
289 plotLocks[layer->plot()->id()]++;
290 }
291 }
292 }
293
294 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
295 SkASSERT(plotLocks[i] == fPlotLocks[i]);
robertphillips21048b52014-07-15 19:46:35 -0700296 }
297}
298
299class GrAutoValidateCache : ::SkNoncopyable {
300public:
301 explicit GrAutoValidateCache(GrLayerCache* cache)
302 : fCache(cache) {
303 fCache->validate();
304 }
305 ~GrAutoValidateCache() {
306 fCache->validate();
307 }
308private:
309 GrLayerCache* fCache;
310};
311#endif
312
robertphillipsd771f6b2014-07-22 10:18:06 -0700313void GrLayerCache::purge(uint32_t pictureID) {
314
robertphillips21048b52014-07-15 19:46:35 -0700315 SkDEBUGCODE(GrAutoValidateCache avc(this);)
316
robertphillips3d533ac2014-07-20 09:40:00 -0700317 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700318 SkTDArray<GrCachedLayer*> toBeRemoved;
319
robertphillips3d533ac2014-07-20 09:40:00 -0700320 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
321 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700322 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700323 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700324 }
325 }
326
327 for (int i = 0; i < toBeRemoved.count(); ++i) {
328 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700329 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700330 SkDELETE(toBeRemoved[i]);
331 }
robertphillips261b8a92014-07-17 08:26:44 -0700332
robertphillipsd771f6b2014-07-22 10:18:06 -0700333 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700334 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700335 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700336 SkDELETE(pictInfo);
337 }
robertphillips4ec84da2014-06-24 13:10:43 -0700338}
robertphillipsd771f6b2014-07-22 10:18:06 -0700339
robertphillips320c9232014-07-29 06:07:19 -0700340bool GrLayerCache::purgePlot() {
341 SkDEBUGCODE(GrAutoValidateCache avc(this);)
342
343 GrAtlas::PlotIter iter;
344 GrPlot* plot;
345 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700346 plot;
robertphillips320c9232014-07-29 06:07:19 -0700347 plot = iter.prev()) {
348 if (fPlotLocks[plot->id()] > 0) {
349 continue;
350 }
351
robertphillips6f294af2014-08-18 08:50:03 -0700352 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700353 return true;
354 }
355
356 return false;
357}
358
robertphillips6f294af2014-08-18 08:50:03 -0700359void GrLayerCache::purgePlot(GrPlot* plot) {
360 SkASSERT(0 == fPlotLocks[plot->id()]);
361
362 // We need to find all the layers in 'plot' and remove them.
363 SkTDArray<GrCachedLayer*> toBeRemoved;
364
365 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
366 for (; !iter.done(); ++iter) {
367 if (plot == (*iter).plot()) {
368 *toBeRemoved.append() = &(*iter);
369 }
370 }
371
372 for (int i = 0; i < toBeRemoved.count(); ++i) {
373 SkASSERT(!toBeRemoved[i]->locked());
374
375 GrPictureInfo* pictInfo = fPictureHash.find(toBeRemoved[i]->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700376 SkASSERT(pictInfo);
robertphillips6f294af2014-08-18 08:50:03 -0700377
378 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
379
380 // Aggressively remove layers and, if now totally uncached, picture info
381 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
382 SkDELETE(toBeRemoved[i]);
383
384 if (pictInfo->fPlotUsage.isEmpty()) {
385 fPictureHash.remove(pictInfo->fPictureID);
386 SkDELETE(pictInfo);
387 }
388 }
389
390 plot->resetRects();
391}
392
393void GrLayerCache::purgeAll() {
394 GrAtlas::PlotIter iter;
395 GrPlot* plot;
396 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700397 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700398 plot = iter.prev()) {
399 SkASSERT(0 == fPlotLocks[plot->id()]);
400
401 this->purgePlot(plot);
402 }
403}
404
robertphillipsd771f6b2014-07-22 10:18:06 -0700405class GrPictureDeletionListener : public SkPicture::DeletionListener {
406 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
407 const GrPictureDeletedMessage message = { pictureID };
408 SkMessageBus<GrPictureDeletedMessage>::Post(message);
409 }
410};
411
412void GrLayerCache::trackPicture(const SkPicture* picture) {
413 if (NULL == fDeletionListener) {
414 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
415 }
416
417 picture->addDeletionListener(fDeletionListener);
418}
419
420void GrLayerCache::processDeletedPictures() {
421 SkTDArray<GrPictureDeletedMessage> deletedPictures;
422 fPictDeletionInbox.poll(&deletedPictures);
423
424 for (int i = 0; i < deletedPictures.count(); i++) {
425 this->purge(deletedPictures[i].pictureID);
426 }
427}
428