blob: a19531a541ec67f3f5b2f295ee9e085d8f687591 [file] [log] [blame]
humper@google.com3aad3b02013-09-04 19:23:53 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/effects/GrTextureDomain.h"
12#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000013
Brian Salomon1127c0b2019-06-13 20:22:10 +000014class GrInvariantOutput;
15
Brian Salomon6cd51b52017-07-26 19:07:15 -040016class GrBicubicEffect : public GrFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000017public:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000018 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.com3aad3b02013-09-04 19:23:53 +000022
Brian Salomona86fc7a2019-05-28 20:42:58 -040023 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
mtklein36352bf2015-03-25 18:17:31 -070032 const char* name() const override { return "Bicubic"; }
humper@google.com3aad3b02013-09-04 19:23:53 +000033
Brian Salomonaff329b2017-08-11 09:40:37 -040034 std::unique_ptr<GrFragmentProcessor> clone() const override {
35 return std::unique_ptr<GrFragmentProcessor>(new GrBicubicEffect(*this));
Brian Salomon3f6f9652017-07-28 07:34:05 -040036 }
37
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000038 const GrTextureDomain& domain() const { return fDomain; }
humper@google.com3aad3b02013-09-04 19:23:53 +000039
Brian Salomona86fc7a2019-05-28 20:42:58 -040040 Direction direction() const { return fDirection; }
41
Brian Osmane22dba82019-03-13 10:22:28 -040042 SkAlphaType alphaType() const { return fAlphaType; }
43
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000044 /**
Brian Salomon1127c0b2019-06-13 20:22:10 +000045 * Create a Mitchell filter effect with specified texture matrix with clamp wrap mode.
Brian Salomon031b0ba2019-05-23 11:05:26 -040046 */
47 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
48 const SkMatrix& matrix,
Brian Salomona86fc7a2019-05-28 20:42:58 -040049 Direction direction,
Brian Salomon031b0ba2019-05-23 11:05:26 -040050 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 Salomon1127c0b2019-06-13 20:22:10 +000054 GrTextureDomain::kIgnore_Mode, direction, alphaType);
Brian Salomon031b0ba2019-05-23 11:05:26 -040055 }
56
57 /**
Brian Salomon1127c0b2019-06-13 20:22:10 +000058 * Create a Mitchell filter effect with specified texture matrix and x/y tile modes.
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000059 */
Brian Salomonaff329b2017-08-11 09:40:37 -040060 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomonaff329b2017-08-11 09:40:37 -040061 const SkMatrix& matrix,
Brian Osmane22dba82019-03-13 10:22:28 -040062 const GrSamplerState::WrapMode wrapModes[2],
Brian Salomona86fc7a2019-05-28 20:42:58 -040063 Direction direction,
Brian Osmane22dba82019-03-13 10:22:28 -040064 SkAlphaType alphaType) {
Michael Ludwigbe315a22018-12-17 09:50:51 -050065 // 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 Salomon1127c0b2019-06-13 20:22:10 +000068 GrTextureDomain::kIgnore_Mode, direction, alphaType);
Michael Ludwigbe315a22018-12-17 09:50:51 -050069 }
70
71 /**
Brian Salomon1127c0b2019-06-13 20:22:10 +000072 * Create a Mitchell filter effect with specified texture matrix and x/y tile modes. This
Michael Ludwigbe315a22018-12-17 09:50:51 -050073 * 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 Ludwigddeed372019-02-20 16:50:10 -050080 GrTextureDomain::Mode modeY,
Brian Salomona86fc7a2019-05-28 20:42:58 -040081 Direction direction,
Brian Osmane22dba82019-03-13 10:22:28 -040082 SkAlphaType alphaType,
Michael Ludwigddeed372019-02-20 16:50:10 -050083 const SkRect* domain = nullptr) {
84 SkRect resolvedDomain = domain ? *domain : GrTextureDomain::MakeTexelDomain(
85 SkIRect::MakeWH(proxy->width(), proxy->height()), modeX, modeY);
Brian Salomona86fc7a2019-05-28 20:42:58 -040086 return std::unique_ptr<GrFragmentProcessor>(
87 new GrBicubicEffect(std::move(proxy), matrix, resolvedDomain, wrapModes, modeX,
Brian Salomon1127c0b2019-06-13 20:22:10 +000088 modeY, direction, alphaType));
Robert Phillips40fd7c92017-01-30 08:06:27 -050089 }
90
91 /**
Brian Salomon1127c0b2019-06-13 20:22:10 +000092 * Create a Mitchell filter effect with a texture matrix and a domain.
Robert Phillips40fd7c92017-01-30 08:06:27 -050093 */
Brian Salomonaff329b2017-08-11 09:40:37 -040094 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomonaff329b2017-08-11 09:40:37 -040095 const SkMatrix& matrix,
Brian Osmane22dba82019-03-13 10:22:28 -040096 const SkRect& domain,
Brian Salomona86fc7a2019-05-28 20:42:58 -040097 Direction direction,
Brian Osmane22dba82019-03-13 10:22:28 -040098 SkAlphaType alphaType) {
Michael Ludwigddeed372019-02-20 16:50:10 -050099 static const GrSamplerState::WrapMode kClampClamp[] = {
100 GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp};
101 return Make(std::move(proxy), matrix, kClampClamp, GrTextureDomain::kClamp_Mode,
Brian Salomon1127c0b2019-06-13 20:22:10 +0000102 GrTextureDomain::kClamp_Mode, direction, alphaType, &domain);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500103 }
104
105 /**
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000106 * 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 Salomon2bbdcc42017-09-07 12:36:34 -0400113 GrSamplerState::Filter* filterMode);
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000114
humper@google.com3aad3b02013-09-04 19:23:53 +0000115private:
Brian Salomon1127c0b2019-06-13 20:22:10 +0000116 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 Salomon3f6f9652017-07-28 07:34:05 -0400119 explicit GrBicubicEffect(const GrBicubicEffect&);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500120
egdaniel57d3b032015-11-13 11:57:27 -0800121 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700122
Brian Salomon94efbf52016-11-29 13:43:05 -0500123 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700124
mtklein36352bf2015-03-25 18:17:31 -0700125 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000126
Brian Salomonf7dcd762018-07-30 14:48:15 -0400127 const TextureSampler& onTextureSampler(int) const override { return fTextureSampler; }
128
Brian Salomon6cd51b52017-07-26 19:07:15 -0400129 GrCoordTransform fCoordTransform;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000130 GrTextureDomain fDomain;
Brian Salomon6cd51b52017-07-26 19:07:15 -0400131 TextureSampler fTextureSampler;
Brian Osmane22dba82019-03-13 10:22:28 -0400132 SkAlphaType fAlphaType;
Brian Salomona86fc7a2019-05-28 20:42:58 -0400133 Direction fDirection;
humper@google.com3aad3b02013-09-04 19:23:53 +0000134
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400135 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +0000136
Brian Salomon6cd51b52017-07-26 19:07:15 -0400137 typedef GrFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +0000138};
139
140#endif