blob: 677c0e15dca86fd59ccafa1734aa1dfdffbec982 [file] [log] [blame]
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +00001/*
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
Yuqian Li8d2fb472017-01-30 11:33:46 -050010#include "SkArenaAlloc.h"
robertphillips25a5b0d2015-09-28 09:32:50 -070011#include "SkBitmap.h"
12#include "SkBitmapProcShader.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000013#include "SkCanvas.h"
Matt Sarett28a7ad22017-04-21 15:12:34 -040014#include "SkColorSpaceXformCanvas.h"
fmalita1b46a572016-02-01 02:34:03 -080015#include "SkImage.h"
fmalitac9a9ca92016-10-12 13:43:43 -070016#include "SkImageShader.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000017#include "SkMatrixUtils.h"
18#include "SkPicture.h"
Matt Sarett9df70bb2017-02-14 13:50:43 -050019#include "SkPictureImageGenerator.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000020#include "SkReadBuffer.h"
fmalita23df2d62014-10-22 07:39:08 -070021#include "SkResourceCache.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000022
23#if SK_SUPPORT_GPU
bsalomon76228632015-05-29 08:02:10 -070024#include "GrCaps.h"
Brian Salomon4cbb6e62017-10-25 15:12:19 -040025#include "GrColorSpaceInfo.h"
26#include "GrContext.h"
Mike Reed84dd8572017-03-08 22:21:00 -050027#include "GrFragmentProcessor.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000028#endif
29
fmalita171e5b72014-10-22 11:20:40 -070030namespace {
31static unsigned gBitmapSkaderKeyNamespaceLabel;
32
fmalita23df2d62014-10-22 07:39:08 -070033struct BitmapShaderKey : public SkResourceCache::Key {
34public:
Matt Sarette71db442017-04-21 15:06:51 -040035 BitmapShaderKey(sk_sp<SkColorSpace> colorSpace,
Florin Malitab00a3602017-07-13 22:34:04 -040036 uint32_t shaderID,
fmalita23df2d62014-10-22 07:39:08 -070037 const SkRect& tile,
38 SkShader::TileMode tmx,
39 SkShader::TileMode tmy,
40 const SkSize& scale,
Matt Sarett28a7ad22017-04-21 15:12:34 -040041 const SkMatrix& localMatrix,
42 SkTransferFunctionBehavior blendBehavior)
Matt Sarette71db442017-04-21 15:06:51 -040043 : fColorSpace(std::move(colorSpace))
fmalita23df2d62014-10-22 07:39:08 -070044 , fTile(tile)
45 , fTmx(tmx)
46 , fTmy(tmy)
Matt Sarett28a7ad22017-04-21 15:12:34 -040047 , fScale(scale)
48 , fBlendBehavior(blendBehavior) {
fmalita04b49c32014-12-10 13:01:43 -080049
50 for (int i = 0; i < 9; ++i) {
51 fLocalMatrixStorage[i] = localMatrix[i];
52 }
fmalita23df2d62014-10-22 07:39:08 -070053
Matt Sarette71db442017-04-21 15:06:51 -040054 static const size_t keySize = sizeof(fColorSpace) +
fmalita23df2d62014-10-22 07:39:08 -070055 sizeof(fTile) +
56 sizeof(fTmx) + sizeof(fTmy) +
57 sizeof(fScale) +
Matt Sarett28a7ad22017-04-21 15:12:34 -040058 sizeof(fLocalMatrixStorage) +
59 sizeof(fBlendBehavior);
fmalita23df2d62014-10-22 07:39:08 -070060 // This better be packed.
Matt Sarette71db442017-04-21 15:06:51 -040061 SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - (uint32_t*)&fColorSpace) == keySize);
Florin Malitab00a3602017-07-13 22:34:04 -040062 this->init(&gBitmapSkaderKeyNamespaceLabel, MakeSharedID(shaderID), keySize);
63 }
64
65 static uint64_t MakeSharedID(uint32_t shaderID) {
66 uint64_t sharedID = SkSetFourByteTag('p', 's', 'd', 'r');
67 return (sharedID << 32) | shaderID;
fmalita23df2d62014-10-22 07:39:08 -070068 }
69
70private:
Florin Malitab00a3602017-07-13 22:34:04 -040071 // 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.
Matt Sarett28a7ad22017-04-21 15:12:34 -040078 sk_sp<SkColorSpace> fColorSpace;
Matt Sarett28a7ad22017-04-21 15:12:34 -040079 SkRect fTile;
80 SkShader::TileMode fTmx, fTmy;
81 SkSize fScale;
82 SkScalar fLocalMatrixStorage[9];
83 SkTransferFunctionBehavior fBlendBehavior;
fmalita23df2d62014-10-22 07:39:08 -070084
85 SkDEBUGCODE(uint32_t fEndOfStruct;)
86};
87
88struct BitmapShaderRec : public SkResourceCache::Rec {
fmalitac9a9ca92016-10-12 13:43:43 -070089 BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader)
fmalita23df2d62014-10-22 07:39:08 -070090 : fKey(key)
fmalitac9a9ca92016-10-12 13:43:43 -070091 , fShader(SkRef(tileShader)) {}
fmalita23df2d62014-10-22 07:39:08 -070092
Hal Canary704cd322016-11-07 14:13:52 -050093 BitmapShaderKey fKey;
94 sk_sp<SkShader> fShader;
95 size_t fBitmapBytes;
fmalita23df2d62014-10-22 07:39:08 -070096
mtklein36352bf2015-03-25 18:17:31 -070097 const Key& getKey() const override { return fKey; }
98 size_t bytesUsed() const override {
fmalitac9a9ca92016-10-12 13:43:43 -070099 // Just the record overhead -- the actual pixels are accounted by SkImageCacherator.
100 return sizeof(fKey) + sizeof(SkImageShader);
fmalita23df2d62014-10-22 07:39:08 -0700101 }
reed216b6432015-08-19 12:25:40 -0700102 const char* getCategory() const override { return "bitmap-shader"; }
103 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
fmalita23df2d62014-10-22 07:39:08 -0700104
105 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader) {
106 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec);
Hal Canary704cd322016-11-07 14:13:52 -0500107 sk_sp<SkShader>* result = reinterpret_cast<sk_sp<SkShader>*>(contextShader);
fmalita23df2d62014-10-22 07:39:08 -0700108
Hal Canary704cd322016-11-07 14:13:52 -0500109 *result = rec.fShader;
fmalita387a01a2014-12-10 12:17:58 -0800110
fmalitaddc4b462015-08-31 19:54:03 -0700111 // The bitmap shader is backed by an image generator, thus it can always re-generate its
112 // pixels if discarded.
113 return true;
fmalita23df2d62014-10-22 07:39:08 -0700114 }
115};
116
Florin Malitab00a3602017-07-13 22:34:04 -0400117static int32_t gNextID = 1;
118uint32_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
fmalita171e5b72014-10-22 11:20:40 -0700126} // namespace
127
reed7fb4f8b2016-03-11 04:33:52 -0800128SkPictureShader::SkPictureShader(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
Matt Sarett28a7ad22017-04-21 15:12:34 -0400129 const SkMatrix* localMatrix, const SkRect* tile,
130 sk_sp<SkColorSpace> colorSpace)
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000131 : INHERITED(localMatrix)
reed8a21c9f2016-03-08 18:50:00 -0800132 , fPicture(std::move(picture))
133 , fTile(tile ? *tile : fPicture->cullRect())
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000134 , fTmx(tmx)
Matt Sarett28a7ad22017-04-21 15:12:34 -0400135 , fTmy(tmy)
136 , fColorSpace(std::move(colorSpace))
Florin Malitab00a3602017-07-13 22:34:04 -0400137 , fUniqueID(next_id())
138 , fAddedToCache(false) {}
139
140SkPictureShader::~SkPictureShader() {
141 if (fAddedToCache.load()) {
142 SkResourceCache::PostPurgeSharedID(BitmapShaderKey::MakeSharedID(fUniqueID));
143 }
144}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000145
reed7fb4f8b2016-03-11 04:33:52 -0800146sk_sp<SkShader> SkPictureShader::Make(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800147 const SkMatrix* localMatrix, const SkRect* tile) {
Robert Phillips54be5c92017-02-11 01:19:30 +0000148 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
reed8a21c9f2016-03-08 18:50:00 -0800149 return SkShader::MakeEmptyShader();
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000150 }
Matt Sarett28a7ad22017-04-21 15:12:34 -0400151 return sk_sp<SkShader>(new SkPictureShader(std::move(picture), tmx, tmy, localMatrix, tile,
152 nullptr));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000153}
154
reed60c9b582016-04-03 09:11:13 -0700155sk_sp<SkFlattenable> SkPictureShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700156 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);
mtklein76be9c82015-05-20 12:05:15 -0700162
reed8a21c9f2016-03-08 18:50:00 -0800163 sk_sp<SkPicture> picture;
hendrikw446ee672015-06-16 09:28:37 -0700164
165 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700166 // Newer code won't serialize pictures in disallow-cross-process-picture mode.
167 // Assert that they didn't serialize anything except a false here.
168 buffer.validate(!buffer.readBool());
hendrikw446ee672015-06-16 09:28:37 -0700169 } else {
Mike Reed70bc94f2017-06-08 12:45:52 -0400170 bool didSerialize = buffer.readBool();
171 if (didSerialize) {
reedca2622b2016-03-18 07:25:55 -0700172 picture = SkPicture::MakeFromBuffer(buffer);
mtklein76be9c82015-05-20 12:05:15 -0700173 }
174 }
reed60c9b582016-04-03 09:11:13 -0700175 return SkPictureShader::Make(picture, mx, my, &lm, &tile);
reed9fa60da2014-08-21 07:59:51 -0700176}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000177
reed9fa60da2014-08-21 07:59:51 -0700178void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
179 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000180 buffer.write32(fTmx);
181 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -0700182 buffer.writeRect(fTile);
mtklein76be9c82015-05-20 12:05:15 -0700183
mtklein76be9c82015-05-20 12:05:15 -0700184 // The deserialization code won't trust that our serialized picture is safe to deserialize.
185 // So write a 'false' telling it that we're not serializing a picture.
hendrikw446ee672015-06-16 09:28:37 -0700186 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700187 buffer.writeBool(false);
hendrikw446ee672015-06-16 09:28:37 -0700188 } else {
mtklein76be9c82015-05-20 12:05:15 -0700189 buffer.writeBool(true);
190 fPicture->flatten(buffer);
191 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000192}
193
reed8a21c9f2016-03-08 18:50:00 -0800194sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, const SkMatrix* localM,
Brian Osman138ea972016-12-16 11:55:18 -0500195 SkColorSpace* dstColorSpace,
reed8a21c9f2016-03-08 18:50:00 -0800196 const int maxTextureSize) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700197 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000198
199 SkMatrix m;
robertphillips44c31282015-09-03 12:58:48 -0700200 m.setConcat(viewMatrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000201 if (localM) {
202 m.preConcat(*localM);
203 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000204
205 // Use a rotation-invariant scale
206 SkPoint scale;
reedadf99902015-03-19 16:10:54 -0700207 //
208 // TODO: replace this with decomposeScale() -- but beware LayoutTest rebaselines!
209 //
halcanary96fcdcc2015-08-27 07:41:13 -0700210 if (!SkDecomposeUpper2x2(m, nullptr, &scale, nullptr)) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000211 // Decomposition failed, use an approximation.
212 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
213 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
214 }
fmalitab0878792015-01-15 10:45:56 -0800215 SkSize scaledSize = SkSize::Make(SkScalarAbs(scale.x() * fTile.width()),
216 SkScalarAbs(scale.y() * fTile.height()));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000217
fmalita511005b2015-03-13 06:50:44 -0700218 // Clamp the tile size to about 4M pixels
219 static const SkScalar kMaxTileArea = 2048 * 2048;
Mike Reeda99b6ce2017-02-04 11:04:26 -0500220 SkScalar tileArea = scaledSize.width() * scaledSize.height();
fmalitabb204f42014-08-07 08:39:24 -0700221 if (tileArea > kMaxTileArea) {
reed80ea19c2015-05-12 10:37:34 -0700222 SkScalar clampScale = SkScalarSqrt(kMaxTileArea / tileArea);
Mike Reeda99b6ce2017-02-04 11:04:26 -0500223 scaledSize.set(scaledSize.width() * clampScale,
224 scaledSize.height() * clampScale);
fmalitabb204f42014-08-07 08:39:24 -0700225 }
gen.kimb9ed8842015-05-03 22:36:30 -0700226#if SK_SUPPORT_GPU
227 // Scale down the tile size if larger than maxTextureSize for GPU Path or it should fail on create texture
228 if (maxTextureSize) {
229 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkf469fc02015-10-14 17:33:29 -0700230 SkScalar downScale = maxTextureSize / SkMaxScalar(scaledSize.width(), scaledSize.height());
Mike Reeda99b6ce2017-02-04 11:04:26 -0500231 scaledSize.set(SkScalarFloorToScalar(scaledSize.width() * downScale),
232 SkScalarFloorToScalar(scaledSize.height() * downScale));
gen.kimb9ed8842015-05-03 22:36:30 -0700233 }
234 }
235#endif
fmalitabb204f42014-08-07 08:39:24 -0700236
fmalita85c6f982016-02-29 09:18:31 -0800237 const SkISize tileSize = scaledSize.toCeil();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000238 if (tileSize.isEmpty()) {
reed8a21c9f2016-03-08 18:50:00 -0800239 return SkShader::MakeEmptyShader();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000240 }
241
fmalitabb204f42014-08-07 08:39:24 -0700242 // The actual scale, compensating for rounding & clamping.
fmalita85c6f982016-02-29 09:18:31 -0800243 const SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
244 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000245
Matt Sarett28a7ad22017-04-21 15:12:34 -0400246 // |fColorSpace| will only be set when using an SkColorSpaceXformCanvas to do pre-draw xforms.
247 // This canvas is strictly for legacy mode. A non-null |dstColorSpace| indicates that we
248 // should perform color correct rendering and xform at draw time.
249 SkASSERT(!fColorSpace || !dstColorSpace);
250 sk_sp<SkColorSpace> keyCS = dstColorSpace ? sk_ref_sp(dstColorSpace) : fColorSpace;
251 SkTransferFunctionBehavior blendBehavior = dstColorSpace ? SkTransferFunctionBehavior::kRespect
252 : SkTransferFunctionBehavior::kIgnore;
253
reed8a21c9f2016-03-08 18:50:00 -0800254 sk_sp<SkShader> tileShader;
Matt Sarett28a7ad22017-04-21 15:12:34 -0400255 BitmapShaderKey key(std::move(keyCS),
Florin Malitab00a3602017-07-13 22:34:04 -0400256 fUniqueID,
fmalita23df2d62014-10-22 07:39:08 -0700257 fTile,
258 fTmx,
259 fTmy,
260 tileScale,
Matt Sarett28a7ad22017-04-21 15:12:34 -0400261 this->getLocalMatrix(),
262 blendBehavior);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000263
fmalita171e5b72014-10-22 11:20:40 -0700264 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
robertphillips25a5b0d2015-09-28 09:32:50 -0700265 SkMatrix tileMatrix;
266 tileMatrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.height()),
fmalita1b46a572016-02-01 02:34:03 -0800267 SkMatrix::kFill_ScaleToFit);
268
Matt Sarett9df70bb2017-02-14 13:50:43 -0500269 sk_sp<SkImage> tileImage = SkImage::MakeFromGenerator(
Mike Reed185130c2017-02-15 15:14:16 -0500270 SkPictureImageGenerator::Make(tileSize, fPicture, &tileMatrix, nullptr,
271 SkImage::BitDepth::kU8, sk_ref_sp(dstColorSpace)));
fmalita1b46a572016-02-01 02:34:03 -0800272 if (!tileImage) {
fmalitaddc4b462015-08-31 19:54:03 -0700273 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000274 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000275
Matt Sarett28a7ad22017-04-21 15:12:34 -0400276 if (fColorSpace) {
277 tileImage = tileImage->makeColorSpace(fColorSpace, SkTransferFunctionBehavior::kIgnore);
278 }
279
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000280 SkMatrix shaderMatrix = this->getLocalMatrix();
281 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
reed5671c5b2016-03-09 14:47:34 -0800282 tileShader = tileImage->makeShader(fTmx, fTmy, &shaderMatrix);
fmalita23df2d62014-10-22 07:39:08 -0700283
fmalitac9a9ca92016-10-12 13:43:43 -0700284 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get()));
Florin Malitab00a3602017-07-13 22:34:04 -0400285 fAddedToCache.store(true);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000286 }
287
reed8a21c9f2016-03-08 18:50:00 -0800288 return tileShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000289}
290
Mike Reed34042072017-08-08 16:29:22 -0400291bool SkPictureShader::onIsRasterPipelineOnly(const SkMatrix& ctm) const {
292 return SkImageShader::IsRasterPipelineOnly(ctm, kN32_SkColorType, kPremul_SkAlphaType,
Mike Reedeefe9f92017-08-21 11:39:15 -0400293 fTmx, fTmy, this->getLocalMatrix());
Mike Reed980e2792017-06-21 13:15:58 -0700294}
295
Mike Reed1d8c42e2017-08-29 14:58:19 -0400296bool SkPictureShader::onAppendStages(const StageRec& rec) const {
Yuqian Li8d2fb472017-01-30 11:33:46 -0500297 // Keep bitmapShader alive by using alloc instead of stack memory
Mike Reed1d8c42e2017-08-29 14:58:19 -0400298 auto& bitmapShader = *rec.fAlloc->make<sk_sp<SkShader>>();
299 bitmapShader = this->refBitmapShader(rec.fCTM, rec.fLocalM, rec.fDstCS);
300 return bitmapShader && as_SB(bitmapShader)->appendStages(rec);
Yuqian Li8d2fb472017-01-30 11:33:46 -0500301}
302
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000303/////////////////////////////////////////////////////////////////////////////////////////
Florin Malita4aed1382017-05-25 10:38:07 -0400304SkShaderBase::Context* SkPictureShader::onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc)
Herb Derby83e939b2017-02-07 14:25:11 -0500305const {
306 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix,
307 rec.fDstColorSpace));
308 if (!bitmapShader) {
309 return nullptr;
310 }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000311
Herb Derby83e939b2017-02-07 14:25:11 -0500312 PictureShaderContext* ctx =
313 alloc->make<PictureShaderContext>(*this, rec, std::move(bitmapShader), alloc);
halcanary96fcdcc2015-08-27 07:41:13 -0700314 if (nullptr == ctx->fBitmapShaderContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700315 ctx = nullptr;
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000316 }
317 return ctx;
318}
319
Matt Sarett28a7ad22017-04-21 15:12:34 -0400320sk_sp<SkShader> SkPictureShader::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
Florin Malita604f0d52017-07-13 14:29:12 -0400321 sk_sp<SkColorSpace> dstCS = xformer->dst();
322 if (SkColorSpace::Equals(dstCS.get(), fColorSpace.get())) {
323 return sk_ref_sp(const_cast<SkPictureShader*>(this));
324 }
325
Matt Sarett28a7ad22017-04-21 15:12:34 -0400326 return sk_sp<SkPictureShader>(new SkPictureShader(fPicture, fTmx, fTmy, &this->getLocalMatrix(),
Florin Malita604f0d52017-07-13 14:29:12 -0400327 &fTile, std::move(dstCS)));
Matt Sarett28a7ad22017-04-21 15:12:34 -0400328}
329
Herb Derby83e939b2017-02-07 14:25:11 -0500330/////////////////////////////////////////////////////////////////////////////////////////
331
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000332SkPictureShader::PictureShaderContext::PictureShaderContext(
Herb Derby83e939b2017-02-07 14:25:11 -0500333 const SkPictureShader& shader, const ContextRec& rec, sk_sp<SkShader> bitmapShader,
334 SkArenaAlloc* alloc)
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000335 : INHERITED(shader, rec)
reed8a21c9f2016-03-08 18:50:00 -0800336 , fBitmapShader(std::move(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000337{
Florin Malita4aed1382017-05-25 10:38:07 -0400338 fBitmapShaderContext = as_SB(fBitmapShader)->makeContext(rec, alloc);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000339 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000340}
341
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000342uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000343 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000344 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000345}
346
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000347void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
348 SkASSERT(fBitmapShaderContext);
349 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
350}
351
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000352#ifndef SK_IGNORE_TO_STRING
353void SkPictureShader::toString(SkString* str) const {
354 static const char* gTileModeName[SkShader::kTileModeCount] = {
355 "clamp", "repeat", "mirror"
356 };
357
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700358 str->appendf("PictureShader: [%f:%f:%f:%f] ",
fmalitaddc4b462015-08-31 19:54:03 -0700359 fPicture->cullRect().fLeft,
360 fPicture->cullRect().fTop,
361 fPicture->cullRect().fRight,
362 fPicture->cullRect().fBottom);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000363
364 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
365
366 this->INHERITED::toString(str);
367}
368#endif
369
370#if SK_SUPPORT_GPU
Brian Salomonaff329b2017-08-11 09:40:37 -0400371std::unique_ptr<GrFragmentProcessor> SkPictureShader::asFragmentProcessor(
372 const AsFPArgs& args) const {
gen.kimb9ed8842015-05-03 22:36:30 -0700373 int maxTextureSize = 0;
brianosman839345d2016-07-22 11:04:53 -0700374 if (args.fContext) {
375 maxTextureSize = args.fContext->caps()->maxTextureSize();
gen.kimb9ed8842015-05-03 22:36:30 -0700376 }
brianosman839345d2016-07-22 11:04:53 -0700377 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*args.fViewMatrix, args.fLocalMatrix,
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400378 args.fDstColorSpaceInfo->colorSpace(),
379 maxTextureSize));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000380 if (!bitmapShader) {
bsalomonc21b09e2015-08-28 18:46:56 -0700381 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000382 }
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400383 return as_SB(bitmapShader)
384 ->asFragmentProcessor(SkShaderBase::AsFPArgs(args.fContext, args.fViewMatrix, nullptr,
385 args.fFilterQuality,
386 args.fDstColorSpaceInfo));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000387}
388#endif