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