blob: 13533af3808b6e37b780fc7e162648ccefb0c414 [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"
ethannicholasde4166a2015-11-30 08:57:38 -08009#include "GrPipeline.h"
cdaltonedbb31f2015-06-08 12:14:44 -070010#include "GrProcOptInfo.h"
bsalomon50785a32015-02-06 07:02:37 -080011#include "gl/GrGLCaps.h"
12
egdanielc19cdc22015-05-10 08:45:18 -070013GrXferProcessor::GrXferProcessor()
cdalton86ae0a92015-06-08 15:11:04 -070014 : fWillReadDstColor(false)
15 , fDstReadUsesMixedSamples(false)
cdalton86ae0a92015-06-08 15:11:04 -070016 , fDstTextureOffset() {
bsalomon50785a32015-02-06 07:02:37 -080017}
18
cdalton86ae0a92015-06-08 15:11:04 -070019GrXferProcessor::GrXferProcessor(const DstTexture* dstTexture,
20 bool willReadDstColor,
21 bool hasMixedSamples)
bsalomon50785a32015-02-06 07:02:37 -080022 : fWillReadDstColor(willReadDstColor)
cdalton86ae0a92015-06-08 15:11:04 -070023 , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples)
bsalomon6a44c6a2015-05-26 09:49:05 -070024 , fDstTextureOffset() {
25 if (dstTexture && dstTexture->texture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070026 SkASSERT(willReadDstColor);
bsalomon6a44c6a2015-05-26 09:49:05 -070027 fDstTexture.reset(dstTexture->texture());
28 fDstTextureOffset = dstTexture->offset();
Brian Salomon0bbecb22016-11-17 11:38:22 -050029 this->addTextureSampler(&fDstTexture);
egdaniel060a52c2015-04-07 07:31:11 -070030 this->setWillReadFragmentPosition();
bsalomon50785a32015-02-06 07:02:37 -080031 }
32}
33
Brian Salomon92aee3d2016-12-21 09:20:25 -050034GrXferProcessor::OptFlags GrXferProcessor::getOptimizations(const GrPipelineAnalysis& analysis,
35 bool doesStencilWrite,
36 GrColor* overrideColor,
37 const GrCaps& caps) const {
38 GrXferProcessor::OptFlags flags =
39 this->onGetOptimizations(analysis, doesStencilWrite, overrideColor, caps);
egdanielc19cdc22015-05-10 08:45:18 -070040
cdaltonedbb31f2015-06-08 12:14:44 -070041 if (this->willReadDstColor()) {
42 // When performing a dst read we handle coverage in the base class.
43 SkASSERT(!(flags & GrXferProcessor::kIgnoreCoverage_OptFlag));
Brian Salomon92aee3d2016-12-21 09:20:25 -050044 if (analysis.fCoveragePOI.isSolidWhite()) {
cdaltonedbb31f2015-06-08 12:14:44 -070045 flags |= GrXferProcessor::kIgnoreCoverage_OptFlag;
46 }
47 }
egdanielc19cdc22015-05-10 08:45:18 -070048 return flags;
49}
50
cdaltonedbb31f2015-06-08 12:14:44 -070051bool GrXferProcessor::hasSecondaryOutput() const {
52 if (!this->willReadDstColor()) {
53 return this->onHasSecondaryOutput();
54 }
cdalton86ae0a92015-06-08 15:11:04 -070055 return this->dstReadUsesMixedSamples();
cdaltonedbb31f2015-06-08 12:14:44 -070056}
57
58void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const {
59 blendInfo->reset();
60 if (!this->willReadDstColor()) {
61 this->onGetBlendInfo(blendInfo);
cdalton86ae0a92015-06-08 15:11:04 -070062 } else if (this->dstReadUsesMixedSamples()) {
63 blendInfo->fDstBlend = kIS2A_GrBlendCoeff;
cdaltonedbb31f2015-06-08 12:14:44 -070064 }
65}
66
Brian Salomon94efbf52016-11-29 13:43:05 -050067void GrXferProcessor::getGLSLProcessorKey(const GrShaderCaps& caps,
68 GrProcessorKeyBuilder* b) const {
bsalomon50785a32015-02-06 07:02:37 -080069 uint32_t key = this->willReadDstColor() ? 0x1 : 0x0;
cdaltonedbb31f2015-06-08 12:14:44 -070070 if (key) {
cdalton827bae12015-06-08 13:43:33 -070071 if (const GrTexture* dstTexture = this->getDstTexture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070072 key |= 0x2;
cdalton827bae12015-06-08 13:43:33 -070073 if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) {
74 key |= 0x4;
75 }
cdaltonedbb31f2015-06-08 12:14:44 -070076 }
cdalton86ae0a92015-06-08 15:11:04 -070077 if (this->dstReadUsesMixedSamples()) {
egdaniel56cf6dc2015-11-30 10:15:58 -080078 key |= 0x8;
cdalton86ae0a92015-06-08 15:11:04 -070079 }
bsalomon50785a32015-02-06 07:02:37 -080080 }
81 b->add32(key);
egdaniel57d3b032015-11-13 11:57:27 -080082 this->onGetGLSLProcessorKey(caps, b);
bsalomon50785a32015-02-06 07:02:37 -080083}
84
bsalomoncb02b382015-08-12 11:14:50 -070085GrXferBarrierType GrXferProcessor::xferBarrierType(const GrRenderTarget* rt,
86 const GrCaps& caps) const {
87 SkASSERT(rt);
bsalomon6a44c6a2015-05-26 09:49:05 -070088 if (static_cast<const GrSurface*>(rt) == this->getDstTexture()) {
cdalton9954bc32015-04-29 14:17:00 -070089 // Texture barriers are required when a shader reads and renders to the same texture.
cdalton9954bc32015-04-29 14:17:00 -070090 SkASSERT(caps.textureBarrierSupport());
bsalomoncb02b382015-08-12 11:14:50 -070091 return kTexture_GrXferBarrierType;
cdalton9954bc32015-04-29 14:17:00 -070092 }
bsalomoncb02b382015-08-12 11:14:50 -070093 return this->onXferBarrier(rt, caps);
cdalton9954bc32015-04-29 14:17:00 -070094}
95
bsalomonf7cc8772015-05-11 11:21:14 -070096#ifdef SK_DEBUG
97static const char* equation_string(GrBlendEquation eq) {
98 switch (eq) {
99 case kAdd_GrBlendEquation:
100 return "add";
101 case kSubtract_GrBlendEquation:
102 return "subtract";
103 case kReverseSubtract_GrBlendEquation:
104 return "reverse_subtract";
105 case kScreen_GrBlendEquation:
106 return "screen";
107 case kOverlay_GrBlendEquation:
108 return "overlay";
109 case kDarken_GrBlendEquation:
110 return "darken";
111 case kLighten_GrBlendEquation:
112 return "lighten";
113 case kColorDodge_GrBlendEquation:
114 return "color_dodge";
115 case kColorBurn_GrBlendEquation:
116 return "color_burn";
117 case kHardLight_GrBlendEquation:
118 return "hard_light";
119 case kSoftLight_GrBlendEquation:
120 return "soft_light";
121 case kDifference_GrBlendEquation:
122 return "difference";
123 case kExclusion_GrBlendEquation:
124 return "exclusion";
125 case kMultiply_GrBlendEquation:
126 return "multiply";
127 case kHSLHue_GrBlendEquation:
128 return "hsl_hue";
129 case kHSLSaturation_GrBlendEquation:
130 return "hsl_saturation";
131 case kHSLColor_GrBlendEquation:
132 return "hsl_color";
133 case kHSLLuminosity_GrBlendEquation:
134 return "hsl_luminosity";
135 };
136 return "";
137}
138
139static const char* coeff_string(GrBlendCoeff coeff) {
140 switch (coeff) {
141 case kZero_GrBlendCoeff:
142 return "zero";
143 case kOne_GrBlendCoeff:
144 return "one";
145 case kSC_GrBlendCoeff:
146 return "src_color";
147 case kISC_GrBlendCoeff:
148 return "inv_src_color";
149 case kDC_GrBlendCoeff:
150 return "dst_color";
151 case kIDC_GrBlendCoeff:
152 return "inv_dst_color";
153 case kSA_GrBlendCoeff:
154 return "src_alpha";
155 case kISA_GrBlendCoeff:
156 return "inv_src_alpha";
157 case kDA_GrBlendCoeff:
158 return "dst_alpha";
159 case kIDA_GrBlendCoeff:
160 return "inv_dst_alpha";
161 case kConstC_GrBlendCoeff:
162 return "const_color";
163 case kIConstC_GrBlendCoeff:
164 return "inv_const_color";
165 case kConstA_GrBlendCoeff:
166 return "const_alpha";
167 case kIConstA_GrBlendCoeff:
168 return "inv_const_alpha";
169 case kS2C_GrBlendCoeff:
170 return "src2_color";
171 case kIS2C_GrBlendCoeff:
172 return "inv_src2_color";
173 case kS2A_GrBlendCoeff:
174 return "src2_alpha";
175 case kIS2A_GrBlendCoeff:
176 return "inv_src2_alpha";
177 }
178 return "";
179}
180
181SkString GrXferProcessor::BlendInfo::dump() const {
182 SkString out;
183 out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)",
184 fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend),
185 coeff_string(fDstBlend), fBlendConstant);
186 return out;
187}
188#endif
189
bsalomon50785a32015-02-06 07:02:37 -0800190///////////////////////////////////////////////////////////////////////////////
191
Brian Salomon92aee3d2016-12-21 09:20:25 -0500192GrXferProcessor* GrXPFactory::createXferProcessor(const GrPipelineAnalysis& analysis,
cdalton86ae0a92015-06-08 15:11:04 -0700193 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700194 const DstTexture* dstTexture,
bsalomon4b91f762015-05-19 09:29:46 -0700195 const GrCaps& caps) const {
bsalomon50785a32015-02-06 07:02:37 -0800196#ifdef SK_DEBUG
Brian Salomon92aee3d2016-12-21 09:20:25 -0500197 if (this->willReadDstColor(caps, analysis)) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700198 if (!caps.shaderCaps()->dstReadInShaderSupport()) {
bsalomon6a44c6a2015-05-26 09:49:05 -0700199 SkASSERT(dstTexture && dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800200 } else {
halcanary9d524f22016-03-29 09:03:52 -0700201 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800202 }
203 } else {
halcanary9d524f22016-03-29 09:03:52 -0700204 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800205 }
cdalton86ae0a92015-06-08 15:11:04 -0700206 SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport());
bsalomon50785a32015-02-06 07:02:37 -0800207#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500208 return this->onCreateXferProcessor(caps, analysis, hasMixedSamples, dstTexture);
bsalomon50785a32015-02-06 07:02:37 -0800209}
210
Brian Salomon92aee3d2016-12-21 09:20:25 -0500211bool GrXPFactory::willNeedDstTexture(const GrCaps& caps, const GrPipelineAnalysis& analysis) const {
212 return (this->willReadDstColor(caps, analysis) && !caps.shaderCaps()->dstReadInShaderSupport());
bsalomon50785a32015-02-06 07:02:37 -0800213}
ethannicholas22793252016-01-30 09:59:10 -0800214
Brian Salomon92aee3d2016-12-21 09:20:25 -0500215bool GrXPFactory::willReadDstColor(const GrCaps& caps, const GrPipelineAnalysis& analysis) const {
216 return analysis.fUsesPLSDstRead || this->onWillReadDstColor(caps, analysis);
ethannicholas22793252016-01-30 09:59:10 -0800217}