blob: aea1c9b30f9f737dbc7a3b82dc2cfbddf688c958 [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,
robertphillips478dd722014-12-16 08:25:55 -0800126 const SkIRect& srcIR,
127 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800128 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800129 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800130 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700131 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700132 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700133
robertphillips478dd722014-12-16 08:25:55 -0800134 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop,
135 srcIR, dstIR, initialMat,
robertphillips01d6e5f2014-12-01 09:09:27 -0800136 key, keySize, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700137 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000138 return layer;
139}
140
robertphillips01d6e5f2014-12-01 09:09:27 -0800141GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID, const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800142 const unsigned* key, int keySize) {
robertphillips01d6e5f2014-12-01 09:09:27 -0800143 SkASSERT(pictureID != SK_InvalidGenID);
144 return fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
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,
robertphillips478dd722014-12-16 08:25:55 -0800149 const SkIRect& srcIR,
150 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800151 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800152 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800153 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700154 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700155 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips01d6e5f2014-12-01 09:09:27 -0800156 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips@google.come930a072014-04-03 00:34:27 +0000157 if (NULL == layer) {
robertphillips478dd722014-12-16 08:25:55 -0800158 layer = this->createLayer(pictureID, start, stop,
159 srcIR, dstIR, initialMat,
160 key, keySize, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000161 }
robertphillips4ec84da2014-06-24 13:10:43 -0700162
robertphillips@google.come930a072014-04-03 00:34:27 +0000163 return layer;
164}
robertphillips4ec84da2014-06-24 13:10:43 -0700165
mtklein04c96952014-11-24 08:20:57 -0800166bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
167 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700168 bool* needsRendering) {
robertphillips4ab5a902014-10-29 13:56:02 -0700169 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700170
robertphillipsfd61ed02014-10-28 07:21:44 -0700171 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
robertphillipsa63f32e2014-11-10 08:10:42 -0800172 SkASSERT(0 == desc.fSampleCnt);
robertphillipsfd61ed02014-10-28 07:21:44 -0700173
robertphillips320c9232014-07-29 06:07:19 -0700174 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700175 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700176 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700177 SkASSERT(layer->isAtlased());
178 SkASSERT(layer->rect().width() == desc.fWidth);
179 SkASSERT(layer->rect().height() == desc.fHeight);
180 *needsRendering = false;
181 return true;
robertphillips952841b2014-06-30 08:26:50 -0700182 }
183
robertphillips320c9232014-07-29 06:07:19 -0700184 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700185 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700186 // Hooray it is still in the atlas - make sure it stays there
187 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700188 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700189 *needsRendering = false;
190 return true;
191 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700192 if (!fAtlas) {
193 this->initAtlas();
194 if (!fAtlas) {
195 return false;
196 }
197 }
robertphillips320c9232014-07-29 06:07:19 -0700198 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700199 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700200 if (NULL == pictInfo) {
201 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700202 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700203 }
204
205 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700206 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
207 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
208 desc.fWidth, desc.fHeight,
209 NULL, &loc);
210 // addToAtlas can allocate the backing texture
211 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700212 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700213#if !GR_CACHE_HOISTED_LAYERS
214 pictInfo->incPlotUsage(plot->id());
215#endif
robertphillips320c9232014-07-29 06:07:19 -0700216 // The layer was successfully added to the atlas
robertphillipse99d4992014-12-03 07:33:57 -0800217 const SkIRect bounds = SkIRect::MakeXYWH(loc.fX, loc.fY,
218 desc.fWidth, desc.fHeight);
robertphillips320c9232014-07-29 06:07:19 -0700219 layer->setTexture(fAtlas->getTexture(), bounds);
220 layer->setPlot(plot);
221 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700222 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700223 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700224 return true;
robertphillips320c9232014-07-29 06:07:19 -0700225 }
226
mtklein04c96952014-11-24 08:20:57 -0800227 // The layer was rejected by the atlas (even though we know it is
robertphillips320c9232014-07-29 06:07:19 -0700228 // plausibly atlas-able). See if a plot can be purged and try again.
229 if (!this->purgePlot()) {
230 break; // We weren't able to purge any plots
231 }
robertphillips261b8a92014-07-17 08:26:44 -0700232 }
robertphillipsbc0c4bd2014-12-01 11:39:59 -0800233
234 if (pictInfo->fPlotUsage.isEmpty()) {
235 fPictureHash.remove(pictInfo->fPictureID);
236 SkDELETE(pictInfo);
237 }
robertphillips952841b2014-06-30 08:26:50 -0700238 }
robertphillips952841b2014-06-30 08:26:50 -0700239
robertphillipsfd61ed02014-10-28 07:21:44 -0700240 return false;
241}
242
bsalomonf2703d82014-10-28 14:33:06 -0700243bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700244 if (layer->locked()) {
245 // This layer is already locked
246 *needsRendering = false;
247 return true;
248 }
249
robertphillips478dd722014-12-16 08:25:55 -0800250 // TODO: make the test for exact match depend on the image filters themselves
bsalomond309e7a2015-04-30 14:18:54 -0700251 GrTextureProvider::ScratchTexMatch usage = GrTextureProvider::kApprox_ScratchTexMatch;
robertphillips478dd722014-12-16 08:25:55 -0800252 if (layer->fFilter) {
bsalomond309e7a2015-04-30 14:18:54 -0700253 usage = GrTextureProvider::kExact_ScratchTexMatch;
robertphillips478dd722014-12-16 08:25:55 -0800254 }
255
bsalomond309e7a2015-04-30 14:18:54 -0700256 SkAutoTUnref<GrTexture> tex(fContext->textureProvider()->refScratchTexture(desc, usage));
robertphillipsfd61ed02014-10-28 07:21:44 -0700257 if (!tex) {
258 return false;
259 }
260
robertphillipse99d4992014-12-03 07:33:57 -0800261 layer->setTexture(tex, SkIRect::MakeWH(desc.fWidth, desc.fHeight));
robertphillips320c9232014-07-29 06:07:19 -0700262 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700263 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700264 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700265}
266
267void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700268 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700269
robertphillipsa32c6bc2014-10-09 12:47:01 -0700270 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700271 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700272 return;
273 }
274
robertphillips21048b52014-07-15 19:46:35 -0700275 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700276 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700277
robertphillips7bb9ed72014-10-10 11:38:29 -0700278 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700279 // At this point we could aggressively clear out un-locked plots but
280 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700281#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700282 // This testing code aggressively removes the atlased layers. This
283 // can be used to separate the performance contribution of less
284 // render target pingponging from that due to the re-use of cached layers
285 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700286 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700287
288 pictInfo->decPlotUsage(plotID);
289
290 if (0 == pictInfo->plotUsage(plotID)) {
291 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800292
293 if (pictInfo->fPlotUsage.isEmpty()) {
294 fPictureHash.remove(pictInfo->fPictureID);
295 SkDELETE(pictInfo);
296 }
robertphillips225a6272014-10-30 11:39:19 -0700297 }
mtklein04c96952014-11-24 08:20:57 -0800298
robertphillips0c423322014-07-31 11:02:38 -0700299 layer->setPlot(NULL);
robertphillipse99d4992014-12-03 07:33:57 -0800300 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips0c423322014-07-31 11:02:38 -0700301#endif
302
robertphillips21048b52014-07-15 19:46:35 -0700303 } else {
robertphillipse99d4992014-12-03 07:33:57 -0800304 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips952841b2014-06-30 08:26:50 -0700305 }
robertphillips320c9232014-07-29 06:07:19 -0700306
307 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700308}
309
robertphillips21048b52014-07-15 19:46:35 -0700310#ifdef SK_DEBUG
311void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700312 int plotLocks[kNumPlotsX * kNumPlotsY];
313 memset(plotLocks, 0, sizeof(plotLocks));
314
robertphillips3d533ac2014-07-20 09:40:00 -0700315 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
316 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700317 const GrCachedLayer* layer = &(*iter);
318
robertphillips4ab5a902014-10-29 13:56:02 -0700319 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700320
321 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700322 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700323 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700324 // layers should be non-atlased.
325 SkASSERT(!layer->isAtlased());
326 }
327
bsalomon49f085d2014-09-05 13:34:00 -0700328 if (layer->plot()) {
329 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700330 SkASSERT(pictInfo->fPictureID == layer->pictureID());
331
332 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700333#if !GR_CACHE_HOISTED_LAYERS
334 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
335#endif
robertphillips320c9232014-07-29 06:07:19 -0700336
337 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700338 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700339 }
mtklein04c96952014-11-24 08:20:57 -0800340 }
robertphillips320c9232014-07-29 06:07:19 -0700341 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700342
343 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
344 SkASSERT(plotLocks[i] == fPlotLocks[i]);
345 }
robertphillips21048b52014-07-15 19:46:35 -0700346}
347
348class GrAutoValidateCache : ::SkNoncopyable {
349public:
350 explicit GrAutoValidateCache(GrLayerCache* cache)
351 : fCache(cache) {
352 fCache->validate();
353 }
354 ~GrAutoValidateCache() {
355 fCache->validate();
356 }
357private:
358 GrLayerCache* fCache;
359};
360#endif
361
robertphillipsd771f6b2014-07-22 10:18:06 -0700362void GrLayerCache::purge(uint32_t pictureID) {
363
robertphillips21048b52014-07-15 19:46:35 -0700364 SkDEBUGCODE(GrAutoValidateCache avc(this);)
365
robertphillips3d533ac2014-07-20 09:40:00 -0700366 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700367 SkTDArray<GrCachedLayer*> toBeRemoved;
368
robertphillips3d533ac2014-07-20 09:40:00 -0700369 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
370 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700371 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700372 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700373 }
374 }
375
376 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700377 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700378 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700379 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700380 SkDELETE(toBeRemoved[i]);
381 }
robertphillips261b8a92014-07-17 08:26:44 -0700382
robertphillipsd771f6b2014-07-22 10:18:06 -0700383 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700384 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700385 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700386 SkDELETE(pictInfo);
387 }
robertphillips4ec84da2014-06-24 13:10:43 -0700388}
robertphillipsd771f6b2014-07-22 10:18:06 -0700389
robertphillips320c9232014-07-29 06:07:19 -0700390bool GrLayerCache::purgePlot() {
391 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700392 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700393
394 GrAtlas::PlotIter iter;
395 GrPlot* plot;
396 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700397 plot;
robertphillips320c9232014-07-29 06:07:19 -0700398 plot = iter.prev()) {
399 if (fPlotLocks[plot->id()] > 0) {
400 continue;
401 }
402
robertphillips6f294af2014-08-18 08:50:03 -0700403 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700404 return true;
405 }
406
407 return false;
408}
409
robertphillips6f294af2014-08-18 08:50:03 -0700410void GrLayerCache::purgePlot(GrPlot* plot) {
411 SkASSERT(0 == fPlotLocks[plot->id()]);
412
413 // We need to find all the layers in 'plot' and remove them.
414 SkTDArray<GrCachedLayer*> toBeRemoved;
415
416 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
417 for (; !iter.done(); ++iter) {
418 if (plot == (*iter).plot()) {
419 *toBeRemoved.append() = &(*iter);
420 }
421 }
422
423 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700424 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700425 SkASSERT(!toBeRemoved[i]->locked());
426
robertphillips410dd052014-10-06 12:19:50 -0700427 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700428
robertphillips410dd052014-10-06 12:19:50 -0700429 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700430 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
431 SkDELETE(toBeRemoved[i]);
432
robertphillips410dd052014-10-06 12:19:50 -0700433 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
434 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700435#if !GR_CACHE_HOISTED_LAYERS
436 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
437#endif
robertphillips410dd052014-10-06 12:19:50 -0700438 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
439
440 if (pictInfo->fPlotUsage.isEmpty()) {
441 fPictureHash.remove(pictInfo->fPictureID);
442 SkDELETE(pictInfo);
443 }
robertphillips6f294af2014-08-18 08:50:03 -0700444 }
445 }
446
447 plot->resetRects();
448}
449
robertphillips4ab5a902014-10-29 13:56:02 -0700450#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700451void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700452 if (!fAtlas) {
453 return;
454 }
455
robertphillips6f294af2014-08-18 08:50:03 -0700456 GrAtlas::PlotIter iter;
457 GrPlot* plot;
458 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700459 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700460 plot = iter.prev()) {
461 SkASSERT(0 == fPlotLocks[plot->id()]);
462
463 this->purgePlot(plot);
464 }
robertphillips4ab5a902014-10-29 13:56:02 -0700465
robertphillipsb32f0ad2014-11-04 06:46:11 -0800466 SkASSERT(0 == fPictureHash.count());
467
robertphillips4ab5a902014-10-29 13:56:02 -0700468 fContext->discardRenderTarget(fAtlas->getTexture()->asRenderTarget());
robertphillips6f294af2014-08-18 08:50:03 -0700469}
robertphillips4ab5a902014-10-29 13:56:02 -0700470#endif
robertphillips6f294af2014-08-18 08:50:03 -0700471
robertphillipsd771f6b2014-07-22 10:18:06 -0700472void GrLayerCache::processDeletedPictures() {
bsalomon23e619c2015-02-06 11:54:28 -0800473 SkTArray<SkPicture::DeletionMessage> deletedPictures;
robertphillipsd771f6b2014-07-22 10:18:06 -0700474 fPictDeletionInbox.poll(&deletedPictures);
475
476 for (int i = 0; i < deletedPictures.count(); i++) {
mtklein04c96952014-11-24 08:20:57 -0800477 this->purge(deletedPictures[i].fUniqueID);
robertphillipsd771f6b2014-07-22 10:18:06 -0700478 }
479}
480
robertphillips84ac0822014-10-14 07:07:59 -0700481#ifdef SK_DEVELOPER
482void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
483
robertphillips4ab5a902014-10-29 13:56:02 -0700484 if (fAtlas) {
485 GrTexture* atlasTexture = fAtlas->getTexture();
486 if (NULL != atlasTexture) {
487 SkString fileName(dirName);
488 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700489
robertphillips4ab5a902014-10-29 13:56:02 -0700490 atlasTexture->surfacePriv().savePixels(fileName.c_str());
491 }
robertphillips84ac0822014-10-14 07:07:59 -0700492 }
493
494 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
495 for (; !iter.done(); ++iter) {
496 GrCachedLayer* layer = &(*iter);
497
498 if (layer->isAtlased() || !layer->texture()) {
499 continue;
500 }
501
502 SkString fileName(dirName);
robertphillips01d6e5f2014-12-01 09:09:27 -0800503 fileName.appendf("\\%d", layer->fKey.pictureID());
504 for (int i = 0; i < layer->fKey.keySize(); ++i) {
505 fileName.appendf("-%d", layer->fKey.key()[i]);
506 }
507 fileName.appendf(".png");
robertphillips84ac0822014-10-14 07:07:59 -0700508
509 layer->texture()->surfacePriv().savePixels(fileName.c_str());
510 }
511}
512#endif