2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkPictureShader.h"
11 #include "SkBitmapProcShader.h"
13 #include "SkMatrixUtils.h"
14 #include "SkPicture.h"
15 #include "SkReadBuffer.h"
18 #include "GrContext.h"
21 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMode tmy,
22 const SkMatrix* localMatrix, const SkRect* tile)
23 : INHERITED(localMatrix)
24 , fPicture(SkRef(picture))
27 fTile = tile ? *tile : SkRect::MakeWH(SkIntToScalar(picture->width()),
28 SkIntToScalar(picture->height()));
31 SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
33 fTmx = static_cast<SkShader::TileMode>(buffer.read32());
34 fTmy = static_cast<SkShader::TileMode>(buffer.read32());
35 buffer.readRect(&fTile);
36 fPicture = SkPicture::CreateFromBuffer(buffer);
39 SkPictureShader::~SkPictureShader() {
43 SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy,
44 const SkMatrix* localMatrix, const SkRect* tile) {
45 if (!picture || 0 == picture->width() || 0 == picture->height()
46 || (NULL != tile && tile->isEmpty())) {
49 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
52 void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
53 this->INHERITED::flatten(buffer);
57 buffer.writeRect(fTile);
58 fPicture->flatten(buffer);
61 SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const {
62 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
65 m.setConcat(matrix, this->getLocalMatrix());
70 // Use a rotation-invariant scale
72 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
73 // Decomposition failed, use an approximation.
74 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
75 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
77 SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTile.height());
79 // Clamp the tile size to about 16M pixels
80 static const SkScalar kMaxTileArea = 4096 * 4096;
81 SkScalar tileArea = SkScalarMul(scaledSize.width(), scaledSize.height());
82 if (tileArea > kMaxTileArea) {
83 SkScalar clampScale = SkScalarSqrt(SkScalarDiv(kMaxTileArea, tileArea));
84 scaledSize.set(SkScalarMul(scaledSize.width(), clampScale),
85 SkScalarMul(scaledSize.height(), clampScale));
88 SkISize tileSize = scaledSize.toRound();
89 if (tileSize.isEmpty()) {
93 // The actual scale, compensating for rounding & clamping.
94 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
95 SkIntToScalar(tileSize.height()) / fTile.height());
97 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
99 if (!fCachedBitmapShader || tileScale != fCachedTileScale) {
101 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
104 bm.eraseColor(SK_ColorTRANSPARENT);
107 canvas.scale(tileScale.width(), tileScale.height());
108 canvas.translate(fTile.x(), fTile.y());
109 canvas.drawPicture(fPicture);
111 fCachedTileScale = tileScale;
113 SkMatrix shaderMatrix = this->getLocalMatrix();
114 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
115 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
118 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
119 // Otherwise, the pointer may have been overwritten on a different thread before the object's
120 // ref count was incremented.
121 fCachedBitmapShader.get()->ref();
122 return fCachedBitmapShader;
125 size_t SkPictureShader::contextSize() const {
126 return sizeof(PictureShaderContext);
129 SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
130 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
131 if (NULL == bitmapShader.get()) {
134 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
137 /////////////////////////////////////////////////////////////////////////////////////////
139 SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
140 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) {
141 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext,
142 (shader, rec, bitmapShader));
143 if (NULL == ctx->fBitmapShaderContext) {
144 ctx->~PictureShaderContext();
150 SkPictureShader::PictureShaderContext::PictureShaderContext(
151 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
152 : INHERITED(shader, rec)
153 , fBitmapShader(SkRef(bitmapShader))
155 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
156 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
157 //if fBitmapShaderContext is null, we are invalid
160 SkPictureShader::PictureShaderContext::~PictureShaderContext() {
161 if (fBitmapShaderContext) {
162 fBitmapShaderContext->~Context();
164 sk_free(fBitmapShaderContextStorage);
167 uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
168 SkASSERT(fBitmapShaderContext);
169 return fBitmapShaderContext->getFlags();
172 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
173 SkASSERT(fBitmapShaderContext);
174 return fBitmapShaderContext->asAShadeProc(ctx);
177 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
178 SkASSERT(fBitmapShaderContext);
179 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
182 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
183 SkASSERT(fBitmapShaderContext);
184 fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
187 #ifndef SK_IGNORE_TO_STRING
188 void SkPictureShader::toString(SkString* str) const {
189 static const char* gTileModeName[SkShader::kTileModeCount] = {
190 "clamp", "repeat", "mirror"
193 str->appendf("PictureShader: [%d:%d] ",
194 fPicture ? fPicture->width() : 0,
195 fPicture ? fPicture->height() : 0);
197 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
199 this->INHERITED::toString(str);
204 bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
205 const SkMatrix* localMatrix, GrColor* paintColor,
206 GrEffect** effect) const {
207 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
211 return bitmapShader->asNewEffect(context, paint, NULL, paintColor, effect);
214 bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
215 const SkMatrix* localMatrix, GrColor* paintColor,
216 GrEffect** effect) const {
217 SkDEBUGFAIL("Should not call in GPU-less build");