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/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"
17 #include "src/gpu/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 GrOp::VisitProxyFunc & func)98 void visitProxies(const GrOp::VisitProxyFunc& 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 GrSwizzle writeViewSwizzle,
117 GrAppliedClip&&,
118 const GrXferProcessor::DstProxyView&,
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 GrSwizzle writeViewSwizzle,
131 GrAppliedClip&&,
132 const GrXferProcessor::DstProxyView&);
133
134 static GrProgramInfo* CreateProgramInfo(SkArenaAlloc*,
135 const GrPipeline*,
136 const GrSurfaceProxyView& writeView,
137 GrGeometryProcessor*,
138 GrPrimitiveType,
139 GrXferBarrierFlags renderPassXferBarriers,
140 GrLoadOp colorLoadOp,
141 const GrUserStencilSettings*
142 = &GrUserStencilSettings::kUnused);
143
144 // Create a programInfo with the following properties:
145 // its primitive processor uses no textures
146 // it has no dynamic state besides the scissor clip
147 static GrProgramInfo* CreateProgramInfo(const GrCaps*,
148 SkArenaAlloc*,
149 const GrSurfaceProxyView& writeView,
150 GrAppliedClip&&,
151 const GrXferProcessor::DstProxyView&,
152 GrGeometryProcessor*,
153 GrProcessorSet&&,
154 GrPrimitiveType,
155 GrXferBarrierFlags renderPassXferBarriers,
156 GrLoadOp colorLoadOp,
157 GrPipeline::InputFlags pipelineFlags
158 = GrPipeline::InputFlags::kNone,
159 const GrUserStencilSettings*
160 = &GrUserStencilSettings::kUnused);
161
162 GrProgramInfo* createProgramInfo(const GrCaps*,
163 SkArenaAlloc*,
164 const GrSurfaceProxyView& writeView,
165 GrAppliedClip&&,
166 const GrXferProcessor::DstProxyView&,
167 GrGeometryProcessor*,
168 GrPrimitiveType,
169 GrXferBarrierFlags renderPassXferBarriers,
170 GrLoadOp colorLoadOp);
171
detachProcessorSet()172 GrProcessorSet detachProcessorSet() {
173 return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet();
174 }
175
pipelineFlags()176 GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; }
177
178 protected:
179 GrProcessorSet::Analysis finalizeProcessors(const GrCaps& caps, const GrAppliedClip*,
180 const GrUserStencilSettings*, GrClampType,
181 GrProcessorAnalysisCoverage geometryCoverage,
182 GrProcessorAnalysisColor* geometryColor);
183
184 GrProcessorSet* fProcessors;
185 GrPipeline::InputFlags fPipelineFlags;
186 unsigned fAAType : 2;
187 unsigned fUsesLocalCoords : 1;
188 unsigned fCompatibleWithCoverageAsAlpha : 1;
189 SkDEBUGCODE(unsigned fMadePipeline : 1;)
190 SkDEBUGCODE(unsigned fDidAnalysis : 1;)
191 };
192
193 template<typename Op, typename... Args>
MakeWithProcessorSet(GrRecordingContext * context,const SkPMColor4f & color,GrPaint && paint,Args &&...args)194 GrOp::Owner GrOp::MakeWithProcessorSet(
195 GrRecordingContext* context, const SkPMColor4f& color,
196 GrPaint&& paint, Args&&... args) {
197 char* bytes = (char*)::operator new(sizeof(Op) + sizeof(GrProcessorSet));
198 char* setMem = bytes + sizeof(Op);
199 GrProcessorSet* processorSet = new (setMem) GrProcessorSet{std::move(paint)};
200 return Owner{new (bytes) Op(processorSet, color, std::forward<Args>(args)...)};
201 }
202
203 template <typename Op, typename... OpArgs>
FactoryHelper(GrRecordingContext * context,GrPaint && paint,OpArgs &&...opArgs)204 GrOp::Owner GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context,
205 GrPaint&& paint,
206 OpArgs&& ... opArgs) {
207 auto color = paint.getColor4f();
208 if (paint.isTrivial()) {
209 return GrOp::Make<Op>(context, nullptr, color, std::forward<OpArgs>(opArgs)...);
210 } else {
211 return GrOp::MakeWithProcessorSet<Op>(
212 context, color, std::move(paint), std::forward<OpArgs>(opArgs)...);
213 }
214 }
215
216 GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags)
217
218 #endif
219