blob: 4c746ee94f30746e40af20af0d989abe0932e765 [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
24#include "GrContext.h"
bsalomon76228632015-05-29 08:02:10 -070025#include "GrCaps.h"
Mike Reed84dd8572017-03-08 22:21:00 -050026#include "GrFragmentProcessor.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000027#endif
28
fmalita171e5b72014-10-22 11:20:40 -070029namespace {
30static unsigned gBitmapSkaderKeyNamespaceLabel;
31
fmalita23df2d62014-10-22 07:39:08 -070032struct BitmapShaderKey : public SkResourceCache::Key {
33public:
Matt Sarette71db442017-04-21 15:06:51 -040034 BitmapShaderKey(sk_sp<SkColorSpace> colorSpace,
Florin Malitab00a3602017-07-13 22:34:04 -040035 uint32_t shaderID,
fmalita23df2d62014-10-22 07:39:08 -070036 const SkRect& tile,
37 SkShader::TileMode tmx,
38 SkShader::TileMode tmy,
39 const SkSize& scale,
Matt Sarett28a7ad22017-04-21 15:12:34 -040040 const SkMatrix& localMatrix,
41 SkTransferFunctionBehavior blendBehavior)
Matt Sarette71db442017-04-21 15:06:51 -040042 : fColorSpace(std::move(colorSpace))
fmalita23df2d62014-10-22 07:39:08 -070043 , fTile(tile)
44 , fTmx(tmx)
45 , fTmy(tmy)
Matt Sarett28a7ad22017-04-21 15:12:34 -040046 , fScale(scale)
47 , fBlendBehavior(blendBehavior) {
fmalita04b49c32014-12-10 13:01:43 -080048
49 for (int i = 0; i < 9; ++i) {
50 fLocalMatrixStorage[i] = localMatrix[i];
51 }
fmalita23df2d62014-10-22 07:39:08 -070052
Matt Sarette71db442017-04-21 15:06:51 -040053 static const size_t keySize = sizeof(fColorSpace) +
fmalita23df2d62014-10-22 07:39:08 -070054 sizeof(fTile) +
55 sizeof(fTmx) + sizeof(fTmy) +
56 sizeof(fScale) +
Matt Sarett28a7ad22017-04-21 15:12:34 -040057 sizeof(fLocalMatrixStorage) +
58 sizeof(fBlendBehavior);
fmalita23df2d62014-10-22 07:39:08 -070059 // This better be packed.
Matt Sarette71db442017-04-21 15:06:51 -040060 SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - (uint32_t*)&fColorSpace) == keySize);
Florin Malitab00a3602017-07-13 22:34:04 -040061 this->init(&gBitmapSkaderKeyNamespaceLabel, MakeSharedID(shaderID), keySize);
62 }
63
64 static uint64_t MakeSharedID(uint32_t shaderID) {
65 uint64_t sharedID = SkSetFourByteTag('p', 's', 'd', 'r');
66 return (sharedID << 32) | shaderID;
fmalita23df2d62014-10-22 07:39:08 -070067 }
68
69private:
Florin Malitab00a3602017-07-13 22:34:04 -040070 // 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.
Matt Sarett28a7ad22017-04-21 15:12:34 -040077 sk_sp<SkColorSpace> fColorSpace;
Matt Sarett28a7ad22017-04-21 15:12:34 -040078 SkRect fTile;
79 SkShader::TileMode fTmx, fTmy;
80 SkSize fScale;
81 SkScalar fLocalMatrixStorage[9];
82 SkTransferFunctionBehavior fBlendBehavior;
fmalita23df2d62014-10-22 07:39:08 -070083
84 SkDEBUGCODE(uint32_t fEndOfStruct;)
85};
86
87struct BitmapShaderRec : public SkResourceCache::Rec {
fmalitac9a9ca92016-10-12 13:43:43 -070088 BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader)
fmalita23df2d62014-10-22 07:39:08 -070089 : fKey(key)
fmalitac9a9ca92016-10-12 13:43:43 -070090 , fShader(SkRef(tileShader)) {}
fmalita23df2d62014-10-22 07:39:08 -070091
Hal Canary704cd322016-11-07 14:13:52 -050092 BitmapShaderKey fKey;
93 sk_sp<SkShader> fShader;
94 size_t fBitmapBytes;
fmalita23df2d62014-10-22 07:39:08 -070095
mtklein36352bf2015-03-25 18:17:31 -070096 const Key& getKey() const override { return fKey; }
97 size_t bytesUsed() const override {
fmalitac9a9ca92016-10-12 13:43:43 -070098 // Just the record overhead -- the actual pixels are accounted by SkImageCacherator.
99 return sizeof(fKey) + sizeof(SkImageShader);
fmalita23df2d62014-10-22 07:39:08 -0700100 }
reed216b6432015-08-19 12:25:40 -0700101 const char* getCategory() const override { return "bitmap-shader"; }
102 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
fmalita23df2d62014-10-22 07:39:08 -0700103
104 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader) {
105 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec);
Hal Canary704cd322016-11-07 14:13:52 -0500106 sk_sp<SkShader>* result = reinterpret_cast<sk_sp<SkShader>*>(contextShader);
fmalita23df2d62014-10-22 07:39:08 -0700107
Hal Canary704cd322016-11-07 14:13:52 -0500108 *result = rec.fShader;
fmalita387a01a2014-12-10 12:17:58 -0800109
fmalitaddc4b462015-08-31 19:54:03 -0700110 // The bitmap shader is backed by an image generator, thus it can always re-generate its
111 // pixels if discarded.
112 return true;
fmalita23df2d62014-10-22 07:39:08 -0700113 }
114};
115
Florin Malitab00a3602017-07-13 22:34:04 -0400116static int32_t gNextID = 1;
117uint32_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
fmalita171e5b72014-10-22 11:20:40 -0700125} // namespace
126
reed7fb4f8b2016-03-11 04:33:52 -0800127SkPictureShader::SkPictureShader(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
Matt Sarett28a7ad22017-04-21 15:12:34 -0400128 const SkMatrix* localMatrix, const SkRect* tile,
129 sk_sp<SkColorSpace> colorSpace)
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000130 : INHERITED(localMatrix)
reed8a21c9f2016-03-08 18:50:00 -0800131 , fPicture(std::move(picture))
132 , fTile(tile ? *tile : fPicture->cullRect())
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000133 , fTmx(tmx)
Matt Sarett28a7ad22017-04-21 15:12:34 -0400134 , fTmy(tmy)
135 , fColorSpace(std::move(colorSpace))
Florin Malitab00a3602017-07-13 22:34:04 -0400136 , fUniqueID(next_id())
137 , fAddedToCache(false) {}
138
139SkPictureShader::~SkPictureShader() {
140 if (fAddedToCache.load()) {
141 SkResourceCache::PostPurgeSharedID(BitmapShaderKey::MakeSharedID(fUniqueID));
142 }
143}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000144
reed7fb4f8b2016-03-11 04:33:52 -0800145sk_sp<SkShader> SkPictureShader::Make(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800146 const SkMatrix* localMatrix, const SkRect* tile) {
Robert Phillips54be5c92017-02-11 01:19:30 +0000147 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
reed8a21c9f2016-03-08 18:50:00 -0800148 return SkShader::MakeEmptyShader();
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000149 }
Matt Sarett28a7ad22017-04-21 15:12:34 -0400150 return sk_sp<SkShader>(new SkPictureShader(std::move(picture), tmx, tmy, localMatrix, tile,
151 nullptr));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000152}
153
reed60c9b582016-04-03 09:11:13 -0700154sk_sp<SkFlattenable> SkPictureShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700155 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);
mtklein76be9c82015-05-20 12:05:15 -0700161
reed8a21c9f2016-03-08 18:50:00 -0800162 sk_sp<SkPicture> picture;
hendrikw446ee672015-06-16 09:28:37 -0700163
164 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700165 // 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());
hendrikw446ee672015-06-16 09:28:37 -0700168 } else {
Mike Reed70bc94f2017-06-08 12:45:52 -0400169 bool didSerialize = buffer.readBool();
170 if (didSerialize) {
reedca2622b2016-03-18 07:25:55 -0700171 picture = SkPicture::MakeFromBuffer(buffer);
mtklein76be9c82015-05-20 12:05:15 -0700172 }
173 }
reed60c9b582016-04-03 09:11:13 -0700174 return SkPictureShader::Make(picture, mx, my, &lm, &tile);
reed9fa60da2014-08-21 07:59:51 -0700175}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000176
reed9fa60da2014-08-21 07:59:51 -0700177void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
178 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000179 buffer.write32(fTmx);
180 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -0700181 buffer.writeRect(fTile);
mtklein76be9c82015-05-20 12:05:15 -0700182
mtklein76be9c82015-05-20 12:05:15 -0700183 // 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.
hendrikw446ee672015-06-16 09:28:37 -0700185 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700186 buffer.writeBool(false);
hendrikw446ee672015-06-16 09:28:37 -0700187 } else {
mtklein76be9c82015-05-20 12:05:15 -0700188 buffer.writeBool(true);
189 fPicture->flatten(buffer);
190 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000191}
192
reed8a21c9f2016-03-08 18:50:00 -0800193sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, const SkMatrix* localM,
Brian Osman138ea972016-12-16 11:55:18 -0500194 SkColorSpace* dstColorSpace,
reed8a21c9f2016-03-08 18:50:00 -0800195 const int maxTextureSize) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700196 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000197
198 SkMatrix m;
robertphillips44c31282015-09-03 12:58:48 -0700199 m.setConcat(viewMatrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000200 if (localM) {
201 m.preConcat(*localM);
202 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000203
204 // Use a rotation-invariant scale
205 SkPoint scale;
reedadf99902015-03-19 16:10:54 -0700206 //
207 // TODO: replace this with decomposeScale() -- but beware LayoutTest rebaselines!
208 //
halcanary96fcdcc2015-08-27 07:41:13 -0700209 if (!SkDecomposeUpper2x2(m, nullptr, &scale, nullptr)) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000210 // 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 }
fmalitab0878792015-01-15 10:45:56 -0800214 SkSize scaledSize = SkSize::Make(SkScalarAbs(scale.x() * fTile.width()),
215 SkScalarAbs(scale.y() * fTile.height()));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000216
fmalita511005b2015-03-13 06:50:44 -0700217 // Clamp the tile size to about 4M pixels
218 static const SkScalar kMaxTileArea = 2048 * 2048;
Mike Reeda99b6ce2017-02-04 11:04:26 -0500219 SkScalar tileArea = scaledSize.width() * scaledSize.height();
fmalitabb204f42014-08-07 08:39:24 -0700220 if (tileArea > kMaxTileArea) {
reed80ea19c2015-05-12 10:37:34 -0700221 SkScalar clampScale = SkScalarSqrt(kMaxTileArea / tileArea);
Mike Reeda99b6ce2017-02-04 11:04:26 -0500222 scaledSize.set(scaledSize.width() * clampScale,
223 scaledSize.height() * clampScale);
fmalitabb204f42014-08-07 08:39:24 -0700224 }
gen.kimb9ed8842015-05-03 22:36:30 -0700225#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) {
ericrkf469fc02015-10-14 17:33:29 -0700229 SkScalar downScale = maxTextureSize / SkMaxScalar(scaledSize.width(), scaledSize.height());
Mike Reeda99b6ce2017-02-04 11:04:26 -0500230 scaledSize.set(SkScalarFloorToScalar(scaledSize.width() * downScale),
231 SkScalarFloorToScalar(scaledSize.height() * downScale));
gen.kimb9ed8842015-05-03 22:36:30 -0700232 }
233 }
234#endif
fmalitabb204f42014-08-07 08:39:24 -0700235
fmalita85c6f982016-02-29 09:18:31 -0800236#ifdef SK_SUPPORT_LEGACY_PICTURESHADER_ROUNDING
237 const SkISize tileSize = scaledSize.toRound();
238#else
239 const SkISize tileSize = scaledSize.toCeil();
240#endif
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000241 if (tileSize.isEmpty()) {
reed8a21c9f2016-03-08 18:50:00 -0800242 return SkShader::MakeEmptyShader();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000243 }
244
fmalitabb204f42014-08-07 08:39:24 -0700245 // The actual scale, compensating for rounding & clamping.
fmalita85c6f982016-02-29 09:18:31 -0800246 const SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
247 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000248
Matt Sarett28a7ad22017-04-21 15:12:34 -0400249 // |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
reed8a21c9f2016-03-08 18:50:00 -0800257 sk_sp<SkShader> tileShader;
Matt Sarett28a7ad22017-04-21 15:12:34 -0400258 BitmapShaderKey key(std::move(keyCS),
Florin Malitab00a3602017-07-13 22:34:04 -0400259 fUniqueID,
fmalita23df2d62014-10-22 07:39:08 -0700260 fTile,
261 fTmx,
262 fTmy,
263 tileScale,
Matt Sarett28a7ad22017-04-21 15:12:34 -0400264 this->getLocalMatrix(),
265 blendBehavior);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000266
fmalita171e5b72014-10-22 11:20:40 -0700267 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
robertphillips25a5b0d2015-09-28 09:32:50 -0700268 SkMatrix tileMatrix;
269 tileMatrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.height()),
fmalita1b46a572016-02-01 02:34:03 -0800270 SkMatrix::kFill_ScaleToFit);
271
Matt Sarett9df70bb2017-02-14 13:50:43 -0500272 sk_sp<SkImage> tileImage = SkImage::MakeFromGenerator(
Mike Reed185130c2017-02-15 15:14:16 -0500273 SkPictureImageGenerator::Make(tileSize, fPicture, &tileMatrix, nullptr,
274 SkImage::BitDepth::kU8, sk_ref_sp(dstColorSpace)));
fmalita1b46a572016-02-01 02:34:03 -0800275 if (!tileImage) {
fmalitaddc4b462015-08-31 19:54:03 -0700276 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000277 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000278
Matt Sarett28a7ad22017-04-21 15:12:34 -0400279 if (fColorSpace) {
280 tileImage = tileImage->makeColorSpace(fColorSpace, SkTransferFunctionBehavior::kIgnore);
281 }
282
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000283 SkMatrix shaderMatrix = this->getLocalMatrix();
284 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
reed5671c5b2016-03-09 14:47:34 -0800285 tileShader = tileImage->makeShader(fTmx, fTmy, &shaderMatrix);
fmalita23df2d62014-10-22 07:39:08 -0700286
fmalitac9a9ca92016-10-12 13:43:43 -0700287 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get()));
Florin Malitab00a3602017-07-13 22:34:04 -0400288 fAddedToCache.store(true);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000289 }
290
reed8a21c9f2016-03-08 18:50:00 -0800291 return tileShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000292}
293
Mike Reed34042072017-08-08 16:29:22 -0400294bool SkPictureShader::onIsRasterPipelineOnly(const SkMatrix& ctm) const {
295 return SkImageShader::IsRasterPipelineOnly(ctm, kN32_SkColorType, kPremul_SkAlphaType,
296 fTmx, fTmy);
Mike Reed980e2792017-06-21 13:15:58 -0700297}
298
Yuqian Li8d2fb472017-01-30 11:33:46 -0500299bool SkPictureShader::onAppendStages(SkRasterPipeline* p, SkColorSpace* cs, SkArenaAlloc* alloc,
300 const SkMatrix& ctm, const SkPaint& paint,
301 const SkMatrix* localMatrix) const {
302 // Keep bitmapShader alive by using alloc instead of stack memory
303 auto& bitmapShader = *alloc->make<sk_sp<SkShader>>();
304 bitmapShader = this->refBitmapShader(ctm, localMatrix, cs);
Florin Malita4aed1382017-05-25 10:38:07 -0400305 return bitmapShader && as_SB(bitmapShader)->appendStages(p, cs, alloc, ctm, paint);
Yuqian Li8d2fb472017-01-30 11:33:46 -0500306}
307
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000308/////////////////////////////////////////////////////////////////////////////////////////
Florin Malita4aed1382017-05-25 10:38:07 -0400309SkShaderBase::Context* SkPictureShader::onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc)
Herb Derby83e939b2017-02-07 14:25:11 -0500310const {
311 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix,
312 rec.fDstColorSpace));
313 if (!bitmapShader) {
314 return nullptr;
315 }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000316
Herb Derby83e939b2017-02-07 14:25:11 -0500317 PictureShaderContext* ctx =
318 alloc->make<PictureShaderContext>(*this, rec, std::move(bitmapShader), alloc);
halcanary96fcdcc2015-08-27 07:41:13 -0700319 if (nullptr == ctx->fBitmapShaderContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700320 ctx = nullptr;
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000321 }
322 return ctx;
323}
324
Matt Sarett28a7ad22017-04-21 15:12:34 -0400325sk_sp<SkShader> SkPictureShader::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
Florin Malita604f0d52017-07-13 14:29:12 -0400326 sk_sp<SkColorSpace> dstCS = xformer->dst();
327 if (SkColorSpace::Equals(dstCS.get(), fColorSpace.get())) {
328 return sk_ref_sp(const_cast<SkPictureShader*>(this));
329 }
330
Matt Sarett28a7ad22017-04-21 15:12:34 -0400331 return sk_sp<SkPictureShader>(new SkPictureShader(fPicture, fTmx, fTmy, &this->getLocalMatrix(),
Florin Malita604f0d52017-07-13 14:29:12 -0400332 &fTile, std::move(dstCS)));
Matt Sarett28a7ad22017-04-21 15:12:34 -0400333}
334
Herb Derby83e939b2017-02-07 14:25:11 -0500335/////////////////////////////////////////////////////////////////////////////////////////
336
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000337SkPictureShader::PictureShaderContext::PictureShaderContext(
Herb Derby83e939b2017-02-07 14:25:11 -0500338 const SkPictureShader& shader, const ContextRec& rec, sk_sp<SkShader> bitmapShader,
339 SkArenaAlloc* alloc)
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000340 : INHERITED(shader, rec)
reed8a21c9f2016-03-08 18:50:00 -0800341 , fBitmapShader(std::move(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000342{
Florin Malita4aed1382017-05-25 10:38:07 -0400343 fBitmapShaderContext = as_SB(fBitmapShader)->makeContext(rec, alloc);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000344 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000345}
346
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000347uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000348 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000349 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000350}
351
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000352void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
353 SkASSERT(fBitmapShaderContext);
354 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
355}
356
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000357#ifndef SK_IGNORE_TO_STRING
358void SkPictureShader::toString(SkString* str) const {
359 static const char* gTileModeName[SkShader::kTileModeCount] = {
360 "clamp", "repeat", "mirror"
361 };
362
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700363 str->appendf("PictureShader: [%f:%f:%f:%f] ",
fmalitaddc4b462015-08-31 19:54:03 -0700364 fPicture->cullRect().fLeft,
365 fPicture->cullRect().fTop,
366 fPicture->cullRect().fRight,
367 fPicture->cullRect().fBottom);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000368
369 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
370
371 this->INHERITED::toString(str);
372}
373#endif
374
375#if SK_SUPPORT_GPU
brianosman839345d2016-07-22 11:04:53 -0700376sk_sp<GrFragmentProcessor> SkPictureShader::asFragmentProcessor(const AsFPArgs& args) const {
gen.kimb9ed8842015-05-03 22:36:30 -0700377 int maxTextureSize = 0;
brianosman839345d2016-07-22 11:04:53 -0700378 if (args.fContext) {
379 maxTextureSize = args.fContext->caps()->maxTextureSize();
gen.kimb9ed8842015-05-03 22:36:30 -0700380 }
brianosman839345d2016-07-22 11:04:53 -0700381 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*args.fViewMatrix, args.fLocalMatrix,
Brian Osman138ea972016-12-16 11:55:18 -0500382 args.fDstColorSpace, maxTextureSize));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000383 if (!bitmapShader) {
bsalomonc21b09e2015-08-28 18:46:56 -0700384 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000385 }
Florin Malita4aed1382017-05-25 10:38:07 -0400386 return as_SB(bitmapShader)->asFragmentProcessor(SkShaderBase::AsFPArgs(
Brian Osman61624f02016-12-09 14:51:59 -0500387 args.fContext, args.fViewMatrix, nullptr, args.fFilterQuality, args.fDstColorSpace));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000388}
389#endif