blob: 1f32a7ecdfd5c8a99b0e39046c10756b600213b0 [file] [log] [blame]
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkPictureShader.h"
9
10#include "SkBitmap.h"
11#include "SkBitmapProcShader.h"
12#include "SkCanvas.h"
13#include "SkMatrixUtils.h"
14#include "SkPicture.h"
15#include "SkReadBuffer.h"
fmalita23df2d62014-10-22 07:39:08 -070016#include "SkResourceCache.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000017
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
20#endif
21
fmalita171e5b72014-10-22 11:20:40 -070022namespace {
23static unsigned gBitmapSkaderKeyNamespaceLabel;
24
fmalita23df2d62014-10-22 07:39:08 -070025struct BitmapShaderKey : public SkResourceCache::Key {
26public:
27 BitmapShaderKey(uint32_t pictureID,
28 const SkRect& tile,
29 SkShader::TileMode tmx,
30 SkShader::TileMode tmy,
31 const SkSize& scale,
32 const SkMatrix& localMatrix)
33 : fPictureID(pictureID)
34 , fTile(tile)
35 , fTmx(tmx)
36 , fTmy(tmy)
37 , fScale(scale)
38 , fLocalMatrix(localMatrix) {
39
40 static const size_t keySize = sizeof(fPictureID) +
41 sizeof(fTile) +
42 sizeof(fTmx) + sizeof(fTmy) +
43 sizeof(fScale) +
44 sizeof(fLocalMatrix);
45 // This better be packed.
46 SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - &fPictureID) == keySize);
fmalita171e5b72014-10-22 11:20:40 -070047 this->init(&gBitmapSkaderKeyNamespaceLabel, keySize);
fmalita23df2d62014-10-22 07:39:08 -070048 }
49
50private:
51 uint32_t fPictureID;
52 SkRect fTile;
53 SkShader::TileMode fTmx, fTmy;
54 SkSize fScale;
55 SkMatrix fLocalMatrix;
56
57 SkDEBUGCODE(uint32_t fEndOfStruct;)
58};
59
60struct BitmapShaderRec : public SkResourceCache::Rec {
61 BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader, size_t bitmapBytes)
62 : fKey(key)
63 , fShader(SkRef(tileShader))
64 , fBitmapBytes(bitmapBytes) {}
65
66 BitmapShaderKey fKey;
67 SkAutoTUnref<SkShader> fShader;
68 size_t fBitmapBytes;
69
70 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
71 virtual size_t bytesUsed() const SK_OVERRIDE {
72 return sizeof(fKey) + sizeof(SkShader) + fBitmapBytes;
73 }
74
75 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader) {
76 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec);
77 SkAutoTUnref<SkShader>* result = reinterpret_cast<SkAutoTUnref<SkShader>*>(contextShader);
78
79 result->reset(SkRef(rec.fShader.get()));
80 return true;
81 }
82};
83
fmalita23df2d62014-10-22 07:39:08 -070084static bool cache_try_alloc_pixels(SkBitmap* bitmap) {
fmalita171e5b72014-10-22 11:20:40 -070085 SkBitmap::Allocator* allocator = SkResourceCache::GetAllocator();
fmalita23df2d62014-10-22 07:39:08 -070086
fmalita171e5b72014-10-22 11:20:40 -070087 return NULL != allocator
88 ? allocator->allocPixelRef(bitmap, NULL)
89 : bitmap->tryAllocPixels();
fmalita23df2d62014-10-22 07:39:08 -070090}
91
fmalita171e5b72014-10-22 11:20:40 -070092} // namespace
93
robertphillipse8464992014-07-14 07:53:26 -070094SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMode tmy,
fmalitab5f78262014-08-06 13:07:15 -070095 const SkMatrix* localMatrix, const SkRect* tile)
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000096 : INHERITED(localMatrix)
97 , fPicture(SkRef(picture))
bsalomon49f085d2014-09-05 13:34:00 -070098 , fTile(tile ? *tile : picture->cullRect())
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000099 , fTmx(tmx)
fmalitab5f78262014-08-06 13:07:15 -0700100 , fTmy(tmy) {
fmalitab5f78262014-08-06 13:07:15 -0700101}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000102
reed9fa60da2014-08-21 07:59:51 -0700103#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
104SkPictureShader::SkPictureShader(SkReadBuffer& buffer) : INHERITED(buffer) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000105 fTmx = static_cast<SkShader::TileMode>(buffer.read32());
106 fTmy = static_cast<SkShader::TileMode>(buffer.read32());
fmalitab5f78262014-08-06 13:07:15 -0700107 buffer.readRect(&fTile);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000108 fPicture = SkPicture::CreateFromBuffer(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000109}
reed9fa60da2014-08-21 07:59:51 -0700110#endif
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000111
112SkPictureShader::~SkPictureShader() {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000113 fPicture->unref();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000114}
115
robertphillipse8464992014-07-14 07:53:26 -0700116SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy,
fmalitab5f78262014-08-06 13:07:15 -0700117 const SkMatrix* localMatrix, const SkRect* tile) {
bsalomon49f085d2014-09-05 13:34:00 -0700118 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000119 return NULL;
120 }
fmalitab5f78262014-08-06 13:07:15 -0700121 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000122}
123
reed9fa60da2014-08-21 07:59:51 -0700124SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) {
125 SkMatrix lm;
126 buffer.readMatrix(&lm);
127 TileMode mx = (TileMode)buffer.read32();
128 TileMode my = (TileMode)buffer.read32();
129 SkRect tile;
130 buffer.readRect(&tile);
131 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromBuffer(buffer));
132 return SkPictureShader::Create(picture, mx, my, &lm, &tile);
133}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000134
reed9fa60da2014-08-21 07:59:51 -0700135void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
136 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000137 buffer.write32(fTmx);
138 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -0700139 buffer.writeRect(fTile);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000140 fPicture->flatten(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000141}
142
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000143SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700144 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000145
146 SkMatrix m;
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000147 m.setConcat(matrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000148 if (localM) {
149 m.preConcat(*localM);
150 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000151
152 // Use a rotation-invariant scale
153 SkPoint scale;
154 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
155 // Decomposition failed, use an approximation.
156 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
157 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
158 }
fmalitab5f78262014-08-06 13:07:15 -0700159 SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000160
fmalitabb204f42014-08-07 08:39:24 -0700161 // Clamp the tile size to about 16M pixels
162 static const SkScalar kMaxTileArea = 4096 * 4096;
163 SkScalar tileArea = SkScalarMul(scaledSize.width(), scaledSize.height());
164 if (tileArea > kMaxTileArea) {
165 SkScalar clampScale = SkScalarSqrt(SkScalarDiv(kMaxTileArea, tileArea));
166 scaledSize.set(SkScalarMul(scaledSize.width(), clampScale),
167 SkScalarMul(scaledSize.height(), clampScale));
168 }
169
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000170 SkISize tileSize = scaledSize.toRound();
171 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000172 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000173 }
174
fmalitabb204f42014-08-07 08:39:24 -0700175 // The actual scale, compensating for rounding & clamping.
fmalitab5f78262014-08-06 13:07:15 -0700176 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
177 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000178
fmalita23df2d62014-10-22 07:39:08 -0700179 SkAutoTUnref<SkShader> tileShader;
180 BitmapShaderKey key(fPicture->uniqueID(),
181 fTile,
182 fTmx,
183 fTmy,
184 tileScale,
185 this->getLocalMatrix());
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000186
fmalita171e5b72014-10-22 11:20:40 -0700187 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000188 SkBitmap bm;
fmalita23df2d62014-10-22 07:39:08 -0700189 bm.setInfo(SkImageInfo::MakeN32Premul(tileSize));
190 if (!cache_try_alloc_pixels(&bm)) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000191 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000192 }
193 bm.eraseColor(SK_ColorTRANSPARENT);
194
195 SkCanvas canvas(bm);
196 canvas.scale(tileScale.width(), tileScale.height());
fmalitab5f78262014-08-06 13:07:15 -0700197 canvas.translate(fTile.x(), fTile.y());
robertphillips9b14f262014-06-04 05:40:44 -0700198 canvas.drawPicture(fPicture);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000199
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000200 SkMatrix shaderMatrix = this->getLocalMatrix();
201 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
fmalita23df2d62014-10-22 07:39:08 -0700202 tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
203
fmalita171e5b72014-10-22 11:20:40 -0700204 SkResourceCache::Add(SkNEW_ARGS(BitmapShaderRec, (key, tileShader.get(), bm.getSize())));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000205 }
206
fmalita23df2d62014-10-22 07:39:08 -0700207 return tileShader.detach();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000208}
209
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000210size_t SkPictureShader::contextSize() const {
211 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000212}
213
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000214SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000215 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000216 if (NULL == bitmapShader.get()) {
217 return NULL;
218 }
commit-bot@chromium.orgf03f9ff2014-05-06 13:43:17 +0000219 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000220}
221
222/////////////////////////////////////////////////////////////////////////////////////////
223
224SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
225 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
226 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
227 (shader, rec, bitmapShader));
228 if (NULL == ctx->fBitmapShaderContext) {
229 ctx->~PictureShaderContext();
230 ctx = NULL;
231 }
232 return ctx;
233}
234
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000235SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000236 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
237 : INHERITED(shader, rec)
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000238 , fBitmapShader(SkRef(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000239{
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000240 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
241 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
242 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000243}
244
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000245SkPictureShader::PictureShaderContext::~PictureShaderContext() {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000246 if (fBitmapShaderContext) {
247 fBitmapShaderContext->~Context();
248 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000249 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000250}
251
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000252uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000253 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000254 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000255}
256
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000257SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000258 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000259 return fBitmapShaderContext->asAShadeProc(ctx);
260}
261
262void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
263 SkASSERT(fBitmapShaderContext);
264 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
265}
266
267void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
268 SkASSERT(fBitmapShaderContext);
269 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000270}
271
272#ifndef SK_IGNORE_TO_STRING
273void SkPictureShader::toString(SkString* str) const {
274 static const char* gTileModeName[SkShader::kTileModeCount] = {
275 "clamp", "repeat", "mirror"
276 };
277
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700278 str->appendf("PictureShader: [%f:%f:%f:%f] ",
279 fPicture ? fPicture->cullRect().fLeft : 0,
280 fPicture ? fPicture->cullRect().fTop : 0,
281 fPicture ? fPicture->cullRect().fRight : 0,
282 fPicture ? fPicture->cullRect().fBottom : 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000283
284 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
285
286 this->INHERITED::toString(str);
287}
288#endif
289
290#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -0700291bool SkPictureShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
292 const SkMatrix* localMatrix, GrColor* paintColor,
293 GrFragmentProcessor** fp) const {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000294 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000295 if (!bitmapShader) {
dandov9de5b512014-06-10 14:38:28 -0700296 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000297 }
joshualittb0a8a372014-09-23 09:50:21 -0700298 return bitmapShader->asFragmentProcessor(context, paint, NULL, paintColor, fp);
dandov9de5b512014-06-10 14:38:28 -0700299}
300#else
joshualittb0a8a372014-09-23 09:50:21 -0700301bool SkPictureShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
302 GrFragmentProcessor**) const {
dandov9de5b512014-06-10 14:38:28 -0700303 SkDEBUGFAIL("Should not call in GPU-less build");
304 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000305}
306#endif