reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Salomon | 0c24320 | 2020-06-29 14:29:25 -0400 | [diff] [blame] | 8 | #include "src/shaders/SkImageShader.h" |
| 9 | |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 10 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkColorSpacePriv.h" |
| 12 | #include "src/core/SkColorSpaceXformSteps.h" |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 13 | #include "src/core/SkMatrixPriv.h" |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 14 | #include "src/core/SkMatrixProvider.h" |
Mike Reed | 60a2ec0 | 2020-12-08 09:18:14 -0500 | [diff] [blame] | 15 | #include "src/core/SkMipmapAccessor.h" |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 16 | #include "src/core/SkOpts.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkRasterPipeline.h" |
| 18 | #include "src/core/SkReadBuffer.h" |
Mike Reed | 9223665 | 2021-02-01 13:07:32 -0500 | [diff] [blame^] | 19 | #include "src/core/SkSamplingPriv.h" |
Brian Salomon | 0c24320 | 2020-06-29 14:29:25 -0400 | [diff] [blame] | 20 | #include "src/core/SkScopeExit.h" |
Mike Klein | 8e71744 | 2020-01-07 10:22:33 -0600 | [diff] [blame] | 21 | #include "src/core/SkVM.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkWriteBuffer.h" |
| 23 | #include "src/image/SkImage_Base.h" |
| 24 | #include "src/shaders/SkBitmapProcShader.h" |
| 25 | #include "src/shaders/SkEmptyShader.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 26 | |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 27 | SkM44 SkImageShader::CubicResamplerMatrix(float B, float C) { |
Mike Reed | 3867c70 | 2020-09-01 13:28:10 -0400 | [diff] [blame] | 28 | #if 0 |
| 29 | constexpr SkM44 kMitchell = SkM44( 1.f/18.f, -9.f/18.f, 15.f/18.f, -7.f/18.f, |
| 30 | 16.f/18.f, 0.f/18.f, -36.f/18.f, 21.f/18.f, |
| 31 | 1.f/18.f, 9.f/18.f, 27.f/18.f, -21.f/18.f, |
| 32 | 0.f/18.f, 0.f/18.f, -6.f/18.f, 7.f/18.f); |
| 33 | |
| 34 | constexpr SkM44 kCatmull = SkM44(0.0f, -0.5f, 1.0f, -0.5f, |
| 35 | 1.0f, 0.0f, -2.5f, 1.5f, |
| 36 | 0.0f, 0.5f, 2.0f, -1.5f, |
| 37 | 0.0f, 0.0f, -0.5f, 0.5f); |
| 38 | |
| 39 | if (B == 1.0f/3 && C == 1.0f/3) { |
| 40 | return kMitchell; |
| 41 | } |
| 42 | if (B == 0 && C == 0.5f) { |
| 43 | return kCatmull; |
| 44 | } |
| 45 | #endif |
| 46 | return SkM44( (1.f/6)*B, -(3.f/6)*B - C, (3.f/6)*B + 2*C, - (1.f/6)*B - C, |
| 47 | 1 - (2.f/6)*B, 0, -3 + (12.f/6)*B + C, 2 - (9.f/6)*B - C, |
| 48 | (1.f/6)*B, (3.f/6)*B + C, 3 - (15.f/6)*B - 2*C, -2 + (9.f/6)*B + C, |
| 49 | 0, 0, -C, (1.f/6)*B + C); |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 52 | /** |
| 53 | * We are faster in clamp, so always use that tiling when we can. |
| 54 | */ |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 55 | static SkTileMode optimize(SkTileMode tm, int dimension) { |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 56 | SkASSERT(dimension > 0); |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 57 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 58 | // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow |
| 59 | // for transforming to clamp. |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 60 | return tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 61 | #else |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 62 | return dimension == 1 ? SkTileMode::kClamp : tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 63 | #endif |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 66 | SkImageShader::SkImageShader(sk_sp<SkImage> img, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 67 | SkTileMode tmx, SkTileMode tmy, |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 68 | const SkSamplingOptions* sampling, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 69 | const SkMatrix* localMatrix, |
| 70 | bool clampAsIfUnpremul) |
| 71 | : INHERITED(localMatrix) |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 72 | , fImage(std::move(img)) |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 73 | , fSampling(sampling ? *sampling : SkSamplingOptions()) |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 74 | , fTileModeX(optimize(tmx, fImage->width())) |
| 75 | , fTileModeY(optimize(tmy, fImage->height())) |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 76 | , fClampAsIfUnpremul(clampAsIfUnpremul) |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 77 | , fUseSamplingOptions(sampling != nullptr) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 78 | {} |
| 79 | |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 80 | // just used for legacy-unflattening |
| 81 | enum class LegacyFilterEnum { |
| 82 | kNone, |
| 83 | kLow, |
| 84 | kMedium, |
| 85 | kHigh, |
| 86 | // this is the special value for backward compatibility |
| 87 | kInheritFromPaint, |
| 88 | // this signals we should use the new SkFilterOptions |
| 89 | kUseFilterOptions, |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 90 | // use cubic and ignore FilterOptions |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 91 | kUseCubicResampler, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 92 | |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 93 | kLast = kUseCubicResampler, |
| 94 | }; |
| 95 | |
| 96 | sk_sp<SkFlattenable> SkImageShader::PreSamplingCreate(SkReadBuffer& buffer) { |
| 97 | SkASSERT(buffer.isVersionLT(SkPicturePriv::kSamplingInImageShader_Version)); |
| 98 | |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 99 | auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
| 100 | auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 101 | |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 102 | LegacyFilterEnum fe = LegacyFilterEnum::kInheritFromPaint; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 103 | if (!buffer.isVersionLT(SkPicturePriv::kFilterEnumInImageShader_Version)) { |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 104 | fe = buffer.read32LE<LegacyFilterEnum>(LegacyFilterEnum::kLast); |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Mike Reed | 15b95d6 | 2020-11-06 09:50:47 -0500 | [diff] [blame] | 107 | SkSamplingOptions op; |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 108 | |
| 109 | if (buffer.isVersionLT(SkPicturePriv::kCubicResamplerImageShader_Version)) { |
| 110 | if (!buffer.isVersionLT(SkPicturePriv::kFilterOptionsInImageShader_Version)) { |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 111 | auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear); |
| 112 | auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear); |
| 113 | op = SkSamplingOptions(filter, mipmap); |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 114 | } |
| 115 | } else { |
| 116 | switch (fe) { |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 117 | case LegacyFilterEnum::kUseFilterOptions: { |
| 118 | auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear); |
| 119 | auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear); |
| 120 | op = SkSamplingOptions(filter, mipmap); |
| 121 | } break; |
| 122 | case LegacyFilterEnum::kUseCubicResampler: { |
| 123 | SkScalar B = buffer.readScalar(), |
| 124 | C = buffer.readScalar(); |
| 125 | op = SkSamplingOptions({B,C}); |
| 126 | } break; |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 127 | default: |
| 128 | break; |
| 129 | } |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 132 | SkMatrix localMatrix; |
| 133 | buffer.readMatrix(&localMatrix); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 134 | sk_sp<SkImage> img = buffer.readImage(); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 135 | if (!img) { |
| 136 | return nullptr; |
| 137 | } |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 138 | |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 139 | switch (fe) { |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 140 | case LegacyFilterEnum::kUseFilterOptions: |
| 141 | case LegacyFilterEnum::kUseCubicResampler: |
| 142 | return SkImageShader::Make(std::move(img), tmx, tmy, &op, &localMatrix); |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 143 | default: |
| 144 | break; |
| 145 | } |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 146 | return SkImageShader::Make(std::move(img), tmx, tmy, nullptr, &localMatrix); |
| 147 | } |
| 148 | |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 149 | // fClampAsIfUnpremul is always false when constructed through public APIs, |
| 150 | // so there's no need to read or write it here. |
| 151 | |
| 152 | sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { |
| 153 | if (buffer.isVersionLT(SkPicturePriv::kSamplingInImageShader_Version)) { |
| 154 | return PreSamplingCreate(buffer); |
| 155 | } |
| 156 | |
| 157 | auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
| 158 | auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
| 159 | |
| 160 | SkSamplingOptions sampling, |
| 161 | *samplingPtr = nullptr; |
| 162 | |
| 163 | if (buffer.readBool()) { // fUseSamplingOptions |
Mike Reed | 9223665 | 2021-02-01 13:07:32 -0500 | [diff] [blame^] | 164 | sampling = SkSamplingPriv::Read(buffer); |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 165 | samplingPtr = &sampling; |
| 166 | } |
| 167 | |
| 168 | SkMatrix localMatrix; |
| 169 | buffer.readMatrix(&localMatrix); |
| 170 | sk_sp<SkImage> img = buffer.readImage(); |
| 171 | if (!img) { |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | return SkImageShader::Make(std::move(img), tmx, tmy, samplingPtr, &localMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void SkImageShader::flatten(SkWriteBuffer& buffer) const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 179 | buffer.writeUInt((unsigned)fTileModeX); |
| 180 | buffer.writeUInt((unsigned)fTileModeY); |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 181 | |
| 182 | buffer.writeBool(fUseSamplingOptions); |
| 183 | if (fUseSamplingOptions) { |
Mike Reed | 9223665 | 2021-02-01 13:07:32 -0500 | [diff] [blame^] | 184 | SkSamplingPriv::Write(buffer, fSampling); |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 185 | } |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 186 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 187 | buffer.writeMatrix(this->getLocalMatrix()); |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 188 | buffer.writeImage(fImage.get()); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 189 | SkASSERT(fClampAsIfUnpremul == false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | bool SkImageShader::isOpaque() const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 193 | return fImage->isOpaque() && |
| 194 | fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 197 | constexpr SkCubicResampler kDefaultCubicResampler{1.0f/3, 1.0f/3}; |
| 198 | |
| 199 | static bool is_default_cubic_resampler(SkCubicResampler cubic) { |
| 200 | return SkScalarNearlyEqual(cubic.B, kDefaultCubicResampler.B) && |
| 201 | SkScalarNearlyEqual(cubic.C, kDefaultCubicResampler.C); |
| 202 | } |
| 203 | |
Mike Reed | 604e4c2 | 2020-11-25 21:08:17 -0500 | [diff] [blame] | 204 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 205 | |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 206 | static bool legacy_shader_can_handle(const SkMatrix& inv) { |
Mike Reed | 8106311 | 2020-06-09 17:36:33 -0400 | [diff] [blame] | 207 | SkASSERT(!inv.hasPerspective()); |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 208 | |
| 209 | // Scale+translate methods are always present, but affine might not be. |
| 210 | if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) { |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | |
| 214 | // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates |
| 215 | // out of range. |
| 216 | const SkScalar max_dev_coord = 32767.0f; |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 217 | const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord)); |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 218 | |
Mike Reed | 1eb5ca4 | 2018-03-08 14:20:52 -0500 | [diff] [blame] | 219 | // take 1/4 of max signed 32bits so we have room to subtract local values |
Kevin Lubick | f76da63 | 2020-01-28 10:39:56 -0500 | [diff] [blame] | 220 | const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f; |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 221 | if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32, |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 222 | +max_fixed32dot32, +max_fixed32dot32).contains(src)) { |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | |
| 226 | // legacy shader impl should be able to handle these matrices |
| 227 | return true; |
| 228 | } |
| 229 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 230 | SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec, |
| 231 | SkArenaAlloc* alloc) const { |
Brian Osman | 0e18937 | 2018-10-19 11:58:29 -0400 | [diff] [blame] | 232 | if (fImage->alphaType() == kUnpremul_SkAlphaType) { |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 233 | return nullptr; |
| 234 | } |
Brian Osman | b70fd91 | 2018-10-22 16:10:44 -0400 | [diff] [blame] | 235 | if (fImage->colorType() != kN32_SkColorType) { |
| 236 | return nullptr; |
| 237 | } |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 238 | if (fTileModeX != fTileModeY) { |
| 239 | return nullptr; |
| 240 | } |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 241 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 242 | return nullptr; |
| 243 | } |
| 244 | |
Mike Reed | bc4d88a | 2021-01-21 12:49:36 -0500 | [diff] [blame] | 245 | SkSamplingOptions sampling = fUseSamplingOptions ? fSampling |
| 246 | : rec.fPaintSampling; |
| 247 | |
| 248 | auto supported = [](const SkSamplingOptions& sampling) { |
| 249 | const std::tuple<SkFilterMode,SkMipmapMode> supported[] = { |
| 250 | {SkFilterMode::kNearest, SkMipmapMode::kNone}, // legacy kNone_SkFilterQuality |
| 251 | {SkFilterMode::kLinear, SkMipmapMode::kNone}, // legacy kLow_SkFilterQuality |
| 252 | {SkFilterMode::kLinear, SkMipmapMode::kNearest}, // legacy kMedium_SkFilterQuality |
| 253 | }; |
| 254 | for (auto [f, m] : supported) { |
| 255 | if (sampling.filter == f && sampling.mipmap == m) { |
| 256 | return true; |
| 257 | } |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 258 | } |
Mike Reed | bc4d88a | 2021-01-21 12:49:36 -0500 | [diff] [blame] | 259 | return false; |
| 260 | }; |
| 261 | if (sampling.useCubic || !supported(sampling)) { |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 262 | return nullptr; |
| 263 | } |
| 264 | |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 265 | // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, |
Mike Klein | 67761eb | 2018-12-18 10:16:53 -0500 | [diff] [blame] | 266 | // so it can't handle bitmaps larger than 65535. |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 267 | // |
Mike Klein | 67761eb | 2018-12-18 10:16:53 -0500 | [diff] [blame] | 268 | // We back off another bit to 32767 to make small amounts of |
| 269 | // intermediate math safe, e.g. in |
| 270 | // |
| 271 | // SkFixed fx = ...; |
| 272 | // fx = tile(fx + SK_Fixed1); |
| 273 | // |
| 274 | // we want to make sure (fx + SK_Fixed1) never overflows. |
| 275 | if (fImage-> width() > 32767 || |
| 276 | fImage->height() > 32767) { |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 277 | return nullptr; |
| 278 | } |
| 279 | |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 280 | SkMatrix inv; |
| 281 | if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) || |
| 282 | !legacy_shader_can_handle(inv)) { |
| 283 | return nullptr; |
| 284 | } |
| 285 | |
Mike Reed | 011d166 | 2019-02-28 17:19:25 -0500 | [diff] [blame] | 286 | if (!rec.isLegacyCompatible(fImage->colorSpace())) { |
| 287 | return nullptr; |
| 288 | } |
| 289 | |
Mike Reed | bc4d88a | 2021-01-21 12:49:36 -0500 | [diff] [blame] | 290 | // Can remove this once fUseSamplingOptions is always true |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 291 | ContextRec modifiedRec = rec; |
Mike Reed | bc4d88a | 2021-01-21 12:49:36 -0500 | [diff] [blame] | 292 | modifiedRec.fPaintSampling = sampling; |
| 293 | |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 294 | return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY, |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 295 | as_IB(fImage.get()), modifiedRec, alloc); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 296 | } |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 297 | #endif |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 298 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 299 | SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const { |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 300 | if (texM) { |
| 301 | *texM = this->getLocalMatrix(); |
| 302 | } |
| 303 | if (xy) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 304 | xy[0] = fTileModeX; |
| 305 | xy[1] = fTileModeY; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 306 | } |
| 307 | return const_cast<SkImage*>(fImage.get()); |
| 308 | } |
| 309 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 310 | sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 311 | SkTileMode tmx, SkTileMode tmy, |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 312 | const SkSamplingOptions* options, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 313 | const SkMatrix* localMatrix, |
| 314 | bool clampAsIfUnpremul) { |
Mike Reed | 15b95d6 | 2020-11-06 09:50:47 -0500 | [diff] [blame] | 315 | auto is_unit = [](float x) { |
| 316 | return x >= 0 && x <= 1; |
| 317 | }; |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 318 | if (options && options->useCubic) { |
| 319 | if (!is_unit(options->cubic.B) || !is_unit(options->cubic.C)) { |
Mike Reed | 15b95d6 | 2020-11-06 09:50:47 -0500 | [diff] [blame] | 320 | return nullptr; |
| 321 | } |
| 322 | } |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 323 | if (!image) { |
| 324 | return sk_make_sp<SkEmptyShader>(); |
| 325 | } |
| 326 | return sk_sp<SkShader>{ |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 327 | new SkImageShader(image, tmx, tmy, options, localMatrix, clampAsIfUnpremul) |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 328 | }; |
| 329 | } |
| 330 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 331 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 332 | |
| 333 | #if SK_SUPPORT_GPU |
| 334 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 335 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | 0c24320 | 2020-06-29 14:29:25 -0400 | [diff] [blame] | 336 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 337 | #include "src/gpu/GrCaps.h" |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 338 | #include "src/gpu/GrColorInfo.h" |
Brian Salomon | 0c24320 | 2020-06-29 14:29:25 -0400 | [diff] [blame] | 339 | #include "src/gpu/GrImageTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 340 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | 0c24320 | 2020-06-29 14:29:25 -0400 | [diff] [blame] | 341 | #include "src/gpu/GrTextureAdjuster.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 342 | #include "src/gpu/SkGr.h" |
| 343 | #include "src/gpu/effects/GrBicubicEffect.h" |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 344 | #include "src/gpu/effects/GrBlendFragmentProcessor.h" |
Brian Salomon | b8f098d | 2020-01-07 11:15:44 -0500 | [diff] [blame] | 345 | #include "src/gpu/effects/GrTextureEffect.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 346 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 347 | std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor( |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 348 | const GrFPArgs& args) const { |
Florin Malita | 52f0291 | 2020-03-09 16:33:17 -0400 | [diff] [blame] | 349 | const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 350 | SkMatrix lmInverse; |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 351 | if (!lm->invert(&lmInverse)) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 352 | return nullptr; |
| 353 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 354 | |
Brian Salomon | 694ec49 | 2020-04-14 13:39:31 -0400 | [diff] [blame] | 355 | GrSamplerState::WrapMode wmX = SkTileModeToWrapMode(fTileModeX), |
| 356 | wmY = SkTileModeToWrapMode(fTileModeY); |
Brian Salomon | 0c24320 | 2020-06-29 14:29:25 -0400 | [diff] [blame] | 357 | // Must set wrap and filter on the sampler before requesting a texture. In two places |
| 358 | // below we check the matrix scale factors to determine how to interpret the filter |
| 359 | // quality setting. This completely ignores the complexity of the drawVertices case |
| 360 | // where explicit local coords are provided by the caller. |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 361 | bool sharpen = args.fContext->priv().options().fSharpenMipmappedTextures; |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 362 | GrSamplerState::Filter fm = GrSamplerState::Filter::kNearest; |
| 363 | GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone; |
Mike Reed | 52130b0 | 2020-12-28 15:33:13 -0500 | [diff] [blame] | 364 | SkCubicResampler kernel = kInvalidCubicResampler; |
Mike Reed | 3867c70 | 2020-09-01 13:28:10 -0400 | [diff] [blame] | 365 | |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 366 | if (fUseSamplingOptions) { |
Brian Salomon | 490f192 | 2021-02-01 13:14:37 -0500 | [diff] [blame] | 367 | if (fSampling.useCubic) { |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 368 | kernel = fSampling.cubic; |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 369 | } else { |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 370 | switch (fSampling.filter) { |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 371 | case SkFilterMode::kNearest: fm = GrSamplerState::Filter::kNearest; break; |
| 372 | case SkFilterMode::kLinear : fm = GrSamplerState::Filter::kLinear ; break; |
Mike Reed | 3867c70 | 2020-09-01 13:28:10 -0400 | [diff] [blame] | 373 | } |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 374 | switch (fSampling.mipmap) { |
Mike Reed | 3867c70 | 2020-09-01 13:28:10 -0400 | [diff] [blame] | 375 | case SkMipmapMode::kNone : mm = GrSamplerState::MipmapMode::kNone ; break; |
| 376 | case SkMipmapMode::kNearest: mm = GrSamplerState::MipmapMode::kNearest; break; |
| 377 | case SkMipmapMode::kLinear : mm = GrSamplerState::MipmapMode::kLinear ; break; |
| 378 | } |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 379 | } |
| 380 | } else { // inherit filterquality from paint |
Mike Reed | 52130b0 | 2020-12-28 15:33:13 -0500 | [diff] [blame] | 381 | std::tie(fm, mm, kernel) = |
| 382 | GrInterpretSamplingOptions(fImage->dimensions(), |
| 383 | args.fSampling, |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 384 | args.fMatrixProvider.localToDevice(), |
| 385 | *lm, |
| 386 | sharpen, |
| 387 | args.fAllowFilterQualityReduction); |
Brian Salomon | f735351 | 2020-07-22 19:26:48 -0400 | [diff] [blame] | 388 | } |
Brian Salomon | 490f192 | 2021-02-01 13:14:37 -0500 | [diff] [blame] | 389 | |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 390 | std::unique_ptr<GrFragmentProcessor> fp; |
Brian Salomon | 490f192 | 2021-02-01 13:14:37 -0500 | [diff] [blame] | 391 | // TODO: Replace this mess with SkImage_Base::asFragmentProcessor() after it's implemented. |
| 392 | if (as_IB(fImage)->isYUVA()) { |
| 393 | GrYUVAImageTextureMaker maker(args.fContext, fImage.get()); |
| 394 | if (GrValidCubicResampler(kernel)) { |
| 395 | fp = maker.createBicubicFragmentProcessor(lmInverse, |
| 396 | nullptr, |
| 397 | nullptr, |
| 398 | wmX, wmY, |
| 399 | kernel); |
| 400 | } else { |
| 401 | fp = maker.createFragmentProcessor(lmInverse, nullptr, nullptr, {wmX, wmY, fm, mm}); |
| 402 | } |
| 403 | if (!fp) { |
| 404 | return nullptr; |
| 405 | } |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 406 | } else { |
Brian Salomon | 490f192 | 2021-02-01 13:14:37 -0500 | [diff] [blame] | 407 | auto mipmapped = !GrValidCubicResampler(kernel) && mm != SkMipmapMode::kNone |
| 408 | ? GrMipmapped::kYes |
| 409 | : GrMipmapped::kNo; |
| 410 | GrSurfaceProxyView view = as_IB(fImage)->refView(args.fContext, mipmapped); |
| 411 | if (!view) { |
| 412 | return nullptr; |
| 413 | } |
| 414 | if (GrValidCubicResampler(kernel)) { |
| 415 | fp = GrBicubicEffect::Make(std::move(view), |
| 416 | fImage->alphaType(), |
| 417 | lmInverse, |
| 418 | wmX, wmY, |
| 419 | kernel, |
| 420 | GrBicubicEffect::Direction::kXY, |
| 421 | *args.fContext->priv().caps()); |
| 422 | } else { |
| 423 | fp = GrTextureEffect::Make(std::move(view), |
| 424 | fImage->alphaType(), |
| 425 | lmInverse, |
| 426 | {wmX, wmY, fm, mm}, |
| 427 | *args.fContext->priv().caps()); |
| 428 | } |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 429 | } |
Brian Salomon | 490f192 | 2021-02-01 13:14:37 -0500 | [diff] [blame] | 430 | SkASSERT(fp); |
| 431 | fp = GrColorSpaceXformEffect::Make(std::move(fp), |
| 432 | fImage->colorSpace(), |
| 433 | fImage->alphaType(), |
| 434 | args.fDstColorInfo->colorSpace(), |
| 435 | kPremul_SkAlphaType); |
Brian Salomon | b43d699 | 2021-01-05 14:37:40 -0500 | [diff] [blame] | 436 | if (fImage->isAlphaOnly()) { |
| 437 | return GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kDstIn); |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 438 | } else if (args.fInputColorIsOpaque) { |
Brian Salomon | b43d699 | 2021-01-05 14:37:40 -0500 | [diff] [blame] | 439 | // This special case isn't needed for correctness. It just avoids a multiplication by |
| 440 | // a vertex attribute alpha that is known to be 1 if we take the kSrcIn path. |
Brian Salomon | 0c24320 | 2020-06-29 14:29:25 -0400 | [diff] [blame] | 441 | return GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 442 | } |
Brian Salomon | b43d699 | 2021-01-05 14:37:40 -0500 | [diff] [blame] | 443 | return GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kSrcIn); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | #endif |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 447 | |
| 448 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 449 | #include "src/core/SkImagePriv.h" |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 450 | |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 451 | sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 452 | SkTileMode tmx, SkTileMode tmy, |
Mike Reed | 172ba9e | 2020-12-17 15:57:55 -0500 | [diff] [blame] | 453 | const SkSamplingOptions& sampling, |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 454 | const SkMatrix* localMatrix, SkCopyPixelsMode mode) { |
Mike Reed | 172ba9e | 2020-12-17 15:57:55 -0500 | [diff] [blame] | 455 | auto s = SkImageShader::Make(SkMakeImageFromRasterBitmap(src, mode), |
| 456 | tmx, tmy, &sampling, localMatrix); |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 457 | if (!s) { |
| 458 | return nullptr; |
| 459 | } |
| 460 | if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) { |
| 461 | // Compose the image shader with the paint's shader. Alpha images+shaders should output the |
| 462 | // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with |
| 463 | // the source image and dst shader (MakeBlend takes dst first, src second). |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 464 | s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s)); |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 465 | } |
| 466 | return s; |
| 467 | } |
| 468 | |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 469 | void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); } |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 470 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 471 | class SkImageStageUpdater : public SkStageUpdater { |
| 472 | public: |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 473 | SkImageStageUpdater(const SkImageShader* shader, bool usePersp) |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 474 | : fShader(shader) |
| 475 | , fUsePersp(usePersp || as_SB(shader)->getLocalMatrix().hasPerspective()) |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 476 | {} |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 477 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 478 | const SkImageShader* fShader; |
| 479 | const bool fUsePersp; // else use affine |
| 480 | |
| 481 | // large enough for perspective, though often we just use 2x3 |
| 482 | float fMatrixStorage[9]; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 483 | |
| 484 | #if 0 // TODO: when we support mipmaps |
| 485 | SkRasterPipeline_GatherCtx* fGather; |
| 486 | SkRasterPipeline_TileCtx* fLimitX; |
| 487 | SkRasterPipeline_TileCtx* fLimitY; |
| 488 | SkRasterPipeline_DecalTileCtx* fDecal; |
| 489 | #endif |
| 490 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 491 | void append_matrix_stage(SkRasterPipeline* p) { |
| 492 | if (fUsePersp) { |
| 493 | p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage); |
| 494 | } else { |
| 495 | p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage); |
| 496 | } |
| 497 | } |
| 498 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 499 | bool update(const SkMatrix& ctm, const SkMatrix* localM) override { |
| 500 | SkMatrix matrix; |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 501 | if (fShader->computeTotalInverse(ctm, localM, &matrix)) { |
| 502 | if (fUsePersp) { |
| 503 | matrix.get9(fMatrixStorage); |
| 504 | } else { |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 505 | // if we get here, matrix should be affine. If it isn't, then defensively we |
| 506 | // won't draw (by returning false), but we should work to never let this |
| 507 | // happen (i.e. better preflight by the caller to know ahead of time that we |
| 508 | // may encounter perspective, either in the CTM, or in the localM). |
| 509 | // |
| 510 | // See https://bugs.chromium.org/p/skia/issues/detail?id=10004 |
| 511 | // |
| 512 | if (!matrix.asAffine(fMatrixStorage)) { |
| 513 | SkASSERT(false); |
| 514 | return false; |
| 515 | } |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 516 | } |
| 517 | return true; |
| 518 | } |
| 519 | return false; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 520 | } |
| 521 | }; |
| 522 | |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 523 | static SkSamplingOptions tweak_filter_and_inv_matrix(SkSamplingOptions sampling, SkMatrix* matrix) { |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 524 | SkFilterMode filter = sampling.filter; |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 525 | |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 526 | // When the matrix is just an integer translate, bilerp == nearest neighbor. |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 527 | if (filter == SkFilterMode::kLinear && |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 528 | matrix->getType() <= SkMatrix::kTranslate_Mask && |
| 529 | matrix->getTranslateX() == (int)matrix->getTranslateX() && |
| 530 | matrix->getTranslateY() == (int)matrix->getTranslateY()) { |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 531 | filter = SkFilterMode::kNearest; |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // See skia:4649 and the GM image_scale_aligned. |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 535 | if (filter == SkFilterMode::kNearest) { |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 536 | if (matrix->getScaleX() >= 0) { |
| 537 | matrix->setTranslateX(nextafterf(matrix->getTranslateX(), |
| 538 | floorf(matrix->getTranslateX()))); |
| 539 | } |
| 540 | if (matrix->getScaleY() >= 0) { |
| 541 | matrix->setTranslateY(nextafterf(matrix->getTranslateY(), |
| 542 | floorf(matrix->getTranslateY()))); |
| 543 | } |
| 544 | } |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 545 | |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 546 | return SkSamplingOptions(filter, sampling.mipmap); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 547 | } |
| 548 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 549 | bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const { |
Mike Reed | 39b4c86 | 2020-11-25 16:15:53 -0500 | [diff] [blame] | 550 | auto sampling = fUseSamplingOptions ? fSampling |
| 551 | : SkSamplingOptions(rec.fPaint.getFilterQuality()); |
| 552 | |
| 553 | // We only support certain sampling options in stages so far |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 554 | if (sampling.useCubic) { |
| 555 | if (!is_default_cubic_resampler(sampling.cubic)) { |
Mike Reed | 39b4c86 | 2020-11-25 16:15:53 -0500 | [diff] [blame] | 556 | return false; |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 557 | } |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 558 | } else if (sampling.mipmap == SkMipmapMode::kLinear) { |
Mike Reed | 39b4c86 | 2020-11-25 16:15:53 -0500 | [diff] [blame] | 559 | return false; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 560 | } |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 561 | |
Mike Reed | 39b4c86 | 2020-11-25 16:15:53 -0500 | [diff] [blame] | 562 | |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 563 | if (updater && (sampling.mipmap != SkMipmapMode::kNone)) { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 564 | // TODO: medium: recall RequestBitmap and update width/height accordingly |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 565 | return false; |
| 566 | } |
| 567 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 568 | SkRasterPipeline* p = rec.fPipeline; |
| 569 | SkArenaAlloc* alloc = rec.fAlloc; |
| 570 | |
Florin Malita | 7558e4d | 2018-02-07 10:05:53 -0500 | [diff] [blame] | 571 | SkMatrix matrix; |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 572 | if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) { |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 573 | return false; |
| 574 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 575 | |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 576 | if (sampling.useCubic && |
| 577 | SkMatrixPriv::AdjustHighQualityFilterLevel(matrix, true) != kHigh_SkFilterQuality) |
| 578 | { |
| 579 | sampling = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNearest); |
Mike Klein | f447dee | 2016-11-29 16:37:12 -0500 | [diff] [blame] | 580 | } |
Mike Reed | bccdd90 | 2020-12-07 12:36:02 -0500 | [diff] [blame] | 581 | auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), matrix, sampling.mipmap); |
| 582 | if (!access) { |
| 583 | return false; |
| 584 | } |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 585 | SkPixmap pm; |
| 586 | std::tie(pm, matrix) = access->level(); |
Mike Klein | f447dee | 2016-11-29 16:37:12 -0500 | [diff] [blame] | 587 | |
Mike Klein | e8de024 | 2018-03-10 12:37:11 -0500 | [diff] [blame] | 588 | p->append(SkRasterPipeline::seed_shader); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 589 | |
| 590 | if (updater) { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 591 | updater->append_matrix_stage(p); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 592 | } else { |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 593 | if (!sampling.useCubic) { |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 594 | sampling = tweak_filter_and_inv_matrix(sampling, &matrix); |
Mike Reed | 3d58d5a | 2020-11-23 13:32:36 -0500 | [diff] [blame] | 595 | } |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 596 | p->append_matrix(alloc, matrix); |
| 597 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 598 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 599 | auto gather = alloc->make<SkRasterPipeline_GatherCtx>(); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 600 | gather->pixels = pm.addr(); |
Mike Klein | 968af43 | 2017-07-18 16:31:55 -0400 | [diff] [blame] | 601 | gather->stride = pm.rowBytesAsPixels(); |
Mike Klein | f3b4e16 | 2017-09-22 15:32:59 -0400 | [diff] [blame] | 602 | gather->width = pm.width(); |
| 603 | gather->height = pm.height(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 604 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 605 | auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(), |
| 606 | limit_y = alloc->make<SkRasterPipeline_TileCtx>(); |
Mike Reed | 51e46d5 | 2017-06-23 14:21:25 -0400 | [diff] [blame] | 607 | limit_x->scale = pm.width(); |
| 608 | limit_x->invScale = 1.0f / pm.width(); |
| 609 | limit_y->scale = pm.height(); |
| 610 | limit_y->invScale = 1.0f / pm.height(); |
Mike Klein | fc84dc5 | 2017-05-11 15:29:31 -0400 | [diff] [blame] | 611 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 612 | SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 613 | bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal; |
| 614 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 615 | decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>(); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 616 | decal_ctx->limit_x = limit_x->scale; |
| 617 | decal_ctx->limit_y = limit_y->scale; |
| 618 | } |
| 619 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 620 | #if 0 // TODO: when we support kMedium |
| 621 | if (updator && (quality == kMedium_SkFilterQuality)) { |
| 622 | // if we change levels in mipmap, we need to update the scales (and invScales) |
| 623 | updator->fGather = gather; |
| 624 | updator->fLimitX = limit_x; |
| 625 | updator->fLimitY = limit_y; |
| 626 | updator->fDecal = decal_ctx; |
| 627 | } |
| 628 | #endif |
| 629 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 630 | auto append_tiling_and_gather = [&] { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 631 | if (decal_x_and_y) { |
| 632 | p->append(SkRasterPipeline::decal_x_and_y, decal_ctx); |
| 633 | } else { |
| 634 | switch (fTileModeX) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 635 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 636 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break; |
| 637 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break; |
| 638 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 639 | } |
| 640 | switch (fTileModeY) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 641 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 642 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break; |
| 643 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break; |
| 644 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 645 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 646 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 647 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 648 | void* ctx = gather; |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 649 | switch (pm.colorType()) { |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 650 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 651 | case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break; |
| 652 | case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 653 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break; |
| 654 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 655 | case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break; |
| 656 | case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break; |
| 657 | case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 658 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break; |
| 659 | case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 660 | case kR16G16B16A16_unorm_SkColorType: |
| 661 | p->append(SkRasterPipeline::gather_16161616,ctx); break; |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 662 | case kRGBA_F16Norm_SkColorType: |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 663 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 664 | case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 665 | |
Mike Klein | b1df5e5 | 2018-10-17 17:06:03 -0400 | [diff] [blame] | 666 | case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); |
| 667 | p->append(SkRasterPipeline::alpha_to_gray ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 668 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 669 | case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 670 | p->append(SkRasterPipeline::force_opaque ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 671 | |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 672 | case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 673 | p->append(SkRasterPipeline::swap_rb ); break; |
| 674 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 675 | case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 676 | p->append(SkRasterPipeline::force_opaque ); break; |
| 677 | |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 678 | case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 679 | p->append(SkRasterPipeline::force_opaque ); |
| 680 | p->append(SkRasterPipeline::swap_rb ); break; |
| 681 | |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 682 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 683 | p->append(SkRasterPipeline::swap_rb ); break; |
| 684 | |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 685 | case kUnknown_SkColorType: SkASSERT(false); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 686 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 687 | if (decal_ctx) { |
| 688 | p->append(SkRasterPipeline::check_decal_mask, decal_ctx); |
| 689 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 690 | }; |
| 691 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 692 | auto append_misc = [&] { |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 693 | SkColorSpace* cs = pm.colorSpace(); |
| 694 | SkAlphaType at = pm.alphaType(); |
Mike Klein | 6a9f1c4 | 2020-01-02 23:23:42 -0600 | [diff] [blame] | 695 | |
| 696 | // Color for A8 images comes from the paint. TODO: all alpha images? none? |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 697 | if (pm.colorType() == kAlpha_8_SkColorType) { |
Mike Klein | 6a9f1c4 | 2020-01-02 23:23:42 -0600 | [diff] [blame] | 698 | SkColor4f rgb = rec.fPaint.getColor4f(); |
| 699 | p->append_set_rgb(alloc, rgb); |
| 700 | |
Mike Klein | 6a9f1c4 | 2020-01-02 23:23:42 -0600 | [diff] [blame] | 701 | cs = sk_srgb_singleton(); |
| 702 | at = kUnpremul_SkAlphaType; |
| 703 | } |
| 704 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 705 | // Bicubic filtering naturally produces out of range values on both sides of [0,1]. |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 706 | if (sampling.useCubic) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 707 | p->append(SkRasterPipeline::clamp_0); |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 708 | p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul |
| 709 | ? SkRasterPipeline::clamp_1 |
| 710 | : SkRasterPipeline::clamp_a); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 711 | } |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 712 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 713 | // Transform color space and alpha type to match shader convention (dst CS, premul alpha). |
| 714 | alloc->make<SkColorSpaceXformSteps>(cs, at, |
| 715 | rec.fDstCS, kPremul_SkAlphaType) |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 716 | ->apply(p); |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 717 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 718 | return true; |
| 719 | }; |
| 720 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 721 | // Check for fast-path stages. |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 722 | auto ct = pm.colorType(); |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 723 | if (true |
| 724 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 725 | && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 726 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 727 | |
| 728 | p->append(SkRasterPipeline::bilerp_clamp_8888, gather); |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 729 | if (ct == kBGRA_8888_SkColorType) { |
| 730 | p->append(SkRasterPipeline::swap_rb); |
| 731 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 732 | return append_misc(); |
| 733 | } |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 734 | if (true |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 735 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 736 | && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 737 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 738 | && fTileModeY != SkTileMode::kDecal) { |
| 739 | |
| 740 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 741 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 742 | ctx->ct = ct; |
| 743 | ctx->tileX = fTileModeX; |
| 744 | ctx->tileY = fTileModeY; |
| 745 | ctx->invWidth = 1.0f / ctx->width; |
| 746 | ctx->invHeight = 1.0f / ctx->height; |
| 747 | p->append(SkRasterPipeline::bilinear, ctx); |
| 748 | return append_misc(); |
| 749 | } |
| 750 | if (true |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 751 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 752 | && sampling.useCubic |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 753 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
| 754 | |
| 755 | p->append(SkRasterPipeline::bicubic_clamp_8888, gather); |
| 756 | if (ct == kBGRA_8888_SkColorType) { |
| 757 | p->append(SkRasterPipeline::swap_rb); |
| 758 | } |
| 759 | return append_misc(); |
| 760 | } |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 761 | if (true |
| 762 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 763 | && sampling.useCubic |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 764 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 765 | && fTileModeY != SkTileMode::kDecal) { |
| 766 | |
| 767 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 768 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 769 | ctx->ct = ct; |
| 770 | ctx->tileX = fTileModeX; |
| 771 | ctx->tileY = fTileModeY; |
| 772 | ctx->invWidth = 1.0f / ctx->width; |
| 773 | ctx->invHeight = 1.0f / ctx->height; |
| 774 | p->append(SkRasterPipeline::bicubic, ctx); |
| 775 | return append_misc(); |
| 776 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 777 | |
Mike Reed | 3d58d5a | 2020-11-23 13:32:36 -0500 | [diff] [blame] | 778 | SkRasterPipeline_SamplerCtx* sampler = alloc->make<SkRasterPipeline_SamplerCtx>(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 779 | |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 780 | auto sample = [&](SkRasterPipeline::StockStage setup_x, |
| 781 | SkRasterPipeline::StockStage setup_y) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 782 | p->append(setup_x, sampler); |
| 783 | p->append(setup_y, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 784 | append_tiling_and_gather(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 785 | p->append(SkRasterPipeline::accumulate, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 786 | }; |
| 787 | |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 788 | if (sampling.useCubic) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 789 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 790 | |
| 791 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y); |
| 792 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y); |
| 793 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y); |
| 794 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y); |
| 795 | |
| 796 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y); |
| 797 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y); |
| 798 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y); |
| 799 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y); |
| 800 | |
| 801 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y); |
| 802 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y); |
| 803 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y); |
| 804 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y); |
| 805 | |
| 806 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y); |
| 807 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y); |
| 808 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y); |
| 809 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y); |
| 810 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 811 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 812 | } else if (sampling.filter == SkFilterMode::kLinear) { |
Mike Reed | 3d58d5a | 2020-11-23 13:32:36 -0500 | [diff] [blame] | 813 | p->append(SkRasterPipeline::save_xy, sampler); |
| 814 | |
| 815 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny); |
| 816 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny); |
| 817 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py); |
| 818 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py); |
| 819 | |
| 820 | p->append(SkRasterPipeline::move_dst_src); |
| 821 | } else { |
| 822 | append_tiling_and_gather(); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 823 | } |
| 824 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 825 | return append_misc(); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 826 | } |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 827 | |
| 828 | bool SkImageShader::onAppendStages(const SkStageRec& rec) const { |
| 829 | return this->doStages(rec, nullptr); |
| 830 | } |
| 831 | |
| 832 | SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const { |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 833 | bool usePersp = rec.fMatrixProvider.localToDevice().hasPerspective(); |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 834 | auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 835 | return this->doStages(rec, updater) ? updater : nullptr; |
| 836 | } |
| 837 | |
Brian Osman | 5aaaeea | 2020-06-22 14:26:03 -0400 | [diff] [blame] | 838 | skvm::Color SkImageShader::onProgram(skvm::Builder* p, |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 839 | skvm::Coord device, skvm::Coord origLocal, skvm::Color paint, |
Mike Klein | e81b108 | 2020-06-19 11:29:13 -0500 | [diff] [blame] | 840 | const SkMatrixProvider& matrices, const SkMatrix* localM, |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 841 | SkFilterQuality paintQuality, const SkColorInfo& dst, |
Mike Klein | 276a785 | 2020-03-15 08:46:09 -0500 | [diff] [blame] | 842 | skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const { |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 843 | SkMatrix baseInv; |
| 844 | if (!this->computeTotalInverse(matrices.localToDevice(), localM, &baseInv)) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 845 | return {}; |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 846 | } |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 847 | baseInv.normalizePerspective(); |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 848 | |
Mike Reed | 3d58d5a | 2020-11-23 13:32:36 -0500 | [diff] [blame] | 849 | auto sampling = fUseSamplingOptions ? fSampling : SkSamplingOptions(paintQuality); |
Mike Reed | bccdd90 | 2020-12-07 12:36:02 -0500 | [diff] [blame] | 850 | auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), baseInv, sampling.mipmap); |
| 851 | if (!access) { |
| 852 | return {}; |
| 853 | } |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 854 | auto [upper, upperInv] = access->level(); |
| 855 | if (!sampling.useCubic) { |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 856 | sampling = tweak_filter_and_inv_matrix(sampling, &upperInv); |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 857 | } |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 858 | |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 859 | SkPixmap lowerPixmap; |
| 860 | SkMatrix lowerInv; |
| 861 | SkPixmap* lower = nullptr; |
| 862 | float lowerWeight = access->lowerWeight(); |
| 863 | if (lowerWeight > 0) { |
| 864 | std::tie(lowerPixmap, lowerInv) = access->lowerLevel(); |
| 865 | lower = &lowerPixmap; |
| 866 | } |
| 867 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 868 | skvm::Coord upperLocal = SkShaderBase::ApplyMatrix(p, upperInv, origLocal, uniforms); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 869 | |
Mike Klein | 89b3c1f | 2020-07-29 16:45:05 -0500 | [diff] [blame] | 870 | // All existing SkColorTypes pass these checks. We'd only fail here adding new ones. |
Mike Klein | 03c932c | 2020-07-13 09:07:42 -0500 | [diff] [blame] | 871 | skvm::PixelFormat unused; |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 872 | if (true && !SkColorType_to_PixelFormat(upper.colorType(), &unused)) { |
Mike Klein | 03c932c | 2020-07-13 09:07:42 -0500 | [diff] [blame] | 873 | return {}; |
| 874 | } |
Mike Klein | 9662fd6 | 2020-07-16 15:11:27 -0500 | [diff] [blame] | 875 | if (lower && !SkColorType_to_PixelFormat(lower->colorType(), &unused)) { |
Mike Klein | 03c932c | 2020-07-13 09:07:42 -0500 | [diff] [blame] | 876 | return {}; |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 877 | } |
| 878 | |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 879 | // We can exploit image opacity to skip work unpacking alpha channels. |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 880 | const bool input_is_opaque = SkAlphaTypeIsOpaque(upper.alphaType()) |
| 881 | || SkColorTypeIsAlwaysOpaque(upper.colorType()); |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 882 | |
Mike Klein | 03d89ef | 2020-01-14 17:18:29 -0600 | [diff] [blame] | 883 | // Each call to sample() will try to rewrite the same uniforms over and over, |
| 884 | // so remember where we start and reset back there each time. That way each |
| 885 | // sample() call uses the same uniform offsets. |
Mike Klein | 03d89ef | 2020-01-14 17:18:29 -0600 | [diff] [blame] | 886 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 887 | auto compute_clamp_limit = [&](float limit) { |
| 888 | // Subtract an ulp so the upper clamp limit excludes limit itself. |
| 889 | int bits; |
| 890 | memcpy(&bits, &limit, 4); |
| 891 | return p->uniformF(uniforms->push(bits-1)); |
| 892 | }; |
Mike Klein | 03d89ef | 2020-01-14 17:18:29 -0600 | [diff] [blame] | 893 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 894 | // Except in the simplest case (no mips, no filtering), we reference uniforms |
| 895 | // more than once. To avoid adding/registering them multiple times, we pre-load them |
| 896 | // into a struct (just to logically group them together), based on the "current" |
| 897 | // pixmap (level of a mipmap). |
| 898 | // |
| 899 | struct Uniforms { |
| 900 | skvm::F32 w, iw, i2w, |
| 901 | h, ih, i2h; |
| 902 | |
| 903 | skvm::F32 clamp_w, |
| 904 | clamp_h; |
| 905 | |
| 906 | skvm::Uniform addr; |
| 907 | skvm::I32 rowBytesAsPixels; |
| 908 | |
Mike Klein | 03c932c | 2020-07-13 09:07:42 -0500 | [diff] [blame] | 909 | skvm::PixelFormat pixelFormat; // not a uniform, but needed for each texel sample, |
| 910 | // so we store it here, since it is also dependent on |
| 911 | // the current pixmap (level). |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 912 | }; |
| 913 | |
| 914 | auto setup_uniforms = [&](const SkPixmap& pm) -> Uniforms { |
Mike Klein | 03c932c | 2020-07-13 09:07:42 -0500 | [diff] [blame] | 915 | skvm::PixelFormat pixelFormat; |
Mike Klein | 9662fd6 | 2020-07-16 15:11:27 -0500 | [diff] [blame] | 916 | SkAssertResult(SkColorType_to_PixelFormat(pm.colorType(), &pixelFormat)); |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 917 | return { |
| 918 | p->uniformF(uniforms->pushF( pm.width())), |
| 919 | p->uniformF(uniforms->pushF(1.0f/pm.width())), // iff tileX == kRepeat |
| 920 | p->uniformF(uniforms->pushF(0.5f/pm.width())), // iff tileX == kMirror |
| 921 | |
| 922 | p->uniformF(uniforms->pushF( pm.height())), |
| 923 | p->uniformF(uniforms->pushF(1.0f/pm.height())), // iff tileY == kRepeat |
| 924 | p->uniformF(uniforms->pushF(0.5f/pm.height())), // iff tileY == kMirror |
| 925 | |
| 926 | compute_clamp_limit(pm. width()), |
| 927 | compute_clamp_limit(pm.height()), |
| 928 | |
| 929 | uniforms->pushPtr(pm.addr()), |
| 930 | p->uniform32(uniforms->push(pm.rowBytesAsPixels())), |
| 931 | |
Mike Klein | 03c932c | 2020-07-13 09:07:42 -0500 | [diff] [blame] | 932 | pixelFormat, |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 933 | }; |
| 934 | }; |
| 935 | |
| 936 | auto sample_texel = [&](const Uniforms& u, skvm::F32 sx, skvm::F32 sy) -> skvm::Color { |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 937 | // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp. |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 938 | auto repeat = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I) { |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 939 | return v - floor(v * I) * S; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 940 | }; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 941 | auto mirror = [&](skvm::F32 v, skvm::F32 S, skvm::F32 I2) { |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 942 | // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale ) |
| 943 | // {---A---} {------------------B------------------} |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 944 | skvm::F32 A = v - S, |
| 945 | B = (S + S) * floor(A * I2); |
| 946 | return abs(A - B - S); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 947 | }; |
| 948 | switch (fTileModeX) { |
| 949 | case SkTileMode::kDecal: /* handled after gather */ break; |
| 950 | case SkTileMode::kClamp: /* we always clamp */ break; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 951 | case SkTileMode::kRepeat: sx = repeat(sx, u.w, u.iw); break; |
| 952 | case SkTileMode::kMirror: sx = mirror(sx, u.w, u.i2w); break; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 953 | } |
| 954 | switch (fTileModeY) { |
| 955 | case SkTileMode::kDecal: /* handled after gather */ break; |
| 956 | case SkTileMode::kClamp: /* we always clamp */ break; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 957 | case SkTileMode::kRepeat: sy = repeat(sy, u.h, u.ih); break; |
| 958 | case SkTileMode::kMirror: sy = mirror(sy, u.h, u.i2h); break; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | // Always clamp sample coordinates to [0,width), [0,height), both for memory |
| 962 | // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror. |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 963 | skvm::F32 clamped_x = clamp(sx, 0, u.clamp_w), |
| 964 | clamped_y = clamp(sy, 0, u.clamp_h); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 965 | |
| 966 | // Load pixels from pm.addr()[(int)sx + (int)sy*stride]. |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 967 | skvm::I32 index = trunc(clamped_x) + |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 968 | trunc(clamped_y) * u.rowBytesAsPixels; |
Mike Klein | 03c932c | 2020-07-13 09:07:42 -0500 | [diff] [blame] | 969 | skvm::Color c = gather(u.pixelFormat, u.addr, index); |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 970 | |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 971 | // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it. |
| 972 | if (input_is_opaque) { |
Mike Klein | 46874ef | 2020-02-13 10:17:08 -0600 | [diff] [blame] | 973 | c.a = p->splat(1.0f); |
| 974 | } |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 975 | |
| 976 | // Mask away any pixels that we tried to sample outside the bounds in kDecal. |
| 977 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
| 978 | skvm::I32 mask = p->splat(~0); |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 979 | if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); } |
| 980 | if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); } |
Mike Klein | 5ec9c4e | 2020-12-01 10:43:46 -0600 | [diff] [blame] | 981 | c.r = pun_to_F32(p->bit_and(mask, pun_to_I32(c.r))); |
| 982 | c.g = pun_to_F32(p->bit_and(mask, pun_to_I32(c.g))); |
| 983 | c.b = pun_to_F32(p->bit_and(mask, pun_to_I32(c.b))); |
| 984 | c.a = pun_to_F32(p->bit_and(mask, pun_to_I32(c.a))); |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 985 | // Notice that even if input_is_opaque, c.a might now be 0. |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | return c; |
| 989 | }; |
| 990 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 991 | auto sample_level = [&](const SkPixmap& pm, const SkMatrix& inv, skvm::Coord local) { |
| 992 | const Uniforms u = setup_uniforms(pm); |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 993 | |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 994 | if (sampling.useCubic) { |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 995 | // All bicubic samples have the same fractional offset (fx,fy) from the center. |
| 996 | // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center. |
| 997 | skvm::F32 fx = fract(local.x + 0.5f), |
| 998 | fy = fract(local.y + 0.5f); |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 999 | skvm::F32 wx[4], |
| 1000 | wy[4]; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 1001 | |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 1002 | SkM44 weights = CubicResamplerMatrix(sampling.cubic.B, sampling.cubic.C); |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 1003 | |
Mike Klein | d318489 | 2020-07-22 17:39:20 -0500 | [diff] [blame] | 1004 | auto dot = [](const skvm::F32 a[], const skvm::F32 b[]) { |
| 1005 | return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]; |
| 1006 | }; |
| 1007 | const skvm::F32 tmpx[] = { p->splat(1.0f), fx, fx*fx, fx*fx*fx }; |
| 1008 | const skvm::F32 tmpy[] = { p->splat(1.0f), fy, fy*fy, fy*fy*fy }; |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 1009 | |
Mike Klein | d318489 | 2020-07-22 17:39:20 -0500 | [diff] [blame] | 1010 | for (int row = 0; row < 4; ++row) { |
| 1011 | SkV4 r = weights.row(row); |
| 1012 | skvm::F32 ru[] = { |
| 1013 | p->uniformF(uniforms->pushF(r[0])), |
| 1014 | p->uniformF(uniforms->pushF(r[1])), |
| 1015 | p->uniformF(uniforms->pushF(r[2])), |
| 1016 | p->uniformF(uniforms->pushF(r[3])), |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 1017 | }; |
Mike Klein | d318489 | 2020-07-22 17:39:20 -0500 | [diff] [blame] | 1018 | wx[row] = dot(ru, tmpx); |
| 1019 | wy[row] = dot(ru, tmpy); |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 1020 | } |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 1021 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 1022 | skvm::Color c; |
| 1023 | c.r = c.g = c.b = c.a = p->splat(0.0f); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 1024 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 1025 | skvm::F32 sy = local.y - 1.5f; |
| 1026 | for (int j = 0; j < 4; j++, sy += 1.0f) { |
| 1027 | skvm::F32 sx = local.x - 1.5f; |
| 1028 | for (int i = 0; i < 4; i++, sx += 1.0f) { |
| 1029 | skvm::Color s = sample_texel(u, sx,sy); |
| 1030 | skvm::F32 w = wx[i] * wy[j]; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 1031 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 1032 | c.r += s.r * w; |
| 1033 | c.g += s.g * w; |
| 1034 | c.b += s.b * w; |
| 1035 | c.a += s.a * w; |
| 1036 | } |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 1037 | } |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 1038 | return c; |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 1039 | } else if (sampling.filter == SkFilterMode::kLinear) { |
Mike Reed | 3d58d5a | 2020-11-23 13:32:36 -0500 | [diff] [blame] | 1040 | // Our four sample points are the corners of a logical 1x1 pixel |
| 1041 | // box surrounding (x,y) at (0.5,0.5) off-center. |
| 1042 | skvm::F32 left = local.x - 0.5f, |
| 1043 | top = local.y - 0.5f, |
| 1044 | right = local.x + 0.5f, |
| 1045 | bottom = local.y + 0.5f; |
| 1046 | |
| 1047 | // The fractional parts of right and bottom are our lerp factors in x and y respectively. |
| 1048 | skvm::F32 fx = fract(right ), |
| 1049 | fy = fract(bottom); |
| 1050 | |
| 1051 | return lerp(lerp(sample_texel(u, left,top ), sample_texel(u, right,top ), fx), |
| 1052 | lerp(sample_texel(u, left,bottom), sample_texel(u, right,bottom), fx), fy); |
| 1053 | } else { |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 1054 | SkASSERT(sampling.filter == SkFilterMode::kNearest); |
Mike Reed | 3d58d5a | 2020-11-23 13:32:36 -0500 | [diff] [blame] | 1055 | return sample_texel(u, local.x,local.y); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 1056 | } |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 1057 | }; |
| 1058 | |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 1059 | skvm::Color c = sample_level(upper, upperInv, upperLocal); |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 1060 | if (lower) { |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 1061 | auto lowerLocal = SkShaderBase::ApplyMatrix(p, lowerInv, origLocal, uniforms); |
| 1062 | // lower * weight + upper * (1 - weight) |
| 1063 | c = lerp(c, |
| 1064 | sample_level(*lower, lowerInv, lowerLocal), |
| 1065 | p->uniformF(uniforms->pushF(lowerWeight))); |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 1066 | } |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame] | 1067 | |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 1068 | // If the input is opaque and we're not in decal mode, that means the output is too. |
| 1069 | // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas. |
| 1070 | if (input_is_opaque |
| 1071 | && fTileModeX != SkTileMode::kDecal |
| 1072 | && fTileModeY != SkTileMode::kDecal) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 1073 | c.a = p->splat(1.0f); |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 1074 | } |
| 1075 | |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 1076 | // Alpha-only images get their color from the paint (already converted to dst color space). |
Mike Reed | 8c1ad7e | 2020-12-02 20:41:52 -0500 | [diff] [blame] | 1077 | SkColorSpace* cs = upper.colorSpace(); |
| 1078 | SkAlphaType at = upper.alphaType(); |
| 1079 | if (SkColorTypeIsAlphaOnly(upper.colorType())) { |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 1080 | c.r = paint.r; |
| 1081 | c.g = paint.g; |
| 1082 | c.b = paint.b; |
| 1083 | |
| 1084 | cs = dst.colorSpace(); |
| 1085 | at = kUnpremul_SkAlphaType; |
| 1086 | } |
| 1087 | |
Mike Klein | bb1933e | 2020-12-02 15:45:29 -0600 | [diff] [blame] | 1088 | if (sampling.useCubic) { |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 1089 | // Bicubic filtering naturally produces out of range values on both sides of [0,1]. |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 1090 | c.a = clamp01(c.a); |
Mike Klein | 3f83bfd | 2020-01-07 12:01:50 -0600 | [diff] [blame] | 1091 | |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 1092 | skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul) |
Mike Klein | 3f83bfd | 2020-01-07 12:01:50 -0600 | [diff] [blame] | 1093 | ? p->splat(1.0f) |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 1094 | : c.a; |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 1095 | c.r = clamp(c.r, 0.0f, limit); |
| 1096 | c.g = clamp(c.g, 0.0f, limit); |
| 1097 | c.b = clamp(c.b, 0.0f, limit); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 1098 | } |
Mike Klein | 4bc86d5 | 2020-01-06 18:39:42 -0600 | [diff] [blame] | 1099 | |
Mike Klein | ef0fa43 | 2020-07-29 12:35:45 -0500 | [diff] [blame] | 1100 | return SkColorSpaceXformSteps{cs,at, dst.colorSpace(),dst.alphaType()}.program(p, uniforms, c); |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 1101 | } |