blob: 8dec8e594d8fc19ea67f42393b29ecea05370e45 [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"
robertphillipsd771f6b2014-07-22 10:18:06 -070015#include "SkMessageBus.h"
robertphillips82365912014-11-12 09:32:34 -080016#include "SkPicture.h"
robertphillips9e6835d2014-10-22 05:33:52 -070017#include "SkTDynamicHash.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000018
robertphillips4ab5a902014-10-29 13:56:02 -070019// Set to 0 to disable caching of hoisted layers
20#define GR_CACHE_HOISTED_LAYERS 0
21
robertphillipsd771f6b2014-07-22 10:18:06 -070022// The layer cache listens for these messages to purge picture-related resources.
23struct GrPictureDeletedMessage {
24 uint32_t pictureID;
25};
26
robertphillips261b8a92014-07-17 08:26:44 -070027// GrPictureInfo stores the atlas plots used by a single picture. A single
28// plot may be used to store layers from multiple pictures.
29struct GrPictureInfo {
30public:
robertphillips225a6272014-10-30 11:39:19 -070031 static const int kNumPlots = 4;
32
robertphillips3d533ac2014-07-20 09:40:00 -070033 // for SkTDynamicHash - just use the pictureID as the hash key
34 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; }
35 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); }
36
37 // GrPictureInfo proper
robertphillips225a6272014-10-30 11:39:19 -070038 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) {
39#if !GR_CACHE_HOISTED_LAYERS
40 memset(fPlotUses, 0, sizeof(fPlotUses));
41#endif
42 }
43
44#if !GR_CACHE_HOISTED_LAYERS
45 void incPlotUsage(int plotID) {
46 SkASSERT(plotID < kNumPlots);
47 fPlotUses[plotID]++;
48 }
49
50 void decPlotUsage(int plotID) {
51 SkASSERT(plotID < kNumPlots);
52 SkASSERT(fPlotUses[plotID] > 0);
53 fPlotUses[plotID]--;
54 }
55
56 int plotUsage(int plotID) const {
57 SkASSERT(plotID < kNumPlots);
58 return fPlotUses[plotID];
59 }
60#endif
robertphillips261b8a92014-07-17 08:26:44 -070061
robertphillips3d533ac2014-07-20 09:40:00 -070062 const uint32_t fPictureID;
robertphillips261b8a92014-07-17 08:26:44 -070063 GrAtlas::ClientPlotUsage fPlotUsage;
robertphillips225a6272014-10-30 11:39:19 -070064
65#if !GR_CACHE_HOISTED_LAYERS
66private:
67 int fPlotUses[kNumPlots];
68#endif
robertphillips261b8a92014-07-17 08:26:44 -070069};
70
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000071// GrCachedLayer encapsulates the caching information for a single saveLayer.
72//
robertphillips21048b52014-07-15 19:46:35 -070073// Atlased layers get a ref to the backing GrTexture while non-atlased layers
74// get a ref to the GrTexture in which they reside. In both cases 'fRect'
75// contains the layer's extent in its texture.
robertphillips261b8a92014-07-17 08:26:44 -070076// Atlased layers also get a pointer to the plot in which they reside.
robertphillips0c423322014-07-31 11:02:38 -070077// For non-atlased layers, the lock field just corresponds to locking in
78// the resource cache. For atlased layers, it implements an additional level
robertphillips320c9232014-07-29 06:07:19 -070079// of locking to allow atlased layers to be reused multiple times.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000080struct GrCachedLayer {
robertphillips@google.come930a072014-04-03 00:34:27 +000081public:
robertphillips3d533ac2014-07-20 09:40:00 -070082 // For SkTDynamicHash
83 struct Key {
robertphillips3aac6e02014-10-20 08:52:40 -070084 Key(uint32_t pictureID, int start, const SkIRect& bounds, const SkMatrix& ctm)
robertphillips0c423322014-07-31 11:02:38 -070085 : fPictureID(pictureID)
robertphillips3aac6e02014-10-20 08:52:40 -070086 , fStart(start)
87 , fBounds(bounds)
88 , fCTM(ctm) {
89 fCTM.getType(); // force initialization of type so hashes match
90
robertphillips0c423322014-07-31 11:02:38 -070091 // Key needs to be tightly packed.
robertphillipsed420592014-09-29 11:39:38 -070092 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // picture ID
93 sizeof(int) + // start index
robertphillips3aac6e02014-10-20 08:52:40 -070094 4 * sizeof(uint32_t) + // bounds
95 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix
robertphillips3d533ac2014-07-20 09:40:00 -070096 }
97
robertphillips0c423322014-07-31 11:02:38 -070098 bool operator==(const Key& other) const {
99 return fPictureID == other.fPictureID &&
100 fStart == other.fStart &&
robertphillips3aac6e02014-10-20 08:52:40 -0700101 fBounds == other.fBounds &&
102 fCTM.cheapEqualTo(other.fCTM);
robertphillips0c423322014-07-31 11:02:38 -0700103 }
104
105 uint32_t pictureID() const { return fPictureID; }
106 int start() const { return fStart; }
robertphillips3aac6e02014-10-20 08:52:40 -0700107 const SkIRect& bound() const { return fBounds; }
robertphillips3d533ac2014-07-20 09:40:00 -0700108
109 private:
110 // ID of the picture of which this layer is a part
111 const uint32_t fPictureID;
robertphillipsed420592014-09-29 11:39:38 -0700112 // The the index of the saveLayer command in the picture
robertphillips0c423322014-07-31 11:02:38 -0700113 const int fStart;
robertphillips3aac6e02014-10-20 08:52:40 -0700114 // The bounds of the layer. The TL corner is its offset.
115 const SkIRect fBounds;
robertphillipsed420592014-09-29 11:39:38 -0700116 // The 2x2 portion of the CTM applied to this layer in the picture
robertphillips3aac6e02014-10-20 08:52:40 -0700117 SkMatrix fCTM;
robertphillips3d533ac2014-07-20 09:40:00 -0700118 };
119
120 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
121 static uint32_t Hash(const Key& key) {
robertphillips0c423322014-07-31 11:02:38 -0700122 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
robertphillips952841b2014-06-30 08:26:50 -0700123 }
124
robertphillips3d533ac2014-07-20 09:40:00 -0700125 // GrCachedLayer proper
robertphillips4815fe52014-09-16 10:32:43 -0700126 GrCachedLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700127 const SkIRect& bounds, const SkMatrix& ctm,
128 const SkPaint* paint)
129 : fKey(pictureID, start, bounds, ctm)
robertphillipsed420592014-09-29 11:39:38 -0700130 , fStop(stop)
robertphillipsa0537de2014-09-18 08:01:23 -0700131 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL)
robertphillips3d533ac2014-07-20 09:40:00 -0700132 , fTexture(NULL)
133 , fRect(GrIRect16::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -0700134 , fPlot(NULL)
robertphillips7bb9ed72014-10-10 11:38:29 -0700135 , fUses(0)
robertphillips320c9232014-07-29 06:07:19 -0700136 , fLocked(false) {
robertphillips0c423322014-07-31 11:02:38 -0700137 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -0700138 }
139
robertphillipsed6f03e2014-07-30 07:31:35 -0700140 ~GrCachedLayer() {
141 SkSafeUnref(fTexture);
robertphillipsa0537de2014-09-18 08:01:23 -0700142 SkDELETE(fPaint);
robertphillipsed6f03e2014-07-30 07:31:35 -0700143 }
144
robertphillips0c423322014-07-31 11:02:38 -0700145 uint32_t pictureID() const { return fKey.pictureID(); }
146 int start() const { return fKey.start(); }
robertphillips3aac6e02014-10-20 08:52:40 -0700147 const SkIRect& bound() const { return fKey.bound(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000148
robertphillipsed420592014-09-29 11:39:38 -0700149 int stop() const { return fStop; }
robertphillips952841b2014-06-30 08:26:50 -0700150 void setTexture(GrTexture* texture, const GrIRect16& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700151 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700152 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000153 }
robertphillips952841b2014-06-30 08:26:50 -0700154 GrTexture* texture() { return fTexture; }
robertphillips4aa6dfc2014-09-17 07:50:47 -0700155 const SkPaint* paint() const { return fPaint; }
robertphillips952841b2014-06-30 08:26:50 -0700156 const GrIRect16& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000157
robertphillips261b8a92014-07-17 08:26:44 -0700158 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700159 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700160 fPlot = plot;
161 }
162 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700163
bsalomon49f085d2014-09-05 13:34:00 -0700164 bool isAtlased() const { return SkToBool(fPlot); }
robertphillips261b8a92014-07-17 08:26:44 -0700165
robertphillips320c9232014-07-29 06:07:19 -0700166 void setLocked(bool locked) { fLocked = locked; }
167 bool locked() const { return fLocked; }
168
169 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700170 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700171
robertphillips@google.come930a072014-04-03 00:34:27 +0000172private:
robertphillips3d533ac2014-07-20 09:40:00 -0700173 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000174
robertphillipsed420592014-09-29 11:39:38 -0700175 // The final "restore" operation index of the cached layer
176 const int fStop;
177
robertphillips4aa6dfc2014-09-17 07:50:47 -0700178 // The paint used when dropping the layer down into the owning canvas.
robertphillipsa0537de2014-09-18 08:01:23 -0700179 // Can be NULL. This class makes a copy for itself.
robertphillips4aa6dfc2014-09-17 07:50:47 -0700180 const SkPaint* fPaint;
181
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000182 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700183 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000184 GrTexture* fTexture;
185
robertphillips21048b52014-07-15 19:46:35 -0700186 // For both atlased and non-atlased layers 'fRect' contains the bound of
187 // the layer in whichever texture it resides. It is empty when 'fTexture'
188 // is NULL.
robertphillips952841b2014-06-30 08:26:50 -0700189 GrIRect16 fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700190
191 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
192 // It is always NULL for non-atlased layers.
193 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700194
robertphillips7bb9ed72014-10-10 11:38:29 -0700195 // The number of actively hoisted layers using this cached image (e.g.,
196 // extant GrHoistedLayers pointing at this object). This object will
197 // be unlocked when the use count reaches 0.
198 int fUses;
199
bsalomon49f085d2014-09-05 13:34:00 -0700200 // For non-atlased layers 'fLocked' should always match "fTexture".
robertphillips320c9232014-07-29 06:07:19 -0700201 // (i.e., if there is a texture it is locked).
202 // For atlased layers, 'fLocked' is true if the layer is in a plot and
203 // actively required for rendering. If the layer is in a plot but not
204 // actively required for rendering, then 'fLocked' is false. If the
205 // layer isn't in a plot then is can never be locked.
206 bool fLocked;
robertphillips7bb9ed72014-10-10 11:38:29 -0700207
208 void addUse() { ++fUses; }
209 void removeUse() { SkASSERT(fUses > 0); --fUses; }
210 int uses() const { return fUses; }
211
212 friend class GrLayerCache; // for access to usage methods
213 friend class TestingAccess; // for testing
robertphillips@google.come930a072014-04-03 00:34:27 +0000214};
215
216// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000217// Non-atlased layers are stored in their own GrTexture while the atlased
218// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700219// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000220// and one GrPlot (for the entire atlas). As such, the GrLayerCache
221// roughly combines the functionality of the GrFontCache and GrTextStrike
222// classes.
223class GrLayerCache {
224public:
robertphillips4ec84da2014-06-24 13:10:43 -0700225 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000226 ~GrLayerCache();
227
robertphillips4ec84da2014-06-24 13:10:43 -0700228 // As a cache, the GrLayerCache can be ordered to free up all its cached
229 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000230 void freeAll();
231
robertphillips3aac6e02014-10-20 08:52:40 -0700232 GrCachedLayer* findLayer(uint32_t pictureID, int start,
233 const SkIRect& bounds, const SkMatrix& ctm);
robertphillips6f294af2014-08-18 08:50:03 -0700234 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700235 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700236 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700237 const SkMatrix& ctm,
238 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700239
robertphillipsfd61ed02014-10-28 07:21:44 -0700240 // Attempt to place 'layer' in the atlas. Return true on success; false on failure.
241 // When true is returned, 'needsRendering' will indicate if the layer must be (re)drawn.
242 // Additionally, the GPU resources will be locked.
bsalomonf2703d82014-10-28 14:33:06 -0700243 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillipsfd61ed02014-10-28 07:21:44 -0700244
245 // Attempt to lock the GPU resources required for a layer. Return true on success;
246 // false on failure. When true is returned 'needsRendering' will indicate if the
247 // layer must be (re)drawn.
248 // Note that atlased layers should already have been locked and rendered so only
249 // free floating layers will have 'needsRendering' set.
250 // Currently, this path always uses a new scratch texture for non-Atlased layers
251 // and (thus) doesn't cache anything. This can yield a lot of re-rendering.
252 // TODO: allow rediscovery of free-floating layers that are still in the resource cache.
bsalomonf2703d82014-10-28 14:33:06 -0700253 bool lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillips4ec84da2014-06-24 13:10:43 -0700254
robertphillips7bb9ed72014-10-10 11:38:29 -0700255 // addUse is just here to keep the API symmetric
256 void addUse(GrCachedLayer* layer) { layer->addUse(); }
257 void removeUse(GrCachedLayer* layer) {
258 layer->removeUse();
259 if (layer->uses() == 0) {
260 // If no one cares about the layer allow it to be recycled.
261 this->unlock(layer);
262 }
263 }
robertphillips@google.come930a072014-04-03 00:34:27 +0000264
robertphillipsd771f6b2014-07-22 10:18:06 -0700265 // Setup to be notified when 'picture' is deleted
266 void trackPicture(const SkPicture* picture);
267
268 // Cleanup after any SkPicture deletions
269 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700270
robertphillips21048b52014-07-15 19:46:35 -0700271 SkDEBUGCODE(void validate() const;)
272
robertphillips84ac0822014-10-14 07:07:59 -0700273#ifdef SK_DEVELOPER
274 void writeLayersToDisk(const SkString& dirName);
275#endif
276
robertphillipsfd61ed02014-10-28 07:21:44 -0700277 static bool PlausiblyAtlasable(int width, int height) {
278 return width <= kPlotWidth && height <= kPlotHeight;
279 }
280
robertphillips4ab5a902014-10-29 13:56:02 -0700281#if !GR_CACHE_HOISTED_LAYERS
282 void purgeAll();
283#endif
284
robertphillips@google.come930a072014-04-03 00:34:27 +0000285private:
robertphillips320c9232014-07-29 06:07:19 -0700286 static const int kAtlasTextureWidth = 1024;
287 static const int kAtlasTextureHeight = 1024;
288
robertphillips261b8a92014-07-17 08:26:44 -0700289 static const int kNumPlotsX = 2;
290 static const int kNumPlotsY = 2;
291
robertphillips320c9232014-07-29 06:07:19 -0700292 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
293 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
294
robertphillips4ec84da2014-06-24 13:10:43 -0700295 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700296 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700297
298 // We cache this information here (rather then, say, on the owning picture)
299 // because we want to be able to clean it up as needed (e.g., if a picture
300 // is leaked and never cleans itself up we still want to be able to
301 // remove the GrPictureInfo once its layers are purged from all the atlas
302 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700303 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000304
robertphillips3d533ac2014-07-20 09:40:00 -0700305 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000306
robertphillipsd771f6b2014-07-22 10:18:06 -0700307 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
308
309 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
310
robertphillips320c9232014-07-29 06:07:19 -0700311 // This implements a plot-centric locking mechanism (since the atlas
312 // backing texture is always locked). Each layer that is locked (i.e.,
313 // needed for the current rendering) in a plot increments the plot lock
robertphillipsa32c6bc2014-10-09 12:47:01 -0700314 // count for that plot. Similarly, once a rendering is complete all the
315 // layers used in it decrement the lock count for the used plots.
316 // Plots with a 0 lock count are open for recycling/purging.
robertphillips320c9232014-07-29 06:07:19 -0700317 int fPlotLocks[kNumPlotsX * kNumPlotsY];
318
robertphillips7bb9ed72014-10-10 11:38:29 -0700319 // Inform the cache that layer's cached image is not currently required
320 void unlock(GrCachedLayer* layer);
321
robertphillips952841b2014-06-30 08:26:50 -0700322 void initAtlas();
robertphillips4815fe52014-09-16 10:32:43 -0700323 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700324 const SkIRect& bounds, const SkMatrix& ctm,
325 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700326
robertphillipsd771f6b2014-07-22 10:18:06 -0700327 // Remove all the layers (and unlock any resources) associated with 'pictureID'
328 void purge(uint32_t pictureID);
329
robertphillips6f294af2014-08-18 08:50:03 -0700330 void purgePlot(GrPlot* plot);
331
robertphillips320c9232014-07-29 06:07:19 -0700332 // Try to find a purgeable plot and clear it out. Return true if a plot
333 // was purged; false otherwise.
334 bool purgePlot();
335
robertphillips7bb9ed72014-10-10 11:38:29 -0700336 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; }
337 void decPlotLock(int plotIdx) {
338 SkASSERT(fPlotLocks[plotIdx] > 0);
339 --fPlotLocks[plotIdx];
340 }
341
robertphillips952841b2014-06-30 08:26:50 -0700342 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700343 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700344 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000345};
346
347#endif