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 "SkArenaAlloc.h"
11 #include "SkBitmap.h"
12 #include "SkBitmapProcShader.h"
13 #include "SkCanvas.h"
14 #include "SkColorSpaceXformCanvas.h"
15 #include "SkImage.h"
16 #include "SkImageShader.h"
17 #include "SkMatrixUtils.h"
18 #include "SkPicture.h"
19 #include "SkPictureImageGenerator.h"
20 #include "SkReadBuffer.h"
21 #include "SkResourceCache.h"
22
23 #if SK_SUPPORT_GPU
24 #include "GrContext.h"
25 #include "GrCaps.h"
26 #include "GrFragmentProcessor.h"
27 #endif
28
29 namespace {
30 static unsigned gBitmapSkaderKeyNamespaceLabel;
31
32 struct BitmapShaderKey : public SkResourceCache::Key {
33 public:
BitmapShaderKey__anondb291e390111::BitmapShaderKey34 BitmapShaderKey(sk_sp<SkColorSpace> colorSpace,
35 uint32_t shaderID,
36 const SkRect& tile,
37 SkShader::TileMode tmx,
38 SkShader::TileMode tmy,
39 const SkSize& scale,
40 const SkMatrix& localMatrix,
41 SkTransferFunctionBehavior blendBehavior)
42 : fColorSpace(std::move(colorSpace))
43 , fTile(tile)
44 , fTmx(tmx)
45 , fTmy(tmy)
46 , fScale(scale)
47 , fBlendBehavior(blendBehavior) {
48
49 for (int i = 0; i < 9; ++i) {
50 fLocalMatrixStorage[i] = localMatrix[i];
51 }
52
53 static const size_t keySize = sizeof(fColorSpace) +
54 sizeof(fTile) +
55 sizeof(fTmx) + sizeof(fTmy) +
56 sizeof(fScale) +
57 sizeof(fLocalMatrixStorage) +
58 sizeof(fBlendBehavior);
59 // This better be packed.
60 SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - (uint32_t*)&fColorSpace) == keySize);
61 this->init(&gBitmapSkaderKeyNamespaceLabel, MakeSharedID(shaderID), keySize);
62 }
63
MakeSharedID__anondb291e390111::BitmapShaderKey64 static uint64_t MakeSharedID(uint32_t shaderID) {
65 uint64_t sharedID = SkSetFourByteTag('p', 's', 'd', 'r');
66 return (sharedID << 32) | shaderID;
67 }
68
69 private:
70 // TODO: there are some fishy things about using CS sk_sps in the key:
71 // - false negatives: keys are memcmp'ed, so we don't detect equivalent CSs
72 // (SkColorspace::Equals)
73 // - we're keeping the CS alive, even when the client releases it
74 //
75 // Ideally we'd be using unique IDs or some other weak ref + purge mechanism
76 // when the CS is deleted.
77 sk_sp<SkColorSpace> fColorSpace;
78 SkRect fTile;
79 SkShader::TileMode fTmx, fTmy;
80 SkSize fScale;
81 SkScalar fLocalMatrixStorage[9];
82 SkTransferFunctionBehavior fBlendBehavior;
83
84 SkDEBUGCODE(uint32_t fEndOfStruct;)
85 };
86
87 struct BitmapShaderRec : public SkResourceCache::Rec {
BitmapShaderRec__anondb291e390111::BitmapShaderRec88 BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader)
89 : fKey(key)
90 , fShader(SkRef(tileShader)) {}
91
92 BitmapShaderKey fKey;
93 sk_sp<SkShader> fShader;
94 size_t fBitmapBytes;
95
getKey__anondb291e390111::BitmapShaderRec96 const Key& getKey() const override { return fKey; }
bytesUsed__anondb291e390111::BitmapShaderRec97 size_t bytesUsed() const override {
98 // Just the record overhead -- the actual pixels are accounted by SkImageCacherator.
99 return sizeof(fKey) + sizeof(SkImageShader);
100 }
getCategory__anondb291e390111::BitmapShaderRec101 const char* getCategory() const override { return "bitmap-shader"; }
diagnostic_only_getDiscardable__anondb291e390111::BitmapShaderRec102 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
103
Visitor__anondb291e390111::BitmapShaderRec104 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader) {
105 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec);
106 sk_sp<SkShader>* result = reinterpret_cast<sk_sp<SkShader>*>(contextShader);
107
108 *result = rec.fShader;
109
110 // The bitmap shader is backed by an image generator, thus it can always re-generate its
111 // pixels if discarded.
112 return true;
113 }
114 };
115
116 static int32_t gNextID = 1;
next_id()117 uint32_t next_id() {
118 int32_t id;
119 do {
120 id = sk_atomic_inc(&gNextID);
121 } while (id == SK_InvalidGenID);
122 return static_cast<uint32_t>(id);
123 }
124
125 } // namespace
126
SkPictureShader(sk_sp<SkPicture> picture,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix,const SkRect * tile,sk_sp<SkColorSpace> colorSpace)127 SkPictureShader::SkPictureShader(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
128 const SkMatrix* localMatrix, const SkRect* tile,
129 sk_sp<SkColorSpace> colorSpace)
130 : INHERITED(localMatrix)
131 , fPicture(std::move(picture))
132 , fTile(tile ? *tile : fPicture->cullRect())
133 , fTmx(tmx)
134 , fTmy(tmy)
135 , fColorSpace(std::move(colorSpace))
136 , fUniqueID(next_id())
137 , fAddedToCache(false) {}
138
~SkPictureShader()139 SkPictureShader::~SkPictureShader() {
140 if (fAddedToCache.load()) {
141 SkResourceCache::PostPurgeSharedID(BitmapShaderKey::MakeSharedID(fUniqueID));
142 }
143 }
144
Make(sk_sp<SkPicture> picture,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix,const SkRect * tile)145 sk_sp<SkShader> SkPictureShader::Make(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
146 const SkMatrix* localMatrix, const SkRect* tile) {
147 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
148 return SkShader::MakeEmptyShader();
149 }
150 return sk_sp<SkShader>(new SkPictureShader(std::move(picture), tmx, tmy, localMatrix, tile,
151 nullptr));
152 }
153
CreateProc(SkReadBuffer & buffer)154 sk_sp<SkFlattenable> SkPictureShader::CreateProc(SkReadBuffer& buffer) {
155 SkMatrix lm;
156 buffer.readMatrix(&lm);
157 TileMode mx = (TileMode)buffer.read32();
158 TileMode my = (TileMode)buffer.read32();
159 SkRect tile;
160 buffer.readRect(&tile);
161
162 sk_sp<SkPicture> picture;
163
164 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
165 // Newer code won't serialize pictures in disallow-cross-process-picture mode.
166 // Assert that they didn't serialize anything except a false here.
167 buffer.validate(!buffer.readBool());
168 } else {
169 bool didSerialize = buffer.readBool();
170 if (didSerialize) {
171 picture = SkPicture::MakeFromBuffer(buffer);
172 }
173 }
174 return SkPictureShader::Make(picture, mx, my, &lm, &tile);
175 }
176
flatten(SkWriteBuffer & buffer) const177 void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
178 buffer.writeMatrix(this->getLocalMatrix());
179 buffer.write32(fTmx);
180 buffer.write32(fTmy);
181 buffer.writeRect(fTile);
182
183 // The deserialization code won't trust that our serialized picture is safe to deserialize.
184 // So write a 'false' telling it that we're not serializing a picture.
185 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
186 buffer.writeBool(false);
187 } else {
188 buffer.writeBool(true);
189 fPicture->flatten(buffer);
190 }
191 }
192
refBitmapShader(const SkMatrix & viewMatrix,const SkMatrix * localM,SkColorSpace * dstColorSpace,const int maxTextureSize) const193 sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, const SkMatrix* localM,
194 SkColorSpace* dstColorSpace,
195 const int maxTextureSize) const {
196 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
197
198 SkMatrix m;
199 m.setConcat(viewMatrix, this->getLocalMatrix());
200 if (localM) {
201 m.preConcat(*localM);
202 }
203
204 // Use a rotation-invariant scale
205 SkPoint scale;
206 //
207 // TODO: replace this with decomposeScale() -- but beware LayoutTest rebaselines!
208 //
209 if (!SkDecomposeUpper2x2(m, nullptr, &scale, nullptr)) {
210 // Decomposition failed, use an approximation.
211 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
212 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
213 }
214 SkSize scaledSize = SkSize::Make(SkScalarAbs(scale.x() * fTile.width()),
215 SkScalarAbs(scale.y() * fTile.height()));
216
217 // Clamp the tile size to about 4M pixels
218 static const SkScalar kMaxTileArea = 2048 * 2048;
219 SkScalar tileArea = scaledSize.width() * scaledSize.height();
220 if (tileArea > kMaxTileArea) {
221 SkScalar clampScale = SkScalarSqrt(kMaxTileArea / tileArea);
222 scaledSize.set(scaledSize.width() * clampScale,
223 scaledSize.height() * clampScale);
224 }
225 #if SK_SUPPORT_GPU
226 // Scale down the tile size if larger than maxTextureSize for GPU Path or it should fail on create texture
227 if (maxTextureSize) {
228 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
229 SkScalar downScale = maxTextureSize / SkMaxScalar(scaledSize.width(), scaledSize.height());
230 scaledSize.set(SkScalarFloorToScalar(scaledSize.width() * downScale),
231 SkScalarFloorToScalar(scaledSize.height() * downScale));
232 }
233 }
234 #endif
235
236 #ifdef SK_SUPPORT_LEGACY_PICTURESHADER_ROUNDING
237 const SkISize tileSize = scaledSize.toRound();
238 #else
239 const SkISize tileSize = scaledSize.toCeil();
240 #endif
241 if (tileSize.isEmpty()) {
242 return SkShader::MakeEmptyShader();
243 }
244
245 // The actual scale, compensating for rounding & clamping.
246 const SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
247 SkIntToScalar(tileSize.height()) / fTile.height());
248
249 // |fColorSpace| will only be set when using an SkColorSpaceXformCanvas to do pre-draw xforms.
250 // This canvas is strictly for legacy mode. A non-null |dstColorSpace| indicates that we
251 // should perform color correct rendering and xform at draw time.
252 SkASSERT(!fColorSpace || !dstColorSpace);
253 sk_sp<SkColorSpace> keyCS = dstColorSpace ? sk_ref_sp(dstColorSpace) : fColorSpace;
254 SkTransferFunctionBehavior blendBehavior = dstColorSpace ? SkTransferFunctionBehavior::kRespect
255 : SkTransferFunctionBehavior::kIgnore;
256
257 sk_sp<SkShader> tileShader;
258 BitmapShaderKey key(std::move(keyCS),
259 fUniqueID,
260 fTile,
261 fTmx,
262 fTmy,
263 tileScale,
264 this->getLocalMatrix(),
265 blendBehavior);
266
267 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
268 SkMatrix tileMatrix;
269 tileMatrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.height()),
270 SkMatrix::kFill_ScaleToFit);
271
272 sk_sp<SkImage> tileImage = SkImage::MakeFromGenerator(
273 SkPictureImageGenerator::Make(tileSize, fPicture, &tileMatrix, nullptr,
274 SkImage::BitDepth::kU8, sk_ref_sp(dstColorSpace)));
275 if (!tileImage) {
276 return nullptr;
277 }
278
279 if (fColorSpace) {
280 tileImage = tileImage->makeColorSpace(fColorSpace, SkTransferFunctionBehavior::kIgnore);
281 }
282
283 SkMatrix shaderMatrix = this->getLocalMatrix();
284 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
285 tileShader = tileImage->makeShader(fTmx, fTmy, &shaderMatrix);
286
287 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get()));
288 fAddedToCache.store(true);
289 }
290
291 return tileShader;
292 }
293
onIsRasterPipelineOnly() const294 bool SkPictureShader::onIsRasterPipelineOnly() const {
295 return SkImageShader::IsRasterPipelineOnly(kN32_SkColorType, fTmx, fTmy);
296 }
297
onAppendStages(SkRasterPipeline * p,SkColorSpace * cs,SkArenaAlloc * alloc,const SkMatrix & ctm,const SkPaint & paint,const SkMatrix * localMatrix) const298 bool SkPictureShader::onAppendStages(SkRasterPipeline* p, SkColorSpace* cs, SkArenaAlloc* alloc,
299 const SkMatrix& ctm, const SkPaint& paint,
300 const SkMatrix* localMatrix) const {
301 // Keep bitmapShader alive by using alloc instead of stack memory
302 auto& bitmapShader = *alloc->make<sk_sp<SkShader>>();
303 bitmapShader = this->refBitmapShader(ctm, localMatrix, cs);
304 return bitmapShader && as_SB(bitmapShader)->appendStages(p, cs, alloc, ctm, paint);
305 }
306
307 /////////////////////////////////////////////////////////////////////////////////////////
onMakeContext(const ContextRec & rec,SkArenaAlloc * alloc) const308 SkShaderBase::Context* SkPictureShader::onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc)
309 const {
310 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix,
311 rec.fDstColorSpace));
312 if (!bitmapShader) {
313 return nullptr;
314 }
315
316 PictureShaderContext* ctx =
317 alloc->make<PictureShaderContext>(*this, rec, std::move(bitmapShader), alloc);
318 if (nullptr == ctx->fBitmapShaderContext) {
319 ctx = nullptr;
320 }
321 return ctx;
322 }
323
onMakeColorSpace(SkColorSpaceXformer * xformer) const324 sk_sp<SkShader> SkPictureShader::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
325 sk_sp<SkColorSpace> dstCS = xformer->dst();
326 if (SkColorSpace::Equals(dstCS.get(), fColorSpace.get())) {
327 return sk_ref_sp(const_cast<SkPictureShader*>(this));
328 }
329
330 return sk_sp<SkPictureShader>(new SkPictureShader(fPicture, fTmx, fTmy, &this->getLocalMatrix(),
331 &fTile, std::move(dstCS)));
332 }
333
334 /////////////////////////////////////////////////////////////////////////////////////////
335
PictureShaderContext(const SkPictureShader & shader,const ContextRec & rec,sk_sp<SkShader> bitmapShader,SkArenaAlloc * alloc)336 SkPictureShader::PictureShaderContext::PictureShaderContext(
337 const SkPictureShader& shader, const ContextRec& rec, sk_sp<SkShader> bitmapShader,
338 SkArenaAlloc* alloc)
339 : INHERITED(shader, rec)
340 , fBitmapShader(std::move(bitmapShader))
341 {
342 fBitmapShaderContext = as_SB(fBitmapShader)->makeContext(rec, alloc);
343 //if fBitmapShaderContext is null, we are invalid
344 }
345
getFlags() const346 uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
347 SkASSERT(fBitmapShaderContext);
348 return fBitmapShaderContext->getFlags();
349 }
350
asAShadeProc(void ** ctx)351 SkShaderBase::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
352 SkASSERT(fBitmapShaderContext);
353 return fBitmapShaderContext->asAShadeProc(ctx);
354 }
355
shadeSpan(int x,int y,SkPMColor dstC[],int count)356 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
357 SkASSERT(fBitmapShaderContext);
358 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
359 }
360
361 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const362 void SkPictureShader::toString(SkString* str) const {
363 static const char* gTileModeName[SkShader::kTileModeCount] = {
364 "clamp", "repeat", "mirror"
365 };
366
367 str->appendf("PictureShader: [%f:%f:%f:%f] ",
368 fPicture->cullRect().fLeft,
369 fPicture->cullRect().fTop,
370 fPicture->cullRect().fRight,
371 fPicture->cullRect().fBottom);
372
373 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
374
375 this->INHERITED::toString(str);
376 }
377 #endif
378
379 #if SK_SUPPORT_GPU
asFragmentProcessor(const AsFPArgs & args) const380 sk_sp<GrFragmentProcessor> SkPictureShader::asFragmentProcessor(const AsFPArgs& args) const {
381 int maxTextureSize = 0;
382 if (args.fContext) {
383 maxTextureSize = args.fContext->caps()->maxTextureSize();
384 }
385 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*args.fViewMatrix, args.fLocalMatrix,
386 args.fDstColorSpace, maxTextureSize));
387 if (!bitmapShader) {
388 return nullptr;
389 }
390 return as_SB(bitmapShader)->asFragmentProcessor(SkShaderBase::AsFPArgs(
391 args.fContext, args.fViewMatrix, nullptr, args.fFilterQuality, args.fDstColorSpace));
392 }
393 #endif
394