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 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 17 | #include "src/gpu/ops/GrOp.h" |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 18 | #include <new> |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 19 | |
| 20 | struct SkRect; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 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: |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 30 | /** |
| 31 | * 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] | 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: |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 34 | * Op(ProcessorSet*, GrColor, OpArgs...). |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 35 | */ |
| 36 | template <typename Op, typename... OpArgs> |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 37 | static GrOp::Owner FactoryHelper(GrRecordingContext*, GrPaint&&, OpArgs&&...); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 38 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 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, |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 43 | kConservativeRaster = (uint8_t)GrPipeline::InputFlags::kConservativeRaster, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 44 | }; |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 45 | GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(InputFlags); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 46 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 47 | GrSimpleMeshDrawOpHelper(GrProcessorSet*, GrAAType, InputFlags = InputFlags::kNone); |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 48 | ~GrSimpleMeshDrawOpHelper(); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 49 | |
| 50 | GrSimpleMeshDrawOpHelper() = delete; |
| 51 | GrSimpleMeshDrawOpHelper(const GrSimpleMeshDrawOpHelper&) = delete; |
| 52 | GrSimpleMeshDrawOpHelper& operator=(const GrSimpleMeshDrawOpHelper&) = delete; |
| 53 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 54 | GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 55 | |
Robert Phillips | b69001f | 2019-10-29 12:16:35 -0400 | [diff] [blame] | 56 | // 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] | 57 | bool isCompatible(const GrSimpleMeshDrawOpHelper& that, const GrCaps&, const SkRect& thisBounds, |
Robert Phillips | b69001f | 2019-10-29 12:16:35 -0400 | [diff] [blame] | 58 | const SkRect& thatBounds, bool ignoreAAType = false) const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 59 | |
Brian Salomon | 0088f94 | 2017-07-12 11:51:27 -0400 | [diff] [blame] | 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 | */ |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 70 | GrProcessorSet::Analysis finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 71 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 72 | GrClampType clampType, GrProcessorAnalysisCoverage geometryCoverage, |
| 73 | GrProcessorAnalysisColor* geometryColor) { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 74 | return this->finalizeProcessors( |
| 75 | caps, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, clampType, |
| 76 | geometryCoverage, geometryColor); |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 77 | } |
Brian Salomon | 0088f94 | 2017-07-12 11:51:27 -0400 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Version of above that can be used by ops that have a constant color geometry processor |
| 81 | * output. The op passes this color as 'geometryColor' and after return if 'geometryColor' has |
| 82 | * changed the op must override its geometry processor color output with the new color. |
| 83 | */ |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 84 | GrProcessorSet::Analysis finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 85 | const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 86 | GrProcessorAnalysisCoverage geometryCoverage, SkPMColor4f* geometryColor, |
| 87 | bool* wideColor); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 88 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 89 | bool isTrivial() const { |
| 90 | return fProcessors == nullptr; |
| 91 | } |
| 92 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 93 | bool usesLocalCoords() const { |
| 94 | SkASSERT(fDidAnalysis); |
| 95 | return fUsesLocalCoords; |
| 96 | } |
| 97 | |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 98 | bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
Brian Salomon | 28207df | 2017-06-05 12:25:13 -0400 | [diff] [blame] | 99 | |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 100 | void visitProxies(const GrOp::VisitProxyFunc& func) const { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 101 | if (fProcessors) { |
| 102 | fProcessors->visitProxies(func); |
| 103 | } |
| 104 | } |
| 105 | |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 106 | #if GR_TEST_UTILS |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 107 | SkString dumpInfo() const; |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 108 | #endif |
Michael Ludwig | c647324 | 2018-11-01 11:08:35 -0400 | [diff] [blame] | 109 | GrAAType aaType() const { return static_cast<GrAAType>(fAAType); } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 110 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 111 | void setAAType(GrAAType aaType) { |
Robert Phillips | 438d986 | 2019-11-14 12:46:05 -0500 | [diff] [blame] | 112 | fAAType = static_cast<unsigned>(aaType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 115 | static const GrPipeline* CreatePipeline( |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 116 | const GrCaps*, |
| 117 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 118 | GrSwizzle writeViewSwizzle, |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 119 | GrAppliedClip&&, |
| 120 | const GrXferProcessor::DstProxyView&, |
| 121 | GrProcessorSet&&, |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 122 | GrPipeline::InputFlags pipelineFlags); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 123 | static const GrPipeline* CreatePipeline( |
| 124 | GrOpFlushState*, |
| 125 | GrProcessorSet&&, |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 126 | GrPipeline::InputFlags pipelineFlags); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 127 | |
| 128 | const GrPipeline* createPipeline(GrOpFlushState* flushState); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 129 | |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 130 | const GrPipeline* createPipeline(const GrCaps*, |
| 131 | SkArenaAlloc*, |
| 132 | GrSwizzle writeViewSwizzle, |
| 133 | GrAppliedClip&&, |
| 134 | const GrXferProcessor::DstProxyView&); |
| 135 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 136 | static GrProgramInfo* CreateProgramInfo(SkArenaAlloc*, |
| 137 | const GrPipeline*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 138 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 139 | GrGeometryProcessor*, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 140 | GrPrimitiveType, |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 141 | GrXferBarrierFlags renderPassXferBarriers, |
| 142 | const GrUserStencilSettings* |
| 143 | = &GrUserStencilSettings::kUnused); |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 144 | |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 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 |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 148 | static GrProgramInfo* CreateProgramInfo(const GrCaps*, |
| 149 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 150 | const GrSurfaceProxyView* writeView, |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 151 | GrAppliedClip&&, |
| 152 | const GrXferProcessor::DstProxyView&, |
| 153 | GrGeometryProcessor*, |
| 154 | GrProcessorSet&&, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 155 | GrPrimitiveType, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 156 | GrXferBarrierFlags renderPassXferBarriers, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 157 | GrPipeline::InputFlags pipelineFlags |
| 158 | = GrPipeline::InputFlags::kNone, |
| 159 | const GrUserStencilSettings* |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 160 | = &GrUserStencilSettings::kUnused); |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 161 | |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 162 | GrProgramInfo* createProgramInfo(const GrCaps*, |
| 163 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 164 | const GrSurfaceProxyView* writeView, |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 165 | GrAppliedClip&&, |
| 166 | const GrXferProcessor::DstProxyView&, |
| 167 | GrGeometryProcessor*, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 168 | GrPrimitiveType, |
| 169 | GrXferBarrierFlags renderPassXferBarriers); |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 170 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 171 | GrProcessorSet detachProcessorSet() { |
| 172 | return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet(); |
| 173 | } |
| 174 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 175 | GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; } |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 176 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 177 | protected: |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 178 | GrProcessorSet::Analysis finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 179 | const GrCaps& caps, const GrAppliedClip*, const GrUserStencilSettings*, |
| 180 | bool hasMixedSampledCoverage, GrClampType, GrProcessorAnalysisCoverage geometryCoverage, |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 181 | GrProcessorAnalysisColor* geometryColor); |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 182 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 183 | GrProcessorSet* fProcessors; |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 184 | GrPipeline::InputFlags fPipelineFlags; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 185 | unsigned fAAType : 2; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 186 | unsigned fUsesLocalCoords : 1; |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 187 | unsigned fCompatibleWithCoverageAsAlpha : 1; |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 188 | SkDEBUGCODE(unsigned fMadePipeline : 1;) |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 189 | SkDEBUGCODE(unsigned fDidAnalysis : 1;) |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 190 | }; |
| 191 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 192 | template<typename Op, typename... Args> |
| 193 | GrOp::Owner GrOp::MakeWithProcessorSet( |
| 194 | GrRecordingContext* context, const SkPMColor4f& color, |
| 195 | GrPaint&& paint, Args&&... args) { |
| 196 | #if defined(GR_OP_ALLOCATE_USE_NEW) |
| 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 | #else |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 202 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 203 | char* bytes = (char*)pool->allocate(sizeof(Op) + sizeof(GrProcessorSet)); |
| 204 | char* setMem = bytes + sizeof(Op); |
| 205 | GrProcessorSet* processorSet = new (setMem) GrProcessorSet{std::move(paint)}; |
| 206 | return Owner{new (bytes) Op(processorSet, color, std::forward<Args>(args)...), pool}; |
| 207 | #endif |
| 208 | } |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 209 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 210 | template <typename Op, typename... OpArgs> |
| 211 | GrOp::Owner GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context, |
| 212 | GrPaint&& paint, |
| 213 | OpArgs&& ... opArgs) { |
| 214 | auto color = paint.getColor4f(); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 215 | if (paint.isTrivial()) { |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 216 | return GrOp::Make<Op>(context, nullptr, color, std::forward<OpArgs>(opArgs)...); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 217 | } else { |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame^] | 218 | return GrOp::MakeWithProcessorSet<Op>( |
| 219 | context, color, std::move(paint), std::forward<OpArgs>(opArgs)...); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 223 | GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 224 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 225 | #endif |