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