• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SkPictureShader(SkPicture * picture,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix)21 SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy,
22                                  const SkMatrix* localMatrix)
23     : INHERITED(localMatrix)
24     , fPicture(SkRef(picture))
25     , fTmx(tmx)
26     , fTmy(tmy) { }
27 
SkPictureShader(SkReadBuffer & buffer)28 SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
29         : INHERITED(buffer) {
30     fTmx = static_cast<SkShader::TileMode>(buffer.read32());
31     fTmy = static_cast<SkShader::TileMode>(buffer.read32());
32     fPicture = SkPicture::CreateFromBuffer(buffer);
33 }
34 
~SkPictureShader()35 SkPictureShader::~SkPictureShader() {
36     fPicture->unref();
37 }
38 
Create(SkPicture * picture,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix)39 SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy,
40                                          const SkMatrix* localMatrix) {
41     if (!picture || 0 == picture->width() || 0 == picture->height()) {
42         return NULL;
43     }
44     return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix));
45 }
46 
flatten(SkWriteBuffer & buffer) const47 void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
48     this->INHERITED::flatten(buffer);
49 
50     buffer.write32(fTmx);
51     buffer.write32(fTmy);
52     fPicture->flatten(buffer);
53 }
54 
refBitmapShader(const SkMatrix & matrix,const SkMatrix * localM) const55 SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const {
56     SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
57 
58     SkMatrix m;
59     m.setConcat(matrix, this->getLocalMatrix());
60     if (localM) {
61         m.preConcat(*localM);
62     }
63 
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()) {
75         return NULL;
76     }
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 
82     SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
83 
84     // TODO(fmalita): remove fCachedLocalMatrix from this key after getLocalMatrix is removed.
85     if (!fCachedBitmapShader || tileScale != fCachedTileScale ||
86         this->getLocalMatrix() != fCachedLocalMatrix) {
87         SkBitmap bm;
88         if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
89             return NULL;
90         }
91         bm.eraseColor(SK_ColorTRANSPARENT);
92 
93         SkCanvas canvas(bm);
94         canvas.scale(tileScale.width(), tileScale.height());
95         canvas.drawPicture(fPicture);
96 
97         fCachedTileScale = tileScale;
98         fCachedLocalMatrix = this->getLocalMatrix();
99 
100         SkMatrix shaderMatrix = this->getLocalMatrix();
101         shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
102         fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
103     }
104 
105     // 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;
110 }
111 
contextSize() const112 size_t SkPictureShader::contextSize() const {
113     return sizeof(PictureShaderContext);
114 }
115 
onCreateContext(const ContextRec & rec,void * storage) const116 SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
117     SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
118     if (NULL == bitmapShader.get()) {
119         return NULL;
120     }
121     return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
122 }
123 
124 /////////////////////////////////////////////////////////////////////////////////////////
125 
Create(void * storage,const SkPictureShader & shader,const ContextRec & rec,SkShader * bitmapShader)126 SkShader::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 
PictureShaderContext(const SkPictureShader & shader,const ContextRec & rec,SkShader * bitmapShader)137 SkPictureShader::PictureShaderContext::PictureShaderContext(
138         const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
139     : INHERITED(shader, rec)
140     , fBitmapShader(SkRef(bitmapShader))
141 {
142     fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
143     fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
144     //if fBitmapShaderContext is null, we are invalid
145 }
146 
~PictureShaderContext()147 SkPictureShader::PictureShaderContext::~PictureShaderContext() {
148     if (fBitmapShaderContext) {
149         fBitmapShaderContext->~Context();
150     }
151     sk_free(fBitmapShaderContextStorage);
152 }
153 
getFlags() const154 uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
155     SkASSERT(fBitmapShaderContext);
156     return fBitmapShaderContext->getFlags();
157 }
158 
asAShadeProc(void ** ctx)159 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
160     SkASSERT(fBitmapShaderContext);
161     return fBitmapShaderContext->asAShadeProc(ctx);
162 }
163 
shadeSpan(int x,int y,SkPMColor dstC[],int count)164 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
165     SkASSERT(fBitmapShaderContext);
166     fBitmapShaderContext->shadeSpan(x, y, dstC, count);
167 }
168 
shadeSpan16(int x,int y,uint16_t dstC[],int count)169 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
170     SkASSERT(fBitmapShaderContext);
171     fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
172 }
173 
174 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const175 void 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
asNewEffect(GrContext * context,const SkPaint & paint,const SkMatrix * localMatrix,GrColor * grColor,GrEffectRef ** grEffect) const191 bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint,
192                                   const SkMatrix* localMatrix, GrColor* grColor,
193                                   GrEffectRef** grEffect) const {
194     SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix));
195     if (!bitmapShader) {
196         return false;
197     }
198     return bitmapShader->asNewEffect(context, paint, NULL, grColor, grEffect);
199 }
200 #else
asNewEffect(GrContext * context,const SkPaint & paint,const SkMatrix * localMatrix,GrColor * grColor,GrEffectRef ** grEffect) const201 bool 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;
206 }
207 #endif
208