blob: 40724357e7a5496dda59d99e085ead2490d931e6 [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
Mike Reedd1c65d62018-01-03 11:32:13 -0500165 if (SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700166 buffer.validate(!buffer.readBool());
hendrikw446ee672015-06-16 09:28:37 -0700167 } else {
Mike Reed70bc94f2017-06-08 12:45:52 -0400168 bool didSerialize = buffer.readBool();
169 if (didSerialize) {
reedca2622b2016-03-18 07:25:55 -0700170 picture = SkPicture::MakeFromBuffer(buffer);
mtklein76be9c82015-05-20 12:05:15 -0700171 }
172 }
reed60c9b582016-04-03 09:11:13 -0700173 return SkPictureShader::Make(picture, mx, my, &lm, &tile);
reed9fa60da2014-08-21 07:59:51 -0700174}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000175
reed9fa60da2014-08-21 07:59:51 -0700176void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
177 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000178 buffer.write32(fTmx);
179 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -0700180 buffer.writeRect(fTile);
mtklein76be9c82015-05-20 12:05:15 -0700181
mtklein76be9c82015-05-20 12:05:15 -0700182 // The deserialization code won't trust that our serialized picture is safe to deserialize.
183 // So write a 'false' telling it that we're not serializing a picture.
Mike Reedd1c65d62018-01-03 11:32:13 -0500184 if (SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700185 buffer.writeBool(false);
hendrikw446ee672015-06-16 09:28:37 -0700186 } else {
mtklein76be9c82015-05-20 12:05:15 -0700187 buffer.writeBool(true);
188 fPicture->flatten(buffer);
189 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000190}
191
reed8a21c9f2016-03-08 18:50:00 -0800192sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, const SkMatrix* localM,
Brian Osman138ea972016-12-16 11:55:18 -0500193 SkColorSpace* dstColorSpace,
reed8a21c9f2016-03-08 18:50:00 -0800194 const int maxTextureSize) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700195 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000196
197 SkMatrix m;
robertphillips44c31282015-09-03 12:58:48 -0700198 m.setConcat(viewMatrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000199 if (localM) {
200 m.preConcat(*localM);
201 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000202
203 // Use a rotation-invariant scale
204 SkPoint scale;
reedadf99902015-03-19 16:10:54 -0700205 //
206 // TODO: replace this with decomposeScale() -- but beware LayoutTest rebaselines!
207 //
halcanary96fcdcc2015-08-27 07:41:13 -0700208 if (!SkDecomposeUpper2x2(m, nullptr, &scale, nullptr)) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000209 // Decomposition failed, use an approximation.
210 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
211 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
212 }
fmalitab0878792015-01-15 10:45:56 -0800213 SkSize scaledSize = SkSize::Make(SkScalarAbs(scale.x() * fTile.width()),
214 SkScalarAbs(scale.y() * fTile.height()));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000215
fmalita511005b2015-03-13 06:50:44 -0700216 // Clamp the tile size to about 4M pixels
217 static const SkScalar kMaxTileArea = 2048 * 2048;
Mike Reeda99b6ce2017-02-04 11:04:26 -0500218 SkScalar tileArea = scaledSize.width() * scaledSize.height();
fmalitabb204f42014-08-07 08:39:24 -0700219 if (tileArea > kMaxTileArea) {
reed80ea19c2015-05-12 10:37:34 -0700220 SkScalar clampScale = SkScalarSqrt(kMaxTileArea / tileArea);
Mike Reeda99b6ce2017-02-04 11:04:26 -0500221 scaledSize.set(scaledSize.width() * clampScale,
222 scaledSize.height() * clampScale);
fmalitabb204f42014-08-07 08:39:24 -0700223 }
gen.kimb9ed8842015-05-03 22:36:30 -0700224#if SK_SUPPORT_GPU
225 // Scale down the tile size if larger than maxTextureSize for GPU Path or it should fail on create texture
226 if (maxTextureSize) {
227 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkf469fc02015-10-14 17:33:29 -0700228 SkScalar downScale = maxTextureSize / SkMaxScalar(scaledSize.width(), scaledSize.height());
Mike Reeda99b6ce2017-02-04 11:04:26 -0500229 scaledSize.set(SkScalarFloorToScalar(scaledSize.width() * downScale),
230 SkScalarFloorToScalar(scaledSize.height() * downScale));
gen.kimb9ed8842015-05-03 22:36:30 -0700231 }
232 }
233#endif
fmalitabb204f42014-08-07 08:39:24 -0700234
fmalita85c6f982016-02-29 09:18:31 -0800235 const SkISize tileSize = scaledSize.toCeil();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000236 if (tileSize.isEmpty()) {
reed8a21c9f2016-03-08 18:50:00 -0800237 return SkShader::MakeEmptyShader();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000238 }
239
fmalitabb204f42014-08-07 08:39:24 -0700240 // The actual scale, compensating for rounding & clamping.
fmalita85c6f982016-02-29 09:18:31 -0800241 const SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
242 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000243
Matt Sarett28a7ad22017-04-21 15:12:34 -0400244 // |fColorSpace| will only be set when using an SkColorSpaceXformCanvas to do pre-draw xforms.
245 // This canvas is strictly for legacy mode. A non-null |dstColorSpace| indicates that we
246 // should perform color correct rendering and xform at draw time.
247 SkASSERT(!fColorSpace || !dstColorSpace);
248 sk_sp<SkColorSpace> keyCS = dstColorSpace ? sk_ref_sp(dstColorSpace) : fColorSpace;
249 SkTransferFunctionBehavior blendBehavior = dstColorSpace ? SkTransferFunctionBehavior::kRespect
250 : SkTransferFunctionBehavior::kIgnore;
251
reed8a21c9f2016-03-08 18:50:00 -0800252 sk_sp<SkShader> tileShader;
Matt Sarett28a7ad22017-04-21 15:12:34 -0400253 BitmapShaderKey key(std::move(keyCS),
Florin Malitab00a3602017-07-13 22:34:04 -0400254 fUniqueID,
fmalita23df2d62014-10-22 07:39:08 -0700255 fTile,
256 fTmx,
257 fTmy,
258 tileScale,
Matt Sarett28a7ad22017-04-21 15:12:34 -0400259 this->getLocalMatrix(),
260 blendBehavior);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000261
fmalita171e5b72014-10-22 11:20:40 -0700262 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
robertphillips25a5b0d2015-09-28 09:32:50 -0700263 SkMatrix tileMatrix;
264 tileMatrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.height()),
fmalita1b46a572016-02-01 02:34:03 -0800265 SkMatrix::kFill_ScaleToFit);
266
Matt Sarett9df70bb2017-02-14 13:50:43 -0500267 sk_sp<SkImage> tileImage = SkImage::MakeFromGenerator(
Mike Reed185130c2017-02-15 15:14:16 -0500268 SkPictureImageGenerator::Make(tileSize, fPicture, &tileMatrix, nullptr,
269 SkImage::BitDepth::kU8, sk_ref_sp(dstColorSpace)));
fmalita1b46a572016-02-01 02:34:03 -0800270 if (!tileImage) {
fmalitaddc4b462015-08-31 19:54:03 -0700271 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000272 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000273
Matt Sarett28a7ad22017-04-21 15:12:34 -0400274 if (fColorSpace) {
275 tileImage = tileImage->makeColorSpace(fColorSpace, SkTransferFunctionBehavior::kIgnore);
276 }
277
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000278 SkMatrix shaderMatrix = this->getLocalMatrix();
279 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
reed5671c5b2016-03-09 14:47:34 -0800280 tileShader = tileImage->makeShader(fTmx, fTmy, &shaderMatrix);
fmalita23df2d62014-10-22 07:39:08 -0700281
fmalitac9a9ca92016-10-12 13:43:43 -0700282 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get()));
Florin Malitab00a3602017-07-13 22:34:04 -0400283 fAddedToCache.store(true);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000284 }
285
reed8a21c9f2016-03-08 18:50:00 -0800286 return tileShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000287}
288
Mike Reed34042072017-08-08 16:29:22 -0400289bool SkPictureShader::onIsRasterPipelineOnly(const SkMatrix& ctm) const {
290 return SkImageShader::IsRasterPipelineOnly(ctm, kN32_SkColorType, kPremul_SkAlphaType,
Mike Reedeefe9f92017-08-21 11:39:15 -0400291 fTmx, fTmy, this->getLocalMatrix());
Mike Reed980e2792017-06-21 13:15:58 -0700292}
293
Mike Reed1d8c42e2017-08-29 14:58:19 -0400294bool SkPictureShader::onAppendStages(const StageRec& rec) const {
Yuqian Li8d2fb472017-01-30 11:33:46 -0500295 // Keep bitmapShader alive by using alloc instead of stack memory
Mike Reed1d8c42e2017-08-29 14:58:19 -0400296 auto& bitmapShader = *rec.fAlloc->make<sk_sp<SkShader>>();
297 bitmapShader = this->refBitmapShader(rec.fCTM, rec.fLocalM, rec.fDstCS);
298 return bitmapShader && as_SB(bitmapShader)->appendStages(rec);
Yuqian Li8d2fb472017-01-30 11:33:46 -0500299}
300
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000301/////////////////////////////////////////////////////////////////////////////////////////
Florin Malita4aed1382017-05-25 10:38:07 -0400302SkShaderBase::Context* SkPictureShader::onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc)
Herb Derby83e939b2017-02-07 14:25:11 -0500303const {
304 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix,
305 rec.fDstColorSpace));
306 if (!bitmapShader) {
307 return nullptr;
308 }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000309
Herb Derby83e939b2017-02-07 14:25:11 -0500310 PictureShaderContext* ctx =
311 alloc->make<PictureShaderContext>(*this, rec, std::move(bitmapShader), alloc);
halcanary96fcdcc2015-08-27 07:41:13 -0700312 if (nullptr == ctx->fBitmapShaderContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700313 ctx = nullptr;
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000314 }
315 return ctx;
316}
317
Matt Sarett28a7ad22017-04-21 15:12:34 -0400318sk_sp<SkShader> SkPictureShader::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
Florin Malita604f0d52017-07-13 14:29:12 -0400319 sk_sp<SkColorSpace> dstCS = xformer->dst();
320 if (SkColorSpace::Equals(dstCS.get(), fColorSpace.get())) {
321 return sk_ref_sp(const_cast<SkPictureShader*>(this));
322 }
323
Matt Sarett28a7ad22017-04-21 15:12:34 -0400324 return sk_sp<SkPictureShader>(new SkPictureShader(fPicture, fTmx, fTmy, &this->getLocalMatrix(),
Florin Malita604f0d52017-07-13 14:29:12 -0400325 &fTile, std::move(dstCS)));
Matt Sarett28a7ad22017-04-21 15:12:34 -0400326}
327
Herb Derby83e939b2017-02-07 14:25:11 -0500328/////////////////////////////////////////////////////////////////////////////////////////
329
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000330SkPictureShader::PictureShaderContext::PictureShaderContext(
Herb Derby83e939b2017-02-07 14:25:11 -0500331 const SkPictureShader& shader, const ContextRec& rec, sk_sp<SkShader> bitmapShader,
332 SkArenaAlloc* alloc)
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000333 : INHERITED(shader, rec)
reed8a21c9f2016-03-08 18:50:00 -0800334 , fBitmapShader(std::move(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000335{
Florin Malita4aed1382017-05-25 10:38:07 -0400336 fBitmapShaderContext = as_SB(fBitmapShader)->makeContext(rec, alloc);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000337 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000338}
339
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000340uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000341 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000342 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000343}
344
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000345void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
346 SkASSERT(fBitmapShaderContext);
347 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
348}
349
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000350#ifndef SK_IGNORE_TO_STRING
351void SkPictureShader::toString(SkString* str) const {
352 static const char* gTileModeName[SkShader::kTileModeCount] = {
353 "clamp", "repeat", "mirror"
354 };
355
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700356 str->appendf("PictureShader: [%f:%f:%f:%f] ",
fmalitaddc4b462015-08-31 19:54:03 -0700357 fPicture->cullRect().fLeft,
358 fPicture->cullRect().fTop,
359 fPicture->cullRect().fRight,
360 fPicture->cullRect().fBottom);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000361
362 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
363
364 this->INHERITED::toString(str);
365}
366#endif
367
368#if SK_SUPPORT_GPU
Brian Salomonaff329b2017-08-11 09:40:37 -0400369std::unique_ptr<GrFragmentProcessor> SkPictureShader::asFragmentProcessor(
370 const AsFPArgs& args) const {
gen.kimb9ed8842015-05-03 22:36:30 -0700371 int maxTextureSize = 0;
brianosman839345d2016-07-22 11:04:53 -0700372 if (args.fContext) {
373 maxTextureSize = args.fContext->caps()->maxTextureSize();
gen.kimb9ed8842015-05-03 22:36:30 -0700374 }
brianosman839345d2016-07-22 11:04:53 -0700375 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*args.fViewMatrix, args.fLocalMatrix,
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400376 args.fDstColorSpaceInfo->colorSpace(),
377 maxTextureSize));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000378 if (!bitmapShader) {
bsalomonc21b09e2015-08-28 18:46:56 -0700379 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000380 }
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400381 return as_SB(bitmapShader)
382 ->asFragmentProcessor(SkShaderBase::AsFPArgs(args.fContext, args.fViewMatrix, nullptr,
383 args.fFilterQuality,
384 args.fDstColorSpaceInfo));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000385}
386#endif