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 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 11 | #include "GrMeshDrawOp.h" |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 12 | #include "GrOpFlushState.h" |
| 13 | #include "GrPipeline.h" |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 14 | |
| 15 | struct SkRect; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 16 | |
| 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 | */ |
| 23 | class GrSimpleMeshDrawOpHelper { |
| 24 | public: |
| 25 | struct MakeArgs; |
| 26 | |
| 27 | /** |
| 28 | * 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] | 29 | * 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 Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 31 | * 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 Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 37 | enum class Flags : uint32_t { |
| 38 | kNone = 0x0, |
| 39 | kSnapVerticesToPixelCenters = 0x1, |
| 40 | }; |
| 41 | GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags); |
| 42 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 43 | GrSimpleMeshDrawOpHelper(const MakeArgs&, GrAAType, Flags = Flags::kNone); |
| 44 | ~GrSimpleMeshDrawOpHelper(); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 45 | |
| 46 | GrSimpleMeshDrawOpHelper() = delete; |
| 47 | GrSimpleMeshDrawOpHelper(const GrSimpleMeshDrawOpHelper&) = delete; |
| 48 | GrSimpleMeshDrawOpHelper& operator=(const GrSimpleMeshDrawOpHelper&) = delete; |
| 49 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 50 | GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 51 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 52 | bool isCompatible(const GrSimpleMeshDrawOpHelper& that, const GrCaps&, const SkRect& thisBounds, |
| 53 | const SkRect& thatBounds) const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 54 | |
Brian Salomon | 0088f94 | 2017-07-12 11:51:27 -0400 | [diff] [blame] | 55 | /** |
| 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 Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 74 | GrDrawOp::RequiresDstTexture xpRequiresDstTexture(const GrCaps&, const GrAppliedClip*, |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 75 | GrProcessorAnalysisCoverage geometryCoverage, |
Brian Salomon | 0088f94 | 2017-07-12 11:51:27 -0400 | [diff] [blame] | 76 | GrColor* geometryColor); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 77 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 78 | bool usesLocalCoords() const { |
| 79 | SkASSERT(fDidAnalysis); |
| 80 | return fUsesLocalCoords; |
| 81 | } |
| 82 | |
Brian Salomon | 28207df | 2017-06-05 12:25:13 -0400 | [diff] [blame] | 83 | bool compatibleWithAlphaAsCoverage() const { return fCompatibleWithAlphaAsCoveage; } |
| 84 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame^] | 85 | GrPipeline* makePipeline(GrMeshDrawOp::Target* target) { |
| 86 | return this->internalMakePipeline(target, this->pipelineInitArgs(target)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 87 | } |
| 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 Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 99 | SkString dumpInfo() const; |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 100 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 101 | protected: |
| 102 | GrAAType aaType() const { return static_cast<GrAAType>(fAAType); } |
| 103 | uint32_t pipelineFlags() const { return fPipelineFlags; } |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 104 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 105 | GrPipeline::InitArgs pipelineInitArgs(GrMeshDrawOp::Target* target) const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 106 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame^] | 107 | GrPipeline* internalMakePipeline(GrMeshDrawOp::Target*, const GrPipeline::InitArgs&); |
| 108 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 109 | private: |
| 110 | GrProcessorSet* fProcessors; |
| 111 | unsigned fPipelineFlags : 8; |
| 112 | unsigned fAAType : 2; |
Brian Salomon | a4677b5 | 2017-05-04 12:39:56 -0400 | [diff] [blame] | 113 | unsigned fRequiresDstTexture : 1; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 114 | unsigned fUsesLocalCoords : 1; |
Brian Salomon | 28207df | 2017-06-05 12:25:13 -0400 | [diff] [blame] | 115 | unsigned fCompatibleWithAlphaAsCoveage : 1; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 116 | SkDEBUGCODE(unsigned fDidAnalysis : 1;) |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 117 | }; |
| 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 | */ |
| 124 | class GrSimpleMeshDrawOpHelperWithStencil : private GrSimpleMeshDrawOpHelper { |
| 125 | public: |
| 126 | using MakeArgs = GrSimpleMeshDrawOpHelper::MakeArgs; |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 127 | using Flags = GrSimpleMeshDrawOpHelper::Flags; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 128 | |
| 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 Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 136 | GrSimpleMeshDrawOpHelperWithStencil(const MakeArgs&, GrAAType, const GrUserStencilSettings*, |
| 137 | Flags = Flags::kNone); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 138 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 139 | GrDrawOp::FixedFunctionFlags fixedFunctionFlags() const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 140 | |
| 141 | using GrSimpleMeshDrawOpHelper::xpRequiresDstTexture; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 142 | using GrSimpleMeshDrawOpHelper::usesLocalCoords; |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 143 | using GrSimpleMeshDrawOpHelper::compatibleWithAlphaAsCoverage; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 144 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 145 | bool isCompatible(const GrSimpleMeshDrawOpHelperWithStencil& that, const GrCaps&, |
| 146 | const SkRect& thisBounds, const SkRect& thatBounds) const; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 147 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame^] | 148 | const GrPipeline* makePipeline(GrMeshDrawOp::Target*); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 149 | |
Brian Salomon | b4d6106 | 2017-07-12 11:24:41 -0400 | [diff] [blame] | 150 | SkString dumpInfo() const; |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 151 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 152 | private: |
| 153 | const GrUserStencilSettings* fStencilSettings; |
| 154 | typedef GrSimpleMeshDrawOpHelper INHERITED; |
| 155 | }; |
| 156 | |
| 157 | template <typename Op, typename... OpArgs> |
| 158 | std::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 Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 175 | GR_MAKE_BITFIELD_CLASS_OPS(GrSimpleMeshDrawOpHelper::Flags) |
| 176 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 177 | #endif |