blob: 7dfd130c863110c288fbf49e5587c07bd1808125 [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
Brian Salomonb4d61062017-07-12 11:24:41 -040011#include "GrMeshDrawOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040012#include "GrOpFlushState.h"
13#include "GrPipeline.h"
Brian Salomonb4d61062017-07-12 11:24:41 -040014
15struct SkRect;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040016
17/**
18 * This class can be used to help implement simple mesh draw ops. It reduces the amount of
19 * boilerplate code to type and also provides a mechanism for optionally allocating space for a
20 * GrProcessorSet based on a GrPaint. It is intended to be used by ops that construct a single
21 * GrPipeline for a uniform primitive color and a GrPaint.
22 */
23class GrSimpleMeshDrawOpHelper {
24public:
25 struct MakeArgs;
26
27 /**
28 * This can be used by a Op class to perform allocation and initialization such that a
Brian Salomonb4d61062017-07-12 11:24:41 -040029 * GrProcessorSet (if required) is allocated as part of the the same allocation that as
30 * the Op instance. It requires that Op implements a constructor of the form:
Brian Salomon6d4b65e2017-05-03 17:06:09 -040031 * Op(MakeArgs, GrColor, OpArgs...)
32 * which is public or made accessible via 'friend'.
33 */
34 template <typename Op, typename... OpArgs>
35 static std::unique_ptr<GrDrawOp> FactoryHelper(GrPaint&& paint, OpArgs... opArgs);
36
Brian Salomonbaaf4392017-06-15 09:59:23 -040037 enum class Flags : uint32_t {
38 kNone = 0x0,
39 kSnapVerticesToPixelCenters = 0x1,
40 };
41 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
42
Brian Salomonb4d61062017-07-12 11:24:41 -040043 GrSimpleMeshDrawOpHelper(const MakeArgs&, GrAAType, Flags = Flags::kNone);
44 ~GrSimpleMeshDrawOpHelper();
Brian Salomon6d4b65e2017-05-03 17:06:09 -040045
46 GrSimpleMeshDrawOpHelper() = delete;
47 GrSimpleMeshDrawOpHelper(const GrSimpleMeshDrawOpHelper&) = delete;
48 GrSimpleMeshDrawOpHelper& operator=(const GrSimpleMeshDrawOpHelper&) = delete;
49
Brian Salomonb4d61062017-07-12 11:24:41 -040050 GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040051
Brian Salomonb4d61062017-07-12 11:24:41 -040052 bool isCompatible(const GrSimpleMeshDrawOpHelper& that, const GrCaps&, const SkRect& thisBounds,
53 const SkRect& thatBounds) const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -040054
Brian Salomon0088f942017-07-12 11:51:27 -040055 /**
56 * Finalizes the processor set and determines whether the destination must be provided
57 * to the fragment shader as a texture for blending.
58 *
59 * @param geometryCoverage Describes the coverage output of the op's geometry processor
60 * @param geometryColor An in/out param. As input this informs processor analysis about the
61 * color the op expects to output from its geometry processor. As output
62 * this may be set to a known color in which case the op must output this
63 * color from its geometry processor instead.
64 */
65 GrDrawOp::RequiresDstTexture xpRequiresDstTexture(const GrCaps& caps, const GrAppliedClip* clip,
66 GrProcessorAnalysisCoverage geometryCoverage,
67 GrProcessorAnalysisColor* geometryColor);
68
69 /**
70 * Version of above that can be used by ops that have a constant color geometry processor
71 * output. The op passes this color as 'geometryColor' and after return if 'geometryColor' has
72 * changed the op must override its geometry processor color output with the new color.
73 */
Brian Salomonb4d61062017-07-12 11:24:41 -040074 GrDrawOp::RequiresDstTexture xpRequiresDstTexture(const GrCaps&, const GrAppliedClip*,
Brian Salomonf86d37b2017-06-16 10:04:34 -040075 GrProcessorAnalysisCoverage geometryCoverage,
Brian Salomon0088f942017-07-12 11:51:27 -040076 GrColor* geometryColor);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040077
Brian Salomon05441c42017-05-15 16:45:49 -040078 bool usesLocalCoords() const {
79 SkASSERT(fDidAnalysis);
80 return fUsesLocalCoords;
81 }
82
Brian Salomon28207df2017-06-05 12:25:13 -040083 bool compatibleWithAlphaAsCoverage() const { return fCompatibleWithAlphaAsCoveage; }
84
Brian Salomon91326c32017-08-09 16:02:19 -040085 GrPipeline* makePipeline(GrMeshDrawOp::Target* target) {
86 return this->internalMakePipeline(target, this->pipelineInitArgs(target));
Brian Salomon6d4b65e2017-05-03 17:06:09 -040087 }
88
89 struct MakeArgs {
90 private:
91 MakeArgs() = default;
92
93 GrProcessorSet* fProcessorSet;
94 uint32_t fSRGBFlags;
95
96 friend class GrSimpleMeshDrawOpHelper;
97 };
98
Brian Salomonb4d61062017-07-12 11:24:41 -040099 SkString dumpInfo() const;
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400100
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400101protected:
102 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
103 uint32_t pipelineFlags() const { return fPipelineFlags; }
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400104
Brian Salomonb4d61062017-07-12 11:24:41 -0400105 GrPipeline::InitArgs pipelineInitArgs(GrMeshDrawOp::Target* target) const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400106
Brian Salomon91326c32017-08-09 16:02:19 -0400107 GrPipeline* internalMakePipeline(GrMeshDrawOp::Target*, const GrPipeline::InitArgs&);
108
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400109private:
110 GrProcessorSet* fProcessors;
111 unsigned fPipelineFlags : 8;
112 unsigned fAAType : 2;
Brian Salomona4677b52017-05-04 12:39:56 -0400113 unsigned fRequiresDstTexture : 1;
Brian Salomon05441c42017-05-15 16:45:49 -0400114 unsigned fUsesLocalCoords : 1;
Brian Salomon28207df2017-06-05 12:25:13 -0400115 unsigned fCompatibleWithAlphaAsCoveage : 1;
Brian Salomon05441c42017-05-15 16:45:49 -0400116 SkDEBUGCODE(unsigned fDidAnalysis : 1;)
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400117};
118
119/**
120 * This class extends GrSimpleMeshDrawOpHelper to support an optional GrUserStencilSettings. This
121 * uses private inheritance because it non-virtually overrides methods in the base class and should
122 * never be used with a GrSimpleMeshDrawOpHelper pointer or reference.
123 */
124class GrSimpleMeshDrawOpHelperWithStencil : private GrSimpleMeshDrawOpHelper {
125public:
126 using MakeArgs = GrSimpleMeshDrawOpHelper::MakeArgs;
Brian Salomonbaaf4392017-06-15 09:59:23 -0400127 using Flags = GrSimpleMeshDrawOpHelper::Flags;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400128
129 // using declarations can't be templated, so this is a pass through function instead.
130 template <typename Op, typename... OpArgs>
131 static std::unique_ptr<GrDrawOp> FactoryHelper(GrPaint&& paint, OpArgs... opArgs) {
132 return GrSimpleMeshDrawOpHelper::FactoryHelper<Op, OpArgs...>(
133 std::move(paint), std::forward<OpArgs>(opArgs)...);
134 }
135
Brian Salomonb4d61062017-07-12 11:24:41 -0400136 GrSimpleMeshDrawOpHelperWithStencil(const MakeArgs&, GrAAType, const GrUserStencilSettings*,
137 Flags = Flags::kNone);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400138
Brian Salomonb4d61062017-07-12 11:24:41 -0400139 GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400140
141 using GrSimpleMeshDrawOpHelper::xpRequiresDstTexture;
Brian Salomon05441c42017-05-15 16:45:49 -0400142 using GrSimpleMeshDrawOpHelper::usesLocalCoords;
Brian Salomonbaaf4392017-06-15 09:59:23 -0400143 using GrSimpleMeshDrawOpHelper::compatibleWithAlphaAsCoverage;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400144
Brian Salomonb4d61062017-07-12 11:24:41 -0400145 bool isCompatible(const GrSimpleMeshDrawOpHelperWithStencil& that, const GrCaps&,
146 const SkRect& thisBounds, const SkRect& thatBounds) const;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400147
Brian Salomon91326c32017-08-09 16:02:19 -0400148 const GrPipeline* makePipeline(GrMeshDrawOp::Target*);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400149
Brian Salomonb4d61062017-07-12 11:24:41 -0400150 SkString dumpInfo() const;
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400151
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400152private:
153 const GrUserStencilSettings* fStencilSettings;
154 typedef GrSimpleMeshDrawOpHelper INHERITED;
155};
156
157template <typename Op, typename... OpArgs>
158std::unique_ptr<GrDrawOp> GrSimpleMeshDrawOpHelper::FactoryHelper(GrPaint&& paint,
159 OpArgs... opArgs) {
160 MakeArgs makeArgs;
161 makeArgs.fSRGBFlags = GrPipeline::SRGBFlagsFromPaint(paint);
162 GrColor color = paint.getColor();
163 if (paint.isTrivial()) {
164 makeArgs.fProcessorSet = nullptr;
165 return std::unique_ptr<GrDrawOp>(new Op(makeArgs, color, std::forward<OpArgs>(opArgs)...));
166 } else {
167 char* mem = (char*)GrOp::operator new(sizeof(Op) + sizeof(GrProcessorSet));
168 char* setMem = mem + sizeof(Op);
169 makeArgs.fProcessorSet = new (setMem) GrProcessorSet(std::move(paint));
170 return std::unique_ptr<GrDrawOp>(
171 new (mem) Op(makeArgs, color, std::forward<OpArgs>(opArgs)...));
172 }
173}
174
Brian Salomonbaaf4392017-06-15 09:59:23 -0400175GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::Flags)
176
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400177#endif