Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrLayerCache.h
1 /*
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
11 #include "GrAtlas.h"
12 #include "GrPictureUtils.h"
13 #include "GrRect.h"
14 #include "SkChecksum.h"
15 #include "SkTDynamicHash.h"
16 #include "SkMessageBus.h"
17
18 class SkPicture;
19
20 // The layer cache listens for these messages to purge picture-related resources.
21 struct GrPictureDeletedMessage {
22     uint32_t pictureID;
23 };
24
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 {
28 public:
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); }
32
33     // GrPictureInfo proper
34     GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { }
35
36     const uint32_t fPictureID;
37
38     GrAtlas::ClientPlotUsage  fPlotUsage;
39 };
40
41 // GrCachedLayer encapsulates the caching information for a single saveLayer.
42 //
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 {
51 public:
52     // For SkTDynamicHash
53     struct Key {
54         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
60
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));
64         }
65
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);
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; }
77
78     private:
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
82         const int      fStart;
83         const int      fStop;
84         // The CTM applied to this layer in the picture
85         SkMatrix       fCTM;
86     };
87
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));
91     }
92
93     // GrCachedLayer proper
94     GrCachedLayer(uint32_t pictureID, int start, int stop, const SkMatrix& ctm) 
95         : fKey(pictureID, start, stop, ctm)
96         , fTexture(NULL)
97         , fRect(GrIRect16::MakeEmpty())
98         , fPlot(NULL)
99         , fLocked(false) {
100         SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0);
101     }
102
103     ~GrCachedLayer() {
104         SkSafeUnref(fTexture);
105     }
106
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(); }
111
112     void setTexture(GrTexture* texture, const GrIRect16& rect) {
113         SkRefCnt_SafeAssign(fTexture, texture);
114         fRect = rect;
115     }
116     GrTexture* texture() { return fTexture; }
117     const GrIRect16& rect() const { return fRect; }
118
119     void setPlot(GrPlot* plot) {
120         SkASSERT(NULL == fPlot);
121         fPlot = plot;
122     }
123     GrPlot* plot() { return fPlot; }
124
125     bool isAtlased() const { return NULL != fPlot; }
126
127     void setLocked(bool locked) { fLocked = locked; }
128     bool locked() const { return fLocked; }
129
130     SkDEBUGCODE(const GrPlot* plot() const { return fPlot; })
131     SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
132
133 private:
134     const Key       fKey;
135
136     // fTexture is a ref on the atlasing texture for atlased layers and a
137     // ref on a GrTexture for non-atlased textures.
138     GrTexture*      fTexture;
139
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'
142     // is NULL.
143     GrIRect16       fRect;
144
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;
148
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;
156 };
157
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
164 // classes.
165 class GrLayerCache {
166 public:
167     GrLayerCache(GrContext*);
168     ~GrLayerCache();
169
170     // As a cache, the GrLayerCache can be ordered to free up all its cached
171     // elements by the GrContext
172     void freeAll();
173
174     GrCachedLayer* findLayer(const SkPicture* picture, int start, int stop, const SkMatrix& ctm);
175     GrCachedLayer* findLayerOrCreate(const SkPicture* picture, 
176                                      int start, int stop, 
177                                      const SkMatrix& ctm);
178     
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);
184
185     // Inform the cache that layer's cached image is not currently required
186     void unlock(GrCachedLayer* layer);
187
188     // Setup to be notified when 'picture' is deleted
189     void trackPicture(const SkPicture* picture);
190
191     // Cleanup after any SkPicture deletions
192     void processDeletedPictures();
193
194     SkDEBUGCODE(void validate() const;)
195
196 private:
197     static const int kAtlasTextureWidth = 1024;
198     static const int kAtlasTextureHeight = 1024;
199
200     static const int kNumPlotsX = 2;
201     static const int kNumPlotsY = 2;
202
203     static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX;
204     static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY;
205
206     GrContext*                fContext;  // pointer back to owning context
207     SkAutoTDelete<GrAtlas>    fAtlas;    // TODO: could lazily allocate
208
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
213     // plots).
214     SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
215
216     SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
217
218     SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox;
219
220     SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener;
221
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];
229
230     void initAtlas();
231     GrCachedLayer* createLayer(const SkPicture* picture, int start, int stop, const SkMatrix& ctm);
232
233     // Remove all the layers (and unlock any resources) associated with 'pictureID'
234     void purge(uint32_t pictureID);
235
236     static bool PlausiblyAtlasable(int width, int height) {
237         return width <= kPlotWidth && height <= kPlotHeight;
238     }
239
240     // Try to find a purgeable plot and clear it out. Return true if a plot
241     // was purged; false otherwise.
242     bool purgePlot();
243
244     // for testing
245     friend class TestingAccess;
246     int numLayers() const { return fLayerHash.count(); }
247 };
248
249 #endif