blob: 30a8c4cc50392867b379e4d1d1ee846310360e12 [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"
Mike Klein37bc8f92019-10-21 13:10:07 -050012#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkRasterPipeline.h"
14#include "src/core/SkReadBuffer.h"
15#include "src/core/SkWriteBuffer.h"
16#include "src/image/SkImage_Base.h"
17#include "src/shaders/SkBitmapProcShader.h"
18#include "src/shaders/SkEmptyShader.h"
19#include "src/shaders/SkImageShader.h"
reed856e9d92015-09-30 12:21:45 -070020
Mike Reed587d0822017-06-23 16:49:12 -040021/**
22 * We are faster in clamp, so always use that tiling when we can.
23 */
Mike Reedfae8fce2019-04-03 10:27:45 -040024static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040025 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040026#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
27 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
28 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040029 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040030#else
Mike Reedfae8fce2019-04-03 10:27:45 -040031 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040032#endif
Mike Reed587d0822017-06-23 16:49:12 -040033}
34
Mike Klein1f313092018-01-03 10:30:21 -050035SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040036 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -050037 const SkMatrix* localMatrix,
38 bool clampAsIfUnpremul)
39 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070040 , fImage(std::move(img))
Mike Reed587d0822017-06-23 16:49:12 -040041 , fTileModeX(optimize(tmx, fImage->width()))
42 , fTileModeY(optimize(tmy, fImage->height()))
Mike Klein1f313092018-01-03 10:30:21 -050043 , fClampAsIfUnpremul(clampAsIfUnpremul)
reed856e9d92015-09-30 12:21:45 -070044{}
45
Mike Klein1f313092018-01-03 10:30:21 -050046// fClampAsIfUnpremul is always false when constructed through public APIs,
47// so there's no need to read or write it here.
48
reed60c9b582016-04-03 09:11:13 -070049sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
Mike Reede25b4472019-04-02 17:49:12 -040050 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
51 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Klein1f313092018-01-03 10:30:21 -050052 SkMatrix localMatrix;
53 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -070054 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -070055 if (!img) {
56 return nullptr;
57 }
Mike Reede25b4472019-04-02 17:49:12 -040058 return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix);
reed856e9d92015-09-30 12:21:45 -070059}
60
61void SkImageShader::flatten(SkWriteBuffer& buffer) const {
Mike Reedfae8fce2019-04-03 10:27:45 -040062 buffer.writeUInt((unsigned)fTileModeX);
63 buffer.writeUInt((unsigned)fTileModeY);
reed856e9d92015-09-30 12:21:45 -070064 buffer.writeMatrix(this->getLocalMatrix());
reed6b2d7ac2016-08-11 06:42:26 -070065 buffer.writeImage(fImage.get());
Mike Klein1f313092018-01-03 10:30:21 -050066 SkASSERT(fClampAsIfUnpremul == false);
reed856e9d92015-09-30 12:21:45 -070067}
68
69bool SkImageShader::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -040070 return fImage->isOpaque() &&
71 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
reed856e9d92015-09-30 12:21:45 -070072}
73
Mike Reede92aae62018-10-17 10:21:51 -040074#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malitaaf2769d2018-04-04 13:46:35 -040075static bool legacy_shader_can_handle(const SkMatrix& inv) {
Mike Klein37bc8f92019-10-21 13:10:07 -050076 if (inv.hasPerspective()) {
77 return false;
78 }
79
80 // Scale+translate methods are always present, but affine might not be.
81 if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -050082 return false;
83 }
84
85 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
86 // out of range.
87 const SkScalar max_dev_coord = 32767.0f;
Mike Klein37bc8f92019-10-21 13:10:07 -050088 const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord));
Mike Reeda12c4192018-02-01 16:34:03 -050089
Mike Reed1eb5ca42018-03-08 14:20:52 -050090 // take 1/4 of max signed 32bits so we have room to subtract local values
91 const SkScalar max_fixed32dot32 = SK_MaxS32 * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -050092 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
Mike Klein37bc8f92019-10-21 13:10:07 -050093 +max_fixed32dot32, +max_fixed32dot32).contains(src)) {
Mike Reeda12c4192018-02-01 16:34:03 -050094 return false;
95 }
96
97 // legacy shader impl should be able to handle these matrices
98 return true;
99}
100
Florin Malita4aed1382017-05-25 10:38:07 -0400101SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
102 SkArenaAlloc* alloc) const {
Brian Osman0e189372018-10-19 11:58:29 -0400103 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400104 return nullptr;
105 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400106 if (fImage->colorType() != kN32_SkColorType) {
107 return nullptr;
108 }
Bharat Ahujaa7e4fe42019-11-14 23:53:58 +0530109#if !defined(SK_SUPPORT_LEGACY_TILED_BITMAPS)
Florin Malitaaf2769d2018-04-04 13:46:35 -0400110 if (fTileModeX != fTileModeY) {
111 return nullptr;
112 }
Bharat Ahujaa7e4fe42019-11-14 23:53:58 +0530113#endif
Mike Reedfae8fce2019-04-03 10:27:45 -0400114 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400115 return nullptr;
116 }
117
Mike Kleindac694d2018-12-18 10:13:52 -0500118 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500119 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500120 //
Mike Klein67761eb2018-12-18 10:16:53 -0500121 // We back off another bit to 32767 to make small amounts of
122 // intermediate math safe, e.g. in
123 //
124 // SkFixed fx = ...;
125 // fx = tile(fx + SK_Fixed1);
126 //
127 // we want to make sure (fx + SK_Fixed1) never overflows.
128 if (fImage-> width() > 32767 ||
129 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500130 return nullptr;
131 }
132
Florin Malitaaf2769d2018-04-04 13:46:35 -0400133 SkMatrix inv;
134 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
135 !legacy_shader_can_handle(inv)) {
136 return nullptr;
137 }
138
Mike Reed011d1662019-02-28 17:19:25 -0500139 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
140 return nullptr;
141 }
142
reed320a40d2016-08-02 06:12:06 -0700143 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed64acf4f2019-08-01 15:35:20 -0400144 as_IB(fImage.get()), rec, alloc);
reed856e9d92015-09-30 12:21:45 -0700145}
Mike Reede92aae62018-10-17 10:21:51 -0400146#endif
reed856e9d92015-09-30 12:21:45 -0700147
Mike Reedfae8fce2019-04-03 10:27:45 -0400148SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700149 if (texM) {
150 *texM = this->getLocalMatrix();
151 }
152 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400153 xy[0] = fTileModeX;
154 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700155 }
156 return const_cast<SkImage*>(fImage.get());
157}
158
Mike Klein1f313092018-01-03 10:30:21 -0500159sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400160 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -0500161 const SkMatrix* localMatrix,
162 bool clampAsIfUnpremul) {
Mike Kleindac694d2018-12-18 10:13:52 -0500163 if (!image) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000164 return sk_make_sp<SkEmptyShader>();
reed320a40d2016-08-02 06:12:06 -0700165 }
Mike Reede25b4472019-04-02 17:49:12 -0400166 return sk_sp<SkShader>{ new SkImageShader(image, tmx, tmy, localMatrix, clampAsIfUnpremul) };
reed856e9d92015-09-30 12:21:45 -0700167}
168
reed856e9d92015-09-30 12:21:45 -0700169///////////////////////////////////////////////////////////////////////////////////////////////////
170
171#if SK_SUPPORT_GPU
172
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500173#include "include/private/GrRecordingContext.h"
174#include "src/gpu/GrCaps.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400175#include "src/gpu/GrColorInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500176#include "src/gpu/GrRecordingContextPriv.h"
177#include "src/gpu/SkGr.h"
178#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon90673ec2020-01-06 15:50:57 -0500179#include "src/gpu/effects/GrTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700180
Mike Reedfae8fce2019-04-03 10:27:45 -0400181static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkTileMode tileMode) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400182 switch (tileMode) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400183 case SkTileMode::kClamp:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400184 return GrSamplerState::WrapMode::kClamp;
Mike Reedfae8fce2019-04-03 10:27:45 -0400185 case SkTileMode::kRepeat:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400186 return GrSamplerState::WrapMode::kRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400187 case SkTileMode::kMirror:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400188 return GrSamplerState::WrapMode::kMirrorRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400189 case SkTileMode::kDecal:
Michael Ludwigf23a1522018-12-10 11:36:13 -0500190 return GrSamplerState::WrapMode::kClampToBorder;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400191 }
192 SK_ABORT("Unknown tile mode.");
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400193}
194
Brian Salomonaff329b2017-08-11 09:40:37 -0400195std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500196 const GrFPArgs& args) const {
Florin Malitac6c5ead2018-04-11 15:33:40 -0400197 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700198 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400199 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700200 return nullptr;
201 }
reed856e9d92015-09-30 12:21:45 -0700202
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400203 GrSamplerState::WrapMode wrapModes[] = {tile_mode_to_wrap_mode(fTileModeX),
204 tile_mode_to_wrap_mode(fTileModeY)};
Michael Ludwigbe315a22018-12-17 09:50:51 -0500205
206 // If either domainX or domainY are un-ignored, a texture domain effect has to be used to
207 // implement the decal mode (while leaving non-decal axes alone). The wrap mode originally
208 // clamp-to-border is reset to clamp since the hw cannot implement it directly.
209 GrTextureDomain::Mode domainX = GrTextureDomain::kIgnore_Mode;
210 GrTextureDomain::Mode domainY = GrTextureDomain::kIgnore_Mode;
Robert Phillips9da87e02019-02-04 13:26:26 -0500211 if (!args.fContext->priv().caps()->clampToBorderSupport()) {
Michael Ludwigf23a1522018-12-10 11:36:13 -0500212 if (wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500213 domainX = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500214 wrapModes[0] = GrSamplerState::WrapMode::kClamp;
215 }
216 if (wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500217 domainY = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500218 wrapModes[1] = GrSamplerState::WrapMode::kClamp;
219 }
220 }
reed856e9d92015-09-30 12:21:45 -0700221
222 // Must set wrap and filter on the sampler before requesting a texture. In two places below
223 // we check the matrix scale factors to determine how to interpret the filter quality setting.
224 // This completely ignores the complexity of the drawVertices case where explicit local coords
225 // are provided by the caller.
226 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400227 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600228 fImage->width(), fImage->height(), args.fFilterQuality, *args.fViewMatrix, *lm,
Robert Phillips9da87e02019-02-04 13:26:26 -0500229 args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400230 GrSamplerState samplerState(wrapModes, textureFilterMode);
Robert Phillips67c18d62017-01-20 12:44:06 -0500231 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Osmane7fd8c32018-10-19 13:30:39 -0400232 sk_sp<GrTextureProxy> proxy(as_IB(fImage)->asTextureProxyRef(args.fContext, samplerState,
Brian Osman6064e1c2018-10-19 14:27:54 -0400233 scaleAdjust));
Robert Phillipsb726d582017-03-09 16:36:32 -0500234 if (!proxy) {
reed856e9d92015-09-30 12:21:45 -0700235 return nullptr;
236 }
237
Brian Salomonfc118442019-11-22 19:09:27 -0500238 SkAlphaType srcAlphaType = fImage->alphaType();
Robert Phillipsb726d582017-03-09 16:36:32 -0500239
Robert Phillips67c18d62017-01-20 12:44:06 -0500240 lmInverse.postScale(scaleAdjust[0], scaleAdjust[1]);
241
Brian Salomonaff329b2017-08-11 09:40:37 -0400242 std::unique_ptr<GrFragmentProcessor> inner;
reed856e9d92015-09-30 12:21:45 -0700243 if (doBicubic) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500244 // domainX and domainY will properly apply the decal effect with the texture domain used in
245 // the bicubic filter if clamp to border was unsupported in hardware
Brian Salomona86fc7a2019-05-28 20:42:58 -0400246 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomonfc118442019-11-22 19:09:27 -0500247 inner = GrBicubicEffect::Make(std::move(proxy), lmInverse, wrapModes, domainX, domainY,
248 kDir, srcAlphaType);
reed856e9d92015-09-30 12:21:45 -0700249 } else {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500250 auto dimensions = proxy->dimensions();
Brian Salomon90673ec2020-01-06 15:50:57 -0500251 inner = GrTextureEffect::Make(std::move(proxy), srcAlphaType, lmInverse, samplerState);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500252 if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500253 SkRect domain = GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(dimensions),
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400254 domainX, domainY);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500255 inner = GrDomainEffect::Make(std::move(inner), domain, domainX, domainY,
256 samplerState.filter());
Michael Ludwigbe315a22018-12-17 09:50:51 -0500257 }
reed856e9d92015-09-30 12:21:45 -0700258 }
Brian Salomonfc118442019-11-22 19:09:27 -0500259 inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(), srcAlphaType,
260 args.fDstColorInfo->colorSpace());
Greg Danielc594e622019-10-15 14:01:49 -0400261
262 bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType());
Robert Phillipsb726d582017-03-09 16:36:32 -0500263 if (isAlphaOnly) {
bungeman06ca8ec2016-06-09 08:01:03 -0700264 return inner;
Brian Salomonc0d79e52019-04-10 15:02:11 -0400265 } else if (args.fInputColorIsOpaque) {
266 return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700267 }
Mike Reed28eaed22018-02-01 11:24:53 -0500268 return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner));
reed856e9d92015-09-30 12:21:45 -0700269}
270
271#endif
reed320a40d2016-08-02 06:12:06 -0700272
273///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500274#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700275
Mike Reede25b4472019-04-02 17:49:12 -0400276sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy,
277 const SkMatrix* localMatrix, SkCopyPixelsMode cpm) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000278 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm),
279 tmx, tmy, localMatrix);
reed320a40d2016-08-02 06:12:06 -0700280}
281
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400282sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400283 SkTileMode tmx, SkTileMode tmy,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400284 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
285 auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode);
286 if (!s) {
287 return nullptr;
288 }
289 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
290 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
291 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
292 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400293 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400294 }
295 return s;
296}
297
Brian Salomon23356442018-11-30 15:33:19 -0500298void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700299
Mike Reed9318a6c2019-08-16 16:16:25 -0400300class SkImageStageUpdater : public SkStageUpdater {
301public:
Mike Reed8845c372019-12-19 13:22:08 -0500302 SkImageStageUpdater(const SkImageShader* shader, bool usePersp)
303 : fShader(shader), fUsePersp(usePersp)
304 {}
Mike Reed9318a6c2019-08-16 16:16:25 -0400305
Mike Reed8845c372019-12-19 13:22:08 -0500306 const SkImageShader* fShader;
307 const bool fUsePersp; // else use affine
308
309 // large enough for perspective, though often we just use 2x3
310 float fMatrixStorage[9];
Mike Reed9318a6c2019-08-16 16:16:25 -0400311
312#if 0 // TODO: when we support mipmaps
313 SkRasterPipeline_GatherCtx* fGather;
314 SkRasterPipeline_TileCtx* fLimitX;
315 SkRasterPipeline_TileCtx* fLimitY;
316 SkRasterPipeline_DecalTileCtx* fDecal;
317#endif
318
Mike Reed8845c372019-12-19 13:22:08 -0500319 void append_matrix_stage(SkRasterPipeline* p) {
320 if (fUsePersp) {
321 p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage);
322 } else {
323 p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage);
324 }
325 }
326
Mike Reed9318a6c2019-08-16 16:16:25 -0400327 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
328 SkMatrix matrix;
Mike Reed8845c372019-12-19 13:22:08 -0500329 if (fShader->computeTotalInverse(ctm, localM, &matrix)) {
330 if (fUsePersp) {
331 matrix.get9(fMatrixStorage);
332 } else {
333 SkAssertResult(matrix.asAffine(fMatrixStorage));
334 }
335 return true;
336 }
337 return false;
Mike Reed9318a6c2019-08-16 16:16:25 -0400338 }
339};
340
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600341static void tweak_quality_and_inv_matrix(SkFilterQuality* quality, SkMatrix* matrix) {
342 // When the matrix is just an integer translate, bilerp == nearest neighbor.
343 if (*quality == kLow_SkFilterQuality &&
344 matrix->getType() <= SkMatrix::kTranslate_Mask &&
345 matrix->getTranslateX() == (int)matrix->getTranslateX() &&
346 matrix->getTranslateY() == (int)matrix->getTranslateY()) {
347 *quality = kNone_SkFilterQuality;
348 }
349
350 // See skia:4649 and the GM image_scale_aligned.
351 if (*quality == kNone_SkFilterQuality) {
352 if (matrix->getScaleX() >= 0) {
353 matrix->setTranslateX(nextafterf(matrix->getTranslateX(),
354 floorf(matrix->getTranslateX())));
355 }
356 if (matrix->getScaleY() >= 0) {
357 matrix->setTranslateY(nextafterf(matrix->getTranslateY(),
358 floorf(matrix->getTranslateY())));
359 }
360 }
361}
362
Mike Reed9318a6c2019-08-16 16:16:25 -0400363bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
Mike Reed8845c372019-12-19 13:22:08 -0500364 if (updater && rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality) {
365 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400366 return false;
367 }
368
Mike Reed1d8c42e2017-08-29 14:58:19 -0400369 SkRasterPipeline* p = rec.fPipeline;
370 SkArenaAlloc* alloc = rec.fAlloc;
Mike Reed9318a6c2019-08-16 16:16:25 -0400371 auto quality = rec.fPaint.getFilterQuality();
Mike Reed1d8c42e2017-08-29 14:58:19 -0400372
Florin Malita7558e4d2018-02-07 10:05:53 -0500373 SkMatrix matrix;
374 if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500375 return false;
376 }
Mike Klein06a65e22016-11-17 12:39:09 -0500377
Mike Reed64acf4f2019-08-01 15:35:20 -0400378 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
379 matrix, quality, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500380 if (!state) {
381 return false;
382 }
383
384 const SkPixmap& pm = state->pixmap();
385 matrix = state->invMatrix();
386 quality = state->quality();
387 auto info = pm.info();
388
Mike Kleine8de0242018-03-10 12:37:11 -0500389 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400390
391 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500392 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400393 } else {
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600394 tweak_quality_and_inv_matrix(&quality, &matrix);
Mike Reed9318a6c2019-08-16 16:16:25 -0400395 p->append_matrix(alloc, matrix);
396 }
Mike Klein06a65e22016-11-17 12:39:09 -0500397
Mike Kleinb11ab572018-10-24 06:42:14 -0400398 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500399 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400400 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400401 gather->width = pm.width();
402 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400403
Mike Kleinb11ab572018-10-24 06:42:14 -0400404 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
405 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400406 limit_x->scale = pm.width();
407 limit_x->invScale = 1.0f / pm.width();
408 limit_y->scale = pm.height();
409 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400410
Mike Kleinb11ab572018-10-24 06:42:14 -0400411 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400412 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
413 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400414 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500415 decal_ctx->limit_x = limit_x->scale;
416 decal_ctx->limit_y = limit_y->scale;
417 }
418
Mike Reed9318a6c2019-08-16 16:16:25 -0400419#if 0 // TODO: when we support kMedium
420 if (updator && (quality == kMedium_SkFilterQuality)) {
421 // if we change levels in mipmap, we need to update the scales (and invScales)
422 updator->fGather = gather;
423 updator->fLimitX = limit_x;
424 updator->fLimitY = limit_y;
425 updator->fDecal = decal_ctx;
426 }
427#endif
428
Mike Kleinb04c3522016-11-28 11:55:58 -0500429 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500430 if (decal_x_and_y) {
431 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
432 } else {
433 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400434 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
435 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
436 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
437 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500438 }
439 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400440 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
441 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
442 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
443 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500444 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500445 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500446
Mike Kleinac568a92018-01-25 09:09:32 -0500447 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500448 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500449 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400450 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
451 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500452 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
453 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400454 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
455 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
456 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500457 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
458 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400459 case kR16G16B16A16_unorm_SkColorType:
460 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600461 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500462 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400463 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500464
Mike Kleinb1df5e52018-10-17 17:06:03 -0400465 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
466 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400467
Mike Kleinac568a92018-01-25 09:09:32 -0500468 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
469 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400470
Mike Kleinac568a92018-01-25 09:09:32 -0500471 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
472 p->append(SkRasterPipeline::force_opaque ); break;
473
Mike Klein1a3eb522018-10-18 10:11:00 -0400474 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
475 p->append(SkRasterPipeline::swap_rb ); break;
476
Mike Kleinb70990e2019-02-28 10:03:27 -0600477 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500478 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500479 if (decal_ctx) {
480 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
481 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500482 };
483
Mike Klein1fa9c432017-12-11 09:59:47 -0500484 auto append_misc = [&] {
Mike Klein059e0432020-01-02 16:32:38 -0600485 // This is an inessential optimization... it's logically safe to set this to false.
486 // But if...
487 // - we know the image is definitely normalized, and
488 // - we're doing some color space conversion, and
489 // - sRGB curves are involved,
490 // then we can use slightly faster math that doesn't work well outside [0,1].
491 bool src_is_normalized = SkColorTypeIsNormalized(info.colorType());
492
Mike Klein6a9f1c42020-01-02 23:23:42 -0600493 SkColorSpace* cs = info.colorSpace();
494 SkAlphaType at = info.alphaType();
495
496 // Color for A8 images comes from the paint. TODO: all alpha images? none?
497 if (info.colorType() == kAlpha_8_SkColorType) {
498 SkColor4f rgb = rec.fPaint.getColor4f();
499 p->append_set_rgb(alloc, rgb);
500
501 src_is_normalized = rgb.fitsInBytes();
502 cs = sk_srgb_singleton();
503 at = kUnpremul_SkAlphaType;
504 }
505
Mike Klein059e0432020-01-02 16:32:38 -0600506 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Klein250138d2020-01-02 09:39:26 -0600507 if (quality == kHigh_SkFilterQuality) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500508 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600509 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
510 ? SkRasterPipeline::clamp_1
511 : SkRasterPipeline::clamp_a);
512 src_is_normalized = true;
Mike Klein1fa9c432017-12-11 09:59:47 -0500513 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000514
Mike Klein059e0432020-01-02 16:32:38 -0600515 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
516 alloc->make<SkColorSpaceXformSteps>(cs, at,
517 rec.fDstCS, kPremul_SkAlphaType)
518 ->apply(p, src_is_normalized);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000519
Mike Klein1fa9c432017-12-11 09:59:47 -0500520 return true;
521 };
522
Mike Reed9318a6c2019-08-16 16:16:25 -0400523 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400524 auto ct = info.colorType();
525 if (true
526 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
527 && quality == kLow_SkFilterQuality
Mike Reedfae8fce2019-04-03 10:27:45 -0400528 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500529
530 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400531 if (ct == kBGRA_8888_SkColorType) {
532 p->append(SkRasterPipeline::swap_rb);
533 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500534 return append_misc();
535 }
Mike Reed78eedba2019-07-31 16:39:15 -0400536 if (true
Mike Klein01005622019-08-13 12:22:17 -0400537 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
538 && quality == kLow_SkFilterQuality
539 && fTileModeX != SkTileMode::kDecal // TODO decal too?
540 && fTileModeY != SkTileMode::kDecal) {
541
542 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
543 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
544 ctx->ct = ct;
545 ctx->tileX = fTileModeX;
546 ctx->tileY = fTileModeY;
547 ctx->invWidth = 1.0f / ctx->width;
548 ctx->invHeight = 1.0f / ctx->height;
549 p->append(SkRasterPipeline::bilinear, ctx);
550 return append_misc();
551 }
552 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400553 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
554 && quality == kHigh_SkFilterQuality
555 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
556
557 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
558 if (ct == kBGRA_8888_SkColorType) {
559 p->append(SkRasterPipeline::swap_rb);
560 }
561 return append_misc();
562 }
Mike Klein01005622019-08-13 12:22:17 -0400563 if (true
564 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
565 && quality == kHigh_SkFilterQuality
566 && fTileModeX != SkTileMode::kDecal // TODO decal too?
567 && fTileModeY != SkTileMode::kDecal) {
568
569 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
570 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
571 ctx->ct = ct;
572 ctx->tileX = fTileModeX;
573 ctx->tileY = fTileModeY;
574 ctx->invWidth = 1.0f / ctx->width;
575 ctx->invHeight = 1.0f / ctx->height;
576 p->append(SkRasterPipeline::bicubic, ctx);
577 return append_misc();
578 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500579
Mike Kleinb11ab572018-10-24 06:42:14 -0400580 SkRasterPipeline_SamplerCtx* sampler = nullptr;
Mike Klein0a904492017-04-12 12:52:48 -0400581 if (quality != kNone_SkFilterQuality) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400582 sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400583 }
584
Mike Kleinb0b17d12016-12-09 16:25:44 -0500585 auto sample = [&](SkRasterPipeline::StockStage setup_x,
586 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400587 p->append(setup_x, sampler);
588 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500589 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400590 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500591 };
592
Mike Kleinf7f883b2016-11-21 15:09:45 -0500593 if (quality == kNone_SkFilterQuality) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500594 append_tiling_and_gather();
Mike Kleinb0b17d12016-12-09 16:25:44 -0500595 } else if (quality == kLow_SkFilterQuality) {
Mike Klein0a904492017-04-12 12:52:48 -0400596 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500597
598 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
599 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
600 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
601 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
602
603 p->append(SkRasterPipeline::move_dst_src);
Mike Klein1fa9c432017-12-11 09:59:47 -0500604
Mike Klein46e66a22016-11-21 16:19:34 -0500605 } else {
Mike Klein250138d2020-01-02 09:39:26 -0600606 SkASSERT(quality == kHigh_SkFilterQuality);
Mike Klein0a904492017-04-12 12:52:48 -0400607 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500608
609 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
610 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
611 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
612 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
613
614 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
615 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
616 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
617 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
618
619 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
620 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
621 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
622 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
623
624 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
625 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
626 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
627 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
628
Mike Kleinb04c3522016-11-28 11:55:58 -0500629 p->append(SkRasterPipeline::move_dst_src);
Mike Klein06a65e22016-11-17 12:39:09 -0500630 }
631
Mike Klein1fa9c432017-12-11 09:59:47 -0500632 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500633}
Mike Reed9318a6c2019-08-16 16:16:25 -0400634
635bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
636 return this->doStages(rec, nullptr);
637}
638
639SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Mike Reed8845c372019-12-19 13:22:08 -0500640 bool usePersp = rec.fCTM.hasPerspective();
641 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400642 return this->doStages(rec, updater) ? updater : nullptr;
643}
644
Mike Kleinf6a715b2019-12-30 15:24:18 -0600645bool SkImageShader::onProgram(skvm::Builder* p,
646 const SkMatrix& ctm, const SkMatrix* localM,
647 SkFilterQuality quality, SkColorSpace* dstCS,
648 skvm::Uniforms* uniforms,
649 skvm::F32 x, skvm::F32 y,
650 skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const {
651 SkMatrix inv;
652 if (!this->computeTotalInverse(ctm, localM, &inv)) {
653 return false;
654 }
655
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600656 // TODO: need to extend lifetime of this once we start supporting kMedium_SkFilterQuality.
Mike Kleinf6a715b2019-12-30 15:24:18 -0600657 SkBitmapController::State state{as_IB(fImage.get()), inv, quality};
658 const SkPixmap& pm = state.pixmap();
659 if (!pm.addr()) {
660 return false;
661 }
662 inv = state.invMatrix();
663 quality = state.quality();
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600664 tweak_quality_and_inv_matrix(&quality, &inv);
Mike Kleinf6a715b2019-12-30 15:24:18 -0600665
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600666 // TODO: and need to extend lifetime of this too once supporting steps.flags.mask() != 0.
667 SkColorSpaceXformSteps steps{pm.colorSpace(), pm.alphaType(),
668 dstCS, kPremul_SkAlphaType};
669
Mike Klein25480bf2020-01-06 14:01:58 -0600670 if (steps.flags.mask() != 0) { return false; }
671 if (quality != kNone_SkFilterQuality) { return false; }
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600672
Mike Klein25480bf2020-01-06 14:01:58 -0600673 // Apply matrix to convert dst coords to sample coords.
674 inv.normalizePerspective();
675 if (inv.isIdentity()) {
676 // That was easy.
677 } else if (inv.isTranslate()) {
678 x = p->add(x, p->uniformF(uniforms->pushF(inv[2])));
679 y = p->add(y, p->uniformF(uniforms->pushF(inv[5])));
680 } else if (inv.isScaleTranslate()) {
681 x = p->mad(x, p->uniformF(uniforms->pushF(inv[0])), p->uniformF(uniforms->pushF(inv[2])));
682 y = p->mad(y, p->uniformF(uniforms->pushF(inv[4])), p->uniformF(uniforms->pushF(inv[5])));
683 } else { // Affine or perspective.
684 auto dot = [&,x,y](int row) {
685 return p->mad(x, p->uniformF(uniforms->pushF(inv[3*row+0])),
686 p->mad(y, p->uniformF(uniforms->pushF(inv[3*row+1])),
687 p->uniformF(uniforms->pushF(inv[3*row+2]))));
688 };
689 x = dot(0);
690 y = dot(1);
691 if (inv.hasPerspective()) {
692 x = p->div(x, dot(2));
693 y = p->div(y, dot(2));
694 }
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600695 }
696
Mike Klein25480bf2020-01-06 14:01:58 -0600697 // Apply tile mode.
698 // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp.
699 auto repeat = [&](skvm::F32 v, float scale) {
700 skvm::F32 S = p->uniformF(uniforms->pushF( scale)),
701 I = p->uniformF(uniforms->pushF(1.0f/scale));
702 // v - floor(v/scale)*scale
703 return p->sub(v, p->mul(p->floor(p->mul(v,I)), S));
704 };
705 auto mirror = [&](skvm::F32 v, float scale) {
706 skvm::F32 S = p->uniformF(uniforms->pushF( scale)),
707 I2 = p->uniformF(uniforms->pushF(0.5f/scale));
708 // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale )
709 // {---A---} {------------------B------------------}
710 skvm::F32 A = p->sub(v,S),
711 B = p->mul(p->add(S,S), p->floor(p->mul(A,I2)));
712 return p->abs(p->sub(p->sub(A,B), S));
713 };
Mike Klein25480bf2020-01-06 14:01:58 -0600714 switch (fTileModeX) {
Mike Kleind67a5c32020-01-06 16:01:01 -0600715 case SkTileMode::kDecal: /* handled after gather */ break;
716 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Klein25480bf2020-01-06 14:01:58 -0600717 case SkTileMode::kRepeat: x = repeat(x, pm.width()); break;
718 case SkTileMode::kMirror: x = mirror(x, pm.width()); break;
719 }
720 switch (fTileModeY) {
Mike Kleind67a5c32020-01-06 16:01:01 -0600721 case SkTileMode::kDecal: /* handled after gather */ break;
722 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Klein25480bf2020-01-06 14:01:58 -0600723 case SkTileMode::kRepeat: y = repeat(y, pm.height()); break;
724 case SkTileMode::kMirror: y = mirror(y, pm.height()); break;
725 }
726
727 // Clamp sample coordinates to [0,width), [0,height),
728 // both for memory safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror.
Mike Kleind67a5c32020-01-06 16:01:01 -0600729 auto clamp = [&](skvm::F32 v, float limit) {
730 // Subtract one ulp to make an exclusive limit inclusive.
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600731 int bits;
Mike Kleind67a5c32020-01-06 16:01:01 -0600732 memcpy(&bits, &limit, 4);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600733 bits--;
Mike Kleind67a5c32020-01-06 16:01:01 -0600734 memcpy(&limit, &bits, 4);
735 return p->max(p->splat(0.0f), p->min(v, p->uniformF(uniforms->pushF(limit))));
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600736 };
Mike Kleind67a5c32020-01-06 16:01:01 -0600737 skvm::F32 clamped_x = clamp(x, pm. width()),
738 clamped_y = clamp(y, pm.height());
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600739
Mike Klein25480bf2020-01-06 14:01:58 -0600740 // Load pixels from pm.addr()[(int)x + (int)y*stride].
741 skvm::Builder::Uniform img = uniforms->pushPtr(pm.addr());
Mike Kleind67a5c32020-01-06 16:01:01 -0600742 skvm::I32 index = p->add(p->trunc(clamped_x),
743 p->mul(p->trunc(clamped_y),
744 p->uniform32(uniforms->push(pm.rowBytesAsPixels()))));
Mike Klein25480bf2020-01-06 14:01:58 -0600745 skvm::Color c;
746 switch (pm.colorType()) {
747 default: return false;
748
749 case kRGB_565_SkColorType: c = p->unpack_565(p->gather16(img, index)); break;
750
751 case kRGBA_8888_SkColorType: c = p->unpack_8888(p->gather32(img, index)); break;
752 case kBGRA_8888_SkColorType: c = p->unpack_8888(p->gather32(img, index));
753 std::swap(c.r, c.b);
754 break;
755 }
756
Mike Kleind67a5c32020-01-06 16:01:01 -0600757 // Mask away any pixels that we tried to sample outside the bounds in kDecal.
758 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
759 skvm::I32 mask = p->splat(~0);
760 if (fTileModeX == SkTileMode::kDecal) { mask = p->bit_and(mask, p->eq(x, clamped_x)); }
761 if (fTileModeY == SkTileMode::kDecal) { mask = p->bit_and(mask, p->eq(y, clamped_y)); }
762 c.r = p->bit_cast(p->bit_and(mask, p->bit_cast(c.r)));
763 c.g = p->bit_cast(p->bit_and(mask, p->bit_cast(c.g)));
764 c.b = p->bit_cast(p->bit_and(mask, p->bit_cast(c.b)));
765 c.a = p->bit_cast(p->bit_and(mask, p->bit_cast(c.a)));
766 }
767
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600768 *r = c.r;
769 *g = c.g;
770 *b = c.b;
771 *a = c.a;
772 return true;
Mike Kleinf6a715b2019-12-30 15:24:18 -0600773}
774