blob: 75e913056027113ce0b94c885dbc4bece527d7e2 [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
mtklein04c96952014-11-24 08:20:57 -080022// GrPictureInfo stores the atlas plots used by a single picture. A single
robertphillips261b8a92014-07-17 08:26:44 -070023// plot may be used to store layers from multiple pictures.
24struct GrPictureInfo {
25public:
robertphillips225a6272014-10-30 11:39:19 -070026 static const int kNumPlots = 4;
27
robertphillips3d533ac2014-07-20 09:40:00 -070028 // for SkTDynamicHash - just use the pictureID as the hash key
29 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; }
30 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); }
31
32 // GrPictureInfo proper
robertphillips225a6272014-10-30 11:39:19 -070033 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) {
34#if !GR_CACHE_HOISTED_LAYERS
35 memset(fPlotUses, 0, sizeof(fPlotUses));
36#endif
37 }
38
39#if !GR_CACHE_HOISTED_LAYERS
40 void incPlotUsage(int plotID) {
41 SkASSERT(plotID < kNumPlots);
42 fPlotUses[plotID]++;
43 }
44
45 void decPlotUsage(int plotID) {
46 SkASSERT(plotID < kNumPlots);
47 SkASSERT(fPlotUses[plotID] > 0);
48 fPlotUses[plotID]--;
49 }
50
51 int plotUsage(int plotID) const {
52 SkASSERT(plotID < kNumPlots);
53 return fPlotUses[plotID];
54 }
55#endif
robertphillips261b8a92014-07-17 08:26:44 -070056
robertphillips3d533ac2014-07-20 09:40:00 -070057 const uint32_t fPictureID;
robertphillips261b8a92014-07-17 08:26:44 -070058 GrAtlas::ClientPlotUsage fPlotUsage;
robertphillips225a6272014-10-30 11:39:19 -070059
60#if !GR_CACHE_HOISTED_LAYERS
61private:
62 int fPlotUses[kNumPlots];
63#endif
robertphillips261b8a92014-07-17 08:26:44 -070064};
65
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000066// GrCachedLayer encapsulates the caching information for a single saveLayer.
67//
robertphillips21048b52014-07-15 19:46:35 -070068// Atlased layers get a ref to the backing GrTexture while non-atlased layers
69// get a ref to the GrTexture in which they reside. In both cases 'fRect'
70// contains the layer's extent in its texture.
robertphillips261b8a92014-07-17 08:26:44 -070071// Atlased layers also get a pointer to the plot in which they reside.
robertphillips0c423322014-07-31 11:02:38 -070072// For non-atlased layers, the lock field just corresponds to locking in
73// the resource cache. For atlased layers, it implements an additional level
robertphillips320c9232014-07-29 06:07:19 -070074// of locking to allow atlased layers to be reused multiple times.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000075struct GrCachedLayer {
robertphillips@google.come930a072014-04-03 00:34:27 +000076public:
robertphillips3d533ac2014-07-20 09:40:00 -070077 // For SkTDynamicHash
78 struct Key {
robertphillips3aac6e02014-10-20 08:52:40 -070079 Key(uint32_t pictureID, int start, const SkIRect& bounds, const SkMatrix& ctm)
robertphillips0c423322014-07-31 11:02:38 -070080 : fPictureID(pictureID)
robertphillips3aac6e02014-10-20 08:52:40 -070081 , fStart(start)
82 , fBounds(bounds)
83 , fCTM(ctm) {
84 fCTM.getType(); // force initialization of type so hashes match
85
robertphillips0c423322014-07-31 11:02:38 -070086 // Key needs to be tightly packed.
robertphillipsed420592014-09-29 11:39:38 -070087 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // picture ID
88 sizeof(int) + // start index
robertphillips3aac6e02014-10-20 08:52:40 -070089 4 * sizeof(uint32_t) + // bounds
90 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix
robertphillips3d533ac2014-07-20 09:40:00 -070091 }
92
robertphillips0c423322014-07-31 11:02:38 -070093 bool operator==(const Key& other) const {
94 return fPictureID == other.fPictureID &&
95 fStart == other.fStart &&
robertphillips3aac6e02014-10-20 08:52:40 -070096 fBounds == other.fBounds &&
97 fCTM.cheapEqualTo(other.fCTM);
robertphillips0c423322014-07-31 11:02:38 -070098 }
99
100 uint32_t pictureID() const { return fPictureID; }
101 int start() const { return fStart; }
robertphillips3aac6e02014-10-20 08:52:40 -0700102 const SkIRect& bound() const { return fBounds; }
robertphillips3d533ac2014-07-20 09:40:00 -0700103
104 private:
105 // ID of the picture of which this layer is a part
106 const uint32_t fPictureID;
robertphillipsed420592014-09-29 11:39:38 -0700107 // The the index of the saveLayer command in the picture
robertphillips0c423322014-07-31 11:02:38 -0700108 const int fStart;
robertphillips3aac6e02014-10-20 08:52:40 -0700109 // The bounds of the layer. The TL corner is its offset.
110 const SkIRect fBounds;
robertphillipsed420592014-09-29 11:39:38 -0700111 // The 2x2 portion of the CTM applied to this layer in the picture
robertphillips3aac6e02014-10-20 08:52:40 -0700112 SkMatrix fCTM;
robertphillips3d533ac2014-07-20 09:40:00 -0700113 };
114
115 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
116 static uint32_t Hash(const Key& key) {
robertphillips0c423322014-07-31 11:02:38 -0700117 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
robertphillips952841b2014-06-30 08:26:50 -0700118 }
119
robertphillips3d533ac2014-07-20 09:40:00 -0700120 // GrCachedLayer proper
robertphillips4815fe52014-09-16 10:32:43 -0700121 GrCachedLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700122 const SkIRect& bounds, const SkMatrix& ctm,
123 const SkPaint* paint)
124 : fKey(pictureID, start, bounds, ctm)
robertphillipsed420592014-09-29 11:39:38 -0700125 , fStop(stop)
robertphillipsa0537de2014-09-18 08:01:23 -0700126 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL)
robertphillips3d533ac2014-07-20 09:40:00 -0700127 , fTexture(NULL)
128 , fRect(GrIRect16::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -0700129 , fPlot(NULL)
robertphillips7bb9ed72014-10-10 11:38:29 -0700130 , fUses(0)
robertphillips320c9232014-07-29 06:07:19 -0700131 , fLocked(false) {
robertphillips0c423322014-07-31 11:02:38 -0700132 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -0700133 }
134
robertphillipsed6f03e2014-07-30 07:31:35 -0700135 ~GrCachedLayer() {
136 SkSafeUnref(fTexture);
robertphillipsa0537de2014-09-18 08:01:23 -0700137 SkDELETE(fPaint);
robertphillipsed6f03e2014-07-30 07:31:35 -0700138 }
139
robertphillips0c423322014-07-31 11:02:38 -0700140 uint32_t pictureID() const { return fKey.pictureID(); }
141 int start() const { return fKey.start(); }
robertphillips3aac6e02014-10-20 08:52:40 -0700142 const SkIRect& bound() const { return fKey.bound(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000143
robertphillipsed420592014-09-29 11:39:38 -0700144 int stop() const { return fStop; }
robertphillips952841b2014-06-30 08:26:50 -0700145 void setTexture(GrTexture* texture, const GrIRect16& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700146 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700147 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000148 }
robertphillips952841b2014-06-30 08:26:50 -0700149 GrTexture* texture() { return fTexture; }
robertphillips4aa6dfc2014-09-17 07:50:47 -0700150 const SkPaint* paint() const { return fPaint; }
robertphillips952841b2014-06-30 08:26:50 -0700151 const GrIRect16& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000152
robertphillips261b8a92014-07-17 08:26:44 -0700153 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700154 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700155 fPlot = plot;
156 }
157 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700158
bsalomon49f085d2014-09-05 13:34:00 -0700159 bool isAtlased() const { return SkToBool(fPlot); }
robertphillips261b8a92014-07-17 08:26:44 -0700160
robertphillips320c9232014-07-29 06:07:19 -0700161 void setLocked(bool locked) { fLocked = locked; }
162 bool locked() const { return fLocked; }
163
164 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700165 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700166
robertphillips@google.come930a072014-04-03 00:34:27 +0000167private:
robertphillips3d533ac2014-07-20 09:40:00 -0700168 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000169
robertphillipsed420592014-09-29 11:39:38 -0700170 // The final "restore" operation index of the cached layer
171 const int fStop;
172
robertphillips4aa6dfc2014-09-17 07:50:47 -0700173 // The paint used when dropping the layer down into the owning canvas.
robertphillipsa0537de2014-09-18 08:01:23 -0700174 // Can be NULL. This class makes a copy for itself.
robertphillips4aa6dfc2014-09-17 07:50:47 -0700175 const SkPaint* fPaint;
176
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000177 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700178 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000179 GrTexture* fTexture;
180
robertphillips21048b52014-07-15 19:46:35 -0700181 // For both atlased and non-atlased layers 'fRect' contains the bound of
182 // the layer in whichever texture it resides. It is empty when 'fTexture'
183 // is NULL.
robertphillips952841b2014-06-30 08:26:50 -0700184 GrIRect16 fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700185
186 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
187 // It is always NULL for non-atlased layers.
188 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700189
robertphillips7bb9ed72014-10-10 11:38:29 -0700190 // The number of actively hoisted layers using this cached image (e.g.,
191 // extant GrHoistedLayers pointing at this object). This object will
192 // be unlocked when the use count reaches 0.
193 int fUses;
194
bsalomon49f085d2014-09-05 13:34:00 -0700195 // For non-atlased layers 'fLocked' should always match "fTexture".
robertphillips320c9232014-07-29 06:07:19 -0700196 // (i.e., if there is a texture it is locked).
197 // For atlased layers, 'fLocked' is true if the layer is in a plot and
198 // actively required for rendering. If the layer is in a plot but not
199 // actively required for rendering, then 'fLocked' is false. If the
200 // layer isn't in a plot then is can never be locked.
201 bool fLocked;
robertphillips7bb9ed72014-10-10 11:38:29 -0700202
203 void addUse() { ++fUses; }
204 void removeUse() { SkASSERT(fUses > 0); --fUses; }
205 int uses() const { return fUses; }
206
207 friend class GrLayerCache; // for access to usage methods
208 friend class TestingAccess; // for testing
robertphillips@google.come930a072014-04-03 00:34:27 +0000209};
210
211// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000212// Non-atlased layers are stored in their own GrTexture while the atlased
213// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700214// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000215// and one GrPlot (for the entire atlas). As such, the GrLayerCache
216// roughly combines the functionality of the GrFontCache and GrTextStrike
217// classes.
218class GrLayerCache {
219public:
robertphillips4ec84da2014-06-24 13:10:43 -0700220 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000221 ~GrLayerCache();
222
robertphillips4ec84da2014-06-24 13:10:43 -0700223 // As a cache, the GrLayerCache can be ordered to free up all its cached
224 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000225 void freeAll();
226
robertphillips3aac6e02014-10-20 08:52:40 -0700227 GrCachedLayer* findLayer(uint32_t pictureID, int start,
228 const SkIRect& bounds, const SkMatrix& ctm);
robertphillips6f294af2014-08-18 08:50:03 -0700229 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700230 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700231 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700232 const SkMatrix& ctm,
233 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700234
robertphillipsfd61ed02014-10-28 07:21:44 -0700235 // Attempt to place 'layer' in the atlas. Return true on success; false on failure.
236 // When true is returned, 'needsRendering' will indicate if the layer must be (re)drawn.
237 // Additionally, the GPU resources will be locked.
bsalomonf2703d82014-10-28 14:33:06 -0700238 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillipsfd61ed02014-10-28 07:21:44 -0700239
240 // Attempt to lock the GPU resources required for a layer. Return true on success;
241 // false on failure. When true is returned 'needsRendering' will indicate if the
242 // layer must be (re)drawn.
243 // Note that atlased layers should already have been locked and rendered so only
244 // free floating layers will have 'needsRendering' set.
245 // Currently, this path always uses a new scratch texture for non-Atlased layers
246 // and (thus) doesn't cache anything. This can yield a lot of re-rendering.
247 // TODO: allow rediscovery of free-floating layers that are still in the resource cache.
bsalomonf2703d82014-10-28 14:33:06 -0700248 bool lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillips4ec84da2014-06-24 13:10:43 -0700249
robertphillips7bb9ed72014-10-10 11:38:29 -0700250 // addUse is just here to keep the API symmetric
251 void addUse(GrCachedLayer* layer) { layer->addUse(); }
252 void removeUse(GrCachedLayer* layer) {
253 layer->removeUse();
254 if (layer->uses() == 0) {
255 // If no one cares about the layer allow it to be recycled.
256 this->unlock(layer);
257 }
258 }
robertphillips@google.come930a072014-04-03 00:34:27 +0000259
robertphillipsd771f6b2014-07-22 10:18:06 -0700260 // Cleanup after any SkPicture deletions
261 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700262
robertphillips21048b52014-07-15 19:46:35 -0700263 SkDEBUGCODE(void validate() const;)
264
robertphillips84ac0822014-10-14 07:07:59 -0700265#ifdef SK_DEVELOPER
266 void writeLayersToDisk(const SkString& dirName);
267#endif
268
robertphillipsfd61ed02014-10-28 07:21:44 -0700269 static bool PlausiblyAtlasable(int width, int height) {
270 return width <= kPlotWidth && height <= kPlotHeight;
271 }
272
robertphillips4ab5a902014-10-29 13:56:02 -0700273#if !GR_CACHE_HOISTED_LAYERS
274 void purgeAll();
275#endif
276
robertphillips@google.come930a072014-04-03 00:34:27 +0000277private:
robertphillips320c9232014-07-29 06:07:19 -0700278 static const int kAtlasTextureWidth = 1024;
279 static const int kAtlasTextureHeight = 1024;
280
robertphillips261b8a92014-07-17 08:26:44 -0700281 static const int kNumPlotsX = 2;
282 static const int kNumPlotsY = 2;
283
robertphillips320c9232014-07-29 06:07:19 -0700284 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
285 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
286
robertphillips4ec84da2014-06-24 13:10:43 -0700287 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700288 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700289
290 // We cache this information here (rather then, say, on the owning picture)
291 // because we want to be able to clean it up as needed (e.g., if a picture
292 // is leaked and never cleans itself up we still want to be able to
293 // remove the GrPictureInfo once its layers are purged from all the atlas
294 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700295 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000296
robertphillips3d533ac2014-07-20 09:40:00 -0700297 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000298
mtklein04c96952014-11-24 08:20:57 -0800299 SkMessageBus<SkPicture::DeletionMessage>::Inbox fPictDeletionInbox;
robertphillipsd771f6b2014-07-22 10:18:06 -0700300
robertphillips320c9232014-07-29 06:07:19 -0700301 // This implements a plot-centric locking mechanism (since the atlas
302 // backing texture is always locked). Each layer that is locked (i.e.,
303 // needed for the current rendering) in a plot increments the plot lock
robertphillipsa32c6bc2014-10-09 12:47:01 -0700304 // count for that plot. Similarly, once a rendering is complete all the
305 // layers used in it decrement the lock count for the used plots.
306 // Plots with a 0 lock count are open for recycling/purging.
robertphillips320c9232014-07-29 06:07:19 -0700307 int fPlotLocks[kNumPlotsX * kNumPlotsY];
308
robertphillips7bb9ed72014-10-10 11:38:29 -0700309 // Inform the cache that layer's cached image is not currently required
310 void unlock(GrCachedLayer* layer);
311
robertphillips952841b2014-06-30 08:26:50 -0700312 void initAtlas();
robertphillips4815fe52014-09-16 10:32:43 -0700313 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700314 const SkIRect& bounds, const SkMatrix& ctm,
315 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700316
robertphillipsd771f6b2014-07-22 10:18:06 -0700317 // Remove all the layers (and unlock any resources) associated with 'pictureID'
318 void purge(uint32_t pictureID);
319
robertphillips6f294af2014-08-18 08:50:03 -0700320 void purgePlot(GrPlot* plot);
321
robertphillips320c9232014-07-29 06:07:19 -0700322 // Try to find a purgeable plot and clear it out. Return true if a plot
323 // was purged; false otherwise.
324 bool purgePlot();
325
robertphillips7bb9ed72014-10-10 11:38:29 -0700326 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; }
327 void decPlotLock(int plotIdx) {
328 SkASSERT(fPlotLocks[plotIdx] > 0);
329 --fPlotLocks[plotIdx];
330 }
331
robertphillips952841b2014-06-30 08:26:50 -0700332 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700333 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700334 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000335};
336
337#endif