blob: 2de8d4d4cb49669fff013535d5f9914b884556b2 [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.org5970f622014-05-12 20:42:21 +000059 m.setConcat(matrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000060 if (localM) {
61 m.preConcat(*localM);
62 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000063
64 // Use a rotation-invariant scale
65 SkPoint scale;
66 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
67 // Decomposition failed, use an approximation.
68 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
69 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
70 }
71 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height());
72
73 SkISize tileSize = scaledSize.toRound();
74 if (tileSize.isEmpty()) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000075 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000076 }
77
78 // The actual scale, compensating for rounding.
79 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(),
80 SkIntToScalar(tileSize.height()) / fPicture->height());
81
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000082 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
83
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000084 // TODO(fmalita): remove fCachedLocalMatrix from this key after getLocalMatrix is removed.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000085 if (!fCachedBitmapShader || tileScale != fCachedTileScale ||
86 this->getLocalMatrix() != fCachedLocalMatrix) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000087 SkBitmap bm;
88 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000089 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000090 }
91 bm.eraseColor(SK_ColorTRANSPARENT);
92
93 SkCanvas canvas(bm);
94 canvas.scale(tileScale.width(), tileScale.height());
robertphillips9b14f262014-06-04 05:40:44 -070095 canvas.drawPicture(fPicture);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000096
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000097 fCachedTileScale = tileScale;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000098 fCachedLocalMatrix = this->getLocalMatrix();
99
100 SkMatrix shaderMatrix = this->getLocalMatrix();
101 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000102 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000103 }
104
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000105 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
106 // Otherwise, the pointer may have been overwritten on a different thread before the object's
107 // ref count was incremented.
108 fCachedBitmapShader.get()->ref();
109 return fCachedBitmapShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000110}
111
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000112size_t SkPictureShader::contextSize() const {
113 return sizeof(PictureShaderContext);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000114}
115
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000116SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000117 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000118 if (NULL == bitmapShader.get()) {
119 return NULL;
120 }
commit-bot@chromium.orgf03f9ff2014-05-06 13:43:17 +0000121 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000122}
123
124/////////////////////////////////////////////////////////////////////////////////////////
125
126SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
127 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
128 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
129 (shader, rec, bitmapShader));
130 if (NULL == ctx->fBitmapShaderContext) {
131 ctx->~PictureShaderContext();
132 ctx = NULL;
133 }
134 return ctx;
135}
136
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000137SkPictureShader::PictureShaderContext::PictureShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000138 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
139 : INHERITED(shader, rec)
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000140 , fBitmapShader(SkRef(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000141{
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000142 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
143 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
144 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000145}
146
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000147SkPictureShader::PictureShaderContext::~PictureShaderContext() {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000148 if (fBitmapShaderContext) {
149 fBitmapShaderContext->~Context();
150 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000151 sk_free(fBitmapShaderContextStorage);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000152}
153
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000154uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000155 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000156 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000157}
158
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000159SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000160 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000161 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
dandov9de5b512014-06-10 14:38:28 -0700191bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
192 const SkMatrix* localMatrix, GrColor* grColor,
193 GrEffectRef** grEffect) const {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000194 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000195 if (!bitmapShader) {
dandov9de5b512014-06-10 14:38:28 -0700196 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000197 }
dandov9de5b512014-06-10 14:38:28 -0700198 return bitmapShader->asNewEffect(context, paint, NULL, grColor, grEffect);
199}
200#else
201bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
202 const SkMatrix* localMatrix, GrColor* grColor,
203 GrEffectRef** grEffect) const {
204 SkDEBUGFAIL("Should not call in GPU-less build");
205 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000206}
207#endif