blob: 9555624ffe3ff3a1a2d84e92ab1696dfb1348813 [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
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000021SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy,
22 const SkMatrix* localMatrix)
23 : INHERITED(localMatrix)
24 , fPicture(SkRef(picture))
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000025 , fTmx(tmx)
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000026 , fTmy(tmy) { }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000027
28SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
29 : INHERITED(buffer) {
30 fTmx = static_cast<SkShader::TileMode>(buffer.read32());
31 fTmy = static_cast<SkShader::TileMode>(buffer.read32());
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000032 fPicture = SkPicture::CreateFromBuffer(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000033}
34
35SkPictureShader::~SkPictureShader() {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000036 fPicture->unref();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000037}
38
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000039SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy,
40 const SkMatrix* localMatrix) {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000041 if (!picture || 0 == picture->width() || 0 == picture->height()) {
42 return NULL;
43 }
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000044 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000045}
46
47void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
48 this->INHERITED::flatten(buffer);
49
50 buffer.write32(fTmx);
51 buffer.write32(fTmy);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000052 fPicture->flatten(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000053}
54
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000055SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix) const {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000056 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000057
58 SkMatrix m;
59 if (this->hasLocalMatrix()) {
60 m.setConcat(matrix, this->getLocalMatrix());
61 } else {
62 m = matrix;
63 }
64
65 // Use a rotation-invariant scale
66 SkPoint scale;
67 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
68 // Decomposition failed, use an approximation.
69 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
70 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
71 }
72 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height());
73
74 SkISize tileSize = scaledSize.toRound();
75 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000076 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000077 }
78
79 // The actual scale, compensating for rounding.
80 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(),
81 SkIntToScalar(tileSize.height()) / fPicture->height());
82
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000083 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
84
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000085 // TODO(fmalita): remove fCachedLocalMatrix from this key after getLocalMatrix is removed.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000086 if (!fCachedBitmapShader || tileScale != fCachedTileScale ||
87 this->getLocalMatrix() != fCachedLocalMatrix) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000088 SkBitmap bm;
89 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000090 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000091 }
92 bm.eraseColor(SK_ColorTRANSPARENT);
93
94 SkCanvas canvas(bm);
95 canvas.scale(tileScale.width(), tileScale.height());
96 canvas.drawPicture(*fPicture);
97
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000098 fCachedTileScale = tileScale;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000099 fCachedLocalMatrix = this->getLocalMatrix();
100
101 SkMatrix shaderMatrix = this->getLocalMatrix();
102 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000103 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000104 }
105
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000106 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
107 // Otherwise, the pointer may have been overwritten on a different thread before the object's
108 // ref count was incremented.
109 fCachedBitmapShader.get()->ref();
110 return fCachedBitmapShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000111}
112
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000113size_t SkPictureShader::contextSize() const {
114 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000115}
116
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000117SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
118 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix));
119 if (NULL == bitmapShader.get()) {
120 return NULL;
121 }
122 return PictureShaderContext::Create(storage, *this, rec, bitmapShader.detach());
123}
124
125/////////////////////////////////////////////////////////////////////////////////////////
126
127SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
128 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
129 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
130 (shader, rec, bitmapShader));
131 if (NULL == ctx->fBitmapShaderContext) {
132 ctx->~PictureShaderContext();
133 ctx = NULL;
134 }
135 return ctx;
136}
137
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000138SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000139 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
140 : INHERITED(shader, rec)
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000141 , fBitmapShader(SkRef(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000142{
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000143 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
144 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
145 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000146}
147
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000148SkPictureShader::PictureShaderContext::~PictureShaderContext() {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000149 if (fBitmapShaderContext) {
150 fBitmapShaderContext->~Context();
151 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000152 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000153}
154
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000155uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000156 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000157 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000158}
159
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000160SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000161 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000162 return fBitmapShaderContext->asAShadeProc(ctx);
163}
164
165void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
166 SkASSERT(fBitmapShaderContext);
167 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
168}
169
170void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
171 SkASSERT(fBitmapShaderContext);
172 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000173}
174
175#ifndef SK_IGNORE_TO_STRING
176void SkPictureShader::toString(SkString* str) const {
177 static const char* gTileModeName[SkShader::kTileModeCount] = {
178 "clamp", "repeat", "mirror"
179 };
180
181 str->appendf("PictureShader: [%d:%d] ",
182 fPicture ? fPicture->width() : 0,
183 fPicture ? fPicture->height() : 0);
184
185 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
186
187 this->INHERITED::toString(str);
188}
189#endif
190
191#if SK_SUPPORT_GPU
192GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000193 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix()));
194 if (!bitmapShader) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000195 return NULL;
196 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000197 return bitmapShader->asNewEffect(context, paint);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000198}
199#endif