blob: 1998e68780c18a7bc213ae1313c60df77cda69cd [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"
humper@google.com3aad3b02013-09-04 19:23:53 +000013#include "GrDrawEffect.h"
14#include "gl/GrGLEffect.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000015#include "GrTBackendEffectFactory.h"
16
17class GrGLBicubicEffect;
18
19class GrBicubicEffect : public GrSingleTextureEffect {
20public:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000021 enum {
22 kFilterTexelPad = 2, // Given a src rect in texels to be filtered, this number of
23 // surrounding texels are needed by the kernel in x and y.
24 };
humper@google.com3aad3b02013-09-04 19:23:53 +000025 virtual ~GrBicubicEffect();
26
27 static const char* Name() { return "Bicubic"; }
28 const float* coefficients() const { return fCoefficients; }
29
30 typedef GrGLBicubicEffect GLEffect;
31
32 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
33 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
34
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000035 const GrTextureDomain& domain() const { return fDomain; }
humper@google.com3aad3b02013-09-04 19:23:53 +000036
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000037 /**
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000038 * Create a simple filter effect with custom bicubic coefficients and optional domain.
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000039 */
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000040 static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16],
41 const SkRect* domain = NULL) {
42 if (NULL == domain) {
43 static const SkShader::TileMode kTileModes[] = { SkShader::kClamp_TileMode,
44 SkShader::kClamp_TileMode };
45 return Create(tex, coefficients, MakeDivByTextureWHMatrix(tex), kTileModes);
46 } else {
47 AutoEffectUnref effect(SkNEW_ARGS(GrBicubicEffect, (tex, coefficients,
48 MakeDivByTextureWHMatrix(tex),
49 *domain)));
50 return CreateEffectRef(effect);
51 }
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000052 }
53
54 /**
55 * Create a Mitchell filter effect with specified texture matrix and x/y tile modes.
56 */
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000057 static GrEffectRef* Create(GrTexture* tex, const SkMatrix& matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000058 SkShader::TileMode tileModes[2]) {
59 return Create(tex, gMitchellCoefficients, matrix, tileModes);
60 }
61
62 /**
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000063 * Create a filter effect with custom bicubic coefficients, the texture matrix, and the x/y
64 * tilemodes.
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000065 */
66 static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16],
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000067 const SkMatrix& matrix, const SkShader::TileMode tileModes[2]) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000068 AutoEffectUnref effect(SkNEW_ARGS(GrBicubicEffect, (tex, coefficients, matrix, tileModes)));
69 return CreateEffectRef(effect);
humper@google.com3aad3b02013-09-04 19:23:53 +000070 }
71
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000072 /**
73 * Create a Mitchell filter effect with a texture matrix and a domain.
74 */
75 static GrEffectRef* Create(GrTexture* tex, const SkMatrix& matrix, const SkRect& domain) {
76 AutoEffectUnref effect(SkNEW_ARGS(GrBicubicEffect, (tex, gMitchellCoefficients, matrix,
77 domain)));
78 return CreateEffectRef(effect);
79 }
80
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +000081 /**
82 * Determines whether the bicubic effect should be used based on the transformation from the
83 * local coords to the device. Returns true if the bicubic effect should be used. filterMode
84 * is set to appropriate filtering mode to use regardless of the return result (e.g. when this
85 * returns false it may indicate that the best fallback is to use kMipMap, kBilerp, or
86 * kNearest).
87 */
88 static bool ShouldUseBicubic(const SkMatrix& localCoordsToDevice,
89 GrTextureParams::FilterMode* filterMode);
90
humper@google.com3aad3b02013-09-04 19:23:53 +000091private:
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +000092 GrBicubicEffect(GrTexture*, const SkScalar coefficients[16],
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +000093 const SkMatrix &matrix, const SkShader::TileMode tileModes[2]);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000094 GrBicubicEffect(GrTexture*, const SkScalar coefficients[16],
95 const SkMatrix &matrix, const SkRect& domain);
humper@google.com3aad3b02013-09-04 19:23:53 +000096 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000097
98 float fCoefficients[16];
99 GrTextureDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000100
101 GR_DECLARE_EFFECT_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