blob: e65f4fab67f95ab2cb224d5d5e197a7ac824c574 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrXferProcessor.h"
Robert Phillipsbe77a022018-04-03 17:17:05 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrCaps.h"
11#include "src/gpu/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
Brian Salomon18dfa982017-04-03 16:57:43 -040033void GrXferProcessor::getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b,
Greg Daniel70dd6a92020-10-09 10:09:06 -040034 const GrSurfaceOrigin* originIfDstTexture,
35 GrDstSampleType dstSampleType) const {
bsalomon50785a32015-02-06 07:02:37 -080036 uint32_t key = this->willReadDstColor() ? 0x1 : 0x0;
cdaltonedbb31f2015-06-08 12:14:44 -070037 if (key) {
Brian Salomon18dfa982017-04-03 16:57:43 -040038 if (originIfDstTexture) {
cdaltonedbb31f2015-06-08 12:14:44 -070039 key |= 0x2;
Brian Salomon18dfa982017-04-03 16:57:43 -040040 if (kTopLeft_GrSurfaceOrigin == *originIfDstTexture) {
cdalton827bae12015-06-08 13:43:33 -070041 key |= 0x4;
42 }
Greg Daniel70dd6a92020-10-09 10:09:06 -040043 // We don't just add the whole dstSampleType to the key because sampling a copy or the
44 // rt directly produces the same shader code.
45 if (dstSampleType == GrDstSampleType::kAsInputAttachment) {
46 key |= 0x8;
47 }
cdaltonedbb31f2015-06-08 12:14:44 -070048 }
cdalton86ae0a92015-06-08 15:11:04 -070049 if (this->dstReadUsesMixedSamples()) {
Greg Daniel70dd6a92020-10-09 10:09:06 -040050 key |= 0x10;
cdalton86ae0a92015-06-08 15:11:04 -070051 }
bsalomon50785a32015-02-06 07:02:37 -080052 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -040053 if (fIsLCD) {
Greg Daniel70dd6a92020-10-09 10:09:06 -040054 key |= 0x20;
Greg Daniel6ebe4b92017-05-19 10:56:46 -040055 }
bsalomon50785a32015-02-06 07:02:37 -080056 b->add32(key);
egdaniel57d3b032015-11-13 11:57:27 -080057 this->onGetGLSLProcessorKey(caps, b);
bsalomon50785a32015-02-06 07:02:37 -080058}
59
bsalomonf7cc8772015-05-11 11:21:14 -070060#ifdef SK_DEBUG
61static const char* equation_string(GrBlendEquation eq) {
62 switch (eq) {
63 case kAdd_GrBlendEquation:
64 return "add";
65 case kSubtract_GrBlendEquation:
66 return "subtract";
67 case kReverseSubtract_GrBlendEquation:
68 return "reverse_subtract";
69 case kScreen_GrBlendEquation:
70 return "screen";
71 case kOverlay_GrBlendEquation:
72 return "overlay";
73 case kDarken_GrBlendEquation:
74 return "darken";
75 case kLighten_GrBlendEquation:
76 return "lighten";
77 case kColorDodge_GrBlendEquation:
78 return "color_dodge";
79 case kColorBurn_GrBlendEquation:
80 return "color_burn";
81 case kHardLight_GrBlendEquation:
82 return "hard_light";
83 case kSoftLight_GrBlendEquation:
84 return "soft_light";
85 case kDifference_GrBlendEquation:
86 return "difference";
87 case kExclusion_GrBlendEquation:
88 return "exclusion";
89 case kMultiply_GrBlendEquation:
90 return "multiply";
91 case kHSLHue_GrBlendEquation:
92 return "hsl_hue";
93 case kHSLSaturation_GrBlendEquation:
94 return "hsl_saturation";
95 case kHSLColor_GrBlendEquation:
96 return "hsl_color";
97 case kHSLLuminosity_GrBlendEquation:
98 return "hsl_luminosity";
Mike Klein36743362018-11-06 08:23:30 -050099 case kIllegal_GrBlendEquation:
100 SkASSERT(false);
101 return "<illegal>";
Brian Salomon23356442018-11-30 15:33:19 -0500102 }
bsalomonf7cc8772015-05-11 11:21:14 -0700103 return "";
104}
105
106static const char* coeff_string(GrBlendCoeff coeff) {
107 switch (coeff) {
108 case kZero_GrBlendCoeff:
109 return "zero";
110 case kOne_GrBlendCoeff:
111 return "one";
112 case kSC_GrBlendCoeff:
113 return "src_color";
114 case kISC_GrBlendCoeff:
115 return "inv_src_color";
116 case kDC_GrBlendCoeff:
117 return "dst_color";
118 case kIDC_GrBlendCoeff:
119 return "inv_dst_color";
120 case kSA_GrBlendCoeff:
121 return "src_alpha";
122 case kISA_GrBlendCoeff:
123 return "inv_src_alpha";
124 case kDA_GrBlendCoeff:
125 return "dst_alpha";
126 case kIDA_GrBlendCoeff:
127 return "inv_dst_alpha";
128 case kConstC_GrBlendCoeff:
129 return "const_color";
130 case kIConstC_GrBlendCoeff:
131 return "inv_const_color";
bsalomonf7cc8772015-05-11 11:21:14 -0700132 case kS2C_GrBlendCoeff:
133 return "src2_color";
134 case kIS2C_GrBlendCoeff:
135 return "inv_src2_color";
136 case kS2A_GrBlendCoeff:
137 return "src2_alpha";
138 case kIS2A_GrBlendCoeff:
139 return "inv_src2_alpha";
Mike Klein36743362018-11-06 08:23:30 -0500140 case kIllegal_GrBlendCoeff:
141 SkASSERT(false);
142 return "<illegal>";
bsalomonf7cc8772015-05-11 11:21:14 -0700143 }
144 return "";
145}
146
147SkString GrXferProcessor::BlendInfo::dump() const {
148 SkString out;
149 out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)",
150 fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend),
Brian Osman422f95b2018-11-05 16:49:04 -0500151 coeff_string(fDstBlend), fBlendConstant.toBytes_RGBA());
bsalomonf7cc8772015-05-11 11:21:14 -0700152 return out;
153}
154#endif
155
bsalomon50785a32015-02-06 07:02:37 -0800156///////////////////////////////////////////////////////////////////////////////
157
Brian Salomon31853842017-03-28 16:32:05 -0400158GrXPFactory::AnalysisProperties GrXPFactory::GetAnalysisProperties(
159 const GrXPFactory* factory,
Brian Salomona811b122017-03-30 08:21:32 -0400160 const GrProcessorAnalysisColor& color,
161 const GrProcessorAnalysisCoverage& coverage,
Chris Dalton2833ec62020-12-03 01:43:08 -0700162 bool hasMixedSamples,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400163 const GrCaps& caps,
164 GrClampType clampType) {
Brian Salomon31853842017-03-28 16:32:05 -0400165 AnalysisProperties result;
Brian Salomon5298dc82017-02-22 11:52:03 -0500166 if (factory) {
Chris Dalton2833ec62020-12-03 01:43:08 -0700167 result = factory->analysisProperties(color, coverage, hasMixedSamples, caps, clampType);
Brian Salomon5298dc82017-02-22 11:52:03 -0500168 } else {
Chris Dalton2833ec62020-12-03 01:43:08 -0700169 result = GrPorterDuffXPFactory::SrcOverAnalysisProperties(color, coverage, hasMixedSamples,
170 caps, clampType);
Brian Salomon31853842017-03-28 16:32:05 -0400171 }
Greg Danielc5296812020-02-28 13:06:02 -0500172 if (coverage == GrProcessorAnalysisCoverage::kNone) {
173 result |= AnalysisProperties::kCompatibleWithCoverageAsAlpha;
174 }
Brian Salomon31853842017-03-28 16:32:05 -0400175 SkASSERT(!(result & AnalysisProperties::kRequiresDstTexture));
176 if ((result & AnalysisProperties::kReadsDstInShader) &&
177 !caps.shaderCaps()->dstReadInShaderSupport()) {
Chris Dalton945ee652019-01-23 09:10:36 -0700178 result |= AnalysisProperties::kRequiresDstTexture |
179 AnalysisProperties::kRequiresNonOverlappingDraws;
Brian Salomon5298dc82017-02-22 11:52:03 -0500180 }
Brian Salomon5298dc82017-02-22 11:52:03 -0500181 return result;
Brian Salomon9a514982017-02-14 10:28:22 -0500182}
183
Brian Salomond61c9d92017-04-10 10:54:25 -0400184sk_sp<const GrXferProcessor> GrXPFactory::MakeXferProcessor(const GrXPFactory* factory,
185 const GrProcessorAnalysisColor& color,
186 GrProcessorAnalysisCoverage coverage,
187 bool hasMixedSamples,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400188 const GrCaps& caps,
189 GrClampType clampType) {
cdalton86ae0a92015-06-08 15:11:04 -0700190 SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport());
Greg Danielf6d60d32020-01-08 13:39:16 -0500191
Brian Salomona076d872017-04-04 15:17:03 -0400192 if (factory) {
Brian Osman5ced0bf2019-03-15 10:15:29 -0400193 return factory->makeXferProcessor(color, coverage, hasMixedSamples, caps, clampType);
Brian Salomona076d872017-04-04 15:17:03 -0400194 } else {
195 return GrPorterDuffXPFactory::MakeSrcOverXferProcessor(color, coverage, hasMixedSamples,
196 caps);
197 }
bsalomon50785a32015-02-06 07:02:37 -0800198}