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