| 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 const unsigned* fKey; | 137 const unsigned* fKey; |
| 138 const int fKeySize; | 138 const int fKeySize; |
| 139 bool fFreeKey; | 139 bool fFreeKey; |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } | 142 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
| 143 static uint32_t Hash(const Key& key) { return key.hash(); } | 143 static uint32_t Hash(const Key& key) { return key.hash(); } |
| 144 | 144 |
| 145 // GrCachedLayer proper | 145 // GrCachedLayer proper |
| 146 GrCachedLayer(uint32_t pictureID, unsigned start, unsigned stop, | 146 GrCachedLayer(uint32_t pictureID, unsigned start, unsigned stop, |
| 147 const SkIRect& srcIR, const SkIRect& dstIR, | 147 const SkIRect& bounds, const SkMatrix& ctm, |
| 148 const SkMatrix& ctm, | |
| 149 const unsigned* key, int keySize, | 148 const unsigned* key, int keySize, |
| 150 const SkPaint* paint) | 149 const SkPaint* paint) |
| 151 : fKey(pictureID, ctm, key, keySize, true) | 150 : fKey(pictureID, ctm, key, keySize, true) |
| 152 , fStart(start) | 151 , fStart(start) |
| 153 , fStop(stop) | 152 , fStop(stop) |
| 154 , fSrcIR(srcIR) | 153 , fBounds(bounds) |
| 155 , fDstIR(dstIR) | |
| 156 , fOffset(SkIPoint::Make(0, 0)) | |
| 157 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) | 154 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) |
| 158 , fFilter(NULL) | 155 , fFilter(NULL) |
| 159 , fTexture(NULL) | 156 , fTexture(NULL) |
| 160 , fRect(SkIRect::MakeEmpty()) | 157 , fRect(SkIRect::MakeEmpty()) |
| 161 , fPlot(NULL) | 158 , fPlot(NULL) |
| 162 , fUses(0) | 159 , fUses(0) |
| 163 , fLocked(false) { | 160 , fLocked(false) { |
| 164 SkASSERT(SK_InvalidGenID != pictureID); | 161 SkASSERT(SK_InvalidGenID != pictureID); |
| 165 | 162 |
| 166 if (fPaint) { | 163 if (fPaint) { |
| 167 if (fPaint->getImageFilter() && fPaint->getImageFilter()->canFilterI
mageGPU()) { | 164 fFilter = SkSafeRef(fPaint->getImageFilter()); |
| 168 fFilter = SkSafeRef(fPaint->getImageFilter()); | 165 fPaint->setImageFilter(NULL); |
| 169 fPaint->setImageFilter(NULL); | |
| 170 } | |
| 171 } | 166 } |
| 172 } | 167 } |
| 173 | 168 |
| 174 ~GrCachedLayer() { | 169 ~GrCachedLayer() { |
| 175 SkSafeUnref(fTexture); | 170 SkSafeUnref(fTexture); |
| 176 SkSafeUnref(fFilter); | 171 SkSafeUnref(fFilter); |
| 177 SkDELETE(fPaint); | 172 SkDELETE(fPaint); |
| 178 } | 173 } |
| 179 | 174 |
| 180 uint32_t pictureID() const { return fKey.pictureID(); } | 175 uint32_t pictureID() const { return fKey.pictureID(); } |
| 181 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse | 176 // TODO: remove these when GrCachedLayer & ReplacementInfo fuse |
| 182 const unsigned* key() const { return fKey.key(); } | 177 const unsigned* key() const { return fKey.key(); } |
| 183 int keySize() const { return fKey.keySize(); } | 178 int keySize() const { return fKey.keySize(); } |
| 184 | 179 |
| 185 unsigned start() const { return fStart; } | 180 unsigned start() const { return fStart; } |
| 186 // TODO: make bound debug only | 181 // TODO: make bound debug only |
| 187 const SkIRect& srcIR() const { return fSrcIR; } | 182 const SkIRect& bound() const { return fBounds; } |
| 188 const SkIRect& dstIR() const { return fDstIR; } | |
| 189 unsigned stop() const { return fStop; } | 183 unsigned stop() const { return fStop; } |
| 190 void setTexture(GrTexture* texture, const SkIRect& rect) { | 184 void setTexture(GrTexture* texture, const SkIRect& rect) { |
| 191 SkRefCnt_SafeAssign(fTexture, texture); | 185 SkRefCnt_SafeAssign(fTexture, texture); |
| 192 fRect = rect; | 186 fRect = rect; |
| 193 } | 187 } |
| 194 GrTexture* texture() { return fTexture; } | 188 GrTexture* texture() { return fTexture; } |
| 195 const SkPaint* paint() const { return fPaint; } | 189 const SkPaint* paint() const { return fPaint; } |
| 196 const SkImageFilter* filter() const { return fFilter; } | 190 const SkImageFilter* filter() const { return fFilter; } |
| 197 const SkIRect& rect() const { return fRect; } | 191 const SkIRect& rect() const { return fRect; } |
| 198 | 192 |
| 199 void setOffset(const SkIPoint& offset) { fOffset = offset; } | |
| 200 const SkIPoint& offset() const { return fOffset; } | |
| 201 | |
| 202 void setPlot(GrPlot* plot) { | 193 void setPlot(GrPlot* plot) { |
| 203 SkASSERT(NULL == plot || NULL == fPlot); | 194 SkASSERT(NULL == plot || NULL == fPlot); |
| 204 fPlot = plot; | 195 fPlot = plot; |
| 205 } | 196 } |
| 206 GrPlot* plot() { return fPlot; } | 197 GrPlot* plot() { return fPlot; } |
| 207 | 198 |
| 208 bool isAtlased() const { return SkToBool(fPlot); } | 199 bool isAtlased() const { return SkToBool(fPlot); } |
| 209 | 200 |
| 210 void setLocked(bool locked) { fLocked = locked; } | 201 void setLocked(bool locked) { fLocked = locked; } |
| 211 bool locked() const { return fLocked; } | 202 bool locked() const { return fLocked; } |
| 212 | 203 |
| 213 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) | 204 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) |
| 214 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) | 205 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
| 215 | 206 |
| 216 private: | 207 private: |
| 217 const Key fKey; | 208 const Key fKey; |
| 218 | 209 |
| 219 // The "saveLayer" operation index of the cached layer | 210 // The "saveLayer" operation index of the cached layer |
| 220 const unsigned fStart; | 211 const unsigned fStart; |
| 221 // The final "restore" operation index of the cached layer | 212 // The final "restore" operation index of the cached layer |
| 222 const unsigned fStop; | 213 const unsigned fStop; |
| 223 | 214 |
| 224 // The layer's src rect (i.e., the portion of the source scene required | 215 const SkIRect fBounds; |
| 225 // for filtering). | |
| 226 const SkIRect fSrcIR; | |
| 227 // The layer's dest rect (i.e., where it will land in device space) | |
| 228 const SkIRect fDstIR; | |
| 229 // Offset sometimes required by image filters | |
| 230 SkIPoint fOffset; | |
| 231 | 216 |
| 232 // The paint used when dropping the layer down into the owning canvas. | 217 // The paint used when dropping the layer down into the owning canvas. |
| 233 // Can be NULL. This class makes a copy for itself. | 218 // Can be NULL. This class makes a copy for itself. |
| 234 SkPaint* fPaint; | 219 SkPaint* fPaint; |
| 235 | 220 |
| 236 // The imagefilter that needs to be applied to the layer prior to it being | 221 // The imagefilter that needs to be applied to the layer prior to it being |
| 237 // composited with the rest of the scene. | 222 // composited with the rest of the scene. |
| 238 const SkImageFilter* fFilter; | 223 const SkImageFilter* fFilter; |
| 239 | 224 |
| 240 // fTexture is a ref on the atlasing texture for atlased layers and a | 225 // fTexture is a ref on the atlasing texture for atlased layers and a |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 ~GrLayerCache(); | 269 ~GrLayerCache(); |
| 285 | 270 |
| 286 // As a cache, the GrLayerCache can be ordered to free up all its cached | 271 // As a cache, the GrLayerCache can be ordered to free up all its cached |
| 287 // elements by the GrContext | 272 // elements by the GrContext |
| 288 void freeAll(); | 273 void freeAll(); |
| 289 | 274 |
| 290 GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm, | 275 GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm, |
| 291 const unsigned* key, int keySize); | 276 const unsigned* key, int keySize); |
| 292 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, | 277 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, |
| 293 int start, int stop, | 278 int start, int stop, |
| 294 const SkIRect& srcIR, | 279 const SkIRect& bounds, |
| 295 const SkIRect& dstIR, | |
| 296 const SkMatrix& initialMat, | 280 const SkMatrix& initialMat, |
| 297 const unsigned* key, int keySize, | 281 const unsigned* key, int keySize, |
| 298 const SkPaint* paint); | 282 const SkPaint* paint); |
| 299 | 283 |
| 300 // Attempt to place 'layer' in the atlas. Return true on success; false on f
ailure. | 284 // Attempt to place 'layer' in the atlas. Return true on success; false on f
ailure. |
| 301 // When true is returned, 'needsRendering' will indicate if the layer must b
e (re)drawn. | 285 // When true is returned, 'needsRendering' will indicate if the layer must b
e (re)drawn. |
| 302 // Additionally, the GPU resources will be locked. | 286 // Additionally, the GPU resources will be locked. |
| 303 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needs
Rendering); | 287 bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needs
Rendering); |
| 304 | 288 |
| 305 // Attempt to lock the GPU resources required for a layer. Return true on su
ccess; | 289 // Attempt to lock the GPU resources required for a layer. Return true on su
ccess; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 // count for that plot. Similarly, once a rendering is complete all the | 353 // count for that plot. Similarly, once a rendering is complete all the |
| 370 // layers used in it decrement the lock count for the used plots. | 354 // layers used in it decrement the lock count for the used plots. |
| 371 // Plots with a 0 lock count are open for recycling/purging. | 355 // Plots with a 0 lock count are open for recycling/purging. |
| 372 int fPlotLocks[kNumPlotsX * kNumPlotsY]; | 356 int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
| 373 | 357 |
| 374 // Inform the cache that layer's cached image is not currently required | 358 // Inform the cache that layer's cached image is not currently required |
| 375 void unlock(GrCachedLayer* layer); | 359 void unlock(GrCachedLayer* layer); |
| 376 | 360 |
| 377 void initAtlas(); | 361 void initAtlas(); |
| 378 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, | 362 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, |
| 379 const SkIRect& srcIR, const SkIRect& dstIR, | 363 const SkIRect& bounds, const SkMatrix& initialMat
, |
| 380 const SkMatrix& initialMat, | |
| 381 const unsigned* key, int keySize, | 364 const unsigned* key, int keySize, |
| 382 const SkPaint* paint); | 365 const SkPaint* paint); |
| 383 | 366 |
| 384 // Remove all the layers (and unlock any resources) associated with 'picture
ID' | 367 // Remove all the layers (and unlock any resources) associated with 'picture
ID' |
| 385 void purge(uint32_t pictureID); | 368 void purge(uint32_t pictureID); |
| 386 | 369 |
| 387 void purgePlot(GrPlot* plot); | 370 void purgePlot(GrPlot* plot); |
| 388 | 371 |
| 389 // Try to find a purgeable plot and clear it out. Return true if a plot | 372 // Try to find a purgeable plot and clear it out. Return true if a plot |
| 390 // was purged; false otherwise. | 373 // was purged; false otherwise. |
| 391 bool purgePlot(); | 374 bool purgePlot(); |
| 392 | 375 |
| 393 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } | 376 void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } |
| 394 void decPlotLock(int plotIdx) { | 377 void decPlotLock(int plotIdx) { |
| 395 SkASSERT(fPlotLocks[plotIdx] > 0); | 378 SkASSERT(fPlotLocks[plotIdx] > 0); |
| 396 --fPlotLocks[plotIdx]; | 379 --fPlotLocks[plotIdx]; |
| 397 } | 380 } |
| 398 | 381 |
| 399 // for testing | 382 // for testing |
| 400 friend class TestingAccess; | 383 friend class TestingAccess; |
| 401 int numLayers() const { return fLayerHash.count(); } | 384 int numLayers() const { return fLayerHash.count(); } |
| 402 }; | 385 }; |
| 403 | 386 |
| 404 #endif | 387 #endif |
| OLD | NEW |