blob: fc5be5fd059d913b60883ab2467ea2d520165e88 [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"
robertphillips84ac0822014-10-14 07:07:59 -070011#include "GrSurfacePriv.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000012
robertphillips21048b52014-07-15 19:46:35 -070013#ifdef SK_DEBUG
robertphillips261b8a92014-07-17 08:26:44 -070014void GrCachedLayer::validate(const GrTexture* backingTexture) const {
robertphillips0c423322014-07-31 11:02:38 -070015 SkASSERT(SK_InvalidGenID != fKey.pictureID());
robertphillips3d533ac2014-07-20 09:40:00 -070016
bsalomon49f085d2014-09-05 13:34:00 -070017 if (fTexture) {
robertphillips21048b52014-07-15 19:46:35 -070018 // If the layer is in some texture then it must occupy some rectangle
19 SkASSERT(!fRect.isEmpty());
20 if (!this->isAtlased()) {
21 // If it isn't atlased then the rectangle should start at the origin
22 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
23 }
24 } else {
25 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070026 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070027 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070028 }
29
bsalomon49f085d2014-09-05 13:34:00 -070030 if (fPlot) {
robertphillips261b8a92014-07-17 08:26:44 -070031 // If a layer has a plot (i.e., is atlased) then it must point to
32 // the backing texture. Additionally, its rect should be non-empty.
bsalomon49f085d2014-09-05 13:34:00 -070033 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070034 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070035 }
robertphillips320c9232014-07-29 06:07:19 -070036
37 if (fLocked) {
38 // If a layer is locked it must have a texture (though it need not be
39 // the atlas-backing texture) and occupy some space.
bsalomon49f085d2014-09-05 13:34:00 -070040 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070041 SkASSERT(!fRect.isEmpty());
42 }
robertphillips7bb9ed72014-10-10 11:38:29 -070043
44 // Unfortunately there is a brief time where a layer can be locked
45 // but not used, so we can only check the "used implies locked"
46 // invariant.
47 if (fUses > 0) {
48 SkASSERT(fLocked);
49 } else {
50 SkASSERT(0 == fUses);
51 }
robertphillips21048b52014-07-15 19:46:35 -070052}
53
54class GrAutoValidateLayer : ::SkNoncopyable {
55public:
mtklein04c96952014-11-24 08:20:57 -080056 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
robertphillips21048b52014-07-15 19:46:35 -070057 : fBackingTexture(backingTexture)
58 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070059 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070060 fLayer->validate(backingTexture);
61 }
62 }
63 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070064 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070065 fLayer->validate(fBackingTexture);
66 }
67 }
robertphillips261b8a92014-07-17 08:26:44 -070068 void setBackingTexture(GrTexture* backingTexture) {
69 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
70 fBackingTexture = backingTexture;
71 }
robertphillips21048b52014-07-15 19:46:35 -070072
73private:
robertphillips261b8a92014-07-17 08:26:44 -070074 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070075 const GrCachedLayer* fLayer;
76};
77#endif
78
robertphillips4ec84da2014-06-24 13:10:43 -070079GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070080 : fContext(context) {
robertphillips320c9232014-07-29 06:07:19 -070081 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000082}
83
84GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070085
robertphillips3d533ac2014-07-20 09:40:00 -070086 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
87 for (; !iter.done(); ++iter) {
88 GrCachedLayer* layer = &(*iter);
robertphillips7bb9ed72014-10-10 11:38:29 -070089 SkASSERT(0 == layer->uses());
robertphillips3d533ac2014-07-20 09:40:00 -070090 this->unlock(layer);
91 SkDELETE(layer);
92 }
robertphillips952841b2014-06-30 08:26:50 -070093
robertphillipsb32f0ad2014-11-04 06:46:11 -080094 SkASSERT(0 == fPictureHash.count());
95
mtklein04c96952014-11-24 08:20:57 -080096 // The atlas only lets go of its texture when the atlas is deleted.
97 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000098}
99
robertphillips952841b2014-06-30 08:26:50 -0700100void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -0700101 SkASSERT(NULL == fAtlas.get());
robertphillips225a6272014-10-30 11:39:19 -0700102 GR_STATIC_ASSERT(kNumPlotsX*kNumPlotsX == GrPictureInfo::kNumPlots);
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,
bsalomonf2703d82014-10-28 14:33:06 -0700106 kRenderTarget_GrSurfaceFlag,
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
mtklein04c96952014-11-24 08:20:57 -0800120 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700121 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000122}
123
mtklein04c96952014-11-24 08:20:57 -0800124GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
125 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700126 const SkIRect& bounds,
robertphillips01d6e5f2014-12-01 09:09:27 -0800127 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800128 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800129 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700130 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700131 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700132
robertphillips01d6e5f2014-12-01 09:09:27 -0800133 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, bounds, initialMat,
134 key, keySize, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700135 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000136 return layer;
137}
138
robertphillips01d6e5f2014-12-01 09:09:27 -0800139GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID, const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800140 const unsigned* key, int keySize) {
robertphillips01d6e5f2014-12-01 09:09:27 -0800141 SkASSERT(pictureID != SK_InvalidGenID);
142 return fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips4ec84da2014-06-24 13:10:43 -0700143}
robertphillips@google.come930a072014-04-03 00:34:27 +0000144
robertphillips6f294af2014-08-18 08:50:03 -0700145GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700146 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700147 const SkIRect& bounds,
robertphillips01d6e5f2014-12-01 09:09:27 -0800148 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800149 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800150 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700151 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700152 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips01d6e5f2014-12-01 09:09:27 -0800153 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips@google.come930a072014-04-03 00:34:27 +0000154 if (NULL == layer) {
robertphillips01d6e5f2014-12-01 09:09:27 -0800155 layer = this->createLayer(pictureID, start, stop, bounds, initialMat, key, keySize, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000156 }
robertphillips4ec84da2014-06-24 13:10:43 -0700157
robertphillips@google.come930a072014-04-03 00:34:27 +0000158 return layer;
159}
robertphillips4ec84da2014-06-24 13:10:43 -0700160
mtklein04c96952014-11-24 08:20:57 -0800161bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
162 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700163 bool* needsRendering) {
robertphillips4ab5a902014-10-29 13:56:02 -0700164 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700165
robertphillipsfd61ed02014-10-28 07:21:44 -0700166 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
robertphillipsa63f32e2014-11-10 08:10:42 -0800167 SkASSERT(0 == desc.fSampleCnt);
robertphillipsfd61ed02014-10-28 07:21:44 -0700168
robertphillips320c9232014-07-29 06:07:19 -0700169 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700170 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700171 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700172 SkASSERT(layer->isAtlased());
173 SkASSERT(layer->rect().width() == desc.fWidth);
174 SkASSERT(layer->rect().height() == desc.fHeight);
175 *needsRendering = false;
176 return true;
robertphillips952841b2014-06-30 08:26:50 -0700177 }
178
robertphillips320c9232014-07-29 06:07:19 -0700179 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700180 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700181 // Hooray it is still in the atlas - make sure it stays there
182 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700183 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700184 *needsRendering = false;
185 return true;
186 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700187 if (!fAtlas) {
188 this->initAtlas();
189 if (!fAtlas) {
190 return false;
191 }
192 }
robertphillips320c9232014-07-29 06:07:19 -0700193 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700194 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700195 if (NULL == pictInfo) {
196 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700197 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700198 }
199
200 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700201 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
202 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
203 desc.fWidth, desc.fHeight,
204 NULL, &loc);
205 // addToAtlas can allocate the backing texture
206 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700207 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700208#if !GR_CACHE_HOISTED_LAYERS
209 pictInfo->incPlotUsage(plot->id());
210#endif
robertphillips320c9232014-07-29 06:07:19 -0700211 // The layer was successfully added to the atlas
robertphillipse99d4992014-12-03 07:33:57 -0800212 const SkIRect bounds = SkIRect::MakeXYWH(loc.fX, loc.fY,
213 desc.fWidth, desc.fHeight);
robertphillips320c9232014-07-29 06:07:19 -0700214 layer->setTexture(fAtlas->getTexture(), bounds);
215 layer->setPlot(plot);
216 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700217 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700218 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700219 return true;
robertphillips320c9232014-07-29 06:07:19 -0700220 }
221
mtklein04c96952014-11-24 08:20:57 -0800222 // The layer was rejected by the atlas (even though we know it is
robertphillips320c9232014-07-29 06:07:19 -0700223 // plausibly atlas-able). See if a plot can be purged and try again.
224 if (!this->purgePlot()) {
225 break; // We weren't able to purge any plots
226 }
robertphillips261b8a92014-07-17 08:26:44 -0700227 }
robertphillipsbc0c4bd2014-12-01 11:39:59 -0800228
229 if (pictInfo->fPlotUsage.isEmpty()) {
230 fPictureHash.remove(pictInfo->fPictureID);
231 SkDELETE(pictInfo);
232 }
robertphillips952841b2014-06-30 08:26:50 -0700233 }
robertphillips952841b2014-06-30 08:26:50 -0700234
robertphillipsfd61ed02014-10-28 07:21:44 -0700235 return false;
236}
237
bsalomonf2703d82014-10-28 14:33:06 -0700238bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700239 if (layer->locked()) {
240 // This layer is already locked
241 *needsRendering = false;
242 return true;
243 }
244
bsalomone3059732014-10-14 11:47:22 -0700245 SkAutoTUnref<GrTexture> tex(
246 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700247
robertphillipsfd61ed02014-10-28 07:21:44 -0700248 if (!tex) {
249 return false;
250 }
251
robertphillipse99d4992014-12-03 07:33:57 -0800252 layer->setTexture(tex, SkIRect::MakeWH(desc.fWidth, desc.fHeight));
robertphillips320c9232014-07-29 06:07:19 -0700253 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700254 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700255 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700256}
257
258void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700259 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700260
robertphillipsa32c6bc2014-10-09 12:47:01 -0700261 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700262 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700263 return;
264 }
265
robertphillips21048b52014-07-15 19:46:35 -0700266 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700267 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700268
robertphillips7bb9ed72014-10-10 11:38:29 -0700269 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700270 // At this point we could aggressively clear out un-locked plots but
271 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700272#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700273 // This testing code aggressively removes the atlased layers. This
274 // can be used to separate the performance contribution of less
275 // render target pingponging from that due to the re-use of cached layers
276 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700277 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700278
279 pictInfo->decPlotUsage(plotID);
280
281 if (0 == pictInfo->plotUsage(plotID)) {
282 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800283
284 if (pictInfo->fPlotUsage.isEmpty()) {
285 fPictureHash.remove(pictInfo->fPictureID);
286 SkDELETE(pictInfo);
287 }
robertphillips225a6272014-10-30 11:39:19 -0700288 }
mtklein04c96952014-11-24 08:20:57 -0800289
robertphillips0c423322014-07-31 11:02:38 -0700290 layer->setPlot(NULL);
robertphillipse99d4992014-12-03 07:33:57 -0800291 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips0c423322014-07-31 11:02:38 -0700292#endif
293
robertphillips21048b52014-07-15 19:46:35 -0700294 } else {
robertphillipse99d4992014-12-03 07:33:57 -0800295 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips952841b2014-06-30 08:26:50 -0700296 }
robertphillips320c9232014-07-29 06:07:19 -0700297
298 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700299}
300
robertphillips21048b52014-07-15 19:46:35 -0700301#ifdef SK_DEBUG
302void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700303 int plotLocks[kNumPlotsX * kNumPlotsY];
304 memset(plotLocks, 0, sizeof(plotLocks));
305
robertphillips3d533ac2014-07-20 09:40:00 -0700306 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
307 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700308 const GrCachedLayer* layer = &(*iter);
309
robertphillips4ab5a902014-10-29 13:56:02 -0700310 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700311
312 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700313 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700314 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700315 // layers should be non-atlased.
316 SkASSERT(!layer->isAtlased());
317 }
318
bsalomon49f085d2014-09-05 13:34:00 -0700319 if (layer->plot()) {
320 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700321 SkASSERT(pictInfo->fPictureID == layer->pictureID());
322
323 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700324#if !GR_CACHE_HOISTED_LAYERS
325 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
326#endif
robertphillips320c9232014-07-29 06:07:19 -0700327
328 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700329 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700330 }
mtklein04c96952014-11-24 08:20:57 -0800331 }
robertphillips320c9232014-07-29 06:07:19 -0700332 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700333
334 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
335 SkASSERT(plotLocks[i] == fPlotLocks[i]);
336 }
robertphillips21048b52014-07-15 19:46:35 -0700337}
338
339class GrAutoValidateCache : ::SkNoncopyable {
340public:
341 explicit GrAutoValidateCache(GrLayerCache* cache)
342 : fCache(cache) {
343 fCache->validate();
344 }
345 ~GrAutoValidateCache() {
346 fCache->validate();
347 }
348private:
349 GrLayerCache* fCache;
350};
351#endif
352
robertphillipsd771f6b2014-07-22 10:18:06 -0700353void GrLayerCache::purge(uint32_t pictureID) {
354
robertphillips21048b52014-07-15 19:46:35 -0700355 SkDEBUGCODE(GrAutoValidateCache avc(this);)
356
robertphillips3d533ac2014-07-20 09:40:00 -0700357 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700358 SkTDArray<GrCachedLayer*> toBeRemoved;
359
robertphillips3d533ac2014-07-20 09:40:00 -0700360 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
361 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700362 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700363 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700364 }
365 }
366
367 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700368 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700369 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700370 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700371 SkDELETE(toBeRemoved[i]);
372 }
robertphillips261b8a92014-07-17 08:26:44 -0700373
robertphillipsd771f6b2014-07-22 10:18:06 -0700374 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700375 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700376 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700377 SkDELETE(pictInfo);
378 }
robertphillips4ec84da2014-06-24 13:10:43 -0700379}
robertphillipsd771f6b2014-07-22 10:18:06 -0700380
robertphillips320c9232014-07-29 06:07:19 -0700381bool GrLayerCache::purgePlot() {
382 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700383 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700384
385 GrAtlas::PlotIter iter;
386 GrPlot* plot;
387 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700388 plot;
robertphillips320c9232014-07-29 06:07:19 -0700389 plot = iter.prev()) {
390 if (fPlotLocks[plot->id()] > 0) {
391 continue;
392 }
393
robertphillips6f294af2014-08-18 08:50:03 -0700394 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700395 return true;
396 }
397
398 return false;
399}
400
robertphillips6f294af2014-08-18 08:50:03 -0700401void GrLayerCache::purgePlot(GrPlot* plot) {
402 SkASSERT(0 == fPlotLocks[plot->id()]);
403
404 // We need to find all the layers in 'plot' and remove them.
405 SkTDArray<GrCachedLayer*> toBeRemoved;
406
407 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
408 for (; !iter.done(); ++iter) {
409 if (plot == (*iter).plot()) {
410 *toBeRemoved.append() = &(*iter);
411 }
412 }
413
414 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700415 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700416 SkASSERT(!toBeRemoved[i]->locked());
417
robertphillips410dd052014-10-06 12:19:50 -0700418 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700419
robertphillips410dd052014-10-06 12:19:50 -0700420 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700421 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
422 SkDELETE(toBeRemoved[i]);
423
robertphillips410dd052014-10-06 12:19:50 -0700424 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
425 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700426#if !GR_CACHE_HOISTED_LAYERS
427 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
428#endif
robertphillips410dd052014-10-06 12:19:50 -0700429 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
430
431 if (pictInfo->fPlotUsage.isEmpty()) {
432 fPictureHash.remove(pictInfo->fPictureID);
433 SkDELETE(pictInfo);
434 }
robertphillips6f294af2014-08-18 08:50:03 -0700435 }
436 }
437
438 plot->resetRects();
439}
440
robertphillips4ab5a902014-10-29 13:56:02 -0700441#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700442void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700443 if (!fAtlas) {
444 return;
445 }
446
robertphillips6f294af2014-08-18 08:50:03 -0700447 GrAtlas::PlotIter iter;
448 GrPlot* plot;
449 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700450 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700451 plot = iter.prev()) {
452 SkASSERT(0 == fPlotLocks[plot->id()]);
453
454 this->purgePlot(plot);
455 }
robertphillips4ab5a902014-10-29 13:56:02 -0700456
robertphillipsb32f0ad2014-11-04 06:46:11 -0800457 SkASSERT(0 == fPictureHash.count());
458
robertphillips4ab5a902014-10-29 13:56:02 -0700459 fContext->discardRenderTarget(fAtlas->getTexture()->asRenderTarget());
robertphillips6f294af2014-08-18 08:50:03 -0700460}
robertphillips4ab5a902014-10-29 13:56:02 -0700461#endif
robertphillips6f294af2014-08-18 08:50:03 -0700462
robertphillipsd771f6b2014-07-22 10:18:06 -0700463void GrLayerCache::processDeletedPictures() {
mtklein04c96952014-11-24 08:20:57 -0800464 SkTDArray<SkPicture::DeletionMessage> deletedPictures;
robertphillipsd771f6b2014-07-22 10:18:06 -0700465 fPictDeletionInbox.poll(&deletedPictures);
466
467 for (int i = 0; i < deletedPictures.count(); i++) {
mtklein04c96952014-11-24 08:20:57 -0800468 this->purge(deletedPictures[i].fUniqueID);
robertphillipsd771f6b2014-07-22 10:18:06 -0700469 }
470}
471
robertphillips84ac0822014-10-14 07:07:59 -0700472#ifdef SK_DEVELOPER
473void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
474
robertphillips4ab5a902014-10-29 13:56:02 -0700475 if (fAtlas) {
476 GrTexture* atlasTexture = fAtlas->getTexture();
477 if (NULL != atlasTexture) {
478 SkString fileName(dirName);
479 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700480
robertphillips4ab5a902014-10-29 13:56:02 -0700481 atlasTexture->surfacePriv().savePixels(fileName.c_str());
482 }
robertphillips84ac0822014-10-14 07:07:59 -0700483 }
484
485 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
486 for (; !iter.done(); ++iter) {
487 GrCachedLayer* layer = &(*iter);
488
489 if (layer->isAtlased() || !layer->texture()) {
490 continue;
491 }
492
493 SkString fileName(dirName);
robertphillips01d6e5f2014-12-01 09:09:27 -0800494 fileName.appendf("\\%d", layer->fKey.pictureID());
495 for (int i = 0; i < layer->fKey.keySize(); ++i) {
496 fileName.appendf("-%d", layer->fKey.key()[i]);
497 }
498 fileName.appendf(".png");
robertphillips84ac0822014-10-14 07:07:59 -0700499
500 layer->texture()->surfacePriv().savePixels(fileName.c_str());
501 }
502}
503#endif