xref: /aosp_15_r20/external/skia/src/gpu/ganesh/tessellate/GrStrokeTessellationShader.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef GrStrokeTessellationShader_DEFINED
8 #define GrStrokeTessellationShader_DEFINED
9 
10 #include "include/core/SkStrokeRec.h"
11 #include "include/private/SkColorData.h"
12 #include "include/private/base/SkTArray.h"
13 #include "src/gpu/ganesh/tessellate/GrTessellationShader.h"
14 #include "src/gpu/tessellate/Tessellation.h"
15 
16 #include <memory>
17 
18 class SkMatrix;
19 struct GrShaderCaps;
20 
21 namespace skgpu {
22 class KeyBuilder;
23 }
24 
25 // Tessellates a batch of stroke patches directly to the canvas. Tessellated stroking works by
26 // creating stroke-width, orthogonal edges at set locations along the curve and then connecting them
27 // with a quad strip. These orthogonal edges come from two different sets: "parametric edges" and
28 // "radial edges". Parametric edges are spaced evenly in the parametric sense, and radial edges
29 // divide the curve's _rotation_ into even steps. The tessellation shader evaluates both sets of
30 // edges and sorts them into a single quad strip. With this combined set of edges we can stroke any
31 // curve, regardless of curvature.
32 class GrStrokeTessellationShader : public GrTessellationShader {
33     using PatchAttribs = skgpu::tess::PatchAttribs;
34 
35 public:
36 
37     // 'viewMatrix' is applied to the geometry post tessellation. It cannot have perspective.
38     GrStrokeTessellationShader(const GrShaderCaps&, PatchAttribs, const SkMatrix& viewMatrix,
39                                const SkStrokeRec&, SkPMColor4f);
40 
attribs()41     PatchAttribs attribs() const { return fPatchAttribs; }
hasDynamicStroke()42     bool hasDynamicStroke() const { return fPatchAttribs & PatchAttribs::kStrokeParams; }
hasDynamicColor()43     bool hasDynamicColor() const { return fPatchAttribs & PatchAttribs::kColor; }
hasExplicitCurveType()44     bool hasExplicitCurveType() const { return fPatchAttribs & PatchAttribs::kExplicitCurveType; }
stroke()45     const SkStrokeRec& stroke() const { return fStroke;}
46 
47 private:
name()48     const char* name() const override { return "GrStrokeTessellationShader"; }
49     void addToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override;
50     std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final;
51 
52     const PatchAttribs fPatchAttribs;
53     const SkStrokeRec fStroke;
54 
55     constexpr static int kMaxAttribCount = 6;
56     skia_private::STArray<kMaxAttribCount, Attribute> fAttribs;
57 
58     class Impl;
59 };
60 
61 #endif
62