OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrLayerCache_DEFINED | 8 #ifndef GrLayerCache_DEFINED |
9 #define GrLayerCache_DEFINED | 9 #define GrLayerCache_DEFINED |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 GrAtlas::ClientPlotUsage fPlotUsage; | 38 GrAtlas::ClientPlotUsage fPlotUsage; |
39 }; | 39 }; |
40 | 40 |
41 // GrCachedLayer encapsulates the caching information for a single saveLayer. | 41 // GrCachedLayer encapsulates the caching information for a single saveLayer. |
42 // | 42 // |
43 // Atlased layers get a ref to the backing GrTexture while non-atlased layers | 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' | 44 // get a ref to the GrTexture in which they reside. In both cases 'fRect' |
45 // contains the layer's extent in its texture. | 45 // contains the layer's extent in its texture. |
46 // Atlased layers also get a pointer to the plot in which they reside. | 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 | 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 | 48 // the resource cache. For atlased layers, it implements an additional level |
49 // of locking to allow atlased layers to be reused multiple times. | 49 // of locking to allow atlased layers to be reused multiple times. |
50 struct GrCachedLayer { | 50 struct GrCachedLayer { |
51 public: | 51 public: |
52 // For SkTDynamicHash | 52 // For SkTDynamicHash |
53 struct Key { | 53 struct Key { |
54 Key(uint32_t pictureID, int layerID) : fPictureID(pictureID) , fLayerID(
layerID) {} | 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 } |
55 | 65 |
56 bool operator==(const Key& other) const { | 66 bool operator==(const Key& other) const { |
57 return fPictureID == other.fPictureID && fLayerID == other.fLayerID; | 67 return fPictureID == other.fPictureID && |
| 68 fStart == other.fStart && |
| 69 fStop == other.fStop && |
| 70 fCTM.cheapEqualTo(other.fCTM); |
58 } | 71 } |
59 | 72 |
60 uint32_t getPictureID() const { return fPictureID; } | 73 uint32_t pictureID() const { return fPictureID; } |
61 int getLayerID() const { return fLayerID; } | 74 int start() const { return fStart; } |
| 75 int stop() const { return fStop; } |
| 76 const SkMatrix& ctm() const { return fCTM; } |
62 | 77 |
63 private: | 78 private: |
64 // ID of the picture of which this layer is a part | 79 // ID of the picture of which this layer is a part |
65 const uint32_t fPictureID; | 80 const uint32_t fPictureID; |
66 // fLayerID is the index of this layer in the picture (one of 0 .. #laye
rs). | 81 // The range of commands in the picture this layer represents |
67 const int fLayerID; | 82 const int fStart; |
| 83 const int fStop; |
| 84 // The CTM applied to this layer in the picture |
| 85 SkMatrix fCTM; |
68 }; | 86 }; |
69 | 87 |
70 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } | 88 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
71 static uint32_t Hash(const Key& key) { | 89 static uint32_t Hash(const Key& key) { |
72 return SkChecksum::Mix((key.getPictureID() << 16) | key.getLayerID()); | 90 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), size
of(Key)); |
73 } | 91 } |
74 | 92 |
75 // GrCachedLayer proper | 93 // GrCachedLayer proper |
76 GrCachedLayer(uint32_t pictureID, int layerID) | 94 GrCachedLayer(uint32_t pictureID, int start, int stop, const SkMatrix& ctm) |
77 : fKey(pictureID, layerID) | 95 : fKey(pictureID, start, stop, ctm) |
78 , fTexture(NULL) | 96 , fTexture(NULL) |
79 , fRect(GrIRect16::MakeEmpty()) | 97 , fRect(GrIRect16::MakeEmpty()) |
80 , fPlot(NULL) | 98 , fPlot(NULL) |
81 , fLocked(false) { | 99 , fLocked(false) { |
82 SkASSERT(SK_InvalidGenID != pictureID && layerID >= 0); | 100 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0); |
83 } | 101 } |
84 | 102 |
85 ~GrCachedLayer() { | 103 ~GrCachedLayer() { |
86 SkSafeUnref(fTexture); | 104 SkSafeUnref(fTexture); |
87 } | 105 } |
88 | 106 |
89 uint32_t pictureID() const { return fKey.getPictureID(); } | 107 uint32_t pictureID() const { return fKey.pictureID(); } |
90 int layerID() const { return fKey.getLayerID(); } | 108 int start() const { return fKey.start(); } |
| 109 int stop() const { return fKey.stop(); } |
| 110 const SkMatrix& ctm() const { return fKey.ctm(); } |
91 | 111 |
92 void setTexture(GrTexture* texture, const GrIRect16& rect) { | 112 void setTexture(GrTexture* texture, const GrIRect16& rect) { |
93 SkRefCnt_SafeAssign(fTexture, texture); | 113 SkRefCnt_SafeAssign(fTexture, texture); |
94 fRect = rect; | 114 fRect = rect; |
95 } | 115 } |
96 GrTexture* texture() { return fTexture; } | 116 GrTexture* texture() { return fTexture; } |
97 const GrIRect16& rect() const { return fRect; } | 117 const GrIRect16& rect() const { return fRect; } |
98 | 118 |
99 void setPlot(GrPlot* plot) { | 119 void setPlot(GrPlot* plot) { |
100 SkASSERT(NULL == fPlot); | 120 SkASSERT(NULL == fPlot); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // classes. | 164 // classes. |
145 class GrLayerCache { | 165 class GrLayerCache { |
146 public: | 166 public: |
147 GrLayerCache(GrContext*); | 167 GrLayerCache(GrContext*); |
148 ~GrLayerCache(); | 168 ~GrLayerCache(); |
149 | 169 |
150 // As a cache, the GrLayerCache can be ordered to free up all its cached | 170 // As a cache, the GrLayerCache can be ordered to free up all its cached |
151 // elements by the GrContext | 171 // elements by the GrContext |
152 void freeAll(); | 172 void freeAll(); |
153 | 173 |
154 GrCachedLayer* findLayer(const SkPicture* picture, int layerID); | 174 GrCachedLayer* findLayer(const SkPicture* picture, int start, int stop, cons
t SkMatrix& ctm); |
155 GrCachedLayer* findLayerOrCreate(const SkPicture* picture, int layerID); | 175 GrCachedLayer* findLayerOrCreate(const SkPicture* picture, |
| 176 int start, int stop, |
| 177 const SkMatrix& ctm); |
156 | 178 |
157 // Inform the cache that layer's cached image is now required. Return true | 179 // Inform the cache that layer's cached image is now required. Return true |
158 // if it was found in the ResourceCache and doesn't need to be regenerated. | 180 // if it was found in the ResourceCache and doesn't need to be regenerated. |
159 // If false is returned the caller should (re)render the layer into the | 181 // If false is returned the caller should (re)render the layer into the |
160 // newly acquired texture. | 182 // newly acquired texture. |
161 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc); | 183 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc); |
162 | 184 |
163 // Inform the cache that layer's cached image is not currently required | 185 // Inform the cache that layer's cached image is not currently required |
164 void unlock(GrCachedLayer* layer); | 186 void unlock(GrCachedLayer* layer); |
165 | 187 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 221 |
200 // This implements a plot-centric locking mechanism (since the atlas | 222 // This implements a plot-centric locking mechanism (since the atlas |
201 // backing texture is always locked). Each layer that is locked (i.e., | 223 // backing texture is always locked). Each layer that is locked (i.e., |
202 // needed for the current rendering) in a plot increments the plot lock | 224 // needed for the current rendering) in a plot increments the plot lock |
203 // count for that plot. Similarly, once a rendering is complete all the | 225 // count for that plot. Similarly, once a rendering is complete all the |
204 // layers used in it decrement the lock count for the used plots. | 226 // layers used in it decrement the lock count for the used plots. |
205 // Plots with a 0 lock count are open for recycling/purging. | 227 // Plots with a 0 lock count are open for recycling/purging. |
206 int fPlotLocks[kNumPlotsX * kNumPlotsY]; | 228 int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
207 | 229 |
208 void initAtlas(); | 230 void initAtlas(); |
209 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); | 231 GrCachedLayer* createLayer(const SkPicture* picture, int start, int stop, co
nst SkMatrix& ctm); |
210 | 232 |
211 // Remove all the layers (and unlock any resources) associated with 'picture
ID' | 233 // Remove all the layers (and unlock any resources) associated with 'picture
ID' |
212 void purge(uint32_t pictureID); | 234 void purge(uint32_t pictureID); |
213 | 235 |
214 static bool PlausiblyAtlasable(int width, int height) { | 236 static bool PlausiblyAtlasable(int width, int height) { |
215 return width <= kPlotWidth && height <= kPlotHeight; | 237 return width <= kPlotWidth && height <= kPlotHeight; |
216 } | 238 } |
217 | 239 |
218 // Try to find a purgeable plot and clear it out. Return true if a plot | 240 // Try to find a purgeable plot and clear it out. Return true if a plot |
219 // was purged; false otherwise. | 241 // was purged; false otherwise. |
220 bool purgePlot(); | 242 bool purgePlot(); |
221 | 243 |
222 // for testing | 244 // for testing |
223 friend class TestingAccess; | 245 friend class TestingAccess; |
224 int numLayers() const { return fLayerHash.count(); } | 246 int numLayers() const { return fLayerHash.count(); } |
225 }; | 247 }; |
226 | 248 |
227 #endif | 249 #endif |
OLD | NEW |