blob: d876c629bbacdec7ac0e6f9cd648be657ac2ded3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrRecordingContext.h"
12#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"
Mike Klein79aea6a2018-06-11 10:45:26 -040017#include <new>
Brian Salomonb4d61062017-07-12 11:24:41 -040018
19struct SkRect;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040020
21/**
22 * This class can be used to help implement simple mesh draw ops. It reduces the amount of
23 * boilerplate code to type and also provides a mechanism for optionally allocating space for a
24 * GrProcessorSet based on a GrPaint. It is intended to be used by ops that construct a single
25 * GrPipeline for a uniform primitive color and a GrPaint.
26 */
27class GrSimpleMeshDrawOpHelper {
28public:
29 struct MakeArgs;
30
31 /**
32 * This can be used by a Op class to perform allocation and initialization such that a
Brian Salomonb4d61062017-07-12 11:24:41 -040033 * GrProcessorSet (if required) is allocated as part of the the same allocation that as
34 * the Op instance. It requires that Op implements a constructor of the form:
Brian Salomon6d4b65e2017-05-03 17:06:09 -040035 * Op(MakeArgs, GrColor, OpArgs...)
36 * which is public or made accessible via 'friend'.
37 */
38 template <typename Op, typename... OpArgs>
Robert Phillipsb97da532019-02-12 15:24:12 -050039 static std::unique_ptr<GrDrawOp> FactoryHelper(GrRecordingContext*, GrPaint&&, OpArgs...);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040040
Chris Daltonbaa1b352019-04-03 12:03:00 -060041 // Here we allow callers to specify a subset of the GrPipeline::InputFlags upon creation.
42 enum class InputFlags : uint8_t {
43 kNone = 0,
44 kSnapVerticesToPixelCenters = (uint8_t)GrPipeline::InputFlags::kSnapVerticesToPixelCenters,
Chris Daltonc3b67eb2020-02-10 21:09:58 -070045 kConservativeRaster = (uint8_t)GrPipeline::InputFlags::kConservativeRaster,
Brian Salomonbaaf4392017-06-15 09:59:23 -040046 };
Chris Daltonbaa1b352019-04-03 12:03:00 -060047 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(InputFlags);
Brian Salomonbaaf4392017-06-15 09:59:23 -040048
Chris Daltonbaa1b352019-04-03 12:03:00 -060049 GrSimpleMeshDrawOpHelper(const MakeArgs&, GrAAType, InputFlags = InputFlags::kNone);
Brian Salomonb4d61062017-07-12 11:24:41 -040050 ~GrSimpleMeshDrawOpHelper();
Brian Salomon6d4b65e2017-05-03 17:06:09 -040051
52 GrSimpleMeshDrawOpHelper() = delete;
53 GrSimpleMeshDrawOpHelper(const GrSimpleMeshDrawOpHelper&) = delete;
54 GrSimpleMeshDrawOpHelper& operator=(const GrSimpleMeshDrawOpHelper&) = delete;
55
Brian Salomonb4d61062017-07-12 11:24:41 -040056 GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040057
Robert Phillipsb69001f2019-10-29 12:16:35 -040058 // ignoreAAType should be set to true if the op already knows the AA settings are acceptible
Brian Salomonb4d61062017-07-12 11:24:41 -040059 bool isCompatible(const GrSimpleMeshDrawOpHelper& that, const GrCaps&, const SkRect& thisBounds,
Robert Phillipsb69001f2019-10-29 12:16:35 -040060 const SkRect& thatBounds, bool ignoreAAType = false) const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040061
Brian Salomon0088f942017-07-12 11:51:27 -040062 /**
63 * Finalizes the processor set and determines whether the destination must be provided
64 * to the fragment shader as a texture for blending.
65 *
66 * @param geometryCoverage Describes the coverage output of the op's geometry processor
67 * @param geometryColor An in/out param. As input this informs processor analysis about the
68 * color the op expects to output from its geometry processor. As output
69 * this may be set to a known color in which case the op must output this
70 * color from its geometry processor instead.
71 */
Chris Daltonb8fff0d2019-03-05 10:11:58 -070072 GrProcessorSet::Analysis finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -060073 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
Brian Osman5ced0bf2019-03-15 10:15:29 -040074 GrClampType clampType, GrProcessorAnalysisCoverage geometryCoverage,
75 GrProcessorAnalysisColor* geometryColor) {
Chris Dalton6ce447a2019-06-23 18:07:38 -060076 return this->finalizeProcessors(
77 caps, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, clampType,
78 geometryCoverage, geometryColor);
Chris Daltonb8fff0d2019-03-05 10:11:58 -070079 }
Brian Salomon0088f942017-07-12 11:51:27 -040080
81 /**
82 * Version of above that can be used by ops that have a constant color geometry processor
83 * output. The op passes this color as 'geometryColor' and after return if 'geometryColor' has
84 * changed the op must override its geometry processor color output with the new color.
85 */
Chris Daltonb8fff0d2019-03-05 10:11:58 -070086 GrProcessorSet::Analysis finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -060087 const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -040088 GrProcessorAnalysisCoverage geometryCoverage, SkPMColor4f* geometryColor,
89 bool* wideColor);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040090
Michael Ludwigdcd48212019-01-08 15:28:57 -050091 bool isTrivial() const {
92 return fProcessors == nullptr;
93 }
94
Brian Salomon05441c42017-05-15 16:45:49 -040095 bool usesLocalCoords() const {
96 SkASSERT(fDidAnalysis);
97 return fUsesLocalCoords;
98 }
99
Brian Osman605c6d52019-03-15 12:10:35 -0400100 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Brian Salomon28207df2017-06-05 12:25:13 -0400101
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400102 struct MakeArgs {
103 private:
104 MakeArgs() = default;
105
106 GrProcessorSet* fProcessorSet;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400107
108 friend class GrSimpleMeshDrawOpHelper;
109 };
110
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600111 void visitProxies(const GrOp::VisitProxyFunc& func) const {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400112 if (fProcessors) {
113 fProcessors->visitProxies(func);
114 }
115 }
116
Brian Osman9a390ac2018-11-12 09:47:48 -0500117#ifdef SK_DEBUG
Brian Salomonb4d61062017-07-12 11:24:41 -0400118 SkString dumpInfo() const;
Brian Osman9a390ac2018-11-12 09:47:48 -0500119#endif
Michael Ludwigc6473242018-11-01 11:08:35 -0400120 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400121
Michael Ludwig69858532018-11-28 15:34:34 -0500122 void setAAType(GrAAType aaType) {
Robert Phillips438d9862019-11-14 12:46:05 -0500123 fAAType = static_cast<unsigned>(aaType);
Michael Ludwig69858532018-11-28 15:34:34 -0500124 }
125
Robert Phillips3968fcb2019-12-05 16:40:31 -0500126 static const GrPipeline* CreatePipeline(
Robert Phillips6c59fe42020-02-27 09:30:37 -0500127 const GrCaps*,
128 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400129 GrSwizzle writeViewSwizzle,
Robert Phillips6c59fe42020-02-27 09:30:37 -0500130 GrAppliedClip&&,
131 const GrXferProcessor::DstProxyView&,
132 GrProcessorSet&&,
133 GrPipeline::InputFlags pipelineFlags,
134 const GrUserStencilSettings* = &GrUserStencilSettings::kUnused);
135 static const GrPipeline* CreatePipeline(
136 GrOpFlushState*,
137 GrProcessorSet&&,
138 GrPipeline::InputFlags pipelineFlags,
139 const GrUserStencilSettings* = &GrUserStencilSettings::kUnused);
140
141 const GrPipeline* createPipeline(GrOpFlushState* flushState);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700142
Robert Phillips4f93c572020-03-18 08:13:53 -0400143 static GrProgramInfo* CreateProgramInfo(SkArenaAlloc*,
144 const GrPipeline*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400145 const GrSurfaceProxyView* writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400146 GrGeometryProcessor*,
Robert Phillips709e2402020-03-23 18:29:16 +0000147 GrPrimitiveType);
Robert Phillips4f93c572020-03-18 08:13:53 -0400148
Robert Phillipsce978572020-02-28 11:56:44 -0500149 // Create a programInfo with the following properties:
150 // its primitive processor uses no textures
151 // it has no dynamic state besides the scissor clip
Robert Phillipsce978572020-02-28 11:56:44 -0500152 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
153 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400154 const GrSurfaceProxyView* writeView,
Robert Phillipsce978572020-02-28 11:56:44 -0500155 GrAppliedClip&&,
156 const GrXferProcessor::DstProxyView&,
157 GrGeometryProcessor*,
158 GrProcessorSet&&,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500159 GrPrimitiveType,
160 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*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400167 const GrSurfaceProxyView* writeView,
Robert Phillipsb58098f2020-03-02 16:25:29 -0500168 GrAppliedClip&&,
169 const GrXferProcessor::DstProxyView&,
170 GrGeometryProcessor*,
171 GrPrimitiveType);
172
Robert Phillips3968fcb2019-12-05 16:40:31 -0500173 GrProcessorSet detachProcessorSet() {
174 return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet();
175 }
176
Chris Daltonbaa1b352019-04-03 12:03:00 -0600177 GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; }
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400178
Robert Phillips3968fcb2019-12-05 16:40:31 -0500179protected:
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700180 GrProcessorSet::Analysis finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600181 const GrCaps& caps, const GrAppliedClip*, const GrUserStencilSettings*,
182 bool hasMixedSampledCoverage, GrClampType, GrProcessorAnalysisCoverage geometryCoverage,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400183 GrProcessorAnalysisColor* geometryColor);
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700184
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400185 GrProcessorSet* fProcessors;
Chris Daltonbaa1b352019-04-03 12:03:00 -0600186 GrPipeline::InputFlags fPipelineFlags;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400187 unsigned fAAType : 2;
Brian Salomon05441c42017-05-15 16:45:49 -0400188 unsigned fUsesLocalCoords : 1;
Brian Osman605c6d52019-03-15 12:10:35 -0400189 unsigned fCompatibleWithCoverageAsAlpha : 1;
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400190 SkDEBUGCODE(unsigned fMadePipeline : 1;)
Brian Salomon05441c42017-05-15 16:45:49 -0400191 SkDEBUGCODE(unsigned fDidAnalysis : 1;)
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400192};
193
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400194template <typename Op, typename... OpArgs>
Robert Phillipsb97da532019-02-12 15:24:12 -0500195std::unique_ptr<GrDrawOp> GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400196 GrPaint&& paint,
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400197 OpArgs... opArgs) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500198 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400199
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400200 MakeArgs makeArgs;
Robert Phillips7c525e62018-06-12 10:11:12 -0400201
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400202 if (paint.isTrivial()) {
203 makeArgs.fProcessorSet = nullptr;
Brian Osmancf860852018-10-31 14:04:39 -0400204 return pool->allocate<Op>(makeArgs, paint.getColor4f(), std::forward<OpArgs>(opArgs)...);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400205 } else {
Robert Phillipsc994a932018-06-19 13:09:54 -0400206 char* mem = (char*) pool->allocate(sizeof(Op) + sizeof(GrProcessorSet));
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400207 char* setMem = mem + sizeof(Op);
Mike Kleinad648732018-12-12 09:40:40 -0500208 auto color = paint.getColor4f();
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400209 makeArgs.fProcessorSet = new (setMem) GrProcessorSet(std::move(paint));
Mike Kleinad648732018-12-12 09:40:40 -0500210 return std::unique_ptr<GrDrawOp>(new (mem) Op(makeArgs, color,
Robert Phillipsc994a932018-06-19 13:09:54 -0400211 std::forward<OpArgs>(opArgs)...));
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400212 }
213}
214
Chris Daltonbaa1b352019-04-03 12:03:00 -0600215GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400216
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400217#endif