blob: a24c9e040b70bb7ecd3581e48cd9dacc4cd4c617 [file] [log] [blame]
robertphillips@google.come930a072014-04-03 00:34:27 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrAtlas.h"
9#include "GrGpu.h"
10#include "GrLayerCache.h"
robertphillips84ac0822014-10-14 07:07:59 -070011#include "GrSurfacePriv.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000012
robertphillipsd771f6b2014-07-22 10:18:06 -070013DECLARE_SKMESSAGEBUS_MESSAGE(GrPictureDeletedMessage);
14
robertphillips21048b52014-07-15 19:46:35 -070015#ifdef SK_DEBUG
robertphillips261b8a92014-07-17 08:26:44 -070016void GrCachedLayer::validate(const GrTexture* backingTexture) const {
robertphillips0c423322014-07-31 11:02:38 -070017 SkASSERT(SK_InvalidGenID != fKey.pictureID());
robertphillips410dd052014-10-06 12:19:50 -070018 SkASSERT(fKey.start() >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -070019
bsalomon49f085d2014-09-05 13:34:00 -070020 if (fTexture) {
robertphillips21048b52014-07-15 19:46:35 -070021 // If the layer is in some texture then it must occupy some rectangle
22 SkASSERT(!fRect.isEmpty());
23 if (!this->isAtlased()) {
24 // If it isn't atlased then the rectangle should start at the origin
25 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
26 }
27 } else {
28 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070029 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070030 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070031 }
32
bsalomon49f085d2014-09-05 13:34:00 -070033 if (fPlot) {
robertphillips261b8a92014-07-17 08:26:44 -070034 // If a layer has a plot (i.e., is atlased) then it must point to
35 // the backing texture. Additionally, its rect should be non-empty.
bsalomon49f085d2014-09-05 13:34:00 -070036 SkASSERT(fTexture && backingTexture == fTexture);
robertphillips261b8a92014-07-17 08:26:44 -070037 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070038 }
robertphillips320c9232014-07-29 06:07:19 -070039
40 if (fLocked) {
41 // If a layer is locked it must have a texture (though it need not be
42 // the atlas-backing texture) and occupy some space.
bsalomon49f085d2014-09-05 13:34:00 -070043 SkASSERT(fTexture);
robertphillips320c9232014-07-29 06:07:19 -070044 SkASSERT(!fRect.isEmpty());
45 }
robertphillips7bb9ed72014-10-10 11:38:29 -070046
47 // Unfortunately there is a brief time where a layer can be locked
48 // but not used, so we can only check the "used implies locked"
49 // invariant.
50 if (fUses > 0) {
51 SkASSERT(fLocked);
52 } else {
53 SkASSERT(0 == fUses);
54 }
robertphillips21048b52014-07-15 19:46:35 -070055}
56
57class GrAutoValidateLayer : ::SkNoncopyable {
58public:
59 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
60 : fBackingTexture(backingTexture)
61 , fLayer(layer) {
bsalomon49f085d2014-09-05 13:34:00 -070062 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070063 fLayer->validate(backingTexture);
64 }
65 }
66 ~GrAutoValidateLayer() {
bsalomon49f085d2014-09-05 13:34:00 -070067 if (fLayer) {
robertphillips21048b52014-07-15 19:46:35 -070068 fLayer->validate(fBackingTexture);
69 }
70 }
robertphillips261b8a92014-07-17 08:26:44 -070071 void setBackingTexture(GrTexture* backingTexture) {
72 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
73 fBackingTexture = backingTexture;
74 }
robertphillips21048b52014-07-15 19:46:35 -070075
76private:
robertphillips261b8a92014-07-17 08:26:44 -070077 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070078 const GrCachedLayer* fLayer;
79};
80#endif
81
robertphillips4ec84da2014-06-24 13:10:43 -070082GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070083 : fContext(context) {
robertphillips320c9232014-07-29 06:07:19 -070084 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000085}
86
87GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070088
robertphillips3d533ac2014-07-20 09:40:00 -070089 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
90 for (; !iter.done(); ++iter) {
91 GrCachedLayer* layer = &(*iter);
robertphillips7bb9ed72014-10-10 11:38:29 -070092 SkASSERT(0 == layer->uses());
robertphillips3d533ac2014-07-20 09:40:00 -070093 this->unlock(layer);
94 SkDELETE(layer);
95 }
robertphillips952841b2014-06-30 08:26:50 -070096
97 // The atlas only lets go of its texture when the atlas is deleted.
98 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000099}
100
robertphillips952841b2014-06-30 08:26:50 -0700101void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -0700102 SkASSERT(NULL == fAtlas.get());
robertphillips@google.come930a072014-04-03 00:34:27 +0000103
robertphillips@google.come930a072014-04-03 00:34:27 +0000104 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -0700105 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
bsalomonf2703d82014-10-28 14:33:06 -0700106 kRenderTarget_GrSurfaceFlag,
robertphillips261b8a92014-07-17 08:26:44 -0700107 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000108}
109
110void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700111
robertphillips3d533ac2014-07-20 09:40:00 -0700112 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
113 for (; !iter.done(); ++iter) {
114 GrCachedLayer* layer = &(*iter);
115 this->unlock(layer);
116 SkDELETE(layer);
117 }
118 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700119
120 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700121 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +0000122}
123
robertphillips6f294af2014-08-18 08:50:03 -0700124GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700125 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700126 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700127 const SkMatrix& ctm,
128 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700129 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips952841b2014-06-30 08:26:50 -0700130
robertphillips3aac6e02014-10-20 08:52:40 -0700131 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, bounds, ctm, paint));
robertphillips3d533ac2014-07-20 09:40:00 -0700132 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000133 return layer;
134}
135
robertphillips6f294af2014-08-18 08:50:03 -0700136GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID,
robertphillipsed420592014-09-29 11:39:38 -0700137 int start,
robertphillips3aac6e02014-10-20 08:52:40 -0700138 const SkIRect& bounds,
robertphillips0c423322014-07-31 11:02:38 -0700139 const SkMatrix& ctm) {
robertphillipsed420592014-09-29 11:39:38 -0700140 SkASSERT(pictureID != SK_InvalidGenID && start > 0);
robertphillips3aac6e02014-10-20 08:52:40 -0700141 return fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips4ec84da2014-06-24 13:10:43 -0700142}
robertphillips@google.come930a072014-04-03 00:34:27 +0000143
robertphillips6f294af2014-08-18 08:50:03 -0700144GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700145 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700146 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700147 const SkMatrix& ctm,
148 const SkPaint* paint) {
robertphillips410dd052014-10-06 12:19:50 -0700149 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0);
robertphillips3aac6e02014-10-20 08:52:40 -0700150 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, bounds, ctm));
robertphillips@google.come930a072014-04-03 00:34:27 +0000151 if (NULL == layer) {
robertphillips3aac6e02014-10-20 08:52:40 -0700152 layer = this->createLayer(pictureID, start, stop, bounds, ctm, paint);
robertphillips@google.come930a072014-04-03 00:34:27 +0000153 }
robertphillips4ec84da2014-06-24 13:10:43 -0700154
robertphillips@google.come930a072014-04-03 00:34:27 +0000155 return layer;
156}
robertphillips4ec84da2014-06-24 13:10:43 -0700157
robertphillipsfd61ed02014-10-28 07:21:44 -0700158bool GrLayerCache::tryToAtlas(GrCachedLayer* layer,
bsalomonf2703d82014-10-28 14:33:06 -0700159 const GrSurfaceDesc& desc,
robertphillipsfd61ed02014-10-28 07:21:44 -0700160 bool* needsRendering) {
robertphillips4ab5a902014-10-29 13:56:02 -0700161 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700162
robertphillipsfd61ed02014-10-28 07:21:44 -0700163 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight));
164
robertphillips320c9232014-07-29 06:07:19 -0700165 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700166 // This layer is already locked
robertphillips4ab5a902014-10-29 13:56:02 -0700167 SkASSERT(fAtlas);
robertphillipsfd61ed02014-10-28 07:21:44 -0700168 SkASSERT(layer->isAtlased());
169 SkASSERT(layer->rect().width() == desc.fWidth);
170 SkASSERT(layer->rect().height() == desc.fHeight);
171 *needsRendering = false;
172 return true;
robertphillips952841b2014-06-30 08:26:50 -0700173 }
174
robertphillips320c9232014-07-29 06:07:19 -0700175 if (layer->isAtlased()) {
robertphillips4ab5a902014-10-29 13:56:02 -0700176 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700177 // Hooray it is still in the atlas - make sure it stays there
178 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700179 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700180 *needsRendering = false;
181 return true;
182 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700183 if (!fAtlas) {
184 this->initAtlas();
185 if (!fAtlas) {
186 return false;
187 }
188 }
robertphillips320c9232014-07-29 06:07:19 -0700189 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700190 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700191 if (NULL == pictInfo) {
192 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700193 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700194 }
195
196 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700197 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
198 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
199 desc.fWidth, desc.fHeight,
200 NULL, &loc);
201 // addToAtlas can allocate the backing texture
202 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700203 if (plot) {
robertphillips320c9232014-07-29 06:07:19 -0700204 // The layer was successfully added to the atlas
205 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
robertphillips6f294af2014-08-18 08:50:03 -0700206 SkToS16(desc.fWidth),
robertphillips320c9232014-07-29 06:07:19 -0700207 SkToS16(desc.fHeight));
208 layer->setTexture(fAtlas->getTexture(), bounds);
209 layer->setPlot(plot);
210 layer->setLocked(true);
robertphillips7bb9ed72014-10-10 11:38:29 -0700211 this->incPlotLock(layer->plot()->id());
robertphillipsfd61ed02014-10-28 07:21:44 -0700212 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700213 return true;
robertphillips320c9232014-07-29 06:07:19 -0700214 }
215
216 // The layer was rejected by the atlas (even though we know it is
217 // plausibly atlas-able). See if a plot can be purged and try again.
218 if (!this->purgePlot()) {
219 break; // We weren't able to purge any plots
220 }
robertphillips261b8a92014-07-17 08:26:44 -0700221 }
robertphillips952841b2014-06-30 08:26:50 -0700222 }
robertphillips952841b2014-06-30 08:26:50 -0700223
robertphillipsfd61ed02014-10-28 07:21:44 -0700224 return false;
225}
226
bsalomonf2703d82014-10-28 14:33:06 -0700227bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) {
robertphillipsfd61ed02014-10-28 07:21:44 -0700228 if (layer->locked()) {
229 // This layer is already locked
230 *needsRendering = false;
231 return true;
232 }
233
bsalomone3059732014-10-14 11:47:22 -0700234 SkAutoTUnref<GrTexture> tex(
235 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
robertphillipsed6f03e2014-07-30 07:31:35 -0700236
robertphillipsfd61ed02014-10-28 07:21:44 -0700237 if (!tex) {
238 return false;
239 }
240
robertphillipsed6f03e2014-07-30 07:31:35 -0700241 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700242 layer->setLocked(true);
robertphillipsfd61ed02014-10-28 07:21:44 -0700243 *needsRendering = true;
robertphillips6f294af2014-08-18 08:50:03 -0700244 return true;
robertphillips4ec84da2014-06-24 13:10:43 -0700245}
246
247void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips4ab5a902014-10-29 13:56:02 -0700248 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);)
robertphillips21048b52014-07-15 19:46:35 -0700249
robertphillipsa32c6bc2014-10-09 12:47:01 -0700250 if (NULL == layer || !layer->locked()) {
robertphillips320c9232014-07-29 06:07:19 -0700251 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700252 return;
253 }
254
robertphillips21048b52014-07-15 19:46:35 -0700255 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700256 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700257
robertphillips7bb9ed72014-10-10 11:38:29 -0700258 this->decPlotLock(plotID);
robertphillips320c9232014-07-29 06:07:19 -0700259 // At this point we could aggressively clear out un-locked plots but
260 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips4ab5a902014-10-29 13:56:02 -0700261#if !GR_CACHE_HOISTED_LAYERS
robertphillips0c423322014-07-31 11:02:38 -0700262 // This testing code aggressively removes the atlased layers. This
263 // can be used to separate the performance contribution of less
264 // render target pingponging from that due to the re-use of cached layers
265 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
bsalomon49f085d2014-09-05 13:34:00 -0700266 SkASSERT(pictInfo);
robertphillips0c423322014-07-31 11:02:38 -0700267
268 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot());
269
270 layer->setPlot(NULL);
271 layer->setTexture(NULL, GrIRect16::MakeEmpty());
272#endif
273
robertphillips21048b52014-07-15 19:46:35 -0700274 } else {
robertphillips952841b2014-06-30 08:26:50 -0700275 layer->setTexture(NULL, GrIRect16::MakeEmpty());
276 }
robertphillips320c9232014-07-29 06:07:19 -0700277
278 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700279}
280
robertphillips21048b52014-07-15 19:46:35 -0700281#ifdef SK_DEBUG
282void GrLayerCache::validate() const {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700283 int plotLocks[kNumPlotsX * kNumPlotsY];
284 memset(plotLocks, 0, sizeof(plotLocks));
285
robertphillips3d533ac2014-07-20 09:40:00 -0700286 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
287 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700288 const GrCachedLayer* layer = &(*iter);
289
robertphillips4ab5a902014-10-29 13:56:02 -0700290 layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL);
robertphillips320c9232014-07-29 06:07:19 -0700291
292 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillipsfd61ed02014-10-28 07:21:44 -0700293 if (!pictInfo) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700294 // If there is no picture info for this picture then all of its
robertphillips320c9232014-07-29 06:07:19 -0700295 // layers should be non-atlased.
296 SkASSERT(!layer->isAtlased());
297 }
298
bsalomon49f085d2014-09-05 13:34:00 -0700299 if (layer->plot()) {
300 SkASSERT(pictInfo);
robertphillips320c9232014-07-29 06:07:19 -0700301 SkASSERT(pictInfo->fPictureID == layer->pictureID());
302
303 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
304
305 if (layer->locked()) {
robertphillipsa32c6bc2014-10-09 12:47:01 -0700306 plotLocks[layer->plot()->id()]++;
robertphillips320c9232014-07-29 06:07:19 -0700307 }
308 }
309 }
robertphillipsa32c6bc2014-10-09 12:47:01 -0700310
311 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
312 SkASSERT(plotLocks[i] == fPlotLocks[i]);
313 }
robertphillips21048b52014-07-15 19:46:35 -0700314}
315
316class GrAutoValidateCache : ::SkNoncopyable {
317public:
318 explicit GrAutoValidateCache(GrLayerCache* cache)
319 : fCache(cache) {
320 fCache->validate();
321 }
322 ~GrAutoValidateCache() {
323 fCache->validate();
324 }
325private:
326 GrLayerCache* fCache;
327};
328#endif
329
robertphillipsd771f6b2014-07-22 10:18:06 -0700330void GrLayerCache::purge(uint32_t pictureID) {
331
robertphillips21048b52014-07-15 19:46:35 -0700332 SkDEBUGCODE(GrAutoValidateCache avc(this);)
333
robertphillips3d533ac2014-07-20 09:40:00 -0700334 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700335 SkTDArray<GrCachedLayer*> toBeRemoved;
336
robertphillips3d533ac2014-07-20 09:40:00 -0700337 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
338 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700339 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700340 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700341 }
342 }
343
344 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700345 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips952841b2014-06-30 08:26:50 -0700346 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700347 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700348 SkDELETE(toBeRemoved[i]);
349 }
robertphillips261b8a92014-07-17 08:26:44 -0700350
robertphillipsd771f6b2014-07-22 10:18:06 -0700351 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
bsalomon49f085d2014-09-05 13:34:00 -0700352 if (pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700353 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700354 SkDELETE(pictInfo);
355 }
robertphillips4ec84da2014-06-24 13:10:43 -0700356}
robertphillipsd771f6b2014-07-22 10:18:06 -0700357
robertphillips320c9232014-07-29 06:07:19 -0700358bool GrLayerCache::purgePlot() {
359 SkDEBUGCODE(GrAutoValidateCache avc(this);)
robertphillips4ab5a902014-10-29 13:56:02 -0700360 SkASSERT(fAtlas);
robertphillips320c9232014-07-29 06:07:19 -0700361
362 GrAtlas::PlotIter iter;
363 GrPlot* plot;
364 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700365 plot;
robertphillips320c9232014-07-29 06:07:19 -0700366 plot = iter.prev()) {
367 if (fPlotLocks[plot->id()] > 0) {
368 continue;
369 }
370
robertphillips6f294af2014-08-18 08:50:03 -0700371 this->purgePlot(plot);
robertphillips320c9232014-07-29 06:07:19 -0700372 return true;
373 }
374
375 return false;
376}
377
robertphillips6f294af2014-08-18 08:50:03 -0700378void GrLayerCache::purgePlot(GrPlot* plot) {
379 SkASSERT(0 == fPlotLocks[plot->id()]);
380
381 // We need to find all the layers in 'plot' and remove them.
382 SkTDArray<GrCachedLayer*> toBeRemoved;
383
384 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
385 for (; !iter.done(); ++iter) {
386 if (plot == (*iter).plot()) {
387 *toBeRemoved.append() = &(*iter);
388 }
389 }
390
391 for (int i = 0; i < toBeRemoved.count(); ++i) {
robertphillips7bb9ed72014-10-10 11:38:29 -0700392 SkASSERT(0 == toBeRemoved[i]->uses());
robertphillips6f294af2014-08-18 08:50:03 -0700393 SkASSERT(!toBeRemoved[i]->locked());
394
robertphillips410dd052014-10-06 12:19:50 -0700395 uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID();
robertphillips6f294af2014-08-18 08:50:03 -0700396
robertphillips410dd052014-10-06 12:19:50 -0700397 // Aggressively remove layers and, if it becomes totally uncached, delete the picture info
robertphillips6f294af2014-08-18 08:50:03 -0700398 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
399 SkDELETE(toBeRemoved[i]);
400
robertphillips410dd052014-10-06 12:19:50 -0700401 GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove);
402 if (pictInfo) {
403 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
404
405 if (pictInfo->fPlotUsage.isEmpty()) {
406 fPictureHash.remove(pictInfo->fPictureID);
407 SkDELETE(pictInfo);
408 }
robertphillips6f294af2014-08-18 08:50:03 -0700409 }
410 }
411
412 plot->resetRects();
413}
414
robertphillips4ab5a902014-10-29 13:56:02 -0700415#if !GR_CACHE_HOISTED_LAYERS
robertphillips6f294af2014-08-18 08:50:03 -0700416void GrLayerCache::purgeAll() {
robertphillips4ab5a902014-10-29 13:56:02 -0700417 if (!fAtlas) {
418 return;
419 }
420
robertphillips6f294af2014-08-18 08:50:03 -0700421 GrAtlas::PlotIter iter;
422 GrPlot* plot;
423 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
bsalomon49f085d2014-09-05 13:34:00 -0700424 plot;
robertphillips6f294af2014-08-18 08:50:03 -0700425 plot = iter.prev()) {
426 SkASSERT(0 == fPlotLocks[plot->id()]);
427
428 this->purgePlot(plot);
429 }
robertphillips4ab5a902014-10-29 13:56:02 -0700430
431 fContext->discardRenderTarget(fAtlas->getTexture()->asRenderTarget());
robertphillips6f294af2014-08-18 08:50:03 -0700432}
robertphillips4ab5a902014-10-29 13:56:02 -0700433#endif
robertphillips6f294af2014-08-18 08:50:03 -0700434
robertphillipsd771f6b2014-07-22 10:18:06 -0700435class GrPictureDeletionListener : public SkPicture::DeletionListener {
436 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
437 const GrPictureDeletedMessage message = { pictureID };
438 SkMessageBus<GrPictureDeletedMessage>::Post(message);
439 }
440};
441
442void GrLayerCache::trackPicture(const SkPicture* picture) {
443 if (NULL == fDeletionListener) {
444 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
445 }
446
447 picture->addDeletionListener(fDeletionListener);
448}
449
450void GrLayerCache::processDeletedPictures() {
451 SkTDArray<GrPictureDeletedMessage> deletedPictures;
452 fPictDeletionInbox.poll(&deletedPictures);
453
454 for (int i = 0; i < deletedPictures.count(); i++) {
455 this->purge(deletedPictures[i].pictureID);
456 }
457}
458
robertphillips84ac0822014-10-14 07:07:59 -0700459#ifdef SK_DEVELOPER
460void GrLayerCache::writeLayersToDisk(const SkString& dirName) {
461
robertphillips4ab5a902014-10-29 13:56:02 -0700462 if (fAtlas) {
463 GrTexture* atlasTexture = fAtlas->getTexture();
464 if (NULL != atlasTexture) {
465 SkString fileName(dirName);
466 fileName.append("\\atlas.png");
robertphillips84ac0822014-10-14 07:07:59 -0700467
robertphillips4ab5a902014-10-29 13:56:02 -0700468 atlasTexture->surfacePriv().savePixels(fileName.c_str());
469 }
robertphillips84ac0822014-10-14 07:07:59 -0700470 }
471
472 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
473 for (; !iter.done(); ++iter) {
474 GrCachedLayer* layer = &(*iter);
475
476 if (layer->isAtlased() || !layer->texture()) {
477 continue;
478 }
479
480 SkString fileName(dirName);
robertphillips3aac6e02014-10-20 08:52:40 -0700481 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.start());
robertphillips84ac0822014-10-14 07:07:59 -0700482
483 layer->texture()->surfacePriv().savePixels(fileName.c_str());
484 }
485}
486#endif