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 | |
| 11 | #include "GrSingleTextureEffect.h" |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 12 | #include "GrTextureDomain.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 13 | #include "GrDrawEffect.h" |
| 14 | #include "gl/GrGLEffect.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 15 | #include "GrTBackendEffectFactory.h" |
| 16 | |
| 17 | class GrGLBicubicEffect; |
| 18 | |
| 19 | class GrBicubicEffect : public GrSingleTextureEffect { |
| 20 | public: |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 21 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 25 | 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.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 35 | const GrTextureDomain& domain() const { return fDomain; } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 37 | /** |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 38 | * Create a simple filter effect with custom bicubic coefficients and optional domain. |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 39 | */ |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 40 | 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.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create a Mitchell filter effect with specified texture matrix and x/y tile modes. |
| 56 | */ |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 57 | static GrEffectRef* Create(GrTexture* tex, const SkMatrix& matrix, |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 58 | SkShader::TileMode tileModes[2]) { |
| 59 | return Create(tex, gMitchellCoefficients, matrix, tileModes); |
| 60 | } |
| 61 | |
| 62 | /** |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 63 | * Create a filter effect with custom bicubic coefficients, the texture matrix, and the x/y |
| 64 | * tilemodes. |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 65 | */ |
| 66 | static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16], |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 67 | const SkMatrix& matrix, const SkShader::TileMode tileModes[2]) { |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 68 | AutoEffectUnref effect(SkNEW_ARGS(GrBicubicEffect, (tex, coefficients, matrix, tileModes))); |
| 69 | return CreateEffectRef(effect); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 70 | } |
| 71 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 72 | /** |
| 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.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 81 | /** |
| 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 91 | private: |
skia.committer@gmail.com | c3723db | 2013-09-05 07:01:19 +0000 | [diff] [blame] | 92 | GrBicubicEffect(GrTexture*, const SkScalar coefficients[16], |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 93 | const SkMatrix &matrix, const SkShader::TileMode tileModes[2]); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 94 | GrBicubicEffect(GrTexture*, const SkScalar coefficients[16], |
| 95 | const SkMatrix &matrix, const SkRect& domain); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 96 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 97 | |
| 98 | float fCoefficients[16]; |
| 99 | GrTextureDomain fDomain; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 100 | |
| 101 | GR_DECLARE_EFFECT_TEST; |
skia.committer@gmail.com | c3723db | 2013-09-05 07:01:19 +0000 | [diff] [blame] | 102 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 103 | static const SkScalar gMitchellCoefficients[16]; |
| 104 | |
| 105 | typedef GrSingleTextureEffect INHERITED; |
| 106 | }; |
| 107 | |
| 108 | #endif |