Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrPaint.h
1
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10 #ifndef GrPaint_DEFINED
11 #define GrPaint_DEFINED
12
13 #include "GrColor.h"
14 #include "GrProcessorStage.h"
15
16 #include "SkXfermode.h"
17
18 /**
19  * The paint describes how color and coverage are computed at each pixel by GrContext draw
20  * functions and the how color is blended with the destination pixel.
21  *
22  * The paint allows installation of custom color and coverage stages. New types of stages are
23  * created by subclassing GrProcessor.
24  *
25  * The primitive color computation starts with the color specified by setColor(). This color is the
26  * input to the first color stage. Each color stage feeds its output to the next color stage. The
27  * final color stage's output color is input to the color filter specified by
28  * setXfermodeColorFilter which produces the final source color, S.
29  *
30  * Fractional pixel coverage follows a similar flow. The coverage is initially the value specified
31  * by setCoverage(). This is input to the first coverage stage. Coverage stages are chained
32  * together in the same manner as color stages. The output of the last stage is modulated by any
33  * fractional coverage produced by anti-aliasing. This last step produces the final coverage, C.
34  *
35  * setBlendFunc() specifies blending coefficients for S (described above) and D, the initial value
36  * of the destination pixel, labeled Bs and Bd respectively. The final value of the destination
37  * pixel is then D' = (1-C)*D + C*(Bd*D + Bs*S).
38  *
39  * Note that the coverage is applied after the blend. This is why they are computed as distinct
40  * values.
41  *
42  * TODO: Encapsulate setXfermodeColorFilter in a GrProcessor and remove from GrPaint.
43  */
44 class GrPaint {
45 public:
46     GrPaint() { this->reset(); }
47
48     GrPaint(const GrPaint& paint) { *this = paint; }
49
50     ~GrPaint() {}
51
52     /**
53      * Sets the blending coefficients to use to blend the final primitive color with the
54      * destination color. Defaults to kOne for src and kZero for dst (i.e. src mode).
55      */
56     void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
57         fSrcBlendCoeff = srcCoeff;
58         fDstBlendCoeff = dstCoeff;
59     }
60     GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlendCoeff; }
61     GrBlendCoeff getDstBlendCoeff() const { return fDstBlendCoeff; }
62
63     /**
64      * The initial color of the drawn primitive. Defaults to solid white.
65      */
66     void setColor(GrColor color) { fColor = color; }
67     GrColor getColor() const { return fColor; }
68
69     /**
70      * Applies fractional coverage to the entire drawn primitive. Defaults to 0xff.
71      */
72     void setCoverage(uint8_t coverage) { fCoverage = coverage; }
73     uint8_t getCoverage() const { return fCoverage; }
74
75     /**
76      * Should primitives be anti-aliased or not. Defaults to false.
77      */
78     void setAntiAlias(bool aa) { fAntiAlias = aa; }
79     bool isAntiAlias() const { return fAntiAlias; }
80
81     /**
82      * Should dithering be applied. Defaults to false.
83      */
84     void setDither(bool dither) { fDither = dither; }
85     bool isDither() const { return fDither; }
86
87     /**
88      * Appends an additional color processor to the color computation.
89      */
90     const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* fp) {
91         SkASSERT(fp);
92         SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (fp));
93         return fp;
94     }
95
96     /**
97      * Appends an additional coverage processor to the coverage computation.
98      */
99     const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* fp) {
100         SkASSERT(fp);
101         SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (fp));
102         return fp;
103     }
104
105     /**
106      * Helpers for adding color or coverage effects that sample a texture. The matrix is applied
107      * to the src space position to compute texture coordinates.
108      */
109     void addColorTextureProcessor(GrTexture*, const SkMatrix&);
110     void addCoverageTextureProcessor(GrTexture*, const SkMatrix&);
111     void addColorTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&);
112     void addCoverageTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&);
113
114     int numColorStages() const { return fColorStages.count(); }
115     int numCoverageStages() const { return fCoverageStages.count(); }
116     int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
117
118     const GrFragmentStage& getColorStage(int s) const { return fColorStages[s]; }
119     const GrFragmentStage& getCoverageStage(int s) const { return fCoverageStages[s]; }
120
121     GrPaint& operator=(const GrPaint& paint) {
122         fSrcBlendCoeff = paint.fSrcBlendCoeff;
123         fDstBlendCoeff = paint.fDstBlendCoeff;
124         fAntiAlias = paint.fAntiAlias;
125         fDither = paint.fDither;
126
127         fColor = paint.fColor;
128         fCoverage = paint.fCoverage;
129
130         fColorStages = paint.fColorStages;
131         fCoverageStages = paint.fCoverageStages;
132
133         return *this;
134     }
135
136     /**
137      * Resets the paint to the defaults.
138      */
139     void reset() {
140         this->resetBlend();
141         this->resetOptions();
142         this->resetColor();
143         this->resetCoverage();
144         this->resetStages();
145     }
146
147     /**
148      * Determines whether the drawing with this paint is opaque with respect to both color blending
149      * and fractional coverage. It does not consider whether AA has been enabled on the paint or
150      * not. Depending upon whether multisampling or coverage-based AA is in use, AA may make the
151      * result only apply to the interior of primitives.
152      *
153      */
154     bool isOpaque() const;
155
156     /**
157      * Returns true if isOpaque would return true and the paint represents a solid constant color
158      * draw. If the result is true, constantColor will be updated to contain the constant color.
159      */
160     bool isOpaqueAndConstantColor(GrColor* constantColor) const;
161
162 private:
163
164     /**
165      * Helper for isOpaque and isOpaqueAndConstantColor.
166      */
167     bool getOpaqueAndKnownColor(GrColor* solidColor, uint32_t* solidColorKnownComponents) const;
168
169     /**
170      * Called when the source coord system from which geometry is rendered changes. It ensures that
171      * the local coordinates seen by effects remains unchanged. oldToNew gives the transformation
172      * from the previous coord system to the new coord system.
173      */
174     void localCoordChange(const SkMatrix& oldToNew) {
175         for (int i = 0; i < fColorStages.count(); ++i) {
176             fColorStages[i].localCoordChange(oldToNew);
177         }
178         for (int i = 0; i < fCoverageStages.count(); ++i) {
179             fCoverageStages[i].localCoordChange(oldToNew);
180         }
181     }
182
183     bool localCoordChangeInverse(const SkMatrix& newToOld) {
184         SkMatrix oldToNew;
185         bool computed = false;
186         for (int i = 0; i < fColorStages.count(); ++i) {
187             if (!computed && !newToOld.invert(&oldToNew)) {
188                 return false;
189             } else {
190                 computed = true;
191             }
192             fColorStages[i].localCoordChange(oldToNew);
193         }
194         for (int i = 0; i < fCoverageStages.count(); ++i) {
195             if (!computed && !newToOld.invert(&oldToNew)) {
196                 return false;
197             } else {
198                 computed = true;
199             }
200             fCoverageStages[i].localCoordChange(oldToNew);
201         }
202         return true;
203     }
204
205     friend class GrContext; // To access above two functions
206     friend class GrStencilAndCoverTextContext;  // To access above two functions
207
208     SkSTArray<4, GrFragmentStage> fColorStages;
209     SkSTArray<2, GrFragmentStage> fCoverageStages;
210
211     GrBlendCoeff                fSrcBlendCoeff;
212     GrBlendCoeff                fDstBlendCoeff;
213     bool                        fAntiAlias;
214     bool                        fDither;
215
216     GrColor                     fColor;
217     uint8_t                     fCoverage;
218
219     void resetBlend() {
220         fSrcBlendCoeff = kOne_GrBlendCoeff;
221         fDstBlendCoeff = kZero_GrBlendCoeff;
222     }
223
224     void resetOptions() {
225         fAntiAlias = false;
226         fDither = false;
227     }
228
229     void resetColor() {
230         fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
231     }
232
233     void resetCoverage() {
234         fCoverage = 0xff;
235     }
236
237     void resetStages() {
238         fColorStages.reset();
239         fCoverageStages.reset();
240     }
241 };
242
243 #endif