Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC. |
| 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 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 8 | #ifndef GrStrokeTessellateOp_DEFINED |
| 9 | #define GrStrokeTessellateOp_DEFINED |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 10 | |
| 11 | #include "include/core/SkStrokeRec.h" |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 12 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 13 | #include "src/gpu/tessellate/GrPathShader.h" |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 14 | #include "src/gpu/tessellate/GrStrokeTessellateShader.h" |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 15 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 16 | class GrRecordingContext; |
| 17 | |
| 18 | // Prepares GPU data for, and then draws a stroke's tessellated geometry. |
| 19 | class GrStrokeTessellator { |
| 20 | public: |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 21 | using ShaderFlags = GrStrokeTessellateShader::ShaderFlags; |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 22 | |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame^] | 23 | struct PathStrokeList { |
| 24 | PathStrokeList(const SkPath& path, const SkStrokeRec& stroke, const SkPMColor4f& color) |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 25 | : fPath(path), fStroke(stroke), fColor(color) {} |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 26 | SkPath fPath; |
| 27 | SkStrokeRec fStroke; |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 28 | SkPMColor4f fColor; |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame^] | 29 | PathStrokeList* fNext = nullptr; |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame^] | 32 | GrStrokeTessellator(ShaderFlags shaderFlags, PathStrokeList* pathStrokeList) |
| 33 | : fShaderFlags(shaderFlags), fPathStrokeList(pathStrokeList) {} |
Chris Dalton | 82094cd | 2021-02-19 01:11:27 -0700 | [diff] [blame] | 34 | |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 35 | // Called before draw(). Prepares GPU buffers containing the geometry to tessellate. |
Chris Dalton | 82094cd | 2021-02-19 01:11:27 -0700 | [diff] [blame] | 36 | virtual void prepare(GrMeshDrawOp::Target*, const SkMatrix&) = 0; |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 37 | |
| 38 | // Issues draw calls for the tessellated stroke. The caller is responsible for binding its |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 39 | // desired pipeline ahead of time. |
| 40 | virtual void draw(GrOpFlushState*) const = 0; |
| 41 | |
| 42 | virtual ~GrStrokeTessellator() {} |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 43 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 44 | protected: |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 45 | const ShaderFlags fShaderFlags; |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame^] | 46 | PathStrokeList* fPathStrokeList; |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // Renders strokes by linearizing them into sorted "parametric" and "radial" edges. See |
| 50 | // GrStrokeTessellateShader. |
| 51 | class GrStrokeTessellateOp : public GrDrawOp { |
| 52 | public: |
| 53 | GrStrokeTessellateOp(GrAAType, const SkMatrix&, const SkPath&, const SkStrokeRec&, GrPaint&&); |
| 54 | |
| 55 | private: |
| 56 | using ShaderFlags = GrStrokeTessellateShader::ShaderFlags; |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame^] | 57 | using PathStrokeList = GrStrokeTessellator::PathStrokeList; |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 58 | DEFINE_OP_CLASS_ID |
| 59 | |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame^] | 60 | SkStrokeRec& headStroke() { return fPathStrokeList.fStroke; } |
| 61 | SkPMColor4f& headColor() { return fPathStrokeList.fColor; } |
Chris Dalton | 82094cd | 2021-02-19 01:11:27 -0700 | [diff] [blame] | 62 | GrStrokeTessellateOp* nextInChain() const { |
| 63 | return static_cast<GrStrokeTessellateOp*>(this->GrDrawOp::nextInChain()); |
| 64 | } |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 65 | |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 66 | // Returns whether it is a good tradeoff to use the dynamic states flagged in the given |
| 67 | // bitfield. Dynamic states improve batching, but if they aren't already enabled, they come at |
| 68 | // the cost of having to write out more data with each patch or instance. |
| 69 | bool shouldUseDynamicStates(ShaderFlags neededDynamicStates) const { |
| 70 | // Use the dynamic states if either (1) they are all already enabled anyway, or (2) we don't |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 71 | // have many verbs. |
| 72 | constexpr static int kMaxVerbsToEnableDynamicState = 50; |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 73 | bool anyStateDisabled = (bool)(~fShaderFlags & neededDynamicStates); |
| 74 | bool allStatesEnabled = !anyStateDisabled; |
| 75 | return allStatesEnabled || (fTotalCombinedVerbCnt <= kMaxVerbsToEnableDynamicState); |
| 76 | } |
| 77 | |
| 78 | bool canUseHardwareTessellation(const GrCaps& caps) { |
| 79 | SkASSERT(!fStencilProgram && !fFillProgram); // Ensure we haven't std::moved fProcessors. |
| 80 | // Our back door for HW tessellation shaders isn't currently capable of passing varyings to |
| 81 | // the fragment shader, so if the processors have varyings we need to use indirect draws. |
| 82 | return caps.shaderCaps()->tessellationSupport() && !fProcessors.usesVaryingCoords(); |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 85 | const char* name() const override { return "GrStrokeTessellateOp"; } |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 86 | void visitProxies(const VisitProxyFunc& fn) const override; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 87 | FixedFunctionFlags fixedFunctionFlags() const override; |
| 88 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 89 | bool hasMixedSampledCoverage, GrClampType) override; |
| 90 | CombineResult onCombineIfPossible(GrOp*, SkArenaAlloc*, const GrCaps&) override; |
| 91 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 92 | // Creates the tessellator and the stencil/fill program(s) we will use with it. |
| 93 | void prePrepareTessellator(GrPathShader::ProgramArgs&&, GrAppliedClip&&); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 94 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 95 | void onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView&, GrAppliedClip*, |
| 96 | const GrXferProcessor::DstProxyView&, GrXferBarrierFlags, |
| 97 | GrLoadOp colorLoadOp) override; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 98 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 99 | void onPrepare(GrOpFlushState*) override; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 100 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 101 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
Chris Dalton | 06b52ad | 2020-12-15 10:01:35 -0700 | [diff] [blame] | 102 | |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 103 | const GrAAType fAAType; |
| 104 | const SkMatrix fViewMatrix; |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 105 | ShaderFlags fShaderFlags = ShaderFlags::kNone; |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame^] | 106 | PathStrokeList fPathStrokeList; |
| 107 | PathStrokeList** fPathStrokeTail = &fPathStrokeList.fNext; |
Chris Dalton | 7b80726 | 2020-12-10 10:22:50 -0700 | [diff] [blame] | 108 | int fTotalCombinedVerbCnt = 0; |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 109 | GrProcessorSet fProcessors; |
| 110 | bool fNeedsStencil = false; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 111 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 112 | GrStrokeTessellator* fTessellator = nullptr; |
| 113 | const GrProgramInfo* fStencilProgram = nullptr; // Only used if the stroke has transparency. |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 114 | const GrProgramInfo* fFillProgram = nullptr; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | #endif |