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 "src/gpu/effects/GrTextureDomain.h" |
12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
13 | |
14 | class GrInvariantOutput; |
15 | |
16 | class GrBicubicEffect : public GrFragmentProcessor { |
17 | public: |
18 | 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 | }; |
22 | |
23 | 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 | |
32 | const char* name() const override { return "Bicubic" ; } |
33 | |
34 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
35 | return std::unique_ptr<GrFragmentProcessor>(new GrBicubicEffect(*this)); |
36 | } |
37 | |
38 | /** |
39 | * Create a Mitchell filter effect with specified texture matrix with clamp wrap mode. |
40 | */ |
41 | static std::unique_ptr<GrFragmentProcessor> Make(GrSurfaceProxyView view, |
42 | SkAlphaType, |
43 | const SkMatrix&, |
44 | Direction direction); |
45 | |
46 | /** |
47 | * Create a Mitchell filter effect for a texture with arbitrary wrap modes. |
48 | */ |
49 | static std::unique_ptr<GrFragmentProcessor> Make(GrSurfaceProxyView view, |
50 | SkAlphaType, |
51 | const SkMatrix&, |
52 | const GrSamplerState::WrapMode wrapX, |
53 | const GrSamplerState::WrapMode wrapY, |
54 | Direction, |
55 | const GrCaps&); |
56 | |
57 | /** |
58 | * Create a Mitchell filter effect for a subset of a texture, specified by a texture coordinate |
59 | * rectangle subset. The WrapModes apply to the subset. |
60 | */ |
61 | static std::unique_ptr<GrFragmentProcessor> MakeSubset(GrSurfaceProxyView view, |
62 | SkAlphaType, |
63 | const SkMatrix&, |
64 | const GrSamplerState::WrapMode wrapX, |
65 | const GrSamplerState::WrapMode wrapY, |
66 | const SkRect& subset, |
67 | Direction, |
68 | const GrCaps&); |
69 | |
70 | /** |
71 | * Make a Mitchell filter of a another fragment processor. The bicubic filter assumes that the |
72 | * discrete samples of the provided processor are at half-integer coords. |
73 | */ |
74 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor>, |
75 | SkAlphaType, |
76 | const SkMatrix&, |
77 | Direction); |
78 | |
79 | /** |
80 | * Determines whether the bicubic effect should be used based on the transformation from the |
81 | * local coords to the device. Returns true if the bicubic effect should be used. filterMode |
82 | * is set to appropriate filtering mode to use regardless of the return result (e.g. when this |
83 | * returns false it may indicate that the best fallback is to use kMipMap, kBilerp, or |
84 | * kNearest). |
85 | */ |
86 | static bool ShouldUseBicubic(const SkMatrix& localCoordsToDevice, |
87 | GrSamplerState::Filter* filterMode); |
88 | |
89 | private: |
90 | class Impl; |
91 | |
92 | enum class Clamp { |
93 | kUnpremul, // clamps rgba to 0..1 |
94 | kPremul, // clamps a to 0..1 and rgb to 0..a |
95 | }; |
96 | |
97 | GrBicubicEffect(std::unique_ptr<GrFragmentProcessor>, const SkMatrix&, Direction, Clamp); |
98 | |
99 | explicit GrBicubicEffect(const GrBicubicEffect&); |
100 | |
101 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
102 | |
103 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
104 | |
105 | bool onIsEqual(const GrFragmentProcessor&) const override; |
106 | |
107 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f&) const override; |
108 | |
109 | GrCoordTransform fCoordTransform; |
110 | Direction fDirection; |
111 | Clamp fClamp; |
112 | |
113 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
114 | |
115 | typedef GrFragmentProcessor INHERITED; |
116 | }; |
117 | |
118 | #endif |
119 | |