blob: a023e6536e1faa59c2e4b0c0200f38b28bf48429 [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"
robertphillipsea461502015-05-26 11:38:03 -07009#include "GrDrawContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000010#include "GrGpu.h"
11#include "GrLayerCache.h"
robertphillips84ac0822014-10-14 07:07:59 -070012#include "GrSurfacePriv.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000013
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());
robertphillips3d533ac2014-07-20 09:40:00 -070017
bsalomon49f085d2014-09-05 13:34:00 -070018 if (fTexture) {
robertphillips21048b52014-07-15 19:46:35 -070019 // If the layer is in some texture then it must occupy some rectangle
20 SkASSERT(!fRect.isEmpty());
21 if (!this->isAtlased()) {
22 // If it isn't atlased then the rectangle should start at the origin
23 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
24 }
25 } else {
26 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070027 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070028 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070029 }
30
bsalomon49f085d2014-09-05 13:34:00 -070031 if (fPlot) {
robertphillips261b8a92014-07-17 08:26:44 -070032 // If a layer has a plot (i.e., is atlased) then it must point to
33 // the backing texture. Additionally, its rect should be non-empty.
bsalomon49f085d2014-09-05 13:34:00 -070034 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070035 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070036 }
robertphillips320c9232014-07-29 06:07:19 -070037
38 if (fLocked) {
39 // If a layer is locked it must have a texture (though it need not be
40 // the atlas-backing texture) and occupy some space.
bsalomon49f085d2014-09-05 13:34:00 -070041 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070042 SkASSERT(!fRect.isEmpty());
43 }
robertphillips7bb9ed72014-10-10 11:38:29 -070044
45 // Unfortunately there is a brief time where a layer can be locked
46 // but not used, so we can only check the "used implies locked"
47 // invariant.
48 if (fUses > 0) {
49 SkASSERT(fLocked);
50 } else {
51 SkASSERT(0 == fUses);
52 }
robertphillips21048b52014-07-15 19:46:35 -070053}
54
55class GrAutoValidateLayer : ::SkNoncopyable {
56public:
mtklein04c96952014-11-24 08:20:57 -080057 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
robertphillips21048b52014-07-15 19:46:35 -070058 : fBackingTexture(backingTexture)
59 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070060 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070061 fLayer->validate(backingTexture);
62 }
63 }
64 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070065 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070066 fLayer->validate(fBackingTexture);
67 }
68 }
robertphillips261b8a92014-07-17 08:26:44 -070069 void setBackingTexture(GrTexture* backingTexture) {
70 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
71 fBackingTexture = backingTexture;
72 }
robertphillips21048b52014-07-15 19:46:35 -070073
74private:
robertphillips261b8a92014-07-17 08:26:44 -070075 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070076 const GrCachedLayer* fLayer;
77};
78#endif
79
robertphillips4ec84da2014-06-24 13:10:43 -070080GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070081 : fContext(context) {
robertphillips320c9232014-07-29 06:07:19 -070082 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000083}
84
85GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070086
robertphillips3d533ac2014-07-20 09:40:00 -070087 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
88 for (; !iter.done(); ++iter) {
89 GrCachedLayer* layer = &(*iter);
robertphillips7bb9ed72014-10-10 11:38:29 -070090 SkASSERT(0 == layer->uses());
robertphillips3d533ac2014-07-20 09:40:00 -070091 this->unlock(layer);
92 SkDELETE(layer);
93 }
robertphillips952841b2014-06-30 08:26:50 -070094
robertphillipsb32f0ad2014-11-04 06:46:11 -080095 SkASSERT(0 == fPictureHash.count());
96
mtklein04c96952014-11-24 08:20:57 -080097 // 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());
robertphillips225a6272014-10-30 11:39:19 -0700103 GR_STATIC_ASSERT(kNumPlotsX*kNumPlotsX == GrPictureInfo::kNumPlots);
robertphillips@google.come930a072014-04-03 00:34:27 +0000104
robertphillips@google.come930a072014-04-03 00:34:27 +0000105 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -0700106 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
bsalomonf2703d82014-10-28 14:33:06 -0700107 kRenderTarget_GrSurfaceFlag,
robertphillips261b8a92014-07-17 08:26:44 -0700108 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000109}
110
111void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700112
robertphillips3d533ac2014-07-20 09:40:00 -0700113 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
114 for (; !iter.done(); ++iter) {
115 GrCachedLayer* layer = &(*iter);
116 this->unlock(layer);
117 SkDELETE(layer);
118 }
119 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700120
mtklein04c96952014-11-24 08:20:57 -0800121 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700122 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000123}
124
mtklein04c96952014-11-24 08:20:57 -0800125GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
126 int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800127 const SkIRect& srcIR,
128 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800129 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800130 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800131 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700132 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
robertphillips478dd722014-12-16 08:25:55 -0800135 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop,
136 srcIR, dstIR, initialMat,
robertphillips01d6e5f2014-12-01 09:09:27 -0800137 key, keySize, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700138 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000139 return layer;
140}
141
robertphillips01d6e5f2014-12-01 09:09:27 -0800142GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID, const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800143 const unsigned* key, int keySize) {
robertphillips01d6e5f2014-12-01 09:09:27 -0800144 SkASSERT(pictureID != SK_InvalidGenID);
145 return fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips4ec84da2014-06-24 13:10:43 -0700146}
robertphillips@google.come930a072014-04-03 00:34:27 +0000147
robertphillips6f294af2014-08-18 08:50:03 -0700148GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700149 int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800150 const SkIRect& srcIR,
151 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800152 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800153 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800154 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700155 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700156 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips01d6e5f2014-12-01 09:09:27 -0800157 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips@google.come930a072014-04-03 00:34:27 +0000158 if (NULL == layer) {
robertphillips478dd722014-12-16 08:25:55 -0800159 layer = this->createLayer(pictureID, start, stop,
160 srcIR, dstIR, initialMat,
161 key, keySize, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000162 }
robertphillips4ec84da2014-06-24 13:10:43 -0700163
robertphillips@google.come930a072014-04-03 00:34:27 +0000164 return layer;
165}
robertphillips4ec84da2014-06-24 13:10:43 -0700166
mtklein04c96952014-11-24 08:20:57 -0800167bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
168 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700169 bool* needsRendering) {
robertphillips4ab5a902014-10-29 13:56:02 -0700170 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700171
robertphillipsfd61ed02014-10-28 07:21:44 -0700172 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
robertphillipsa63f32e2014-11-10 08:10:42 -0800173 SkASSERT(0 == desc.fSampleCnt);
robertphillipsfd61ed02014-10-28 07:21:44 -0700174
robertphillips320c9232014-07-29 06:07:19 -0700175 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700176 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700177 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700178 SkASSERT(layer->isAtlased());
179 SkASSERT(layer->rect().width() == desc.fWidth);
180 SkASSERT(layer->rect().height() == desc.fHeight);
181 *needsRendering = false;
182 return true;
robertphillips952841b2014-06-30 08:26:50 -0700183 }
184
robertphillips320c9232014-07-29 06:07:19 -0700185 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700186 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700187 // Hooray it is still in the atlas - make sure it stays there
188 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700189 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700190 *needsRendering = false;
191 return true;
192 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700193 if (!fAtlas) {
194 this->initAtlas();
195 if (!fAtlas) {
196 return false;
197 }
198 }
robertphillips320c9232014-07-29 06:07:19 -0700199 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700200 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700201 if (NULL == pictInfo) {
202 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700203 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700204 }
205
206 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700207 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
208 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
209 desc.fWidth, desc.fHeight,
210 NULL, &loc);
211 // addToAtlas can allocate the backing texture
212 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700213 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700214#if !GR_CACHE_HOISTED_LAYERS
215 pictInfo->incPlotUsage(plot->id());
216#endif
robertphillips320c9232014-07-29 06:07:19 -0700217 // The layer was successfully added to the atlas
robertphillipse99d4992014-12-03 07:33:57 -0800218 const SkIRect bounds = SkIRect::MakeXYWH(loc.fX, loc.fY,
219 desc.fWidth, desc.fHeight);
robertphillips320c9232014-07-29 06:07:19 -0700220 layer->setTexture(fAtlas->getTexture(), bounds);
221 layer->setPlot(plot);
222 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700223 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700224 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700225 return true;
robertphillips320c9232014-07-29 06:07:19 -0700226 }
227
mtklein04c96952014-11-24 08:20:57 -0800228 // The layer was rejected by the atlas (even though we know it is
robertphillips320c9232014-07-29 06:07:19 -0700229 // plausibly atlas-able). See if a plot can be purged and try again.
230 if (!this->purgePlot()) {
231 break; // We weren't able to purge any plots
232 }
robertphillips261b8a92014-07-17 08:26:44 -0700233 }
robertphillipsbc0c4bd2014-12-01 11:39:59 -0800234
235 if (pictInfo->fPlotUsage.isEmpty()) {
236 fPictureHash.remove(pictInfo->fPictureID);
237 SkDELETE(pictInfo);
238 }
robertphillips952841b2014-06-30 08:26:50 -0700239 }
robertphillips952841b2014-06-30 08:26:50 -0700240
robertphillipsfd61ed02014-10-28 07:21:44 -0700241 return false;
242}
243
bsalomonf2703d82014-10-28 14:33:06 -0700244bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700245 if (layer->locked()) {
246 // This layer is already locked
247 *needsRendering = false;
248 return true;
249 }
250
robertphillips478dd722014-12-16 08:25:55 -0800251 // TODO: make the test for exact match depend on the image filters themselves
bsalomond309e7a2015-04-30 14:18:54 -0700252 GrTextureProvider::ScratchTexMatch usage = GrTextureProvider::kApprox_ScratchTexMatch;
robertphillips478dd722014-12-16 08:25:55 -0800253 if (layer->fFilter) {
bsalomond309e7a2015-04-30 14:18:54 -0700254 usage = GrTextureProvider::kExact_ScratchTexMatch;
robertphillips478dd722014-12-16 08:25:55 -0800255 }
256
bsalomond309e7a2015-04-30 14:18:54 -0700257 SkAutoTUnref<GrTexture> tex(fContext->textureProvider()->refScratchTexture(desc, usage));
robertphillipsfd61ed02014-10-28 07:21:44 -0700258 if (!tex) {
259 return false;
260 }
261
robertphillipse99d4992014-12-03 07:33:57 -0800262 layer->setTexture(tex, SkIRect::MakeWH(desc.fWidth, desc.fHeight));
robertphillips320c9232014-07-29 06:07:19 -0700263 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700264 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700265 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700266}
267
268void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700269 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700270
robertphillipsa32c6bc2014-10-09 12:47:01 -0700271 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700272 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700273 return;
274 }
275
robertphillips21048b52014-07-15 19:46:35 -0700276 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700277 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700278
robertphillips7bb9ed72014-10-10 11:38:29 -0700279 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700280 // At this point we could aggressively clear out un-locked plots but
281 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700282#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700283 // This testing code aggressively removes the atlased layers. This
284 // can be used to separate the performance contribution of less
285 // render target pingponging from that due to the re-use of cached layers
286 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700287 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700288
289 pictInfo->decPlotUsage(plotID);
290
291 if (0 == pictInfo->plotUsage(plotID)) {
292 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800293
294 if (pictInfo->fPlotUsage.isEmpty()) {
295 fPictureHash.remove(pictInfo->fPictureID);
296 SkDELETE(pictInfo);
297 }
robertphillips225a6272014-10-30 11:39:19 -0700298 }
mtklein04c96952014-11-24 08:20:57 -0800299
robertphillips0c423322014-07-31 11:02:38 -0700300 layer->setPlot(NULL);
robertphillipse99d4992014-12-03 07:33:57 -0800301 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips0c423322014-07-31 11:02:38 -0700302#endif
303
robertphillips21048b52014-07-15 19:46:35 -0700304 } else {
robertphillipse99d4992014-12-03 07:33:57 -0800305 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips952841b2014-06-30 08:26:50 -0700306 }
robertphillips320c9232014-07-29 06:07:19 -0700307
308 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700309}
310
robertphillips21048b52014-07-15 19:46:35 -0700311#ifdef SK_DEBUG
312void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700313 int plotLocks[kNumPlotsX * kNumPlotsY];
314 memset(plotLocks, 0, sizeof(plotLocks));
315
robertphillips3d533ac2014-07-20 09:40:00 -0700316 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
317 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700318 const GrCachedLayer* layer = &(*iter);
319
robertphillips4ab5a902014-10-29 13:56:02 -0700320 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700321
322 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700323 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700324 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700325 // layers should be non-atlased.
326 SkASSERT(!layer->isAtlased());
327 }
328
bsalomon49f085d2014-09-05 13:34:00 -0700329 if (layer->plot()) {
330 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700331 SkASSERT(pictInfo->fPictureID == layer->pictureID());
332
333 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700334#if !GR_CACHE_HOISTED_LAYERS
335 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
336#endif
robertphillips320c9232014-07-29 06:07:19 -0700337
338 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700339 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700340 }
mtklein04c96952014-11-24 08:20:57 -0800341 }
robertphillips320c9232014-07-29 06:07:19 -0700342 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700343
344 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
345 SkASSERT(plotLocks[i] == fPlotLocks[i]);
346 }
robertphillips21048b52014-07-15 19:46:35 -0700347}
348
349class GrAutoValidateCache : ::SkNoncopyable {
350public:
351 explicit GrAutoValidateCache(GrLayerCache* cache)
352 : fCache(cache) {
353 fCache->validate();
354 }
355 ~GrAutoValidateCache() {
356 fCache->validate();
357 }
358private:
359 GrLayerCache* fCache;
360};
361#endif
362
robertphillipsd771f6b2014-07-22 10:18:06 -0700363void GrLayerCache::purge(uint32_t pictureID) {
364
robertphillips21048b52014-07-15 19:46:35 -0700365 SkDEBUGCODE(GrAutoValidateCache avc(this);)
366
robertphillips3d533ac2014-07-20 09:40:00 -0700367 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700368 SkTDArray<GrCachedLayer*> toBeRemoved;
369
robertphillips3d533ac2014-07-20 09:40:00 -0700370 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
371 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700372 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700373 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700374 }
375 }
376
377 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700378 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700379 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700380 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700381 SkDELETE(toBeRemoved[i]);
382 }
robertphillips261b8a92014-07-17 08:26:44 -0700383
robertphillipsd771f6b2014-07-22 10:18:06 -0700384 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700385 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700386 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700387 SkDELETE(pictInfo);
388 }
robertphillips4ec84da2014-06-24 13:10:43 -0700389}
robertphillipsd771f6b2014-07-22 10:18:06 -0700390
robertphillips320c9232014-07-29 06:07:19 -0700391bool GrLayerCache::purgePlot() {
392 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700393 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700394
395 GrAtlas::PlotIter iter;
396 GrPlot* plot;
397 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700398 plot;
robertphillips320c9232014-07-29 06:07:19 -0700399 plot = iter.prev()) {
400 if (fPlotLocks[plot->id()] > 0) {
401 continue;
402 }
403
robertphillips6f294af2014-08-18 08:50:03 -0700404 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700405 return true;
406 }
407
408 return false;
409}
410
robertphillips6f294af2014-08-18 08:50:03 -0700411void GrLayerCache::purgePlot(GrPlot* plot) {
412 SkASSERT(0 == fPlotLocks[plot->id()]);
413
414 // We need to find all the layers in 'plot' and remove them.
415 SkTDArray<GrCachedLayer*> toBeRemoved;
416
417 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
418 for (; !iter.done(); ++iter) {
419 if (plot == (*iter).plot()) {
420 *toBeRemoved.append() = &(*iter);
421 }
422 }
423
424 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700425 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700426 SkASSERT(!toBeRemoved[i]->locked());
427
robertphillips410dd052014-10-06 12:19:50 -0700428 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700429
robertphillips410dd052014-10-06 12:19:50 -0700430 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700431 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
432 SkDELETE(toBeRemoved[i]);
433
robertphillips410dd052014-10-06 12:19:50 -0700434 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
435 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700436#if !GR_CACHE_HOISTED_LAYERS
437 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
438#endif
robertphillips410dd052014-10-06 12:19:50 -0700439 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
440
441 if (pictInfo->fPlotUsage.isEmpty()) {
442 fPictureHash.remove(pictInfo->fPictureID);
443 SkDELETE(pictInfo);
444 }
robertphillips6f294af2014-08-18 08:50:03 -0700445 }
446 }
447
448 plot->resetRects();
449}
450
robertphillips4ab5a902014-10-29 13:56:02 -0700451#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700452void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700453 if (!fAtlas) {
454 return;
455 }
456
robertphillips6f294af2014-08-18 08:50:03 -0700457 GrAtlas::PlotIter iter;
458 GrPlot* plot;
459 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700460 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700461 plot = iter.prev()) {
462 SkASSERT(0 == fPlotLocks[plot->id()]);
463
464 this->purgePlot(plot);
465 }
robertphillips4ab5a902014-10-29 13:56:02 -0700466
robertphillipsb32f0ad2014-11-04 06:46:11 -0800467 SkASSERT(0 == fPictureHash.count());
468
robertphillipsea461502015-05-26 11:38:03 -0700469 GrDrawContext* drawContext = fContext->drawContext();
470
471 if (drawContext) {
472 drawContext->discard(fAtlas->getTexture()->asRenderTarget());
473 }
robertphillips6f294af2014-08-18 08:50:03 -0700474}
robertphillips4ab5a902014-10-29 13:56:02 -0700475#endif
robertphillips6f294af2014-08-18 08:50:03 -0700476
robertphillipsd771f6b2014-07-22 10:18:06 -0700477void GrLayerCache::processDeletedPictures() {
bsalomon23e619c2015-02-06 11:54:28 -0800478 SkTArray<SkPicture::DeletionMessage> deletedPictures;
robertphillipsd771f6b2014-07-22 10:18:06 -0700479 fPictDeletionInbox.poll(&deletedPictures);
480
481 for (int i = 0; i < deletedPictures.count(); i++) {
mtklein04c96952014-11-24 08:20:57 -0800482 this->purge(deletedPictures[i].fUniqueID);
robertphillipsd771f6b2014-07-22 10:18:06 -0700483 }
484}
485
robertphillips84ac0822014-10-14 07:07:59 -0700486#ifdef SK_DEVELOPER
487void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
488
robertphillips4ab5a902014-10-29 13:56:02 -0700489 if (fAtlas) {
490 GrTexture* atlasTexture = fAtlas->getTexture();
491 if (NULL != atlasTexture) {
492 SkString fileName(dirName);
493 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700494
robertphillips4ab5a902014-10-29 13:56:02 -0700495 atlasTexture->surfacePriv().savePixels(fileName.c_str());
496 }
robertphillips84ac0822014-10-14 07:07:59 -0700497 }
498
499 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
500 for (; !iter.done(); ++iter) {
501 GrCachedLayer* layer = &(*iter);
502
503 if (layer->isAtlased() || !layer->texture()) {
504 continue;
505 }
506
507 SkString fileName(dirName);
robertphillips01d6e5f2014-12-01 09:09:27 -0800508 fileName.appendf("\\%d", layer->fKey.pictureID());
509 for (int i = 0; i < layer->fKey.keySize(); ++i) {
510 fileName.appendf("-%d", layer->fKey.key()[i]);
511 }
512 fileName.appendf(".png");
robertphillips84ac0822014-10-14 07:07:59 -0700513
514 layer->texture()->surfacePriv().savePixels(fileName.c_str());
515 }
516}
517#endif