blob: 5e274e02834014f3bb1d0ee90055d7c1e3b2aa94 [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,
Brian Salomon078e8fa2019-11-22 04:10:18 +000048 GrColorType srcColorType,
Brian Salomon031b0ba2019-05-23 11:05:26 -040049 const SkMatrix& matrix,
Brian Salomona86fc7a2019-05-28 20:42:58 -040050 Direction direction,
Brian Salomon031b0ba2019-05-23 11:05:26 -040051 SkAlphaType alphaType) {
52 static constexpr GrSamplerState::WrapMode kClampClamp[] = {
53 GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp};
Brian Salomon078e8fa2019-11-22 04:10:18 +000054 return Make(std::move(proxy), srcColorType, matrix, kClampClamp,
55 GrTextureDomain::kIgnore_Mode, GrTextureDomain::kIgnore_Mode, direction,
56 alphaType);
Brian Salomon031b0ba2019-05-23 11:05:26 -040057 }
58
59 /**
Brian Salomon1127c0b2019-06-13 20:22:10 +000060 * Create a Mitchell filter effect with specified texture matrix and x/y tile modes.
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000061 */
Brian Salomonaff329b2017-08-11 09:40:37 -040062 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon078e8fa2019-11-22 04:10:18 +000063 GrColorType srcColorType,
Brian Salomonaff329b2017-08-11 09:40:37 -040064 const SkMatrix& matrix,
Brian Osmane22dba82019-03-13 10:22:28 -040065 const GrSamplerState::WrapMode wrapModes[2],
Brian Salomona86fc7a2019-05-28 20:42:58 -040066 Direction direction,
Brian Osmane22dba82019-03-13 10:22:28 -040067 SkAlphaType alphaType) {
Michael Ludwigbe315a22018-12-17 09:50:51 -050068 // Ignore the domain on x and y, since this factory relies solely on the wrap mode of the
69 // sampler to constrain texture coordinates
Brian Salomon078e8fa2019-11-22 04:10:18 +000070 return Make(std::move(proxy), srcColorType, matrix, wrapModes,
71 GrTextureDomain::kIgnore_Mode, GrTextureDomain::kIgnore_Mode, direction,
72 alphaType);
Michael Ludwigbe315a22018-12-17 09:50:51 -050073 }
74
75 /**
Brian Salomon1127c0b2019-06-13 20:22:10 +000076 * Create a Mitchell filter effect with specified texture matrix and x/y tile modes. This
Michael Ludwigbe315a22018-12-17 09:50:51 -050077 * supports providing modes for the texture domain explicitly, in the event that it should
78 * override the behavior of the sampler's tile mode (e.g. clamp to border unsupported).
79 */
80 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon078e8fa2019-11-22 04:10:18 +000081 GrColorType srcColorType,
Michael Ludwigbe315a22018-12-17 09:50:51 -050082 const SkMatrix& matrix,
83 const GrSamplerState::WrapMode wrapModes[2],
84 GrTextureDomain::Mode modeX,
Michael Ludwigddeed372019-02-20 16:50:10 -050085 GrTextureDomain::Mode modeY,
Brian Salomona86fc7a2019-05-28 20:42:58 -040086 Direction direction,
Brian Osmane22dba82019-03-13 10:22:28 -040087 SkAlphaType alphaType,
Michael Ludwigddeed372019-02-20 16:50:10 -050088 const SkRect* domain = nullptr) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -040089 SkRect resolvedDomain;
90 if (domain) {
91 resolvedDomain = *domain;
92 } else {
93 resolvedDomain = GrTextureDomain::MakeTexelDomain(
94 SkIRect::MakeSize(proxy->dimensions()), modeX, modeY);
95 }
Brian Salomona86fc7a2019-05-28 20:42:58 -040096 return std::unique_ptr<GrFragmentProcessor>(
Brian Salomon078e8fa2019-11-22 04:10:18 +000097 new GrBicubicEffect(std::move(proxy), srcColorType, matrix, resolvedDomain,
98 wrapModes, modeX, modeY, direction, alphaType));
Robert Phillips40fd7c92017-01-30 08:06:27 -050099 }
100
101 /**
Brian Salomon1127c0b2019-06-13 20:22:10 +0000102 * Create a Mitchell filter effect with a texture matrix and a domain.
Robert Phillips40fd7c92017-01-30 08:06:27 -0500103 */
Brian Salomonaff329b2017-08-11 09:40:37 -0400104 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon078e8fa2019-11-22 04:10:18 +0000105 GrColorType srcColorType,
Brian Salomonaff329b2017-08-11 09:40:37 -0400106 const SkMatrix& matrix,
Brian Osmane22dba82019-03-13 10:22:28 -0400107 const SkRect& domain,
Brian Salomona86fc7a2019-05-28 20:42:58 -0400108 Direction direction,
Brian Osmane22dba82019-03-13 10:22:28 -0400109 SkAlphaType alphaType) {
Michael Ludwigddeed372019-02-20 16:50:10 -0500110 static const GrSamplerState::WrapMode kClampClamp[] = {
111 GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp};
Brian Salomon078e8fa2019-11-22 04:10:18 +0000112 return Make(std::move(proxy), srcColorType, matrix, kClampClamp,
113 GrTextureDomain::kClamp_Mode, GrTextureDomain::kClamp_Mode, direction,
114 alphaType, &domain);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500115 }
116
117 /**
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000118 * Determines whether the bicubic effect should be used based on the transformation from the
119 * local coords to the device. Returns true if the bicubic effect should be used. filterMode
120 * is set to appropriate filtering mode to use regardless of the return result (e.g. when this
121 * returns false it may indicate that the best fallback is to use kMipMap, kBilerp, or
122 * kNearest).
123 */
124 static bool ShouldUseBicubic(const SkMatrix& localCoordsToDevice,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400125 GrSamplerState::Filter* filterMode);
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000126
humper@google.com3aad3b02013-09-04 19:23:53 +0000127private:
Brian Salomon078e8fa2019-11-22 04:10:18 +0000128 GrBicubicEffect(sk_sp<GrTextureProxy>, GrColorType srcColorType, const SkMatrix& matrix,
129 const SkRect& domain, const GrSamplerState::WrapMode wrapModes[2],
130 GrTextureDomain::Mode modeX, GrTextureDomain::Mode modeY, Direction direction,
131 SkAlphaType alphaType);
Brian Salomon3f6f9652017-07-28 07:34:05 -0400132 explicit GrBicubicEffect(const GrBicubicEffect&);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500133
egdaniel57d3b032015-11-13 11:57:27 -0800134 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700135
Brian Salomon94efbf52016-11-29 13:43:05 -0500136 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700137
mtklein36352bf2015-03-25 18:17:31 -0700138 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000139
Brian Salomonf7dcd762018-07-30 14:48:15 -0400140 const TextureSampler& onTextureSampler(int) const override { return fTextureSampler; }
141
Brian Salomon6cd51b52017-07-26 19:07:15 -0400142 GrCoordTransform fCoordTransform;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000143 GrTextureDomain fDomain;
Brian Salomon6cd51b52017-07-26 19:07:15 -0400144 TextureSampler fTextureSampler;
Brian Osmane22dba82019-03-13 10:22:28 -0400145 SkAlphaType fAlphaType;
Brian Salomona86fc7a2019-05-28 20:42:58 -0400146 Direction fDirection;
humper@google.com3aad3b02013-09-04 19:23:53 +0000147
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400148 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +0000149
Brian Salomon6cd51b52017-07-26 19:07:15 -0400150 typedef GrFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +0000151};
152
153#endif