Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrFragmentProcessor.h
1 /*
2  * Copyright 2014 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 GrFragmentProcessor_DEFINED
9 #define GrFragmentProcessor_DEFINED
10
11 #include "GrProcessor.h"
12
13 class GrCoordTransform;
14
15 /** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
16     produce an output color. They may reference textures and uniforms. They may use
17     GrCoordTransforms to receive a transformation of the local coordinates that map from local space
18     to the fragment being processed.
19  */
20 class GrFragmentProcessor : public GrProcessor {
21 public:
22     GrFragmentProcessor()
23         : INHERITED()
24         , fWillReadDstColor(false)
25         , fWillUseInputColor(true) {}
26
27     virtual const GrBackendFragmentProcessorFactory& getFactory() const = 0;
28
29     int numTransforms() const { return fCoordTransforms.count(); }
30
31     /** Returns the coordinate transformation at index. index must be valid according to
32         numTransforms(). */
33     const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
34
35     /** Will this prceossor read the destination pixel value? */
36     bool willReadDstColor() const { return fWillReadDstColor; }
37
38     /** Will this prceossor read the source color value? */
39     bool willUseInputColor() const { return fWillUseInputColor; }
40
41     /** Returns true if this and other prceossor conservatively draw identically. It can only return
42         true when the two prceossor are of the same subclass (i.e. they return the same object from
43         from getFactory()).
44
45         A return value of true from isEqual() should not be used to test whether the prceossor would
46         generate the same shader code. To test for identical code generation use the prceossor' keys
47         computed by the GrBackendProcessorFactory. */
48     bool isEqual(const GrFragmentProcessor& that) const {
49         if (&this->getFactory() != &that.getFactory() ||
50             !this->hasSameTransforms(that) ||
51             !this->hasSameTextureAccesses(that)) {
52             return false;
53         }
54         return this->onIsEqual(that);
55     }
56
57 protected:
58     /**
59      * Fragment Processor subclasses call this from their constructor to register coordinate
60      * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
61      * in their FS code. The matrix expresses a transformation from local space. For a given
62      * fragment the matrix will be applied to the local coordinate that maps to the fragment.
63      *
64      * When the transformation has perspective, the transformed coordinates will have
65      * 3 components. Otherwise they'll have 2. 
66      *
67      * This must only be called from the constructor because GrProcessors are immutable. The
68      * processor subclass manages the lifetime of the transformations (this function only stores a
69      * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass. 
70      *
71      * A processor subclass that has multiple methods of construction should always add its coord
72      * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
73      * compares transforms and will assume they line up across the two processor instances.
74      */
75     void addCoordTransform(const GrCoordTransform*);
76
77     /**
78      * If the prceossor subclass will read the destination pixel value then it must call this
79      * function from its constructor. Otherwise, when its generated backend-specific prceossor class
80      * attempts to generate code that reads the destination pixel it will fail.
81      */
82     void setWillReadDstColor() { fWillReadDstColor = true; }
83
84     /**
85      * If the prceossor will generate a result that does not depend on the input color value then it
86      * must call this function from its constructor. Otherwise, when its generated backend-specific
87      * code might fail during variable binding due to unused variables.
88      */
89     void setWillNotUseInputColor() { fWillUseInputColor = false; }
90
91 private:
92     /**
93      * Subclass implements this to support isEqual(). It will only be called if it is known that
94      * the two processors are of the same subclass (i.e. they return the same object from
95      * getFactory()). The processor subclass should not compare its coord transforms as that will
96      * be performed automatically in the non-virtual isEqual().
97      */
98     virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
99
100     bool hasSameTransforms(const GrFragmentProcessor&) const;
101
102     SkSTArray<4, const GrCoordTransform*, true>  fCoordTransforms;
103     bool                                         fWillReadDstColor;
104     bool                                         fWillUseInputColor;
105
106     typedef GrProcessor INHERITED;
107 };
108
109 #endif