blob: 7767bf220d4175eede1b43f4b6fca14f747dfb39 [file] [log] [blame]
egdaniel3658f382014-09-15 07:01:59 -07001/*
egdaniel8dd688b2015-01-22 10:16:09 -08002 * Copyright 2015 Google Inc.
egdaniel3658f382014-09-15 07:01:59 -07003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
egdaniel8dd688b2015-01-22 10:16:09 -08008#include "GrPipeline.h"
egdaniel3658f382014-09-15 07:01:59 -07009
bsalomoneb1cb5c2015-05-22 08:01:09 -070010#include "GrCaps.h"
robertphillips55fdccc2016-06-06 06:16:20 -070011#include "GrDrawContext.h"
robertphillips498d7ac2015-10-30 10:11:30 -070012#include "GrDrawTarget.h"
joshualitt4973d9d2014-11-08 09:24:25 -080013#include "GrGpu.h"
egdaniel8dd688b2015-01-22 10:16:09 -080014#include "GrPipelineBuilder.h"
egdanielb6cbc382014-11-13 11:00:34 -080015#include "GrProcOptInfo.h"
cdalton193d9cf2016-05-12 11:52:02 -070016#include "GrRenderTargetPriv.h"
egdaniel95131432014-12-09 11:15:43 -080017#include "GrXferProcessor.h"
egdaniel3658f382014-09-15 07:01:59 -070018
joshualitt74417822015-08-07 11:42:16 -070019#include "batches/GrBatch.h"
20
bsalomona387a112015-08-11 14:47:42 -070021GrPipeline* GrPipeline::CreateAt(void* memory, const CreateArgs& args,
ethannicholasff210322015-11-24 12:10:10 -080022 GrXPOverridesForBatch* overrides) {
bsalomona387a112015-08-11 14:47:42 -070023 const GrPipelineBuilder& builder = *args.fPipelineBuilder;
bsalomonc6998732015-08-10 12:01:15 -070024
cdalton93a379b2016-05-11 13:58:08 -070025 GrPipeline* pipeline = new (memory) GrPipeline;
robertphillips55fdccc2016-06-06 06:16:20 -070026 GrRenderTarget* rt = args.fDrawContext->accessRenderTarget();
27 pipeline->fRenderTarget.reset(rt);
cdalton93a379b2016-05-11 13:58:08 -070028 SkASSERT(pipeline->fRenderTarget);
29 pipeline->fScissorState = *args.fScissor;
30 if (builder.hasUserStencilSettings() || args.fHasStencilClip) {
robertphillips55fdccc2016-06-06 06:16:20 -070031 const GrRenderTargetPriv& rtPriv = rt->renderTargetPriv();
cdalton93a379b2016-05-11 13:58:08 -070032 pipeline->fStencilSettings.reset(*builder.getUserStencil(), args.fHasStencilClip,
cdalton193d9cf2016-05-12 11:52:02 -070033 rtPriv.numStencilBits());
cdalton93a379b2016-05-11 13:58:08 -070034 SkASSERT(!pipeline->fStencilSettings.usesWrapOp() || args.fCaps->stencilWrapOpsSupport());
35 }
36 pipeline->fDrawFace = builder.getDrawFace();
37
38 pipeline->fFlags = 0;
39 if (builder.isHWAntialias()) {
40 pipeline->fFlags |= kHWAA_Flag;
41 }
42 if (builder.snapVerticesToPixelCenters()) {
43 pipeline->fFlags |= kSnapVertices_Flag;
44 }
45 if (builder.getDisableOutputConversionToSRGB()) {
46 pipeline->fFlags |= kDisableOutputConversionToSRGB_Flag;
47 }
48 if (builder.getAllowSRGBInputs()) {
49 pipeline->fFlags |= kAllowSRGBInputs_Flag;
50 }
cdalton193d9cf2016-05-12 11:52:02 -070051 if (args.fHasStencilClip) {
52 pipeline->fFlags |= kHasStencilClip_Flag;
53 }
cdalton93a379b2016-05-11 13:58:08 -070054
egdaniel95131432014-12-09 11:15:43 -080055 // Create XferProcessor from DS's XPFactory
robertphillips55fdccc2016-06-06 06:16:20 -070056 bool hasMixedSamples = args.fDrawContext->hasMixedSamples() &&
cdalton93a379b2016-05-11 13:58:08 -070057 (builder.isHWAntialias() || !pipeline->fStencilSettings.isDisabled());
egdanielc4b72722015-11-23 13:20:41 -080058 const GrXPFactory* xpFactory = builder.getXPFactory();
59 SkAutoTUnref<GrXferProcessor> xferProcessor;
60 if (xpFactory) {
ethannicholasde4166a2015-11-30 08:57:38 -080061 xferProcessor.reset(xpFactory->createXferProcessor(args.fOpts,
cdalton3ccf2e72016-05-06 09:41:16 -070062 hasMixedSamples,
egdanielc4b72722015-11-23 13:20:41 -080063 &args.fDstTexture,
64 *args.fCaps));
bsalomon2047b782015-12-21 13:12:54 -080065 if (!xferProcessor) {
cdalton93a379b2016-05-11 13:58:08 -070066 pipeline->~GrPipeline();
bsalomon2047b782015-12-21 13:12:54 -080067 return nullptr;
68 }
egdanielc4b72722015-11-23 13:20:41 -080069 } else {
bsalomon2047b782015-12-21 13:12:54 -080070 // This may return nullptr in the common case of src-over implemented using hw blending.
egdanielc4b72722015-11-23 13:20:41 -080071 xferProcessor.reset(GrPorterDuffXPFactory::CreateSrcOverXferProcessor(
72 *args.fCaps,
ethannicholasde4166a2015-11-30 08:57:38 -080073 args.fOpts,
cdalton3ccf2e72016-05-06 09:41:16 -070074 hasMixedSamples,
egdanielc4b72722015-11-23 13:20:41 -080075 &args.fDstTexture));
76 }
cdalton3ccf2e72016-05-06 09:41:16 -070077 GrColor overrideColor = GrColor_ILLEGAL;
ethannicholasff210322015-11-24 12:10:10 -080078 if (args.fOpts.fColorPOI.firstEffectiveProcessorIndex() != 0) {
79 overrideColor = args.fOpts.fColorPOI.inputColorToFirstEffectiveProccesor();
joshualitt9b989322014-12-15 14:16:27 -080080 }
81
bsalomon7765a472015-07-08 11:26:37 -070082 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
egdaniel95131432014-12-09 11:15:43 -080083
halcanary9d524f22016-03-29 09:03:52 -070084 const GrXferProcessor* xpForOpts = xferProcessor ? xferProcessor.get() :
bsalomon2047b782015-12-21 13:12:54 -080085 &GrPorterDuffXPFactory::SimpleSrcOverXP();
86 optFlags = xpForOpts->getOptimizations(args.fOpts,
cdalton93a379b2016-05-11 13:58:08 -070087 pipeline->fStencilSettings.doesWrite(),
bsalomon2047b782015-12-21 13:12:54 -080088 &overrideColor,
89 *args.fCaps);
bsalomonc8d3f572015-08-13 06:44:04 -070090
91 // When path rendering the stencil settings are not always set on the GrPipelineBuilder
92 // so we must check the draw type. In cases where we will skip drawing we simply return a
93 // null GrPipeline.
94 if (GrXferProcessor::kSkipDraw_OptFlag & optFlags) {
cdalton93a379b2016-05-11 13:58:08 -070095 pipeline->~GrPipeline();
bsalomonc8d3f572015-08-13 06:44:04 -070096 return nullptr;
egdaniel95131432014-12-09 11:15:43 -080097 }
bsalomonb03c4a32014-11-20 09:56:11 -080098
bsalomon7765a472015-07-08 11:26:37 -070099 // No need to have an override color if it isn't even going to be used.
100 if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) {
101 overrideColor = GrColor_ILLEGAL;
102 }
103
egdanielc4b72722015-11-23 13:20:41 -0800104 pipeline->fXferProcessor.reset(xferProcessor);
bsalomonb03c4a32014-11-20 09:56:11 -0800105
ethannicholasff210322015-11-24 12:10:10 -0800106 int firstColorProcessorIdx = args.fOpts.fColorPOI.firstEffectiveProcessorIndex();
egdaniel912b3d22014-11-17 07:45:53 -0800107
bsalomonac856c92015-08-27 06:30:17 -0700108 // TODO: Once we can handle single or four channel input into coverage GrFragmentProcessors
109 // then we can use GrPipelineBuilder's coverageProcInfo (like color above) to set this initial
110 // information.
111 int firstCoverageProcessorIdx = 0;
egdaniel912b3d22014-11-17 07:45:53 -0800112
halcanary9d524f22016-03-29 09:03:52 -0700113 pipeline->adjustProgramFromOptimizations(builder, optFlags, args.fOpts.fColorPOI,
114 args.fOpts.fCoveragePOI, &firstColorProcessorIdx,
ethannicholasff210322015-11-24 12:10:10 -0800115 &firstCoverageProcessorIdx);
egdaniel95131432014-12-09 11:15:43 -0800116
joshualitt290c09b2014-12-19 13:45:20 -0800117 bool usesLocalCoords = false;
118
bsalomonac856c92015-08-27 06:30:17 -0700119 // Copy GrFragmentProcessors from GrPipelineBuilder to Pipeline
120 pipeline->fNumColorProcessors = builder.numColorFragmentProcessors() - firstColorProcessorIdx;
121 int numTotalProcessors = pipeline->fNumColorProcessors +
122 builder.numCoverageFragmentProcessors() - firstCoverageProcessorIdx;
123 pipeline->fFragmentProcessors.reset(numTotalProcessors);
124 int currFPIdx = 0;
125 for (int i = firstColorProcessorIdx; i < builder.numColorFragmentProcessors();
126 ++i, ++currFPIdx) {
127 const GrFragmentProcessor* fp = builder.getColorFragmentProcessor(i);
128 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700129 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
bsalomonae59b772014-11-19 08:23:49 -0800130 }
egdaniel95131432014-12-09 11:15:43 -0800131
bsalomonac856c92015-08-27 06:30:17 -0700132 for (int i = firstCoverageProcessorIdx; i < builder.numCoverageFragmentProcessors();
133 ++i, ++currFPIdx) {
134 const GrFragmentProcessor* fp = builder.getCoverageFragmentProcessor(i);
135 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700136 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
egdaniel9cf45bf2014-10-08 06:49:10 -0700137 }
joshualitt79f8fae2014-10-28 17:59:26 -0700138
bsalomon7765a472015-07-08 11:26:37 -0700139 // Setup info we need to pass to GrPrimitiveProcessors that are used with this GrPipeline.
ethannicholasff210322015-11-24 12:10:10 -0800140 overrides->fFlags = 0;
bsalomon7765a472015-07-08 11:26:37 -0700141 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag)) {
ethannicholasff210322015-11-24 12:10:10 -0800142 overrides->fFlags |= GrXPOverridesForBatch::kReadsColor_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700143 }
144 if (GrColor_ILLEGAL != overrideColor) {
ethannicholasff210322015-11-24 12:10:10 -0800145 overrides->fFlags |= GrXPOverridesForBatch::kUseOverrideColor_Flag;
146 overrides->fOverrideColor = overrideColor;
bsalomon7765a472015-07-08 11:26:37 -0700147 }
148 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag)) {
ethannicholasff210322015-11-24 12:10:10 -0800149 overrides->fFlags |= GrXPOverridesForBatch::kReadsCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700150 }
151 if (usesLocalCoords) {
ethannicholasff210322015-11-24 12:10:10 -0800152 overrides->fFlags |= GrXPOverridesForBatch::kReadsLocalCoords_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700153 }
154 if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) {
halcanary9d524f22016-03-29 09:03:52 -0700155 overrides->fFlags |= GrXPOverridesForBatch::kCanTweakAlphaForCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700156 }
bsalomon2d563032015-08-13 07:08:31 -0700157
158 GrXPFactory::InvariantBlendedColor blendedColor;
egdanielc4b72722015-11-23 13:20:41 -0800159 if (xpFactory) {
ethannicholasff210322015-11-24 12:10:10 -0800160 xpFactory->getInvariantBlendedColor(args.fOpts.fColorPOI, &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800161 } else {
ethannicholasff210322015-11-24 12:10:10 -0800162 GrPorterDuffXPFactory::SrcOverInvariantBlendedColor(args.fOpts.fColorPOI.color(),
163 args.fOpts.fColorPOI.validFlags(),
164 args.fOpts.fColorPOI.isOpaque(),
halcanary9d524f22016-03-29 09:03:52 -0700165 &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800166 }
bsalomon2d563032015-08-13 07:08:31 -0700167 if (blendedColor.fWillBlendWithDst) {
ethannicholasff210322015-11-24 12:10:10 -0800168 overrides->fFlags |= GrXPOverridesForBatch::kWillColorBlendWithDst_Flag;
bsalomon2d563032015-08-13 07:08:31 -0700169 }
170
bsalomonc6998732015-08-10 12:01:15 -0700171 return pipeline;
egdanielc0648242014-09-22 13:17:02 -0700172}
173
robertphillips498d7ac2015-10-30 10:11:30 -0700174static void add_dependencies_for_processor(const GrFragmentProcessor* proc, GrRenderTarget* rt) {
175 for (int i = 0; i < proc->numChildProcessors(); ++i) {
176 // need to recurse
177 add_dependencies_for_processor(&proc->childProcessor(i), rt);
178 }
179
180 for (int i = 0; i < proc->numTextures(); ++i) {
181 GrTexture* texture = proc->textureAccess(i).getTexture();
182 SkASSERT(rt->getLastDrawTarget());
183 rt->getLastDrawTarget()->addDependency(texture);
184 }
185}
186
187void GrPipeline::addDependenciesTo(GrRenderTarget* rt) const {
188 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
189 add_dependencies_for_processor(fFragmentProcessors[i].get(), rt);
190 }
191
bsalomon2047b782015-12-21 13:12:54 -0800192 const GrXferProcessor& xfer = this->getXferProcessor();
robertphillips498d7ac2015-10-30 10:11:30 -0700193
bsalomon2047b782015-12-21 13:12:54 -0800194 for (int i = 0; i < xfer.numTextures(); ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700195 GrTexture* texture = xfer.textureAccess(i).getTexture();
bsalomon2047b782015-12-21 13:12:54 -0800196 SkASSERT(rt->getLastDrawTarget());
197 rt->getLastDrawTarget()->addDependency(texture);
robertphillips498d7ac2015-10-30 10:11:30 -0700198 }
199}
200
egdaniel8dd688b2015-01-22 10:16:09 -0800201void GrPipeline::adjustProgramFromOptimizations(const GrPipelineBuilder& pipelineBuilder,
202 GrXferProcessor::OptFlags flags,
203 const GrProcOptInfo& colorPOI,
204 const GrProcOptInfo& coveragePOI,
bsalomonac856c92015-08-27 06:30:17 -0700205 int* firstColorProcessorIdx,
206 int* firstCoverageProcessorIdx) {
egdaniel56cf6dc2015-11-30 10:15:58 -0800207 fIgnoresCoverage = SkToBool(flags & GrXferProcessor::kIgnoreCoverage_OptFlag);
egdaniela7dc0a82014-09-17 08:25:05 -0700208
joshualitt9b989322014-12-15 14:16:27 -0800209 if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) ||
210 (flags & GrXferProcessor::kOverrideColor_OptFlag)) {
bsalomonac856c92015-08-27 06:30:17 -0700211 *firstColorProcessorIdx = pipelineBuilder.numColorFragmentProcessors();
egdaniela7dc0a82014-09-17 08:25:05 -0700212 }
egdaniel95131432014-12-09 11:15:43 -0800213
joshualitt9b989322014-12-15 14:16:27 -0800214 if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) {
bsalomonac856c92015-08-27 06:30:17 -0700215 *firstCoverageProcessorIdx = pipelineBuilder.numCoverageFragmentProcessors();
egdaniela7dc0a82014-09-17 08:25:05 -0700216 }
217}
218
egdaniel89af44a2014-09-26 06:15:04 -0700219////////////////////////////////////////////////////////////////////////////////
220
bsalomoncb02b382015-08-12 11:14:50 -0700221bool GrPipeline::AreEqual(const GrPipeline& a, const GrPipeline& b,
222 bool ignoreCoordTransforms) {
223 SkASSERT(&a != &b);
joshualitt8cab9a72015-07-16 09:13:50 -0700224
bsalomoncb02b382015-08-12 11:14:50 -0700225 if (a.getRenderTarget() != b.getRenderTarget() ||
bsalomonac856c92015-08-27 06:30:17 -0700226 a.fFragmentProcessors.count() != b.fFragmentProcessors.count() ||
227 a.fNumColorProcessors != b.fNumColorProcessors ||
bsalomoncb02b382015-08-12 11:14:50 -0700228 a.fScissorState != b.fScissorState ||
229 a.fFlags != b.fFlags ||
230 a.fStencilSettings != b.fStencilSettings ||
csmartdaltonafd63402016-07-06 09:47:38 -0700231 a.fDrawFace != b.fDrawFace ||
232 a.fIgnoresCoverage != b.fIgnoresCoverage) {
egdaniel89af44a2014-09-26 06:15:04 -0700233 return false;
234 }
235
bsalomon2047b782015-12-21 13:12:54 -0800236 // Most of the time both are nullptr
237 if (a.fXferProcessor.get() || b.fXferProcessor.get()) {
238 if (!a.getXferProcessor().isEqual(b.getXferProcessor())) {
239 return false;
240 }
egdanielc2304142014-12-11 13:15:13 -0800241 }
242
bsalomonac856c92015-08-27 06:30:17 -0700243 for (int i = 0; i < a.numFragmentProcessors(); i++) {
244 if (!a.getFragmentProcessor(i).isEqual(b.getFragmentProcessor(i), ignoreCoordTransforms)) {
egdaniel89af44a2014-09-26 06:15:04 -0700245 return false;
246 }
247 }
egdaniel89af44a2014-09-26 06:15:04 -0700248 return true;
249}