OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrDrawState.h" | 8 #include "GrDrawState.h" |
9 | 9 |
10 #include "GrBlend.h" | 10 #include "GrBlend.h" |
11 #include "GrOptDrawState.h" | 11 #include "GrOptDrawState.h" |
12 #include "GrPaint.h" | 12 #include "GrPaint.h" |
13 #include "GrProcOptInfo.h" | 13 #include "GrProcOptInfo.h" |
14 #include "GrXferProcessor.h" | 14 #include "GrXferProcessor.h" |
15 #include "effects/GrPorterDuffXferProcessor.h" | 15 #include "effects/GrPorterDuffXferProcessor.h" |
16 | 16 |
17 bool GrDrawState::isEqual(const GrDrawState& that) const { | 17 bool GrDrawState::isEqual(const GrDrawState& that, bool explicitLocalCoords) con
st { |
18 if (this->getRenderTarget() != that.getRenderTarget() || | 18 if (this->getRenderTarget() != that.getRenderTarget() || |
19 this->fColorStages.count() != that.fColorStages.count() || | 19 this->fColorStages.count() != that.fColorStages.count() || |
20 this->fCoverageStages.count() != that.fCoverageStages.count() || | 20 this->fCoverageStages.count() != that.fCoverageStages.count() || |
21 !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || | 21 !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
22 this->fFlagBits != that.fFlagBits || | 22 this->fFlagBits != that.fFlagBits || |
23 this->fStencilSettings != that.fStencilSettings || | 23 this->fStencilSettings != that.fStencilSettings || |
24 this->fDrawFace != that.fDrawFace) { | 24 this->fDrawFace != that.fDrawFace) { |
25 return false; | 25 return false; |
26 } | 26 } |
27 | 27 |
28 bool explicitLocalCoords = this->hasLocalCoordAttribute(); | |
29 if (this->hasGeometryProcessor()) { | |
30 if (!that.hasGeometryProcessor()) { | |
31 return false; | |
32 } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProce
ssor())) { | |
33 return false; | |
34 } | |
35 } else if (that.hasGeometryProcessor()) { | |
36 return false; | |
37 } | |
38 | |
39 if (!this->getXPFactory()->isEqual(*that.getXPFactory())) { | 28 if (!this->getXPFactory()->isEqual(*that.getXPFactory())) { |
40 return false; | 29 return false; |
41 } | 30 } |
42 | 31 |
43 for (int i = 0; i < this->numColorStages(); i++) { | 32 for (int i = 0; i < this->numColorStages(); i++) { |
44 if (!GrFragmentStage::AreCompatible(this->getColorStage(i), that.getColo
rStage(i), | 33 if (!GrFragmentStage::AreCompatible(this->getColorStage(i), that.getColo
rStage(i), |
45 explicitLocalCoords)) { | 34 explicitLocalCoords)) { |
46 return false; | 35 return false; |
47 } | 36 } |
48 } | 37 } |
(...skipping 21 matching lines...) Expand all Loading... |
70 } | 59 } |
71 } | 60 } |
72 } | 61 } |
73 | 62 |
74 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { | 63 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { |
75 fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get())); | 64 fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get())); |
76 fViewMatrix = that.fViewMatrix; | 65 fViewMatrix = that.fViewMatrix; |
77 fFlagBits = that.fFlagBits; | 66 fFlagBits = that.fFlagBits; |
78 fStencilSettings = that.fStencilSettings; | 67 fStencilSettings = that.fStencilSettings; |
79 fDrawFace = that.fDrawFace; | 68 fDrawFace = that.fDrawFace; |
80 fGeometryProcessor.reset(SkSafeRef(that.fGeometryProcessor.get())); | |
81 fXPFactory.reset(SkRef(that.getXPFactory())); | 69 fXPFactory.reset(SkRef(that.getXPFactory())); |
82 fColorStages = that.fColorStages; | 70 fColorStages = that.fColorStages; |
83 fCoverageStages = that.fCoverageStages; | 71 fCoverageStages = that.fCoverageStages; |
84 | 72 |
85 fHints = that.fHints; | 73 fHints = that.fHints; |
86 | 74 |
87 fColorProcInfoValid = that.fColorProcInfoValid; | 75 fColorProcInfoValid = that.fColorProcInfoValid; |
88 fCoverageProcInfoValid = that.fCoverageProcInfoValid; | 76 fCoverageProcInfoValid = that.fCoverageProcInfoValid; |
89 if (fColorProcInfoValid) { | 77 if (fColorProcInfoValid) { |
90 fColorProcInfo = that.fColorProcInfo; | 78 fColorProcInfo = that.fColorProcInfo; |
91 } | 79 } |
92 if (fCoverageProcInfoValid) { | 80 if (fCoverageProcInfoValid) { |
93 fCoverageProcInfo = that.fCoverageProcInfo; | 81 fCoverageProcInfo = that.fCoverageProcInfo; |
94 } | 82 } |
95 return *this; | 83 return *this; |
96 } | 84 } |
97 | 85 |
98 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { | 86 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { |
99 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); | 87 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
100 fRenderTarget.reset(NULL); | 88 fRenderTarget.reset(NULL); |
101 | 89 |
102 fGeometryProcessor.reset(NULL); | |
103 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode)); | 90 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode)); |
104 fColorStages.reset(); | 91 fColorStages.reset(); |
105 fCoverageStages.reset(); | 92 fCoverageStages.reset(); |
106 | 93 |
107 if (NULL == initialViewMatrix) { | 94 if (NULL == initialViewMatrix) { |
108 fViewMatrix.reset(); | 95 fViewMatrix.reset(); |
109 } else { | 96 } else { |
110 fViewMatrix = *initialViewMatrix; | 97 fViewMatrix = *initialViewMatrix; |
111 } | 98 } |
112 fFlagBits = 0x0; | 99 fFlagBits = 0x0; |
113 fStencilSettings.setDisabled(); | 100 fStencilSettings.setDisabled(); |
114 fDrawFace = kBoth_DrawFace; | 101 fDrawFace = kBoth_DrawFace; |
115 | 102 |
116 fHints = 0; | 103 fHints = 0; |
117 | 104 |
118 fColorProcInfoValid = false; | 105 fColorProcInfoValid = false; |
119 fCoverageProcInfoValid = false; | 106 fCoverageProcInfoValid = false; |
| 107 |
| 108 fColorCache = GrColor_ILLEGAL; |
| 109 fCoverageCache = GrColor_ILLEGAL; |
| 110 |
| 111 fColorPrimProc = NULL; |
| 112 fCoveragePrimProc = NULL; |
| 113 |
120 } | 114 } |
121 | 115 |
122 bool GrDrawState::setIdentityViewMatrix() { | 116 bool GrDrawState::setIdentityViewMatrix() { |
123 if (this->numFragmentStages()) { | 117 if (this->numFragmentStages()) { |
124 SkMatrix invVM; | 118 SkMatrix invVM; |
125 if (!fViewMatrix.invert(&invVM)) { | 119 if (!fViewMatrix.invert(&invVM)) { |
126 // sad trombone sound | 120 // sad trombone sound |
127 return false; | 121 return false; |
128 } | 122 } |
129 for (int s = 0; s < this->numColorStages(); ++s) { | 123 for (int s = 0; s < this->numColorStages(); ++s) { |
130 fColorStages[s].localCoordChange(invVM); | 124 fColorStages[s].localCoordChange(invVM); |
131 } | 125 } |
132 for (int s = 0; s < this->numCoverageStages(); ++s) { | 126 for (int s = 0; s < this->numCoverageStages(); ++s) { |
133 fCoverageStages[s].localCoordChange(invVM); | 127 fCoverageStages[s].localCoordChange(invVM); |
134 } | 128 } |
135 } | 129 } |
136 fViewMatrix.reset(); | 130 fViewMatrix.reset(); |
137 return true; | 131 return true; |
138 } | 132 } |
139 | 133 |
140 void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRende
rTarget* rt) { | 134 void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRende
rTarget* rt) { |
141 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); | 135 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
142 | 136 |
143 fGeometryProcessor.reset(NULL); | |
144 fColorStages.reset(); | 137 fColorStages.reset(); |
145 fCoverageStages.reset(); | 138 fCoverageStages.reset(); |
146 | 139 |
147 for (int i = 0; i < paint.numColorStages(); ++i) { | 140 for (int i = 0; i < paint.numColorStages(); ++i) { |
148 fColorStages.push_back(paint.getColorStage(i)); | 141 fColorStages.push_back(paint.getColorStage(i)); |
149 } | 142 } |
150 | 143 |
151 for (int i = 0; i < paint.numCoverageStages(); ++i) { | 144 for (int i = 0; i < paint.numCoverageStages(); ++i) { |
152 fCoverageStages.push_back(paint.getCoverageStage(i)); | 145 fCoverageStages.push_back(paint.getCoverageStage(i)); |
153 } | 146 } |
(...skipping 30 matching lines...) Expand all Loading... |
184 this->calcColorInvariantOutput(color); | 177 this->calcColorInvariantOutput(color); |
185 | 178 |
186 // The coverage isn't actually white, its unknown, but this will produce the
same effect | 179 // The coverage isn't actually white, its unknown, but this will produce the
same effect |
187 // TODO we want to cache the result of this call, but we can probably clean
up the interface | 180 // TODO we want to cache the result of this call, but we can probably clean
up the interface |
188 // so we don't have to pass in a seemingly known coverage | 181 // so we don't have to pass in a seemingly known coverage |
189 this->calcCoverageInvariantOutput(GrColor_WHITE); | 182 this->calcCoverageInvariantOutput(GrColor_WHITE); |
190 return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo, | 183 return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo, |
191 this->isCoverageDrawing(), this->isColor
WriteDisabled()); | 184 this->isCoverageDrawing(), this->isColor
WriteDisabled()); |
192 } | 185 } |
193 | 186 |
194 bool GrDrawState::hasSolidCoverage(GrColor coverage) const { | 187 bool GrDrawState::hasSolidCoverage(const GrPrimitiveProcessor* pp) const { |
195 // If we're drawing coverage directly then coverage is effectively treated a
s color. | 188 // If we're drawing coverage directly then coverage is effectively treated a
s color. |
196 if (this->isCoverageDrawing()) { | 189 if (this->isCoverageDrawing()) { |
197 return true; | 190 return true; |
198 } | 191 } |
199 | 192 |
200 if (this->numCoverageStages() > 0) { | 193 if (this->numCoverageStages() > 0) { |
201 return false; | 194 return false; |
202 } | 195 } |
203 | 196 |
204 this->calcCoverageInvariantOutput(coverage); | 197 this->calcCoverageInvariantOutput(pp); |
205 return fCoverageProcInfo.isSolidWhite(); | 198 return fCoverageProcInfo.isSolidWhite(); |
206 } | 199 } |
207 | 200 |
208 //////////////////////////////////////////////////////////////////////////////s | 201 //////////////////////////////////////////////////////////////////////////////s |
209 | 202 |
210 bool GrDrawState::willEffectReadDstColor(GrColor color, GrColor coverage) const
{ | 203 bool GrDrawState::willEffectReadDstColor(const GrPrimitiveProcessor* pp) const { |
211 this->calcColorInvariantOutput(color); | 204 this->calcColorInvariantOutput(pp); |
212 this->calcCoverageInvariantOutput(coverage); | 205 this->calcCoverageInvariantOutput(pp); |
213 // TODO: Remove need to create the XP here. | 206 // TODO: Remove need to create the XP here. |
214 // Also once all custom blends are turned into XPs we can remove the n
eed | 207 // Also once all custom blends are turned into XPs we can remove the n
eed |
215 // to check other stages since only xp's will be able to read dst | 208 // to check other stages since only xp's will be able to read dst |
216 SkAutoTUnref<GrXferProcessor> xferProcessor(fXPFactory->createXferProcessor(
fColorProcInfo, | 209 SkAutoTUnref<GrXferProcessor> xferProcessor(fXPFactory->createXferProcessor(
fColorProcInfo, |
217
fCoverageProcInfo)); | 210
fCoverageProcInfo)); |
218 if (xferProcessor && xferProcessor->willReadDstColor()) { | 211 if (xferProcessor && xferProcessor->willReadDstColor()) { |
219 return true; | 212 return true; |
220 } | 213 } |
221 | 214 |
222 if (!this->isColorWriteDisabled()) { | 215 if (!this->isColorWriteDisabled()) { |
223 if (fColorProcInfo.readsDst()) { | 216 if (fColorProcInfo.readsDst()) { |
224 return true; | 217 return true; |
225 } | 218 } |
226 } | 219 } |
227 return fCoverageProcInfo.readsDst(); | 220 return fCoverageProcInfo.readsDst(); |
228 } | 221 } |
229 | 222 |
230 void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { | 223 void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { |
231 if (fDrawState) { | 224 if (fDrawState) { |
232 // See the big comment on the class definition about GPs. | |
233 if (SK_InvalidUniqueID == fOriginalGPID) { | |
234 fDrawState->fGeometryProcessor.reset(NULL); | |
235 } else { | |
236 SkASSERT(fDrawState->getGeometryProcessor()->getUniqueID() == | |
237 fOriginalGPID); | |
238 fOriginalGPID = SK_InvalidUniqueID; | |
239 } | |
240 | |
241 int m = fDrawState->numColorStages() - fColorEffectCnt; | 225 int m = fDrawState->numColorStages() - fColorEffectCnt; |
242 SkASSERT(m >= 0); | 226 SkASSERT(m >= 0); |
243 fDrawState->fColorStages.pop_back_n(m); | 227 fDrawState->fColorStages.pop_back_n(m); |
244 | 228 |
245 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; | 229 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; |
246 SkASSERT(n >= 0); | 230 SkASSERT(n >= 0); |
247 fDrawState->fCoverageStages.pop_back_n(n); | 231 fDrawState->fCoverageStages.pop_back_n(n); |
248 if (m + n > 0) { | 232 if (m + n > 0) { |
249 fDrawState->fColorProcInfoValid = false; | 233 fDrawState->fColorProcInfoValid = false; |
250 fDrawState->fCoverageProcInfoValid = false; | 234 fDrawState->fCoverageProcInfoValid = false; |
251 } | 235 } |
252 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) | 236 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
253 } | 237 } |
254 fDrawState = ds; | 238 fDrawState = ds; |
255 if (NULL != ds) { | 239 if (NULL != ds) { |
256 SkASSERT(SK_InvalidUniqueID == fOriginalGPID); | |
257 if (NULL != ds->getGeometryProcessor()) { | |
258 fOriginalGPID = ds->getGeometryProcessor()->getUniqueID(); | |
259 } | |
260 fColorEffectCnt = ds->numColorStages(); | 240 fColorEffectCnt = ds->numColorStages(); |
261 fCoverageEffectCnt = ds->numCoverageStages(); | 241 fCoverageEffectCnt = ds->numCoverageStages(); |
262 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) | 242 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
263 } | 243 } |
264 } | 244 } |
265 | 245 |
266 //////////////////////////////////////////////////////////////////////////////// | 246 //////////////////////////////////////////////////////////////////////////////// |
267 | 247 |
268 // Some blend modes allow folding a fractional coverage value into the color's a
lpha channel, while | 248 // Some blend modes allow folding a fractional coverage value into the color's a
lpha channel, while |
269 // others will blend incorrectly. | 249 // others will blend incorrectly. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 } | 339 } |
360 | 340 |
361 //////////////////////////////////////////////////////////////////////////////// | 341 //////////////////////////////////////////////////////////////////////////////// |
362 | 342 |
363 GrDrawState::~GrDrawState() { | 343 GrDrawState::~GrDrawState() { |
364 SkASSERT(0 == fBlockEffectRemovalCnt); | 344 SkASSERT(0 == fBlockEffectRemovalCnt); |
365 } | 345 } |
366 | 346 |
367 //////////////////////////////////////////////////////////////////////////////// | 347 //////////////////////////////////////////////////////////////////////////////// |
368 | 348 |
369 bool GrDrawState::srcAlphaWillBeOne(GrColor color, GrColor coverage) const { | 349 bool GrDrawState::willBlendWithDst(const GrPrimitiveProcessor* pp) const { |
370 this->calcColorInvariantOutput(color); | 350 this->calcColorInvariantOutput(pp); |
371 if (this->isCoverageDrawing()) { | 351 this->calcCoverageInvariantOutput(pp); |
372 this->calcCoverageInvariantOutput(coverage); | |
373 return (fColorProcInfo.isOpaque() && fCoverageProcInfo.isOpaque()); | |
374 } | |
375 return fColorProcInfo.isOpaque(); | |
376 } | |
377 | |
378 bool GrDrawState::willBlendWithDst(GrColor color, GrColor coverage) const { | |
379 this->calcColorInvariantOutput(color); | |
380 this->calcCoverageInvariantOutput(coverage); | |
381 return fXPFactory->willBlendWithDst(fColorProcInfo, fCoverageProcInfo, | 352 return fXPFactory->willBlendWithDst(fColorProcInfo, fCoverageProcInfo, |
382 this->isCoverageDrawing(), this->isColor
WriteDisabled()); | 353 this->isCoverageDrawing(), this->isColor
WriteDisabled()); |
383 } | 354 } |
384 | 355 |
| 356 void GrDrawState::calcColorInvariantOutput(const GrPrimitiveProcessor* pp) const
{ |
| 357 if (!fColorProcInfoValid || fColorPrimProc != pp) { |
| 358 fColorProcInfo.calcColorWithPrimProc(pp, fColorStages.begin(), this->num
ColorStages()); |
| 359 fColorProcInfoValid = true; |
| 360 fColorPrimProc = pp; |
| 361 } |
| 362 } |
| 363 |
| 364 void GrDrawState::calcCoverageInvariantOutput(const GrPrimitiveProcessor* pp) co
nst { |
| 365 if (!fCoverageProcInfoValid || fCoveragePrimProc != pp) { |
| 366 fCoverageProcInfo.calcCoverageWithPrimProc(pp, fCoverageStages.begin(), |
| 367 this->numCoverageStages()); |
| 368 fCoverageProcInfoValid = true; |
| 369 fCoveragePrimProc = pp; |
| 370 } |
| 371 } |
| 372 |
385 void GrDrawState::calcColorInvariantOutput(GrColor color) const { | 373 void GrDrawState::calcColorInvariantOutput(GrColor color) const { |
386 if (!fColorProcInfoValid || color != fColorCache) { | 374 if (!fColorProcInfoValid || color != fColorCache) { |
387 GrColorComponentFlags flags; | 375 GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
388 if (this->hasColorVertexAttribute()) { | 376 fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColo
rStages(), color, |
389 if (fHints & kVertexColorsAreOpaque_Hint) { | 377 flags, false); |
390 flags = kA_GrColorComponentFlag; | |
391 color = 0xFF << GrColor_SHIFT_A; | |
392 } else { | |
393 flags = static_cast<GrColorComponentFlags>(0); | |
394 color = 0; | |
395 } | |
396 } else { | |
397 flags = kRGBA_GrColorComponentFlags; | |
398 } | |
399 fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColo
rStages(), | |
400 color, flags, false); | |
401 fColorProcInfoValid = true; | 378 fColorProcInfoValid = true; |
402 fColorCache = color; | 379 fColorCache = color; |
403 } | 380 } |
404 } | 381 } |
405 | 382 |
406 void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const { | 383 void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const { |
407 if (!fCoverageProcInfoValid || coverage != fCoverageCache) { | 384 if (!fCoverageProcInfoValid || coverage != fCoverageCache) { |
408 GrColorComponentFlags flags; | 385 GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
409 // Check if per-vertex or constant color may have partial alpha | 386 fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), |
410 if (this->hasCoverageVertexAttribute()) { | 387 this->numCoverageStages(), c
overage, flags, |
411 flags = static_cast<GrColorComponentFlags>(0); | 388 true); |
412 coverage = 0; | |
413 } else { | |
414 flags = kRGBA_GrColorComponentFlags; | |
415 } | |
416 fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), this->n
umCoverageStages(), | |
417 coverage, flags, true, fGeometry
Processor.get()); | |
418 fCoverageProcInfoValid = true; | 389 fCoverageProcInfoValid = true; |
419 fCoverageCache = coverage; | 390 fCoverageCache = coverage; |
420 } | 391 } |
421 } | 392 } |
OLD | NEW |