blob: 0cc57b201bcb2a24de591f173112d73ef61e86bf [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"
Brian Salomon0c243202020-06-29 14:29:25 -040019#include "src/core/SkScopeExit.h"
Mike Klein8e717442020-01-07 10:22:33 -060020#include "src/core/SkVM.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkWriteBuffer.h"
22#include "src/image/SkImage_Base.h"
23#include "src/shaders/SkBitmapProcShader.h"
24#include "src/shaders/SkEmptyShader.h"
reed856e9d92015-09-30 12:21:45 -070025
Mike Reed3d30ca62020-07-22 16:55:02 -040026SkM44 SkImageShader::CubicResamplerMatrix(float B, float C) {
Mike Reed3867c702020-09-01 13:28:10 -040027#if 0
28 constexpr SkM44 kMitchell = SkM44( 1.f/18.f, -9.f/18.f, 15.f/18.f, -7.f/18.f,
29 16.f/18.f, 0.f/18.f, -36.f/18.f, 21.f/18.f,
30 1.f/18.f, 9.f/18.f, 27.f/18.f, -21.f/18.f,
31 0.f/18.f, 0.f/18.f, -6.f/18.f, 7.f/18.f);
32
33 constexpr SkM44 kCatmull = SkM44(0.0f, -0.5f, 1.0f, -0.5f,
34 1.0f, 0.0f, -2.5f, 1.5f,
35 0.0f, 0.5f, 2.0f, -1.5f,
36 0.0f, 0.0f, -0.5f, 0.5f);
37
38 if (B == 1.0f/3 && C == 1.0f/3) {
39 return kMitchell;
40 }
41 if (B == 0 && C == 0.5f) {
42 return kCatmull;
43 }
44#endif
45 return SkM44( (1.f/6)*B, -(3.f/6)*B - C, (3.f/6)*B + 2*C, - (1.f/6)*B - C,
46 1 - (2.f/6)*B, 0, -3 + (12.f/6)*B + C, 2 - (9.f/6)*B - C,
47 (1.f/6)*B, (3.f/6)*B + C, 3 - (15.f/6)*B - 2*C, -2 + (9.f/6)*B + C,
48 0, 0, -C, (1.f/6)*B + C);
Mike Reed3d30ca62020-07-22 16:55:02 -040049}
50
Mike Reed587d0822017-06-23 16:49:12 -040051/**
52 * We are faster in clamp, so always use that tiling when we can.
53 */
Mike Reedfae8fce2019-04-03 10:27:45 -040054static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040055 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040056#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
57 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
58 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040059 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040060#else
Mike Reedfae8fce2019-04-03 10:27:45 -040061 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040062#endif
Mike Reed587d0822017-06-23 16:49:12 -040063}
64
Mike Klein1f313092018-01-03 10:30:21 -050065SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040066 SkTileMode tmx, SkTileMode tmy,
Mike Reed74c51ac2020-11-20 10:23:58 -050067 const SkSamplingOptions* sampling,
Mike Klein1f313092018-01-03 10:30:21 -050068 const SkMatrix* localMatrix,
69 bool clampAsIfUnpremul)
70 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070071 , fImage(std::move(img))
Mike Reed74c51ac2020-11-20 10:23:58 -050072 , fSampling(sampling ? *sampling : SkSamplingOptions())
Mike Reed587d0822017-06-23 16:49:12 -040073 , fTileModeX(optimize(tmx, fImage->width()))
74 , fTileModeY(optimize(tmy, fImage->height()))
Mike Klein1f313092018-01-03 10:30:21 -050075 , fClampAsIfUnpremul(clampAsIfUnpremul)
Mike Reed74c51ac2020-11-20 10:23:58 -050076 , fUseSamplingOptions(sampling != nullptr)
reed856e9d92015-09-30 12:21:45 -070077{}
78
Mike Reed74c51ac2020-11-20 10:23:58 -050079// just used for legacy-unflattening
80enum class LegacyFilterEnum {
81 kNone,
82 kLow,
83 kMedium,
84 kHigh,
85 // this is the special value for backward compatibility
86 kInheritFromPaint,
87 // this signals we should use the new SkFilterOptions
88 kUseFilterOptions,
Mike Kleinbb1933e2020-12-02 15:45:29 -060089 // use cubic and ignore FilterOptions
Mike Reed74c51ac2020-11-20 10:23:58 -050090 kUseCubicResampler,
Mike Klein1f313092018-01-03 10:30:21 -050091
Mike Reed74c51ac2020-11-20 10:23:58 -050092 kLast = kUseCubicResampler,
93};
94
95sk_sp<SkFlattenable> SkImageShader::PreSamplingCreate(SkReadBuffer& buffer) {
96 SkASSERT(buffer.isVersionLT(SkPicturePriv::kSamplingInImageShader_Version));
97
Mike Reede25b4472019-04-02 17:49:12 -040098 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
99 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Reed9290d012020-06-11 16:56:06 -0400100
Mike Reed74c51ac2020-11-20 10:23:58 -0500101 LegacyFilterEnum fe = LegacyFilterEnum::kInheritFromPaint;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400102 if (!buffer.isVersionLT(SkPicturePriv::kFilterEnumInImageShader_Version)) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500103 fe = buffer.read32LE<LegacyFilterEnum>(LegacyFilterEnum::kLast);
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400104 }
105
Mike Reed15b95d62020-11-06 09:50:47 -0500106 SkSamplingOptions op;
Mike Reed3d30ca62020-07-22 16:55:02 -0400107
108 if (buffer.isVersionLT(SkPicturePriv::kCubicResamplerImageShader_Version)) {
109 if (!buffer.isVersionLT(SkPicturePriv::kFilterOptionsInImageShader_Version)) {
Mike Kleindcc89602020-12-02 15:06:15 -0600110 auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
111 auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
112 op = SkSamplingOptions(filter, mipmap);
Mike Reed3d30ca62020-07-22 16:55:02 -0400113 }
114 } else {
115 switch (fe) {
Mike Kleindcc89602020-12-02 15:06:15 -0600116 case LegacyFilterEnum::kUseFilterOptions: {
117 auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
118 auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
119 op = SkSamplingOptions(filter, mipmap);
120 } break;
121 case LegacyFilterEnum::kUseCubicResampler: {
122 SkScalar B = buffer.readScalar(),
123 C = buffer.readScalar();
124 op = SkSamplingOptions({B,C});
125 } break;
Mike Reed3d30ca62020-07-22 16:55:02 -0400126 default:
127 break;
128 }
Mike Reed9290d012020-06-11 16:56:06 -0400129 }
130
Mike Klein1f313092018-01-03 10:30:21 -0500131 SkMatrix localMatrix;
132 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -0700133 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -0700134 if (!img) {
135 return nullptr;
136 }
Mike Reed9290d012020-06-11 16:56:06 -0400137
Mike Reed3d30ca62020-07-22 16:55:02 -0400138 switch (fe) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500139 case LegacyFilterEnum::kUseFilterOptions:
140 case LegacyFilterEnum::kUseCubicResampler:
141 return SkImageShader::Make(std::move(img), tmx, tmy, &op, &localMatrix);
Mike Reed3d30ca62020-07-22 16:55:02 -0400142 default:
143 break;
144 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500145 return SkImageShader::Make(std::move(img), tmx, tmy, nullptr, &localMatrix);
146}
147
148static void write_sampling(SkWriteBuffer& buffer, SkSamplingOptions sampling) {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600149 buffer.writeBool(sampling.useCubic);
150 if (sampling.useCubic) {
151 buffer.writeScalar(sampling.cubic.B);
152 buffer.writeScalar(sampling.cubic.C);
Mike Reed74c51ac2020-11-20 10:23:58 -0500153 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600154 buffer.writeUInt((unsigned)sampling.filter);
155 buffer.writeUInt((unsigned)sampling.mipmap);
Mike Reed74c51ac2020-11-20 10:23:58 -0500156 }
157}
158
159static SkSamplingOptions read_sampling(SkReadBuffer& buffer) {
Mike Kleindcc89602020-12-02 15:06:15 -0600160 if (buffer.readBool()) {
161 SkScalar B = buffer.readScalar(),
162 C = buffer.readScalar();
163 return SkSamplingOptions({B,C});
Mike Reed74c51ac2020-11-20 10:23:58 -0500164 } else {
Mike Kleindcc89602020-12-02 15:06:15 -0600165 auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
166 auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
167 return SkSamplingOptions(filter, mipmap);
Mike Reed74c51ac2020-11-20 10:23:58 -0500168 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500169}
170
171// fClampAsIfUnpremul is always false when constructed through public APIs,
172// so there's no need to read or write it here.
173
174sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
175 if (buffer.isVersionLT(SkPicturePriv::kSamplingInImageShader_Version)) {
176 return PreSamplingCreate(buffer);
177 }
178
179 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
180 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
181
182 SkSamplingOptions sampling,
183 *samplingPtr = nullptr;
184
185 if (buffer.readBool()) { // fUseSamplingOptions
186 sampling = read_sampling(buffer);
187 samplingPtr = &sampling;
188 }
189
190 SkMatrix localMatrix;
191 buffer.readMatrix(&localMatrix);
192 sk_sp<SkImage> img = buffer.readImage();
193 if (!img) {
194 return nullptr;
195 }
196
197 return SkImageShader::Make(std::move(img), tmx, tmy, samplingPtr, &localMatrix);
reed856e9d92015-09-30 12:21:45 -0700198}
199
200void SkImageShader::flatten(SkWriteBuffer& buffer) const {
Mike Reedfae8fce2019-04-03 10:27:45 -0400201 buffer.writeUInt((unsigned)fTileModeX);
202 buffer.writeUInt((unsigned)fTileModeY);
Mike Reed74c51ac2020-11-20 10:23:58 -0500203
204 buffer.writeBool(fUseSamplingOptions);
205 if (fUseSamplingOptions) {
206 write_sampling(buffer, fSampling);
Mike Reed3d30ca62020-07-22 16:55:02 -0400207 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500208
reed856e9d92015-09-30 12:21:45 -0700209 buffer.writeMatrix(this->getLocalMatrix());
reed6b2d7ac2016-08-11 06:42:26 -0700210 buffer.writeImage(fImage.get());
Mike Klein1f313092018-01-03 10:30:21 -0500211 SkASSERT(fClampAsIfUnpremul == false);
reed856e9d92015-09-30 12:21:45 -0700212}
213
214bool SkImageShader::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -0400215 return fImage->isOpaque() &&
216 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
reed856e9d92015-09-30 12:21:45 -0700217}
218
Mike Reed74c51ac2020-11-20 10:23:58 -0500219constexpr SkCubicResampler kDefaultCubicResampler{1.0f/3, 1.0f/3};
220
221static bool is_default_cubic_resampler(SkCubicResampler cubic) {
222 return SkScalarNearlyEqual(cubic.B, kDefaultCubicResampler.B) &&
223 SkScalarNearlyEqual(cubic.C, kDefaultCubicResampler.C);
224}
225
Mike Reed604e4c22020-11-25 21:08:17 -0500226#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Mike Reed74c51ac2020-11-20 10:23:58 -0500227
Florin Malitaaf2769d2018-04-04 13:46:35 -0400228static bool legacy_shader_can_handle(const SkMatrix& inv) {
Mike Reed81063112020-06-09 17:36:33 -0400229 SkASSERT(!inv.hasPerspective());
Mike Klein37bc8f92019-10-21 13:10:07 -0500230
231 // Scale+translate methods are always present, but affine might not be.
232 if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -0500233 return false;
234 }
235
236 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
237 // out of range.
238 const SkScalar max_dev_coord = 32767.0f;
Mike Klein37bc8f92019-10-21 13:10:07 -0500239 const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord));
Mike Reeda12c4192018-02-01 16:34:03 -0500240
Mike Reed1eb5ca42018-03-08 14:20:52 -0500241 // take 1/4 of max signed 32bits so we have room to subtract local values
Kevin Lubickf76da632020-01-28 10:39:56 -0500242 const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -0500243 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
Mike Klein37bc8f92019-10-21 13:10:07 -0500244 +max_fixed32dot32, +max_fixed32dot32).contains(src)) {
Mike Reeda12c4192018-02-01 16:34:03 -0500245 return false;
246 }
247
248 // legacy shader impl should be able to handle these matrices
249 return true;
250}
251
Florin Malita4aed1382017-05-25 10:38:07 -0400252SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
253 SkArenaAlloc* alloc) const {
Brian Osman0e189372018-10-19 11:58:29 -0400254 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400255 return nullptr;
256 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400257 if (fImage->colorType() != kN32_SkColorType) {
258 return nullptr;
259 }
Florin Malitaaf2769d2018-04-04 13:46:35 -0400260 if (fTileModeX != fTileModeY) {
261 return nullptr;
262 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400263 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400264 return nullptr;
265 }
266
Mike Reedbc4d88a2021-01-21 12:49:36 -0500267 SkSamplingOptions sampling = fUseSamplingOptions ? fSampling
268 : rec.fPaintSampling;
269
270 auto supported = [](const SkSamplingOptions& sampling) {
271 const std::tuple<SkFilterMode,SkMipmapMode> supported[] = {
272 {SkFilterMode::kNearest, SkMipmapMode::kNone}, // legacy kNone_SkFilterQuality
273 {SkFilterMode::kLinear, SkMipmapMode::kNone}, // legacy kLow_SkFilterQuality
274 {SkFilterMode::kLinear, SkMipmapMode::kNearest}, // legacy kMedium_SkFilterQuality
275 };
276 for (auto [f, m] : supported) {
277 if (sampling.filter == f && sampling.mipmap == m) {
278 return true;
279 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500280 }
Mike Reedbc4d88a2021-01-21 12:49:36 -0500281 return false;
282 };
283 if (sampling.useCubic || !supported(sampling)) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500284 return nullptr;
285 }
286
Mike Kleindac694d2018-12-18 10:13:52 -0500287 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500288 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500289 //
Mike Klein67761eb2018-12-18 10:16:53 -0500290 // We back off another bit to 32767 to make small amounts of
291 // intermediate math safe, e.g. in
292 //
293 // SkFixed fx = ...;
294 // fx = tile(fx + SK_Fixed1);
295 //
296 // we want to make sure (fx + SK_Fixed1) never overflows.
297 if (fImage-> width() > 32767 ||
298 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500299 return nullptr;
300 }
301
Florin Malitaaf2769d2018-04-04 13:46:35 -0400302 SkMatrix inv;
303 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
304 !legacy_shader_can_handle(inv)) {
305 return nullptr;
306 }
307
Mike Reed011d1662019-02-28 17:19:25 -0500308 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
309 return nullptr;
310 }
311
Mike Reedbc4d88a2021-01-21 12:49:36 -0500312 // Can remove this once fUseSamplingOptions is always true
Mike Reed9290d012020-06-11 16:56:06 -0400313 ContextRec modifiedRec = rec;
Mike Reedbc4d88a2021-01-21 12:49:36 -0500314 modifiedRec.fPaintSampling = sampling;
315
reed320a40d2016-08-02 06:12:06 -0700316 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed9290d012020-06-11 16:56:06 -0400317 as_IB(fImage.get()), modifiedRec, alloc);
reed856e9d92015-09-30 12:21:45 -0700318}
Mike Reede92aae62018-10-17 10:21:51 -0400319#endif
reed856e9d92015-09-30 12:21:45 -0700320
Mike Reedfae8fce2019-04-03 10:27:45 -0400321SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700322 if (texM) {
323 *texM = this->getLocalMatrix();
324 }
325 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400326 xy[0] = fTileModeX;
327 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700328 }
329 return const_cast<SkImage*>(fImage.get());
330}
331
Mike Klein1f313092018-01-03 10:30:21 -0500332sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400333 SkTileMode tmx, SkTileMode tmy,
Mike Reed74c51ac2020-11-20 10:23:58 -0500334 const SkSamplingOptions* options,
Mike Klein1f313092018-01-03 10:30:21 -0500335 const SkMatrix* localMatrix,
336 bool clampAsIfUnpremul) {
Mike Reed15b95d62020-11-06 09:50:47 -0500337 auto is_unit = [](float x) {
338 return x >= 0 && x <= 1;
339 };
Mike Kleinbb1933e2020-12-02 15:45:29 -0600340 if (options && options->useCubic) {
341 if (!is_unit(options->cubic.B) || !is_unit(options->cubic.C)) {
Mike Reed15b95d62020-11-06 09:50:47 -0500342 return nullptr;
343 }
344 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400345 if (!image) {
346 return sk_make_sp<SkEmptyShader>();
347 }
348 return sk_sp<SkShader>{
Mike Reed74c51ac2020-11-20 10:23:58 -0500349 new SkImageShader(image, tmx, tmy, options, localMatrix, clampAsIfUnpremul)
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400350 };
351}
352
reed856e9d92015-09-30 12:21:45 -0700353///////////////////////////////////////////////////////////////////////////////////////////////////
354
355#if SK_SUPPORT_GPU
356
Robert Phillipsb7bfbc22020-07-01 12:55:01 -0400357#include "include/gpu/GrRecordingContext.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400358#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500359#include "src/gpu/GrCaps.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400360#include "src/gpu/GrColorInfo.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400361#include "src/gpu/GrImageTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500362#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400363#include "src/gpu/GrTextureAdjuster.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500364#include "src/gpu/SkGr.h"
365#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -0400366#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -0500367#include "src/gpu/effects/GrTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700368
Brian Salomonaff329b2017-08-11 09:40:37 -0400369std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500370 const GrFPArgs& args) const {
Florin Malita52f02912020-03-09 16:33:17 -0400371 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700372 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400373 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700374 return nullptr;
375 }
reed856e9d92015-09-30 12:21:45 -0700376
Brian Salomon0c243202020-06-29 14:29:25 -0400377 // This would all be much nicer with std::variant.
378 static constexpr size_t kSize = std::max({sizeof(GrYUVAImageTextureMaker),
379 sizeof(GrTextureAdjuster ),
380 sizeof(GrImageTextureMaker ),
381 sizeof(GrBitmapTextureMaker )});
382 static constexpr size_t kAlign = std::max({alignof(GrYUVAImageTextureMaker),
383 alignof(GrTextureAdjuster ),
384 alignof(GrImageTextureMaker ),
385 alignof(GrBitmapTextureMaker )});
Mike Kleinfb5850f2020-11-09 15:50:37 -0600386 alignas(kAlign) char storage[kSize];
Brian Salomon0c243202020-06-29 14:29:25 -0400387 GrTextureProducer* producer = nullptr;
388 SkScopeExit destroyProducer([&producer]{ if (producer) { producer->~GrTextureProducer(); } });
389
390 uint32_t pinnedUniqueID;
391 SkBitmap bm;
392 if (as_IB(fImage)->isYUVA()) {
393 producer = new (&storage) GrYUVAImageTextureMaker(args.fContext, fImage.get());
394 } else if (GrSurfaceProxyView view =
395 as_IB(fImage)->refPinnedView(args.fContext, &pinnedUniqueID)) {
396 GrColorInfo colorInfo;
397 if (args.fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
398 SkASSERT(fImage->colorType() == kRGBA_8888_SkColorType);
399 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, fImage->alphaType(),
400 fImage->refColorSpace());
401 } else {
402 colorInfo = fImage->imageInfo().colorInfo();
403 }
404 producer = new (&storage)
405 GrTextureAdjuster(args.fContext, std::move(view), colorInfo, pinnedUniqueID);
406 } else if (fImage->isLazyGenerated()) {
407 producer = new (&storage)
408 GrImageTextureMaker(args.fContext, fImage.get(), GrImageTexGenPolicy::kDraw);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400409 } else if (as_IB(fImage)->getROPixels(nullptr, &bm)) {
Brian Salomon0c243202020-06-29 14:29:25 -0400410 producer =
411 new (&storage) GrBitmapTextureMaker(args.fContext, bm, GrImageTexGenPolicy::kDraw);
412 } else {
413 return nullptr;
414 }
Brian Salomon694ec492020-04-14 13:39:31 -0400415 GrSamplerState::WrapMode wmX = SkTileModeToWrapMode(fTileModeX),
416 wmY = SkTileModeToWrapMode(fTileModeY);
Brian Salomon0c243202020-06-29 14:29:25 -0400417 // Must set wrap and filter on the sampler before requesting a texture. In two places
418 // below we check the matrix scale factors to determine how to interpret the filter
419 // quality setting. This completely ignores the complexity of the drawVertices case
420 // where explicit local coords are provided by the caller.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400421 bool sharpen = args.fContext->priv().options().fSharpenMipmappedTextures;
Mike Reed74c51ac2020-11-20 10:23:58 -0500422 GrSamplerState::Filter fm = GrSamplerState::Filter::kNearest;
423 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
Mike Reed52130b02020-12-28 15:33:13 -0500424 bool bicubic = false;
425 SkCubicResampler kernel = kInvalidCubicResampler;
Mike Reed3867c702020-09-01 13:28:10 -0400426
Mike Reed74c51ac2020-11-20 10:23:58 -0500427 if (fUseSamplingOptions) {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600428 bicubic = fSampling.useCubic;
Mike Reed74c51ac2020-11-20 10:23:58 -0500429 if (bicubic) {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600430 kernel = fSampling.cubic;
Mike Reed74c51ac2020-11-20 10:23:58 -0500431 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600432 switch (fSampling.filter) {
Mike Reeda03f8bf2020-11-20 18:45:36 -0500433 case SkFilterMode::kNearest: fm = GrSamplerState::Filter::kNearest; break;
434 case SkFilterMode::kLinear : fm = GrSamplerState::Filter::kLinear ; break;
Mike Reed3867c702020-09-01 13:28:10 -0400435 }
Mike Kleinbb1933e2020-12-02 15:45:29 -0600436 switch (fSampling.mipmap) {
Mike Reed3867c702020-09-01 13:28:10 -0400437 case SkMipmapMode::kNone : mm = GrSamplerState::MipmapMode::kNone ; break;
438 case SkMipmapMode::kNearest: mm = GrSamplerState::MipmapMode::kNearest; break;
439 case SkMipmapMode::kLinear : mm = GrSamplerState::MipmapMode::kLinear ; break;
440 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500441 }
442 } else { // inherit filterquality from paint
Mike Reed52130b02020-12-28 15:33:13 -0500443 std::tie(fm, mm, kernel) =
444 GrInterpretSamplingOptions(fImage->dimensions(),
445 args.fSampling,
Mike Reed74c51ac2020-11-20 10:23:58 -0500446 args.fMatrixProvider.localToDevice(),
447 *lm,
448 sharpen,
449 args.fAllowFilterQualityReduction);
Mike Reed52130b02020-12-28 15:33:13 -0500450 bicubic = GrValidCubicResampler(kernel);
Brian Salomonf7353512020-07-22 19:26:48 -0400451 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400452 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400453 if (bicubic) {
Mike Reed3867c702020-09-01 13:28:10 -0400454 fp = producer->createBicubicFragmentProcessor(lmInverse, nullptr, nullptr, wmX, wmY, kernel);
Brian Salomon0ea33072020-07-14 10:43:42 -0400455 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400456 fp = producer->createFragmentProcessor(lmInverse, nullptr, nullptr, {wmX, wmY, fm, mm});
Brian Salomon0ea33072020-07-14 10:43:42 -0400457 }
Brian Salomon0c243202020-06-29 14:29:25 -0400458 if (!fp) {
reed856e9d92015-09-30 12:21:45 -0700459 return nullptr;
460 }
Brian Osman958a3bb2020-07-30 14:13:23 -0400461 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->colorSpace(), producer->alphaType(),
462 args.fDstColorInfo->colorSpace(), kPremul_SkAlphaType);
Brian Salomonb43d6992021-01-05 14:37:40 -0500463 if (fImage->isAlphaOnly()) {
464 return GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kDstIn);
Brian Salomonc0d79e52019-04-10 15:02:11 -0400465 } else if (args.fInputColorIsOpaque) {
Brian Salomonb43d6992021-01-05 14:37:40 -0500466 // This special case isn't needed for correctness. It just avoids a multiplication by
467 // a vertex attribute alpha that is known to be 1 if we take the kSrcIn path.
Brian Salomon0c243202020-06-29 14:29:25 -0400468 return GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700469 }
Brian Salomonb43d6992021-01-05 14:37:40 -0500470 return GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kSrcIn);
reed856e9d92015-09-30 12:21:45 -0700471}
472
473#endif
reed320a40d2016-08-02 06:12:06 -0700474
475///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500476#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700477
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400478sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400479 SkTileMode tmx, SkTileMode tmy,
Mike Reed172ba9e2020-12-17 15:57:55 -0500480 const SkSamplingOptions& sampling,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400481 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
Mike Reed172ba9e2020-12-17 15:57:55 -0500482 auto s = SkImageShader::Make(SkMakeImageFromRasterBitmap(src, mode),
483 tmx, tmy, &sampling, localMatrix);
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400484 if (!s) {
485 return nullptr;
486 }
487 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
488 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
489 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
490 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400491 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400492 }
493 return s;
494}
495
Brian Salomon23356442018-11-30 15:33:19 -0500496void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700497
Mike Reed9318a6c2019-08-16 16:16:25 -0400498class SkImageStageUpdater : public SkStageUpdater {
499public:
Mike Reed8845c372019-12-19 13:22:08 -0500500 SkImageStageUpdater(const SkImageShader* shader, bool usePersp)
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500501 : fShader(shader)
502 , fUsePersp(usePersp || as_SB(shader)->getLocalMatrix().hasPerspective())
Mike Reed8845c372019-12-19 13:22:08 -0500503 {}
Mike Reed9318a6c2019-08-16 16:16:25 -0400504
Mike Reed8845c372019-12-19 13:22:08 -0500505 const SkImageShader* fShader;
506 const bool fUsePersp; // else use affine
507
508 // large enough for perspective, though often we just use 2x3
509 float fMatrixStorage[9];
Mike Reed9318a6c2019-08-16 16:16:25 -0400510
511#if 0 // TODO: when we support mipmaps
512 SkRasterPipeline_GatherCtx* fGather;
513 SkRasterPipeline_TileCtx* fLimitX;
514 SkRasterPipeline_TileCtx* fLimitY;
515 SkRasterPipeline_DecalTileCtx* fDecal;
516#endif
517
Mike Reed8845c372019-12-19 13:22:08 -0500518 void append_matrix_stage(SkRasterPipeline* p) {
519 if (fUsePersp) {
520 p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage);
521 } else {
522 p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage);
523 }
524 }
525
Mike Reed9318a6c2019-08-16 16:16:25 -0400526 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
527 SkMatrix matrix;
Mike Reed8845c372019-12-19 13:22:08 -0500528 if (fShader->computeTotalInverse(ctm, localM, &matrix)) {
529 if (fUsePersp) {
530 matrix.get9(fMatrixStorage);
531 } else {
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500532 // if we get here, matrix should be affine. If it isn't, then defensively we
533 // won't draw (by returning false), but we should work to never let this
534 // happen (i.e. better preflight by the caller to know ahead of time that we
535 // may encounter perspective, either in the CTM, or in the localM).
536 //
537 // See https://bugs.chromium.org/p/skia/issues/detail?id=10004
538 //
539 if (!matrix.asAffine(fMatrixStorage)) {
540 SkASSERT(false);
541 return false;
542 }
Mike Reed8845c372019-12-19 13:22:08 -0500543 }
544 return true;
545 }
546 return false;
Mike Reed9318a6c2019-08-16 16:16:25 -0400547 }
548};
549
Mike Kleindcc89602020-12-02 15:06:15 -0600550static SkSamplingOptions tweak_filter_and_inv_matrix(SkSamplingOptions sampling, SkMatrix* matrix) {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600551 SkFilterMode filter = sampling.filter;
Mike Kleindcc89602020-12-02 15:06:15 -0600552
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600553 // When the matrix is just an integer translate, bilerp == nearest neighbor.
Mike Kleindcc89602020-12-02 15:06:15 -0600554 if (filter == SkFilterMode::kLinear &&
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600555 matrix->getType() <= SkMatrix::kTranslate_Mask &&
556 matrix->getTranslateX() == (int)matrix->getTranslateX() &&
557 matrix->getTranslateY() == (int)matrix->getTranslateY()) {
Mike Kleindcc89602020-12-02 15:06:15 -0600558 filter = SkFilterMode::kNearest;
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600559 }
560
561 // See skia:4649 and the GM image_scale_aligned.
Mike Kleindcc89602020-12-02 15:06:15 -0600562 if (filter == SkFilterMode::kNearest) {
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600563 if (matrix->getScaleX() >= 0) {
564 matrix->setTranslateX(nextafterf(matrix->getTranslateX(),
565 floorf(matrix->getTranslateX())));
566 }
567 if (matrix->getScaleY() >= 0) {
568 matrix->setTranslateY(nextafterf(matrix->getTranslateY(),
569 floorf(matrix->getTranslateY())));
570 }
571 }
Mike Kleindcc89602020-12-02 15:06:15 -0600572
Mike Kleinbb1933e2020-12-02 15:45:29 -0600573 return SkSamplingOptions(filter, sampling.mipmap);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600574}
575
Mike Reed9318a6c2019-08-16 16:16:25 -0400576bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
Mike Reed39b4c862020-11-25 16:15:53 -0500577 auto sampling = fUseSamplingOptions ? fSampling
578 : SkSamplingOptions(rec.fPaint.getFilterQuality());
579
580 // We only support certain sampling options in stages so far
Mike Kleinbb1933e2020-12-02 15:45:29 -0600581 if (sampling.useCubic) {
582 if (!is_default_cubic_resampler(sampling.cubic)) {
Mike Reed39b4c862020-11-25 16:15:53 -0500583 return false;
Mike Reed74c51ac2020-11-20 10:23:58 -0500584 }
Mike Kleinbb1933e2020-12-02 15:45:29 -0600585 } else if (sampling.mipmap == SkMipmapMode::kLinear) {
Mike Reed39b4c862020-11-25 16:15:53 -0500586 return false;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400587 }
Mike Reed9290d012020-06-11 16:56:06 -0400588
Mike Reed39b4c862020-11-25 16:15:53 -0500589
Mike Kleinbb1933e2020-12-02 15:45:29 -0600590 if (updater && (sampling.mipmap != SkMipmapMode::kNone)) {
Mike Reed8845c372019-12-19 13:22:08 -0500591 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400592 return false;
593 }
594
Mike Reed1d8c42e2017-08-29 14:58:19 -0400595 SkRasterPipeline* p = rec.fPipeline;
596 SkArenaAlloc* alloc = rec.fAlloc;
597
Florin Malita7558e4d2018-02-07 10:05:53 -0500598 SkMatrix matrix;
Brian Osman9aaec362020-05-08 14:54:37 -0400599 if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500600 return false;
601 }
Mike Klein06a65e22016-11-17 12:39:09 -0500602
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500603 if (sampling.useCubic &&
604 SkMatrixPriv::AdjustHighQualityFilterLevel(matrix, true) != kHigh_SkFilterQuality)
605 {
606 sampling = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNearest);
Mike Kleinf447dee2016-11-29 16:37:12 -0500607 }
Mike Reedbccdd902020-12-07 12:36:02 -0500608 auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), matrix, sampling.mipmap);
609 if (!access) {
610 return false;
611 }
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500612 SkPixmap pm;
613 std::tie(pm, matrix) = access->level();
Mike Kleinf447dee2016-11-29 16:37:12 -0500614
Mike Kleine8de0242018-03-10 12:37:11 -0500615 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400616
617 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500618 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400619 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -0600620 if (!sampling.useCubic) {
Mike Kleindcc89602020-12-02 15:06:15 -0600621 sampling = tweak_filter_and_inv_matrix(sampling, &matrix);
Mike Reed3d58d5a2020-11-23 13:32:36 -0500622 }
Mike Reed9318a6c2019-08-16 16:16:25 -0400623 p->append_matrix(alloc, matrix);
624 }
Mike Klein06a65e22016-11-17 12:39:09 -0500625
Mike Kleinb11ab572018-10-24 06:42:14 -0400626 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500627 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400628 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400629 gather->width = pm.width();
630 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400631
Mike Kleinb11ab572018-10-24 06:42:14 -0400632 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
633 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400634 limit_x->scale = pm.width();
635 limit_x->invScale = 1.0f / pm.width();
636 limit_y->scale = pm.height();
637 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400638
Mike Kleinb11ab572018-10-24 06:42:14 -0400639 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400640 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
641 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400642 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500643 decal_ctx->limit_x = limit_x->scale;
644 decal_ctx->limit_y = limit_y->scale;
645 }
646
Mike Reed9318a6c2019-08-16 16:16:25 -0400647#if 0 // TODO: when we support kMedium
648 if (updator && (quality == kMedium_SkFilterQuality)) {
649 // if we change levels in mipmap, we need to update the scales (and invScales)
650 updator->fGather = gather;
651 updator->fLimitX = limit_x;
652 updator->fLimitY = limit_y;
653 updator->fDecal = decal_ctx;
654 }
655#endif
656
Mike Kleinb04c3522016-11-28 11:55:58 -0500657 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500658 if (decal_x_and_y) {
659 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
660 } else {
661 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400662 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
663 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
664 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
665 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500666 }
667 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400668 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
669 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
670 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
671 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500672 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500673 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500674
Mike Kleinac568a92018-01-25 09:09:32 -0500675 void* ctx = gather;
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500676 switch (pm.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500677 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400678 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
679 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500680 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
681 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400682 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
683 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
684 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500685 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
686 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400687 case kR16G16B16A16_unorm_SkColorType:
688 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600689 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500690 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400691 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500692
Mike Kleinb1df5e52018-10-17 17:06:03 -0400693 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
694 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400695
Mike Kleinac568a92018-01-25 09:09:32 -0500696 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
697 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400698
Mike Kleinf7eb0542020-02-11 12:19:08 -0600699 case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
700 p->append(SkRasterPipeline::swap_rb ); break;
701
Mike Kleinac568a92018-01-25 09:09:32 -0500702 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
703 p->append(SkRasterPipeline::force_opaque ); break;
704
Mike Kleinf7eb0542020-02-11 12:19:08 -0600705 case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
706 p->append(SkRasterPipeline::force_opaque );
707 p->append(SkRasterPipeline::swap_rb ); break;
708
Mike Klein1a3eb522018-10-18 10:11:00 -0400709 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
710 p->append(SkRasterPipeline::swap_rb ); break;
711
Mike Kleinb70990e2019-02-28 10:03:27 -0600712 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500713 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500714 if (decal_ctx) {
715 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
716 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500717 };
718
Mike Klein1fa9c432017-12-11 09:59:47 -0500719 auto append_misc = [&] {
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500720 SkColorSpace* cs = pm.colorSpace();
721 SkAlphaType at = pm.alphaType();
Mike Klein6a9f1c42020-01-02 23:23:42 -0600722
723 // Color for A8 images comes from the paint. TODO: all alpha images? none?
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500724 if (pm.colorType() == kAlpha_8_SkColorType) {
Mike Klein6a9f1c42020-01-02 23:23:42 -0600725 SkColor4f rgb = rec.fPaint.getColor4f();
726 p->append_set_rgb(alloc, rgb);
727
Mike Klein6a9f1c42020-01-02 23:23:42 -0600728 cs = sk_srgb_singleton();
729 at = kUnpremul_SkAlphaType;
730 }
731
Mike Klein059e0432020-01-02 16:32:38 -0600732 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Kleinbb1933e2020-12-02 15:45:29 -0600733 if (sampling.useCubic) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500734 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600735 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
736 ? SkRasterPipeline::clamp_1
737 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500738 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000739
Mike Klein059e0432020-01-02 16:32:38 -0600740 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
741 alloc->make<SkColorSpaceXformSteps>(cs, at,
742 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500743 ->apply(p);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000744
Mike Klein1fa9c432017-12-11 09:59:47 -0500745 return true;
746 };
747
Mike Reed9318a6c2019-08-16 16:16:25 -0400748 // Check for fast-path stages.
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500749 auto ct = pm.colorType();
Mike Klein8e3426f2018-04-16 12:56:24 -0400750 if (true
751 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Kleinbb1933e2020-12-02 15:45:29 -0600752 && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear
Mike Reedfae8fce2019-04-03 10:27:45 -0400753 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500754
755 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400756 if (ct == kBGRA_8888_SkColorType) {
757 p->append(SkRasterPipeline::swap_rb);
758 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500759 return append_misc();
760 }
Mike Reed78eedba2019-07-31 16:39:15 -0400761 if (true
Mike Klein01005622019-08-13 12:22:17 -0400762 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Kleinbb1933e2020-12-02 15:45:29 -0600763 && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear
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::bilinear, ctx);
775 return append_misc();
776 }
777 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400778 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Kleinbb1933e2020-12-02 15:45:29 -0600779 && sampling.useCubic
Mike Reed78eedba2019-07-31 16:39:15 -0400780 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
781
782 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
783 if (ct == kBGRA_8888_SkColorType) {
784 p->append(SkRasterPipeline::swap_rb);
785 }
786 return append_misc();
787 }
Mike Klein01005622019-08-13 12:22:17 -0400788 if (true
789 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Kleinbb1933e2020-12-02 15:45:29 -0600790 && sampling.useCubic
Mike Klein01005622019-08-13 12:22:17 -0400791 && fTileModeX != SkTileMode::kDecal // TODO decal too?
792 && fTileModeY != SkTileMode::kDecal) {
793
794 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
795 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
796 ctx->ct = ct;
797 ctx->tileX = fTileModeX;
798 ctx->tileY = fTileModeY;
799 ctx->invWidth = 1.0f / ctx->width;
800 ctx->invHeight = 1.0f / ctx->height;
801 p->append(SkRasterPipeline::bicubic, ctx);
802 return append_misc();
803 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500804
Mike Reed3d58d5a2020-11-23 13:32:36 -0500805 SkRasterPipeline_SamplerCtx* sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400806
Mike Kleinb0b17d12016-12-09 16:25:44 -0500807 auto sample = [&](SkRasterPipeline::StockStage setup_x,
808 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400809 p->append(setup_x, sampler);
810 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500811 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400812 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500813 };
814
Mike Kleinbb1933e2020-12-02 15:45:29 -0600815 if (sampling.useCubic) {
Mike Klein0a904492017-04-12 12:52:48 -0400816 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500817
818 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
819 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
820 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
821 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
822
823 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
824 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
825 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
826 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
827
828 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
829 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
830 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
831 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
832
833 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
834 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
835 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
836 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
837
Mike Kleinb04c3522016-11-28 11:55:58 -0500838 p->append(SkRasterPipeline::move_dst_src);
Mike Kleinbb1933e2020-12-02 15:45:29 -0600839 } else if (sampling.filter == SkFilterMode::kLinear) {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500840 p->append(SkRasterPipeline::save_xy, sampler);
841
842 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
843 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
844 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
845 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
846
847 p->append(SkRasterPipeline::move_dst_src);
848 } else {
849 append_tiling_and_gather();
Mike Klein06a65e22016-11-17 12:39:09 -0500850 }
851
Mike Klein1fa9c432017-12-11 09:59:47 -0500852 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500853}
Mike Reed9318a6c2019-08-16 16:16:25 -0400854
855bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
856 return this->doStages(rec, nullptr);
857}
858
859SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Brian Osman9aaec362020-05-08 14:54:37 -0400860 bool usePersp = rec.fMatrixProvider.localToDevice().hasPerspective();
Mike Reed8845c372019-12-19 13:22:08 -0500861 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400862 return this->doStages(rec, updater) ? updater : nullptr;
863}
864
Brian Osman5aaaeea2020-06-22 14:26:03 -0400865skvm::Color SkImageShader::onProgram(skvm::Builder* p,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400866 skvm::Coord device, skvm::Coord origLocal, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500867 const SkMatrixProvider& matrices, const SkMatrix* localM,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400868 SkFilterQuality paintQuality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500869 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400870 SkMatrix baseInv;
871 if (!this->computeTotalInverse(matrices.localToDevice(), localM, &baseInv)) {
Mike Reed6352f002020-03-14 23:30:10 -0400872 return {};
Mike Kleinf6a715b2019-12-30 15:24:18 -0600873 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400874 baseInv.normalizePerspective();
Mike Kleinf6a715b2019-12-30 15:24:18 -0600875
Mike Reed3d58d5a2020-11-23 13:32:36 -0500876 auto sampling = fUseSamplingOptions ? fSampling : SkSamplingOptions(paintQuality);
Mike Reedbccdd902020-12-07 12:36:02 -0500877 auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), baseInv, sampling.mipmap);
878 if (!access) {
879 return {};
880 }
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500881 auto [upper, upperInv] = access->level();
882 if (!sampling.useCubic) {
Mike Kleindcc89602020-12-02 15:06:15 -0600883 sampling = tweak_filter_and_inv_matrix(sampling, &upperInv);
Mike Kleinf6a715b2019-12-30 15:24:18 -0600884 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600885
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500886 SkPixmap lowerPixmap;
887 SkMatrix lowerInv;
888 SkPixmap* lower = nullptr;
889 float lowerWeight = access->lowerWeight();
890 if (lowerWeight > 0) {
891 std::tie(lowerPixmap, lowerInv) = access->lowerLevel();
892 lower = &lowerPixmap;
893 }
894
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400895 skvm::Coord upperLocal = SkShaderBase::ApplyMatrix(p, upperInv, origLocal, uniforms);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600896
Mike Klein89b3c1f2020-07-29 16:45:05 -0500897 // All existing SkColorTypes pass these checks. We'd only fail here adding new ones.
Mike Klein03c932c2020-07-13 09:07:42 -0500898 skvm::PixelFormat unused;
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500899 if (true && !SkColorType_to_PixelFormat(upper.colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500900 return {};
901 }
Mike Klein9662fd62020-07-16 15:11:27 -0500902 if (lower && !SkColorType_to_PixelFormat(lower->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500903 return {};
Mike Klein25480bf2020-01-06 14:01:58 -0600904 }
905
Mike Klein921236b2020-02-13 11:20:54 -0600906 // We can exploit image opacity to skip work unpacking alpha channels.
Mike Reed8c1ad7e2020-12-02 20:41:52 -0500907 const bool input_is_opaque = SkAlphaTypeIsOpaque(upper.alphaType())
908 || SkColorTypeIsAlwaysOpaque(upper.colorType());
Mike Klein921236b2020-02-13 11:20:54 -0600909
Mike Klein03d89ef2020-01-14 17:18:29 -0600910 // Each call to sample() will try to rewrite the same uniforms over and over,
911 // so remember where we start and reset back there each time. That way each
912 // sample() call uses the same uniform offsets.
Mike Klein03d89ef2020-01-14 17:18:29 -0600913
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400914 auto compute_clamp_limit = [&](float limit) {
915 // Subtract an ulp so the upper clamp limit excludes limit itself.
916 int bits;
917 memcpy(&bits, &limit, 4);
918 return p->uniformF(uniforms->push(bits-1));
919 };
Mike Klein03d89ef2020-01-14 17:18:29 -0600920
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400921 // Except in the simplest case (no mips, no filtering), we reference uniforms
922 // more than once. To avoid adding/registering them multiple times, we pre-load them
923 // into a struct (just to logically group them together), based on the "current"
924 // pixmap (level of a mipmap).
925 //
926 struct Uniforms {
927 skvm::F32 w, iw, i2w,
928 h, ih, i2h;
929
930 skvm::F32 clamp_w,
931 clamp_h;
932
933 skvm::Uniform addr;
934 skvm::I32 rowBytesAsPixels;
935
Mike Klein03c932c2020-07-13 09:07:42 -0500936 skvm::PixelFormat pixelFormat; // not a uniform, but needed for each texel sample,
937 // so we store it here, since it is also dependent on
938 // the current pixmap (level).
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400939 };
940
941 auto setup_uniforms = [&](const SkPixmap& pm) -> Uniforms {
Mike Klein03c932c2020-07-13 09:07:42 -0500942 skvm::PixelFormat pixelFormat;
Mike Klein9662fd62020-07-16 15:11:27 -0500943 SkAssertResult(SkColorType_to_PixelFormat(pm.colorType(), &pixelFormat));
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400944 return {
945 p->uniformF(uniforms->pushF( pm.width())),
946 p->uniformF(uniforms->pushF(1.0f/pm.width())), // iff tileX == kRepeat
947 p->uniformF(uniforms->pushF(0.5f/pm.width())), // iff tileX == kMirror
948
949 p->uniformF(uniforms->pushF( pm.height())),
950 p->uniformF(uniforms->pushF(1.0f/pm.height())), // iff tileY == kRepeat
951 p->uniformF(uniforms->pushF(0.5f/pm.height())), // iff tileY == kMirror
952
953 compute_clamp_limit(pm. width()),
954 compute_clamp_limit(pm.height()),
955
956 uniforms->pushPtr(pm.addr()),
957 p->uniform32(uniforms->push(pm.rowBytesAsPixels())),
958
Mike Klein03c932c2020-07-13 09:07:42 -0500959 pixelFormat,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400960 };
961 };
962
963 auto sample_texel = [&](const Uniforms& u, skvm::F32 sx, skvm::F32 sy) -> skvm::Color {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600964 // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400965 auto repeat = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I) {
Mike Reedf3b9a302020-04-01 13:18:02 -0400966 return v - floor(v * I) * S;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600967 };
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400968 auto mirror = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I2) {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600969 // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale )
970 // {---A---} {------------------B------------------}
Mike Reedf3b9a302020-04-01 13:18:02 -0400971 skvm::F32 A = v - S,
972 B = (S + S) * floor(A * I2);
973 return abs(A - B - S);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600974 };
975 switch (fTileModeX) {
976 case SkTileMode::kDecal: /* handled after gather */ break;
977 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400978 case SkTileMode::kRepeat: sx = repeat(sx, u.w, u.iw); break;
979 case SkTileMode::kMirror: sx = mirror(sx, u.w, u.i2w); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600980 }
981 switch (fTileModeY) {
982 case SkTileMode::kDecal: /* handled after gather */ break;
983 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400984 case SkTileMode::kRepeat: sy = repeat(sy, u.h, u.ih); break;
985 case SkTileMode::kMirror: sy = mirror(sy, u.h, u.i2h); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600986 }
987
988 // Always clamp sample coordinates to [0,width), [0,height), both for memory
989 // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400990 skvm::F32 clamped_x = clamp(sx, 0, u.clamp_w),
991 clamped_y = clamp(sy, 0, u.clamp_h);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600992
993 // Load pixels from pm.addr()[(int)sx + (int)sy*stride].
Mike Reedf3b9a302020-04-01 13:18:02 -0400994 skvm::I32 index = trunc(clamped_x) +
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400995 trunc(clamped_y) * u.rowBytesAsPixels;
Mike Klein03c932c2020-07-13 09:07:42 -0500996 skvm::Color c = gather(u.pixelFormat, u.addr, index);
Mike Klein913a6f52020-03-23 11:06:25 -0500997
Mike Klein921236b2020-02-13 11:20:54 -0600998 // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it.
999 if (input_is_opaque) {
Mike Klein46874ef2020-02-13 10:17:08 -06001000 c.a = p->splat(1.0f);
1001 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001002
1003 // Mask away any pixels that we tried to sample outside the bounds in kDecal.
1004 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
1005 skvm::I32 mask = p->splat(~0);
Mike Reedf3b9a302020-04-01 13:18:02 -04001006 if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); }
1007 if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); }
Mike Klein5ec9c4e2020-12-01 10:43:46 -06001008 c.r = pun_to_F32(p->bit_and(mask, pun_to_I32(c.r)));
1009 c.g = pun_to_F32(p->bit_and(mask, pun_to_I32(c.g)));
1010 c.b = pun_to_F32(p->bit_and(mask, pun_to_I32(c.b)));
1011 c.a = pun_to_F32(p->bit_and(mask, pun_to_I32(c.a)));
Mike Klein921236b2020-02-13 11:20:54 -06001012 // Notice that even if input_is_opaque, c.a might now be 0.
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001013 }
1014
1015 return c;
1016 };
1017
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001018 auto sample_level = [&](const SkPixmap& pm, const SkMatrix& inv, skvm::Coord local) {
1019 const Uniforms u = setup_uniforms(pm);
Mike Reed6352f002020-03-14 23:30:10 -04001020
Mike Kleinbb1933e2020-12-02 15:45:29 -06001021 if (sampling.useCubic) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001022 // All bicubic samples have the same fractional offset (fx,fy) from the center.
1023 // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center.
1024 skvm::F32 fx = fract(local.x + 0.5f),
1025 fy = fract(local.y + 0.5f);
Mike Reed3d30ca62020-07-22 16:55:02 -04001026 skvm::F32 wx[4],
1027 wy[4];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001028
Mike Kleinbb1933e2020-12-02 15:45:29 -06001029 SkM44 weights = CubicResamplerMatrix(sampling.cubic.B, sampling.cubic.C);
Mike Reed3d30ca62020-07-22 16:55:02 -04001030
Mike Kleind3184892020-07-22 17:39:20 -05001031 auto dot = [](const skvm::F32 a[], const skvm::F32 b[]) {
1032 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
1033 };
1034 const skvm::F32 tmpx[] = { p->splat(1.0f), fx, fx*fx, fx*fx*fx };
1035 const skvm::F32 tmpy[] = { p->splat(1.0f), fy, fy*fy, fy*fy*fy };
Mike Reed3d30ca62020-07-22 16:55:02 -04001036
Mike Kleind3184892020-07-22 17:39:20 -05001037 for (int row = 0; row < 4; ++row) {
1038 SkV4 r = weights.row(row);
1039 skvm::F32 ru[] = {
1040 p->uniformF(uniforms->pushF(r[0])),
1041 p->uniformF(uniforms->pushF(r[1])),
1042 p->uniformF(uniforms->pushF(r[2])),
1043 p->uniformF(uniforms->pushF(r[3])),
Mike Reed3d30ca62020-07-22 16:55:02 -04001044 };
Mike Kleind3184892020-07-22 17:39:20 -05001045 wx[row] = dot(ru, tmpx);
1046 wy[row] = dot(ru, tmpy);
Mike Reed3d30ca62020-07-22 16:55:02 -04001047 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001048
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001049 skvm::Color c;
1050 c.r = c.g = c.b = c.a = p->splat(0.0f);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001051
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001052 skvm::F32 sy = local.y - 1.5f;
1053 for (int j = 0; j < 4; j++, sy += 1.0f) {
1054 skvm::F32 sx = local.x - 1.5f;
1055 for (int i = 0; i < 4; i++, sx += 1.0f) {
1056 skvm::Color s = sample_texel(u, sx,sy);
1057 skvm::F32 w = wx[i] * wy[j];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001058
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001059 c.r += s.r * w;
1060 c.g += s.g * w;
1061 c.b += s.b * w;
1062 c.a += s.a * w;
1063 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001064 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001065 return c;
Mike Kleinbb1933e2020-12-02 15:45:29 -06001066 } else if (sampling.filter == SkFilterMode::kLinear) {
Mike Reed3d58d5a2020-11-23 13:32:36 -05001067 // Our four sample points are the corners of a logical 1x1 pixel
1068 // box surrounding (x,y) at (0.5,0.5) off-center.
1069 skvm::F32 left = local.x - 0.5f,
1070 top = local.y - 0.5f,
1071 right = local.x + 0.5f,
1072 bottom = local.y + 0.5f;
1073
1074 // The fractional parts of right and bottom are our lerp factors in x and y respectively.
1075 skvm::F32 fx = fract(right ),
1076 fy = fract(bottom);
1077
1078 return lerp(lerp(sample_texel(u, left,top ), sample_texel(u, right,top ), fx),
1079 lerp(sample_texel(u, left,bottom), sample_texel(u, right,bottom), fx), fy);
1080 } else {
Mike Kleinbb1933e2020-12-02 15:45:29 -06001081 SkASSERT(sampling.filter == SkFilterMode::kNearest);
Mike Reed3d58d5a2020-11-23 13:32:36 -05001082 return sample_texel(u, local.x,local.y);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001083 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001084 };
1085
Mike Reed8c1ad7e2020-12-02 20:41:52 -05001086 skvm::Color c = sample_level(upper, upperInv, upperLocal);
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001087 if (lower) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001088 auto lowerLocal = SkShaderBase::ApplyMatrix(p, lowerInv, origLocal, uniforms);
1089 // lower * weight + upper * (1 - weight)
1090 c = lerp(c,
1091 sample_level(*lower, lowerInv, lowerLocal),
1092 p->uniformF(uniforms->pushF(lowerWeight)));
Mike Klein921236b2020-02-13 11:20:54 -06001093 }
Mike Kleind67a5c32020-01-06 16:01:01 -06001094
Mike Klein921236b2020-02-13 11:20:54 -06001095 // If the input is opaque and we're not in decal mode, that means the output is too.
1096 // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas.
1097 if (input_is_opaque
1098 && fTileModeX != SkTileMode::kDecal
1099 && fTileModeY != SkTileMode::kDecal) {
Mike Reed6352f002020-03-14 23:30:10 -04001100 c.a = p->splat(1.0f);
Mike Klein921236b2020-02-13 11:20:54 -06001101 }
1102
Mike Klein913a6f52020-03-23 11:06:25 -05001103 // Alpha-only images get their color from the paint (already converted to dst color space).
Mike Reed8c1ad7e2020-12-02 20:41:52 -05001104 SkColorSpace* cs = upper.colorSpace();
1105 SkAlphaType at = upper.alphaType();
1106 if (SkColorTypeIsAlphaOnly(upper.colorType())) {
Mike Klein913a6f52020-03-23 11:06:25 -05001107 c.r = paint.r;
1108 c.g = paint.g;
1109 c.b = paint.b;
1110
1111 cs = dst.colorSpace();
1112 at = kUnpremul_SkAlphaType;
1113 }
1114
Mike Kleinbb1933e2020-12-02 15:45:29 -06001115 if (sampling.useCubic) {
Mike Klein5c660e02020-01-08 11:36:35 -06001116 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reedf3b9a302020-04-01 13:18:02 -04001117 c.a = clamp01(c.a);
Mike Klein3f83bfd2020-01-07 12:01:50 -06001118
Mike Klein913a6f52020-03-23 11:06:25 -05001119 skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul)
Mike Klein3f83bfd2020-01-07 12:01:50 -06001120 ? p->splat(1.0f)
Mike Reed6352f002020-03-14 23:30:10 -04001121 : c.a;
Mike Reedf3b9a302020-04-01 13:18:02 -04001122 c.r = clamp(c.r, 0.0f, limit);
1123 c.g = clamp(c.g, 0.0f, limit);
1124 c.b = clamp(c.b, 0.0f, limit);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001125 }
Mike Klein4bc86d52020-01-06 18:39:42 -06001126
Mike Kleinef0fa432020-07-29 12:35:45 -05001127 return SkColorSpaceXformSteps{cs,at, dst.colorSpace(),dst.alphaType()}.program(p, uniforms, c);
Mike Kleinf6a715b2019-12-30 15:24:18 -06001128}