blob: 875c50e88c0961393ec1b09e688a6c32b09a1d09 [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"
Brian Osman11052242016-10-27 14:47:55 -040011#include "GrRenderTargetContext.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"
Robert Phillipsf2361d22016-10-25 14:20:06 -040015#include "GrRenderTargetOpList.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;
csmartdaltonc633abb2016-11-01 08:55:55 -070024 const GrUserStencilSettings* userStencil = builder.getUserStencil();
25 GrRenderTarget* rt = args.fRenderTargetContext->accessRenderTarget();
bsalomonc6998732015-08-10 12:01:15 -070026
cdalton93a379b2016-05-11 13:58:08 -070027 GrPipeline* pipeline = new (memory) GrPipeline;
robertphillips55fdccc2016-06-06 06:16:20 -070028 pipeline->fRenderTarget.reset(rt);
cdalton93a379b2016-05-11 13:58:08 -070029 SkASSERT(pipeline->fRenderTarget);
30 pipeline->fScissorState = *args.fScissor;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070031 pipeline->fWindowRectsState = *args.fWindowRectsState;
csmartdaltonc633abb2016-11-01 08:55:55 -070032 pipeline->fUserStencilSettings = userStencil;
cdalton93a379b2016-05-11 13:58:08 -070033 pipeline->fDrawFace = builder.getDrawFace();
34
35 pipeline->fFlags = 0;
36 if (builder.isHWAntialias()) {
37 pipeline->fFlags |= kHWAA_Flag;
38 }
39 if (builder.snapVerticesToPixelCenters()) {
40 pipeline->fFlags |= kSnapVertices_Flag;
41 }
42 if (builder.getDisableOutputConversionToSRGB()) {
43 pipeline->fFlags |= kDisableOutputConversionToSRGB_Flag;
44 }
45 if (builder.getAllowSRGBInputs()) {
46 pipeline->fFlags |= kAllowSRGBInputs_Flag;
47 }
dvonbeck9b03e7b2016-08-01 11:01:56 -070048 if (builder.getUsesDistanceVectorField()) {
49 pipeline->fFlags |= kUsesDistanceVectorField_Flag;
50 }
cdalton193d9cf2016-05-12 11:52:02 -070051 if (args.fHasStencilClip) {
52 pipeline->fFlags |= kHasStencilClip_Flag;
53 }
csmartdaltonc633abb2016-11-01 08:55:55 -070054 if (!userStencil->isDisabled(args.fHasStencilClip)) {
55 pipeline->fFlags |= kStencilEnabled_Flag;
56 }
cdalton93a379b2016-05-11 13:58:08 -070057
egdaniel95131432014-12-09 11:15:43 -080058 // Create XferProcessor from DS's XPFactory
Brian Osman11052242016-10-27 14:47:55 -040059 bool hasMixedSamples = args.fRenderTargetContext->hasMixedSamples() &&
csmartdaltonc633abb2016-11-01 08:55:55 -070060 (builder.isHWAntialias() || pipeline->isStencilEnabled());
egdanielc4b72722015-11-23 13:20:41 -080061 const GrXPFactory* xpFactory = builder.getXPFactory();
Hal Canary144caf52016-11-07 17:57:18 -050062 sk_sp<GrXferProcessor> xferProcessor;
egdanielc4b72722015-11-23 13:20:41 -080063 if (xpFactory) {
ethannicholasde4166a2015-11-30 08:57:38 -080064 xferProcessor.reset(xpFactory->createXferProcessor(args.fOpts,
cdalton3ccf2e72016-05-06 09:41:16 -070065 hasMixedSamples,
egdanielc4b72722015-11-23 13:20:41 -080066 &args.fDstTexture,
67 *args.fCaps));
bsalomon2047b782015-12-21 13:12:54 -080068 if (!xferProcessor) {
cdalton93a379b2016-05-11 13:58:08 -070069 pipeline->~GrPipeline();
bsalomon2047b782015-12-21 13:12:54 -080070 return nullptr;
71 }
egdanielc4b72722015-11-23 13:20:41 -080072 } else {
bsalomon2047b782015-12-21 13:12:54 -080073 // This may return nullptr in the common case of src-over implemented using hw blending.
egdanielc4b72722015-11-23 13:20:41 -080074 xferProcessor.reset(GrPorterDuffXPFactory::CreateSrcOverXferProcessor(
75 *args.fCaps,
ethannicholasde4166a2015-11-30 08:57:38 -080076 args.fOpts,
cdalton3ccf2e72016-05-06 09:41:16 -070077 hasMixedSamples,
egdanielc4b72722015-11-23 13:20:41 -080078 &args.fDstTexture));
79 }
cdalton3ccf2e72016-05-06 09:41:16 -070080 GrColor overrideColor = GrColor_ILLEGAL;
ethannicholasff210322015-11-24 12:10:10 -080081 if (args.fOpts.fColorPOI.firstEffectiveProcessorIndex() != 0) {
82 overrideColor = args.fOpts.fColorPOI.inputColorToFirstEffectiveProccesor();
joshualitt9b989322014-12-15 14:16:27 -080083 }
84
bsalomon7765a472015-07-08 11:26:37 -070085 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
egdaniel95131432014-12-09 11:15:43 -080086
halcanary9d524f22016-03-29 09:03:52 -070087 const GrXferProcessor* xpForOpts = xferProcessor ? xferProcessor.get() :
bsalomon2047b782015-12-21 13:12:54 -080088 &GrPorterDuffXPFactory::SimpleSrcOverXP();
89 optFlags = xpForOpts->getOptimizations(args.fOpts,
csmartdaltonc633abb2016-11-01 08:55:55 -070090 userStencil->doesWrite(args.fHasStencilClip),
bsalomon2047b782015-12-21 13:12:54 -080091 &overrideColor,
92 *args.fCaps);
bsalomonc8d3f572015-08-13 06:44:04 -070093
94 // When path rendering the stencil settings are not always set on the GrPipelineBuilder
95 // so we must check the draw type. In cases where we will skip drawing we simply return a
96 // null GrPipeline.
97 if (GrXferProcessor::kSkipDraw_OptFlag & optFlags) {
cdalton93a379b2016-05-11 13:58:08 -070098 pipeline->~GrPipeline();
bsalomonc8d3f572015-08-13 06:44:04 -070099 return nullptr;
egdaniel95131432014-12-09 11:15:43 -0800100 }
bsalomonb03c4a32014-11-20 09:56:11 -0800101
bsalomon7765a472015-07-08 11:26:37 -0700102 // No need to have an override color if it isn't even going to be used.
103 if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) {
104 overrideColor = GrColor_ILLEGAL;
105 }
106
Hal Canary144caf52016-11-07 17:57:18 -0500107 pipeline->fXferProcessor.reset(xferProcessor.get());
bsalomonb03c4a32014-11-20 09:56:11 -0800108
ethannicholasff210322015-11-24 12:10:10 -0800109 int firstColorProcessorIdx = args.fOpts.fColorPOI.firstEffectiveProcessorIndex();
egdaniel912b3d22014-11-17 07:45:53 -0800110
bsalomonac856c92015-08-27 06:30:17 -0700111 // TODO: Once we can handle single or four channel input into coverage GrFragmentProcessors
112 // then we can use GrPipelineBuilder's coverageProcInfo (like color above) to set this initial
113 // information.
114 int firstCoverageProcessorIdx = 0;
egdaniel912b3d22014-11-17 07:45:53 -0800115
halcanary9d524f22016-03-29 09:03:52 -0700116 pipeline->adjustProgramFromOptimizations(builder, optFlags, args.fOpts.fColorPOI,
117 args.fOpts.fCoveragePOI, &firstColorProcessorIdx,
ethannicholasff210322015-11-24 12:10:10 -0800118 &firstCoverageProcessorIdx);
egdaniel95131432014-12-09 11:15:43 -0800119
joshualitt290c09b2014-12-19 13:45:20 -0800120 bool usesLocalCoords = false;
121
bsalomonac856c92015-08-27 06:30:17 -0700122 // Copy GrFragmentProcessors from GrPipelineBuilder to Pipeline
123 pipeline->fNumColorProcessors = builder.numColorFragmentProcessors() - firstColorProcessorIdx;
124 int numTotalProcessors = pipeline->fNumColorProcessors +
125 builder.numCoverageFragmentProcessors() - firstCoverageProcessorIdx;
126 pipeline->fFragmentProcessors.reset(numTotalProcessors);
127 int currFPIdx = 0;
128 for (int i = firstColorProcessorIdx; i < builder.numColorFragmentProcessors();
129 ++i, ++currFPIdx) {
130 const GrFragmentProcessor* fp = builder.getColorFragmentProcessor(i);
131 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700132 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
bsalomonae59b772014-11-19 08:23:49 -0800133 }
egdaniel95131432014-12-09 11:15:43 -0800134
bsalomonac856c92015-08-27 06:30:17 -0700135 for (int i = firstCoverageProcessorIdx; i < builder.numCoverageFragmentProcessors();
136 ++i, ++currFPIdx) {
137 const GrFragmentProcessor* fp = builder.getCoverageFragmentProcessor(i);
138 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700139 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
egdaniel9cf45bf2014-10-08 06:49:10 -0700140 }
joshualitt79f8fae2014-10-28 17:59:26 -0700141
bsalomon7765a472015-07-08 11:26:37 -0700142 // Setup info we need to pass to GrPrimitiveProcessors that are used with this GrPipeline.
ethannicholasff210322015-11-24 12:10:10 -0800143 overrides->fFlags = 0;
bsalomon7765a472015-07-08 11:26:37 -0700144 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag)) {
ethannicholasff210322015-11-24 12:10:10 -0800145 overrides->fFlags |= GrXPOverridesForBatch::kReadsColor_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700146 }
147 if (GrColor_ILLEGAL != overrideColor) {
ethannicholasff210322015-11-24 12:10:10 -0800148 overrides->fFlags |= GrXPOverridesForBatch::kUseOverrideColor_Flag;
149 overrides->fOverrideColor = overrideColor;
bsalomon7765a472015-07-08 11:26:37 -0700150 }
151 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag)) {
ethannicholasff210322015-11-24 12:10:10 -0800152 overrides->fFlags |= GrXPOverridesForBatch::kReadsCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700153 }
154 if (usesLocalCoords) {
ethannicholasff210322015-11-24 12:10:10 -0800155 overrides->fFlags |= GrXPOverridesForBatch::kReadsLocalCoords_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700156 }
157 if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) {
halcanary9d524f22016-03-29 09:03:52 -0700158 overrides->fFlags |= GrXPOverridesForBatch::kCanTweakAlphaForCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700159 }
bsalomon2d563032015-08-13 07:08:31 -0700160
161 GrXPFactory::InvariantBlendedColor blendedColor;
egdanielc4b72722015-11-23 13:20:41 -0800162 if (xpFactory) {
ethannicholasff210322015-11-24 12:10:10 -0800163 xpFactory->getInvariantBlendedColor(args.fOpts.fColorPOI, &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800164 } else {
ethannicholasff210322015-11-24 12:10:10 -0800165 GrPorterDuffXPFactory::SrcOverInvariantBlendedColor(args.fOpts.fColorPOI.color(),
166 args.fOpts.fColorPOI.validFlags(),
167 args.fOpts.fColorPOI.isOpaque(),
halcanary9d524f22016-03-29 09:03:52 -0700168 &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800169 }
bsalomon2d563032015-08-13 07:08:31 -0700170 if (blendedColor.fWillBlendWithDst) {
ethannicholasff210322015-11-24 12:10:10 -0800171 overrides->fFlags |= GrXPOverridesForBatch::kWillColorBlendWithDst_Flag;
bsalomon2d563032015-08-13 07:08:31 -0700172 }
173
bsalomonc6998732015-08-10 12:01:15 -0700174 return pipeline;
egdanielc0648242014-09-22 13:17:02 -0700175}
176
robertphillips498d7ac2015-10-30 10:11:30 -0700177static void add_dependencies_for_processor(const GrFragmentProcessor* proc, GrRenderTarget* rt) {
bsalomonb58a2b42016-09-26 06:55:02 -0700178 GrFragmentProcessor::TextureAccessIter iter(proc);
179 while (const GrTextureAccess* access = iter.next()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400180 SkASSERT(rt->getLastOpList());
181 rt->getLastOpList()->addDependency(access->getTexture());
robertphillips498d7ac2015-10-30 10:11:30 -0700182 }
183}
184
185void GrPipeline::addDependenciesTo(GrRenderTarget* rt) const {
186 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
187 add_dependencies_for_processor(fFragmentProcessors[i].get(), rt);
188 }
189
bsalomon2047b782015-12-21 13:12:54 -0800190 const GrXferProcessor& xfer = this->getXferProcessor();
robertphillips498d7ac2015-10-30 10:11:30 -0700191
bsalomon2047b782015-12-21 13:12:54 -0800192 for (int i = 0; i < xfer.numTextures(); ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700193 GrTexture* texture = xfer.textureAccess(i).getTexture();
Robert Phillipsf2361d22016-10-25 14:20:06 -0400194 SkASSERT(rt->getLastOpList());
195 rt->getLastOpList()->addDependency(texture);
robertphillips498d7ac2015-10-30 10:11:30 -0700196 }
197}
198
egdaniel8dd688b2015-01-22 10:16:09 -0800199void GrPipeline::adjustProgramFromOptimizations(const GrPipelineBuilder& pipelineBuilder,
200 GrXferProcessor::OptFlags flags,
201 const GrProcOptInfo& colorPOI,
202 const GrProcOptInfo& coveragePOI,
bsalomonac856c92015-08-27 06:30:17 -0700203 int* firstColorProcessorIdx,
204 int* firstCoverageProcessorIdx) {
egdaniel56cf6dc2015-11-30 10:15:58 -0800205 fIgnoresCoverage = SkToBool(flags & GrXferProcessor::kIgnoreCoverage_OptFlag);
egdaniela7dc0a82014-09-17 08:25:05 -0700206
joshualitt9b989322014-12-15 14:16:27 -0800207 if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) ||
208 (flags & GrXferProcessor::kOverrideColor_OptFlag)) {
bsalomonac856c92015-08-27 06:30:17 -0700209 *firstColorProcessorIdx = pipelineBuilder.numColorFragmentProcessors();
egdaniela7dc0a82014-09-17 08:25:05 -0700210 }
egdaniel95131432014-12-09 11:15:43 -0800211
joshualitt9b989322014-12-15 14:16:27 -0800212 if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) {
bsalomonac856c92015-08-27 06:30:17 -0700213 *firstCoverageProcessorIdx = pipelineBuilder.numCoverageFragmentProcessors();
egdaniela7dc0a82014-09-17 08:25:05 -0700214 }
215}
216
egdaniel89af44a2014-09-26 06:15:04 -0700217////////////////////////////////////////////////////////////////////////////////
218
bsalomon7312ff82016-09-12 08:55:38 -0700219bool GrPipeline::AreEqual(const GrPipeline& a, const GrPipeline& b) {
bsalomoncb02b382015-08-12 11:14:50 -0700220 SkASSERT(&a != &b);
joshualitt8cab9a72015-07-16 09:13:50 -0700221
bsalomoncb02b382015-08-12 11:14:50 -0700222 if (a.getRenderTarget() != b.getRenderTarget() ||
bsalomonac856c92015-08-27 06:30:17 -0700223 a.fFragmentProcessors.count() != b.fFragmentProcessors.count() ||
224 a.fNumColorProcessors != b.fNumColorProcessors ||
bsalomoncb02b382015-08-12 11:14:50 -0700225 a.fScissorState != b.fScissorState ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700226 !a.fWindowRectsState.cheapEqualTo(b.fWindowRectsState) ||
bsalomoncb02b382015-08-12 11:14:50 -0700227 a.fFlags != b.fFlags ||
csmartdaltonc633abb2016-11-01 08:55:55 -0700228 a.fUserStencilSettings != b.fUserStencilSettings ||
csmartdaltonafd63402016-07-06 09:47:38 -0700229 a.fDrawFace != b.fDrawFace ||
230 a.fIgnoresCoverage != b.fIgnoresCoverage) {
egdaniel89af44a2014-09-26 06:15:04 -0700231 return false;
232 }
233
bsalomon2047b782015-12-21 13:12:54 -0800234 // Most of the time both are nullptr
235 if (a.fXferProcessor.get() || b.fXferProcessor.get()) {
236 if (!a.getXferProcessor().isEqual(b.getXferProcessor())) {
237 return false;
238 }
egdanielc2304142014-12-11 13:15:13 -0800239 }
240
bsalomonac856c92015-08-27 06:30:17 -0700241 for (int i = 0; i < a.numFragmentProcessors(); i++) {
bsalomon7312ff82016-09-12 08:55:38 -0700242 if (!a.getFragmentProcessor(i).isEqual(b.getFragmentProcessor(i))) {
egdaniel89af44a2014-09-26 06:15:04 -0700243 return false;
244 }
245 }
egdaniel89af44a2014-09-26 06:15:04 -0700246 return true;
247}