blob: 88f5aa214c8ac7ed9fc94d960cf5f60e07e5d31f [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;
Brian Salomond0924f32021-02-03 10:15:31 -0500410 auto [view, ct] = as_IB(fImage)->asView(args.fContext, mipmapped);
Brian Salomon490f1922021-02-01 13:14:37 -0500411 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 Klein71d420d2021-02-03 09:54:07 -0600523static SkSamplingOptions tweak_sampling(SkSamplingOptions sampling, const 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 Klein71d420d2021-02-03 09:54:07 -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
Mike Klein71d420d2021-02-03 09:54:07 -0600534 return SkSamplingOptions(filter, sampling.mipmap);
535}
536
537static SkMatrix tweak_inv_matrix(SkFilterMode filter, SkMatrix matrix) {
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600538 // See skia:4649 and the GM image_scale_aligned.
Mike Kleindcc89602020-12-02 15:06:15 -0600539 if (filter == SkFilterMode::kNearest) {
Mike Klein71d420d2021-02-03 09:54:07 -0600540 if (matrix.getScaleX() >= 0) {
541 matrix.setTranslateX(nextafterf(matrix.getTranslateX(),
542 floorf(matrix.getTranslateX())));
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600543 }
Mike Klein71d420d2021-02-03 09:54:07 -0600544 if (matrix.getScaleY() >= 0) {
545 matrix.setTranslateY(nextafterf(matrix.getTranslateY(),
546 floorf(matrix.getTranslateY())));
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600547 }
548 }
Mike Klein71d420d2021-02-03 09:54:07 -0600549 return matrix;
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600550}
551
Mike Reed9318a6c2019-08-16 16:16:25 -0400552bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
Mike Reed39b4c862020-11-25 16:15:53 -0500553 auto sampling = fUseSamplingOptions ? fSampling
554 : SkSamplingOptions(rec.fPaint.getFilterQuality());
555
556 // We only support certain sampling options in stages so far
Mike Kleinbb1933e2020-12-02 15:45:29 -0600557 if (sampling.useCubic) {
558 if (!is_default_cubic_resampler(sampling.cubic)) {
Mike Reed39b4c862020-11-25 16:15:53 -0500559 return false;
Mike Reed74c51ac2020-11-20 10:23:58 -0500560 }
Mike Kleinbb1933e2020-12-02 15:45:29 -0600561 } else if (sampling.mipmap == SkMipmapMode::kLinear) {
Mike Reed39b4c862020-11-25 16:15:53 -0500562 return false;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400563 }
Mike Reed9290d012020-06-11 16:56:06 -0400564
Mike Reed39b4c862020-11-25 16:15:53 -0500565
Mike Kleinbb1933e2020-12-02 15:45:29 -0600566 if (updater && (sampling.mipmap != SkMipmapMode::kNone)) {
Mike Reed8845c372019-12-19 13:22:08 -0500567 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400568 return false;
569 }
570
Mike Reed1d8c42e2017-08-29 14:58:19 -0400571 SkRasterPipeline* p = rec.fPipeline;
572 SkArenaAlloc* alloc = rec.fAlloc;
573
Florin Malita7558e4d2018-02-07 10:05:53 -0500574 SkMatrix matrix;
Brian Osman9aaec362020-05-08 14:54:37 -0400575 if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500576 return false;
577 }
Florin Malitac7d6e0b2021-02-04 17:46:25 -0500578 matrix.normalizePerspective();
Mike Klein06a65e22016-11-17 12:39:09 -0500579
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500580 if (sampling.useCubic &&
581 SkMatrixPriv::AdjustHighQualityFilterLevel(matrix, true) != kHigh_SkFilterQuality)
582 {
583 sampling = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNearest);
Mike Kleinf447dee2016-11-29 16:37:12 -0500584 }
Mike Reedbccdd902020-12-07 12:36:02 -0500585 auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), matrix, sampling.mipmap);
586 if (!access) {
587 return false;
588 }
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500589 SkPixmap pm;
590 std::tie(pm, matrix) = access->level();
Mike Kleinf447dee2016-11-29 16:37:12 -0500591
Mike Kleine8de0242018-03-10 12:37:11 -0500592 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400593
594 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500595 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400596 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600597 if (!sampling.useCubic) {
Mike Klein71d420d2021-02-03 09:54:07 -0600598 // TODO: can tweak_sampling sometimes for cubic too when B=0
599 if (rec.fMatrixProvider.localToDeviceHitsPixelCenters()) {
600 sampling = tweak_sampling(sampling, matrix);
601 }
602 matrix = tweak_inv_matrix(sampling.filter, matrix);
Mike Reed3d58d5a2020-11-23 13:32:36 -0500603 }
Mike Reed9318a6c2019-08-16 16:16:25 -0400604 p->append_matrix(alloc, matrix);
605 }
Mike Klein06a65e22016-11-17 12:39:09 -0500606
Mike Kleinb11ab572018-10-24 06:42:14 -0400607 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500608 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400609 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400610 gather->width = pm.width();
611 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400612
Mike Kleinb11ab572018-10-24 06:42:14 -0400613 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
614 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400615 limit_x->scale = pm.width();
616 limit_x->invScale = 1.0f / pm.width();
617 limit_y->scale = pm.height();
618 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400619
Mike Kleinb11ab572018-10-24 06:42:14 -0400620 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400621 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
622 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400623 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500624 decal_ctx->limit_x = limit_x->scale;
625 decal_ctx->limit_y = limit_y->scale;
626 }
627
Mike Reed9318a6c2019-08-16 16:16:25 -0400628#if 0 // TODO: when we support kMedium
629 if (updator && (quality == kMedium_SkFilterQuality)) {
630 // if we change levels in mipmap, we need to update the scales (and invScales)
631 updator->fGather = gather;
632 updator->fLimitX = limit_x;
633 updator->fLimitY = limit_y;
634 updator->fDecal = decal_ctx;
635 }
636#endif
637
Mike Kleinb04c3522016-11-28 11:55:58 -0500638 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500639 if (decal_x_and_y) {
640 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
641 } else {
642 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400643 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
644 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
645 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
646 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500647 }
648 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400649 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
650 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
651 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
652 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500653 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500654 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500655
Mike Kleinac568a92018-01-25 09:09:32 -0500656 void* ctx = gather;
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500657 switch (pm.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500658 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400659 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
660 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500661 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
662 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400663 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
664 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
665 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500666 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
667 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400668 case kR16G16B16A16_unorm_SkColorType:
669 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600670 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500671 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400672 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500673
Mike Kleinb1df5e52018-10-17 17:06:03 -0400674 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
675 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400676
Mike Kleinac568a92018-01-25 09:09:32 -0500677 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
678 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400679
Mike Kleinf7eb0542020-02-11 12:19:08 -0600680 case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
681 p->append(SkRasterPipeline::swap_rb ); break;
682
Mike Kleinac568a92018-01-25 09:09:32 -0500683 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
684 p->append(SkRasterPipeline::force_opaque ); break;
685
Mike Kleinf7eb0542020-02-11 12:19:08 -0600686 case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
687 p->append(SkRasterPipeline::force_opaque );
688 p->append(SkRasterPipeline::swap_rb ); break;
689
Mike Klein1a3eb522018-10-18 10:11:00 -0400690 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
691 p->append(SkRasterPipeline::swap_rb ); break;
692
Mike Kleinb70990e2019-02-28 10:03:27 -0600693 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500694 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500695 if (decal_ctx) {
696 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
697 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500698 };
699
Mike Klein1fa9c432017-12-11 09:59:47 -0500700 auto append_misc = [&] {
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500701 SkColorSpace* cs = pm.colorSpace();
702 SkAlphaType at = pm.alphaType();
Mike Klein6a9f1c42020-01-02 23:23:42 -0600703
704 // Color for A8 images comes from the paint. TODO: all alpha images? none?
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500705 if (pm.colorType() == kAlpha_8_SkColorType) {
Mike Klein6a9f1c42020-01-02 23:23:42 -0600706 SkColor4f rgb = rec.fPaint.getColor4f();
707 p->append_set_rgb(alloc, rgb);
708
Mike Klein6a9f1c42020-01-02 23:23:42 -0600709 cs = sk_srgb_singleton();
710 at = kUnpremul_SkAlphaType;
711 }
712
Mike Klein059e0432020-01-02 16:32:38 -0600713 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Kleinbb1933e2020-12-02 15:45:29 -0600714 if (sampling.useCubic) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500715 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600716 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
717 ? SkRasterPipeline::clamp_1
718 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500719 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000720
Mike Klein059e0432020-01-02 16:32:38 -0600721 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
722 alloc->make<SkColorSpaceXformSteps>(cs, at,
723 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500724 ->apply(p);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000725
Mike Klein1fa9c432017-12-11 09:59:47 -0500726 return true;
727 };
728
Mike Reed9318a6c2019-08-16 16:16:25 -0400729 // Check for fast-path stages.
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500730 auto ct = pm.colorType();
Mike Klein8e3426f2018-04-16 12:56:24 -0400731 if (true
732 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Kleinbb1933e2020-12-02 15:45:29 -0600733 && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear
Mike Reedfae8fce2019-04-03 10:27:45 -0400734 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500735
736 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400737 if (ct == kBGRA_8888_SkColorType) {
738 p->append(SkRasterPipeline::swap_rb);
739 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500740 return append_misc();
741 }
Mike Reed78eedba2019-07-31 16:39:15 -0400742 if (true
Mike Klein01005622019-08-13 12:22:17 -0400743 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Kleinbb1933e2020-12-02 15:45:29 -0600744 && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear
Mike Klein01005622019-08-13 12:22:17 -0400745 && fTileModeX != SkTileMode::kDecal // TODO decal too?
746 && fTileModeY != SkTileMode::kDecal) {
747
748 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
749 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
750 ctx->ct = ct;
751 ctx->tileX = fTileModeX;
752 ctx->tileY = fTileModeY;
753 ctx->invWidth = 1.0f / ctx->width;
754 ctx->invHeight = 1.0f / ctx->height;
755 p->append(SkRasterPipeline::bilinear, ctx);
756 return append_misc();
757 }
758 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400759 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Kleinbb1933e2020-12-02 15:45:29 -0600760 && sampling.useCubic
Mike Reed78eedba2019-07-31 16:39:15 -0400761 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
762
763 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
764 if (ct == kBGRA_8888_SkColorType) {
765 p->append(SkRasterPipeline::swap_rb);
766 }
767 return append_misc();
768 }
Mike Klein01005622019-08-13 12:22:17 -0400769 if (true
770 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Kleinbb1933e2020-12-02 15:45:29 -0600771 && sampling.useCubic
Mike Klein01005622019-08-13 12:22:17 -0400772 && fTileModeX != SkTileMode::kDecal // TODO decal too?
773 && fTileModeY != SkTileMode::kDecal) {
774
775 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
776 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
777 ctx->ct = ct;
778 ctx->tileX = fTileModeX;
779 ctx->tileY = fTileModeY;
780 ctx->invWidth = 1.0f / ctx->width;
781 ctx->invHeight = 1.0f / ctx->height;
782 p->append(SkRasterPipeline::bicubic, ctx);
783 return append_misc();
784 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500785
Mike Reed3d58d5a2020-11-23 13:32:36 -0500786 SkRasterPipeline_SamplerCtx* sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400787
Mike Kleinb0b17d12016-12-09 16:25:44 -0500788 auto sample = [&](SkRasterPipeline::StockStage setup_x,
789 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400790 p->append(setup_x, sampler);
791 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500792 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400793 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500794 };
795
Mike Kleinbb1933e2020-12-02 15:45:29 -0600796 if (sampling.useCubic) {
Mike Klein0a904492017-04-12 12:52:48 -0400797 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500798
799 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
800 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
801 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
802 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
803
804 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
805 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
806 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
807 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
808
809 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
810 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
811 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
812 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
813
814 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
815 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
816 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
817 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
818
Mike Kleinb04c3522016-11-28 11:55:58 -0500819 p->append(SkRasterPipeline::move_dst_src);
Mike Kleinbb1933e2020-12-02 15:45:29 -0600820 } else if (sampling.filter == SkFilterMode::kLinear) {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500821 p->append(SkRasterPipeline::save_xy, sampler);
822
823 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
824 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
825 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
826 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
827
828 p->append(SkRasterPipeline::move_dst_src);
829 } else {
830 append_tiling_and_gather();
Mike Klein06a65e22016-11-17 12:39:09 -0500831 }
832
Mike Klein1fa9c432017-12-11 09:59:47 -0500833 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500834}
Mike Reed9318a6c2019-08-16 16:16:25 -0400835
836bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
837 return this->doStages(rec, nullptr);
838}
839
840SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Brian Osman9aaec362020-05-08 14:54:37 -0400841 bool usePersp = rec.fMatrixProvider.localToDevice().hasPerspective();
Mike Reed8845c372019-12-19 13:22:08 -0500842 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400843 return this->doStages(rec, updater) ? updater : nullptr;
844}
845
Brian Osman5aaaeea2020-06-22 14:26:03 -0400846skvm::Color SkImageShader::onProgram(skvm::Builder* p,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400847 skvm::Coord device, skvm::Coord origLocal, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500848 const SkMatrixProvider& matrices, const SkMatrix* localM,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400849 SkFilterQuality paintQuality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500850 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400851 SkMatrix baseInv;
852 if (!this->computeTotalInverse(matrices.localToDevice(), localM, &baseInv)) {
Mike Reed6352f002020-03-14 23:30:10 -0400853 return {};
Mike Kleinf6a715b2019-12-30 15:24:18 -0600854 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400855 baseInv.normalizePerspective();
Mike Kleinf6a715b2019-12-30 15:24:18 -0600856
Mike Reed3d58d5a2020-11-23 13:32:36 -0500857 auto sampling = fUseSamplingOptions ? fSampling : SkSamplingOptions(paintQuality);
Mike Reedbccdd902020-12-07 12:36:02 -0500858 auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), baseInv, sampling.mipmap);
859 if (!access) {
860 return {};
861 }
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500862 auto [upper, upperInv] = access->level();
863 if (!sampling.useCubic) {
Mike Klein71d420d2021-02-03 09:54:07 -0600864 // TODO: can tweak_sampling sometimes for cubic too when B=0
865 if (matrices.localToDeviceHitsPixelCenters()) {
866 sampling = tweak_sampling(sampling, upperInv);
867 }
868 upperInv = tweak_inv_matrix(sampling.filter, upperInv);
Mike Kleinf6a715b2019-12-30 15:24:18 -0600869 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600870
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500871 SkPixmap lowerPixmap;
872 SkMatrix lowerInv;
873 SkPixmap* lower = nullptr;
874 float lowerWeight = access->lowerWeight();
875 if (lowerWeight > 0) {
876 std::tie(lowerPixmap, lowerInv) = access->lowerLevel();
877 lower = &lowerPixmap;
878 }
879
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400880 skvm::Coord upperLocal = SkShaderBase::ApplyMatrix(p, upperInv, origLocal, uniforms);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600881
Mike Klein921236b2020-02-13 11:20:54 -0600882 // We can exploit image opacity to skip work unpacking alpha channels.
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500883 const bool input_is_opaque = SkAlphaTypeIsOpaque(upper.alphaType())
884 || SkColorTypeIsAlwaysOpaque(upper.colorType());
Mike Klein921236b2020-02-13 11:20:54 -0600885
Mike Klein03d89ef2020-01-14 17:18:29 -0600886 // Each call to sample() will try to rewrite the same uniforms over and over,
887 // so remember where we start and reset back there each time. That way each
888 // sample() call uses the same uniform offsets.
Mike Klein03d89ef2020-01-14 17:18:29 -0600889
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400890 auto compute_clamp_limit = [&](float limit) {
891 // Subtract an ulp so the upper clamp limit excludes limit itself.
892 int bits;
893 memcpy(&bits, &limit, 4);
894 return p->uniformF(uniforms->push(bits-1));
895 };
Mike Klein03d89ef2020-01-14 17:18:29 -0600896
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400897 // Except in the simplest case (no mips, no filtering), we reference uniforms
898 // more than once. To avoid adding/registering them multiple times, we pre-load them
899 // into a struct (just to logically group them together), based on the "current"
900 // pixmap (level of a mipmap).
901 //
902 struct Uniforms {
903 skvm::F32 w, iw, i2w,
904 h, ih, i2h;
905
906 skvm::F32 clamp_w,
907 clamp_h;
908
909 skvm::Uniform addr;
910 skvm::I32 rowBytesAsPixels;
911
Mike Klein03c932c2020-07-13 09:07:42 -0500912 skvm::PixelFormat pixelFormat; // not a uniform, but needed for each texel sample,
913 // so we store it here, since it is also dependent on
914 // the current pixmap (level).
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400915 };
916
917 auto setup_uniforms = [&](const SkPixmap& pm) -> Uniforms {
Mike Klein447f3312021-02-08 09:46:59 -0600918 skvm::PixelFormat pixelFormat = skvm::SkColorType_to_PixelFormat(pm.colorType());
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400919 return {
920 p->uniformF(uniforms->pushF( pm.width())),
921 p->uniformF(uniforms->pushF(1.0f/pm.width())), // iff tileX == kRepeat
922 p->uniformF(uniforms->pushF(0.5f/pm.width())), // iff tileX == kMirror
923
924 p->uniformF(uniforms->pushF( pm.height())),
925 p->uniformF(uniforms->pushF(1.0f/pm.height())), // iff tileY == kRepeat
926 p->uniformF(uniforms->pushF(0.5f/pm.height())), // iff tileY == kMirror
927
928 compute_clamp_limit(pm. width()),
929 compute_clamp_limit(pm.height()),
930
931 uniforms->pushPtr(pm.addr()),
932 p->uniform32(uniforms->push(pm.rowBytesAsPixels())),
933
Mike Klein03c932c2020-07-13 09:07:42 -0500934 pixelFormat,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400935 };
936 };
937
938 auto sample_texel = [&](const Uniforms& u, skvm::F32 sx, skvm::F32 sy) -> skvm::Color {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600939 // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400940 auto repeat = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I) {
Mike Reedf3b9a302020-04-01 13:18:02 -0400941 return v - floor(v * I) * S;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600942 };
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400943 auto mirror = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I2) {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600944 // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale )
945 // {---A---} {------------------B------------------}
Mike Reedf3b9a302020-04-01 13:18:02 -0400946 skvm::F32 A = v - S,
947 B = (S + S) * floor(A * I2);
948 return abs(A - B - S);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600949 };
950 switch (fTileModeX) {
951 case SkTileMode::kDecal: /* handled after gather */ break;
952 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400953 case SkTileMode::kRepeat: sx = repeat(sx, u.w, u.iw); break;
954 case SkTileMode::kMirror: sx = mirror(sx, u.w, u.i2w); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600955 }
956 switch (fTileModeY) {
957 case SkTileMode::kDecal: /* handled after gather */ break;
958 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400959 case SkTileMode::kRepeat: sy = repeat(sy, u.h, u.ih); break;
960 case SkTileMode::kMirror: sy = mirror(sy, u.h, u.i2h); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600961 }
962
963 // Always clamp sample coordinates to [0,width), [0,height), both for memory
964 // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400965 skvm::F32 clamped_x = clamp(sx, 0, u.clamp_w),
966 clamped_y = clamp(sy, 0, u.clamp_h);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600967
968 // Load pixels from pm.addr()[(int)sx + (int)sy*stride].
Mike Reedf3b9a302020-04-01 13:18:02 -0400969 skvm::I32 index = trunc(clamped_x) +
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400970 trunc(clamped_y) * u.rowBytesAsPixels;
Mike Klein03c932c2020-07-13 09:07:42 -0500971 skvm::Color c = gather(u.pixelFormat, u.addr, index);
Mike Klein913a6f52020-03-23 11:06:25 -0500972
Mike Klein921236b2020-02-13 11:20:54 -0600973 // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it.
974 if (input_is_opaque) {
Mike Klein46874ef2020-02-13 10:17:08 -0600975 c.a = p->splat(1.0f);
976 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600977
978 // Mask away any pixels that we tried to sample outside the bounds in kDecal.
979 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
980 skvm::I32 mask = p->splat(~0);
Mike Reedf3b9a302020-04-01 13:18:02 -0400981 if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); }
982 if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); }
Mike Klein5ec9c4e2020-12-01 10:43:46 -0600983 c.r = pun_to_F32(p->bit_and(mask, pun_to_I32(c.r)));
984 c.g = pun_to_F32(p->bit_and(mask, pun_to_I32(c.g)));
985 c.b = pun_to_F32(p->bit_and(mask, pun_to_I32(c.b)));
986 c.a = pun_to_F32(p->bit_and(mask, pun_to_I32(c.a)));
Mike Klein921236b2020-02-13 11:20:54 -0600987 // Notice that even if input_is_opaque, c.a might now be 0.
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600988 }
989
990 return c;
991 };
992
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400993 auto sample_level = [&](const SkPixmap& pm, const SkMatrix& inv, skvm::Coord local) {
994 const Uniforms u = setup_uniforms(pm);
Mike Reed6352f002020-03-14 23:30:10 -0400995
Mike Kleinbb1933e2020-12-02 15:45:29 -0600996 if (sampling.useCubic) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400997 // All bicubic samples have the same fractional offset (fx,fy) from the center.
998 // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center.
999 skvm::F32 fx = fract(local.x + 0.5f),
1000 fy = fract(local.y + 0.5f);
Mike Reed3d30ca62020-07-22 16:55:02 -04001001 skvm::F32 wx[4],
1002 wy[4];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001003
Mike Kleinbb1933e2020-12-02 15:45:29 -06001004 SkM44 weights = CubicResamplerMatrix(sampling.cubic.B, sampling.cubic.C);
Mike Reed3d30ca62020-07-22 16:55:02 -04001005
Mike Kleind3184892020-07-22 17:39:20 -05001006 auto dot = [](const skvm::F32 a[], const skvm::F32 b[]) {
1007 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
1008 };
1009 const skvm::F32 tmpx[] = { p->splat(1.0f), fx, fx*fx, fx*fx*fx };
1010 const skvm::F32 tmpy[] = { p->splat(1.0f), fy, fy*fy, fy*fy*fy };
Mike Reed3d30ca62020-07-22 16:55:02 -04001011
Mike Kleind3184892020-07-22 17:39:20 -05001012 for (int row = 0; row < 4; ++row) {
1013 SkV4 r = weights.row(row);
1014 skvm::F32 ru[] = {
1015 p->uniformF(uniforms->pushF(r[0])),
1016 p->uniformF(uniforms->pushF(r[1])),
1017 p->uniformF(uniforms->pushF(r[2])),
1018 p->uniformF(uniforms->pushF(r[3])),
Mike Reed3d30ca62020-07-22 16:55:02 -04001019 };
Mike Kleind3184892020-07-22 17:39:20 -05001020 wx[row] = dot(ru, tmpx);
1021 wy[row] = dot(ru, tmpy);
Mike Reed3d30ca62020-07-22 16:55:02 -04001022 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001023
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001024 skvm::Color c;
1025 c.r = c.g = c.b = c.a = p->splat(0.0f);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001026
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001027 skvm::F32 sy = local.y - 1.5f;
1028 for (int j = 0; j < 4; j++, sy += 1.0f) {
1029 skvm::F32 sx = local.x - 1.5f;
1030 for (int i = 0; i < 4; i++, sx += 1.0f) {
1031 skvm::Color s = sample_texel(u, sx,sy);
1032 skvm::F32 w = wx[i] * wy[j];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001033
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001034 c.r += s.r * w;
1035 c.g += s.g * w;
1036 c.b += s.b * w;
1037 c.a += s.a * w;
1038 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001039 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001040 return c;
Mike Kleinbb1933e2020-12-02 15:45:29 -06001041 } else if (sampling.filter == SkFilterMode::kLinear) {
Mike Reed3d58d5a2020-11-23 13:32:36 -05001042 // Our four sample points are the corners of a logical 1x1 pixel
1043 // box surrounding (x,y) at (0.5,0.5) off-center.
1044 skvm::F32 left = local.x - 0.5f,
1045 top = local.y - 0.5f,
1046 right = local.x + 0.5f,
1047 bottom = local.y + 0.5f;
1048
1049 // The fractional parts of right and bottom are our lerp factors in x and y respectively.
1050 skvm::F32 fx = fract(right ),
1051 fy = fract(bottom);
1052
1053 return lerp(lerp(sample_texel(u, left,top ), sample_texel(u, right,top ), fx),
1054 lerp(sample_texel(u, left,bottom), sample_texel(u, right,bottom), fx), fy);
1055 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -06001056 SkASSERT(sampling.filter == SkFilterMode::kNearest);
Mike Reed3d58d5a2020-11-23 13:32:36 -05001057 return sample_texel(u, local.x,local.y);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001058 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001059 };
1060
Mike Reed8c1ad7e2020-12-02 20:41:52 -05001061 skvm::Color c = sample_level(upper, upperInv, upperLocal);
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001062 if (lower) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001063 auto lowerLocal = SkShaderBase::ApplyMatrix(p, lowerInv, origLocal, uniforms);
1064 // lower * weight + upper * (1 - weight)
1065 c = lerp(c,
1066 sample_level(*lower, lowerInv, lowerLocal),
1067 p->uniformF(uniforms->pushF(lowerWeight)));
Mike Klein921236b2020-02-13 11:20:54 -06001068 }
Mike Kleind67a5c32020-01-06 16:01:01 -06001069
Mike Klein921236b2020-02-13 11:20:54 -06001070 // If the input is opaque and we're not in decal mode, that means the output is too.
1071 // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas.
1072 if (input_is_opaque
1073 && fTileModeX != SkTileMode::kDecal
1074 && fTileModeY != SkTileMode::kDecal) {
Mike Reed6352f002020-03-14 23:30:10 -04001075 c.a = p->splat(1.0f);
Mike Klein921236b2020-02-13 11:20:54 -06001076 }
1077
Mike Klein913a6f52020-03-23 11:06:25 -05001078 // Alpha-only images get their color from the paint (already converted to dst color space).
Mike Reed8c1ad7e2020-12-02 20:41:52 -05001079 SkColorSpace* cs = upper.colorSpace();
1080 SkAlphaType at = upper.alphaType();
1081 if (SkColorTypeIsAlphaOnly(upper.colorType())) {
Mike Klein913a6f52020-03-23 11:06:25 -05001082 c.r = paint.r;
1083 c.g = paint.g;
1084 c.b = paint.b;
1085
1086 cs = dst.colorSpace();
1087 at = kUnpremul_SkAlphaType;
1088 }
1089
Mike Kleinbb1933e2020-12-02 15:45:29 -06001090 if (sampling.useCubic) {
Mike Klein5c660e02020-01-08 11:36:35 -06001091 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reedf3b9a302020-04-01 13:18:02 -04001092 c.a = clamp01(c.a);
Mike Klein3f83bfd2020-01-07 12:01:50 -06001093
Mike Klein913a6f52020-03-23 11:06:25 -05001094 skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul)
Mike Klein3f83bfd2020-01-07 12:01:50 -06001095 ? p->splat(1.0f)
Mike Reed6352f002020-03-14 23:30:10 -04001096 : c.a;
Mike Reedf3b9a302020-04-01 13:18:02 -04001097 c.r = clamp(c.r, 0.0f, limit);
1098 c.g = clamp(c.g, 0.0f, limit);
1099 c.b = clamp(c.b, 0.0f, limit);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001100 }
Mike Klein4bc86d52020-01-06 18:39:42 -06001101
Mike Kleinef0fa432020-07-29 12:35:45 -05001102 return SkColorSpaceXformSteps{cs,at, dst.colorSpace(),dst.alphaType()}.program(p, uniforms, c);
Mike Kleinf6a715b2019-12-30 15:24:18 -06001103}