blob: d4f8b70b393858cf8db77f1f116fe893a551b5dd [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
robertphillipsb32f0ad2014-11-04 06:46:11 -080097 SkASSERT(0 == fPictureHash.count());
98
robertphillips952841b2014-06-30 08:26:50 -070099 // The atlas only lets go of its texture when the atlas is deleted.
100 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000101}
102
robertphillips952841b2014-06-30 08:26:50 -0700103void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -0700104 SkASSERT(NULL == fAtlas.get());
robertphillips225a6272014-10-30 11:39:19 -0700105 GR_STATIC_ASSERT(kNumPlotsX*kNumPlotsX == GrPictureInfo::kNumPlots);
robertphillips@google.come930a072014-04-03 00:34:27 +0000106
robertphillips@google.come930a072014-04-03 00:34:27 +0000107 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -0700108 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
bsalomonf2703d82014-10-28 14:33:06 -0700109 kRenderTarget_GrSurfaceFlag,
robertphillips261b8a92014-07-17 08:26:44 -0700110 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000111}
112
113void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700114
robertphillips3d533ac2014-07-20 09:40:00 -0700115 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
116 for (; !iter.done(); ++iter) {
117 GrCachedLayer* layer = &(*iter);
118 this->unlock(layer);
119 SkDELETE(layer);
120 }
121 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700122
123 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700124 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000125}
126
robertphillips6f294af2014-08-18 08:50:03 -0700127GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700128 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700129 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700130 const SkMatrix& ctm,
131 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700132 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700133
robertphillips3aac6e02014-10-20 08:52:40 -0700134 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, bounds, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700135 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000136 return layer;
137}
138
robertphillips6f294af2014-08-18 08:50:03 -0700139GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
robertphillipsed420592014-09-29 11:39:38 -0700140 int start,
robertphillips3aac6e02014-10-20 08:52:40 -0700141 const SkIRect& bounds,
robertphillips0c423322014-07-31 11:02:38 -0700142 const SkMatrix& ctm) {
robertphillipsed420592014-09-29 11:39:38 -0700143 SkASSERT(pictureID != SK_InvalidGenID && start > 0);
robertphillips3aac6e02014-10-20 08:52:40 -0700144 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700145}
robertphillips@google.come930a072014-04-03 00:34:27 +0000146
robertphillips6f294af2014-08-18 08:50:03 -0700147GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700148 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700149 const SkIRect& bounds,
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);
robertphillips3aac6e02014-10-20 08:52:40 -0700153 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000154 if (NULL == layer) {
robertphillips3aac6e02014-10-20 08:52:40 -0700155 layer = this->createLayer(pictureID, start, stop, bounds, 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
robertphillipsfd61ed02014-10-28 07:21:44 -0700161bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
bsalomonf2703d82014-10-28 14:33:06 -0700162 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700163 bool* needsRendering) {
robertphillips4ab5a902014-10-29 13:56:02 -0700164 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700165
robertphillipsfd61ed02014-10-28 07:21:44 -0700166 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
167
robertphillips320c9232014-07-29 06:07:19 -0700168 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700169 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700170 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700171 SkASSERT(layer->isAtlased());
172 SkASSERT(layer->rect().width() == desc.fWidth);
173 SkASSERT(layer->rect().height() == desc.fHeight);
174 *needsRendering = false;
175 return true;
robertphillips952841b2014-06-30 08:26:50 -0700176 }
177
robertphillips320c9232014-07-29 06:07:19 -0700178 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700179 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700180 // Hooray it is still in the atlas - make sure it stays there
181 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700182 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700183 *needsRendering = false;
184 return true;
185 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700186 if (!fAtlas) {
187 this->initAtlas();
188 if (!fAtlas) {
189 return false;
190 }
191 }
robertphillips320c9232014-07-29 06:07:19 -0700192 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700193 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700194 if (NULL == pictInfo) {
195 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700196 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700197 }
198
199 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700200 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
201 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
202 desc.fWidth, desc.fHeight,
203 NULL, &loc);
204 // addToAtlas can allocate the backing texture
205 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700206 if (plot) {
robertphillips225a6272014-10-30 11:39:19 -0700207#if !GR_CACHE_HOISTED_LAYERS
208 pictInfo->incPlotUsage(plot->id());
209#endif
robertphillips320c9232014-07-29 06:07:19 -0700210 // The layer was successfully added to the atlas
211 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700212 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700213 SkToS16(desc.fHeight));
214 layer->setTexture(fAtlas->getTexture(), bounds);
215 layer->setPlot(plot);
216 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700217 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700218 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700219 return true;
robertphillips320c9232014-07-29 06:07:19 -0700220 }
221
222 // The layer was rejected by the atlas (even though we know it is
223 // plausibly atlas-able). See if a plot can be purged and try again.
224 if (!this->purgePlot()) {
225 break; // We weren't able to purge any plots
226 }
robertphillips261b8a92014-07-17 08:26:44 -0700227 }
robertphillips952841b2014-06-30 08:26:50 -0700228 }
robertphillips952841b2014-06-30 08:26:50 -0700229
robertphillipsfd61ed02014-10-28 07:21:44 -0700230 return false;
231}
232
bsalomonf2703d82014-10-28 14:33:06 -0700233bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700234 if (layer->locked()) {
235 // This layer is already locked
236 *needsRendering = false;
237 return true;
238 }
239
bsalomone3059732014-10-14 11:47:22 -0700240 SkAutoTUnref<GrTexture> tex(
241 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700242
robertphillipsfd61ed02014-10-28 07:21:44 -0700243 if (!tex) {
244 return false;
245 }
246
robertphillipsed6f03e2014-07-30 07:31:35 -0700247 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700248 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700249 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700250 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700251}
252
253void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700254 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700255
robertphillipsa32c6bc2014-10-09 12:47:01 -0700256 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700257 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700258 return;
259 }
260
robertphillips21048b52014-07-15 19:46:35 -0700261 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700262 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700263
robertphillips7bb9ed72014-10-10 11:38:29 -0700264 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700265 // At this point we could aggressively clear out un-locked plots but
266 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700267#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700268 // This testing code aggressively removes the atlased layers. This
269 // can be used to separate the performance contribution of less
270 // render target pingponging from that due to the re-use of cached layers
271 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700272 SkASSERT(pictInfo);
robertphillips225a6272014-10-30 11:39:19 -0700273
274 pictInfo->decPlotUsage(plotID);
275
276 if (0 == pictInfo->plotUsage(plotID)) {
277 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
robertphillipsb32f0ad2014-11-04 06:46:11 -0800278
279 if (pictInfo->fPlotUsage.isEmpty()) {
280 fPictureHash.remove(pictInfo->fPictureID);
281 SkDELETE(pictInfo);
282 }
robertphillips225a6272014-10-30 11:39:19 -0700283 }
robertphillips0c423322014-07-31 11:02:38 -0700284
285 layer->setPlot(NULL);
286 layer->setTexture(NULL, GrIRect16::MakeEmpty());
287#endif
288
robertphillips21048b52014-07-15 19:46:35 -0700289 } else {
robertphillips952841b2014-06-30 08:26:50 -0700290 layer->setTexture(NULL, GrIRect16::MakeEmpty());
291 }
robertphillips320c9232014-07-29 06:07:19 -0700292
293 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700294}
295
robertphillips21048b52014-07-15 19:46:35 -0700296#ifdef SK_DEBUG
297void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700298 int plotLocks[kNumPlotsX * kNumPlotsY];
299 memset(plotLocks, 0, sizeof(plotLocks));
300
robertphillips3d533ac2014-07-20 09:40:00 -0700301 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
302 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700303 const GrCachedLayer* layer = &(*iter);
304
robertphillips4ab5a902014-10-29 13:56:02 -0700305 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700306
307 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700308 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700309 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700310 // layers should be non-atlased.
311 SkASSERT(!layer->isAtlased());
312 }
313
bsalomon49f085d2014-09-05 13:34:00 -0700314 if (layer->plot()) {
315 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700316 SkASSERT(pictInfo->fPictureID == layer->pictureID());
317
318 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
robertphillips225a6272014-10-30 11:39:19 -0700319#if !GR_CACHE_HOISTED_LAYERS
320 SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0);
321#endif
robertphillips320c9232014-07-29 06:07:19 -0700322
323 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700324 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700325 }
326 }
327 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700328
329 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
330 SkASSERT(plotLocks[i] == fPlotLocks[i]);
331 }
robertphillips21048b52014-07-15 19:46:35 -0700332}
333
334class GrAutoValidateCache : ::SkNoncopyable {
335public:
336 explicit GrAutoValidateCache(GrLayerCache* cache)
337 : fCache(cache) {
338 fCache->validate();
339 }
340 ~GrAutoValidateCache() {
341 fCache->validate();
342 }
343private:
344 GrLayerCache* fCache;
345};
346#endif
347
robertphillipsd771f6b2014-07-22 10:18:06 -0700348void GrLayerCache::purge(uint32_t pictureID) {
349
robertphillips21048b52014-07-15 19:46:35 -0700350 SkDEBUGCODE(GrAutoValidateCache avc(this);)
351
robertphillips3d533ac2014-07-20 09:40:00 -0700352 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700353 SkTDArray<GrCachedLayer*> toBeRemoved;
354
robertphillips3d533ac2014-07-20 09:40:00 -0700355 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
356 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700357 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700358 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700359 }
360 }
361
362 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700363 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700364 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700365 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700366 SkDELETE(toBeRemoved[i]);
367 }
robertphillips261b8a92014-07-17 08:26:44 -0700368
robertphillipsd771f6b2014-07-22 10:18:06 -0700369 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700370 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700371 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700372 SkDELETE(pictInfo);
373 }
robertphillips4ec84da2014-06-24 13:10:43 -0700374}
robertphillipsd771f6b2014-07-22 10:18:06 -0700375
robertphillips320c9232014-07-29 06:07:19 -0700376bool GrLayerCache::purgePlot() {
377 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700378 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700379
380 GrAtlas::PlotIter iter;
381 GrPlot* plot;
382 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700383 plot;
robertphillips320c9232014-07-29 06:07:19 -0700384 plot = iter.prev()) {
385 if (fPlotLocks[plot->id()] > 0) {
386 continue;
387 }
388
robertphillips6f294af2014-08-18 08:50:03 -0700389 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700390 return true;
391 }
392
393 return false;
394}
395
robertphillips6f294af2014-08-18 08:50:03 -0700396void GrLayerCache::purgePlot(GrPlot* plot) {
397 SkASSERT(0 == fPlotLocks[plot->id()]);
398
399 // We need to find all the layers in 'plot' and remove them.
400 SkTDArray<GrCachedLayer*> toBeRemoved;
401
402 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
403 for (; !iter.done(); ++iter) {
404 if (plot == (*iter).plot()) {
405 *toBeRemoved.append() = &(*iter);
406 }
407 }
408
409 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700410 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700411 SkASSERT(!toBeRemoved[i]->locked());
412
robertphillips410dd052014-10-06 12:19:50 -0700413 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700414
robertphillips410dd052014-10-06 12:19:50 -0700415 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700416 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
417 SkDELETE(toBeRemoved[i]);
418
robertphillips410dd052014-10-06 12:19:50 -0700419 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
420 if (pictInfo) {
robertphillips225a6272014-10-30 11:39:19 -0700421#if !GR_CACHE_HOISTED_LAYERS
422 SkASSERT(0 == pictInfo->plotUsage(plot->id()));
423#endif
robertphillips410dd052014-10-06 12:19:50 -0700424 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
425
426 if (pictInfo->fPlotUsage.isEmpty()) {
427 fPictureHash.remove(pictInfo->fPictureID);
428 SkDELETE(pictInfo);
429 }
robertphillips6f294af2014-08-18 08:50:03 -0700430 }
431 }
432
433 plot->resetRects();
434}
435
robertphillips4ab5a902014-10-29 13:56:02 -0700436#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700437void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700438 if (!fAtlas) {
439 return;
440 }
441
robertphillips6f294af2014-08-18 08:50:03 -0700442 GrAtlas::PlotIter iter;
443 GrPlot* plot;
444 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700445 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700446 plot = iter.prev()) {
447 SkASSERT(0 == fPlotLocks[plot->id()]);
448
449 this->purgePlot(plot);
450 }
robertphillips4ab5a902014-10-29 13:56:02 -0700451
robertphillipsb32f0ad2014-11-04 06:46:11 -0800452 SkASSERT(0 == fPictureHash.count());
453
robertphillips4ab5a902014-10-29 13:56:02 -0700454 fContext->discardRenderTarget(fAtlas->getTexture()->asRenderTarget());
robertphillips6f294af2014-08-18 08:50:03 -0700455}
robertphillips4ab5a902014-10-29 13:56:02 -0700456#endif
robertphillips6f294af2014-08-18 08:50:03 -0700457
robertphillipsd771f6b2014-07-22 10:18:06 -0700458class GrPictureDeletionListener : public SkPicture::DeletionListener {
459 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
460 const GrPictureDeletedMessage message = { pictureID };
461 SkMessageBus<GrPictureDeletedMessage>::Post(message);
462 }
463};
464
465void GrLayerCache::trackPicture(const SkPicture* picture) {
466 if (NULL == fDeletionListener) {
467 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
468 }
469
470 picture->addDeletionListener(fDeletionListener);
471}
472
473void GrLayerCache::processDeletedPictures() {
474 SkTDArray<GrPictureDeletedMessage> deletedPictures;
475 fPictDeletionInbox.poll(&deletedPictures);
476
477 for (int i = 0; i < deletedPictures.count(); i++) {
478 this->purge(deletedPictures[i].pictureID);
479 }
480}
481
robertphillips84ac0822014-10-14 07:07:59 -0700482#ifdef SK_DEVELOPER
483void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
484
robertphillips4ab5a902014-10-29 13:56:02 -0700485 if (fAtlas) {
486 GrTexture* atlasTexture = fAtlas->getTexture();
487 if (NULL != atlasTexture) {
488 SkString fileName(dirName);
489 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700490
robertphillips4ab5a902014-10-29 13:56:02 -0700491 atlasTexture->surfacePriv().savePixels(fileName.c_str());
492 }
robertphillips84ac0822014-10-14 07:07:59 -0700493 }
494
495 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
496 for (; !iter.done(); ++iter) {
497 GrCachedLayer* layer = &(*iter);
498
499 if (layer->isAtlased() || !layer->texture()) {
500 continue;
501 }
502
503 SkString fileName(dirName);
robertphillips3aac6e02014-10-20 08:52:40 -0700504 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.start());
robertphillips84ac0822014-10-14 07:07:59 -0700505
506 layer->texture()->surfacePriv().savePixels(fileName.c_str());
507 }
508}
509#endif