blob: 632cd0054028547722a91c2caa35931e918abd5d [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 "GrPictureUtils.h"
13#include "GrRect.h"
robertphillips9e6835d2014-10-22 05:33:52 -070014
robertphillips3d533ac2014-07-20 09:40:00 -070015#include "SkChecksum.h"
robertphillipsd771f6b2014-07-22 10:18:06 -070016#include "SkMessageBus.h"
robertphillips9e6835d2014-10-22 05:33:52 -070017#include "SkTDynamicHash.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000018
robertphillips@google.come930a072014-04-03 00:34:27 +000019class SkPicture;
20
robertphillips4ab5a902014-10-29 13:56:02 -070021// Set to 0 to disable caching of hoisted layers
22#define GR_CACHE_HOISTED_LAYERS 0
23
robertphillipsd771f6b2014-07-22 10:18:06 -070024// The layer cache listens for these messages to purge picture-related resources.
25struct GrPictureDeletedMessage {
26 uint32_t pictureID;
27};
28
robertphillips261b8a92014-07-17 08:26:44 -070029// GrPictureInfo stores the atlas plots used by a single picture. A single
30// plot may be used to store layers from multiple pictures.
31struct GrPictureInfo {
32public:
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
robertphillips261b8a92014-07-17 08:26:44 -070038 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { }
39
robertphillips3d533ac2014-07-20 09:40:00 -070040 const uint32_t fPictureID;
robertphillips261b8a92014-07-17 08:26:44 -070041
42 GrAtlas::ClientPlotUsage fPlotUsage;
43};
44
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000045// GrCachedLayer encapsulates the caching information for a single saveLayer.
46//
robertphillips21048b52014-07-15 19:46:35 -070047// Atlased layers get a ref to the backing GrTexture while non-atlased layers
48// get a ref to the GrTexture in which they reside. In both cases 'fRect'
49// contains the layer's extent in its texture.
robertphillips261b8a92014-07-17 08:26:44 -070050// Atlased layers also get a pointer to the plot in which they reside.
robertphillips0c423322014-07-31 11:02:38 -070051// For non-atlased layers, the lock field just corresponds to locking in
52// the resource cache. For atlased layers, it implements an additional level
robertphillips320c9232014-07-29 06:07:19 -070053// of locking to allow atlased layers to be reused multiple times.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000054struct GrCachedLayer {
robertphillips@google.come930a072014-04-03 00:34:27 +000055public:
robertphillips3d533ac2014-07-20 09:40:00 -070056 // For SkTDynamicHash
57 struct Key {
robertphillips3aac6e02014-10-20 08:52:40 -070058 Key(uint32_t pictureID, int start, const SkIRect& bounds, const SkMatrix& ctm)
robertphillips0c423322014-07-31 11:02:38 -070059 : fPictureID(pictureID)
robertphillips3aac6e02014-10-20 08:52:40 -070060 , fStart(start)
61 , fBounds(bounds)
62 , fCTM(ctm) {
63 fCTM.getType(); // force initialization of type so hashes match
64
robertphillips0c423322014-07-31 11:02:38 -070065 // Key needs to be tightly packed.
robertphillipsed420592014-09-29 11:39:38 -070066 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // picture ID
67 sizeof(int) + // start index
robertphillips3aac6e02014-10-20 08:52:40 -070068 4 * sizeof(uint32_t) + // bounds
69 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix
robertphillips3d533ac2014-07-20 09:40:00 -070070 }
71
robertphillips0c423322014-07-31 11:02:38 -070072 bool operator==(const Key& other) const {
73 return fPictureID == other.fPictureID &&
74 fStart == other.fStart &&
robertphillips3aac6e02014-10-20 08:52:40 -070075 fBounds == other.fBounds &&
76 fCTM.cheapEqualTo(other.fCTM);
robertphillips0c423322014-07-31 11:02:38 -070077 }
78
79 uint32_t pictureID() const { return fPictureID; }
80 int start() const { return fStart; }
robertphillips3aac6e02014-10-20 08:52:40 -070081 const SkIRect& bound() const { return fBounds; }
robertphillips3d533ac2014-07-20 09:40:00 -070082
83 private:
84 // ID of the picture of which this layer is a part
85 const uint32_t fPictureID;
robertphillipsed420592014-09-29 11:39:38 -070086 // The the index of the saveLayer command in the picture
robertphillips0c423322014-07-31 11:02:38 -070087 const int fStart;
robertphillips3aac6e02014-10-20 08:52:40 -070088 // The bounds of the layer. The TL corner is its offset.
89 const SkIRect fBounds;
robertphillipsed420592014-09-29 11:39:38 -070090 // The 2x2 portion of the CTM applied to this layer in the picture
robertphillips3aac6e02014-10-20 08:52:40 -070091 SkMatrix fCTM;
robertphillips3d533ac2014-07-20 09:40:00 -070092 };
93
94 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
95 static uint32_t Hash(const Key& key) {
robertphillips0c423322014-07-31 11:02:38 -070096 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
robertphillips952841b2014-06-30 08:26:50 -070097 }
98
robertphillips3d533ac2014-07-20 09:40:00 -070099 // GrCachedLayer proper
robertphillips4815fe52014-09-16 10:32:43 -0700100 GrCachedLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700101 const SkIRect& bounds, const SkMatrix& ctm,
102 const SkPaint* paint)
103 : fKey(pictureID, start, bounds, ctm)
robertphillipsed420592014-09-29 11:39:38 -0700104 , fStop(stop)
robertphillipsa0537de2014-09-18 08:01:23 -0700105 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL)
robertphillips3d533ac2014-07-20 09:40:00 -0700106 , fTexture(NULL)
107 , fRect(GrIRect16::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -0700108 , fPlot(NULL)
robertphillips7bb9ed72014-10-10 11:38:29 -0700109 , fUses(0)
robertphillips320c9232014-07-29 06:07:19 -0700110 , fLocked(false) {
robertphillips0c423322014-07-31 11:02:38 -0700111 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -0700112 }
113
robertphillipsed6f03e2014-07-30 07:31:35 -0700114 ~GrCachedLayer() {
115 SkSafeUnref(fTexture);
robertphillipsa0537de2014-09-18 08:01:23 -0700116 SkDELETE(fPaint);
robertphillipsed6f03e2014-07-30 07:31:35 -0700117 }
118
robertphillips0c423322014-07-31 11:02:38 -0700119 uint32_t pictureID() const { return fKey.pictureID(); }
120 int start() const { return fKey.start(); }
robertphillips3aac6e02014-10-20 08:52:40 -0700121 const SkIRect& bound() const { return fKey.bound(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000122
robertphillipsed420592014-09-29 11:39:38 -0700123 int stop() const { return fStop; }
robertphillips952841b2014-06-30 08:26:50 -0700124 void setTexture(GrTexture* texture, const GrIRect16& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700125 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700126 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000127 }
robertphillips952841b2014-06-30 08:26:50 -0700128 GrTexture* texture() { return fTexture; }
robertphillips4aa6dfc2014-09-17 07:50:47 -0700129 const SkPaint* paint() const { return fPaint; }
robertphillips952841b2014-06-30 08:26:50 -0700130 const GrIRect16& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000131
robertphillips261b8a92014-07-17 08:26:44 -0700132 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700133 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700134 fPlot = plot;
135 }
136 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700137
bsalomon49f085d2014-09-05 13:34:00 -0700138 bool isAtlased() const { return SkToBool(fPlot); }
robertphillips261b8a92014-07-17 08:26:44 -0700139
robertphillips320c9232014-07-29 06:07:19 -0700140 void setLocked(bool locked) { fLocked = locked; }
141 bool locked() const { return fLocked; }
142
143 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700144 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700145
robertphillips@google.come930a072014-04-03 00:34:27 +0000146private:
robertphillips3d533ac2014-07-20 09:40:00 -0700147 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000148
robertphillipsed420592014-09-29 11:39:38 -0700149 // The final "restore" operation index of the cached layer
150 const int fStop;
151
robertphillips4aa6dfc2014-09-17 07:50:47 -0700152 // The paint used when dropping the layer down into the owning canvas.
robertphillipsa0537de2014-09-18 08:01:23 -0700153 // Can be NULL. This class makes a copy for itself.
robertphillips4aa6dfc2014-09-17 07:50:47 -0700154 const SkPaint* fPaint;
155
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000156 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700157 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000158 GrTexture* fTexture;
159
robertphillips21048b52014-07-15 19:46:35 -0700160 // For both atlased and non-atlased layers 'fRect' contains the bound of
161 // the layer in whichever texture it resides. It is empty when 'fTexture'
162 // is NULL.
robertphillips952841b2014-06-30 08:26:50 -0700163 GrIRect16 fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700164
165 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
166 // It is always NULL for non-atlased layers.
167 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700168
robertphillips7bb9ed72014-10-10 11:38:29 -0700169 // The number of actively hoisted layers using this cached image (e.g.,
170 // extant GrHoistedLayers pointing at this object). This object will
171 // be unlocked when the use count reaches 0.
172 int fUses;
173
bsalomon49f085d2014-09-05 13:34:00 -0700174 // For non-atlased layers 'fLocked' should always match "fTexture".
robertphillips320c9232014-07-29 06:07:19 -0700175 // (i.e., if there is a texture it is locked).
176 // For atlased layers, 'fLocked' is true if the layer is in a plot and
177 // actively required for rendering. If the layer is in a plot but not
178 // actively required for rendering, then 'fLocked' is false. If the
179 // layer isn't in a plot then is can never be locked.
180 bool fLocked;
robertphillips7bb9ed72014-10-10 11:38:29 -0700181
182 void addUse() { ++fUses; }
183 void removeUse() { SkASSERT(fUses > 0); --fUses; }
184 int uses() const { return fUses; }
185
186 friend class GrLayerCache; // for access to usage methods
187 friend class TestingAccess; // for testing
robertphillips@google.come930a072014-04-03 00:34:27 +0000188};
189
190// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000191// Non-atlased layers are stored in their own GrTexture while the atlased
192// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700193// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000194// and one GrPlot (for the entire atlas). As such, the GrLayerCache
195// roughly combines the functionality of the GrFontCache and GrTextStrike
196// classes.
197class GrLayerCache {
198public:
robertphillips4ec84da2014-06-24 13:10:43 -0700199 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000200 ~GrLayerCache();
201
robertphillips4ec84da2014-06-24 13:10:43 -0700202 // As a cache, the GrLayerCache can be ordered to free up all its cached
203 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000204 void freeAll();
205
robertphillips3aac6e02014-10-20 08:52:40 -0700206 GrCachedLayer* findLayer(uint32_t pictureID, int start,
207 const SkIRect& bounds, const SkMatrix& ctm);
robertphillips6f294af2014-08-18 08:50:03 -0700208 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700209 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700210 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700211 const SkMatrix& ctm,
212 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700213
robertphillipsfd61ed02014-10-28 07:21:44 -0700214 // Attempt to place 'layer' in the atlas. Return true on success; false on failure.
215 // When true is returned, 'needsRendering' will indicate if the layer must be (re)drawn.
216 // Additionally, the GPU resources will be locked.
bsalomonf2703d82014-10-28 14:33:06 -0700217 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillipsfd61ed02014-10-28 07:21:44 -0700218
219 // Attempt to lock the GPU resources required for a layer. Return true on success;
220 // false on failure. When true is returned 'needsRendering' will indicate if the
221 // layer must be (re)drawn.
222 // Note that atlased layers should already have been locked and rendered so only
223 // free floating layers will have 'needsRendering' set.
224 // Currently, this path always uses a new scratch texture for non-Atlased layers
225 // and (thus) doesn't cache anything. This can yield a lot of re-rendering.
226 // TODO: allow rediscovery of free-floating layers that are still in the resource cache.
bsalomonf2703d82014-10-28 14:33:06 -0700227 bool lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillips4ec84da2014-06-24 13:10:43 -0700228
robertphillips7bb9ed72014-10-10 11:38:29 -0700229 // addUse is just here to keep the API symmetric
230 void addUse(GrCachedLayer* layer) { layer->addUse(); }
231 void removeUse(GrCachedLayer* layer) {
232 layer->removeUse();
233 if (layer->uses() == 0) {
234 // If no one cares about the layer allow it to be recycled.
235 this->unlock(layer);
236 }
237 }
robertphillips@google.come930a072014-04-03 00:34:27 +0000238
robertphillipsd771f6b2014-07-22 10:18:06 -0700239 // Setup to be notified when 'picture' is deleted
240 void trackPicture(const SkPicture* picture);
241
242 // Cleanup after any SkPicture deletions
243 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700244
robertphillips21048b52014-07-15 19:46:35 -0700245 SkDEBUGCODE(void validate() const;)
246
robertphillips84ac0822014-10-14 07:07:59 -0700247#ifdef SK_DEVELOPER
248 void writeLayersToDisk(const SkString& dirName);
249#endif
250
robertphillipsfd61ed02014-10-28 07:21:44 -0700251 static bool PlausiblyAtlasable(int width, int height) {
252 return width <= kPlotWidth && height <= kPlotHeight;
253 }
254
robertphillips4ab5a902014-10-29 13:56:02 -0700255#if !GR_CACHE_HOISTED_LAYERS
256 void purgeAll();
257#endif
258
robertphillips@google.come930a072014-04-03 00:34:27 +0000259private:
robertphillips320c9232014-07-29 06:07:19 -0700260 static const int kAtlasTextureWidth = 1024;
261 static const int kAtlasTextureHeight = 1024;
262
robertphillips261b8a92014-07-17 08:26:44 -0700263 static const int kNumPlotsX = 2;
264 static const int kNumPlotsY = 2;
265
robertphillips320c9232014-07-29 06:07:19 -0700266 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
267 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
268
robertphillips4ec84da2014-06-24 13:10:43 -0700269 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700270 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700271
272 // We cache this information here (rather then, say, on the owning picture)
273 // because we want to be able to clean it up as needed (e.g., if a picture
274 // is leaked and never cleans itself up we still want to be able to
275 // remove the GrPictureInfo once its layers are purged from all the atlas
276 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700277 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000278
robertphillips3d533ac2014-07-20 09:40:00 -0700279 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000280
robertphillipsd771f6b2014-07-22 10:18:06 -0700281 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
282
283 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
284
robertphillips320c9232014-07-29 06:07:19 -0700285 // This implements a plot-centric locking mechanism (since the atlas
286 // backing texture is always locked). Each layer that is locked (i.e.,
287 // needed for the current rendering) in a plot increments the plot lock
robertphillipsa32c6bc2014-10-09 12:47:01 -0700288 // count for that plot. Similarly, once a rendering is complete all the
289 // layers used in it decrement the lock count for the used plots.
290 // Plots with a 0 lock count are open for recycling/purging.
robertphillips320c9232014-07-29 06:07:19 -0700291 int fPlotLocks[kNumPlotsX * kNumPlotsY];
292
robertphillips7bb9ed72014-10-10 11:38:29 -0700293 // Inform the cache that layer's cached image is not currently required
294 void unlock(GrCachedLayer* layer);
295
robertphillips952841b2014-06-30 08:26:50 -0700296 void initAtlas();
robertphillips4815fe52014-09-16 10:32:43 -0700297 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700298 const SkIRect& bounds, const SkMatrix& ctm,
299 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700300
robertphillipsd771f6b2014-07-22 10:18:06 -0700301 // Remove all the layers (and unlock any resources) associated with 'pictureID'
302 void purge(uint32_t pictureID);
303
robertphillips6f294af2014-08-18 08:50:03 -0700304 void purgePlot(GrPlot* plot);
305
robertphillips320c9232014-07-29 06:07:19 -0700306 // Try to find a purgeable plot and clear it out. Return true if a plot
307 // was purged; false otherwise.
308 bool purgePlot();
309
robertphillips7bb9ed72014-10-10 11:38:29 -0700310 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; }
311 void decPlotLock(int plotIdx) {
312 SkASSERT(fPlotLocks[plotIdx] > 0);
313 --fPlotLocks[plotIdx];
314 }
315
robertphillips952841b2014-06-30 08:26:50 -0700316 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700317 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700318 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000319};
320
321#endif