Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/core/SkPictureShader.cpp

Issue 671683004: Picture shader resource caching. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: cannot combine scale & localmatrix Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkPictureShader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "SkPictureShader.h" 8 #include "SkPictureShader.h"
9 9
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
11 #include "SkBitmapProcShader.h" 11 #include "SkBitmapProcShader.h"
12 #include "SkCanvas.h" 12 #include "SkCanvas.h"
13 #include "SkDiscardableMemory.h"
13 #include "SkMatrixUtils.h" 14 #include "SkMatrixUtils.h"
14 #include "SkPicture.h" 15 #include "SkPicture.h"
15 #include "SkReadBuffer.h" 16 #include "SkReadBuffer.h"
17 #include "SkResourceCache.h"
18 #include "SkThread.h"
16 19
17 #if SK_SUPPORT_GPU 20 #if SK_SUPPORT_GPU
18 #include "GrContext.h" 21 #include "GrContext.h"
19 #endif 22 #endif
20 23
24 struct BitmapShaderKey : public SkResourceCache::Key {
25 public:
26 BitmapShaderKey(uint32_t pictureID,
27 const SkRect& tile,
28 SkShader::TileMode tmx,
29 SkShader::TileMode tmy,
30 const SkSize& scale,
31 const SkMatrix& localMatrix)
32 : fPictureID(pictureID)
33 , fTile(tile)
34 , fTmx(tmx)
35 , fTmy(tmy)
36 , fScale(scale)
37 , fLocalMatrix(localMatrix) {
38
39 static const size_t keySize = sizeof(fPictureID) +
40 sizeof(fTile) +
41 sizeof(fTmx) + sizeof(fTmy) +
42 sizeof(fScale) +
43 sizeof(fLocalMatrix);
44 // This better be packed.
45 SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - &fPictureID) == keySize);
46 this->init(keySize);
47 }
48
49 private:
50 uint32_t fPictureID;
51 SkRect fTile;
52 SkShader::TileMode fTmx, fTmy;
53 SkSize fScale;
54 SkMatrix fLocalMatrix;
55
56 SkDEBUGCODE(uint32_t fEndOfStruct;)
57 };
58
59 struct BitmapShaderRec : public SkResourceCache::Rec {
60 BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader, size_t bit mapBytes)
61 : fKey(key)
62 , fShader(SkRef(tileShader))
63 , fBitmapBytes(bitmapBytes) {}
64
65 BitmapShaderKey fKey;
66 SkAutoTUnref<SkShader> fShader;
67 size_t fBitmapBytes;
68
69 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
70 virtual size_t bytesUsed() const SK_OVERRIDE {
71 return sizeof(fKey) + sizeof(SkShader) + fBitmapBytes;
72 }
73
74 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader ) {
75 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec );
76 SkAutoTUnref<SkShader>* result = reinterpret_cast<SkAutoTUnref<SkShader> *>(contextShader);
77
78 result->reset(SkRef(rec.fShader.get()));
79 return true;
80 }
81 };
82
83 // FIXME: there's considerable boilerplate/duplication here vs. the global resou rce cache.
mtklein 2014/10/21 17:27:45 Why aren't we using the global cache?
84 SK_DECLARE_STATIC_MUTEX(gBitmapShaderCacheMutex);
85 static SkResourceCache* gBitmapShaderCache = NULL;
86
87 #ifndef SK_DEFAULT_TILE_CACHE_LIMIT
88 #define SK_DEFAULT_TILE_CACHE_LIMIT (2 * 1024 * 1024)
89 #endif
90
91 static void cleanup_cache() {
92 // We'll clean this up in our own tests, but disable for clients.
93 // Chrome seems to have funky multi-process things going on in unit tests th at
94 // makes this unsafe to delete when the main process atexit()s.
95 // SkLazyPtr does the same sort of thing.
96 #if SK_DEVELOPER
97 SkDELETE(gBitmapShaderCache);
98 #endif
99 }
100
101 /** Must hold gBitmapShaderCacheMutex when calling. */
102 static SkResourceCache* cache() {
103 // gTileCacheMutex is always held when this is called, so we don't need to b e fancy in here.
104 gBitmapShaderCacheMutex.assertHeld();
105 if (NULL == gBitmapShaderCache) {
106 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE
107 gBitmapShaderCache = SkNEW_ARGS(SkResourceCache, (SkDiscardableMemory::C reate));
108 #else
109 gBitmapShaderCache = SkNEW_ARGS(SkResourceCache, (SK_DEFAULT_TILE_CACHE_ LIMIT));
110 #endif
111 atexit(cleanup_cache);
112 }
113 return gBitmapShaderCache;
114 }
115
116 static bool cache_find(const BitmapShaderKey& key, SkAutoTUnref<SkShader>* resul t) {
117 SkAutoMutexAcquire am(gBitmapShaderCacheMutex);
118 return cache()->find(key, BitmapShaderRec::Visitor, result);
119 }
120
121 static void cache_add(BitmapShaderRec* rec) {
122 SkAutoMutexAcquire am(gBitmapShaderCacheMutex);
123 cache()->add(rec);
124 }
125
21 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod e tmy, 126 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod e tmy,
22 const SkMatrix* localMatrix, const SkRect* tile ) 127 const SkMatrix* localMatrix, const SkRect* tile )
23 : INHERITED(localMatrix) 128 : INHERITED(localMatrix)
24 , fPicture(SkRef(picture)) 129 , fPicture(SkRef(picture))
25 , fTile(tile ? *tile : picture->cullRect()) 130 , fTile(tile ? *tile : picture->cullRect())
26 , fTmx(tmx) 131 , fTmx(tmx)
27 , fTmy(tmy) { 132 , fTmy(tmy) {
28 } 133 }
29 134
30 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING 135 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 201
97 SkISize tileSize = scaledSize.toRound(); 202 SkISize tileSize = scaledSize.toRound();
98 if (tileSize.isEmpty()) { 203 if (tileSize.isEmpty()) {
99 return NULL; 204 return NULL;
100 } 205 }
101 206
102 // The actual scale, compensating for rounding & clamping. 207 // The actual scale, compensating for rounding & clamping.
103 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.widt h(), 208 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.widt h(),
104 SkIntToScalar(tileSize.height()) / fTile.hei ght()); 209 SkIntToScalar(tileSize.height()) / fTile.hei ght());
105 210
106 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); 211 SkAutoTUnref<SkShader> tileShader;
212 BitmapShaderKey key(fPicture->uniqueID(),
213 fTile,
214 fTmx,
215 fTmy,
216 tileScale,
217 this->getLocalMatrix());
107 218
108 if (!fCachedBitmapShader || tileScale != fCachedTileScale) { 219 if (!cache_find(key, &tileShader)) {
109 SkBitmap bm; 220 SkBitmap bm;
110 if (!bm.tryAllocN32Pixels(tileSize.width(), tileSize.height())) { 221 if (!bm.tryAllocN32Pixels(tileSize.width(), tileSize.height())) {
reed1 2014/10/21 20:28:00 need to use the cache's GetAllocator to ensure we'
f(malita) 2014/10/21 23:49:24 Done.
111 return NULL; 222 return NULL;
112 } 223 }
113 bm.eraseColor(SK_ColorTRANSPARENT); 224 bm.eraseColor(SK_ColorTRANSPARENT);
114 225
115 SkCanvas canvas(bm); 226 SkCanvas canvas(bm);
116 canvas.scale(tileScale.width(), tileScale.height()); 227 canvas.scale(tileScale.width(), tileScale.height());
117 canvas.translate(fTile.x(), fTile.y()); 228 canvas.translate(fTile.x(), fTile.y());
118 canvas.drawPicture(fPicture); 229 canvas.drawPicture(fPicture);
119 230
120 fCachedTileScale = tileScale;
121
122 SkMatrix shaderMatrix = this->getLocalMatrix(); 231 SkMatrix shaderMatrix = this->getLocalMatrix();
123 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); 232 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
124 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatr ix)); 233 tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
234
235 cache_add(SkNEW_ARGS(BitmapShaderRec, (key, tileShader.get(), bm.getSize ())));
125 } 236 }
126 237
127 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid. 238 return tileShader.detach();
128 // Otherwise, the pointer may have been overwritten on a different thread be fore the object's
129 // ref count was incremented.
130 fCachedBitmapShader.get()->ref();
131 return fCachedBitmapShader;
132 } 239 }
133 240
134 size_t SkPictureShader::contextSize() const { 241 size_t SkPictureShader::contextSize() const {
135 return sizeof(PictureShaderContext); 242 return sizeof(PictureShaderContext);
136 } 243 }
137 244
138 SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const { 245 SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
139 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec. fLocalMatrix)); 246 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec. fLocalMatrix));
140 if (NULL == bitmapShader.get()) { 247 if (NULL == bitmapShader.get()) {
141 return NULL; 248 return NULL;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 } 328 }
222 return bitmapShader->asFragmentProcessor(context, paint, NULL, paintColor, f p); 329 return bitmapShader->asFragmentProcessor(context, paint, NULL, paintColor, f p);
223 } 330 }
224 #else 331 #else
225 bool SkPictureShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMa trix*, GrColor*, 332 bool SkPictureShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMa trix*, GrColor*,
226 GrFragmentProcessor**) const { 333 GrFragmentProcessor**) const {
227 SkDEBUGFAIL("Should not call in GPU-less build"); 334 SkDEBUGFAIL("Should not call in GPU-less build");
228 return false; 335 return false;
229 } 336 }
230 #endif 337 #endif
OLDNEW
« no previous file with comments | « src/core/SkPictureShader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698