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

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

Issue 866773002: Use an image generator to back SkPictureShader tiles. (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: preserve LCD text comment Created 5 years, 9 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
« src/core/SkPictureShader.h ('K') | « 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 #endif 21 #endif
21 22
22 namespace { 23 namespace {
23 static unsigned gBitmapSkaderKeyNamespaceLabel; 24 static unsigned gBitmapSkaderKeyNamespaceLabel;
24 25
26 class PictureImageGenerator : public SkImageGenerator {
27 public:
28 PictureImageGenerator(const SkPicture* picture,
29 const SkRect& pictureTile,
30 const SkISize& tileSize)
31 : fPicture(SkRef(picture))
32 , fPictureTile(pictureTile)
33 , fRasterTileInfo(SkImageInfo::MakeN32Premul(tileSize)) {}
34
35 virtual ~PictureImageGenerator() {}
36
37 protected:
38 virtual bool onGetInfo(SkImageInfo *info) SK_OVERRIDE {
39 *info = fRasterTileInfo;
40 return true;
41 }
42
43 virtual Result onGetPixels(const SkImageInfo& info, void *pixels, size_t row Bytes,
44 SkPMColor ctable[], int *ctableCount) SK_OVERRIDE {
45 if (info != fRasterTileInfo || SkToBool(ctable) || SkToBool(ctableCount) ) {
46 return kInvalidConversion;
47 }
48
49 SkSize tileScale = SkSize::Make(SkIntToScalar(info.width()) / fPictureTi le.width(),
50 SkIntToScalar(info.height()) / fPictureT ile.height());
51 SkBitmap tileBitmap;
52 if (!tileBitmap.installPixels(info, pixels, rowBytes)) {
53 return kInvalidParameters;
54 }
55 tileBitmap.eraseColor(SK_ColorTRANSPARENT);
56
57 // Always disable LCD text, since we can't assume our image will be opaq ue.
58 SkCanvas tileCanvas(tileBitmap, SkSurfaceProps(0, kUnknown_SkPixelGeomet ry));
59 tileCanvas.scale(tileScale.width(), tileScale.height());
60 tileCanvas.translate(-fPictureTile.x(), -fPictureTile.y());
61 tileCanvas.drawPicture(fPicture);
62
63 return kSuccess;
64 }
65
66 private:
67 SkAutoTUnref<const SkPicture> fPicture;
68 const SkRect fPictureTile;
69 const SkImageInfo fRasterTileInfo;
70 };
71
25 struct BitmapShaderKey : public SkResourceCache::Key { 72 struct BitmapShaderKey : public SkResourceCache::Key {
26 public: 73 public:
27 BitmapShaderKey(uint32_t pictureID, 74 BitmapShaderKey(uint32_t pictureID,
28 const SkRect& tile, 75 const SkRect& tile,
29 SkShader::TileMode tmx, 76 SkShader::TileMode tmx,
30 SkShader::TileMode tmy, 77 SkShader::TileMode tmy,
31 const SkSize& scale, 78 const SkSize& scale,
32 const SkMatrix& localMatrix) 79 const SkMatrix& localMatrix)
33 : fPictureID(pictureID) 80 : fPictureID(pictureID)
34 , fTile(tile) 81 , fTile(tile)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 size_t bytesUsed() const SK_OVERRIDE { 121 size_t bytesUsed() const SK_OVERRIDE {
75 return sizeof(fKey) + sizeof(SkShader) + fBitmapBytes; 122 return sizeof(fKey) + sizeof(SkShader) + fBitmapBytes;
76 } 123 }
77 124
78 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader ) { 125 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader ) {
79 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec ); 126 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec );
80 SkAutoTUnref<SkShader>* result = reinterpret_cast<SkAutoTUnref<SkShader> *>(contextShader); 127 SkAutoTUnref<SkShader>* result = reinterpret_cast<SkAutoTUnref<SkShader> *>(contextShader);
81 128
82 result->reset(SkRef(rec.fShader.get())); 129 result->reset(SkRef(rec.fShader.get()));
83 130
84 SkBitmap tile; 131 // The bitmap shader is backed by an image generator, thus it can always re-generate its
85 rec.fShader.get()->asABitmap(&tile, NULL, NULL); 132 // pixels if discarded.
86 // FIXME: this doesn't protect the pixels from being discarded as soon a s we unlock. 133 return true;
87 // Should be handled via a pixel ref generator instead
88 // (https://code.google.com/p/skia/issues/detail?id=3220).
89 SkAutoLockPixels alp(tile, true);
90 return tile.getPixels() != NULL;
91 } 134 }
92 }; 135 };
93 136
94 static bool cache_try_alloc_pixels(SkBitmap* bitmap) {
95 SkBitmap::Allocator* allocator = SkResourceCache::GetAllocator();
96
97 return NULL != allocator
98 ? allocator->allocPixelRef(bitmap, NULL)
99 : bitmap->tryAllocPixels();
100 }
101
102 } // namespace 137 } // namespace
103 138
104 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod e tmy, 139 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod e tmy,
105 const SkMatrix* localMatrix, const SkRect* tile ) 140 const SkMatrix* localMatrix, const SkRect* tile )
106 : INHERITED(localMatrix) 141 : INHERITED(localMatrix)
107 , fPicture(SkRef(picture)) 142 , fPicture(SkRef(picture))
108 , fTile(tile ? *tile : picture->cullRect()) 143 , fTile(tile ? *tile : picture->cullRect())
109 , fTmx(tmx) 144 , fTmx(tmx)
110 , fTmy(tmy) { 145 , fTmy(tmy) {
111 } 146 }
112 147
113 SkPictureShader::~SkPictureShader() {
114 fPicture->unref();
115 }
116
117 SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy, 148 SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy,
118 const SkMatrix* localMatrix, const SkRe ct* tile) { 149 const SkMatrix* localMatrix, const SkRe ct* tile) {
119 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) { 150 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
120 return NULL; 151 return NULL;
121 } 152 }
122 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile)); 153 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
123 } 154 }
124 155
125 SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) { 156 SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) {
126 SkMatrix lm; 157 SkMatrix lm;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 SkAutoTUnref<SkShader> tileShader; 212 SkAutoTUnref<SkShader> tileShader;
182 BitmapShaderKey key(fPicture->uniqueID(), 213 BitmapShaderKey key(fPicture->uniqueID(),
183 fTile, 214 fTile,
184 fTmx, 215 fTmx,
185 fTmy, 216 fTmy,
186 tileScale, 217 tileScale,
187 this->getLocalMatrix()); 218 this->getLocalMatrix());
188 219
189 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) { 220 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
190 SkBitmap bm; 221 SkBitmap bm;
191 bm.setInfo(SkImageInfo::MakeN32Premul(tileSize)); 222 if (!SkInstallDiscardablePixelRef(SkNEW_ARGS(PictureImageGenerator,
192 if (!cache_try_alloc_pixels(&bm)) { 223 (fPicture, fTile, tileSize) ), &bm)) {
193 return NULL; 224 return NULL;
194 } 225 }
195 bm.eraseColor(SK_ColorTRANSPARENT);
196
197 // Always disable LCD text, since we can't assume our image will be opaq ue.
198 SkCanvas canvas(bm, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
199
200 canvas.scale(tileScale.width(), tileScale.height());
201 canvas.translate(-fTile.x(), -fTile.y());
202 canvas.drawPicture(fPicture);
203 226
204 SkMatrix shaderMatrix = this->getLocalMatrix(); 227 SkMatrix shaderMatrix = this->getLocalMatrix();
205 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); 228 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
206 tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix)); 229 tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
207 230
208 SkResourceCache::Add(SkNEW_ARGS(BitmapShaderRec, (key, tileShader.get(), bm.getSize()))); 231 SkResourceCache::Add(SkNEW_ARGS(BitmapShaderRec, (key, tileShader.get(), bm.getSize())));
209 } 232 }
210 233
211 return tileShader.detach(); 234 return tileShader.detach();
212 } 235 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 fBitmapShaderContext->shadeSpan16(x, y, dstC, count); 296 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
274 } 297 }
275 298
276 #ifndef SK_IGNORE_TO_STRING 299 #ifndef SK_IGNORE_TO_STRING
277 void SkPictureShader::toString(SkString* str) const { 300 void SkPictureShader::toString(SkString* str) const {
278 static const char* gTileModeName[SkShader::kTileModeCount] = { 301 static const char* gTileModeName[SkShader::kTileModeCount] = {
279 "clamp", "repeat", "mirror" 302 "clamp", "repeat", "mirror"
280 }; 303 };
281 304
282 str->appendf("PictureShader: [%f:%f:%f:%f] ", 305 str->appendf("PictureShader: [%f:%f:%f:%f] ",
283 fPicture ? fPicture->cullRect().fLeft : 0, 306 fPicture->cullRect().fLeft,
284 fPicture ? fPicture->cullRect().fTop : 0, 307 fPicture->cullRect().fTop,
285 fPicture ? fPicture->cullRect().fRight : 0, 308 fPicture->cullRect().fRight,
286 fPicture ? fPicture->cullRect().fBottom : 0); 309 fPicture->cullRect().fBottom);
287 310
288 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); 311 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
289 312
290 this->INHERITED::toString(str); 313 this->INHERITED::toString(str);
291 } 314 }
292 #endif 315 #endif
293 316
294 #if SK_SUPPORT_GPU 317 #if SK_SUPPORT_GPU
295 bool SkPictureShader::asFragmentProcessor(GrContext* context, const SkPaint& pai nt, 318 bool SkPictureShader::asFragmentProcessor(GrContext* context, const SkPaint& pai nt,
296 const SkMatrix& viewM, const SkMatrix* localMatrix, 319 const SkMatrix& viewM, const SkMatrix* localMatrix,
297 GrColor* paintColor, 320 GrColor* paintColor,
298 GrFragmentProcessor** fp) const { 321 GrFragmentProcessor** fp) const {
299 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(viewM, localMatrix )); 322 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(viewM, localMatrix ));
300 if (!bitmapShader) { 323 if (!bitmapShader) {
301 return false; 324 return false;
302 } 325 }
303 return bitmapShader->asFragmentProcessor(context, paint, viewM, NULL, paintC olor, fp); 326 return bitmapShader->asFragmentProcessor(context, paint, viewM, NULL, paintC olor, fp);
304 } 327 }
305 #else 328 #else
306 bool SkPictureShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMa trix&, 329 bool SkPictureShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMa trix&,
307 const SkMatrix*, GrColor*, 330 const SkMatrix*, GrColor*,
308 GrFragmentProcessor**) const { 331 GrFragmentProcessor**) const {
309 SkDEBUGFAIL("Should not call in GPU-less build"); 332 SkDEBUGFAIL("Should not call in GPU-less build");
310 return false; 333 return false;
311 } 334 }
312 #endif 335 #endif
OLDNEW
« src/core/SkPictureShader.h ('K') | « src/core/SkPictureShader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698