blob: 154c6f3621f16c515f054f949171ab539daaa5ad [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
robertphillips6f294af2014-08-18 08:50:03 -0700164bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas) {
robertphillips21048b52014-07-15 19:46:35 -0700165 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700166
robertphillips320c9232014-07-29 06:07:19 -0700167 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700168 // This layer is already locked
robertphillipsa32c6bc2014-10-09 12:47:01 -0700169#ifdef SK_DEBUG
robertphillips21048b52014-07-15 19:46:35 -0700170 if (layer->isAtlased()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700171 // It claims to be atlased
robertphillips6f294af2014-08-18 08:50:03 -0700172 SkASSERT(!dontAtlas);
robertphillips952841b2014-06-30 08:26:50 -0700173 SkASSERT(layer->rect().width() == desc.fWidth);
174 SkASSERT(layer->rect().height() == desc.fHeight);
175 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700176#endif
robertphillips6f294af2014-08-18 08:50:03 -0700177 return false;
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
robertphillips6f294af2014-08-18 08:50:03 -0700182 SkASSERT(!dontAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700183 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700184 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700185 return false;
186 } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
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());
robertphillips6f294af2014-08-18 08:50:03 -0700210 return true;
robertphillips320c9232014-07-29 06:07:19 -0700211 }
212
213 // The layer was rejected by the atlas (even though we know it is
214 // plausibly atlas-able). See if a plot can be purged and try again.
215 if (!this->purgePlot()) {
216 break; // We weren't able to purge any plots
217 }
robertphillips261b8a92014-07-17 08:26:44 -0700218 }
robertphillips952841b2014-06-30 08:26:50 -0700219 }
robertphillips952841b2014-06-30 08:26:50 -0700220
robertphillips21048b52014-07-15 19:46:35 -0700221 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700222 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700223 // This can yield a lot of re-rendering
bsalomone3059732014-10-14 11:47:22 -0700224 SkAutoTUnref<GrTexture> tex(
225 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700226
227 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700228 layer->setLocked(true);
robertphillips6f294af2014-08-18 08:50:03 -0700229 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700230}
231
232void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700233 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
234
robertphillipsa32c6bc2014-10-09 12:47:01 -0700235 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700236 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700237 return;
238 }
239
robertphillips21048b52014-07-15 19:46:35 -0700240 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700241 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700242
robertphillips7bb9ed72014-10-10 11:38:29 -0700243 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700244 // At this point we could aggressively clear out un-locked plots but
245 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips6f294af2014-08-18 08:50:03 -0700246#if DISABLE_CACHING
robertphillips0c423322014-07-31 11:02:38 -0700247 // This testing code aggressively removes the atlased layers. This
248 // can be used to separate the performance contribution of less
249 // render target pingponging from that due to the re-use of cached layers
250 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700251 SkASSERT(pictInfo);
robertphillips0c423322014-07-31 11:02:38 -0700252
253 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
254
255 layer->setPlot(NULL);
256 layer->setTexture(NULL, GrIRect16::MakeEmpty());
257#endif
258
robertphillips21048b52014-07-15 19:46:35 -0700259 } else {
robertphillips952841b2014-06-30 08:26:50 -0700260 layer->setTexture(NULL, GrIRect16::MakeEmpty());
261 }
robertphillips320c9232014-07-29 06:07:19 -0700262
263 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700264}
265
robertphillips21048b52014-07-15 19:46:35 -0700266#ifdef SK_DEBUG
267void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700268 int plotLocks[kNumPlotsX * kNumPlotsY];
269 memset(plotLocks, 0, sizeof(plotLocks));
270
robertphillips3d533ac2014-07-20 09:40:00 -0700271 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
272 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700273 const GrCachedLayer* layer = &(*iter);
274
275 layer->validate(fAtlas->getTexture());
276
277 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700278 if (pictInfo) {
robertphillips320c9232014-07-29 06:07:19 -0700279 // In aggressive cleanup mode a picture info should only exist if
280 // it has some atlased layers
robertphillips6f294af2014-08-18 08:50:03 -0700281#if !DISABLE_CACHING
robertphillips320c9232014-07-29 06:07:19 -0700282 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
robertphillips6f294af2014-08-18 08:50:03 -0700283#endif
robertphillips320c9232014-07-29 06:07:19 -0700284 } else {
robertphillips7bb9ed72014-10-10 11:38:29 -0700285 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700286 // layers should be non-atlased.
287 SkASSERT(!layer->isAtlased());
288 }
289
bsalomon49f085d2014-09-05 13:34:00 -0700290 if (layer->plot()) {
291 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700292 SkASSERT(pictInfo->fPictureID == layer->pictureID());
293
294 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
295
296 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700297 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700298 }
299 }
300 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700301
302 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
303 SkASSERT(plotLocks[i] == fPlotLocks[i]);
304 }
robertphillips21048b52014-07-15 19:46:35 -0700305}
306
307class GrAutoValidateCache : ::SkNoncopyable {
308public:
309 explicit GrAutoValidateCache(GrLayerCache* cache)
310 : fCache(cache) {
311 fCache->validate();
312 }
313 ~GrAutoValidateCache() {
314 fCache->validate();
315 }
316private:
317 GrLayerCache* fCache;
318};
319#endif
320
robertphillipsd771f6b2014-07-22 10:18:06 -0700321void GrLayerCache::purge(uint32_t pictureID) {
322
robertphillips21048b52014-07-15 19:46:35 -0700323 SkDEBUGCODE(GrAutoValidateCache avc(this);)
324
robertphillips3d533ac2014-07-20 09:40:00 -0700325 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700326 SkTDArray<GrCachedLayer*> toBeRemoved;
327
robertphillips3d533ac2014-07-20 09:40:00 -0700328 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
329 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700330 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700331 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700332 }
333 }
334
335 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700336 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700337 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700338 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700339 SkDELETE(toBeRemoved[i]);
340 }
robertphillips261b8a92014-07-17 08:26:44 -0700341
robertphillipsd771f6b2014-07-22 10:18:06 -0700342 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700343 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700344 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700345 SkDELETE(pictInfo);
346 }
robertphillips4ec84da2014-06-24 13:10:43 -0700347}
robertphillipsd771f6b2014-07-22 10:18:06 -0700348
robertphillips320c9232014-07-29 06:07:19 -0700349bool GrLayerCache::purgePlot() {
350 SkDEBUGCODE(GrAutoValidateCache avc(this);)
351
352 GrAtlas::PlotIter iter;
353 GrPlot* plot;
354 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700355 plot;
robertphillips320c9232014-07-29 06:07:19 -0700356 plot = iter.prev()) {
357 if (fPlotLocks[plot->id()] > 0) {
358 continue;
359 }
360
robertphillips6f294af2014-08-18 08:50:03 -0700361 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700362 return true;
363 }
364
365 return false;
366}
367
robertphillips6f294af2014-08-18 08:50:03 -0700368void GrLayerCache::purgePlot(GrPlot* plot) {
369 SkASSERT(0 == fPlotLocks[plot->id()]);
370
371 // We need to find all the layers in 'plot' and remove them.
372 SkTDArray<GrCachedLayer*> toBeRemoved;
373
374 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
375 for (; !iter.done(); ++iter) {
376 if (plot == (*iter).plot()) {
377 *toBeRemoved.append() = &(*iter);
378 }
379 }
380
381 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700382 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700383 SkASSERT(!toBeRemoved[i]->locked());
384
robertphillips410dd052014-10-06 12:19:50 -0700385 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700386
robertphillips410dd052014-10-06 12:19:50 -0700387 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700388 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
389 SkDELETE(toBeRemoved[i]);
390
robertphillips410dd052014-10-06 12:19:50 -0700391 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
392 if (pictInfo) {
393 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
394
395 if (pictInfo->fPlotUsage.isEmpty()) {
396 fPictureHash.remove(pictInfo->fPictureID);
397 SkDELETE(pictInfo);
398 }
robertphillips6f294af2014-08-18 08:50:03 -0700399 }
400 }
401
402 plot->resetRects();
403}
404
405void GrLayerCache::purgeAll() {
406 GrAtlas::PlotIter iter;
407 GrPlot* plot;
408 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700409 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700410 plot = iter.prev()) {
411 SkASSERT(0 == fPlotLocks[plot->id()]);
412
413 this->purgePlot(plot);
414 }
415}
416
robertphillipsd771f6b2014-07-22 10:18:06 -0700417class GrPictureDeletionListener : public SkPicture::DeletionListener {
418 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
419 const GrPictureDeletedMessage message = { pictureID };
420 SkMessageBus<GrPictureDeletedMessage>::Post(message);
421 }
422};
423
424void GrLayerCache::trackPicture(const SkPicture* picture) {
425 if (NULL == fDeletionListener) {
426 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
427 }
428
429 picture->addDeletionListener(fDeletionListener);
430}
431
432void GrLayerCache::processDeletedPictures() {
433 SkTDArray<GrPictureDeletedMessage> deletedPictures;
434 fPictDeletionInbox.poll(&deletedPictures);
435
436 for (int i = 0; i < deletedPictures.count(); i++) {
437 this->purge(deletedPictures[i].pictureID);
438 }
439}
440
robertphillips84ac0822014-10-14 07:07:59 -0700441#ifdef SK_DEVELOPER
442void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
443
444 GrTexture* atlasTexture = fAtlas->getTexture();
445 if (NULL != atlasTexture) {
446 SkString fileName(dirName);
447 fileName.append("\\atlas.png");
448
449 atlasTexture->surfacePriv().savePixels(fileName.c_str());
450 }
451
452 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
453 for (; !iter.done(); ++iter) {
454 GrCachedLayer* layer = &(*iter);
455
456 if (layer->isAtlased() || !layer->texture()) {
457 continue;
458 }
459
460 SkString fileName(dirName);
robertphillips3aac6e02014-10-20 08:52:40 -0700461 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.start());
robertphillips84ac0822014-10-14 07:07:59 -0700462
463 layer->texture()->surfacePriv().savePixels(fileName.c_str());
464 }
465}
466#endif