| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 10 | #ifndef GrPaint_DEFINED |
| 11 | #define GrPaint_DEFINED |
| 12 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 13 | #include "GrColor.h" |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 14 | #include "GrEffectStage.h" |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 15 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 16 | #include "SkXfermode.h" |
| 17 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 18 | /** |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 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 |
| bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 23 | * created by subclassing GrEffect. |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 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 |
| bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 28 | * setXfermodeColorFilter which produces the final source color, S. |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 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 | * |
| bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 42 | * TODO: Encapsulate setXfermodeColorFilter in a GrEffect and remove from GrPaint. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 43 | */ |
| 44 | class GrPaint { |
| 45 | public: |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 46 | GrPaint() { this->reset(); } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 47 | |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 48 | GrPaint(const GrPaint& paint) { *this = paint; } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 49 | |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 50 | ~GrPaint() {} |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 51 | |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 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 | * Enables a SkXfermode::Mode-based color filter applied to the primitive color. The constant |
| 89 | * color passed to this function is considered the "src" color and the primitive's color is |
| 90 | * considered the "dst" color. Defaults to kDst_Mode which equates to simply passing through |
| 91 | * the primitive color unmodified. |
| 92 | */ |
| 93 | void setXfermodeColorFilter(SkXfermode::Mode mode, GrColor color) { |
| 94 | fColorFilterColor = color; |
| 95 | fColorFilterXfermode = mode; |
| 96 | } |
| 97 | SkXfermode::Mode getColorFilterMode() const { return fColorFilterXfermode; } |
| 98 | GrColor getColorFilterColor() const { return fColorFilterColor; } |
| 99 | |
| 100 | /** |
| bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 101 | * Disables the SkXfermode::Mode color filter. |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 102 | */ |
| 103 | void resetColorFilter() { |
| 104 | fColorFilterXfermode = SkXfermode::kDst_Mode; |
| 105 | fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 109 | * Appends an additional color effect to the color computation. |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 110 | */ |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 111 | const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 112 | SkASSERT(NULL != effect); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 113 | SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1)); |
| 114 | return effect; |
| tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 117 | /** |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 118 | * Appends an additional coverage effect to the coverage computation. |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 119 | */ |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 120 | const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 121 | SkASSERT(NULL != effect); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 122 | SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1)); |
| 123 | return effect; |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 126 | /** |
| 127 | * Helpers for adding color or coverage effects that sample a texture. The matrix is applied |
| 128 | * to the src space position to compute texture coordinates. |
| 129 | */ |
| 130 | void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix); |
| 131 | void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 132 | |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 133 | void addColorTextureEffect(GrTexture* texture, |
| 134 | const SkMatrix& matrix, |
| 135 | const GrTextureParams& params); |
| 136 | void addCoverageTextureEffect(GrTexture* texture, |
| 137 | const SkMatrix& matrix, |
| 138 | const GrTextureParams& params); |
| tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 139 | |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 140 | int numColorStages() const { return fColorStages.count(); } |
| 141 | int numCoverageStages() const { return fCoverageStages.count(); } |
| 142 | int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); } |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 143 | |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 144 | const GrEffectStage& getColorStage(int s) const { return fColorStages[s]; } |
| 145 | const GrEffectStage& getCoverageStage(int s) const { return fCoverageStages[s]; } |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 146 | |
| bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 147 | GrPaint& operator=(const GrPaint& paint) { |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 148 | fSrcBlendCoeff = paint.fSrcBlendCoeff; |
| 149 | fDstBlendCoeff = paint.fDstBlendCoeff; |
| 150 | fAntiAlias = paint.fAntiAlias; |
| 151 | fDither = paint.fDither; |
| 152 | |
| 153 | fColor = paint.fColor; |
| bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 154 | fCoverage = paint.fCoverage; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 155 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 156 | fColorFilterColor = paint.fColorFilterColor; |
| 157 | fColorFilterXfermode = paint.fColorFilterXfermode; |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 158 | |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 159 | fColorStages = paint.fColorStages; |
| 160 | fCoverageStages = paint.fCoverageStages; |
| 161 | |
| bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 162 | return *this; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 165 | /** |
| 166 | * Resets the paint to the defaults. |
| 167 | */ |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 168 | void reset() { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 169 | this->resetBlend(); |
| 170 | this->resetOptions(); |
| 171 | this->resetColor(); |
| bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 172 | this->resetCoverage(); |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 173 | this->resetStages(); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 174 | this->resetColorFilter(); |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 177 | /** |
| 178 | * Determines whether the drawing with this paint is opaque with respect to both color blending |
| 179 | * and fractional coverage. It does not consider whether AA has been enabled on the paint or |
| 180 | * not. Depending upon whether multisampling or coverage-based AA is in use, AA may make the |
| 181 | * result only apply to the interior of primitives. |
| 182 | * |
| 183 | */ |
| 184 | bool isOpaque() const; |
| 185 | |
| 186 | /** |
| 187 | * Returns true if isOpaque would return true and the paint represents a solid constant color |
| 188 | * draw. If the result is true, constantColor will be updated to contain the constant color. |
| 189 | */ |
| 190 | bool isOpaqueAndConstantColor(GrColor* constantColor) const; |
| 191 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 192 | private: |
| commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Helper for isOpaque and isOpaqueAndConstantColor. |
| 196 | */ |
| 197 | bool getOpaqueAndKnownColor(GrColor* solidColor, uint32_t* solidColorKnownComponents) const; |
| 198 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 199 | /** |
| 200 | * Called when the source coord system from which geometry is rendered changes. It ensures that |
| 201 | * the local coordinates seen by effects remains unchanged. oldToNew gives the transformation |
| 202 | * from the previous coord system to the new coord system. |
| 203 | */ |
| 204 | void localCoordChange(const SkMatrix& oldToNew) { |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 205 | for (int i = 0; i < fColorStages.count(); ++i) { |
| 206 | fColorStages[i].localCoordChange(oldToNew); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 207 | } |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 208 | for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 209 | fCoverageStages[i].localCoordChange(oldToNew); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | bool localCoordChangeInverse(const SkMatrix& newToOld) { |
| 214 | SkMatrix oldToNew; |
| 215 | bool computed = false; |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 216 | for (int i = 0; i < fColorStages.count(); ++i) { |
| 217 | if (!computed && !newToOld.invert(&oldToNew)) { |
| 218 | return false; |
| 219 | } else { |
| 220 | computed = true; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 221 | } |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 222 | fColorStages[i].localCoordChange(oldToNew); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 223 | } |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 224 | for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 225 | if (!computed && !newToOld.invert(&oldToNew)) { |
| 226 | return false; |
| 227 | } else { |
| 228 | computed = true; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 229 | } |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 230 | fCoverageStages[i].localCoordChange(oldToNew); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | friend class GrContext; // To access above two functions |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 236 | |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 237 | SkSTArray<4, GrEffectStage> fColorStages; |
| 238 | SkSTArray<2, GrEffectStage> fCoverageStages; |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 239 | |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 240 | GrBlendCoeff fSrcBlendCoeff; |
| 241 | GrBlendCoeff fDstBlendCoeff; |
| 242 | bool fAntiAlias; |
| 243 | bool fDither; |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 244 | |
| 245 | GrColor fColor; |
| 246 | uint8_t fCoverage; |
| 247 | |
| 248 | GrColor fColorFilterColor; |
| 249 | SkXfermode::Mode fColorFilterXfermode; |
| bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 250 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 251 | void resetBlend() { |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 252 | fSrcBlendCoeff = kOne_GrBlendCoeff; |
| 253 | fDstBlendCoeff = kZero_GrBlendCoeff; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void resetOptions() { |
| 257 | fAntiAlias = false; |
| 258 | fDither = false; |
| 259 | } |
| 260 | |
| 261 | void resetColor() { |
| 262 | fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| 263 | } |
| 264 | |
| bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 265 | void resetCoverage() { |
| 266 | fCoverage = 0xff; |
| 267 | } |
| 268 | |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 269 | void resetStages() { |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 270 | fColorStages.reset(); |
| 271 | fCoverageStages.reset(); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 272 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | #endif |