blob: b1b9f441969bbadc4e86158815946fffc97b4920 [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 Reed3d58d5a2020-11-23 13:32:36 -0500603 SkFilterQuality paintQuality = rec.fPaint.getFilterQuality();
Mike Reed74c51ac2020-11-20 10:23:58 -0500604 if (fUseSamplingOptions) {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500605 if (!sampling_to_quality(fSampling, &paintQuality)) {
Mike Reed74c51ac2020-11-20 10:23:58 -0500606 return false; // TODO: support samplingoptions in stages?
607 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400608 }
Mike Reed9290d012020-06-11 16:56:06 -0400609
Mike Reed3d58d5a2020-11-23 13:32:36 -0500610 if (updater && paintQuality == kMedium_SkFilterQuality) {
Mike Reed8845c372019-12-19 13:22:08 -0500611 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400612 return false;
613 }
614
Mike Reed1d8c42e2017-08-29 14:58:19 -0400615 SkRasterPipeline* p = rec.fPipeline;
616 SkArenaAlloc* alloc = rec.fAlloc;
617
Florin Malita7558e4d2018-02-07 10:05:53 -0500618 SkMatrix matrix;
Brian Osman9aaec362020-05-08 14:54:37 -0400619 if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500620 return false;
621 }
Mike Klein06a65e22016-11-17 12:39:09 -0500622
Mike Reed64acf4f2019-08-01 15:35:20 -0400623 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
Mike Reed3d58d5a2020-11-23 13:32:36 -0500624 matrix, paintQuality, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500625 if (!state) {
626 return false;
627 }
628
629 const SkPixmap& pm = state->pixmap();
630 matrix = state->invMatrix();
Mike Kleinf447dee2016-11-29 16:37:12 -0500631 auto info = pm.info();
632
Mike Reed3d58d5a2020-11-23 13:32:36 -0500633 // from here down, we don't look at paintQuality, only sampling.
634 SkSamplingOptions sampling(state->quality());
635
Mike Kleine8de0242018-03-10 12:37:11 -0500636 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400637
638 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500639 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400640 } else {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500641 if (!sampling.fUseCubic) {
642 tweak_filter_and_inv_matrix(&sampling.fFilter, &matrix);
643 }
Mike Reed9318a6c2019-08-16 16:16:25 -0400644 p->append_matrix(alloc, matrix);
645 }
Mike Klein06a65e22016-11-17 12:39:09 -0500646
Mike Kleinb11ab572018-10-24 06:42:14 -0400647 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500648 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400649 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400650 gather->width = pm.width();
651 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400652
Mike Kleinb11ab572018-10-24 06:42:14 -0400653 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
654 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400655 limit_x->scale = pm.width();
656 limit_x->invScale = 1.0f / pm.width();
657 limit_y->scale = pm.height();
658 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400659
Mike Kleinb11ab572018-10-24 06:42:14 -0400660 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400661 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
662 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400663 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500664 decal_ctx->limit_x = limit_x->scale;
665 decal_ctx->limit_y = limit_y->scale;
666 }
667
Mike Reed9318a6c2019-08-16 16:16:25 -0400668#if 0 // TODO: when we support kMedium
669 if (updator && (quality == kMedium_SkFilterQuality)) {
670 // if we change levels in mipmap, we need to update the scales (and invScales)
671 updator->fGather = gather;
672 updator->fLimitX = limit_x;
673 updator->fLimitY = limit_y;
674 updator->fDecal = decal_ctx;
675 }
676#endif
677
Mike Kleinb04c3522016-11-28 11:55:58 -0500678 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500679 if (decal_x_and_y) {
680 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
681 } else {
682 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400683 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
684 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
685 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
686 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500687 }
688 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400689 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
690 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
691 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
692 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500693 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500694 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500695
Mike Kleinac568a92018-01-25 09:09:32 -0500696 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500697 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500698 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400699 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
700 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500701 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
702 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400703 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
704 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
705 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500706 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
707 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400708 case kR16G16B16A16_unorm_SkColorType:
709 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600710 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500711 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400712 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500713
Mike Kleinb1df5e52018-10-17 17:06:03 -0400714 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
715 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400716
Mike Kleinac568a92018-01-25 09:09:32 -0500717 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
718 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400719
Mike Kleinf7eb0542020-02-11 12:19:08 -0600720 case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
721 p->append(SkRasterPipeline::swap_rb ); break;
722
Mike Kleinac568a92018-01-25 09:09:32 -0500723 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
724 p->append(SkRasterPipeline::force_opaque ); break;
725
Mike Kleinf7eb0542020-02-11 12:19:08 -0600726 case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
727 p->append(SkRasterPipeline::force_opaque );
728 p->append(SkRasterPipeline::swap_rb ); break;
729
Mike Klein1a3eb522018-10-18 10:11:00 -0400730 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
731 p->append(SkRasterPipeline::swap_rb ); break;
732
Mike Kleinb70990e2019-02-28 10:03:27 -0600733 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500734 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500735 if (decal_ctx) {
736 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
737 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500738 };
739
Mike Klein1fa9c432017-12-11 09:59:47 -0500740 auto append_misc = [&] {
Mike Klein6a9f1c42020-01-02 23:23:42 -0600741 SkColorSpace* cs = info.colorSpace();
742 SkAlphaType at = info.alphaType();
743
744 // Color for A8 images comes from the paint. TODO: all alpha images? none?
745 if (info.colorType() == kAlpha_8_SkColorType) {
746 SkColor4f rgb = rec.fPaint.getColor4f();
747 p->append_set_rgb(alloc, rgb);
748
Mike Klein6a9f1c42020-01-02 23:23:42 -0600749 cs = sk_srgb_singleton();
750 at = kUnpremul_SkAlphaType;
751 }
752
Mike Klein059e0432020-01-02 16:32:38 -0600753 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reed3d58d5a2020-11-23 13:32:36 -0500754 if (sampling.fUseCubic) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500755 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600756 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
757 ? SkRasterPipeline::clamp_1
758 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500759 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000760
Mike Klein059e0432020-01-02 16:32:38 -0600761 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
762 alloc->make<SkColorSpaceXformSteps>(cs, at,
763 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500764 ->apply(p);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000765
Mike Klein1fa9c432017-12-11 09:59:47 -0500766 return true;
767 };
768
Mike Reed9318a6c2019-08-16 16:16:25 -0400769 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400770 auto ct = info.colorType();
771 if (true
772 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Reed3d58d5a2020-11-23 13:32:36 -0500773 && !sampling.fUseCubic && sampling.fFilter == SkFilterMode::kLinear
Mike Reedfae8fce2019-04-03 10:27:45 -0400774 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500775
776 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400777 if (ct == kBGRA_8888_SkColorType) {
778 p->append(SkRasterPipeline::swap_rb);
779 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500780 return append_misc();
781 }
Mike Reed78eedba2019-07-31 16:39:15 -0400782 if (true
Mike Klein01005622019-08-13 12:22:17 -0400783 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Reed3d58d5a2020-11-23 13:32:36 -0500784 && !sampling.fUseCubic && sampling.fFilter == SkFilterMode::kLinear
Mike Klein01005622019-08-13 12:22:17 -0400785 && fTileModeX != SkTileMode::kDecal // TODO decal too?
786 && fTileModeY != SkTileMode::kDecal) {
787
788 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
789 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
790 ctx->ct = ct;
791 ctx->tileX = fTileModeX;
792 ctx->tileY = fTileModeY;
793 ctx->invWidth = 1.0f / ctx->width;
794 ctx->invHeight = 1.0f / ctx->height;
795 p->append(SkRasterPipeline::bilinear, ctx);
796 return append_misc();
797 }
798 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400799 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
Mike Reed3d58d5a2020-11-23 13:32:36 -0500800 && sampling.fUseCubic
Mike Reed78eedba2019-07-31 16:39:15 -0400801 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
802
803 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
804 if (ct == kBGRA_8888_SkColorType) {
805 p->append(SkRasterPipeline::swap_rb);
806 }
807 return append_misc();
808 }
Mike Klein01005622019-08-13 12:22:17 -0400809 if (true
810 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
Mike Reed3d58d5a2020-11-23 13:32:36 -0500811 && sampling.fUseCubic
Mike Klein01005622019-08-13 12:22:17 -0400812 && fTileModeX != SkTileMode::kDecal // TODO decal too?
813 && fTileModeY != SkTileMode::kDecal) {
814
815 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
816 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
817 ctx->ct = ct;
818 ctx->tileX = fTileModeX;
819 ctx->tileY = fTileModeY;
820 ctx->invWidth = 1.0f / ctx->width;
821 ctx->invHeight = 1.0f / ctx->height;
822 p->append(SkRasterPipeline::bicubic, ctx);
823 return append_misc();
824 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500825
Mike Reed3d58d5a2020-11-23 13:32:36 -0500826 SkRasterPipeline_SamplerCtx* sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400827
Mike Kleinb0b17d12016-12-09 16:25:44 -0500828 auto sample = [&](SkRasterPipeline::StockStage setup_x,
829 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400830 p->append(setup_x, sampler);
831 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500832 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400833 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500834 };
835
Mike Reed3d58d5a2020-11-23 13:32:36 -0500836 if (sampling.fUseCubic) {
Mike Klein0a904492017-04-12 12:52:48 -0400837 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500838
839 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
840 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
841 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
842 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
843
844 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
845 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
846 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
847 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
848
849 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
850 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
851 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
852 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
853
854 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
855 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
856 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
857 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
858
Mike Kleinb04c3522016-11-28 11:55:58 -0500859 p->append(SkRasterPipeline::move_dst_src);
Mike Reed3d58d5a2020-11-23 13:32:36 -0500860 } else if (sampling.fFilter == SkFilterMode::kLinear) {
861 p->append(SkRasterPipeline::save_xy, sampler);
862
863 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
864 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
865 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
866 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
867
868 p->append(SkRasterPipeline::move_dst_src);
869 } else {
870 append_tiling_and_gather();
Mike Klein06a65e22016-11-17 12:39:09 -0500871 }
872
Mike Klein1fa9c432017-12-11 09:59:47 -0500873 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500874}
Mike Reed9318a6c2019-08-16 16:16:25 -0400875
876bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
877 return this->doStages(rec, nullptr);
878}
879
880SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Brian Osman9aaec362020-05-08 14:54:37 -0400881 bool usePersp = rec.fMatrixProvider.localToDevice().hasPerspective();
Mike Reed8845c372019-12-19 13:22:08 -0500882 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400883 return this->doStages(rec, updater) ? updater : nullptr;
884}
885
Brian Osman5aaaeea2020-06-22 14:26:03 -0400886skvm::Color SkImageShader::onProgram(skvm::Builder* p,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400887 skvm::Coord device, skvm::Coord origLocal, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500888 const SkMatrixProvider& matrices, const SkMatrix* localM,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400889 SkFilterQuality paintQuality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500890 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400891 SkMatrix baseInv;
892 if (!this->computeTotalInverse(matrices.localToDevice(), localM, &baseInv)) {
Mike Reed6352f002020-03-14 23:30:10 -0400893 return {};
Mike Kleinf6a715b2019-12-30 15:24:18 -0600894 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400895 baseInv.normalizePerspective();
Mike Kleinf6a715b2019-12-30 15:24:18 -0600896
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400897 const SkPixmap *upper = nullptr,
898 *lower = nullptr;
899 SkMatrix upperInv;
900 float lowerWeight = 0;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400901
902 auto post_scale = [&](SkISize level, const SkMatrix& base) {
903 return SkMatrix::Scale(SkIntToScalar(level.width()) / fImage->width(),
904 SkIntToScalar(level.height()) / fImage->height())
905 * base;
906 };
907
Mike Reed3d58d5a2020-11-23 13:32:36 -0500908 auto sampling = fUseSamplingOptions ? fSampling : SkSamplingOptions(paintQuality);
909 if (sampling.fUseCubic) {
910 auto* access = alloc->make<SkMipmapAccessor>(as_IB(fImage.get()), baseInv,
911 SkMipmapMode::kNone);
912 upper = &access->level();
913 upperInv = post_scale(upper->dimensions(), baseInv);
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400914 } else {
Mike Reed3d58d5a2020-11-23 13:32:36 -0500915 auto* access = alloc->make<SkMipmapAccessor>(as_IB(fImage.get()), baseInv,
916 sampling.fMipmap);
917 upper = &access->level();
918 upperInv = post_scale(upper->dimensions(), baseInv);
919 lowerWeight = access->lowerWeight();
920 if (lowerWeight > 0) {
921 lower = &access->lowerLevel();
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400922 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400923
Mike Reed3d58d5a2020-11-23 13:32:36 -0500924 // should we do this all the time (not use in UseSampling?)
925 if (!fUseSamplingOptions) {
926 if (!sampling.fUseCubic) {
927 tweak_filter_and_inv_matrix(&sampling.fFilter, &upperInv);
928 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400929 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600930 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600931
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400932 skvm::Coord upperLocal = SkShaderBase::ApplyMatrix(p, upperInv, origLocal, uniforms);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600933
Mike Klein89b3c1f2020-07-29 16:45:05 -0500934 // All existing SkColorTypes pass these checks. We'd only fail here adding new ones.
Mike Klein03c932c2020-07-13 09:07:42 -0500935 skvm::PixelFormat unused;
Mike Klein9662fd62020-07-16 15:11:27 -0500936 if (true && !SkColorType_to_PixelFormat(upper->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500937 return {};
938 }
Mike Klein9662fd62020-07-16 15:11:27 -0500939 if (lower && !SkColorType_to_PixelFormat(lower->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500940 return {};
Mike Klein25480bf2020-01-06 14:01:58 -0600941 }
942
Mike Klein921236b2020-02-13 11:20:54 -0600943 // We can exploit image opacity to skip work unpacking alpha channels.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400944 const bool input_is_opaque = SkAlphaTypeIsOpaque(upper->alphaType())
945 || SkColorTypeIsAlwaysOpaque(upper->colorType());
Mike Klein921236b2020-02-13 11:20:54 -0600946
Mike Klein03d89ef2020-01-14 17:18:29 -0600947 // Each call to sample() will try to rewrite the same uniforms over and over,
948 // so remember where we start and reset back there each time. That way each
949 // sample() call uses the same uniform offsets.
Mike Klein03d89ef2020-01-14 17:18:29 -0600950
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400951 auto compute_clamp_limit = [&](float limit) {
952 // Subtract an ulp so the upper clamp limit excludes limit itself.
953 int bits;
954 memcpy(&bits, &limit, 4);
955 return p->uniformF(uniforms->push(bits-1));
956 };
Mike Klein03d89ef2020-01-14 17:18:29 -0600957
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400958 // Except in the simplest case (no mips, no filtering), we reference uniforms
959 // more than once. To avoid adding/registering them multiple times, we pre-load them
960 // into a struct (just to logically group them together), based on the "current"
961 // pixmap (level of a mipmap).
962 //
963 struct Uniforms {
964 skvm::F32 w, iw, i2w,
965 h, ih, i2h;
966
967 skvm::F32 clamp_w,
968 clamp_h;
969
970 skvm::Uniform addr;
971 skvm::I32 rowBytesAsPixels;
972
Mike Klein03c932c2020-07-13 09:07:42 -0500973 skvm::PixelFormat pixelFormat; // not a uniform, but needed for each texel sample,
974 // so we store it here, since it is also dependent on
975 // the current pixmap (level).
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400976 };
977
978 auto setup_uniforms = [&](const SkPixmap& pm) -> Uniforms {
Mike Klein03c932c2020-07-13 09:07:42 -0500979 skvm::PixelFormat pixelFormat;
Mike Klein9662fd62020-07-16 15:11:27 -0500980 SkAssertResult(SkColorType_to_PixelFormat(pm.colorType(), &pixelFormat));
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400981 return {
982 p->uniformF(uniforms->pushF( pm.width())),
983 p->uniformF(uniforms->pushF(1.0f/pm.width())), // iff tileX == kRepeat
984 p->uniformF(uniforms->pushF(0.5f/pm.width())), // iff tileX == kMirror
985
986 p->uniformF(uniforms->pushF( pm.height())),
987 p->uniformF(uniforms->pushF(1.0f/pm.height())), // iff tileY == kRepeat
988 p->uniformF(uniforms->pushF(0.5f/pm.height())), // iff tileY == kMirror
989
990 compute_clamp_limit(pm. width()),
991 compute_clamp_limit(pm.height()),
992
993 uniforms->pushPtr(pm.addr()),
994 p->uniform32(uniforms->push(pm.rowBytesAsPixels())),
995
Mike Klein03c932c2020-07-13 09:07:42 -0500996 pixelFormat,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400997 };
998 };
999
1000 auto sample_texel = [&](const Uniforms& u, skvm::F32 sx, skvm::F32 sy) -> skvm::Color {
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001001 // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp.
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001002 auto repeat = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I) {
Mike Reedf3b9a302020-04-01 13:18:02 -04001003 return v - floor(v * I) * S;
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001004 };
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001005 auto mirror = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I2) {
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001006 // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale )
1007 // {---A---} {------------------B------------------}
Mike Reedf3b9a302020-04-01 13:18:02 -04001008 skvm::F32 A = v - S,
1009 B = (S + S) * floor(A * I2);
1010 return abs(A - B - S);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001011 };
1012 switch (fTileModeX) {
1013 case SkTileMode::kDecal: /* handled after gather */ break;
1014 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001015 case SkTileMode::kRepeat: sx = repeat(sx, u.w, u.iw); break;
1016 case SkTileMode::kMirror: sx = mirror(sx, u.w, u.i2w); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001017 }
1018 switch (fTileModeY) {
1019 case SkTileMode::kDecal: /* handled after gather */ break;
1020 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001021 case SkTileMode::kRepeat: sy = repeat(sy, u.h, u.ih); break;
1022 case SkTileMode::kMirror: sy = mirror(sy, u.h, u.i2h); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001023 }
1024
1025 // Always clamp sample coordinates to [0,width), [0,height), both for memory
1026 // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror.
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001027 skvm::F32 clamped_x = clamp(sx, 0, u.clamp_w),
1028 clamped_y = clamp(sy, 0, u.clamp_h);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001029
1030 // Load pixels from pm.addr()[(int)sx + (int)sy*stride].
Mike Reedf3b9a302020-04-01 13:18:02 -04001031 skvm::I32 index = trunc(clamped_x) +
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001032 trunc(clamped_y) * u.rowBytesAsPixels;
Mike Klein03c932c2020-07-13 09:07:42 -05001033 skvm::Color c = gather(u.pixelFormat, u.addr, index);
Mike Klein913a6f52020-03-23 11:06:25 -05001034
Mike Klein921236b2020-02-13 11:20:54 -06001035 // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it.
1036 if (input_is_opaque) {
Mike Klein46874ef2020-02-13 10:17:08 -06001037 c.a = p->splat(1.0f);
1038 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001039
1040 // Mask away any pixels that we tried to sample outside the bounds in kDecal.
1041 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
1042 skvm::I32 mask = p->splat(~0);
Mike Reedf3b9a302020-04-01 13:18:02 -04001043 if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); }
1044 if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); }
1045 c.r = bit_cast(p->bit_and(mask, bit_cast(c.r)));
1046 c.g = bit_cast(p->bit_and(mask, bit_cast(c.g)));
1047 c.b = bit_cast(p->bit_and(mask, bit_cast(c.b)));
1048 c.a = bit_cast(p->bit_and(mask, bit_cast(c.a)));
Mike Klein921236b2020-02-13 11:20:54 -06001049 // Notice that even if input_is_opaque, c.a might now be 0.
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001050 }
1051
1052 return c;
1053 };
1054
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001055 auto sample_level = [&](const SkPixmap& pm, const SkMatrix& inv, skvm::Coord local) {
1056 const Uniforms u = setup_uniforms(pm);
Mike Reed6352f002020-03-14 23:30:10 -04001057
Mike Reed3d58d5a2020-11-23 13:32:36 -05001058 if (sampling.fUseCubic) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001059 // All bicubic samples have the same fractional offset (fx,fy) from the center.
1060 // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center.
1061 skvm::F32 fx = fract(local.x + 0.5f),
1062 fy = fract(local.y + 0.5f);
Mike Reed3d30ca62020-07-22 16:55:02 -04001063 skvm::F32 wx[4],
1064 wy[4];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001065
Mike Reed3d58d5a2020-11-23 13:32:36 -05001066 SkM44 weights = CubicResamplerMatrix(sampling.fCubic.B, sampling.fCubic.C);
Mike Reed3d30ca62020-07-22 16:55:02 -04001067
Mike Kleind3184892020-07-22 17:39:20 -05001068 auto dot = [](const skvm::F32 a[], const skvm::F32 b[]) {
1069 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
1070 };
1071 const skvm::F32 tmpx[] = { p->splat(1.0f), fx, fx*fx, fx*fx*fx };
1072 const skvm::F32 tmpy[] = { p->splat(1.0f), fy, fy*fy, fy*fy*fy };
Mike Reed3d30ca62020-07-22 16:55:02 -04001073
Mike Kleind3184892020-07-22 17:39:20 -05001074 for (int row = 0; row < 4; ++row) {
1075 SkV4 r = weights.row(row);
1076 skvm::F32 ru[] = {
1077 p->uniformF(uniforms->pushF(r[0])),
1078 p->uniformF(uniforms->pushF(r[1])),
1079 p->uniformF(uniforms->pushF(r[2])),
1080 p->uniformF(uniforms->pushF(r[3])),
Mike Reed3d30ca62020-07-22 16:55:02 -04001081 };
Mike Kleind3184892020-07-22 17:39:20 -05001082 wx[row] = dot(ru, tmpx);
1083 wy[row] = dot(ru, tmpy);
Mike Reed3d30ca62020-07-22 16:55:02 -04001084 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001085
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001086 skvm::Color c;
1087 c.r = c.g = c.b = c.a = p->splat(0.0f);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001088
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001089 skvm::F32 sy = local.y - 1.5f;
1090 for (int j = 0; j < 4; j++, sy += 1.0f) {
1091 skvm::F32 sx = local.x - 1.5f;
1092 for (int i = 0; i < 4; i++, sx += 1.0f) {
1093 skvm::Color s = sample_texel(u, sx,sy);
1094 skvm::F32 w = wx[i] * wy[j];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001095
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001096 c.r += s.r * w;
1097 c.g += s.g * w;
1098 c.b += s.b * w;
1099 c.a += s.a * w;
1100 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001101 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001102 return c;
Mike Reed3d58d5a2020-11-23 13:32:36 -05001103 } else if (sampling.fFilter == SkFilterMode::kLinear) {
1104 // Our four sample points are the corners of a logical 1x1 pixel
1105 // box surrounding (x,y) at (0.5,0.5) off-center.
1106 skvm::F32 left = local.x - 0.5f,
1107 top = local.y - 0.5f,
1108 right = local.x + 0.5f,
1109 bottom = local.y + 0.5f;
1110
1111 // The fractional parts of right and bottom are our lerp factors in x and y respectively.
1112 skvm::F32 fx = fract(right ),
1113 fy = fract(bottom);
1114
1115 return lerp(lerp(sample_texel(u, left,top ), sample_texel(u, right,top ), fx),
1116 lerp(sample_texel(u, left,bottom), sample_texel(u, right,bottom), fx), fy);
1117 } else {
1118 SkASSERT(sampling.fFilter == SkFilterMode::kNearest);
1119 return sample_texel(u, local.x,local.y);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001120 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001121 };
1122
1123 skvm::Color c = sample_level(*upper, upperInv, upperLocal);
1124 if (lower) {
1125 auto lowerInv = post_scale(lower->dimensions(), baseInv);
1126 auto lowerLocal = SkShaderBase::ApplyMatrix(p, lowerInv, origLocal, uniforms);
1127 // lower * weight + upper * (1 - weight)
1128 c = lerp(c,
1129 sample_level(*lower, lowerInv, lowerLocal),
1130 p->uniformF(uniforms->pushF(lowerWeight)));
Mike Klein921236b2020-02-13 11:20:54 -06001131 }
Mike Kleind67a5c32020-01-06 16:01:01 -06001132
Mike Klein921236b2020-02-13 11:20:54 -06001133 // If the input is opaque and we're not in decal mode, that means the output is too.
1134 // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas.
1135 if (input_is_opaque
1136 && fTileModeX != SkTileMode::kDecal
1137 && fTileModeY != SkTileMode::kDecal) {
Mike Reed6352f002020-03-14 23:30:10 -04001138 c.a = p->splat(1.0f);
Mike Klein921236b2020-02-13 11:20:54 -06001139 }
1140
Mike Klein913a6f52020-03-23 11:06:25 -05001141 // Alpha-only images get their color from the paint (already converted to dst color space).
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001142 SkColorSpace* cs = upper->colorSpace();
1143 SkAlphaType at = upper->alphaType();
1144 if (SkColorTypeIsAlphaOnly(upper->colorType())) {
Mike Klein913a6f52020-03-23 11:06:25 -05001145 c.r = paint.r;
1146 c.g = paint.g;
1147 c.b = paint.b;
1148
1149 cs = dst.colorSpace();
1150 at = kUnpremul_SkAlphaType;
1151 }
1152
Mike Reed3d58d5a2020-11-23 13:32:36 -05001153 if (sampling.fUseCubic) {
Mike Klein5c660e02020-01-08 11:36:35 -06001154 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reedf3b9a302020-04-01 13:18:02 -04001155 c.a = clamp01(c.a);
Mike Klein3f83bfd2020-01-07 12:01:50 -06001156
Mike Klein913a6f52020-03-23 11:06:25 -05001157 skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul)
Mike Klein3f83bfd2020-01-07 12:01:50 -06001158 ? p->splat(1.0f)
Mike Reed6352f002020-03-14 23:30:10 -04001159 : c.a;
Mike Reedf3b9a302020-04-01 13:18:02 -04001160 c.r = clamp(c.r, 0.0f, limit);
1161 c.g = clamp(c.g, 0.0f, limit);
1162 c.b = clamp(c.b, 0.0f, limit);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001163 }
Mike Klein4bc86d52020-01-06 18:39:42 -06001164
Mike Kleinef0fa432020-07-29 12:35:45 -05001165 return SkColorSpaceXformSteps{cs,at, dst.colorSpace(),dst.alphaType()}.program(p, uniforms, c);
Mike Kleinf6a715b2019-12-30 15:24:18 -06001166}