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 { |
| 27 | PathStroke(const SkPath& path, const SkStrokeRec& stroke) : fPath(path), fStroke(stroke) {} |
| 28 | SkPath fPath; |
| 29 | SkStrokeRec fStroke; |
| 30 | }; |
| 31 | |
| 32 | // Called before draw(). Prepares GPU buffers containing the geometry to tessellate. |
| 33 | virtual void prepare(GrMeshDrawOp::Target*, const SkMatrix&, const GrSTArenaList<PathStroke>&, |
| 34 | int totalCombinedVerbCnt) = 0; |
| 35 | |
| 36 | // 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] | 37 | // desired pipeline ahead of time. |
| 38 | virtual void draw(GrOpFlushState*) const = 0; |
| 39 | |
| 40 | virtual ~GrStrokeTessellator() {} |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 41 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 42 | protected: |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame^] | 43 | const ShaderFlags fShaderFlags; |
| 44 | }; |
| 45 | |
| 46 | // Renders strokes by linearizing them into sorted "parametric" and "radial" edges. See |
| 47 | // GrStrokeTessellateShader. |
| 48 | class GrStrokeTessellateOp : public GrDrawOp { |
| 49 | public: |
| 50 | GrStrokeTessellateOp(GrAAType, const SkMatrix&, const SkPath&, const SkStrokeRec&, GrPaint&&); |
| 51 | |
| 52 | private: |
| 53 | using ShaderFlags = GrStrokeTessellateShader::ShaderFlags; |
| 54 | using PathStroke = GrStrokeTessellator::PathStroke; |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 55 | DEFINE_OP_CLASS_ID |
| 56 | |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame^] | 57 | SkStrokeRec& headStroke() { return fPathStrokeList.head().fStroke; } |
| 58 | |
| 59 | // Returns whether it is a good tradeoff to use the given dynamic state. Dynamic state improves |
| 60 | // batching, but if it isn't already enabled, it comes at the cost of having to write out more |
| 61 | // data with each patch or instance. |
| 62 | bool shouldUseDynamicState(ShaderFlags dynamicState) const { |
| 63 | // Use the dynamic state if either (1) the state is already enabled anyway, or (2) we don't |
| 64 | // have many verbs. |
| 65 | constexpr static int kMaxVerbsToEnableDynamicState = 50; |
| 66 | return (fShaderFlags & dynamicState) || |
| 67 | (fTotalCombinedVerbCnt <= kMaxVerbsToEnableDynamicState); |
| 68 | } |
| 69 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 70 | const char* name() const override { return "GrStrokeTessellateOp"; } |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 71 | void visitProxies(const VisitProxyFunc& fn) const override; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 72 | FixedFunctionFlags fixedFunctionFlags() const override; |
| 73 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 74 | bool hasMixedSampledCoverage, GrClampType) override; |
| 75 | CombineResult onCombineIfPossible(GrOp*, SkArenaAlloc*, const GrCaps&) override; |
| 76 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 77 | // Creates the tessellator and the stencil/fill program(s) we will use with it. |
| 78 | void prePrepareTessellator(GrPathShader::ProgramArgs&&, GrAppliedClip&&); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 79 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 80 | void onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView&, GrAppliedClip*, |
| 81 | const GrXferProcessor::DstProxyView&, GrXferBarrierFlags, |
| 82 | GrLoadOp colorLoadOp) override; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 83 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 84 | void onPrepare(GrOpFlushState*) override; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 85 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 86 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
Chris Dalton | 06b52ad | 2020-12-15 10:01:35 -0700 | [diff] [blame] | 87 | |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 88 | const GrAAType fAAType; |
| 89 | const SkMatrix fViewMatrix; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 90 | SkPMColor4f fColor; |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 91 | bool fNeedsStencil = false; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 92 | GrProcessorSet fProcessors; |
| 93 | |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame^] | 94 | ShaderFlags fShaderFlags = ShaderFlags::kNone; |
| 95 | GrSTArenaList<PathStroke> fPathStrokeList; |
Chris Dalton | 7b80726 | 2020-12-10 10:22:50 -0700 | [diff] [blame] | 96 | int fTotalCombinedVerbCnt = 0; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 97 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 98 | GrStrokeTessellator* fTessellator = nullptr; |
| 99 | const GrProgramInfo* fStencilProgram = nullptr; // Only used if the stroke has transparency. |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 100 | const GrProgramInfo* fFillProgram = nullptr; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | #endif |