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