xref: /aosp_15_r20/external/skia/src/gpu/ganesh/tessellate/StrokeTessellator.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 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 
8 #ifndef StrokeTessellator_DEFINED
9 #define StrokeTessellator_DEFINED
10 
11 #include "include/core/SkPath.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkStrokeRec.h"
14 #include "include/private/SkColorData.h"
15 #include "src/gpu/ganesh/GrGpuBuffer.h"
16 #include "src/gpu/ganesh/GrVertexChunkArray.h"
17 #include "src/gpu/tessellate/Tessellation.h"
18 
19 class GrMeshDrawTarget;
20 class GrOpFlushState;
21 class SkMatrix;
22 
23 namespace skgpu::ganesh {
24 
25 // Prepares GPU data for, and then draws a stroke's tessellated geometry. Renders strokes as
26 // fixed-count triangle strip instances. Any extra triangles not needed by the instance are emitted
27 // as degenerate triangles.
28 class StrokeTessellator {
29 public:
30     using PatchAttribs = tess::PatchAttribs;
31 
32     struct PathStrokeList {
PathStrokeListPathStrokeList33         PathStrokeList(const SkPath& path, const SkStrokeRec& stroke, const SkPMColor4f& color)
34                 : fPath(path), fStroke(stroke), fColor(color) {}
35         SkPath fPath;
36         SkStrokeRec fStroke;
37         SkPMColor4f fColor;
38         PathStrokeList* fNext = nullptr;
39     };
40 
StrokeTessellator(PatchAttribs attribs)41     StrokeTessellator(PatchAttribs attribs) : fAttribs(attribs | PatchAttribs::kJoinControlPoint) {}
42 
43     // Called before draw(). Prepares GPU buffers containing the geometry to tessellate.
44     //
45     // Returns the fixed number of edges the tessellator will draw per patch.
46     void prepare(GrMeshDrawTarget*,
47                  const SkMatrix& shaderMatrix,
48                  PathStrokeList*,
49                  int totalCombinedStrokeVerbCnt);
50 
51     // Issues draw calls for the tessellated stroke. The caller is responsible for creating and
52     // binding a pipeline that uses this class's shader() before calling draw().
53     void draw(GrOpFlushState*) const;
54 
55 protected:
56     const PatchAttribs fAttribs;
57 
58     GrVertexChunkArray fVertexChunkArray;
59 
60     int fVertexCount = 0;
61 
62     // Only used if sk_VertexID is not supported.
63     sk_sp<const GrGpuBuffer> fVertexBufferIfNoIDSupport;
64 };
65 
66 }  // namespace skgpu::ganesh
67 
68 #endif  // StrokeTessellator_DEFINED
69