bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrXferProcessor.h" |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrCaps.h" |
| 11 | #include "src/gpu/GrPipeline.h" |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 12 | |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 13 | GrXferProcessor::GrXferProcessor(ClassID classID) |
| 14 | : INHERITED(classID) |
| 15 | , fWillReadDstColor(false) |
Greg Daniel | 6ebe4b9 | 2017-05-19 10:56:46 -0400 | [diff] [blame] | 16 | , fDstReadUsesMixedSamples(false) |
| 17 | , fIsLCD(false) {} |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 18 | |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 19 | GrXferProcessor::GrXferProcessor(ClassID classID, bool willReadDstColor, bool hasMixedSamples, |
Greg Daniel | 6ebe4b9 | 2017-05-19 10:56:46 -0400 | [diff] [blame] | 20 | GrProcessorAnalysisCoverage coverage) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 21 | : INHERITED(classID) |
| 22 | , fWillReadDstColor(willReadDstColor) |
Greg Daniel | 6ebe4b9 | 2017-05-19 10:56:46 -0400 | [diff] [blame] | 23 | , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples) |
| 24 | , fIsLCD(GrProcessorAnalysisCoverage::kLCD == coverage) {} |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 25 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 26 | bool GrXferProcessor::hasSecondaryOutput() const { |
| 27 | if (!this->willReadDstColor()) { |
| 28 | return this->onHasSecondaryOutput(); |
| 29 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 30 | return this->dstReadUsesMixedSamples(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 33 | void GrXferProcessor::getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b, |
Greg Daniel | 70dd6a9 | 2020-10-09 10:09:06 -0400 | [diff] [blame] | 34 | const GrSurfaceOrigin* originIfDstTexture, |
| 35 | GrDstSampleType dstSampleType) const { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 36 | uint32_t key = this->willReadDstColor() ? 0x1 : 0x0; |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 37 | if (key) { |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 38 | if (originIfDstTexture) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 39 | key |= 0x2; |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 40 | if (kTopLeft_GrSurfaceOrigin == *originIfDstTexture) { |
cdalton | 827bae1 | 2015-06-08 13:43:33 -0700 | [diff] [blame] | 41 | key |= 0x4; |
| 42 | } |
Greg Daniel | 70dd6a9 | 2020-10-09 10:09:06 -0400 | [diff] [blame] | 43 | // 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 | } |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 48 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 49 | if (this->dstReadUsesMixedSamples()) { |
Greg Daniel | 70dd6a9 | 2020-10-09 10:09:06 -0400 | [diff] [blame] | 50 | key |= 0x10; |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 51 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 52 | } |
Greg Daniel | 6ebe4b9 | 2017-05-19 10:56:46 -0400 | [diff] [blame] | 53 | if (fIsLCD) { |
Greg Daniel | 70dd6a9 | 2020-10-09 10:09:06 -0400 | [diff] [blame] | 54 | key |= 0x20; |
Greg Daniel | 6ebe4b9 | 2017-05-19 10:56:46 -0400 | [diff] [blame] | 55 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 56 | b->add32(key); |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 57 | this->onGetGLSLProcessorKey(caps, b); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 58 | } |
| 59 | |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 60 | #ifdef SK_DEBUG |
| 61 | static 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 Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 99 | case kIllegal_GrBlendEquation: |
| 100 | SkASSERT(false); |
| 101 | return "<illegal>"; |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 102 | } |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 103 | return ""; |
| 104 | } |
| 105 | |
| 106 | static 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"; |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 132 | 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 Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 140 | case kIllegal_GrBlendCoeff: |
| 141 | SkASSERT(false); |
| 142 | return "<illegal>"; |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 143 | } |
| 144 | return ""; |
| 145 | } |
| 146 | |
| 147 | SkString 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 Osman | 422f95b | 2018-11-05 16:49:04 -0500 | [diff] [blame] | 151 | coeff_string(fDstBlend), fBlendConstant.toBytes_RGBA()); |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 152 | return out; |
| 153 | } |
| 154 | #endif |
| 155 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 156 | /////////////////////////////////////////////////////////////////////////////// |
| 157 | |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 158 | GrXPFactory::AnalysisProperties GrXPFactory::GetAnalysisProperties( |
| 159 | const GrXPFactory* factory, |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 160 | const GrProcessorAnalysisColor& color, |
| 161 | const GrProcessorAnalysisCoverage& coverage, |
Chris Dalton | 2833ec6 | 2020-12-03 01:43:08 -0700 | [diff] [blame] | 162 | bool hasMixedSamples, |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 163 | const GrCaps& caps, |
| 164 | GrClampType clampType) { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 165 | AnalysisProperties result; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 166 | if (factory) { |
Chris Dalton | 2833ec6 | 2020-12-03 01:43:08 -0700 | [diff] [blame] | 167 | result = factory->analysisProperties(color, coverage, hasMixedSamples, caps, clampType); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 168 | } else { |
Chris Dalton | 2833ec6 | 2020-12-03 01:43:08 -0700 | [diff] [blame] | 169 | result = GrPorterDuffXPFactory::SrcOverAnalysisProperties(color, coverage, hasMixedSamples, |
| 170 | caps, clampType); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 171 | } |
Greg Daniel | c529681 | 2020-02-28 13:06:02 -0500 | [diff] [blame] | 172 | if (coverage == GrProcessorAnalysisCoverage::kNone) { |
| 173 | result |= AnalysisProperties::kCompatibleWithCoverageAsAlpha; |
| 174 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 175 | SkASSERT(!(result & AnalysisProperties::kRequiresDstTexture)); |
| 176 | if ((result & AnalysisProperties::kReadsDstInShader) && |
| 177 | !caps.shaderCaps()->dstReadInShaderSupport()) { |
Chris Dalton | 945ee65 | 2019-01-23 09:10:36 -0700 | [diff] [blame] | 178 | result |= AnalysisProperties::kRequiresDstTexture | |
| 179 | AnalysisProperties::kRequiresNonOverlappingDraws; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 180 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 181 | return result; |
Brian Salomon | 9a51498 | 2017-02-14 10:28:22 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 184 | sk_sp<const GrXferProcessor> GrXPFactory::MakeXferProcessor(const GrXPFactory* factory, |
| 185 | const GrProcessorAnalysisColor& color, |
| 186 | GrProcessorAnalysisCoverage coverage, |
| 187 | bool hasMixedSamples, |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 188 | const GrCaps& caps, |
| 189 | GrClampType clampType) { |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 190 | SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport()); |
Greg Daniel | f6d60d3 | 2020-01-08 13:39:16 -0500 | [diff] [blame] | 191 | |
Brian Salomon | a076d87 | 2017-04-04 15:17:03 -0400 | [diff] [blame] | 192 | if (factory) { |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 193 | return factory->makeXferProcessor(color, coverage, hasMixedSamples, caps, clampType); |
Brian Salomon | a076d87 | 2017-04-04 15:17:03 -0400 | [diff] [blame] | 194 | } else { |
| 195 | return GrPorterDuffXPFactory::MakeSrcOverXferProcessor(color, coverage, hasMixedSamples, |
| 196 | caps); |
| 197 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 198 | } |