blob: ea3f0f234fbd3787a1410ec453aa3b1ad71d0292 [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
Brian Salomon89527432016-12-16 09:52:16 -050019#include "ops/GrOp.h"
joshualitt74417822015-08-07 11:42:16 -070020
bsalomona387a112015-08-11 14:47:42 -070021GrPipeline* GrPipeline::CreateAt(void* memory, const CreateArgs& args,
Brian Salomon92aee3d2016-12-21 09:20:25 -050022 GrPipelineOptimizations* optimizations) {
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();
Robert Phillipse60ad622016-11-17 10:22:48 -050026 if (!rt) {
27 return nullptr;
28 }
Brian Salomon92aee3d2016-12-21 09:20:25 -050029
cdalton93a379b2016-05-11 13:58:08 -070030 GrPipeline* pipeline = new (memory) GrPipeline;
robertphillips55fdccc2016-06-06 06:16:20 -070031 pipeline->fRenderTarget.reset(rt);
cdalton93a379b2016-05-11 13:58:08 -070032 SkASSERT(pipeline->fRenderTarget);
33 pipeline->fScissorState = *args.fScissor;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070034 pipeline->fWindowRectsState = *args.fWindowRectsState;
csmartdaltonc633abb2016-11-01 08:55:55 -070035 pipeline->fUserStencilSettings = userStencil;
cdalton93a379b2016-05-11 13:58:08 -070036 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 }
dvonbeck9b03e7b2016-08-01 11:01:56 -070051 if (builder.getUsesDistanceVectorField()) {
52 pipeline->fFlags |= kUsesDistanceVectorField_Flag;
53 }
cdalton193d9cf2016-05-12 11:52:02 -070054 if (args.fHasStencilClip) {
55 pipeline->fFlags |= kHasStencilClip_Flag;
56 }
csmartdaltonc633abb2016-11-01 08:55:55 -070057 if (!userStencil->isDisabled(args.fHasStencilClip)) {
58 pipeline->fFlags |= kStencilEnabled_Flag;
59 }
cdalton93a379b2016-05-11 13:58:08 -070060
egdaniel95131432014-12-09 11:15:43 -080061 // Create XferProcessor from DS's XPFactory
Brian Osman11052242016-10-27 14:47:55 -040062 bool hasMixedSamples = args.fRenderTargetContext->hasMixedSamples() &&
csmartdaltonc633abb2016-11-01 08:55:55 -070063 (builder.isHWAntialias() || pipeline->isStencilEnabled());
egdanielc4b72722015-11-23 13:20:41 -080064 const GrXPFactory* xpFactory = builder.getXPFactory();
Hal Canary144caf52016-11-07 17:57:18 -050065 sk_sp<GrXferProcessor> xferProcessor;
egdanielc4b72722015-11-23 13:20:41 -080066 if (xpFactory) {
Brian Salomon92aee3d2016-12-21 09:20:25 -050067 xferProcessor.reset(xpFactory->createXferProcessor(
68 args.fAnalysis, hasMixedSamples, &args.fDstTexture, *args.fCaps));
bsalomon2047b782015-12-21 13:12:54 -080069 if (!xferProcessor) {
cdalton93a379b2016-05-11 13:58:08 -070070 pipeline->~GrPipeline();
bsalomon2047b782015-12-21 13:12:54 -080071 return nullptr;
72 }
egdanielc4b72722015-11-23 13:20:41 -080073 } else {
bsalomon2047b782015-12-21 13:12:54 -080074 // This may return nullptr in the common case of src-over implemented using hw blending.
egdanielc4b72722015-11-23 13:20:41 -080075 xferProcessor.reset(GrPorterDuffXPFactory::CreateSrcOverXferProcessor(
Brian Salomon92aee3d2016-12-21 09:20:25 -050076 *args.fCaps, args.fAnalysis, hasMixedSamples, &args.fDstTexture));
egdanielc4b72722015-11-23 13:20:41 -080077 }
cdalton3ccf2e72016-05-06 09:41:16 -070078 GrColor overrideColor = GrColor_ILLEGAL;
Brian Salomon92aee3d2016-12-21 09:20:25 -050079 if (args.fAnalysis.fColorPOI.firstEffectiveProcessorIndex() != 0) {
80 overrideColor = args.fAnalysis.fColorPOI.inputColorToFirstEffectiveProccesor();
joshualitt9b989322014-12-15 14:16:27 -080081 }
82
bsalomon7765a472015-07-08 11:26:37 -070083 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
egdaniel95131432014-12-09 11:15:43 -080084
halcanary9d524f22016-03-29 09:03:52 -070085 const GrXferProcessor* xpForOpts = xferProcessor ? xferProcessor.get() :
bsalomon2047b782015-12-21 13:12:54 -080086 &GrPorterDuffXPFactory::SimpleSrcOverXP();
Brian Salomon92aee3d2016-12-21 09:20:25 -050087 optFlags = xpForOpts->getOptimizations(args.fAnalysis,
csmartdaltonc633abb2016-11-01 08:55:55 -070088 userStencil->doesWrite(args.fHasStencilClip),
bsalomon2047b782015-12-21 13:12:54 -080089 &overrideColor,
90 *args.fCaps);
bsalomonc8d3f572015-08-13 06:44:04 -070091
92 // When path rendering the stencil settings are not always set on the GrPipelineBuilder
93 // so we must check the draw type. In cases where we will skip drawing we simply return a
94 // null GrPipeline.
95 if (GrXferProcessor::kSkipDraw_OptFlag & optFlags) {
cdalton93a379b2016-05-11 13:58:08 -070096 pipeline->~GrPipeline();
bsalomonc8d3f572015-08-13 06:44:04 -070097 return nullptr;
egdaniel95131432014-12-09 11:15:43 -080098 }
bsalomonb03c4a32014-11-20 09:56:11 -080099
bsalomon7765a472015-07-08 11:26:37 -0700100 // No need to have an override color if it isn't even going to be used.
101 if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) {
102 overrideColor = GrColor_ILLEGAL;
103 }
104
Hal Canary144caf52016-11-07 17:57:18 -0500105 pipeline->fXferProcessor.reset(xferProcessor.get());
bsalomonb03c4a32014-11-20 09:56:11 -0800106
Brian Salomon92aee3d2016-12-21 09:20:25 -0500107 int firstColorProcessorIdx = args.fAnalysis.fColorPOI.firstEffectiveProcessorIndex();
egdaniel912b3d22014-11-17 07:45:53 -0800108
bsalomonac856c92015-08-27 06:30:17 -0700109 // TODO: Once we can handle single or four channel input into coverage GrFragmentProcessors
110 // then we can use GrPipelineBuilder's coverageProcInfo (like color above) to set this initial
111 // information.
112 int firstCoverageProcessorIdx = 0;
egdaniel912b3d22014-11-17 07:45:53 -0800113
Brian Salomonbfd51832017-01-04 13:22:08 -0500114 if ((optFlags & GrXferProcessor::kIgnoreColor_OptFlag) ||
115 (optFlags & GrXferProcessor::kOverrideColor_OptFlag)) {
116 firstColorProcessorIdx = builder.numColorFragmentProcessors();
117 }
egdaniel95131432014-12-09 11:15:43 -0800118
joshualitt290c09b2014-12-19 13:45:20 -0800119 bool usesLocalCoords = false;
120
bsalomonac856c92015-08-27 06:30:17 -0700121 // Copy GrFragmentProcessors from GrPipelineBuilder to Pipeline
122 pipeline->fNumColorProcessors = builder.numColorFragmentProcessors() - firstColorProcessorIdx;
123 int numTotalProcessors = pipeline->fNumColorProcessors +
124 builder.numCoverageFragmentProcessors() - firstCoverageProcessorIdx;
125 pipeline->fFragmentProcessors.reset(numTotalProcessors);
126 int currFPIdx = 0;
127 for (int i = firstColorProcessorIdx; i < builder.numColorFragmentProcessors();
128 ++i, ++currFPIdx) {
129 const GrFragmentProcessor* fp = builder.getColorFragmentProcessor(i);
130 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700131 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
bsalomonae59b772014-11-19 08:23:49 -0800132 }
egdaniel95131432014-12-09 11:15:43 -0800133
bsalomonac856c92015-08-27 06:30:17 -0700134 for (int i = firstCoverageProcessorIdx; i < builder.numCoverageFragmentProcessors();
135 ++i, ++currFPIdx) {
136 const GrFragmentProcessor* fp = builder.getCoverageFragmentProcessor(i);
137 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700138 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
egdaniel9cf45bf2014-10-08 06:49:10 -0700139 }
joshualitt79f8fae2014-10-28 17:59:26 -0700140
bsalomon7765a472015-07-08 11:26:37 -0700141 // Setup info we need to pass to GrPrimitiveProcessors that are used with this GrPipeline.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500142 optimizations->fFlags = 0;
bsalomon7765a472015-07-08 11:26:37 -0700143 if (GrColor_ILLEGAL != overrideColor) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500144 optimizations->fFlags |= GrPipelineOptimizations::kUseOverrideColor_Flag;
145 optimizations->fOverrideColor = overrideColor;
bsalomon7765a472015-07-08 11:26:37 -0700146 }
bsalomon7765a472015-07-08 11:26:37 -0700147 if (usesLocalCoords) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500148 optimizations->fFlags |= GrPipelineOptimizations::kReadsLocalCoords_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700149 }
150 if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500151 optimizations->fFlags |= GrPipelineOptimizations::kCanTweakAlphaForCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700152 }
bsalomon2d563032015-08-13 07:08:31 -0700153
154 GrXPFactory::InvariantBlendedColor blendedColor;
egdanielc4b72722015-11-23 13:20:41 -0800155 if (xpFactory) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500156 xpFactory->getInvariantBlendedColor(args.fAnalysis.fColorPOI, &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800157 } else {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500158 GrPorterDuffXPFactory::SrcOverInvariantBlendedColor(args.fAnalysis.fColorPOI.color(),
159 args.fAnalysis.fColorPOI.validFlags(),
160 args.fAnalysis.fColorPOI.isOpaque(),
halcanary9d524f22016-03-29 09:03:52 -0700161 &blendedColor);
egdanielc4b72722015-11-23 13:20:41 -0800162 }
bsalomon2d563032015-08-13 07:08:31 -0700163 if (blendedColor.fWillBlendWithDst) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500164 optimizations->fFlags |= GrPipelineOptimizations::kWillColorBlendWithDst_Flag;
bsalomon2d563032015-08-13 07:08:31 -0700165 }
166
bsalomonc6998732015-08-10 12:01:15 -0700167 return pipeline;
egdanielc0648242014-09-22 13:17:02 -0700168}
169
robertphillips498d7ac2015-10-30 10:11:30 -0700170static void add_dependencies_for_processor(const GrFragmentProcessor* proc, GrRenderTarget* rt) {
bsalomonb58a2b42016-09-26 06:55:02 -0700171 GrFragmentProcessor::TextureAccessIter iter(proc);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500172 while (const GrProcessor::TextureSampler* sampler = iter.next()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400173 SkASSERT(rt->getLastOpList());
Brian Salomondb4183d2016-11-17 12:48:40 -0500174 rt->getLastOpList()->addDependency(sampler->texture());
robertphillips498d7ac2015-10-30 10:11:30 -0700175 }
176}
177
178void GrPipeline::addDependenciesTo(GrRenderTarget* rt) const {
179 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
180 add_dependencies_for_processor(fFragmentProcessors[i].get(), rt);
181 }
182
bsalomon2047b782015-12-21 13:12:54 -0800183 const GrXferProcessor& xfer = this->getXferProcessor();
robertphillips498d7ac2015-10-30 10:11:30 -0700184
Brian Salomon0bbecb22016-11-17 11:38:22 -0500185 for (int i = 0; i < xfer.numTextureSamplers(); ++i) {
Brian Salomondb4183d2016-11-17 12:48:40 -0500186 GrTexture* texture = xfer.textureSampler(i).texture();
Robert Phillipsf2361d22016-10-25 14:20:06 -0400187 SkASSERT(rt->getLastOpList());
188 rt->getLastOpList()->addDependency(texture);
robertphillips498d7ac2015-10-30 10:11:30 -0700189 }
190}
191
egdaniel89af44a2014-09-26 06:15:04 -0700192////////////////////////////////////////////////////////////////////////////////
193
bsalomon7312ff82016-09-12 08:55:38 -0700194bool GrPipeline::AreEqual(const GrPipeline& a, const GrPipeline& b) {
bsalomoncb02b382015-08-12 11:14:50 -0700195 SkASSERT(&a != &b);
joshualitt8cab9a72015-07-16 09:13:50 -0700196
bsalomoncb02b382015-08-12 11:14:50 -0700197 if (a.getRenderTarget() != b.getRenderTarget() ||
bsalomonac856c92015-08-27 06:30:17 -0700198 a.fFragmentProcessors.count() != b.fFragmentProcessors.count() ||
199 a.fNumColorProcessors != b.fNumColorProcessors ||
bsalomoncb02b382015-08-12 11:14:50 -0700200 a.fScissorState != b.fScissorState ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700201 !a.fWindowRectsState.cheapEqualTo(b.fWindowRectsState) ||
bsalomoncb02b382015-08-12 11:14:50 -0700202 a.fFlags != b.fFlags ||
csmartdaltonc633abb2016-11-01 08:55:55 -0700203 a.fUserStencilSettings != b.fUserStencilSettings ||
Brian Salomon8c852be2017-01-04 10:44:42 -0500204 a.fDrawFace != b.fDrawFace) {
egdaniel89af44a2014-09-26 06:15:04 -0700205 return false;
206 }
207
bsalomon2047b782015-12-21 13:12:54 -0800208 // Most of the time both are nullptr
209 if (a.fXferProcessor.get() || b.fXferProcessor.get()) {
210 if (!a.getXferProcessor().isEqual(b.getXferProcessor())) {
211 return false;
212 }
egdanielc2304142014-12-11 13:15:13 -0800213 }
214
bsalomonac856c92015-08-27 06:30:17 -0700215 for (int i = 0; i < a.numFragmentProcessors(); i++) {
bsalomon7312ff82016-09-12 08:55:38 -0700216 if (!a.getFragmentProcessor(i).isEqual(b.getFragmentProcessor(i))) {
egdaniel89af44a2014-09-26 06:15:04 -0700217 return false;
218 }
219 }
egdaniel89af44a2014-09-26 06:15:04 -0700220 return true;
221}