blob: 1ead010b6eace6df63dd2eaa27b81e9b05b8fff4 [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"
cdalton86ae0a92015-06-08 15:11:04 -07009#include "GrPipelineBuilder.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)
16 , fReadsCoverage(true)
17 , 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)
egdanielc19cdc22015-05-10 08:45:18 -070025 , fReadsCoverage(true)
bsalomon6a44c6a2015-05-26 09:49:05 -070026 , fDstTextureOffset() {
27 if (dstTexture && dstTexture->texture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070028 SkASSERT(willReadDstColor);
bsalomon6a44c6a2015-05-26 09:49:05 -070029 fDstTexture.reset(dstTexture->texture());
30 fDstTextureOffset = dstTexture->offset();
31 this->addTextureAccess(&fDstTexture);
egdaniel060a52c2015-04-07 07:31:11 -070032 this->setWillReadFragmentPosition();
bsalomon50785a32015-02-06 07:02:37 -080033 }
34}
35
egdanielc19cdc22015-05-10 08:45:18 -070036GrXferProcessor::OptFlags GrXferProcessor::getOptimizations(const GrProcOptInfo& colorPOI,
37 const GrProcOptInfo& coveragePOI,
38 bool doesStencilWrite,
39 GrColor* overrideColor,
bsalomon4b91f762015-05-19 09:29:46 -070040 const GrCaps& caps) {
egdanielc19cdc22015-05-10 08:45:18 -070041 GrXferProcessor::OptFlags flags = this->onGetOptimizations(colorPOI,
42 coveragePOI,
43 doesStencilWrite,
44 overrideColor,
45 caps);
46
cdaltonedbb31f2015-06-08 12:14:44 -070047 if (this->willReadDstColor()) {
48 // When performing a dst read we handle coverage in the base class.
49 SkASSERT(!(flags & GrXferProcessor::kIgnoreCoverage_OptFlag));
50 if (coveragePOI.isSolidWhite()) {
51 flags |= GrXferProcessor::kIgnoreCoverage_OptFlag;
52 }
53 }
egdanielc19cdc22015-05-10 08:45:18 -070054 if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) {
55 fReadsCoverage = false;
56 }
57 return flags;
58}
59
cdaltonedbb31f2015-06-08 12:14:44 -070060bool GrXferProcessor::hasSecondaryOutput() const {
61 if (!this->willReadDstColor()) {
62 return this->onHasSecondaryOutput();
63 }
cdalton86ae0a92015-06-08 15:11:04 -070064 return this->dstReadUsesMixedSamples();
cdaltonedbb31f2015-06-08 12:14:44 -070065}
66
67void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const {
68 blendInfo->reset();
69 if (!this->willReadDstColor()) {
70 this->onGetBlendInfo(blendInfo);
cdalton86ae0a92015-06-08 15:11:04 -070071 } else if (this->dstReadUsesMixedSamples()) {
72 blendInfo->fDstBlend = kIS2A_GrBlendCoeff;
cdaltonedbb31f2015-06-08 12:14:44 -070073 }
74}
75
jvanverthcfc18862015-04-28 08:48:20 -070076void GrXferProcessor::getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
bsalomon50785a32015-02-06 07:02:37 -080077 uint32_t key = this->willReadDstColor() ? 0x1 : 0x0;
cdaltonedbb31f2015-06-08 12:14:44 -070078 if (key) {
cdalton827bae12015-06-08 13:43:33 -070079 if (const GrTexture* dstTexture = this->getDstTexture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070080 key |= 0x2;
cdalton827bae12015-06-08 13:43:33 -070081 if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) {
82 key |= 0x4;
83 }
cdaltonedbb31f2015-06-08 12:14:44 -070084 }
85 if (this->readsCoverage()) {
86 key |= 0x8;
87 }
cdalton86ae0a92015-06-08 15:11:04 -070088 if (this->dstReadUsesMixedSamples()) {
89 key |= 0x10;
90 }
bsalomon50785a32015-02-06 07:02:37 -080091 }
92 b->add32(key);
93 this->onGetGLProcessorKey(caps, b);
94}
95
cdalton9954bc32015-04-29 14:17:00 -070096bool GrXferProcessor::willNeedXferBarrier(const GrRenderTarget* rt,
bsalomon4b91f762015-05-19 09:29:46 -070097 const GrCaps& caps,
cdalton9954bc32015-04-29 14:17:00 -070098 GrXferBarrierType* outBarrierType) const {
bsalomon6a44c6a2015-05-26 09:49:05 -070099 if (static_cast<const GrSurface*>(rt) == this->getDstTexture()) {
cdalton9954bc32015-04-29 14:17:00 -0700100 // Texture barriers are required when a shader reads and renders to the same texture.
101 SkASSERT(rt);
102 SkASSERT(caps.textureBarrierSupport());
103 *outBarrierType = kTexture_GrXferBarrierType;
104 return true;
105 }
cdalton8917d622015-05-06 13:40:21 -0700106 return this->onWillNeedXferBarrier(rt, caps, outBarrierType);
cdalton9954bc32015-04-29 14:17:00 -0700107}
108
bsalomonf7cc8772015-05-11 11:21:14 -0700109#ifdef SK_DEBUG
110static const char* equation_string(GrBlendEquation eq) {
111 switch (eq) {
112 case kAdd_GrBlendEquation:
113 return "add";
114 case kSubtract_GrBlendEquation:
115 return "subtract";
116 case kReverseSubtract_GrBlendEquation:
117 return "reverse_subtract";
118 case kScreen_GrBlendEquation:
119 return "screen";
120 case kOverlay_GrBlendEquation:
121 return "overlay";
122 case kDarken_GrBlendEquation:
123 return "darken";
124 case kLighten_GrBlendEquation:
125 return "lighten";
126 case kColorDodge_GrBlendEquation:
127 return "color_dodge";
128 case kColorBurn_GrBlendEquation:
129 return "color_burn";
130 case kHardLight_GrBlendEquation:
131 return "hard_light";
132 case kSoftLight_GrBlendEquation:
133 return "soft_light";
134 case kDifference_GrBlendEquation:
135 return "difference";
136 case kExclusion_GrBlendEquation:
137 return "exclusion";
138 case kMultiply_GrBlendEquation:
139 return "multiply";
140 case kHSLHue_GrBlendEquation:
141 return "hsl_hue";
142 case kHSLSaturation_GrBlendEquation:
143 return "hsl_saturation";
144 case kHSLColor_GrBlendEquation:
145 return "hsl_color";
146 case kHSLLuminosity_GrBlendEquation:
147 return "hsl_luminosity";
148 };
149 return "";
150}
151
152static const char* coeff_string(GrBlendCoeff coeff) {
153 switch (coeff) {
154 case kZero_GrBlendCoeff:
155 return "zero";
156 case kOne_GrBlendCoeff:
157 return "one";
158 case kSC_GrBlendCoeff:
159 return "src_color";
160 case kISC_GrBlendCoeff:
161 return "inv_src_color";
162 case kDC_GrBlendCoeff:
163 return "dst_color";
164 case kIDC_GrBlendCoeff:
165 return "inv_dst_color";
166 case kSA_GrBlendCoeff:
167 return "src_alpha";
168 case kISA_GrBlendCoeff:
169 return "inv_src_alpha";
170 case kDA_GrBlendCoeff:
171 return "dst_alpha";
172 case kIDA_GrBlendCoeff:
173 return "inv_dst_alpha";
174 case kConstC_GrBlendCoeff:
175 return "const_color";
176 case kIConstC_GrBlendCoeff:
177 return "inv_const_color";
178 case kConstA_GrBlendCoeff:
179 return "const_alpha";
180 case kIConstA_GrBlendCoeff:
181 return "inv_const_alpha";
182 case kS2C_GrBlendCoeff:
183 return "src2_color";
184 case kIS2C_GrBlendCoeff:
185 return "inv_src2_color";
186 case kS2A_GrBlendCoeff:
187 return "src2_alpha";
188 case kIS2A_GrBlendCoeff:
189 return "inv_src2_alpha";
190 }
191 return "";
192}
193
194SkString GrXferProcessor::BlendInfo::dump() const {
195 SkString out;
196 out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)",
197 fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend),
198 coeff_string(fDstBlend), fBlendConstant);
199 return out;
200}
201#endif
202
bsalomon50785a32015-02-06 07:02:37 -0800203///////////////////////////////////////////////////////////////////////////////
204
205GrXferProcessor* GrXPFactory::createXferProcessor(const GrProcOptInfo& colorPOI,
206 const GrProcOptInfo& coveragePOI,
cdalton86ae0a92015-06-08 15:11:04 -0700207 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700208 const DstTexture* dstTexture,
bsalomon4b91f762015-05-19 09:29:46 -0700209 const GrCaps& caps) const {
bsalomon50785a32015-02-06 07:02:37 -0800210#ifdef SK_DEBUG
cdalton86ae0a92015-06-08 15:11:04 -0700211 if (this->willReadDstColor(caps, colorPOI, coveragePOI, hasMixedSamples)) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700212 if (!caps.shaderCaps()->dstReadInShaderSupport()) {
bsalomon6a44c6a2015-05-26 09:49:05 -0700213 SkASSERT(dstTexture && dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800214 } else {
bsalomon6a44c6a2015-05-26 09:49:05 -0700215 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800216 }
217 } else {
bsalomon6a44c6a2015-05-26 09:49:05 -0700218 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800219 }
cdalton86ae0a92015-06-08 15:11:04 -0700220 SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport());
bsalomon50785a32015-02-06 07:02:37 -0800221#endif
cdalton86ae0a92015-06-08 15:11:04 -0700222 return this->onCreateXferProcessor(caps, colorPOI, coveragePOI, hasMixedSamples, dstTexture);
bsalomon50785a32015-02-06 07:02:37 -0800223}
224
cdalton86ae0a92015-06-08 15:11:04 -0700225bool GrXPFactory::willNeedDstTexture(const GrCaps& caps,
226 const GrProcOptInfo& colorPOI,
227 const GrProcOptInfo& coveragePOI,
228 bool hasMixedSamples) const {
229 return (this->willReadDstColor(caps, colorPOI, coveragePOI, hasMixedSamples) &&
230 !caps.shaderCaps()->dstReadInShaderSupport());
bsalomon50785a32015-02-06 07:02:37 -0800231}