Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrEffect.h
1 /*
2  * Copyright 2012 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 GrEffect_DEFINED
9 #define GrEffect_DEFINED
10
11 #include "GrColor.h"
12 #include "GrEffectUnitTest.h"
13 #include "GrTexture.h"
14 #include "GrTextureAccess.h"
15 #include "GrTypesPriv.h"
16
17 class GrBackendEffectFactory;
18 class GrContext;
19 class GrCoordTransform;
20 class GrEffect;
21 class GrVertexEffect;
22 class SkString;
23
24 /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
25     Ganesh shading pipeline.
26     Subclasses must have a function that produces a human-readable name:
27         static const char* Name();
28     GrEffect objects *must* be immutable: after being constructed, their fields may not change.
29
30     Dynamically allocated GrEffects are managed by a per-thread memory pool. The ref count of an
31     effect must reach 0 before the thread terminates and the pool is destroyed. To create a static
32     effect use the macro GR_CREATE_STATIC_EFFECT declared below.
33   */
34 class GrEffect : public SkRefCnt {
35 public:
36     SK_DECLARE_INST_COUNT(GrEffect)
37
38     virtual ~GrEffect();
39
40     /**
41      * This function is used to perform optimizations. When called the color and validFlags params
42      * indicate whether the input components to this effect in the FS will have known values.
43      * validFlags is a bitfield of GrColorComponentFlags. The function updates both params to
44      * indicate known values of its output. A component of the color param only has meaning if the
45      * corresponding bit in validFlags is set.
46      */
47     virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
48
49     /** Will this effect read the source color value? */
50     bool willUseInputColor() const { return fWillUseInputColor; }
51
52     /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
53         identification. The factory should be an instance of templated class,
54         GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
55         a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
56         by the factory.
57
58         Example:
59         class MyCustomEffect : public GrEffect {
60         ...
61             virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
62                 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
63             }
64         ...
65         };
66      */
67     virtual const GrBackendEffectFactory& getFactory() const = 0;
68
69     /** Returns true if this and other effect conservatively draw identically. It can only return
70         true when the two effects are of the same subclass (i.e. they return the same object from
71         from getFactory()).
72
73         A return value of true from isEqual() should not be used to test whether the effects would
74         generate the same shader code. To test for identical code generation use the effects' keys
75         computed by the GrBackendEffectFactory.
76      */
77     bool isEqual(const GrEffect& other) const {
78         if (&this->getFactory() != &other.getFactory()) {
79             return false;
80         }
81         bool result = this->onIsEqual(other);
82 #ifdef SK_DEBUG
83         if (result) {
84             this->assertEquality(other);
85         }
86 #endif
87         return result;
88     }
89
90     /** Human-meaningful string to identify this effect; may be embedded
91         in generated shader code. */
92     const char* name() const;
93
94     int numTransforms() const { return fCoordTransforms.count(); }
95
96     /** Returns the coordinate transformation at index. index must be valid according to
97         numTransforms(). */
98     const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
99
100     int numTextures() const { return fTextureAccesses.count(); }
101
102     /** Returns the access pattern for the texture at index. index must be valid according to
103         numTextures(). */
104     const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
105
106     /** Shortcut for textureAccess(index).texture(); */
107     GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
108
109     /** Will this effect read the destination pixel value? */
110     bool willReadDstColor() const { return fWillReadDstColor; }
111
112     /** Will this effect read the fragment position? */
113     bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
114
115     /** Will this effect emit custom vertex shader code?
116         (To set this value the effect must inherit from GrVertexEffect.) */
117     bool hasVertexCode() const { return fHasVertexCode; }
118
119     int numVertexAttribs() const {
120         SkASSERT(0 == fVertexAttribTypes.count() || fHasVertexCode);
121         return fVertexAttribTypes.count();
122     }
123
124     GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; }
125
126     static const int kMaxVertexAttribs = 2;
127
128     /** Useful for effects that want to insert a texture matrix that is implied by the texture
129         dimensions */
130     static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
131         SkASSERT(NULL != texture);
132         SkMatrix mat;
133         mat.setIDiv(texture->width(), texture->height());
134         return mat;
135     }
136
137     void* operator new(size_t size);
138     void operator delete(void* target);
139
140     void* operator new(size_t size, void* placement) {
141         return ::operator new(size, placement);
142     }
143     void operator delete(void* target, void* placement) {
144         ::operator delete(target, placement);
145     }
146
147 protected:
148     /**
149      * Subclasses call this from their constructor to register coordinate transformations. The
150      * effect subclass manages the lifetime of the transformations (this function only stores a
151      * pointer). The GrCoordTransform is typically a member field of the GrEffect subclass. When the
152      * matrix has perspective, the transformed coordinates will have 3 components. Otherwise they'll
153      * have 2. This must only be called from the constructor because GrEffects are immutable.
154      */
155     void addCoordTransform(const GrCoordTransform* coordTransform);
156
157     /**
158      * Subclasses call this from their constructor to register GrTextureAccesses. The effect
159      * subclass manages the lifetime of the accesses (this function only stores a pointer). The
160      * GrTextureAccess is typically a member field of the GrEffect subclass. This must only be
161      * called from the constructor because GrEffects are immutable.
162      */
163     void addTextureAccess(const GrTextureAccess* textureAccess);
164
165     GrEffect()
166         : fWillReadDstColor(false)
167         , fWillReadFragmentPosition(false)
168         , fWillUseInputColor(true)
169         , fHasVertexCode(false) {}
170
171     /**
172       * Helper for down-casting to a GrEffect subclass
173       */
174     template <typename T> static const T& CastEffect(const GrEffect& effect) {
175         return *static_cast<const T*>(&effect);
176     }
177
178     /**
179      * If the effect subclass will read the destination pixel value then it must call this function
180      * from its constructor. Otherwise, when its generated backend-specific effect class attempts
181      * to generate code that reads the destination pixel it will fail.
182      */
183     void setWillReadDstColor() { fWillReadDstColor = true; }
184
185     /**
186      * If the effect will generate a backend-specific effect that will read the fragment position
187      * in the FS then it must call this method from its constructor. Otherwise, the request to
188      * access the fragment position will be denied.
189      */
190     void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
191
192     /**
193      * If the effect will generate a result that does not depend on the input color value then it must
194      * call this function from its constructor. Otherwise, when its generated backend-specific code
195      * might fail during variable binding due to unused variables.
196      */
197     void setWillNotUseInputColor() { fWillUseInputColor = false; }
198
199 private:
200     SkDEBUGCODE(void assertEquality(const GrEffect& other) const;)
201
202     /** Subclass implements this to support isEqual(). It will only be called if it is known that
203         the two effects are of the same subclass (i.e. they return the same object from
204         getFactory()).*/
205     virtual bool onIsEqual(const GrEffect& other) const = 0;
206
207     friend class GrVertexEffect; // to set fHasVertexCode and build fVertexAttribTypes.
208
209     SkSTArray<4, const GrCoordTransform*, true>  fCoordTransforms;
210     SkSTArray<4, const GrTextureAccess*, true>   fTextureAccesses;
211     SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes;
212     bool                                         fWillReadDstColor;
213     bool                                         fWillReadFragmentPosition;
214     bool                                         fWillUseInputColor;
215     bool                                         fHasVertexCode;
216
217     typedef SkRefCnt INHERITED;
218 };
219
220 /**
221  * This creates an effect outside of the effect memory pool. The effect's destructor will be called
222  * at global destruction time. NAME will be the name of the created GrEffect.
223  */
224 #define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS)                                         \
225 static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage;                                \
226 static GrEffect* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS);         \
227 static SkAutoTDestroy<GrEffect> NAME##_ad(NAME);
228
229
230 #endif