blob: 5fded935cccb8728d776a348ef3326d1d66ccd9a [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
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000103SkPictureShader::~SkPictureShader() {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000104 fPicture->unref();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000105}
106
robertphillipse8464992014-07-14 07:53:26 -0700107SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy,
fmalitab5f78262014-08-06 13:07:15 -0700108 const SkMatrix* localMatrix, const SkRect* tile) {
bsalomon49f085d2014-09-05 13:34:00 -0700109 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000110 return NULL;
111 }
fmalitab5f78262014-08-06 13:07:15 -0700112 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000113}
114
reed9fa60da2014-08-21 07:59:51 -0700115SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) {
116 SkMatrix lm;
117 buffer.readMatrix(&lm);
118 TileMode mx = (TileMode)buffer.read32();
119 TileMode my = (TileMode)buffer.read32();
120 SkRect tile;
121 buffer.readRect(&tile);
122 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromBuffer(buffer));
123 return SkPictureShader::Create(picture, mx, my, &lm, &tile);
124}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000125
reed9fa60da2014-08-21 07:59:51 -0700126void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
127 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000128 buffer.write32(fTmx);
129 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -0700130 buffer.writeRect(fTile);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000131 fPicture->flatten(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000132}
133
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000134SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700135 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000136
137 SkMatrix m;
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000138 m.setConcat(matrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000139 if (localM) {
140 m.preConcat(*localM);
141 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000142
143 // Use a rotation-invariant scale
144 SkPoint scale;
145 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
146 // Decomposition failed, use an approximation.
147 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
148 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
149 }
fmalitab5f78262014-08-06 13:07:15 -0700150 SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000151
fmalitabb204f42014-08-07 08:39:24 -0700152 // Clamp the tile size to about 16M pixels
153 static const SkScalar kMaxTileArea = 4096 * 4096;
154 SkScalar tileArea = SkScalarMul(scaledSize.width(), scaledSize.height());
155 if (tileArea > kMaxTileArea) {
156 SkScalar clampScale = SkScalarSqrt(SkScalarDiv(kMaxTileArea, tileArea));
157 scaledSize.set(SkScalarMul(scaledSize.width(), clampScale),
158 SkScalarMul(scaledSize.height(), clampScale));
159 }
160
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000161 SkISize tileSize = scaledSize.toRound();
162 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000163 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000164 }
165
fmalitabb204f42014-08-07 08:39:24 -0700166 // The actual scale, compensating for rounding & clamping.
fmalitab5f78262014-08-06 13:07:15 -0700167 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
168 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000169
fmalita23df2d62014-10-22 07:39:08 -0700170 SkAutoTUnref<SkShader> tileShader;
171 BitmapShaderKey key(fPicture->uniqueID(),
172 fTile,
173 fTmx,
174 fTmy,
175 tileScale,
176 this->getLocalMatrix());
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000177
fmalita171e5b72014-10-22 11:20:40 -0700178 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000179 SkBitmap bm;
fmalita23df2d62014-10-22 07:39:08 -0700180 bm.setInfo(SkImageInfo::MakeN32Premul(tileSize));
181 if (!cache_try_alloc_pixels(&bm)) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000182 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000183 }
184 bm.eraseColor(SK_ColorTRANSPARENT);
185
186 SkCanvas canvas(bm);
187 canvas.scale(tileScale.width(), tileScale.height());
fmalitab5f78262014-08-06 13:07:15 -0700188 canvas.translate(fTile.x(), fTile.y());
robertphillips9b14f262014-06-04 05:40:44 -0700189 canvas.drawPicture(fPicture);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000190
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000191 SkMatrix shaderMatrix = this->getLocalMatrix();
192 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
fmalita23df2d62014-10-22 07:39:08 -0700193 tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
194
fmalita171e5b72014-10-22 11:20:40 -0700195 SkResourceCache::Add(SkNEW_ARGS(BitmapShaderRec, (key, tileShader.get(), bm.getSize())));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000196 }
197
fmalita23df2d62014-10-22 07:39:08 -0700198 return tileShader.detach();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000199}
200
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000201size_t SkPictureShader::contextSize() const {
202 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000203}
204
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000205SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000206 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000207 if (NULL == bitmapShader.get()) {
208 return NULL;
209 }
commit-bot@chromium.orgf03f9ff2014-05-06 13:43:17 +0000210 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000211}
212
213/////////////////////////////////////////////////////////////////////////////////////////
214
215SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
216 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
217 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
218 (shader, rec, bitmapShader));
219 if (NULL == ctx->fBitmapShaderContext) {
220 ctx->~PictureShaderContext();
221 ctx = NULL;
222 }
223 return ctx;
224}
225
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000226SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000227 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
228 : INHERITED(shader, rec)
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000229 , fBitmapShader(SkRef(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000230{
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000231 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
232 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
233 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000234}
235
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000236SkPictureShader::PictureShaderContext::~PictureShaderContext() {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000237 if (fBitmapShaderContext) {
238 fBitmapShaderContext->~Context();
239 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000240 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000241}
242
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000243uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000244 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000245 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000246}
247
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000248SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000249 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000250 return fBitmapShaderContext->asAShadeProc(ctx);
251}
252
253void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
254 SkASSERT(fBitmapShaderContext);
255 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
256}
257
258void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
259 SkASSERT(fBitmapShaderContext);
260 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000261}
262
263#ifndef SK_IGNORE_TO_STRING
264void SkPictureShader::toString(SkString* str) const {
265 static const char* gTileModeName[SkShader::kTileModeCount] = {
266 "clamp", "repeat", "mirror"
267 };
268
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700269 str->appendf("PictureShader: [%f:%f:%f:%f] ",
270 fPicture ? fPicture->cullRect().fLeft : 0,
271 fPicture ? fPicture->cullRect().fTop : 0,
272 fPicture ? fPicture->cullRect().fRight : 0,
273 fPicture ? fPicture->cullRect().fBottom : 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000274
275 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
276
277 this->INHERITED::toString(str);
278}
279#endif
280
281#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -0700282bool SkPictureShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
283 const SkMatrix* localMatrix, GrColor* paintColor,
284 GrFragmentProcessor** fp) const {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000285 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000286 if (!bitmapShader) {
dandov9de5b512014-06-10 14:38:28 -0700287 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000288 }
joshualittb0a8a372014-09-23 09:50:21 -0700289 return bitmapShader->asFragmentProcessor(context, paint, NULL, paintColor, fp);
dandov9de5b512014-06-10 14:38:28 -0700290}
291#else
joshualittb0a8a372014-09-23 09:50:21 -0700292bool SkPictureShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
293 GrFragmentProcessor**) const {
dandov9de5b512014-06-10 14:38:28 -0700294 SkDEBUGFAIL("Should not call in GPU-less build");
295 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000296}
297#endif