Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkPictureShader.cpp
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
21 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMode tmy,
22                                  const SkMatrix* localMatrix, const SkRect* tile)
23     : INHERITED(localMatrix)
24     , fPicture(SkRef(picture))
25     , fTmx(tmx)
26     , fTmy(tmy) {
27     fTile = tile ? *tile : SkRect::MakeWH(SkIntToScalar(picture->width()),
28                                           SkIntToScalar(picture->height()));
29 }
30
31 SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
32         : INHERITED(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);
37 }
38
39 SkPictureShader::~SkPictureShader() {
40     fPicture->unref();
41 }
42
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())) {
47         return NULL;
48     }
49     return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
50 }
51
52 void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
53     this->INHERITED::flatten(buffer);
54
55     buffer.write32(fTmx);
56     buffer.write32(fTmy);
57     buffer.writeRect(fTile);
58     fPicture->flatten(buffer);
59 }
60
61 SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const {
62     SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
63
64     SkMatrix m;
65     m.setConcat(matrix, this->getLocalMatrix());
66     if (localM) {
67         m.preConcat(*localM);
68     }
69
70     // Use a rotation-invariant scale
71     SkPoint 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()));
76     }
77     SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTile.height());
78
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));
86     }
87
88     SkISize tileSize = scaledSize.toRound();
89     if (tileSize.isEmpty()) {
90         return NULL;
91     }
92
93     // The actual scale, compensating for rounding & clamping.
94     SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
95                                     SkIntToScalar(tileSize.height()) / fTile.height());
96
97     SkAutoMutexAcquire ama(fCachedBitmapShaderMutex);
98
99     if (!fCachedBitmapShader || tileScale != fCachedTileScale) {
100         SkBitmap bm;
101         if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
102             return NULL;
103         }
104         bm.eraseColor(SK_ColorTRANSPARENT);
105
106         SkCanvas canvas(bm);
107         canvas.scale(tileScale.width(), tileScale.height());
108         canvas.translate(fTile.x(), fTile.y());
109         canvas.drawPicture(fPicture);
110
111         fCachedTileScale = tileScale;
112
113         SkMatrix shaderMatrix = this->getLocalMatrix();
114         shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
115         fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
116     }
117
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;
123 }
124
125 size_t SkPictureShader::contextSize() const {
126     return sizeof(PictureShaderContext);
127 }
128
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()) {
132         return NULL;
133     }
134     return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
135 }
136
137 /////////////////////////////////////////////////////////////////////////////////////////
138
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();
145         ctx = NULL;
146     }
147     return ctx;
148 }
149
150 SkPictureShader::PictureShaderContext::PictureShaderContext(
151         const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader)
152     : INHERITED(shader, rec)
153     , fBitmapShader(SkRef(bitmapShader))
154 {
155     fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize());
156     fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage);
157     //if fBitmapShaderContext is null, we are invalid
158 }
159
160 SkPictureShader::PictureShaderContext::~PictureShaderContext() {
161     if (fBitmapShaderContext) {
162         fBitmapShaderContext->~Context();
163     }
164     sk_free(fBitmapShaderContextStorage);
165 }
166
167 uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
168     SkASSERT(fBitmapShaderContext);
169     return fBitmapShaderContext->getFlags();
170 }
171
172 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
173     SkASSERT(fBitmapShaderContext);
174     return fBitmapShaderContext->asAShadeProc(ctx);
175 }
176
177 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
178     SkASSERT(fBitmapShaderContext);
179     fBitmapShaderContext->shadeSpan(x, y, dstC, count);
180 }
181
182 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
183     SkASSERT(fBitmapShaderContext);
184     fBitmapShaderContext->shadeSpan16(x, y, dstC, count);
185 }
186
187 #ifndef SK_IGNORE_TO_STRING
188 void SkPictureShader::toString(SkString* str) const {
189     static const char* gTileModeName[SkShader::kTileModeCount] = {
190         "clamp", "repeat", "mirror"
191     };
192
193     str->appendf("PictureShader: [%d:%d] ",
194                  fPicture ? fPicture->width() : 0,
195                  fPicture ? fPicture->height() : 0);
196
197     str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
198
199     this->INHERITED::toString(str);
200 }
201 #endif
202
203 #if SK_SUPPORT_GPU
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));
208     if (!bitmapShader) {
209         return false;
210     }
211     return bitmapShader->asNewEffect(context, paint, NULL, paintColor, effect);
212 }
213 #else
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");
218     return false;
219 }
220 #endif