1 /*
2 * Copyright 2021 Google Inc.
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 tessellate_Tessellation_DEFINED
9 #define tessellate_Tessellation_DEFINED
10
11 #include "include/core/SkStrokeRec.h"
12 #include "include/gpu/GrTypes.h"
13 #include "include/private/SkVx.h"
14
15 class SkMatrix;
16 class SkPath;
17 struct SkRect;
18
19 namespace skgpu {
20
21 struct VertexWriter;
22
23 // Use familiar type names from SkSL.
24 template<int N> using vec = skvx::Vec<N, float>;
25 using float2 = vec<2>;
26 using float4 = vec<4>;
27
28 template<int N> using ivec = skvx::Vec<N, int32_t>;
29 using int2 = ivec<2>;
30 using int4 = ivec<4>;
31
32 template<int N> using uvec = skvx::Vec<N, uint32_t>;
33 using uint2 = uvec<2>;
34 using uint4 = uvec<4>;
35
36 #define AI SK_MAYBE_UNUSED SK_ALWAYS_INLINE
37
dot(float2 a,float2 b)38 AI float dot(float2 a, float2 b) {
39 float2 ab = a*b;
40 return ab.x() + ab.y();
41 }
42
cross(float2 a,float2 b)43 AI float cross(float2 a, float2 b) {
44 float2 x = a * b.yx();
45 return x[0] - x[1];
46 }
47
48 // This does not return b when t==1, but it otherwise seems to get better precision than
49 // "a*(1 - t) + b*t" for things like chopping cubics on exact cusp points.
50 // The responsibility falls on the caller to check that t != 1 before calling.
51 template<int N>
mix(vec<N> a,vec<N> b,vec<N> T)52 AI vec<N> mix(vec<N> a, vec<N> b, vec<N> T) {
53 SkASSERT(all((0 <= T) & (T < 1)));
54 return (b - a)*T + a;
55 }
56
57 template<int N>
mix(vec<N> a,vec<N> b,float T)58 AI vec<N> mix(vec<N> a, vec<N> b, float T) {
59 return mix(a, b, vec<N>(T));
60 }
61
pow2(float x)62 AI constexpr float pow2(float x) { return x*x; }
pow4(float x)63 AI constexpr float pow4(float x) { return pow2(x*x); }
64
65 #undef AI
66
67 // Don't allow linearized segments to be off by more than 1/4th of a pixel from the true curve.
68 SK_MAYBE_UNUSED constexpr static float kTessellationPrecision = 4;
69
70 // Optional attribs that are included in tessellation patches, following the control points and in
71 // the same order as they appear here.
72 enum class PatchAttribs {
73 // Attribs.
74 kNone = 0,
75 kFanPoint = 1 << 0, // [float2] Used by wedges. This is the center point the wedges fan around.
76 kStrokeParams = 1 << 1, // [float2] Used when strokes have different widths or join types.
77 kColor = 1 << 2, // [ubyte4 or float4] Used when patches have different colors.
78 kExplicitCurveType = 1 << 3, // [float] Used when GPU can't infer curve type based on infinity.
79
80 // Extra flags.
81 kWideColorIfEnabled = 1 << 4, // If kColor is set, specifies it to be float4 wide color.
82 };
83
GR_MAKE_BITFIELD_CLASS_OPS(PatchAttribs)84 GR_MAKE_BITFIELD_CLASS_OPS(PatchAttribs)
85
86 // We encode all of a join's information in a single float value:
87 //
88 // Negative => Round Join
89 // Zero => Bevel Join
90 // Positive => Miter join, and the value is also the miter limit
91 //
92 static float GetJoinType(const SkStrokeRec& stroke) {
93 switch (stroke.getJoin()) {
94 case SkPaint::kRound_Join: return -1;
95 case SkPaint::kBevel_Join: return 0;
96 case SkPaint::kMiter_Join: SkASSERT(stroke.getMiter() >= 0); return stroke.getMiter();
97 }
98 SkUNREACHABLE;
99 }
100
101 // This float2 gets written out with each patch/instance if PatchAttribs::kStrokeParams is enabled.
102 struct StrokeParams {
StrokesHaveEqualParamsStrokeParams103 static bool StrokesHaveEqualParams(const SkStrokeRec& a, const SkStrokeRec& b) {
104 return a.getWidth() == b.getWidth() && a.getJoin() == b.getJoin() &&
105 (a.getJoin() != SkPaint::kMiter_Join || a.getMiter() == b.getMiter());
106 }
setStrokeParams107 void set(const SkStrokeRec& stroke) {
108 fRadius = stroke.getWidth() * .5f;
109 fJoinType = GetJoinType(stroke);
110 }
111 float fRadius;
112 float fJoinType; // See GetJoinType().
113 };
114
115 // Returns the packed size in bytes of the attribs portion of tessellation patches (or instances) in
116 // GPU buffers.
PatchAttribsStride(PatchAttribs attribs)117 constexpr size_t PatchAttribsStride(PatchAttribs attribs) {
118 return (attribs & PatchAttribs::kFanPoint ? sizeof(float) * 2 : 0) +
119 (attribs & PatchAttribs::kStrokeParams ? sizeof(float) * 2 : 0) +
120 (attribs & PatchAttribs::kColor
121 ? (attribs & PatchAttribs::kWideColorIfEnabled ? sizeof(float)
122 : sizeof(uint8_t)) * 4 : 0) +
123 (attribs & PatchAttribs::kExplicitCurveType ? sizeof(float) : 0);
124 }
125
126 // Don't tessellate paths that might have an individual curve that requires more than 1024 segments.
127 // (See wangs_formula::worst_case_cubic). If this is the case, call "PreChopPathCurves" first.
128 constexpr static float kMaxTessellationSegmentsPerCurve SK_MAYBE_UNUSED = 1024;
129
130 // Returns a new path, equivalent to 'path' within the given viewport, whose verbs can all be drawn
131 // with 'maxSegments' tessellation segments or fewer. Curves and chops that fall completely outside
132 // the viewport are flattened into lines.
133 SkPath PreChopPathCurves(const SkPath&, const SkMatrix&, const SkRect& viewport);
134
135 } // namespace skgpu
136
137 #endif // tessellate_Tessellation_DEFINED
138