blob: f8a776397167e30c27e1fd7c0a83d20a1a85e21e [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
8#include "GrSimpleMeshDrawOpHelper.h"
9#include "GrAppliedClip.h"
10#include "GrProcessorSet.h"
11#include "GrRect.h"
12#include "GrUserStencilSettings.h"
13
14GrSimpleMeshDrawOpHelper::GrSimpleMeshDrawOpHelper(const MakeArgs& args, GrAAType aaType,
15 Flags flags)
16 : fProcessors(args.fProcessorSet)
Brian Osman9aa30c62018-07-02 15:21:46 -040017 , fPipelineFlags(0)
Brian Salomonb4d61062017-07-12 11:24:41 -040018 , fAAType((int)aaType)
19 , fRequiresDstTexture(false)
20 , fUsesLocalCoords(false)
21 , fCompatibleWithAlphaAsCoveage(false) {
22 SkDEBUGCODE(fDidAnalysis = false);
Brian Salomonbfd18cd2017-08-09 16:27:09 -040023 SkDEBUGCODE(fMadePipeline = false);
Brian Salomonb4d61062017-07-12 11:24:41 -040024 if (GrAATypeIsHW(aaType)) {
25 fPipelineFlags |= GrPipeline::kHWAntialias_Flag;
26 }
27 if (flags & Flags::kSnapVerticesToPixelCenters) {
28 fPipelineFlags |= GrPipeline::kSnapVerticesToPixelCenters_Flag;
29 }
30}
31
32GrSimpleMeshDrawOpHelper::~GrSimpleMeshDrawOpHelper() {
33 if (fProcessors) {
34 fProcessors->~GrProcessorSet();
35 }
36}
37
38GrDrawOp::FixedFunctionFlags GrSimpleMeshDrawOpHelper::fixedFunctionFlags() const {
39 return GrAATypeIsHW((this->aaType())) ? GrDrawOp::FixedFunctionFlags::kUsesHWAA
40 : GrDrawOp::FixedFunctionFlags::kNone;
41}
42
Michael Ludwig69858532018-11-28 15:34:34 -050043static bool none_as_coverage_aa_compatible(GrAAType aa1, GrAAType aa2) {
44 return (aa1 == GrAAType::kNone && aa2 == GrAAType::kCoverage) ||
45 (aa1 == GrAAType::kCoverage && aa2 == GrAAType::kNone);
46}
47
Brian Salomonb4d61062017-07-12 11:24:41 -040048bool GrSimpleMeshDrawOpHelper::isCompatible(const GrSimpleMeshDrawOpHelper& that,
49 const GrCaps& caps, const SkRect& thisBounds,
Michael Ludwig69858532018-11-28 15:34:34 -050050 const SkRect& thatBounds, bool noneAsCoverageAA) const {
Brian Salomonb4d61062017-07-12 11:24:41 -040051 if (SkToBool(fProcessors) != SkToBool(that.fProcessors)) {
52 return false;
53 }
54 if (fProcessors) {
55 if (*fProcessors != *that.fProcessors) {
56 return false;
57 }
58 if (fRequiresDstTexture ||
59 (fProcessors->xferProcessor() && fProcessors->xferProcessor()->xferBarrierType(caps))) {
60 if (GrRectsTouchOrOverlap(thisBounds, thatBounds)) {
61 return false;
62 }
63 }
64 }
Michael Ludwig69858532018-11-28 15:34:34 -050065 bool result = fPipelineFlags == that.fPipelineFlags && (fAAType == that.fAAType ||
66 (noneAsCoverageAA && none_as_coverage_aa_compatible(this->aaType(), that.aaType())));
Brian Salomonb4d61062017-07-12 11:24:41 -040067 SkASSERT(!result || fCompatibleWithAlphaAsCoveage == that.fCompatibleWithAlphaAsCoveage);
68 SkASSERT(!result || fUsesLocalCoords == that.fUsesLocalCoords);
69 return result;
70}
71
72GrDrawOp::RequiresDstTexture GrSimpleMeshDrawOpHelper::xpRequiresDstTexture(
Brian Osman532b3f92018-07-11 10:02:07 -040073 const GrCaps& caps, const GrAppliedClip* clip, GrProcessorAnalysisCoverage geometryCoverage,
74 GrProcessorAnalysisColor* geometryColor) {
Brian Salomonb4d61062017-07-12 11:24:41 -040075 SkDEBUGCODE(fDidAnalysis = true);
76 GrProcessorSet::Analysis analysis;
77 if (fProcessors) {
78 GrProcessorAnalysisCoverage coverage = geometryCoverage;
79 if (GrProcessorAnalysisCoverage::kNone == coverage) {
Chris Dalton69824002017-10-31 00:37:52 -060080 coverage = clip->numClipCoverageFragmentProcessors()
Brian Salomonb4d61062017-07-12 11:24:41 -040081 ? GrProcessorAnalysisCoverage::kSingleChannel
82 : GrProcessorAnalysisCoverage::kNone;
83 }
84 bool isMixedSamples = this->aaType() == GrAAType::kMixedSamples;
Brian Osmancf860852018-10-31 14:04:39 -040085 SkPMColor4f overrideColor;
Brian Salomon0088f942017-07-12 11:51:27 -040086 analysis = fProcessors->finalize(*geometryColor, coverage, clip, isMixedSamples, caps,
Brian Osman532b3f92018-07-11 10:02:07 -040087 &overrideColor);
Brian Salomon0088f942017-07-12 11:51:27 -040088 if (analysis.inputColorIsOverridden()) {
89 *geometryColor = overrideColor;
90 }
Brian Salomonb4d61062017-07-12 11:24:41 -040091 } else {
92 analysis = GrProcessorSet::EmptySetAnalysis();
93 }
94 fRequiresDstTexture = analysis.requiresDstTexture();
95 fUsesLocalCoords = analysis.usesLocalCoords();
96 fCompatibleWithAlphaAsCoveage = analysis.isCompatibleWithCoverageAsAlpha();
97 return analysis.requiresDstTexture() ? GrDrawOp::RequiresDstTexture::kYes
98 : GrDrawOp::RequiresDstTexture::kNo;
99}
100
Brian Salomon0088f942017-07-12 11:51:27 -0400101GrDrawOp::RequiresDstTexture GrSimpleMeshDrawOpHelper::xpRequiresDstTexture(
Brian Osman532b3f92018-07-11 10:02:07 -0400102 const GrCaps& caps, const GrAppliedClip* clip, GrProcessorAnalysisCoverage geometryCoverage,
Brian Osmancf860852018-10-31 14:04:39 -0400103 SkPMColor4f* geometryColor) {
Brian Salomon0088f942017-07-12 11:51:27 -0400104 GrProcessorAnalysisColor color = *geometryColor;
Brian Osman532b3f92018-07-11 10:02:07 -0400105 auto result = this->xpRequiresDstTexture(caps, clip, geometryCoverage, &color);
Brian Salomon0088f942017-07-12 11:51:27 -0400106 color.isConstant(geometryColor);
107 return result;
108}
109
Brian Osman9a390ac2018-11-12 09:47:48 -0500110#ifdef SK_DEBUG
Brian Salomonb4d61062017-07-12 11:24:41 -0400111SkString GrSimpleMeshDrawOpHelper::dumpInfo() const {
Brian Salomon91326c32017-08-09 16:02:19 -0400112 const GrProcessorSet& processors = fProcessors ? *fProcessors : GrProcessorSet::EmptySet();
113 SkString result = processors.dumpProcessors();
Brian Salomonb4d61062017-07-12 11:24:41 -0400114 result.append("AA Type: ");
115 switch (this->aaType()) {
116 case GrAAType::kNone:
117 result.append(" none\n");
118 break;
119 case GrAAType::kCoverage:
120 result.append(" coverage\n");
121 break;
122 case GrAAType::kMSAA:
123 result.append(" msaa\n");
124 break;
125 case GrAAType::kMixedSamples:
126 result.append(" mixed samples\n");
127 break;
128 }
129 result.append(GrPipeline::DumpFlags(fPipelineFlags));
130 return result;
131}
Brian Osman9a390ac2018-11-12 09:47:48 -0500132#endif
Brian Salomonb4d61062017-07-12 11:24:41 -0400133
134GrPipeline::InitArgs GrSimpleMeshDrawOpHelper::pipelineInitArgs(
135 GrMeshDrawOp::Target* target) const {
136 GrPipeline::InitArgs args;
137 args.fFlags = this->pipelineFlags();
Robert Phillips2890fbf2017-07-26 15:48:41 -0400138 args.fProxy = target->proxy();
Brian Salomonb4d61062017-07-12 11:24:41 -0400139 args.fDstProxy = target->dstProxy();
140 args.fCaps = &target->caps();
141 args.fResourceProvider = target->resourceProvider();
142 return args;
143}
144
Brian Salomon49348902018-06-26 09:12:38 -0400145auto GrSimpleMeshDrawOpHelper::internalMakePipeline(GrMeshDrawOp::Target* target,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000146 const GrPipeline::InitArgs& args,
147 int numPrimitiveProcessorProxies)
Brian Salomon49348902018-06-26 09:12:38 -0400148 -> PipelineAndFixedDynamicState {
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400149 // A caller really should only call this once as the processor set and applied clip get
150 // moved into the GrPipeline.
151 SkASSERT(!fMadePipeline);
152 SkDEBUGCODE(fMadePipeline = true);
Brian Salomon49348902018-06-26 09:12:38 -0400153 auto clip = target->detachAppliedClip();
Brian Salomonf5136822018-08-03 09:09:36 -0400154 GrPipeline::FixedDynamicState* fixedDynamicState = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000155 if (clip.scissorState().enabled() || numPrimitiveProcessorProxies) {
Brian Salomonf5136822018-08-03 09:09:36 -0400156 fixedDynamicState = target->allocFixedDynamicState(clip.scissorState().rect());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000157 if (numPrimitiveProcessorProxies) {
158 fixedDynamicState->fPrimitiveProcessorTextures =
159 target->allocPrimitiveProcessorTextureArray(numPrimitiveProcessorProxies);
160 }
Brian Salomonf5136822018-08-03 09:09:36 -0400161 }
Brian Salomon91326c32017-08-09 16:02:19 -0400162 if (fProcessors) {
Brian Salomon49348902018-06-26 09:12:38 -0400163 return {target->allocPipeline(args, std::move(*fProcessors), std::move(clip)),
Brian Salomonf5136822018-08-03 09:09:36 -0400164 fixedDynamicState};
Brian Salomon91326c32017-08-09 16:02:19 -0400165 } else {
Brian Salomon49348902018-06-26 09:12:38 -0400166 return {target->allocPipeline(args, GrProcessorSet::MakeEmptySet(), std::move(clip)),
Brian Salomonf5136822018-08-03 09:09:36 -0400167 fixedDynamicState};
Brian Salomon91326c32017-08-09 16:02:19 -0400168 }
169}
170
Brian Salomonb4d61062017-07-12 11:24:41 -0400171GrSimpleMeshDrawOpHelperWithStencil::GrSimpleMeshDrawOpHelperWithStencil(
172 const MakeArgs& args, GrAAType aaType, const GrUserStencilSettings* stencilSettings,
173 Flags flags)
174 : INHERITED(args, aaType, flags)
175 , fStencilSettings(stencilSettings ? stencilSettings : &GrUserStencilSettings::kUnused) {}
176
177GrDrawOp::FixedFunctionFlags GrSimpleMeshDrawOpHelperWithStencil::fixedFunctionFlags() const {
178 GrDrawOp::FixedFunctionFlags flags = INHERITED::fixedFunctionFlags();
179 if (fStencilSettings != &GrUserStencilSettings::kUnused) {
180 flags |= GrDrawOp::FixedFunctionFlags::kUsesStencil;
181 }
182 return flags;
183}
184
185bool GrSimpleMeshDrawOpHelperWithStencil::isCompatible(
186 const GrSimpleMeshDrawOpHelperWithStencil& that, const GrCaps& caps,
Michael Ludwig69858532018-11-28 15:34:34 -0500187 const SkRect& thisBounds, const SkRect& thatBounds, bool noneAsCoverageAA) const {
188 return INHERITED::isCompatible(that, caps, thisBounds, thatBounds, noneAsCoverageAA) &&
Brian Salomonb4d61062017-07-12 11:24:41 -0400189 fStencilSettings == that.fStencilSettings;
190}
191
Brian Salomon7eae3e02018-08-07 14:02:38 +0000192auto GrSimpleMeshDrawOpHelperWithStencil::makePipeline(GrMeshDrawOp::Target* target,
193 int numPrimitiveProcessorTextures)
Brian Salomon49348902018-06-26 09:12:38 -0400194 -> PipelineAndFixedDynamicState {
Brian Salomonb4d61062017-07-12 11:24:41 -0400195 auto args = INHERITED::pipelineInitArgs(target);
196 args.fUserStencil = fStencilSettings;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000197 return this->internalMakePipeline(target, args, numPrimitiveProcessorTextures);
Brian Salomonb4d61062017-07-12 11:24:41 -0400198}
199
Brian Osman9a390ac2018-11-12 09:47:48 -0500200#ifdef SK_DEBUG
Brian Salomonb4d61062017-07-12 11:24:41 -0400201SkString GrSimpleMeshDrawOpHelperWithStencil::dumpInfo() const {
202 SkString result = INHERITED::dumpInfo();
203 result.appendf("Stencil settings: %s\n", (fStencilSettings ? "yes" : "no"));
204 return result;
205}
Brian Osman9a390ac2018-11-12 09:47:48 -0500206#endif