blob: 556958b4ba2610bc173053b271d7a98a30660653 [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) {
26 const float scale = 1.0f/18;
27 B *= scale;
28 C *= scale;
29 return SkM44( 3*B, -9*B - 18*C, 9*B + 36*C, -3*B - 18*C,
30 1 - 6*B, 0, -3 + 36*B + 18*C, 2 - 27*B - 18*C,
31 3*B, 9*B + 18*C, 3 - 45*B - 36*C, -2 + 27*B + 18*C,
32 0, 0, -18*C, 3*B + 18*C);
33}
34
Mike Reed587d0822017-06-23 16:49:12 -040035/**
36 * We are faster in clamp, so always use that tiling when we can.
37 */
Mike Reedfae8fce2019-04-03 10:27:45 -040038static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040039 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040040#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
41 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
42 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040043 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040044#else
Mike Reedfae8fce2019-04-03 10:27:45 -040045 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040046#endif
Mike Reed587d0822017-06-23 16:49:12 -040047}
48
Mike Klein1f313092018-01-03 10:30:21 -050049SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040050 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -050051 const SkMatrix* localMatrix,
Mike Reed9290d012020-06-11 16:56:06 -040052 FilterEnum filtering,
Mike Klein1f313092018-01-03 10:30:21 -050053 bool clampAsIfUnpremul)
54 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070055 , fImage(std::move(img))
Mike Reed587d0822017-06-23 16:49:12 -040056 , fTileModeX(optimize(tmx, fImage->width()))
57 , fTileModeY(optimize(tmy, fImage->height()))
Mike Reedf8a6b5b2020-07-10 08:36:42 -040058 , fFilterEnum(filtering)
Mike Klein1f313092018-01-03 10:30:21 -050059 , fClampAsIfUnpremul(clampAsIfUnpremul)
Mike Reed3d30ca62020-07-22 16:55:02 -040060 , fFilterOptions({}) // ignored
Mike Reedf8a6b5b2020-07-10 08:36:42 -040061{
62 SkASSERT(filtering != kUseFilterOptions);
63}
64
65SkImageShader::SkImageShader(sk_sp<SkImage> img,
66 SkTileMode tmx, SkTileMode tmy,
67 const SkFilterOptions& options,
68 const SkMatrix* localMatrix)
69 : INHERITED(localMatrix)
70 , fImage(std::move(img))
71 , fTileModeX(optimize(tmx, fImage->width()))
72 , fTileModeY(optimize(tmy, fImage->height()))
73 , fFilterEnum(FilterEnum::kUseFilterOptions)
74 , fClampAsIfUnpremul(false)
75 , fFilterOptions(options)
Mike Reed3d30ca62020-07-22 16:55:02 -040076{}
77
78SkImageShader::SkImageShader(sk_sp<SkImage> img,
79 SkTileMode tmx, SkTileMode tmy,
80 SkImage::CubicResampler cubic,
81 const SkMatrix* localMatrix)
82 : INHERITED(localMatrix)
83 , fImage(std::move(img))
84 , fTileModeX(optimize(tmx, fImage->width()))
85 , fTileModeY(optimize(tmy, fImage->height()))
86 , fFilterEnum(FilterEnum::kUseCubicResampler)
87 , fClampAsIfUnpremul(false)
88 , fFilterOptions({}) // ignored
89 , fCubic(cubic)
reed856e9d92015-09-30 12:21:45 -070090{}
91
Mike Klein1f313092018-01-03 10:30:21 -050092// fClampAsIfUnpremul is always false when constructed through public APIs,
93// so there's no need to read or write it here.
94
reed60c9b582016-04-03 09:11:13 -070095sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
Mike Reede25b4472019-04-02 17:49:12 -040096 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
97 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Reed9290d012020-06-11 16:56:06 -040098
Mike Reedf8a6b5b2020-07-10 08:36:42 -040099 FilterEnum fe = kInheritFromPaint;
100 if (!buffer.isVersionLT(SkPicturePriv::kFilterEnumInImageShader_Version)) {
Mike Reed3d30ca62020-07-22 16:55:02 -0400101 fe = buffer.read32LE<FilterEnum>(kLast);
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400102 }
103
Mike Reed3d30ca62020-07-22 16:55:02 -0400104 SkFilterOptions fo{ SkSamplingMode::kNearest, SkMipmapMode::kNone };
105 SkImage::CubicResampler cubic{};
106
107 if (buffer.isVersionLT(SkPicturePriv::kCubicResamplerImageShader_Version)) {
108 if (!buffer.isVersionLT(SkPicturePriv::kFilterOptionsInImageShader_Version)) {
109 fo.fSampling = buffer.read32LE<SkSamplingMode>(SkSamplingMode::kLinear);
110 fo.fMipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
111 }
112 } else {
113 switch (fe) {
114 case kUseFilterOptions:
115 fo.fSampling = buffer.read32LE<SkSamplingMode>(SkSamplingMode::kLinear);
116 fo.fMipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear);
117 break;
118 case kUseCubicResampler:
119 cubic.B = buffer.readScalar();
120 cubic.C = buffer.readScalar();
121 break;
122 default:
123 break;
124 }
Mike Reed9290d012020-06-11 16:56:06 -0400125 }
126
Mike Klein1f313092018-01-03 10:30:21 -0500127 SkMatrix localMatrix;
128 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -0700129 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -0700130 if (!img) {
131 return nullptr;
132 }
Mike Reed9290d012020-06-11 16:56:06 -0400133
Mike Reed3d30ca62020-07-22 16:55:02 -0400134 switch (fe) {
135 case kUseFilterOptions:
136 return SkImageShader::Make(std::move(img), tmx, tmy, fo, &localMatrix);
137 case kUseCubicResampler:
138 return SkImageShader::Make(std::move(img), tmx, tmy, cubic, &localMatrix);
139 default:
140 break;
141 }
142 return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix, fe);
reed856e9d92015-09-30 12:21:45 -0700143}
144
145void SkImageShader::flatten(SkWriteBuffer& buffer) const {
Mike Reedfae8fce2019-04-03 10:27:45 -0400146 buffer.writeUInt((unsigned)fTileModeX);
147 buffer.writeUInt((unsigned)fTileModeY);
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400148 buffer.writeUInt((unsigned)fFilterEnum);
Mike Reed3d30ca62020-07-22 16:55:02 -0400149 switch (fFilterEnum) {
150 case kUseCubicResampler:
151 buffer.writeScalar(fCubic.B);
152 buffer.writeScalar(fCubic.C);
153 break;
154 case kUseFilterOptions:
155 buffer.writeUInt((unsigned)fFilterOptions.fSampling);
156 buffer.writeUInt((unsigned)fFilterOptions.fMipmap);
157 break;
158 default:
159 break;
160 }
reed856e9d92015-09-30 12:21:45 -0700161 buffer.writeMatrix(this->getLocalMatrix());
reed6b2d7ac2016-08-11 06:42:26 -0700162 buffer.writeImage(fImage.get());
Mike Klein1f313092018-01-03 10:30:21 -0500163 SkASSERT(fClampAsIfUnpremul == false);
reed856e9d92015-09-30 12:21:45 -0700164}
165
166bool SkImageShader::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -0400167 return fImage->isOpaque() &&
168 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
reed856e9d92015-09-30 12:21:45 -0700169}
170
Mike Reede92aae62018-10-17 10:21:51 -0400171#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malitaaf2769d2018-04-04 13:46:35 -0400172static bool legacy_shader_can_handle(const SkMatrix& inv) {
Mike Reed81063112020-06-09 17:36:33 -0400173 SkASSERT(!inv.hasPerspective());
Mike Klein37bc8f92019-10-21 13:10:07 -0500174
175 // Scale+translate methods are always present, but affine might not be.
176 if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -0500177 return false;
178 }
179
180 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
181 // out of range.
182 const SkScalar max_dev_coord = 32767.0f;
Mike Klein37bc8f92019-10-21 13:10:07 -0500183 const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord));
Mike Reeda12c4192018-02-01 16:34:03 -0500184
Mike Reed1eb5ca42018-03-08 14:20:52 -0500185 // take 1/4 of max signed 32bits so we have room to subtract local values
Kevin Lubickf76da632020-01-28 10:39:56 -0500186 const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -0500187 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
Mike Klein37bc8f92019-10-21 13:10:07 -0500188 +max_fixed32dot32, +max_fixed32dot32).contains(src)) {
Mike Reeda12c4192018-02-01 16:34:03 -0500189 return false;
190 }
191
192 // legacy shader impl should be able to handle these matrices
193 return true;
194}
195
Florin Malita4aed1382017-05-25 10:38:07 -0400196SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
197 SkArenaAlloc* alloc) const {
Mike Reed3d30ca62020-07-22 16:55:02 -0400198 // we only support the old SkFilterQuality setting
199 if (fFilterEnum > kInheritFromPaint) {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400200 return nullptr;
201 }
202
203 auto quality = this->resolveFiltering(rec.fPaint->getFilterQuality());
Mike Reed9290d012020-06-11 16:56:06 -0400204
205 if (quality == kHigh_SkFilterQuality) {
Mike Reed81063112020-06-09 17:36:33 -0400206 return nullptr;
207 }
Brian Osman0e189372018-10-19 11:58:29 -0400208 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400209 return nullptr;
210 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400211 if (fImage->colorType() != kN32_SkColorType) {
212 return nullptr;
213 }
Florin Malitaaf2769d2018-04-04 13:46:35 -0400214 if (fTileModeX != fTileModeY) {
215 return nullptr;
216 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400217 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400218 return nullptr;
219 }
220
Mike Kleindac694d2018-12-18 10:13:52 -0500221 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500222 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500223 //
Mike Klein67761eb2018-12-18 10:16:53 -0500224 // We back off another bit to 32767 to make small amounts of
225 // intermediate math safe, e.g. in
226 //
227 // SkFixed fx = ...;
228 // fx = tile(fx + SK_Fixed1);
229 //
230 // we want to make sure (fx + SK_Fixed1) never overflows.
231 if (fImage-> width() > 32767 ||
232 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500233 return nullptr;
234 }
235
Florin Malitaaf2769d2018-04-04 13:46:35 -0400236 SkMatrix inv;
237 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
238 !legacy_shader_can_handle(inv)) {
239 return nullptr;
240 }
241
Mike Reed011d1662019-02-28 17:19:25 -0500242 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
243 return nullptr;
244 }
245
Mike Reed9290d012020-06-11 16:56:06 -0400246 // Send in a modified paint with different filter-quality if we don't agree with the paint
247 SkPaint modifiedPaint;
248 ContextRec modifiedRec = rec;
249 if (quality != rec.fPaint->getFilterQuality()) {
250 modifiedPaint = *rec.fPaint;
251 modifiedPaint.setFilterQuality(quality);
252 modifiedRec.fPaint = &modifiedPaint;
253 }
reed320a40d2016-08-02 06:12:06 -0700254 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed9290d012020-06-11 16:56:06 -0400255 as_IB(fImage.get()), modifiedRec, alloc);
reed856e9d92015-09-30 12:21:45 -0700256}
Mike Reede92aae62018-10-17 10:21:51 -0400257#endif
reed856e9d92015-09-30 12:21:45 -0700258
Mike Reedfae8fce2019-04-03 10:27:45 -0400259SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700260 if (texM) {
261 *texM = this->getLocalMatrix();
262 }
263 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400264 xy[0] = fTileModeX;
265 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700266 }
267 return const_cast<SkImage*>(fImage.get());
268}
269
Mike Klein1f313092018-01-03 10:30:21 -0500270sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400271 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -0500272 const SkMatrix* localMatrix,
Mike Reed9290d012020-06-11 16:56:06 -0400273 FilterEnum filtering,
Mike Klein1f313092018-01-03 10:30:21 -0500274 bool clampAsIfUnpremul) {
Mike Kleindac694d2018-12-18 10:13:52 -0500275 if (!image) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000276 return sk_make_sp<SkEmptyShader>();
reed320a40d2016-08-02 06:12:06 -0700277 }
Mike Reed9290d012020-06-11 16:56:06 -0400278 return sk_sp<SkShader>{
279 new SkImageShader(image, tmx, tmy, localMatrix, filtering, clampAsIfUnpremul)
280 };
reed856e9d92015-09-30 12:21:45 -0700281}
282
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400283sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
284 SkTileMode tmx, SkTileMode tmy,
285 const SkFilterOptions& options,
286 const SkMatrix* localMatrix) {
287 if (!image) {
288 return sk_make_sp<SkEmptyShader>();
289 }
290 return sk_sp<SkShader>{
291 new SkImageShader(image, tmx, tmy, options, localMatrix)
292 };
293}
294
Mike Reed3d30ca62020-07-22 16:55:02 -0400295sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image, SkTileMode tmx, SkTileMode tmy,
296 SkImage::CubicResampler cubic, const SkMatrix* localMatrix) {
297 if (!(cubic.B >= 0 && cubic.B <= 1 &&
298 cubic.C >= 0 && cubic.C <= 1)) {
299 return nullptr;
300 }
301 if (!image) {
302 return sk_make_sp<SkEmptyShader>();
303 }
304 return sk_sp<SkShader>{
305 new SkImageShader(image, tmx, tmy, cubic, localMatrix)
306 };
307}
308
reed856e9d92015-09-30 12:21:45 -0700309///////////////////////////////////////////////////////////////////////////////////////////////////
310
311#if SK_SUPPORT_GPU
312
Robert Phillipsb7bfbc22020-07-01 12:55:01 -0400313#include "include/gpu/GrRecordingContext.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400314#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500315#include "src/gpu/GrCaps.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400316#include "src/gpu/GrColorInfo.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400317#include "src/gpu/GrImageTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500318#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon0c243202020-06-29 14:29:25 -0400319#include "src/gpu/GrTextureAdjuster.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500320#include "src/gpu/SkGr.h"
321#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -0400322#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -0500323#include "src/gpu/effects/GrTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700324
Brian Salomonaff329b2017-08-11 09:40:37 -0400325std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500326 const GrFPArgs& args) const {
Florin Malita52f02912020-03-09 16:33:17 -0400327 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700328 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400329 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700330 return nullptr;
331 }
reed856e9d92015-09-30 12:21:45 -0700332
Brian Salomon0c243202020-06-29 14:29:25 -0400333 // This would all be much nicer with std::variant.
334 static constexpr size_t kSize = std::max({sizeof(GrYUVAImageTextureMaker),
335 sizeof(GrTextureAdjuster ),
336 sizeof(GrImageTextureMaker ),
337 sizeof(GrBitmapTextureMaker )});
338 static constexpr size_t kAlign = std::max({alignof(GrYUVAImageTextureMaker),
339 alignof(GrTextureAdjuster ),
340 alignof(GrImageTextureMaker ),
341 alignof(GrBitmapTextureMaker )});
342 std::aligned_storage_t<kSize, kAlign> storage;
343 GrTextureProducer* producer = nullptr;
344 SkScopeExit destroyProducer([&producer]{ if (producer) { producer->~GrTextureProducer(); } });
345
346 uint32_t pinnedUniqueID;
347 SkBitmap bm;
348 if (as_IB(fImage)->isYUVA()) {
349 producer = new (&storage) GrYUVAImageTextureMaker(args.fContext, fImage.get());
350 } else if (GrSurfaceProxyView view =
351 as_IB(fImage)->refPinnedView(args.fContext, &pinnedUniqueID)) {
352 GrColorInfo colorInfo;
353 if (args.fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
354 SkASSERT(fImage->colorType() == kRGBA_8888_SkColorType);
355 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, fImage->alphaType(),
356 fImage->refColorSpace());
357 } else {
358 colorInfo = fImage->imageInfo().colorInfo();
359 }
360 producer = new (&storage)
361 GrTextureAdjuster(args.fContext, std::move(view), colorInfo, pinnedUniqueID);
362 } else if (fImage->isLazyGenerated()) {
363 producer = new (&storage)
364 GrImageTextureMaker(args.fContext, fImage.get(), GrImageTexGenPolicy::kDraw);
365 } else if (as_IB(fImage)->getROPixels(&bm)) {
366 producer =
367 new (&storage) GrBitmapTextureMaker(args.fContext, bm, GrImageTexGenPolicy::kDraw);
368 } else {
369 return nullptr;
370 }
Brian Salomon694ec492020-04-14 13:39:31 -0400371 GrSamplerState::WrapMode wmX = SkTileModeToWrapMode(fTileModeX),
372 wmY = SkTileModeToWrapMode(fTileModeY);
Brian Salomon0c243202020-06-29 14:29:25 -0400373 // Must set wrap and filter on the sampler before requesting a texture. In two places
374 // below we check the matrix scale factors to determine how to interpret the filter
375 // quality setting. This completely ignores the complexity of the drawVertices case
376 // where explicit local coords are provided by the caller.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400377 bool sharpen = args.fContext->priv().options().fSharpenMipmappedTextures;
Brian Salomonf7353512020-07-22 19:26:48 -0400378 GrSamplerState::Filter fm;
379 GrSamplerState::MipmapMode mm;
380 bool bicubic;
381 if (fFilterEnum == kUseFilterOptions) {
382 bicubic = false;
383 switch (fFilterOptions.fSampling) {
384 case SkSamplingMode::kNearest: fm = GrSamplerState::Filter::kNearest; break;
385 case SkSamplingMode::kLinear : fm = GrSamplerState::Filter::kLinear ; break;
386 }
387 switch (fFilterOptions.fMipmap) {
388 case SkMipmapMode::kNone : mm = GrSamplerState::MipmapMode::kNone ; break;
389 case SkMipmapMode::kNearest: mm = GrSamplerState::MipmapMode::kNearest; break;
390 case SkMipmapMode::kLinear : mm = GrSamplerState::MipmapMode::kLinear ; break;
391 }
392 } else {
393 std::tie(fm, mm, bicubic) =
394 GrInterpretFilterQuality(fImage->dimensions(),
395 this->resolveFiltering(args.fFilterQuality),
396 args.fMatrixProvider.localToDevice(),
397 *lm,
398 sharpen);
399 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400400 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400401 if (bicubic) {
Brian Salomon0ea33072020-07-14 10:43:42 -0400402 fp = producer->createBicubicFragmentProcessor(lmInverse, nullptr, nullptr, wmX, wmY);
403 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400404 fp = producer->createFragmentProcessor(lmInverse, nullptr, nullptr, {wmX, wmY, fm, mm});
Brian Salomon0ea33072020-07-14 10:43:42 -0400405 }
Brian Osmanf48f76e2020-07-15 16:04:17 -0400406 if (fp) {
John Stilesf743d4e2020-07-23 11:35:08 -0400407 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Brian Osmanf48f76e2020-07-15 16:04:17 -0400408 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400409 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->colorSpace(), producer->alphaType(),
410 args.fDstColorInfo->colorSpace(), kPremul_SkAlphaType);
Brian Salomon0c243202020-06-29 14:29:25 -0400411 if (!fp) {
reed856e9d92015-09-30 12:21:45 -0700412 return nullptr;
413 }
Greg Danielc594e622019-10-15 14:01:49 -0400414 bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType());
Robert Phillipsb726d582017-03-09 16:36:32 -0500415 if (isAlphaOnly) {
Brian Salomon0c243202020-06-29 14:29:25 -0400416 return fp;
Brian Salomonc0d79e52019-04-10 15:02:11 -0400417 } else if (args.fInputColorIsOpaque) {
Brian Salomon0c243202020-06-29 14:29:25 -0400418 return GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700419 }
Brian Salomon0c243202020-06-29 14:29:25 -0400420 return GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
reed856e9d92015-09-30 12:21:45 -0700421}
422
423#endif
reed320a40d2016-08-02 06:12:06 -0700424
425///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500426#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700427
Mike Reede25b4472019-04-02 17:49:12 -0400428sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy,
429 const SkMatrix* localMatrix, SkCopyPixelsMode cpm) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000430 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm),
Mike Reed9290d012020-06-11 16:56:06 -0400431 tmx, tmy, localMatrix, SkImageShader::kInheritFromPaint);
reed320a40d2016-08-02 06:12:06 -0700432}
433
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400434sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400435 SkTileMode tmx, SkTileMode tmy,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400436 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
437 auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode);
438 if (!s) {
439 return nullptr;
440 }
441 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
442 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
443 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
444 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400445 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400446 }
447 return s;
448}
449
Brian Salomon23356442018-11-30 15:33:19 -0500450void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700451
Mike Reed9318a6c2019-08-16 16:16:25 -0400452class SkImageStageUpdater : public SkStageUpdater {
453public:
Mike Reed8845c372019-12-19 13:22:08 -0500454 SkImageStageUpdater(const SkImageShader* shader, bool usePersp)
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500455 : fShader(shader)
456 , fUsePersp(usePersp || as_SB(shader)->getLocalMatrix().hasPerspective())
Mike Reed8845c372019-12-19 13:22:08 -0500457 {}
Mike Reed9318a6c2019-08-16 16:16:25 -0400458
Mike Reed8845c372019-12-19 13:22:08 -0500459 const SkImageShader* fShader;
460 const bool fUsePersp; // else use affine
461
462 // large enough for perspective, though often we just use 2x3
463 float fMatrixStorage[9];
Mike Reed9318a6c2019-08-16 16:16:25 -0400464
465#if 0 // TODO: when we support mipmaps
466 SkRasterPipeline_GatherCtx* fGather;
467 SkRasterPipeline_TileCtx* fLimitX;
468 SkRasterPipeline_TileCtx* fLimitY;
469 SkRasterPipeline_DecalTileCtx* fDecal;
470#endif
471
Mike Reed8845c372019-12-19 13:22:08 -0500472 void append_matrix_stage(SkRasterPipeline* p) {
473 if (fUsePersp) {
474 p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage);
475 } else {
476 p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage);
477 }
478 }
479
Mike Reed9318a6c2019-08-16 16:16:25 -0400480 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
481 SkMatrix matrix;
Mike Reed8845c372019-12-19 13:22:08 -0500482 if (fShader->computeTotalInverse(ctm, localM, &matrix)) {
483 if (fUsePersp) {
484 matrix.get9(fMatrixStorage);
485 } else {
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500486 // if we get here, matrix should be affine. If it isn't, then defensively we
487 // won't draw (by returning false), but we should work to never let this
488 // happen (i.e. better preflight by the caller to know ahead of time that we
489 // may encounter perspective, either in the CTM, or in the localM).
490 //
491 // See https://bugs.chromium.org/p/skia/issues/detail?id=10004
492 //
493 if (!matrix.asAffine(fMatrixStorage)) {
494 SkASSERT(false);
495 return false;
496 }
Mike Reed8845c372019-12-19 13:22:08 -0500497 }
498 return true;
499 }
500 return false;
Mike Reed9318a6c2019-08-16 16:16:25 -0400501 }
502};
503
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600504static void tweak_quality_and_inv_matrix(SkFilterQuality* quality, SkMatrix* matrix) {
505 // When the matrix is just an integer translate, bilerp == nearest neighbor.
506 if (*quality == kLow_SkFilterQuality &&
507 matrix->getType() <= SkMatrix::kTranslate_Mask &&
508 matrix->getTranslateX() == (int)matrix->getTranslateX() &&
509 matrix->getTranslateY() == (int)matrix->getTranslateY()) {
510 *quality = kNone_SkFilterQuality;
511 }
512
513 // See skia:4649 and the GM image_scale_aligned.
514 if (*quality == kNone_SkFilterQuality) {
515 if (matrix->getScaleX() >= 0) {
516 matrix->setTranslateX(nextafterf(matrix->getTranslateX(),
517 floorf(matrix->getTranslateX())));
518 }
519 if (matrix->getScaleY() >= 0) {
520 matrix->setTranslateY(nextafterf(matrix->getTranslateY(),
521 floorf(matrix->getTranslateY())));
522 }
523 }
524}
525
Mike Reed9318a6c2019-08-16 16:16:25 -0400526bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400527 SkFilterQuality quality;
528 switch (fFilterEnum) {
Mike Reed3d30ca62020-07-22 16:55:02 -0400529 case FilterEnum::kUseFilterOptions:
530 case FilterEnum::kUseCubicResampler:
531 return false; // TODO: support these in stages
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400532 case FilterEnum::kInheritFromPaint:
533 quality = rec.fPaint.getFilterQuality();
534 break;
535 default:
536 quality = (SkFilterQuality)fFilterEnum;
537 break;
538 }
Mike Reed9290d012020-06-11 16:56:06 -0400539
540 if (updater && quality == kMedium_SkFilterQuality) {
Mike Reed8845c372019-12-19 13:22:08 -0500541 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400542 return false;
543 }
544
Mike Reed1d8c42e2017-08-29 14:58:19 -0400545 SkRasterPipeline* p = rec.fPipeline;
546 SkArenaAlloc* alloc = rec.fAlloc;
547
Florin Malita7558e4d2018-02-07 10:05:53 -0500548 SkMatrix matrix;
Brian Osman9aaec362020-05-08 14:54:37 -0400549 if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500550 return false;
551 }
Mike Klein06a65e22016-11-17 12:39:09 -0500552
Mike Reed64acf4f2019-08-01 15:35:20 -0400553 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
554 matrix, quality, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500555 if (!state) {
556 return false;
557 }
558
559 const SkPixmap& pm = state->pixmap();
560 matrix = state->invMatrix();
561 quality = state->quality();
562 auto info = pm.info();
563
Mike Kleine8de0242018-03-10 12:37:11 -0500564 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400565
566 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500567 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400568 } else {
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600569 tweak_quality_and_inv_matrix(&quality, &matrix);
Mike Reed9318a6c2019-08-16 16:16:25 -0400570 p->append_matrix(alloc, matrix);
571 }
Mike Klein06a65e22016-11-17 12:39:09 -0500572
Mike Kleinb11ab572018-10-24 06:42:14 -0400573 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500574 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400575 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400576 gather->width = pm.width();
577 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400578
Mike Kleinb11ab572018-10-24 06:42:14 -0400579 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
580 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400581 limit_x->scale = pm.width();
582 limit_x->invScale = 1.0f / pm.width();
583 limit_y->scale = pm.height();
584 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400585
Mike Kleinb11ab572018-10-24 06:42:14 -0400586 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400587 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
588 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400589 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500590 decal_ctx->limit_x = limit_x->scale;
591 decal_ctx->limit_y = limit_y->scale;
592 }
593
Mike Reed9318a6c2019-08-16 16:16:25 -0400594#if 0 // TODO: when we support kMedium
595 if (updator && (quality == kMedium_SkFilterQuality)) {
596 // if we change levels in mipmap, we need to update the scales (and invScales)
597 updator->fGather = gather;
598 updator->fLimitX = limit_x;
599 updator->fLimitY = limit_y;
600 updator->fDecal = decal_ctx;
601 }
602#endif
603
Mike Kleinb04c3522016-11-28 11:55:58 -0500604 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500605 if (decal_x_and_y) {
606 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
607 } else {
608 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400609 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
610 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
611 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
612 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500613 }
614 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400615 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
616 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
617 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
618 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500619 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500620 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500621
Mike Kleinac568a92018-01-25 09:09:32 -0500622 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500623 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500624 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400625 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
626 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500627 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
628 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400629 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
630 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
631 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500632 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
633 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400634 case kR16G16B16A16_unorm_SkColorType:
635 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600636 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500637 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400638 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500639
Mike Kleinb1df5e52018-10-17 17:06:03 -0400640 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
641 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400642
Mike Kleinac568a92018-01-25 09:09:32 -0500643 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
644 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400645
Mike Kleinf7eb0542020-02-11 12:19:08 -0600646 case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
647 p->append(SkRasterPipeline::swap_rb ); break;
648
Mike Kleinac568a92018-01-25 09:09:32 -0500649 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
650 p->append(SkRasterPipeline::force_opaque ); break;
651
Mike Kleinf7eb0542020-02-11 12:19:08 -0600652 case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
653 p->append(SkRasterPipeline::force_opaque );
654 p->append(SkRasterPipeline::swap_rb ); break;
655
Mike Klein1a3eb522018-10-18 10:11:00 -0400656 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
657 p->append(SkRasterPipeline::swap_rb ); break;
658
Mike Kleinb70990e2019-02-28 10:03:27 -0600659 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500660 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500661 if (decal_ctx) {
662 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
663 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500664 };
665
Mike Klein1fa9c432017-12-11 09:59:47 -0500666 auto append_misc = [&] {
Mike Klein6a9f1c42020-01-02 23:23:42 -0600667 SkColorSpace* cs = info.colorSpace();
668 SkAlphaType at = info.alphaType();
669
670 // Color for A8 images comes from the paint. TODO: all alpha images? none?
671 if (info.colorType() == kAlpha_8_SkColorType) {
672 SkColor4f rgb = rec.fPaint.getColor4f();
673 p->append_set_rgb(alloc, rgb);
674
Mike Klein6a9f1c42020-01-02 23:23:42 -0600675 cs = sk_srgb_singleton();
676 at = kUnpremul_SkAlphaType;
677 }
678
Mike Klein059e0432020-01-02 16:32:38 -0600679 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Klein250138d2020-01-02 09:39:26 -0600680 if (quality == kHigh_SkFilterQuality) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500681 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600682 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
683 ? SkRasterPipeline::clamp_1
684 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500685 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000686
Mike Klein059e0432020-01-02 16:32:38 -0600687 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
688 alloc->make<SkColorSpaceXformSteps>(cs, at,
689 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500690 ->apply(p);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000691
Mike Klein1fa9c432017-12-11 09:59:47 -0500692 return true;
693 };
694
Mike Reed9318a6c2019-08-16 16:16:25 -0400695 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400696 auto ct = info.colorType();
697 if (true
698 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
699 && quality == kLow_SkFilterQuality
Mike Reedfae8fce2019-04-03 10:27:45 -0400700 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500701
702 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400703 if (ct == kBGRA_8888_SkColorType) {
704 p->append(SkRasterPipeline::swap_rb);
705 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500706 return append_misc();
707 }
Mike Reed78eedba2019-07-31 16:39:15 -0400708 if (true
Mike Klein01005622019-08-13 12:22:17 -0400709 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
710 && quality == kLow_SkFilterQuality
711 && fTileModeX != SkTileMode::kDecal // TODO decal too?
712 && fTileModeY != SkTileMode::kDecal) {
713
714 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
715 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
716 ctx->ct = ct;
717 ctx->tileX = fTileModeX;
718 ctx->tileY = fTileModeY;
719 ctx->invWidth = 1.0f / ctx->width;
720 ctx->invHeight = 1.0f / ctx->height;
721 p->append(SkRasterPipeline::bilinear, ctx);
722 return append_misc();
723 }
724 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400725 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
726 && quality == kHigh_SkFilterQuality
727 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
728
729 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
730 if (ct == kBGRA_8888_SkColorType) {
731 p->append(SkRasterPipeline::swap_rb);
732 }
733 return append_misc();
734 }
Mike Klein01005622019-08-13 12:22:17 -0400735 if (true
736 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
737 && quality == kHigh_SkFilterQuality
738 && fTileModeX != SkTileMode::kDecal // TODO decal too?
739 && fTileModeY != SkTileMode::kDecal) {
740
741 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
742 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
743 ctx->ct = ct;
744 ctx->tileX = fTileModeX;
745 ctx->tileY = fTileModeY;
746 ctx->invWidth = 1.0f / ctx->width;
747 ctx->invHeight = 1.0f / ctx->height;
748 p->append(SkRasterPipeline::bicubic, ctx);
749 return append_misc();
750 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500751
Mike Kleinb11ab572018-10-24 06:42:14 -0400752 SkRasterPipeline_SamplerCtx* sampler = nullptr;
Mike Klein0a904492017-04-12 12:52:48 -0400753 if (quality != kNone_SkFilterQuality) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400754 sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400755 }
756
Mike Kleinb0b17d12016-12-09 16:25:44 -0500757 auto sample = [&](SkRasterPipeline::StockStage setup_x,
758 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400759 p->append(setup_x, sampler);
760 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500761 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400762 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500763 };
764
Mike Kleinf7f883b2016-11-21 15:09:45 -0500765 if (quality == kNone_SkFilterQuality) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500766 append_tiling_and_gather();
Mike Kleinb0b17d12016-12-09 16:25:44 -0500767 } else if (quality == kLow_SkFilterQuality) {
Mike Klein0a904492017-04-12 12:52:48 -0400768 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500769
770 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
771 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
772 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
773 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
774
775 p->append(SkRasterPipeline::move_dst_src);
Mike Klein1fa9c432017-12-11 09:59:47 -0500776
Mike Klein46e66a22016-11-21 16:19:34 -0500777 } else {
Mike Klein250138d2020-01-02 09:39:26 -0600778 SkASSERT(quality == kHigh_SkFilterQuality);
Mike Klein0a904492017-04-12 12:52:48 -0400779 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500780
781 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
782 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
783 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
784 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
785
786 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
787 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
788 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
789 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
790
791 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
792 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
793 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
794 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
795
796 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
797 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
798 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
799 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
800
Mike Kleinb04c3522016-11-28 11:55:58 -0500801 p->append(SkRasterPipeline::move_dst_src);
Mike Klein06a65e22016-11-17 12:39:09 -0500802 }
803
Mike Klein1fa9c432017-12-11 09:59:47 -0500804 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500805}
Mike Reed9318a6c2019-08-16 16:16:25 -0400806
807bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
808 return this->doStages(rec, nullptr);
809}
810
811SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Brian Osman9aaec362020-05-08 14:54:37 -0400812 bool usePersp = rec.fMatrixProvider.localToDevice().hasPerspective();
Mike Reed8845c372019-12-19 13:22:08 -0500813 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400814 return this->doStages(rec, updater) ? updater : nullptr;
815}
816
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400817enum class SamplingEnum {
818 kNearest,
819 kLinear,
820 kBicubic,
821};
822
Brian Osman5aaaeea2020-06-22 14:26:03 -0400823skvm::Color SkImageShader::onProgram(skvm::Builder* p,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400824 skvm::Coord device, skvm::Coord origLocal, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500825 const SkMatrixProvider& matrices, const SkMatrix* localM,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400826 SkFilterQuality paintQuality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500827 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400828 SkMatrix baseInv;
829 if (!this->computeTotalInverse(matrices.localToDevice(), localM, &baseInv)) {
Mike Reed6352f002020-03-14 23:30:10 -0400830 return {};
Mike Kleinf6a715b2019-12-30 15:24:18 -0600831 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400832 baseInv.normalizePerspective();
Mike Kleinf6a715b2019-12-30 15:24:18 -0600833
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400834 const SkPixmap *upper = nullptr,
835 *lower = nullptr;
836 SkMatrix upperInv;
837 float lowerWeight = 0;
838 SamplingEnum sampling = (SamplingEnum)fFilterOptions.fSampling;
839
840 auto post_scale = [&](SkISize level, const SkMatrix& base) {
841 return SkMatrix::Scale(SkIntToScalar(level.width()) / fImage->width(),
842 SkIntToScalar(level.height()) / fImage->height())
843 * base;
844 };
845
846 if (fFilterEnum == kUseFilterOptions) {
847 auto* access = alloc->make<SkMipmapAccessor>(as_IB(fImage.get()), baseInv,
848 fFilterOptions.fMipmap);
849 upper = &access->level();
850 upperInv = post_scale(upper->dimensions(), baseInv);
851 lowerWeight = access->lowerWeight();
852 if (lowerWeight > 0) {
853 lower = &access->lowerLevel();
854 }
Mike Reed3d30ca62020-07-22 16:55:02 -0400855 } else if (fFilterEnum == kUseCubicResampler){
856 auto* access = alloc->make<SkMipmapAccessor>(as_IB(fImage.get()), baseInv,
857 SkMipmapMode::kNone);
858 upper = &access->level();
859 upperInv = post_scale(upper->dimensions(), baseInv);
860 sampling = SamplingEnum::kBicubic;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400861 } else {
862 // Convert from the filter-quality enum to our working description:
863 // sampling : nearest, bilerp, bicubic
864 // miplevel(s) and associated matrices
865 //
866 SkFilterQuality quality = paintQuality;
867 if (fFilterEnum != kInheritFromPaint) {
868 quality = (SkFilterQuality)fFilterEnum;
869 }
870
871 // We use RequestBitmap() to make sure our SkBitmapController::State lives in the alloc.
872 // This lets the SkVMBlitter hang on to this state and keep our image alive.
873 auto state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), baseInv, quality, alloc);
874 if (!state) {
875 return {};
876 }
877 upper = &state->pixmap();
878 upperInv = state->invMatrix();
879
880 quality = state->quality();
881 tweak_quality_and_inv_matrix(&quality, &upperInv);
882 switch (quality) {
883 case kNone_SkFilterQuality: sampling = SamplingEnum::kNearest; break;
884 case kLow_SkFilterQuality: sampling = SamplingEnum::kLinear; break;
885 case kMedium_SkFilterQuality: sampling = SamplingEnum::kLinear; break;
886 case kHigh_SkFilterQuality: sampling = SamplingEnum::kBicubic; break;
887 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600888 }
Mike Kleinf6a715b2019-12-30 15:24:18 -0600889
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400890 skvm::Coord upperLocal = SkShaderBase::ApplyMatrix(p, upperInv, origLocal, uniforms);
Mike Klein6dbd7ff2020-01-06 11:50:37 -0600891
Mike Klein03c932c2020-07-13 09:07:42 -0500892 // Bail out if sample() can't yet handle our image's color type(s).
893 skvm::PixelFormat unused;
Mike Klein9662fd62020-07-16 15:11:27 -0500894 if (true && !SkColorType_to_PixelFormat(upper->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500895 return {};
896 }
Mike Klein9662fd62020-07-16 15:11:27 -0500897 if (lower && !SkColorType_to_PixelFormat(lower->colorType(), &unused)) {
Mike Klein03c932c2020-07-13 09:07:42 -0500898 return {};
Mike Klein25480bf2020-01-06 14:01:58 -0600899 }
900
Mike Klein921236b2020-02-13 11:20:54 -0600901 // We can exploit image opacity to skip work unpacking alpha channels.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400902 const bool input_is_opaque = SkAlphaTypeIsOpaque(upper->alphaType())
903 || SkColorTypeIsAlwaysOpaque(upper->colorType());
Mike Klein921236b2020-02-13 11:20:54 -0600904
Mike Klein03d89ef2020-01-14 17:18:29 -0600905 // Each call to sample() will try to rewrite the same uniforms over and over,
906 // so remember where we start and reset back there each time. That way each
907 // sample() call uses the same uniform offsets.
Mike Klein03d89ef2020-01-14 17:18:29 -0600908
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400909 auto compute_clamp_limit = [&](float limit) {
910 // Subtract an ulp so the upper clamp limit excludes limit itself.
911 int bits;
912 memcpy(&bits, &limit, 4);
913 return p->uniformF(uniforms->push(bits-1));
914 };
Mike Klein03d89ef2020-01-14 17:18:29 -0600915
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400916 // Except in the simplest case (no mips, no filtering), we reference uniforms
917 // more than once. To avoid adding/registering them multiple times, we pre-load them
918 // into a struct (just to logically group them together), based on the "current"
919 // pixmap (level of a mipmap).
920 //
921 struct Uniforms {
922 skvm::F32 w, iw, i2w,
923 h, ih, i2h;
924
925 skvm::F32 clamp_w,
926 clamp_h;
927
928 skvm::Uniform addr;
929 skvm::I32 rowBytesAsPixels;
930
Mike Klein03c932c2020-07-13 09:07:42 -0500931 skvm::PixelFormat pixelFormat; // not a uniform, but needed for each texel sample,
932 // so we store it here, since it is also dependent on
933 // the current pixmap (level).
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400934 };
935
936 auto setup_uniforms = [&](const SkPixmap& pm) -> Uniforms {
Mike Klein03c932c2020-07-13 09:07:42 -0500937 skvm::PixelFormat pixelFormat;
Mike Klein9662fd62020-07-16 15:11:27 -0500938 SkAssertResult(SkColorType_to_PixelFormat(pm.colorType(), &pixelFormat));
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400939 return {
940 p->uniformF(uniforms->pushF( pm.width())),
941 p->uniformF(uniforms->pushF(1.0f/pm.width())), // iff tileX == kRepeat
942 p->uniformF(uniforms->pushF(0.5f/pm.width())), // iff tileX == kMirror
943
944 p->uniformF(uniforms->pushF( pm.height())),
945 p->uniformF(uniforms->pushF(1.0f/pm.height())), // iff tileY == kRepeat
946 p->uniformF(uniforms->pushF(0.5f/pm.height())), // iff tileY == kMirror
947
948 compute_clamp_limit(pm. width()),
949 compute_clamp_limit(pm.height()),
950
951 uniforms->pushPtr(pm.addr()),
952 p->uniform32(uniforms->push(pm.rowBytesAsPixels())),
953
Mike Klein03c932c2020-07-13 09:07:42 -0500954 pixelFormat,
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400955 };
956 };
957
958 auto sample_texel = [&](const Uniforms& u, skvm::F32 sx, skvm::F32 sy) -> skvm::Color {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600959 // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400960 auto repeat = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I) {
Mike Reedf3b9a302020-04-01 13:18:02 -0400961 return v - floor(v * I) * S;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600962 };
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400963 auto mirror = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I2) {
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600964 // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale )
965 // {---A---} {------------------B------------------}
Mike Reedf3b9a302020-04-01 13:18:02 -0400966 skvm::F32 A = v - S,
967 B = (S + S) * floor(A * I2);
968 return abs(A - B - S);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600969 };
970 switch (fTileModeX) {
971 case SkTileMode::kDecal: /* handled after gather */ break;
972 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400973 case SkTileMode::kRepeat: sx = repeat(sx, u.w, u.iw); break;
974 case SkTileMode::kMirror: sx = mirror(sx, u.w, u.i2w); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600975 }
976 switch (fTileModeY) {
977 case SkTileMode::kDecal: /* handled after gather */ break;
978 case SkTileMode::kClamp: /* we always clamp */ break;
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400979 case SkTileMode::kRepeat: sy = repeat(sy, u.h, u.ih); break;
980 case SkTileMode::kMirror: sy = mirror(sy, u.h, u.i2h); break;
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600981 }
982
983 // Always clamp sample coordinates to [0,width), [0,height), both for memory
984 // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror.
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400985 skvm::F32 clamped_x = clamp(sx, 0, u.clamp_w),
986 clamped_y = clamp(sy, 0, u.clamp_h);
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600987
988 // Load pixels from pm.addr()[(int)sx + (int)sy*stride].
Mike Reedf3b9a302020-04-01 13:18:02 -0400989 skvm::I32 index = trunc(clamped_x) +
Mike Reedf8a6b5b2020-07-10 08:36:42 -0400990 trunc(clamped_y) * u.rowBytesAsPixels;
Mike Klein03c932c2020-07-13 09:07:42 -0500991 skvm::Color c = gather(u.pixelFormat, u.addr, index);
Mike Klein913a6f52020-03-23 11:06:25 -0500992
Mike Klein921236b2020-02-13 11:20:54 -0600993 // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it.
994 if (input_is_opaque) {
Mike Klein46874ef2020-02-13 10:17:08 -0600995 c.a = p->splat(1.0f);
996 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -0600997
998 // Mask away any pixels that we tried to sample outside the bounds in kDecal.
999 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
1000 skvm::I32 mask = p->splat(~0);
Mike Reedf3b9a302020-04-01 13:18:02 -04001001 if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); }
1002 if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); }
1003 c.r = bit_cast(p->bit_and(mask, bit_cast(c.r)));
1004 c.g = bit_cast(p->bit_and(mask, bit_cast(c.g)));
1005 c.b = bit_cast(p->bit_and(mask, bit_cast(c.b)));
1006 c.a = bit_cast(p->bit_and(mask, bit_cast(c.a)));
Mike Klein921236b2020-02-13 11:20:54 -06001007 // Notice that even if input_is_opaque, c.a might now be 0.
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001008 }
1009
1010 return c;
1011 };
1012
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001013 auto sample_level = [&](const SkPixmap& pm, const SkMatrix& inv, skvm::Coord local) {
1014 const Uniforms u = setup_uniforms(pm);
Mike Reed6352f002020-03-14 23:30:10 -04001015
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001016 if (sampling == SamplingEnum::kNearest) {
1017 return sample_texel(u, local.x,local.y);
1018 } else if (sampling == SamplingEnum::kLinear) {
1019 // Our four sample points are the corners of a logical 1x1 pixel
1020 // box surrounding (x,y) at (0.5,0.5) off-center.
1021 skvm::F32 left = local.x - 0.5f,
1022 top = local.y - 0.5f,
1023 right = local.x + 0.5f,
1024 bottom = local.y + 0.5f;
Mike Klein5c660e02020-01-08 11:36:35 -06001025
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001026 // The fractional parts of right and bottom are our lerp factors in x and y respectively.
1027 skvm::F32 fx = fract(right ),
1028 fy = fract(bottom);
Mike Klein5c660e02020-01-08 11:36:35 -06001029
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001030 return lerp(lerp(sample_texel(u, left,top ), sample_texel(u, right,top ), fx),
1031 lerp(sample_texel(u, left,bottom), sample_texel(u, right,bottom), fx), fy);
1032 } else {
1033 SkASSERT(sampling == SamplingEnum::kBicubic);
Mike Klein5c660e02020-01-08 11:36:35 -06001034
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001035 // All bicubic samples have the same fractional offset (fx,fy) from the center.
1036 // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center.
1037 skvm::F32 fx = fract(local.x + 0.5f),
1038 fy = fract(local.y + 0.5f);
Mike Reed3d30ca62020-07-22 16:55:02 -04001039 skvm::F32 wx[4],
1040 wy[4];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001041
Mike Kleind3184892020-07-22 17:39:20 -05001042 SkM44 weights = CubicResamplerMatrix(fCubic.B, fCubic.C);
Mike Reed3d30ca62020-07-22 16:55:02 -04001043
Mike Kleind3184892020-07-22 17:39:20 -05001044 auto dot = [](const skvm::F32 a[], const skvm::F32 b[]) {
1045 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
1046 };
1047 const skvm::F32 tmpx[] = { p->splat(1.0f), fx, fx*fx, fx*fx*fx };
1048 const skvm::F32 tmpy[] = { p->splat(1.0f), fy, fy*fy, fy*fy*fy };
Mike Reed3d30ca62020-07-22 16:55:02 -04001049
Mike Kleind3184892020-07-22 17:39:20 -05001050 for (int row = 0; row < 4; ++row) {
1051 SkV4 r = weights.row(row);
1052 skvm::F32 ru[] = {
1053 p->uniformF(uniforms->pushF(r[0])),
1054 p->uniformF(uniforms->pushF(r[1])),
1055 p->uniformF(uniforms->pushF(r[2])),
1056 p->uniformF(uniforms->pushF(r[3])),
Mike Reed3d30ca62020-07-22 16:55:02 -04001057 };
Mike Kleind3184892020-07-22 17:39:20 -05001058 wx[row] = dot(ru, tmpx);
1059 wy[row] = dot(ru, tmpy);
Mike Reed3d30ca62020-07-22 16:55:02 -04001060 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001061
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001062 skvm::Color c;
1063 c.r = c.g = c.b = c.a = p->splat(0.0f);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001064
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001065 skvm::F32 sy = local.y - 1.5f;
1066 for (int j = 0; j < 4; j++, sy += 1.0f) {
1067 skvm::F32 sx = local.x - 1.5f;
1068 for (int i = 0; i < 4; i++, sx += 1.0f) {
1069 skvm::Color s = sample_texel(u, sx,sy);
1070 skvm::F32 w = wx[i] * wy[j];
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001071
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001072 c.r += s.r * w;
1073 c.g += s.g * w;
1074 c.b += s.b * w;
1075 c.a += s.a * w;
1076 }
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001077 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001078 return c;
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001079 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001080 };
1081
1082 skvm::Color c = sample_level(*upper, upperInv, upperLocal);
1083 if (lower) {
1084 auto lowerInv = post_scale(lower->dimensions(), baseInv);
1085 auto lowerLocal = SkShaderBase::ApplyMatrix(p, lowerInv, origLocal, uniforms);
1086 // lower * weight + upper * (1 - weight)
1087 c = lerp(c,
1088 sample_level(*lower, lowerInv, lowerLocal),
1089 p->uniformF(uniforms->pushF(lowerWeight)));
Mike Klein921236b2020-02-13 11:20:54 -06001090 }
Mike Kleind67a5c32020-01-06 16:01:01 -06001091
Mike Klein921236b2020-02-13 11:20:54 -06001092 // If the input is opaque and we're not in decal mode, that means the output is too.
1093 // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas.
1094 if (input_is_opaque
1095 && fTileModeX != SkTileMode::kDecal
1096 && fTileModeY != SkTileMode::kDecal) {
Mike Reed6352f002020-03-14 23:30:10 -04001097 c.a = p->splat(1.0f);
Mike Klein921236b2020-02-13 11:20:54 -06001098 }
1099
Mike Klein913a6f52020-03-23 11:06:25 -05001100 // Alpha-only images get their color from the paint (already converted to dst color space).
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001101 SkColorSpace* cs = upper->colorSpace();
1102 SkAlphaType at = upper->alphaType();
1103 if (SkColorTypeIsAlphaOnly(upper->colorType())) {
Mike Klein913a6f52020-03-23 11:06:25 -05001104 c.r = paint.r;
1105 c.g = paint.g;
1106 c.b = paint.b;
1107
1108 cs = dst.colorSpace();
1109 at = kUnpremul_SkAlphaType;
1110 }
1111
Mike Reedf8a6b5b2020-07-10 08:36:42 -04001112 if (sampling == SamplingEnum::kBicubic) {
Mike Klein5c660e02020-01-08 11:36:35 -06001113 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Reedf3b9a302020-04-01 13:18:02 -04001114 c.a = clamp01(c.a);
Mike Klein3f83bfd2020-01-07 12:01:50 -06001115
Mike Klein913a6f52020-03-23 11:06:25 -05001116 skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul)
Mike Klein3f83bfd2020-01-07 12:01:50 -06001117 ? p->splat(1.0f)
Mike Reed6352f002020-03-14 23:30:10 -04001118 : c.a;
Mike Reedf3b9a302020-04-01 13:18:02 -04001119 c.r = clamp(c.r, 0.0f, limit);
1120 c.g = clamp(c.g, 0.0f, limit);
1121 c.b = clamp(c.b, 0.0f, limit);
Mike Kleinb1ff79a2020-01-07 07:53:39 -06001122 }
Mike Klein4bc86d52020-01-06 18:39:42 -06001123
Mike Reed0113d292020-03-24 16:36:37 -04001124 SkColorSpaceXformSteps steps{cs,at, dst.colorSpace(),kPremul_SkAlphaType};
1125 return steps.program(p, uniforms, c);
Mike Kleinf6a715b2019-12-30 15:24:18 -06001126}