1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef tessellate_StrokeFixedCountTessellator_DEFINED
9cb93a386Sopenharmony_ci#define tessellate_StrokeFixedCountTessellator_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "src/gpu/GrGpuBuffer.h"
12cb93a386Sopenharmony_ci#include "src/gpu/GrVertexChunkArray.h"
13cb93a386Sopenharmony_ci#include "src/gpu/tessellate/StrokeTessellator.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_cinamespace skgpu {
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci// Renders strokes as fixed-count triangle strip instances. Any extra triangles not needed by the
18cb93a386Sopenharmony_ci// instance are emitted as degenerate triangles.
19cb93a386Sopenharmony_ciclass StrokeFixedCountTessellator : public StrokeTessellator {
20cb93a386Sopenharmony_cipublic:
21cb93a386Sopenharmony_ci    constexpr static float kMaxParametricSegments_pow4 = 32*32*32*32;  // 32^4
22cb93a386Sopenharmony_ci    constexpr static int8_t kMaxParametricSegments_log2 = 5;  // log2(32)
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    StrokeFixedCountTessellator(PatchAttribs attribs) : StrokeTessellator(attribs) {}
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci    int prepare(GrMeshDrawTarget*,
27cb93a386Sopenharmony_ci                const SkMatrix& shaderMatrix,
28cb93a386Sopenharmony_ci                std::array<float,2> matrixMinMaxScales,
29cb93a386Sopenharmony_ci                PathStrokeList*,
30cb93a386Sopenharmony_ci                int totalCombinedVerbCnt) override;
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci#if SK_GPU_V1
33cb93a386Sopenharmony_ci    void draw(GrOpFlushState*) const override;
34cb93a386Sopenharmony_ci#endif
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    // Initializes the fallback vertex buffer that should be bound when sk_VertexID is not
37cb93a386Sopenharmony_ci    // supported. Each vertex is a single float and each edge is composed of two vertices, so the
38cb93a386Sopenharmony_ci    // desired edge count in the buffer is presumed to be "bufferSize / (sizeof(float) * 2)". The
39cb93a386Sopenharmony_ci    // caller cannot draw more vertices than edgeCount * 2.
40cb93a386Sopenharmony_ci    static void InitializeVertexIDFallbackBuffer(VertexWriter vertexWriter, size_t bufferSize);
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    // Returns the fixed number of edges that are always emitted with the given join type. If the
43cb93a386Sopenharmony_ci    // join is round, the caller needs to account for the additional radial edges on their own.
44cb93a386Sopenharmony_ci    // Specifically, each join always emits:
45cb93a386Sopenharmony_ci    //
46cb93a386Sopenharmony_ci    //   * Two colocated edges at the beginning (a full-width edge to seam with the preceding stroke
47cb93a386Sopenharmony_ci    //     and a half-width edge to begin the join).
48cb93a386Sopenharmony_ci    //
49cb93a386Sopenharmony_ci    //   * An extra edge in the middle for miter joins, or else a variable number of radial edges
50cb93a386Sopenharmony_ci    //     for round joins (the caller is responsible for counting radial edges from round joins).
51cb93a386Sopenharmony_ci    //
52cb93a386Sopenharmony_ci    //   * A half-width edge at the end of the join that will be colocated with the first
53cb93a386Sopenharmony_ci    //     (full-width) edge of the stroke.
54cb93a386Sopenharmony_ci    //
55cb93a386Sopenharmony_ci    constexpr static int NumFixedEdgesInJoin(SkPaint::Join joinType) {
56cb93a386Sopenharmony_ci        switch (joinType) {
57cb93a386Sopenharmony_ci            case SkPaint::kMiter_Join:
58cb93a386Sopenharmony_ci                return 4;
59cb93a386Sopenharmony_ci            case SkPaint::kRound_Join:
60cb93a386Sopenharmony_ci                // The caller is responsible for counting the variable number of middle, radial
61cb93a386Sopenharmony_ci                // segments on round joins.
62cb93a386Sopenharmony_ci                [[fallthrough]];
63cb93a386Sopenharmony_ci            case SkPaint::kBevel_Join:
64cb93a386Sopenharmony_ci                return 3;
65cb93a386Sopenharmony_ci        }
66cb93a386Sopenharmony_ci        SkUNREACHABLE;
67cb93a386Sopenharmony_ci    }
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ciprivate:
70cb93a386Sopenharmony_ci    GrVertexChunkArray fInstanceChunks;
71cb93a386Sopenharmony_ci    int fFixedEdgeCount = 0;
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    // Only used if sk_VertexID is not supported.
74cb93a386Sopenharmony_ci    sk_sp<const GrGpuBuffer> fVertexBufferIfNoIDSupport;
75cb93a386Sopenharmony_ci};
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci}  // namespace skgpu
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci#endif  // tessellate_StrokeFixedCountTessellator_DEFINED
80