bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 1 | /* |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2 | * Copyright 2015 Google Inc. |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 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 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 8 | #include "GrPipelineBuilder.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 9 | |
egdaniel | b1cff03 | 2014-11-13 06:19:25 -0800 | [diff] [blame] | 10 | #include "GrBlend.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 11 | #include "GrPaint.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 12 | #include "GrPipeline.h" |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 13 | #include "GrProcOptInfo.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 14 | #include "GrXferProcessor.h" |
| 15 | #include "effects/GrPorterDuffXferProcessor.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 16 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 17 | GrPipelineBuilder::GrPipelineBuilder() |
joshualitt | 2fdeda0 | 2015-01-22 07:11:44 -0800 | [diff] [blame] | 18 | : fFlagBits(0x0) |
| 19 | , fDrawFace(kBoth_DrawFace) |
| 20 | , fColorProcInfoValid(false) |
| 21 | , fCoverageProcInfoValid(false) |
| 22 | , fColorCache(GrColor_ILLEGAL) |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 23 | , fCoverageCache(GrColor_ILLEGAL) { |
joshualitt | 2fdeda0 | 2015-01-22 07:11:44 -0800 | [diff] [blame] | 24 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 25 | } |
| 26 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 27 | GrPipelineBuilder& GrPipelineBuilder::operator=(const GrPipelineBuilder& that) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 28 | fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get())); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 29 | fFlagBits = that.fFlagBits; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 30 | fStencilSettings = that.fStencilSettings; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 31 | fDrawFace = that.fDrawFace; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 32 | fXPFactory.reset(SkRef(that.getXPFactory())); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 33 | fColorStages = that.fColorStages; |
| 34 | fCoverageStages = that.fCoverageStages; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 35 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 36 | fColorProcInfoValid = that.fColorProcInfoValid; |
| 37 | fCoverageProcInfoValid = that.fCoverageProcInfoValid; |
joshualitt | f364b61 | 2014-12-11 06:52:01 -0800 | [diff] [blame] | 38 | fColorCache = that.fColorCache; |
| 39 | fCoverageCache = that.fCoverageCache; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 40 | if (fColorProcInfoValid) { |
| 41 | fColorProcInfo = that.fColorProcInfo; |
| 42 | } |
| 43 | if (fCoverageProcInfoValid) { |
| 44 | fCoverageProcInfo = that.fCoverageProcInfo; |
| 45 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 46 | return *this; |
| 47 | } |
| 48 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 49 | void GrPipelineBuilder::setFromPaint(const GrPaint& paint, GrRenderTarget* rt) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 50 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 51 | |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 52 | fColorStages.reset(); |
| 53 | fCoverageStages.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 54 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 55 | for (int i = 0; i < paint.numColorStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 56 | fColorStages.push_back(paint.getColorStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 57 | } |
| 58 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 59 | for (int i = 0; i < paint.numCoverageStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 60 | fCoverageStages.push_back(paint.getCoverageStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 61 | } |
| 62 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 63 | fXPFactory.reset(SkRef(paint.getXPFactory())); |
| 64 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 65 | this->setRenderTarget(rt); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 67 | // These have no equivalent in GrPaint, set them to defaults |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 68 | fDrawFace = kBoth_DrawFace; |
| 69 | fStencilSettings.setDisabled(); |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 70 | fFlagBits = 0; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 71 | |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 72 | // Enable the clip bit |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 73 | this->enableState(GrPipelineBuilder::kClip_StateBit); |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 74 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 75 | this->setState(GrPipelineBuilder::kDither_StateBit, paint.isDither()); |
| 76 | this->setState(GrPipelineBuilder::kHWAntialias_StateBit, paint.isAntiAlias()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 77 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 78 | fColorProcInfoValid = false; |
| 79 | fCoverageProcInfoValid = false; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 80 | |
| 81 | fColorCache = GrColor_ILLEGAL; |
| 82 | fCoverageCache = GrColor_ILLEGAL; |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 83 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 84 | |
| 85 | //////////////////////////////////////////////////////////////////////////////// |
| 86 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 87 | bool GrPipelineBuilder::canUseFracCoveragePrimProc(GrColor color, |
| 88 | const GrDrawTargetCaps& caps) const { |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 89 | if (caps.dualSourceBlendingSupport()) { |
| 90 | return true; |
| 91 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 92 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 93 | this->calcColorInvariantOutput(color); |
| 94 | |
| 95 | // The coverage isn't actually white, its unknown, but this will produce the same effect |
| 96 | // TODO we want to cache the result of this call, but we can probably clean up the interface |
| 97 | // so we don't have to pass in a seemingly known coverage |
| 98 | this->calcCoverageInvariantOutput(GrColor_WHITE); |
joshualitt | 2fdeda0 | 2015-01-22 07:11:44 -0800 | [diff] [blame] | 99 | return this->getXPFactory()->canApplyCoverage(fColorProcInfo, fCoverageProcInfo); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 102 | //////////////////////////////////////////////////////////////////////////////s |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 103 | |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame^] | 104 | bool GrPipelineBuilder::willXPNeedDstCopy(const GrDrawTargetCaps& caps, |
| 105 | const GrProcOptInfo& colorPOI, |
| 106 | const GrProcOptInfo& coveragePOI) const { |
| 107 | return this->getXPFactory()->willNeedDstCopy(caps, colorPOI, coveragePOI); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 108 | } |
| 109 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 110 | void GrPipelineBuilder::AutoRestoreEffects::set(GrPipelineBuilder* pipelineBuilder) { |
| 111 | if (fPipelineBuilder) { |
| 112 | int m = fPipelineBuilder->numColorStages() - fColorEffectCnt; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 113 | SkASSERT(m >= 0); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 114 | fPipelineBuilder->fColorStages.pop_back_n(m); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 115 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 116 | int n = fPipelineBuilder->numCoverageStages() - fCoverageEffectCnt; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 117 | SkASSERT(n >= 0); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 118 | fPipelineBuilder->fCoverageStages.pop_back_n(n); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 119 | if (m + n > 0) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 120 | fPipelineBuilder->fColorProcInfoValid = false; |
| 121 | fPipelineBuilder->fCoverageProcInfoValid = false; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 122 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 123 | SkDEBUGCODE(--fPipelineBuilder->fBlockEffectRemovalCnt;) |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 124 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 125 | fPipelineBuilder = pipelineBuilder; |
| 126 | if (NULL != pipelineBuilder) { |
| 127 | fColorEffectCnt = pipelineBuilder->numColorStages(); |
| 128 | fCoverageEffectCnt = pipelineBuilder->numCoverageStages(); |
| 129 | SkDEBUGCODE(++pipelineBuilder->fBlockEffectRemovalCnt;) |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 130 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 131 | } |
| 132 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 133 | //////////////////////////////////////////////////////////////////////////////// |
| 134 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 135 | // Some blend modes allow folding a fractional coverage value into the color's alpha channel, while |
| 136 | // others will blend incorrectly. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 137 | bool GrPipelineBuilder::canTweakAlphaForCoverage() const { |
joshualitt | 2fdeda0 | 2015-01-22 07:11:44 -0800 | [diff] [blame] | 138 | return this->getXPFactory()->canTweakAlphaForCoverage(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | //////////////////////////////////////////////////////////////////////////////// |
| 142 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 143 | GrPipelineBuilder::~GrPipelineBuilder() { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 144 | SkASSERT(0 == fBlockEffectRemovalCnt); |
| 145 | } |
| 146 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 147 | //////////////////////////////////////////////////////////////////////////////// |
| 148 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 149 | bool GrPipelineBuilder::willBlendWithDst(const GrPrimitiveProcessor* pp) const { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 150 | this->calcColorInvariantOutput(pp); |
| 151 | this->calcCoverageInvariantOutput(pp); |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 152 | |
| 153 | GrXPFactory::InvariantOutput output; |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 154 | fXPFactory->getInvariantOutput(fColorProcInfo, fCoverageProcInfo, &output); |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 155 | return output.fWillBlendWithDst; |
egdaniel | cd8b630 | 2014-11-11 14:46:05 -0800 | [diff] [blame] | 156 | } |
| 157 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 158 | void GrPipelineBuilder::calcColorInvariantOutput(const GrPrimitiveProcessor* pp) const { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 159 | fColorProcInfo.calcColorWithPrimProc(pp, fColorStages.begin(), this->numColorStages()); |
| 160 | fColorProcInfoValid = false; |
| 161 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 162 | } |
| 163 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 164 | void GrPipelineBuilder::calcCoverageInvariantOutput(const GrPrimitiveProcessor* pp) const { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 165 | fCoverageProcInfo.calcCoverageWithPrimProc(pp, fCoverageStages.begin(), |
| 166 | this->numCoverageStages()); |
| 167 | fCoverageProcInfoValid = false; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 168 | } |
| 169 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 170 | void GrPipelineBuilder::calcColorInvariantOutput(const GrBatch* batch) const { |
| 171 | fColorProcInfo.calcColorWithBatch(batch, fColorStages.begin(), this->numColorStages()); |
| 172 | fColorProcInfoValid = false; |
| 173 | } |
| 174 | |
| 175 | void GrPipelineBuilder::calcCoverageInvariantOutput(const GrBatch* batch) const { |
| 176 | fCoverageProcInfo.calcCoverageWithBatch(batch, fCoverageStages.begin(), |
| 177 | this->numCoverageStages()); |
| 178 | fCoverageProcInfoValid = false; |
| 179 | } |
| 180 | |
| 181 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 182 | void GrPipelineBuilder::calcColorInvariantOutput(GrColor color) const { |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 183 | if (!fColorProcInfoValid || color != fColorCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 184 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 185 | fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(), color, |
| 186 | flags, false); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 187 | fColorProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 188 | fColorCache = color; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 192 | void GrPipelineBuilder::calcCoverageInvariantOutput(GrColor coverage) const { |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 193 | if (!fCoverageProcInfoValid || coverage != fCoverageCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 194 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 195 | fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), |
| 196 | this->numCoverageStages(), coverage, flags, |
| 197 | true); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 198 | fCoverageProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 199 | fCoverageCache = coverage; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 200 | } |
| 201 | } |