blob: fb35cbc4ab6ccb868cfe7096acca0b7cbc83259d [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/SkBitmapController.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkColorSpacePriv.h"
13#include "src/core/SkColorSpaceXformSteps.h"
Brian Osman449b1152020-04-15 16:43:00 -040014#include "src/core/SkMatrixProvider.h"
Mike Klein37bc8f92019-10-21 13:10:07 -050015#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkRasterPipeline.h"
17#include "src/core/SkReadBuffer.h"
Brian Salomon0c243202020-06-29 14:29:25 -040018#include "src/core/SkScopeExit.h"
Mike Klein8e717442020-01-07 10:22:33 -060019#include "src/core/SkVM.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkWriteBuffer.h"
21#include "src/image/SkImage_Base.h"
22#include "src/shaders/SkBitmapProcShader.h"
23#include "src/shaders/SkEmptyShader.h"
reed856e9d92015-09-30 12:21:45 -070024
Mike Reed3d30ca62020-07-22 16:55:02 -040025SkM44 SkImageShader::CubicResamplerMatrix(float B, float C) {
Mike Reed3867c702020-09-01 13:28:10 -040026#if 0
27 constexpr SkM44 kMitchell = SkM44( 1.f/18.f, -9.f/18.f, 15.f/18.f, -7.f/18.f,
28 16.f/18.f, 0.f/18.f, -36.f/18.f, 21.f/18.f,
29 1.f/18.f, 9.f/18.f, 27.f/18.f, -21.f/18.f,
30 0.f/18.f, 0.f/18.f, -6.f/18.f, 7.f/18.f);
31
32 constexpr SkM44 kCatmull = SkM44(0.0f, -0.5f, 1.0f, -0.5f,
33 1.0f, 0.0f, -2.5f, 1.5f,
34 0.0f, 0.5f, 2.0f, -1.5f,
35 0.0f, 0.0f, -0.5f, 0.5f);
36
37 if (B == 1.0f/3 && C == 1.0f/3) {
38 return kMitchell;
39 }
40 if (B == 0 && C == 0.5f) {
41 return kCatmull;
42 }
43#endif
44 return SkM44( (1.f/6)*B, -(3.f/6)*B - C, (3.f/6)*B + 2*C, - (1.f/6)*B - C,
45 1 - (2.f/6)*B, 0, -3 + (12.f/6)*B + C, 2 - (9.f/6)*B - C,
46 (1.f/6)*B, (3.f/6)*B + C, 3 - (15.f/6)*B - 2*C, -2 + (9.f/6)*B + C,
47 0, 0, -C, (1.f/6)*B + C);
Mike Reed3d30ca62020-07-22 16:55:02 -040048}
49
Mike Reed587d0822017-06-23 16:49:12 -040050/**
51 * We are faster in clamp, so always use that tiling when we can.
52 */
Mike Reedfae8fce2019-04-03 10:27:45 -040053static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040054 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040055#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
56 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
57 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040058 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040059#else
Mike Reedfae8fce2019-04-03 10:27:45 -040060 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040061#endif
Mike Reed587d0822017-06-23 16:49:12 -040062}
63
Mike Klein1f313092018-01-03 10:30:21 -050064SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040065 SkTileMode tmx, SkTileMode tmy,
Mike Reed74c51ac2020-11-20 10:23:58 -050066 const SkSamplingOptions* sampling,
Mike Klein1f313092018-01-03 10:30:21 -050067 const SkMatrix* localMatrix,
68 bool clampAsIfUnpremul)
69 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070070 , fImage(std::move(img))
Mike Reed74c51ac2020-11-20 10:23:58 -050071 , fSampling(sampling ? *sampling : SkSamplingOptions())
Mike Reed587d0822017-06-23 16:49:12 -040072 , fTileModeX(optimize(tmx, fImage->width()))
73 , fTileModeY(optimize(tmy, fImage->height()))
Mike Klein1f313092018-01-03 10:30:21 -050074 , fClampAsIfUnpremul(clampAsIfUnpremul)
Mike Reed74c51ac2020-11-20 10:23:58 -050075 , fUseSamplingOptions(sampling != nullptr)
reed856e9d92015-09-30 12:21:45 -070076{}
77
Mike Reed74c51ac2020-11-20 10:23:58 -050078// just used for legacy-unflattening
79enum class LegacyFilterEnum {
80 kNone,
81 kLow,
82 kMedium,
83 kHigh,
84 // this is the special value for backward compatibility
85 kInheritFromPaint,
86 // this signals we should use the new SkFilterOptions
87 kUseFilterOptions,
88 // use fCubic and ignore FilterOptions
89 kUseCubicResampler,
Mike Klein1f313092018-01-03 10:30:21 -050090
Mike Reed74c51ac2020-11-20 10:23:58 -050091 kLast = kUseCubicResampler,
92};
93
94sk_sp<SkFlattenable> SkImageShader::PreSamplingCreate(SkReadBuffer& buffer) {
95 SkASSERT(buffer.isVersionLT(SkPicturePriv::kSamplingInImageShader_Version));
96
Mike Reede25b4472019-04-02 17:49:12 -040097 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
98 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Reed9290d012020-06-11 16:56:06 -040099
Mike Reed74c51ac2020-11-20 10:23:58 -0500100 LegacyFilterEnum fe = LegacyFilterEnum::kInheritFromPaint;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400101 if (!buffer.isVersionLT(SkPicturePriv::kFilterEnumInImageShader_Version)) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500102 fe = buffer.read32LE<LegacyFilterEnum>(LegacyFilterEnum::kLast);
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400103 }
104
Mike Reed15b95d62020-11-06 09:50:47 -0500105 SkSamplingOptions op;
Mike Reed3d30ca62020-07-22 16:55:02 -0400106
107 if (buffer.isVersionLT(SkPicturePriv::kCubicResamplerImageShader_Version)) {
108 if (!buffer.isVersionLT(SkPicturePriv::kFilterOptionsInImageShader_Version)) {
Mike Reed15b95d62020-11-06 09:50:47 -0500109 op.fUseCubic = false;
Mike Reeda03f8bf2020-11-20 18:45:36 -0500110 op.fFilter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
111 op.fMipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
Mike Reed3d30ca62020-07-22 16:55:02 -0400112 }
113 } else {
114 switch (fe) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500115 case LegacyFilterEnum::kUseFilterOptions:
Mike Reed15b95d62020-11-06 09:50:47 -0500116 op.fUseCubic = false;
Mike Reeda03f8bf2020-11-20 18:45:36 -0500117 op.fFilter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
118 op.fMipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
Mike Reed3d30ca62020-07-22 16:55:02 -0400119 break;
Mike Reed74c51ac2020-11-20 10:23:58 -0500120 case LegacyFilterEnum::kUseCubicResampler:
Mike Reed15b95d62020-11-06 09:50:47 -0500121 op.fUseCubic = true;
122 op.fCubic.B = buffer.readScalar();
123 op.fCubic.C = buffer.readScalar();
Mike Reed3d30ca62020-07-22 16:55:02 -0400124 break;
125 default:
126 break;
127 }
Mike Reed9290d012020-06-11 16:56:06 -0400128 }
129
Mike Klein1f313092018-01-03 10:30:21 -0500130 SkMatrix localMatrix;
131 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -0700132 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -0700133 if (!img) {
134 return nullptr;
135 }
Mike Reed9290d012020-06-11 16:56:06 -0400136
Mike Reed3d30ca62020-07-22 16:55:02 -0400137 switch (fe) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500138 case LegacyFilterEnum::kUseFilterOptions:
139 case LegacyFilterEnum::kUseCubicResampler:
140 return SkImageShader::Make(std::move(img), tmx, tmy, &op, &localMatrix);
Mike Reed3d30ca62020-07-22 16:55:02 -0400141 default:
142 break;
143 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500144 return SkImageShader::Make(std::move(img), tmx, tmy, nullptr, &localMatrix);
145}
146
147static void write_sampling(SkWriteBuffer& buffer, SkSamplingOptions sampling) {
148 buffer.writeBool(sampling.fUseCubic);
149 if (sampling.fUseCubic) {
150 buffer.writeScalar(sampling.fCubic.B);
151 buffer.writeScalar(sampling.fCubic.C);
152 } else {
Mike Reeda03f8bf2020-11-20 18:45:36 -0500153 buffer.writeUInt((unsigned)sampling.fFilter);
154 buffer.writeUInt((unsigned)sampling.fMipmap);
Mike Reed74c51ac2020-11-20 10:23:58 -0500155 }
156}
157
158static SkSamplingOptions read_sampling(SkReadBuffer& buffer) {
159 SkSamplingOptions sampling;
160 sampling.fUseCubic = buffer.readBool();
161 if (sampling.fUseCubic) {
162 sampling.fCubic.B = buffer.readScalar();
163 sampling.fCubic.C = buffer.readScalar();
164 } else {
Mike Reeda03f8bf2020-11-20 18:45:36 -0500165 sampling.fFilter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear);
166 sampling.fMipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
Mike Reed74c51ac2020-11-20 10:23:58 -0500167 }
168 return sampling;
169}
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
226static bool sampling_to_quality(SkSamplingOptions sampling, SkFilterQuality* quality) {
227 int q = -1; // not a legal quality enum
228
229 if (sampling.fUseCubic) {
230 if (is_default_cubic_resampler(sampling.fCubic)) {
231 q = kHigh_SkFilterQuality;
232 }
233 } else {
Mike Reeda03f8bf2020-11-20 18:45:36 -0500234 switch (sampling.fMipmap) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500235 case SkMipmapMode::kNone:
Mike Reeda03f8bf2020-11-20 18:45:36 -0500236 q = sampling.fFilter == SkFilterMode::kLinear ?
Mike Reed74c51ac2020-11-20 10:23:58 -0500237 kLow_SkFilterQuality :
238 kNone_SkFilterQuality;
239 break;
240 case SkMipmapMode::kNearest:
Mike Reeda03f8bf2020-11-20 18:45:36 -0500241 if (sampling.fFilter == SkFilterMode::kLinear) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500242 q = kMedium_SkFilterQuality;
243 }
244 break;
245 case SkMipmapMode::kLinear:
246 break;
247 }
248 }
249 if (q >= 0) {
250 *quality = (SkFilterQuality)q;
251 return true;
252 }
253 return false;
254}
255
Mike Reede92aae62018-10-17 10:21:51 -0400256#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malitaaf2769d2018-04-04 13:46:35 -0400257static bool legacy_shader_can_handle(const SkMatrix& inv) {
Mike Reed81063112020-06-09 17:36:33 -0400258 SkASSERT(!inv.hasPerspective());
Mike Klein37bc8f92019-10-21 13:10:07 -0500259
260 // Scale+translate methods are always present, but affine might not be.
261 if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -0500262 return false;
263 }
264
265 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
266 // out of range.
267 const SkScalar max_dev_coord = 32767.0f;
Mike Klein37bc8f92019-10-21 13:10:07 -0500268 const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord));
Mike Reeda12c4192018-02-01 16:34:03 -0500269
Mike Reed1eb5ca42018-03-08 14:20:52 -0500270 // take 1/4 of max signed 32bits so we have room to subtract local values
Kevin Lubickf76da632020-01-28 10:39:56 -0500271 const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -0500272 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
Mike Klein37bc8f92019-10-21 13:10:07 -0500273 +max_fixed32dot32, +max_fixed32dot32).contains(src)) {
Mike Reeda12c4192018-02-01 16:34:03 -0500274 return false;
275 }
276
277 // legacy shader impl should be able to handle these matrices
278 return true;
279}
280
Florin Malita4aed1382017-05-25 10:38:07 -0400281SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
282 SkArenaAlloc* alloc) const {
Brian Osman0e189372018-10-19 11:58:29 -0400283 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400284 return nullptr;
285 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400286 if (fImage->colorType() != kN32_SkColorType) {
287 return nullptr;
288 }
Florin Malitaaf2769d2018-04-04 13:46:35 -0400289 if (fTileModeX != fTileModeY) {
290 return nullptr;
291 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400292 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400293 return nullptr;
294 }
295
Mike Reed74c51ac2020-11-20 10:23:58 -0500296 SkFilterQuality quality = rec.fPaint->getFilterQuality();
297 if (fUseSamplingOptions) {
298 // we turn our sampling backwards into a quality (if possible)
299 // Note: if/when we can retool the legacy shader to explicitly take SkFilterOptions
300 // we can skip this funny step.
301 if (!sampling_to_quality(fSampling, &quality)) {
302 return nullptr;
303 }
304 }
305 if (quality == kHigh_SkFilterQuality) {
306 return nullptr;
307 }
308
Mike Kleindac694d2018-12-18 10:13:52 -0500309 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500310 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500311 //
Mike Klein67761eb2018-12-18 10:16:53 -0500312 // We back off another bit to 32767 to make small amounts of
313 // intermediate math safe, e.g. in
314 //
315 // SkFixed fx = ...;
316 // fx = tile(fx + SK_Fixed1);
317 //
318 // we want to make sure (fx + SK_Fixed1) never overflows.
319 if (fImage-> width() > 32767 ||
320 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500321 return nullptr;
322 }
323
Florin Malitaaf2769d2018-04-04 13:46:35 -0400324 SkMatrix inv;
325 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
326 !legacy_shader_can_handle(inv)) {
327 return nullptr;
328 }
329
Mike Reed011d1662019-02-28 17:19:25 -0500330 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
331 return nullptr;
332 }
333
Mike Reed9290d012020-06-11 16:56:06 -0400334 // Send in a modified paint with different filter-quality if we don't agree with the paint
335 SkPaint modifiedPaint;
336 ContextRec modifiedRec = rec;
337 if (quality != rec.fPaint->getFilterQuality()) {
338 modifiedPaint = *rec.fPaint;
339 modifiedPaint.setFilterQuality(quality);
340 modifiedRec.fPaint = &modifiedPaint;
341 }
reed320a40d2016-08-02 06:12:06 -0700342 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed9290d012020-06-11 16:56:06 -0400343 as_IB(fImage.get()), modifiedRec, alloc);
reed856e9d92015-09-30 12:21:45 -0700344}
Mike Reede92aae62018-10-17 10:21:51 -0400345#endif
reed856e9d92015-09-30 12:21:45 -0700346
Mike Reedfae8fce2019-04-03 10:27:45 -0400347SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700348 if (texM) {
349 *texM = this->getLocalMatrix();
350 }
351 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400352 xy[0] = fTileModeX;
353 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700354 }
355 return const_cast<SkImage*>(fImage.get());
356}
357
Mike Klein1f313092018-01-03 10:30:21 -0500358sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400359 SkTileMode tmx, SkTileMode tmy,
Mike Reed74c51ac2020-11-20 10:23:58 -0500360 const SkSamplingOptions* options,
Mike Klein1f313092018-01-03 10:30:21 -0500361 const SkMatrix* localMatrix,
362 bool clampAsIfUnpremul) {
Mike Reed15b95d62020-11-06 09:50:47 -0500363 auto is_unit = [](float x) {
364 return x >= 0 && x <= 1;
365 };
Mike Reed74c51ac2020-11-20 10:23:58 -0500366 if (options && options->fUseCubic) {
367 if (!is_unit(options->fCubic.B) || !is_unit(options->fCubic.C)) {
Mike Reed15b95d62020-11-06 09:50:47 -0500368 return nullptr;
369 }
370 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400371 if (!image) {
372 return sk_make_sp<SkEmptyShader>();
373 }
374 return sk_sp<SkShader>{
Mike Reed74c51ac2020-11-20 10:23:58 -0500375 new SkImageShader(image, tmx, tmy, options, localMatrix, clampAsIfUnpremul)
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400376 };
377}
378
reed856e9d92015-09-30 12:21:45 -0700379///////////////////////////////////////////////////////////////////////////////////////////////////
380
381#if SK_SUPPORT_GPU
382
Robert Phillipsb7bfbc22020-07-01 12:55:01 -0400383#include "include/gpu/GrRecordingContext.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400384#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500385#include "src/gpu/GrCaps.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400386#include "src/gpu/GrColorInfo.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400387#include "src/gpu/GrImageTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500388#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400389#include "src/gpu/GrTextureAdjuster.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500390#include "src/gpu/SkGr.h"
391#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -0400392#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -0500393#include "src/gpu/effects/GrTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700394
Brian Salomonaff329b2017-08-11 09:40:37 -0400395std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500396 const GrFPArgs& args) const {
Florin Malita52f02912020-03-09 16:33:17 -0400397 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700398 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400399 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700400 return nullptr;
401 }
reed856e9d92015-09-30 12:21:45 -0700402
Brian Salomon0c243202020-06-29 14:29:25 -0400403 // This would all be much nicer with std::variant.
404 static constexpr size_t kSize = std::max({sizeof(GrYUVAImageTextureMaker),
405 sizeof(GrTextureAdjuster ),
406 sizeof(GrImageTextureMaker ),
407 sizeof(GrBitmapTextureMaker )});
408 static constexpr size_t kAlign = std::max({alignof(GrYUVAImageTextureMaker),
409 alignof(GrTextureAdjuster ),
410 alignof(GrImageTextureMaker ),
411 alignof(GrBitmapTextureMaker )});
Mike Kleinfb5850f2020-11-09 15:50:37 -0600412 alignas(kAlign) char storage[kSize];
Brian Salomon0c243202020-06-29 14:29:25 -0400413 GrTextureProducer* producer = nullptr;
414 SkScopeExit destroyProducer([&producer]{ if (producer) { producer->~GrTextureProducer(); } });
415
416 uint32_t pinnedUniqueID;
417 SkBitmap bm;
418 if (as_IB(fImage)->isYUVA()) {
419 producer = new (&storage) GrYUVAImageTextureMaker(args.fContext, fImage.get());
420 } else if (GrSurfaceProxyView view =
421 as_IB(fImage)->refPinnedView(args.fContext, &pinnedUniqueID)) {
422 GrColorInfo colorInfo;
423 if (args.fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
424 SkASSERT(fImage->colorType() == kRGBA_8888_SkColorType);
425 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, fImage->alphaType(),
426 fImage->refColorSpace());
427 } else {
428 colorInfo = fImage->imageInfo().colorInfo();
429 }
430 producer = new (&storage)
431 GrTextureAdjuster(args.fContext, std::move(view), colorInfo, pinnedUniqueID);
432 } else if (fImage->isLazyGenerated()) {
433 producer = new (&storage)
434 GrImageTextureMaker(args.fContext, fImage.get(), GrImageTexGenPolicy::kDraw);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400435 } else if (as_IB(fImage)->getROPixels(nullptr, &bm)) {
Brian Salomon0c243202020-06-29 14:29:25 -0400436 producer =
437 new (&storage) GrBitmapTextureMaker(args.fContext, bm, GrImageTexGenPolicy::kDraw);
438 } else {
439 return nullptr;
440 }
Brian Salomon694ec492020-04-14 13:39:31 -0400441 GrSamplerState::WrapMode wmX = SkTileModeToWrapMode(fTileModeX),
442 wmY = SkTileModeToWrapMode(fTileModeY);
Brian Salomon0c243202020-06-29 14:29:25 -0400443 // Must set wrap and filter on the sampler before requesting a texture. In two places
444 // below we check the matrix scale factors to determine how to interpret the filter
445 // quality setting. This completely ignores the complexity of the drawVertices case
446 // where explicit local coords are provided by the caller.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400447 bool sharpen = args.fContext->priv().options().fSharpenMipmappedTextures;
Mike Reed74c51ac2020-11-20 10:23:58 -0500448 GrSamplerState::Filter fm = GrSamplerState::Filter::kNearest;
449 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
Brian Salomonf7353512020-07-22 19:26:48 -0400450 bool bicubic;
Mike Reed15b95d62020-11-06 09:50:47 -0500451 SkCubicResampler kernel = GrBicubicEffect::gMitchell;
Mike Reed3867c702020-09-01 13:28:10 -0400452
Mike Reed74c51ac2020-11-20 10:23:58 -0500453 if (fUseSamplingOptions) {
454 bicubic = fSampling.fUseCubic;
455 if (bicubic) {
456 kernel = fSampling.fCubic;
457 } else {
Mike Reeda03f8bf2020-11-20 18:45:36 -0500458 switch (fSampling.fFilter) {
459 case SkFilterMode::kNearest: fm = GrSamplerState::Filter::kNearest; break;
460 case SkFilterMode::kLinear : fm = GrSamplerState::Filter::kLinear ; break;
Mike Reed3867c702020-09-01 13:28:10 -0400461 }
Mike Reeda03f8bf2020-11-20 18:45:36 -0500462 switch (fSampling.fMipmap) {
Mike Reed3867c702020-09-01 13:28:10 -0400463 case SkMipmapMode::kNone : mm = GrSamplerState::MipmapMode::kNone ; break;
464 case SkMipmapMode::kNearest: mm = GrSamplerState::MipmapMode::kNearest; break;
465 case SkMipmapMode::kLinear : mm = GrSamplerState::MipmapMode::kLinear ; break;
466 }
Mike Reed74c51ac2020-11-20 10:23:58 -0500467 }
468 } else { // inherit filterquality from paint
469 std::tie(fm, mm, bicubic) =
470 GrInterpretFilterQuality(fImage->dimensions(),
471 args.fFilterQuality,
472 args.fMatrixProvider.localToDevice(),
473 *lm,
474 sharpen,
475 args.fAllowFilterQualityReduction);
Brian Salomonf7353512020-07-22 19:26:48 -0400476 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400477 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400478 if (bicubic) {
Mike Reed3867c702020-09-01 13:28:10 -0400479 fp = producer->createBicubicFragmentProcessor(lmInverse, nullptr, nullptr, wmX, wmY, kernel);
Brian Salomon0ea33072020-07-14 10:43:42 -0400480 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400481 fp = producer->createFragmentProcessor(lmInverse, nullptr, nullptr, {wmX, wmY, fm, mm});
Brian Salomon0ea33072020-07-14 10:43:42 -0400482 }
Brian Salomon0c243202020-06-29 14:29:25 -0400483 if (!fp) {
reed856e9d92015-09-30 12:21:45 -0700484 return nullptr;
485 }
Brian Osman958a3bb2020-07-30 14:13:23 -0400486 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->colorSpace(), producer->alphaType(),
487 args.fDstColorInfo->colorSpace(), kPremul_SkAlphaType);
488 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Greg Danielc594e622019-10-15 14:01:49 -0400489 bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType());
Robert Phillipsb726d582017-03-09 16:36:32 -0500490 if (isAlphaOnly) {
Brian Salomon0c243202020-06-29 14:29:25 -0400491 return fp;
Brian Salomonc0d79e52019-04-10 15:02:11 -0400492 } else if (args.fInputColorIsOpaque) {
Brian Salomon0c243202020-06-29 14:29:25 -0400493 return GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700494 }
Brian Salomon0c243202020-06-29 14:29:25 -0400495 return GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
reed856e9d92015-09-30 12:21:45 -0700496}
497
498#endif
reed320a40d2016-08-02 06:12:06 -0700499
500///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500501#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700502
Mike Reede25b4472019-04-02 17:49:12 -0400503sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy,
504 const SkMatrix* localMatrix, SkCopyPixelsMode cpm) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500505 const SkSamplingOptions* inherit_from_paint = nullptr;
Herb Derbybfdc87a2017-02-14 15:06:23 +0000506 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm),
Mike Reed74c51ac2020-11-20 10:23:58 -0500507 tmx, tmy, inherit_from_paint, localMatrix);
reed320a40d2016-08-02 06:12:06 -0700508}
509
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400510sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400511 SkTileMode tmx, SkTileMode tmy,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400512 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
513 auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode);
514 if (!s) {
515 return nullptr;
516 }
517 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
518 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
519 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
520 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400521 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400522 }
523 return s;
524}
525
Brian Salomon23356442018-11-30 15:33:19 -0500526void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700527
Mike Reed9318a6c2019-08-16 16:16:25 -0400528class SkImageStageUpdater : public SkStageUpdater {
529public:
Mike Reed8845c372019-12-19 13:22:08 -0500530 SkImageStageUpdater(const SkImageShader* shader, bool usePersp)
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500531 : fShader(shader)
532 , fUsePersp(usePersp || as_SB(shader)->getLocalMatrix().hasPerspective())
Mike Reed8845c372019-12-19 13:22:08 -0500533 {}
Mike Reed9318a6c2019-08-16 16:16:25 -0400534
Mike Reed8845c372019-12-19 13:22:08 -0500535 const SkImageShader* fShader;
536 const bool fUsePersp; // else use affine
537
538 // large enough for perspective, though often we just use 2x3
539 float fMatrixStorage[9];
Mike Reed9318a6c2019-08-16 16:16:25 -0400540
541#if 0 // TODO: when we support mipmaps
542 SkRasterPipeline_GatherCtx* fGather;
543 SkRasterPipeline_TileCtx* fLimitX;
544 SkRasterPipeline_TileCtx* fLimitY;
545 SkRasterPipeline_DecalTileCtx* fDecal;
546#endif
547
Mike Reed8845c372019-12-19 13:22:08 -0500548 void append_matrix_stage(SkRasterPipeline* p) {
549 if (fUsePersp) {
550 p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage);
551 } else {
552 p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage);
553 }
554 }
555
Mike Reed9318a6c2019-08-16 16:16:25 -0400556 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
557 SkMatrix matrix;
Mike Reed8845c372019-12-19 13:22:08 -0500558 if (fShader->computeTotalInverse(ctm, localM, &matrix)) {
559 if (fUsePersp) {
560 matrix.get9(fMatrixStorage);
561 } else {
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500562 // if we get here, matrix should be affine. If it isn't, then defensively we
563 // won't draw (by returning false), but we should work to never let this
564 // happen (i.e. better preflight by the caller to know ahead of time that we
565 // may encounter perspective, either in the CTM, or in the localM).
566 //
567 // See https://bugs.chromium.org/p/skia/issues/detail?id=10004
568 //
569 if (!matrix.asAffine(fMatrixStorage)) {
570 SkASSERT(false);
571 return false;
572 }
Mike Reed8845c372019-12-19 13:22:08 -0500573 }
574 return true;
575 }
576 return false;
Mike Reed9318a6c2019-08-16 16:16:25 -0400577 }
578};
579
Mike Reed3d58d5a2020-11-23 13:32:36 -0500580static void tweak_filter_and_inv_matrix(SkFilterMode* filter, SkMatrix* matrix) {
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600581 // When the matrix is just an integer translate, bilerp == nearest neighbor.
Mike Reed3d58d5a2020-11-23 13:32:36 -0500582 if (*filter == SkFilterMode::kLinear &&
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600583 matrix->getType() <= SkMatrix::kTranslate_Mask &&
584 matrix->getTranslateX() == (int)matrix->getTranslateX() &&
585 matrix->getTranslateY() == (int)matrix->getTranslateY()) {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500586 *filter = SkFilterMode::kNearest;
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600587 }
588
589 // See skia:4649 and the GM image_scale_aligned.
Mike Reed3d58d5a2020-11-23 13:32:36 -0500590 if (*filter == SkFilterMode::kNearest) {
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600591 if (matrix->getScaleX() >= 0) {
592 matrix->setTranslateX(nextafterf(matrix->getTranslateX(),
593 floorf(matrix->getTranslateX())));
594 }
595 if (matrix->getScaleY() >= 0) {
596 matrix->setTranslateY(nextafterf(matrix->getTranslateY(),
597 floorf(matrix->getTranslateY())));
598 }
599 }
600}
601
Mike Reed9318a6c2019-08-16 16:16:25 -0400602bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
Mike Reed39b4c862020-11-25 16:15:53 -0500603 auto sampling = fUseSamplingOptions ? fSampling
604 : SkSamplingOptions(rec.fPaint.getFilterQuality());
605
606 // We only support certain sampling options in stages so far
607 if (sampling.fUseCubic) {
608 if (!is_default_cubic_resampler(sampling.fCubic)) {
609 return false;
Mike Reed74c51ac2020-11-20 10:23:58 -0500610 }
Mike Reed39b4c862020-11-25 16:15:53 -0500611 } else if (sampling.fMipmap == SkMipmapMode::kLinear) {
612 return false;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400613 }
Mike Reed9290d012020-06-11 16:56:06 -0400614
Mike Reed39b4c862020-11-25 16:15:53 -0500615
616 if (updater && (sampling.fMipmap != SkMipmapMode::kNone)) {
Mike Reed8845c372019-12-19 13:22:08 -0500617 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400618 return false;
619 }
620
Mike Reed1d8c42e2017-08-29 14:58:19 -0400621 SkRasterPipeline* p = rec.fPipeline;
622 SkArenaAlloc* alloc = rec.fAlloc;
623
Florin Malita7558e4d2018-02-07 10:05:53 -0500624 SkMatrix matrix;
Brian Osman9aaec362020-05-08 14:54:37 -0400625 if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500626 return false;
627 }
Mike Klein06a65e22016-11-17 12:39:09 -0500628
Mike Reed64acf4f2019-08-01 15:35:20 -0400629 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
Mike Reed39b4c862020-11-25 16:15:53 -0500630 matrix, sampling, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500631 if (!state) {
632 return false;
633 }
634
635 const SkPixmap& pm = state->pixmap();
Mike Reed39b4c862020-11-25 16:15:53 -0500636 matrix = state->invMatrix();
637 sampling = state->sampling();
Mike Kleinf447dee2016-11-29 16:37:12 -0500638 auto info = pm.info();
639
Mike Kleine8de0242018-03-10 12:37:11 -0500640 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400641
642 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500643 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400644 } else {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500645 if (!sampling.fUseCubic) {
646 tweak_filter_and_inv_matrix(&sampling.fFilter, &matrix);
647 }
Mike Reed9318a6c2019-08-16 16:16:25 -0400648 p->append_matrix(alloc, matrix);
649 }
Mike Klein06a65e22016-11-17 12:39:09 -0500650
Mike Kleinb11ab572018-10-24 06:42:14 -0400651 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500652 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400653 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400654 gather->width = pm.width();
655 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400656
Mike Kleinb11ab572018-10-24 06:42:14 -0400657 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
658 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400659 limit_x->scale = pm.width();
660 limit_x->invScale = 1.0f / pm.width();
661 limit_y->scale = pm.height();
662 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400663
Mike Kleinb11ab572018-10-24 06:42:14 -0400664 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400665 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
666 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400667 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500668 decal_ctx->limit_x = limit_x->scale;
669 decal_ctx->limit_y = limit_y->scale;
670 }
671
Mike Reed9318a6c2019-08-16 16:16:25 -0400672#if 0 // TODO: when we support kMedium
673 if (updator && (quality == kMedium_SkFilterQuality)) {
674 // if we change levels in mipmap, we need to update the scales (and invScales)
675 updator->fGather = gather;
676 updator->fLimitX = limit_x;
677 updator->fLimitY = limit_y;
678 updator->fDecal = decal_ctx;
679 }
680#endif
681
Mike Kleinb04c3522016-11-28 11:55:58 -0500682 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500683 if (decal_x_and_y) {
684 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
685 } else {
686 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400687 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
688 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
689 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
690 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500691 }
692 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400693 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
694 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
695 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
696 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500697 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500698 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500699
Mike Kleinac568a92018-01-25 09:09:32 -0500700 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500701 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500702 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400703 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
704 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500705 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
706 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400707 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
708 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
709 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500710 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
711 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400712 case kR16G16B16A16_unorm_SkColorType:
713 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600714 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500715 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400716 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500717
Mike Kleinb1df5e52018-10-17 17:06:03 -0400718 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
719 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400720
Mike Kleinac568a92018-01-25 09:09:32 -0500721 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
722 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400723
Mike Kleinf7eb0542020-02-11 12:19:08 -0600724 case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
725 p->append(SkRasterPipeline::swap_rb ); break;
726
Mike Kleinac568a92018-01-25 09:09:32 -0500727 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
728 p->append(SkRasterPipeline::force_opaque ); break;
729
Mike Kleinf7eb0542020-02-11 12:19:08 -0600730 case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
731 p->append(SkRasterPipeline::force_opaque );
732 p->append(SkRasterPipeline::swap_rb ); break;
733
Mike Klein1a3eb522018-10-18 10:11:00 -0400734 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
735 p->append(SkRasterPipeline::swap_rb ); break;
736
Mike Kleinb70990e2019-02-28 10:03:27 -0600737 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500738 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500739 if (decal_ctx) {
740 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
741 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500742 };
743
Mike Klein1fa9c432017-12-11 09:59:47 -0500744 auto append_misc = [&] {
Mike Klein6a9f1c42020-01-02 23:23:42 -0600745 SkColorSpace* cs = info.colorSpace();
746 SkAlphaType at = info.alphaType();
747
748 // Color for A8 images comes from the paint. TODO: all alpha images? none?
749 if (info.colorType() == kAlpha_8_SkColorType) {
750 SkColor4f rgb = rec.fPaint.getColor4f();
751 p->append_set_rgb(alloc, rgb);
752
Mike Klein6a9f1c42020-01-02 23:23:42 -0600753 cs = sk_srgb_singleton();
754 at = kUnpremul_SkAlphaType;
755 }
756
Mike Klein059e0432020-01-02 16:32:38 -0600757 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reed3d58d5a2020-11-23 13:32:36 -0500758 if (sampling.fUseCubic) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500759 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600760 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
761 ? SkRasterPipeline::clamp_1
762 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500763 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000764
Mike Klein059e0432020-01-02 16:32:38 -0600765 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
766 alloc->make<SkColorSpaceXformSteps>(cs, at,
767 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500768 ->apply(p);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000769
Mike Klein1fa9c432017-12-11 09:59:47 -0500770 return true;
771 };
772
Mike Reed9318a6c2019-08-16 16:16:25 -0400773 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400774 auto ct = info.colorType();
775 if (true
776 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Reed3d58d5a2020-11-23 13:32:36 -0500777 && !sampling.fUseCubic && sampling.fFilter == SkFilterMode::kLinear
Mike Reedfae8fce2019-04-03 10:27:45 -0400778 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500779
780 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400781 if (ct == kBGRA_8888_SkColorType) {
782 p->append(SkRasterPipeline::swap_rb);
783 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500784 return append_misc();
785 }
Mike Reed78eedba2019-07-31 16:39:15 -0400786 if (true
Mike Klein01005622019-08-13 12:22:17 -0400787 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Reed3d58d5a2020-11-23 13:32:36 -0500788 && !sampling.fUseCubic && sampling.fFilter == SkFilterMode::kLinear
Mike Klein01005622019-08-13 12:22:17 -0400789 && fTileModeX != SkTileMode::kDecal // TODO decal too?
790 && fTileModeY != SkTileMode::kDecal) {
791
792 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
793 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
794 ctx->ct = ct;
795 ctx->tileX = fTileModeX;
796 ctx->tileY = fTileModeY;
797 ctx->invWidth = 1.0f / ctx->width;
798 ctx->invHeight = 1.0f / ctx->height;
799 p->append(SkRasterPipeline::bilinear, ctx);
800 return append_misc();
801 }
802 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400803 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Reed3d58d5a2020-11-23 13:32:36 -0500804 && sampling.fUseCubic
Mike Reed78eedba2019-07-31 16:39:15 -0400805 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
806
807 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
808 if (ct == kBGRA_8888_SkColorType) {
809 p->append(SkRasterPipeline::swap_rb);
810 }
811 return append_misc();
812 }
Mike Klein01005622019-08-13 12:22:17 -0400813 if (true
814 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Reed3d58d5a2020-11-23 13:32:36 -0500815 && sampling.fUseCubic
Mike Klein01005622019-08-13 12:22:17 -0400816 && fTileModeX != SkTileMode::kDecal // TODO decal too?
817 && fTileModeY != SkTileMode::kDecal) {
818
819 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
820 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
821 ctx->ct = ct;
822 ctx->tileX = fTileModeX;
823 ctx->tileY = fTileModeY;
824 ctx->invWidth = 1.0f / ctx->width;
825 ctx->invHeight = 1.0f / ctx->height;
826 p->append(SkRasterPipeline::bicubic, ctx);
827 return append_misc();
828 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500829
Mike Reed3d58d5a2020-11-23 13:32:36 -0500830 SkRasterPipeline_SamplerCtx* sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400831
Mike Kleinb0b17d12016-12-09 16:25:44 -0500832 auto sample = [&](SkRasterPipeline::StockStage setup_x,
833 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400834 p->append(setup_x, sampler);
835 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500836 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400837 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500838 };
839
Mike Reed3d58d5a2020-11-23 13:32:36 -0500840 if (sampling.fUseCubic) {
Mike Klein0a904492017-04-12 12:52:48 -0400841 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500842
843 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
844 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
845 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
846 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
847
848 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
849 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
850 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
851 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
852
853 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
854 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
855 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
856 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
857
858 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
859 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
860 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
861 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
862
Mike Kleinb04c3522016-11-28 11:55:58 -0500863 p->append(SkRasterPipeline::move_dst_src);
Mike Reed3d58d5a2020-11-23 13:32:36 -0500864 } else if (sampling.fFilter == SkFilterMode::kLinear) {
865 p->append(SkRasterPipeline::save_xy, sampler);
866
867 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
868 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
869 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
870 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
871
872 p->append(SkRasterPipeline::move_dst_src);
873 } else {
874 append_tiling_and_gather();
Mike Klein06a65e22016-11-17 12:39:09 -0500875 }
876
Mike Klein1fa9c432017-12-11 09:59:47 -0500877 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500878}
Mike Reed9318a6c2019-08-16 16:16:25 -0400879
880bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
881 return this->doStages(rec, nullptr);
882}
883
884SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Brian Osman9aaec362020-05-08 14:54:37 -0400885 bool usePersp = rec.fMatrixProvider.localToDevice().hasPerspective();
Mike Reed8845c372019-12-19 13:22:08 -0500886 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400887 return this->doStages(rec, updater) ? updater : nullptr;
888}
889
Brian Osman5aaaeea2020-06-22 14:26:03 -0400890skvm::Color SkImageShader::onProgram(skvm::Builder* p,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400891 skvm::Coord device, skvm::Coord origLocal, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500892 const SkMatrixProvider& matrices, const SkMatrix* localM,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400893 SkFilterQuality paintQuality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500894 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400895 SkMatrix baseInv;
896 if (!this->computeTotalInverse(matrices.localToDevice(), localM, &baseInv)) {
Mike Reed6352f002020-03-14 23:30:10 -0400897 return {};
Mike Kleinf6a715b2019-12-30 15:24:18 -0600898 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400899 baseInv.normalizePerspective();
Mike Kleinf6a715b2019-12-30 15:24:18 -0600900
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400901 const SkPixmap *upper = nullptr,
902 *lower = nullptr;
903 SkMatrix upperInv;
904 float lowerWeight = 0;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400905
906 auto post_scale = [&](SkISize level, const SkMatrix& base) {
907 return SkMatrix::Scale(SkIntToScalar(level.width()) / fImage->width(),
908 SkIntToScalar(level.height()) / fImage->height())
909 * base;
910 };
911
Mike Reed3d58d5a2020-11-23 13:32:36 -0500912 auto sampling = fUseSamplingOptions ? fSampling : SkSamplingOptions(paintQuality);
913 if (sampling.fUseCubic) {
914 auto* access = alloc->make<SkMipmapAccessor>(as_IB(fImage.get()), baseInv,
915 SkMipmapMode::kNone);
916 upper = &access->level();
917 upperInv = post_scale(upper->dimensions(), baseInv);
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400918 } else {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500919 auto* access = alloc->make<SkMipmapAccessor>(as_IB(fImage.get()), baseInv,
920 sampling.fMipmap);
921 upper = &access->level();
922 upperInv = post_scale(upper->dimensions(), baseInv);
923 lowerWeight = access->lowerWeight();
924 if (lowerWeight > 0) {
925 lower = &access->lowerLevel();
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400926 }
Mike Reed65fb1012020-11-24 01:58:03 +0000927 tweak_filter_and_inv_matrix(&sampling.fFilter, &upperInv);
Mike Kleinf6a715b2019-12-30 15:24:18 -0600928 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600929
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400930 skvm::Coord upperLocal = SkShaderBase::ApplyMatrix(p, upperInv, origLocal, uniforms);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600931
Mike Klein89b3c1f2020-07-29 16:45:05 -0500932 // All existing SkColorTypes pass these checks. We'd only fail here adding new ones.
Mike Klein03c932c2020-07-13 09:07:42 -0500933 skvm::PixelFormat unused;
Mike Klein9662fd62020-07-16 15:11:27 -0500934 if (true && !SkColorType_to_PixelFormat(upper->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500935 return {};
936 }
Mike Klein9662fd62020-07-16 15:11:27 -0500937 if (lower && !SkColorType_to_PixelFormat(lower->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500938 return {};
Mike Klein25480bf2020-01-06 14:01:58 -0600939 }
940
Mike Klein921236b2020-02-13 11:20:54 -0600941 // We can exploit image opacity to skip work unpacking alpha channels.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400942 const bool input_is_opaque = SkAlphaTypeIsOpaque(upper->alphaType())
943 || SkColorTypeIsAlwaysOpaque(upper->colorType());
Mike Klein921236b2020-02-13 11:20:54 -0600944
Mike Klein03d89ef2020-01-14 17:18:29 -0600945 // Each call to sample() will try to rewrite the same uniforms over and over,
946 // so remember where we start and reset back there each time. That way each
947 // sample() call uses the same uniform offsets.
Mike Klein03d89ef2020-01-14 17:18:29 -0600948
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400949 auto compute_clamp_limit = [&](float limit) {
950 // Subtract an ulp so the upper clamp limit excludes limit itself.
951 int bits;
952 memcpy(&bits, &limit, 4);
953 return p->uniformF(uniforms->push(bits-1));
954 };
Mike Klein03d89ef2020-01-14 17:18:29 -0600955
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400956 // Except in the simplest case (no mips, no filtering), we reference uniforms
957 // more than once. To avoid adding/registering them multiple times, we pre-load them
958 // into a struct (just to logically group them together), based on the "current"
959 // pixmap (level of a mipmap).
960 //
961 struct Uniforms {
962 skvm::F32 w, iw, i2w,
963 h, ih, i2h;
964
965 skvm::F32 clamp_w,
966 clamp_h;
967
968 skvm::Uniform addr;
969 skvm::I32 rowBytesAsPixels;
970
Mike Klein03c932c2020-07-13 09:07:42 -0500971 skvm::PixelFormat pixelFormat; // not a uniform, but needed for each texel sample,
972 // so we store it here, since it is also dependent on
973 // the current pixmap (level).
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400974 };
975
976 auto setup_uniforms = [&](const SkPixmap& pm) -> Uniforms {
Mike Klein03c932c2020-07-13 09:07:42 -0500977 skvm::PixelFormat pixelFormat;
Mike Klein9662fd62020-07-16 15:11:27 -0500978 SkAssertResult(SkColorType_to_PixelFormat(pm.colorType(), &pixelFormat));
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400979 return {
980 p->uniformF(uniforms->pushF( pm.width())),
981 p->uniformF(uniforms->pushF(1.0f/pm.width())), // iff tileX == kRepeat
982 p->uniformF(uniforms->pushF(0.5f/pm.width())), // iff tileX == kMirror
983
984 p->uniformF(uniforms->pushF( pm.height())),
985 p->uniformF(uniforms->pushF(1.0f/pm.height())), // iff tileY == kRepeat
986 p->uniformF(uniforms->pushF(0.5f/pm.height())), // iff tileY == kMirror
987
988 compute_clamp_limit(pm. width()),
989 compute_clamp_limit(pm.height()),
990
991 uniforms->pushPtr(pm.addr()),
992 p->uniform32(uniforms->push(pm.rowBytesAsPixels())),
993
Mike Klein03c932c2020-07-13 09:07:42 -0500994 pixelFormat,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400995 };
996 };
997
998 auto sample_texel = [&](const Uniforms& u, skvm::F32 sx, skvm::F32 sy) -> skvm::Color {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600999 // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp.
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001000 auto repeat = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I) {
Mike Reedf3b9a302020-04-01 13:18:02 -04001001 return v - floor(v * I) * S;
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001002 };
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001003 auto mirror = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I2) {
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001004 // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale )
1005 // {---A---} {------------------B------------------}
Mike Reedf3b9a302020-04-01 13:18:02 -04001006 skvm::F32 A = v - S,
1007 B = (S + S) * floor(A * I2);
1008 return abs(A - B - S);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001009 };
1010 switch (fTileModeX) {
1011 case SkTileMode::kDecal: /* handled after gather */ break;
1012 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001013 case SkTileMode::kRepeat: sx = repeat(sx, u.w, u.iw); break;
1014 case SkTileMode::kMirror: sx = mirror(sx, u.w, u.i2w); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001015 }
1016 switch (fTileModeY) {
1017 case SkTileMode::kDecal: /* handled after gather */ break;
1018 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001019 case SkTileMode::kRepeat: sy = repeat(sy, u.h, u.ih); break;
1020 case SkTileMode::kMirror: sy = mirror(sy, u.h, u.i2h); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001021 }
1022
1023 // Always clamp sample coordinates to [0,width), [0,height), both for memory
1024 // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror.
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001025 skvm::F32 clamped_x = clamp(sx, 0, u.clamp_w),
1026 clamped_y = clamp(sy, 0, u.clamp_h);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001027
1028 // Load pixels from pm.addr()[(int)sx + (int)sy*stride].
Mike Reedf3b9a302020-04-01 13:18:02 -04001029 skvm::I32 index = trunc(clamped_x) +
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001030 trunc(clamped_y) * u.rowBytesAsPixels;
Mike Klein03c932c2020-07-13 09:07:42 -05001031 skvm::Color c = gather(u.pixelFormat, u.addr, index);
Mike Klein913a6f52020-03-23 11:06:25 -05001032
Mike Klein921236b2020-02-13 11:20:54 -06001033 // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it.
1034 if (input_is_opaque) {
Mike Klein46874ef2020-02-13 10:17:08 -06001035 c.a = p->splat(1.0f);
1036 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001037
1038 // Mask away any pixels that we tried to sample outside the bounds in kDecal.
1039 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
1040 skvm::I32 mask = p->splat(~0);
Mike Reedf3b9a302020-04-01 13:18:02 -04001041 if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); }
1042 if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); }
1043 c.r = bit_cast(p->bit_and(mask, bit_cast(c.r)));
1044 c.g = bit_cast(p->bit_and(mask, bit_cast(c.g)));
1045 c.b = bit_cast(p->bit_and(mask, bit_cast(c.b)));
1046 c.a = bit_cast(p->bit_and(mask, bit_cast(c.a)));
Mike Klein921236b2020-02-13 11:20:54 -06001047 // Notice that even if input_is_opaque, c.a might now be 0.
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001048 }
1049
1050 return c;
1051 };
1052
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001053 auto sample_level = [&](const SkPixmap& pm, const SkMatrix& inv, skvm::Coord local) {
1054 const Uniforms u = setup_uniforms(pm);
Mike Reed6352f002020-03-14 23:30:10 -04001055
Mike Reed3d58d5a2020-11-23 13:32:36 -05001056 if (sampling.fUseCubic) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001057 // All bicubic samples have the same fractional offset (fx,fy) from the center.
1058 // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center.
1059 skvm::F32 fx = fract(local.x + 0.5f),
1060 fy = fract(local.y + 0.5f);
Mike Reed3d30ca62020-07-22 16:55:02 -04001061 skvm::F32 wx[4],
1062 wy[4];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001063
Mike Reed3d58d5a2020-11-23 13:32:36 -05001064 SkM44 weights = CubicResamplerMatrix(sampling.fCubic.B, sampling.fCubic.C);
Mike Reed3d30ca62020-07-22 16:55:02 -04001065
Mike Kleind3184892020-07-22 17:39:20 -05001066 auto dot = [](const skvm::F32 a[], const skvm::F32 b[]) {
1067 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
1068 };
1069 const skvm::F32 tmpx[] = { p->splat(1.0f), fx, fx*fx, fx*fx*fx };
1070 const skvm::F32 tmpy[] = { p->splat(1.0f), fy, fy*fy, fy*fy*fy };
Mike Reed3d30ca62020-07-22 16:55:02 -04001071
Mike Kleind3184892020-07-22 17:39:20 -05001072 for (int row = 0; row < 4; ++row) {
1073 SkV4 r = weights.row(row);
1074 skvm::F32 ru[] = {
1075 p->uniformF(uniforms->pushF(r[0])),
1076 p->uniformF(uniforms->pushF(r[1])),
1077 p->uniformF(uniforms->pushF(r[2])),
1078 p->uniformF(uniforms->pushF(r[3])),
Mike Reed3d30ca62020-07-22 16:55:02 -04001079 };
Mike Kleind3184892020-07-22 17:39:20 -05001080 wx[row] = dot(ru, tmpx);
1081 wy[row] = dot(ru, tmpy);
Mike Reed3d30ca62020-07-22 16:55:02 -04001082 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001083
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001084 skvm::Color c;
1085 c.r = c.g = c.b = c.a = p->splat(0.0f);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001086
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001087 skvm::F32 sy = local.y - 1.5f;
1088 for (int j = 0; j < 4; j++, sy += 1.0f) {
1089 skvm::F32 sx = local.x - 1.5f;
1090 for (int i = 0; i < 4; i++, sx += 1.0f) {
1091 skvm::Color s = sample_texel(u, sx,sy);
1092 skvm::F32 w = wx[i] * wy[j];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001093
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001094 c.r += s.r * w;
1095 c.g += s.g * w;
1096 c.b += s.b * w;
1097 c.a += s.a * w;
1098 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001099 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001100 return c;
Mike Reed3d58d5a2020-11-23 13:32:36 -05001101 } else if (sampling.fFilter == SkFilterMode::kLinear) {
1102 // Our four sample points are the corners of a logical 1x1 pixel
1103 // box surrounding (x,y) at (0.5,0.5) off-center.
1104 skvm::F32 left = local.x - 0.5f,
1105 top = local.y - 0.5f,
1106 right = local.x + 0.5f,
1107 bottom = local.y + 0.5f;
1108
1109 // The fractional parts of right and bottom are our lerp factors in x and y respectively.
1110 skvm::F32 fx = fract(right ),
1111 fy = fract(bottom);
1112
1113 return lerp(lerp(sample_texel(u, left,top ), sample_texel(u, right,top ), fx),
1114 lerp(sample_texel(u, left,bottom), sample_texel(u, right,bottom), fx), fy);
1115 } else {
1116 SkASSERT(sampling.fFilter == SkFilterMode::kNearest);
1117 return sample_texel(u, local.x,local.y);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001118 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001119 };
1120
1121 skvm::Color c = sample_level(*upper, upperInv, upperLocal);
1122 if (lower) {
1123 auto lowerInv = post_scale(lower->dimensions(), baseInv);
1124 auto lowerLocal = SkShaderBase::ApplyMatrix(p, lowerInv, origLocal, uniforms);
1125 // lower * weight + upper * (1 - weight)
1126 c = lerp(c,
1127 sample_level(*lower, lowerInv, lowerLocal),
1128 p->uniformF(uniforms->pushF(lowerWeight)));
Mike Klein921236b2020-02-13 11:20:54 -06001129 }
Mike Kleind67a5c32020-01-06 16:01:01 -06001130
Mike Klein921236b2020-02-13 11:20:54 -06001131 // If the input is opaque and we're not in decal mode, that means the output is too.
1132 // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas.
1133 if (input_is_opaque
1134 && fTileModeX != SkTileMode::kDecal
1135 && fTileModeY != SkTileMode::kDecal) {
Mike Reed6352f002020-03-14 23:30:10 -04001136 c.a = p->splat(1.0f);
Mike Klein921236b2020-02-13 11:20:54 -06001137 }
1138
Mike Klein913a6f52020-03-23 11:06:25 -05001139 // Alpha-only images get their color from the paint (already converted to dst color space).
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001140 SkColorSpace* cs = upper->colorSpace();
1141 SkAlphaType at = upper->alphaType();
1142 if (SkColorTypeIsAlphaOnly(upper->colorType())) {
Mike Klein913a6f52020-03-23 11:06:25 -05001143 c.r = paint.r;
1144 c.g = paint.g;
1145 c.b = paint.b;
1146
1147 cs = dst.colorSpace();
1148 at = kUnpremul_SkAlphaType;
1149 }
1150
Mike Reed3d58d5a2020-11-23 13:32:36 -05001151 if (sampling.fUseCubic) {
Mike Klein5c660e02020-01-08 11:36:35 -06001152 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reedf3b9a302020-04-01 13:18:02 -04001153 c.a = clamp01(c.a);
Mike Klein3f83bfd2020-01-07 12:01:50 -06001154
Mike Klein913a6f52020-03-23 11:06:25 -05001155 skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul)
Mike Klein3f83bfd2020-01-07 12:01:50 -06001156 ? p->splat(1.0f)
Mike Reed6352f002020-03-14 23:30:10 -04001157 : c.a;
Mike Reedf3b9a302020-04-01 13:18:02 -04001158 c.r = clamp(c.r, 0.0f, limit);
1159 c.g = clamp(c.g, 0.0f, limit);
1160 c.b = clamp(c.b, 0.0f, limit);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001161 }
Mike Klein4bc86d52020-01-06 18:39:42 -06001162
Mike Kleinef0fa432020-07-29 12:35:45 -05001163 return SkColorSpaceXformSteps{cs,at, dst.colorSpace(),dst.alphaType()}.program(p, uniforms, c);
Mike Kleinf6a715b2019-12-30 15:24:18 -06001164}