blob: 790b82a8f3e74651ca52efae623ab7e6de9012fe [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 {
robertphillipsed420592014-09-29 11:39:38 -070054 // TODO: the key needs to include the clip
55 Key(uint32_t pictureID, int start, const SkMatrix& ctm)
robertphillips0c423322014-07-31 11:02:38 -070056 : fPictureID(pictureID)
robertphillipsed420592014-09-29 11:39:38 -070057 , fStart(start) {
58 fCTM[0] = ctm.getScaleX();
59 fCTM[1] = ctm.getSkewX();
60 fCTM[2] = ctm.getSkewY();
61 fCTM[3] = ctm.getScaleY();
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
65 4 * sizeof(SkScalar)); // 2x2 from CTM
robertphillips3d533ac2014-07-20 09:40:00 -070066 }
67
robertphillips0c423322014-07-31 11:02:38 -070068 bool operator==(const Key& other) const {
69 return fPictureID == other.fPictureID &&
70 fStart == other.fStart &&
robertphillipsed420592014-09-29 11:39:38 -070071 0 == memcmp(fCTM, other.fCTM, sizeof(fCTM));
robertphillips0c423322014-07-31 11:02:38 -070072 }
73
74 uint32_t pictureID() const { return fPictureID; }
75 int start() const { return fStart; }
robertphillips3d533ac2014-07-20 09:40:00 -070076
77 private:
78 // ID of the picture of which this layer is a part
79 const uint32_t fPictureID;
robertphillipsed420592014-09-29 11:39:38 -070080 // The the index of the saveLayer command in the picture
robertphillips0c423322014-07-31 11:02:38 -070081 const int fStart;
robertphillipsed420592014-09-29 11:39:38 -070082 // The 2x2 portion of the CTM applied to this layer in the picture
83 SkScalar fCTM[4];
robertphillips3d533ac2014-07-20 09:40:00 -070084 };
85
86 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
87 static uint32_t Hash(const Key& key) {
robertphillips0c423322014-07-31 11:02:38 -070088 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
robertphillips952841b2014-06-30 08:26:50 -070089 }
90
robertphillips3d533ac2014-07-20 09:40:00 -070091 // GrCachedLayer proper
robertphillips4815fe52014-09-16 10:32:43 -070092 GrCachedLayer(uint32_t pictureID, int start, int stop,
robertphillipsed420592014-09-29 11:39:38 -070093 const SkMatrix& ctm, const SkPaint* paint)
94 : fKey(pictureID, start, ctm)
95 , fStop(stop)
robertphillipsa0537de2014-09-18 08:01:23 -070096 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL)
robertphillips3d533ac2014-07-20 09:40:00 -070097 , fTexture(NULL)
98 , fRect(GrIRect16::MakeEmpty())
robertphillips320c9232014-07-29 06:07:19 -070099 , fPlot(NULL)
100 , fLocked(false) {
robertphillips0c423322014-07-31 11:02:38 -0700101 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
robertphillips3d533ac2014-07-20 09:40:00 -0700102 }
103
robertphillipsed6f03e2014-07-30 07:31:35 -0700104 ~GrCachedLayer() {
105 SkSafeUnref(fTexture);
robertphillipsa0537de2014-09-18 08:01:23 -0700106 SkDELETE(fPaint);
robertphillipsed6f03e2014-07-30 07:31:35 -0700107 }
108
robertphillips0c423322014-07-31 11:02:38 -0700109 uint32_t pictureID() const { return fKey.pictureID(); }
110 int start() const { return fKey.start(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000111
robertphillipsed420592014-09-29 11:39:38 -0700112 int stop() const { return fStop; }
robertphillips952841b2014-06-30 08:26:50 -0700113 void setTexture(GrTexture* texture, const GrIRect16& rect) {
robertphillipsed6f03e2014-07-30 07:31:35 -0700114 SkRefCnt_SafeAssign(fTexture, texture);
robertphillips952841b2014-06-30 08:26:50 -0700115 fRect = rect;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000116 }
robertphillips952841b2014-06-30 08:26:50 -0700117 GrTexture* texture() { return fTexture; }
robertphillips4aa6dfc2014-09-17 07:50:47 -0700118 const SkPaint* paint() const { return fPaint; }
robertphillips952841b2014-06-30 08:26:50 -0700119 const GrIRect16& rect() const { return fRect; }
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000120
robertphillips261b8a92014-07-17 08:26:44 -0700121 void setPlot(GrPlot* plot) {
robertphillips6f294af2014-08-18 08:50:03 -0700122 SkASSERT(NULL == plot || NULL == fPlot);
robertphillips261b8a92014-07-17 08:26:44 -0700123 fPlot = plot;
124 }
125 GrPlot* plot() { return fPlot; }
robertphillips21048b52014-07-15 19:46:35 -0700126
bsalomon49f085d2014-09-05 13:34:00 -0700127 bool isAtlased() const { return SkToBool(fPlot); }
robertphillips261b8a92014-07-17 08:26:44 -0700128
robertphillips320c9232014-07-29 06:07:19 -0700129 void setLocked(bool locked) { fLocked = locked; }
130 bool locked() const { return fLocked; }
131
132 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
robertphillips261b8a92014-07-17 08:26:44 -0700133 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
robertphillips21048b52014-07-15 19:46:35 -0700134
robertphillips@google.come930a072014-04-03 00:34:27 +0000135private:
robertphillips3d533ac2014-07-20 09:40:00 -0700136 const Key fKey;
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000137
robertphillipsed420592014-09-29 11:39:38 -0700138 // The final "restore" operation index of the cached layer
139 const int fStop;
140
robertphillips4aa6dfc2014-09-17 07:50:47 -0700141 // The paint used when dropping the layer down into the owning canvas.
robertphillipsa0537de2014-09-18 08:01:23 -0700142 // Can be NULL. This class makes a copy for itself.
robertphillips4aa6dfc2014-09-17 07:50:47 -0700143 const SkPaint* fPaint;
144
skia.committer@gmail.comd9427312014-04-12 03:05:59 +0000145 // fTexture is a ref on the atlasing texture for atlased layers and a
robertphillips320c9232014-07-29 06:07:19 -0700146 // ref on a GrTexture for non-atlased textures.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000147 GrTexture* fTexture;
148
robertphillips21048b52014-07-15 19:46:35 -0700149 // For both atlased and non-atlased layers 'fRect' contains the bound of
150 // the layer in whichever texture it resides. It is empty when 'fTexture'
151 // is NULL.
robertphillips952841b2014-06-30 08:26:50 -0700152 GrIRect16 fRect;
robertphillips261b8a92014-07-17 08:26:44 -0700153
154 // For atlased layers, fPlot stores the atlas plot in which the layer rests.
155 // It is always NULL for non-atlased layers.
156 GrPlot* fPlot;
robertphillips320c9232014-07-29 06:07:19 -0700157
bsalomon49f085d2014-09-05 13:34:00 -0700158 // For non-atlased layers 'fLocked' should always match "fTexture".
robertphillips320c9232014-07-29 06:07:19 -0700159 // (i.e., if there is a texture it is locked).
160 // For atlased layers, 'fLocked' is true if the layer is in a plot and
161 // actively required for rendering. If the layer is in a plot but not
162 // actively required for rendering, then 'fLocked' is false. If the
163 // layer isn't in a plot then is can never be locked.
164 bool fLocked;
robertphillips@google.come930a072014-04-03 00:34:27 +0000165};
166
167// The GrLayerCache caches pre-computed saveLayers for later rendering.
commit-bot@chromium.org365cd312014-04-11 15:53:47 +0000168// Non-atlased layers are stored in their own GrTexture while the atlased
169// layers share a single GrTexture.
robertphillips1d86ee82014-06-24 15:08:49 -0700170// Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888)
robertphillips@google.come930a072014-04-03 00:34:27 +0000171// and one GrPlot (for the entire atlas). As such, the GrLayerCache
172// roughly combines the functionality of the GrFontCache and GrTextStrike
173// classes.
174class GrLayerCache {
175public:
robertphillips4ec84da2014-06-24 13:10:43 -0700176 GrLayerCache(GrContext*);
robertphillips@google.come930a072014-04-03 00:34:27 +0000177 ~GrLayerCache();
178
robertphillips4ec84da2014-06-24 13:10:43 -0700179 // As a cache, the GrLayerCache can be ordered to free up all its cached
180 // elements by the GrContext
robertphillips@google.come930a072014-04-03 00:34:27 +0000181 void freeAll();
182
robertphillipsed420592014-09-29 11:39:38 -0700183 GrCachedLayer* findLayer(uint32_t pictureID, int start, const SkMatrix& ctm);
robertphillips6f294af2014-08-18 08:50:03 -0700184 GrCachedLayer* findLayerOrCreate(uint32_t pictureID,
robertphillips0c423322014-07-31 11:02:38 -0700185 int start, int stop,
robertphillips4aa6dfc2014-09-17 07:50:47 -0700186 const SkMatrix& ctm,
187 const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700188
189 // Inform the cache that layer's cached image is now required.
190 // Return true if the layer must be re-rendered. Return false if the
191 // layer was found in the cache and can be reused.
192 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas);
robertphillips4ec84da2014-06-24 13:10:43 -0700193
194 // Inform the cache that layer's cached image is not currently required
195 void unlock(GrCachedLayer* layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000196
robertphillipsd771f6b2014-07-22 10:18:06 -0700197 // Setup to be notified when 'picture' is deleted
198 void trackPicture(const SkPicture* picture);
199
200 // Cleanup after any SkPicture deletions
201 void processDeletedPictures();
robertphillips952841b2014-06-30 08:26:50 -0700202
robertphillips21048b52014-07-15 19:46:35 -0700203 SkDEBUGCODE(void validate() const;)
204
robertphillips@google.come930a072014-04-03 00:34:27 +0000205private:
robertphillips320c9232014-07-29 06:07:19 -0700206 static const int kAtlasTextureWidth = 1024;
207 static const int kAtlasTextureHeight = 1024;
208
robertphillips261b8a92014-07-17 08:26:44 -0700209 static const int kNumPlotsX = 2;
210 static const int kNumPlotsY = 2;
211
robertphillips320c9232014-07-29 06:07:19 -0700212 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
213 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
214
robertphillips4ec84da2014-06-24 13:10:43 -0700215 GrContext* fContext; // pointer back to owning context
robertphillips1d86ee82014-06-24 15:08:49 -0700216 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
robertphillips261b8a92014-07-17 08:26:44 -0700217
218 // We cache this information here (rather then, say, on the owning picture)
219 // because we want to be able to clean it up as needed (e.g., if a picture
220 // is leaked and never cleans itself up we still want to be able to
221 // remove the GrPictureInfo once its layers are purged from all the atlas
222 // plots).
robertphillips3d533ac2014-07-20 09:40:00 -0700223 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000224
robertphillips3d533ac2014-07-20 09:40:00 -0700225 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
robertphillips@google.come930a072014-04-03 00:34:27 +0000226
robertphillipsd771f6b2014-07-22 10:18:06 -0700227 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
228
229 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
230
robertphillips320c9232014-07-29 06:07:19 -0700231 // This implements a plot-centric locking mechanism (since the atlas
232 // backing texture is always locked). Each layer that is locked (i.e.,
233 // needed for the current rendering) in a plot increments the plot lock
234 // count for that plot. Similarly, once a rendering is complete all the
235 // layers used in it decrement the lock count for the used plots.
236 // Plots with a 0 lock count are open for recycling/purging.
237 int fPlotLocks[kNumPlotsX * kNumPlotsY];
238
robertphillips952841b2014-06-30 08:26:50 -0700239 void initAtlas();
robertphillips4815fe52014-09-16 10:32:43 -0700240 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop,
robertphillipsed420592014-09-29 11:39:38 -0700241 const SkMatrix& ctm, const SkPaint* paint);
robertphillips6f294af2014-08-18 08:50:03 -0700242
robertphillips6f294af2014-08-18 08:50:03 -0700243 void purgeAll();
robertphillips952841b2014-06-30 08:26:50 -0700244
robertphillipsd771f6b2014-07-22 10:18:06 -0700245 // Remove all the layers (and unlock any resources) associated with 'pictureID'
246 void purge(uint32_t pictureID);
247
robertphillips320c9232014-07-29 06:07:19 -0700248 static bool PlausiblyAtlasable(int width, int height) {
249 return width <= kPlotWidth && height <= kPlotHeight;
250 }
251
robertphillips6f294af2014-08-18 08:50:03 -0700252 void purgePlot(GrPlot* plot);
253
robertphillips320c9232014-07-29 06:07:19 -0700254 // Try to find a purgeable plot and clear it out. Return true if a plot
255 // was purged; false otherwise.
256 bool purgePlot();
257
robertphillips952841b2014-06-30 08:26:50 -0700258 // for testing
robertphillipsd771f6b2014-07-22 10:18:06 -0700259 friend class TestingAccess;
robertphillips952841b2014-06-30 08:26:50 -0700260 int numLayers() const { return fLayerHash.count(); }
robertphillips@google.come930a072014-04-03 00:34:27 +0000261};
262
263#endif