blob: 300a65329be41c719b67bb0b15aa8b0995e4de72 [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.org80116dc2014-05-06 17:16:03 +000055SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) 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;
commit-bot@chromium.orgd12de022014-05-09 15:42:07 +000059 if (this->hasLocalMatrix()) {
60 m.setConcat(matrix, this->getLocalMatrix());
61 } else {
62 m = matrix;
63 }
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000064 if (localM) {
65 m.preConcat(*localM);
66 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000067
68 // Use a rotation-invariant scale
69 SkPoint scale;
70 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
71 // Decomposition failed, use an approximation.
72 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
73 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
74 }
75 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height());
76
77 SkISize tileSize = scaledSize.toRound();
78 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000079 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000080 }
81
82 // The actual scale, compensating for rounding.
83 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(),
84 SkIntToScalar(tileSize.height()) / fPicture->height());
85
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000086 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
87
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000088 // TODO(fmalita): remove fCachedLocalMatrix from this key after getLocalMatrix is removed.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000089 if (!fCachedBitmapShader || tileScale != fCachedTileScale ||
90 this->getLocalMatrix() != fCachedLocalMatrix) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000091 SkBitmap bm;
92 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000093 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000094 }
95 bm.eraseColor(SK_ColorTRANSPARENT);
96
97 SkCanvas canvas(bm);
98 canvas.scale(tileScale.width(), tileScale.height());
99 canvas.drawPicture(*fPicture);
100
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000101 fCachedTileScale = tileScale;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000102 fCachedLocalMatrix = this->getLocalMatrix();
103
104 SkMatrix shaderMatrix = this->getLocalMatrix();
105 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000106 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000107 }
108
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000109 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
110 // Otherwise, the pointer may have been overwritten on a different thread before the object's
111 // ref count was incremented.
112 fCachedBitmapShader.get()->ref();
113 return fCachedBitmapShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000114}
115
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000116size_t SkPictureShader::contextSize() const {
117 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000118}
119
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000120SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000121 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000122 if (NULL == bitmapShader.get()) {
123 return NULL;
124 }
commit-bot@chromium.orgf03f9ff2014-05-06 13:43:17 +0000125 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000126}
127
128/////////////////////////////////////////////////////////////////////////////////////////
129
130SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
131 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
132 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
133 (shader, rec, bitmapShader));
134 if (NULL == ctx->fBitmapShaderContext) {
135 ctx->~PictureShaderContext();
136 ctx = NULL;
137 }
138 return ctx;
139}
140
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000141SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000142 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
143 : INHERITED(shader, rec)
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000144 , fBitmapShader(SkRef(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000145{
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000146 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
147 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
148 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000149}
150
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000151SkPictureShader::PictureShaderContext::~PictureShaderContext() {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000152 if (fBitmapShaderContext) {
153 fBitmapShaderContext->~Context();
154 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000155 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000156}
157
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000158uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000159 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000160 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000161}
162
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000163SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000164 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000165 return fBitmapShaderContext->asAShadeProc(ctx);
166}
167
168void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
169 SkASSERT(fBitmapShaderContext);
170 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
171}
172
173void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
174 SkASSERT(fBitmapShaderContext);
175 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000176}
177
178#ifndef SK_IGNORE_TO_STRING
179void SkPictureShader::toString(SkString* str) const {
180 static const char* gTileModeName[SkShader::kTileModeCount] = {
181 "clamp", "repeat", "mirror"
182 };
183
184 str->appendf("PictureShader: [%d:%d] ",
185 fPicture ? fPicture->width() : 0,
186 fPicture ? fPicture->height() : 0);
187
188 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
189
190 this->INHERITED::toString(str);
191}
192#endif
193
194#if SK_SUPPORT_GPU
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000195GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
196 const SkMatrix* localMatrix) const {
197 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000198 if (!bitmapShader) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000199 return NULL;
200 }
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000201 return bitmapShader->asNewEffect(context, paint, NULL);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000202}
203#endif