blob: 910b873e861aa42a893da1cb2302ca03b2913a76 [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"
cdalton86ae0a92015-06-08 15:11:04 -070010#include "GrPipelineBuilder.h"
cdaltonedbb31f2015-06-08 12:14:44 -070011#include "GrProcOptInfo.h"
bsalomon50785a32015-02-06 07:02:37 -080012#include "gl/GrGLCaps.h"
13
egdanielc19cdc22015-05-10 08:45:18 -070014GrXferProcessor::GrXferProcessor()
cdalton86ae0a92015-06-08 15:11:04 -070015 : fWillReadDstColor(false)
16 , fDstReadUsesMixedSamples(false)
cdalton86ae0a92015-06-08 15:11:04 -070017 , fDstTextureOffset() {
bsalomon50785a32015-02-06 07:02:37 -080018}
19
cdalton86ae0a92015-06-08 15:11:04 -070020GrXferProcessor::GrXferProcessor(const DstTexture* dstTexture,
21 bool willReadDstColor,
22 bool hasMixedSamples)
bsalomon50785a32015-02-06 07:02:37 -080023 : fWillReadDstColor(willReadDstColor)
cdalton86ae0a92015-06-08 15:11:04 -070024 , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples)
bsalomon6a44c6a2015-05-26 09:49:05 -070025 , fDstTextureOffset() {
26 if (dstTexture && dstTexture->texture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070027 SkASSERT(willReadDstColor);
bsalomon6a44c6a2015-05-26 09:49:05 -070028 fDstTexture.reset(dstTexture->texture());
29 fDstTextureOffset = dstTexture->offset();
30 this->addTextureAccess(&fDstTexture);
egdaniel060a52c2015-04-07 07:31:11 -070031 this->setWillReadFragmentPosition();
bsalomon50785a32015-02-06 07:02:37 -080032 }
33}
34
ethannicholasde4166a2015-11-30 08:57:38 -080035GrXferProcessor::OptFlags GrXferProcessor::getOptimizations(
36 const GrPipelineOptimizations& optimizations,
37 bool doesStencilWrite,
38 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -080039 const GrCaps& caps) const {
ethannicholasde4166a2015-11-30 08:57:38 -080040 GrXferProcessor::OptFlags flags = this->onGetOptimizations(optimizations,
egdanielc19cdc22015-05-10 08:45:18 -070041 doesStencilWrite,
42 overrideColor,
43 caps);
44
cdaltonedbb31f2015-06-08 12:14:44 -070045 if (this->willReadDstColor()) {
46 // When performing a dst read we handle coverage in the base class.
47 SkASSERT(!(flags & GrXferProcessor::kIgnoreCoverage_OptFlag));
ethannicholasde4166a2015-11-30 08:57:38 -080048 if (optimizations.fCoveragePOI.isSolidWhite()) {
cdaltonedbb31f2015-06-08 12:14:44 -070049 flags |= GrXferProcessor::kIgnoreCoverage_OptFlag;
50 }
51 }
egdanielc19cdc22015-05-10 08:45:18 -070052 return flags;
53}
54
cdaltonedbb31f2015-06-08 12:14:44 -070055bool GrXferProcessor::hasSecondaryOutput() const {
56 if (!this->willReadDstColor()) {
57 return this->onHasSecondaryOutput();
58 }
cdalton86ae0a92015-06-08 15:11:04 -070059 return this->dstReadUsesMixedSamples();
cdaltonedbb31f2015-06-08 12:14:44 -070060}
61
62void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const {
63 blendInfo->reset();
64 if (!this->willReadDstColor()) {
65 this->onGetBlendInfo(blendInfo);
cdalton86ae0a92015-06-08 15:11:04 -070066 } else if (this->dstReadUsesMixedSamples()) {
67 blendInfo->fDstBlend = kIS2A_GrBlendCoeff;
cdaltonedbb31f2015-06-08 12:14:44 -070068 }
69}
70
egdaniel57d3b032015-11-13 11:57:27 -080071void GrXferProcessor::getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
bsalomon50785a32015-02-06 07:02:37 -080072 uint32_t key = this->willReadDstColor() ? 0x1 : 0x0;
cdaltonedbb31f2015-06-08 12:14:44 -070073 if (key) {
cdalton827bae12015-06-08 13:43:33 -070074 if (const GrTexture* dstTexture = this->getDstTexture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070075 key |= 0x2;
cdalton827bae12015-06-08 13:43:33 -070076 if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) {
77 key |= 0x4;
78 }
cdaltonedbb31f2015-06-08 12:14:44 -070079 }
cdalton86ae0a92015-06-08 15:11:04 -070080 if (this->dstReadUsesMixedSamples()) {
egdaniel56cf6dc2015-11-30 10:15:58 -080081 key |= 0x8;
cdalton86ae0a92015-06-08 15:11:04 -070082 }
bsalomon50785a32015-02-06 07:02:37 -080083 }
84 b->add32(key);
egdaniel57d3b032015-11-13 11:57:27 -080085 this->onGetGLSLProcessorKey(caps, b);
bsalomon50785a32015-02-06 07:02:37 -080086}
87
bsalomoncb02b382015-08-12 11:14:50 -070088GrXferBarrierType GrXferProcessor::xferBarrierType(const GrRenderTarget* rt,
89 const GrCaps& caps) const {
90 SkASSERT(rt);
bsalomon6a44c6a2015-05-26 09:49:05 -070091 if (static_cast<const GrSurface*>(rt) == this->getDstTexture()) {
cdalton9954bc32015-04-29 14:17:00 -070092 // Texture barriers are required when a shader reads and renders to the same texture.
cdalton9954bc32015-04-29 14:17:00 -070093 SkASSERT(caps.textureBarrierSupport());
bsalomoncb02b382015-08-12 11:14:50 -070094 return kTexture_GrXferBarrierType;
cdalton9954bc32015-04-29 14:17:00 -070095 }
bsalomoncb02b382015-08-12 11:14:50 -070096 return this->onXferBarrier(rt, caps);
cdalton9954bc32015-04-29 14:17:00 -070097}
98
bsalomonf7cc8772015-05-11 11:21:14 -070099#ifdef SK_DEBUG
100static const char* equation_string(GrBlendEquation eq) {
101 switch (eq) {
102 case kAdd_GrBlendEquation:
103 return "add";
104 case kSubtract_GrBlendEquation:
105 return "subtract";
106 case kReverseSubtract_GrBlendEquation:
107 return "reverse_subtract";
108 case kScreen_GrBlendEquation:
109 return "screen";
110 case kOverlay_GrBlendEquation:
111 return "overlay";
112 case kDarken_GrBlendEquation:
113 return "darken";
114 case kLighten_GrBlendEquation:
115 return "lighten";
116 case kColorDodge_GrBlendEquation:
117 return "color_dodge";
118 case kColorBurn_GrBlendEquation:
119 return "color_burn";
120 case kHardLight_GrBlendEquation:
121 return "hard_light";
122 case kSoftLight_GrBlendEquation:
123 return "soft_light";
124 case kDifference_GrBlendEquation:
125 return "difference";
126 case kExclusion_GrBlendEquation:
127 return "exclusion";
128 case kMultiply_GrBlendEquation:
129 return "multiply";
130 case kHSLHue_GrBlendEquation:
131 return "hsl_hue";
132 case kHSLSaturation_GrBlendEquation:
133 return "hsl_saturation";
134 case kHSLColor_GrBlendEquation:
135 return "hsl_color";
136 case kHSLLuminosity_GrBlendEquation:
137 return "hsl_luminosity";
138 };
139 return "";
140}
141
142static const char* coeff_string(GrBlendCoeff coeff) {
143 switch (coeff) {
144 case kZero_GrBlendCoeff:
145 return "zero";
146 case kOne_GrBlendCoeff:
147 return "one";
148 case kSC_GrBlendCoeff:
149 return "src_color";
150 case kISC_GrBlendCoeff:
151 return "inv_src_color";
152 case kDC_GrBlendCoeff:
153 return "dst_color";
154 case kIDC_GrBlendCoeff:
155 return "inv_dst_color";
156 case kSA_GrBlendCoeff:
157 return "src_alpha";
158 case kISA_GrBlendCoeff:
159 return "inv_src_alpha";
160 case kDA_GrBlendCoeff:
161 return "dst_alpha";
162 case kIDA_GrBlendCoeff:
163 return "inv_dst_alpha";
164 case kConstC_GrBlendCoeff:
165 return "const_color";
166 case kIConstC_GrBlendCoeff:
167 return "inv_const_color";
168 case kConstA_GrBlendCoeff:
169 return "const_alpha";
170 case kIConstA_GrBlendCoeff:
171 return "inv_const_alpha";
172 case kS2C_GrBlendCoeff:
173 return "src2_color";
174 case kIS2C_GrBlendCoeff:
175 return "inv_src2_color";
176 case kS2A_GrBlendCoeff:
177 return "src2_alpha";
178 case kIS2A_GrBlendCoeff:
179 return "inv_src2_alpha";
180 }
181 return "";
182}
183
184SkString GrXferProcessor::BlendInfo::dump() const {
185 SkString out;
186 out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)",
187 fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend),
188 coeff_string(fDstBlend), fBlendConstant);
189 return out;
190}
191#endif
192
bsalomon50785a32015-02-06 07:02:37 -0800193///////////////////////////////////////////////////////////////////////////////
194
ethannicholasde4166a2015-11-30 08:57:38 -0800195GrXferProcessor* GrXPFactory::createXferProcessor(const GrPipelineOptimizations& optimizations,
cdalton86ae0a92015-06-08 15:11:04 -0700196 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700197 const DstTexture* dstTexture,
bsalomon4b91f762015-05-19 09:29:46 -0700198 const GrCaps& caps) const {
bsalomon50785a32015-02-06 07:02:37 -0800199#ifdef SK_DEBUG
cdalton3ccf2e72016-05-06 09:41:16 -0700200 if (this->willReadDstColor(caps, optimizations)) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700201 if (!caps.shaderCaps()->dstReadInShaderSupport()) {
bsalomon6a44c6a2015-05-26 09:49:05 -0700202 SkASSERT(dstTexture && dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800203 } else {
halcanary9d524f22016-03-29 09:03:52 -0700204 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800205 }
206 } else {
halcanary9d524f22016-03-29 09:03:52 -0700207 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800208 }
cdalton86ae0a92015-06-08 15:11:04 -0700209 SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport());
bsalomon50785a32015-02-06 07:02:37 -0800210#endif
ethannicholasde4166a2015-11-30 08:57:38 -0800211 return this->onCreateXferProcessor(caps, optimizations, hasMixedSamples, dstTexture);
bsalomon50785a32015-02-06 07:02:37 -0800212}
213
cdalton86ae0a92015-06-08 15:11:04 -0700214bool GrXPFactory::willNeedDstTexture(const GrCaps& caps,
cdalton3ccf2e72016-05-06 09:41:16 -0700215 const GrPipelineOptimizations& optimizations) const {
216 return (this->willReadDstColor(caps, optimizations) &&
cdalton86ae0a92015-06-08 15:11:04 -0700217 !caps.shaderCaps()->dstReadInShaderSupport());
bsalomon50785a32015-02-06 07:02:37 -0800218}
ethannicholas22793252016-01-30 09:59:10 -0800219
220bool GrXPFactory::willReadDstColor(const GrCaps& caps,
cdalton3ccf2e72016-05-06 09:41:16 -0700221 const GrPipelineOptimizations& optimizations) const {
222 return optimizations.fOverrides.fUsePLSDstRead || this->onWillReadDstColor(caps, optimizations);
ethannicholas22793252016-01-30 09:59:10 -0800223}