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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // contains the layer's extent in its texture. | 70 // contains the layer's extent in its texture. |
71 // Atlased layers also get a pointer to the plot in which they reside. | 71 // Atlased layers also get a pointer to the plot in which they reside. |
72 // For non-atlased layers, the lock field just corresponds to locking in | 72 // For non-atlased layers, the lock field just corresponds to locking in |
73 // the resource cache. For atlased layers, it implements an additional level | 73 // the resource cache. For atlased layers, it implements an additional level |
74 // of locking to allow atlased layers to be reused multiple times. | 74 // of locking to allow atlased layers to be reused multiple times. |
75 struct GrCachedLayer { | 75 struct GrCachedLayer { |
76 public: | 76 public: |
77 // For SkTDynamicHash | 77 // For SkTDynamicHash |
78 struct Key { | 78 struct Key { |
79 Key(uint32_t pictureID, const SkMatrix& initialMat, | 79 Key(uint32_t pictureID, const SkMatrix& initialMat, |
80 const int* key, int keySize, bool copyKey = false) | 80 const unsigned* key, int keySize, bool copyKey = false) |
81 : fKeySize(keySize) | 81 : fKeySize(keySize) |
82 , fFreeKey(copyKey) { | 82 , fFreeKey(copyKey) { |
83 fIDMatrix.fPictureID = pictureID; | 83 fIDMatrix.fPictureID = pictureID; |
84 fIDMatrix.fInitialMat = initialMat; | 84 fIDMatrix.fInitialMat = initialMat; |
85 fIDMatrix.fInitialMat.getType(); // force initialization of type so
hashes match | 85 fIDMatrix.fInitialMat.getType(); // force initialization of type so
hashes match |
86 | 86 |
87 if (copyKey) { | 87 if (copyKey) { |
88 int* tempKey = SkNEW_ARRAY(int, keySize); | 88 unsigned* tempKey = SkNEW_ARRAY(unsigned, keySize); |
89 memcpy(tempKey, key, keySize*sizeof(int)); | 89 memcpy(tempKey, key, keySize*sizeof(unsigned)); |
90 fKey = tempKey; | 90 fKey = tempKey; |
91 } else { | 91 } else { |
92 fKey = key; | 92 fKey = key; |
93 } | 93 } |
94 | 94 |
95 // The pictureID/matrix portion needs to be tightly packed. | 95 // The pictureID/matrix portion needs to be tightly packed. |
96 GR_STATIC_ASSERT(sizeof(IDMatrix) == sizeof(uint32_t)+
// pictureID | 96 GR_STATIC_ASSERT(sizeof(IDMatrix) == sizeof(uint32_t)+
// pictureID |
97 9 * sizeof(SkScalar) + sizeof(uint3
2_t)); // matrix | 97 9 * sizeof(SkScalar) + sizeof(uint3
2_t)); // matrix |
98 } | 98 } |
99 | 99 |
100 ~Key() { | 100 ~Key() { |
101 if (fFreeKey) { | 101 if (fFreeKey) { |
102 SkDELETE_ARRAY(fKey); | 102 SkDELETE_ARRAY(fKey); |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 bool operator==(const Key& other) const { | 106 bool operator==(const Key& other) const { |
107 if (fKeySize != other.fKeySize) { | 107 if (fKeySize != other.fKeySize) { |
108 return false; | 108 return false; |
109 } | 109 } |
110 return fIDMatrix.fPictureID == other.fIDMatrix.fPictureID && | 110 return fIDMatrix.fPictureID == other.fIDMatrix.fPictureID && |
111 fIDMatrix.fInitialMat.cheapEqualTo(other.fIDMatrix.fInitialMa
t) && | 111 fIDMatrix.fInitialMat.cheapEqualTo(other.fIDMatrix.fInitialMa
t) && |
112 !memcmp(fKey, other.fKey, fKeySize * sizeof(int)); | 112 !memcmp(fKey, other.fKey, fKeySize * sizeof(int)); |
113 } | 113 } |
114 | 114 |
115 uint32_t pictureID() const { return fIDMatrix.fPictureID; } | 115 uint32_t pictureID() const { return fIDMatrix.fPictureID; } |
116 | 116 |
117 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse | 117 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse |
118 const int* key() const { SkASSERT(fFreeKey); return fKey; } | 118 const unsigned* key() const { SkASSERT(fFreeKey); return fKey; } |
119 int keySize() const { SkASSERT(fFreeKey); return fKeySize; } | 119 int keySize() const { SkASSERT(fFreeKey); return fKeySize; } |
120 | 120 |
121 uint32_t hash() const { | 121 uint32_t hash() const { |
122 uint32_t hash = SkChecksum::Murmur3(reinterpret_cast<const uint32_t*
>(fKey), | 122 uint32_t hash = SkChecksum::Murmur3(reinterpret_cast<const uint32_t*
>(fKey), |
123 fKeySize * sizeof(int)); | 123 fKeySize * sizeof(int)); |
124 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&fIDMat
rix), | 124 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&fIDMat
rix), |
125 sizeof(IDMatrix), hash); | 125 sizeof(IDMatrix), hash); |
126 } | 126 } |
127 | 127 |
128 private: | 128 private: |
129 struct IDMatrix { | 129 struct IDMatrix { |
130 // ID of the picture of which this layer is a part | 130 // ID of the picture of which this layer is a part |
131 uint32_t fPictureID; | 131 uint32_t fPictureID; |
132 // The initial matrix passed into drawPicture | 132 // The initial matrix passed into drawPicture |
133 SkMatrix fInitialMat; | 133 SkMatrix fInitialMat; |
134 } fIDMatrix; | 134 } fIDMatrix; |
135 | 135 |
136 const int* fKey; | 136 const unsigned* fKey; |
137 const int fKeySize; | 137 const int fKeySize; |
138 bool fFreeKey; | 138 bool fFreeKey; |
139 }; | 139 }; |
140 | 140 |
141 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } | 141 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
142 static uint32_t Hash(const Key& key) { return key.hash(); } | 142 static uint32_t Hash(const Key& key) { return key.hash(); } |
143 | 143 |
144 // GrCachedLayer proper | 144 // GrCachedLayer proper |
145 GrCachedLayer(uint32_t pictureID, int start, int stop, | 145 GrCachedLayer(uint32_t pictureID, unsigned start, unsigned stop, |
146 const SkIRect& bounds, const SkMatrix& ctm, | 146 const SkIRect& bounds, const SkMatrix& ctm, |
147 const int* key, int keySize, | 147 const unsigned* key, int keySize, |
148 const SkPaint* paint) | 148 const SkPaint* paint) |
149 : fKey(pictureID, ctm, key, keySize, true) | 149 : fKey(pictureID, ctm, key, keySize, true) |
150 , fStart(start) | 150 , fStart(start) |
151 , fStop(stop) | 151 , fStop(stop) |
152 , fBounds(bounds) | 152 , fBounds(bounds) |
153 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) | 153 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) |
154 , fTexture(NULL) | 154 , fTexture(NULL) |
155 , fRect(GrIRect16::MakeEmpty()) | 155 , fRect(SkIRect::MakeEmpty()) |
156 , fPlot(NULL) | 156 , fPlot(NULL) |
157 , fUses(0) | 157 , fUses(0) |
158 , fLocked(false) { | 158 , fLocked(false) { |
159 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0); | 159 SkASSERT(SK_InvalidGenID != pictureID); |
160 } | 160 } |
161 | 161 |
162 ~GrCachedLayer() { | 162 ~GrCachedLayer() { |
163 SkSafeUnref(fTexture); | 163 SkSafeUnref(fTexture); |
164 SkDELETE(fPaint); | 164 SkDELETE(fPaint); |
165 } | 165 } |
166 | 166 |
167 uint32_t pictureID() const { return fKey.pictureID(); } | 167 uint32_t pictureID() const { return fKey.pictureID(); } |
168 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse | 168 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse |
169 const int* key() const { return fKey.key(); } | 169 const unsigned* key() const { return fKey.key(); } |
170 int keySize() const { return fKey.keySize(); } | 170 int keySize() const { return fKey.keySize(); } |
171 | 171 |
172 int start() const { return fStart; } | 172 unsigned start() const { return fStart; } |
173 // TODO: make bound debug only | 173 // TODO: make bound debug only |
174 const SkIRect& bound() const { return fBounds; } | 174 const SkIRect& bound() const { return fBounds; } |
175 int stop() const { return fStop; } | 175 unsigned stop() const { return fStop; } |
176 void setTexture(GrTexture* texture, const GrIRect16& rect) { | 176 void setTexture(GrTexture* texture, const SkIRect& rect) { |
177 SkRefCnt_SafeAssign(fTexture, texture); | 177 SkRefCnt_SafeAssign(fTexture, texture); |
178 fRect = rect; | 178 fRect = rect; |
179 } | 179 } |
180 GrTexture* texture() { return fTexture; } | 180 GrTexture* texture() { return fTexture; } |
181 const SkPaint* paint() const { return fPaint; } | 181 const SkPaint* paint() const { return fPaint; } |
182 const GrIRect16& rect() const { return fRect; } | 182 const SkIRect& rect() const { return fRect; } |
183 | 183 |
184 void setPlot(GrPlot* plot) { | 184 void setPlot(GrPlot* plot) { |
185 SkASSERT(NULL == plot || NULL == fPlot); | 185 SkASSERT(NULL == plot || NULL == fPlot); |
186 fPlot = plot; | 186 fPlot = plot; |
187 } | 187 } |
188 GrPlot* plot() { return fPlot; } | 188 GrPlot* plot() { return fPlot; } |
189 | 189 |
190 bool isAtlased() const { return SkToBool(fPlot); } | 190 bool isAtlased() const { return SkToBool(fPlot); } |
191 | 191 |
192 void setLocked(bool locked) { fLocked = locked; } | 192 void setLocked(bool locked) { fLocked = locked; } |
193 bool locked() const { return fLocked; } | 193 bool locked() const { return fLocked; } |
194 | 194 |
195 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) | 195 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) |
196 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) | 196 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
197 | 197 |
198 private: | 198 private: |
199 const Key fKey; | 199 const Key fKey; |
200 | 200 |
201 // The "saveLayer" operation index of the cached layer | 201 // The "saveLayer" operation index of the cached layer |
202 const int fStart; | 202 const unsigned fStart; |
203 // The final "restore" operation index of the cached layer | 203 // The final "restore" operation index of the cached layer |
204 const int fStop; | 204 const unsigned fStop; |
205 | 205 |
206 const SkIRect fBounds; | 206 const SkIRect fBounds; |
207 | 207 |
208 // The paint used when dropping the layer down into the owning canvas. | 208 // The paint used when dropping the layer down into the owning canvas. |
209 // Can be NULL. This class makes a copy for itself. | 209 // Can be NULL. This class makes a copy for itself. |
210 const SkPaint* fPaint; | 210 const SkPaint* fPaint; |
211 | 211 |
212 // fTexture is a ref on the atlasing texture for atlased layers and a | 212 // fTexture is a ref on the atlasing texture for atlased layers and a |
213 // ref on a GrTexture for non-atlased textures. | 213 // ref on a GrTexture for non-atlased textures. |
214 GrTexture* fTexture; | 214 GrTexture* fTexture; |
215 | 215 |
216 // For both atlased and non-atlased layers 'fRect' contains the bound of | 216 // For both atlased and non-atlased layers 'fRect' contains the bound of |
217 // the layer in whichever texture it resides. It is empty when 'fTexture' | 217 // the layer in whichever texture it resides. It is empty when 'fTexture' |
218 // is NULL. | 218 // is NULL. |
219 GrIRect16 fRect; | 219 SkIRect fRect; |
220 | 220 |
221 // For atlased layers, fPlot stores the atlas plot in which the layer rests. | 221 // For atlased layers, fPlot stores the atlas plot in which the layer rests. |
222 // It is always NULL for non-atlased layers. | 222 // It is always NULL for non-atlased layers. |
223 GrPlot* fPlot; | 223 GrPlot* fPlot; |
224 | 224 |
225 // The number of actively hoisted layers using this cached image (e.g., | 225 // The number of actively hoisted layers using this cached image (e.g., |
226 // extant GrHoistedLayers pointing at this object). This object will | 226 // extant GrHoistedLayers pointing at this object). This object will |
227 // be unlocked when the use count reaches 0. | 227 // be unlocked when the use count reaches 0. |
228 int fUses; | 228 int fUses; |
229 | 229 |
(...skipping 22 matching lines...) Expand all Loading... |
252 // classes. | 252 // classes. |
253 class GrLayerCache { | 253 class GrLayerCache { |
254 public: | 254 public: |
255 GrLayerCache(GrContext*); | 255 GrLayerCache(GrContext*); |
256 ~GrLayerCache(); | 256 ~GrLayerCache(); |
257 | 257 |
258 // As a cache, the GrLayerCache can be ordered to free up all its cached | 258 // As a cache, the GrLayerCache can be ordered to free up all its cached |
259 // elements by the GrContext | 259 // elements by the GrContext |
260 void freeAll(); | 260 void freeAll(); |
261 | 261 |
| 262 GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm, |
| 263 const unsigned* key, int keySize); |
262 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, | 264 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, |
263 int start, int stop, | 265 int start, int stop, |
264 const SkIRect& bounds, | 266 const SkIRect& bounds, |
265 const SkMatrix& initialMat, | 267 const SkMatrix& initialMat, |
266 const int* key, int keySize, | 268 const unsigned* key, int keySize, |
267 const SkPaint* paint); | 269 const SkPaint* paint); |
268 | 270 |
269 // Attempt to place 'layer' in the atlas. Return true on success; false on f
ailure. | 271 // Attempt to place 'layer' in the atlas. Return true on success; false on f
ailure. |
270 // When true is returned, 'needsRendering' will indicate if the layer must b
e (re)drawn. | 272 // When true is returned, 'needsRendering' will indicate if the layer must b
e (re)drawn. |
271 // Additionally, the GPU resources will be locked. | 273 // Additionally, the GPU resources will be locked. |
272 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needs
Rendering); | 274 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needs
Rendering); |
273 | 275 |
274 // Attempt to lock the GPU resources required for a layer. Return true on su
ccess; | 276 // Attempt to lock the GPU resources required for a layer. Return true on su
ccess; |
275 // false on failure. When true is returned 'needsRendering' will indicate if
the | 277 // false on failure. When true is returned 'needsRendering' will indicate if
the |
276 // layer must be (re)drawn. | 278 // layer must be (re)drawn. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 // layers used in it decrement the lock count for the used plots. | 341 // layers used in it decrement the lock count for the used plots. |
340 // Plots with a 0 lock count are open for recycling/purging. | 342 // Plots with a 0 lock count are open for recycling/purging. |
341 int fPlotLocks[kNumPlotsX * kNumPlotsY]; | 343 int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
342 | 344 |
343 // Inform the cache that layer's cached image is not currently required | 345 // Inform the cache that layer's cached image is not currently required |
344 void unlock(GrCachedLayer* layer); | 346 void unlock(GrCachedLayer* layer); |
345 | 347 |
346 void initAtlas(); | 348 void initAtlas(); |
347 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, | 349 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, |
348 const SkIRect& bounds, const SkMatrix& initialMat
, | 350 const SkIRect& bounds, const SkMatrix& initialMat
, |
349 const int* key, int keySize, | 351 const unsigned* key, int keySize, |
350 const SkPaint* paint); | 352 const SkPaint* paint); |
351 | 353 |
352 // Remove all the layers (and unlock any resources) associated with 'picture
ID' | 354 // Remove all the layers (and unlock any resources) associated with 'picture
ID' |
353 void purge(uint32_t pictureID); | 355 void purge(uint32_t pictureID); |
354 | 356 |
355 void purgePlot(GrPlot* plot); | 357 void purgePlot(GrPlot* plot); |
356 | 358 |
357 // Try to find a purgeable plot and clear it out. Return true if a plot | 359 // Try to find a purgeable plot and clear it out. Return true if a plot |
358 // was purged; false otherwise. | 360 // was purged; false otherwise. |
359 bool purgePlot(); | 361 bool purgePlot(); |
360 | 362 |
361 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } | 363 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } |
362 void decPlotLock(int plotIdx) { | 364 void decPlotLock(int plotIdx) { |
363 SkASSERT(fPlotLocks[plotIdx] > 0); | 365 SkASSERT(fPlotLocks[plotIdx] > 0); |
364 --fPlotLocks[plotIdx]; | 366 --fPlotLocks[plotIdx]; |
365 } | 367 } |
366 | 368 |
367 // for testing | 369 // for testing |
368 friend class TestingAccess; | 370 friend class TestingAccess; |
369 int numLayers() const { return fLayerHash.count(); } | 371 int numLayers() const { return fLayerHash.count(); } |
370 GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm, | |
371 const int* key, int keySize); | |
372 }; | 372 }; |
373 | 373 |
374 #endif | 374 #endif |
OLD | NEW |