blob: 50a32a5743a5279ae294545b25755ac1144c8c91 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrPaint_DEFINED
11#define GrPaint_DEFINED
12
bsalomon@google.com27847de2011-02-22 20:59:41 +000013#include "GrColor.h"
bsalomon@google.com08283af2012-10-26 13:01:20 +000014#include "GrEffectStage.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000015
Scroggo97c88c22011-05-11 14:05:25 +000016#include "SkXfermode.h"
17
bsalomon@google.com27847de2011-02-22 20:59:41 +000018/**
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000019 * 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
bsalomon@google.coma469c282012-10-24 18:28:34 +000023 * created by subclassing GrEffect.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000024 *
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
bsalomon@google.com67e78c92012-10-17 13:36:14 +000028 * setXfermodeColorFilter which produces the final source color, S.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000029 *
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 *
bsalomon@google.coma469c282012-10-24 18:28:34 +000042 * TODO: Encapsulate setXfermodeColorFilter in a GrEffect and remove from GrPaint.
bsalomon@google.com27847de2011-02-22 20:59:41 +000043 */
44class GrPaint {
45public:
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000046 GrPaint() { this->reset(); }
bsalomon@google.com27847de2011-02-22 20:59:41 +000047
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000048 GrPaint(const GrPaint& paint) { *this = paint; }
bsalomon@google.com27847de2011-02-22 20:59:41 +000049
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000050 ~GrPaint() {}
Scroggo97c88c22011-05-11 14:05:25 +000051
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000052 /**
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 /**
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000088 * Appends an additional color effect to the color computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000089 */
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000090 const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000091 SkASSERT(NULL != effect);
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000092 if (!(*effect)->willUseInputColor()) {
93 fColorStages.reset();
94 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000095 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1));
96 return effect;
tomhudson@google.comf13f5882012-06-25 17:27:28 +000097 }
98
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000099 /**
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000100 * Appends an additional coverage effect to the coverage computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000101 */
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000102 const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000103 SkASSERT(NULL != effect);
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000104 if (!(*effect)->willUseInputColor()) {
105 fCoverageStages.reset();
106 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000107 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1));
108 return effect;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000109 }
110
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000111 /**
112 * Helpers for adding color or coverage effects that sample a texture. The matrix is applied
113 * to the src space position to compute texture coordinates.
114 */
115 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix);
116 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000117
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000118 void addColorTextureEffect(GrTexture* texture,
119 const SkMatrix& matrix,
120 const GrTextureParams& params);
121 void addCoverageTextureEffect(GrTexture* texture,
122 const SkMatrix& matrix,
123 const GrTextureParams& params);
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000124
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000125 int numColorStages() const { return fColorStages.count(); }
126 int numCoverageStages() const { return fCoverageStages.count(); }
127 int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000128
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000129 const GrEffectStage& getColorStage(int s) const { return fColorStages[s]; }
130 const GrEffectStage& getCoverageStage(int s) const { return fCoverageStages[s]; }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000131
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000132 GrPaint& operator=(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000133 fSrcBlendCoeff = paint.fSrcBlendCoeff;
134 fDstBlendCoeff = paint.fDstBlendCoeff;
135 fAntiAlias = paint.fAntiAlias;
136 fDither = paint.fDither;
137
138 fColor = paint.fColor;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000139 fCoverage = paint.fCoverage;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000140
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000141 fColorStages = paint.fColorStages;
142 fCoverageStages = paint.fCoverageStages;
143
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000144 return *this;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000145 }
146
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000147 /**
148 * Resets the paint to the defaults.
149 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000150 void reset() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000151 this->resetBlend();
152 this->resetOptions();
153 this->resetColor();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000154 this->resetCoverage();
bsalomon@google.com08283af2012-10-26 13:01:20 +0000155 this->resetStages();
Scroggo97c88c22011-05-11 14:05:25 +0000156 }
157
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000158 /**
159 * Determines whether the drawing with this paint is opaque with respect to both color blending
160 * and fractional coverage. It does not consider whether AA has been enabled on the paint or
161 * not. Depending upon whether multisampling or coverage-based AA is in use, AA may make the
162 * result only apply to the interior of primitives.
163 *
164 */
165 bool isOpaque() const;
166
167 /**
168 * Returns true if isOpaque would return true and the paint represents a solid constant color
169 * draw. If the result is true, constantColor will be updated to contain the constant color.
170 */
171 bool isOpaqueAndConstantColor(GrColor* constantColor) const;
172
bsalomon@google.com27847de2011-02-22 20:59:41 +0000173private:
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000174
175 /**
176 * Helper for isOpaque and isOpaqueAndConstantColor.
177 */
178 bool getOpaqueAndKnownColor(GrColor* solidColor, uint32_t* solidColorKnownComponents) const;
179
bsalomon@google.comc7818882013-03-20 19:19:53 +0000180 /**
181 * Called when the source coord system from which geometry is rendered changes. It ensures that
182 * the local coordinates seen by effects remains unchanged. oldToNew gives the transformation
183 * from the previous coord system to the new coord system.
184 */
185 void localCoordChange(const SkMatrix& oldToNew) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000186 for (int i = 0; i < fColorStages.count(); ++i) {
187 fColorStages[i].localCoordChange(oldToNew);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000188 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000189 for (int i = 0; i < fCoverageStages.count(); ++i) {
190 fCoverageStages[i].localCoordChange(oldToNew);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000191 }
192 }
193
194 bool localCoordChangeInverse(const SkMatrix& newToOld) {
195 SkMatrix oldToNew;
196 bool computed = false;
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000197 for (int i = 0; i < fColorStages.count(); ++i) {
198 if (!computed && !newToOld.invert(&oldToNew)) {
199 return false;
200 } else {
201 computed = true;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000202 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000203 fColorStages[i].localCoordChange(oldToNew);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000204 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000205 for (int i = 0; i < fCoverageStages.count(); ++i) {
206 if (!computed && !newToOld.invert(&oldToNew)) {
207 return false;
208 } else {
209 computed = true;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000210 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000211 fCoverageStages[i].localCoordChange(oldToNew);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000212 }
213 return true;
214 }
215
216 friend class GrContext; // To access above two functions
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000217
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000218 SkSTArray<4, GrEffectStage> fColorStages;
219 SkSTArray<2, GrEffectStage> fCoverageStages;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000220
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000221 GrBlendCoeff fSrcBlendCoeff;
222 GrBlendCoeff fDstBlendCoeff;
223 bool fAntiAlias;
224 bool fDither;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000225
226 GrColor fColor;
227 uint8_t fCoverage;
228
bsalomon@google.com27847de2011-02-22 20:59:41 +0000229 void resetBlend() {
bsalomon@google.com47059542012-06-06 20:51:20 +0000230 fSrcBlendCoeff = kOne_GrBlendCoeff;
231 fDstBlendCoeff = kZero_GrBlendCoeff;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000232 }
233
234 void resetOptions() {
235 fAntiAlias = false;
236 fDither = false;
237 }
238
239 void resetColor() {
240 fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
241 }
242
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000243 void resetCoverage() {
244 fCoverage = 0xff;
245 }
246
bsalomon@google.com08283af2012-10-26 13:01:20 +0000247 void resetStages() {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000248 fColorStages.reset();
249 fCoverageStages.reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000250 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000251};
252
253#endif