blob: f33384c3e994db64a27604b5149c1b73c2d4db08 [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());
halcanary96fcdcc2015-08-27 07:41:13 -070028 SkASSERT(nullptr == 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) {
halcanary96fcdcc2015-08-27 07:41:13 -070071 SkASSERT(nullptr == fBackingTexture || fBackingTexture == backingTexture);
robertphillips261b8a92014-07-17 08:26:44 -070072 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);
halcanary385fe4d2015-08-26 13:07:48 -070093 delete layer;
robertphillips3d533ac2014-07-20 09:40:00 -070094 }
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() {
halcanary96fcdcc2015-08-27 07:41:13 -0700103 SkASSERT(nullptr == 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);
halcanary385fe4d2015-08-26 13:07:48 -0700107 fAtlas.reset(new GrAtlas(fContext->getGpu(), kSkia8888_GrPixelConfig,
108 kRenderTarget_GrSurfaceFlag, textureSize, kNumPlotsX, kNumPlotsY,
109 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);
halcanary385fe4d2015-08-26 13:07:48 -0700118 delete layer;
robertphillips3d533ac2014-07-20 09:40:00 -0700119 }
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,
mtkleinc6ad06a2015-08-19 09:51:00 -0700131 const int* 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
halcanary385fe4d2015-08-26 13:07:48 -0700136 GrCachedLayer* layer = new GrCachedLayer(pictureID, start, stop, srcIR, dstIR, initialMat, key,
137 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,
mtkleinc6ad06a2015-08-19 09:51:00 -0700143 const int* 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,
mtkleinc6ad06a2015-08-19 09:51:00 -0700153 const int* 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));
halcanary96fcdcc2015-08-27 07:41:13 -0700158 if (nullptr == 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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700170 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : nullptr, 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());
halcanary96fcdcc2015-08-27 07:41:13 -0700201 if (nullptr == pictInfo) {
halcanary385fe4d2015-08-26 13:07:48 -0700202 pictInfo = new 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,
halcanary96fcdcc2015-08-27 07:41:13 -0700210 nullptr, &loc);
robertphillips320c9232014-07-29 06:07:19 -0700211 // 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
mtkleinc6ad06a2015-08-19 09:51:00 -0700218 const SkIRect bounds = SkIRect::MakeXYWH(loc.fX, loc.fY,
robertphillipse99d4992014-12-03 07:33:57 -0800219 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);
halcanary385fe4d2015-08-26 13:07:48 -0700237 delete pictInfo;
robertphillipsbc0c4bd2014-12-01 11:39:59 -0800238 }
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
bsalomoneae62002015-07-31 13:59:30 -0700252 SkAutoTUnref<GrTexture> tex;
robertphillips478dd722014-12-16 08:25:55 -0800253 if (layer->fFilter) {
bsalomoneae62002015-07-31 13:59:30 -0700254 tex.reset(fContext->textureProvider()->createTexture(desc, true));
255 } else {
256 tex.reset(fContext->textureProvider()->createApproxTexture(desc));
robertphillips478dd722014-12-16 08:25:55 -0800257 }
258
robertphillipsfd61ed02014-10-28 07:21:44 -0700259 if (!tex) {
260 return false;
261 }
262
robertphillipse99d4992014-12-03 07:33:57 -0800263 layer->setTexture(tex, SkIRect::MakeWH(desc.fWidth, desc.fHeight));
robertphillips320c9232014-07-29 06:07:19 -0700264 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700265 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700266 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700267}
268
269void GrLayerCache::unlock(GrCachedLayer* layer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700270 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : nullptr, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700271
halcanary96fcdcc2015-08-27 07:41:13 -0700272 if (nullptr == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700273 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700274 return;
275 }
276
robertphillips21048b52014-07-15 19:46:35 -0700277 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700278 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700279
robertphillips7bb9ed72014-10-10 11:38:29 -0700280 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700281 // At this point we could aggressively clear out un-locked plots but
282 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700283#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700284 // This testing code aggressively removes the atlased layers. This
285 // can be used to separate the performance contribution of less
286 // render target pingponging from that due to the re-use of cached layers
287 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700288 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700289
290 pictInfo->decPlotUsage(plotID);
291
292 if (0 == pictInfo->plotUsage(plotID)) {
293 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800294
295 if (pictInfo->fPlotUsage.isEmpty()) {
296 fPictureHash.remove(pictInfo->fPictureID);
halcanary385fe4d2015-08-26 13:07:48 -0700297 delete pictInfo;
robertphillipsb32f0ad2014-11-04 06:46:11 -0800298 }
robertphillips225a6272014-10-30 11:39:19 -0700299 }
mtklein04c96952014-11-24 08:20:57 -0800300
halcanary96fcdcc2015-08-27 07:41:13 -0700301 layer->setPlot(nullptr);
302 layer->setTexture(nullptr, SkIRect::MakeEmpty());
robertphillips0c423322014-07-31 11:02:38 -0700303#endif
304
robertphillips21048b52014-07-15 19:46:35 -0700305 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700306 layer->setTexture(nullptr, SkIRect::MakeEmpty());
robertphillips952841b2014-06-30 08:26:50 -0700307 }
robertphillips320c9232014-07-29 06:07:19 -0700308
309 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700310}
311
robertphillips21048b52014-07-15 19:46:35 -0700312#ifdef SK_DEBUG
313void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700314 int plotLocks[kNumPlotsX * kNumPlotsY];
315 memset(plotLocks, 0, sizeof(plotLocks));
316
robertphillips3d533ac2014-07-20 09:40:00 -0700317 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
318 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700319 const GrCachedLayer* layer = &(*iter);
320
halcanary96fcdcc2015-08-27 07:41:13 -0700321 layer->validate(fAtlas.get() ? fAtlas->getTexture() : nullptr);
robertphillips320c9232014-07-29 06:07:19 -0700322
323 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700324 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700325 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700326 // layers should be non-atlased.
327 SkASSERT(!layer->isAtlased());
328 }
329
bsalomon49f085d2014-09-05 13:34:00 -0700330 if (layer->plot()) {
331 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700332 SkASSERT(pictInfo->fPictureID == layer->pictureID());
333
334 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700335#if !GR_CACHE_HOISTED_LAYERS
336 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
337#endif
robertphillips320c9232014-07-29 06:07:19 -0700338
339 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700340 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700341 }
mtklein04c96952014-11-24 08:20:57 -0800342 }
robertphillips320c9232014-07-29 06:07:19 -0700343 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700344
345 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
346 SkASSERT(plotLocks[i] == fPlotLocks[i]);
347 }
robertphillips21048b52014-07-15 19:46:35 -0700348}
349
350class GrAutoValidateCache : ::SkNoncopyable {
351public:
352 explicit GrAutoValidateCache(GrLayerCache* cache)
353 : fCache(cache) {
354 fCache->validate();
355 }
356 ~GrAutoValidateCache() {
357 fCache->validate();
358 }
359private:
360 GrLayerCache* fCache;
361};
362#endif
363
robertphillipsd771f6b2014-07-22 10:18:06 -0700364void GrLayerCache::purge(uint32_t pictureID) {
365
robertphillips21048b52014-07-15 19:46:35 -0700366 SkDEBUGCODE(GrAutoValidateCache avc(this);)
367
robertphillips3d533ac2014-07-20 09:40:00 -0700368 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700369 SkTDArray<GrCachedLayer*> toBeRemoved;
370
robertphillips3d533ac2014-07-20 09:40:00 -0700371 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
372 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700373 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700374 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700375 }
376 }
377
378 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700379 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700380 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700381 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
halcanary385fe4d2015-08-26 13:07:48 -0700382 delete toBeRemoved[i];
robertphillips952841b2014-06-30 08:26:50 -0700383 }
robertphillips261b8a92014-07-17 08:26:44 -0700384
robertphillipsd771f6b2014-07-22 10:18:06 -0700385 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700386 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700387 fPictureHash.remove(pictureID);
halcanary385fe4d2015-08-26 13:07:48 -0700388 delete pictInfo;
robertphillips261b8a92014-07-17 08:26:44 -0700389 }
robertphillips4ec84da2014-06-24 13:10:43 -0700390}
robertphillipsd771f6b2014-07-22 10:18:06 -0700391
robertphillips320c9232014-07-29 06:07:19 -0700392bool GrLayerCache::purgePlot() {
393 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700394 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700395
396 GrAtlas::PlotIter iter;
397 GrPlot* plot;
398 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700399 plot;
robertphillips320c9232014-07-29 06:07:19 -0700400 plot = iter.prev()) {
401 if (fPlotLocks[plot->id()] > 0) {
402 continue;
403 }
404
robertphillips6f294af2014-08-18 08:50:03 -0700405 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700406 return true;
407 }
408
409 return false;
410}
411
robertphillips6f294af2014-08-18 08:50:03 -0700412void GrLayerCache::purgePlot(GrPlot* plot) {
413 SkASSERT(0 == fPlotLocks[plot->id()]);
414
415 // We need to find all the layers in 'plot' and remove them.
416 SkTDArray<GrCachedLayer*> toBeRemoved;
417
418 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
419 for (; !iter.done(); ++iter) {
420 if (plot == (*iter).plot()) {
421 *toBeRemoved.append() = &(*iter);
422 }
423 }
424
425 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700426 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700427 SkASSERT(!toBeRemoved[i]->locked());
428
robertphillips410dd052014-10-06 12:19:50 -0700429 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700430
robertphillips410dd052014-10-06 12:19:50 -0700431 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700432 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
halcanary385fe4d2015-08-26 13:07:48 -0700433 delete toBeRemoved[i];
robertphillips6f294af2014-08-18 08:50:03 -0700434
robertphillips410dd052014-10-06 12:19:50 -0700435 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
436 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700437#if !GR_CACHE_HOISTED_LAYERS
438 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
439#endif
robertphillips410dd052014-10-06 12:19:50 -0700440 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
441
442 if (pictInfo->fPlotUsage.isEmpty()) {
443 fPictureHash.remove(pictInfo->fPictureID);
halcanary385fe4d2015-08-26 13:07:48 -0700444 delete pictInfo;
robertphillips410dd052014-10-06 12:19:50 -0700445 }
robertphillips6f294af2014-08-18 08:50:03 -0700446 }
447 }
448
449 plot->resetRects();
450}
451
robertphillips4ab5a902014-10-29 13:56:02 -0700452#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700453void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700454 if (!fAtlas) {
455 return;
456 }
457
robertphillips6f294af2014-08-18 08:50:03 -0700458 GrAtlas::PlotIter iter;
459 GrPlot* plot;
460 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700461 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700462 plot = iter.prev()) {
463 SkASSERT(0 == fPlotLocks[plot->id()]);
464
465 this->purgePlot(plot);
466 }
robertphillips4ab5a902014-10-29 13:56:02 -0700467
robertphillipsb32f0ad2014-11-04 06:46:11 -0800468 SkASSERT(0 == fPictureHash.count());
469
robertphillipsea461502015-05-26 11:38:03 -0700470 GrDrawContext* drawContext = fContext->drawContext();
471
472 if (drawContext) {
473 drawContext->discard(fAtlas->getTexture()->asRenderTarget());
474 }
robertphillips6f294af2014-08-18 08:50:03 -0700475}
robertphillips4ab5a902014-10-29 13:56:02 -0700476#endif
robertphillips6f294af2014-08-18 08:50:03 -0700477
robertphillipsd771f6b2014-07-22 10:18:06 -0700478void GrLayerCache::processDeletedPictures() {
bsalomon23e619c2015-02-06 11:54:28 -0800479 SkTArray<SkPicture::DeletionMessage> deletedPictures;
robertphillipsd771f6b2014-07-22 10:18:06 -0700480 fPictDeletionInbox.poll(&deletedPictures);
481
482 for (int i = 0; i < deletedPictures.count(); i++) {
mtklein04c96952014-11-24 08:20:57 -0800483 this->purge(deletedPictures[i].fUniqueID);
robertphillipsd771f6b2014-07-22 10:18:06 -0700484 }
485}
486
robertphillips84ac0822014-10-14 07:07:59 -0700487#ifdef SK_DEVELOPER
488void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
489
robertphillips4ab5a902014-10-29 13:56:02 -0700490 if (fAtlas) {
491 GrTexture* atlasTexture = fAtlas->getTexture();
halcanary96fcdcc2015-08-27 07:41:13 -0700492 if (nullptr != atlasTexture) {
robertphillips4ab5a902014-10-29 13:56:02 -0700493 SkString fileName(dirName);
494 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700495
robertphillips4ab5a902014-10-29 13:56:02 -0700496 atlasTexture->surfacePriv().savePixels(fileName.c_str());
497 }
robertphillips84ac0822014-10-14 07:07:59 -0700498 }
499
500 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
501 for (; !iter.done(); ++iter) {
502 GrCachedLayer* layer = &(*iter);
503
504 if (layer->isAtlased() || !layer->texture()) {
505 continue;
506 }
507
508 SkString fileName(dirName);
robertphillips01d6e5f2014-12-01 09:09:27 -0800509 fileName.appendf("\\%d", layer->fKey.pictureID());
510 for (int i = 0; i < layer->fKey.keySize(); ++i) {
511 fileName.appendf("-%d", layer->fKey.key()[i]);
512 }
513 fileName.appendf(".png");
robertphillips84ac0822014-10-14 07:07:59 -0700514
515 layer->texture()->surfacePriv().savePixels(fileName.c_str());
516 }
517}
518#endif