blob: c5545dad50ca394c0a7cc92a7ef912f89d1528db [file] [log] [blame]
Brian Salomon6d4b65e2017-05-03 17:06:09 -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#ifndef GrSimpleMeshDrawOpHelper_DEFINED
9#define GrSimpleMeshDrawOpHelper_DEFINED
10
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040011#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrMemoryPool.h"
13#include "src/gpu/GrOpFlushState.h"
14#include "src/gpu/GrPipeline.h"
15#include "src/gpu/GrRecordingContextPriv.h"
16#include "src/gpu/ops/GrMeshDrawOp.h"
Herb Derbyc76d4092020-10-07 16:46:15 -040017#include "src/gpu/ops/GrOp.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040018#include <new>
Brian Salomonb4d61062017-07-12 11:24:41 -040019
20struct SkRect;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040021
22/**
23 * This class can be used to help implement simple mesh draw ops. It reduces the amount of
24 * boilerplate code to type and also provides a mechanism for optionally allocating space for a
25 * GrProcessorSet based on a GrPaint. It is intended to be used by ops that construct a single
26 * GrPipeline for a uniform primitive color and a GrPaint.
27 */
28class GrSimpleMeshDrawOpHelper {
29public:
Brian Salomon6d4b65e2017-05-03 17:06:09 -040030 /**
31 * This can be used by a Op class to perform allocation and initialization such that a
Brian Salomonb4d61062017-07-12 11:24:41 -040032 * GrProcessorSet (if required) is allocated as part of the the same allocation that as
33 * the Op instance. It requires that Op implements a constructor of the form:
Herb Derbyc76d4092020-10-07 16:46:15 -040034 * Op(ProcessorSet*, GrColor, OpArgs...).
Brian Salomon6d4b65e2017-05-03 17:06:09 -040035 */
36 template <typename Op, typename... OpArgs>
Herb Derbyc76d4092020-10-07 16:46:15 -040037 static GrOp::Owner FactoryHelper(GrRecordingContext*, GrPaint&&, OpArgs&&...);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040038
Chris Daltonbaa1b352019-04-03 12:03:00 -060039 // Here we allow callers to specify a subset of the GrPipeline::InputFlags upon creation.
40 enum class InputFlags : uint8_t {
41 kNone = 0,
42 kSnapVerticesToPixelCenters = (uint8_t)GrPipeline::InputFlags::kSnapVerticesToPixelCenters,
Chris Daltonc3b67eb2020-02-10 21:09:58 -070043 kConservativeRaster = (uint8_t)GrPipeline::InputFlags::kConservativeRaster,
Brian Salomonbaaf4392017-06-15 09:59:23 -040044 };
Chris Daltonbaa1b352019-04-03 12:03:00 -060045 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(InputFlags);
Brian Salomonbaaf4392017-06-15 09:59:23 -040046
Herb Derbyc76d4092020-10-07 16:46:15 -040047 GrSimpleMeshDrawOpHelper(GrProcessorSet*, GrAAType, InputFlags = InputFlags::kNone);
Brian Salomonb4d61062017-07-12 11:24:41 -040048 ~GrSimpleMeshDrawOpHelper();
Brian Salomon6d4b65e2017-05-03 17:06:09 -040049
50 GrSimpleMeshDrawOpHelper() = delete;
51 GrSimpleMeshDrawOpHelper(const GrSimpleMeshDrawOpHelper&) = delete;
52 GrSimpleMeshDrawOpHelper& operator=(const GrSimpleMeshDrawOpHelper&) = delete;
53
Brian Salomonb4d61062017-07-12 11:24:41 -040054 GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040055
Robert Phillipsb69001f2019-10-29 12:16:35 -040056 // ignoreAAType should be set to true if the op already knows the AA settings are acceptible
Brian Salomonb4d61062017-07-12 11:24:41 -040057 bool isCompatible(const GrSimpleMeshDrawOpHelper& that, const GrCaps&, const SkRect& thisBounds,
Robert Phillipsb69001f2019-10-29 12:16:35 -040058 const SkRect& thatBounds, bool ignoreAAType = false) const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040059
Brian Salomon0088f942017-07-12 11:51:27 -040060 /**
61 * Finalizes the processor set and determines whether the destination must be provided
62 * to the fragment shader as a texture for blending.
63 *
64 * @param geometryCoverage Describes the coverage output of the op's geometry processor
65 * @param geometryColor An in/out param. As input this informs processor analysis about the
66 * color the op expects to output from its geometry processor. As output
67 * this may be set to a known color in which case the op must output this
68 * color from its geometry processor instead.
69 */
Chris Dalton57ab06c2021-04-22 12:57:28 -060070 GrProcessorSet::Analysis finalizeProcessors(const GrCaps& caps, const GrAppliedClip* clip,
71 GrClampType clampType,
72 GrProcessorAnalysisCoverage geometryCoverage,
73 GrProcessorAnalysisColor* geometryColor) {
74 return this->finalizeProcessors(caps, clip, &GrUserStencilSettings::kUnused, clampType,
75 geometryCoverage, geometryColor);
Chris Daltonb8fff0d2019-03-05 10:11:58 -070076 }
Brian Salomon0088f942017-07-12 11:51:27 -040077
78 /**
79 * Version of above that can be used by ops that have a constant color geometry processor
80 * output. The op passes this color as 'geometryColor' and after return if 'geometryColor' has
81 * changed the op must override its geometry processor color output with the new color.
82 */
Chris Dalton57ab06c2021-04-22 12:57:28 -060083 GrProcessorSet::Analysis finalizeProcessors(const GrCaps&, const GrAppliedClip*, GrClampType,
84 GrProcessorAnalysisCoverage geometryCoverage,
85 SkPMColor4f* geometryColor, bool* wideColor);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040086
Michael Ludwigdcd48212019-01-08 15:28:57 -050087 bool isTrivial() const {
88 return fProcessors == nullptr;
89 }
90
Brian Salomon05441c42017-05-15 16:45:49 -040091 bool usesLocalCoords() const {
92 SkASSERT(fDidAnalysis);
93 return fUsesLocalCoords;
94 }
95
Brian Osman605c6d52019-03-15 12:10:35 -040096 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Brian Salomon28207df2017-06-05 12:25:13 -040097
Robert Phillips294723d2021-06-17 09:23:58 -040098 void visitProxies(const GrVisitProxyFunc& func) const {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040099 if (fProcessors) {
100 fProcessors->visitProxies(func);
101 }
102 }
103
John Stiles8d9bf642020-08-12 15:07:45 -0400104#if GR_TEST_UTILS
Brian Salomonb4d61062017-07-12 11:24:41 -0400105 SkString dumpInfo() const;
Brian Osman9a390ac2018-11-12 09:47:48 -0500106#endif
Michael Ludwigc6473242018-11-01 11:08:35 -0400107 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400108
Michael Ludwig69858532018-11-28 15:34:34 -0500109 void setAAType(GrAAType aaType) {
Robert Phillips438d9862019-11-14 12:46:05 -0500110 fAAType = static_cast<unsigned>(aaType);
Michael Ludwig69858532018-11-28 15:34:34 -0500111 }
112
Robert Phillips3968fcb2019-12-05 16:40:31 -0500113 static const GrPipeline* CreatePipeline(
Robert Phillips6c59fe42020-02-27 09:30:37 -0500114 const GrCaps*,
115 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400116 GrSwizzle writeViewSwizzle,
Robert Phillips6c59fe42020-02-27 09:30:37 -0500117 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400118 const GrDstProxyView&,
Robert Phillips6c59fe42020-02-27 09:30:37 -0500119 GrProcessorSet&&,
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600120 GrPipeline::InputFlags pipelineFlags);
Robert Phillips6c59fe42020-02-27 09:30:37 -0500121 static const GrPipeline* CreatePipeline(
122 GrOpFlushState*,
123 GrProcessorSet&&,
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600124 GrPipeline::InputFlags pipelineFlags);
Robert Phillips6c59fe42020-02-27 09:30:37 -0500125
126 const GrPipeline* createPipeline(GrOpFlushState* flushState);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700127
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600128 const GrPipeline* createPipeline(const GrCaps*,
129 SkArenaAlloc*,
130 GrSwizzle writeViewSwizzle,
131 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400132 const GrDstProxyView&);
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600133
Chris Dalton2a26c502021-08-26 10:05:11 -0600134 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
135 SkArenaAlloc*,
Robert Phillips4f93c572020-03-18 08:13:53 -0400136 const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500137 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600138 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -0400139 GrGeometryProcessor*,
Greg Danield358cbe2020-09-11 09:33:54 -0400140 GrPrimitiveType,
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600141 GrXferBarrierFlags renderPassXferBarriers,
Greg Daniel42dbca52020-11-20 10:22:43 -0500142 GrLoadOp colorLoadOp,
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600143 const GrUserStencilSettings*
144 = &GrUserStencilSettings::kUnused);
Robert Phillips4f93c572020-03-18 08:13:53 -0400145
Robert Phillipsce978572020-02-28 11:56:44 -0500146 // Create a programInfo with the following properties:
147 // its primitive processor uses no textures
148 // it has no dynamic state besides the scissor clip
Robert Phillipsce978572020-02-28 11:56:44 -0500149 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
150 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500151 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600152 bool usesMSAASurface,
Robert Phillipsce978572020-02-28 11:56:44 -0500153 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400154 const GrDstProxyView&,
Robert Phillipsce978572020-02-28 11:56:44 -0500155 GrGeometryProcessor*,
156 GrProcessorSet&&,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500157 GrPrimitiveType,
Greg Danield358cbe2020-09-11 09:33:54 -0400158 GrXferBarrierFlags renderPassXferBarriers,
Greg Daniel42dbca52020-11-20 10:22:43 -0500159 GrLoadOp colorLoadOp,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500160 GrPipeline::InputFlags pipelineFlags
161 = GrPipeline::InputFlags::kNone,
162 const GrUserStencilSettings*
Robert Phillips709e2402020-03-23 18:29:16 +0000163 = &GrUserStencilSettings::kUnused);
Robert Phillipsce978572020-02-28 11:56:44 -0500164
Robert Phillipsb58098f2020-03-02 16:25:29 -0500165 GrProgramInfo* createProgramInfo(const GrCaps*,
166 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500167 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600168 bool usesMSAASurface,
Robert Phillipsb58098f2020-03-02 16:25:29 -0500169 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400170 const GrDstProxyView&,
Robert Phillipsb58098f2020-03-02 16:25:29 -0500171 GrGeometryProcessor*,
Greg Danield358cbe2020-09-11 09:33:54 -0400172 GrPrimitiveType,
Greg Daniel42dbca52020-11-20 10:22:43 -0500173 GrXferBarrierFlags renderPassXferBarriers,
174 GrLoadOp colorLoadOp);
Robert Phillipsb58098f2020-03-02 16:25:29 -0500175
Robert Phillips3968fcb2019-12-05 16:40:31 -0500176 GrProcessorSet detachProcessorSet() {
177 return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet();
178 }
179
Chris Daltonbaa1b352019-04-03 12:03:00 -0600180 GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; }
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400181
Robert Phillips3968fcb2019-12-05 16:40:31 -0500182protected:
Chris Dalton57ab06c2021-04-22 12:57:28 -0600183 GrProcessorSet::Analysis finalizeProcessors(const GrCaps& caps, const GrAppliedClip*,
184 const GrUserStencilSettings*, GrClampType,
185 GrProcessorAnalysisCoverage geometryCoverage,
186 GrProcessorAnalysisColor* geometryColor);
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700187
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400188 GrProcessorSet* fProcessors;
Chris Daltonbaa1b352019-04-03 12:03:00 -0600189 GrPipeline::InputFlags fPipelineFlags;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400190 unsigned fAAType : 2;
Brian Salomon05441c42017-05-15 16:45:49 -0400191 unsigned fUsesLocalCoords : 1;
Brian Osman605c6d52019-03-15 12:10:35 -0400192 unsigned fCompatibleWithCoverageAsAlpha : 1;
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400193 SkDEBUGCODE(unsigned fMadePipeline : 1;)
Brian Salomon05441c42017-05-15 16:45:49 -0400194 SkDEBUGCODE(unsigned fDidAnalysis : 1;)
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400195};
196
Herb Derbyc76d4092020-10-07 16:46:15 -0400197template<typename Op, typename... Args>
198GrOp::Owner GrOp::MakeWithProcessorSet(
199 GrRecordingContext* context, const SkPMColor4f& color,
200 GrPaint&& paint, Args&&... args) {
Herb Derbyc9a24c92020-12-01 16:59:40 -0500201 char* bytes = (char*)::operator new(sizeof(Op) + sizeof(GrProcessorSet));
202 char* setMem = bytes + sizeof(Op);
203 GrProcessorSet* processorSet = new (setMem) GrProcessorSet{std::move(paint)};
204 return Owner{new (bytes) Op(processorSet, color, std::forward<Args>(args)...)};
Herb Derbyc76d4092020-10-07 16:46:15 -0400205}
Robert Phillipsc994a932018-06-19 13:09:54 -0400206
Herb Derbyc76d4092020-10-07 16:46:15 -0400207template <typename Op, typename... OpArgs>
208GrOp::Owner GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context,
209 GrPaint&& paint,
210 OpArgs&& ... opArgs) {
211 auto color = paint.getColor4f();
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400212 if (paint.isTrivial()) {
Herb Derbyc76d4092020-10-07 16:46:15 -0400213 return GrOp::Make<Op>(context, nullptr, color, std::forward<OpArgs>(opArgs)...);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400214 } else {
Herb Derbyc76d4092020-10-07 16:46:15 -0400215 return GrOp::MakeWithProcessorSet<Op>(
216 context, color, std::move(paint), std::forward<OpArgs>(opArgs)...);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400217 }
218}
219
Chris Daltonbaa1b352019-04-03 12:03:00 -0600220GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400221
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400222#endif