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; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 35 | fClip = that.fClip; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 36 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 37 | fColorProcInfoValid = that.fColorProcInfoValid; |
| 38 | fCoverageProcInfoValid = that.fCoverageProcInfoValid; |
joshualitt | f364b61 | 2014-12-11 06:52:01 -0800 | [diff] [blame] | 39 | fColorCache = that.fColorCache; |
| 40 | fCoverageCache = that.fCoverageCache; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 41 | if (fColorProcInfoValid) { |
| 42 | fColorProcInfo = that.fColorProcInfo; |
| 43 | } |
| 44 | if (fCoverageProcInfoValid) { |
| 45 | fCoverageProcInfo = that.fCoverageProcInfo; |
| 46 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 47 | return *this; |
| 48 | } |
| 49 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 50 | void GrPipelineBuilder::setFromPaint(const GrPaint& paint, GrRenderTarget* rt, const GrClip& clip) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 51 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 52 | |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 53 | fColorStages.reset(); |
| 54 | fCoverageStages.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 56 | for (int i = 0; i < paint.numColorStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 57 | fColorStages.push_back(paint.getColorStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 60 | for (int i = 0; i < paint.numCoverageStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 61 | fCoverageStages.push_back(paint.getCoverageStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 64 | fXPFactory.reset(SkRef(paint.getXPFactory())); |
| 65 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 66 | this->setRenderTarget(rt); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 67 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 68 | // These have no equivalent in GrPaint, set them to defaults |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 69 | fDrawFace = kBoth_DrawFace; |
| 70 | fStencilSettings.setDisabled(); |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 71 | fFlagBits = 0; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 72 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 73 | fClip = clip; |
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 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 85 | //////////////////////////////////////////////////////////////////////////////s |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 86 | |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 87 | bool GrPipelineBuilder::willXPNeedDstCopy(const GrDrawTargetCaps& caps, |
| 88 | const GrProcOptInfo& colorPOI, |
| 89 | const GrProcOptInfo& coveragePOI) const { |
| 90 | return this->getXPFactory()->willNeedDstCopy(caps, colorPOI, coveragePOI); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 91 | } |
| 92 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 93 | void GrPipelineBuilder::AutoRestoreEffects::set(GrPipelineBuilder* pipelineBuilder) { |
| 94 | if (fPipelineBuilder) { |
| 95 | int m = fPipelineBuilder->numColorStages() - fColorEffectCnt; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 96 | SkASSERT(m >= 0); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 97 | fPipelineBuilder->fColorStages.pop_back_n(m); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 98 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 99 | int n = fPipelineBuilder->numCoverageStages() - fCoverageEffectCnt; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 100 | SkASSERT(n >= 0); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 101 | fPipelineBuilder->fCoverageStages.pop_back_n(n); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 102 | if (m + n > 0) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 103 | fPipelineBuilder->fColorProcInfoValid = false; |
| 104 | fPipelineBuilder->fCoverageProcInfoValid = false; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 105 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 106 | SkDEBUGCODE(--fPipelineBuilder->fBlockEffectRemovalCnt;) |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 107 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 108 | fPipelineBuilder = pipelineBuilder; |
| 109 | if (NULL != pipelineBuilder) { |
| 110 | fColorEffectCnt = pipelineBuilder->numColorStages(); |
| 111 | fCoverageEffectCnt = pipelineBuilder->numCoverageStages(); |
| 112 | SkDEBUGCODE(++pipelineBuilder->fBlockEffectRemovalCnt;) |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 113 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 114 | } |
| 115 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 116 | //////////////////////////////////////////////////////////////////////////////// |
| 117 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 118 | // Some blend modes allow folding a fractional coverage value into the color's alpha channel, while |
| 119 | // others will blend incorrectly. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 120 | bool GrPipelineBuilder::canTweakAlphaForCoverage() const { |
joshualitt | 2fdeda0 | 2015-01-22 07:11:44 -0800 | [diff] [blame] | 121 | return this->getXPFactory()->canTweakAlphaForCoverage(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | //////////////////////////////////////////////////////////////////////////////// |
| 125 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 126 | GrPipelineBuilder::~GrPipelineBuilder() { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 127 | SkASSERT(0 == fBlockEffectRemovalCnt); |
| 128 | } |
| 129 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 130 | //////////////////////////////////////////////////////////////////////////////// |
| 131 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 132 | bool GrPipelineBuilder::willBlendWithDst(const GrPrimitiveProcessor* pp) const { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 133 | this->calcColorInvariantOutput(pp); |
| 134 | this->calcCoverageInvariantOutput(pp); |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 135 | |
| 136 | GrXPFactory::InvariantOutput output; |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 137 | fXPFactory->getInvariantOutput(fColorProcInfo, fCoverageProcInfo, &output); |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 138 | return output.fWillBlendWithDst; |
egdaniel | cd8b630 | 2014-11-11 14:46:05 -0800 | [diff] [blame] | 139 | } |
| 140 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 141 | void GrPipelineBuilder::calcColorInvariantOutput(const GrPrimitiveProcessor* pp) const { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 142 | fColorProcInfo.calcColorWithPrimProc(pp, fColorStages.begin(), this->numColorStages()); |
| 143 | fColorProcInfoValid = false; |
| 144 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 145 | } |
| 146 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 147 | void GrPipelineBuilder::calcCoverageInvariantOutput(const GrPrimitiveProcessor* pp) const { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 148 | fCoverageProcInfo.calcCoverageWithPrimProc(pp, fCoverageStages.begin(), |
| 149 | this->numCoverageStages()); |
| 150 | fCoverageProcInfoValid = false; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 151 | } |
| 152 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 153 | void GrPipelineBuilder::calcColorInvariantOutput(const GrBatch* batch) const { |
| 154 | fColorProcInfo.calcColorWithBatch(batch, fColorStages.begin(), this->numColorStages()); |
| 155 | fColorProcInfoValid = false; |
| 156 | } |
| 157 | |
| 158 | void GrPipelineBuilder::calcCoverageInvariantOutput(const GrBatch* batch) const { |
| 159 | fCoverageProcInfo.calcCoverageWithBatch(batch, fCoverageStages.begin(), |
| 160 | this->numCoverageStages()); |
| 161 | fCoverageProcInfoValid = false; |
| 162 | } |
| 163 | |
| 164 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 165 | void GrPipelineBuilder::calcColorInvariantOutput(GrColor color) const { |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 166 | if (!fColorProcInfoValid || color != fColorCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 167 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 168 | fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(), color, |
| 169 | flags, false); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 170 | fColorProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 171 | fColorCache = color; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 175 | void GrPipelineBuilder::calcCoverageInvariantOutput(GrColor coverage) const { |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 176 | if (!fCoverageProcInfoValid || coverage != fCoverageCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 177 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 178 | fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), |
| 179 | this->numCoverageStages(), coverage, flags, |
| 180 | true); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 181 | fCoverageProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 182 | fCoverageCache = coverage; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 183 | } |
| 184 | } |