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 GrSimpleTextureEffect_DEFINED
9 #define GrSimpleTextureEffect_DEFINED
10 
11 #include "GrSingleTextureEffect.h"
12 #include "GrTextureProxy.h"
13 
14 class GrInvariantOutput;
15 
16 /**
17  * The output color of this effect is a modulation of the input color and a sample from a texture.
18  * It allows explicit specification of the filtering and wrap modes (GrSamplerParams) and accepts
19  * a matrix that is used to compute texture coordinates from local coordinates.
20  */
21 class GrSimpleTextureEffect : public GrSingleTextureEffect {
22 public:
23     /* unfiltered, clamp mode */
Make(GrTexture * tex,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix)24     static sk_sp<GrFragmentProcessor> Make(GrTexture* tex,
25                                            sk_sp<GrColorSpaceXform> colorSpaceXform,
26                                            const SkMatrix& matrix) {
27         return sk_sp<GrFragmentProcessor>(
28             new GrSimpleTextureEffect(tex, std::move(colorSpaceXform), matrix,
29                                       GrSamplerParams::kNone_FilterMode));
30     }
31 
32     /* clamp mode */
Make(GrTexture * tex,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,GrSamplerParams::FilterMode filterMode)33     static sk_sp<GrFragmentProcessor> Make(GrTexture* tex,
34                                            sk_sp<GrColorSpaceXform> colorSpaceXform,
35                                            const SkMatrix& matrix,
36                                            GrSamplerParams::FilterMode filterMode) {
37         return sk_sp<GrFragmentProcessor>(
38             new GrSimpleTextureEffect(tex, std::move(colorSpaceXform), matrix, filterMode));
39     }
40 
Make(GrTexture * tex,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,const GrSamplerParams & p)41     static sk_sp<GrFragmentProcessor> Make(GrTexture* tex,
42                                            sk_sp<GrColorSpaceXform> colorSpaceXform,
43                                            const SkMatrix& matrix,
44                                            const GrSamplerParams& p) {
45         return sk_sp<GrFragmentProcessor>(new GrSimpleTextureEffect(tex, std::move(colorSpaceXform),
46                                                                     matrix, p));
47     }
48 
49     /* unfiltered, clamp mode */
Make(GrResourceProvider * resourceProvider,sk_sp<GrTextureProxy> proxy,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix)50     static sk_sp<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider,
51                                            sk_sp<GrTextureProxy> proxy,
52                                            sk_sp<GrColorSpaceXform> colorSpaceXform,
53                                            const SkMatrix& matrix) {
54         return sk_sp<GrFragmentProcessor>(
55             new GrSimpleTextureEffect(resourceProvider, std::move(proxy),
56                                       std::move(colorSpaceXform), matrix,
57                                       GrSamplerParams::kNone_FilterMode));
58     }
59 
60     /* clamp mode */
Make(GrResourceProvider * resourceProvider,sk_sp<GrTextureProxy> proxy,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,GrSamplerParams::FilterMode filterMode)61     static sk_sp<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider,
62                                            sk_sp<GrTextureProxy> proxy,
63                                            sk_sp<GrColorSpaceXform> colorSpaceXform,
64                                            const SkMatrix& matrix,
65                                            GrSamplerParams::FilterMode filterMode) {
66         return sk_sp<GrFragmentProcessor>(
67             new GrSimpleTextureEffect(resourceProvider, std::move(proxy),
68                                       std::move(colorSpaceXform),
69                                       matrix, filterMode));
70     }
71 
Make(GrResourceProvider * resourceProvider,sk_sp<GrTextureProxy> proxy,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,const GrSamplerParams & p)72     static sk_sp<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider,
73                                            sk_sp<GrTextureProxy> proxy,
74                                            sk_sp<GrColorSpaceXform> colorSpaceXform,
75                                            const SkMatrix& matrix,
76                                            const GrSamplerParams& p) {
77         return sk_sp<GrFragmentProcessor>(new GrSimpleTextureEffect(resourceProvider,
78                                                                     std::move(proxy),
79                                                                     std::move(colorSpaceXform),
80                                                                     matrix, p));
81     }
82 
~GrSimpleTextureEffect()83     ~GrSimpleTextureEffect() override {}
84 
name()85     const char* name() const override { return "SimpleTexture"; }
86 
87 private:
GrSimpleTextureEffect(GrTexture * texture,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,GrSamplerParams::FilterMode filterMode)88     GrSimpleTextureEffect(GrTexture* texture,
89                           sk_sp<GrColorSpaceXform> colorSpaceXform,
90                           const SkMatrix& matrix,
91                           GrSamplerParams::FilterMode filterMode)
92             : INHERITED(texture, std::move(colorSpaceXform), matrix, filterMode,
93                         ModulationFlags(texture->config())) {
94         this->initClassID<GrSimpleTextureEffect>();
95     }
96 
GrSimpleTextureEffect(GrTexture * texture,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,const GrSamplerParams & params)97     GrSimpleTextureEffect(GrTexture* texture,
98                           sk_sp<GrColorSpaceXform>colorSpaceXform,
99                           const SkMatrix& matrix,
100                           const GrSamplerParams& params)
101             : INHERITED(texture, std::move(colorSpaceXform), matrix, params,
102                         ModulationFlags(texture->config())) {
103         this->initClassID<GrSimpleTextureEffect>();
104     }
105 
106     GrSimpleTextureEffect(GrResourceProvider*, sk_sp<GrTextureProxy>,
107                           sk_sp<GrColorSpaceXform>, const SkMatrix& matrix,
108                           GrSamplerParams::FilterMode);
109 
110     GrSimpleTextureEffect(GrResourceProvider*, sk_sp<GrTextureProxy>,
111                           sk_sp<GrColorSpaceXform>, const SkMatrix& matrix,
112                           const GrSamplerParams&);
113 
114     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
115 
116     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
117 
onIsEqual(const GrFragmentProcessor & other)118     bool onIsEqual(const GrFragmentProcessor& other) const override { return true; }
119 
120     GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
121 
122     typedef GrSingleTextureEffect INHERITED;
123 };
124 
125 #endif
126