Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #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 Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 17 | #include <new> |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 18 | |
| 19 | struct SkRect; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 20 | |
| 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 | */ |
| 27 | class GrSimpleMeshDrawOpHelper { |
| 28 | public: |
| 29 | struct MakeArgs; |
| 30 | |
| 31 | /** |
| 32 | * This can be used by a Op class to perform allocation and initialization such that a |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 33 | * 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 Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 35 | * Op(MakeArgs, GrColor, OpArgs...) |
| 36 | * which is public or made accessible via 'friend'. |
| 37 | */ |
| 38 | template <typename Op, typename... OpArgs> |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 39 | static std::unique_ptr<GrDrawOp> FactoryHelper(GrRecordingContext*, GrPaint&&, OpArgs...); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 40 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 41 | // 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 Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 45 | kConservativeRaster = (uint8_t)GrPipeline::InputFlags::kConservativeRaster, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 46 | }; |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 47 | GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(InputFlags); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 48 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 49 | GrSimpleMeshDrawOpHelper(const MakeArgs&, GrAAType, InputFlags = InputFlags::kNone); |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 50 | ~GrSimpleMeshDrawOpHelper(); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 51 | |
| 52 | GrSimpleMeshDrawOpHelper() = delete; |
| 53 | GrSimpleMeshDrawOpHelper(const GrSimpleMeshDrawOpHelper&) = delete; |
| 54 | GrSimpleMeshDrawOpHelper& operator=(const GrSimpleMeshDrawOpHelper&) = delete; |
| 55 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 56 | GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 57 | |
Robert Phillips | b69001f | 2019-10-29 12:16:35 -0400 | [diff] [blame] | 58 | // ignoreAAType should be set to true if the op already knows the AA settings are acceptible |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 59 | bool isCompatible(const GrSimpleMeshDrawOpHelper& that, const GrCaps&, const SkRect& thisBounds, |
Robert Phillips | b69001f | 2019-10-29 12:16:35 -0400 | [diff] [blame] | 60 | const SkRect& thatBounds, bool ignoreAAType = false) const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 61 | |
Brian Salomon | 0088f94 | 2017-07-12 11:51:27 -0400 | [diff] [blame] | 62 | /** |
| 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 Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 72 | GrProcessorSet::Analysis finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 73 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 74 | GrClampType clampType, GrProcessorAnalysisCoverage geometryCoverage, |
| 75 | GrProcessorAnalysisColor* geometryColor) { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 76 | return this->finalizeProcessors( |
| 77 | caps, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, clampType, |
| 78 | geometryCoverage, geometryColor); |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 79 | } |
Brian Salomon | 0088f94 | 2017-07-12 11:51:27 -0400 | [diff] [blame] | 80 | |
| 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 Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 86 | GrProcessorSet::Analysis finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 87 | const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 88 | GrProcessorAnalysisCoverage geometryCoverage, SkPMColor4f* geometryColor, |
| 89 | bool* wideColor); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 90 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 91 | bool isTrivial() const { |
| 92 | return fProcessors == nullptr; |
| 93 | } |
| 94 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 95 | bool usesLocalCoords() const { |
| 96 | SkASSERT(fDidAnalysis); |
| 97 | return fUsesLocalCoords; |
| 98 | } |
| 99 | |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 100 | bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
Brian Salomon | 28207df | 2017-06-05 12:25:13 -0400 | [diff] [blame] | 101 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 102 | struct MakeArgs { |
| 103 | private: |
| 104 | MakeArgs() = default; |
| 105 | |
| 106 | GrProcessorSet* fProcessorSet; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 107 | |
| 108 | friend class GrSimpleMeshDrawOpHelper; |
| 109 | }; |
| 110 | |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 111 | void visitProxies(const GrOp::VisitProxyFunc& func) const { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 112 | if (fProcessors) { |
| 113 | fProcessors->visitProxies(func); |
| 114 | } |
| 115 | } |
| 116 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 117 | #ifdef SK_DEBUG |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 118 | SkString dumpInfo() const; |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 119 | #endif |
Michael Ludwig | c647324 | 2018-11-01 11:08:35 -0400 | [diff] [blame] | 120 | GrAAType aaType() const { return static_cast<GrAAType>(fAAType); } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 121 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 122 | void setAAType(GrAAType aaType) { |
Robert Phillips | 438d986 | 2019-11-14 12:46:05 -0500 | [diff] [blame] | 123 | fAAType = static_cast<unsigned>(aaType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 126 | static const GrPipeline* CreatePipeline( |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 127 | const GrCaps*, |
| 128 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 129 | GrSwizzle writeViewSwizzle, |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 130 | 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 Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 142 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 143 | static GrProgramInfo* CreateProgramInfo(SkArenaAlloc*, |
| 144 | const GrPipeline*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 145 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 146 | GrGeometryProcessor*, |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 147 | GrPrimitiveType); |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 148 | |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 149 | // 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 Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 152 | static GrProgramInfo* CreateProgramInfo(const GrCaps*, |
| 153 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 154 | const GrSurfaceProxyView* writeView, |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 155 | GrAppliedClip&&, |
| 156 | const GrXferProcessor::DstProxyView&, |
| 157 | GrGeometryProcessor*, |
| 158 | GrProcessorSet&&, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 159 | GrPrimitiveType, |
| 160 | GrPipeline::InputFlags pipelineFlags |
| 161 | = GrPipeline::InputFlags::kNone, |
| 162 | const GrUserStencilSettings* |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 163 | = &GrUserStencilSettings::kUnused); |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 164 | |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 165 | GrProgramInfo* createProgramInfo(const GrCaps*, |
| 166 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 167 | const GrSurfaceProxyView* writeView, |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 168 | GrAppliedClip&&, |
| 169 | const GrXferProcessor::DstProxyView&, |
| 170 | GrGeometryProcessor*, |
| 171 | GrPrimitiveType); |
| 172 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 173 | GrProcessorSet detachProcessorSet() { |
| 174 | return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet(); |
| 175 | } |
| 176 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 177 | GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; } |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 178 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 179 | protected: |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 180 | GrProcessorSet::Analysis finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 181 | const GrCaps& caps, const GrAppliedClip*, const GrUserStencilSettings*, |
| 182 | bool hasMixedSampledCoverage, GrClampType, GrProcessorAnalysisCoverage geometryCoverage, |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 183 | GrProcessorAnalysisColor* geometryColor); |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 184 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 185 | GrProcessorSet* fProcessors; |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 186 | GrPipeline::InputFlags fPipelineFlags; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 187 | unsigned fAAType : 2; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 188 | unsigned fUsesLocalCoords : 1; |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 189 | unsigned fCompatibleWithCoverageAsAlpha : 1; |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 190 | SkDEBUGCODE(unsigned fMadePipeline : 1;) |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 191 | SkDEBUGCODE(unsigned fDidAnalysis : 1;) |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 192 | }; |
| 193 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 194 | template <typename Op, typename... OpArgs> |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 195 | std::unique_ptr<GrDrawOp> GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 196 | GrPaint&& paint, |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 197 | OpArgs... opArgs) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 198 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 199 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 200 | MakeArgs makeArgs; |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 201 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 202 | if (paint.isTrivial()) { |
| 203 | makeArgs.fProcessorSet = nullptr; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 204 | return pool->allocate<Op>(makeArgs, paint.getColor4f(), std::forward<OpArgs>(opArgs)...); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 205 | } else { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 206 | char* mem = (char*) pool->allocate(sizeof(Op) + sizeof(GrProcessorSet)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 207 | char* setMem = mem + sizeof(Op); |
Mike Klein | ad64873 | 2018-12-12 09:40:40 -0500 | [diff] [blame] | 208 | auto color = paint.getColor4f(); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 209 | makeArgs.fProcessorSet = new (setMem) GrProcessorSet(std::move(paint)); |
Mike Klein | ad64873 | 2018-12-12 09:40:40 -0500 | [diff] [blame] | 210 | return std::unique_ptr<GrDrawOp>(new (mem) Op(makeArgs, color, |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 211 | std::forward<OpArgs>(opArgs)...)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 215 | GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 216 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 217 | #endif |