blob: 6b176753987c6634830a2b20ee55dc7c6c9029a3 [file] [log] [blame]
robertphillips@google.come930a072014-04-03 00:34:27 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrAtlas.h"
9#include "GrGpu.h"
10#include "GrLayerCache.h"
robertphillips84ac0822014-10-14 07:07:59 -070011#include "GrSurfacePriv.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000012
robertphillips21048b52014-07-15 19:46:35 -070013#ifdef SK_DEBUG
robertphillips261b8a92014-07-17 08:26:44 -070014void GrCachedLayer::validate(const GrTexture* backingTexture) const {
robertphillips0c423322014-07-31 11:02:38 -070015 SkASSERT(SK_InvalidGenID != fKey.pictureID());
robertphillips410dd052014-10-06 12:19:50 -070016 SkASSERT(fKey.start() >= 0);
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,
robertphillips3aac6e02014-10-20 08:52:40 -0700127 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700128 const SkMatrix& ctm,
129 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700130 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700131
robertphillips3aac6e02014-10-20 08:52:40 -0700132 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, bounds, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700133 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000134 return layer;
135}
136
robertphillips6f294af2014-08-18 08:50:03 -0700137GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
mtklein04c96952014-11-24 08:20:57 -0800138 int start,
robertphillips3aac6e02014-10-20 08:52:40 -0700139 const SkIRect& bounds,
robertphillips0c423322014-07-31 11:02:38 -0700140 const SkMatrix& ctm) {
robertphillipsed420592014-09-29 11:39:38 -0700141 SkASSERT(pictureID != SK_InvalidGenID && start > 0);
robertphillips3aac6e02014-10-20 08:52:40 -0700142 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700143}
robertphillips@google.come930a072014-04-03 00:34:27 +0000144
robertphillips6f294af2014-08-18 08:50:03 -0700145GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700146 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700147 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700148 const SkMatrix& ctm,
149 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700150 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips3aac6e02014-10-20 08:52:40 -0700151 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000152 if (NULL == layer) {
robertphillips3aac6e02014-10-20 08:52:40 -0700153 layer = this->createLayer(pictureID, start, stop, bounds, ctm, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000154 }
robertphillips4ec84da2014-06-24 13:10:43 -0700155
robertphillips@google.come930a072014-04-03 00:34:27 +0000156 return layer;
157}
robertphillips4ec84da2014-06-24 13:10:43 -0700158
mtklein04c96952014-11-24 08:20:57 -0800159bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
160 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700161 bool* needsRendering) {
robertphillips4ab5a902014-10-29 13:56:02 -0700162 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700163
robertphillipsfd61ed02014-10-28 07:21:44 -0700164 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
robertphillipsa63f32e2014-11-10 08:10:42 -0800165 SkASSERT(0 == desc.fSampleCnt);
robertphillipsfd61ed02014-10-28 07:21:44 -0700166
robertphillips320c9232014-07-29 06:07:19 -0700167 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700168 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700169 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700170 SkASSERT(layer->isAtlased());
171 SkASSERT(layer->rect().width() == desc.fWidth);
172 SkASSERT(layer->rect().height() == desc.fHeight);
173 *needsRendering = false;
174 return true;
robertphillips952841b2014-06-30 08:26:50 -0700175 }
176
robertphillips320c9232014-07-29 06:07:19 -0700177 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700178 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700179 // Hooray it is still in the atlas - make sure it stays there
180 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700181 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700182 *needsRendering = false;
183 return true;
184 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700185 if (!fAtlas) {
186 this->initAtlas();
187 if (!fAtlas) {
188 return false;
189 }
190 }
robertphillips320c9232014-07-29 06:07:19 -0700191 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700192 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700193 if (NULL == pictInfo) {
194 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700195 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700196 }
197
198 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700199 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
200 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
201 desc.fWidth, desc.fHeight,
202 NULL, &loc);
203 // addToAtlas can allocate the backing texture
204 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700205 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700206#if !GR_CACHE_HOISTED_LAYERS
207 pictInfo->incPlotUsage(plot->id());
208#endif
robertphillips320c9232014-07-29 06:07:19 -0700209 // The layer was successfully added to the atlas
210 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700211 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700212 SkToS16(desc.fHeight));
213 layer->setTexture(fAtlas->getTexture(), bounds);
214 layer->setPlot(plot);
215 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700216 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700217 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700218 return true;
robertphillips320c9232014-07-29 06:07:19 -0700219 }
220
mtklein04c96952014-11-24 08:20:57 -0800221 // The layer was rejected by the atlas (even though we know it is
robertphillips320c9232014-07-29 06:07:19 -0700222 // plausibly atlas-able). See if a plot can be purged and try again.
223 if (!this->purgePlot()) {
224 break; // We weren't able to purge any plots
225 }
robertphillips261b8a92014-07-17 08:26:44 -0700226 }
robertphillips952841b2014-06-30 08:26:50 -0700227 }
robertphillips952841b2014-06-30 08:26:50 -0700228
robertphillipsfd61ed02014-10-28 07:21:44 -0700229 return false;
230}
231
bsalomonf2703d82014-10-28 14:33:06 -0700232bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700233 if (layer->locked()) {
234 // This layer is already locked
235 *needsRendering = false;
236 return true;
237 }
238
bsalomone3059732014-10-14 11:47:22 -0700239 SkAutoTUnref<GrTexture> tex(
240 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700241
robertphillipsfd61ed02014-10-28 07:21:44 -0700242 if (!tex) {
243 return false;
244 }
245
robertphillipsed6f03e2014-07-30 07:31:35 -0700246 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700247 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700248 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700249 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700250}
251
252void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700253 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700254
robertphillipsa32c6bc2014-10-09 12:47:01 -0700255 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700256 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700257 return;
258 }
259
robertphillips21048b52014-07-15 19:46:35 -0700260 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700261 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700262
robertphillips7bb9ed72014-10-10 11:38:29 -0700263 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700264 // At this point we could aggressively clear out un-locked plots but
265 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700266#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700267 // This testing code aggressively removes the atlased layers. This
268 // can be used to separate the performance contribution of less
269 // render target pingponging from that due to the re-use of cached layers
270 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700271 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700272
273 pictInfo->decPlotUsage(plotID);
274
275 if (0 == pictInfo->plotUsage(plotID)) {
276 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800277
278 if (pictInfo->fPlotUsage.isEmpty()) {
279 fPictureHash.remove(pictInfo->fPictureID);
280 SkDELETE(pictInfo);
281 }
robertphillips225a6272014-10-30 11:39:19 -0700282 }
mtklein04c96952014-11-24 08:20:57 -0800283
robertphillips0c423322014-07-31 11:02:38 -0700284 layer->setPlot(NULL);
285 layer->setTexture(NULL, GrIRect16::MakeEmpty());
286#endif
287
robertphillips21048b52014-07-15 19:46:35 -0700288 } else {
robertphillips952841b2014-06-30 08:26:50 -0700289 layer->setTexture(NULL, GrIRect16::MakeEmpty());
290 }
robertphillips320c9232014-07-29 06:07:19 -0700291
292 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700293}
294
robertphillips21048b52014-07-15 19:46:35 -0700295#ifdef SK_DEBUG
296void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700297 int plotLocks[kNumPlotsX * kNumPlotsY];
298 memset(plotLocks, 0, sizeof(plotLocks));
299
robertphillips3d533ac2014-07-20 09:40:00 -0700300 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
301 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700302 const GrCachedLayer* layer = &(*iter);
303
robertphillips4ab5a902014-10-29 13:56:02 -0700304 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700305
306 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700307 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700308 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700309 // layers should be non-atlased.
310 SkASSERT(!layer->isAtlased());
311 }
312
bsalomon49f085d2014-09-05 13:34:00 -0700313 if (layer->plot()) {
314 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700315 SkASSERT(pictInfo->fPictureID == layer->pictureID());
316
317 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700318#if !GR_CACHE_HOISTED_LAYERS
319 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
320#endif
robertphillips320c9232014-07-29 06:07:19 -0700321
322 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700323 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700324 }
mtklein04c96952014-11-24 08:20:57 -0800325 }
robertphillips320c9232014-07-29 06:07:19 -0700326 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700327
328 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
329 SkASSERT(plotLocks[i] == fPlotLocks[i]);
330 }
robertphillips21048b52014-07-15 19:46:35 -0700331}
332
333class GrAutoValidateCache : ::SkNoncopyable {
334public:
335 explicit GrAutoValidateCache(GrLayerCache* cache)
336 : fCache(cache) {
337 fCache->validate();
338 }
339 ~GrAutoValidateCache() {
340 fCache->validate();
341 }
342private:
343 GrLayerCache* fCache;
344};
345#endif
346
robertphillipsd771f6b2014-07-22 10:18:06 -0700347void GrLayerCache::purge(uint32_t pictureID) {
348
robertphillips21048b52014-07-15 19:46:35 -0700349 SkDEBUGCODE(GrAutoValidateCache avc(this);)
350
robertphillips3d533ac2014-07-20 09:40:00 -0700351 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700352 SkTDArray<GrCachedLayer*> toBeRemoved;
353
robertphillips3d533ac2014-07-20 09:40:00 -0700354 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
355 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700356 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700357 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700358 }
359 }
360
361 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700362 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700363 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700364 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700365 SkDELETE(toBeRemoved[i]);
366 }
robertphillips261b8a92014-07-17 08:26:44 -0700367
robertphillipsd771f6b2014-07-22 10:18:06 -0700368 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700369 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700370 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700371 SkDELETE(pictInfo);
372 }
robertphillips4ec84da2014-06-24 13:10:43 -0700373}
robertphillipsd771f6b2014-07-22 10:18:06 -0700374
robertphillips320c9232014-07-29 06:07:19 -0700375bool GrLayerCache::purgePlot() {
376 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700377 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700378
379 GrAtlas::PlotIter iter;
380 GrPlot* plot;
381 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700382 plot;
robertphillips320c9232014-07-29 06:07:19 -0700383 plot = iter.prev()) {
384 if (fPlotLocks[plot->id()] > 0) {
385 continue;
386 }
387
robertphillips6f294af2014-08-18 08:50:03 -0700388 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700389 return true;
390 }
391
392 return false;
393}
394
robertphillips6f294af2014-08-18 08:50:03 -0700395void GrLayerCache::purgePlot(GrPlot* plot) {
396 SkASSERT(0 == fPlotLocks[plot->id()]);
397
398 // We need to find all the layers in 'plot' and remove them.
399 SkTDArray<GrCachedLayer*> toBeRemoved;
400
401 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
402 for (; !iter.done(); ++iter) {
403 if (plot == (*iter).plot()) {
404 *toBeRemoved.append() = &(*iter);
405 }
406 }
407
408 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700409 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700410 SkASSERT(!toBeRemoved[i]->locked());
411
robertphillips410dd052014-10-06 12:19:50 -0700412 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700413
robertphillips410dd052014-10-06 12:19:50 -0700414 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700415 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
416 SkDELETE(toBeRemoved[i]);
417
robertphillips410dd052014-10-06 12:19:50 -0700418 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
419 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700420#if !GR_CACHE_HOISTED_LAYERS
421 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
422#endif
robertphillips410dd052014-10-06 12:19:50 -0700423 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
424
425 if (pictInfo->fPlotUsage.isEmpty()) {
426 fPictureHash.remove(pictInfo->fPictureID);
427 SkDELETE(pictInfo);
428 }
robertphillips6f294af2014-08-18 08:50:03 -0700429 }
430 }
431
432 plot->resetRects();
433}
434
robertphillips4ab5a902014-10-29 13:56:02 -0700435#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700436void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700437 if (!fAtlas) {
438 return;
439 }
440
robertphillips6f294af2014-08-18 08:50:03 -0700441 GrAtlas::PlotIter iter;
442 GrPlot* plot;
443 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700444 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700445 plot = iter.prev()) {
446 SkASSERT(0 == fPlotLocks[plot->id()]);
447
448 this->purgePlot(plot);
449 }
robertphillips4ab5a902014-10-29 13:56:02 -0700450
robertphillipsb32f0ad2014-11-04 06:46:11 -0800451 SkASSERT(0 == fPictureHash.count());
452
robertphillips4ab5a902014-10-29 13:56:02 -0700453 fContext->discardRenderTarget(fAtlas->getTexture()->asRenderTarget());
robertphillips6f294af2014-08-18 08:50:03 -0700454}
robertphillips4ab5a902014-10-29 13:56:02 -0700455#endif
robertphillips6f294af2014-08-18 08:50:03 -0700456
robertphillipsd771f6b2014-07-22 10:18:06 -0700457void GrLayerCache::processDeletedPictures() {
mtklein04c96952014-11-24 08:20:57 -0800458 SkTDArray<SkPicture::DeletionMessage> deletedPictures;
robertphillipsd771f6b2014-07-22 10:18:06 -0700459 fPictDeletionInbox.poll(&deletedPictures);
460
461 for (int i = 0; i < deletedPictures.count(); i++) {
mtklein04c96952014-11-24 08:20:57 -0800462 this->purge(deletedPictures[i].fUniqueID);
robertphillipsd771f6b2014-07-22 10:18:06 -0700463 }
464}
465
robertphillips84ac0822014-10-14 07:07:59 -0700466#ifdef SK_DEVELOPER
467void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
468
robertphillips4ab5a902014-10-29 13:56:02 -0700469 if (fAtlas) {
470 GrTexture* atlasTexture = fAtlas->getTexture();
471 if (NULL != atlasTexture) {
472 SkString fileName(dirName);
473 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700474
robertphillips4ab5a902014-10-29 13:56:02 -0700475 atlasTexture->surfacePriv().savePixels(fileName.c_str());
476 }
robertphillips84ac0822014-10-14 07:07:59 -0700477 }
478
479 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
480 for (; !iter.done(); ++iter) {
481 GrCachedLayer* layer = &(*iter);
482
483 if (layer->isAtlased() || !layer->texture()) {
484 continue;
485 }
486
487 SkString fileName(dirName);
robertphillips3aac6e02014-10-20 08:52:40 -0700488 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.start());
robertphillips84ac0822014-10-14 07:07:59 -0700489
490 layer->texture()->surfacePriv().savePixels(fileName.c_str());
491 }
492}
493#endif