| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef GrPaint_DEFINED | 10 #ifndef GrPaint_DEFINED |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 * of the destination pixel, labeled Bs and Bd respectively. The final value of
the destination | 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). | 37 * pixel is then D' = (1-C)*D + C*(Bd*D + Bs*S). |
| 38 * | 38 * |
| 39 * Note that the coverage is applied after the blend. This is why they are compu
ted as distinct | 39 * Note that the coverage is applied after the blend. This is why they are compu
ted as distinct |
| 40 * values. | 40 * values. |
| 41 * | 41 * |
| 42 * TODO: Encapsulate setXfermodeColorFilter in a GrEffect and remove from GrPain
t. | 42 * TODO: Encapsulate setXfermodeColorFilter in a GrEffect and remove from GrPain
t. |
| 43 */ | 43 */ |
| 44 class GrPaint { | 44 class GrPaint { |
| 45 public: | 45 public: |
| 46 enum { | |
| 47 kMaxColorStages = 3, | |
| 48 kMaxCoverageStages = 1, | |
| 49 }; | |
| 50 | |
| 51 GrPaint() { this->reset(); } | 46 GrPaint() { this->reset(); } |
| 52 | 47 |
| 53 GrPaint(const GrPaint& paint) { *this = paint; } | 48 GrPaint(const GrPaint& paint) { *this = paint; } |
| 54 | 49 |
| 55 ~GrPaint() {} | 50 ~GrPaint() {} |
| 56 | 51 |
| 57 /** | 52 /** |
| 58 * Sets the blending coefficients to use to blend the final primitive color
with the | 53 * Sets the blending coefficients to use to blend the final primitive color
with the |
| 59 * destination color. Defaults to kOne for src and kZero for dst (i.e. src m
ode). | 54 * destination color. Defaults to kOne for src and kZero for dst (i.e. src m
ode). |
| 60 */ | 55 */ |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 | 99 |
| 105 /** | 100 /** |
| 106 * Disables the SkXfermode::Mode color filter. | 101 * Disables the SkXfermode::Mode color filter. |
| 107 */ | 102 */ |
| 108 void resetColorFilter() { | 103 void resetColorFilter() { |
| 109 fColorFilterXfermode = SkXfermode::kDst_Mode; | 104 fColorFilterXfermode = SkXfermode::kDst_Mode; |
| 110 fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); | 105 fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| 111 } | 106 } |
| 112 | 107 |
| 113 /** | 108 /** |
| 114 * Specifies a stage of the color pipeline. Usually the texture matrices of
color stages apply | 109 * Appends an additional color effect to the color computation. |
| 115 * to the primitive's positions. Some GrContext calls take explicit coords a
s an array or a | |
| 116 * rect. In this case these are the pre-matrix coords to colorStage(0). | |
| 117 */ | 110 */ |
| 118 GrEffectStage* colorStage(int i) { | 111 const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1,
int attr1 = -1) { |
| 119 GrAssert((unsigned)i < kMaxColorStages); | 112 GrAssert(NULL != effect); |
| 120 return fColorStages + i; | 113 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, att
r1)); |
| 121 } | 114 return effect; |
| 122 | |
| 123 const GrEffectStage& getColorStage(int i) const { | |
| 124 GrAssert((unsigned)i < kMaxColorStages); | |
| 125 return fColorStages[i]; | |
| 126 } | |
| 127 | |
| 128 bool isColorStageEnabled(int i) const { | |
| 129 GrAssert((unsigned)i < kMaxColorStages); | |
| 130 return (NULL != fColorStages[i].getEffect()); | |
| 131 } | 115 } |
| 132 | 116 |
| 133 /** | 117 /** |
| 134 * Specifies a stage of the coverage pipeline. Coverage stages' texture matr
ices are always | 118 * Appends an additional coverage effect to the coverage computation. |
| 135 * applied to the primitive's position, never to explicit texture coords. | |
| 136 */ | 119 */ |
| 137 GrEffectStage* coverageStage(int i) { | 120 const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 =
-1, int attr1 = -1) { |
| 138 GrAssert((unsigned)i < kMaxCoverageStages); | 121 GrAssert(NULL != effect); |
| 139 return fCoverageStages + i; | 122 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0,
attr1)); |
| 123 return effect; |
| 140 } | 124 } |
| 141 | 125 |
| 142 const GrEffectStage& getCoverageStage(int i) const { | 126 /** |
| 143 GrAssert((unsigned)i < kMaxCoverageStages); | 127 * Helpers for adding color or coverage effects that sample a texture. The m
atrix is applied |
| 144 return fCoverageStages[i]; | 128 * to the src space position to compute texture coordinates. |
| 145 } | 129 */ |
| 130 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix); |
| 131 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix); |
| 146 | 132 |
| 147 bool isCoverageStageEnabled(int i) const { | 133 void addColorTextureEffect(GrTexture* texture, |
| 148 GrAssert((unsigned)i < kMaxCoverageStages); | 134 const SkMatrix& matrix, |
| 149 return (NULL != fCoverageStages[i].getEffect()); | 135 const GrTextureParams& params); |
| 150 } | 136 void addCoverageTextureEffect(GrTexture* texture, |
| 137 const SkMatrix& matrix, |
| 138 const GrTextureParams& params); |
| 151 | 139 |
| 152 bool hasCoverageStage() const { | 140 int numColorStages() const { return fColorStages.count(); } |
| 153 for (int i = 0; i < kMaxCoverageStages; ++i) { | 141 int numCoverageStages() const { return fCoverageStages.count(); } |
| 154 if (this->isCoverageStageEnabled(i)) { | 142 int numTotalStages() const { return this->numColorStages() + this->numCovera
geStages(); } |
| 155 return true; | |
| 156 } | |
| 157 } | |
| 158 return false; | |
| 159 } | |
| 160 | 143 |
| 161 bool hasColorStage() const { | 144 const GrEffectStage& getColorStage(int s) const { return fColorStages[s]; } |
| 162 for (int i = 0; i < kMaxColorStages; ++i) { | 145 const GrEffectStage& getCoverageStage(int s) const { return fCoverageStages[
s]; } |
| 163 if (this->isColorStageEnabled(i)) { | |
| 164 return true; | |
| 165 } | |
| 166 } | |
| 167 return false; | |
| 168 } | |
| 169 | |
| 170 bool hasStage() const { return this->hasColorStage() || this->hasCoverageSta
ge(); } | |
| 171 | 146 |
| 172 GrPaint& operator=(const GrPaint& paint) { | 147 GrPaint& operator=(const GrPaint& paint) { |
| 173 fSrcBlendCoeff = paint.fSrcBlendCoeff; | 148 fSrcBlendCoeff = paint.fSrcBlendCoeff; |
| 174 fDstBlendCoeff = paint.fDstBlendCoeff; | 149 fDstBlendCoeff = paint.fDstBlendCoeff; |
| 175 fAntiAlias = paint.fAntiAlias; | 150 fAntiAlias = paint.fAntiAlias; |
| 176 fDither = paint.fDither; | 151 fDither = paint.fDither; |
| 177 | 152 |
| 178 fColor = paint.fColor; | 153 fColor = paint.fColor; |
| 179 fCoverage = paint.fCoverage; | 154 fCoverage = paint.fCoverage; |
| 180 | 155 |
| 181 fColorFilterColor = paint.fColorFilterColor; | 156 fColorFilterColor = paint.fColorFilterColor; |
| 182 fColorFilterXfermode = paint.fColorFilterXfermode; | 157 fColorFilterXfermode = paint.fColorFilterXfermode; |
| 183 | 158 |
| 184 for (int i = 0; i < kMaxColorStages; ++i) { | 159 fColorStages = paint.fColorStages; |
| 185 if (paint.isColorStageEnabled(i)) { | 160 fCoverageStages = paint.fCoverageStages; |
| 186 fColorStages[i] = paint.fColorStages[i]; | 161 |
| 187 } | |
| 188 } | |
| 189 for (int i = 0; i < kMaxCoverageStages; ++i) { | |
| 190 if (paint.isCoverageStageEnabled(i)) { | |
| 191 fCoverageStages[i] = paint.fCoverageStages[i]; | |
| 192 } | |
| 193 } | |
| 194 return *this; | 162 return *this; |
| 195 } | 163 } |
| 196 | 164 |
| 197 /** | 165 /** |
| 198 * Resets the paint to the defaults. | 166 * Resets the paint to the defaults. |
| 199 */ | 167 */ |
| 200 void reset() { | 168 void reset() { |
| 201 this->resetBlend(); | 169 this->resetBlend(); |
| 202 this->resetOptions(); | 170 this->resetOptions(); |
| 203 this->resetColor(); | 171 this->resetColor(); |
| 204 this->resetCoverage(); | 172 this->resetCoverage(); |
| 205 this->resetStages(); | 173 this->resetStages(); |
| 206 this->resetColorFilter(); | 174 this->resetColorFilter(); |
| 207 } | 175 } |
| 208 | 176 |
| 209 // internal use | |
| 210 // GrPaint's textures and masks map to the first N stages | |
| 211 // of GrDrawTarget in that order (textures followed by masks) | |
| 212 enum { | |
| 213 kFirstColorStage = 0, | |
| 214 kFirstCoverageStage = kMaxColorStages, | |
| 215 kTotalStages = kFirstColorStage + kMaxColorStages + kMaxCoverageStages, | |
| 216 }; | |
| 217 | |
| 218 private: | 177 private: |
| 219 /** | 178 /** |
| 220 * Called when the source coord system from which geometry is rendered chang
es. It ensures that | 179 * Called when the source coord system from which geometry is rendered chang
es. It ensures that |
| 221 * the local coordinates seen by effects remains unchanged. oldToNew gives t
he transformation | 180 * the local coordinates seen by effects remains unchanged. oldToNew gives t
he transformation |
| 222 * from the previous coord system to the new coord system. | 181 * from the previous coord system to the new coord system. |
| 223 */ | 182 */ |
| 224 void localCoordChange(const SkMatrix& oldToNew) { | 183 void localCoordChange(const SkMatrix& oldToNew) { |
| 225 for (int i = 0; i < kMaxColorStages; ++i) { | 184 for (int i = 0; i < fColorStages.count(); ++i) { |
| 226 if (this->isColorStageEnabled(i)) { | 185 fColorStages[i].localCoordChange(oldToNew); |
| 227 fColorStages[i].localCoordChange(oldToNew); | |
| 228 } | |
| 229 } | 186 } |
| 230 for (int i = 0; i < kMaxCoverageStages; ++i) { | 187 for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 231 if (this->isCoverageStageEnabled(i)) { | 188 fCoverageStages[i].localCoordChange(oldToNew); |
| 232 fCoverageStages[i].localCoordChange(oldToNew); | |
| 233 } | |
| 234 } | 189 } |
| 235 } | 190 } |
| 236 | 191 |
| 237 bool localCoordChangeInverse(const SkMatrix& newToOld) { | 192 bool localCoordChangeInverse(const SkMatrix& newToOld) { |
| 238 SkMatrix oldToNew; | 193 SkMatrix oldToNew; |
| 239 bool computed = false; | 194 bool computed = false; |
| 240 for (int i = 0; i < kMaxColorStages; ++i) { | 195 for (int i = 0; i < fColorStages.count(); ++i) { |
| 241 if (this->isColorStageEnabled(i)) { | 196 if (!computed && !newToOld.invert(&oldToNew)) { |
| 242 if (!computed && !newToOld.invert(&oldToNew)) { | 197 return false; |
| 243 return false; | 198 } else { |
| 244 } else { | 199 computed = true; |
| 245 computed = true; | |
| 246 } | |
| 247 fColorStages[i].localCoordChange(oldToNew); | |
| 248 } | 200 } |
| 201 fColorStages[i].localCoordChange(oldToNew); |
| 249 } | 202 } |
| 250 for (int i = 0; i < kMaxCoverageStages; ++i) { | 203 for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 251 if (this->isCoverageStageEnabled(i)) { | 204 if (!computed && !newToOld.invert(&oldToNew)) { |
| 252 if (!computed && !newToOld.invert(&oldToNew)) { | 205 return false; |
| 253 return false; | 206 } else { |
| 254 } else { | 207 computed = true; |
| 255 computed = true; | |
| 256 } | |
| 257 fCoverageStages[i].localCoordChange(oldToNew); | |
| 258 } | 208 } |
| 209 fCoverageStages[i].localCoordChange(oldToNew); |
| 259 } | 210 } |
| 260 return true; | 211 return true; |
| 261 } | 212 } |
| 262 | 213 |
| 263 friend class GrContext; // To access above two functions | 214 friend class GrContext; // To access above two functions |
| 264 | 215 |
| 265 GrEffectStage fColorStages[kMaxColorStages]; | 216 SkSTArray<4, GrEffectStage> fColorStages; |
| 266 GrEffectStage fCoverageStages[kMaxCoverageStages]; | 217 SkSTArray<2, GrEffectStage> fCoverageStages; |
| 267 | 218 |
| 268 GrBlendCoeff fSrcBlendCoeff; | 219 GrBlendCoeff fSrcBlendCoeff; |
| 269 GrBlendCoeff fDstBlendCoeff; | 220 GrBlendCoeff fDstBlendCoeff; |
| 270 bool fAntiAlias; | 221 bool fAntiAlias; |
| 271 bool fDither; | 222 bool fDither; |
| 272 | 223 |
| 273 GrColor fColor; | 224 GrColor fColor; |
| 274 uint8_t fCoverage; | 225 uint8_t fCoverage; |
| 275 | 226 |
| 276 GrColor fColorFilterColor; | 227 GrColor fColorFilterColor; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 288 | 239 |
| 289 void resetColor() { | 240 void resetColor() { |
| 290 fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); | 241 fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| 291 } | 242 } |
| 292 | 243 |
| 293 void resetCoverage() { | 244 void resetCoverage() { |
| 294 fCoverage = 0xff; | 245 fCoverage = 0xff; |
| 295 } | 246 } |
| 296 | 247 |
| 297 void resetStages() { | 248 void resetStages() { |
| 298 for (int i = 0; i < kMaxColorStages; ++i) { | 249 fColorStages.reset(); |
| 299 fColorStages[i].reset(); | 250 fCoverageStages.reset(); |
| 300 } | |
| 301 for (int i = 0; i < kMaxCoverageStages; ++i) { | |
| 302 fCoverageStages[i].reset(); | |
| 303 } | |
| 304 } | 251 } |
| 305 }; | 252 }; |
| 306 | 253 |
| 307 #endif | 254 #endif |
| OLD | NEW |