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" |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame^] | 12 | #include "src/core/SkMatrixProvider.h" |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 13 | #include "src/core/SkOpts.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/core/SkRasterPipeline.h" |
| 15 | #include "src/core/SkReadBuffer.h" |
Mike Klein | 8e71744 | 2020-01-07 10:22:33 -0600 | [diff] [blame] | 16 | #include "src/core/SkVM.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkWriteBuffer.h" |
| 18 | #include "src/image/SkImage_Base.h" |
| 19 | #include "src/shaders/SkBitmapProcShader.h" |
| 20 | #include "src/shaders/SkEmptyShader.h" |
| 21 | #include "src/shaders/SkImageShader.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 22 | |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 23 | /** |
| 24 | * We are faster in clamp, so always use that tiling when we can. |
| 25 | */ |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 26 | static SkTileMode optimize(SkTileMode tm, int dimension) { |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 27 | SkASSERT(dimension > 0); |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 28 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 29 | // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow |
| 30 | // for transforming to clamp. |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 31 | return tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 32 | #else |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 33 | return dimension == 1 ? SkTileMode::kClamp : tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 34 | #endif |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 37 | SkImageShader::SkImageShader(sk_sp<SkImage> img, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 38 | SkTileMode tmx, SkTileMode tmy, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 39 | const SkMatrix* localMatrix, |
| 40 | bool clampAsIfUnpremul) |
| 41 | : INHERITED(localMatrix) |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 42 | , fImage(std::move(img)) |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 43 | , fTileModeX(optimize(tmx, fImage->width())) |
| 44 | , fTileModeY(optimize(tmy, fImage->height())) |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 45 | , fClampAsIfUnpremul(clampAsIfUnpremul) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 46 | {} |
| 47 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 48 | // fClampAsIfUnpremul is always false when constructed through public APIs, |
| 49 | // so there's no need to read or write it here. |
| 50 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 51 | sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 52 | auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
| 53 | auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 54 | SkMatrix localMatrix; |
| 55 | buffer.readMatrix(&localMatrix); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 56 | sk_sp<SkImage> img = buffer.readImage(); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 57 | if (!img) { |
| 58 | return nullptr; |
| 59 | } |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 60 | return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void SkImageShader::flatten(SkWriteBuffer& buffer) const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 64 | buffer.writeUInt((unsigned)fTileModeX); |
| 65 | buffer.writeUInt((unsigned)fTileModeY); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 66 | buffer.writeMatrix(this->getLocalMatrix()); |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 67 | buffer.writeImage(fImage.get()); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 68 | SkASSERT(fClampAsIfUnpremul == false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool SkImageShader::isOpaque() const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 72 | return fImage->isOpaque() && |
| 73 | fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 76 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 77 | static bool legacy_shader_can_handle(const SkMatrix& inv) { |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 78 | if (inv.hasPerspective()) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // Scale+translate methods are always present, but affine might not be. |
| 83 | if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) { |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates |
| 88 | // out of range. |
| 89 | const SkScalar max_dev_coord = 32767.0f; |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 90 | 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] | 91 | |
Mike Reed | 1eb5ca4 | 2018-03-08 14:20:52 -0500 | [diff] [blame] | 92 | // 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] | 93 | const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f; |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 94 | if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32, |
Mike Klein | 37bc8f9 | 2019-10-21 13:10:07 -0500 | [diff] [blame] | 95 | +max_fixed32dot32, +max_fixed32dot32).contains(src)) { |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
| 99 | // legacy shader impl should be able to handle these matrices |
| 100 | return true; |
| 101 | } |
| 102 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 103 | SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec, |
| 104 | SkArenaAlloc* alloc) const { |
Brian Osman | 0e18937 | 2018-10-19 11:58:29 -0400 | [diff] [blame] | 105 | if (fImage->alphaType() == kUnpremul_SkAlphaType) { |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 106 | return nullptr; |
| 107 | } |
Brian Osman | b70fd91 | 2018-10-22 16:10:44 -0400 | [diff] [blame] | 108 | if (fImage->colorType() != kN32_SkColorType) { |
| 109 | return nullptr; |
| 110 | } |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 111 | if (fTileModeX != fTileModeY) { |
| 112 | return nullptr; |
| 113 | } |
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 | b8f098d | 2020-01-07 11:15:44 -0500 | [diff] [blame] | 179 | #include "src/gpu/effects/GrTextureEffect.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 180 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 181 | std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor( |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 182 | const GrFPArgs& args) const { |
Florin Malita | 52f0291 | 2020-03-09 16:33:17 -0400 | [diff] [blame] | 183 | const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 184 | SkMatrix lmInverse; |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 185 | if (!lm->invert(&lmInverse)) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 186 | return nullptr; |
| 187 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 188 | |
Brian Salomon | 694ec49 | 2020-04-14 13:39:31 -0400 | [diff] [blame] | 189 | GrSamplerState::WrapMode wmX = SkTileModeToWrapMode(fTileModeX), |
| 190 | wmY = SkTileModeToWrapMode(fTileModeY); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 191 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 192 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
| 193 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
| 194 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
| 195 | // are provided by the caller. |
| 196 | bool doBicubic; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 197 | GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode( |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame^] | 198 | fImage->width(), fImage->height(), args.fFilterQuality, |
| 199 | args.fMatrixProvider.localToDevice(), *lm, |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 200 | args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 201 | GrMipMapped mipMapped = GrMipMapped::kNo; |
| 202 | if (textureFilterMode == GrSamplerState::Filter::kMipMap) { |
| 203 | mipMapped = GrMipMapped::kYes; |
| 204 | } |
| 205 | GrSurfaceProxyView view = as_IB(fImage)->refView(args.fContext, mipMapped); |
Greg Daniel | febdedf | 2020-02-05 17:06:27 -0500 | [diff] [blame] | 206 | if (!view) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 207 | return nullptr; |
| 208 | } |
| 209 | |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 210 | SkAlphaType srcAlphaType = fImage->alphaType(); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 211 | |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 212 | const auto& caps = *args.fContext->priv().caps(); |
| 213 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 214 | std::unique_ptr<GrFragmentProcessor> inner; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 215 | if (doBicubic) { |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 216 | static constexpr auto kDir = GrBicubicEffect::Direction::kXY; |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 217 | inner = GrBicubicEffect::Make(std::move(view), srcAlphaType, lmInverse, wmX, wmY, kDir, |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 218 | caps); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 219 | } else { |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 220 | GrSamplerState samplerState(wmX, wmY, textureFilterMode); |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 221 | inner = GrTextureEffect::Make(std::move(view), srcAlphaType, lmInverse, samplerState, caps); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 222 | } |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 223 | inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(), srcAlphaType, |
| 224 | args.fDstColorInfo->colorSpace()); |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 225 | |
| 226 | bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType()); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 227 | if (isAlphaOnly) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 228 | return inner; |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 229 | } else if (args.fInputColorIsOpaque) { |
| 230 | return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 231 | } |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 232 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | #endif |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 236 | |
| 237 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 238 | #include "src/core/SkImagePriv.h" |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 239 | |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 240 | sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy, |
| 241 | const SkMatrix* localMatrix, SkCopyPixelsMode cpm) { |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 242 | return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm), |
| 243 | tmx, tmy, localMatrix); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 246 | sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 247 | SkTileMode tmx, SkTileMode tmy, |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 248 | const SkMatrix* localMatrix, SkCopyPixelsMode mode) { |
| 249 | auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode); |
| 250 | if (!s) { |
| 251 | return nullptr; |
| 252 | } |
| 253 | if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) { |
| 254 | // Compose the image shader with the paint's shader. Alpha images+shaders should output the |
| 255 | // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with |
| 256 | // the source image and dst shader (MakeBlend takes dst first, src second). |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 257 | s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s)); |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 258 | } |
| 259 | return s; |
| 260 | } |
| 261 | |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 262 | void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); } |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 263 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 264 | class SkImageStageUpdater : public SkStageUpdater { |
| 265 | public: |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 266 | SkImageStageUpdater(const SkImageShader* shader, bool usePersp) |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 267 | : fShader(shader) |
| 268 | , fUsePersp(usePersp || as_SB(shader)->getLocalMatrix().hasPerspective()) |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 269 | {} |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 270 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 271 | const SkImageShader* fShader; |
| 272 | const bool fUsePersp; // else use affine |
| 273 | |
| 274 | // large enough for perspective, though often we just use 2x3 |
| 275 | float fMatrixStorage[9]; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 276 | |
| 277 | #if 0 // TODO: when we support mipmaps |
| 278 | SkRasterPipeline_GatherCtx* fGather; |
| 279 | SkRasterPipeline_TileCtx* fLimitX; |
| 280 | SkRasterPipeline_TileCtx* fLimitY; |
| 281 | SkRasterPipeline_DecalTileCtx* fDecal; |
| 282 | #endif |
| 283 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 284 | void append_matrix_stage(SkRasterPipeline* p) { |
| 285 | if (fUsePersp) { |
| 286 | p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage); |
| 287 | } else { |
| 288 | p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage); |
| 289 | } |
| 290 | } |
| 291 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 292 | bool update(const SkMatrix& ctm, const SkMatrix* localM) override { |
| 293 | SkMatrix matrix; |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 294 | if (fShader->computeTotalInverse(ctm, localM, &matrix)) { |
| 295 | if (fUsePersp) { |
| 296 | matrix.get9(fMatrixStorage); |
| 297 | } else { |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 298 | // if we get here, matrix should be affine. If it isn't, then defensively we |
| 299 | // won't draw (by returning false), but we should work to never let this |
| 300 | // happen (i.e. better preflight by the caller to know ahead of time that we |
| 301 | // may encounter perspective, either in the CTM, or in the localM). |
| 302 | // |
| 303 | // See https://bugs.chromium.org/p/skia/issues/detail?id=10004 |
| 304 | // |
| 305 | if (!matrix.asAffine(fMatrixStorage)) { |
| 306 | SkASSERT(false); |
| 307 | return false; |
| 308 | } |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 309 | } |
| 310 | return true; |
| 311 | } |
| 312 | return false; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 313 | } |
| 314 | }; |
| 315 | |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 316 | static void tweak_quality_and_inv_matrix(SkFilterQuality* quality, SkMatrix* matrix) { |
| 317 | // When the matrix is just an integer translate, bilerp == nearest neighbor. |
| 318 | if (*quality == kLow_SkFilterQuality && |
| 319 | matrix->getType() <= SkMatrix::kTranslate_Mask && |
| 320 | matrix->getTranslateX() == (int)matrix->getTranslateX() && |
| 321 | matrix->getTranslateY() == (int)matrix->getTranslateY()) { |
| 322 | *quality = kNone_SkFilterQuality; |
| 323 | } |
| 324 | |
| 325 | // See skia:4649 and the GM image_scale_aligned. |
| 326 | if (*quality == kNone_SkFilterQuality) { |
| 327 | if (matrix->getScaleX() >= 0) { |
| 328 | matrix->setTranslateX(nextafterf(matrix->getTranslateX(), |
| 329 | floorf(matrix->getTranslateX()))); |
| 330 | } |
| 331 | if (matrix->getScaleY() >= 0) { |
| 332 | matrix->setTranslateY(nextafterf(matrix->getTranslateY(), |
| 333 | floorf(matrix->getTranslateY()))); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 338 | bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 339 | if (updater && rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality) { |
| 340 | // TODO: medium: recall RequestBitmap and update width/height accordingly |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 344 | SkRasterPipeline* p = rec.fPipeline; |
| 345 | SkArenaAlloc* alloc = rec.fAlloc; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 346 | auto quality = rec.fPaint.getFilterQuality(); |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 347 | |
Florin Malita | 7558e4d | 2018-02-07 10:05:53 -0500 | [diff] [blame] | 348 | SkMatrix matrix; |
| 349 | if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) { |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 350 | return false; |
| 351 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 352 | |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 353 | const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), |
| 354 | matrix, quality, alloc); |
Mike Klein | f447dee | 2016-11-29 16:37:12 -0500 | [diff] [blame] | 355 | if (!state) { |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | const SkPixmap& pm = state->pixmap(); |
| 360 | matrix = state->invMatrix(); |
| 361 | quality = state->quality(); |
| 362 | auto info = pm.info(); |
| 363 | |
Mike Klein | e8de024 | 2018-03-10 12:37:11 -0500 | [diff] [blame] | 364 | p->append(SkRasterPipeline::seed_shader); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 365 | |
| 366 | if (updater) { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 367 | updater->append_matrix_stage(p); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 368 | } else { |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 369 | tweak_quality_and_inv_matrix(&quality, &matrix); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 370 | p->append_matrix(alloc, matrix); |
| 371 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 372 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 373 | auto gather = alloc->make<SkRasterPipeline_GatherCtx>(); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 374 | gather->pixels = pm.addr(); |
Mike Klein | 968af43 | 2017-07-18 16:31:55 -0400 | [diff] [blame] | 375 | gather->stride = pm.rowBytesAsPixels(); |
Mike Klein | f3b4e16 | 2017-09-22 15:32:59 -0400 | [diff] [blame] | 376 | gather->width = pm.width(); |
| 377 | gather->height = pm.height(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 378 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 379 | auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(), |
| 380 | limit_y = alloc->make<SkRasterPipeline_TileCtx>(); |
Mike Reed | 51e46d5 | 2017-06-23 14:21:25 -0400 | [diff] [blame] | 381 | limit_x->scale = pm.width(); |
| 382 | limit_x->invScale = 1.0f / pm.width(); |
| 383 | limit_y->scale = pm.height(); |
| 384 | limit_y->invScale = 1.0f / pm.height(); |
Mike Klein | fc84dc5 | 2017-05-11 15:29:31 -0400 | [diff] [blame] | 385 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 386 | SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 387 | bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal; |
| 388 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 389 | decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>(); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 390 | decal_ctx->limit_x = limit_x->scale; |
| 391 | decal_ctx->limit_y = limit_y->scale; |
| 392 | } |
| 393 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 394 | #if 0 // TODO: when we support kMedium |
| 395 | if (updator && (quality == kMedium_SkFilterQuality)) { |
| 396 | // if we change levels in mipmap, we need to update the scales (and invScales) |
| 397 | updator->fGather = gather; |
| 398 | updator->fLimitX = limit_x; |
| 399 | updator->fLimitY = limit_y; |
| 400 | updator->fDecal = decal_ctx; |
| 401 | } |
| 402 | #endif |
| 403 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 404 | auto append_tiling_and_gather = [&] { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 405 | if (decal_x_and_y) { |
| 406 | p->append(SkRasterPipeline::decal_x_and_y, decal_ctx); |
| 407 | } else { |
| 408 | switch (fTileModeX) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 409 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 410 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break; |
| 411 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break; |
| 412 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 413 | } |
| 414 | switch (fTileModeY) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 415 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 416 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break; |
| 417 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break; |
| 418 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 419 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 420 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 421 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 422 | void* ctx = gather; |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 423 | switch (info.colorType()) { |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 424 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 425 | case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break; |
| 426 | case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 427 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break; |
| 428 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 429 | case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break; |
| 430 | case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break; |
| 431 | case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 432 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break; |
| 433 | case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 434 | case kR16G16B16A16_unorm_SkColorType: |
| 435 | p->append(SkRasterPipeline::gather_16161616,ctx); break; |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 436 | case kRGBA_F16Norm_SkColorType: |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 437 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 438 | case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 439 | |
Mike Klein | b1df5e5 | 2018-10-17 17:06:03 -0400 | [diff] [blame] | 440 | case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); |
| 441 | p->append(SkRasterPipeline::alpha_to_gray ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 442 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 443 | case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 444 | p->append(SkRasterPipeline::force_opaque ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 445 | |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 446 | case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 447 | p->append(SkRasterPipeline::swap_rb ); break; |
| 448 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 449 | case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 450 | p->append(SkRasterPipeline::force_opaque ); break; |
| 451 | |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 452 | case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 453 | p->append(SkRasterPipeline::force_opaque ); |
| 454 | p->append(SkRasterPipeline::swap_rb ); break; |
| 455 | |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 456 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 457 | p->append(SkRasterPipeline::swap_rb ); break; |
| 458 | |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 459 | case kUnknown_SkColorType: SkASSERT(false); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 460 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 461 | if (decal_ctx) { |
| 462 | p->append(SkRasterPipeline::check_decal_mask, decal_ctx); |
| 463 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 464 | }; |
| 465 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 466 | auto append_misc = [&] { |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 467 | // This is an inessential optimization... it's logically safe to set this to false. |
| 468 | // But if... |
| 469 | // - we know the image is definitely normalized, and |
| 470 | // - we're doing some color space conversion, and |
| 471 | // - sRGB curves are involved, |
| 472 | // then we can use slightly faster math that doesn't work well outside [0,1]. |
| 473 | bool src_is_normalized = SkColorTypeIsNormalized(info.colorType()); |
| 474 | |
Mike Klein | 6a9f1c4 | 2020-01-02 23:23:42 -0600 | [diff] [blame] | 475 | SkColorSpace* cs = info.colorSpace(); |
| 476 | SkAlphaType at = info.alphaType(); |
| 477 | |
| 478 | // Color for A8 images comes from the paint. TODO: all alpha images? none? |
| 479 | if (info.colorType() == kAlpha_8_SkColorType) { |
| 480 | SkColor4f rgb = rec.fPaint.getColor4f(); |
| 481 | p->append_set_rgb(alloc, rgb); |
| 482 | |
| 483 | src_is_normalized = rgb.fitsInBytes(); |
| 484 | cs = sk_srgb_singleton(); |
| 485 | at = kUnpremul_SkAlphaType; |
| 486 | } |
| 487 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 488 | // 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] | 489 | if (quality == kHigh_SkFilterQuality) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 490 | p->append(SkRasterPipeline::clamp_0); |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 491 | p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul |
| 492 | ? SkRasterPipeline::clamp_1 |
| 493 | : SkRasterPipeline::clamp_a); |
| 494 | src_is_normalized = true; |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 495 | } |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 496 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame] | 497 | // Transform color space and alpha type to match shader convention (dst CS, premul alpha). |
| 498 | alloc->make<SkColorSpaceXformSteps>(cs, at, |
| 499 | rec.fDstCS, kPremul_SkAlphaType) |
| 500 | ->apply(p, src_is_normalized); |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 501 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 502 | return true; |
| 503 | }; |
| 504 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 505 | // Check for fast-path stages. |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 506 | auto ct = info.colorType(); |
| 507 | if (true |
| 508 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 509 | && quality == kLow_SkFilterQuality |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 510 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 511 | |
| 512 | p->append(SkRasterPipeline::bilerp_clamp_8888, gather); |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 513 | if (ct == kBGRA_8888_SkColorType) { |
| 514 | p->append(SkRasterPipeline::swap_rb); |
| 515 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 516 | return append_misc(); |
| 517 | } |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 518 | if (true |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 519 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 520 | && quality == kLow_SkFilterQuality |
| 521 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 522 | && fTileModeY != SkTileMode::kDecal) { |
| 523 | |
| 524 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 525 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 526 | ctx->ct = ct; |
| 527 | ctx->tileX = fTileModeX; |
| 528 | ctx->tileY = fTileModeY; |
| 529 | ctx->invWidth = 1.0f / ctx->width; |
| 530 | ctx->invHeight = 1.0f / ctx->height; |
| 531 | p->append(SkRasterPipeline::bilinear, ctx); |
| 532 | return append_misc(); |
| 533 | } |
| 534 | if (true |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 535 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 536 | && quality == kHigh_SkFilterQuality |
| 537 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
| 538 | |
| 539 | p->append(SkRasterPipeline::bicubic_clamp_8888, gather); |
| 540 | if (ct == kBGRA_8888_SkColorType) { |
| 541 | p->append(SkRasterPipeline::swap_rb); |
| 542 | } |
| 543 | return append_misc(); |
| 544 | } |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 545 | if (true |
| 546 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 547 | && quality == kHigh_SkFilterQuality |
| 548 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 549 | && fTileModeY != SkTileMode::kDecal) { |
| 550 | |
| 551 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 552 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 553 | ctx->ct = ct; |
| 554 | ctx->tileX = fTileModeX; |
| 555 | ctx->tileY = fTileModeY; |
| 556 | ctx->invWidth = 1.0f / ctx->width; |
| 557 | ctx->invHeight = 1.0f / ctx->height; |
| 558 | p->append(SkRasterPipeline::bicubic, ctx); |
| 559 | return append_misc(); |
| 560 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 561 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 562 | SkRasterPipeline_SamplerCtx* sampler = nullptr; |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 563 | if (quality != kNone_SkFilterQuality) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 564 | sampler = alloc->make<SkRasterPipeline_SamplerCtx>(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 567 | auto sample = [&](SkRasterPipeline::StockStage setup_x, |
| 568 | SkRasterPipeline::StockStage setup_y) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 569 | p->append(setup_x, sampler); |
| 570 | p->append(setup_y, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 571 | append_tiling_and_gather(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 572 | p->append(SkRasterPipeline::accumulate, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 573 | }; |
| 574 | |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 575 | if (quality == kNone_SkFilterQuality) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 576 | append_tiling_and_gather(); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 577 | } else if (quality == kLow_SkFilterQuality) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 578 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 579 | |
| 580 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny); |
| 581 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny); |
| 582 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py); |
| 583 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py); |
| 584 | |
| 585 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 586 | |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 587 | } else { |
Mike Klein | 250138d | 2020-01-02 09:39:26 -0600 | [diff] [blame] | 588 | SkASSERT(quality == kHigh_SkFilterQuality); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 589 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 590 | |
| 591 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y); |
| 592 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y); |
| 593 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y); |
| 594 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y); |
| 595 | |
| 596 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y); |
| 597 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y); |
| 598 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y); |
| 599 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y); |
| 600 | |
| 601 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y); |
| 602 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y); |
| 603 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y); |
| 604 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y); |
| 605 | |
| 606 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y); |
| 607 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y); |
| 608 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y); |
| 609 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y); |
| 610 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 611 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 612 | } |
| 613 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 614 | return append_misc(); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 615 | } |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 616 | |
| 617 | bool SkImageShader::onAppendStages(const SkStageRec& rec) const { |
| 618 | return this->doStages(rec, nullptr); |
| 619 | } |
| 620 | |
| 621 | SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 622 | bool usePersp = rec.fCTM.hasPerspective(); |
| 623 | auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 624 | return this->doStages(rec, updater) ? updater : nullptr; |
| 625 | } |
| 626 | |
Mike Klein | a434e0f | 2020-03-23 09:33:48 -0500 | [diff] [blame] | 627 | skvm::Color SkImageShader::onProgram(skvm::Builder* p, skvm::F32 x, skvm::F32 y, skvm::Color paint, |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 628 | const SkMatrix& ctm, const SkMatrix* localM, |
Mike Klein | a434e0f | 2020-03-23 09:33:48 -0500 | [diff] [blame] | 629 | SkFilterQuality quality, const SkColorInfo& dst, |
Mike Klein | 276a785 | 2020-03-15 08:46:09 -0500 | [diff] [blame] | 630 | skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const { |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 631 | SkMatrix inv; |
| 632 | if (!this->computeTotalInverse(ctm, localM, &inv)) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 633 | return {}; |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 634 | } |
| 635 | |
Mike Klein | 3ab7128 | 2020-01-06 19:53:39 -0600 | [diff] [blame] | 636 | // We use RequestBitmap() to make sure our SkBitmapController::State lives in the alloc. |
| 637 | // This lets the SkVMBlitter hang on to this state and keep our image alive. |
| 638 | auto state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), inv, quality, alloc); |
| 639 | if (!state) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 640 | return {}; |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 641 | } |
Mike Klein | 3ab7128 | 2020-01-06 19:53:39 -0600 | [diff] [blame] | 642 | const SkPixmap& pm = state->pixmap(); |
| 643 | inv = state->invMatrix(); |
| 644 | quality = state->quality(); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 645 | tweak_quality_and_inv_matrix(&quality, &inv); |
Mike Klein | 85754d5 | 2020-01-22 10:04:11 -0600 | [diff] [blame] | 646 | inv.normalizePerspective(); |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 647 | |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 648 | // Apply matrix to convert dst coords to sample center coords. |
Mike Klein | 85754d5 | 2020-01-22 10:04:11 -0600 | [diff] [blame] | 649 | SkShaderBase::ApplyMatrix(p, inv, &x,&y,uniforms); |
Mike Klein | 6dbd7ff | 2020-01-06 11:50:37 -0600 | [diff] [blame] | 650 | |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 651 | // Bail out if sample() can't yet handle our image's color type. |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 652 | switch (pm.colorType()) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 653 | default: return {}; |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 654 | case kGray_8_SkColorType: |
| 655 | case kAlpha_8_SkColorType: |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 656 | case kRGB_565_SkColorType: |
Mike Klein | 748b757 | 2020-02-12 11:21:11 -0600 | [diff] [blame] | 657 | case kRGB_888x_SkColorType: |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 658 | case kRGBA_8888_SkColorType: |
Mike Klein | 748b757 | 2020-02-12 11:21:11 -0600 | [diff] [blame] | 659 | case kBGRA_8888_SkColorType: |
| 660 | case kRGBA_1010102_SkColorType: |
| 661 | case kBGRA_1010102_SkColorType: |
| 662 | case kRGB_101010x_SkColorType: |
| 663 | case kBGR_101010x_SkColorType: break; |
Mike Klein | 25480bf | 2020-01-06 14:01:58 -0600 | [diff] [blame] | 664 | } |
| 665 | |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 666 | // We can exploit image opacity to skip work unpacking alpha channels. |
| 667 | const bool input_is_opaque = SkAlphaTypeIsOpaque(pm.alphaType()) |
| 668 | || SkColorTypeIsAlwaysOpaque(pm.colorType()); |
| 669 | |
Mike Klein | 03d89ef | 2020-01-14 17:18:29 -0600 | [diff] [blame] | 670 | // Each call to sample() will try to rewrite the same uniforms over and over, |
| 671 | // so remember where we start and reset back there each time. That way each |
| 672 | // sample() call uses the same uniform offsets. |
| 673 | const size_t uniforms_before_sample = uniforms->buf.size(); |
| 674 | |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 675 | auto sample = [&](skvm::F32 sx, skvm::F32 sy) -> skvm::Color { |
Mike Klein | 03d89ef | 2020-01-14 17:18:29 -0600 | [diff] [blame] | 676 | uniforms->buf.resize(uniforms_before_sample); |
| 677 | |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 678 | // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp. |
| 679 | auto repeat = [&](skvm::F32 v, float scale) { |
| 680 | skvm::F32 S = p->uniformF(uniforms->pushF( scale)), |
| 681 | I = p->uniformF(uniforms->pushF(1.0f/scale)); |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 682 | return v - floor(v * I) * S; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 683 | }; |
| 684 | auto mirror = [&](skvm::F32 v, float scale) { |
| 685 | skvm::F32 S = p->uniformF(uniforms->pushF( scale)), |
| 686 | I2 = p->uniformF(uniforms->pushF(0.5f/scale)); |
| 687 | // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale ) |
| 688 | // {---A---} {------------------B------------------} |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 689 | skvm::F32 A = v - S, |
| 690 | B = (S + S) * floor(A * I2); |
| 691 | return abs(A - B - S); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 692 | }; |
| 693 | switch (fTileModeX) { |
| 694 | case SkTileMode::kDecal: /* handled after gather */ break; |
| 695 | case SkTileMode::kClamp: /* we always clamp */ break; |
| 696 | case SkTileMode::kRepeat: sx = repeat(sx, pm.width()); break; |
| 697 | case SkTileMode::kMirror: sx = mirror(sx, pm.width()); break; |
| 698 | } |
| 699 | switch (fTileModeY) { |
| 700 | case SkTileMode::kDecal: /* handled after gather */ break; |
| 701 | case SkTileMode::kClamp: /* we always clamp */ break; |
| 702 | case SkTileMode::kRepeat: sy = repeat(sy, pm.height()); break; |
| 703 | case SkTileMode::kMirror: sy = mirror(sy, pm.height()); break; |
| 704 | } |
| 705 | |
| 706 | // Always clamp sample coordinates to [0,width), [0,height), both for memory |
| 707 | // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror. |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 708 | auto clamp0x = [&](skvm::F32 v, float limit) { |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 709 | // Subtract an ulp so the upper clamp limit excludes limit itself. |
| 710 | int bits; |
| 711 | memcpy(&bits, &limit, 4); |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 712 | return clamp(v, 0.0f, p->uniformF(uniforms->push(bits-1))); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 713 | }; |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 714 | skvm::F32 clamped_x = clamp0x(sx, pm. width()), |
| 715 | clamped_y = clamp0x(sy, pm.height()); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 716 | |
| 717 | // Load pixels from pm.addr()[(int)sx + (int)sy*stride]. |
Mike Klein | 8b99b9e | 2020-03-31 12:28:41 -0500 | [diff] [blame] | 718 | skvm::Uniform img = uniforms->pushPtr(pm.addr()); |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 719 | skvm::I32 index = trunc(clamped_x) + |
| 720 | trunc(clamped_y) * p->uniform32(uniforms->push(pm.rowBytesAsPixels())); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 721 | skvm::Color c; |
| 722 | switch (pm.colorType()) { |
| 723 | default: SkUNREACHABLE; |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 724 | |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 725 | case kGray_8_SkColorType: c.r = c.g = c.b = from_unorm(8, gather8(img, index)); |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 726 | c.a = p->splat(1.0f); |
| 727 | break; |
| 728 | |
| 729 | case kAlpha_8_SkColorType: c.r = c.g = c.b = p->splat(0.0f); |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 730 | c.a = from_unorm(8, gather8(img, index)); |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 731 | break; |
| 732 | |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 733 | case kRGB_565_SkColorType: c = unpack_565 (gather16(img, index)); break; |
Mike Klein | 748b757 | 2020-02-12 11:21:11 -0600 | [diff] [blame] | 734 | |
| 735 | case kRGB_888x_SkColorType: [[fallthrough]]; |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 736 | case kRGBA_8888_SkColorType: c = unpack_8888(gather32(img, index)); |
Mike Klein | 748b757 | 2020-02-12 11:21:11 -0600 | [diff] [blame] | 737 | break; |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 738 | case kBGRA_8888_SkColorType: c = unpack_8888(gather32(img, index)); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 739 | std::swap(c.r, c.b); |
| 740 | break; |
Mike Klein | 748b757 | 2020-02-12 11:21:11 -0600 | [diff] [blame] | 741 | |
| 742 | case kRGB_101010x_SkColorType: [[fallthrough]]; |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 743 | case kRGBA_1010102_SkColorType: c = unpack_1010102(gather32(img, index)); |
Mike Klein | 748b757 | 2020-02-12 11:21:11 -0600 | [diff] [blame] | 744 | break; |
| 745 | |
| 746 | case kBGR_101010x_SkColorType: [[fallthrough]]; |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 747 | case kBGRA_1010102_SkColorType: c = unpack_1010102(gather32(img, index)); |
Mike Klein | 748b757 | 2020-02-12 11:21:11 -0600 | [diff] [blame] | 748 | std::swap(c.r, c.b); |
| 749 | break; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 750 | } |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 751 | // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it. |
| 752 | if (input_is_opaque) { |
Mike Klein | 46874ef | 2020-02-13 10:17:08 -0600 | [diff] [blame] | 753 | c.a = p->splat(1.0f); |
| 754 | } |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 755 | |
| 756 | // Mask away any pixels that we tried to sample outside the bounds in kDecal. |
| 757 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
| 758 | skvm::I32 mask = p->splat(~0); |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 759 | if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); } |
| 760 | if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); } |
| 761 | c.r = bit_cast(p->bit_and(mask, bit_cast(c.r))); |
| 762 | c.g = bit_cast(p->bit_and(mask, bit_cast(c.g))); |
| 763 | c.b = bit_cast(p->bit_and(mask, bit_cast(c.b))); |
| 764 | c.a = bit_cast(p->bit_and(mask, bit_cast(c.a))); |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 765 | // 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] | 766 | } |
| 767 | |
| 768 | return c; |
| 769 | }; |
| 770 | |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 771 | skvm::Color c; |
| 772 | |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 773 | if (quality == kNone_SkFilterQuality) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 774 | c = sample(x,y); |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 775 | } else if (quality == kLow_SkFilterQuality) { |
| 776 | // Our four sample points are the corners of a logical 1x1 pixel |
| 777 | // box surrounding (x,y) at (0.5,0.5) off-center. |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 778 | skvm::F32 left = x - 0.5f, |
| 779 | top = y - 0.5f, |
| 780 | right = x + 0.5f, |
| 781 | bottom = y + 0.5f; |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 782 | |
| 783 | // The fractional parts of right and bottom are our lerp factors in x and y respectively. |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 784 | skvm::F32 fx = fract(right ), |
| 785 | fy = fract(bottom); |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 786 | |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 787 | c = lerp(lerp(sample(left,top ), sample(right,top ), fx), |
| 788 | lerp(sample(left,bottom), sample(right,bottom), fx), fy); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 789 | } else { |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 790 | SkASSERT(quality == kHigh_SkFilterQuality); |
| 791 | |
| 792 | // All bicubic samples have the same fractional offset (fx,fy) from the center. |
| 793 | // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center. |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 794 | skvm::F32 fx = fract(x + 0.5f), |
| 795 | fy = fract(y + 0.5f); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 796 | |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 797 | // See GrCubicEffect for details of these weights. |
| 798 | // TODO: these maybe don't seem right looking at gm/bicubic and GrBicubicEffect. |
| 799 | auto near = [&](skvm::F32 t) { |
| 800 | // 1/18 + 9/18t + 27/18t^2 - 21/18t^3 == t ( t ( -21/18t + 27/18) + 9/18) + 1/18 |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 801 | return t * (t * (t * (-21/18.0f) + 27/18.0f) + 9/18.0f) + 1/18.0f; |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 802 | }; |
| 803 | auto far = [&](skvm::F32 t) { |
| 804 | // 0/18 + 0/18*t - 6/18t^2 + 7/18t^3 == t^2 (7/18t - 6/18) |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 805 | return t * t * (t * (7/18.0f) - 6/18.0f); |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 806 | }; |
| 807 | const skvm::F32 wx[] = { |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 808 | far (1.0f - fx), |
| 809 | near(1.0f - fx), |
| 810 | near( fx), |
| 811 | far ( fx), |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 812 | }; |
| 813 | const skvm::F32 wy[] = { |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 814 | far (1.0f - fy), |
| 815 | near(1.0f - fy), |
| 816 | near( fy), |
| 817 | far ( fy), |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 818 | }; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 819 | |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 820 | c.r = c.g = c.b = c.a = p->splat(0.0f); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 821 | |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 822 | skvm::F32 sy = y - 1.5f; |
| 823 | for (int j = 0; j < 4; j++, sy += 1.0f) { |
| 824 | skvm::F32 sx = x - 1.5f; |
| 825 | for (int i = 0; i < 4; i++, sx += 1.0f) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 826 | skvm::Color s = sample(sx,sy); |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 827 | skvm::F32 w = wx[i] * wy[j]; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 828 | |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 829 | c.r += s.r * w; |
| 830 | c.g += s.g * w; |
| 831 | c.b += s.b * w; |
| 832 | c.a += s.a * w; |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 833 | } |
| 834 | } |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 835 | } |
Mike Klein | d67a5c3 | 2020-01-06 16:01:01 -0600 | [diff] [blame] | 836 | |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 837 | // If the input is opaque and we're not in decal mode, that means the output is too. |
| 838 | // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas. |
| 839 | if (input_is_opaque |
| 840 | && fTileModeX != SkTileMode::kDecal |
| 841 | && fTileModeY != SkTileMode::kDecal) { |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 842 | c.a = p->splat(1.0f); |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 843 | } |
| 844 | |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 845 | // Alpha-only images get their color from the paint (already converted to dst color space). |
| 846 | SkColorSpace* cs = pm.colorSpace(); |
| 847 | SkAlphaType at = pm.alphaType(); |
| 848 | if (SkColorTypeIsAlphaOnly(pm.colorType())) { |
| 849 | c.r = paint.r; |
| 850 | c.g = paint.g; |
| 851 | c.b = paint.b; |
| 852 | |
| 853 | cs = dst.colorSpace(); |
| 854 | at = kUnpremul_SkAlphaType; |
| 855 | } |
| 856 | |
Mike Klein | 921236b | 2020-02-13 11:20:54 -0600 | [diff] [blame] | 857 | if (quality == kHigh_SkFilterQuality) { |
Mike Klein | 5c660e0 | 2020-01-08 11:36:35 -0600 | [diff] [blame] | 858 | // 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] | 859 | c.a = clamp01(c.a); |
Mike Klein | 3f83bfd | 2020-01-07 12:01:50 -0600 | [diff] [blame] | 860 | |
Mike Klein | 913a6f5 | 2020-03-23 11:06:25 -0500 | [diff] [blame] | 861 | skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul) |
Mike Klein | 3f83bfd | 2020-01-07 12:01:50 -0600 | [diff] [blame] | 862 | ? p->splat(1.0f) |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame] | 863 | : c.a; |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 864 | c.r = clamp(c.r, 0.0f, limit); |
| 865 | c.g = clamp(c.g, 0.0f, limit); |
| 866 | c.b = clamp(c.b, 0.0f, limit); |
Mike Klein | b1ff79a | 2020-01-07 07:53:39 -0600 | [diff] [blame] | 867 | } |
Mike Klein | 4bc86d5 | 2020-01-06 18:39:42 -0600 | [diff] [blame] | 868 | |
Mike Reed | 0113d29 | 2020-03-24 16:36:37 -0400 | [diff] [blame] | 869 | SkColorSpaceXformSteps steps{cs,at, dst.colorSpace(),kPremul_SkAlphaType}; |
| 870 | return steps.program(p, uniforms, c); |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 871 | } |
| 872 | |