2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrLayerCache_DEFINED
9 #define GrLayerCache_DEFINED
12 #include "GrPictureUtils.h"
14 #include "SkChecksum.h"
15 #include "SkTDynamicHash.h"
16 #include "SkMessageBus.h"
20 // The layer cache listens for these messages to purge picture-related resources.
21 struct GrPictureDeletedMessage {
25 // GrPictureInfo stores the atlas plots used by a single picture. A single
26 // plot may be used to store layers from multiple pictures.
27 struct GrPictureInfo {
29 // 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); }
33 // GrPictureInfo proper
34 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { }
36 const uint32_t fPictureID;
38 GrAtlas::ClientPlotUsage fPlotUsage;
41 // GrCachedLayer encapsulates the caching information for a single saveLayer.
43 // 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.
46 // Atlased layers also get a pointer to the plot in which they reside.
47 // For non-atlased layers, the lock field just corresponds to locking in
48 // the resource cache. For atlased layers, it implements an additional level
49 // of locking to allow atlased layers to be reused multiple times.
50 struct GrCachedLayer {
54 Key(uint32_t pictureID, int start, int stop, const SkMatrix& ctm)
55 : fPictureID(pictureID)
59 fCTM.getType(); // force initialization of type so hashes match
61 // 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));
66 bool operator==(const Key& other) const {
67 return fPictureID == other.fPictureID &&
68 fStart == other.fStart &&
69 fStop == other.fStop &&
70 fCTM.cheapEqualTo(other.fCTM);
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; }
79 // ID of the picture of which this layer is a part
80 const uint32_t fPictureID;
81 // The range of commands in the picture this layer represents
84 // The CTM applied to this layer in the picture
88 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
89 static uint32_t Hash(const Key& key) {
90 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
93 // GrCachedLayer proper
94 GrCachedLayer(uint32_t pictureID, int start, int stop, const SkMatrix& ctm)
95 : fKey(pictureID, start, stop, ctm)
97 , fRect(GrIRect16::MakeEmpty())
100 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
104 SkSafeUnref(fTexture);
107 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(); }
112 void setTexture(GrTexture* texture, const GrIRect16& rect) {
113 SkRefCnt_SafeAssign(fTexture, texture);
116 GrTexture* texture() { return fTexture; }
117 const GrIRect16& rect() const { return fRect; }
119 void setPlot(GrPlot* plot) {
120 SkASSERT(NULL == fPlot);
123 GrPlot* plot() { return fPlot; }
125 bool isAtlased() const { return NULL != fPlot; }
127 void setLocked(bool locked) { fLocked = locked; }
128 bool locked() const { return fLocked; }
130 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
131 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
136 // fTexture is a ref on the atlasing texture for atlased layers and a
137 // ref on a GrTexture for non-atlased textures.
140 // 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'
145 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
146 // It is always NULL for non-atlased layers.
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.
158 // The GrLayerCache caches pre-computed saveLayers for later rendering.
159 // Non-atlased layers are stored in their own GrTexture while the atlased
160 // layers share a single GrTexture.
161 // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
162 // and one GrPlot (for the entire atlas). As such, the GrLayerCache
163 // roughly combines the functionality of the GrFontCache and GrTextStrike
167 GrLayerCache(GrContext*);
170 // As a cache, the GrLayerCache can be ordered to free up all its cached
171 // elements by the GrContext
174 GrCachedLayer* findLayer(const SkPicture* picture, int start, int stop, const SkMatrix& ctm);
175 GrCachedLayer* findLayerOrCreate(const SkPicture* picture,
177 const SkMatrix& ctm);
179 // Inform the cache that layer's cached image is now required. Return true
180 // if it was found in the ResourceCache and doesn't need to be regenerated.
181 // If false is returned the caller should (re)render the layer into the
182 // newly acquired texture.
183 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc);
185 // Inform the cache that layer's cached image is not currently required
186 void unlock(GrCachedLayer* layer);
188 // Setup to be notified when 'picture' is deleted
189 void trackPicture(const SkPicture* picture);
191 // Cleanup after any SkPicture deletions
192 void processDeletedPictures();
194 SkDEBUGCODE(void validate() const;)
197 static const int kAtlasTextureWidth = 1024;
198 static const int kAtlasTextureHeight = 1024;
200 static const int kNumPlotsX = 2;
201 static const int kNumPlotsY = 2;
203 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
204 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
206 GrContext* fContext; // pointer back to owning context
207 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
209 // We cache this information here (rather then, say, on the owning picture)
210 // because we want to be able to clean it up as needed (e.g., if a picture
211 // is leaked and never cleans itself up we still want to be able to
212 // remove the GrPictureInfo once its layers are purged from all the atlas
214 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
216 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
218 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
220 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
222 // This implements a plot-centric locking mechanism (since the atlas
223 // backing texture is always locked). Each layer that is locked (i.e.,
224 // needed for the current rendering) in a plot increments the plot lock
225 // count for that plot. Similarly, once a rendering is complete all the
226 // layers used in it decrement the lock count for the used plots.
227 // Plots with a 0 lock count are open for recycling/purging.
228 int fPlotLocks[kNumPlotsX * kNumPlotsY];
231 GrCachedLayer* createLayer(const SkPicture* picture, int start, int stop, const SkMatrix& ctm);
233 // Remove all the layers (and unlock any resources) associated with 'pictureID'
234 void purge(uint32_t pictureID);
236 static bool PlausiblyAtlasable(int width, int height) {
237 return width <= kPlotWidth && height <= kPlotHeight;
240 // Try to find a purgeable plot and clear it out. Return true if a plot
241 // was purged; false otherwise.
245 friend class TestingAccess;
246 int numLayers() const { return fLayerHash.count(); }