blob: 93647d6a5632307901f78f4262ac54ac369e1446 [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"
joshualitt4973d9d2014-11-08 09:24:25 -080011#include "GrGpu.h"
egdaniel8dd688b2015-01-22 10:16:09 -080012#include "GrPipelineBuilder.h"
egdanielb6cbc382014-11-13 11:00:34 -080013#include "GrProcOptInfo.h"
egdaniel95131432014-12-09 11:15:43 -080014#include "GrXferProcessor.h"
egdaniel3658f382014-09-15 07:01:59 -070015
joshualitt74417822015-08-07 11:42:16 -070016#include "batches/GrBatch.h"
17
bsalomona387a112015-08-11 14:47:42 -070018GrPipeline* GrPipeline::CreateAt(void* memory, const CreateArgs& args,
bsalomonc6998732015-08-10 12:01:15 -070019 GrPipelineOptimizations* opts) {
bsalomona387a112015-08-11 14:47:42 -070020 const GrPipelineBuilder& builder = *args.fPipelineBuilder;
bsalomonc6998732015-08-10 12:01:15 -070021
egdaniel95131432014-12-09 11:15:43 -080022 // Create XferProcessor from DS's XPFactory
23 SkAutoTUnref<GrXferProcessor> xferProcessor(
bsalomona387a112015-08-11 14:47:42 -070024 builder.getXPFactory()->createXferProcessor(args.fColorPOI, args.fCoveragePOI,
25 builder.hasMixedSamples(), &args.fDstTexture,
26 *args.fCaps));
bsalomonc8d3f572015-08-13 06:44:04 -070027 if (!xferProcessor) {
28 return nullptr;
29 }
egdaniel95131432014-12-09 11:15:43 -080030
joshualitt9b989322014-12-15 14:16:27 -080031 GrColor overrideColor = GrColor_ILLEGAL;
bsalomonac856c92015-08-27 06:30:17 -070032 if (args.fColorPOI.firstEffectiveProcessorIndex() != 0) {
33 overrideColor = args.fColorPOI.inputColorToFirstEffectiveProccesor();
joshualitt9b989322014-12-15 14:16:27 -080034 }
35
bsalomon7765a472015-07-08 11:26:37 -070036 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
egdaniel95131432014-12-09 11:15:43 -080037
bsalomonc8d3f572015-08-13 06:44:04 -070038 optFlags = xferProcessor->getOptimizations(args.fColorPOI,
39 args.fCoveragePOI,
40 builder.getStencil().doesWrite(),
41 &overrideColor,
42 *args.fCaps);
43
44 // When path rendering the stencil settings are not always set on the GrPipelineBuilder
45 // so we must check the draw type. In cases where we will skip drawing we simply return a
46 // null GrPipeline.
47 if (GrXferProcessor::kSkipDraw_OptFlag & optFlags) {
48 return nullptr;
egdaniel95131432014-12-09 11:15:43 -080049 }
bsalomonb03c4a32014-11-20 09:56:11 -080050
bsalomon7765a472015-07-08 11:26:37 -070051 // No need to have an override color if it isn't even going to be used.
52 if (SkToBool(GrXferProcessor::kIgnoreColor_OptFlag & optFlags)) {
53 overrideColor = GrColor_ILLEGAL;
54 }
55
halcanary385fe4d2015-08-26 13:07:48 -070056 GrPipeline* pipeline = new (memory) GrPipeline;
bsalomonc8d3f572015-08-13 06:44:04 -070057 pipeline->fXferProcessor.reset(xferProcessor.get());
bsalomonb03c4a32014-11-20 09:56:11 -080058
bsalomonc6998732015-08-10 12:01:15 -070059 pipeline->fRenderTarget.reset(builder.fRenderTarget.get());
60 SkASSERT(pipeline->fRenderTarget);
bsalomona387a112015-08-11 14:47:42 -070061 pipeline->fScissorState = *args.fScissor;
bsalomonc6998732015-08-10 12:01:15 -070062 pipeline->fStencilSettings = builder.getStencil();
63 pipeline->fDrawFace = builder.getDrawFace();
joshualitt9176e2c2014-11-20 07:28:52 -080064
bsalomonc6998732015-08-10 12:01:15 -070065 pipeline->fFlags = 0;
66 if (builder.isHWAntialias()) {
67 pipeline->fFlags |= kHWAA_Flag;
bsalomon04ddf892014-11-19 12:36:22 -080068 }
bsalomonc6998732015-08-10 12:01:15 -070069 if (builder.isDither()) {
70 pipeline->fFlags |= kDither_Flag;
bsalomon04ddf892014-11-19 12:36:22 -080071 }
bsalomonc6998732015-08-10 12:01:15 -070072 if (builder.snapVerticesToPixelCenters()) {
73 pipeline->fFlags |= kSnapVertices_Flag;
bsalomond79c5492015-04-27 10:07:04 -070074 }
bsalomon04ddf892014-11-19 12:36:22 -080075
bsalomonac856c92015-08-27 06:30:17 -070076 int firstColorProcessorIdx = args.fColorPOI.firstEffectiveProcessorIndex();
egdaniel912b3d22014-11-17 07:45:53 -080077
bsalomonac856c92015-08-27 06:30:17 -070078 // TODO: Once we can handle single or four channel input into coverage GrFragmentProcessors
79 // then we can use GrPipelineBuilder's coverageProcInfo (like color above) to set this initial
80 // information.
81 int firstCoverageProcessorIdx = 0;
egdaniel912b3d22014-11-17 07:45:53 -080082
bsalomona387a112015-08-11 14:47:42 -070083 pipeline->adjustProgramFromOptimizations(builder, optFlags, args.fColorPOI, args.fCoveragePOI,
bsalomonac856c92015-08-27 06:30:17 -070084 &firstColorProcessorIdx, &firstCoverageProcessorIdx);
egdaniel95131432014-12-09 11:15:43 -080085
joshualitt290c09b2014-12-19 13:45:20 -080086 bool usesLocalCoords = false;
87
bsalomonac856c92015-08-27 06:30:17 -070088 // Copy GrFragmentProcessors from GrPipelineBuilder to Pipeline
89 pipeline->fNumColorProcessors = builder.numColorFragmentProcessors() - firstColorProcessorIdx;
90 int numTotalProcessors = pipeline->fNumColorProcessors +
91 builder.numCoverageFragmentProcessors() - firstCoverageProcessorIdx;
92 pipeline->fFragmentProcessors.reset(numTotalProcessors);
93 int currFPIdx = 0;
94 for (int i = firstColorProcessorIdx; i < builder.numColorFragmentProcessors();
95 ++i, ++currFPIdx) {
96 const GrFragmentProcessor* fp = builder.getColorFragmentProcessor(i);
97 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -070098 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
bsalomonc6998732015-08-10 12:01:15 -070099 fp->gatherCoordTransforms(&pipeline->fCoordTransforms);
bsalomonae59b772014-11-19 08:23:49 -0800100 }
egdaniel95131432014-12-09 11:15:43 -0800101
bsalomonac856c92015-08-27 06:30:17 -0700102 for (int i = firstCoverageProcessorIdx; i < builder.numCoverageFragmentProcessors();
103 ++i, ++currFPIdx) {
104 const GrFragmentProcessor* fp = builder.getCoverageFragmentProcessor(i);
105 pipeline->fFragmentProcessors[currFPIdx].reset(fp);
joshualitt2fe79232015-08-05 12:02:27 -0700106 usesLocalCoords = usesLocalCoords || fp->usesLocalCoords();
bsalomonc6998732015-08-10 12:01:15 -0700107 fp->gatherCoordTransforms(&pipeline->fCoordTransforms);
egdaniel9cf45bf2014-10-08 06:49:10 -0700108 }
joshualitt79f8fae2014-10-28 17:59:26 -0700109
bsalomon7765a472015-07-08 11:26:37 -0700110 // Setup info we need to pass to GrPrimitiveProcessors that are used with this GrPipeline.
bsalomonc6998732015-08-10 12:01:15 -0700111 opts->fFlags = 0;
bsalomon7765a472015-07-08 11:26:37 -0700112 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag)) {
bsalomonc6998732015-08-10 12:01:15 -0700113 opts->fFlags |= GrPipelineOptimizations::kReadsColor_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700114 }
115 if (GrColor_ILLEGAL != overrideColor) {
bsalomonc6998732015-08-10 12:01:15 -0700116 opts->fFlags |= GrPipelineOptimizations::kUseOverrideColor_Flag;
117 opts->fOverrideColor = overrideColor;
bsalomon7765a472015-07-08 11:26:37 -0700118 }
119 if (!SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag)) {
bsalomonc6998732015-08-10 12:01:15 -0700120 opts->fFlags |= GrPipelineOptimizations::kReadsCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700121 }
122 if (usesLocalCoords) {
bsalomonc6998732015-08-10 12:01:15 -0700123 opts->fFlags |= GrPipelineOptimizations::kReadsLocalCoords_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700124 }
125 if (SkToBool(optFlags & GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag)) {
bsalomonc6998732015-08-10 12:01:15 -0700126 opts->fFlags |= GrPipelineOptimizations::kCanTweakAlphaForCoverage_Flag;
bsalomon7765a472015-07-08 11:26:37 -0700127 }
bsalomon2d563032015-08-13 07:08:31 -0700128
129 GrXPFactory::InvariantBlendedColor blendedColor;
130 builder.fXPFactory->getInvariantBlendedColor(args.fColorPOI, &blendedColor);
131 if (blendedColor.fWillBlendWithDst) {
132 opts->fFlags |= GrPipelineOptimizations::kWillColorBlendWithDst_Flag;
133 }
134
bsalomonc6998732015-08-10 12:01:15 -0700135 return pipeline;
egdanielc0648242014-09-22 13:17:02 -0700136}
137
egdaniel8dd688b2015-01-22 10:16:09 -0800138void GrPipeline::adjustProgramFromOptimizations(const GrPipelineBuilder& pipelineBuilder,
139 GrXferProcessor::OptFlags flags,
140 const GrProcOptInfo& colorPOI,
141 const GrProcOptInfo& coveragePOI,
bsalomonac856c92015-08-27 06:30:17 -0700142 int* firstColorProcessorIdx,
143 int* firstCoverageProcessorIdx) {
egdaniel060a52c2015-04-07 07:31:11 -0700144 fReadsFragPosition = fXferProcessor->willReadFragmentPosition();
egdaniela7dc0a82014-09-17 08:25:05 -0700145
joshualitt9b989322014-12-15 14:16:27 -0800146 if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) ||
147 (flags & GrXferProcessor::kOverrideColor_OptFlag)) {
bsalomonac856c92015-08-27 06:30:17 -0700148 *firstColorProcessorIdx = pipelineBuilder.numColorFragmentProcessors();
egdaniel95131432014-12-09 11:15:43 -0800149 } else {
egdaniel060a52c2015-04-07 07:31:11 -0700150 if (coveragePOI.readsFragPosition()) {
151 fReadsFragPosition = true;
152 }
egdaniela7dc0a82014-09-17 08:25:05 -0700153 }
egdaniel95131432014-12-09 11:15:43 -0800154
joshualitt9b989322014-12-15 14:16:27 -0800155 if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) {
bsalomonac856c92015-08-27 06:30:17 -0700156 *firstCoverageProcessorIdx = pipelineBuilder.numCoverageFragmentProcessors();
egdaniel95131432014-12-09 11:15:43 -0800157 } else {
egdaniel95131432014-12-09 11:15:43 -0800158 if (coveragePOI.readsFragPosition()) {
bsalomon50785a32015-02-06 07:02:37 -0800159 fReadsFragPosition = true;
egdaniel95131432014-12-09 11:15:43 -0800160 }
egdaniela7dc0a82014-09-17 08:25:05 -0700161 }
162}
163
egdaniel89af44a2014-09-26 06:15:04 -0700164////////////////////////////////////////////////////////////////////////////////
165
bsalomoncb02b382015-08-12 11:14:50 -0700166bool GrPipeline::AreEqual(const GrPipeline& a, const GrPipeline& b,
167 bool ignoreCoordTransforms) {
168 SkASSERT(&a != &b);
joshualitt8cab9a72015-07-16 09:13:50 -0700169
bsalomoncb02b382015-08-12 11:14:50 -0700170 if (a.getRenderTarget() != b.getRenderTarget() ||
bsalomonac856c92015-08-27 06:30:17 -0700171 a.fFragmentProcessors.count() != b.fFragmentProcessors.count() ||
172 a.fNumColorProcessors != b.fNumColorProcessors ||
bsalomoncb02b382015-08-12 11:14:50 -0700173 a.fScissorState != b.fScissorState ||
174 a.fFlags != b.fFlags ||
175 a.fStencilSettings != b.fStencilSettings ||
176 a.fDrawFace != b.fDrawFace) {
egdaniel89af44a2014-09-26 06:15:04 -0700177 return false;
178 }
179
bsalomoncb02b382015-08-12 11:14:50 -0700180 if (!a.getXferProcessor()->isEqual(*b.getXferProcessor())) {
egdanielc2304142014-12-11 13:15:13 -0800181 return false;
182 }
183
bsalomonac856c92015-08-27 06:30:17 -0700184 for (int i = 0; i < a.numFragmentProcessors(); i++) {
185 if (!a.getFragmentProcessor(i).isEqual(b.getFragmentProcessor(i), ignoreCoordTransforms)) {
egdaniel89af44a2014-09-26 06:15:04 -0700186 return false;
187 }
188 }
egdaniel89af44a2014-09-26 06:15:04 -0700189 return true;
190}
191