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" |
| 12 | #include "src/core/SkRasterPipeline.h" |
| 13 | #include "src/core/SkReadBuffer.h" |
| 14 | #include "src/core/SkWriteBuffer.h" |
| 15 | #include "src/image/SkImage_Base.h" |
| 16 | #include "src/shaders/SkBitmapProcShader.h" |
| 17 | #include "src/shaders/SkEmptyShader.h" |
| 18 | #include "src/shaders/SkImageShader.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 19 | |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 20 | /** |
| 21 | * We are faster in clamp, so always use that tiling when we can. |
| 22 | */ |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 23 | static SkTileMode optimize(SkTileMode tm, int dimension) { |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 24 | SkASSERT(dimension > 0); |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 25 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 26 | // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow |
| 27 | // for transforming to clamp. |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 28 | return tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 29 | #else |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 30 | return dimension == 1 ? SkTileMode::kClamp : tm; |
Mike Reed | 2e3c955 | 2017-06-23 21:33:58 -0400 | [diff] [blame] | 31 | #endif |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 34 | SkImageShader::SkImageShader(sk_sp<SkImage> img, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 35 | SkTileMode tmx, SkTileMode tmy, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 36 | const SkMatrix* localMatrix, |
| 37 | bool clampAsIfUnpremul) |
| 38 | : INHERITED(localMatrix) |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 39 | , fImage(std::move(img)) |
Mike Reed | 587d082 | 2017-06-23 16:49:12 -0400 | [diff] [blame] | 40 | , fTileModeX(optimize(tmx, fImage->width())) |
| 41 | , fTileModeY(optimize(tmy, fImage->height())) |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 42 | , fClampAsIfUnpremul(clampAsIfUnpremul) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 43 | {} |
| 44 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 45 | // fClampAsIfUnpremul is always false when constructed through public APIs, |
| 46 | // so there's no need to read or write it here. |
| 47 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 48 | sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 49 | auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
| 50 | auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 51 | SkMatrix localMatrix; |
| 52 | buffer.readMatrix(&localMatrix); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 53 | sk_sp<SkImage> img = buffer.readImage(); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 54 | if (!img) { |
| 55 | return nullptr; |
| 56 | } |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 57 | return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void SkImageShader::flatten(SkWriteBuffer& buffer) const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 61 | buffer.writeUInt((unsigned)fTileModeX); |
| 62 | buffer.writeUInt((unsigned)fTileModeY); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 63 | buffer.writeMatrix(this->getLocalMatrix()); |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 64 | buffer.writeImage(fImage.get()); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 65 | SkASSERT(fClampAsIfUnpremul == false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool SkImageShader::isOpaque() const { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 69 | return fImage->isOpaque() && |
| 70 | fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 73 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 74 | static bool legacy_shader_can_handle(const SkMatrix& inv) { |
| 75 | if (!inv.isScaleTranslate()) { |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates |
| 80 | // out of range. |
| 81 | const SkScalar max_dev_coord = 32767.0f; |
| 82 | SkRect src; |
| 83 | SkAssertResult(inv.mapRect(&src, SkRect::MakeWH(max_dev_coord, max_dev_coord))); |
| 84 | |
Mike Reed | 1eb5ca4 | 2018-03-08 14:20:52 -0500 | [diff] [blame] | 85 | // take 1/4 of max signed 32bits so we have room to subtract local values |
| 86 | const SkScalar max_fixed32dot32 = SK_MaxS32 * 0.25f; |
Mike Reed | a12c419 | 2018-02-01 16:34:03 -0500 | [diff] [blame] | 87 | if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32, |
| 88 | max_fixed32dot32, max_fixed32dot32).contains(src)) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // legacy shader impl should be able to handle these matrices |
| 93 | return true; |
| 94 | } |
| 95 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 96 | SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec, |
| 97 | SkArenaAlloc* alloc) const { |
Brian Osman | 0e18937 | 2018-10-19 11:58:29 -0400 | [diff] [blame] | 98 | if (fImage->alphaType() == kUnpremul_SkAlphaType) { |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 99 | return nullptr; |
| 100 | } |
Brian Osman | b70fd91 | 2018-10-22 16:10:44 -0400 | [diff] [blame] | 101 | if (fImage->colorType() != kN32_SkColorType) { |
| 102 | return nullptr; |
| 103 | } |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 104 | if (fTileModeX != fTileModeY) { |
| 105 | return nullptr; |
| 106 | } |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 107 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 108 | return nullptr; |
| 109 | } |
| 110 | |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 111 | // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, |
Mike Klein | 67761eb | 2018-12-18 10:16:53 -0500 | [diff] [blame] | 112 | // so it can't handle bitmaps larger than 65535. |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 113 | // |
Mike Klein | 67761eb | 2018-12-18 10:16:53 -0500 | [diff] [blame] | 114 | // We back off another bit to 32767 to make small amounts of |
| 115 | // intermediate math safe, e.g. in |
| 116 | // |
| 117 | // SkFixed fx = ...; |
| 118 | // fx = tile(fx + SK_Fixed1); |
| 119 | // |
| 120 | // we want to make sure (fx + SK_Fixed1) never overflows. |
| 121 | if (fImage-> width() > 32767 || |
| 122 | fImage->height() > 32767) { |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 123 | return nullptr; |
| 124 | } |
| 125 | |
Florin Malita | af2769d | 2018-04-04 13:46:35 -0400 | [diff] [blame] | 126 | SkMatrix inv; |
| 127 | if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) || |
| 128 | !legacy_shader_can_handle(inv)) { |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
Mike Reed | 011d166 | 2019-02-28 17:19:25 -0500 | [diff] [blame] | 132 | if (!rec.isLegacyCompatible(fImage->colorSpace())) { |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 136 | return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY, |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 137 | as_IB(fImage.get()), rec, alloc); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 138 | } |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 139 | #endif |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 140 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 141 | SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const { |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 142 | if (texM) { |
| 143 | *texM = this->getLocalMatrix(); |
| 144 | } |
| 145 | if (xy) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 146 | xy[0] = fTileModeX; |
| 147 | xy[1] = fTileModeY; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 148 | } |
| 149 | return const_cast<SkImage*>(fImage.get()); |
| 150 | } |
| 151 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 152 | sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 153 | SkTileMode tmx, SkTileMode tmy, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 154 | const SkMatrix* localMatrix, |
| 155 | bool clampAsIfUnpremul) { |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 156 | if (!image) { |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 157 | return sk_make_sp<SkEmptyShader>(); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 158 | } |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 159 | return sk_sp<SkShader>{ new SkImageShader(image, tmx, tmy, localMatrix, clampAsIfUnpremul) }; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 160 | } |
| 161 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 162 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 163 | |
| 164 | #if SK_SUPPORT_GPU |
| 165 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 166 | #include "include/private/GrRecordingContext.h" |
| 167 | #include "src/gpu/GrCaps.h" |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 168 | #include "src/gpu/GrColorInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 169 | #include "src/gpu/GrRecordingContextPriv.h" |
| 170 | #include "src/gpu/SkGr.h" |
| 171 | #include "src/gpu/effects/GrBicubicEffect.h" |
| 172 | #include "src/gpu/effects/generated/GrSimpleTextureEffect.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 173 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 174 | static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkTileMode tileMode) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 175 | switch (tileMode) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 176 | case SkTileMode::kClamp: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 177 | return GrSamplerState::WrapMode::kClamp; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 178 | case SkTileMode::kRepeat: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 179 | return GrSamplerState::WrapMode::kRepeat; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 180 | case SkTileMode::kMirror: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 181 | return GrSamplerState::WrapMode::kMirrorRepeat; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 182 | case SkTileMode::kDecal: |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 183 | return GrSamplerState::WrapMode::kClampToBorder; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 184 | } |
| 185 | SK_ABORT("Unknown tile mode."); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 188 | std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor( |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 189 | const GrFPArgs& args) const { |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 190 | const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 191 | SkMatrix lmInverse; |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 192 | if (!lm->invert(&lmInverse)) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 193 | return nullptr; |
| 194 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 195 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 196 | GrSamplerState::WrapMode wrapModes[] = {tile_mode_to_wrap_mode(fTileModeX), |
| 197 | tile_mode_to_wrap_mode(fTileModeY)}; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 198 | |
| 199 | // If either domainX or domainY are un-ignored, a texture domain effect has to be used to |
| 200 | // implement the decal mode (while leaving non-decal axes alone). The wrap mode originally |
| 201 | // clamp-to-border is reset to clamp since the hw cannot implement it directly. |
| 202 | GrTextureDomain::Mode domainX = GrTextureDomain::kIgnore_Mode; |
| 203 | GrTextureDomain::Mode domainY = GrTextureDomain::kIgnore_Mode; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 204 | if (!args.fContext->priv().caps()->clampToBorderSupport()) { |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 205 | if (wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 206 | domainX = GrTextureDomain::kDecal_Mode; |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 207 | wrapModes[0] = GrSamplerState::WrapMode::kClamp; |
| 208 | } |
| 209 | if (wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 210 | domainY = GrTextureDomain::kDecal_Mode; |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 211 | wrapModes[1] = GrSamplerState::WrapMode::kClamp; |
| 212 | } |
| 213 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 214 | |
| 215 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
| 216 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
| 217 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
| 218 | // are provided by the caller. |
| 219 | bool doBicubic; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 220 | GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode( |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 221 | fImage->width(), fImage->height(), args.fFilterQuality, *args.fViewMatrix, *lm, |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 222 | args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 223 | GrSamplerState samplerState(wrapModes, textureFilterMode); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 224 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 225 | sk_sp<GrTextureProxy> proxy(as_IB(fImage)->asTextureProxyRef(args.fContext, samplerState, |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 226 | scaleAdjust)); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 227 | if (!proxy) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 228 | return nullptr; |
| 229 | } |
| 230 | |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 231 | GrColorType srcColorType = SkColorTypeToGrColorType(fImage->colorType()); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 232 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 233 | lmInverse.postScale(scaleAdjust[0], scaleAdjust[1]); |
| 234 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 235 | std::unique_ptr<GrFragmentProcessor> inner; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 236 | if (doBicubic) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 237 | // domainX and domainY will properly apply the decal effect with the texture domain used in |
| 238 | // the bicubic filter if clamp to border was unsupported in hardware |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 239 | static constexpr auto kDir = GrBicubicEffect::Direction::kXY; |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 240 | inner = GrBicubicEffect::Make(std::move(proxy), srcColorType, lmInverse, wrapModes, domainX, |
| 241 | domainY, kDir, fImage->alphaType()); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 242 | } else { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 243 | if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) { |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame^] | 244 | SkRect domain = GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(proxy->dimensions()), |
| 245 | domainX, domainY); |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 246 | inner = GrTextureDomainEffect::Make(std::move(proxy), srcColorType, lmInverse, domain, |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 247 | domainX, domainY, samplerState); |
| 248 | } else { |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 249 | inner = GrSimpleTextureEffect::Make(std::move(proxy), srcColorType, lmInverse, |
| 250 | samplerState); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 251 | } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 252 | } |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 253 | inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(), |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 254 | fImage->alphaType(), args.fDstColorInfo->colorSpace()); |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 255 | |
| 256 | bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType()); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 257 | if (isAlphaOnly) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 258 | return inner; |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 259 | } else if (args.fInputColorIsOpaque) { |
| 260 | return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 261 | } |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 262 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | #endif |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 266 | |
| 267 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 268 | #include "src/core/SkImagePriv.h" |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 269 | |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 270 | sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy, |
| 271 | const SkMatrix* localMatrix, SkCopyPixelsMode cpm) { |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 272 | return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm), |
| 273 | tmx, tmy, localMatrix); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 276 | sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 277 | SkTileMode tmx, SkTileMode tmy, |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 278 | const SkMatrix* localMatrix, SkCopyPixelsMode mode) { |
| 279 | auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode); |
| 280 | if (!s) { |
| 281 | return nullptr; |
| 282 | } |
| 283 | if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) { |
| 284 | // Compose the image shader with the paint's shader. Alpha images+shaders should output the |
| 285 | // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with |
| 286 | // the source image and dst shader (MakeBlend takes dst first, src second). |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 287 | s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s)); |
Michael Ludwig | c47e81b | 2019-04-02 15:18:02 -0400 | [diff] [blame] | 288 | } |
| 289 | return s; |
| 290 | } |
| 291 | |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 292 | void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); } |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 293 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 294 | class SkImageStageUpdater : public SkStageUpdater { |
| 295 | public: |
| 296 | const SkImageShader* fShader; |
| 297 | |
| 298 | float fMatrixStorage[6]; |
| 299 | |
| 300 | #if 0 // TODO: when we support mipmaps |
| 301 | SkRasterPipeline_GatherCtx* fGather; |
| 302 | SkRasterPipeline_TileCtx* fLimitX; |
| 303 | SkRasterPipeline_TileCtx* fLimitY; |
| 304 | SkRasterPipeline_DecalTileCtx* fDecal; |
| 305 | #endif |
| 306 | |
| 307 | bool update(const SkMatrix& ctm, const SkMatrix* localM) override { |
| 308 | SkMatrix matrix; |
| 309 | return fShader->computeTotalInverse(ctm, localM, &matrix) && |
| 310 | matrix.asAffine(fMatrixStorage); |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const { |
| 315 | if (updater && |
| 316 | (rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality || |
| 317 | rec.fCTM.hasPerspective())) |
| 318 | { |
| 319 | // TODO: handle these cases |
| 320 | // medium: recall RequestBitmap and update width/height accordingly |
| 321 | // perspt: store 9 floats and use persp stage |
| 322 | return false; |
| 323 | } |
| 324 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 325 | SkRasterPipeline* p = rec.fPipeline; |
| 326 | SkArenaAlloc* alloc = rec.fAlloc; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 327 | auto quality = rec.fPaint.getFilterQuality(); |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 328 | |
Florin Malita | 7558e4d | 2018-02-07 10:05:53 -0500 | [diff] [blame] | 329 | SkMatrix matrix; |
| 330 | if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) { |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 331 | return false; |
| 332 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 333 | |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 334 | const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), |
| 335 | matrix, quality, alloc); |
Mike Klein | f447dee | 2016-11-29 16:37:12 -0500 | [diff] [blame] | 336 | if (!state) { |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | const SkPixmap& pm = state->pixmap(); |
| 341 | matrix = state->invMatrix(); |
| 342 | quality = state->quality(); |
| 343 | auto info = pm.info(); |
| 344 | |
Mike Klein | e8de024 | 2018-03-10 12:37:11 -0500 | [diff] [blame] | 345 | p->append(SkRasterPipeline::seed_shader); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 346 | |
| 347 | if (updater) { |
| 348 | p->append(SkRasterPipeline::matrix_2x3, updater->fMatrixStorage); |
| 349 | } else { |
| 350 | // When the matrix is just an integer translate, bilerp == nearest neighbor. |
| 351 | if (quality == kLow_SkFilterQuality && |
| 352 | matrix.getType() <= SkMatrix::kTranslate_Mask && |
| 353 | matrix.getTranslateX() == (int)matrix.getTranslateX() && |
| 354 | matrix.getTranslateY() == (int)matrix.getTranslateY()) { |
| 355 | quality = kNone_SkFilterQuality; |
| 356 | } |
| 357 | |
| 358 | // See skia:4649 and the GM image_scale_aligned. |
| 359 | if (quality == kNone_SkFilterQuality) { |
| 360 | if (matrix.getScaleX() >= 0) { |
| 361 | matrix.setTranslateX(nextafterf(matrix.getTranslateX(), |
| 362 | floorf(matrix.getTranslateX()))); |
| 363 | } |
| 364 | if (matrix.getScaleY() >= 0) { |
| 365 | matrix.setTranslateY(nextafterf(matrix.getTranslateY(), |
| 366 | floorf(matrix.getTranslateY()))); |
| 367 | } |
| 368 | } |
| 369 | p->append_matrix(alloc, matrix); |
| 370 | } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 371 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 372 | auto gather = alloc->make<SkRasterPipeline_GatherCtx>(); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 373 | gather->pixels = pm.addr(); |
Mike Klein | 968af43 | 2017-07-18 16:31:55 -0400 | [diff] [blame] | 374 | gather->stride = pm.rowBytesAsPixels(); |
Mike Klein | f3b4e16 | 2017-09-22 15:32:59 -0400 | [diff] [blame] | 375 | gather->width = pm.width(); |
| 376 | gather->height = pm.height(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 377 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 378 | auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(), |
| 379 | limit_y = alloc->make<SkRasterPipeline_TileCtx>(); |
Mike Reed | 51e46d5 | 2017-06-23 14:21:25 -0400 | [diff] [blame] | 380 | limit_x->scale = pm.width(); |
| 381 | limit_x->invScale = 1.0f / pm.width(); |
| 382 | limit_y->scale = pm.height(); |
| 383 | limit_y->invScale = 1.0f / pm.height(); |
Mike Klein | fc84dc5 | 2017-05-11 15:29:31 -0400 | [diff] [blame] | 384 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 385 | SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 386 | bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal; |
| 387 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 388 | decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>(); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 389 | decal_ctx->limit_x = limit_x->scale; |
| 390 | decal_ctx->limit_y = limit_y->scale; |
| 391 | } |
| 392 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 393 | #if 0 // TODO: when we support kMedium |
| 394 | if (updator && (quality == kMedium_SkFilterQuality)) { |
| 395 | // if we change levels in mipmap, we need to update the scales (and invScales) |
| 396 | updator->fGather = gather; |
| 397 | updator->fLimitX = limit_x; |
| 398 | updator->fLimitY = limit_y; |
| 399 | updator->fDecal = decal_ctx; |
| 400 | } |
| 401 | #endif |
| 402 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 403 | auto append_tiling_and_gather = [&] { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 404 | if (decal_x_and_y) { |
| 405 | p->append(SkRasterPipeline::decal_x_and_y, decal_ctx); |
| 406 | } else { |
| 407 | switch (fTileModeX) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 408 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 409 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break; |
| 410 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break; |
| 411 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 412 | } |
| 413 | switch (fTileModeY) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 414 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
| 415 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break; |
| 416 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break; |
| 417 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 418 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 419 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 420 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 421 | void* ctx = gather; |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 422 | switch (info.colorType()) { |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 423 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 424 | case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break; |
| 425 | case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 426 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break; |
| 427 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 428 | case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break; |
| 429 | case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break; |
| 430 | case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 431 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break; |
| 432 | case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 433 | case kR16G16B16A16_unorm_SkColorType: |
| 434 | p->append(SkRasterPipeline::gather_16161616,ctx); break; |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 435 | case kRGBA_F16Norm_SkColorType: |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 436 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 437 | case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 438 | |
Mike Klein | b1df5e5 | 2018-10-17 17:06:03 -0400 | [diff] [blame] | 439 | case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); |
| 440 | p->append(SkRasterPipeline::alpha_to_gray ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 441 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 442 | case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 443 | p->append(SkRasterPipeline::force_opaque ); break; |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 444 | |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 445 | case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
| 446 | p->append(SkRasterPipeline::force_opaque ); break; |
| 447 | |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 448 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
| 449 | p->append(SkRasterPipeline::swap_rb ); break; |
| 450 | |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 451 | case kUnknown_SkColorType: SkASSERT(false); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 452 | } |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 453 | if (decal_ctx) { |
| 454 | p->append(SkRasterPipeline::check_decal_mask, decal_ctx); |
| 455 | } |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 456 | }; |
| 457 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 458 | auto append_misc = [&] { |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 459 | // TODO: if ref.fDstCS isn't null, we'll premul here then immediately unpremul |
| 460 | // to do the color space transformation. Might be possible to streamline. |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 461 | if (info.colorType() == kAlpha_8_SkColorType) { |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 462 | // The color for A8 images comes from the (sRGB) paint color. |
Mike Klein | be56949 | 2018-09-14 09:34:21 -0400 | [diff] [blame] | 463 | p->append_set_rgb(alloc, rec.fPaint.getColor4f()); |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 464 | p->append(SkRasterPipeline::premul); |
| 465 | } else if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 466 | // Convert unpremul images to premul before we carry on with the rest of the pipeline. |
Mike Klein | 47cf048 | 2018-02-09 18:57:54 +0000 | [diff] [blame] | 467 | p->append(SkRasterPipeline::premul); |
| 468 | } |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 469 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 470 | if (quality > kLow_SkFilterQuality) { |
| 471 | // Bicubic filtering naturally produces out of range values on both sides. |
| 472 | p->append(SkRasterPipeline::clamp_0); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 473 | p->append(fClampAsIfUnpremul ? SkRasterPipeline::clamp_1 |
| 474 | : SkRasterPipeline::clamp_a); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 475 | } |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 476 | |
| 477 | if (rec.fDstCS) { |
| 478 | // If color managed, convert from premul source all the way to premul dst color space. |
| 479 | auto srcCS = info.colorSpace(); |
| 480 | if (!srcCS || info.colorType() == kAlpha_8_SkColorType) { |
| 481 | // We treat untagged images as sRGB. |
| 482 | // A8 images get their r,g,b from the paint color, so they're also sRGB. |
Mike Klein | e28a6b5 | 2018-07-25 13:05:17 -0400 | [diff] [blame] | 483 | srcCS = sk_srgb_singleton(); |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 484 | } |
Mike Klein | 8f3d36c | 2018-08-14 10:28:05 -0400 | [diff] [blame] | 485 | alloc->make<SkColorSpaceXformSteps>(srcCS , kPremul_SkAlphaType, |
| 486 | rec.fDstCS, kPremul_SkAlphaType) |
Mike Klein | c23d1ab | 2018-10-29 09:39:52 -0400 | [diff] [blame] | 487 | ->apply(p, info.colorType()); |
Mike Klein | b82edcc | 2018-07-10 18:25:03 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 490 | return true; |
| 491 | }; |
| 492 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 493 | // Check for fast-path stages. |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 494 | auto ct = info.colorType(); |
| 495 | if (true |
| 496 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 497 | && quality == kLow_SkFilterQuality |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 498 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 499 | |
| 500 | p->append(SkRasterPipeline::bilerp_clamp_8888, gather); |
Mike Klein | 8e3426f | 2018-04-16 12:56:24 -0400 | [diff] [blame] | 501 | if (ct == kBGRA_8888_SkColorType) { |
| 502 | p->append(SkRasterPipeline::swap_rb); |
| 503 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 504 | return append_misc(); |
| 505 | } |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 506 | if (true |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 507 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 508 | && quality == kLow_SkFilterQuality |
| 509 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 510 | && fTileModeY != SkTileMode::kDecal) { |
| 511 | |
| 512 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 513 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 514 | ctx->ct = ct; |
| 515 | ctx->tileX = fTileModeX; |
| 516 | ctx->tileY = fTileModeY; |
| 517 | ctx->invWidth = 1.0f / ctx->width; |
| 518 | ctx->invHeight = 1.0f / ctx->height; |
| 519 | p->append(SkRasterPipeline::bilinear, ctx); |
| 520 | return append_misc(); |
| 521 | } |
| 522 | if (true |
Mike Reed | 78eedba | 2019-07-31 16:39:15 -0400 | [diff] [blame] | 523 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
| 524 | && quality == kHigh_SkFilterQuality |
| 525 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
| 526 | |
| 527 | p->append(SkRasterPipeline::bicubic_clamp_8888, gather); |
| 528 | if (ct == kBGRA_8888_SkColorType) { |
| 529 | p->append(SkRasterPipeline::swap_rb); |
| 530 | } |
| 531 | return append_misc(); |
| 532 | } |
Mike Klein | 0100562 | 2019-08-13 12:22:17 -0400 | [diff] [blame] | 533 | if (true |
| 534 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
| 535 | && quality == kHigh_SkFilterQuality |
| 536 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
| 537 | && fTileModeY != SkTileMode::kDecal) { |
| 538 | |
| 539 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
| 540 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
| 541 | ctx->ct = ct; |
| 542 | ctx->tileX = fTileModeX; |
| 543 | ctx->tileY = fTileModeY; |
| 544 | ctx->invWidth = 1.0f / ctx->width; |
| 545 | ctx->invHeight = 1.0f / ctx->height; |
| 546 | p->append(SkRasterPipeline::bicubic, ctx); |
| 547 | return append_misc(); |
| 548 | } |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 549 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 550 | SkRasterPipeline_SamplerCtx* sampler = nullptr; |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 551 | if (quality != kNone_SkFilterQuality) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 552 | sampler = alloc->make<SkRasterPipeline_SamplerCtx>(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 553 | } |
| 554 | |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 555 | auto sample = [&](SkRasterPipeline::StockStage setup_x, |
| 556 | SkRasterPipeline::StockStage setup_y) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 557 | p->append(setup_x, sampler); |
| 558 | p->append(setup_y, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 559 | append_tiling_and_gather(); |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 560 | p->append(SkRasterPipeline::accumulate, sampler); |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 561 | }; |
| 562 | |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 563 | if (quality == kNone_SkFilterQuality) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 564 | append_tiling_and_gather(); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 565 | } else if (quality == kLow_SkFilterQuality) { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 566 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 567 | |
| 568 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny); |
| 569 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny); |
| 570 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py); |
| 571 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py); |
| 572 | |
| 573 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 574 | |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 575 | } else { |
Mike Klein | 0a90449 | 2017-04-12 12:52:48 -0400 | [diff] [blame] | 576 | p->append(SkRasterPipeline::save_xy, sampler); |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 577 | |
| 578 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y); |
| 579 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y); |
| 580 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y); |
| 581 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y); |
| 582 | |
| 583 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y); |
| 584 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y); |
| 585 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y); |
| 586 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y); |
| 587 | |
| 588 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y); |
| 589 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y); |
| 590 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y); |
| 591 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y); |
| 592 | |
| 593 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y); |
| 594 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y); |
| 595 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y); |
| 596 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y); |
| 597 | |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 598 | p->append(SkRasterPipeline::move_dst_src); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 599 | } |
| 600 | |
Mike Klein | 1fa9c43 | 2017-12-11 09:59:47 -0500 | [diff] [blame] | 601 | return append_misc(); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 602 | } |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 603 | |
| 604 | bool SkImageShader::onAppendStages(const SkStageRec& rec) const { |
| 605 | return this->doStages(rec, nullptr); |
| 606 | } |
| 607 | |
| 608 | SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const { |
| 609 | auto updater = rec.fAlloc->make<SkImageStageUpdater>(); |
| 610 | updater->fShader = this; |
| 611 | return this->doStages(rec, updater) ? updater : nullptr; |
| 612 | } |
| 613 | |