blob: fcf62f9c3a8faa59503b2f9ad627657f785c05de [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"
robertphillips3d533ac2014-07-20 09:40:00 -070014#include "SkChecksum.h"
15#include "SkTDynamicHash.h"
robertphillipsd771f6b2014-07-22 10:18:06 -070016#include "SkMessageBus.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000017
robertphillips@google.come930a072014-04-03 00:34:27 +000018class SkPicture;
19
robertphillipsd771f6b2014-07-22 10:18:06 -070020// The layer cache listens for these messages to purge picture-related resources.
21struct GrPictureDeletedMessage {
22 uint32_t pictureID;
23};
24
robertphillips261b8a92014-07-17 08:26:44 -070025// GrPictureInfo stores the atlas plots used by a single picture. A single
26// plot may be used to store layers from multiple pictures.
27struct GrPictureInfo {
28public:
robertphillips3d533ac2014-07-20 09:40:00 -070029 // for SkTDynamicHash - just use the pictureID as the hash key
30 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; }
31 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); }
32
33 // GrPictureInfo proper
robertphillips261b8a92014-07-17 08:26:44 -070034 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { }
35
robertphillips3d533ac2014-07-20 09:40:00 -070036 const uint32_t fPictureID;
robertphillips261b8a92014-07-17 08:26:44 -070037
38 GrAtlas::ClientPlotUsage fPlotUsage;
39};
40
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000041// GrCachedLayer encapsulates the caching information for a single saveLayer.
42//
robertphillips21048b52014-07-15 19:46:35 -070043// Atlased layers get a ref to the backing GrTexture while non-atlased layers
44// get a ref to the GrTexture in which they reside. In both cases 'fRect'
45// contains the layer's extent in its texture.
robertphillips261b8a92014-07-17 08:26:44 -070046// Atlased layers also get a pointer to the plot in which they reside.
robertphillips0c423322014-07-31 11:02:38 -070047// For non-atlased layers, the lock field just corresponds to locking in
48// the resource cache. For atlased layers, it implements an additional level
robertphillips320c9232014-07-29 06:07:19 -070049// of locking to allow atlased layers to be reused multiple times.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000050struct GrCachedLayer {
robertphillips@google.come930a072014-04-03 00:34:27 +000051public:
robertphillips3d533ac2014-07-20 09:40:00 -070052 // For SkTDynamicHash
53 struct Key {
robertphillips0c423322014-07-31 11:02:38 -070054 Key(uint32_t pictureID, int start, int stop, const SkMatrix& ctm)
55 : fPictureID(pictureID)
56 , fStart(start)
57 , fStop(stop)
58 , fCTM(ctm) {
59 fCTM.getType(); // force initialization of type so hashes match
robertphillips3d533ac2014-07-20 09:40:00 -070060
robertphillips0c423322014-07-31 11:02:38 -070061 // Key needs to be tightly packed.
62 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + 2 * sizeof(int) +
63 9 * sizeof(SkScalar) + sizeof(uint32_t));
robertphillips3d533ac2014-07-20 09:40:00 -070064 }
65
robertphillips0c423322014-07-31 11:02:38 -070066 bool operator==(const Key& other) const {
67 return fPictureID == other.fPictureID &&
68 fStart == other.fStart &&
69 fStop == other.fStop &&
70 fCTM.cheapEqualTo(other.fCTM);
71 }
72
73 uint32_t pictureID() const { return fPictureID; }
74 int start() const { return fStart; }
75 int stop() const { return fStop; }
76 const SkMatrix& ctm() const { return fCTM; }
robertphillips3d533ac2014-07-20 09:40:00 -070077
78 private:
79 // ID of the picture of which this layer is a part
80 const uint32_t fPictureID;
robertphillips0c423322014-07-31 11:02:38 -070081 // The range of commands in the picture this layer represents
82 const int fStart;
83 const int fStop;
84 // The CTM applied to this layer in the picture
85 SkMatrix fCTM;
robertphillips3d533ac2014-07-20 09:40:00 -070086 };
87
88 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
89 static uint32_t Hash(const Key& key) {
robertphillips0c423322014-07-31 11:02:38 -070090 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
robertphillips952841b2014-06-30 08:26:50 -070091 }
92
robertphillips3d533ac2014-07-20 09:40:00 -070093 // GrCachedLayer proper
robertphillips0c423322014-07-31 11:02:38 -070094 GrCachedLayer(uint32_t pictureID, int start, int stop, const SkMatrix& ctm)
95 : fKey(pictureID, start, stop, ctm)
robertphillips3d533ac2014-07-20 09:40:00 -070096 , fTexture(NULL)
97 , fRect(GrIRect16::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -070098 , fPlot(NULL)
99 , fLocked(false) {
robertphillips0c423322014-07-31 11:02:38 -0700100 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -0700101 }
102
robertphillipsed6f03e2014-07-30 07:31:35 -0700103 ~GrCachedLayer() {
104 SkSafeUnref(fTexture);
105 }
106
robertphillips0c423322014-07-31 11:02:38 -0700107 uint32_t pictureID() const { return fKey.pictureID(); }
108 int start() const { return fKey.start(); }
109 int stop() const { return fKey.stop(); }
110 const SkMatrix& ctm() const { return fKey.ctm(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000111
robertphillips952841b2014-06-30 08:26:50 -0700112 void setTexture(GrTexture* texture, const GrIRect16& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700113 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700114 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000115 }
robertphillips952841b2014-06-30 08:26:50 -0700116 GrTexture* texture() { return fTexture; }
117 const GrIRect16& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000118
robertphillips261b8a92014-07-17 08:26:44 -0700119 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700120 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700121 fPlot = plot;
122 }
123 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700124
robertphillips261b8a92014-07-17 08:26:44 -0700125 bool isAtlased() const { return NULL != fPlot; }
126
robertphillips320c9232014-07-29 06:07:19 -0700127 void setLocked(bool locked) { fLocked = locked; }
128 bool locked() const { return fLocked; }
129
130 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700131 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700132
robertphillips@google.come930a072014-04-03 00:34:27 +0000133private:
robertphillips3d533ac2014-07-20 09:40:00 -0700134 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000135
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000136 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700137 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000138 GrTexture* fTexture;
139
robertphillips21048b52014-07-15 19:46:35 -0700140 // For both atlased and non-atlased layers 'fRect' contains the bound of
141 // the layer in whichever texture it resides. It is empty when 'fTexture'
142 // is NULL.
robertphillips952841b2014-06-30 08:26:50 -0700143 GrIRect16 fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700144
145 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
146 // It is always NULL for non-atlased layers.
147 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700148
149 // For non-atlased layers 'fLocked' should always match "NULL != fTexture".
150 // (i.e., if there is a texture it is locked).
151 // For atlased layers, 'fLocked' is true if the layer is in a plot and
152 // actively required for rendering. If the layer is in a plot but not
153 // actively required for rendering, then 'fLocked' is false. If the
154 // layer isn't in a plot then is can never be locked.
155 bool fLocked;
robertphillips@google.come930a072014-04-03 00:34:27 +0000156};
157
158// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000159// Non-atlased layers are stored in their own GrTexture while the atlased
160// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700161// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000162// and one GrPlot (for the entire atlas). As such, the GrLayerCache
163// roughly combines the functionality of the GrFontCache and GrTextStrike
164// classes.
165class GrLayerCache {
166public:
robertphillips4ec84da2014-06-24 13:10:43 -0700167 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000168 ~GrLayerCache();
169
robertphillips4ec84da2014-06-24 13:10:43 -0700170 // As a cache, the GrLayerCache can be ordered to free up all its cached
171 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000172 void freeAll();
173
robertphillips6f294af2014-08-18 08:50:03 -0700174 GrCachedLayer* findLayer(uint32_t pictureID, int start, int stop, const SkMatrix& ctm);
175 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700176 int start, int stop,
177 const SkMatrix& ctm);
robertphillips6f294af2014-08-18 08:50:03 -0700178
179 // Inform the cache that layer's cached image is now required.
180 // Return true if the layer must be re-rendered. Return false if the
181 // layer was found in the cache and can be reused.
182 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas);
robertphillips4ec84da2014-06-24 13:10:43 -0700183
184 // Inform the cache that layer's cached image is not currently required
185 void unlock(GrCachedLayer* layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000186
robertphillipsd771f6b2014-07-22 10:18:06 -0700187 // Setup to be notified when 'picture' is deleted
188 void trackPicture(const SkPicture* picture);
189
190 // Cleanup after any SkPicture deletions
191 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700192
robertphillips21048b52014-07-15 19:46:35 -0700193 SkDEBUGCODE(void validate() const;)
194
robertphillips@google.come930a072014-04-03 00:34:27 +0000195private:
robertphillips320c9232014-07-29 06:07:19 -0700196 static const int kAtlasTextureWidth = 1024;
197 static const int kAtlasTextureHeight = 1024;
198
robertphillips261b8a92014-07-17 08:26:44 -0700199 static const int kNumPlotsX = 2;
200 static const int kNumPlotsY = 2;
201
robertphillips320c9232014-07-29 06:07:19 -0700202 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
203 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
204
robertphillips4ec84da2014-06-24 13:10:43 -0700205 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700206 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700207
208 // We cache this information here (rather then, say, on the owning picture)
209 // because we want to be able to clean it up as needed (e.g., if a picture
210 // is leaked and never cleans itself up we still want to be able to
211 // remove the GrPictureInfo once its layers are purged from all the atlas
212 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700213 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000214
robertphillips3d533ac2014-07-20 09:40:00 -0700215 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000216
robertphillipsd771f6b2014-07-22 10:18:06 -0700217 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
218
219 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
220
robertphillips320c9232014-07-29 06:07:19 -0700221 // This implements a plot-centric locking mechanism (since the atlas
222 // backing texture is always locked). Each layer that is locked (i.e.,
223 // needed for the current rendering) in a plot increments the plot lock
224 // count for that plot. Similarly, once a rendering is complete all the
225 // layers used in it decrement the lock count for the used plots.
226 // Plots with a 0 lock count are open for recycling/purging.
227 int fPlotLocks[kNumPlotsX * kNumPlotsY];
228
robertphillips952841b2014-06-30 08:26:50 -0700229 void initAtlas();
robertphillips6f294af2014-08-18 08:50:03 -0700230 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, const SkMatrix& ctm);
231
232public:
233 void purgeAll();
robertphillips952841b2014-06-30 08:26:50 -0700234
robertphillipsd771f6b2014-07-22 10:18:06 -0700235 // Remove all the layers (and unlock any resources) associated with 'pictureID'
236 void purge(uint32_t pictureID);
237
robertphillips320c9232014-07-29 06:07:19 -0700238 static bool PlausiblyAtlasable(int width, int height) {
239 return width <= kPlotWidth && height <= kPlotHeight;
240 }
241
robertphillips6f294af2014-08-18 08:50:03 -0700242 void purgePlot(GrPlot* plot);
243
robertphillips320c9232014-07-29 06:07:19 -0700244 // Try to find a purgeable plot and clear it out. Return true if a plot
245 // was purged; false otherwise.
246 bool purgePlot();
247
robertphillips952841b2014-06-30 08:26:50 -0700248 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700249 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700250 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000251};
252
253#endif