blob: 4d5f2b46f8b967e0105735fcdc589110cba14dd1 [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"
kkinnunencabe20c2015-06-01 01:37:26 -07009#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000011#include "GrGpu.h"
12#include "GrLayerCache.h"
robertphillips84ac0822014-10-14 07:07:59 -070013#include "GrSurfacePriv.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000014
robertphillips21048b52014-07-15 19:46:35 -070015#ifdef SK_DEBUG
robertphillips261b8a92014-07-17 08:26:44 -070016void GrCachedLayer::validate(const GrTexture* backingTexture) const {
robertphillips0c423322014-07-31 11:02:38 -070017 SkASSERT(SK_InvalidGenID != fKey.pictureID());
robertphillips3d533ac2014-07-20 09:40:00 -070018
bsalomon49f085d2014-09-05 13:34:00 -070019 if (fTexture) {
robertphillips21048b52014-07-15 19:46:35 -070020 // If the layer is in some texture then it must occupy some rectangle
21 SkASSERT(!fRect.isEmpty());
22 if (!this->isAtlased()) {
23 // If it isn't atlased then the rectangle should start at the origin
24 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
25 }
26 } else {
27 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070028 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070029 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070030 }
31
bsalomon49f085d2014-09-05 13:34:00 -070032 if (fPlot) {
robertphillips261b8a92014-07-17 08:26:44 -070033 // If a layer has a plot (i.e., is atlased) then it must point to
34 // the backing texture. Additionally, its rect should be non-empty.
bsalomon49f085d2014-09-05 13:34:00 -070035 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070036 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070037 }
robertphillips320c9232014-07-29 06:07:19 -070038
39 if (fLocked) {
40 // If a layer is locked it must have a texture (though it need not be
41 // the atlas-backing texture) and occupy some space.
bsalomon49f085d2014-09-05 13:34:00 -070042 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070043 SkASSERT(!fRect.isEmpty());
44 }
robertphillips7bb9ed72014-10-10 11:38:29 -070045
46 // Unfortunately there is a brief time where a layer can be locked
47 // but not used, so we can only check the "used implies locked"
48 // invariant.
49 if (fUses > 0) {
50 SkASSERT(fLocked);
51 } else {
52 SkASSERT(0 == fUses);
53 }
robertphillips21048b52014-07-15 19:46:35 -070054}
55
56class GrAutoValidateLayer : ::SkNoncopyable {
57public:
mtklein04c96952014-11-24 08:20:57 -080058 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
robertphillips21048b52014-07-15 19:46:35 -070059 : fBackingTexture(backingTexture)
60 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070061 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070062 fLayer->validate(backingTexture);
63 }
64 }
65 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070066 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070067 fLayer->validate(fBackingTexture);
68 }
69 }
robertphillips261b8a92014-07-17 08:26:44 -070070 void setBackingTexture(GrTexture* backingTexture) {
71 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
72 fBackingTexture = backingTexture;
73 }
robertphillips21048b52014-07-15 19:46:35 -070074
75private:
robertphillips261b8a92014-07-17 08:26:44 -070076 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070077 const GrCachedLayer* fLayer;
78};
79#endif
80
robertphillips4ec84da2014-06-24 13:10:43 -070081GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070082 : fContext(context) {
robertphillips320c9232014-07-29 06:07:19 -070083 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000084}
85
86GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070087
robertphillips3d533ac2014-07-20 09:40:00 -070088 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
89 for (; !iter.done(); ++iter) {
90 GrCachedLayer* layer = &(*iter);
robertphillips7bb9ed72014-10-10 11:38:29 -070091 SkASSERT(0 == layer->uses());
robertphillips3d533ac2014-07-20 09:40:00 -070092 this->unlock(layer);
93 SkDELETE(layer);
94 }
robertphillips952841b2014-06-30 08:26:50 -070095
robertphillipsb32f0ad2014-11-04 06:46:11 -080096 SkASSERT(0 == fPictureHash.count());
97
mtklein04c96952014-11-24 08:20:57 -080098 // The atlas only lets go of its texture when the atlas is deleted.
99 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000100}
101
robertphillips952841b2014-06-30 08:26:50 -0700102void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -0700103 SkASSERT(NULL == fAtlas.get());
robertphillips225a6272014-10-30 11:39:19 -0700104 GR_STATIC_ASSERT(kNumPlotsX*kNumPlotsX == GrPictureInfo::kNumPlots);
robertphillips@google.come930a072014-04-03 00:34:27 +0000105
robertphillips@google.come930a072014-04-03 00:34:27 +0000106 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -0700107 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
bsalomonf2703d82014-10-28 14:33:06 -0700108 kRenderTarget_GrSurfaceFlag,
robertphillips261b8a92014-07-17 08:26:44 -0700109 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000110}
111
112void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700113
robertphillips3d533ac2014-07-20 09:40:00 -0700114 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
115 for (; !iter.done(); ++iter) {
116 GrCachedLayer* layer = &(*iter);
117 this->unlock(layer);
118 SkDELETE(layer);
119 }
120 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700121
mtklein04c96952014-11-24 08:20:57 -0800122 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700123 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000124}
125
mtklein04c96952014-11-24 08:20:57 -0800126GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
127 int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800128 const SkIRect& srcIR,
129 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800130 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800131 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800132 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700133 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700134 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700135
robertphillips478dd722014-12-16 08:25:55 -0800136 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop,
137 srcIR, dstIR, initialMat,
robertphillips01d6e5f2014-12-01 09:09:27 -0800138 key, keySize, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700139 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000140 return layer;
141}
142
robertphillips01d6e5f2014-12-01 09:09:27 -0800143GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID, const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800144 const unsigned* key, int keySize) {
robertphillips01d6e5f2014-12-01 09:09:27 -0800145 SkASSERT(pictureID != SK_InvalidGenID);
146 return fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips4ec84da2014-06-24 13:10:43 -0700147}
robertphillips@google.come930a072014-04-03 00:34:27 +0000148
robertphillips6f294af2014-08-18 08:50:03 -0700149GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700150 int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800151 const SkIRect& srcIR,
152 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800153 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800154 const unsigned* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800155 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700156 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700157 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips01d6e5f2014-12-01 09:09:27 -0800158 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips@google.come930a072014-04-03 00:34:27 +0000159 if (NULL == layer) {
robertphillips478dd722014-12-16 08:25:55 -0800160 layer = this->createLayer(pictureID, start, stop,
161 srcIR, dstIR, initialMat,
162 key, keySize, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000163 }
robertphillips4ec84da2014-06-24 13:10:43 -0700164
robertphillips@google.come930a072014-04-03 00:34:27 +0000165 return layer;
166}
robertphillips4ec84da2014-06-24 13:10:43 -0700167
mtklein04c96952014-11-24 08:20:57 -0800168bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
169 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700170 bool* needsRendering) {
robertphillips4ab5a902014-10-29 13:56:02 -0700171 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700172
robertphillipsfd61ed02014-10-28 07:21:44 -0700173 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
robertphillipsa63f32e2014-11-10 08:10:42 -0800174 SkASSERT(0 == desc.fSampleCnt);
robertphillipsfd61ed02014-10-28 07:21:44 -0700175
robertphillips320c9232014-07-29 06:07:19 -0700176 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700177 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700178 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700179 SkASSERT(layer->isAtlased());
180 SkASSERT(layer->rect().width() == desc.fWidth);
181 SkASSERT(layer->rect().height() == desc.fHeight);
182 *needsRendering = false;
183 return true;
robertphillips952841b2014-06-30 08:26:50 -0700184 }
185
robertphillips320c9232014-07-29 06:07:19 -0700186 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700187 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700188 // Hooray it is still in the atlas - make sure it stays there
189 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700190 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700191 *needsRendering = false;
192 return true;
193 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700194 if (!fAtlas) {
195 this->initAtlas();
196 if (!fAtlas) {
197 return false;
198 }
199 }
robertphillips320c9232014-07-29 06:07:19 -0700200 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700201 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700202 if (NULL == pictInfo) {
203 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700204 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700205 }
206
207 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700208 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
209 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
210 desc.fWidth, desc.fHeight,
211 NULL, &loc);
212 // addToAtlas can allocate the backing texture
213 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700214 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700215#if !GR_CACHE_HOISTED_LAYERS
216 pictInfo->incPlotUsage(plot->id());
217#endif
robertphillips320c9232014-07-29 06:07:19 -0700218 // The layer was successfully added to the atlas
robertphillipse99d4992014-12-03 07:33:57 -0800219 const SkIRect bounds = SkIRect::MakeXYWH(loc.fX, loc.fY,
220 desc.fWidth, desc.fHeight);
robertphillips320c9232014-07-29 06:07:19 -0700221 layer->setTexture(fAtlas->getTexture(), bounds);
222 layer->setPlot(plot);
223 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700224 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700225 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700226 return true;
robertphillips320c9232014-07-29 06:07:19 -0700227 }
228
mtklein04c96952014-11-24 08:20:57 -0800229 // The layer was rejected by the atlas (even though we know it is
robertphillips320c9232014-07-29 06:07:19 -0700230 // plausibly atlas-able). See if a plot can be purged and try again.
231 if (!this->purgePlot()) {
232 break; // We weren't able to purge any plots
233 }
robertphillips261b8a92014-07-17 08:26:44 -0700234 }
robertphillipsbc0c4bd2014-12-01 11:39:59 -0800235
236 if (pictInfo->fPlotUsage.isEmpty()) {
237 fPictureHash.remove(pictInfo->fPictureID);
238 SkDELETE(pictInfo);
239 }
robertphillips952841b2014-06-30 08:26:50 -0700240 }
robertphillips952841b2014-06-30 08:26:50 -0700241
robertphillipsfd61ed02014-10-28 07:21:44 -0700242 return false;
243}
244
bsalomonf2703d82014-10-28 14:33:06 -0700245bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700246 if (layer->locked()) {
247 // This layer is already locked
248 *needsRendering = false;
249 return true;
250 }
251
robertphillips478dd722014-12-16 08:25:55 -0800252 // TODO: make the test for exact match depend on the image filters themselves
bsalomoneae62002015-07-31 13:59:30 -0700253 SkAutoTUnref<GrTexture> tex;
robertphillips478dd722014-12-16 08:25:55 -0800254 if (layer->fFilter) {
bsalomoneae62002015-07-31 13:59:30 -0700255 tex.reset(fContext->textureProvider()->createTexture(desc, true));
256 } else {
257 tex.reset(fContext->textureProvider()->createApproxTexture(desc));
robertphillips478dd722014-12-16 08:25:55 -0800258 }
259
robertphillipsfd61ed02014-10-28 07:21:44 -0700260 if (!tex) {
261 return false;
262 }
263
robertphillipse99d4992014-12-03 07:33:57 -0800264 layer->setTexture(tex, SkIRect::MakeWH(desc.fWidth, desc.fHeight));
robertphillips320c9232014-07-29 06:07:19 -0700265 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700266 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700267 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700268}
269
270void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700271 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700272
robertphillipsa32c6bc2014-10-09 12:47:01 -0700273 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700274 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700275 return;
276 }
277
robertphillips21048b52014-07-15 19:46:35 -0700278 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700279 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700280
robertphillips7bb9ed72014-10-10 11:38:29 -0700281 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700282 // At this point we could aggressively clear out un-locked plots but
283 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700284#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700285 // This testing code aggressively removes the atlased layers. This
286 // can be used to separate the performance contribution of less
287 // render target pingponging from that due to the re-use of cached layers
288 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700289 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700290
291 pictInfo->decPlotUsage(plotID);
292
293 if (0 == pictInfo->plotUsage(plotID)) {
294 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800295
296 if (pictInfo->fPlotUsage.isEmpty()) {
297 fPictureHash.remove(pictInfo->fPictureID);
298 SkDELETE(pictInfo);
299 }
robertphillips225a6272014-10-30 11:39:19 -0700300 }
mtklein04c96952014-11-24 08:20:57 -0800301
robertphillips0c423322014-07-31 11:02:38 -0700302 layer->setPlot(NULL);
robertphillipse99d4992014-12-03 07:33:57 -0800303 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips0c423322014-07-31 11:02:38 -0700304#endif
305
robertphillips21048b52014-07-15 19:46:35 -0700306 } else {
robertphillipse99d4992014-12-03 07:33:57 -0800307 layer->setTexture(NULL, SkIRect::MakeEmpty());
robertphillips952841b2014-06-30 08:26:50 -0700308 }
robertphillips320c9232014-07-29 06:07:19 -0700309
310 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700311}
312
robertphillips21048b52014-07-15 19:46:35 -0700313#ifdef SK_DEBUG
314void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700315 int plotLocks[kNumPlotsX * kNumPlotsY];
316 memset(plotLocks, 0, sizeof(plotLocks));
317
robertphillips3d533ac2014-07-20 09:40:00 -0700318 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
319 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700320 const GrCachedLayer* layer = &(*iter);
321
robertphillips4ab5a902014-10-29 13:56:02 -0700322 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700323
324 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700325 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700326 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700327 // layers should be non-atlased.
328 SkASSERT(!layer->isAtlased());
329 }
330
bsalomon49f085d2014-09-05 13:34:00 -0700331 if (layer->plot()) {
332 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700333 SkASSERT(pictInfo->fPictureID == layer->pictureID());
334
335 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700336#if !GR_CACHE_HOISTED_LAYERS
337 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
338#endif
robertphillips320c9232014-07-29 06:07:19 -0700339
340 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700341 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700342 }
mtklein04c96952014-11-24 08:20:57 -0800343 }
robertphillips320c9232014-07-29 06:07:19 -0700344 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700345
346 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
347 SkASSERT(plotLocks[i] == fPlotLocks[i]);
348 }
robertphillips21048b52014-07-15 19:46:35 -0700349}
350
351class GrAutoValidateCache : ::SkNoncopyable {
352public:
353 explicit GrAutoValidateCache(GrLayerCache* cache)
354 : fCache(cache) {
355 fCache->validate();
356 }
357 ~GrAutoValidateCache() {
358 fCache->validate();
359 }
360private:
361 GrLayerCache* fCache;
362};
363#endif
364
robertphillipsd771f6b2014-07-22 10:18:06 -0700365void GrLayerCache::purge(uint32_t pictureID) {
366
robertphillips21048b52014-07-15 19:46:35 -0700367 SkDEBUGCODE(GrAutoValidateCache avc(this);)
368
robertphillips3d533ac2014-07-20 09:40:00 -0700369 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700370 SkTDArray<GrCachedLayer*> toBeRemoved;
371
robertphillips3d533ac2014-07-20 09:40:00 -0700372 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
373 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700374 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700375 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700376 }
377 }
378
379 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700380 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700381 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700382 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700383 SkDELETE(toBeRemoved[i]);
384 }
robertphillips261b8a92014-07-17 08:26:44 -0700385
robertphillipsd771f6b2014-07-22 10:18:06 -0700386 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700387 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700388 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700389 SkDELETE(pictInfo);
390 }
robertphillips4ec84da2014-06-24 13:10:43 -0700391}
robertphillipsd771f6b2014-07-22 10:18:06 -0700392
robertphillips320c9232014-07-29 06:07:19 -0700393bool GrLayerCache::purgePlot() {
394 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700395 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700396
397 GrAtlas::PlotIter iter;
398 GrPlot* plot;
399 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700400 plot;
robertphillips320c9232014-07-29 06:07:19 -0700401 plot = iter.prev()) {
402 if (fPlotLocks[plot->id()] > 0) {
403 continue;
404 }
405
robertphillips6f294af2014-08-18 08:50:03 -0700406 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700407 return true;
408 }
409
410 return false;
411}
412
robertphillips6f294af2014-08-18 08:50:03 -0700413void GrLayerCache::purgePlot(GrPlot* plot) {
414 SkASSERT(0 == fPlotLocks[plot->id()]);
415
416 // We need to find all the layers in 'plot' and remove them.
417 SkTDArray<GrCachedLayer*> toBeRemoved;
418
419 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
420 for (; !iter.done(); ++iter) {
421 if (plot == (*iter).plot()) {
422 *toBeRemoved.append() = &(*iter);
423 }
424 }
425
426 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700427 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700428 SkASSERT(!toBeRemoved[i]->locked());
429
robertphillips410dd052014-10-06 12:19:50 -0700430 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700431
robertphillips410dd052014-10-06 12:19:50 -0700432 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700433 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
434 SkDELETE(toBeRemoved[i]);
435
robertphillips410dd052014-10-06 12:19:50 -0700436 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
437 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700438#if !GR_CACHE_HOISTED_LAYERS
439 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
440#endif
robertphillips410dd052014-10-06 12:19:50 -0700441 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
442
443 if (pictInfo->fPlotUsage.isEmpty()) {
444 fPictureHash.remove(pictInfo->fPictureID);
445 SkDELETE(pictInfo);
446 }
robertphillips6f294af2014-08-18 08:50:03 -0700447 }
448 }
449
450 plot->resetRects();
451}
452
robertphillips4ab5a902014-10-29 13:56:02 -0700453#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700454void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700455 if (!fAtlas) {
456 return;
457 }
458
robertphillips6f294af2014-08-18 08:50:03 -0700459 GrAtlas::PlotIter iter;
460 GrPlot* plot;
461 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700462 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700463 plot = iter.prev()) {
464 SkASSERT(0 == fPlotLocks[plot->id()]);
465
466 this->purgePlot(plot);
467 }
robertphillips4ab5a902014-10-29 13:56:02 -0700468
robertphillipsb32f0ad2014-11-04 06:46:11 -0800469 SkASSERT(0 == fPictureHash.count());
470
robertphillipsea461502015-05-26 11:38:03 -0700471 GrDrawContext* drawContext = fContext->drawContext();
472
473 if (drawContext) {
474 drawContext->discard(fAtlas->getTexture()->asRenderTarget());
475 }
robertphillips6f294af2014-08-18 08:50:03 -0700476}
robertphillips4ab5a902014-10-29 13:56:02 -0700477#endif
robertphillips6f294af2014-08-18 08:50:03 -0700478
robertphillipsd771f6b2014-07-22 10:18:06 -0700479void GrLayerCache::processDeletedPictures() {
bsalomon23e619c2015-02-06 11:54:28 -0800480 SkTArray<SkPicture::DeletionMessage> deletedPictures;
robertphillipsd771f6b2014-07-22 10:18:06 -0700481 fPictDeletionInbox.poll(&deletedPictures);
482
483 for (int i = 0; i < deletedPictures.count(); i++) {
mtklein04c96952014-11-24 08:20:57 -0800484 this->purge(deletedPictures[i].fUniqueID);
robertphillipsd771f6b2014-07-22 10:18:06 -0700485 }
486}
487
robertphillips84ac0822014-10-14 07:07:59 -0700488#ifdef SK_DEVELOPER
489void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
490
robertphillips4ab5a902014-10-29 13:56:02 -0700491 if (fAtlas) {
492 GrTexture* atlasTexture = fAtlas->getTexture();
493 if (NULL != atlasTexture) {
494 SkString fileName(dirName);
495 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700496
robertphillips4ab5a902014-10-29 13:56:02 -0700497 atlasTexture->surfacePriv().savePixels(fileName.c_str());
498 }
robertphillips84ac0822014-10-14 07:07:59 -0700499 }
500
501 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
502 for (; !iter.done(); ++iter) {
503 GrCachedLayer* layer = &(*iter);
504
505 if (layer->isAtlased() || !layer->texture()) {
506 continue;
507 }
508
509 SkString fileName(dirName);
robertphillips01d6e5f2014-12-01 09:09:27 -0800510 fileName.appendf("\\%d", layer->fKey.pictureID());
511 for (int i = 0; i < layer->fKey.keySize(); ++i) {
512 fileName.appendf("-%d", layer->fKey.key()[i]);
513 }
514 fileName.appendf(".png");
robertphillips84ac0822014-10-14 07:07:59 -0700515
516 layer->texture()->surfacePriv().savePixels(fileName.c_str());
517 }
518}
519#endif