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

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

Issue 1323863002: Image generator-backed SkPictureShader (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Created 5 years, 3 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 "SkImageGenerator.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"
16 #include "SkResourceCache.h" 17 #include "SkResourceCache.h"
17 18
18 #if SK_SUPPORT_GPU 19 #if SK_SUPPORT_GPU
19 #include "GrContext.h" 20 #include "GrContext.h"
20 #include "GrCaps.h" 21 #include "GrCaps.h"
21 #endif 22 #endif
22 23
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 } 78 }
78 const char* getCategory() const override { return "bitmap-shader"; } 79 const char* getCategory() const override { return "bitmap-shader"; }
79 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur n nullptr; } 80 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur n nullptr; }
80 81
81 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader ) { 82 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader ) {
82 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec ); 83 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec );
83 SkAutoTUnref<SkShader>* result = reinterpret_cast<SkAutoTUnref<SkShader> *>(contextShader); 84 SkAutoTUnref<SkShader>* result = reinterpret_cast<SkAutoTUnref<SkShader> *>(contextShader);
84 85
85 result->reset(SkRef(rec.fShader.get())); 86 result->reset(SkRef(rec.fShader.get()));
86 87
87 SkBitmap tile; 88 // The bitmap shader is backed by an image generator, thus it can always re-generate its
88 if (rec.fShader.get()->isABitmap(&tile, nullptr, nullptr)) { 89 // pixels if discarded.
89 // FIXME: this doesn't protect the pixels from being discarded as so on as we unlock. 90 return true;
90 // Should be handled via a pixel ref generator instead
91 // (https://code.google.com/p/skia/issues/detail?id=3220).
92 SkAutoLockPixels alp(tile, true);
93 return tile.getPixels() != nullptr;
94 }
95 return false;
96 } 91 }
97 }; 92 };
98 93
99 static bool cache_try_alloc_pixels(SkBitmap* bitmap) {
100 SkBitmap::Allocator* allocator = SkResourceCache::GetAllocator();
101
102 return nullptr != allocator
103 ? allocator->allocPixelRef(bitmap, nullptr)
104 : bitmap->tryAllocPixels();
105 }
106
107 } // namespace 94 } // namespace
108 95
109 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod e tmy, 96 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod e tmy,
110 const SkMatrix* localMatrix, const SkRect* tile ) 97 const SkMatrix* localMatrix, const SkRect* tile )
111 : INHERITED(localMatrix) 98 : INHERITED(localMatrix)
112 , fPicture(SkRef(picture)) 99 , fPicture(SkRef(picture))
113 , fTile(tile ? *tile : picture->cullRect()) 100 , fTile(tile ? *tile : picture->cullRect())
114 , fTmx(tmx) 101 , fTmx(tmx)
115 , fTmy(tmy) { 102 , fTmy(tmy) {
116 } 103 }
117 104
118 SkPictureShader::~SkPictureShader() {
119 fPicture->unref();
120 }
121
122 SkShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMo de tmy, 105 SkShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMo de tmy,
123 const SkMatrix* localMatrix, const SkRe ct* tile) { 106 const SkMatrix* localMatrix, const SkRe ct* tile) {
124 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) { 107 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
125 return SkShader::CreateEmptyShader(); 108 return SkShader::CreateEmptyShader();
126 } 109 }
127 return new SkPictureShader(picture, tmx, tmy, localMatrix, tile); 110 return new SkPictureShader(picture, tmx, tmy, localMatrix, tile);
128 } 111 }
129 112
130 SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) { 113 SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) {
131 SkMatrix lm; 114 SkMatrix lm;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 208
226 SkAutoTUnref<SkShader> tileShader; 209 SkAutoTUnref<SkShader> tileShader;
227 BitmapShaderKey key(fPicture->uniqueID(), 210 BitmapShaderKey key(fPicture->uniqueID(),
228 fTile, 211 fTile,
229 fTmx, 212 fTmx,
230 fTmy, 213 fTmy,
231 tileScale, 214 tileScale,
232 this->getLocalMatrix()); 215 this->getLocalMatrix());
233 216
234 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) { 217 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
218 SkMatrix matrix;
219 matrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.h eight()),
220 SkMatrix::kFill_ScaleToFit);
235 SkBitmap bm; 221 SkBitmap bm;
236 bm.setInfo(SkImageInfo::MakeN32Premul(tileSize)); 222 if (!SkInstallDiscardablePixelRef(
237 if (!cache_try_alloc_pixels(&bm)) { 223 SkImageGenerator::NewFromPicture(tileSize, fPicture, &matrix, nullpt r), &bm)) {
238 return SkShader::CreateEmptyShader(); 224 return nullptr;
239 } 225 }
240 bm.eraseColor(SK_ColorTRANSPARENT);
241
242 // Always disable LCD text, since we can't assume our image will be opaq ue.
243 SkCanvas canvas(bm, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
244
245 canvas.scale(tileScale.width(), tileScale.height());
246 canvas.translate(-fTile.x(), -fTile.y());
247 canvas.drawPicture(fPicture);
248 226
249 SkMatrix shaderMatrix = this->getLocalMatrix(); 227 SkMatrix shaderMatrix = this->getLocalMatrix();
250 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); 228 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
251 tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix)); 229 tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
252 230
253 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get(), bm.getSi ze())); 231 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get(), bm.getSi ze()));
254 } 232 }
255 233
256 return tileShader.detach(); 234 return tileShader.detach();
257 } 235 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 fBitmapShaderContext->shadeSpan16(x, y, dstC, count); 295 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
318 } 296 }
319 297
320 #ifndef SK_IGNORE_TO_STRING 298 #ifndef SK_IGNORE_TO_STRING
321 void SkPictureShader::toString(SkString* str) const { 299 void SkPictureShader::toString(SkString* str) const {
322 static const char* gTileModeName[SkShader::kTileModeCount] = { 300 static const char* gTileModeName[SkShader::kTileModeCount] = {
323 "clamp", "repeat", "mirror" 301 "clamp", "repeat", "mirror"
324 }; 302 };
325 303
326 str->appendf("PictureShader: [%f:%f:%f:%f] ", 304 str->appendf("PictureShader: [%f:%f:%f:%f] ",
327 fPicture ? fPicture->cullRect().fLeft : 0, 305 fPicture->cullRect().fLeft,
328 fPicture ? fPicture->cullRect().fTop : 0, 306 fPicture->cullRect().fTop,
329 fPicture ? fPicture->cullRect().fRight : 0, 307 fPicture->cullRect().fRight,
330 fPicture ? fPicture->cullRect().fBottom : 0); 308 fPicture->cullRect().fBottom);
331 309
332 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); 310 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
333 311
334 this->INHERITED::toString(str); 312 this->INHERITED::toString(str);
335 } 313 }
336 #endif 314 #endif
337 315
338 #if SK_SUPPORT_GPU 316 #if SK_SUPPORT_GPU
339 const GrFragmentProcessor* SkPictureShader::asFragmentProcessor( 317 const GrFragmentProcessor* SkPictureShader::asFragmentProcessor(
340 GrContext* context, 318 GrContext* context,
341 const SkMatrix& viewM, 319 const SkMatrix& viewM,
342 const SkMatrix* localMatrix, 320 const SkMatrix* localMatrix,
343 SkFilterQuality fq, 321 SkFilterQuality fq,
344 GrProcessorDataManager* proc DataManager) const { 322 GrProcessorDataManager* proc DataManager) const {
345 int maxTextureSize = 0; 323 int maxTextureSize = 0;
346 if (context) { 324 if (context) {
347 maxTextureSize = context->caps()->maxTextureSize(); 325 maxTextureSize = context->caps()->maxTextureSize();
348 } 326 }
349 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(viewM, localMatrix , maxTextureSize)); 327 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(viewM, localMatrix , maxTextureSize));
350 if (!bitmapShader) { 328 if (!bitmapShader) {
351 return nullptr; 329 return nullptr;
352 } 330 }
353 return bitmapShader->asFragmentProcessor(context, viewM, nullptr, fq, procDa taManager); 331 return bitmapShader->asFragmentProcessor(context, viewM, nullptr, fq, procDa taManager);
354 } 332 }
355 #endif 333 #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