blob: b14423aaa27fc313d82d3b81b4d6494ed197cae9 [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
robertphillipse51fa4a2015-11-03 07:04:47 -08008#include "GrLayerAtlas.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
robertphillips60029a52015-11-09 13:51:06 -080030 SkASSERT(!fAtlased); // can't be atlased if it doesn't have a texture
robertphillips261b8a92014-07-17 08:26:44 -070031 }
32
bsalomon49f085d2014-09-05 13:34:00 -070033 if (fPlot) {
robertphillips60029a52015-11-09 13:51:06 -080034 SkASSERT(fAtlased);
robertphillips261b8a92014-07-17 08:26:44 -070035 // If a layer has a plot (i.e., is atlased) then it must point to
36 // the backing texture. Additionally, its rect should be non-empty.
bsalomon49f085d2014-09-05 13:34:00 -070037 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070038 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070039 }
robertphillips320c9232014-07-29 06:07:19 -070040
41 if (fLocked) {
42 // If a layer is locked it must have a texture (though it need not be
43 // the atlas-backing texture) and occupy some space.
bsalomon49f085d2014-09-05 13:34:00 -070044 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070045 SkASSERT(!fRect.isEmpty());
46 }
robertphillips7bb9ed72014-10-10 11:38:29 -070047
48 // Unfortunately there is a brief time where a layer can be locked
49 // but not used, so we can only check the "used implies locked"
50 // invariant.
51 if (fUses > 0) {
52 SkASSERT(fLocked);
53 } else {
54 SkASSERT(0 == fUses);
55 }
robertphillips21048b52014-07-15 19:46:35 -070056}
57
58class GrAutoValidateLayer : ::SkNoncopyable {
59public:
mtklein04c96952014-11-24 08:20:57 -080060 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
robertphillips21048b52014-07-15 19:46:35 -070061 : fBackingTexture(backingTexture)
62 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070063 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070064 fLayer->validate(backingTexture);
65 }
66 }
67 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070068 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070069 fLayer->validate(fBackingTexture);
70 }
71 }
robertphillips261b8a92014-07-17 08:26:44 -070072 void setBackingTexture(GrTexture* backingTexture) {
halcanary96fcdcc2015-08-27 07:41:13 -070073 SkASSERT(nullptr == fBackingTexture || fBackingTexture == backingTexture);
robertphillips261b8a92014-07-17 08:26:44 -070074 fBackingTexture = backingTexture;
75 }
robertphillips21048b52014-07-15 19:46:35 -070076
77private:
robertphillips261b8a92014-07-17 08:26:44 -070078 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070079 const GrCachedLayer* fLayer;
80};
81#endif
82
robertphillips4ec84da2014-06-24 13:10:43 -070083GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070084 : fContext(context) {
robertphillips320c9232014-07-29 06:07:19 -070085 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000086}
87
88GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070089
robertphillips3d533ac2014-07-20 09:40:00 -070090 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
91 for (; !iter.done(); ++iter) {
92 GrCachedLayer* layer = &(*iter);
robertphillips7bb9ed72014-10-10 11:38:29 -070093 SkASSERT(0 == layer->uses());
robertphillips3d533ac2014-07-20 09:40:00 -070094 this->unlock(layer);
halcanary385fe4d2015-08-26 13:07:48 -070095 delete layer;
robertphillips3d533ac2014-07-20 09:40:00 -070096 }
robertphillips952841b2014-06-30 08:26:50 -070097
robertphillipsb32f0ad2014-11-04 06:46:11 -080098 SkASSERT(0 == fPictureHash.count());
99
mtklein04c96952014-11-24 08:20:57 -0800100 // The atlas only lets go of its texture when the atlas is deleted.
mtklein852f15d2016-03-17 10:51:27 -0700101 fAtlas.reset();
robertphillips@google.come930a072014-04-03 00:34:27 +0000102}
103
robertphillips952841b2014-06-30 08:26:50 -0700104void GrLayerCache::initAtlas() {
halcanary96fcdcc2015-08-27 07:41:13 -0700105 SkASSERT(nullptr == fAtlas.get());
robertphillips225a6272014-10-30 11:39:19 -0700106 GR_STATIC_ASSERT(kNumPlotsX*kNumPlotsX == GrPictureInfo::kNumPlots);
robertphillips@google.come930a072014-04-03 00:34:27 +0000107
robertphillips@google.come930a072014-04-03 00:34:27 +0000108 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillipse51fa4a2015-11-03 07:04:47 -0800109 fAtlas.reset(new GrLayerAtlas(fContext->textureProvider(), kSkia8888_GrPixelConfig,
110 kRenderTarget_GrSurfaceFlag, textureSize,
111 kNumPlotsX, kNumPlotsY));
robertphillips@google.come930a072014-04-03 00:34:27 +0000112}
113
114void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700115
robertphillips3d533ac2014-07-20 09:40:00 -0700116 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
117 for (; !iter.done(); ++iter) {
118 GrCachedLayer* layer = &(*iter);
119 this->unlock(layer);
halcanary385fe4d2015-08-26 13:07:48 -0700120 delete layer;
robertphillips3d533ac2014-07-20 09:40:00 -0700121 }
122 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700123
robertphillips60029a52015-11-09 13:51:06 -0800124 if (fAtlas) {
125 fAtlas->resetPlots();
126 fAtlas->detachBackingTexture();
127 }
robertphillips@google.come930a072014-04-03 00:34:27 +0000128}
129
mtklein04c96952014-11-24 08:20:57 -0800130GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
131 int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800132 const SkIRect& srcIR,
133 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800134 const SkMatrix& initialMat,
mtkleinc6ad06a2015-08-19 09:51:00 -0700135 const int* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800136 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700137 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700138 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700139
halcanary385fe4d2015-08-26 13:07:48 -0700140 GrCachedLayer* layer = new GrCachedLayer(pictureID, start, stop, srcIR, dstIR, initialMat, key,
141 keySize, paint);
robertphillips3d533ac2014-07-20 09:40:00 -0700142 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000143 return layer;
144}
145
robertphillips01d6e5f2014-12-01 09:09:27 -0800146GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID, const SkMatrix& initialMat,
mtkleinc6ad06a2015-08-19 09:51:00 -0700147 const int* key, int keySize) {
robertphillips01d6e5f2014-12-01 09:09:27 -0800148 SkASSERT(pictureID != SK_InvalidGenID);
149 return fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
robertphillips4ec84da2014-06-24 13:10:43 -0700150}
robertphillips@google.come930a072014-04-03 00:34:27 +0000151
robertphillips6f294af2014-08-18 08:50:03 -0700152GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700153 int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800154 const SkIRect& srcIR,
155 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800156 const SkMatrix& initialMat,
mtkleinc6ad06a2015-08-19 09:51:00 -0700157 const int* key,
robertphillips01d6e5f2014-12-01 09:09:27 -0800158 int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700159 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700160 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips01d6e5f2014-12-01 09:09:27 -0800161 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize));
halcanary96fcdcc2015-08-27 07:41:13 -0700162 if (nullptr == layer) {
robertphillips478dd722014-12-16 08:25:55 -0800163 layer = this->createLayer(pictureID, start, stop,
164 srcIR, dstIR, initialMat,
165 key, keySize, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000166 }
robertphillips4ec84da2014-06-24 13:10:43 -0700167
robertphillips@google.come930a072014-04-03 00:34:27 +0000168 return layer;
169}
robertphillips4ec84da2014-06-24 13:10:43 -0700170
mtklein04c96952014-11-24 08:20:57 -0800171bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
172 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700173 bool* needsRendering) {
robertphillips60029a52015-11-09 13:51:06 -0800174 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTextureOrNull() : nullptr, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700175
robertphillipsfd61ed02014-10-28 07:21:44 -0700176 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
robertphillipsa63f32e2014-11-10 08:10:42 -0800177 SkASSERT(0 == desc.fSampleCnt);
robertphillipsfd61ed02014-10-28 07:21:44 -0700178
robertphillips320c9232014-07-29 06:07:19 -0700179 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700180 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700181 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700182 SkASSERT(layer->isAtlased());
183 SkASSERT(layer->rect().width() == desc.fWidth);
184 SkASSERT(layer->rect().height() == desc.fHeight);
185 *needsRendering = false;
186 return true;
robertphillips952841b2014-06-30 08:26:50 -0700187 }
188
robertphillips320c9232014-07-29 06:07:19 -0700189 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700190 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700191 // Hooray it is still in the atlas - make sure it stays there
192 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700193 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700194 *needsRendering = false;
195 return true;
196 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700197 if (!fAtlas) {
198 this->initAtlas();
199 if (!fAtlas) {
200 return false;
201 }
202 }
robertphillips320c9232014-07-29 06:07:19 -0700203 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700204 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
halcanary96fcdcc2015-08-27 07:41:13 -0700205 if (nullptr == pictInfo) {
halcanary385fe4d2015-08-26 13:07:48 -0700206 pictInfo = new GrPictureInfo(layer->pictureID());
robertphillips3d533ac2014-07-20 09:40:00 -0700207 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700208 }
209
210 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700211 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
robertphillipse51fa4a2015-11-03 07:04:47 -0800212 GrLayerAtlas::Plot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
213 desc.fWidth, desc.fHeight,
214 &loc);
robertphillips320c9232014-07-29 06:07:19 -0700215 // addToAtlas can allocate the backing texture
216 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700217 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700218#if !GR_CACHE_HOISTED_LAYERS
219 pictInfo->incPlotUsage(plot->id());
220#endif
robertphillips320c9232014-07-29 06:07:19 -0700221 // The layer was successfully added to the atlas
mtkleinc6ad06a2015-08-19 09:51:00 -0700222 const SkIRect bounds = SkIRect::MakeXYWH(loc.fX, loc.fY,
robertphillipse99d4992014-12-03 07:33:57 -0800223 desc.fWidth, desc.fHeight);
robertphillips60029a52015-11-09 13:51:06 -0800224 layer->setTexture(fAtlas->getTexture(), bounds, true);
robertphillips320c9232014-07-29 06:07:19 -0700225 layer->setPlot(plot);
226 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700227 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700228 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700229 return true;
robertphillips320c9232014-07-29 06:07:19 -0700230 }
231
mtklein04c96952014-11-24 08:20:57 -0800232 // The layer was rejected by the atlas (even though we know it is
robertphillips320c9232014-07-29 06:07:19 -0700233 // plausibly atlas-able). See if a plot can be purged and try again.
robertphillips60029a52015-11-09 13:51:06 -0800234 if (!this->purgePlots(true)) {
robertphillips320c9232014-07-29 06:07:19 -0700235 break; // We weren't able to purge any plots
236 }
robertphillips261b8a92014-07-17 08:26:44 -0700237 }
robertphillipsbc0c4bd2014-12-01 11:39:59 -0800238
239 if (pictInfo->fPlotUsage.isEmpty()) {
240 fPictureHash.remove(pictInfo->fPictureID);
halcanary385fe4d2015-08-26 13:07:48 -0700241 delete pictInfo;
robertphillipsbc0c4bd2014-12-01 11:39:59 -0800242 }
robertphillips952841b2014-06-30 08:26:50 -0700243 }
robertphillips952841b2014-06-30 08:26:50 -0700244
robertphillipsfd61ed02014-10-28 07:21:44 -0700245 return false;
246}
247
bsalomonf2703d82014-10-28 14:33:06 -0700248bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700249 if (layer->locked()) {
250 // This layer is already locked
251 *needsRendering = false;
252 return true;
253 }
254
robertphillips478dd722014-12-16 08:25:55 -0800255 // TODO: make the test for exact match depend on the image filters themselves
bsalomoneae62002015-07-31 13:59:30 -0700256 SkAutoTUnref<GrTexture> tex;
robertphillips478dd722014-12-16 08:25:55 -0800257 if (layer->fFilter) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800258 tex.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -0700259 } else {
260 tex.reset(fContext->textureProvider()->createApproxTexture(desc));
robertphillips478dd722014-12-16 08:25:55 -0800261 }
262
robertphillipsfd61ed02014-10-28 07:21:44 -0700263 if (!tex) {
264 return false;
265 }
266
robertphillips60029a52015-11-09 13:51:06 -0800267 layer->setTexture(tex, SkIRect::MakeWH(desc.fWidth, desc.fHeight), false);
robertphillips320c9232014-07-29 06:07:19 -0700268 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700269 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700270 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700271}
272
273void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips60029a52015-11-09 13:51:06 -0800274 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTextureOrNull() : nullptr, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700275
halcanary96fcdcc2015-08-27 07:41:13 -0700276 if (nullptr == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700277 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700278 return;
279 }
280
robertphillips21048b52014-07-15 19:46:35 -0700281 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700282 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700283
robertphillips7bb9ed72014-10-10 11:38:29 -0700284 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700285 // At this point we could aggressively clear out un-locked plots but
286 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700287#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700288 // This testing code aggressively removes the atlased layers. This
289 // can be used to separate the performance contribution of less
290 // render target pingponging from that due to the re-use of cached layers
291 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700292 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700293
294 pictInfo->decPlotUsage(plotID);
295
296 if (0 == pictInfo->plotUsage(plotID)) {
robertphillipse51fa4a2015-11-03 07:04:47 -0800297 pictInfo->fPlotUsage.removePlot(layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800298
299 if (pictInfo->fPlotUsage.isEmpty()) {
300 fPictureHash.remove(pictInfo->fPictureID);
halcanary385fe4d2015-08-26 13:07:48 -0700301 delete pictInfo;
robertphillipsb32f0ad2014-11-04 06:46:11 -0800302 }
robertphillips225a6272014-10-30 11:39:19 -0700303 }
mtklein04c96952014-11-24 08:20:57 -0800304
halcanary96fcdcc2015-08-27 07:41:13 -0700305 layer->setPlot(nullptr);
robertphillips60029a52015-11-09 13:51:06 -0800306 layer->setTexture(nullptr, SkIRect::MakeEmpty(), false);
robertphillips0c423322014-07-31 11:02:38 -0700307#endif
308
robertphillips21048b52014-07-15 19:46:35 -0700309 } else {
robertphillips60029a52015-11-09 13:51:06 -0800310 layer->setTexture(nullptr, SkIRect::MakeEmpty(), false);
robertphillips952841b2014-06-30 08:26:50 -0700311 }
robertphillips320c9232014-07-29 06:07:19 -0700312
313 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700314}
315
robertphillips21048b52014-07-15 19:46:35 -0700316#ifdef SK_DEBUG
317void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700318 int plotLocks[kNumPlotsX * kNumPlotsY];
319 memset(plotLocks, 0, sizeof(plotLocks));
320
robertphillips3d533ac2014-07-20 09:40:00 -0700321 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
322 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700323 const GrCachedLayer* layer = &(*iter);
324
robertphillips60029a52015-11-09 13:51:06 -0800325 layer->validate(fAtlas.get() ? fAtlas->getTextureOrNull() : nullptr);
robertphillips320c9232014-07-29 06:07:19 -0700326
327 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700328 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700329 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700330 // layers should be non-atlased.
331 SkASSERT(!layer->isAtlased());
332 }
333
bsalomon49f085d2014-09-05 13:34:00 -0700334 if (layer->plot()) {
335 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700336 SkASSERT(pictInfo->fPictureID == layer->pictureID());
337
338 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700339#if !GR_CACHE_HOISTED_LAYERS
340 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
341#endif
robertphillips320c9232014-07-29 06:07:19 -0700342
343 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700344 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700345 }
mtklein04c96952014-11-24 08:20:57 -0800346 }
robertphillips320c9232014-07-29 06:07:19 -0700347 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700348
349 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
350 SkASSERT(plotLocks[i] == fPlotLocks[i]);
351 }
robertphillips21048b52014-07-15 19:46:35 -0700352}
353
354class GrAutoValidateCache : ::SkNoncopyable {
355public:
356 explicit GrAutoValidateCache(GrLayerCache* cache)
357 : fCache(cache) {
358 fCache->validate();
359 }
360 ~GrAutoValidateCache() {
361 fCache->validate();
362 }
363private:
364 GrLayerCache* fCache;
365};
366#endif
367
robertphillipsd771f6b2014-07-22 10:18:06 -0700368void GrLayerCache::purge(uint32_t pictureID) {
369
robertphillips21048b52014-07-15 19:46:35 -0700370 SkDEBUGCODE(GrAutoValidateCache avc(this);)
371
robertphillips3d533ac2014-07-20 09:40:00 -0700372 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700373 SkTDArray<GrCachedLayer*> toBeRemoved;
374
robertphillips3d533ac2014-07-20 09:40:00 -0700375 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
376 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700377 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700378 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700379 }
380 }
381
382 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700383 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700384 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700385 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
halcanary385fe4d2015-08-26 13:07:48 -0700386 delete toBeRemoved[i];
robertphillips952841b2014-06-30 08:26:50 -0700387 }
robertphillips261b8a92014-07-17 08:26:44 -0700388
robertphillipsd771f6b2014-07-22 10:18:06 -0700389 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700390 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700391 fPictureHash.remove(pictureID);
halcanary385fe4d2015-08-26 13:07:48 -0700392 delete pictInfo;
robertphillips261b8a92014-07-17 08:26:44 -0700393 }
robertphillips4ec84da2014-06-24 13:10:43 -0700394}
robertphillipsd771f6b2014-07-22 10:18:06 -0700395
robertphillips60029a52015-11-09 13:51:06 -0800396bool GrLayerCache::purgePlots(bool justOne) {
robertphillips320c9232014-07-29 06:07:19 -0700397 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700398 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700399
robertphillips60029a52015-11-09 13:51:06 -0800400 bool anyPurged = false;
robertphillipse51fa4a2015-11-03 07:04:47 -0800401 GrLayerAtlas::PlotIter iter;
402 GrLayerAtlas::Plot* plot;
403 for (plot = fAtlas->iterInit(&iter, GrLayerAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700404 plot;
robertphillips320c9232014-07-29 06:07:19 -0700405 plot = iter.prev()) {
406 if (fPlotLocks[plot->id()] > 0) {
407 continue;
408 }
409
robertphillips60029a52015-11-09 13:51:06 -0800410 anyPurged = true;
robertphillips6f294af2014-08-18 08:50:03 -0700411 this->purgePlot(plot);
robertphillips60029a52015-11-09 13:51:06 -0800412 if (justOne) {
413 break;
414 }
robertphillips320c9232014-07-29 06:07:19 -0700415 }
416
robertphillips60029a52015-11-09 13:51:06 -0800417 return anyPurged;
robertphillips320c9232014-07-29 06:07:19 -0700418}
419
robertphillipse51fa4a2015-11-03 07:04:47 -0800420void GrLayerCache::purgePlot(GrLayerAtlas::Plot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700421 SkASSERT(0 == fPlotLocks[plot->id()]);
422
423 // We need to find all the layers in 'plot' and remove them.
424 SkTDArray<GrCachedLayer*> toBeRemoved;
425
426 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
427 for (; !iter.done(); ++iter) {
428 if (plot == (*iter).plot()) {
429 *toBeRemoved.append() = &(*iter);
430 }
431 }
432
433 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700434 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700435 SkASSERT(!toBeRemoved[i]->locked());
436
robertphillips410dd052014-10-06 12:19:50 -0700437 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700438
robertphillips410dd052014-10-06 12:19:50 -0700439 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700440 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
halcanary385fe4d2015-08-26 13:07:48 -0700441 delete toBeRemoved[i];
robertphillips6f294af2014-08-18 08:50:03 -0700442
robertphillips410dd052014-10-06 12:19:50 -0700443 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
444 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700445#if !GR_CACHE_HOISTED_LAYERS
446 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
447#endif
robertphillipse51fa4a2015-11-03 07:04:47 -0800448 pictInfo->fPlotUsage.removePlot(plot);
robertphillips410dd052014-10-06 12:19:50 -0700449
450 if (pictInfo->fPlotUsage.isEmpty()) {
451 fPictureHash.remove(pictInfo->fPictureID);
halcanary385fe4d2015-08-26 13:07:48 -0700452 delete pictInfo;
robertphillips410dd052014-10-06 12:19:50 -0700453 }
robertphillips6f294af2014-08-18 08:50:03 -0700454 }
455 }
456
robertphillipse51fa4a2015-11-03 07:04:47 -0800457 plot->reset();
robertphillips6f294af2014-08-18 08:50:03 -0700458}
459
robertphillips4ab5a902014-10-29 13:56:02 -0700460#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700461void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700462 if (!fAtlas) {
463 return;
464 }
465
robertphillips60029a52015-11-09 13:51:06 -0800466 this->purgePlots(false); // clear them all out
robertphillips4ab5a902014-10-29 13:56:02 -0700467
robertphillipsb32f0ad2014-11-04 06:46:11 -0800468 SkASSERT(0 == fPictureHash.count());
469
robertphillips60029a52015-11-09 13:51:06 -0800470 if (fAtlas->getTextureOrNull()) {
robertphillips6c7e3252016-04-27 10:47:51 -0700471 sk_sp<GrDrawContext> drawContext(
472 fContext->drawContext(sk_ref_sp(fAtlas->getTexture()->asRenderTarget())));
robertphillipsea461502015-05-26 11:38:03 -0700473
robertphillips60029a52015-11-09 13:51:06 -0800474 if (drawContext) {
475 drawContext->discard();
476 }
robertphillipsea461502015-05-26 11:38:03 -0700477 }
robertphillips6f294af2014-08-18 08:50:03 -0700478}
robertphillips4ab5a902014-10-29 13:56:02 -0700479#endif
robertphillips6f294af2014-08-18 08:50:03 -0700480
robertphillips60029a52015-11-09 13:51:06 -0800481void GrLayerCache::begin() {
482 if (!fAtlas) {
483 return;
484 }
485
486 if (!fAtlas->reattachBackingTexture()) {
487 // We weren't able to re-attach. Clear out all the atlased layers.
488 this->purgePlots(false);
489 SkASSERT(0 == fPictureHash.count());
490 }
491#ifdef SK_DEBUG
492 else {
493 // we've reattached - everything had better make sense
494 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
495 for (; !iter.done(); ++iter) {
496 GrCachedLayer* layer = &(*iter);
497
498 if (layer->isAtlased()) {
499 SkASSERT(fAtlas->getTexture() == layer->texture());
500 }
501 }
502 }
503#endif
504}
505
506void GrLayerCache::end() {
507 if (!fAtlas) {
508 return;
509 }
510
511 // Adding this call will clear out all the layers in the atlas
512 //this->purgePlots(false);
513
514 fAtlas->detachBackingTexture();
515}
516
robertphillipsd771f6b2014-07-22 10:18:06 -0700517void GrLayerCache::processDeletedPictures() {
bsalomon23e619c2015-02-06 11:54:28 -0800518 SkTArray<SkPicture::DeletionMessage> deletedPictures;
robertphillipsd771f6b2014-07-22 10:18:06 -0700519 fPictDeletionInbox.poll(&deletedPictures);
520
521 for (int i = 0; i < deletedPictures.count(); i++) {
mtklein04c96952014-11-24 08:20:57 -0800522 this->purge(deletedPictures[i].fUniqueID);
robertphillipsd771f6b2014-07-22 10:18:06 -0700523 }
524}
525
djsollenefe46d22016-04-29 06:41:35 -0700526#ifdef SK_DEBUG
robertphillips84ac0822014-10-14 07:07:59 -0700527void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
528
robertphillips4ab5a902014-10-29 13:56:02 -0700529 if (fAtlas) {
robertphillips60029a52015-11-09 13:51:06 -0800530 GrTexture* atlasTexture = fAtlas->getTextureOrNull();
halcanary96fcdcc2015-08-27 07:41:13 -0700531 if (nullptr != atlasTexture) {
robertphillips4ab5a902014-10-29 13:56:02 -0700532 SkString fileName(dirName);
533 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700534
robertphillips4ab5a902014-10-29 13:56:02 -0700535 atlasTexture->surfacePriv().savePixels(fileName.c_str());
536 }
robertphillips84ac0822014-10-14 07:07:59 -0700537 }
538
539 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
540 for (; !iter.done(); ++iter) {
541 GrCachedLayer* layer = &(*iter);
542
543 if (layer->isAtlased() || !layer->texture()) {
544 continue;
545 }
546
547 SkString fileName(dirName);
robertphillips01d6e5f2014-12-01 09:09:27 -0800548 fileName.appendf("\\%d", layer->fKey.pictureID());
549 for (int i = 0; i < layer->fKey.keySize(); ++i) {
550 fileName.appendf("-%d", layer->fKey.key()[i]);
551 }
552 fileName.appendf(".png");
robertphillips84ac0822014-10-14 07:07:59 -0700553
554 layer->texture()->surfacePriv().savePixels(fileName.c_str());
555 }
556}
557#endif