SkPictureShader.cpp revision bc2f1dc85e458af7bdb87873e60207f9f7299e4a
1/*
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)
22    : fPicture(SkRef(picture))
23    , fTmx(tmx)
24    , fTmy(tmy) { }
25
26SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
27        : INHERITED(buffer) {
28    fTmx = static_cast<SkShader::TileMode>(buffer.read32());
29    fTmy = static_cast<SkShader::TileMode>(buffer.read32());
30    fPicture = SkPicture::CreateFromBuffer(buffer);
31}
32
33SkPictureShader::~SkPictureShader() {
34    fPicture->unref();
35}
36
37SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy) {
38    if (!picture || 0 == picture->width() || 0 == picture->height()) {
39        return NULL;
40    }
41    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);
49    fPicture->flatten(buffer);
50}
51
52SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix) const {
53    SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
54
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()) {
73        return NULL;
74    }
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
80    SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
81
82    if (!fCachedBitmapShader || tileScale != fCachedTileScale ||
83        this->getLocalMatrix() != fCachedLocalMatrix) {
84        SkBitmap bm;
85        if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
86            return NULL;
87        }
88        bm.eraseColor(SK_ColorTRANSPARENT);
89
90        SkCanvas canvas(bm);
91        canvas.scale(tileScale.width(), tileScale.height());
92        canvas.drawPicture(*fPicture);
93
94        fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy));
95        fCachedTileScale = tileScale;
96        fCachedLocalMatrix = this->getLocalMatrix();
97
98        SkMatrix shaderMatrix = this->getLocalMatrix();
99        shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
100        fCachedBitmapShader->setLocalMatrix(shaderMatrix);
101    }
102
103    // 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;
108}
109
110SkShader* 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;
114    }
115
116    SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(matrix));
117    if (!bitmapShader || !bitmapShader->validContext(device, paint, matrix)) {
118        return NULL;
119    }
120
121    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;
135    }
136
137    return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
138                                (*this, device, paint, matrix, bitmapShader.detach()));
139}
140
141size_t SkPictureShader::contextSize() const {
142    return sizeof(PictureShaderContext);
143}
144
145SkPictureShader::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);
156}
157
158SkPictureShader::PictureShaderContext::~PictureShaderContext() {
159    fBitmapShaderContext->~Context();
160    sk_free(fBitmapShaderContextStorage);
161}
162
163uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
164    return fBitmapShaderContext->getFlags();
165}
166
167SkShader::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);
179}
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 {
199    SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix()));
200    if (!bitmapShader) {
201        return NULL;
202    }
203    return bitmapShader->asNewEffect(context, paint);
204}
205#endif
206