blob: 73ab170f119cd15948c74fa71b0cde1191b57e56 [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"
16
17#if SK_SUPPORT_GPU
18#include "GrContext.h"
19#endif
20
robertphillipse8464992014-07-14 07:53:26 -070021SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMode tmy,
fmalitab5f78262014-08-06 13:07:15 -070022 const SkMatrix* localMatrix, const SkRect* tile)
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000023 : INHERITED(localMatrix)
24 , fPicture(SkRef(picture))
robertphillipsa8d7f0b2014-08-29 08:03:56 -070025 , fTile(NULL != tile ? *tile : picture->cullRect())
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000026 , fTmx(tmx)
fmalitab5f78262014-08-06 13:07:15 -070027 , fTmy(tmy) {
fmalitab5f78262014-08-06 13:07:15 -070028}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000029
reed9fa60da2014-08-21 07:59:51 -070030#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
31SkPictureShader::SkPictureShader(SkReadBuffer& buffer) : INHERITED(buffer) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000032 fTmx = static_cast<SkShader::TileMode>(buffer.read32());
33 fTmy = static_cast<SkShader::TileMode>(buffer.read32());
fmalitab5f78262014-08-06 13:07:15 -070034 buffer.readRect(&fTile);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000035 fPicture = SkPicture::CreateFromBuffer(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000036}
reed9fa60da2014-08-21 07:59:51 -070037#endif
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000038
39SkPictureShader::~SkPictureShader() {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000040 fPicture->unref();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000041}
42
robertphillipse8464992014-07-14 07:53:26 -070043SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy,
fmalitab5f78262014-08-06 13:07:15 -070044 const SkMatrix* localMatrix, const SkRect* tile) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -070045 if (!picture || picture->cullRect().isEmpty() || (NULL != tile && tile->isEmpty())) {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000046 return NULL;
47 }
fmalitab5f78262014-08-06 13:07:15 -070048 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000049}
50
reed9fa60da2014-08-21 07:59:51 -070051SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) {
52 SkMatrix lm;
53 buffer.readMatrix(&lm);
54 TileMode mx = (TileMode)buffer.read32();
55 TileMode my = (TileMode)buffer.read32();
56 SkRect tile;
57 buffer.readRect(&tile);
58 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromBuffer(buffer));
59 return SkPictureShader::Create(picture, mx, my, &lm, &tile);
60}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000061
reed9fa60da2014-08-21 07:59:51 -070062void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
63 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000064 buffer.write32(fTmx);
65 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -070066 buffer.writeRect(fTile);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000067 fPicture->flatten(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000068}
69
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000070SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -070071 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000072
73 SkMatrix m;
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000074 m.setConcat(matrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000075 if (localM) {
76 m.preConcat(*localM);
77 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000078
79 // Use a rotation-invariant scale
80 SkPoint scale;
81 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
82 // Decomposition failed, use an approximation.
83 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
84 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
85 }
fmalitab5f78262014-08-06 13:07:15 -070086 SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000087
fmalitabb204f42014-08-07 08:39:24 -070088 // Clamp the tile size to about 16M pixels
89 static const SkScalar kMaxTileArea = 4096 * 4096;
90 SkScalar tileArea = SkScalarMul(scaledSize.width(), scaledSize.height());
91 if (tileArea > kMaxTileArea) {
92 SkScalar clampScale = SkScalarSqrt(SkScalarDiv(kMaxTileArea, tileArea));
93 scaledSize.set(SkScalarMul(scaledSize.width(), clampScale),
94 SkScalarMul(scaledSize.height(), clampScale));
95 }
96
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000097 SkISize tileSize = scaledSize.toRound();
98 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000099 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000100 }
101
fmalitabb204f42014-08-07 08:39:24 -0700102 // The actual scale, compensating for rounding & clamping.
fmalitab5f78262014-08-06 13:07:15 -0700103 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
104 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000105
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000106 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
107
scroggo1b0aa002014-07-11 12:19:00 -0700108 if (!fCachedBitmapShader || tileScale != fCachedTileScale) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000109 SkBitmap bm;
110 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000111 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000112 }
113 bm.eraseColor(SK_ColorTRANSPARENT);
114
115 SkCanvas canvas(bm);
116 canvas.scale(tileScale.width(), tileScale.height());
fmalitab5f78262014-08-06 13:07:15 -0700117 canvas.translate(fTile.x(), fTile.y());
robertphillips9b14f262014-06-04 05:40:44 -0700118 canvas.drawPicture(fPicture);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000119
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000120 fCachedTileScale = tileScale;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000121
122 SkMatrix shaderMatrix = this->getLocalMatrix();
123 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000124 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000125 }
126
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000127 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
128 // Otherwise, the pointer may have been overwritten on a different thread before the object's
129 // ref count was incremented.
130 fCachedBitmapShader.get()->ref();
131 return fCachedBitmapShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000132}
133
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000134size_t SkPictureShader::contextSize() const {
135 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000136}
137
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000138SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000139 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000140 if (NULL == bitmapShader.get()) {
141 return NULL;
142 }
commit-bot@chromium.orgf03f9ff2014-05-06 13:43:17 +0000143 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000144}
145
146/////////////////////////////////////////////////////////////////////////////////////////
147
148SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
149 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
150 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
151 (shader, rec, bitmapShader));
152 if (NULL == ctx->fBitmapShaderContext) {
153 ctx->~PictureShaderContext();
154 ctx = NULL;
155 }
156 return ctx;
157}
158
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000159SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000160 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
161 : INHERITED(shader, rec)
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000162 , fBitmapShader(SkRef(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000163{
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000164 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
165 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
166 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000167}
168
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000169SkPictureShader::PictureShaderContext::~PictureShaderContext() {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000170 if (fBitmapShaderContext) {
171 fBitmapShaderContext->~Context();
172 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000173 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000174}
175
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000176uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000177 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000178 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000179}
180
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000181SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000182 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000183 return fBitmapShaderContext->asAShadeProc(ctx);
184}
185
186void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
187 SkASSERT(fBitmapShaderContext);
188 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
189}
190
191void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
192 SkASSERT(fBitmapShaderContext);
193 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000194}
195
196#ifndef SK_IGNORE_TO_STRING
197void SkPictureShader::toString(SkString* str) const {
198 static const char* gTileModeName[SkShader::kTileModeCount] = {
199 "clamp", "repeat", "mirror"
200 };
201
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700202 str->appendf("PictureShader: [%f:%f:%f:%f] ",
203 fPicture ? fPicture->cullRect().fLeft : 0,
204 fPicture ? fPicture->cullRect().fTop : 0,
205 fPicture ? fPicture->cullRect().fRight : 0,
206 fPicture ? fPicture->cullRect().fBottom : 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000207
208 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
209
210 this->INHERITED::toString(str);
211}
212#endif
213
214#if SK_SUPPORT_GPU
dandov9de5b512014-06-10 14:38:28 -0700215bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700216 const SkMatrix* localMatrix, GrColor* paintColor,
217 GrEffect** effect) const {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000218 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000219 if (!bitmapShader) {
dandov9de5b512014-06-10 14:38:28 -0700220 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000221 }
bsalomon83d081a2014-07-08 09:56:10 -0700222 return bitmapShader->asNewEffect(context, paint, NULL, paintColor, effect);
dandov9de5b512014-06-10 14:38:28 -0700223}
224#else
225bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700226 const SkMatrix* localMatrix, GrColor* paintColor,
227 GrEffect** effect) const {
dandov9de5b512014-06-10 14:38:28 -0700228 SkDEBUGFAIL("Should not call in GPU-less build");
229 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000230}
231#endif