blob: 7c2a8bd61c16bbc0e1d03dc4dcd967916e84e181 [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,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700132 const SkMatrix& ctm,
133 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700134 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700135
robertphillipsed420592014-09-29 11:39:38 -0700136 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700137 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000138 return layer;
139}
140
robertphillips6f294af2014-08-18 08:50:03 -0700141GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
robertphillipsed420592014-09-29 11:39:38 -0700142 int start,
robertphillips0c423322014-07-31 11:02:38 -0700143 const SkMatrix& ctm) {
robertphillipsed420592014-09-29 11:39:38 -0700144 SkASSERT(pictureID != SK_InvalidGenID && start > 0);
145 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700146}
robertphillips@google.come930a072014-04-03 00:34:27 +0000147
robertphillips6f294af2014-08-18 08:50:03 -0700148GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700149 int start, int stop,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700150 const SkMatrix& ctm,
151 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700152 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillipsed420592014-09-29 11:39:38 -0700153 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000154 if (NULL == layer) {
robertphillipsed420592014-09-29 11:39:38 -0700155 layer = this->createLayer(pictureID, start, stop, ctm, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000156 }
robertphillips4ec84da2014-06-24 13:10:43 -0700157
robertphillips@google.come930a072014-04-03 00:34:27 +0000158 return layer;
159}
robertphillips4ec84da2014-06-24 13:10:43 -0700160
robertphillips6f294af2014-08-18 08:50:03 -0700161bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas) {
robertphillips21048b52014-07-15 19:46:35 -0700162 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700163
robertphillips320c9232014-07-29 06:07:19 -0700164 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700165 // This layer is already locked
robertphillipsa32c6bc2014-10-09 12:47:01 -0700166#ifdef SK_DEBUG
robertphillips21048b52014-07-15 19:46:35 -0700167 if (layer->isAtlased()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700168 // It claims to be atlased
robertphillips6f294af2014-08-18 08:50:03 -0700169 SkASSERT(!dontAtlas);
robertphillips952841b2014-06-30 08:26:50 -0700170 SkASSERT(layer->rect().width() == desc.fWidth);
171 SkASSERT(layer->rect().height() == desc.fHeight);
172 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700173#endif
robertphillips6f294af2014-08-18 08:50:03 -0700174 return false;
robertphillips952841b2014-06-30 08:26:50 -0700175 }
176
robertphillips320c9232014-07-29 06:07:19 -0700177 if (layer->isAtlased()) {
178 // Hooray it is still in the atlas - make sure it stays there
robertphillips6f294af2014-08-18 08:50:03 -0700179 SkASSERT(!dontAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700180 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700181 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700182 return false;
183 } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
robertphillips320c9232014-07-29 06:07:19 -0700184 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700185 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700186 if (NULL == pictInfo) {
187 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700188 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700189 }
190
191 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700192 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
193 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
194 desc.fWidth, desc.fHeight,
195 NULL, &loc);
196 // addToAtlas can allocate the backing texture
197 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700198 if (plot) {
robertphillips320c9232014-07-29 06:07:19 -0700199 // The layer was successfully added to the atlas
200 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700201 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700202 SkToS16(desc.fHeight));
203 layer->setTexture(fAtlas->getTexture(), bounds);
204 layer->setPlot(plot);
205 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700206 this->incPlotLock(layer->plot()->id());
robertphillips6f294af2014-08-18 08:50:03 -0700207 return true;
robertphillips320c9232014-07-29 06:07:19 -0700208 }
209
210 // The layer was rejected by the atlas (even though we know it is
211 // plausibly atlas-able). See if a plot can be purged and try again.
212 if (!this->purgePlot()) {
213 break; // We weren't able to purge any plots
214 }
robertphillips261b8a92014-07-17 08:26:44 -0700215 }
robertphillips952841b2014-06-30 08:26:50 -0700216 }
robertphillips952841b2014-06-30 08:26:50 -0700217
robertphillips21048b52014-07-15 19:46:35 -0700218 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700219 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700220 // This can yield a lot of re-rendering
bsalomone3059732014-10-14 11:47:22 -0700221 SkAutoTUnref<GrTexture> tex(
222 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700223
224 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700225 layer->setLocked(true);
robertphillips6f294af2014-08-18 08:50:03 -0700226 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700227}
228
229void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700230 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
231
robertphillipsa32c6bc2014-10-09 12:47:01 -0700232 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700233 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700234 return;
235 }
236
robertphillips21048b52014-07-15 19:46:35 -0700237 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700238 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700239
robertphillips7bb9ed72014-10-10 11:38:29 -0700240 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700241 // At this point we could aggressively clear out un-locked plots but
242 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips6f294af2014-08-18 08:50:03 -0700243#if DISABLE_CACHING
robertphillips0c423322014-07-31 11:02:38 -0700244 // This testing code aggressively removes the atlased layers. This
245 // can be used to separate the performance contribution of less
246 // render target pingponging from that due to the re-use of cached layers
247 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700248 SkASSERT(pictInfo);
robertphillips0c423322014-07-31 11:02:38 -0700249
250 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
251
252 layer->setPlot(NULL);
253 layer->setTexture(NULL, GrIRect16::MakeEmpty());
254#endif
255
robertphillips21048b52014-07-15 19:46:35 -0700256 } else {
robertphillips952841b2014-06-30 08:26:50 -0700257 layer->setTexture(NULL, GrIRect16::MakeEmpty());
258 }
robertphillips320c9232014-07-29 06:07:19 -0700259
260 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700261}
262
robertphillips21048b52014-07-15 19:46:35 -0700263#ifdef SK_DEBUG
264void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700265 int plotLocks[kNumPlotsX * kNumPlotsY];
266 memset(plotLocks, 0, sizeof(plotLocks));
267
robertphillips3d533ac2014-07-20 09:40:00 -0700268 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
269 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700270 const GrCachedLayer* layer = &(*iter);
271
272 layer->validate(fAtlas->getTexture());
273
274 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700275 if (pictInfo) {
robertphillips320c9232014-07-29 06:07:19 -0700276 // In aggressive cleanup mode a picture info should only exist if
277 // it has some atlased layers
robertphillips6f294af2014-08-18 08:50:03 -0700278#if !DISABLE_CACHING
robertphillips320c9232014-07-29 06:07:19 -0700279 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
robertphillips6f294af2014-08-18 08:50:03 -0700280#endif
robertphillips320c9232014-07-29 06:07:19 -0700281 } else {
robertphillips7bb9ed72014-10-10 11:38:29 -0700282 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700283 // layers should be non-atlased.
284 SkASSERT(!layer->isAtlased());
285 }
286
bsalomon49f085d2014-09-05 13:34:00 -0700287 if (layer->plot()) {
288 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700289 SkASSERT(pictInfo->fPictureID == layer->pictureID());
290
291 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
292
293 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700294 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700295 }
296 }
297 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700298
299 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
300 SkASSERT(plotLocks[i] == fPlotLocks[i]);
301 }
robertphillips21048b52014-07-15 19:46:35 -0700302}
303
304class GrAutoValidateCache : ::SkNoncopyable {
305public:
306 explicit GrAutoValidateCache(GrLayerCache* cache)
307 : fCache(cache) {
308 fCache->validate();
309 }
310 ~GrAutoValidateCache() {
311 fCache->validate();
312 }
313private:
314 GrLayerCache* fCache;
315};
316#endif
317
robertphillipsd771f6b2014-07-22 10:18:06 -0700318void GrLayerCache::purge(uint32_t pictureID) {
319
robertphillips21048b52014-07-15 19:46:35 -0700320 SkDEBUGCODE(GrAutoValidateCache avc(this);)
321
robertphillips3d533ac2014-07-20 09:40:00 -0700322 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700323 SkTDArray<GrCachedLayer*> toBeRemoved;
324
robertphillips3d533ac2014-07-20 09:40:00 -0700325 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
326 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700327 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700328 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700329 }
330 }
331
332 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700333 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700334 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700335 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700336 SkDELETE(toBeRemoved[i]);
337 }
robertphillips261b8a92014-07-17 08:26:44 -0700338
robertphillipsd771f6b2014-07-22 10:18:06 -0700339 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700340 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700341 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700342 SkDELETE(pictInfo);
343 }
robertphillips4ec84da2014-06-24 13:10:43 -0700344}
robertphillipsd771f6b2014-07-22 10:18:06 -0700345
robertphillips320c9232014-07-29 06:07:19 -0700346bool GrLayerCache::purgePlot() {
347 SkDEBUGCODE(GrAutoValidateCache avc(this);)
348
349 GrAtlas::PlotIter iter;
350 GrPlot* plot;
351 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700352 plot;
robertphillips320c9232014-07-29 06:07:19 -0700353 plot = iter.prev()) {
354 if (fPlotLocks[plot->id()] > 0) {
355 continue;
356 }
357
robertphillips6f294af2014-08-18 08:50:03 -0700358 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700359 return true;
360 }
361
362 return false;
363}
364
robertphillips6f294af2014-08-18 08:50:03 -0700365void GrLayerCache::purgePlot(GrPlot* plot) {
366 SkASSERT(0 == fPlotLocks[plot->id()]);
367
368 // We need to find all the layers in 'plot' and remove them.
369 SkTDArray<GrCachedLayer*> toBeRemoved;
370
371 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
372 for (; !iter.done(); ++iter) {
373 if (plot == (*iter).plot()) {
374 *toBeRemoved.append() = &(*iter);
375 }
376 }
377
378 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700379 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700380 SkASSERT(!toBeRemoved[i]->locked());
381
robertphillips410dd052014-10-06 12:19:50 -0700382 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700383
robertphillips410dd052014-10-06 12:19:50 -0700384 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700385 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
386 SkDELETE(toBeRemoved[i]);
387
robertphillips410dd052014-10-06 12:19:50 -0700388 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
389 if (pictInfo) {
390 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
391
392 if (pictInfo->fPlotUsage.isEmpty()) {
393 fPictureHash.remove(pictInfo->fPictureID);
394 SkDELETE(pictInfo);
395 }
robertphillips6f294af2014-08-18 08:50:03 -0700396 }
397 }
398
399 plot->resetRects();
400}
401
402void GrLayerCache::purgeAll() {
403 GrAtlas::PlotIter iter;
404 GrPlot* plot;
405 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700406 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700407 plot = iter.prev()) {
408 SkASSERT(0 == fPlotLocks[plot->id()]);
409
410 this->purgePlot(plot);
411 }
412}
413
robertphillipsd771f6b2014-07-22 10:18:06 -0700414class GrPictureDeletionListener : public SkPicture::DeletionListener {
415 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
416 const GrPictureDeletedMessage message = { pictureID };
417 SkMessageBus<GrPictureDeletedMessage>::Post(message);
418 }
419};
420
421void GrLayerCache::trackPicture(const SkPicture* picture) {
422 if (NULL == fDeletionListener) {
423 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
424 }
425
426 picture->addDeletionListener(fDeletionListener);
427}
428
429void GrLayerCache::processDeletedPictures() {
430 SkTDArray<GrPictureDeletedMessage> deletedPictures;
431 fPictDeletionInbox.poll(&deletedPictures);
432
433 for (int i = 0; i < deletedPictures.count(); i++) {
434 this->purge(deletedPictures[i].pictureID);
435 }
436}
437
robertphillips84ac0822014-10-14 07:07:59 -0700438#ifdef SK_DEVELOPER
439void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
440
441 GrTexture* atlasTexture = fAtlas->getTexture();
442 if (NULL != atlasTexture) {
443 SkString fileName(dirName);
444 fileName.append("\\atlas.png");
445
446 atlasTexture->surfacePriv().savePixels(fileName.c_str());
447 }
448
449 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
450 for (; !iter.done(); ++iter) {
451 GrCachedLayer* layer = &(*iter);
452
453 if (layer->isAtlased() || !layer->texture()) {
454 continue;
455 }
456
457 SkString fileName(dirName);
458 fileName.append("\\");
459 fileName.appendU32(layer->fKey.pictureID());
460 fileName.append("-");
461 fileName.appendU32(layer->fKey.start());
462 fileName.append(".png");
463
464 layer->texture()->surfacePriv().savePixels(fileName.c_str());
465 }
466}
467#endif