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 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(
114 const GrCaps*,
115 SkArenaAlloc*,
116 skgpu::Swizzle writeViewSwizzle,
117 GrAppliedClip&&,
118 const GrDstProxyView&,
119 GrProcessorSet&&,
120 GrPipeline::InputFlags pipelineFlags);
121 static const GrPipeline* CreatePipeline(
122 GrOpFlushState*,
123 GrProcessorSet&&,
124 GrPipeline::InputFlags pipelineFlags);
125
126 const GrPipeline* createPipeline(GrOpFlushState* flushState);
127
128 const GrPipeline* createPipeline(const GrCaps*,
129 SkArenaAlloc*,
130 skgpu::Swizzle writeViewSwizzle,
131 GrAppliedClip&&,
132 const GrDstProxyView&);
133
134 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
135 SkArenaAlloc*,
136 const GrPipeline*,
137 const GrSurfaceProxyView& writeView,
138 bool usesMSAASurface,
139 GrGeometryProcessor*,
140 GrPrimitiveType,
141 GrXferBarrierFlags renderPassXferBarriers,
142 GrLoadOp colorLoadOp,
143 const GrUserStencilSettings*
144 = &GrUserStencilSettings::kUnused);
145
146 // Create a programInfo with the following properties:
147 // its primitive processor uses no textures
148 // it has no dynamic state besides the scissor clip
149 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
150 SkArenaAlloc*,
151 const GrSurfaceProxyView& writeView,
152 bool usesMSAASurface,
153 GrAppliedClip&&,
154 const GrDstProxyView&,
155 GrGeometryProcessor*,
156 GrProcessorSet&&,
157 GrPrimitiveType,
158 GrXferBarrierFlags renderPassXferBarriers,
159 GrLoadOp colorLoadOp,
160 GrPipeline::InputFlags pipelineFlags
161 = GrPipeline::InputFlags::kNone,
162 const GrUserStencilSettings*
163 = &GrUserStencilSettings::kUnused);
164
165 GrProgramInfo* createProgramInfo(const GrCaps*,
166 SkArenaAlloc*,
167 const GrSurfaceProxyView& writeView,
168 bool usesMSAASurface,
169 GrAppliedClip&&,
170 const GrDstProxyView&,
171 GrGeometryProcessor*,
172 GrPrimitiveType,
173 GrXferBarrierFlags renderPassXferBarriers,
174 GrLoadOp colorLoadOp);
175
detachProcessorSet()176 GrProcessorSet detachProcessorSet() {
177 return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet();
178 }
179
pipelineFlags()180 GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; }
181
182 protected:
183 GrProcessorSet::Analysis finalizeProcessors(const GrCaps& caps, const GrAppliedClip*,
184 const GrUserStencilSettings*, GrClampType,
185 GrProcessorAnalysisCoverage geometryCoverage,
186 GrProcessorAnalysisColor* geometryColor);
187
188 GrProcessorSet* fProcessors;
189 GrPipeline::InputFlags fPipelineFlags;
190 unsigned fAAType : 2;
191 unsigned fUsesLocalCoords : 1;
192 unsigned fCompatibleWithCoverageAsAlpha : 1;
193 SkDEBUGCODE(unsigned fMadePipeline : 1;)
194 SkDEBUGCODE(unsigned fDidAnalysis : 1;)
195 };
196
197 template<typename Op, typename... Args>
MakeWithProcessorSet(GrRecordingContext * context,const SkPMColor4f & color,GrPaint && paint,Args &&...args)198 GrOp::Owner GrOp::MakeWithProcessorSet(
199 GrRecordingContext* context, const SkPMColor4f& color,
200 GrPaint&& paint, Args&&... args) {
201 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)...)};
205 }
206
207 template <typename Op, typename... OpArgs>
FactoryHelper(GrRecordingContext * context,GrPaint && paint,OpArgs &&...opArgs)208 GrOp::Owner GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context,
209 GrPaint&& paint,
210 OpArgs&& ... opArgs) {
211 auto color = paint.getColor4f();
212 if (paint.isTrivial()) {
213 return GrOp::Make<Op>(context, nullptr, color, std::forward<OpArgs>(opArgs)...);
214 } else {
215 return GrOp::MakeWithProcessorSet<Op>(
216 context, color, std::move(paint), std::forward<OpArgs>(opArgs)...);
217 }
218 }
219
220 GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags)
221
222 #endif
223