blob: ccb5bb0be48661de61f055d9960e711bfef883e5 [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) {
84 this->initAtlas();
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);
95 SkDELETE(layer);
96 }
robertphillips952841b2014-06-30 08:26:50 -070097
98 // The atlas only lets go of its texture when the atlas is deleted.
99 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000100}
101
robertphillips952841b2014-06-30 08:26:50 -0700102void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -0700103 SkASSERT(NULL == fAtlas.get());
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,
robertphillips952841b2014-06-30 08:26:50 -0700107 kRenderTarget_GrTextureFlagBit,
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();
robertphillips952841b2014-06-30 08:26:50 -0700123 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
124 // lazily allocates a replacement texture so reallocating a new
bsalomonc8dc1f72014-08-21 13:02:13 -0700125 // atlas here won't disrupt a GrContext::abandonContext or freeGpuResources.
robertphillips952841b2014-06-30 08:26:50 -0700126 // TODO: Make GrLayerCache lazily allocate the atlas manager?
127 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +0000128}
129
robertphillips6f294af2014-08-18 08:50:03 -0700130GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700131 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700132 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700133 const SkMatrix& ctm,
134 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700135 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700136
robertphillips3aac6e02014-10-20 08:52:40 -0700137 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, bounds, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700138 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000139 return layer;
140}
141
robertphillips6f294af2014-08-18 08:50:03 -0700142GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
robertphillipsed420592014-09-29 11:39:38 -0700143 int start,
robertphillips3aac6e02014-10-20 08:52:40 -0700144 const SkIRect& bounds,
robertphillips0c423322014-07-31 11:02:38 -0700145 const SkMatrix& ctm) {
robertphillipsed420592014-09-29 11:39:38 -0700146 SkASSERT(pictureID != SK_InvalidGenID && start > 0);
robertphillips3aac6e02014-10-20 08:52:40 -0700147 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700148}
robertphillips@google.come930a072014-04-03 00:34:27 +0000149
robertphillips6f294af2014-08-18 08:50:03 -0700150GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700151 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700152 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700153 const SkMatrix& ctm,
154 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700155 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips3aac6e02014-10-20 08:52:40 -0700156 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000157 if (NULL == layer) {
robertphillips3aac6e02014-10-20 08:52:40 -0700158 layer = this->createLayer(pictureID, start, stop, bounds, ctm, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000159 }
robertphillips4ec84da2014-06-24 13:10:43 -0700160
robertphillips@google.come930a072014-04-03 00:34:27 +0000161 return layer;
162}
robertphillips4ec84da2014-06-24 13:10:43 -0700163
robertphillipsfd61ed02014-10-28 07:21:44 -0700164bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
165 const GrTextureDesc& desc,
166 bool* needsRendering) {
robertphillips21048b52014-07-15 19:46:35 -0700167 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700168
robertphillipsfd61ed02014-10-28 07:21:44 -0700169 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
170
robertphillips320c9232014-07-29 06:07:19 -0700171 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700172 // This layer is already locked
robertphillipsfd61ed02014-10-28 07:21:44 -0700173 SkASSERT(layer->isAtlased());
174 SkASSERT(layer->rect().width() == desc.fWidth);
175 SkASSERT(layer->rect().height() == desc.fHeight);
176 *needsRendering = false;
177 return true;
robertphillips952841b2014-06-30 08:26:50 -0700178 }
179
robertphillips320c9232014-07-29 06:07:19 -0700180 if (layer->isAtlased()) {
181 // Hooray it is still in the atlas - make sure it stays there
182 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700183 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700184 *needsRendering = false;
185 return true;
186 } else {
robertphillips320c9232014-07-29 06:07:19 -0700187 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700188 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700189 if (NULL == pictInfo) {
190 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700191 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700192 }
193
194 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700195 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
196 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
197 desc.fWidth, desc.fHeight,
198 NULL, &loc);
199 // addToAtlas can allocate the backing texture
200 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700201 if (plot) {
robertphillips320c9232014-07-29 06:07:19 -0700202 // The layer was successfully added to the atlas
203 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700204 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700205 SkToS16(desc.fHeight));
206 layer->setTexture(fAtlas->getTexture(), bounds);
207 layer->setPlot(plot);
208 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700209 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700210 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700211 return true;
robertphillips320c9232014-07-29 06:07:19 -0700212 }
213
214 // The layer was rejected by the atlas (even though we know it is
215 // plausibly atlas-able). See if a plot can be purged and try again.
216 if (!this->purgePlot()) {
217 break; // We weren't able to purge any plots
218 }
robertphillips261b8a92014-07-17 08:26:44 -0700219 }
robertphillips952841b2014-06-30 08:26:50 -0700220 }
robertphillips952841b2014-06-30 08:26:50 -0700221
robertphillipsfd61ed02014-10-28 07:21:44 -0700222 return false;
223}
224
225bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool* needsRendering) {
226 if (layer->locked()) {
227 // This layer is already locked
228 *needsRendering = false;
229 return true;
230 }
231
bsalomone3059732014-10-14 11:47:22 -0700232 SkAutoTUnref<GrTexture> tex(
233 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700234
robertphillipsfd61ed02014-10-28 07:21:44 -0700235 if (!tex) {
236 return false;
237 }
238
robertphillipsed6f03e2014-07-30 07:31:35 -0700239 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700240 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700241 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700242 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700243}
244
245void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700246 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
247
robertphillipsa32c6bc2014-10-09 12:47:01 -0700248 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700249 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700250 return;
251 }
252
robertphillips21048b52014-07-15 19:46:35 -0700253 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700254 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700255
robertphillips7bb9ed72014-10-10 11:38:29 -0700256 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700257 // At this point we could aggressively clear out un-locked plots but
258 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips6f294af2014-08-18 08:50:03 -0700259#if DISABLE_CACHING
robertphillips0c423322014-07-31 11:02:38 -0700260 // This testing code aggressively removes the atlased layers. This
261 // can be used to separate the performance contribution of less
262 // render target pingponging from that due to the re-use of cached layers
263 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700264 SkASSERT(pictInfo);
robertphillips0c423322014-07-31 11:02:38 -0700265
266 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
267
268 layer->setPlot(NULL);
269 layer->setTexture(NULL, GrIRect16::MakeEmpty());
270#endif
271
robertphillips21048b52014-07-15 19:46:35 -0700272 } else {
robertphillips952841b2014-06-30 08:26:50 -0700273 layer->setTexture(NULL, GrIRect16::MakeEmpty());
274 }
robertphillips320c9232014-07-29 06:07:19 -0700275
276 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700277}
278
robertphillips21048b52014-07-15 19:46:35 -0700279#ifdef SK_DEBUG
280void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700281 int plotLocks[kNumPlotsX * kNumPlotsY];
282 memset(plotLocks, 0, sizeof(plotLocks));
283
robertphillips3d533ac2014-07-20 09:40:00 -0700284 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
285 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700286 const GrCachedLayer* layer = &(*iter);
287
288 layer->validate(fAtlas->getTexture());
289
290 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700291 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700292 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700293 // layers should be non-atlased.
294 SkASSERT(!layer->isAtlased());
295 }
296
bsalomon49f085d2014-09-05 13:34:00 -0700297 if (layer->plot()) {
298 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700299 SkASSERT(pictInfo->fPictureID == layer->pictureID());
300
301 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
302
303 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700304 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700305 }
306 }
307 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700308
309 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
310 SkASSERT(plotLocks[i] == fPlotLocks[i]);
311 }
robertphillips21048b52014-07-15 19:46:35 -0700312}
313
314class GrAutoValidateCache : ::SkNoncopyable {
315public:
316 explicit GrAutoValidateCache(GrLayerCache* cache)
317 : fCache(cache) {
318 fCache->validate();
319 }
320 ~GrAutoValidateCache() {
321 fCache->validate();
322 }
323private:
324 GrLayerCache* fCache;
325};
326#endif
327
robertphillipsd771f6b2014-07-22 10:18:06 -0700328void GrLayerCache::purge(uint32_t pictureID) {
329
robertphillips21048b52014-07-15 19:46:35 -0700330 SkDEBUGCODE(GrAutoValidateCache avc(this);)
331
robertphillips3d533ac2014-07-20 09:40:00 -0700332 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700333 SkTDArray<GrCachedLayer*> toBeRemoved;
334
robertphillips3d533ac2014-07-20 09:40:00 -0700335 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
336 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700337 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700338 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700339 }
340 }
341
342 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700343 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700344 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700345 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700346 SkDELETE(toBeRemoved[i]);
347 }
robertphillips261b8a92014-07-17 08:26:44 -0700348
robertphillipsd771f6b2014-07-22 10:18:06 -0700349 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700350 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700351 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700352 SkDELETE(pictInfo);
353 }
robertphillips4ec84da2014-06-24 13:10:43 -0700354}
robertphillipsd771f6b2014-07-22 10:18:06 -0700355
robertphillips320c9232014-07-29 06:07:19 -0700356bool GrLayerCache::purgePlot() {
357 SkDEBUGCODE(GrAutoValidateCache avc(this);)
358
359 GrAtlas::PlotIter iter;
360 GrPlot* plot;
361 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700362 plot;
robertphillips320c9232014-07-29 06:07:19 -0700363 plot = iter.prev()) {
364 if (fPlotLocks[plot->id()] > 0) {
365 continue;
366 }
367
robertphillips6f294af2014-08-18 08:50:03 -0700368 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700369 return true;
370 }
371
372 return false;
373}
374
robertphillips6f294af2014-08-18 08:50:03 -0700375void GrLayerCache::purgePlot(GrPlot* plot) {
376 SkASSERT(0 == fPlotLocks[plot->id()]);
377
378 // We need to find all the layers in 'plot' and remove them.
379 SkTDArray<GrCachedLayer*> toBeRemoved;
380
381 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
382 for (; !iter.done(); ++iter) {
383 if (plot == (*iter).plot()) {
384 *toBeRemoved.append() = &(*iter);
385 }
386 }
387
388 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700389 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700390 SkASSERT(!toBeRemoved[i]->locked());
391
robertphillips410dd052014-10-06 12:19:50 -0700392 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700393
robertphillips410dd052014-10-06 12:19:50 -0700394 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700395 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
396 SkDELETE(toBeRemoved[i]);
397
robertphillips410dd052014-10-06 12:19:50 -0700398 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
399 if (pictInfo) {
400 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
401
402 if (pictInfo->fPlotUsage.isEmpty()) {
403 fPictureHash.remove(pictInfo->fPictureID);
404 SkDELETE(pictInfo);
405 }
robertphillips6f294af2014-08-18 08:50:03 -0700406 }
407 }
408
409 plot->resetRects();
410}
411
412void GrLayerCache::purgeAll() {
413 GrAtlas::PlotIter iter;
414 GrPlot* plot;
415 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700416 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700417 plot = iter.prev()) {
418 SkASSERT(0 == fPlotLocks[plot->id()]);
419
420 this->purgePlot(plot);
421 }
422}
423
robertphillipsd771f6b2014-07-22 10:18:06 -0700424class GrPictureDeletionListener : public SkPicture::DeletionListener {
425 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
426 const GrPictureDeletedMessage message = { pictureID };
427 SkMessageBus<GrPictureDeletedMessage>::Post(message);
428 }
429};
430
431void GrLayerCache::trackPicture(const SkPicture* picture) {
432 if (NULL == fDeletionListener) {
433 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
434 }
435
436 picture->addDeletionListener(fDeletionListener);
437}
438
439void GrLayerCache::processDeletedPictures() {
440 SkTDArray<GrPictureDeletedMessage> deletedPictures;
441 fPictDeletionInbox.poll(&deletedPictures);
442
443 for (int i = 0; i < deletedPictures.count(); i++) {
444 this->purge(deletedPictures[i].pictureID);
445 }
446}
447
robertphillips84ac0822014-10-14 07:07:59 -0700448#ifdef SK_DEVELOPER
449void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
450
451 GrTexture* atlasTexture = fAtlas->getTexture();
452 if (NULL != atlasTexture) {
453 SkString fileName(dirName);
454 fileName.append("\\atlas.png");
455
456 atlasTexture->surfacePriv().savePixels(fileName.c_str());
457 }
458
459 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
460 for (; !iter.done(); ++iter) {
461 GrCachedLayer* layer = &(*iter);
462
463 if (layer->isAtlased() || !layer->texture()) {
464 continue;
465 }
466
467 SkString fileName(dirName);
robertphillips3aac6e02014-10-20 08:52:40 -0700468 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.start());
robertphillips84ac0822014-10-14 07:07:59 -0700469
470 layer->texture()->surfacePriv().savePixels(fileName.c_str());
471 }
472}
473#endif