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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 const SkPaint* paint) | 158 const SkPaint* paint) |
159 : fKey(pictureID, ctm, key, keySize, true) | 159 : fKey(pictureID, ctm, key, keySize, true) |
160 , fStart(start) | 160 , fStart(start) |
161 , fStop(stop) | 161 , fStop(stop) |
162 , fSrcIR(srcIR) | 162 , fSrcIR(srcIR) |
163 , fDstIR(dstIR) | 163 , fDstIR(dstIR) |
164 , fOffset(SkIPoint::Make(0, 0)) | 164 , fOffset(SkIPoint::Make(0, 0)) |
165 , fPaint(paint ? new SkPaint(*paint) : nullptr) | 165 , fPaint(paint ? new SkPaint(*paint) : nullptr) |
166 , fFilter(nullptr) | 166 , fFilter(nullptr) |
167 , fTexture(nullptr) | 167 , fTexture(nullptr) |
| 168 , fAtlased(false) |
168 , fRect(SkIRect::MakeEmpty()) | 169 , fRect(SkIRect::MakeEmpty()) |
169 , fPlot(nullptr) | 170 , fPlot(nullptr) |
170 , fUses(0) | 171 , fUses(0) |
171 , fLocked(false) { | 172 , fLocked(false) { |
172 SkASSERT(SK_InvalidGenID != pictureID); | 173 SkASSERT(SK_InvalidGenID != pictureID); |
173 | 174 |
174 if (fPaint) { | 175 if (fPaint) { |
175 if (fPaint->getImageFilter()) { | 176 if (fPaint->getImageFilter()) { |
176 fFilter = SkSafeRef(fPaint->getImageFilter()); | 177 fFilter = SkSafeRef(fPaint->getImageFilter()); |
177 fPaint->setImageFilter(nullptr); | 178 fPaint->setImageFilter(nullptr); |
178 } | 179 } |
179 } | 180 } |
180 } | 181 } |
181 | 182 |
182 ~GrCachedLayer() { | 183 ~GrCachedLayer() { |
183 SkSafeUnref(fTexture); | 184 if (!fAtlased) { |
| 185 SkSafeUnref(fTexture); |
| 186 } |
184 SkSafeUnref(fFilter); | 187 SkSafeUnref(fFilter); |
185 delete fPaint; | 188 delete fPaint; |
186 } | 189 } |
187 | 190 |
188 uint32_t pictureID() const { return fKey.pictureID(); } | 191 uint32_t pictureID() const { return fKey.pictureID(); } |
189 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse | 192 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse |
190 const int* key() const { return fKey.key(); } | 193 const int* key() const { return fKey.key(); } |
191 int keySize() const { return fKey.keySize(); } | 194 int keySize() const { return fKey.keySize(); } |
192 | 195 |
193 int start() const { return fStart; } | 196 int start() const { return fStart; } |
194 // TODO: make bound debug only | 197 // TODO: make bound debug only |
195 const SkIRect& srcIR() const { return fSrcIR; } | 198 const SkIRect& srcIR() const { return fSrcIR; } |
196 const SkIRect& dstIR() const { return fDstIR; } | 199 const SkIRect& dstIR() const { return fDstIR; } |
197 int stop() const { return fStop; } | 200 int stop() const { return fStop; } |
198 void setTexture(GrTexture* texture, const SkIRect& rect) { | 201 void setTexture(GrTexture* texture, const SkIRect& rect, bool atlased) { |
199 SkRefCnt_SafeAssign(fTexture, texture); | 202 if (texture && !atlased) { |
| 203 texture->ref(); // non-atlased textures carry a ref |
| 204 } |
| 205 if (fTexture && !fAtlased) { |
| 206 fTexture->unref(); // non-atlased textures carry a ref |
| 207 } |
| 208 fTexture = texture; |
| 209 fAtlased = atlased; |
200 fRect = rect; | 210 fRect = rect; |
201 if (!fTexture) { | 211 if (!fTexture) { |
202 fLocked = false; | 212 fLocked = false; |
203 } | 213 } |
204 } | 214 } |
205 GrTexture* texture() { return fTexture; } | 215 GrTexture* texture() { return fTexture; } |
206 const SkPaint* paint() const { return fPaint; } | 216 const SkPaint* paint() const { return fPaint; } |
207 const SkImageFilter* filter() const { return fFilter; } | 217 const SkImageFilter* filter() const { return fFilter; } |
208 const SkIRect& rect() const { return fRect; } | 218 const SkIRect& rect() const { return fRect; } |
209 | 219 |
210 void setOffset(const SkIPoint& offset) { fOffset = offset; } | 220 void setOffset(const SkIPoint& offset) { fOffset = offset; } |
211 const SkIPoint& offset() const { return fOffset; } | 221 const SkIPoint& offset() const { return fOffset; } |
212 | 222 |
213 void setPlot(GrLayerAtlas::Plot* plot) { | 223 void setPlot(GrLayerAtlas::Plot* plot) { |
214 SkASSERT(nullptr == plot || nullptr == fPlot); | 224 SkASSERT(nullptr == plot || nullptr == fPlot); |
215 fPlot = plot; | 225 fPlot = plot; |
216 } | 226 } |
217 GrLayerAtlas::Plot* plot() { return fPlot; } | 227 GrLayerAtlas::Plot* plot() { return fPlot; } |
218 | 228 |
219 bool isAtlased() const { return SkToBool(fPlot); } | 229 bool isAtlased() const { SkASSERT(fAtlased == SkToBool(fPlot)); return fAtla
sed; } |
220 | 230 |
221 void setLocked(bool locked) { fLocked = locked; } | 231 void setLocked(bool locked) { fLocked = locked; } |
222 bool locked() const { return fLocked; } | 232 bool locked() const { return fLocked; } |
223 | 233 |
224 SkDEBUGCODE(const GrLayerAtlas::Plot* plot() const { return fPlot; }) | 234 SkDEBUGCODE(const GrLayerAtlas::Plot* plot() const { return fPlot; }) |
225 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) | 235 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
226 | 236 |
227 private: | 237 private: |
228 const Key fKey; | 238 const Key fKey; |
229 | 239 |
(...skipping 15 matching lines...) Expand all Loading... |
245 SkPaint* fPaint; | 255 SkPaint* fPaint; |
246 | 256 |
247 // The imagefilter that needs to be applied to the layer prior to it being | 257 // The imagefilter that needs to be applied to the layer prior to it being |
248 // composited with the rest of the scene. | 258 // composited with the rest of the scene. |
249 const SkImageFilter* fFilter; | 259 const SkImageFilter* fFilter; |
250 | 260 |
251 // fTexture is a ref on the atlasing texture for atlased layers and a | 261 // fTexture is a ref on the atlasing texture for atlased layers and a |
252 // ref on a GrTexture for non-atlased textures. | 262 // ref on a GrTexture for non-atlased textures. |
253 GrTexture* fTexture; | 263 GrTexture* fTexture; |
254 | 264 |
| 265 // true if this layer is in the atlas (and 'fTexture' doesn't carry a ref) |
| 266 // and false if the layer is a free floater (and carries a ref). |
| 267 bool fAtlased; |
| 268 |
255 // For both atlased and non-atlased layers 'fRect' contains the bound of | 269 // For both atlased and non-atlased layers 'fRect' contains the bound of |
256 // the layer in whichever texture it resides. It is empty when 'fTexture' | 270 // the layer in whichever texture it resides. It is empty when 'fTexture' |
257 // is nullptr. | 271 // is nullptr. |
258 SkIRect fRect; | 272 SkIRect fRect; |
259 | 273 |
260 // For atlased layers, fPlot stores the atlas plot in which the layer rests. | 274 // For atlased layers, fPlot stores the atlas plot in which the layer rests. |
261 // It is always nullptr for non-atlased layers. | 275 // It is always nullptr for non-atlased layers. |
262 GrLayerAtlas::Plot* fPlot; | 276 GrLayerAtlas::Plot* fPlot; |
263 | 277 |
264 // The number of actively hoisted layers using this cached image (e.g., | 278 // The number of actively hoisted layers using this cached image (e.g., |
(...skipping 13 matching lines...) Expand all Loading... |
278 void removeUse() { SkASSERT(fUses > 0); --fUses; } | 292 void removeUse() { SkASSERT(fUses > 0); --fUses; } |
279 int uses() const { return fUses; } | 293 int uses() const { return fUses; } |
280 | 294 |
281 friend class GrLayerCache; // for access to usage methods | 295 friend class GrLayerCache; // for access to usage methods |
282 friend class TestingAccess; // for testing | 296 friend class TestingAccess; // for testing |
283 }; | 297 }; |
284 | 298 |
285 // The GrLayerCache caches pre-computed saveLayers for later rendering. | 299 // The GrLayerCache caches pre-computed saveLayers for later rendering. |
286 // Non-atlased layers are stored in their own GrTexture while the atlased | 300 // Non-atlased layers are stored in their own GrTexture while the atlased |
287 // layers share a single GrTexture. | 301 // layers share a single GrTexture. |
288 // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888) | 302 // Unlike the GrFontCache, the GrLayerCache only has one atlas (for 8888). |
289 // and one GrPlot (for the entire atlas). As such, the GrLayerCache | 303 // As such, the GrLayerCache roughly combines the functionality of the |
290 // roughly combines the functionality of the GrFontCache and GrTextStrike | 304 // GrFontCache and GrTextStrike classes. |
291 // classes. | |
292 class GrLayerCache { | 305 class GrLayerCache { |
293 public: | 306 public: |
294 GrLayerCache(GrContext*); | 307 GrLayerCache(GrContext*); |
295 ~GrLayerCache(); | 308 ~GrLayerCache(); |
296 | 309 |
297 // As a cache, the GrLayerCache can be ordered to free up all its cached | 310 // As a cache, the GrLayerCache can be ordered to free up all its cached |
298 // elements by the GrContext | 311 // elements by the GrContext |
299 void freeAll(); | 312 void freeAll(); |
300 | 313 |
301 GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm, | 314 GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 SkDEBUGCODE(void validate() const;) | 352 SkDEBUGCODE(void validate() const;) |
340 | 353 |
341 #ifdef SK_DEVELOPER | 354 #ifdef SK_DEVELOPER |
342 void writeLayersToDisk(const SkString& dirName); | 355 void writeLayersToDisk(const SkString& dirName); |
343 #endif | 356 #endif |
344 | 357 |
345 static bool PlausiblyAtlasable(int width, int height) { | 358 static bool PlausiblyAtlasable(int width, int height) { |
346 return width <= kPlotWidth && height <= kPlotHeight; | 359 return width <= kPlotWidth && height <= kPlotHeight; |
347 } | 360 } |
348 | 361 |
| 362 void begin(); |
| 363 void end(); |
| 364 |
349 #if !GR_CACHE_HOISTED_LAYERS | 365 #if !GR_CACHE_HOISTED_LAYERS |
350 void purgeAll(); | 366 void purgeAll(); |
351 #endif | 367 #endif |
352 | 368 |
353 private: | 369 private: |
354 static const int kAtlasTextureWidth = 1024; | 370 static const int kAtlasTextureWidth = 1024; |
355 static const int kAtlasTextureHeight = 1024; | 371 static const int kAtlasTextureHeight = 1024; |
356 | 372 |
357 static const int kNumPlotsX = 2; | 373 static const int kNumPlotsX = 2; |
358 static const int kNumPlotsY = 2; | 374 static const int kNumPlotsY = 2; |
359 | 375 |
360 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX; | 376 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX; |
361 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY; | 377 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY; |
362 | 378 |
363 GrContext* fContext; // pointer back to owning context | 379 GrContext* fContext; // pointer back to owning context |
364 SkAutoTDelete<GrLayerAtlas> fAtlas; // TODO: could lazily allocate | 380 SkAutoTDelete<GrLayerAtlas> fAtlas; // lazily allocated |
365 | 381 |
366 // We cache this information here (rather then, say, on the owning picture) | 382 // We cache this information here (rather then, say, on the owning picture) |
367 // because we want to be able to clean it up as needed (e.g., if a picture | 383 // because we want to be able to clean it up as needed (e.g., if a picture |
368 // is leaked and never cleans itself up we still want to be able to | 384 // is leaked and never cleans itself up we still want to be able to |
369 // remove the GrPictureInfo once its layers are purged from all the atlas | 385 // remove the GrPictureInfo once its layers are purged from all the atlas |
370 // plots). | 386 // plots). |
371 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; | 387 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; |
372 | 388 |
373 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; | 389 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; |
374 | 390 |
(...skipping 15 matching lines...) Expand all Loading... |
390 const SkIRect& srcIR, const SkIRect& dstIR, | 406 const SkIRect& srcIR, const SkIRect& dstIR, |
391 const SkMatrix& initialMat, | 407 const SkMatrix& initialMat, |
392 const int* key, int keySize, | 408 const int* key, int keySize, |
393 const SkPaint* paint); | 409 const SkPaint* paint); |
394 | 410 |
395 // Remove all the layers (and unlock any resources) associated with 'picture
ID' | 411 // Remove all the layers (and unlock any resources) associated with 'picture
ID' |
396 void purge(uint32_t pictureID); | 412 void purge(uint32_t pictureID); |
397 | 413 |
398 void purgePlot(GrLayerAtlas::Plot* plot); | 414 void purgePlot(GrLayerAtlas::Plot* plot); |
399 | 415 |
400 // Try to find a purgeable plot and clear it out. Return true if a plot | 416 // Either purge all un-locked plots or just one. Return true if >= 1 plot |
401 // was purged; false otherwise. | 417 // was purged; false otherwise. |
402 bool purgePlot(); | 418 bool purgePlots(bool justOne); |
403 | 419 |
404 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } | 420 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } |
405 void decPlotLock(int plotIdx) { | 421 void decPlotLock(int plotIdx) { |
406 SkASSERT(fPlotLocks[plotIdx] > 0); | 422 SkASSERT(fPlotLocks[plotIdx] > 0); |
407 --fPlotLocks[plotIdx]; | 423 --fPlotLocks[plotIdx]; |
408 } | 424 } |
409 | 425 |
410 // for testing | 426 // for testing |
411 friend class TestingAccess; | 427 friend class TestingAccess; |
412 int numLayers() const { return fLayerHash.count(); } | 428 int numLayers() const { return fLayerHash.count(); } |
413 }; | 429 }; |
414 | 430 |
415 #endif | 431 #endif |
OLD | NEW |