blob: 27cbb00f63d0bd0ad0a9feaea43a75e97783c092 [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
21SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy)
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000022 : fPicture(SkRef(picture))
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000023 , fTmx(tmx)
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000024 , fTmy(tmy) { }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000025
26SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
27 : INHERITED(buffer) {
28 fTmx = static_cast<SkShader::TileMode>(buffer.read32());
29 fTmy = static_cast<SkShader::TileMode>(buffer.read32());
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000030 fPicture = SkPicture::CreateFromBuffer(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000031}
32
33SkPictureShader::~SkPictureShader() {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000034 fPicture->unref();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000035}
36
37SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy) {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000038 if (!picture || 0 == picture->width() || 0 == picture->height()) {
39 return NULL;
40 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000041 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy));
42}
43
44void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
45 this->INHERITED::flatten(buffer);
46
47 buffer.write32(fTmx);
48 buffer.write32(fTmy);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000049 fPicture->flatten(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000050}
51
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000052SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix) const {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000053 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000054
55 SkMatrix m;
56 if (this->hasLocalMatrix()) {
57 m.setConcat(matrix, this->getLocalMatrix());
58 } else {
59 m = matrix;
60 }
61
62 // Use a rotation-invariant scale
63 SkPoint scale;
64 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
65 // Decomposition failed, use an approximation.
66 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
67 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
68 }
69 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height());
70
71 SkISize tileSize = scaledSize.toRound();
72 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000073 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000074 }
75
76 // The actual scale, compensating for rounding.
77 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(),
78 SkIntToScalar(tileSize.height()) / fPicture->height());
79
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000080 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
81
82 if (!fCachedBitmapShader || tileScale != fCachedTileScale ||
83 this->getLocalMatrix() != fCachedLocalMatrix) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000084 SkBitmap bm;
85 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000086 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000087 }
88 bm.eraseColor(SK_ColorTRANSPARENT);
89
90 SkCanvas canvas(bm);
91 canvas.scale(tileScale.width(), tileScale.height());
92 canvas.drawPicture(*fPicture);
93
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000094 fCachedTileScale = tileScale;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000095 fCachedLocalMatrix = this->getLocalMatrix();
96
97 SkMatrix shaderMatrix = this->getLocalMatrix();
98 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000099 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000100 }
101
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000102 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
103 // Otherwise, the pointer may have been overwritten on a different thread before the object's
104 // ref count was incremented.
105 fCachedBitmapShader.get()->ref();
106 return fCachedBitmapShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000107}
108
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000109SkShader* SkPictureShader::validInternal(const ContextRec& rec, SkMatrix* totalInverse) const {
110 if (!this->INHERITED::validContext(rec, totalInverse)) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000111 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000112 }
113
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000114 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix));
115 if (!bitmapShader || !bitmapShader->validContext(rec)) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000116 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000117 }
118
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000119 return bitmapShader.detach();
120}
121
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000122bool SkPictureShader::validContext(const ContextRec& rec, SkMatrix* totalInverse) const {
123 SkAutoTUnref<SkShader> shader(this->validInternal(rec, totalInverse));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000124 return shader != NULL;
125}
126
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000127SkShader::Context* SkPictureShader::createContext(const ContextRec& rec, void* storage) const {
128 SkAutoTUnref<SkShader> bitmapShader(this->validInternal(rec, NULL));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000129 if (!bitmapShader) {
130 return NULL;
commit-bot@chromium.org68ea91a2014-04-17 21:02:29 +0000131 }
132
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000133 return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, (*this, rec, bitmapShader.detach()));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000134}
135
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000136size_t SkPictureShader::contextSize() const {
137 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000138}
139
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000140SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000141 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
142 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000143 , fBitmapShader(bitmapShader)
144{
145 SkASSERT(fBitmapShader);
146 fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize());
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000147 fBitmapShaderContext = fBitmapShader->createContext(rec, fBitmapShaderContextStorage);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000148 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000149}
150
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000151SkPictureShader::PictureShaderContext::~PictureShaderContext() {
152 fBitmapShaderContext->~Context();
153 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000154}
155
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000156uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
157 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) {
161 return fBitmapShaderContext->asAShadeProc(ctx);
162}
163
164void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
165 SkASSERT(fBitmapShaderContext);
166 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
167}
168
169void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
170 SkASSERT(fBitmapShaderContext);
171 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000172}
173
174#ifndef SK_IGNORE_TO_STRING
175void SkPictureShader::toString(SkString* str) const {
176 static const char* gTileModeName[SkShader::kTileModeCount] = {
177 "clamp", "repeat", "mirror"
178 };
179
180 str->appendf("PictureShader: [%d:%d] ",
181 fPicture ? fPicture->width() : 0,
182 fPicture ? fPicture->height() : 0);
183
184 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
185
186 this->INHERITED::toString(str);
187}
188#endif
189
190#if SK_SUPPORT_GPU
191GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000192 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix()));
193 if (!bitmapShader) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000194 return NULL;
195 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000196 return bitmapShader->asNewEffect(context, paint);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000197}
198#endif