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 | 3a09a37 | 2019-12-19 17:13:26 -0500 | [diff] [blame] | 179 | #include "src/gpu/effects/GrSimpleTextureEffect.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(); |
| 251 | inner = GrSimpleTextureEffect::Make(std::move(proxy), srcAlphaType, lmInverse, |
| 252 | samplerState); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 253 | if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 254 | SkRect domain = GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(dimensions), |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 255 | domainX, domainY); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 256 | inner = GrDomainEffect::Make(std::move(inner), domain, domainX, domainY, |
| 257 | samplerState.filter()); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 258 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 259 | } |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 260 | inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(), srcAlphaType, |
| 261 | args.fDstColorInfo->colorSpace()); |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 262 | |
| 263 | bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType()); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 264 | if (isAlphaOnly) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 265 | return inner; |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 266 | } else if (args.fInputColorIsOpaque) { |
| 267 | return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 268 | } |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 269 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | #endif |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 273 | |
| 274 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 275 | #include "src/core/SkImagePriv.h" |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 276 | |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 277 | sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy, |
| 278 | const SkMatrix* localMatrix, SkCopyPixelsMode cpm) { |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 279 | return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm), |
| 280 | tmx, tmy, localMatrix); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 283 | sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 284 | SkTileMode tmx, SkTileMode tmy, |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 285 | const SkMatrix* localMatrix, SkCopyPixelsMode mode) { |
| 286 | auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode); |
| 287 | if (!s) { |
| 288 | return nullptr; |
| 289 | } |
| 290 | if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) { |
| 291 | // Compose the image shader with the paint's shader. Alpha images+shaders should output the |
| 292 | // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with |
| 293 | // the source image and dst shader (MakeBlend takes dst first, src second). |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 294 | s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s)); |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 295 | } |
| 296 | return s; |
| 297 | } |
| 298 | |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 299 | void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); } |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 300 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 301 | class SkImageStageUpdater : public SkStageUpdater { |
| 302 | public: |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 303 | SkImageStageUpdater(const SkImageShader* shader, bool usePersp) |
| 304 | : fShader(shader), fUsePersp(usePersp) |
| 305 | {} |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 306 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 307 | const SkImageShader* fShader; |
| 308 | const bool fUsePersp; // else use affine |
| 309 | |
| 310 | // large enough for perspective, though often we just use 2x3 |
| 311 | float fMatrixStorage[9]; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 312 | |
| 313 | #if 0 // TODO: when we support mipmaps |
| 314 | SkRasterPipeline_GatherCtx* fGather; |
| 315 | SkRasterPipeline_TileCtx* fLimitX; |
| 316 | SkRasterPipeline_TileCtx* fLimitY; |
| 317 | SkRasterPipeline_DecalTileCtx* fDecal; |
| 318 | #endif |
| 319 | |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 320 | void append_matrix_stage(SkRasterPipeline* p) { |
| 321 | if (fUsePersp) { |
| 322 | p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage); |
| 323 | } else { |
| 324 | p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage); |
| 325 | } |
| 326 | } |
| 327 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 328 | bool update(const SkMatrix& ctm, const SkMatrix* localM) override { |
| 329 | SkMatrix matrix; |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 330 | if (fShader->computeTotalInverse(ctm, localM, &matrix)) { |
| 331 | if (fUsePersp) { |
| 332 | matrix.get9(fMatrixStorage); |
| 333 | } else { |
| 334 | SkAssertResult(matrix.asAffine(fMatrixStorage)); |
| 335 | } |
| 336 | return true; |
| 337 | } |
| 338 | return false; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 339 | } |
| 340 | }; |
| 341 | |
| 342 | bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 343 | if (updater && rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality) { |
| 344 | // TODO: medium: recall RequestBitmap and update width/height accordingly |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 345 | return false; |
| 346 | } |
| 347 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 348 | SkRasterPipeline* p = rec.fPipeline; |
| 349 | SkArenaAlloc* alloc = rec.fAlloc; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 350 | auto quality = rec.fPaint.getFilterQuality(); |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 351 | |
Florin Malita | 7558e4d | 2018-02-07 10:05:53 -0500 | [diff] [blame] | 352 | SkMatrix matrix; |
| 353 | if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) { |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 354 | return false; |
| 355 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 356 | |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 357 | const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), |
| 358 | matrix, quality, alloc); |
Mike Klein | f447dee | 2016-11-29 16:37:12 -0500 | [diff] [blame] | 359 | if (!state) { |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | const SkPixmap& pm = state->pixmap(); |
| 364 | matrix = state->invMatrix(); |
| 365 | quality = state->quality(); |
| 366 | auto info = pm.info(); |
| 367 | |
Mike Klein | e8de024 | 2018-03-10 12:37:11 -0500 | [diff] [blame] | 368 | p->append(SkRasterPipeline::seed_shader); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 369 | |
| 370 | if (updater) { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 371 | updater->append_matrix_stage(p); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 372 | } else { |
| 373 | // When the matrix is just an integer translate, bilerp == nearest neighbor. |
| 374 | if (quality == kLow_SkFilterQuality && |
| 375 | matrix.getType() <= SkMatrix::kTranslate_Mask && |
| 376 | matrix.getTranslateX() == (int)matrix.getTranslateX() && |
| 377 | matrix.getTranslateY() == (int)matrix.getTranslateY()) { |
| 378 | quality = kNone_SkFilterQuality; |
| 379 | } |
| 380 | |
| 381 | // See skia:4649 and the GM image_scale_aligned. |
| 382 | if (quality == kNone_SkFilterQuality) { |
| 383 | if (matrix.getScaleX() >= 0) { |
| 384 | matrix.setTranslateX(nextafterf(matrix.getTranslateX(), |
| 385 | floorf(matrix.getTranslateX()))); |
| 386 | } |
| 387 | if (matrix.getScaleY() >= 0) { |
| 388 | matrix.setTranslateY(nextafterf(matrix.getTranslateY(), |
| 389 | floorf(matrix.getTranslateY()))); |
| 390 | } |
| 391 | } |
| 392 | p->append_matrix(alloc, matrix); |
| 393 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 394 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 395 | auto gather = alloc->make<SkRasterPipeline_GatherCtx>(); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 396 | gather->pixels = pm.addr(); |
Mike Klein | 968af43 | 2017-07-18 16:31:55 -0400 | [diff] [blame] | 397 | gather->stride = pm.rowBytesAsPixels(); |
Mike Klein | f3b4e16 | 2017-09-22 15:32:59 -0400 | [diff] [blame] | 398 | gather->width = pm.width(); |
| 399 | gather->height = pm.height(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 400 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 401 | auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(), |
| 402 | limit_y = alloc->make<SkRasterPipeline_TileCtx>(); |
Mike Reed | 51e46d5 | 2017-06-23 14:21:25 -0400 | [diff] [blame] | 403 | limit_x->scale = pm.width(); |
| 404 | limit_x->invScale = 1.0f / pm.width(); |
| 405 | limit_y->scale = pm.height(); |
| 406 | limit_y->invScale = 1.0f / pm.height(); |
Mike Klein | fc84dc5 | 2017-05-11 15:29:31 -0400 | [diff] [blame] | 407 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 408 | SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 409 | bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal; |
| 410 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 411 | decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>(); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 412 | decal_ctx->limit_x = limit_x->scale; |
| 413 | decal_ctx->limit_y = limit_y->scale; |
| 414 | } |
| 415 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 416 | #if 0 // TODO: when we support kMedium |
| 417 | if (updator && (quality == kMedium_SkFilterQuality)) { |
| 418 | // if we change levels in mipmap, we need to update the scales (and invScales) |
| 419 | updator->fGather = gather; |
| 420 | updator->fLimitX = limit_x; |
| 421 | updator->fLimitY = limit_y; |
| 422 | updator->fDecal = decal_ctx; |
| 423 | } |
| 424 | #endif |
| 425 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 426 | auto append_tiling_and_gather = [&] { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 427 | if (decal_x_and_y) { |
| 428 | p->append(SkRasterPipeline::decal_x_and_y, decal_ctx); |
| 429 | } else { |
| 430 | switch (fTileModeX) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 431 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 432 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break; |
| 433 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break; |
| 434 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 435 | } |
| 436 | switch (fTileModeY) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 437 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 438 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break; |
| 439 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break; |
| 440 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 441 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 442 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 443 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 444 | void* ctx = gather; |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 445 | switch (info.colorType()) { |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 446 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 447 | case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break; |
| 448 | case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 449 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break; |
| 450 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 451 | case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break; |
| 452 | case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break; |
| 453 | case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 454 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break; |
| 455 | case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 456 | case kR16G16B16A16_unorm_SkColorType: |
| 457 | p->append(SkRasterPipeline::gather_16161616,ctx); break; |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 458 | case kRGBA_F16Norm_SkColorType: |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 459 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 460 | case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 461 | |
Mike Klein | b1df5e5 | 2018-10-17 17:06:03 -0400 | [diff] [blame] | 462 | case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); |
| 463 | p->append(SkRasterPipeline::alpha_to_gray ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 464 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 465 | case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 466 | p->append(SkRasterPipeline::force_opaque ); 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_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 469 | p->append(SkRasterPipeline::force_opaque ); break; |
| 470 | |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 471 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 472 | p->append(SkRasterPipeline::swap_rb ); break; |
| 473 | |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 474 | case kUnknown_SkColorType: SkASSERT(false); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 475 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 476 | if (decal_ctx) { |
| 477 | p->append(SkRasterPipeline::check_decal_mask, decal_ctx); |
| 478 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 479 | }; |
| 480 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 481 | auto append_misc = [&] { |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame^] | 482 | SkColorSpace* cs = info.colorSpace(); |
| 483 | SkAlphaType at = info.alphaType(); |
| 484 | |
| 485 | // Color for A8 images comes from the paint. TODO: all alpha images? none? |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 486 | if (info.colorType() == kAlpha_8_SkColorType) { |
Mike Klein | be56949 | 2018-09-14 09:34:21 -0400 | [diff] [blame] | 487 | p->append_set_rgb(alloc, rec.fPaint.getColor4f()); |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame^] | 488 | cs = sk_srgb_singleton(); |
| 489 | at = kUnpremul_SkAlphaType; |
Mike Klein | 47cf048 | 2018-02-09 18:57:54 +0000 | [diff] [blame] | 490 | } |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 491 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame^] | 492 | // This is an inessential optimization... it's logically safe to set this to false. |
| 493 | // But if... |
| 494 | // - we know the image is definitely normalized, and |
| 495 | // - we're doing some color space conversion, and |
| 496 | // - sRGB curves are involved, |
| 497 | // then we can use slightly faster math that doesn't work well outside [0,1]. |
| 498 | bool src_is_normalized = SkColorTypeIsNormalized(info.colorType()); |
| 499 | |
| 500 | // 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] | 501 | if (quality == kHigh_SkFilterQuality) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 502 | p->append(SkRasterPipeline::clamp_0); |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame^] | 503 | p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul |
| 504 | ? SkRasterPipeline::clamp_1 |
| 505 | : SkRasterPipeline::clamp_a); |
| 506 | src_is_normalized = true; |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 507 | } |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 508 | |
Mike Klein | 059e043 | 2020-01-02 16:32:38 -0600 | [diff] [blame^] | 509 | // Transform color space and alpha type to match shader convention (dst CS, premul alpha). |
| 510 | alloc->make<SkColorSpaceXformSteps>(cs, at, |
| 511 | rec.fDstCS, kPremul_SkAlphaType) |
| 512 | ->apply(p, src_is_normalized); |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 513 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 514 | return true; |
| 515 | }; |
| 516 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 517 | // Check for fast-path stages. |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 518 | auto ct = info.colorType(); |
| 519 | if (true |
| 520 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 521 | && quality == kLow_SkFilterQuality |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 522 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 523 | |
| 524 | p->append(SkRasterPipeline::bilerp_clamp_8888, gather); |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 525 | if (ct == kBGRA_8888_SkColorType) { |
| 526 | p->append(SkRasterPipeline::swap_rb); |
| 527 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 528 | return append_misc(); |
| 529 | } |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 530 | if (true |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 531 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 532 | && quality == kLow_SkFilterQuality |
| 533 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 534 | && fTileModeY != SkTileMode::kDecal) { |
| 535 | |
| 536 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 537 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 538 | ctx->ct = ct; |
| 539 | ctx->tileX = fTileModeX; |
| 540 | ctx->tileY = fTileModeY; |
| 541 | ctx->invWidth = 1.0f / ctx->width; |
| 542 | ctx->invHeight = 1.0f / ctx->height; |
| 543 | p->append(SkRasterPipeline::bilinear, ctx); |
| 544 | return append_misc(); |
| 545 | } |
| 546 | if (true |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 547 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 548 | && quality == kHigh_SkFilterQuality |
| 549 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
| 550 | |
| 551 | p->append(SkRasterPipeline::bicubic_clamp_8888, gather); |
| 552 | if (ct == kBGRA_8888_SkColorType) { |
| 553 | p->append(SkRasterPipeline::swap_rb); |
| 554 | } |
| 555 | return append_misc(); |
| 556 | } |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 557 | if (true |
| 558 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 559 | && quality == kHigh_SkFilterQuality |
| 560 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 561 | && fTileModeY != SkTileMode::kDecal) { |
| 562 | |
| 563 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 564 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 565 | ctx->ct = ct; |
| 566 | ctx->tileX = fTileModeX; |
| 567 | ctx->tileY = fTileModeY; |
| 568 | ctx->invWidth = 1.0f / ctx->width; |
| 569 | ctx->invHeight = 1.0f / ctx->height; |
| 570 | p->append(SkRasterPipeline::bicubic, ctx); |
| 571 | return append_misc(); |
| 572 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 573 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 574 | SkRasterPipeline_SamplerCtx* sampler = nullptr; |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 575 | if (quality != kNone_SkFilterQuality) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 576 | sampler = alloc->make<SkRasterPipeline_SamplerCtx>(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 577 | } |
| 578 | |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 579 | auto sample = [&](SkRasterPipeline::StockStage setup_x, |
| 580 | SkRasterPipeline::StockStage setup_y) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 581 | p->append(setup_x, sampler); |
| 582 | p->append(setup_y, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 583 | append_tiling_and_gather(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 584 | p->append(SkRasterPipeline::accumulate, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 585 | }; |
| 586 | |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 587 | if (quality == kNone_SkFilterQuality) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 588 | append_tiling_and_gather(); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 589 | } else if (quality == kLow_SkFilterQuality) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 590 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 591 | |
| 592 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny); |
| 593 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny); |
| 594 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py); |
| 595 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py); |
| 596 | |
| 597 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 598 | |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 599 | } else { |
Mike Klein | 250138d | 2020-01-02 09:39:26 -0600 | [diff] [blame] | 600 | SkASSERT(quality == kHigh_SkFilterQuality); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 601 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 602 | |
| 603 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y); |
| 604 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y); |
| 605 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y); |
| 606 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y); |
| 607 | |
| 608 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y); |
| 609 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y); |
| 610 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y); |
| 611 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y); |
| 612 | |
| 613 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y); |
| 614 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y); |
| 615 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y); |
| 616 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y); |
| 617 | |
| 618 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y); |
| 619 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y); |
| 620 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y); |
| 621 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y); |
| 622 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 623 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 624 | } |
| 625 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 626 | return append_misc(); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 627 | } |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 628 | |
| 629 | bool SkImageShader::onAppendStages(const SkStageRec& rec) const { |
| 630 | return this->doStages(rec, nullptr); |
| 631 | } |
| 632 | |
| 633 | SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const { |
Mike Reed | 8845c37 | 2019-12-19 13:22:08 -0500 | [diff] [blame] | 634 | bool usePersp = rec.fCTM.hasPerspective(); |
| 635 | auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 636 | return this->doStages(rec, updater) ? updater : nullptr; |
| 637 | } |
| 638 | |
Mike Klein | f6a715b | 2019-12-30 15:24:18 -0600 | [diff] [blame] | 639 | bool SkImageShader::onProgram(skvm::Builder* p, |
| 640 | const SkMatrix& ctm, const SkMatrix* localM, |
| 641 | SkFilterQuality quality, SkColorSpace* dstCS, |
| 642 | skvm::Uniforms* uniforms, |
| 643 | skvm::F32 x, skvm::F32 y, |
| 644 | skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const { |
| 645 | SkMatrix inv; |
| 646 | if (!this->computeTotalInverse(ctm, localM, &inv)) { |
| 647 | return false; |
| 648 | } |
| 649 | |
| 650 | SkBitmapController::State state{as_IB(fImage.get()), inv, quality}; |
| 651 | const SkPixmap& pm = state.pixmap(); |
| 652 | if (!pm.addr()) { |
| 653 | return false; |
| 654 | } |
| 655 | inv = state.invMatrix(); |
| 656 | quality = state.quality(); |
| 657 | |
| 658 | return false; |
| 659 | } |
| 660 | |