blob: 65a2cd322291d2236754e3c965a81738d17aac1a [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))
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000025 , fTmx(tmx)
fmalitab5f78262014-08-06 13:07:15 -070026 , fTmy(tmy) {
27 fTile = tile ? *tile : SkRect::MakeWH(SkIntToScalar(picture->width()),
28 SkIntToScalar(picture->height()));
29}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000030
31SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
32 : INHERITED(buffer) {
33 fTmx = static_cast<SkShader::TileMode>(buffer.read32());
34 fTmy = static_cast<SkShader::TileMode>(buffer.read32());
fmalitab5f78262014-08-06 13:07:15 -070035 buffer.readRect(&fTile);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000036 fPicture = SkPicture::CreateFromBuffer(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000037}
38
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) {
45 if (!picture || 0 == picture->width() || 0 == picture->height()
46 || (NULL != tile && tile->isEmpty())) {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000047 return NULL;
48 }
fmalitab5f78262014-08-06 13:07:15 -070049 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000050}
51
52void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
53 this->INHERITED::flatten(buffer);
54
55 buffer.write32(fTmx);
56 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -070057 buffer.writeRect(fTile);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000058 fPicture->flatten(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000059}
60
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000061SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000062 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000063
64 SkMatrix m;
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000065 m.setConcat(matrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000066 if (localM) {
67 m.preConcat(*localM);
68 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000069
70 // Use a rotation-invariant scale
71 SkPoint scale;
72 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
73 // Decomposition failed, use an approximation.
74 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
75 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
76 }
fmalitab5f78262014-08-06 13:07:15 -070077 SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000078
fmalitabb204f42014-08-07 08:39:24 -070079 // Clamp the tile size to about 16M pixels
80 static const SkScalar kMaxTileArea = 4096 * 4096;
81 SkScalar tileArea = SkScalarMul(scaledSize.width(), scaledSize.height());
82 if (tileArea > kMaxTileArea) {
83 SkScalar clampScale = SkScalarSqrt(SkScalarDiv(kMaxTileArea, tileArea));
84 scaledSize.set(SkScalarMul(scaledSize.width(), clampScale),
85 SkScalarMul(scaledSize.height(), clampScale));
86 }
87
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000088 SkISize tileSize = scaledSize.toRound();
89 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000090 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000091 }
92
fmalitabb204f42014-08-07 08:39:24 -070093 // The actual scale, compensating for rounding & clamping.
fmalitab5f78262014-08-06 13:07:15 -070094 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
95 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000096
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000097 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
98
scroggo1b0aa002014-07-11 12:19:00 -070099 if (!fCachedBitmapShader || tileScale != fCachedTileScale) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000100 SkBitmap bm;
101 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000102 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000103 }
104 bm.eraseColor(SK_ColorTRANSPARENT);
105
106 SkCanvas canvas(bm);
107 canvas.scale(tileScale.width(), tileScale.height());
fmalitab5f78262014-08-06 13:07:15 -0700108 canvas.translate(fTile.x(), fTile.y());
robertphillips9b14f262014-06-04 05:40:44 -0700109 canvas.drawPicture(fPicture);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000110
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000111 fCachedTileScale = tileScale;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000112
113 SkMatrix shaderMatrix = this->getLocalMatrix();
114 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000115 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000116 }
117
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000118 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
119 // Otherwise, the pointer may have been overwritten on a different thread before the object's
120 // ref count was incremented.
121 fCachedBitmapShader.get()->ref();
122 return fCachedBitmapShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000123}
124
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000125size_t SkPictureShader::contextSize() const {
126 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000127}
128
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000129SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000130 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000131 if (NULL == bitmapShader.get()) {
132 return NULL;
133 }
commit-bot@chromium.orgf03f9ff2014-05-06 13:43:17 +0000134 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000135}
136
137/////////////////////////////////////////////////////////////////////////////////////////
138
139SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
140 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
141 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
142 (shader, rec, bitmapShader));
143 if (NULL == ctx->fBitmapShaderContext) {
144 ctx->~PictureShaderContext();
145 ctx = NULL;
146 }
147 return ctx;
148}
149
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000150SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000151 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
152 : INHERITED(shader, rec)
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000153 , fBitmapShader(SkRef(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000154{
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000155 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
156 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
157 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000158}
159
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000160SkPictureShader::PictureShaderContext::~PictureShaderContext() {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000161 if (fBitmapShaderContext) {
162 fBitmapShaderContext->~Context();
163 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000164 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000165}
166
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000167uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000168 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000169 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000170}
171
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000172SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000173 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000174 return fBitmapShaderContext->asAShadeProc(ctx);
175}
176
177void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
178 SkASSERT(fBitmapShaderContext);
179 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
180}
181
182void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
183 SkASSERT(fBitmapShaderContext);
184 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000185}
186
187#ifndef SK_IGNORE_TO_STRING
188void SkPictureShader::toString(SkString* str) const {
189 static const char* gTileModeName[SkShader::kTileModeCount] = {
190 "clamp", "repeat", "mirror"
191 };
192
193 str->appendf("PictureShader: [%d:%d] ",
194 fPicture ? fPicture->width() : 0,
195 fPicture ? fPicture->height() : 0);
196
197 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
198
199 this->INHERITED::toString(str);
200}
201#endif
202
203#if SK_SUPPORT_GPU
dandov9de5b512014-06-10 14:38:28 -0700204bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700205 const SkMatrix* localMatrix, GrColor* paintColor,
206 GrEffect** effect) const {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000207 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000208 if (!bitmapShader) {
dandov9de5b512014-06-10 14:38:28 -0700209 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000210 }
bsalomon83d081a2014-07-08 09:56:10 -0700211 return bitmapShader->asNewEffect(context, paint, NULL, paintColor, effect);
dandov9de5b512014-06-10 14:38:28 -0700212}
213#else
214bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700215 const SkMatrix* localMatrix, GrColor* paintColor,
216 GrEffect** effect) const {
dandov9de5b512014-06-10 14:38:28 -0700217 SkDEBUGFAIL("Should not call in GPU-less build");
218 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000219}
220#endif