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