humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| 8 | #ifndef GrBicubicTextureEffect_DEFINED |
| 9 | #define GrBicubicTextureEffect_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/effects/GrTextureDomain.h" |
| 12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 13 | |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 14 | class GrInvariantOutput; |
| 15 | |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 16 | class GrBicubicEffect : public GrFragmentProcessor { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 17 | public: |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 18 | enum { |
| 19 | kFilterTexelPad = 2, // Given a src rect in texels to be filtered, this number of |
| 20 | // surrounding texels are needed by the kernel in x and y. |
| 21 | }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 22 | |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 23 | enum class Direction { |
| 24 | /** Apply bicubic kernel in local coord x, nearest neighbor in y. */ |
| 25 | kX, |
| 26 | /** Apply bicubic kernel in local coord y, nearest neighbor in x. */ |
| 27 | kY, |
| 28 | /** Apply bicubic in both x and y. */ |
| 29 | kXY |
| 30 | }; |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | const char* name() const override { return "Bicubic"; } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 33 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 34 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 35 | return std::unique_ptr<GrFragmentProcessor>(new GrBicubicEffect(*this)); |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 36 | } |
| 37 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 38 | const GrTextureDomain& domain() const { return fDomain; } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 39 | |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 40 | Direction direction() const { return fDirection; } |
| 41 | |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 42 | SkAlphaType alphaType() const { return fAlphaType; } |
| 43 | |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 44 | /** |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 45 | * Create a Mitchell filter effect with specified texture matrix with clamp wrap mode. |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 46 | */ |
| 47 | static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
| 48 | const SkMatrix& matrix, |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 49 | Direction direction, |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 50 | SkAlphaType alphaType) { |
| 51 | static constexpr GrSamplerState::WrapMode kClampClamp[] = { |
| 52 | GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp}; |
| 53 | return Make(std::move(proxy), matrix, kClampClamp, GrTextureDomain::kIgnore_Mode, |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 54 | GrTextureDomain::kIgnore_Mode, direction, alphaType); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 58 | * Create a Mitchell filter effect with specified texture matrix and x/y tile modes. |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 59 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 60 | static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 61 | const SkMatrix& matrix, |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 62 | const GrSamplerState::WrapMode wrapModes[2], |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 63 | Direction direction, |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 64 | SkAlphaType alphaType) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 65 | // Ignore the domain on x and y, since this factory relies solely on the wrap mode of the |
| 66 | // sampler to constrain texture coordinates |
| 67 | return Make(std::move(proxy), matrix, wrapModes, GrTextureDomain::kIgnore_Mode, |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 68 | GrTextureDomain::kIgnore_Mode, direction, alphaType); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 72 | * Create a Mitchell filter effect with specified texture matrix and x/y tile modes. This |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 73 | * supports providing modes for the texture domain explicitly, in the event that it should |
| 74 | * override the behavior of the sampler's tile mode (e.g. clamp to border unsupported). |
| 75 | */ |
| 76 | static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
| 77 | const SkMatrix& matrix, |
| 78 | const GrSamplerState::WrapMode wrapModes[2], |
| 79 | GrTextureDomain::Mode modeX, |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 80 | GrTextureDomain::Mode modeY, |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 81 | Direction direction, |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 82 | SkAlphaType alphaType, |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 83 | const SkRect* domain = nullptr) { |
| 84 | SkRect resolvedDomain = domain ? *domain : GrTextureDomain::MakeTexelDomain( |
| 85 | SkIRect::MakeWH(proxy->width(), proxy->height()), modeX, modeY); |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 86 | return std::unique_ptr<GrFragmentProcessor>( |
| 87 | new GrBicubicEffect(std::move(proxy), matrix, resolvedDomain, wrapModes, modeX, |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 88 | modeY, direction, alphaType)); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 92 | * Create a Mitchell filter effect with a texture matrix and a domain. |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 93 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 94 | static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 95 | const SkMatrix& matrix, |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 96 | const SkRect& domain, |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 97 | Direction direction, |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 98 | SkAlphaType alphaType) { |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 99 | static const GrSamplerState::WrapMode kClampClamp[] = { |
| 100 | GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp}; |
| 101 | return Make(std::move(proxy), matrix, kClampClamp, GrTextureDomain::kClamp_Mode, |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 102 | GrTextureDomain::kClamp_Mode, direction, alphaType, &domain); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 106 | * Determines whether the bicubic effect should be used based on the transformation from the |
| 107 | * local coords to the device. Returns true if the bicubic effect should be used. filterMode |
| 108 | * is set to appropriate filtering mode to use regardless of the return result (e.g. when this |
| 109 | * returns false it may indicate that the best fallback is to use kMipMap, kBilerp, or |
| 110 | * kNearest). |
| 111 | */ |
| 112 | static bool ShouldUseBicubic(const SkMatrix& localCoordsToDevice, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 113 | GrSamplerState::Filter* filterMode); |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 114 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 115 | private: |
Brian Salomon | 1127c0b | 2019-06-13 20:22:10 +0000 | [diff] [blame] | 116 | GrBicubicEffect(sk_sp<GrTextureProxy>, const SkMatrix& matrix, const SkRect& domain, |
| 117 | const GrSamplerState::WrapMode wrapModes[2], GrTextureDomain::Mode modeX, |
| 118 | GrTextureDomain::Mode modeY, Direction direction, SkAlphaType alphaType); |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 119 | explicit GrBicubicEffect(const GrBicubicEffect&); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 120 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 121 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 122 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 123 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 124 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 125 | bool onIsEqual(const GrFragmentProcessor&) const override; |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 126 | |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 127 | const TextureSampler& onTextureSampler(int) const override { return fTextureSampler; } |
| 128 | |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 129 | GrCoordTransform fCoordTransform; |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 130 | GrTextureDomain fDomain; |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 131 | TextureSampler fTextureSampler; |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 132 | SkAlphaType fAlphaType; |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 133 | Direction fDirection; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 134 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 135 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
skia.committer@gmail.com | c3723db | 2013-09-05 07:01:19 +0000 | [diff] [blame] | 136 | |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 137 | typedef GrFragmentProcessor INHERITED; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | #endif |