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 "SkImage.h"
14 #include "SkMatrixUtils.h"
15 #include "SkPicture.h"
16 #include "SkReadBuffer.h"
17 #include "SkResourceCache.h"
18
19 #if SK_SUPPORT_GPU
20 #include "GrContext.h"
21 #include "GrCaps.h"
22 #endif
23
24 namespace {
25 static unsigned gBitmapSkaderKeyNamespaceLabel;
26
27 struct BitmapShaderKey : public SkResourceCache::Key {
28 public:
BitmapShaderKey__anond0e525790111::BitmapShaderKey29 BitmapShaderKey(uint32_t pictureID,
30 const SkRect& tile,
31 SkShader::TileMode tmx,
32 SkShader::TileMode tmy,
33 const SkSize& scale,
34 const SkMatrix& localMatrix)
35 : fPictureID(pictureID)
36 , fTile(tile)
37 , fTmx(tmx)
38 , fTmy(tmy)
39 , fScale(scale) {
40
41 for (int i = 0; i < 9; ++i) {
42 fLocalMatrixStorage[i] = localMatrix[i];
43 }
44
45 static const size_t keySize = sizeof(fPictureID) +
46 sizeof(fTile) +
47 sizeof(fTmx) + sizeof(fTmy) +
48 sizeof(fScale) +
49 sizeof(fLocalMatrixStorage);
50 // This better be packed.
51 SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - &fPictureID) == keySize);
52 this->init(&gBitmapSkaderKeyNamespaceLabel, 0, keySize);
53 }
54
55 private:
56 uint32_t fPictureID;
57 SkRect fTile;
58 SkShader::TileMode fTmx, fTmy;
59 SkSize fScale;
60 SkScalar fLocalMatrixStorage[9];
61
62 SkDEBUGCODE(uint32_t fEndOfStruct;)
63 };
64
65 struct BitmapShaderRec : public SkResourceCache::Rec {
BitmapShaderRec__anond0e525790111::BitmapShaderRec66 BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader, size_t bitmapBytes)
67 : fKey(key)
68 , fShader(SkRef(tileShader))
69 , fBitmapBytes(bitmapBytes) {}
70
71 BitmapShaderKey fKey;
72 SkAutoTUnref<SkShader> fShader;
73 size_t fBitmapBytes;
74
getKey__anond0e525790111::BitmapShaderRec75 const Key& getKey() const override { return fKey; }
bytesUsed__anond0e525790111::BitmapShaderRec76 size_t bytesUsed() const override {
77 return sizeof(fKey) + sizeof(SkShader) + fBitmapBytes;
78 }
getCategory__anond0e525790111::BitmapShaderRec79 const char* getCategory() const override { return "bitmap-shader"; }
diagnostic_only_getDiscardable__anond0e525790111::BitmapShaderRec80 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
81
Visitor__anond0e525790111::BitmapShaderRec82 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader) {
83 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec);
84 SkAutoTUnref<SkShader>* result = reinterpret_cast<SkAutoTUnref<SkShader>*>(contextShader);
85
86 result->reset(SkRef(rec.fShader.get()));
87
88 // The bitmap shader is backed by an image generator, thus it can always re-generate its
89 // pixels if discarded.
90 return true;
91 }
92 };
93
94 } // namespace
95
SkPictureShader(sk_sp<SkPicture> picture,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix,const SkRect * tile)96 SkPictureShader::SkPictureShader(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
97 const SkMatrix* localMatrix, const SkRect* tile)
98 : INHERITED(localMatrix)
99 , fPicture(std::move(picture))
100 , fTile(tile ? *tile : fPicture->cullRect())
101 , fTmx(tmx)
102 , fTmy(tmy) {
103 }
104
Make(sk_sp<SkPicture> picture,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix,const SkRect * tile)105 sk_sp<SkShader> SkPictureShader::Make(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
106 const SkMatrix* localMatrix, const SkRect* tile) {
107 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
108 return SkShader::MakeEmptyShader();
109 }
110 return sk_sp<SkShader>(new SkPictureShader(std::move(picture), tmx, tmy, localMatrix, tile));
111 }
112
CreateProc(SkReadBuffer & buffer)113 sk_sp<SkFlattenable> SkPictureShader::CreateProc(SkReadBuffer& buffer) {
114 SkMatrix lm;
115 buffer.readMatrix(&lm);
116 TileMode mx = (TileMode)buffer.read32();
117 TileMode my = (TileMode)buffer.read32();
118 SkRect tile;
119 buffer.readRect(&tile);
120
121 sk_sp<SkPicture> picture;
122
123 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
124 if (buffer.isVersionLT(SkReadBuffer::kPictureShaderHasPictureBool_Version)) {
125 // Older code blindly serialized pictures. We don't trust them.
126 buffer.validate(false);
127 return nullptr;
128 }
129 // Newer code won't serialize pictures in disallow-cross-process-picture mode.
130 // Assert that they didn't serialize anything except a false here.
131 buffer.validate(!buffer.readBool());
132 } else {
133 // Old code always serialized the picture. New code writes a 'true' first if it did.
134 if (buffer.isVersionLT(SkReadBuffer::kPictureShaderHasPictureBool_Version) ||
135 buffer.readBool()) {
136 picture = SkPicture::MakeFromBuffer(buffer);
137 }
138 }
139 return SkPictureShader::Make(picture, mx, my, &lm, &tile);
140 }
141
flatten(SkWriteBuffer & buffer) const142 void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
143 buffer.writeMatrix(this->getLocalMatrix());
144 buffer.write32(fTmx);
145 buffer.write32(fTmy);
146 buffer.writeRect(fTile);
147
148 // The deserialization code won't trust that our serialized picture is safe to deserialize.
149 // So write a 'false' telling it that we're not serializing a picture.
150 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
151 buffer.writeBool(false);
152 } else {
153 buffer.writeBool(true);
154 fPicture->flatten(buffer);
155 }
156 }
157
refBitmapShader(const SkMatrix & viewMatrix,const SkMatrix * localM,const int maxTextureSize) const158 sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, const SkMatrix* localM,
159 const int maxTextureSize) const {
160 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
161
162 SkMatrix m;
163 m.setConcat(viewMatrix, this->getLocalMatrix());
164 if (localM) {
165 m.preConcat(*localM);
166 }
167
168 // Use a rotation-invariant scale
169 SkPoint scale;
170 //
171 // TODO: replace this with decomposeScale() -- but beware LayoutTest rebaselines!
172 //
173 if (!SkDecomposeUpper2x2(m, nullptr, &scale, nullptr)) {
174 // Decomposition failed, use an approximation.
175 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
176 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
177 }
178 SkSize scaledSize = SkSize::Make(SkScalarAbs(scale.x() * fTile.width()),
179 SkScalarAbs(scale.y() * fTile.height()));
180
181 // Clamp the tile size to about 4M pixels
182 static const SkScalar kMaxTileArea = 2048 * 2048;
183 SkScalar tileArea = SkScalarMul(scaledSize.width(), scaledSize.height());
184 if (tileArea > kMaxTileArea) {
185 SkScalar clampScale = SkScalarSqrt(kMaxTileArea / tileArea);
186 scaledSize.set(SkScalarMul(scaledSize.width(), clampScale),
187 SkScalarMul(scaledSize.height(), clampScale));
188 }
189 #if SK_SUPPORT_GPU
190 // Scale down the tile size if larger than maxTextureSize for GPU Path or it should fail on create texture
191 if (maxTextureSize) {
192 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
193 SkScalar downScale = maxTextureSize / SkMaxScalar(scaledSize.width(), scaledSize.height());
194 scaledSize.set(SkScalarFloorToScalar(SkScalarMul(scaledSize.width(), downScale)),
195 SkScalarFloorToScalar(SkScalarMul(scaledSize.height(), downScale)));
196 }
197 }
198 #endif
199
200 #ifdef SK_SUPPORT_LEGACY_PICTURESHADER_ROUNDING
201 const SkISize tileSize = scaledSize.toRound();
202 #else
203 const SkISize tileSize = scaledSize.toCeil();
204 #endif
205 if (tileSize.isEmpty()) {
206 return SkShader::MakeEmptyShader();
207 }
208
209 // The actual scale, compensating for rounding & clamping.
210 const SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
211 SkIntToScalar(tileSize.height()) / fTile.height());
212
213 sk_sp<SkShader> tileShader;
214 BitmapShaderKey key(fPicture->uniqueID(),
215 fTile,
216 fTmx,
217 fTmy,
218 tileScale,
219 this->getLocalMatrix());
220
221 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
222 SkMatrix tileMatrix;
223 tileMatrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.height()),
224 SkMatrix::kFill_ScaleToFit);
225
226 sk_sp<SkImage> tileImage(
227 SkImage::MakeFromPicture(fPicture, tileSize, &tileMatrix, nullptr));
228 if (!tileImage) {
229 return nullptr;
230 }
231
232 SkMatrix shaderMatrix = this->getLocalMatrix();
233 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
234 tileShader = tileImage->makeShader(fTmx, fTmy, &shaderMatrix);
235
236 const SkImageInfo tileInfo = SkImageInfo::MakeN32Premul(tileSize);
237 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get(),
238 tileInfo.getSafeSize(tileInfo.minRowBytes())));
239 }
240
241 return tileShader;
242 }
243
onContextSize(const ContextRec &) const244 size_t SkPictureShader::onContextSize(const ContextRec&) const {
245 return sizeof(PictureShaderContext);
246 }
247
onCreateContext(const ContextRec & rec,void * storage) const248 SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const {
249 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix));
250 if (!bitmapShader) {
251 return nullptr;
252 }
253 return PictureShaderContext::Create(storage, *this, rec, bitmapShader);
254 }
255
256 /////////////////////////////////////////////////////////////////////////////////////////
257
Create(void * storage,const SkPictureShader & shader,const ContextRec & rec,sk_sp<SkShader> bitmapShader)258 SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage,
259 const SkPictureShader& shader, const ContextRec& rec,
260 sk_sp<SkShader> bitmapShader) {
261 PictureShaderContext* ctx = new (storage) PictureShaderContext(shader, rec,
262 std::move(bitmapShader));
263 if (nullptr == ctx->fBitmapShaderContext) {
264 ctx->~PictureShaderContext();
265 ctx = nullptr;
266 }
267 return ctx;
268 }
269
PictureShaderContext(const SkPictureShader & shader,const ContextRec & rec,sk_sp<SkShader> bitmapShader)270 SkPictureShader::PictureShaderContext::PictureShaderContext(
271 const SkPictureShader& shader, const ContextRec& rec, sk_sp<SkShader> bitmapShader)
272 : INHERITED(shader, rec)
273 , fBitmapShader(std::move(bitmapShader))
274 {
275 fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize(rec));
276 fBitmapShaderContext = fBitmapShader->createContext(rec, fBitmapShaderContextStorage);
277 //if fBitmapShaderContext is null, we are invalid
278 }
279
~PictureShaderContext()280 SkPictureShader::PictureShaderContext::~PictureShaderContext() {
281 if (fBitmapShaderContext) {
282 fBitmapShaderContext->~Context();
283 }
284 sk_free(fBitmapShaderContextStorage);
285 }
286
getFlags() const287 uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
288 SkASSERT(fBitmapShaderContext);
289 return fBitmapShaderContext->getFlags();
290 }
291
asAShadeProc(void ** ctx)292 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
293 SkASSERT(fBitmapShaderContext);
294 return fBitmapShaderContext->asAShadeProc(ctx);
295 }
296
shadeSpan(int x,int y,SkPMColor dstC[],int count)297 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
298 SkASSERT(fBitmapShaderContext);
299 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
300 }
301
302 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const303 void SkPictureShader::toString(SkString* str) const {
304 static const char* gTileModeName[SkShader::kTileModeCount] = {
305 "clamp", "repeat", "mirror"
306 };
307
308 str->appendf("PictureShader: [%f:%f:%f:%f] ",
309 fPicture->cullRect().fLeft,
310 fPicture->cullRect().fTop,
311 fPicture->cullRect().fRight,
312 fPicture->cullRect().fBottom);
313
314 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
315
316 this->INHERITED::toString(str);
317 }
318 #endif
319
320 #if SK_SUPPORT_GPU
asFragmentProcessor(const AsFPArgs & args) const321 sk_sp<GrFragmentProcessor> SkPictureShader::asFragmentProcessor(const AsFPArgs& args) const {
322 int maxTextureSize = 0;
323 if (args.fContext) {
324 maxTextureSize = args.fContext->caps()->maxTextureSize();
325 }
326 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*args.fViewMatrix, args.fLocalMatrix,
327 maxTextureSize));
328 if (!bitmapShader) {
329 return nullptr;
330 }
331 return bitmapShader->asFragmentProcessor(SkShader::AsFPArgs(
332 args.fContext, args.fViewMatrix, nullptr, args.fFilterQuality, args.fDstColorSpace,
333 args.fGammaTreatment));
334 }
335 #endif
336