blob: 5ebd5234607d769010af151fac4f9362c185b968 [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
Brian Salomon0c243202020-06-29 14:29:25 -04008#include "src/shaders/SkImageShader.h"
9
Ben Wagner729a23f2019-05-17 16:29:34 -040010#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkColorSpacePriv.h"
12#include "src/core/SkColorSpaceXformSteps.h"
Mike Reed8c1ad7e2020-12-02 20:41:52 -050013#include "src/core/SkMatrixPriv.h"
Brian Osman449b1152020-04-15 16:43:00 -040014#include "src/core/SkMatrixProvider.h"
Mike Reed60a2ec02020-12-08 09:18:14 -050015#include "src/core/SkMipmapAccessor.h"
Mike Klein37bc8f92019-10-21 13:10:07 -050016#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkRasterPipeline.h"
18#include "src/core/SkReadBuffer.h"
Mike Reed92236652021-02-01 13:07:32 -050019#include "src/core/SkSamplingPriv.h"
Brian Salomon0c243202020-06-29 14:29:25 -040020#include "src/core/SkScopeExit.h"
Mike Klein8e717442020-01-07 10:22:33 -060021#include "src/core/SkVM.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkWriteBuffer.h"
23#include "src/image/SkImage_Base.h"
24#include "src/shaders/SkBitmapProcShader.h"
25#include "src/shaders/SkEmptyShader.h"
reed856e9d92015-09-30 12:21:45 -070026
Mike Reed3d30ca62020-07-22 16:55:02 -040027SkM44 SkImageShader::CubicResamplerMatrix(float B, float C) {
Mike Reed3867c702020-09-01 13:28:10 -040028#if 0
29 constexpr SkM44 kMitchell = SkM44( 1.f/18.f, -9.f/18.f, 15.f/18.f, -7.f/18.f,
30 16.f/18.f, 0.f/18.f, -36.f/18.f, 21.f/18.f,
31 1.f/18.f, 9.f/18.f, 27.f/18.f, -21.f/18.f,
32 0.f/18.f, 0.f/18.f, -6.f/18.f, 7.f/18.f);
33
34 constexpr SkM44 kCatmull = SkM44(0.0f, -0.5f, 1.0f, -0.5f,
35 1.0f, 0.0f, -2.5f, 1.5f,
36 0.0f, 0.5f, 2.0f, -1.5f,
37 0.0f, 0.0f, -0.5f, 0.5f);
38
39 if (B == 1.0f/3 && C == 1.0f/3) {
40 return kMitchell;
41 }
42 if (B == 0 && C == 0.5f) {
43 return kCatmull;
44 }
45#endif
46 return SkM44( (1.f/6)*B, -(3.f/6)*B - C, (3.f/6)*B + 2*C, - (1.f/6)*B - C,
47 1 - (2.f/6)*B, 0, -3 + (12.f/6)*B + C, 2 - (9.f/6)*B - C,
48 (1.f/6)*B, (3.f/6)*B + C, 3 - (15.f/6)*B - 2*C, -2 + (9.f/6)*B + C,
49 0, 0, -C, (1.f/6)*B + C);
Mike Reed3d30ca62020-07-22 16:55:02 -040050}
51
Mike Reed587d0822017-06-23 16:49:12 -040052/**
53 * We are faster in clamp, so always use that tiling when we can.
54 */
Mike Reedfae8fce2019-04-03 10:27:45 -040055static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040056 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040057#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
58 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
59 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040060 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040061#else
Mike Reedfae8fce2019-04-03 10:27:45 -040062 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040063#endif
Mike Reed587d0822017-06-23 16:49:12 -040064}
65
Mike Klein1f313092018-01-03 10:30:21 -050066SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040067 SkTileMode tmx, SkTileMode tmy,
Mike Reed74c51ac2020-11-20 10:23:58 -050068 const SkSamplingOptions* sampling,
Mike Klein1f313092018-01-03 10:30:21 -050069 const SkMatrix* localMatrix,
70 bool clampAsIfUnpremul)
71 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070072 , fImage(std::move(img))
Mike Reed74c51ac2020-11-20 10:23:58 -050073 , fSampling(sampling ? *sampling : SkSamplingOptions())
Mike Reed587d0822017-06-23 16:49:12 -040074 , fTileModeX(optimize(tmx, fImage->width()))
75 , fTileModeY(optimize(tmy, fImage->height()))
Mike Klein1f313092018-01-03 10:30:21 -050076 , fClampAsIfUnpremul(clampAsIfUnpremul)
Mike Reed74c51ac2020-11-20 10:23:58 -050077 , fUseSamplingOptions(sampling != nullptr)
reed856e9d92015-09-30 12:21:45 -070078{}
79
Mike Reed74c51ac2020-11-20 10:23:58 -050080// just used for legacy-unflattening
81enum class LegacyFilterEnum {
82 kNone,
83 kLow,
84 kMedium,
85 kHigh,
86 // this is the special value for backward compatibility
87 kInheritFromPaint,
88 // this signals we should use the new SkFilterOptions
89 kUseFilterOptions,
Mike Kleinbb1933e2020-12-02 15:45:29 -060090 // use cubic and ignore FilterOptions
Mike Reed74c51ac2020-11-20 10:23:58 -050091 kUseCubicResampler,
Mike Klein1f313092018-01-03 10:30:21 -050092
Mike Reed74c51ac2020-11-20 10:23:58 -050093 kLast = kUseCubicResampler,
94};
95
96sk_sp<SkFlattenable> SkImageShader::PreSamplingCreate(SkReadBuffer& buffer) {
97 SkASSERT(buffer.isVersionLT(SkPicturePriv::kSamplingInImageShader_Version));
98
Mike Reede25b4472019-04-02 17:49:12 -040099 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
100 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Reed9290d012020-06-11 16:56:06 -0400101
Mike Reed74c51ac2020-11-20 10:23:58 -0500102 LegacyFilterEnum fe = LegacyFilterEnum::kInheritFromPaint;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400103 if (!buffer.isVersionLT(SkPicturePriv::kFilterEnumInImageShader_Version)) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500104 fe = buffer.read32LE<LegacyFilterEnum>(LegacyFilterEnum::kLast);
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400105 }
106
Mike Reed15b95d62020-11-06 09:50:47 -0500107 SkSamplingOptions op;
Mike Reed3d30ca62020-07-22 16:55:02 -0400108
109 if (buffer.isVersionLT(SkPicturePriv::kCubicResamplerImageShader_Version)) {
110 if (!buffer.isVersionLT(SkPicturePriv::kFilterOptionsInImageShader_Version)) {
Mike Kleindcc89602020-12-02 15:06:15 -0600111 auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
112 auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
113 op = SkSamplingOptions(filter, mipmap);
Mike Reed3d30ca62020-07-22 16:55:02 -0400114 }
115 } else {
116 switch (fe) {
Mike Kleindcc89602020-12-02 15:06:15 -0600117 case LegacyFilterEnum::kUseFilterOptions: {
118 auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
119 auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
120 op = SkSamplingOptions(filter, mipmap);
121 } break;
122 case LegacyFilterEnum::kUseCubicResampler: {
123 SkScalar B = buffer.readScalar(),
124 C = buffer.readScalar();
125 op = SkSamplingOptions({B,C});
126 } break;
Mike Reed3d30ca62020-07-22 16:55:02 -0400127 default:
128 break;
129 }
Mike Reed9290d012020-06-11 16:56:06 -0400130 }
131
Mike Klein1f313092018-01-03 10:30:21 -0500132 SkMatrix localMatrix;
133 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -0700134 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -0700135 if (!img) {
136 return nullptr;
137 }
Mike Reed9290d012020-06-11 16:56:06 -0400138
Mike Reed3d30ca62020-07-22 16:55:02 -0400139 switch (fe) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500140 case LegacyFilterEnum::kUseFilterOptions:
141 case LegacyFilterEnum::kUseCubicResampler:
142 return SkImageShader::Make(std::move(img), tmx, tmy, &op, &localMatrix);
Mike Reed3d30ca62020-07-22 16:55:02 -0400143 default:
144 break;
145 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500146 return SkImageShader::Make(std::move(img), tmx, tmy, nullptr, &localMatrix);
147}
148
Mike Reed74c51ac2020-11-20 10:23:58 -0500149// fClampAsIfUnpremul is always false when constructed through public APIs,
150// so there's no need to read or write it here.
151
152sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
153 if (buffer.isVersionLT(SkPicturePriv::kSamplingInImageShader_Version)) {
154 return PreSamplingCreate(buffer);
155 }
156
157 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
158 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
159
160 SkSamplingOptions sampling,
161 *samplingPtr = nullptr;
162
163 if (buffer.readBool()) { // fUseSamplingOptions
Mike Reed92236652021-02-01 13:07:32 -0500164 sampling = SkSamplingPriv::Read(buffer);
Mike Reed74c51ac2020-11-20 10:23:58 -0500165 samplingPtr = &sampling;
166 }
167
168 SkMatrix localMatrix;
169 buffer.readMatrix(&localMatrix);
170 sk_sp<SkImage> img = buffer.readImage();
171 if (!img) {
172 return nullptr;
173 }
174
175 return SkImageShader::Make(std::move(img), tmx, tmy, samplingPtr, &localMatrix);
reed856e9d92015-09-30 12:21:45 -0700176}
177
178void SkImageShader::flatten(SkWriteBuffer& buffer) const {
Mike Reedfae8fce2019-04-03 10:27:45 -0400179 buffer.writeUInt((unsigned)fTileModeX);
180 buffer.writeUInt((unsigned)fTileModeY);
Mike Reed74c51ac2020-11-20 10:23:58 -0500181
182 buffer.writeBool(fUseSamplingOptions);
183 if (fUseSamplingOptions) {
Mike Reed92236652021-02-01 13:07:32 -0500184 SkSamplingPriv::Write(buffer, fSampling);
Mike Reed3d30ca62020-07-22 16:55:02 -0400185 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500186
reed856e9d92015-09-30 12:21:45 -0700187 buffer.writeMatrix(this->getLocalMatrix());
reed6b2d7ac2016-08-11 06:42:26 -0700188 buffer.writeImage(fImage.get());
Mike Klein1f313092018-01-03 10:30:21 -0500189 SkASSERT(fClampAsIfUnpremul == false);
reed856e9d92015-09-30 12:21:45 -0700190}
191
192bool SkImageShader::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -0400193 return fImage->isOpaque() &&
194 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
reed856e9d92015-09-30 12:21:45 -0700195}
196
Mike Reed74c51ac2020-11-20 10:23:58 -0500197constexpr SkCubicResampler kDefaultCubicResampler{1.0f/3, 1.0f/3};
198
199static bool is_default_cubic_resampler(SkCubicResampler cubic) {
200 return SkScalarNearlyEqual(cubic.B, kDefaultCubicResampler.B) &&
201 SkScalarNearlyEqual(cubic.C, kDefaultCubicResampler.C);
202}
203
Mike Reed604e4c22020-11-25 21:08:17 -0500204#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Mike Reed74c51ac2020-11-20 10:23:58 -0500205
Florin Malitaaf2769d2018-04-04 13:46:35 -0400206static bool legacy_shader_can_handle(const SkMatrix& inv) {
Mike Reed81063112020-06-09 17:36:33 -0400207 SkASSERT(!inv.hasPerspective());
Mike Klein37bc8f92019-10-21 13:10:07 -0500208
209 // Scale+translate methods are always present, but affine might not be.
210 if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -0500211 return false;
212 }
213
214 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
215 // out of range.
216 const SkScalar max_dev_coord = 32767.0f;
Mike Klein37bc8f92019-10-21 13:10:07 -0500217 const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord));
Mike Reeda12c4192018-02-01 16:34:03 -0500218
Mike Reed1eb5ca42018-03-08 14:20:52 -0500219 // take 1/4 of max signed 32bits so we have room to subtract local values
Kevin Lubickf76da632020-01-28 10:39:56 -0500220 const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -0500221 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
Mike Klein37bc8f92019-10-21 13:10:07 -0500222 +max_fixed32dot32, +max_fixed32dot32).contains(src)) {
Mike Reeda12c4192018-02-01 16:34:03 -0500223 return false;
224 }
225
226 // legacy shader impl should be able to handle these matrices
227 return true;
228}
229
Florin Malita4aed1382017-05-25 10:38:07 -0400230SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
231 SkArenaAlloc* alloc) const {
Brian Osman0e189372018-10-19 11:58:29 -0400232 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400233 return nullptr;
234 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400235 if (fImage->colorType() != kN32_SkColorType) {
236 return nullptr;
237 }
Florin Malitaaf2769d2018-04-04 13:46:35 -0400238 if (fTileModeX != fTileModeY) {
239 return nullptr;
240 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400241 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400242 return nullptr;
243 }
244
Mike Reedbc4d88a2021-01-21 12:49:36 -0500245 SkSamplingOptions sampling = fUseSamplingOptions ? fSampling
246 : rec.fPaintSampling;
247
248 auto supported = [](const SkSamplingOptions& sampling) {
249 const std::tuple<SkFilterMode,SkMipmapMode> supported[] = {
250 {SkFilterMode::kNearest, SkMipmapMode::kNone}, // legacy kNone_SkFilterQuality
251 {SkFilterMode::kLinear, SkMipmapMode::kNone}, // legacy kLow_SkFilterQuality
252 {SkFilterMode::kLinear, SkMipmapMode::kNearest}, // legacy kMedium_SkFilterQuality
253 };
254 for (auto [f, m] : supported) {
255 if (sampling.filter == f && sampling.mipmap == m) {
256 return true;
257 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500258 }
Mike Reedbc4d88a2021-01-21 12:49:36 -0500259 return false;
260 };
261 if (sampling.useCubic || !supported(sampling)) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500262 return nullptr;
263 }
264
Mike Kleindac694d2018-12-18 10:13:52 -0500265 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500266 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500267 //
Mike Klein67761eb2018-12-18 10:16:53 -0500268 // We back off another bit to 32767 to make small amounts of
269 // intermediate math safe, e.g. in
270 //
271 // SkFixed fx = ...;
272 // fx = tile(fx + SK_Fixed1);
273 //
274 // we want to make sure (fx + SK_Fixed1) never overflows.
275 if (fImage-> width() > 32767 ||
276 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500277 return nullptr;
278 }
279
Florin Malitaaf2769d2018-04-04 13:46:35 -0400280 SkMatrix inv;
281 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
282 !legacy_shader_can_handle(inv)) {
283 return nullptr;
284 }
285
Mike Reed011d1662019-02-28 17:19:25 -0500286 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
287 return nullptr;
288 }
289
Mike Reedbc4d88a2021-01-21 12:49:36 -0500290 // Can remove this once fUseSamplingOptions is always true
Mike Reed9290d012020-06-11 16:56:06 -0400291 ContextRec modifiedRec = rec;
Mike Reedbc4d88a2021-01-21 12:49:36 -0500292 modifiedRec.fPaintSampling = sampling;
293
reed320a40d2016-08-02 06:12:06 -0700294 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed9290d012020-06-11 16:56:06 -0400295 as_IB(fImage.get()), modifiedRec, alloc);
reed856e9d92015-09-30 12:21:45 -0700296}
Mike Reede92aae62018-10-17 10:21:51 -0400297#endif
reed856e9d92015-09-30 12:21:45 -0700298
Mike Reedfae8fce2019-04-03 10:27:45 -0400299SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700300 if (texM) {
301 *texM = this->getLocalMatrix();
302 }
303 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400304 xy[0] = fTileModeX;
305 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700306 }
307 return const_cast<SkImage*>(fImage.get());
308}
309
Mike Klein1f313092018-01-03 10:30:21 -0500310sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400311 SkTileMode tmx, SkTileMode tmy,
Mike Reed74c51ac2020-11-20 10:23:58 -0500312 const SkSamplingOptions* options,
Mike Klein1f313092018-01-03 10:30:21 -0500313 const SkMatrix* localMatrix,
314 bool clampAsIfUnpremul) {
Mike Reed15b95d62020-11-06 09:50:47 -0500315 auto is_unit = [](float x) {
316 return x >= 0 && x <= 1;
317 };
Mike Kleinbb1933e2020-12-02 15:45:29 -0600318 if (options && options->useCubic) {
319 if (!is_unit(options->cubic.B) || !is_unit(options->cubic.C)) {
Mike Reed15b95d62020-11-06 09:50:47 -0500320 return nullptr;
321 }
322 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400323 if (!image) {
324 return sk_make_sp<SkEmptyShader>();
325 }
326 return sk_sp<SkShader>{
Mike Reed74c51ac2020-11-20 10:23:58 -0500327 new SkImageShader(image, tmx, tmy, options, localMatrix, clampAsIfUnpremul)
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400328 };
329}
330
reed856e9d92015-09-30 12:21:45 -0700331///////////////////////////////////////////////////////////////////////////////////////////////////
332
333#if SK_SUPPORT_GPU
334
Robert Phillipsb7bfbc22020-07-01 12:55:01 -0400335#include "include/gpu/GrRecordingContext.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400336#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500337#include "src/gpu/GrCaps.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400338#include "src/gpu/GrColorInfo.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400339#include "src/gpu/GrImageTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500340#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400341#include "src/gpu/GrTextureAdjuster.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500342#include "src/gpu/SkGr.h"
343#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -0400344#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -0500345#include "src/gpu/effects/GrTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700346
Brian Salomonaff329b2017-08-11 09:40:37 -0400347std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500348 const GrFPArgs& args) const {
Florin Malita52f02912020-03-09 16:33:17 -0400349 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700350 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400351 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700352 return nullptr;
353 }
reed856e9d92015-09-30 12:21:45 -0700354
Brian Salomon694ec492020-04-14 13:39:31 -0400355 GrSamplerState::WrapMode wmX = SkTileModeToWrapMode(fTileModeX),
356 wmY = SkTileModeToWrapMode(fTileModeY);
Brian Salomon0c243202020-06-29 14:29:25 -0400357 // Must set wrap and filter on the sampler before requesting a texture. In two places
358 // below we check the matrix scale factors to determine how to interpret the filter
359 // quality setting. This completely ignores the complexity of the drawVertices case
360 // where explicit local coords are provided by the caller.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400361 bool sharpen = args.fContext->priv().options().fSharpenMipmappedTextures;
Mike Reed74c51ac2020-11-20 10:23:58 -0500362 GrSamplerState::Filter fm = GrSamplerState::Filter::kNearest;
363 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
Mike Reed52130b02020-12-28 15:33:13 -0500364 SkCubicResampler kernel = kInvalidCubicResampler;
Mike Reed3867c702020-09-01 13:28:10 -0400365
Mike Reed74c51ac2020-11-20 10:23:58 -0500366 if (fUseSamplingOptions) {
Brian Salomon490f1922021-02-01 13:14:37 -0500367 if (fSampling.useCubic) {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600368 kernel = fSampling.cubic;
Mike Reed74c51ac2020-11-20 10:23:58 -0500369 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600370 switch (fSampling.filter) {
Mike Reeda03f8bf2020-11-20 18:45:36 -0500371 case SkFilterMode::kNearest: fm = GrSamplerState::Filter::kNearest; break;
372 case SkFilterMode::kLinear : fm = GrSamplerState::Filter::kLinear ; break;
Mike Reed3867c702020-09-01 13:28:10 -0400373 }
Mike Kleinbb1933e2020-12-02 15:45:29 -0600374 switch (fSampling.mipmap) {
Mike Reed3867c702020-09-01 13:28:10 -0400375 case SkMipmapMode::kNone : mm = GrSamplerState::MipmapMode::kNone ; break;
376 case SkMipmapMode::kNearest: mm = GrSamplerState::MipmapMode::kNearest; break;
377 case SkMipmapMode::kLinear : mm = GrSamplerState::MipmapMode::kLinear ; break;
378 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500379 }
380 } else { // inherit filterquality from paint
Mike Reed52130b02020-12-28 15:33:13 -0500381 std::tie(fm, mm, kernel) =
382 GrInterpretSamplingOptions(fImage->dimensions(),
383 args.fSampling,
Mike Reed74c51ac2020-11-20 10:23:58 -0500384 args.fMatrixProvider.localToDevice(),
385 *lm,
386 sharpen,
387 args.fAllowFilterQualityReduction);
Brian Salomonf7353512020-07-22 19:26:48 -0400388 }
Brian Salomon490f1922021-02-01 13:14:37 -0500389
Brian Salomon0ea33072020-07-14 10:43:42 -0400390 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon490f1922021-02-01 13:14:37 -0500391 // TODO: Replace this mess with SkImage_Base::asFragmentProcessor() after it's implemented.
392 if (as_IB(fImage)->isYUVA()) {
393 GrYUVAImageTextureMaker maker(args.fContext, fImage.get());
394 if (GrValidCubicResampler(kernel)) {
395 fp = maker.createBicubicFragmentProcessor(lmInverse,
396 nullptr,
397 nullptr,
398 wmX, wmY,
399 kernel);
400 } else {
401 fp = maker.createFragmentProcessor(lmInverse, nullptr, nullptr, {wmX, wmY, fm, mm});
402 }
403 if (!fp) {
404 return nullptr;
405 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400406 } else {
Brian Salomon490f1922021-02-01 13:14:37 -0500407 auto mipmapped = !GrValidCubicResampler(kernel) && mm != SkMipmapMode::kNone
408 ? GrMipmapped::kYes
409 : GrMipmapped::kNo;
410 GrSurfaceProxyView view = as_IB(fImage)->refView(args.fContext, mipmapped);
411 if (!view) {
412 return nullptr;
413 }
414 if (GrValidCubicResampler(kernel)) {
415 fp = GrBicubicEffect::Make(std::move(view),
416 fImage->alphaType(),
417 lmInverse,
418 wmX, wmY,
419 kernel,
420 GrBicubicEffect::Direction::kXY,
421 *args.fContext->priv().caps());
422 } else {
423 fp = GrTextureEffect::Make(std::move(view),
424 fImage->alphaType(),
425 lmInverse,
426 {wmX, wmY, fm, mm},
427 *args.fContext->priv().caps());
428 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400429 }
Brian Salomon490f1922021-02-01 13:14:37 -0500430 SkASSERT(fp);
431 fp = GrColorSpaceXformEffect::Make(std::move(fp),
432 fImage->colorSpace(),
433 fImage->alphaType(),
434 args.fDstColorInfo->colorSpace(),
435 kPremul_SkAlphaType);
Brian Salomonb43d6992021-01-05 14:37:40 -0500436 if (fImage->isAlphaOnly()) {
437 return GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kDstIn);
Brian Salomonc0d79e52019-04-10 15:02:11 -0400438 } else if (args.fInputColorIsOpaque) {
Brian Salomonb43d6992021-01-05 14:37:40 -0500439 // This special case isn't needed for correctness. It just avoids a multiplication by
440 // a vertex attribute alpha that is known to be 1 if we take the kSrcIn path.
Brian Salomon0c243202020-06-29 14:29:25 -0400441 return GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700442 }
Brian Salomonb43d6992021-01-05 14:37:40 -0500443 return GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kSrcIn);
reed856e9d92015-09-30 12:21:45 -0700444}
445
446#endif
reed320a40d2016-08-02 06:12:06 -0700447
448///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500449#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700450
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400451sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400452 SkTileMode tmx, SkTileMode tmy,
Mike Reed172ba9e2020-12-17 15:57:55 -0500453 const SkSamplingOptions& sampling,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400454 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
Mike Reed172ba9e2020-12-17 15:57:55 -0500455 auto s = SkImageShader::Make(SkMakeImageFromRasterBitmap(src, mode),
456 tmx, tmy, &sampling, localMatrix);
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400457 if (!s) {
458 return nullptr;
459 }
460 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
461 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
462 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
463 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400464 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400465 }
466 return s;
467}
468
Brian Salomon23356442018-11-30 15:33:19 -0500469void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700470
Mike Reed9318a6c2019-08-16 16:16:25 -0400471class SkImageStageUpdater : public SkStageUpdater {
472public:
Mike Reed8845c372019-12-19 13:22:08 -0500473 SkImageStageUpdater(const SkImageShader* shader, bool usePersp)
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500474 : fShader(shader)
475 , fUsePersp(usePersp || as_SB(shader)->getLocalMatrix().hasPerspective())
Mike Reed8845c372019-12-19 13:22:08 -0500476 {}
Mike Reed9318a6c2019-08-16 16:16:25 -0400477
Mike Reed8845c372019-12-19 13:22:08 -0500478 const SkImageShader* fShader;
479 const bool fUsePersp; // else use affine
480
481 // large enough for perspective, though often we just use 2x3
482 float fMatrixStorage[9];
Mike Reed9318a6c2019-08-16 16:16:25 -0400483
484#if 0 // TODO: when we support mipmaps
485 SkRasterPipeline_GatherCtx* fGather;
486 SkRasterPipeline_TileCtx* fLimitX;
487 SkRasterPipeline_TileCtx* fLimitY;
488 SkRasterPipeline_DecalTileCtx* fDecal;
489#endif
490
Mike Reed8845c372019-12-19 13:22:08 -0500491 void append_matrix_stage(SkRasterPipeline* p) {
492 if (fUsePersp) {
493 p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage);
494 } else {
495 p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage);
496 }
497 }
498
Mike Reed9318a6c2019-08-16 16:16:25 -0400499 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
500 SkMatrix matrix;
Mike Reed8845c372019-12-19 13:22:08 -0500501 if (fShader->computeTotalInverse(ctm, localM, &matrix)) {
502 if (fUsePersp) {
503 matrix.get9(fMatrixStorage);
504 } else {
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500505 // if we get here, matrix should be affine. If it isn't, then defensively we
506 // won't draw (by returning false), but we should work to never let this
507 // happen (i.e. better preflight by the caller to know ahead of time that we
508 // may encounter perspective, either in the CTM, or in the localM).
509 //
510 // See https://bugs.chromium.org/p/skia/issues/detail?id=10004
511 //
512 if (!matrix.asAffine(fMatrixStorage)) {
513 SkASSERT(false);
514 return false;
515 }
Mike Reed8845c372019-12-19 13:22:08 -0500516 }
517 return true;
518 }
519 return false;
Mike Reed9318a6c2019-08-16 16:16:25 -0400520 }
521};
522
Mike Kleindcc89602020-12-02 15:06:15 -0600523static SkSamplingOptions tweak_filter_and_inv_matrix(SkSamplingOptions sampling, SkMatrix* matrix) {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600524 SkFilterMode filter = sampling.filter;
Mike Kleindcc89602020-12-02 15:06:15 -0600525
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600526 // When the matrix is just an integer translate, bilerp == nearest neighbor.
Mike Kleindcc89602020-12-02 15:06:15 -0600527 if (filter == SkFilterMode::kLinear &&
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600528 matrix->getType() <= SkMatrix::kTranslate_Mask &&
529 matrix->getTranslateX() == (int)matrix->getTranslateX() &&
530 matrix->getTranslateY() == (int)matrix->getTranslateY()) {
Mike Kleindcc89602020-12-02 15:06:15 -0600531 filter = SkFilterMode::kNearest;
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600532 }
533
534 // See skia:4649 and the GM image_scale_aligned.
Mike Kleindcc89602020-12-02 15:06:15 -0600535 if (filter == SkFilterMode::kNearest) {
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600536 if (matrix->getScaleX() >= 0) {
537 matrix->setTranslateX(nextafterf(matrix->getTranslateX(),
538 floorf(matrix->getTranslateX())));
539 }
540 if (matrix->getScaleY() >= 0) {
541 matrix->setTranslateY(nextafterf(matrix->getTranslateY(),
542 floorf(matrix->getTranslateY())));
543 }
544 }
Mike Kleindcc89602020-12-02 15:06:15 -0600545
Mike Kleinbb1933e2020-12-02 15:45:29 -0600546 return SkSamplingOptions(filter, sampling.mipmap);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600547}
548
Mike Reed9318a6c2019-08-16 16:16:25 -0400549bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
Mike Reed39b4c862020-11-25 16:15:53 -0500550 auto sampling = fUseSamplingOptions ? fSampling
551 : SkSamplingOptions(rec.fPaint.getFilterQuality());
552
553 // We only support certain sampling options in stages so far
Mike Kleinbb1933e2020-12-02 15:45:29 -0600554 if (sampling.useCubic) {
555 if (!is_default_cubic_resampler(sampling.cubic)) {
Mike Reed39b4c862020-11-25 16:15:53 -0500556 return false;
Mike Reed74c51ac2020-11-20 10:23:58 -0500557 }
Mike Kleinbb1933e2020-12-02 15:45:29 -0600558 } else if (sampling.mipmap == SkMipmapMode::kLinear) {
Mike Reed39b4c862020-11-25 16:15:53 -0500559 return false;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400560 }
Mike Reed9290d012020-06-11 16:56:06 -0400561
Mike Reed39b4c862020-11-25 16:15:53 -0500562
Mike Kleinbb1933e2020-12-02 15:45:29 -0600563 if (updater && (sampling.mipmap != SkMipmapMode::kNone)) {
Mike Reed8845c372019-12-19 13:22:08 -0500564 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400565 return false;
566 }
567
Mike Reed1d8c42e2017-08-29 14:58:19 -0400568 SkRasterPipeline* p = rec.fPipeline;
569 SkArenaAlloc* alloc = rec.fAlloc;
570
Florin Malita7558e4d2018-02-07 10:05:53 -0500571 SkMatrix matrix;
Brian Osman9aaec362020-05-08 14:54:37 -0400572 if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500573 return false;
574 }
Mike Klein06a65e22016-11-17 12:39:09 -0500575
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500576 if (sampling.useCubic &&
577 SkMatrixPriv::AdjustHighQualityFilterLevel(matrix, true) != kHigh_SkFilterQuality)
578 {
579 sampling = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNearest);
Mike Kleinf447dee2016-11-29 16:37:12 -0500580 }
Mike Reedbccdd902020-12-07 12:36:02 -0500581 auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), matrix, sampling.mipmap);
582 if (!access) {
583 return false;
584 }
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500585 SkPixmap pm;
586 std::tie(pm, matrix) = access->level();
Mike Kleinf447dee2016-11-29 16:37:12 -0500587
Mike Kleine8de0242018-03-10 12:37:11 -0500588 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400589
590 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500591 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400592 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600593 if (!sampling.useCubic) {
Mike Kleindcc89602020-12-02 15:06:15 -0600594 sampling = tweak_filter_and_inv_matrix(sampling, &matrix);
Mike Reed3d58d5a2020-11-23 13:32:36 -0500595 }
Mike Reed9318a6c2019-08-16 16:16:25 -0400596 p->append_matrix(alloc, matrix);
597 }
Mike Klein06a65e22016-11-17 12:39:09 -0500598
Mike Kleinb11ab572018-10-24 06:42:14 -0400599 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500600 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400601 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400602 gather->width = pm.width();
603 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400604
Mike Kleinb11ab572018-10-24 06:42:14 -0400605 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
606 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400607 limit_x->scale = pm.width();
608 limit_x->invScale = 1.0f / pm.width();
609 limit_y->scale = pm.height();
610 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400611
Mike Kleinb11ab572018-10-24 06:42:14 -0400612 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400613 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
614 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400615 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500616 decal_ctx->limit_x = limit_x->scale;
617 decal_ctx->limit_y = limit_y->scale;
618 }
619
Mike Reed9318a6c2019-08-16 16:16:25 -0400620#if 0 // TODO: when we support kMedium
621 if (updator && (quality == kMedium_SkFilterQuality)) {
622 // if we change levels in mipmap, we need to update the scales (and invScales)
623 updator->fGather = gather;
624 updator->fLimitX = limit_x;
625 updator->fLimitY = limit_y;
626 updator->fDecal = decal_ctx;
627 }
628#endif
629
Mike Kleinb04c3522016-11-28 11:55:58 -0500630 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500631 if (decal_x_and_y) {
632 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
633 } else {
634 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400635 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
636 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
637 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
638 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500639 }
640 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400641 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
642 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
643 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
644 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500645 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500646 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500647
Mike Kleinac568a92018-01-25 09:09:32 -0500648 void* ctx = gather;
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500649 switch (pm.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500650 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400651 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
652 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500653 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
654 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400655 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
656 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
657 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500658 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
659 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400660 case kR16G16B16A16_unorm_SkColorType:
661 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600662 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500663 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400664 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500665
Mike Kleinb1df5e52018-10-17 17:06:03 -0400666 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
667 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400668
Mike Kleinac568a92018-01-25 09:09:32 -0500669 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
670 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400671
Mike Kleinf7eb0542020-02-11 12:19:08 -0600672 case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
673 p->append(SkRasterPipeline::swap_rb ); break;
674
Mike Kleinac568a92018-01-25 09:09:32 -0500675 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
676 p->append(SkRasterPipeline::force_opaque ); break;
677
Mike Kleinf7eb0542020-02-11 12:19:08 -0600678 case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
679 p->append(SkRasterPipeline::force_opaque );
680 p->append(SkRasterPipeline::swap_rb ); break;
681
Mike Klein1a3eb522018-10-18 10:11:00 -0400682 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
683 p->append(SkRasterPipeline::swap_rb ); break;
684
Mike Kleinb70990e2019-02-28 10:03:27 -0600685 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500686 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500687 if (decal_ctx) {
688 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
689 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500690 };
691
Mike Klein1fa9c432017-12-11 09:59:47 -0500692 auto append_misc = [&] {
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500693 SkColorSpace* cs = pm.colorSpace();
694 SkAlphaType at = pm.alphaType();
Mike Klein6a9f1c42020-01-02 23:23:42 -0600695
696 // Color for A8 images comes from the paint. TODO: all alpha images? none?
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500697 if (pm.colorType() == kAlpha_8_SkColorType) {
Mike Klein6a9f1c42020-01-02 23:23:42 -0600698 SkColor4f rgb = rec.fPaint.getColor4f();
699 p->append_set_rgb(alloc, rgb);
700
Mike Klein6a9f1c42020-01-02 23:23:42 -0600701 cs = sk_srgb_singleton();
702 at = kUnpremul_SkAlphaType;
703 }
704
Mike Klein059e0432020-01-02 16:32:38 -0600705 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Kleinbb1933e2020-12-02 15:45:29 -0600706 if (sampling.useCubic) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500707 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600708 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
709 ? SkRasterPipeline::clamp_1
710 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500711 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000712
Mike Klein059e0432020-01-02 16:32:38 -0600713 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
714 alloc->make<SkColorSpaceXformSteps>(cs, at,
715 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500716 ->apply(p);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000717
Mike Klein1fa9c432017-12-11 09:59:47 -0500718 return true;
719 };
720
Mike Reed9318a6c2019-08-16 16:16:25 -0400721 // Check for fast-path stages.
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500722 auto ct = pm.colorType();
Mike Klein8e3426f2018-04-16 12:56:24 -0400723 if (true
724 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Kleinbb1933e2020-12-02 15:45:29 -0600725 && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear
Mike Reedfae8fce2019-04-03 10:27:45 -0400726 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500727
728 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400729 if (ct == kBGRA_8888_SkColorType) {
730 p->append(SkRasterPipeline::swap_rb);
731 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500732 return append_misc();
733 }
Mike Reed78eedba2019-07-31 16:39:15 -0400734 if (true
Mike Klein01005622019-08-13 12:22:17 -0400735 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Kleinbb1933e2020-12-02 15:45:29 -0600736 && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear
Mike Klein01005622019-08-13 12:22:17 -0400737 && fTileModeX != SkTileMode::kDecal // TODO decal too?
738 && fTileModeY != SkTileMode::kDecal) {
739
740 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
741 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
742 ctx->ct = ct;
743 ctx->tileX = fTileModeX;
744 ctx->tileY = fTileModeY;
745 ctx->invWidth = 1.0f / ctx->width;
746 ctx->invHeight = 1.0f / ctx->height;
747 p->append(SkRasterPipeline::bilinear, ctx);
748 return append_misc();
749 }
750 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400751 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Kleinbb1933e2020-12-02 15:45:29 -0600752 && sampling.useCubic
Mike Reed78eedba2019-07-31 16:39:15 -0400753 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
754
755 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
756 if (ct == kBGRA_8888_SkColorType) {
757 p->append(SkRasterPipeline::swap_rb);
758 }
759 return append_misc();
760 }
Mike Klein01005622019-08-13 12:22:17 -0400761 if (true
762 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Kleinbb1933e2020-12-02 15:45:29 -0600763 && sampling.useCubic
Mike Klein01005622019-08-13 12:22:17 -0400764 && fTileModeX != SkTileMode::kDecal // TODO decal too?
765 && fTileModeY != SkTileMode::kDecal) {
766
767 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
768 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
769 ctx->ct = ct;
770 ctx->tileX = fTileModeX;
771 ctx->tileY = fTileModeY;
772 ctx->invWidth = 1.0f / ctx->width;
773 ctx->invHeight = 1.0f / ctx->height;
774 p->append(SkRasterPipeline::bicubic, ctx);
775 return append_misc();
776 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500777
Mike Reed3d58d5a2020-11-23 13:32:36 -0500778 SkRasterPipeline_SamplerCtx* sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400779
Mike Kleinb0b17d12016-12-09 16:25:44 -0500780 auto sample = [&](SkRasterPipeline::StockStage setup_x,
781 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400782 p->append(setup_x, sampler);
783 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500784 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400785 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500786 };
787
Mike Kleinbb1933e2020-12-02 15:45:29 -0600788 if (sampling.useCubic) {
Mike Klein0a904492017-04-12 12:52:48 -0400789 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500790
791 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
792 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
793 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
794 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
795
796 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
797 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
798 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
799 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
800
801 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
802 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
803 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
804 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
805
806 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
807 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
808 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
809 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
810
Mike Kleinb04c3522016-11-28 11:55:58 -0500811 p->append(SkRasterPipeline::move_dst_src);
Mike Kleinbb1933e2020-12-02 15:45:29 -0600812 } else if (sampling.filter == SkFilterMode::kLinear) {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500813 p->append(SkRasterPipeline::save_xy, sampler);
814
815 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
816 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
817 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
818 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
819
820 p->append(SkRasterPipeline::move_dst_src);
821 } else {
822 append_tiling_and_gather();
Mike Klein06a65e22016-11-17 12:39:09 -0500823 }
824
Mike Klein1fa9c432017-12-11 09:59:47 -0500825 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500826}
Mike Reed9318a6c2019-08-16 16:16:25 -0400827
828bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
829 return this->doStages(rec, nullptr);
830}
831
832SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Brian Osman9aaec362020-05-08 14:54:37 -0400833 bool usePersp = rec.fMatrixProvider.localToDevice().hasPerspective();
Mike Reed8845c372019-12-19 13:22:08 -0500834 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400835 return this->doStages(rec, updater) ? updater : nullptr;
836}
837
Brian Osman5aaaeea2020-06-22 14:26:03 -0400838skvm::Color SkImageShader::onProgram(skvm::Builder* p,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400839 skvm::Coord device, skvm::Coord origLocal, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500840 const SkMatrixProvider& matrices, const SkMatrix* localM,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400841 SkFilterQuality paintQuality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500842 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400843 SkMatrix baseInv;
844 if (!this->computeTotalInverse(matrices.localToDevice(), localM, &baseInv)) {
Mike Reed6352f002020-03-14 23:30:10 -0400845 return {};
Mike Kleinf6a715b2019-12-30 15:24:18 -0600846 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400847 baseInv.normalizePerspective();
Mike Kleinf6a715b2019-12-30 15:24:18 -0600848
Mike Reed3d58d5a2020-11-23 13:32:36 -0500849 auto sampling = fUseSamplingOptions ? fSampling : SkSamplingOptions(paintQuality);
Mike Reedbccdd902020-12-07 12:36:02 -0500850 auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), baseInv, sampling.mipmap);
851 if (!access) {
852 return {};
853 }
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500854 auto [upper, upperInv] = access->level();
855 if (!sampling.useCubic) {
Mike Kleindcc89602020-12-02 15:06:15 -0600856 sampling = tweak_filter_and_inv_matrix(sampling, &upperInv);
Mike Kleinf6a715b2019-12-30 15:24:18 -0600857 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600858
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500859 SkPixmap lowerPixmap;
860 SkMatrix lowerInv;
861 SkPixmap* lower = nullptr;
862 float lowerWeight = access->lowerWeight();
863 if (lowerWeight > 0) {
864 std::tie(lowerPixmap, lowerInv) = access->lowerLevel();
865 lower = &lowerPixmap;
866 }
867
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400868 skvm::Coord upperLocal = SkShaderBase::ApplyMatrix(p, upperInv, origLocal, uniforms);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600869
Mike Klein89b3c1f2020-07-29 16:45:05 -0500870 // All existing SkColorTypes pass these checks. We'd only fail here adding new ones.
Mike Klein03c932c2020-07-13 09:07:42 -0500871 skvm::PixelFormat unused;
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500872 if (true && !SkColorType_to_PixelFormat(upper.colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500873 return {};
874 }
Mike Klein9662fd62020-07-16 15:11:27 -0500875 if (lower && !SkColorType_to_PixelFormat(lower->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500876 return {};
Mike Klein25480bf2020-01-06 14:01:58 -0600877 }
878
Mike Klein921236b2020-02-13 11:20:54 -0600879 // We can exploit image opacity to skip work unpacking alpha channels.
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500880 const bool input_is_opaque = SkAlphaTypeIsOpaque(upper.alphaType())
881 || SkColorTypeIsAlwaysOpaque(upper.colorType());
Mike Klein921236b2020-02-13 11:20:54 -0600882
Mike Klein03d89ef2020-01-14 17:18:29 -0600883 // Each call to sample() will try to rewrite the same uniforms over and over,
884 // so remember where we start and reset back there each time. That way each
885 // sample() call uses the same uniform offsets.
Mike Klein03d89ef2020-01-14 17:18:29 -0600886
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400887 auto compute_clamp_limit = [&](float limit) {
888 // Subtract an ulp so the upper clamp limit excludes limit itself.
889 int bits;
890 memcpy(&bits, &limit, 4);
891 return p->uniformF(uniforms->push(bits-1));
892 };
Mike Klein03d89ef2020-01-14 17:18:29 -0600893
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400894 // Except in the simplest case (no mips, no filtering), we reference uniforms
895 // more than once. To avoid adding/registering them multiple times, we pre-load them
896 // into a struct (just to logically group them together), based on the "current"
897 // pixmap (level of a mipmap).
898 //
899 struct Uniforms {
900 skvm::F32 w, iw, i2w,
901 h, ih, i2h;
902
903 skvm::F32 clamp_w,
904 clamp_h;
905
906 skvm::Uniform addr;
907 skvm::I32 rowBytesAsPixels;
908
Mike Klein03c932c2020-07-13 09:07:42 -0500909 skvm::PixelFormat pixelFormat; // not a uniform, but needed for each texel sample,
910 // so we store it here, since it is also dependent on
911 // the current pixmap (level).
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400912 };
913
914 auto setup_uniforms = [&](const SkPixmap& pm) -> Uniforms {
Mike Klein03c932c2020-07-13 09:07:42 -0500915 skvm::PixelFormat pixelFormat;
Mike Klein9662fd62020-07-16 15:11:27 -0500916 SkAssertResult(SkColorType_to_PixelFormat(pm.colorType(), &pixelFormat));
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400917 return {
918 p->uniformF(uniforms->pushF( pm.width())),
919 p->uniformF(uniforms->pushF(1.0f/pm.width())), // iff tileX == kRepeat
920 p->uniformF(uniforms->pushF(0.5f/pm.width())), // iff tileX == kMirror
921
922 p->uniformF(uniforms->pushF( pm.height())),
923 p->uniformF(uniforms->pushF(1.0f/pm.height())), // iff tileY == kRepeat
924 p->uniformF(uniforms->pushF(0.5f/pm.height())), // iff tileY == kMirror
925
926 compute_clamp_limit(pm. width()),
927 compute_clamp_limit(pm.height()),
928
929 uniforms->pushPtr(pm.addr()),
930 p->uniform32(uniforms->push(pm.rowBytesAsPixels())),
931
Mike Klein03c932c2020-07-13 09:07:42 -0500932 pixelFormat,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400933 };
934 };
935
936 auto sample_texel = [&](const Uniforms& u, skvm::F32 sx, skvm::F32 sy) -> skvm::Color {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600937 // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400938 auto repeat = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I) {
Mike Reedf3b9a302020-04-01 13:18:02 -0400939 return v - floor(v * I) * S;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600940 };
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400941 auto mirror = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I2) {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600942 // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale )
943 // {---A---} {------------------B------------------}
Mike Reedf3b9a302020-04-01 13:18:02 -0400944 skvm::F32 A = v - S,
945 B = (S + S) * floor(A * I2);
946 return abs(A - B - S);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600947 };
948 switch (fTileModeX) {
949 case SkTileMode::kDecal: /* handled after gather */ break;
950 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400951 case SkTileMode::kRepeat: sx = repeat(sx, u.w, u.iw); break;
952 case SkTileMode::kMirror: sx = mirror(sx, u.w, u.i2w); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600953 }
954 switch (fTileModeY) {
955 case SkTileMode::kDecal: /* handled after gather */ break;
956 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400957 case SkTileMode::kRepeat: sy = repeat(sy, u.h, u.ih); break;
958 case SkTileMode::kMirror: sy = mirror(sy, u.h, u.i2h); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600959 }
960
961 // Always clamp sample coordinates to [0,width), [0,height), both for memory
962 // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400963 skvm::F32 clamped_x = clamp(sx, 0, u.clamp_w),
964 clamped_y = clamp(sy, 0, u.clamp_h);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600965
966 // Load pixels from pm.addr()[(int)sx + (int)sy*stride].
Mike Reedf3b9a302020-04-01 13:18:02 -0400967 skvm::I32 index = trunc(clamped_x) +
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400968 trunc(clamped_y) * u.rowBytesAsPixels;
Mike Klein03c932c2020-07-13 09:07:42 -0500969 skvm::Color c = gather(u.pixelFormat, u.addr, index);
Mike Klein913a6f52020-03-23 11:06:25 -0500970
Mike Klein921236b2020-02-13 11:20:54 -0600971 // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it.
972 if (input_is_opaque) {
Mike Klein46874ef2020-02-13 10:17:08 -0600973 c.a = p->splat(1.0f);
974 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600975
976 // Mask away any pixels that we tried to sample outside the bounds in kDecal.
977 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
978 skvm::I32 mask = p->splat(~0);
Mike Reedf3b9a302020-04-01 13:18:02 -0400979 if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); }
980 if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); }
Mike Klein5ec9c4e2020-12-01 10:43:46 -0600981 c.r = pun_to_F32(p->bit_and(mask, pun_to_I32(c.r)));
982 c.g = pun_to_F32(p->bit_and(mask, pun_to_I32(c.g)));
983 c.b = pun_to_F32(p->bit_and(mask, pun_to_I32(c.b)));
984 c.a = pun_to_F32(p->bit_and(mask, pun_to_I32(c.a)));
Mike Klein921236b2020-02-13 11:20:54 -0600985 // Notice that even if input_is_opaque, c.a might now be 0.
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600986 }
987
988 return c;
989 };
990
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400991 auto sample_level = [&](const SkPixmap& pm, const SkMatrix& inv, skvm::Coord local) {
992 const Uniforms u = setup_uniforms(pm);
Mike Reed6352f002020-03-14 23:30:10 -0400993
Mike Kleinbb1933e2020-12-02 15:45:29 -0600994 if (sampling.useCubic) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400995 // All bicubic samples have the same fractional offset (fx,fy) from the center.
996 // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center.
997 skvm::F32 fx = fract(local.x + 0.5f),
998 fy = fract(local.y + 0.5f);
Mike Reed3d30ca62020-07-22 16:55:02 -0400999 skvm::F32 wx[4],
1000 wy[4];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001001
Mike Kleinbb1933e2020-12-02 15:45:29 -06001002 SkM44 weights = CubicResamplerMatrix(sampling.cubic.B, sampling.cubic.C);
Mike Reed3d30ca62020-07-22 16:55:02 -04001003
Mike Kleind3184892020-07-22 17:39:20 -05001004 auto dot = [](const skvm::F32 a[], const skvm::F32 b[]) {
1005 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
1006 };
1007 const skvm::F32 tmpx[] = { p->splat(1.0f), fx, fx*fx, fx*fx*fx };
1008 const skvm::F32 tmpy[] = { p->splat(1.0f), fy, fy*fy, fy*fy*fy };
Mike Reed3d30ca62020-07-22 16:55:02 -04001009
Mike Kleind3184892020-07-22 17:39:20 -05001010 for (int row = 0; row < 4; ++row) {
1011 SkV4 r = weights.row(row);
1012 skvm::F32 ru[] = {
1013 p->uniformF(uniforms->pushF(r[0])),
1014 p->uniformF(uniforms->pushF(r[1])),
1015 p->uniformF(uniforms->pushF(r[2])),
1016 p->uniformF(uniforms->pushF(r[3])),
Mike Reed3d30ca62020-07-22 16:55:02 -04001017 };
Mike Kleind3184892020-07-22 17:39:20 -05001018 wx[row] = dot(ru, tmpx);
1019 wy[row] = dot(ru, tmpy);
Mike Reed3d30ca62020-07-22 16:55:02 -04001020 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001021
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001022 skvm::Color c;
1023 c.r = c.g = c.b = c.a = p->splat(0.0f);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001024
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001025 skvm::F32 sy = local.y - 1.5f;
1026 for (int j = 0; j < 4; j++, sy += 1.0f) {
1027 skvm::F32 sx = local.x - 1.5f;
1028 for (int i = 0; i < 4; i++, sx += 1.0f) {
1029 skvm::Color s = sample_texel(u, sx,sy);
1030 skvm::F32 w = wx[i] * wy[j];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001031
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001032 c.r += s.r * w;
1033 c.g += s.g * w;
1034 c.b += s.b * w;
1035 c.a += s.a * w;
1036 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001037 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001038 return c;
Mike Kleinbb1933e2020-12-02 15:45:29 -06001039 } else if (sampling.filter == SkFilterMode::kLinear) {
Mike Reed3d58d5a2020-11-23 13:32:36 -05001040 // Our four sample points are the corners of a logical 1x1 pixel
1041 // box surrounding (x,y) at (0.5,0.5) off-center.
1042 skvm::F32 left = local.x - 0.5f,
1043 top = local.y - 0.5f,
1044 right = local.x + 0.5f,
1045 bottom = local.y + 0.5f;
1046
1047 // The fractional parts of right and bottom are our lerp factors in x and y respectively.
1048 skvm::F32 fx = fract(right ),
1049 fy = fract(bottom);
1050
1051 return lerp(lerp(sample_texel(u, left,top ), sample_texel(u, right,top ), fx),
1052 lerp(sample_texel(u, left,bottom), sample_texel(u, right,bottom), fx), fy);
1053 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -06001054 SkASSERT(sampling.filter == SkFilterMode::kNearest);
Mike Reed3d58d5a2020-11-23 13:32:36 -05001055 return sample_texel(u, local.x,local.y);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001056 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001057 };
1058
Mike Reed8c1ad7e2020-12-02 20:41:52 -05001059 skvm::Color c = sample_level(upper, upperInv, upperLocal);
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001060 if (lower) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001061 auto lowerLocal = SkShaderBase::ApplyMatrix(p, lowerInv, origLocal, uniforms);
1062 // lower * weight + upper * (1 - weight)
1063 c = lerp(c,
1064 sample_level(*lower, lowerInv, lowerLocal),
1065 p->uniformF(uniforms->pushF(lowerWeight)));
Mike Klein921236b2020-02-13 11:20:54 -06001066 }
Mike Kleind67a5c32020-01-06 16:01:01 -06001067
Mike Klein921236b2020-02-13 11:20:54 -06001068 // If the input is opaque and we're not in decal mode, that means the output is too.
1069 // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas.
1070 if (input_is_opaque
1071 && fTileModeX != SkTileMode::kDecal
1072 && fTileModeY != SkTileMode::kDecal) {
Mike Reed6352f002020-03-14 23:30:10 -04001073 c.a = p->splat(1.0f);
Mike Klein921236b2020-02-13 11:20:54 -06001074 }
1075
Mike Klein913a6f52020-03-23 11:06:25 -05001076 // Alpha-only images get their color from the paint (already converted to dst color space).
Mike Reed8c1ad7e2020-12-02 20:41:52 -05001077 SkColorSpace* cs = upper.colorSpace();
1078 SkAlphaType at = upper.alphaType();
1079 if (SkColorTypeIsAlphaOnly(upper.colorType())) {
Mike Klein913a6f52020-03-23 11:06:25 -05001080 c.r = paint.r;
1081 c.g = paint.g;
1082 c.b = paint.b;
1083
1084 cs = dst.colorSpace();
1085 at = kUnpremul_SkAlphaType;
1086 }
1087
Mike Kleinbb1933e2020-12-02 15:45:29 -06001088 if (sampling.useCubic) {
Mike Klein5c660e02020-01-08 11:36:35 -06001089 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reedf3b9a302020-04-01 13:18:02 -04001090 c.a = clamp01(c.a);
Mike Klein3f83bfd2020-01-07 12:01:50 -06001091
Mike Klein913a6f52020-03-23 11:06:25 -05001092 skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul)
Mike Klein3f83bfd2020-01-07 12:01:50 -06001093 ? p->splat(1.0f)
Mike Reed6352f002020-03-14 23:30:10 -04001094 : c.a;
Mike Reedf3b9a302020-04-01 13:18:02 -04001095 c.r = clamp(c.r, 0.0f, limit);
1096 c.g = clamp(c.g, 0.0f, limit);
1097 c.b = clamp(c.b, 0.0f, limit);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001098 }
Mike Klein4bc86d52020-01-06 18:39:42 -06001099
Mike Kleinef0fa432020-07-29 12:35:45 -05001100 return SkColorSpaceXformSteps{cs,at, dst.colorSpace(),dst.alphaType()}.program(p, uniforms, c);
Mike Kleinf6a715b2019-12-30 15:24:18 -06001101}