blob: 69b59cf747b948a795119aa10fc512426195c09a [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:
robertphillips225a6272014-10-30 11:39:19 -070033 static const int kNumPlots = 4;
34
robertphillips3d533ac2014-07-20 09:40:00 -070035 // for SkTDynamicHash - just use the pictureID as the hash key
36 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; }
37 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); }
38
39 // GrPictureInfo proper
robertphillips225a6272014-10-30 11:39:19 -070040 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) {
41#if !GR_CACHE_HOISTED_LAYERS
42 memset(fPlotUses, 0, sizeof(fPlotUses));
43#endif
44 }
45
46#if !GR_CACHE_HOISTED_LAYERS
47 void incPlotUsage(int plotID) {
48 SkASSERT(plotID < kNumPlots);
49 fPlotUses[plotID]++;
50 }
51
52 void decPlotUsage(int plotID) {
53 SkASSERT(plotID < kNumPlots);
54 SkASSERT(fPlotUses[plotID] > 0);
55 fPlotUses[plotID]--;
56 }
57
58 int plotUsage(int plotID) const {
59 SkASSERT(plotID < kNumPlots);
60 return fPlotUses[plotID];
61 }
62#endif
robertphillips261b8a92014-07-17 08:26:44 -070063
robertphillips3d533ac2014-07-20 09:40:00 -070064 const uint32_t fPictureID;
robertphillips261b8a92014-07-17 08:26:44 -070065 GrAtlas::ClientPlotUsage fPlotUsage;
robertphillips225a6272014-10-30 11:39:19 -070066
67#if !GR_CACHE_HOISTED_LAYERS
68private:
69 int fPlotUses[kNumPlots];
70#endif
robertphillips261b8a92014-07-17 08:26:44 -070071};
72
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000073// GrCachedLayer encapsulates the caching information for a single saveLayer.
74//
robertphillips21048b52014-07-15 19:46:35 -070075// Atlased layers get a ref to the backing GrTexture while non-atlased layers
76// get a ref to the GrTexture in which they reside. In both cases 'fRect'
77// contains the layer's extent in its texture.
robertphillips261b8a92014-07-17 08:26:44 -070078// Atlased layers also get a pointer to the plot in which they reside.
robertphillips0c423322014-07-31 11:02:38 -070079// For non-atlased layers, the lock field just corresponds to locking in
80// the resource cache. For atlased layers, it implements an additional level
robertphillips320c9232014-07-29 06:07:19 -070081// of locking to allow atlased layers to be reused multiple times.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000082struct GrCachedLayer {
robertphillips@google.come930a072014-04-03 00:34:27 +000083public:
robertphillips3d533ac2014-07-20 09:40:00 -070084 // For SkTDynamicHash
85 struct Key {
robertphillips3aac6e02014-10-20 08:52:40 -070086 Key(uint32_t pictureID, int start, const SkIRect& bounds, const SkMatrix& ctm)
robertphillips0c423322014-07-31 11:02:38 -070087 : fPictureID(pictureID)
robertphillips3aac6e02014-10-20 08:52:40 -070088 , fStart(start)
89 , fBounds(bounds)
90 , fCTM(ctm) {
91 fCTM.getType(); // force initialization of type so hashes match
92
robertphillips0c423322014-07-31 11:02:38 -070093 // Key needs to be tightly packed.
robertphillipsed420592014-09-29 11:39:38 -070094 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // picture ID
95 sizeof(int) + // start index
robertphillips3aac6e02014-10-20 08:52:40 -070096 4 * sizeof(uint32_t) + // bounds
97 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix
robertphillips3d533ac2014-07-20 09:40:00 -070098 }
99
robertphillips0c423322014-07-31 11:02:38 -0700100 bool operator==(const Key& other) const {
101 return fPictureID == other.fPictureID &&
102 fStart == other.fStart &&
robertphillips3aac6e02014-10-20 08:52:40 -0700103 fBounds == other.fBounds &&
104 fCTM.cheapEqualTo(other.fCTM);
robertphillips0c423322014-07-31 11:02:38 -0700105 }
106
107 uint32_t pictureID() const { return fPictureID; }
108 int start() const { return fStart; }
robertphillips3aac6e02014-10-20 08:52:40 -0700109 const SkIRect& bound() const { return fBounds; }
robertphillips3d533ac2014-07-20 09:40:00 -0700110
111 private:
112 // ID of the picture of which this layer is a part
113 const uint32_t fPictureID;
robertphillipsed420592014-09-29 11:39:38 -0700114 // The the index of the saveLayer command in the picture
robertphillips0c423322014-07-31 11:02:38 -0700115 const int fStart;
robertphillips3aac6e02014-10-20 08:52:40 -0700116 // The bounds of the layer. The TL corner is its offset.
117 const SkIRect fBounds;
robertphillipsed420592014-09-29 11:39:38 -0700118 // The 2x2 portion of the CTM applied to this layer in the picture
robertphillips3aac6e02014-10-20 08:52:40 -0700119 SkMatrix fCTM;
robertphillips3d533ac2014-07-20 09:40:00 -0700120 };
121
122 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
123 static uint32_t Hash(const Key& key) {
robertphillips0c423322014-07-31 11:02:38 -0700124 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
robertphillips952841b2014-06-30 08:26:50 -0700125 }
126
robertphillips3d533ac2014-07-20 09:40:00 -0700127 // GrCachedLayer proper
robertphillips4815fe52014-09-16 10:32:43 -0700128 GrCachedLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700129 const SkIRect& bounds, const SkMatrix& ctm,
130 const SkPaint* paint)
131 : fKey(pictureID, start, bounds, ctm)
robertphillipsed420592014-09-29 11:39:38 -0700132 , fStop(stop)
robertphillipsa0537de2014-09-18 08:01:23 -0700133 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL)
robertphillips3d533ac2014-07-20 09:40:00 -0700134 , fTexture(NULL)
135 , fRect(GrIRect16::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -0700136 , fPlot(NULL)
robertphillips7bb9ed72014-10-10 11:38:29 -0700137 , fUses(0)
robertphillips320c9232014-07-29 06:07:19 -0700138 , fLocked(false) {
robertphillips0c423322014-07-31 11:02:38 -0700139 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -0700140 }
141
robertphillipsed6f03e2014-07-30 07:31:35 -0700142 ~GrCachedLayer() {
143 SkSafeUnref(fTexture);
robertphillipsa0537de2014-09-18 08:01:23 -0700144 SkDELETE(fPaint);
robertphillipsed6f03e2014-07-30 07:31:35 -0700145 }
146
robertphillips0c423322014-07-31 11:02:38 -0700147 uint32_t pictureID() const { return fKey.pictureID(); }
148 int start() const { return fKey.start(); }
robertphillips3aac6e02014-10-20 08:52:40 -0700149 const SkIRect& bound() const { return fKey.bound(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000150
robertphillipsed420592014-09-29 11:39:38 -0700151 int stop() const { return fStop; }
robertphillips952841b2014-06-30 08:26:50 -0700152 void setTexture(GrTexture* texture, const GrIRect16& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700153 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700154 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000155 }
robertphillips952841b2014-06-30 08:26:50 -0700156 GrTexture* texture() { return fTexture; }
robertphillips4aa6dfc2014-09-17 07:50:47 -0700157 const SkPaint* paint() const { return fPaint; }
robertphillips952841b2014-06-30 08:26:50 -0700158 const GrIRect16& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000159
robertphillips261b8a92014-07-17 08:26:44 -0700160 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700161 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700162 fPlot = plot;
163 }
164 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700165
bsalomon49f085d2014-09-05 13:34:00 -0700166 bool isAtlased() const { return SkToBool(fPlot); }
robertphillips261b8a92014-07-17 08:26:44 -0700167
robertphillips320c9232014-07-29 06:07:19 -0700168 void setLocked(bool locked) { fLocked = locked; }
169 bool locked() const { return fLocked; }
170
171 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700172 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700173
robertphillips@google.come930a072014-04-03 00:34:27 +0000174private:
robertphillips3d533ac2014-07-20 09:40:00 -0700175 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000176
robertphillipsed420592014-09-29 11:39:38 -0700177 // The final "restore" operation index of the cached layer
178 const int fStop;
179
robertphillips4aa6dfc2014-09-17 07:50:47 -0700180 // The paint used when dropping the layer down into the owning canvas.
robertphillipsa0537de2014-09-18 08:01:23 -0700181 // Can be NULL. This class makes a copy for itself.
robertphillips4aa6dfc2014-09-17 07:50:47 -0700182 const SkPaint* fPaint;
183
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000184 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700185 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000186 GrTexture* fTexture;
187
robertphillips21048b52014-07-15 19:46:35 -0700188 // For both atlased and non-atlased layers 'fRect' contains the bound of
189 // the layer in whichever texture it resides. It is empty when 'fTexture'
190 // is NULL.
robertphillips952841b2014-06-30 08:26:50 -0700191 GrIRect16 fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700192
193 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
194 // It is always NULL for non-atlased layers.
195 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700196
robertphillips7bb9ed72014-10-10 11:38:29 -0700197 // The number of actively hoisted layers using this cached image (e.g.,
198 // extant GrHoistedLayers pointing at this object). This object will
199 // be unlocked when the use count reaches 0.
200 int fUses;
201
bsalomon49f085d2014-09-05 13:34:00 -0700202 // For non-atlased layers 'fLocked' should always match "fTexture".
robertphillips320c9232014-07-29 06:07:19 -0700203 // (i.e., if there is a texture it is locked).
204 // For atlased layers, 'fLocked' is true if the layer is in a plot and
205 // actively required for rendering. If the layer is in a plot but not
206 // actively required for rendering, then 'fLocked' is false. If the
207 // layer isn't in a plot then is can never be locked.
208 bool fLocked;
robertphillips7bb9ed72014-10-10 11:38:29 -0700209
210 void addUse() { ++fUses; }
211 void removeUse() { SkASSERT(fUses > 0); --fUses; }
212 int uses() const { return fUses; }
213
214 friend class GrLayerCache; // for access to usage methods
215 friend class TestingAccess; // for testing
robertphillips@google.come930a072014-04-03 00:34:27 +0000216};
217
218// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000219// Non-atlased layers are stored in their own GrTexture while the atlased
220// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700221// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000222// and one GrPlot (for the entire atlas). As such, the GrLayerCache
223// roughly combines the functionality of the GrFontCache and GrTextStrike
224// classes.
225class GrLayerCache {
226public:
robertphillips4ec84da2014-06-24 13:10:43 -0700227 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000228 ~GrLayerCache();
229
robertphillips4ec84da2014-06-24 13:10:43 -0700230 // As a cache, the GrLayerCache can be ordered to free up all its cached
231 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000232 void freeAll();
233
robertphillips3aac6e02014-10-20 08:52:40 -0700234 GrCachedLayer* findLayer(uint32_t pictureID, int start,
235 const SkIRect& bounds, const SkMatrix& ctm);
robertphillips6f294af2014-08-18 08:50:03 -0700236 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700237 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700238 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700239 const SkMatrix& ctm,
240 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700241
robertphillipsfd61ed02014-10-28 07:21:44 -0700242 // Attempt to place 'layer' in the atlas. Return true on success; false on failure.
243 // When true is returned, 'needsRendering' will indicate if the layer must be (re)drawn.
244 // Additionally, the GPU resources will be locked.
bsalomonf2703d82014-10-28 14:33:06 -0700245 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillipsfd61ed02014-10-28 07:21:44 -0700246
247 // Attempt to lock the GPU resources required for a layer. Return true on success;
248 // false on failure. When true is returned 'needsRendering' will indicate if the
249 // layer must be (re)drawn.
250 // Note that atlased layers should already have been locked and rendered so only
251 // free floating layers will have 'needsRendering' set.
252 // Currently, this path always uses a new scratch texture for non-Atlased layers
253 // and (thus) doesn't cache anything. This can yield a lot of re-rendering.
254 // TODO: allow rediscovery of free-floating layers that are still in the resource cache.
bsalomonf2703d82014-10-28 14:33:06 -0700255 bool lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering);
robertphillips4ec84da2014-06-24 13:10:43 -0700256
robertphillips7bb9ed72014-10-10 11:38:29 -0700257 // addUse is just here to keep the API symmetric
258 void addUse(GrCachedLayer* layer) { layer->addUse(); }
259 void removeUse(GrCachedLayer* layer) {
260 layer->removeUse();
261 if (layer->uses() == 0) {
262 // If no one cares about the layer allow it to be recycled.
263 this->unlock(layer);
264 }
265 }
robertphillips@google.come930a072014-04-03 00:34:27 +0000266
robertphillipsd771f6b2014-07-22 10:18:06 -0700267 // Setup to be notified when 'picture' is deleted
268 void trackPicture(const SkPicture* picture);
269
270 // Cleanup after any SkPicture deletions
271 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700272
robertphillips21048b52014-07-15 19:46:35 -0700273 SkDEBUGCODE(void validate() const;)
274
robertphillips84ac0822014-10-14 07:07:59 -0700275#ifdef SK_DEVELOPER
276 void writeLayersToDisk(const SkString& dirName);
277#endif
278
robertphillipsfd61ed02014-10-28 07:21:44 -0700279 static bool PlausiblyAtlasable(int width, int height) {
280 return width <= kPlotWidth && height <= kPlotHeight;
281 }
282
robertphillips4ab5a902014-10-29 13:56:02 -0700283#if !GR_CACHE_HOISTED_LAYERS
284 void purgeAll();
285#endif
286
robertphillips@google.come930a072014-04-03 00:34:27 +0000287private:
robertphillips320c9232014-07-29 06:07:19 -0700288 static const int kAtlasTextureWidth = 1024;
289 static const int kAtlasTextureHeight = 1024;
290
robertphillips261b8a92014-07-17 08:26:44 -0700291 static const int kNumPlotsX = 2;
292 static const int kNumPlotsY = 2;
293
robertphillips320c9232014-07-29 06:07:19 -0700294 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
295 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
296
robertphillips4ec84da2014-06-24 13:10:43 -0700297 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700298 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700299
300 // We cache this information here (rather then, say, on the owning picture)
301 // because we want to be able to clean it up as needed (e.g., if a picture
302 // is leaked and never cleans itself up we still want to be able to
303 // remove the GrPictureInfo once its layers are purged from all the atlas
304 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700305 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000306
robertphillips3d533ac2014-07-20 09:40:00 -0700307 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000308
robertphillipsd771f6b2014-07-22 10:18:06 -0700309 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
310
311 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
312
robertphillips320c9232014-07-29 06:07:19 -0700313 // This implements a plot-centric locking mechanism (since the atlas
314 // backing texture is always locked). Each layer that is locked (i.e.,
315 // needed for the current rendering) in a plot increments the plot lock
robertphillipsa32c6bc2014-10-09 12:47:01 -0700316 // count for that plot. Similarly, once a rendering is complete all the
317 // layers used in it decrement the lock count for the used plots.
318 // Plots with a 0 lock count are open for recycling/purging.
robertphillips320c9232014-07-29 06:07:19 -0700319 int fPlotLocks[kNumPlotsX * kNumPlotsY];
320
robertphillips7bb9ed72014-10-10 11:38:29 -0700321 // Inform the cache that layer's cached image is not currently required
322 void unlock(GrCachedLayer* layer);
323
robertphillips952841b2014-06-30 08:26:50 -0700324 void initAtlas();
robertphillips4815fe52014-09-16 10:32:43 -0700325 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700326 const SkIRect& bounds, const SkMatrix& ctm,
327 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700328
robertphillipsd771f6b2014-07-22 10:18:06 -0700329 // Remove all the layers (and unlock any resources) associated with 'pictureID'
330 void purge(uint32_t pictureID);
331
robertphillips6f294af2014-08-18 08:50:03 -0700332 void purgePlot(GrPlot* plot);
333
robertphillips320c9232014-07-29 06:07:19 -0700334 // Try to find a purgeable plot and clear it out. Return true if a plot
335 // was purged; false otherwise.
336 bool purgePlot();
337
robertphillips7bb9ed72014-10-10 11:38:29 -0700338 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; }
339 void decPlotLock(int plotIdx) {
340 SkASSERT(fPlotLocks[plotIdx] > 0);
341 --fPlotLocks[plotIdx];
342 }
343
robertphillips952841b2014-06-30 08:26:50 -0700344 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700345 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700346 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000347};
348
349#endif