blob: dc5c90b62e8ec7eb14fc31413f2f3d78ec1afabe [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.org87fcd952014-04-23 19:10:51 +000094 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000095 fCachedTileScale = tileScale;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000096 fCachedLocalMatrix = this->getLocalMatrix();
97
98 SkMatrix shaderMatrix = this->getLocalMatrix();
99 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
100 fCachedBitmapShader->setLocalMatrix(shaderMatrix);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000101 }
102
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000103 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
104 // Otherwise, the pointer may have been overwritten on a different thread before the object's
105 // ref count was incremented.
106 fCachedBitmapShader.get()->ref();
107 return fCachedBitmapShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000108}
109
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000110SkShader* SkPictureShader::validInternal(const SkBitmap& device, const SkPaint& paint,
111 const SkMatrix& matrix, SkMatrix* totalInverse) const {
112 if (!this->INHERITED::validContext(device, paint, matrix, totalInverse)) {
113 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000114 }
115
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000116 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(matrix));
117 if (!bitmapShader || !bitmapShader->validContext(device, paint, matrix)) {
118 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000119 }
120
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000121 return bitmapShader.detach();
122}
123
124bool SkPictureShader::validContext(const SkBitmap& device, const SkPaint& paint,
125 const SkMatrix& matrix, SkMatrix* totalInverse) const {
126 SkAutoTUnref<SkShader> shader(this->validInternal(device, paint, matrix, totalInverse));
127 return shader != NULL;
128}
129
130SkShader::Context* SkPictureShader::createContext(const SkBitmap& device, const SkPaint& paint,
131 const SkMatrix& matrix, void* storage) const {
132 SkAutoTUnref<SkShader> bitmapShader(this->validInternal(device, paint, matrix, NULL));
133 if (!bitmapShader) {
134 return NULL;
commit-bot@chromium.org68ea91a2014-04-17 21:02:29 +0000135 }
136
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000137 return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
138 (*this, device, paint, matrix, bitmapShader.detach()));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000139}
140
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000141size_t SkPictureShader::contextSize() const {
142 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000143}
144
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000145SkPictureShader::PictureShaderContext::PictureShaderContext(
146 const SkPictureShader& shader, const SkBitmap& device,
147 const SkPaint& paint, const SkMatrix& matrix, SkShader* bitmapShader)
148 : INHERITED(shader, device, paint, matrix)
149 , fBitmapShader(bitmapShader)
150{
151 SkASSERT(fBitmapShader);
152 fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize());
153 fBitmapShaderContext = fBitmapShader->createContext(
154 device, paint, matrix, fBitmapShaderContextStorage);
155 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000156}
157
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000158SkPictureShader::PictureShaderContext::~PictureShaderContext() {
159 fBitmapShaderContext->~Context();
160 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000161}
162
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000163uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
164 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000165}
166
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000167SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
168 return fBitmapShaderContext->asAShadeProc(ctx);
169}
170
171void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
172 SkASSERT(fBitmapShaderContext);
173 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
174}
175
176void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
177 SkASSERT(fBitmapShaderContext);
178 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000179}
180
181#ifndef SK_IGNORE_TO_STRING
182void SkPictureShader::toString(SkString* str) const {
183 static const char* gTileModeName[SkShader::kTileModeCount] = {
184 "clamp", "repeat", "mirror"
185 };
186
187 str->appendf("PictureShader: [%d:%d] ",
188 fPicture ? fPicture->width() : 0,
189 fPicture ? fPicture->height() : 0);
190
191 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
192
193 this->INHERITED::toString(str);
194}
195#endif
196
197#if SK_SUPPORT_GPU
198GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000199 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix()));
200 if (!bitmapShader) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000201 return NULL;
202 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000203 return bitmapShader->asNewEffect(context, paint);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000204}
205#endif