blob: 4bef53da75f29c43846b8053ebbf885679f283de [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,
35 uint32_t pictureID,
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))
43 , fPictureID(pictureID)
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) +
55 sizeof(fPictureID) +
fmalita23df2d62014-10-22 07:39:08 -070056 sizeof(fTile) +
57 sizeof(fTmx) + sizeof(fTmy) +
58 sizeof(fScale) +
Matt Sarett28a7ad22017-04-21 15:12:34 -040059 sizeof(fLocalMatrixStorage) +
60 sizeof(fBlendBehavior);
fmalita23df2d62014-10-22 07:39:08 -070061 // This better be packed.
Matt Sarette71db442017-04-21 15:06:51 -040062 SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - (uint32_t*)&fColorSpace) == keySize);
reed7eeba252015-02-24 13:54:23 -080063 this->init(&gBitmapSkaderKeyNamespaceLabel, 0, keySize);
fmalita23df2d62014-10-22 07:39:08 -070064 }
65
66private:
Matt Sarett28a7ad22017-04-21 15:12:34 -040067 sk_sp<SkColorSpace> fColorSpace;
68 uint32_t fPictureID;
69 SkRect fTile;
70 SkShader::TileMode fTmx, fTmy;
71 SkSize fScale;
72 SkScalar fLocalMatrixStorage[9];
73 SkTransferFunctionBehavior fBlendBehavior;
fmalita23df2d62014-10-22 07:39:08 -070074
75 SkDEBUGCODE(uint32_t fEndOfStruct;)
76};
77
78struct BitmapShaderRec : public SkResourceCache::Rec {
fmalitac9a9ca92016-10-12 13:43:43 -070079 BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader)
fmalita23df2d62014-10-22 07:39:08 -070080 : fKey(key)
fmalitac9a9ca92016-10-12 13:43:43 -070081 , fShader(SkRef(tileShader)) {}
fmalita23df2d62014-10-22 07:39:08 -070082
Hal Canary704cd322016-11-07 14:13:52 -050083 BitmapShaderKey fKey;
84 sk_sp<SkShader> fShader;
85 size_t fBitmapBytes;
fmalita23df2d62014-10-22 07:39:08 -070086
mtklein36352bf2015-03-25 18:17:31 -070087 const Key& getKey() const override { return fKey; }
88 size_t bytesUsed() const override {
fmalitac9a9ca92016-10-12 13:43:43 -070089 // Just the record overhead -- the actual pixels are accounted by SkImageCacherator.
90 return sizeof(fKey) + sizeof(SkImageShader);
fmalita23df2d62014-10-22 07:39:08 -070091 }
reed216b6432015-08-19 12:25:40 -070092 const char* getCategory() const override { return "bitmap-shader"; }
93 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
fmalita23df2d62014-10-22 07:39:08 -070094
95 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader) {
96 const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec);
Hal Canary704cd322016-11-07 14:13:52 -050097 sk_sp<SkShader>* result = reinterpret_cast<sk_sp<SkShader>*>(contextShader);
fmalita23df2d62014-10-22 07:39:08 -070098
Hal Canary704cd322016-11-07 14:13:52 -050099 *result = rec.fShader;
fmalita387a01a2014-12-10 12:17:58 -0800100
fmalitaddc4b462015-08-31 19:54:03 -0700101 // The bitmap shader is backed by an image generator, thus it can always re-generate its
102 // pixels if discarded.
103 return true;
fmalita23df2d62014-10-22 07:39:08 -0700104 }
105};
106
fmalita171e5b72014-10-22 11:20:40 -0700107} // namespace
108
reed7fb4f8b2016-03-11 04:33:52 -0800109SkPictureShader::SkPictureShader(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
Matt Sarett28a7ad22017-04-21 15:12:34 -0400110 const SkMatrix* localMatrix, const SkRect* tile,
111 sk_sp<SkColorSpace> colorSpace)
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000112 : INHERITED(localMatrix)
reed8a21c9f2016-03-08 18:50:00 -0800113 , fPicture(std::move(picture))
114 , fTile(tile ? *tile : fPicture->cullRect())
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000115 , fTmx(tmx)
Matt Sarett28a7ad22017-04-21 15:12:34 -0400116 , fTmy(tmy)
117 , fColorSpace(std::move(colorSpace))
118{}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000119
reed7fb4f8b2016-03-11 04:33:52 -0800120sk_sp<SkShader> SkPictureShader::Make(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800121 const SkMatrix* localMatrix, const SkRect* tile) {
Robert Phillips54be5c92017-02-11 01:19:30 +0000122 if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
reed8a21c9f2016-03-08 18:50:00 -0800123 return SkShader::MakeEmptyShader();
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000124 }
Matt Sarett28a7ad22017-04-21 15:12:34 -0400125 return sk_sp<SkShader>(new SkPictureShader(std::move(picture), tmx, tmy, localMatrix, tile,
126 nullptr));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000127}
128
reed60c9b582016-04-03 09:11:13 -0700129sk_sp<SkFlattenable> SkPictureShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700130 SkMatrix lm;
131 buffer.readMatrix(&lm);
132 TileMode mx = (TileMode)buffer.read32();
133 TileMode my = (TileMode)buffer.read32();
134 SkRect tile;
135 buffer.readRect(&tile);
mtklein76be9c82015-05-20 12:05:15 -0700136
reed8a21c9f2016-03-08 18:50:00 -0800137 sk_sp<SkPicture> picture;
hendrikw446ee672015-06-16 09:28:37 -0700138
139 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700140 // Newer code won't serialize pictures in disallow-cross-process-picture mode.
141 // Assert that they didn't serialize anything except a false here.
142 buffer.validate(!buffer.readBool());
hendrikw446ee672015-06-16 09:28:37 -0700143 } else {
Mike Reed70bc94f2017-06-08 12:45:52 -0400144 bool didSerialize = buffer.readBool();
145 if (didSerialize) {
reedca2622b2016-03-18 07:25:55 -0700146 picture = SkPicture::MakeFromBuffer(buffer);
mtklein76be9c82015-05-20 12:05:15 -0700147 }
148 }
reed60c9b582016-04-03 09:11:13 -0700149 return SkPictureShader::Make(picture, mx, my, &lm, &tile);
reed9fa60da2014-08-21 07:59:51 -0700150}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000151
reed9fa60da2014-08-21 07:59:51 -0700152void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
153 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000154 buffer.write32(fTmx);
155 buffer.write32(fTmy);
fmalitab5f78262014-08-06 13:07:15 -0700156 buffer.writeRect(fTile);
mtklein76be9c82015-05-20 12:05:15 -0700157
mtklein76be9c82015-05-20 12:05:15 -0700158 // The deserialization code won't trust that our serialized picture is safe to deserialize.
159 // So write a 'false' telling it that we're not serializing a picture.
hendrikw446ee672015-06-16 09:28:37 -0700160 if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) {
mtklein76be9c82015-05-20 12:05:15 -0700161 buffer.writeBool(false);
hendrikw446ee672015-06-16 09:28:37 -0700162 } else {
mtklein76be9c82015-05-20 12:05:15 -0700163 buffer.writeBool(true);
164 fPicture->flatten(buffer);
165 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000166}
167
reed8a21c9f2016-03-08 18:50:00 -0800168sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, const SkMatrix* localM,
Brian Osman138ea972016-12-16 11:55:18 -0500169 SkColorSpace* dstColorSpace,
reed8a21c9f2016-03-08 18:50:00 -0800170 const int maxTextureSize) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700171 SkASSERT(fPicture && !fPicture->cullRect().isEmpty());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000172
173 SkMatrix m;
robertphillips44c31282015-09-03 12:58:48 -0700174 m.setConcat(viewMatrix, this->getLocalMatrix());
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000175 if (localM) {
176 m.preConcat(*localM);
177 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000178
179 // Use a rotation-invariant scale
180 SkPoint scale;
reedadf99902015-03-19 16:10:54 -0700181 //
182 // TODO: replace this with decomposeScale() -- but beware LayoutTest rebaselines!
183 //
halcanary96fcdcc2015-08-27 07:41:13 -0700184 if (!SkDecomposeUpper2x2(m, nullptr, &scale, nullptr)) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000185 // Decomposition failed, use an approximation.
186 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
187 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
188 }
fmalitab0878792015-01-15 10:45:56 -0800189 SkSize scaledSize = SkSize::Make(SkScalarAbs(scale.x() * fTile.width()),
190 SkScalarAbs(scale.y() * fTile.height()));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000191
fmalita511005b2015-03-13 06:50:44 -0700192 // Clamp the tile size to about 4M pixels
193 static const SkScalar kMaxTileArea = 2048 * 2048;
Mike Reeda99b6ce2017-02-04 11:04:26 -0500194 SkScalar tileArea = scaledSize.width() * scaledSize.height();
fmalitabb204f42014-08-07 08:39:24 -0700195 if (tileArea > kMaxTileArea) {
reed80ea19c2015-05-12 10:37:34 -0700196 SkScalar clampScale = SkScalarSqrt(kMaxTileArea / tileArea);
Mike Reeda99b6ce2017-02-04 11:04:26 -0500197 scaledSize.set(scaledSize.width() * clampScale,
198 scaledSize.height() * clampScale);
fmalitabb204f42014-08-07 08:39:24 -0700199 }
gen.kimb9ed8842015-05-03 22:36:30 -0700200#if SK_SUPPORT_GPU
201 // Scale down the tile size if larger than maxTextureSize for GPU Path or it should fail on create texture
202 if (maxTextureSize) {
203 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkf469fc02015-10-14 17:33:29 -0700204 SkScalar downScale = maxTextureSize / SkMaxScalar(scaledSize.width(), scaledSize.height());
Mike Reeda99b6ce2017-02-04 11:04:26 -0500205 scaledSize.set(SkScalarFloorToScalar(scaledSize.width() * downScale),
206 SkScalarFloorToScalar(scaledSize.height() * downScale));
gen.kimb9ed8842015-05-03 22:36:30 -0700207 }
208 }
209#endif
fmalitabb204f42014-08-07 08:39:24 -0700210
fmalita85c6f982016-02-29 09:18:31 -0800211#ifdef SK_SUPPORT_LEGACY_PICTURESHADER_ROUNDING
212 const SkISize tileSize = scaledSize.toRound();
213#else
214 const SkISize tileSize = scaledSize.toCeil();
215#endif
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000216 if (tileSize.isEmpty()) {
reed8a21c9f2016-03-08 18:50:00 -0800217 return SkShader::MakeEmptyShader();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000218 }
219
fmalitabb204f42014-08-07 08:39:24 -0700220 // The actual scale, compensating for rounding & clamping.
fmalita85c6f982016-02-29 09:18:31 -0800221 const SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(),
222 SkIntToScalar(tileSize.height()) / fTile.height());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000223
Matt Sarett28a7ad22017-04-21 15:12:34 -0400224 // |fColorSpace| will only be set when using an SkColorSpaceXformCanvas to do pre-draw xforms.
225 // This canvas is strictly for legacy mode. A non-null |dstColorSpace| indicates that we
226 // should perform color correct rendering and xform at draw time.
227 SkASSERT(!fColorSpace || !dstColorSpace);
228 sk_sp<SkColorSpace> keyCS = dstColorSpace ? sk_ref_sp(dstColorSpace) : fColorSpace;
229 SkTransferFunctionBehavior blendBehavior = dstColorSpace ? SkTransferFunctionBehavior::kRespect
230 : SkTransferFunctionBehavior::kIgnore;
231
reed8a21c9f2016-03-08 18:50:00 -0800232 sk_sp<SkShader> tileShader;
Matt Sarett28a7ad22017-04-21 15:12:34 -0400233 BitmapShaderKey key(std::move(keyCS),
Matt Sarette71db442017-04-21 15:06:51 -0400234 fPicture->uniqueID(),
fmalita23df2d62014-10-22 07:39:08 -0700235 fTile,
236 fTmx,
237 fTmy,
238 tileScale,
Matt Sarett28a7ad22017-04-21 15:12:34 -0400239 this->getLocalMatrix(),
240 blendBehavior);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000241
fmalita171e5b72014-10-22 11:20:40 -0700242 if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
robertphillips25a5b0d2015-09-28 09:32:50 -0700243 SkMatrix tileMatrix;
244 tileMatrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.height()),
fmalita1b46a572016-02-01 02:34:03 -0800245 SkMatrix::kFill_ScaleToFit);
246
Matt Sarett9df70bb2017-02-14 13:50:43 -0500247 sk_sp<SkImage> tileImage = SkImage::MakeFromGenerator(
Mike Reed185130c2017-02-15 15:14:16 -0500248 SkPictureImageGenerator::Make(tileSize, fPicture, &tileMatrix, nullptr,
249 SkImage::BitDepth::kU8, sk_ref_sp(dstColorSpace)));
fmalita1b46a572016-02-01 02:34:03 -0800250 if (!tileImage) {
fmalitaddc4b462015-08-31 19:54:03 -0700251 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000252 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000253
Matt Sarett28a7ad22017-04-21 15:12:34 -0400254 if (fColorSpace) {
255 tileImage = tileImage->makeColorSpace(fColorSpace, SkTransferFunctionBehavior::kIgnore);
256 }
257
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000258 SkMatrix shaderMatrix = this->getLocalMatrix();
259 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
reed5671c5b2016-03-09 14:47:34 -0800260 tileShader = tileImage->makeShader(fTmx, fTmy, &shaderMatrix);
fmalita23df2d62014-10-22 07:39:08 -0700261
fmalitac9a9ca92016-10-12 13:43:43 -0700262 SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get()));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000263 }
264
reed8a21c9f2016-03-08 18:50:00 -0800265 return tileShader;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000266}
267
Yuqian Li8d2fb472017-01-30 11:33:46 -0500268bool SkPictureShader::onAppendStages(SkRasterPipeline* p, SkColorSpace* cs, SkArenaAlloc* alloc,
269 const SkMatrix& ctm, const SkPaint& paint,
270 const SkMatrix* localMatrix) const {
271 // Keep bitmapShader alive by using alloc instead of stack memory
272 auto& bitmapShader = *alloc->make<sk_sp<SkShader>>();
273 bitmapShader = this->refBitmapShader(ctm, localMatrix, cs);
Florin Malita4aed1382017-05-25 10:38:07 -0400274 return bitmapShader && as_SB(bitmapShader)->appendStages(p, cs, alloc, ctm, paint);
Yuqian Li8d2fb472017-01-30 11:33:46 -0500275}
276
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000277/////////////////////////////////////////////////////////////////////////////////////////
Florin Malita4aed1382017-05-25 10:38:07 -0400278SkShaderBase::Context* SkPictureShader::onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc)
Herb Derby83e939b2017-02-07 14:25:11 -0500279const {
280 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix,
281 rec.fDstColorSpace));
282 if (!bitmapShader) {
283 return nullptr;
284 }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000285
Herb Derby83e939b2017-02-07 14:25:11 -0500286 PictureShaderContext* ctx =
287 alloc->make<PictureShaderContext>(*this, rec, std::move(bitmapShader), alloc);
halcanary96fcdcc2015-08-27 07:41:13 -0700288 if (nullptr == ctx->fBitmapShaderContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700289 ctx = nullptr;
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000290 }
291 return ctx;
292}
293
Matt Sarett28a7ad22017-04-21 15:12:34 -0400294sk_sp<SkShader> SkPictureShader::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
295 return sk_sp<SkPictureShader>(new SkPictureShader(fPicture, fTmx, fTmy, &this->getLocalMatrix(),
296 &fTile, xformer->dst()));
297}
298
Herb Derby83e939b2017-02-07 14:25:11 -0500299/////////////////////////////////////////////////////////////////////////////////////////
300
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000301SkPictureShader::PictureShaderContext::PictureShaderContext(
Herb Derby83e939b2017-02-07 14:25:11 -0500302 const SkPictureShader& shader, const ContextRec& rec, sk_sp<SkShader> bitmapShader,
303 SkArenaAlloc* alloc)
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000304 : INHERITED(shader, rec)
reed8a21c9f2016-03-08 18:50:00 -0800305 , fBitmapShader(std::move(bitmapShader))
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000306{
Florin Malita4aed1382017-05-25 10:38:07 -0400307 fBitmapShaderContext = as_SB(fBitmapShader)->makeContext(rec, alloc);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000308 //if fBitmapShaderContext is null, we are invalid
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000309}
310
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000311uint32_t SkPictureShader::PictureShaderContext::getFlags() const {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000312 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000313 return fBitmapShaderContext->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000314}
315
Florin Malita4aed1382017-05-25 10:38:07 -0400316SkShaderBase::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000317 SkASSERT(fBitmapShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000318 return fBitmapShaderContext->asAShadeProc(ctx);
319}
320
321void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
322 SkASSERT(fBitmapShaderContext);
323 fBitmapShaderContext->shadeSpan(x, y, dstC, count);
324}
325
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000326#ifndef SK_IGNORE_TO_STRING
327void SkPictureShader::toString(SkString* str) const {
328 static const char* gTileModeName[SkShader::kTileModeCount] = {
329 "clamp", "repeat", "mirror"
330 };
331
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700332 str->appendf("PictureShader: [%f:%f:%f:%f] ",
fmalitaddc4b462015-08-31 19:54:03 -0700333 fPicture->cullRect().fLeft,
334 fPicture->cullRect().fTop,
335 fPicture->cullRect().fRight,
336 fPicture->cullRect().fBottom);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000337
338 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
339
340 this->INHERITED::toString(str);
341}
342#endif
343
344#if SK_SUPPORT_GPU
brianosman839345d2016-07-22 11:04:53 -0700345sk_sp<GrFragmentProcessor> SkPictureShader::asFragmentProcessor(const AsFPArgs& args) const {
gen.kimb9ed8842015-05-03 22:36:30 -0700346 int maxTextureSize = 0;
brianosman839345d2016-07-22 11:04:53 -0700347 if (args.fContext) {
348 maxTextureSize = args.fContext->caps()->maxTextureSize();
gen.kimb9ed8842015-05-03 22:36:30 -0700349 }
brianosman839345d2016-07-22 11:04:53 -0700350 sk_sp<SkShader> bitmapShader(this->refBitmapShader(*args.fViewMatrix, args.fLocalMatrix,
Brian Osman138ea972016-12-16 11:55:18 -0500351 args.fDstColorSpace, maxTextureSize));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000352 if (!bitmapShader) {
bsalomonc21b09e2015-08-28 18:46:56 -0700353 return nullptr;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000354 }
Florin Malita4aed1382017-05-25 10:38:07 -0400355 return as_SB(bitmapShader)->asFragmentProcessor(SkShaderBase::AsFPArgs(
Brian Osman61624f02016-12-09 14:51:59 -0500356 args.fContext, args.fViewMatrix, nullptr, args.fFilterQuality, args.fDstColorSpace));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000357}
358#endif