blob: cdbd0806ccb4e0694a8054d98bad4b7b3c931b78 [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#ifndef GrLayerCache_DEFINED
9#define GrLayerCache_DEFINED
10
robertphillips4ec84da2014-06-24 13:10:43 -070011#include "GrAtlas.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000012#include "GrRect.h"
robertphillips9e6835d2014-10-22 05:33:52 -070013
robertphillips3d533ac2014-07-20 09:40:00 -070014#include "SkChecksum.h"
robertphillips7b9e8a42014-12-11 08:20:31 -080015#include "SkImageFilter.h"
robertphillipsd771f6b2014-07-22 10:18:06 -070016#include "SkMessageBus.h"
robertphillips82365912014-11-12 09:32:34 -080017#include "SkPicture.h"
robertphillips9e6835d2014-10-22 05:33:52 -070018#include "SkTDynamicHash.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000019
robertphillips4ab5a902014-10-29 13:56:02 -070020// Set to 0 to disable caching of hoisted layers
21#define GR_CACHE_HOISTED_LAYERS 0
22
mtklein04c96952014-11-24 08:20:57 -080023// GrPictureInfo stores the atlas plots used by a single picture. A single
robertphillips261b8a92014-07-17 08:26:44 -070024// plot may be used to store layers from multiple pictures.
25struct GrPictureInfo {
26public:
robertphillips225a6272014-10-30 11:39:19 -070027 static const int kNumPlots = 4;
28
robertphillips3d533ac2014-07-20 09:40:00 -070029 // for SkTDynamicHash - just use the pictureID as the hash key
30 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; }
31 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); }
32
33 // GrPictureInfo proper
robertphillips225a6272014-10-30 11:39:19 -070034 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) {
35#if !GR_CACHE_HOISTED_LAYERS
36 memset(fPlotUses, 0, sizeof(fPlotUses));
37#endif
38 }
39
40#if !GR_CACHE_HOISTED_LAYERS
41 void incPlotUsage(int plotID) {
42 SkASSERT(plotID < kNumPlots);
43 fPlotUses[plotID]++;
44 }
45
46 void decPlotUsage(int plotID) {
47 SkASSERT(plotID < kNumPlots);
48 SkASSERT(fPlotUses[plotID] > 0);
49 fPlotUses[plotID]--;
50 }
51
52 int plotUsage(int plotID) const {
53 SkASSERT(plotID < kNumPlots);
54 return fPlotUses[plotID];
55 }
56#endif
robertphillips261b8a92014-07-17 08:26:44 -070057
robertphillips3d533ac2014-07-20 09:40:00 -070058 const uint32_t fPictureID;
robertphillips261b8a92014-07-17 08:26:44 -070059 GrAtlas::ClientPlotUsage fPlotUsage;
robertphillips225a6272014-10-30 11:39:19 -070060
61#if !GR_CACHE_HOISTED_LAYERS
62private:
63 int fPlotUses[kNumPlots];
64#endif
robertphillips261b8a92014-07-17 08:26:44 -070065};
66
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000067// GrCachedLayer encapsulates the caching information for a single saveLayer.
68//
robertphillips21048b52014-07-15 19:46:35 -070069// Atlased layers get a ref to the backing GrTexture while non-atlased layers
70// get a ref to the GrTexture in which they reside. In both cases 'fRect'
71// contains the layer's extent in its texture.
robertphillips261b8a92014-07-17 08:26:44 -070072// Atlased layers also get a pointer to the plot in which they reside.
robertphillips0c423322014-07-31 11:02:38 -070073// For non-atlased layers, the lock field just corresponds to locking in
74// the resource cache. For atlased layers, it implements an additional level
robertphillips320c9232014-07-29 06:07:19 -070075// of locking to allow atlased layers to be reused multiple times.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000076struct GrCachedLayer {
robertphillips@google.come930a072014-04-03 00:34:27 +000077public:
robertphillips3d533ac2014-07-20 09:40:00 -070078 // For SkTDynamicHash
79 struct Key {
robertphillips01d6e5f2014-12-01 09:09:27 -080080 Key(uint32_t pictureID, const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -080081 const unsigned* key, int keySize, bool copyKey = false)
robertphillips01d6e5f2014-12-01 09:09:27 -080082 : fKeySize(keySize)
83 , fFreeKey(copyKey) {
84 fIDMatrix.fPictureID = pictureID;
85 fIDMatrix.fInitialMat = initialMat;
86 fIDMatrix.fInitialMat.getType(); // force initialization of type so hashes match
robertphillips3aac6e02014-10-20 08:52:40 -070087
robertphillips01d6e5f2014-12-01 09:09:27 -080088 if (copyKey) {
robertphillipse99d4992014-12-03 07:33:57 -080089 unsigned* tempKey = SkNEW_ARRAY(unsigned, keySize);
90 memcpy(tempKey, key, keySize*sizeof(unsigned));
robertphillips01d6e5f2014-12-01 09:09:27 -080091 fKey = tempKey;
92 } else {
93 fKey = key;
94 }
95
96 // The pictureID/matrix portion needs to be tightly packed.
97 GR_STATIC_ASSERT(sizeof(IDMatrix) == sizeof(uint32_t)+ // pictureID
98 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix
99 }
100
101 ~Key() {
102 if (fFreeKey) {
103 SkDELETE_ARRAY(fKey);
104 }
robertphillips3d533ac2014-07-20 09:40:00 -0700105 }
106
robertphillips0c423322014-07-31 11:02:38 -0700107 bool operator==(const Key& other) const {
robertphillips01d6e5f2014-12-01 09:09:27 -0800108 if (fKeySize != other.fKeySize) {
109 return false;
110 }
111 return fIDMatrix.fPictureID == other.fIDMatrix.fPictureID &&
112 fIDMatrix.fInitialMat.cheapEqualTo(other.fIDMatrix.fInitialMat) &&
113 !memcmp(fKey, other.fKey, fKeySize * sizeof(int));
robertphillips0c423322014-07-31 11:02:38 -0700114 }
115
robertphillips01d6e5f2014-12-01 09:09:27 -0800116 uint32_t pictureID() const { return fIDMatrix.fPictureID; }
117
118 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse
robertphillipse99d4992014-12-03 07:33:57 -0800119 const unsigned* key() const { SkASSERT(fFreeKey); return fKey; }
robertphillips01d6e5f2014-12-01 09:09:27 -0800120 int keySize() const { SkASSERT(fFreeKey); return fKeySize; }
121
122 uint32_t hash() const {
123 uint32_t hash = SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(fKey),
124 fKeySize * sizeof(int));
125 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&fIDMatrix),
126 sizeof(IDMatrix), hash);
127 }
robertphillips3d533ac2014-07-20 09:40:00 -0700128
129 private:
robertphillips01d6e5f2014-12-01 09:09:27 -0800130 struct IDMatrix {
131 // ID of the picture of which this layer is a part
132 uint32_t fPictureID;
133 // The initial matrix passed into drawPicture
134 SkMatrix fInitialMat;
135 } fIDMatrix;
136
robertphillipse99d4992014-12-03 07:33:57 -0800137 const unsigned* fKey;
138 const int fKeySize;
139 bool fFreeKey;
robertphillips3d533ac2014-07-20 09:40:00 -0700140 };
141
142 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
robertphillips01d6e5f2014-12-01 09:09:27 -0800143 static uint32_t Hash(const Key& key) { return key.hash(); }
robertphillips952841b2014-06-30 08:26:50 -0700144
robertphillips3d533ac2014-07-20 09:40:00 -0700145 // GrCachedLayer proper
robertphillipse99d4992014-12-03 07:33:57 -0800146 GrCachedLayer(uint32_t pictureID, unsigned start, unsigned stop,
robertphillips478dd722014-12-16 08:25:55 -0800147 const SkIRect& srcIR, const SkIRect& dstIR,
148 const SkMatrix& ctm,
robertphillipse99d4992014-12-03 07:33:57 -0800149 const unsigned* key, int keySize,
robertphillips3aac6e02014-10-20 08:52:40 -0700150 const SkPaint* paint)
robertphillips01d6e5f2014-12-01 09:09:27 -0800151 : fKey(pictureID, ctm, key, keySize, true)
152 , fStart(start)
robertphillipsed420592014-09-29 11:39:38 -0700153 , fStop(stop)
robertphillips478dd722014-12-16 08:25:55 -0800154 , fSrcIR(srcIR)
155 , fDstIR(dstIR)
156 , fOffset(SkIPoint::Make(0, 0))
robertphillipsa0537de2014-09-18 08:01:23 -0700157 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL)
robertphillips7b9e8a42014-12-11 08:20:31 -0800158 , fFilter(NULL)
robertphillips3d533ac2014-07-20 09:40:00 -0700159 , fTexture(NULL)
robertphillipse99d4992014-12-03 07:33:57 -0800160 , fRect(SkIRect::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -0700161 , fPlot(NULL)
robertphillips7bb9ed72014-10-10 11:38:29 -0700162 , fUses(0)
robertphillips320c9232014-07-29 06:07:19 -0700163 , fLocked(false) {
robertphillipse99d4992014-12-03 07:33:57 -0800164 SkASSERT(SK_InvalidGenID != pictureID);
robertphillips7b9e8a42014-12-11 08:20:31 -0800165
166 if (fPaint) {
robertphillips478dd722014-12-16 08:25:55 -0800167 if (fPaint->getImageFilter() && fPaint->getImageFilter()->canFilterImageGPU()) {
168 fFilter = SkSafeRef(fPaint->getImageFilter());
169 fPaint->setImageFilter(NULL);
170 }
robertphillips7b9e8a42014-12-11 08:20:31 -0800171 }
robertphillips3d533ac2014-07-20 09:40:00 -0700172 }
173
robertphillipsed6f03e2014-07-30 07:31:35 -0700174 ~GrCachedLayer() {
175 SkSafeUnref(fTexture);
robertphillips7b9e8a42014-12-11 08:20:31 -0800176 SkSafeUnref(fFilter);
robertphillipsa0537de2014-09-18 08:01:23 -0700177 SkDELETE(fPaint);
robertphillipsed6f03e2014-07-30 07:31:35 -0700178 }
179
robertphillips0c423322014-07-31 11:02:38 -0700180 uint32_t pictureID() const { return fKey.pictureID(); }
robertphillips01d6e5f2014-12-01 09:09:27 -0800181 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse
robertphillipse99d4992014-12-03 07:33:57 -0800182 const unsigned* key() const { return fKey.key(); }
robertphillips01d6e5f2014-12-01 09:09:27 -0800183 int keySize() const { return fKey.keySize(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000184
robertphillipse99d4992014-12-03 07:33:57 -0800185 unsigned start() const { return fStart; }
robertphillips01d6e5f2014-12-01 09:09:27 -0800186 // TODO: make bound debug only
robertphillips478dd722014-12-16 08:25:55 -0800187 const SkIRect& srcIR() const { return fSrcIR; }
188 const SkIRect& dstIR() const { return fDstIR; }
robertphillipse99d4992014-12-03 07:33:57 -0800189 unsigned stop() const { return fStop; }
190 void setTexture(GrTexture* texture, const SkIRect& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700191 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700192 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000193 }
robertphillips952841b2014-06-30 08:26:50 -0700194 GrTexture* texture() { return fTexture; }
robertphillips4aa6dfc2014-09-17 07:50:47 -0700195 const SkPaint* paint() const { return fPaint; }
robertphillips7b9e8a42014-12-11 08:20:31 -0800196 const SkImageFilter* filter() const { return fFilter; }
robertphillipse99d4992014-12-03 07:33:57 -0800197 const SkIRect& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000198
robertphillips478dd722014-12-16 08:25:55 -0800199 void setOffset(const SkIPoint& offset) { fOffset = offset; }
200 const SkIPoint& offset() const { return fOffset; }
201
robertphillips261b8a92014-07-17 08:26:44 -0700202 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700203 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700204 fPlot = plot;
205 }
206 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700207
bsalomon49f085d2014-09-05 13:34:00 -0700208 bool isAtlased() const { return SkToBool(fPlot); }
robertphillips261b8a92014-07-17 08:26:44 -0700209
robertphillips320c9232014-07-29 06:07:19 -0700210 void setLocked(bool locked) { fLocked = locked; }
211 bool locked() const { return fLocked; }
212
213 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700214 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700215
robertphillips@google.come930a072014-04-03 00:34:27 +0000216private:
robertphillips3d533ac2014-07-20 09:40:00 -0700217 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000218
robertphillips01d6e5f2014-12-01 09:09:27 -0800219 // The "saveLayer" operation index of the cached layer
robertphillipse99d4992014-12-03 07:33:57 -0800220 const unsigned fStart;
robertphillipsed420592014-09-29 11:39:38 -0700221 // The final "restore" operation index of the cached layer
robertphillipse99d4992014-12-03 07:33:57 -0800222 const unsigned fStop;
robertphillipsed420592014-09-29 11:39:38 -0700223
robertphillips478dd722014-12-16 08:25:55 -0800224 // The layer's src rect (i.e., the portion of the source scene required
225 // for filtering).
226 const SkIRect fSrcIR;
227 // The layer's dest rect (i.e., where it will land in device space)
228 const SkIRect fDstIR;
229 // Offset sometimes required by image filters
230 SkIPoint fOffset;
robertphillips01d6e5f2014-12-01 09:09:27 -0800231
robertphillips4aa6dfc2014-09-17 07:50:47 -0700232 // The paint used when dropping the layer down into the owning canvas.
robertphillipsa0537de2014-09-18 08:01:23 -0700233 // Can be NULL. This class makes a copy for itself.
robertphillips7b9e8a42014-12-11 08:20:31 -0800234 SkPaint* fPaint;
235
236 // The imagefilter that needs to be applied to the layer prior to it being
237 // composited with the rest of the scene.
238 const SkImageFilter* fFilter;
robertphillips4aa6dfc2014-09-17 07:50:47 -0700239
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000240 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700241 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000242 GrTexture* fTexture;
243
robertphillips21048b52014-07-15 19:46:35 -0700244 // For both atlased and non-atlased layers 'fRect' contains the bound of
245 // the layer in whichever texture it resides. It is empty when 'fTexture'
246 // is NULL.
robertphillipse99d4992014-12-03 07:33:57 -0800247 SkIRect fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700248
249 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
250 // It is always NULL for non-atlased layers.
251 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700252
robertphillips7bb9ed72014-10-10 11:38:29 -0700253 // The number of actively hoisted layers using this cached image (e.g.,
254 // extant GrHoistedLayers pointing at this object). This object will
255 // be unlocked when the use count reaches 0.
256 int fUses;
257
bsalomon49f085d2014-09-05 13:34:00 -0700258 // For non-atlased layers 'fLocked' should always match "fTexture".
robertphillips320c9232014-07-29 06:07:19 -0700259 // (i.e., if there is a texture it is locked).
260 // For atlased layers, 'fLocked' is true if the layer is in a plot and
261 // actively required for rendering. If the layer is in a plot but not
262 // actively required for rendering, then 'fLocked' is false. If the
263 // layer isn't in a plot then is can never be locked.
264 bool fLocked;
robertphillips7bb9ed72014-10-10 11:38:29 -0700265
266 void addUse() { ++fUses; }
267 void removeUse() { SkASSERT(fUses > 0); --fUses; }
268 int uses() const { return fUses; }
269
270 friend class GrLayerCache; // for access to usage methods
271 friend class TestingAccess; // for testing
robertphillips@google.come930a072014-04-03 00:34:27 +0000272};
273
274// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000275// Non-atlased layers are stored in their own GrTexture while the atlased
276// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700277// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000278// and one GrPlot (for the entire atlas). As such, the GrLayerCache
279// roughly combines the functionality of the GrFontCache and GrTextStrike
280// classes.
281class GrLayerCache {
282public:
robertphillips4ec84da2014-06-24 13:10:43 -0700283 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000284 ~GrLayerCache();
285
robertphillips4ec84da2014-06-24 13:10:43 -0700286 // As a cache, the GrLayerCache can be ordered to free up all its cached
287 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000288 void freeAll();
289
robertphillipse99d4992014-12-03 07:33:57 -0800290 GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm,
291 const unsigned* key, int keySize);
robertphillips6f294af2014-08-18 08:50:03 -0700292 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700293 int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800294 const SkIRect& srcIR,
295 const SkIRect& dstIR,
robertphillips01d6e5f2014-12-01 09:09:27 -0800296 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800297 const unsigned* key, int keySize,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700298 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700299
robertphillipsfd61ed02014-10-28 07:21:44 -0700300 // Attempt to place 'layer' in the atlas. Return true on success; false on failure.
301 // When true is returned, 'needsRendering' will indicate if the layer must be (re)drawn.
302 // Additionally, the GPU resources will be locked.
bsalomonf2703d82014-10-28 14:33:06 -0700303 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillipsfd61ed02014-10-28 07:21:44 -0700304
305 // Attempt to lock the GPU resources required for a layer. Return true on success;
306 // false on failure. When true is returned 'needsRendering' will indicate if the
307 // layer must be (re)drawn.
308 // Note that atlased layers should already have been locked and rendered so only
309 // free floating layers will have 'needsRendering' set.
310 // Currently, this path always uses a new scratch texture for non-Atlased layers
311 // and (thus) doesn't cache anything. This can yield a lot of re-rendering.
312 // TODO: allow rediscovery of free-floating layers that are still in the resource cache.
bsalomonf2703d82014-10-28 14:33:06 -0700313 bool lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillips4ec84da2014-06-24 13:10:43 -0700314
robertphillips7bb9ed72014-10-10 11:38:29 -0700315 // addUse is just here to keep the API symmetric
316 void addUse(GrCachedLayer* layer) { layer->addUse(); }
317 void removeUse(GrCachedLayer* layer) {
318 layer->removeUse();
319 if (layer->uses() == 0) {
320 // If no one cares about the layer allow it to be recycled.
321 this->unlock(layer);
322 }
323 }
robertphillips@google.come930a072014-04-03 00:34:27 +0000324
robertphillipsd771f6b2014-07-22 10:18:06 -0700325 // Cleanup after any SkPicture deletions
326 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700327
robertphillips21048b52014-07-15 19:46:35 -0700328 SkDEBUGCODE(void validate() const;)
329
robertphillips84ac0822014-10-14 07:07:59 -0700330#ifdef SK_DEVELOPER
331 void writeLayersToDisk(const SkString& dirName);
332#endif
333
robertphillipsfd61ed02014-10-28 07:21:44 -0700334 static bool PlausiblyAtlasable(int width, int height) {
335 return width <= kPlotWidth && height <= kPlotHeight;
336 }
337
robertphillips4ab5a902014-10-29 13:56:02 -0700338#if !GR_CACHE_HOISTED_LAYERS
339 void purgeAll();
340#endif
341
robertphillips@google.come930a072014-04-03 00:34:27 +0000342private:
robertphillips320c9232014-07-29 06:07:19 -0700343 static const int kAtlasTextureWidth = 1024;
344 static const int kAtlasTextureHeight = 1024;
345
robertphillips261b8a92014-07-17 08:26:44 -0700346 static const int kNumPlotsX = 2;
347 static const int kNumPlotsY = 2;
348
robertphillips320c9232014-07-29 06:07:19 -0700349 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
350 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
351
robertphillips4ec84da2014-06-24 13:10:43 -0700352 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700353 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700354
355 // We cache this information here (rather then, say, on the owning picture)
356 // because we want to be able to clean it up as needed (e.g., if a picture
357 // is leaked and never cleans itself up we still want to be able to
358 // remove the GrPictureInfo once its layers are purged from all the atlas
359 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700360 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000361
robertphillips3d533ac2014-07-20 09:40:00 -0700362 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000363
mtklein04c96952014-11-24 08:20:57 -0800364 SkMessageBus<SkPicture::DeletionMessage>::Inbox fPictDeletionInbox;
robertphillipsd771f6b2014-07-22 10:18:06 -0700365
robertphillips320c9232014-07-29 06:07:19 -0700366 // This implements a plot-centric locking mechanism (since the atlas
367 // backing texture is always locked). Each layer that is locked (i.e.,
368 // needed for the current rendering) in a plot increments the plot lock
robertphillipsa32c6bc2014-10-09 12:47:01 -0700369 // count for that plot. Similarly, once a rendering is complete all the
370 // layers used in it decrement the lock count for the used plots.
371 // Plots with a 0 lock count are open for recycling/purging.
robertphillips320c9232014-07-29 06:07:19 -0700372 int fPlotLocks[kNumPlotsX * kNumPlotsY];
373
robertphillips7bb9ed72014-10-10 11:38:29 -0700374 // Inform the cache that layer's cached image is not currently required
375 void unlock(GrCachedLayer* layer);
376
robertphillips952841b2014-06-30 08:26:50 -0700377 void initAtlas();
robertphillips01d6e5f2014-12-01 09:09:27 -0800378 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop,
robertphillips478dd722014-12-16 08:25:55 -0800379 const SkIRect& srcIR, const SkIRect& dstIR,
380 const SkMatrix& initialMat,
robertphillipse99d4992014-12-03 07:33:57 -0800381 const unsigned* key, int keySize,
robertphillips3aac6e02014-10-20 08:52:40 -0700382 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700383
robertphillipsd771f6b2014-07-22 10:18:06 -0700384 // Remove all the layers (and unlock any resources) associated with 'pictureID'
385 void purge(uint32_t pictureID);
386
robertphillips6f294af2014-08-18 08:50:03 -0700387 void purgePlot(GrPlot* plot);
388
robertphillips320c9232014-07-29 06:07:19 -0700389 // Try to find a purgeable plot and clear it out. Return true if a plot
390 // was purged; false otherwise.
391 bool purgePlot();
392
robertphillips7bb9ed72014-10-10 11:38:29 -0700393 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; }
394 void decPlotLock(int plotIdx) {
395 SkASSERT(fPlotLocks[plotIdx] > 0);
396 --fPlotLocks[plotIdx];
397 }
398
robertphillips952841b2014-06-30 08:26:50 -0700399 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700400 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700401 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000402};
403
404#endif