blob: d8e2e5d98ef28c3e81bc2ce81f4b14359df0edb4 [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"
robertphillips498d7ac2015-10-30 10:11:30 -070011#include "GrDrawTarget.h"
joshualitt4973d9d2014-11-08 09:24:25 -080012#include "GrGpu.h"
egdaniel8dd688b2015-01-22 10:16:09 -080013#include "GrPipelineBuilder.h"
egdanielb6cbc382014-11-13 11:00:34 -080014#include "GrProcOptInfo.h"
egdaniel95131432014-12-09 11:15:43 -080015#include "GrXferProcessor.h"
egdaniel3658f382014-09-15 07:01:59 -070016
joshualitt74417822015-08-07 11:42:16 -070017#include "batches/GrBatch.h"
18
bsalomona387a112015-08-11 14:47:42 -070019GrPipeline* GrPipeline::CreateAt(void* memory, const CreateArgs& args,
ethannicholasff210322015-11-24 12:10:10 -080020 GrXPOverridesForBatch* overrides) {
bsalomona387a112015-08-11 14:47:42 -070021 const GrPipelineBuilder& builder = *args.fPipelineBuilder;
bsalomonc6998732015-08-10 12:01:15 -070022
egdaniel95131432014-12-09 11:15:43 -080023 // Create XferProcessor from DS's XPFactory
egdanielc4b72722015-11-23 13:20:41 -080024 const GrXPFactory* xpFactory = builder.getXPFactory();
25 SkAutoTUnref<GrXferProcessor> xferProcessor;
26 if (xpFactory) {
ethannicholasde4166a2015-11-30 08:57:38 -080027 xferProcessor.reset(xpFactory->createXferProcessor(args.fOpts,
egdanielc4b72722015-11-23 13:20:41 -080028 builder.hasMixedSamples(),
29 &args.fDstTexture,
30 *args.fCaps));
bsalomon2047b782015-12-21 13:12:54 -080031 if (!xferProcessor) {
32 return nullptr;
33 }
egdanielc4b72722015-11-23 13:20:41 -080034 } else {
bsalomon2047b782015-12-21 13:12:54 -080035 // This may return nullptr in the common case of src-over implemented using hw blending.
egdanielc4b72722015-11-23 13:20:41 -080036 xferProcessor.reset(GrPorterDuffXPFactory::CreateSrcOverXferProcessor(
37 *args.fCaps,
ethannicholasde4166a2015-11-30 08:57:38 -080038 args.fOpts,
egdanielc4b72722015-11-23 13:20:41 -080039 builder.hasMixedSamples(),
40 &args.fDstTexture));
41 }
bsalomon2047b782015-12-21 13:12:54 -080042 GrColor overrideColor = GrColor_ILLEGAL;
ethannicholasff210322015-11-24 12:10:10 -080043 if (args.fOpts.fColorPOI.firstEffectiveProcessorIndex() != 0) {
44 overrideColor = args.fOpts.fColorPOI.inputColorToFirstEffectiveProccesor();
joshualitt9b989322014-12-15 14:16:27 -080045 }
46
bsalomon7765a472015-07-08 11:26:37 -070047 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
egdaniel95131432014-12-09 11:15:43 -080048
halcanary9d524f22016-03-29 09:03:52 -070049 const GrXferProcessor* xpForOpts = xferProcessor ? xferProcessor.get() :
bsalomon2047b782015-12-21 13:12:54 -080050 &GrPorterDuffXPFactory::SimpleSrcOverXP();
51 optFlags = xpForOpts->getOptimizations(args.fOpts,
52 builder.getStencil().doesWrite(),
53 &overrideColor,
54 *args.fCaps);
bsalomonc8d3f572015-08-13 06:44:04 -070055
56 // When path rendering the stencil settings are not always set on the GrPipelineBuilder
57 // so we must check the draw type. In cases where we will skip drawing we simply return a
58 // null GrPipeline.
59 if (GrXferProcessor::kSkipDraw_OptFlag & optFlags) {
60 return nullptr;
egdaniel95131432014-12-09 11:15:43 -080061 }
bsalomonb03c4a32014-11-20 09:56:11 -080062
bsalomon7765a472015-07-08 11:26:37 -070063 // No need to have an override color if it isn't even going to be used.
64 if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) {
65 overrideColor = GrColor_ILLEGAL;
66 }
67
halcanary385fe4d2015-08-26 13:07:48 -070068 GrPipeline* pipeline = new (memory) GrPipeline;
egdanielc4b72722015-11-23 13:20:41 -080069 pipeline->fXferProcessor.reset(xferProcessor);
bsalomonb03c4a32014-11-20 09:56:11 -080070
bsalomonc6998732015-08-10 12:01:15 -070071 pipeline->fRenderTarget.reset(builder.fRenderTarget.get());
72 SkASSERT(pipeline->fRenderTarget);
cdaltond4727922015-11-10 12:49:06 -080073 pipeline->fScissorState = *args.fScissor;
bsalomonc6998732015-08-10 12:01:15 -070074 pipeline->fStencilSettings = builder.getStencil();
75 pipeline->fDrawFace = builder.getDrawFace();
joshualitt9176e2c2014-11-20 07:28:52 -080076
bsalomonc6998732015-08-10 12:01:15 -070077 pipeline->fFlags = 0;
cdaltond4727922015-11-10 12:49:06 -080078 if (builder.isHWAntialias()) {
bsalomonc6998732015-08-10 12:01:15 -070079 pipeline->fFlags |= kHWAA_Flag;
bsalomon04ddf892014-11-19 12:36:22 -080080 }
bsalomonc6998732015-08-10 12:01:15 -070081 if (builder.snapVerticesToPixelCenters()) {
82 pipeline->fFlags |= kSnapVertices_Flag;
bsalomond79c5492015-04-27 10:07:04 -070083 }
brianosman64d094d2016-03-25 06:01:59 -070084 if (builder.getDisableOutputConversionToSRGB()) {
85 pipeline->fFlags |= kDisableOutputConversionToSRGB_Flag;
86 }
bsalomon04ddf892014-11-19 12:36:22 -080087
ethannicholasff210322015-11-24 12:10:10 -080088 int firstColorProcessorIdx = args.fOpts.fColorPOI.firstEffectiveProcessorIndex();
egdaniel912b3d22014-11-17 07:45:53 -080089
bsalomonac856c92015-08-27 06:30:17 -070090 // TODO: Once we can handle single or four channel input into coverage GrFragmentProcessors
91 // then we can use GrPipelineBuilder's coverageProcInfo (like color above) to set this initial
92 // information.
93 int firstCoverageProcessorIdx = 0;
egdaniel912b3d22014-11-17 07:45:53 -080094
halcanary9d524f22016-03-29 09:03:52 -070095 pipeline->adjustProgramFromOptimizations(builder, optFlags, args.fOpts.fColorPOI,
96 args.fOpts.fCoveragePOI, &firstColorProcessorIdx,
ethannicholasff210322015-11-24 12:10:10 -080097 &firstCoverageProcessorIdx);
egdaniel95131432014-12-09 11:15:43 -080098
joshualitt290c09b2014-12-19 13:45:20 -080099 bool usesLocalCoords = false;
100
bsalomonac856c92015-08-27 06:30:17 -0700101 // Copy GrFragmentProcessors from GrPipelineBuilder to Pipeline
102 pipeline->fNumColorProcessors = builder.numColorFragmentProcessors() - firstColorProcessorIdx;
103 int numTotalProcessors = pipeline->fNumColorProcessors +
104 builder.numCoverageFragmentProcessors() - firstCoverageProcessorIdx;
105 pipeline->fFragmentProcessors.reset(numTotalProcessors);
106 int currFPIdx = 0;
107 for (int i = firstColorProcessorIdx; i < builder.numColorFragmentProcessors();
108 ++i, ++currFPIdx) {
109 const GrFragmentProcessor* fp = builder.getColorFragmentProcessor(i);
110 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700111 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
bsalomonae59b772014-11-19 08:23:49 -0800112 }
egdaniel95131432014-12-09 11:15:43 -0800113
bsalomonac856c92015-08-27 06:30:17 -0700114 for (int i = firstCoverageProcessorIdx; i < builder.numCoverageFragmentProcessors();
115 ++i, ++currFPIdx) {
116 const GrFragmentProcessor* fp = builder.getCoverageFragmentProcessor(i);
117 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700118 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
egdaniel9cf45bf2014-10-08 06:49:10 -0700119 }
joshualitt79f8fae2014-10-28 17:59:26 -0700120
bsalomon7765a472015-07-08 11:26:37 -0700121 // Setup info we need to pass to GrPrimitiveProcessors that are used with this GrPipeline.
ethannicholasff210322015-11-24 12:10:10 -0800122 overrides->fFlags = 0;
bsalomon7765a472015-07-08 11:26:37 -0700123 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag)) {
ethannicholasff210322015-11-24 12:10:10 -0800124 overrides->fFlags |= GrXPOverridesForBatch::kReadsColor_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700125 }
126 if (GrColor_ILLEGAL != overrideColor) {
ethannicholasff210322015-11-24 12:10:10 -0800127 overrides->fFlags |= GrXPOverridesForBatch::kUseOverrideColor_Flag;
128 overrides->fOverrideColor = overrideColor;
bsalomon7765a472015-07-08 11:26:37 -0700129 }
130 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag)) {
ethannicholasff210322015-11-24 12:10:10 -0800131 overrides->fFlags |= GrXPOverridesForBatch::kReadsCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700132 }
133 if (usesLocalCoords) {
ethannicholasff210322015-11-24 12:10:10 -0800134 overrides->fFlags |= GrXPOverridesForBatch::kReadsLocalCoords_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700135 }
136 if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) {
halcanary9d524f22016-03-29 09:03:52 -0700137 overrides->fFlags |= GrXPOverridesForBatch::kCanTweakAlphaForCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700138 }
bsalomon2d563032015-08-13 07:08:31 -0700139
140 GrXPFactory::InvariantBlendedColor blendedColor;
egdanielc4b72722015-11-23 13:20:41 -0800141 if (xpFactory) {
ethannicholasff210322015-11-24 12:10:10 -0800142 xpFactory->getInvariantBlendedColor(args.fOpts.fColorPOI, &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800143 } else {
ethannicholasff210322015-11-24 12:10:10 -0800144 GrPorterDuffXPFactory::SrcOverInvariantBlendedColor(args.fOpts.fColorPOI.color(),
145 args.fOpts.fColorPOI.validFlags(),
146 args.fOpts.fColorPOI.isOpaque(),
halcanary9d524f22016-03-29 09:03:52 -0700147 &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800148 }
bsalomon2d563032015-08-13 07:08:31 -0700149 if (blendedColor.fWillBlendWithDst) {
ethannicholasff210322015-11-24 12:10:10 -0800150 overrides->fFlags |= GrXPOverridesForBatch::kWillColorBlendWithDst_Flag;
bsalomon2d563032015-08-13 07:08:31 -0700151 }
152
bsalomonc6998732015-08-10 12:01:15 -0700153 return pipeline;
egdanielc0648242014-09-22 13:17:02 -0700154}
155
robertphillips498d7ac2015-10-30 10:11:30 -0700156static void add_dependencies_for_processor(const GrFragmentProcessor* proc, GrRenderTarget* rt) {
157 for (int i = 0; i < proc->numChildProcessors(); ++i) {
158 // need to recurse
159 add_dependencies_for_processor(&proc->childProcessor(i), rt);
160 }
161
162 for (int i = 0; i < proc->numTextures(); ++i) {
163 GrTexture* texture = proc->textureAccess(i).getTexture();
164 SkASSERT(rt->getLastDrawTarget());
165 rt->getLastDrawTarget()->addDependency(texture);
166 }
167}
168
169void GrPipeline::addDependenciesTo(GrRenderTarget* rt) const {
170 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
171 add_dependencies_for_processor(fFragmentProcessors[i].get(), rt);
172 }
173
bsalomon2047b782015-12-21 13:12:54 -0800174 const GrXferProcessor& xfer = this->getXferProcessor();
robertphillips498d7ac2015-10-30 10:11:30 -0700175
bsalomon2047b782015-12-21 13:12:54 -0800176 for (int i = 0; i < xfer.numTextures(); ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700177 GrTexture* texture = xfer.textureAccess(i).getTexture();
bsalomon2047b782015-12-21 13:12:54 -0800178 SkASSERT(rt->getLastDrawTarget());
179 rt->getLastDrawTarget()->addDependency(texture);
robertphillips498d7ac2015-10-30 10:11:30 -0700180 }
181}
182
egdaniel8dd688b2015-01-22 10:16:09 -0800183void GrPipeline::adjustProgramFromOptimizations(const GrPipelineBuilder& pipelineBuilder,
184 GrXferProcessor::OptFlags flags,
185 const GrProcOptInfo& colorPOI,
186 const GrProcOptInfo& coveragePOI,
bsalomonac856c92015-08-27 06:30:17 -0700187 int* firstColorProcessorIdx,
188 int* firstCoverageProcessorIdx) {
egdaniel56cf6dc2015-11-30 10:15:58 -0800189 fIgnoresCoverage = SkToBool(flags & GrXferProcessor::kIgnoreCoverage_OptFlag);
egdaniela7dc0a82014-09-17 08:25:05 -0700190
joshualitt9b989322014-12-15 14:16:27 -0800191 if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) ||
192 (flags & GrXferProcessor::kOverrideColor_OptFlag)) {
bsalomonac856c92015-08-27 06:30:17 -0700193 *firstColorProcessorIdx = pipelineBuilder.numColorFragmentProcessors();
egdaniela7dc0a82014-09-17 08:25:05 -0700194 }
egdaniel95131432014-12-09 11:15:43 -0800195
joshualitt9b989322014-12-15 14:16:27 -0800196 if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) {
bsalomonac856c92015-08-27 06:30:17 -0700197 *firstCoverageProcessorIdx = pipelineBuilder.numCoverageFragmentProcessors();
egdaniela7dc0a82014-09-17 08:25:05 -0700198 }
199}
200
egdaniel89af44a2014-09-26 06:15:04 -0700201////////////////////////////////////////////////////////////////////////////////
202
bsalomoncb02b382015-08-12 11:14:50 -0700203bool GrPipeline::AreEqual(const GrPipeline& a, const GrPipeline& b,
204 bool ignoreCoordTransforms) {
205 SkASSERT(&a != &b);
joshualitt8cab9a72015-07-16 09:13:50 -0700206
bsalomoncb02b382015-08-12 11:14:50 -0700207 if (a.getRenderTarget() != b.getRenderTarget() ||
bsalomonac856c92015-08-27 06:30:17 -0700208 a.fFragmentProcessors.count() != b.fFragmentProcessors.count() ||
209 a.fNumColorProcessors != b.fNumColorProcessors ||
bsalomoncb02b382015-08-12 11:14:50 -0700210 a.fScissorState != b.fScissorState ||
211 a.fFlags != b.fFlags ||
212 a.fStencilSettings != b.fStencilSettings ||
213 a.fDrawFace != b.fDrawFace) {
egdaniel89af44a2014-09-26 06:15:04 -0700214 return false;
215 }
216
bsalomon2047b782015-12-21 13:12:54 -0800217 // Most of the time both are nullptr
218 if (a.fXferProcessor.get() || b.fXferProcessor.get()) {
219 if (!a.getXferProcessor().isEqual(b.getXferProcessor())) {
220 return false;
221 }
egdanielc2304142014-12-11 13:15:13 -0800222 }
223
bsalomonac856c92015-08-27 06:30:17 -0700224 for (int i = 0; i < a.numFragmentProcessors(); i++) {
225 if (!a.getFragmentProcessor(i).isEqual(b.getFragmentProcessor(i), ignoreCoordTransforms)) {
egdaniel89af44a2014-09-26 06:15:04 -0700226 return false;
227 }
228 }
egdaniel89af44a2014-09-26 06:15:04 -0700229 return true;
230}