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