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 | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 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); |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 76 | } |
Brian Salomon | 0088f94 | 2017-07-12 11:51:27 -0400 | [diff] [blame] | 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 | */ |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 83 | GrProcessorSet::Analysis finalizeProcessors(const GrCaps&, const GrAppliedClip*, GrClampType, |
| 84 | GrProcessorAnalysisCoverage geometryCoverage, |
| 85 | SkPMColor4f* geometryColor, bool* wideColor); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 86 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 87 | bool isTrivial() const { |
| 88 | return fProcessors == nullptr; |
| 89 | } |
| 90 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 91 | bool usesLocalCoords() const { |
| 92 | SkASSERT(fDidAnalysis); |
| 93 | return fUsesLocalCoords; |
| 94 | } |
| 95 | |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 96 | bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
Brian Salomon | 28207df | 2017-06-05 12:25:13 -0400 | [diff] [blame] | 97 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 98 | void visitProxies(const GrVisitProxyFunc& func) const { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 99 | if (fProcessors) { |
| 100 | fProcessors->visitProxies(func); |
| 101 | } |
| 102 | } |
| 103 | |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 104 | #if GR_TEST_UTILS |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 105 | SkString dumpInfo() const; |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 106 | #endif |
Michael Ludwig | c647324 | 2018-11-01 11:08:35 -0400 | [diff] [blame] | 107 | GrAAType aaType() const { return static_cast<GrAAType>(fAAType); } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 108 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 109 | void setAAType(GrAAType aaType) { |
Robert Phillips | 438d986 | 2019-11-14 12:46:05 -0500 | [diff] [blame] | 110 | fAAType = static_cast<unsigned>(aaType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 113 | static const GrPipeline* CreatePipeline( |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 114 | const GrCaps*, |
| 115 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 116 | GrSwizzle writeViewSwizzle, |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 117 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 118 | const GrDstProxyView&, |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 119 | GrProcessorSet&&, |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 120 | GrPipeline::InputFlags pipelineFlags); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 121 | static const GrPipeline* CreatePipeline( |
| 122 | GrOpFlushState*, |
| 123 | GrProcessorSet&&, |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 124 | GrPipeline::InputFlags pipelineFlags); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 125 | |
| 126 | const GrPipeline* createPipeline(GrOpFlushState* flushState); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 127 | |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 128 | const GrPipeline* createPipeline(const GrCaps*, |
| 129 | SkArenaAlloc*, |
| 130 | GrSwizzle writeViewSwizzle, |
| 131 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 132 | const GrDstProxyView&); |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 133 | |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame^] | 134 | static GrProgramInfo* CreateProgramInfo(const GrCaps*, |
| 135 | SkArenaAlloc*, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 136 | const GrPipeline*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 137 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame^] | 138 | bool usesMSAASurface, |
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, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 142 | GrLoadOp colorLoadOp, |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 143 | const GrUserStencilSettings* |
| 144 | = &GrUserStencilSettings::kUnused); |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 145 | |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 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 |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 149 | static GrProgramInfo* CreateProgramInfo(const GrCaps*, |
| 150 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 151 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame^] | 152 | bool usesMSAASurface, |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 153 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 154 | const GrDstProxyView&, |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 155 | GrGeometryProcessor*, |
| 156 | GrProcessorSet&&, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 157 | GrPrimitiveType, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 158 | GrXferBarrierFlags renderPassXferBarriers, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 159 | GrLoadOp colorLoadOp, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 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*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 167 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame^] | 168 | bool usesMSAASurface, |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 169 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 170 | const GrDstProxyView&, |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 171 | GrGeometryProcessor*, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 172 | GrPrimitiveType, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 173 | GrXferBarrierFlags renderPassXferBarriers, |
| 174 | GrLoadOp colorLoadOp); |
Robert Phillips | b58098f | 2020-03-02 16:25:29 -0500 | [diff] [blame] | 175 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 176 | GrProcessorSet detachProcessorSet() { |
| 177 | return fProcessors ? std::move(*fProcessors) : GrProcessorSet::MakeEmptySet(); |
| 178 | } |
| 179 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 180 | GrPipeline::InputFlags pipelineFlags() const { return fPipelineFlags; } |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 181 | |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 182 | protected: |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 183 | GrProcessorSet::Analysis finalizeProcessors(const GrCaps& caps, const GrAppliedClip*, |
| 184 | const GrUserStencilSettings*, GrClampType, |
| 185 | GrProcessorAnalysisCoverage geometryCoverage, |
| 186 | GrProcessorAnalysisColor* geometryColor); |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 187 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 188 | GrProcessorSet* fProcessors; |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 189 | GrPipeline::InputFlags fPipelineFlags; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 190 | unsigned fAAType : 2; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 191 | unsigned fUsesLocalCoords : 1; |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 192 | unsigned fCompatibleWithCoverageAsAlpha : 1; |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 193 | SkDEBUGCODE(unsigned fMadePipeline : 1;) |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 194 | SkDEBUGCODE(unsigned fDidAnalysis : 1;) |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 195 | }; |
| 196 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 197 | template<typename Op, typename... Args> |
| 198 | GrOp::Owner GrOp::MakeWithProcessorSet( |
| 199 | GrRecordingContext* context, const SkPMColor4f& color, |
| 200 | GrPaint&& paint, Args&&... args) { |
Herb Derby | c9a24c9 | 2020-12-01 16:59:40 -0500 | [diff] [blame] | 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)...)}; |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 205 | } |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 206 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 207 | template <typename Op, typename... OpArgs> |
| 208 | GrOp::Owner GrSimpleMeshDrawOpHelper::FactoryHelper(GrRecordingContext* context, |
| 209 | GrPaint&& paint, |
| 210 | OpArgs&& ... opArgs) { |
| 211 | auto color = paint.getColor4f(); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 212 | if (paint.isTrivial()) { |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 213 | return GrOp::Make<Op>(context, nullptr, color, std::forward<OpArgs>(opArgs)...); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 214 | } else { |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 215 | return GrOp::MakeWithProcessorSet<Op>( |
| 216 | context, color, std::move(paint), std::forward<OpArgs>(opArgs)...); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 220 | GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::InputFlags) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 221 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 222 | #endif |