xref: /aosp_15_r20/external/skia/src/gpu/ganesh/effects/GrBicubicEffect.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkSamplingOptions.h"
12 #include "include/private/SkColorData.h"
13 #include "src/gpu/ganesh/GrFragmentProcessor.h"
14 #include "src/gpu/ganesh/GrProcessorUnitTest.h"
15 #include "src/gpu/ganesh/GrSamplerState.h"
16 
17 #include <memory>
18 
19 class GrCaps;
20 class GrSurfaceProxyView;
21 class SkMatrix;
22 enum SkAlphaType : int;
23 struct GrShaderCaps;
24 struct SkRect;
25 
26 namespace skgpu { class KeyBuilder; }
27 
28 class GrBicubicEffect : public GrFragmentProcessor {
29 public:
30     inline static constexpr SkCubicResampler gMitchell = { 1.0f/3, 1.0f/3 };
31     inline static constexpr SkCubicResampler gCatmullRom = {    0, 1.0f/2 };
32 
33     enum class Direction {
34         /** Apply bicubic kernel in local coord x, nearest neighbor in y. */
35         kX,
36         /** Apply bicubic kernel in local coord y, nearest neighbor in x. */
37         kY,
38         /** Apply bicubic in both x and y. */
39         kXY
40     };
41 
name()42     const char* name() const override { return "Bicubic"; }
43 
clone()44     std::unique_ptr<GrFragmentProcessor> clone() const override {
45         return std::unique_ptr<GrFragmentProcessor>(new GrBicubicEffect(*this));
46     }
47 
48     /**
49      * Create a bicubic filter effect with specified texture matrix with clamp wrap mode.
50      */
51     static std::unique_ptr<GrFragmentProcessor> Make(GrSurfaceProxyView view,
52                                                      SkAlphaType,
53                                                      const SkMatrix&,
54                                                      SkCubicResampler,
55                                                      Direction);
56 
57     /**
58      * Create a bicubic filter effect for a texture with arbitrary wrap modes.
59      */
60     static std::unique_ptr<GrFragmentProcessor> Make(GrSurfaceProxyView view,
61                                                      SkAlphaType,
62                                                      const SkMatrix&,
63                                                      const GrSamplerState::WrapMode wrapX,
64                                                      const GrSamplerState::WrapMode wrapY,
65                                                      SkCubicResampler,
66                                                      Direction,
67                                                      const GrCaps&);
68 
69     /**
70      * Create a bicubic filter effect for a subset of a texture, specified by a texture coordinate
71      * rectangle subset. The WrapModes apply to the subset.
72      */
73     static std::unique_ptr<GrFragmentProcessor> MakeSubset(GrSurfaceProxyView view,
74                                                            SkAlphaType,
75                                                            const SkMatrix&,
76                                                            const GrSamplerState::WrapMode wrapX,
77                                                            const GrSamplerState::WrapMode wrapY,
78                                                            const SkRect& subset,
79                                                            SkCubicResampler,
80                                                            Direction,
81                                                            const GrCaps&);
82 
83     /**
84      * Same as above but provides a known 'domain' that bounds the coords at which bicubic sampling
85      * occurs. Note that this is a bound on the coords after transformed by the matrix parameter.
86      */
87     static std::unique_ptr<GrFragmentProcessor> MakeSubset(GrSurfaceProxyView view,
88                                                            SkAlphaType,
89                                                            const SkMatrix&,
90                                                            const GrSamplerState::WrapMode wrapX,
91                                                            const GrSamplerState::WrapMode wrapY,
92                                                            const SkRect& subset,
93                                                            const SkRect& domain,
94                                                            SkCubicResampler,
95                                                            Direction,
96                                                            const GrCaps&);
97 
98     /**
99      * Make a bicubic filter of a another fragment processor. The bicubic filter assumes that the
100      * discrete samples of the provided processor are at half-integer coords.
101      */
102     static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor>,
103                                                      SkAlphaType,
104                                                      const SkMatrix&,
105                                                      SkCubicResampler,
106                                                      Direction);
107 
108 private:
109     class Impl;
110 
111     enum class Clamp {
112         kUnpremul,  // clamps rgba to 0..1
113         kPremul,    // clamps a to 0..1 and rgb to 0..a
114     };
115 
116     GrBicubicEffect(std::unique_ptr<GrFragmentProcessor>,
117                     SkCubicResampler,
118                     Direction,
119                     Clamp);
120 
121     explicit GrBicubicEffect(const GrBicubicEffect&);
122 
123     std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
124 
125     void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override;
126 
127     bool onIsEqual(const GrFragmentProcessor&) const override;
128 
129     SkPMColor4f constantOutputForConstantInput(const SkPMColor4f&) const override;
130 
131     SkCubicResampler fKernel;
132     Direction fDirection;
133     Clamp fClamp;
134 
135     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
136 
137     using INHERITED = GrFragmentProcessor;
138 };
139 
140 #endif
141