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