blob: 9167b9cc6f6e4ad384a34a27cc7f6a22100f96a8 [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
11#include "GrSingleTextureEffect.h"
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000012#include "GrTextureDomain.h"
wangyix6af0c932015-07-22 10:21:17 -070013#include "gl/GrGLFragmentProcessor.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000014
15class GrGLBicubicEffect;
egdaniel605dd0f2014-11-12 08:35:25 -080016class GrInvariantOutput;
humper@google.com3aad3b02013-09-04 19:23:53 +000017
18class GrBicubicEffect : public GrSingleTextureEffect {
19public:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000020 enum {
21 kFilterTexelPad = 2, // Given a src rect in texels to be filtered, this number of
22 // surrounding texels are needed by the kernel in x and y.
23 };
humper@google.com3aad3b02013-09-04 19:23:53 +000024 virtual ~GrBicubicEffect();
25
humper@google.com3aad3b02013-09-04 19:23:53 +000026 const float* coefficients() const { return fCoefficients; }
27
mtklein36352bf2015-03-25 18:17:31 -070028 const char* name() const override { return "Bicubic"; }
humper@google.com3aad3b02013-09-04 19:23:53 +000029
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000030 const GrTextureDomain& domain() const { return fDomain; }
humper@google.com3aad3b02013-09-04 19:23:53 +000031
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000032 /**
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000033 * Create a simple filter effect with custom bicubic coefficients and optional domain.
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000034 */
bsalomon4a339522015-10-06 08:40:50 -070035 static GrFragmentProcessor* Create(GrTexture* tex, const SkScalar coefficients[16],
halcanary96fcdcc2015-08-27 07:41:13 -070036 const SkRect* domain = nullptr) {
37 if (nullptr == domain) {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000038 static const SkShader::TileMode kTileModes[] = { SkShader::kClamp_TileMode,
39 SkShader::kClamp_TileMode };
bsalomon4a339522015-10-06 08:40:50 -070040 return Create(tex, coefficients, GrCoordTransform::MakeDivByTextureWHMatrix(tex),
41 kTileModes);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000042 } else {
bsalomon4a339522015-10-06 08:40:50 -070043 return new GrBicubicEffect(tex, coefficients,
halcanary385fe4d2015-08-26 13:07:48 -070044 GrCoordTransform::MakeDivByTextureWHMatrix(tex), *domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000045 }
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000046 }
47
48 /**
49 * Create a Mitchell filter effect with specified texture matrix and x/y tile modes.
50 */
bsalomon4a339522015-10-06 08:40:50 -070051 static GrFragmentProcessor* Create(GrTexture* tex, const SkMatrix& matrix,
joshualitt5f10b5c2015-07-09 10:24:35 -070052 SkShader::TileMode tileModes[2]) {
bsalomon4a339522015-10-06 08:40:50 -070053 return Create(tex, gMitchellCoefficients, matrix, tileModes);
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000054 }
55
56 /**
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000057 * Create a filter effect with custom bicubic coefficients, the texture matrix, and the x/y
58 * tilemodes.
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000059 */
bsalomon4a339522015-10-06 08:40:50 -070060 static GrFragmentProcessor* Create(GrTexture* tex, const SkScalar coefficients[16],
61 const SkMatrix& matrix,
joshualittb0a8a372014-09-23 09:50:21 -070062 const SkShader::TileMode tileModes[2]) {
bsalomon4a339522015-10-06 08:40:50 -070063 return new GrBicubicEffect(tex, coefficients, matrix, tileModes);
humper@google.com3aad3b02013-09-04 19:23:53 +000064 }
65
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000066 /**
67 * Create a Mitchell filter effect with a texture matrix and a domain.
68 */
bsalomon4a339522015-10-06 08:40:50 -070069 static GrFragmentProcessor* Create(GrTexture* tex, const SkMatrix& matrix,
70 const SkRect& domain) {
71 return new GrBicubicEffect(tex, gMitchellCoefficients, matrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000072 }
73
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +000074 /**
75 * Determines whether the bicubic effect should be used based on the transformation from the
76 * local coords to the device. Returns true if the bicubic effect should be used. filterMode
77 * is set to appropriate filtering mode to use regardless of the return result (e.g. when this
78 * returns false it may indicate that the best fallback is to use kMipMap, kBilerp, or
79 * kNearest).
80 */
81 static bool ShouldUseBicubic(const SkMatrix& localCoordsToDevice,
82 GrTextureParams::FilterMode* filterMode);
83
humper@google.com3aad3b02013-09-04 19:23:53 +000084private:
bsalomon4a339522015-10-06 08:40:50 -070085 GrBicubicEffect(GrTexture*, const SkScalar coefficients[16], const SkMatrix &matrix,
86 const SkShader::TileMode tileModes[2]);
87 GrBicubicEffect(GrTexture*, const SkScalar coefficients[16], const SkMatrix &matrix,
88 const SkRect& domain);
wangyix4b3050b2015-08-04 07:59:37 -070089
wangyixb1daa862015-08-18 11:29:31 -070090 GrGLFragmentProcessor* onCreateGLInstance() const override;
91
wangyix4b3050b2015-08-04 07:59:37 -070092 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
93
mtklein36352bf2015-03-25 18:17:31 -070094 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000095
mtklein36352bf2015-03-25 18:17:31 -070096 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -070097
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000098 float fCoefficients[16];
99 GrTextureDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000100
joshualittb0a8a372014-09-23 09:50:21 -0700101 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +0000102
humper@google.com3aad3b02013-09-04 19:23:53 +0000103 static const SkScalar gMitchellCoefficients[16];
104
105 typedef GrSingleTextureEffect INHERITED;
106};
107
108#endif