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