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 | |
| 8 | #include "GrXferProcessor.h" |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 9 | #include "GrPipeline.h" |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 10 | #include "GrProcOptInfo.h" |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 11 | #include "gl/GrGLCaps.h" |
| 12 | |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 13 | GrXferProcessor::GrXferProcessor() |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 14 | : fWillReadDstColor(false) |
| 15 | , fDstReadUsesMixedSamples(false) |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 16 | , fDstTextureOffset() { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 17 | } |
| 18 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 19 | GrXferProcessor::GrXferProcessor(const DstTexture* dstTexture, |
| 20 | bool willReadDstColor, |
| 21 | bool hasMixedSamples) |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 22 | : fWillReadDstColor(willReadDstColor) |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 23 | , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples) |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 24 | , fDstTextureOffset() { |
| 25 | if (dstTexture && dstTexture->texture()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 26 | SkASSERT(willReadDstColor); |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 27 | fDstTexture.reset(dstTexture->texture()); |
| 28 | fDstTextureOffset = dstTexture->offset(); |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 29 | this->addTextureSampler(&fDstTexture); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 33 | GrXferProcessor::OptFlags GrXferProcessor::getOptimizations(const GrPipelineAnalysis& analysis, |
| 34 | bool doesStencilWrite, |
| 35 | GrColor* overrideColor, |
| 36 | const GrCaps& caps) const { |
| 37 | GrXferProcessor::OptFlags flags = |
| 38 | this->onGetOptimizations(analysis, doesStencilWrite, overrideColor, caps); |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 39 | return flags; |
| 40 | } |
| 41 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 42 | bool GrXferProcessor::hasSecondaryOutput() const { |
| 43 | if (!this->willReadDstColor()) { |
| 44 | return this->onHasSecondaryOutput(); |
| 45 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 46 | return this->dstReadUsesMixedSamples(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const { |
| 50 | blendInfo->reset(); |
| 51 | if (!this->willReadDstColor()) { |
| 52 | this->onGetBlendInfo(blendInfo); |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 53 | } else if (this->dstReadUsesMixedSamples()) { |
| 54 | blendInfo->fDstBlend = kIS2A_GrBlendCoeff; |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 58 | void GrXferProcessor::getGLSLProcessorKey(const GrShaderCaps& caps, |
| 59 | GrProcessorKeyBuilder* b) const { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 60 | uint32_t key = this->willReadDstColor() ? 0x1 : 0x0; |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 61 | if (key) { |
cdalton | 827bae1 | 2015-06-08 13:43:33 -0700 | [diff] [blame] | 62 | if (const GrTexture* dstTexture = this->getDstTexture()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 63 | key |= 0x2; |
cdalton | 827bae1 | 2015-06-08 13:43:33 -0700 | [diff] [blame] | 64 | if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) { |
| 65 | key |= 0x4; |
| 66 | } |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 67 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 68 | if (this->dstReadUsesMixedSamples()) { |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 69 | key |= 0x8; |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 70 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 71 | } |
| 72 | b->add32(key); |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 73 | this->onGetGLSLProcessorKey(caps, b); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 74 | } |
| 75 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 76 | GrXferBarrierType GrXferProcessor::xferBarrierType(const GrRenderTarget* rt, |
| 77 | const GrCaps& caps) const { |
| 78 | SkASSERT(rt); |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 79 | if (static_cast<const GrSurface*>(rt) == this->getDstTexture()) { |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 80 | // Texture barriers are required when a shader reads and renders to the same texture. |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 81 | SkASSERT(caps.textureBarrierSupport()); |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 82 | return kTexture_GrXferBarrierType; |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 83 | } |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 84 | return this->onXferBarrier(rt, caps); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 87 | #ifdef SK_DEBUG |
| 88 | static const char* equation_string(GrBlendEquation eq) { |
| 89 | switch (eq) { |
| 90 | case kAdd_GrBlendEquation: |
| 91 | return "add"; |
| 92 | case kSubtract_GrBlendEquation: |
| 93 | return "subtract"; |
| 94 | case kReverseSubtract_GrBlendEquation: |
| 95 | return "reverse_subtract"; |
| 96 | case kScreen_GrBlendEquation: |
| 97 | return "screen"; |
| 98 | case kOverlay_GrBlendEquation: |
| 99 | return "overlay"; |
| 100 | case kDarken_GrBlendEquation: |
| 101 | return "darken"; |
| 102 | case kLighten_GrBlendEquation: |
| 103 | return "lighten"; |
| 104 | case kColorDodge_GrBlendEquation: |
| 105 | return "color_dodge"; |
| 106 | case kColorBurn_GrBlendEquation: |
| 107 | return "color_burn"; |
| 108 | case kHardLight_GrBlendEquation: |
| 109 | return "hard_light"; |
| 110 | case kSoftLight_GrBlendEquation: |
| 111 | return "soft_light"; |
| 112 | case kDifference_GrBlendEquation: |
| 113 | return "difference"; |
| 114 | case kExclusion_GrBlendEquation: |
| 115 | return "exclusion"; |
| 116 | case kMultiply_GrBlendEquation: |
| 117 | return "multiply"; |
| 118 | case kHSLHue_GrBlendEquation: |
| 119 | return "hsl_hue"; |
| 120 | case kHSLSaturation_GrBlendEquation: |
| 121 | return "hsl_saturation"; |
| 122 | case kHSLColor_GrBlendEquation: |
| 123 | return "hsl_color"; |
| 124 | case kHSLLuminosity_GrBlendEquation: |
| 125 | return "hsl_luminosity"; |
| 126 | }; |
| 127 | return ""; |
| 128 | } |
| 129 | |
| 130 | static const char* coeff_string(GrBlendCoeff coeff) { |
| 131 | switch (coeff) { |
| 132 | case kZero_GrBlendCoeff: |
| 133 | return "zero"; |
| 134 | case kOne_GrBlendCoeff: |
| 135 | return "one"; |
| 136 | case kSC_GrBlendCoeff: |
| 137 | return "src_color"; |
| 138 | case kISC_GrBlendCoeff: |
| 139 | return "inv_src_color"; |
| 140 | case kDC_GrBlendCoeff: |
| 141 | return "dst_color"; |
| 142 | case kIDC_GrBlendCoeff: |
| 143 | return "inv_dst_color"; |
| 144 | case kSA_GrBlendCoeff: |
| 145 | return "src_alpha"; |
| 146 | case kISA_GrBlendCoeff: |
| 147 | return "inv_src_alpha"; |
| 148 | case kDA_GrBlendCoeff: |
| 149 | return "dst_alpha"; |
| 150 | case kIDA_GrBlendCoeff: |
| 151 | return "inv_dst_alpha"; |
| 152 | case kConstC_GrBlendCoeff: |
| 153 | return "const_color"; |
| 154 | case kIConstC_GrBlendCoeff: |
| 155 | return "inv_const_color"; |
| 156 | case kConstA_GrBlendCoeff: |
| 157 | return "const_alpha"; |
| 158 | case kIConstA_GrBlendCoeff: |
| 159 | return "inv_const_alpha"; |
| 160 | case kS2C_GrBlendCoeff: |
| 161 | return "src2_color"; |
| 162 | case kIS2C_GrBlendCoeff: |
| 163 | return "inv_src2_color"; |
| 164 | case kS2A_GrBlendCoeff: |
| 165 | return "src2_alpha"; |
| 166 | case kIS2A_GrBlendCoeff: |
| 167 | return "inv_src2_alpha"; |
| 168 | } |
| 169 | return ""; |
| 170 | } |
| 171 | |
| 172 | SkString GrXferProcessor::BlendInfo::dump() const { |
| 173 | SkString out; |
| 174 | out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)", |
| 175 | fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend), |
| 176 | coeff_string(fDstBlend), fBlendConstant); |
| 177 | return out; |
| 178 | } |
| 179 | #endif |
| 180 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 181 | /////////////////////////////////////////////////////////////////////////////// |
| 182 | |
Brian Salomon | 5be6c95 | 2017-01-20 19:06:29 +0000 | [diff] [blame] | 183 | using ColorType = GrXPFactory::ColorType; |
| 184 | using CoverageType = GrXPFactory::CoverageType; |
| 185 | |
| 186 | ColorType analysis_color_type(const GrPipelineAnalysis& analysis) { |
| 187 | if (analysis.fColorPOI.validFlags() == kRGBA_GrColorComponentFlags) { |
| 188 | return GrColorIsOpaque(analysis.fColorPOI.color()) ? ColorType::kOpaqueConstant |
| 189 | : ColorType::kConstant; |
| 190 | } |
| 191 | if ((analysis.fColorPOI.validFlags() & kA_GrColorComponentFlag) && |
| 192 | GrColorIsOpaque(analysis.fColorPOI.color())) { |
| 193 | return ColorType::kOpaque; |
| 194 | } |
| 195 | return ColorType::kUnknown; |
| 196 | } |
| 197 | |
| 198 | CoverageType analysis_coverage_type(const GrPipelineAnalysis& analysis) { |
| 199 | if (analysis.fCoveragePOI.isSolidWhite()) { |
| 200 | return CoverageType::kNone; |
| 201 | } |
| 202 | if (analysis.fCoveragePOI.isLCDCoverage()) { |
| 203 | return CoverageType::kLCD; |
| 204 | } |
| 205 | return CoverageType::kSingleChannel; |
| 206 | } |
| 207 | |
| 208 | bool GrXPFactory::willReadDstColor(const GrCaps& caps, const GrPipelineAnalysis& analysis) const { |
| 209 | if (analysis.fUsesPLSDstRead) { |
| 210 | return true; |
| 211 | } |
| 212 | ColorType colorType = analysis_color_type(analysis); |
| 213 | CoverageType coverageType = analysis_coverage_type(analysis); |
| 214 | return this->willReadDstColor(caps, colorType, coverageType); |
| 215 | } |
| 216 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 217 | GrXferProcessor* GrXPFactory::createXferProcessor(const GrPipelineAnalysis& analysis, |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 218 | bool hasMixedSamples, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 219 | const DstTexture* dstTexture, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 220 | const GrCaps& caps) const { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 221 | #ifdef SK_DEBUG |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 222 | if (this->willReadDstColor(caps, analysis)) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 223 | if (!caps.shaderCaps()->dstReadInShaderSupport()) { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 224 | SkASSERT(dstTexture && dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 225 | } else { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 226 | SkASSERT(!dstTexture || !dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 227 | } |
| 228 | } else { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 229 | SkASSERT(!dstTexture || !dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 230 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 231 | SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 232 | #endif |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 233 | return this->onCreateXferProcessor(caps, analysis, hasMixedSamples, dstTexture); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 236 | bool GrXPFactory::willNeedDstTexture(const GrCaps& caps, const GrPipelineAnalysis& analysis) const { |
Brian Salomon | 5be6c95 | 2017-01-20 19:06:29 +0000 | [diff] [blame] | 237 | return !analysis.fUsesPLSDstRead && !caps.shaderCaps()->dstReadInShaderSupport() && |
| 238 | this->willReadDstColor(caps, analysis); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 239 | } |