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