blob: f263a2d4ad87a42756247e5879b8cb66908039fb [file] [log] [blame]
reed856e9d92015-09-30 12:21:45 -07001/*
2 * Copyright 2015 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
Ben Wagner729a23f2019-05-17 16:29:34 -04008#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/core/SkBitmapController.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkColorSpacePriv.h"
11#include "src/core/SkColorSpaceXformSteps.h"
12#include "src/core/SkRasterPipeline.h"
13#include "src/core/SkReadBuffer.h"
14#include "src/core/SkWriteBuffer.h"
15#include "src/image/SkImage_Base.h"
16#include "src/shaders/SkBitmapProcShader.h"
17#include "src/shaders/SkEmptyShader.h"
18#include "src/shaders/SkImageShader.h"
reed856e9d92015-09-30 12:21:45 -070019
Mike Reed587d0822017-06-23 16:49:12 -040020/**
21 * We are faster in clamp, so always use that tiling when we can.
22 */
Mike Reedfae8fce2019-04-03 10:27:45 -040023static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040024 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040025#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
26 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
27 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040028 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040029#else
Mike Reedfae8fce2019-04-03 10:27:45 -040030 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040031#endif
Mike Reed587d0822017-06-23 16:49:12 -040032}
33
Mike Klein1f313092018-01-03 10:30:21 -050034SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040035 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -050036 const SkMatrix* localMatrix,
37 bool clampAsIfUnpremul)
38 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070039 , fImage(std::move(img))
Mike Reed587d0822017-06-23 16:49:12 -040040 , fTileModeX(optimize(tmx, fImage->width()))
41 , fTileModeY(optimize(tmy, fImage->height()))
Mike Klein1f313092018-01-03 10:30:21 -050042 , fClampAsIfUnpremul(clampAsIfUnpremul)
reed856e9d92015-09-30 12:21:45 -070043{}
44
Mike Klein1f313092018-01-03 10:30:21 -050045// fClampAsIfUnpremul is always false when constructed through public APIs,
46// so there's no need to read or write it here.
47
reed60c9b582016-04-03 09:11:13 -070048sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
Mike Reede25b4472019-04-02 17:49:12 -040049 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
50 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Klein1f313092018-01-03 10:30:21 -050051 SkMatrix localMatrix;
52 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -070053 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -070054 if (!img) {
55 return nullptr;
56 }
Mike Reede25b4472019-04-02 17:49:12 -040057 return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix);
reed856e9d92015-09-30 12:21:45 -070058}
59
60void SkImageShader::flatten(SkWriteBuffer& buffer) const {
Mike Reedfae8fce2019-04-03 10:27:45 -040061 buffer.writeUInt((unsigned)fTileModeX);
62 buffer.writeUInt((unsigned)fTileModeY);
reed856e9d92015-09-30 12:21:45 -070063 buffer.writeMatrix(this->getLocalMatrix());
reed6b2d7ac2016-08-11 06:42:26 -070064 buffer.writeImage(fImage.get());
Mike Klein1f313092018-01-03 10:30:21 -050065 SkASSERT(fClampAsIfUnpremul == false);
reed856e9d92015-09-30 12:21:45 -070066}
67
68bool SkImageShader::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -040069 return fImage->isOpaque() &&
70 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
reed856e9d92015-09-30 12:21:45 -070071}
72
Mike Reede92aae62018-10-17 10:21:51 -040073#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malitaaf2769d2018-04-04 13:46:35 -040074static bool legacy_shader_can_handle(const SkMatrix& inv) {
75 if (!inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -050076 return false;
77 }
78
79 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
80 // out of range.
81 const SkScalar max_dev_coord = 32767.0f;
82 SkRect src;
83 SkAssertResult(inv.mapRect(&src, SkRect::MakeWH(max_dev_coord, max_dev_coord)));
84
Mike Reed1eb5ca42018-03-08 14:20:52 -050085 // take 1/4 of max signed 32bits so we have room to subtract local values
86 const SkScalar max_fixed32dot32 = SK_MaxS32 * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -050087 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
88 max_fixed32dot32, max_fixed32dot32).contains(src)) {
89 return false;
90 }
91
92 // legacy shader impl should be able to handle these matrices
93 return true;
94}
95
Florin Malita4aed1382017-05-25 10:38:07 -040096SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
97 SkArenaAlloc* alloc) const {
Brian Osman0e189372018-10-19 11:58:29 -040098 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -040099 return nullptr;
100 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400101 if (fImage->colorType() != kN32_SkColorType) {
102 return nullptr;
103 }
Florin Malitaaf2769d2018-04-04 13:46:35 -0400104 if (fTileModeX != fTileModeY) {
105 return nullptr;
106 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400107 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400108 return nullptr;
109 }
110
Mike Kleindac694d2018-12-18 10:13:52 -0500111 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500112 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500113 //
Mike Klein67761eb2018-12-18 10:16:53 -0500114 // We back off another bit to 32767 to make small amounts of
115 // intermediate math safe, e.g. in
116 //
117 // SkFixed fx = ...;
118 // fx = tile(fx + SK_Fixed1);
119 //
120 // we want to make sure (fx + SK_Fixed1) never overflows.
121 if (fImage-> width() > 32767 ||
122 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500123 return nullptr;
124 }
125
Florin Malitaaf2769d2018-04-04 13:46:35 -0400126 SkMatrix inv;
127 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
128 !legacy_shader_can_handle(inv)) {
129 return nullptr;
130 }
131
Mike Reed011d1662019-02-28 17:19:25 -0500132 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
133 return nullptr;
134 }
135
reed320a40d2016-08-02 06:12:06 -0700136 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed64acf4f2019-08-01 15:35:20 -0400137 as_IB(fImage.get()), rec, alloc);
reed856e9d92015-09-30 12:21:45 -0700138}
Mike Reede92aae62018-10-17 10:21:51 -0400139#endif
reed856e9d92015-09-30 12:21:45 -0700140
Mike Reedfae8fce2019-04-03 10:27:45 -0400141SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700142 if (texM) {
143 *texM = this->getLocalMatrix();
144 }
145 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400146 xy[0] = fTileModeX;
147 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700148 }
149 return const_cast<SkImage*>(fImage.get());
150}
151
Mike Klein1f313092018-01-03 10:30:21 -0500152sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400153 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -0500154 const SkMatrix* localMatrix,
155 bool clampAsIfUnpremul) {
Mike Kleindac694d2018-12-18 10:13:52 -0500156 if (!image) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000157 return sk_make_sp<SkEmptyShader>();
reed320a40d2016-08-02 06:12:06 -0700158 }
Mike Reede25b4472019-04-02 17:49:12 -0400159 return sk_sp<SkShader>{ new SkImageShader(image, tmx, tmy, localMatrix, clampAsIfUnpremul) };
reed856e9d92015-09-30 12:21:45 -0700160}
161
reed856e9d92015-09-30 12:21:45 -0700162///////////////////////////////////////////////////////////////////////////////////////////////////
163
164#if SK_SUPPORT_GPU
165
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500166#include "include/private/GrRecordingContext.h"
167#include "src/gpu/GrCaps.h"
168#include "src/gpu/GrColorSpaceInfo.h"
169#include "src/gpu/GrRecordingContextPriv.h"
170#include "src/gpu/SkGr.h"
171#include "src/gpu/effects/GrBicubicEffect.h"
172#include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700173
Mike Reedfae8fce2019-04-03 10:27:45 -0400174static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkTileMode tileMode) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400175 switch (tileMode) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400176 case SkTileMode::kClamp:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400177 return GrSamplerState::WrapMode::kClamp;
Mike Reedfae8fce2019-04-03 10:27:45 -0400178 case SkTileMode::kRepeat:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400179 return GrSamplerState::WrapMode::kRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400180 case SkTileMode::kMirror:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400181 return GrSamplerState::WrapMode::kMirrorRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400182 case SkTileMode::kDecal:
Michael Ludwigf23a1522018-12-10 11:36:13 -0500183 return GrSamplerState::WrapMode::kClampToBorder;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400184 }
185 SK_ABORT("Unknown tile mode.");
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400186}
187
Brian Salomonaff329b2017-08-11 09:40:37 -0400188std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500189 const GrFPArgs& args) const {
Florin Malitac6c5ead2018-04-11 15:33:40 -0400190 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700191 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400192 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700193 return nullptr;
194 }
reed856e9d92015-09-30 12:21:45 -0700195
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400196 GrSamplerState::WrapMode wrapModes[] = {tile_mode_to_wrap_mode(fTileModeX),
197 tile_mode_to_wrap_mode(fTileModeY)};
Michael Ludwigbe315a22018-12-17 09:50:51 -0500198
199 // If either domainX or domainY are un-ignored, a texture domain effect has to be used to
200 // implement the decal mode (while leaving non-decal axes alone). The wrap mode originally
201 // clamp-to-border is reset to clamp since the hw cannot implement it directly.
202 GrTextureDomain::Mode domainX = GrTextureDomain::kIgnore_Mode;
203 GrTextureDomain::Mode domainY = GrTextureDomain::kIgnore_Mode;
Robert Phillips9da87e02019-02-04 13:26:26 -0500204 if (!args.fContext->priv().caps()->clampToBorderSupport()) {
Michael Ludwigf23a1522018-12-10 11:36:13 -0500205 if (wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500206 domainX = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500207 wrapModes[0] = GrSamplerState::WrapMode::kClamp;
208 }
209 if (wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500210 domainY = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500211 wrapModes[1] = GrSamplerState::WrapMode::kClamp;
212 }
213 }
reed856e9d92015-09-30 12:21:45 -0700214
215 // Must set wrap and filter on the sampler before requesting a texture. In two places below
216 // we check the matrix scale factors to determine how to interpret the filter quality setting.
217 // This completely ignores the complexity of the drawVertices case where explicit local coords
218 // are provided by the caller.
219 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400220 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600221 fImage->width(), fImage->height(), args.fFilterQuality, *args.fViewMatrix, *lm,
Robert Phillips9da87e02019-02-04 13:26:26 -0500222 args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400223 GrSamplerState samplerState(wrapModes, textureFilterMode);
Robert Phillips67c18d62017-01-20 12:44:06 -0500224 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Osmane7fd8c32018-10-19 13:30:39 -0400225 sk_sp<GrTextureProxy> proxy(as_IB(fImage)->asTextureProxyRef(args.fContext, samplerState,
Brian Osman6064e1c2018-10-19 14:27:54 -0400226 scaleAdjust));
Robert Phillipsb726d582017-03-09 16:36:32 -0500227 if (!proxy) {
reed856e9d92015-09-30 12:21:45 -0700228 return nullptr;
229 }
230
Brian Salomone4bce012019-09-20 15:34:23 -0400231 bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType());
Robert Phillipsb726d582017-03-09 16:36:32 -0500232
Robert Phillips67c18d62017-01-20 12:44:06 -0500233 lmInverse.postScale(scaleAdjust[0], scaleAdjust[1]);
234
Brian Salomonaff329b2017-08-11 09:40:37 -0400235 std::unique_ptr<GrFragmentProcessor> inner;
reed856e9d92015-09-30 12:21:45 -0700236 if (doBicubic) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500237 // domainX and domainY will properly apply the decal effect with the texture domain used in
238 // the bicubic filter if clamp to border was unsupported in hardware
Brian Salomona86fc7a2019-05-28 20:42:58 -0400239 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Osmane22dba82019-03-13 10:22:28 -0400240 inner = GrBicubicEffect::Make(std::move(proxy), lmInverse, wrapModes, domainX, domainY,
Brian Salomon1127c0b2019-06-13 20:22:10 +0000241 kDir, fImage->alphaType());
reed856e9d92015-09-30 12:21:45 -0700242 } else {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500243 if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) {
244 SkRect domain = GrTextureDomain::MakeTexelDomain(
245 SkIRect::MakeWH(proxy->width(), proxy->height()),
246 domainX, domainY);
247 inner = GrTextureDomainEffect::Make(std::move(proxy), lmInverse, domain,
248 domainX, domainY, samplerState);
249 } else {
250 inner = GrSimpleTextureEffect::Make(std::move(proxy), lmInverse, samplerState);
251 }
reed856e9d92015-09-30 12:21:45 -0700252 }
Brian Osman6064e1c2018-10-19 14:27:54 -0400253 inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(),
Brian Osman21fc5ce2018-08-27 20:36:19 +0000254 fImage->alphaType(),
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400255 args.fDstColorSpaceInfo->colorSpace());
Robert Phillipsb726d582017-03-09 16:36:32 -0500256 if (isAlphaOnly) {
bungeman06ca8ec2016-06-09 08:01:03 -0700257 return inner;
Brian Salomonc0d79e52019-04-10 15:02:11 -0400258 } else if (args.fInputColorIsOpaque) {
259 return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700260 }
Mike Reed28eaed22018-02-01 11:24:53 -0500261 return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner));
reed856e9d92015-09-30 12:21:45 -0700262}
263
264#endif
reed320a40d2016-08-02 06:12:06 -0700265
266///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500267#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700268
Mike Reede25b4472019-04-02 17:49:12 -0400269sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy,
270 const SkMatrix* localMatrix, SkCopyPixelsMode cpm) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000271 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm),
272 tmx, tmy, localMatrix);
reed320a40d2016-08-02 06:12:06 -0700273}
274
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400275sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400276 SkTileMode tmx, SkTileMode tmy,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400277 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
278 auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode);
279 if (!s) {
280 return nullptr;
281 }
282 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
283 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
284 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
285 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400286 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400287 }
288 return s;
289}
290
Brian Salomon23356442018-11-30 15:33:19 -0500291void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700292
Mike Reed9318a6c2019-08-16 16:16:25 -0400293class SkImageStageUpdater : public SkStageUpdater {
294public:
295 const SkImageShader* fShader;
296
297 float fMatrixStorage[6];
298
299#if 0 // TODO: when we support mipmaps
300 SkRasterPipeline_GatherCtx* fGather;
301 SkRasterPipeline_TileCtx* fLimitX;
302 SkRasterPipeline_TileCtx* fLimitY;
303 SkRasterPipeline_DecalTileCtx* fDecal;
304#endif
305
306 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
307 SkMatrix matrix;
308 return fShader->computeTotalInverse(ctm, localM, &matrix) &&
309 matrix.asAffine(fMatrixStorage);
310 }
311};
312
313bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
314 if (updater &&
315 (rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality ||
316 rec.fCTM.hasPerspective()))
317 {
318 // TODO: handle these cases
319 // medium: recall RequestBitmap and update width/height accordingly
320 // perspt: store 9 floats and use persp stage
321 return false;
322 }
323
Mike Reed1d8c42e2017-08-29 14:58:19 -0400324 SkRasterPipeline* p = rec.fPipeline;
325 SkArenaAlloc* alloc = rec.fAlloc;
Mike Reed9318a6c2019-08-16 16:16:25 -0400326 auto quality = rec.fPaint.getFilterQuality();
Mike Reed1d8c42e2017-08-29 14:58:19 -0400327
Florin Malita7558e4d2018-02-07 10:05:53 -0500328 SkMatrix matrix;
329 if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500330 return false;
331 }
Mike Klein06a65e22016-11-17 12:39:09 -0500332
Mike Reed64acf4f2019-08-01 15:35:20 -0400333 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
334 matrix, quality, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500335 if (!state) {
336 return false;
337 }
338
339 const SkPixmap& pm = state->pixmap();
340 matrix = state->invMatrix();
341 quality = state->quality();
342 auto info = pm.info();
343
Mike Kleine8de0242018-03-10 12:37:11 -0500344 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400345
346 if (updater) {
347 p->append(SkRasterPipeline::matrix_2x3, updater->fMatrixStorage);
348 } else {
349 // When the matrix is just an integer translate, bilerp == nearest neighbor.
350 if (quality == kLow_SkFilterQuality &&
351 matrix.getType() <= SkMatrix::kTranslate_Mask &&
352 matrix.getTranslateX() == (int)matrix.getTranslateX() &&
353 matrix.getTranslateY() == (int)matrix.getTranslateY()) {
354 quality = kNone_SkFilterQuality;
355 }
356
357 // See skia:4649 and the GM image_scale_aligned.
358 if (quality == kNone_SkFilterQuality) {
359 if (matrix.getScaleX() >= 0) {
360 matrix.setTranslateX(nextafterf(matrix.getTranslateX(),
361 floorf(matrix.getTranslateX())));
362 }
363 if (matrix.getScaleY() >= 0) {
364 matrix.setTranslateY(nextafterf(matrix.getTranslateY(),
365 floorf(matrix.getTranslateY())));
366 }
367 }
368 p->append_matrix(alloc, matrix);
369 }
Mike Klein06a65e22016-11-17 12:39:09 -0500370
Mike Kleinb11ab572018-10-24 06:42:14 -0400371 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500372 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400373 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400374 gather->width = pm.width();
375 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400376
Mike Kleinb11ab572018-10-24 06:42:14 -0400377 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
378 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400379 limit_x->scale = pm.width();
380 limit_x->invScale = 1.0f / pm.width();
381 limit_y->scale = pm.height();
382 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400383
Mike Kleinb11ab572018-10-24 06:42:14 -0400384 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400385 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
386 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400387 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500388 decal_ctx->limit_x = limit_x->scale;
389 decal_ctx->limit_y = limit_y->scale;
390 }
391
Mike Reed9318a6c2019-08-16 16:16:25 -0400392#if 0 // TODO: when we support kMedium
393 if (updator && (quality == kMedium_SkFilterQuality)) {
394 // if we change levels in mipmap, we need to update the scales (and invScales)
395 updator->fGather = gather;
396 updator->fLimitX = limit_x;
397 updator->fLimitY = limit_y;
398 updator->fDecal = decal_ctx;
399 }
400#endif
401
Mike Kleinb04c3522016-11-28 11:55:58 -0500402 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500403 if (decal_x_and_y) {
404 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
405 } else {
406 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400407 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
408 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
409 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
410 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500411 }
412 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400413 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
414 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
415 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
416 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500417 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500418 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500419
Mike Kleinac568a92018-01-25 09:09:32 -0500420 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500421 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500422 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400423 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
424 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500425 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
426 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400427 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
428 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
429 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500430 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
431 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400432 case kR16G16B16A16_unorm_SkColorType:
433 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600434 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500435 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400436 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500437
Mike Kleinb1df5e52018-10-17 17:06:03 -0400438 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
439 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400440
Mike Kleinac568a92018-01-25 09:09:32 -0500441 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
442 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400443
Mike Kleinac568a92018-01-25 09:09:32 -0500444 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
445 p->append(SkRasterPipeline::force_opaque ); break;
446
Mike Klein1a3eb522018-10-18 10:11:00 -0400447 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
448 p->append(SkRasterPipeline::swap_rb ); break;
449
Mike Kleinb70990e2019-02-28 10:03:27 -0600450 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500451 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500452 if (decal_ctx) {
453 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
454 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500455 };
456
Mike Klein1fa9c432017-12-11 09:59:47 -0500457 auto append_misc = [&] {
Mike Kleinb82edcc2018-07-10 18:25:03 +0000458 // TODO: if ref.fDstCS isn't null, we'll premul here then immediately unpremul
459 // to do the color space transformation. Might be possible to streamline.
Mike Klein1fa9c432017-12-11 09:59:47 -0500460 if (info.colorType() == kAlpha_8_SkColorType) {
Mike Kleinb82edcc2018-07-10 18:25:03 +0000461 // The color for A8 images comes from the (sRGB) paint color.
Mike Kleinbe569492018-09-14 09:34:21 -0400462 p->append_set_rgb(alloc, rec.fPaint.getColor4f());
Mike Kleinb82edcc2018-07-10 18:25:03 +0000463 p->append(SkRasterPipeline::premul);
464 } else if (info.alphaType() == kUnpremul_SkAlphaType) {
465 // Convert unpremul images to premul before we carry on with the rest of the pipeline.
Mike Klein47cf0482018-02-09 18:57:54 +0000466 p->append(SkRasterPipeline::premul);
467 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000468
Mike Klein1fa9c432017-12-11 09:59:47 -0500469 if (quality > kLow_SkFilterQuality) {
470 // Bicubic filtering naturally produces out of range values on both sides.
471 p->append(SkRasterPipeline::clamp_0);
Mike Klein1f313092018-01-03 10:30:21 -0500472 p->append(fClampAsIfUnpremul ? SkRasterPipeline::clamp_1
473 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500474 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000475
476 if (rec.fDstCS) {
477 // If color managed, convert from premul source all the way to premul dst color space.
478 auto srcCS = info.colorSpace();
479 if (!srcCS || info.colorType() == kAlpha_8_SkColorType) {
480 // We treat untagged images as sRGB.
481 // A8 images get their r,g,b from the paint color, so they're also sRGB.
Mike Kleine28a6b52018-07-25 13:05:17 -0400482 srcCS = sk_srgb_singleton();
Mike Kleinb82edcc2018-07-10 18:25:03 +0000483 }
Mike Klein8f3d36c2018-08-14 10:28:05 -0400484 alloc->make<SkColorSpaceXformSteps>(srcCS , kPremul_SkAlphaType,
485 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinc23d1ab2018-10-29 09:39:52 -0400486 ->apply(p, info.colorType());
Mike Kleinb82edcc2018-07-10 18:25:03 +0000487 }
488
Mike Klein1fa9c432017-12-11 09:59:47 -0500489 return true;
490 };
491
Mike Reed9318a6c2019-08-16 16:16:25 -0400492 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400493 auto ct = info.colorType();
494 if (true
495 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
496 && quality == kLow_SkFilterQuality
Mike Reedfae8fce2019-04-03 10:27:45 -0400497 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500498
499 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400500 if (ct == kBGRA_8888_SkColorType) {
501 p->append(SkRasterPipeline::swap_rb);
502 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500503 return append_misc();
504 }
Mike Reed78eedba2019-07-31 16:39:15 -0400505 if (true
Mike Klein01005622019-08-13 12:22:17 -0400506 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
507 && quality == kLow_SkFilterQuality
508 && fTileModeX != SkTileMode::kDecal // TODO decal too?
509 && fTileModeY != SkTileMode::kDecal) {
510
511 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
512 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
513 ctx->ct = ct;
514 ctx->tileX = fTileModeX;
515 ctx->tileY = fTileModeY;
516 ctx->invWidth = 1.0f / ctx->width;
517 ctx->invHeight = 1.0f / ctx->height;
518 p->append(SkRasterPipeline::bilinear, ctx);
519 return append_misc();
520 }
521 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400522 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
523 && quality == kHigh_SkFilterQuality
524 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
525
526 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
527 if (ct == kBGRA_8888_SkColorType) {
528 p->append(SkRasterPipeline::swap_rb);
529 }
530 return append_misc();
531 }
Mike Klein01005622019-08-13 12:22:17 -0400532 if (true
533 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
534 && quality == kHigh_SkFilterQuality
535 && fTileModeX != SkTileMode::kDecal // TODO decal too?
536 && fTileModeY != SkTileMode::kDecal) {
537
538 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
539 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
540 ctx->ct = ct;
541 ctx->tileX = fTileModeX;
542 ctx->tileY = fTileModeY;
543 ctx->invWidth = 1.0f / ctx->width;
544 ctx->invHeight = 1.0f / ctx->height;
545 p->append(SkRasterPipeline::bicubic, ctx);
546 return append_misc();
547 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500548
Mike Kleinb11ab572018-10-24 06:42:14 -0400549 SkRasterPipeline_SamplerCtx* sampler = nullptr;
Mike Klein0a904492017-04-12 12:52:48 -0400550 if (quality != kNone_SkFilterQuality) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400551 sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400552 }
553
Mike Kleinb0b17d12016-12-09 16:25:44 -0500554 auto sample = [&](SkRasterPipeline::StockStage setup_x,
555 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400556 p->append(setup_x, sampler);
557 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500558 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400559 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500560 };
561
Mike Kleinf7f883b2016-11-21 15:09:45 -0500562 if (quality == kNone_SkFilterQuality) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500563 append_tiling_and_gather();
Mike Kleinb0b17d12016-12-09 16:25:44 -0500564 } else if (quality == kLow_SkFilterQuality) {
Mike Klein0a904492017-04-12 12:52:48 -0400565 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500566
567 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
568 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
569 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
570 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
571
572 p->append(SkRasterPipeline::move_dst_src);
Mike Klein1fa9c432017-12-11 09:59:47 -0500573
Mike Klein46e66a22016-11-21 16:19:34 -0500574 } else {
Mike Klein0a904492017-04-12 12:52:48 -0400575 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500576
577 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
578 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
579 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
580 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
581
582 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
583 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
584 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
585 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
586
587 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
588 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
589 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
590 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
591
592 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
593 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
594 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
595 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
596
Mike Kleinb04c3522016-11-28 11:55:58 -0500597 p->append(SkRasterPipeline::move_dst_src);
Mike Klein06a65e22016-11-17 12:39:09 -0500598 }
599
Mike Klein1fa9c432017-12-11 09:59:47 -0500600 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500601}
Mike Reed9318a6c2019-08-16 16:16:25 -0400602
603bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
604 return this->doStages(rec, nullptr);
605}
606
607SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
608 auto updater = rec.fAlloc->make<SkImageStageUpdater>();
609 updater->fShader = this;
610 return this->doStages(rec, updater) ? updater : nullptr;
611}
612