blob: f978a107b695e125cad7be7077159151fff97495 [file] [log] [blame]
Brian Salomonb4d61062017-07-12 11:24:41 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrAppliedClip.h"
9#include "src/gpu/GrProcessorSet.h"
Robert Phillipsce978572020-02-28 11:56:44 -050010#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrUserStencilSettings.h"
12#include "src/gpu/SkGr.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040013#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Brian Salomonb4d61062017-07-12 11:24:41 -040015
16GrSimpleMeshDrawOpHelper::GrSimpleMeshDrawOpHelper(const MakeArgs& args, GrAAType aaType,
Chris Daltonbaa1b352019-04-03 12:03:00 -060017 InputFlags inputFlags)
Brian Salomonb4d61062017-07-12 11:24:41 -040018 : fProcessors(args.fProcessorSet)
Chris Daltonbaa1b352019-04-03 12:03:00 -060019 , fPipelineFlags((GrPipeline::InputFlags)inputFlags)
Brian Salomonb4d61062017-07-12 11:24:41 -040020 , fAAType((int)aaType)
Brian Salomonb4d61062017-07-12 11:24:41 -040021 , fUsesLocalCoords(false)
Brian Osman605c6d52019-03-15 12:10:35 -040022 , fCompatibleWithCoverageAsAlpha(false) {
Brian Salomonb4d61062017-07-12 11:24:41 -040023 SkDEBUGCODE(fDidAnalysis = false);
Brian Salomonbfd18cd2017-08-09 16:27:09 -040024 SkDEBUGCODE(fMadePipeline = false);
Brian Salomonb4d61062017-07-12 11:24:41 -040025 if (GrAATypeIsHW(aaType)) {
Chris Daltonbaa1b352019-04-03 12:03:00 -060026 fPipelineFlags |= GrPipeline::InputFlags::kHWAntialias;
Brian Salomonb4d61062017-07-12 11:24:41 -040027 }
28}
29
30GrSimpleMeshDrawOpHelper::~GrSimpleMeshDrawOpHelper() {
31 if (fProcessors) {
32 fProcessors->~GrProcessorSet();
33 }
34}
35
36GrDrawOp::FixedFunctionFlags GrSimpleMeshDrawOpHelper::fixedFunctionFlags() const {
37 return GrAATypeIsHW((this->aaType())) ? GrDrawOp::FixedFunctionFlags::kUsesHWAA
38 : GrDrawOp::FixedFunctionFlags::kNone;
39}
40
41bool GrSimpleMeshDrawOpHelper::isCompatible(const GrSimpleMeshDrawOpHelper& that,
42 const GrCaps& caps, const SkRect& thisBounds,
Robert Phillipsb69001f2019-10-29 12:16:35 -040043 const SkRect& thatBounds, bool ignoreAAType) const {
Brian Salomonb4d61062017-07-12 11:24:41 -040044 if (SkToBool(fProcessors) != SkToBool(that.fProcessors)) {
45 return false;
46 }
47 if (fProcessors) {
48 if (*fProcessors != *that.fProcessors) {
49 return false;
50 }
Brian Salomonb4d61062017-07-12 11:24:41 -040051 }
Robert Phillipsb69001f2019-10-29 12:16:35 -040052
53#ifdef SK_DEBUG
54 if (ignoreAAType) {
55 // If we're ignoring AA it should be bc we already know they are the same or that
56 // the are different but are compatible (i.e., one is AA and the other is None)
Robert Phillipsbbd459d2019-10-29 14:40:03 -040057 SkASSERT(fAAType == that.fAAType ||
58 GrMeshDrawOp::CanUpgradeAAOnMerge(this->aaType(), that.aaType()));
Robert Phillipsb69001f2019-10-29 12:16:35 -040059 }
60#endif
61
62 bool result = fPipelineFlags == that.fPipelineFlags &&
63 (ignoreAAType || fAAType == that.fAAType);
Brian Osman605c6d52019-03-15 12:10:35 -040064 SkASSERT(!result || fCompatibleWithCoverageAsAlpha == that.fCompatibleWithCoverageAsAlpha);
Brian Salomonb4d61062017-07-12 11:24:41 -040065 SkASSERT(!result || fUsesLocalCoords == that.fUsesLocalCoords);
66 return result;
67}
68
Chris Dalton4b62aed2019-01-15 11:53:00 -070069GrProcessorSet::Analysis GrSimpleMeshDrawOpHelper::finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -060070 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
71 GrClampType clampType, GrProcessorAnalysisCoverage geometryCoverage,
72 SkPMColor4f* geometryColor, bool* wideColor) {
Chris Daltonb8fff0d2019-03-05 10:11:58 -070073 GrProcessorAnalysisColor color = *geometryColor;
Brian Osman5ced0bf2019-03-15 10:15:29 -040074 auto result = this->finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -060075 caps, clip, hasMixedSampledCoverage, clampType, geometryCoverage, &color);
Chris Daltonb8fff0d2019-03-05 10:11:58 -070076 color.isConstant(geometryColor);
Brian Osman8fa7ab42019-03-18 10:22:42 -040077 if (wideColor) {
Brian Osman2715bf52019-12-06 14:38:47 -050078 *wideColor = !geometryColor->fitsInBytes();
Brian Osman8fa7ab42019-03-18 10:22:42 -040079 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -070080 return result;
81}
82
83GrProcessorSet::Analysis GrSimpleMeshDrawOpHelper::finalizeProcessors(
84 const GrCaps& caps, const GrAppliedClip* clip, const GrUserStencilSettings* userStencil,
Chris Dalton6ce447a2019-06-23 18:07:38 -060085 bool hasMixedSampledCoverage, GrClampType clampType,
86 GrProcessorAnalysisCoverage geometryCoverage, GrProcessorAnalysisColor* geometryColor) {
Brian Salomonb4d61062017-07-12 11:24:41 -040087 SkDEBUGCODE(fDidAnalysis = true);
88 GrProcessorSet::Analysis analysis;
89 if (fProcessors) {
90 GrProcessorAnalysisCoverage coverage = geometryCoverage;
91 if (GrProcessorAnalysisCoverage::kNone == coverage) {
Chris Dalton69824002017-10-31 00:37:52 -060092 coverage = clip->numClipCoverageFragmentProcessors()
Brian Salomonb4d61062017-07-12 11:24:41 -040093 ? GrProcessorAnalysisCoverage::kSingleChannel
94 : GrProcessorAnalysisCoverage::kNone;
95 }
Brian Osmancf860852018-10-31 14:04:39 -040096 SkPMColor4f overrideColor;
Chris Dalton6ce447a2019-06-23 18:07:38 -060097 analysis = fProcessors->finalize(*geometryColor, coverage, clip, userStencil,
98 hasMixedSampledCoverage, caps, clampType, &overrideColor);
Brian Salomon0088f942017-07-12 11:51:27 -040099 if (analysis.inputColorIsOverridden()) {
100 *geometryColor = overrideColor;
101 }
Brian Salomonb4d61062017-07-12 11:24:41 -0400102 } else {
103 analysis = GrProcessorSet::EmptySetAnalysis();
104 }
Brian Salomonb4d61062017-07-12 11:24:41 -0400105 fUsesLocalCoords = analysis.usesLocalCoords();
Brian Osman605c6d52019-03-15 12:10:35 -0400106 fCompatibleWithCoverageAsAlpha = analysis.isCompatibleWithCoverageAsAlpha();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700107 return analysis;
Brian Salomonb4d61062017-07-12 11:24:41 -0400108}
109
Robert Phillips3968fcb2019-12-05 16:40:31 -0500110const GrPipeline* GrSimpleMeshDrawOpHelper::CreatePipeline(
Robert Phillips6c59fe42020-02-27 09:30:37 -0500111 const GrCaps* caps,
112 SkArenaAlloc* arena,
Robert Phillipsfad1e0f2020-02-28 09:11:02 -0500113 const GrSurfaceProxyView* outputView,
Robert Phillips6c59fe42020-02-27 09:30:37 -0500114 GrAppliedClip&& appliedClip,
115 const GrXferProcessor::DstProxyView& dstProxyView,
116 GrProcessorSet&& processorSet,
117 GrPipeline::InputFlags pipelineFlags,
118 const GrUserStencilSettings* stencilSettings) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500119 GrPipeline::InitArgs pipelineArgs;
120
121 pipelineArgs.fInputFlags = pipelineFlags;
Robert Phillips3968fcb2019-12-05 16:40:31 -0500122 pipelineArgs.fUserStencil = stencilSettings;
Robert Phillipsce978572020-02-28 11:56:44 -0500123 pipelineArgs.fCaps = caps;
124 pipelineArgs.fDstProxyView = dstProxyView;
Robert Phillipsfad1e0f2020-02-28 09:11:02 -0500125 pipelineArgs.fOutputSwizzle = outputView->swizzle();
Robert Phillips3968fcb2019-12-05 16:40:31 -0500126
Robert Phillips6c59fe42020-02-27 09:30:37 -0500127 return arena->make<GrPipeline>(pipelineArgs,
128 std::move(processorSet),
129 std::move(appliedClip));
130}
131
132const GrPipeline* GrSimpleMeshDrawOpHelper::CreatePipeline(
133 GrOpFlushState* flushState,
134 GrProcessorSet&& processorSet,
135 GrPipeline::InputFlags pipelineFlags,
136 const GrUserStencilSettings* stencilSettings) {
137 return CreatePipeline(&flushState->caps(),
138 flushState->allocator(),
139 flushState->view(),
140 flushState->detachAppliedClip(),
141 flushState->dstProxyView(),
142 std::move(processorSet),
143 pipelineFlags,
144 stencilSettings);
145}
146
147const GrPipeline* GrSimpleMeshDrawOpHelper::createPipeline(GrOpFlushState* flushState) {
148 return CreatePipeline(&flushState->caps(),
149 flushState->allocator(),
150 flushState->view(),
151 flushState->detachAppliedClip(),
152 flushState->dstProxyView(),
153 this->detachProcessorSet(),
154 this->pipelineFlags());
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700155}
156
Robert Phillipsce978572020-02-28 11:56:44 -0500157GrProgramInfo* GrSimpleMeshDrawOpHelper::CreateProgramInfo(
158 const GrCaps* caps,
159 SkArenaAlloc* arena,
160 const GrSurfaceProxyView* outputView,
161 GrAppliedClip&& appliedClip,
162 const GrXferProcessor::DstProxyView& dstProxyView,
163 GrGeometryProcessor* geometryProcessor,
164 GrProcessorSet&& processorSet,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500165 GrPrimitiveType primitiveType,
166 GrPipeline::InputFlags pipelineFlags,
167 const GrUserStencilSettings* stencilSettings) {
Robert Phillipsce978572020-02-28 11:56:44 -0500168 static constexpr int kZeroPrimProcTextures = 0;
169 auto fixedDynamicState = GrMeshDrawOp::Target::MakeFixedDynamicState(arena,
170 &appliedClip,
171 kZeroPrimProcTextures);
172
173 auto pipeline = CreatePipeline(caps,
174 arena,
175 outputView,
176 std::move(appliedClip),
177 dstProxyView,
178 std::move(processorSet),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500179 pipelineFlags,
180 stencilSettings);
Robert Phillipsce978572020-02-28 11:56:44 -0500181
182 GrRenderTargetProxy* outputProxy = outputView->asRenderTargetProxy();
183
184 static constexpr int kOneMesh = 1;
185 auto tmp = arena->make<GrProgramInfo>(outputProxy->numSamples(),
186 outputProxy->numStencilSamples(),
187 outputProxy->backendFormat(),
188 outputView->origin(),
189 pipeline,
190 geometryProcessor,
191 fixedDynamicState,
192 nullptr,
193 kOneMesh,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500194 primitiveType);
Robert Phillipsce978572020-02-28 11:56:44 -0500195 SkASSERT(tmp->primProc().numTextureSamplers() <= 0);
196 return tmp;
197}
198
Brian Osman9a390ac2018-11-12 09:47:48 -0500199#ifdef SK_DEBUG
Chris Daltonbaa1b352019-04-03 12:03:00 -0600200static void dump_pipeline_flags(GrPipeline::InputFlags flags, SkString* result) {
201 if (GrPipeline::InputFlags::kNone != flags) {
202 if (flags & GrPipeline::InputFlags::kSnapVerticesToPixelCenters) {
203 result->append("Snap vertices to pixel center.\n");
204 }
205 if (flags & GrPipeline::InputFlags::kHWAntialias) {
206 result->append("HW Antialiasing enabled.\n");
207 }
208 return;
209 }
210 result->append("No pipeline flags\n");
211}
212
Brian Salomonb4d61062017-07-12 11:24:41 -0400213SkString GrSimpleMeshDrawOpHelper::dumpInfo() const {
Brian Salomon91326c32017-08-09 16:02:19 -0400214 const GrProcessorSet& processors = fProcessors ? *fProcessors : GrProcessorSet::EmptySet();
215 SkString result = processors.dumpProcessors();
Brian Salomonb4d61062017-07-12 11:24:41 -0400216 result.append("AA Type: ");
217 switch (this->aaType()) {
218 case GrAAType::kNone:
219 result.append(" none\n");
220 break;
221 case GrAAType::kCoverage:
222 result.append(" coverage\n");
223 break;
224 case GrAAType::kMSAA:
225 result.append(" msaa\n");
226 break;
Brian Salomonb4d61062017-07-12 11:24:41 -0400227 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600228 dump_pipeline_flags(fPipelineFlags, &result);
Brian Salomonb4d61062017-07-12 11:24:41 -0400229 return result;
230}
Brian Osman9a390ac2018-11-12 09:47:48 -0500231#endif