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 | |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 8 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/core/SkBitmapController.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkColorSpacePriv.h" |
| 11 | #include "src/core/SkColorSpaceXformSteps.h" |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 12 | #include "src/core/SkOpts.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkRasterPipeline.h" |
| 14 | #include "src/core/SkReadBuffer.h" |
| 15 | #include "src/core/SkWriteBuffer.h" |
| 16 | #include "src/image/SkImage_Base.h" |
| 17 | #include "src/shaders/SkBitmapProcShader.h" |
| 18 | #include "src/shaders/SkEmptyShader.h" |
| 19 | #include "src/shaders/SkImageShader.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 20 | |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 21 | /** |
| 22 | * We are faster in clamp, so always use that tiling when we can. |
| 23 | */ |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 24 | static SkTileMode optimize(SkTileMode tm, int dimension) { |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 25 | SkASSERT(dimension > 0); |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 26 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 27 | // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow |
| 28 | // for transforming to clamp. |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 29 | return tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 30 | #else |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 31 | return dimension == 1 ? SkTileMode::kClamp : tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 32 | #endif |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 35 | SkImageShader::SkImageShader(sk_sp<SkImage> img, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 36 | SkTileMode tmx, SkTileMode tmy, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 37 | const SkMatrix* localMatrix, |
| 38 | bool clampAsIfUnpremul) |
| 39 | : INHERITED(localMatrix) |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 40 | , fImage(std::move(img)) |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 41 | , fTileModeX(optimize(tmx, fImage->width())) |
| 42 | , fTileModeY(optimize(tmy, fImage->height())) |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 43 | , fClampAsIfUnpremul(clampAsIfUnpremul) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 44 | {} |
| 45 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 46 | // fClampAsIfUnpremul is always false when constructed through public APIs, |
| 47 | // so there's no need to read or write it here. |
| 48 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 49 | sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 50 | auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
| 51 | auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 52 | SkMatrix localMatrix; |
| 53 | buffer.readMatrix(&localMatrix); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 54 | sk_sp<SkImage> img = buffer.readImage(); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 55 | if (!img) { |
| 56 | return nullptr; |
| 57 | } |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 58 | return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void SkImageShader::flatten(SkWriteBuffer& buffer) const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 62 | buffer.writeUInt((unsigned)fTileModeX); |
| 63 | buffer.writeUInt((unsigned)fTileModeY); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 64 | buffer.writeMatrix(this->getLocalMatrix()); |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 65 | buffer.writeImage(fImage.get()); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 66 | SkASSERT(fClampAsIfUnpremul == false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool SkImageShader::isOpaque() const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 70 | return fImage->isOpaque() && |
| 71 | fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 74 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 75 | static bool legacy_shader_can_handle(const SkMatrix& inv) { |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 76 | if (inv.hasPerspective()) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Scale+translate methods are always present, but affine might not be. |
| 81 | if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) { |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates |
| 86 | // out of range. |
| 87 | const SkScalar max_dev_coord = 32767.0f; |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 88 | 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] | 89 | |
Mike Reed | 1eb5ca4 | 2018-03-08 14:20:52 -0500 | [diff] [blame] | 90 | // take 1/4 of max signed 32bits so we have room to subtract local values |
| 91 | const SkScalar max_fixed32dot32 = SK_MaxS32 * 0.25f; |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 92 | if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32, |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 93 | +max_fixed32dot32, +max_fixed32dot32).contains(src)) { |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
| 97 | // legacy shader impl should be able to handle these matrices |
| 98 | return true; |
| 99 | } |
| 100 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 101 | SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec, |
| 102 | SkArenaAlloc* alloc) const { |
Brian Osman | 0e18937 | 2018-10-19 11:58:29 -0400 | [diff] [blame] | 103 | if (fImage->alphaType() == kUnpremul_SkAlphaType) { |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 104 | return nullptr; |
| 105 | } |
Brian Osman | b70fd91 | 2018-10-22 16:10:44 -0400 | [diff] [blame] | 106 | if (fImage->colorType() != kN32_SkColorType) { |
| 107 | return nullptr; |
| 108 | } |
Bharat Ahuja | a7e4fe4 | 2019-11-14 23:53:58 +0530 | [diff] [blame] | 109 | #if !defined(SK_SUPPORT_LEGACY_TILED_BITMAPS) |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 110 | if (fTileModeX != fTileModeY) { |
| 111 | return nullptr; |
| 112 | } |
Bharat Ahuja | a7e4fe4 | 2019-11-14 23:53:58 +0530 | [diff] [blame] | 113 | #endif |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 114 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 115 | return nullptr; |
| 116 | } |
| 117 | |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 118 | // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, |
Mike Klein | 67761eb | 2018-12-18 10:16:53 -0500 | [diff] [blame] | 119 | // so it can't handle bitmaps larger than 65535. |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 120 | // |
Mike Klein | 67761eb | 2018-12-18 10:16:53 -0500 | [diff] [blame] | 121 | // We back off another bit to 32767 to make small amounts of |
| 122 | // intermediate math safe, e.g. in |
| 123 | // |
| 124 | // SkFixed fx = ...; |
| 125 | // fx = tile(fx + SK_Fixed1); |
| 126 | // |
| 127 | // we want to make sure (fx + SK_Fixed1) never overflows. |
| 128 | if (fImage-> width() > 32767 || |
| 129 | fImage->height() > 32767) { |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 130 | return nullptr; |
| 131 | } |
| 132 | |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 133 | SkMatrix inv; |
| 134 | if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) || |
| 135 | !legacy_shader_can_handle(inv)) { |
| 136 | return nullptr; |
| 137 | } |
| 138 | |
Mike Reed | 011d166 | 2019-02-28 17:19:25 -0500 | [diff] [blame] | 139 | if (!rec.isLegacyCompatible(fImage->colorSpace())) { |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 143 | return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY, |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 144 | as_IB(fImage.get()), rec, alloc); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 145 | } |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 146 | #endif |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 147 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 148 | SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const { |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 149 | if (texM) { |
| 150 | *texM = this->getLocalMatrix(); |
| 151 | } |
| 152 | if (xy) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 153 | xy[0] = fTileModeX; |
| 154 | xy[1] = fTileModeY; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 155 | } |
| 156 | return const_cast<SkImage*>(fImage.get()); |
| 157 | } |
| 158 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 159 | sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 160 | SkTileMode tmx, SkTileMode tmy, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 161 | const SkMatrix* localMatrix, |
| 162 | bool clampAsIfUnpremul) { |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 163 | if (!image) { |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 164 | return sk_make_sp<SkEmptyShader>(); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 165 | } |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 166 | return sk_sp<SkShader>{ new SkImageShader(image, tmx, tmy, localMatrix, clampAsIfUnpremul) }; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 167 | } |
| 168 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 169 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 170 | |
| 171 | #if SK_SUPPORT_GPU |
| 172 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 173 | #include "include/private/GrRecordingContext.h" |
| 174 | #include "src/gpu/GrCaps.h" |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 175 | #include "src/gpu/GrColorInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 176 | #include "src/gpu/GrRecordingContextPriv.h" |
| 177 | #include "src/gpu/SkGr.h" |
| 178 | #include "src/gpu/effects/GrBicubicEffect.h" |
Brian Salomon | 90673ec | 2020-01-06 15:50:57 -0500 | [diff] [blame] | 179 | #include "src/gpu/effects/GrTextureEffect.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 180 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 181 | static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkTileMode tileMode) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 182 | switch (tileMode) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 183 | case SkTileMode::kClamp: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 184 | return GrSamplerState::WrapMode::kClamp; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 185 | case SkTileMode::kRepeat: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 186 | return GrSamplerState::WrapMode::kRepeat; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 187 | case SkTileMode::kMirror: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 188 | return GrSamplerState::WrapMode::kMirrorRepeat; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 189 | case SkTileMode::kDecal: |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 190 | return GrSamplerState::WrapMode::kClampToBorder; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 191 | } |
| 192 | SK_ABORT("Unknown tile mode."); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 195 | std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor( |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 196 | const GrFPArgs& args) const { |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 197 | const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 198 | SkMatrix lmInverse; |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 199 | if (!lm->invert(&lmInverse)) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 200 | return nullptr; |
| 201 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 202 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 203 | GrSamplerState::WrapMode wrapModes[] = {tile_mode_to_wrap_mode(fTileModeX), |
| 204 | tile_mode_to_wrap_mode(fTileModeY)}; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 205 | |
| 206 | // If either domainX or domainY are un-ignored, a texture domain effect has to be used to |
| 207 | // implement the decal mode (while leaving non-decal axes alone). The wrap mode originally |
| 208 | // clamp-to-border is reset to clamp since the hw cannot implement it directly. |
| 209 | GrTextureDomain::Mode domainX = GrTextureDomain::kIgnore_Mode; |
| 210 | GrTextureDomain::Mode domainY = GrTextureDomain::kIgnore_Mode; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 211 | if (!args.fContext->priv().caps()->clampToBorderSupport()) { |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 212 | if (wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 213 | domainX = GrTextureDomain::kDecal_Mode; |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 214 | wrapModes[0] = GrSamplerState::WrapMode::kClamp; |
| 215 | } |
| 216 | if (wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 217 | domainY = GrTextureDomain::kDecal_Mode; |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 218 | wrapModes[1] = GrSamplerState::WrapMode::kClamp; |
| 219 | } |
| 220 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 221 | |
| 222 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
| 223 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
| 224 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
| 225 | // are provided by the caller. |
| 226 | bool doBicubic; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 227 | GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode( |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 228 | fImage->width(), fImage->height(), args.fFilterQuality, *args.fViewMatrix, *lm, |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 229 | args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 230 | GrSamplerState samplerState(wrapModes, textureFilterMode); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 231 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 232 | sk_sp<GrTextureProxy> proxy(as_IB(fImage)->asTextureProxyRef(args.fContext, samplerState, |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 233 | scaleAdjust)); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 234 | if (!proxy) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 235 | return nullptr; |
| 236 | } |
| 237 | |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 238 | SkAlphaType srcAlphaType = fImage->alphaType(); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 239 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 240 | lmInverse.postScale(scaleAdjust[0], scaleAdjust[1]); |
| 241 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 242 | std::unique_ptr<GrFragmentProcessor> inner; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 243 | if (doBicubic) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 244 | // domainX and domainY will properly apply the decal effect with the texture domain used in |
| 245 | // the bicubic filter if clamp to border was unsupported in hardware |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 246 | static constexpr auto kDir = GrBicubicEffect::Direction::kXY; |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 247 | inner = GrBicubicEffect::Make(std::move(proxy), lmInverse, wrapModes, domainX, domainY, |
| 248 | kDir, srcAlphaType); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 249 | } else { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 250 | auto dimensions = proxy->dimensions(); |
Brian Salomon | 90673ec | 2020-01-06 15:50:57 -0500 | [diff] [blame] | 251 | inner = GrTextureEffect::Make(std::move(proxy), srcAlphaType, lmInverse, samplerState); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 252 | if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 253 | SkRect domain = GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(dimensions), |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 254 | domainX, domainY); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 255 | inner = GrDomainEffect::Make(std::move(inner), domain, domainX, domainY, |
| 256 | samplerState.filter()); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 257 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 258 | } |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 259 | inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(), srcAlphaType, |
| 260 | args.fDstColorInfo->colorSpace()); |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 261 | |
| 262 | bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType()); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 263 | if (isAlphaOnly) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 264 | return inner; |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 265 | } else if (args.fInputColorIsOpaque) { |
| 266 | return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 267 | } |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 268 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | #endif |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 272 | |
| 273 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 274 | #include "src/core/SkImagePriv.h" |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 275 | |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 276 | sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy, |
| 277 | const SkMatrix* localMatrix, SkCopyPixelsMode cpm) { |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 278 | return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm), |
| 279 | tmx, tmy, localMatrix); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 282 | sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 283 | SkTileMode tmx, SkTileMode tmy, |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 284 | const SkMatrix* localMatrix, SkCopyPixelsMode mode) { |
| 285 | auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode); |
| 286 | if (!s) { |
| 287 | return nullptr; |
| 288 | } |
| 289 | if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) { |
| 290 | // Compose the image shader with the paint's shader. Alpha images+shaders should output the |
| 291 | // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with |
| 292 | // the source image and dst shader (MakeBlend takes dst first, src second). |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 293 | s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s)); |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 294 | } |
| 295 | return s; |
| 296 | } |
| 297 | |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 298 | void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); } |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 299 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 300 | class SkImageStageUpdater : public SkStageUpdater { |
| 301 | public: |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 302 | SkImageStageUpdater(const SkImageShader* shader, bool usePersp) |
| 303 | : fShader(shader), fUsePersp(usePersp) |
| 304 | {} |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 305 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 306 | const SkImageShader* fShader; |
| 307 | const bool fUsePersp; // else use affine |
| 308 | |
| 309 | // large enough for perspective, though often we just use 2x3 |
| 310 | float fMatrixStorage[9]; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 311 | |
| 312 | #if 0 // TODO: when we support mipmaps |
| 313 | SkRasterPipeline_GatherCtx* fGather; |
| 314 | SkRasterPipeline_TileCtx* fLimitX; |
| 315 | SkRasterPipeline_TileCtx* fLimitY; |
| 316 | SkRasterPipeline_DecalTileCtx* fDecal; |
| 317 | #endif |
| 318 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 319 | void append_matrix_stage(SkRasterPipeline* p) { |
| 320 | if (fUsePersp) { |
| 321 | p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage); |
| 322 | } else { |
| 323 | p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage); |
| 324 | } |
| 325 | } |
| 326 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 327 | bool update(const SkMatrix& ctm, const SkMatrix* localM) override { |
| 328 | SkMatrix matrix; |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 329 | if (fShader->computeTotalInverse(ctm, localM, &matrix)) { |
| 330 | if (fUsePersp) { |
| 331 | matrix.get9(fMatrixStorage); |
| 332 | } else { |
| 333 | SkAssertResult(matrix.asAffine(fMatrixStorage)); |
| 334 | } |
| 335 | return true; |
| 336 | } |
| 337 | return false; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 338 | } |
| 339 | }; |
| 340 | |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 341 | static void tweak_quality_and_inv_matrix(SkFilterQuality* quality, SkMatrix* matrix) { |
| 342 | // When the matrix is just an integer translate, bilerp == nearest neighbor. |
| 343 | if (*quality == kLow_SkFilterQuality && |
| 344 | matrix->getType() <= SkMatrix::kTranslate_Mask && |
| 345 | matrix->getTranslateX() == (int)matrix->getTranslateX() && |
| 346 | matrix->getTranslateY() == (int)matrix->getTranslateY()) { |
| 347 | *quality = kNone_SkFilterQuality; |
| 348 | } |
| 349 | |
| 350 | // See skia:4649 and the GM image_scale_aligned. |
| 351 | if (*quality == kNone_SkFilterQuality) { |
| 352 | if (matrix->getScaleX() >= 0) { |
| 353 | matrix->setTranslateX(nextafterf(matrix->getTranslateX(), |
| 354 | floorf(matrix->getTranslateX()))); |
| 355 | } |
| 356 | if (matrix->getScaleY() >= 0) { |
| 357 | matrix->setTranslateY(nextafterf(matrix->getTranslateY(), |
| 358 | floorf(matrix->getTranslateY()))); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 363 | bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 364 | if (updater && rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality) { |
| 365 | // TODO: medium: recall RequestBitmap and update width/height accordingly |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 369 | SkRasterPipeline* p = rec.fPipeline; |
| 370 | SkArenaAlloc* alloc = rec.fAlloc; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 371 | auto quality = rec.fPaint.getFilterQuality(); |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 372 | |
Florin Malita | 7558e4d | 2018-02-07 10:05:53 -0500 | [diff] [blame] | 373 | SkMatrix matrix; |
| 374 | if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) { |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 375 | return false; |
| 376 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 377 | |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 378 | const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), |
| 379 | matrix, quality, alloc); |
Mike Klein | f447dee | 2016-11-29 16:37:12 -0500 | [diff] [blame] | 380 | if (!state) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | const SkPixmap& pm = state->pixmap(); |
| 385 | matrix = state->invMatrix(); |
| 386 | quality = state->quality(); |
| 387 | auto info = pm.info(); |
| 388 | |
Mike Klein | e8de024 | 2018-03-10 12:37:11 -0500 | [diff] [blame] | 389 | p->append(SkRasterPipeline::seed_shader); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 390 | |
| 391 | if (updater) { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 392 | updater->append_matrix_stage(p); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 393 | } else { |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 394 | tweak_quality_and_inv_matrix(&quality, &matrix); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 395 | p->append_matrix(alloc, matrix); |
| 396 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 397 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 398 | auto gather = alloc->make<SkRasterPipeline_GatherCtx>(); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 399 | gather->pixels = pm.addr(); |
Mike Klein | 968af43 | 2017-07-18 16:31:55 -0400 | [diff] [blame] | 400 | gather->stride = pm.rowBytesAsPixels(); |
Mike Klein | f3b4e16 | 2017-09-22 15:32:59 -0400 | [diff] [blame] | 401 | gather->width = pm.width(); |
| 402 | gather->height = pm.height(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 403 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 404 | auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(), |
| 405 | limit_y = alloc->make<SkRasterPipeline_TileCtx>(); |
Mike Reed | 51e46d5 | 2017-06-23 14:21:25 -0400 | [diff] [blame] | 406 | limit_x->scale = pm.width(); |
| 407 | limit_x->invScale = 1.0f / pm.width(); |
| 408 | limit_y->scale = pm.height(); |
| 409 | limit_y->invScale = 1.0f / pm.height(); |
Mike Klein | fc84dc5 | 2017-05-11 15:29:31 -0400 | [diff] [blame] | 410 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 411 | SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 412 | bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal; |
| 413 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 414 | decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>(); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 415 | decal_ctx->limit_x = limit_x->scale; |
| 416 | decal_ctx->limit_y = limit_y->scale; |
| 417 | } |
| 418 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 419 | #if 0 // TODO: when we support kMedium |
| 420 | if (updator && (quality == kMedium_SkFilterQuality)) { |
| 421 | // if we change levels in mipmap, we need to update the scales (and invScales) |
| 422 | updator->fGather = gather; |
| 423 | updator->fLimitX = limit_x; |
| 424 | updator->fLimitY = limit_y; |
| 425 | updator->fDecal = decal_ctx; |
| 426 | } |
| 427 | #endif |
| 428 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 429 | auto append_tiling_and_gather = [&] { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 430 | if (decal_x_and_y) { |
| 431 | p->append(SkRasterPipeline::decal_x_and_y, decal_ctx); |
| 432 | } else { |
| 433 | switch (fTileModeX) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 434 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 435 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break; |
| 436 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break; |
| 437 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 438 | } |
| 439 | switch (fTileModeY) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 440 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 441 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break; |
| 442 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break; |
| 443 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 444 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 445 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 446 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 447 | void* ctx = gather; |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 448 | switch (info.colorType()) { |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 449 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 450 | case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break; |
| 451 | case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 452 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break; |
| 453 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 454 | case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break; |
| 455 | case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break; |
| 456 | case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 457 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break; |
| 458 | case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 459 | case kR16G16B16A16_unorm_SkColorType: |
| 460 | p->append(SkRasterPipeline::gather_16161616,ctx); break; |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 461 | case kRGBA_F16Norm_SkColorType: |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 462 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 463 | case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 464 | |
Mike Klein | b1df5e5 | 2018-10-17 17:06:03 -0400 | [diff] [blame] | 465 | case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); |
| 466 | p->append(SkRasterPipeline::alpha_to_gray ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 467 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 468 | case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 469 | p->append(SkRasterPipeline::force_opaque ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 470 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 471 | case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 472 | p->append(SkRasterPipeline::force_opaque ); break; |
| 473 | |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 474 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 475 | p->append(SkRasterPipeline::swap_rb ); break; |
| 476 | |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 477 | case kUnknown_SkColorType: SkASSERT(false); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 478 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 479 | if (decal_ctx) { |
| 480 | p->append(SkRasterPipeline::check_decal_mask, decal_ctx); |
| 481 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 482 | }; |
| 483 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 484 | auto append_misc = [&] { |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 485 | // This is an inessential optimization... it's logically safe to set this to false. |
| 486 | // But if... |
| 487 | // - we know the image is definitely normalized, and |
| 488 | // - we're doing some color space conversion, and |
| 489 | // - sRGB curves are involved, |
| 490 | // then we can use slightly faster math that doesn't work well outside [0,1]. |
| 491 | bool src_is_normalized = SkColorTypeIsNormalized(info.colorType()); |
| 492 | |
Mike Klein | 6a9f1c4 | 2020-01-02 23:23:42 -0600 | [diff] [blame] | 493 | SkColorSpace* cs = info.colorSpace(); |
| 494 | SkAlphaType at = info.alphaType(); |
| 495 | |
| 496 | // Color for A8 images comes from the paint. TODO: all alpha images? none? |
| 497 | if (info.colorType() == kAlpha_8_SkColorType) { |
| 498 | SkColor4f rgb = rec.fPaint.getColor4f(); |
| 499 | p->append_set_rgb(alloc, rgb); |
| 500 | |
| 501 | src_is_normalized = rgb.fitsInBytes(); |
| 502 | cs = sk_srgb_singleton(); |
| 503 | at = kUnpremul_SkAlphaType; |
| 504 | } |
| 505 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 506 | // 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] | 507 | if (quality == kHigh_SkFilterQuality) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 508 | p->append(SkRasterPipeline::clamp_0); |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 509 | p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul |
| 510 | ? SkRasterPipeline::clamp_1 |
| 511 | : SkRasterPipeline::clamp_a); |
| 512 | src_is_normalized = true; |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 513 | } |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 514 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 515 | // Transform color space and alpha type to match shader convention (dst CS, premul alpha). |
| 516 | alloc->make<SkColorSpaceXformSteps>(cs, at, |
| 517 | rec.fDstCS, kPremul_SkAlphaType) |
| 518 | ->apply(p, src_is_normalized); |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 519 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 520 | return true; |
| 521 | }; |
| 522 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 523 | // Check for fast-path stages. |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 524 | auto ct = info.colorType(); |
| 525 | if (true |
| 526 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 527 | && quality == kLow_SkFilterQuality |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 528 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 529 | |
| 530 | p->append(SkRasterPipeline::bilerp_clamp_8888, gather); |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 531 | if (ct == kBGRA_8888_SkColorType) { |
| 532 | p->append(SkRasterPipeline::swap_rb); |
| 533 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 534 | return append_misc(); |
| 535 | } |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 536 | if (true |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 537 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 538 | && quality == kLow_SkFilterQuality |
| 539 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 540 | && fTileModeY != SkTileMode::kDecal) { |
| 541 | |
| 542 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 543 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 544 | ctx->ct = ct; |
| 545 | ctx->tileX = fTileModeX; |
| 546 | ctx->tileY = fTileModeY; |
| 547 | ctx->invWidth = 1.0f / ctx->width; |
| 548 | ctx->invHeight = 1.0f / ctx->height; |
| 549 | p->append(SkRasterPipeline::bilinear, ctx); |
| 550 | return append_misc(); |
| 551 | } |
| 552 | if (true |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 553 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 554 | && quality == kHigh_SkFilterQuality |
| 555 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
| 556 | |
| 557 | p->append(SkRasterPipeline::bicubic_clamp_8888, gather); |
| 558 | if (ct == kBGRA_8888_SkColorType) { |
| 559 | p->append(SkRasterPipeline::swap_rb); |
| 560 | } |
| 561 | return append_misc(); |
| 562 | } |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 563 | if (true |
| 564 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 565 | && quality == kHigh_SkFilterQuality |
| 566 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 567 | && fTileModeY != SkTileMode::kDecal) { |
| 568 | |
| 569 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 570 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 571 | ctx->ct = ct; |
| 572 | ctx->tileX = fTileModeX; |
| 573 | ctx->tileY = fTileModeY; |
| 574 | ctx->invWidth = 1.0f / ctx->width; |
| 575 | ctx->invHeight = 1.0f / ctx->height; |
| 576 | p->append(SkRasterPipeline::bicubic, ctx); |
| 577 | return append_misc(); |
| 578 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 579 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 580 | SkRasterPipeline_SamplerCtx* sampler = nullptr; |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 581 | if (quality != kNone_SkFilterQuality) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 582 | sampler = alloc->make<SkRasterPipeline_SamplerCtx>(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 583 | } |
| 584 | |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 585 | auto sample = [&](SkRasterPipeline::StockStage setup_x, |
| 586 | SkRasterPipeline::StockStage setup_y) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 587 | p->append(setup_x, sampler); |
| 588 | p->append(setup_y, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 589 | append_tiling_and_gather(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 590 | p->append(SkRasterPipeline::accumulate, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 591 | }; |
| 592 | |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 593 | if (quality == kNone_SkFilterQuality) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 594 | append_tiling_and_gather(); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 595 | } else if (quality == kLow_SkFilterQuality) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 596 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 597 | |
| 598 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny); |
| 599 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny); |
| 600 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py); |
| 601 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py); |
| 602 | |
| 603 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 604 | |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 605 | } else { |
Mike Klein | 250138d | 2020-01-02 09:39:26 -0600 | [diff] [blame] | 606 | SkASSERT(quality == kHigh_SkFilterQuality); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 607 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 608 | |
| 609 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y); |
| 610 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y); |
| 611 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y); |
| 612 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y); |
| 613 | |
| 614 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y); |
| 615 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y); |
| 616 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y); |
| 617 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y); |
| 618 | |
| 619 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y); |
| 620 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y); |
| 621 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y); |
| 622 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y); |
| 623 | |
| 624 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y); |
| 625 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y); |
| 626 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y); |
| 627 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y); |
| 628 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 629 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 630 | } |
| 631 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 632 | return append_misc(); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 633 | } |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 634 | |
| 635 | bool SkImageShader::onAppendStages(const SkStageRec& rec) const { |
| 636 | return this->doStages(rec, nullptr); |
| 637 | } |
| 638 | |
| 639 | SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 640 | bool usePersp = rec.fCTM.hasPerspective(); |
| 641 | auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 642 | return this->doStages(rec, updater) ? updater : nullptr; |
| 643 | } |
| 644 | |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 645 | bool SkImageShader::onProgram(skvm::Builder* p, |
| 646 | const SkMatrix& ctm, const SkMatrix* localM, |
| 647 | SkFilterQuality quality, SkColorSpace* dstCS, |
| 648 | skvm::Uniforms* uniforms, |
| 649 | skvm::F32 x, skvm::F32 y, |
| 650 | skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const { |
| 651 | SkMatrix inv; |
| 652 | if (!this->computeTotalInverse(ctm, localM, &inv)) { |
| 653 | return false; |
| 654 | } |
| 655 | |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 656 | // TODO: need to extend lifetime of this once we start supporting kMedium_SkFilterQuality. |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 657 | SkBitmapController::State state{as_IB(fImage.get()), inv, quality}; |
| 658 | const SkPixmap& pm = state.pixmap(); |
| 659 | if (!pm.addr()) { |
| 660 | return false; |
| 661 | } |
| 662 | inv = state.invMatrix(); |
| 663 | quality = state.quality(); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 664 | tweak_quality_and_inv_matrix(&quality, &inv); |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 665 | |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 666 | // TODO: and need to extend lifetime of this too once supporting steps.flags.mask() != 0. |
| 667 | SkColorSpaceXformSteps steps{pm.colorSpace(), pm.alphaType(), |
| 668 | dstCS, kPremul_SkAlphaType}; |
| 669 | |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 670 | if (steps.flags.mask() != 0) { return false; } |
| 671 | if (quality != kNone_SkFilterQuality) { return false; } |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 672 | |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 673 | // Apply matrix to convert dst coords to sample coords. |
| 674 | inv.normalizePerspective(); |
| 675 | if (inv.isIdentity()) { |
| 676 | // That was easy. |
| 677 | } else if (inv.isTranslate()) { |
| 678 | x = p->add(x, p->uniformF(uniforms->pushF(inv[2]))); |
| 679 | y = p->add(y, p->uniformF(uniforms->pushF(inv[5]))); |
| 680 | } else if (inv.isScaleTranslate()) { |
| 681 | x = p->mad(x, p->uniformF(uniforms->pushF(inv[0])), p->uniformF(uniforms->pushF(inv[2]))); |
| 682 | y = p->mad(y, p->uniformF(uniforms->pushF(inv[4])), p->uniformF(uniforms->pushF(inv[5]))); |
| 683 | } else { // Affine or perspective. |
| 684 | auto dot = [&,x,y](int row) { |
| 685 | return p->mad(x, p->uniformF(uniforms->pushF(inv[3*row+0])), |
| 686 | p->mad(y, p->uniformF(uniforms->pushF(inv[3*row+1])), |
| 687 | p->uniformF(uniforms->pushF(inv[3*row+2])))); |
| 688 | }; |
| 689 | x = dot(0); |
| 690 | y = dot(1); |
| 691 | if (inv.hasPerspective()) { |
| 692 | x = p->div(x, dot(2)); |
| 693 | y = p->div(y, dot(2)); |
| 694 | } |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 695 | } |
| 696 | |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 697 | // Apply tile mode. |
| 698 | // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp. |
| 699 | auto repeat = [&](skvm::F32 v, float scale) { |
| 700 | skvm::F32 S = p->uniformF(uniforms->pushF( scale)), |
| 701 | I = p->uniformF(uniforms->pushF(1.0f/scale)); |
| 702 | // v - floor(v/scale)*scale |
| 703 | return p->sub(v, p->mul(p->floor(p->mul(v,I)), S)); |
| 704 | }; |
| 705 | auto mirror = [&](skvm::F32 v, float scale) { |
| 706 | skvm::F32 S = p->uniformF(uniforms->pushF( scale)), |
| 707 | I2 = p->uniformF(uniforms->pushF(0.5f/scale)); |
| 708 | // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale ) |
| 709 | // {---A---} {------------------B------------------} |
| 710 | skvm::F32 A = p->sub(v,S), |
| 711 | B = p->mul(p->add(S,S), p->floor(p->mul(A,I2))); |
| 712 | return p->abs(p->sub(p->sub(A,B), S)); |
| 713 | }; |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 714 | switch (fTileModeX) { |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 715 | case SkTileMode::kDecal: /* handled after gather */ break; |
| 716 | case SkTileMode::kClamp: /* we always clamp */ break; |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 717 | case SkTileMode::kRepeat: x = repeat(x, pm.width()); break; |
| 718 | case SkTileMode::kMirror: x = mirror(x, pm.width()); break; |
| 719 | } |
| 720 | switch (fTileModeY) { |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 721 | case SkTileMode::kDecal: /* handled after gather */ break; |
| 722 | case SkTileMode::kClamp: /* we always clamp */ break; |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 723 | case SkTileMode::kRepeat: y = repeat(y, pm.height()); break; |
| 724 | case SkTileMode::kMirror: y = mirror(y, pm.height()); break; |
| 725 | } |
| 726 | |
| 727 | // Clamp sample coordinates to [0,width), [0,height), |
| 728 | // both for memory safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror. |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 729 | auto clamp = [&](skvm::F32 v, float limit) { |
| 730 | // Subtract one ulp to make an exclusive limit inclusive. |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 731 | int bits; |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 732 | memcpy(&bits, &limit, 4); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 733 | bits--; |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 734 | memcpy(&limit, &bits, 4); |
| 735 | return p->max(p->splat(0.0f), p->min(v, p->uniformF(uniforms->pushF(limit)))); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 736 | }; |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 737 | skvm::F32 clamped_x = clamp(x, pm. width()), |
| 738 | clamped_y = clamp(y, pm.height()); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 739 | |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 740 | // Load pixels from pm.addr()[(int)x + (int)y*stride]. |
| 741 | skvm::Builder::Uniform img = uniforms->pushPtr(pm.addr()); |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 742 | skvm::I32 index = p->add(p->trunc(clamped_x), |
| 743 | p->mul(p->trunc(clamped_y), |
| 744 | p->uniform32(uniforms->push(pm.rowBytesAsPixels())))); |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 745 | skvm::Color c; |
| 746 | switch (pm.colorType()) { |
| 747 | default: return false; |
| 748 | |
| 749 | case kRGB_565_SkColorType: c = p->unpack_565(p->gather16(img, index)); break; |
| 750 | |
| 751 | case kRGBA_8888_SkColorType: c = p->unpack_8888(p->gather32(img, index)); break; |
| 752 | case kBGRA_8888_SkColorType: c = p->unpack_8888(p->gather32(img, index)); |
| 753 | std::swap(c.r, c.b); |
| 754 | break; |
| 755 | } |
| 756 | |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame^] | 757 | // Mask away any pixels that we tried to sample outside the bounds in kDecal. |
| 758 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
| 759 | skvm::I32 mask = p->splat(~0); |
| 760 | if (fTileModeX == SkTileMode::kDecal) { mask = p->bit_and(mask, p->eq(x, clamped_x)); } |
| 761 | if (fTileModeY == SkTileMode::kDecal) { mask = p->bit_and(mask, p->eq(y, clamped_y)); } |
| 762 | c.r = p->bit_cast(p->bit_and(mask, p->bit_cast(c.r))); |
| 763 | c.g = p->bit_cast(p->bit_and(mask, p->bit_cast(c.g))); |
| 764 | c.b = p->bit_cast(p->bit_and(mask, p->bit_cast(c.b))); |
| 765 | c.a = p->bit_cast(p->bit_and(mask, p->bit_cast(c.a))); |
| 766 | } |
| 767 | |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 768 | *r = c.r; |
| 769 | *g = c.g; |
| 770 | *b = c.b; |
| 771 | *a = c.a; |
| 772 | return true; |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 773 | } |
| 774 | |