1 /*
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
11 #include "include/gpu/GrRecordingContext.h"
12 #include "src/gpu/ganesh/GrMemoryPool.h"
13 #include "src/gpu/ganesh/GrOpFlushState.h"
14 #include "src/gpu/ganesh/GrPipeline.h"
15 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
16 #include "src/gpu/ganesh/ops/GrMeshDrawOp.h"
17 #include "src/gpu/ganesh/ops/GrOp.h"
18 #include <new>
19
20 struct SkRect;
21
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 */
28 class GrSimpleMeshDrawOpHelper {
29 public:
30 /**
31 * This can be used by a Op class to perform allocation and initialization such that a
32 * 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:
34 * Op(ProcessorSet*, GrColor, OpArgs...).
35 */
36 template <typename Op, typename... OpArgs>
37 static GrOp::Owner FactoryHelper(GrRecordingContext*, GrPaint&&, OpArgs&&...);
38
39 // 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,
43 kConservativeRaster = (uint8_t)GrPipeline::InputFlags::kConservativeRaster,
44 };
45 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(InputFlags);
46
47 GrSimpleMeshDrawOpHelper(GrProcessorSet*, GrAAType, InputFlags = InputFlags::kNone);
48 ~GrSimpleMeshDrawOpHelper();
49
50 GrSimpleMeshDrawOpHelper() = delete;
51 GrSimpleMeshDrawOpHelper(const GrSimpleMeshDrawOpHelper&) = delete;
52 GrSimpleMeshDrawOpHelper& operator=(const GrSimpleMeshDrawOpHelper&) = delete;
53
54 GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const;
55
56 // ignoreAAType should be set to true if the op already knows the AA settings are acceptible
57 bool isCompatible(const GrSimpleMeshDrawOpHelper& that, const GrCaps&, const SkRect& thisBounds,
58 const SkRect& thatBounds, bool ignoreAAType = false) const;
59
60 /**
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 */
finalizeProcessors(const GrCaps & caps,const GrAppliedClip * clip,GrClampType clampType,GrProcessorAnalysisCoverage geometryCoverage,GrProcessorAnalysisColor * geometryColor)70 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);
76 }
77
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 */
83 GrProcessorSet::Analysis finalizeProcessors(const GrCaps&, const GrAppliedClip*, GrClampType,
84 GrProcessorAnalysisCoverage geometryCoverage,
85 SkPMColor4f* geometryColor, bool* wideColor);
86
isTrivial()87 bool isTrivial() const {
88 return fProcessors == nullptr;
89 }
90
usesLocalCoords()91 bool usesLocalCoords() const {
92 SkASSERT(fDidAnalysis);
93 return fUsesLocalCoords;
94 }
95
compatibleWithCoverageAsAlpha()96 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
97
visitProxies(const GrVisitProxyFunc & func)98 void visitProxies(const GrVisitProxyFunc& func) const {
99 if (fProcessors) {
100 fProcessors->visitProxies(func);
101 }
102 }
103
104 #if defined(GR_TEST_UTILS)
105 SkString dumpInfo() const;
106 #endif
aaType()107 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
108
setAAType(GrAAType aaType)109 void setAAType(GrAAType aaType) {
110 fAAType = static_cast<unsigned>(aaType);
111 }
112
113 static const GrPipeline* CreatePipeline(const GrCaps*,
114 SkArenaAlloc*,
115 skgpu::Swizzle writeViewSwizzle,
116 GrAppliedClip&&,
117 const GrDstProxyView&,
118 GrProcessorSet&&,
119 GrPipeline::InputFlags pipelineFlags);
120
121 static const GrPipeline* CreatePipeline(GrOpFlushState*,
122 GrProcessorSet&&,
123 GrPipeline::InputFlags pipelineFlags);
124
125 const GrPipeline* createPipeline(GrOpFlushState* flushState);
126
127 const GrPipeline* createPipeline(const GrCaps*,
128 SkArenaAlloc*,
129 skgpu::Swizzle writeViewSwizzle,
130 GrAppliedClip&&,
131 const GrDstProxyView&);
132
133 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
134 SkArenaAlloc*,
135 const GrPipeline*,
136 const GrSurfaceProxyView& writeView,
137 bool usesMSAASurface,
138 GrGeometryProcessor*,
139 GrPrimitiveType,
140 GrXferBarrierFlags renderPassXferBarriers,
141 GrLoadOp colorLoadOp,
142 const GrUserStencilSettings*
143 = &GrUserStencilSettings::kUnused);
144
145 // Create a programInfo with the following properties:
146 // its primitive processor uses no textures
147 // it has no dynamic state besides the scissor clip
148 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
149 SkArenaAlloc*,
150 const GrSurfaceProxyView& writeView,
151 bool usesMSAASurface,
152 GrAppliedClip&&,
153 const GrDstProxyView&,
154 GrGeometryProcessor*,
155 GrProcessorSet&&,
156 GrPrimitiveType,
157 GrXferBarrierFlags renderPassXferBarriers,
158 GrLoadOp colorLoadOp,
159 GrPipeline::InputFlags pipelineFlags
160 = GrPipeline::InputFlags::kNone,
161 const GrUserStencilSettings*
162 = &GrUserStencilSettings::kUnused);
163
164 GrProgramInfo* createProgramInfo(const GrCaps*,
165 SkArenaAlloc*,
166 const GrSurfaceProxyView& writeView,
167 bool usesMSAASurface,
168 GrAppliedClip&&,
169 const GrDstProxyView&,
170 GrGeometryProcessor*,
171 GrPrimitiveType,
172 GrXferBarrierFlags renderPassXferBarriers,
173 GrLoadOp colorLoadOp);
174
detachProcessorSet()175 GrProcessorSet detachProcessorSet() {
176 return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet();
177 }
178
pipelineFlags()179 GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; }
180
181 protected:
182 GrProcessorSet::Analysis finalizeProcessors(const GrCaps& caps, const GrAppliedClip*,
183 const GrUserStencilSettings*, GrClampType,
184 GrProcessorAnalysisCoverage geometryCoverage,
185 GrProcessorAnalysisColor* geometryColor);
186
187 GrProcessorSet* fProcessors;
188 GrPipeline::InputFlags fPipelineFlags;
189 unsigned fAAType : 2;
190 unsigned fUsesLocalCoords : 1;
191 unsigned fCompatibleWithCoverageAsAlpha : 1;
192 SkDEBUGCODE(unsigned fMadePipeline : 1;)
193 SkDEBUGCODE(unsigned fDidAnalysis : 1;)
194 };
195
196 template<typename Op, typename... Args>
MakeWithProcessorSet(GrRecordingContext * context,const SkPMColor4f & color,GrPaint && paint,Args &&...args)197 GrOp::Owner GrOp::MakeWithProcessorSet(
198 GrRecordingContext* context, const SkPMColor4f& color,
199 GrPaint&& paint, Args&&... args) {
200 char* bytes = (char*)::operator new(sizeof(Op) + sizeof(GrProcessorSet));
201 char* setMem = bytes + sizeof(Op);
202 GrProcessorSet* processorSet = new (setMem) GrProcessorSet{std::move(paint)};
203 return Owner{new (bytes) Op(processorSet, color, std::forward<Args>(args)...)};
204 }
205
206 template <typename Op, typename... OpArgs>
FactoryHelper(GrRecordingContext * context,GrPaint && paint,OpArgs &&...opArgs)207 GrOp::Owner GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context,
208 GrPaint&& paint,
209 OpArgs&& ... opArgs) {
210 auto color = paint.getColor4f();
211 if (paint.isTrivial()) {
212 return GrOp::Make<Op>(context, nullptr, color, std::forward<OpArgs>(opArgs)...);
213 } else {
214 return GrOp::MakeWithProcessorSet<Op>(
215 context, color, std::move(paint), std::forward<OpArgs>(opArgs)...);
216 }
217 }
218
219 GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags)
220
221 #endif
222