blob: 2921ae08583dd33bca6544eb145862983abb9172 [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 , fIsLCD(false) {}
bsalomon50785a32015-02-06 07:02:37 -080017
Chris Dalton57ab06c2021-04-22 12:57:28 -060018GrXferProcessor::GrXferProcessor(ClassID classID, bool willReadDstColor,
Greg Daniel6ebe4b92017-05-19 10:56:46 -040019 GrProcessorAnalysisCoverage coverage)
Ethan Nicholasabff9562017-10-09 10:54:08 -040020 : INHERITED(classID)
21 , fWillReadDstColor(willReadDstColor)
Greg Daniel6ebe4b92017-05-19 10:56:46 -040022 , fIsLCD(GrProcessorAnalysisCoverage::kLCD == coverage) {}
bsalomon50785a32015-02-06 07:02:37 -080023
cdaltonedbb31f2015-06-08 12:14:44 -070024bool GrXferProcessor::hasSecondaryOutput() const {
25 if (!this->willReadDstColor()) {
26 return this->onHasSecondaryOutput();
27 }
Chris Dalton57ab06c2021-04-22 12:57:28 -060028 return false;
cdaltonedbb31f2015-06-08 12:14:44 -070029}
30
Brian Salomon18dfa982017-04-03 16:57:43 -040031void GrXferProcessor::getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b,
Greg Daniel70dd6a92020-10-09 10:09:06 -040032 const GrSurfaceOrigin* originIfDstTexture,
33 GrDstSampleType dstSampleType) const {
bsalomon50785a32015-02-06 07:02:37 -080034 uint32_t key = this->willReadDstColor() ? 0x1 : 0x0;
cdaltonedbb31f2015-06-08 12:14:44 -070035 if (key) {
Brian Salomon18dfa982017-04-03 16:57:43 -040036 if (originIfDstTexture) {
cdaltonedbb31f2015-06-08 12:14:44 -070037 key |= 0x2;
Brian Salomon18dfa982017-04-03 16:57:43 -040038 if (kTopLeft_GrSurfaceOrigin == *originIfDstTexture) {
cdalton827bae12015-06-08 13:43:33 -070039 key |= 0x4;
40 }
Greg Daniel70dd6a92020-10-09 10:09:06 -040041 // We don't just add the whole dstSampleType to the key because sampling a copy or the
42 // rt directly produces the same shader code.
43 if (dstSampleType == GrDstSampleType::kAsInputAttachment) {
44 key |= 0x8;
45 }
cdaltonedbb31f2015-06-08 12:14:44 -070046 }
bsalomon50785a32015-02-06 07:02:37 -080047 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -040048 if (fIsLCD) {
Chris Dalton57ab06c2021-04-22 12:57:28 -060049 key |= 0x10;
Greg Daniel6ebe4b92017-05-19 10:56:46 -040050 }
bsalomon50785a32015-02-06 07:02:37 -080051 b->add32(key);
egdaniel57d3b032015-11-13 11:57:27 -080052 this->onGetGLSLProcessorKey(caps, b);
bsalomon50785a32015-02-06 07:02:37 -080053}
54
bsalomonf7cc8772015-05-11 11:21:14 -070055#ifdef SK_DEBUG
56static const char* equation_string(GrBlendEquation eq) {
57 switch (eq) {
58 case kAdd_GrBlendEquation:
59 return "add";
60 case kSubtract_GrBlendEquation:
61 return "subtract";
62 case kReverseSubtract_GrBlendEquation:
63 return "reverse_subtract";
64 case kScreen_GrBlendEquation:
65 return "screen";
66 case kOverlay_GrBlendEquation:
67 return "overlay";
68 case kDarken_GrBlendEquation:
69 return "darken";
70 case kLighten_GrBlendEquation:
71 return "lighten";
72 case kColorDodge_GrBlendEquation:
73 return "color_dodge";
74 case kColorBurn_GrBlendEquation:
75 return "color_burn";
76 case kHardLight_GrBlendEquation:
77 return "hard_light";
78 case kSoftLight_GrBlendEquation:
79 return "soft_light";
80 case kDifference_GrBlendEquation:
81 return "difference";
82 case kExclusion_GrBlendEquation:
83 return "exclusion";
84 case kMultiply_GrBlendEquation:
85 return "multiply";
86 case kHSLHue_GrBlendEquation:
87 return "hsl_hue";
88 case kHSLSaturation_GrBlendEquation:
89 return "hsl_saturation";
90 case kHSLColor_GrBlendEquation:
91 return "hsl_color";
92 case kHSLLuminosity_GrBlendEquation:
93 return "hsl_luminosity";
Mike Klein36743362018-11-06 08:23:30 -050094 case kIllegal_GrBlendEquation:
95 SkASSERT(false);
96 return "<illegal>";
Brian Salomon23356442018-11-30 15:33:19 -050097 }
bsalomonf7cc8772015-05-11 11:21:14 -070098 return "";
99}
100
101static const char* coeff_string(GrBlendCoeff coeff) {
102 switch (coeff) {
103 case kZero_GrBlendCoeff:
104 return "zero";
105 case kOne_GrBlendCoeff:
106 return "one";
107 case kSC_GrBlendCoeff:
108 return "src_color";
109 case kISC_GrBlendCoeff:
110 return "inv_src_color";
111 case kDC_GrBlendCoeff:
112 return "dst_color";
113 case kIDC_GrBlendCoeff:
114 return "inv_dst_color";
115 case kSA_GrBlendCoeff:
116 return "src_alpha";
117 case kISA_GrBlendCoeff:
118 return "inv_src_alpha";
119 case kDA_GrBlendCoeff:
120 return "dst_alpha";
121 case kIDA_GrBlendCoeff:
122 return "inv_dst_alpha";
123 case kConstC_GrBlendCoeff:
124 return "const_color";
125 case kIConstC_GrBlendCoeff:
126 return "inv_const_color";
bsalomonf7cc8772015-05-11 11:21:14 -0700127 case kS2C_GrBlendCoeff:
128 return "src2_color";
129 case kIS2C_GrBlendCoeff:
130 return "inv_src2_color";
131 case kS2A_GrBlendCoeff:
132 return "src2_alpha";
133 case kIS2A_GrBlendCoeff:
134 return "inv_src2_alpha";
Mike Klein36743362018-11-06 08:23:30 -0500135 case kIllegal_GrBlendCoeff:
136 SkASSERT(false);
137 return "<illegal>";
bsalomonf7cc8772015-05-11 11:21:14 -0700138 }
139 return "";
140}
141
142SkString GrXferProcessor::BlendInfo::dump() const {
143 SkString out;
144 out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)",
145 fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend),
Brian Osman422f95b2018-11-05 16:49:04 -0500146 coeff_string(fDstBlend), fBlendConstant.toBytes_RGBA());
bsalomonf7cc8772015-05-11 11:21:14 -0700147 return out;
148}
149#endif
150
bsalomon50785a32015-02-06 07:02:37 -0800151///////////////////////////////////////////////////////////////////////////////
152
Brian Salomon31853842017-03-28 16:32:05 -0400153GrXPFactory::AnalysisProperties GrXPFactory::GetAnalysisProperties(
154 const GrXPFactory* factory,
Brian Salomona811b122017-03-30 08:21:32 -0400155 const GrProcessorAnalysisColor& color,
156 const GrProcessorAnalysisCoverage& coverage,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400157 const GrCaps& caps,
158 GrClampType clampType) {
Brian Salomon31853842017-03-28 16:32:05 -0400159 AnalysisProperties result;
Brian Salomon5298dc82017-02-22 11:52:03 -0500160 if (factory) {
Chris Dalton57ab06c2021-04-22 12:57:28 -0600161 result = factory->analysisProperties(color, coverage, caps, clampType);
Brian Salomon5298dc82017-02-22 11:52:03 -0500162 } else {
Chris Dalton57ab06c2021-04-22 12:57:28 -0600163 result = GrPorterDuffXPFactory::SrcOverAnalysisProperties(color, coverage, caps, clampType);
Brian Salomon31853842017-03-28 16:32:05 -0400164 }
Greg Danielc5296812020-02-28 13:06:02 -0500165 if (coverage == GrProcessorAnalysisCoverage::kNone) {
166 result |= AnalysisProperties::kCompatibleWithCoverageAsAlpha;
167 }
Brian Salomon31853842017-03-28 16:32:05 -0400168 SkASSERT(!(result & AnalysisProperties::kRequiresDstTexture));
169 if ((result & AnalysisProperties::kReadsDstInShader) &&
170 !caps.shaderCaps()->dstReadInShaderSupport()) {
Chris Dalton945ee652019-01-23 09:10:36 -0700171 result |= AnalysisProperties::kRequiresDstTexture |
172 AnalysisProperties::kRequiresNonOverlappingDraws;
Brian Salomon5298dc82017-02-22 11:52:03 -0500173 }
Brian Salomon5298dc82017-02-22 11:52:03 -0500174 return result;
Brian Salomon9a514982017-02-14 10:28:22 -0500175}
176
Brian Salomond61c9d92017-04-10 10:54:25 -0400177sk_sp<const GrXferProcessor> GrXPFactory::MakeXferProcessor(const GrXPFactory* factory,
178 const GrProcessorAnalysisColor& color,
179 GrProcessorAnalysisCoverage coverage,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400180 const GrCaps& caps,
181 GrClampType clampType) {
Brian Salomona076d872017-04-04 15:17:03 -0400182 if (factory) {
Chris Dalton57ab06c2021-04-22 12:57:28 -0600183 return factory->makeXferProcessor(color, coverage, caps, clampType);
Brian Salomona076d872017-04-04 15:17:03 -0400184 } else {
Chris Dalton57ab06c2021-04-22 12:57:28 -0600185 return GrPorterDuffXPFactory::MakeSrcOverXferProcessor(color, coverage, caps);
Brian Salomona076d872017-04-04 15:17:03 -0400186 }
bsalomon50785a32015-02-06 07:02:37 -0800187}