blob: adf904f833b4688126ec24805000fb1f2fbe4283 [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
robertphillipsd771f6b2014-07-22 10:18:06 -070021// The layer cache listens for these messages to purge picture-related resources.
22struct GrPictureDeletedMessage {
23 uint32_t pictureID;
24};
25
robertphillips261b8a92014-07-17 08:26:44 -070026// GrPictureInfo stores the atlas plots used by a single picture. A single
27// plot may be used to store layers from multiple pictures.
28struct GrPictureInfo {
29public:
robertphillips3d533ac2014-07-20 09:40:00 -070030 // for SkTDynamicHash - just use the pictureID as the hash key
31 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; }
32 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); }
33
34 // GrPictureInfo proper
robertphillips261b8a92014-07-17 08:26:44 -070035 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { }
36
robertphillips3d533ac2014-07-20 09:40:00 -070037 const uint32_t fPictureID;
robertphillips261b8a92014-07-17 08:26:44 -070038
39 GrAtlas::ClientPlotUsage fPlotUsage;
40};
41
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000042// GrCachedLayer encapsulates the caching information for a single saveLayer.
43//
robertphillips21048b52014-07-15 19:46:35 -070044// Atlased layers get a ref to the backing GrTexture while non-atlased layers
45// get a ref to the GrTexture in which they reside. In both cases 'fRect'
46// contains the layer's extent in its texture.
robertphillips261b8a92014-07-17 08:26:44 -070047// Atlased layers also get a pointer to the plot in which they reside.
robertphillips0c423322014-07-31 11:02:38 -070048// For non-atlased layers, the lock field just corresponds to locking in
49// the resource cache. For atlased layers, it implements an additional level
robertphillips320c9232014-07-29 06:07:19 -070050// of locking to allow atlased layers to be reused multiple times.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000051struct GrCachedLayer {
robertphillips@google.come930a072014-04-03 00:34:27 +000052public:
robertphillips3d533ac2014-07-20 09:40:00 -070053 // For SkTDynamicHash
54 struct Key {
robertphillips3aac6e02014-10-20 08:52:40 -070055 Key(uint32_t pictureID, int start, const SkIRect& bounds, const SkMatrix& ctm)
robertphillips0c423322014-07-31 11:02:38 -070056 : fPictureID(pictureID)
robertphillips3aac6e02014-10-20 08:52:40 -070057 , fStart(start)
58 , fBounds(bounds)
59 , fCTM(ctm) {
60 fCTM.getType(); // force initialization of type so hashes match
61
robertphillips0c423322014-07-31 11:02:38 -070062 // Key needs to be tightly packed.
robertphillipsed420592014-09-29 11:39:38 -070063 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // picture ID
64 sizeof(int) + // start index
robertphillips3aac6e02014-10-20 08:52:40 -070065 4 * sizeof(uint32_t) + // bounds
66 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix
robertphillips3d533ac2014-07-20 09:40:00 -070067 }
68
robertphillips0c423322014-07-31 11:02:38 -070069 bool operator==(const Key& other) const {
70 return fPictureID == other.fPictureID &&
71 fStart == other.fStart &&
robertphillips3aac6e02014-10-20 08:52:40 -070072 fBounds == other.fBounds &&
73 fCTM.cheapEqualTo(other.fCTM);
robertphillips0c423322014-07-31 11:02:38 -070074 }
75
76 uint32_t pictureID() const { return fPictureID; }
77 int start() const { return fStart; }
robertphillips3aac6e02014-10-20 08:52:40 -070078 const SkIRect& bound() const { return fBounds; }
robertphillips3d533ac2014-07-20 09:40:00 -070079
80 private:
81 // ID of the picture of which this layer is a part
82 const uint32_t fPictureID;
robertphillipsed420592014-09-29 11:39:38 -070083 // The the index of the saveLayer command in the picture
robertphillips0c423322014-07-31 11:02:38 -070084 const int fStart;
robertphillips3aac6e02014-10-20 08:52:40 -070085 // The bounds of the layer. The TL corner is its offset.
86 const SkIRect fBounds;
robertphillipsed420592014-09-29 11:39:38 -070087 // The 2x2 portion of the CTM applied to this layer in the picture
robertphillips3aac6e02014-10-20 08:52:40 -070088 SkMatrix fCTM;
robertphillips3d533ac2014-07-20 09:40:00 -070089 };
90
91 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
92 static uint32_t Hash(const Key& key) {
robertphillips0c423322014-07-31 11:02:38 -070093 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
robertphillips952841b2014-06-30 08:26:50 -070094 }
95
robertphillips3d533ac2014-07-20 09:40:00 -070096 // GrCachedLayer proper
robertphillips4815fe52014-09-16 10:32:43 -070097 GrCachedLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -070098 const SkIRect& bounds, const SkMatrix& ctm,
99 const SkPaint* paint)
100 : fKey(pictureID, start, bounds, ctm)
robertphillipsed420592014-09-29 11:39:38 -0700101 , fStop(stop)
robertphillipsa0537de2014-09-18 08:01:23 -0700102 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL)
robertphillips3d533ac2014-07-20 09:40:00 -0700103 , fTexture(NULL)
104 , fRect(GrIRect16::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -0700105 , fPlot(NULL)
robertphillips7bb9ed72014-10-10 11:38:29 -0700106 , fUses(0)
robertphillips320c9232014-07-29 06:07:19 -0700107 , fLocked(false) {
robertphillips0c423322014-07-31 11:02:38 -0700108 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -0700109 }
110
robertphillipsed6f03e2014-07-30 07:31:35 -0700111 ~GrCachedLayer() {
112 SkSafeUnref(fTexture);
robertphillipsa0537de2014-09-18 08:01:23 -0700113 SkDELETE(fPaint);
robertphillipsed6f03e2014-07-30 07:31:35 -0700114 }
115
robertphillips0c423322014-07-31 11:02:38 -0700116 uint32_t pictureID() const { return fKey.pictureID(); }
117 int start() const { return fKey.start(); }
robertphillips3aac6e02014-10-20 08:52:40 -0700118 const SkIRect& bound() const { return fKey.bound(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000119
robertphillipsed420592014-09-29 11:39:38 -0700120 int stop() const { return fStop; }
robertphillips952841b2014-06-30 08:26:50 -0700121 void setTexture(GrTexture* texture, const GrIRect16& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700122 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700123 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000124 }
robertphillips952841b2014-06-30 08:26:50 -0700125 GrTexture* texture() { return fTexture; }
robertphillips4aa6dfc2014-09-17 07:50:47 -0700126 const SkPaint* paint() const { return fPaint; }
robertphillips952841b2014-06-30 08:26:50 -0700127 const GrIRect16& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000128
robertphillips261b8a92014-07-17 08:26:44 -0700129 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700130 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700131 fPlot = plot;
132 }
133 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700134
bsalomon49f085d2014-09-05 13:34:00 -0700135 bool isAtlased() const { return SkToBool(fPlot); }
robertphillips261b8a92014-07-17 08:26:44 -0700136
robertphillips320c9232014-07-29 06:07:19 -0700137 void setLocked(bool locked) { fLocked = locked; }
138 bool locked() const { return fLocked; }
139
140 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700141 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700142
robertphillips@google.come930a072014-04-03 00:34:27 +0000143private:
robertphillips3d533ac2014-07-20 09:40:00 -0700144 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000145
robertphillipsed420592014-09-29 11:39:38 -0700146 // The final "restore" operation index of the cached layer
147 const int fStop;
148
robertphillips4aa6dfc2014-09-17 07:50:47 -0700149 // The paint used when dropping the layer down into the owning canvas.
robertphillipsa0537de2014-09-18 08:01:23 -0700150 // Can be NULL. This class makes a copy for itself.
robertphillips4aa6dfc2014-09-17 07:50:47 -0700151 const SkPaint* fPaint;
152
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000153 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700154 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000155 GrTexture* fTexture;
156
robertphillips21048b52014-07-15 19:46:35 -0700157 // For both atlased and non-atlased layers 'fRect' contains the bound of
158 // the layer in whichever texture it resides. It is empty when 'fTexture'
159 // is NULL.
robertphillips952841b2014-06-30 08:26:50 -0700160 GrIRect16 fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700161
162 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
163 // It is always NULL for non-atlased layers.
164 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700165
robertphillips7bb9ed72014-10-10 11:38:29 -0700166 // The number of actively hoisted layers using this cached image (e.g.,
167 // extant GrHoistedLayers pointing at this object). This object will
168 // be unlocked when the use count reaches 0.
169 int fUses;
170
bsalomon49f085d2014-09-05 13:34:00 -0700171 // For non-atlased layers 'fLocked' should always match "fTexture".
robertphillips320c9232014-07-29 06:07:19 -0700172 // (i.e., if there is a texture it is locked).
173 // For atlased layers, 'fLocked' is true if the layer is in a plot and
174 // actively required for rendering. If the layer is in a plot but not
175 // actively required for rendering, then 'fLocked' is false. If the
176 // layer isn't in a plot then is can never be locked.
177 bool fLocked;
robertphillips7bb9ed72014-10-10 11:38:29 -0700178
179 void addUse() { ++fUses; }
180 void removeUse() { SkASSERT(fUses > 0); --fUses; }
181 int uses() const { return fUses; }
182
183 friend class GrLayerCache; // for access to usage methods
184 friend class TestingAccess; // for testing
robertphillips@google.come930a072014-04-03 00:34:27 +0000185};
186
187// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000188// Non-atlased layers are stored in their own GrTexture while the atlased
189// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700190// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000191// and one GrPlot (for the entire atlas). As such, the GrLayerCache
192// roughly combines the functionality of the GrFontCache and GrTextStrike
193// classes.
194class GrLayerCache {
195public:
robertphillips4ec84da2014-06-24 13:10:43 -0700196 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000197 ~GrLayerCache();
198
robertphillips4ec84da2014-06-24 13:10:43 -0700199 // As a cache, the GrLayerCache can be ordered to free up all its cached
200 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000201 void freeAll();
202
robertphillips3aac6e02014-10-20 08:52:40 -0700203 GrCachedLayer* findLayer(uint32_t pictureID, int start,
204 const SkIRect& bounds, const SkMatrix& ctm);
robertphillips6f294af2014-08-18 08:50:03 -0700205 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700206 int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700207 const SkIRect& bounds,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700208 const SkMatrix& ctm,
209 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700210
211 // Inform the cache that layer's cached image is now required.
212 // Return true if the layer must be re-rendered. Return false if the
213 // layer was found in the cache and can be reused.
214 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas);
robertphillips4ec84da2014-06-24 13:10:43 -0700215
robertphillips7bb9ed72014-10-10 11:38:29 -0700216 // addUse is just here to keep the API symmetric
217 void addUse(GrCachedLayer* layer) { layer->addUse(); }
218 void removeUse(GrCachedLayer* layer) {
219 layer->removeUse();
220 if (layer->uses() == 0) {
221 // If no one cares about the layer allow it to be recycled.
222 this->unlock(layer);
223 }
224 }
robertphillips@google.come930a072014-04-03 00:34:27 +0000225
robertphillipsd771f6b2014-07-22 10:18:06 -0700226 // Setup to be notified when 'picture' is deleted
227 void trackPicture(const SkPicture* picture);
228
229 // Cleanup after any SkPicture deletions
230 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700231
robertphillips21048b52014-07-15 19:46:35 -0700232 SkDEBUGCODE(void validate() const;)
233
robertphillips84ac0822014-10-14 07:07:59 -0700234#ifdef SK_DEVELOPER
235 void writeLayersToDisk(const SkString& dirName);
236#endif
237
robertphillips@google.come930a072014-04-03 00:34:27 +0000238private:
robertphillips320c9232014-07-29 06:07:19 -0700239 static const int kAtlasTextureWidth = 1024;
240 static const int kAtlasTextureHeight = 1024;
241
robertphillips261b8a92014-07-17 08:26:44 -0700242 static const int kNumPlotsX = 2;
243 static const int kNumPlotsY = 2;
244
robertphillips320c9232014-07-29 06:07:19 -0700245 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
246 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
247
robertphillips4ec84da2014-06-24 13:10:43 -0700248 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700249 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700250
251 // We cache this information here (rather then, say, on the owning picture)
252 // because we want to be able to clean it up as needed (e.g., if a picture
253 // is leaked and never cleans itself up we still want to be able to
254 // remove the GrPictureInfo once its layers are purged from all the atlas
255 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700256 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000257
robertphillips3d533ac2014-07-20 09:40:00 -0700258 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000259
robertphillipsd771f6b2014-07-22 10:18:06 -0700260 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
261
262 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
263
robertphillips320c9232014-07-29 06:07:19 -0700264 // This implements a plot-centric locking mechanism (since the atlas
265 // backing texture is always locked). Each layer that is locked (i.e.,
266 // needed for the current rendering) in a plot increments the plot lock
robertphillipsa32c6bc2014-10-09 12:47:01 -0700267 // count for that plot. Similarly, once a rendering is complete all the
268 // layers used in it decrement the lock count for the used plots.
269 // Plots with a 0 lock count are open for recycling/purging.
robertphillips320c9232014-07-29 06:07:19 -0700270 int fPlotLocks[kNumPlotsX * kNumPlotsY];
271
robertphillips7bb9ed72014-10-10 11:38:29 -0700272 // Inform the cache that layer's cached image is not currently required
273 void unlock(GrCachedLayer* layer);
274
robertphillips952841b2014-06-30 08:26:50 -0700275 void initAtlas();
robertphillips4815fe52014-09-16 10:32:43 -0700276 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop,
robertphillips3aac6e02014-10-20 08:52:40 -0700277 const SkIRect& bounds, const SkMatrix& ctm,
278 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700279
robertphillips6f294af2014-08-18 08:50:03 -0700280 void purgeAll();
robertphillips952841b2014-06-30 08:26:50 -0700281
robertphillipsd771f6b2014-07-22 10:18:06 -0700282 // Remove all the layers (and unlock any resources) associated with 'pictureID'
283 void purge(uint32_t pictureID);
284
robertphillips320c9232014-07-29 06:07:19 -0700285 static bool PlausiblyAtlasable(int width, int height) {
286 return width <= kPlotWidth && height <= kPlotHeight;
287 }
288
robertphillips6f294af2014-08-18 08:50:03 -0700289 void purgePlot(GrPlot* plot);
290
robertphillips320c9232014-07-29 06:07:19 -0700291 // Try to find a purgeable plot and clear it out. Return true if a plot
292 // was purged; false otherwise.
293 bool purgePlot();
294
robertphillips7bb9ed72014-10-10 11:38:29 -0700295 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; }
296 void decPlotLock(int plotIdx) {
297 SkASSERT(fPlotLocks[plotIdx] > 0);
298 --fPlotLocks[plotIdx];
299 }
300
robertphillips952841b2014-06-30 08:26:50 -0700301 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700302 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700303 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000304};
305
306#endif