blob: f60b6a8120a6792aef1f04d7fca8d3af3e897850 [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
robertphillipsd771f6b2014-07-22 10:18:06 -070013DECLARE_SKMESSAGEBUS_MESSAGE(GrPictureDeletedMessage);
14
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());
robertphillips410dd052014-10-06 12:19:50 -070018 SkASSERT(fKey.start() >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -070019
bsalomon49f085d2014-09-05 13:34:00 -070020 if (fTexture) {
robertphillips21048b52014-07-15 19:46:35 -070021 // If the layer is in some texture then it must occupy some rectangle
22 SkASSERT(!fRect.isEmpty());
23 if (!this->isAtlased()) {
24 // If it isn't atlased then the rectangle should start at the origin
25 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
26 }
27 } else {
28 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070029 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070030 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070031 }
32
bsalomon49f085d2014-09-05 13:34:00 -070033 if (fPlot) {
robertphillips261b8a92014-07-17 08:26:44 -070034 // If a layer has a plot (i.e., is atlased) then it must point to
35 // the backing texture. Additionally, its rect should be non-empty.
bsalomon49f085d2014-09-05 13:34:00 -070036 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070037 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070038 }
robertphillips320c9232014-07-29 06:07:19 -070039
40 if (fLocked) {
41 // If a layer is locked it must have a texture (though it need not be
42 // the atlas-backing texture) and occupy some space.
bsalomon49f085d2014-09-05 13:34:00 -070043 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070044 SkASSERT(!fRect.isEmpty());
45 }
robertphillips7bb9ed72014-10-10 11:38:29 -070046
47 // Unfortunately there is a brief time where a layer can be locked
48 // but not used, so we can only check the "used implies locked"
49 // invariant.
50 if (fUses > 0) {
51 SkASSERT(fLocked);
52 } else {
53 SkASSERT(0 == fUses);
54 }
robertphillips21048b52014-07-15 19:46:35 -070055}
56
57class GrAutoValidateLayer : ::SkNoncopyable {
58public:
59 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
60 : fBackingTexture(backingTexture)
61 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070062 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070063 fLayer->validate(backingTexture);
64 }
65 }
66 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070067 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070068 fLayer->validate(fBackingTexture);
69 }
70 }
robertphillips261b8a92014-07-17 08:26:44 -070071 void setBackingTexture(GrTexture* backingTexture) {
72 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
73 fBackingTexture = backingTexture;
74 }
robertphillips21048b52014-07-15 19:46:35 -070075
76private:
robertphillips261b8a92014-07-17 08:26:44 -070077 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070078 const GrCachedLayer* fLayer;
79};
80#endif
81
robertphillips4ec84da2014-06-24 13:10:43 -070082GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070083 : fContext(context) {
robertphillips320c9232014-07-29 06:07:19 -070084 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000085}
86
87GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070088
robertphillips3d533ac2014-07-20 09:40:00 -070089 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
90 for (; !iter.done(); ++iter) {
91 GrCachedLayer* layer = &(*iter);
robertphillips7bb9ed72014-10-10 11:38:29 -070092 SkASSERT(0 == layer->uses());
robertphillips3d533ac2014-07-20 09:40:00 -070093 this->unlock(layer);
94 SkDELETE(layer);
95 }
robertphillips952841b2014-06-30 08:26:50 -070096
97 // 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
121 // 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
robertphillips6f294af2014-08-18 08:50:03 -0700125GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700126 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,
robertphillipsed420592014-09-29 11:39:38 -0700138 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
robertphillipsfd61ed02014-10-28 07:21:44 -0700159bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
bsalomonf2703d82014-10-28 14:33:06 -0700160 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));
165
robertphillips320c9232014-07-29 06:07:19 -0700166 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700167 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700168 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700169 SkASSERT(layer->isAtlased());
170 SkASSERT(layer->rect().width() == desc.fWidth);
171 SkASSERT(layer->rect().height() == desc.fHeight);
172 *needsRendering = false;
173 return true;
robertphillips952841b2014-06-30 08:26:50 -0700174 }
175
robertphillips320c9232014-07-29 06:07:19 -0700176 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700177 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700178 // Hooray it is still in the atlas - make sure it stays there
179 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700180 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700181 *needsRendering = false;
182 return true;
183 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700184 if (!fAtlas) {
185 this->initAtlas();
186 if (!fAtlas) {
187 return false;
188 }
189 }
robertphillips320c9232014-07-29 06:07:19 -0700190 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700191 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700192 if (NULL == pictInfo) {
193 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700194 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700195 }
196
197 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700198 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
199 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
200 desc.fWidth, desc.fHeight,
201 NULL, &loc);
202 // addToAtlas can allocate the backing texture
203 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700204 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700205#if !GR_CACHE_HOISTED_LAYERS
206 pictInfo->incPlotUsage(plot->id());
207#endif
robertphillips320c9232014-07-29 06:07:19 -0700208 // The layer was successfully added to the atlas
209 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700210 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700211 SkToS16(desc.fHeight));
212 layer->setTexture(fAtlas->getTexture(), bounds);
213 layer->setPlot(plot);
214 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700215 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700216 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700217 return true;
robertphillips320c9232014-07-29 06:07:19 -0700218 }
219
220 // The layer was rejected by the atlas (even though we know it is
221 // plausibly atlas-able). See if a plot can be purged and try again.
222 if (!this->purgePlot()) {
223 break; // We weren't able to purge any plots
224 }
robertphillips261b8a92014-07-17 08:26:44 -0700225 }
robertphillips952841b2014-06-30 08:26:50 -0700226 }
robertphillips952841b2014-06-30 08:26:50 -0700227
robertphillipsfd61ed02014-10-28 07:21:44 -0700228 return false;
229}
230
bsalomonf2703d82014-10-28 14:33:06 -0700231bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700232 if (layer->locked()) {
233 // This layer is already locked
234 *needsRendering = false;
235 return true;
236 }
237
bsalomone3059732014-10-14 11:47:22 -0700238 SkAutoTUnref<GrTexture> tex(
239 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700240
robertphillipsfd61ed02014-10-28 07:21:44 -0700241 if (!tex) {
242 return false;
243 }
244
robertphillipsed6f03e2014-07-30 07:31:35 -0700245 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700246 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700247 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700248 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700249}
250
251void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700252 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700253
robertphillipsa32c6bc2014-10-09 12:47:01 -0700254 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700255 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700256 return;
257 }
258
robertphillips21048b52014-07-15 19:46:35 -0700259 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700260 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700261
robertphillips7bb9ed72014-10-10 11:38:29 -0700262 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700263 // At this point we could aggressively clear out un-locked plots but
264 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700265#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700266 // This testing code aggressively removes the atlased layers. This
267 // can be used to separate the performance contribution of less
268 // render target pingponging from that due to the re-use of cached layers
269 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700270 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700271
272 pictInfo->decPlotUsage(plotID);
273
274 if (0 == pictInfo->plotUsage(plotID)) {
275 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
276 }
robertphillips0c423322014-07-31 11:02:38 -0700277
278 layer->setPlot(NULL);
279 layer->setTexture(NULL, GrIRect16::MakeEmpty());
280#endif
281
robertphillips21048b52014-07-15 19:46:35 -0700282 } else {
robertphillips952841b2014-06-30 08:26:50 -0700283 layer->setTexture(NULL, GrIRect16::MakeEmpty());
284 }
robertphillips320c9232014-07-29 06:07:19 -0700285
286 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700287}
288
robertphillips21048b52014-07-15 19:46:35 -0700289#ifdef SK_DEBUG
290void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700291 int plotLocks[kNumPlotsX * kNumPlotsY];
292 memset(plotLocks, 0, sizeof(plotLocks));
293
robertphillips3d533ac2014-07-20 09:40:00 -0700294 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
295 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700296 const GrCachedLayer* layer = &(*iter);
297
robertphillips4ab5a902014-10-29 13:56:02 -0700298 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700299
300 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700301 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700302 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700303 // layers should be non-atlased.
304 SkASSERT(!layer->isAtlased());
305 }
306
bsalomon49f085d2014-09-05 13:34:00 -0700307 if (layer->plot()) {
308 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700309 SkASSERT(pictInfo->fPictureID == layer->pictureID());
310
311 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700312#if !GR_CACHE_HOISTED_LAYERS
313 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
314#endif
robertphillips320c9232014-07-29 06:07:19 -0700315
316 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700317 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700318 }
319 }
320 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700321
322 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
323 SkASSERT(plotLocks[i] == fPlotLocks[i]);
324 }
robertphillips21048b52014-07-15 19:46:35 -0700325}
326
327class GrAutoValidateCache : ::SkNoncopyable {
328public:
329 explicit GrAutoValidateCache(GrLayerCache* cache)
330 : fCache(cache) {
331 fCache->validate();
332 }
333 ~GrAutoValidateCache() {
334 fCache->validate();
335 }
336private:
337 GrLayerCache* fCache;
338};
339#endif
340
robertphillipsd771f6b2014-07-22 10:18:06 -0700341void GrLayerCache::purge(uint32_t pictureID) {
342
robertphillips21048b52014-07-15 19:46:35 -0700343 SkDEBUGCODE(GrAutoValidateCache avc(this);)
344
robertphillips3d533ac2014-07-20 09:40:00 -0700345 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700346 SkTDArray<GrCachedLayer*> toBeRemoved;
347
robertphillips3d533ac2014-07-20 09:40:00 -0700348 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
349 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700350 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700351 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700352 }
353 }
354
355 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700356 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700357 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700358 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700359 SkDELETE(toBeRemoved[i]);
360 }
robertphillips261b8a92014-07-17 08:26:44 -0700361
robertphillipsd771f6b2014-07-22 10:18:06 -0700362 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700363 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700364 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700365 SkDELETE(pictInfo);
366 }
robertphillips4ec84da2014-06-24 13:10:43 -0700367}
robertphillipsd771f6b2014-07-22 10:18:06 -0700368
robertphillips320c9232014-07-29 06:07:19 -0700369bool GrLayerCache::purgePlot() {
370 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700371 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700372
373 GrAtlas::PlotIter iter;
374 GrPlot* plot;
375 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700376 plot;
robertphillips320c9232014-07-29 06:07:19 -0700377 plot = iter.prev()) {
378 if (fPlotLocks[plot->id()] > 0) {
379 continue;
380 }
381
robertphillips6f294af2014-08-18 08:50:03 -0700382 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700383 return true;
384 }
385
386 return false;
387}
388
robertphillips6f294af2014-08-18 08:50:03 -0700389void GrLayerCache::purgePlot(GrPlot* plot) {
390 SkASSERT(0 == fPlotLocks[plot->id()]);
391
392 // We need to find all the layers in 'plot' and remove them.
393 SkTDArray<GrCachedLayer*> toBeRemoved;
394
395 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
396 for (; !iter.done(); ++iter) {
397 if (plot == (*iter).plot()) {
398 *toBeRemoved.append() = &(*iter);
399 }
400 }
401
402 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700403 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700404 SkASSERT(!toBeRemoved[i]->locked());
405
robertphillips410dd052014-10-06 12:19:50 -0700406 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700407
robertphillips410dd052014-10-06 12:19:50 -0700408 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700409 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
410 SkDELETE(toBeRemoved[i]);
411
robertphillips410dd052014-10-06 12:19:50 -0700412 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
413 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700414#if !GR_CACHE_HOISTED_LAYERS
415 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
416#endif
robertphillips410dd052014-10-06 12:19:50 -0700417 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
418
419 if (pictInfo->fPlotUsage.isEmpty()) {
420 fPictureHash.remove(pictInfo->fPictureID);
421 SkDELETE(pictInfo);
422 }
robertphillips6f294af2014-08-18 08:50:03 -0700423 }
424 }
425
426 plot->resetRects();
427}
428
robertphillips4ab5a902014-10-29 13:56:02 -0700429#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700430void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700431 if (!fAtlas) {
432 return;
433 }
434
robertphillips6f294af2014-08-18 08:50:03 -0700435 GrAtlas::PlotIter iter;
436 GrPlot* plot;
437 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700438 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700439 plot = iter.prev()) {
440 SkASSERT(0 == fPlotLocks[plot->id()]);
441
442 this->purgePlot(plot);
443 }
robertphillips4ab5a902014-10-29 13:56:02 -0700444
445 fContext->discardRenderTarget(fAtlas->getTexture()->asRenderTarget());
robertphillips6f294af2014-08-18 08:50:03 -0700446}
robertphillips4ab5a902014-10-29 13:56:02 -0700447#endif
robertphillips6f294af2014-08-18 08:50:03 -0700448
robertphillipsd771f6b2014-07-22 10:18:06 -0700449class GrPictureDeletionListener : public SkPicture::DeletionListener {
450 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
451 const GrPictureDeletedMessage message = { pictureID };
452 SkMessageBus<GrPictureDeletedMessage>::Post(message);
453 }
454};
455
456void GrLayerCache::trackPicture(const SkPicture* picture) {
457 if (NULL == fDeletionListener) {
458 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
459 }
460
461 picture->addDeletionListener(fDeletionListener);
462}
463
464void GrLayerCache::processDeletedPictures() {
465 SkTDArray<GrPictureDeletedMessage> deletedPictures;
466 fPictDeletionInbox.poll(&deletedPictures);
467
468 for (int i = 0; i < deletedPictures.count(); i++) {
469 this->purge(deletedPictures[i].pictureID);
470 }
471}
472
robertphillips84ac0822014-10-14 07:07:59 -0700473#ifdef SK_DEVELOPER
474void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
475
robertphillips4ab5a902014-10-29 13:56:02 -0700476 if (fAtlas) {
477 GrTexture* atlasTexture = fAtlas->getTexture();
478 if (NULL != atlasTexture) {
479 SkString fileName(dirName);
480 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700481
robertphillips4ab5a902014-10-29 13:56:02 -0700482 atlasTexture->surfacePriv().savePixels(fileName.c_str());
483 }
robertphillips84ac0822014-10-14 07:07:59 -0700484 }
485
486 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
487 for (; !iter.done(); ++iter) {
488 GrCachedLayer* layer = &(*iter);
489
490 if (layer->isAtlased() || !layer->texture()) {
491 continue;
492 }
493
494 SkString fileName(dirName);
robertphillips3aac6e02014-10-20 08:52:40 -0700495 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.start());
robertphillips84ac0822014-10-14 07:07:59 -0700496
497 layer->texture()->surfacePriv().savePixels(fileName.c_str());
498 }
499}
500#endif