OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "effects/GrPorterDuffXferProcessor.h" | 8 #include "effects/GrPorterDuffXferProcessor.h" |
9 | 9 |
10 #include "GrBlend.h" | 10 #include "GrBlend.h" |
11 #include "GrDrawState.h" | 11 #include "GrDrawState.h" |
12 #include "GrDrawTargetCaps.h" | |
13 #include "GrInvariantOutput.h" | 12 #include "GrInvariantOutput.h" |
14 #include "GrProcessor.h" | 13 #include "GrProcessor.h" |
15 #include "GrTypes.h" | 14 #include "GrTypes.h" |
16 #include "GrXferProcessor.h" | 15 #include "GrXferProcessor.h" |
17 #include "gl/GrGLXferProcessor.h" | 16 #include "gl/GrGLProcessor.h" |
18 #include "gl/builders/GrGLFragmentShaderBuilder.h" | 17 #include "gl/builders/GrGLFragmentShaderBuilder.h" |
19 #include "gl/builders/GrGLProgramBuilder.h" | 18 #include "gl/builders/GrGLProgramBuilder.h" |
20 | 19 |
21 static bool can_tweak_alpha_for_coverage(GrBlendCoeff dstCoeff, bool isCoverageD
rawing) { | 20 static bool can_tweak_alpha_for_coverage(GrBlendCoeff dstCoeff, bool isCoverageD
rawing) { |
22 /* | 21 /* |
23 The fractional coverage is f. | 22 The fractional coverage is f. |
24 The src and dst coeffs are Cs and Cd. | 23 The src and dst coeffs are Cs and Cd. |
25 The dst and src colors are S and D. | 24 The dst and src colors are S and D. |
26 We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D. By tweaking the sou
rce color's alpha | 25 We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D. By tweaking the sou
rce color's alpha |
27 we're replacing S with S'=fS. It's obvious that that first term will always
be ok. The second | 26 we're replacing S with S'=fS. It's obvious that that first term will always
be ok. The second |
28 term can be rearranged as [1-(1-Cd)f]D. By substituting in the various poss
ibilities for Cd we | 27 term can be rearranged as [1-(1-Cd)f]D. By substituting in the various poss
ibilities for Cd we |
29 find that only 1, ISA, and ISC produce the correct destination when applied
to S' and D. | 28 find that only 1, ISA, and ISC produce the correct destination when applied
to S' and D. |
30 Also, if we're directly rendering coverage (isCoverageDrawing) then coverag
e is treated as | 29 Also, if we're directly rendering coverage (isCoverageDrawing) then coverag
e is treated as |
31 color by definition. | 30 color by definition. |
32 */ | 31 */ |
33 // TODO: Once we have a CoverageDrawing XP, we don't need to check is Covera
geDrawing here | 32 // TODO: Once we have a CoverageDrawing XP, we don't need to check is Covera
geDrawing here |
34 return kOne_GrBlendCoeff == dstCoeff || | 33 return kOne_GrBlendCoeff == dstCoeff || |
35 kISA_GrBlendCoeff == dstCoeff || | 34 kISA_GrBlendCoeff == dstCoeff || |
36 kISC_GrBlendCoeff == dstCoeff || | 35 kISC_GrBlendCoeff == dstCoeff || |
37 isCoverageDrawing; | 36 isCoverageDrawing; |
38 } | 37 } |
39 | 38 |
40 class GrGLPorterDuffXferProcessor : public GrGLXferProcessor { | 39 class GrGLPorterDuffXferProcessor : public GrGLXferProcessor { |
41 public: | 40 public: |
42 GrGLPorterDuffXferProcessor(const GrProcessor&) {} | 41 GrGLPorterDuffXferProcessor(const GrProcessor&) {} |
43 | 42 |
44 virtual ~GrGLPorterDuffXferProcessor() {} | 43 virtual ~GrGLPorterDuffXferProcessor() {} |
45 | 44 |
46 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { | 45 virtual void emitCode(GrGLFPBuilder* builder, |
47 const GrPorterDuffXferProcessor& xp = args.fXP.cast<GrPorterDuffXferProc
essor>(); | 46 const GrFragmentProcessor& fp, |
48 GrGLFPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 47 const char* outputColor, |
49 if (xp.hasSecondaryOutput()) { | 48 const char* inputColor, |
50 switch(xp.secondaryOutputType()) { | 49 const TransformedCoordsArray& coords, |
51 case GrPorterDuffXferProcessor::kCoverage_SecondaryOutputType: | 50 const TextureSamplerArray& samplers) SK_OVERRIDE { |
52 fsBuilder->codeAppendf("%s = %s;", args.fOutputSecondary, ar
gs.fInputCoverage); | 51 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
53 break; | 52 fsBuilder->codeAppendf("%s = %s;", outputColor, inputColor); |
54 case GrPorterDuffXferProcessor::kCoverageISA_SecondaryOutputType
: | |
55 fsBuilder->codeAppendf("%s = (1.0 - %s.a) * %s;", | |
56 args.fOutputSecondary, args.fInputCol
or, | |
57 args.fInputCoverage); | |
58 break; | |
59 case GrPorterDuffXferProcessor::kCoverageISC_SecondaryOutputType
: | |
60 fsBuilder->codeAppendf("%s = (vec4(1.0) - %s) * %s;", | |
61 args.fOutputSecondary, args.fInputCol
or, | |
62 args.fInputCoverage); | |
63 break; | |
64 default: | |
65 SkFAIL("Unexpected Secondary Output"); | |
66 } | |
67 } | |
68 | |
69 fsBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInput
Color, | |
70 args.fInputCoverage); | |
71 } | 53 } |
72 | 54 |
73 virtual void setData(const GrGLProgramDataManager&, const GrXferProcessor&)
SK_OVERRIDE {}; | 55 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE {}; |
74 | 56 |
75 static void GenKey(const GrProcessor& processor, const GrGLCaps& caps, | 57 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyB
uilder* b) {}; |
76 GrProcessorKeyBuilder* b) { | |
77 const GrPorterDuffXferProcessor& xp = processor.cast<GrPorterDuffXferPro
cessor>(); | |
78 b->add32(xp.secondaryOutputType()); | |
79 }; | |
80 | 58 |
81 private: | 59 private: |
82 typedef GrGLXferProcessor INHERITED; | 60 typedef GrGLXferProcessor INHERITED; |
83 }; | 61 }; |
84 | 62 |
85 /////////////////////////////////////////////////////////////////////////////// | 63 /////////////////////////////////////////////////////////////////////////////// |
86 | 64 |
87 GrPorterDuffXferProcessor::GrPorterDuffXferProcessor(GrBlendCoeff srcBlend, GrBl
endCoeff dstBlend, | 65 GrPorterDuffXferProcessor::GrPorterDuffXferProcessor(GrBlendCoeff srcBlend, GrBl
endCoeff dstBlend, |
88 GrColor constant) | 66 GrColor constant) |
89 : fSrcBlend(srcBlend) | 67 : fSrcBlend(srcBlend), fDstBlend(dstBlend), fBlendConstant(constant) { |
90 , fDstBlend(dstBlend) | |
91 , fBlendConstant(constant) | |
92 , fSecondaryOutputType(kNone_SecondaryOutputType) { | |
93 this->initClassID<GrPorterDuffXferProcessor>(); | 68 this->initClassID<GrPorterDuffXferProcessor>(); |
94 } | 69 } |
95 | 70 |
96 GrPorterDuffXferProcessor::~GrPorterDuffXferProcessor() { | 71 GrPorterDuffXferProcessor::~GrPorterDuffXferProcessor() { |
97 } | 72 } |
98 | 73 |
99 void GrPorterDuffXferProcessor::getGLProcessorKey(const GrGLCaps& caps, | 74 void GrPorterDuffXferProcessor::getGLProcessorKey(const GrGLCaps& caps, |
100 GrProcessorKeyBuilder* b) cons
t { | 75 GrProcessorKeyBuilder* b) cons
t { |
101 GrGLPorterDuffXferProcessor::GenKey(*this, caps, b); | 76 GrGLPorterDuffXferProcessor::GenKey(*this, caps, b); |
102 } | 77 } |
103 | 78 |
104 GrGLXferProcessor* GrPorterDuffXferProcessor::createGLInstance() const { | 79 GrGLFragmentProcessor* GrPorterDuffXferProcessor::createGLInstance() const { |
105 return SkNEW_ARGS(GrGLPorterDuffXferProcessor, (*this)); | 80 return SkNEW_ARGS(GrGLPorterDuffXferProcessor, (*this)); |
106 } | 81 } |
107 | 82 |
108 void GrPorterDuffXferProcessor::onComputeInvariantOutput(GrInvariantOutput* inou
t) const { | 83 void GrPorterDuffXferProcessor::onComputeInvariantOutput(GrInvariantOutput* inou
t) const { |
109 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); | 84 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
110 } | 85 } |
111 | 86 |
112 GrXferProcessor::OptFlags | 87 GrXferProcessor::OptFlags |
113 GrPorterDuffXferProcessor::getOptimizations(const GrProcOptInfo& colorPOI, | 88 GrPorterDuffXferProcessor::getOptimizations(const GrProcOptInfo& colorPOI, |
114 const GrProcOptInfo& coveragePOI, | 89 const GrProcOptInfo& coveragePOI, |
115 bool isCoverageDrawing, | 90 bool isCoverageDrawing, |
116 bool colorWriteDisabled, | 91 bool colorWriteDisabled, |
117 bool doesStencilWrite, | 92 bool doesStencilWrite, |
118 GrColor* color, uint8_t* coverage, | 93 GrColor* color, uint8_t* coverage) { |
119 const GrDrawTargetCaps& caps) { | |
120 GrXferProcessor::OptFlags optFlags = this->internalGetOptimizations(colorPOI
, | |
121 coverage
POI, | |
122 isCovera
geDrawing, | |
123 colorWri
teDisabled, | |
124 doesSten
cilWrite, | |
125 color, | |
126 coverage
); | |
127 | |
128 this->calcOutputTypes(optFlags, caps, isCoverageDrawing, | |
129 colorPOI.readsDst() || coveragePOI.readsDst()); | |
130 return optFlags; | |
131 } | |
132 | |
133 void GrPorterDuffXferProcessor::calcOutputTypes(GrXferProcessor::OptFlags optFla
gs, | |
134 const GrDrawTargetCaps& caps, | |
135 bool isCoverageDrawing, bool rea
dsDst) { | |
136 // If we do have coverage determine whether it matters. Dual source blendin
g is expensive so | |
137 // we don't do it if we are doing coverage drawing. If we aren't then We al
ways do dual source | |
138 // blending if we have any effective coverage stages OR the geometry process
or doesn't emits | |
139 // solid coverage. | |
140 if (!(optFlags & kSetCoverageDrawing_OptFlag) && !isCoverageDrawing) { | |
141 if (caps.dualSourceBlendingSupport()) { | |
142 if (kZero_GrBlendCoeff == fDstBlend) { | |
143 // write the coverage value to second color | |
144 fSecondaryOutputType = kCoverage_SecondaryOutputType; | |
145 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; | |
146 } else if (kSA_GrBlendCoeff == fDstBlend) { | |
147 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. | |
148 fSecondaryOutputType = kCoverageISA_SecondaryOutputType; | |
149 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; | |
150 } else if (kSC_GrBlendCoeff == fDstBlend) { | |
151 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. | |
152 fSecondaryOutputType = kCoverageISC_SecondaryOutputType; | |
153 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; | |
154 } | |
155 } | |
156 } | |
157 } | |
158 | |
159 GrXferProcessor::OptFlags | |
160 GrPorterDuffXferProcessor::internalGetOptimizations(const GrProcOptInfo& colorPO
I, | |
161 const GrProcOptInfo& coverag
ePOI, | |
162 bool isCoverageDrawing, | |
163 bool colorWriteDisabled, | |
164 bool doesStencilWrite, | |
165 GrColor* color, uint8_t* cov
erage) { | |
166 if (colorWriteDisabled) { | 94 if (colorWriteDisabled) { |
167 fSrcBlend = kZero_GrBlendCoeff; | 95 fSrcBlend = kZero_GrBlendCoeff; |
168 fDstBlend = kOne_GrBlendCoeff; | 96 fDstBlend = kOne_GrBlendCoeff; |
169 } | 97 } |
170 | 98 |
171 bool srcAIsOne; | 99 bool srcAIsOne; |
172 bool hasCoverage; | 100 bool hasCoverage; |
173 if (isCoverageDrawing) { | 101 if (isCoverageDrawing) { |
174 srcAIsOne = colorPOI.isOpaque() && coveragePOI.isOpaque(); | 102 srcAIsOne = colorPOI.isOpaque() && coveragePOI.isOpaque(); |
175 hasCoverage = false; | 103 hasCoverage = false; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 } else if (dstCoeffIsOne) { | 185 } else if (dstCoeffIsOne) { |
258 // the dst coeff is effectively one so blend works out to: | 186 // the dst coeff is effectively one so blend works out to: |
259 // cS + (c)(1)D + (1-c)D = cS + D. | 187 // cS + (c)(1)D + (1-c)D = cS + D. |
260 fDstBlend = kOne_GrBlendCoeff; | 188 fDstBlend = kOne_GrBlendCoeff; |
261 return GrXferProcessor::kSetCoverageDrawing_OptFlag; | 189 return GrXferProcessor::kSetCoverageDrawing_OptFlag; |
262 } | 190 } |
263 } | 191 } |
264 | 192 |
265 return GrXferProcessor::kNone_Opt; | 193 return GrXferProcessor::kNone_Opt; |
266 } | 194 } |
267 | |
268 bool GrPorterDuffXferProcessor::hasSecondaryOutput() const { | |
269 return kNone_SecondaryOutputType != fSecondaryOutputType; | |
270 } | |
271 | |
272 /////////////////////////////////////////////////////////////////////////////// | 195 /////////////////////////////////////////////////////////////////////////////// |
273 | 196 |
274 GrPorterDuffXPFactory::GrPorterDuffXPFactory(GrBlendCoeff src, GrBlendCoeff dst) | 197 GrPorterDuffXPFactory::GrPorterDuffXPFactory(GrBlendCoeff src, GrBlendCoeff dst) |
275 : fSrcCoeff(src), fDstCoeff(dst) { | 198 : fSrcCoeff(src), fDstCoeff(dst) { |
276 this->initClassID<GrPorterDuffXPFactory>(); | 199 this->initClassID<GrPorterDuffXPFactory>(); |
277 } | 200 } |
278 | 201 |
279 GrXPFactory* GrPorterDuffXPFactory::Create(SkXfermode::Mode mode) { | 202 GrXPFactory* GrPorterDuffXPFactory::Create(SkXfermode::Mode mode) { |
280 switch (mode) { | 203 switch (mode) { |
281 case SkXfermode::kClear_Mode: { | 204 case SkXfermode::kClear_Mode: { |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 *solidColorKnownComponents = 0; | 443 *solidColorKnownComponents = 0; |
521 break; | 444 break; |
522 } | 445 } |
523 } else { | 446 } else { |
524 solidColorKnownComponents = 0; | 447 solidColorKnownComponents = 0; |
525 } | 448 } |
526 } | 449 } |
527 return opaque; | 450 return opaque; |
528 } | 451 } |
529 | 452 |
530 GR_DEFINE_XP_FACTORY_TEST(GrPorterDuffXPFactory); | |
531 | 453 |
532 GrXPFactory* GrPorterDuffXPFactory::TestCreate(SkRandom* random, | |
533 GrContext*, | |
534 const GrDrawTargetCaps&, | |
535 GrTexture*[]) { | |
536 GrBlendCoeff src; | |
537 do { | |
538 src = GrBlendCoeff(random->nextRangeU(kFirstPublicGrBlendCoeff, kLastPub
licGrBlendCoeff)); | |
539 } while (GrBlendCoeffRefsSrc(src)); | |
540 | |
541 GrBlendCoeff dst; | |
542 do { | |
543 dst = GrBlendCoeff(random->nextRangeU(kFirstPublicGrBlendCoeff, kLastPub
licGrBlendCoeff)); | |
544 } while (GrBlendCoeffRefsDst(dst)); | |
545 | |
546 return GrPorterDuffXPFactory::Create(src, dst); | |
547 } | |
548 | |
OLD | NEW |