blob: e962165725fa954e3b23c61b6d0cc7c5035240bd [file] [log] [blame]
bsalomon50785a32015-02-06 07:02:37 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrXferProcessor.h"
Robert Phillipsbe77a022018-04-03 17:17:05 -04009
10#include "GrCaps.h"
ethannicholasde4166a2015-11-30 08:57:38 -080011#include "GrPipeline.h"
bsalomon50785a32015-02-06 07:02:37 -080012
Ethan Nicholasabff9562017-10-09 10:54:08 -040013GrXferProcessor::GrXferProcessor(ClassID classID)
14 : INHERITED(classID)
15 , fWillReadDstColor(false)
Greg Daniel6ebe4b92017-05-19 10:56:46 -040016 , fDstReadUsesMixedSamples(false)
17 , fIsLCD(false) {}
bsalomon50785a32015-02-06 07:02:37 -080018
Ethan Nicholasabff9562017-10-09 10:54:08 -040019GrXferProcessor::GrXferProcessor(ClassID classID, bool willReadDstColor, bool hasMixedSamples,
Greg Daniel6ebe4b92017-05-19 10:56:46 -040020 GrProcessorAnalysisCoverage coverage)
Ethan Nicholasabff9562017-10-09 10:54:08 -040021 : INHERITED(classID)
22 , fWillReadDstColor(willReadDstColor)
Greg Daniel6ebe4b92017-05-19 10:56:46 -040023 , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples)
24 , fIsLCD(GrProcessorAnalysisCoverage::kLCD == coverage) {}
bsalomon50785a32015-02-06 07:02:37 -080025
cdaltonedbb31f2015-06-08 12:14:44 -070026bool GrXferProcessor::hasSecondaryOutput() const {
27 if (!this->willReadDstColor()) {
28 return this->onHasSecondaryOutput();
29 }
cdalton86ae0a92015-06-08 15:11:04 -070030 return this->dstReadUsesMixedSamples();
cdaltonedbb31f2015-06-08 12:14:44 -070031}
32
33void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const {
34 blendInfo->reset();
35 if (!this->willReadDstColor()) {
36 this->onGetBlendInfo(blendInfo);
cdalton86ae0a92015-06-08 15:11:04 -070037 } else if (this->dstReadUsesMixedSamples()) {
38 blendInfo->fDstBlend = kIS2A_GrBlendCoeff;
cdaltonedbb31f2015-06-08 12:14:44 -070039 }
40}
41
Brian Salomon18dfa982017-04-03 16:57:43 -040042void GrXferProcessor::getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b,
43 const GrSurfaceOrigin* originIfDstTexture) const {
bsalomon50785a32015-02-06 07:02:37 -080044 uint32_t key = this->willReadDstColor() ? 0x1 : 0x0;
cdaltonedbb31f2015-06-08 12:14:44 -070045 if (key) {
Brian Salomon18dfa982017-04-03 16:57:43 -040046 if (originIfDstTexture) {
cdaltonedbb31f2015-06-08 12:14:44 -070047 key |= 0x2;
Brian Salomon18dfa982017-04-03 16:57:43 -040048 if (kTopLeft_GrSurfaceOrigin == *originIfDstTexture) {
cdalton827bae12015-06-08 13:43:33 -070049 key |= 0x4;
50 }
cdaltonedbb31f2015-06-08 12:14:44 -070051 }
cdalton86ae0a92015-06-08 15:11:04 -070052 if (this->dstReadUsesMixedSamples()) {
egdaniel56cf6dc2015-11-30 10:15:58 -080053 key |= 0x8;
cdalton86ae0a92015-06-08 15:11:04 -070054 }
bsalomon50785a32015-02-06 07:02:37 -080055 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -040056 if (fIsLCD) {
57 key |= 0x10;
58 }
bsalomon50785a32015-02-06 07:02:37 -080059 b->add32(key);
egdaniel57d3b032015-11-13 11:57:27 -080060 this->onGetGLSLProcessorKey(caps, b);
bsalomon50785a32015-02-06 07:02:37 -080061}
62
bsalomonf7cc8772015-05-11 11:21:14 -070063#ifdef SK_DEBUG
64static const char* equation_string(GrBlendEquation eq) {
65 switch (eq) {
66 case kAdd_GrBlendEquation:
67 return "add";
68 case kSubtract_GrBlendEquation:
69 return "subtract";
70 case kReverseSubtract_GrBlendEquation:
71 return "reverse_subtract";
72 case kScreen_GrBlendEquation:
73 return "screen";
74 case kOverlay_GrBlendEquation:
75 return "overlay";
76 case kDarken_GrBlendEquation:
77 return "darken";
78 case kLighten_GrBlendEquation:
79 return "lighten";
80 case kColorDodge_GrBlendEquation:
81 return "color_dodge";
82 case kColorBurn_GrBlendEquation:
83 return "color_burn";
84 case kHardLight_GrBlendEquation:
85 return "hard_light";
86 case kSoftLight_GrBlendEquation:
87 return "soft_light";
88 case kDifference_GrBlendEquation:
89 return "difference";
90 case kExclusion_GrBlendEquation:
91 return "exclusion";
92 case kMultiply_GrBlendEquation:
93 return "multiply";
94 case kHSLHue_GrBlendEquation:
95 return "hsl_hue";
96 case kHSLSaturation_GrBlendEquation:
97 return "hsl_saturation";
98 case kHSLColor_GrBlendEquation:
99 return "hsl_color";
100 case kHSLLuminosity_GrBlendEquation:
101 return "hsl_luminosity";
Mike Klein36743362018-11-06 08:23:30 -0500102 case kIllegal_GrBlendEquation:
103 SkASSERT(false);
104 return "<illegal>";
Brian Salomon23356442018-11-30 15:33:19 -0500105 }
bsalomonf7cc8772015-05-11 11:21:14 -0700106 return "";
107}
108
109static const char* coeff_string(GrBlendCoeff coeff) {
110 switch (coeff) {
111 case kZero_GrBlendCoeff:
112 return "zero";
113 case kOne_GrBlendCoeff:
114 return "one";
115 case kSC_GrBlendCoeff:
116 return "src_color";
117 case kISC_GrBlendCoeff:
118 return "inv_src_color";
119 case kDC_GrBlendCoeff:
120 return "dst_color";
121 case kIDC_GrBlendCoeff:
122 return "inv_dst_color";
123 case kSA_GrBlendCoeff:
124 return "src_alpha";
125 case kISA_GrBlendCoeff:
126 return "inv_src_alpha";
127 case kDA_GrBlendCoeff:
128 return "dst_alpha";
129 case kIDA_GrBlendCoeff:
130 return "inv_dst_alpha";
131 case kConstC_GrBlendCoeff:
132 return "const_color";
133 case kIConstC_GrBlendCoeff:
134 return "inv_const_color";
135 case kConstA_GrBlendCoeff:
136 return "const_alpha";
137 case kIConstA_GrBlendCoeff:
138 return "inv_const_alpha";
139 case kS2C_GrBlendCoeff:
140 return "src2_color";
141 case kIS2C_GrBlendCoeff:
142 return "inv_src2_color";
143 case kS2A_GrBlendCoeff:
144 return "src2_alpha";
145 case kIS2A_GrBlendCoeff:
146 return "inv_src2_alpha";
Mike Klein36743362018-11-06 08:23:30 -0500147 case kIllegal_GrBlendCoeff:
148 SkASSERT(false);
149 return "<illegal>";
bsalomonf7cc8772015-05-11 11:21:14 -0700150 }
151 return "";
152}
153
154SkString GrXferProcessor::BlendInfo::dump() const {
155 SkString out;
156 out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)",
157 fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend),
Brian Osman422f95b2018-11-05 16:49:04 -0500158 coeff_string(fDstBlend), fBlendConstant.toBytes_RGBA());
bsalomonf7cc8772015-05-11 11:21:14 -0700159 return out;
160}
161#endif
162
bsalomon50785a32015-02-06 07:02:37 -0800163///////////////////////////////////////////////////////////////////////////////
164
Brian Salomon31853842017-03-28 16:32:05 -0400165GrXPFactory::AnalysisProperties GrXPFactory::GetAnalysisProperties(
166 const GrXPFactory* factory,
Brian Salomona811b122017-03-30 08:21:32 -0400167 const GrProcessorAnalysisColor& color,
168 const GrProcessorAnalysisCoverage& coverage,
Brian Osman532b3f92018-07-11 10:02:07 -0400169 const GrCaps& caps) {
Brian Salomon31853842017-03-28 16:32:05 -0400170 AnalysisProperties result;
Brian Salomon5298dc82017-02-22 11:52:03 -0500171 if (factory) {
Brian Osman532b3f92018-07-11 10:02:07 -0400172 result = factory->analysisProperties(color, coverage, caps);
Brian Salomon5298dc82017-02-22 11:52:03 -0500173 } else {
Brian Osman532b3f92018-07-11 10:02:07 -0400174 result = GrPorterDuffXPFactory::SrcOverAnalysisProperties(color, coverage, caps);
Brian Salomon31853842017-03-28 16:32:05 -0400175 }
176 SkASSERT(!(result & AnalysisProperties::kRequiresDstTexture));
177 if ((result & AnalysisProperties::kReadsDstInShader) &&
178 !caps.shaderCaps()->dstReadInShaderSupport()) {
179 result |= AnalysisProperties::kRequiresDstTexture;
Brian Salomon4fc77402017-03-30 16:48:26 -0400180 if (caps.textureBarrierSupport()) {
181 result |= AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws;
182 }
Brian Salomon5298dc82017-02-22 11:52:03 -0500183 }
Brian Salomon5298dc82017-02-22 11:52:03 -0500184 return result;
Brian Salomon9a514982017-02-14 10:28:22 -0500185}
186
Brian Salomond61c9d92017-04-10 10:54:25 -0400187sk_sp<const GrXferProcessor> GrXPFactory::MakeXferProcessor(const GrXPFactory* factory,
188 const GrProcessorAnalysisColor& color,
189 GrProcessorAnalysisCoverage coverage,
190 bool hasMixedSamples,
Brian Osman532b3f92018-07-11 10:02:07 -0400191 const GrCaps& caps) {
cdalton86ae0a92015-06-08 15:11:04 -0700192 SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport());
Brian Salomona076d872017-04-04 15:17:03 -0400193 if (factory) {
Brian Osman532b3f92018-07-11 10:02:07 -0400194 return factory->makeXferProcessor(color, coverage, hasMixedSamples, caps);
Brian Salomona076d872017-04-04 15:17:03 -0400195 } else {
196 return GrPorterDuffXPFactory::MakeSrcOverXferProcessor(color, coverage, hasMixedSamples,
197 caps);
198 }
bsalomon50785a32015-02-06 07:02:37 -0800199}