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 skgpu_tessellate_Tessellation_DEFINED
9 #define skgpu_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 kJoinControlPoint = 1 << 0, // [float2] Used by strokes. This defines tangent direction.
76 kFanPoint = 1 << 1, // [float2] Used by wedges. This is the center point the wedges fan around.
77 kStrokeParams = 1 << 2, // [float2] Used when strokes have different widths or join types.
78 kColor = 1 << 3, // [ubyte4 or float4] Used when patches have different colors.
79 kExplicitCurveType = 1 << 4, // [float] Used when GPU can't infer curve type based on infinity.
80
81 // Extra flags.
82 kWideColorIfEnabled = 1 << 5, // If kColor is set, specifies it to be float4 wide color.
83 };
84
GR_MAKE_BITFIELD_CLASS_OPS(PatchAttribs)85 GR_MAKE_BITFIELD_CLASS_OPS(PatchAttribs)
86
87 // We encode all of a join's information in a single float value:
88 //
89 // Negative => Round Join
90 // Zero => Bevel Join
91 // Positive => Miter join, and the value is also the miter limit
92 //
93 static float GetJoinType(const SkStrokeRec& stroke) {
94 switch (stroke.getJoin()) {
95 case SkPaint::kRound_Join: return -1;
96 case SkPaint::kBevel_Join: return 0;
97 case SkPaint::kMiter_Join: SkASSERT(stroke.getMiter() >= 0); return stroke.getMiter();
98 }
99 SkUNREACHABLE;
100 }
101
102 // This float2 gets written out with each patch/instance if PatchAttribs::kStrokeParams is enabled.
103 struct StrokeParams {
104 StrokeParams() = default;
StrokeParamsStrokeParams105 StrokeParams(const SkStrokeRec& stroke) {
106 this->set(stroke);
107 }
setStrokeParams108 void set(const SkStrokeRec& stroke) {
109 fRadius = stroke.getWidth() * .5f;
110 fJoinType = GetJoinType(stroke);
111 }
StrokesHaveEqualParamsStrokeParams112 static bool StrokesHaveEqualParams(const SkStrokeRec& a, const SkStrokeRec& b) {
113 return a.getWidth() == b.getWidth() && a.getJoin() == b.getJoin() &&
114 (a.getJoin() != SkPaint::kMiter_Join || a.getMiter() == b.getMiter());
115 }
116 float fRadius;
117 float fJoinType; // See GetJoinType().
118 };
119
120 // When PatchAttribs::kExplicitCurveType is set, these are the values that tell the GPU what type of
121 // curve is being drawn.
122 constexpr static float kCubicCurveType SK_MAYBE_UNUSED = 0;
123 constexpr static float kConicCurveType SK_MAYBE_UNUSED = 1;
124 constexpr static float kTriangularConicCurveType SK_MAYBE_UNUSED = 2; // Conic curve with w=Inf.
125
126 // Returns the packed size in bytes of the attribs portion of tessellation patches (or instances) in
127 // GPU buffers.
PatchAttribsStride(PatchAttribs attribs)128 constexpr size_t PatchAttribsStride(PatchAttribs attribs) {
129 return (attribs & PatchAttribs::kJoinControlPoint ? sizeof(float) * 2 : 0) +
130 (attribs & PatchAttribs::kFanPoint ? sizeof(float) * 2 : 0) +
131 (attribs & PatchAttribs::kStrokeParams ? sizeof(float) * 2 : 0) +
132 (attribs & PatchAttribs::kColor
133 ? (attribs & PatchAttribs::kWideColorIfEnabled ? sizeof(float)
134 : sizeof(uint8_t)) * 4 : 0) +
135 (attribs & PatchAttribs::kExplicitCurveType ? sizeof(float) : 0);
136 }
137
138 // Don't tessellate paths that might have an individual curve that requires more than 1024 segments.
139 // (See wangs_formula::worst_case_cubic). If this is the case, call "PreChopPathCurves" first.
140 constexpr static float kMaxTessellationSegmentsPerCurve SK_MAYBE_UNUSED = 1024;
141
142 // Returns a new path, equivalent to 'path' within the given viewport, whose verbs can all be drawn
143 // with 'maxSegments' tessellation segments or fewer, while staying within '1/tessellationPrecision'
144 // pixels of the true curve. Curves and chops that fall completely outside the viewport are
145 // flattened into lines.
146 SkPath PreChopPathCurves(float tessellationPrecision,
147 const SkPath&,
148 const SkMatrix&,
149 const SkRect& viewport);
150
151 // Finds 0, 1, or 2 T values at which to chop the given curve in order to guarantee the resulting
152 // cubics are convex and rotate no more than 180 degrees.
153 //
154 // - If the cubic is "serpentine", then the T values are any inflection points in [0 < T < 1].
155 // - If the cubic is linear, then the T values are any 180-degree cusp points in [0 < T < 1].
156 // - Otherwise the T value is the point at which rotation reaches 180 degrees, iff in [0 < T < 1].
157 //
158 // 'areCusps' is set to true if the chop point occurred at a cusp (within tolerance), or if the chop
159 // point(s) occurred at 180-degree turnaround points on a degenerate flat line.
160 int FindCubicConvex180Chops(const SkPoint[], float T[2], bool* areCusps);
161
162 // Returns true if the given conic (or quadratic) has a cusp point. The w value is not necessary in
163 // determining this. If there is a cusp, it can be found at the midtangent.
ConicHasCusp(const SkPoint p[3])164 inline bool ConicHasCusp(const SkPoint p[3]) {
165 SkVector a = p[1] - p[0];
166 SkVector b = p[2] - p[1];
167 // A conic of any class can only have a cusp if it is a degenerate flat line with a 180 degree
168 // turnarund. To detect this, the beginning and ending tangents must be parallel
169 // (a.cross(b) == 0) and pointing in opposite directions (a.dot(b) < 0).
170 return a.cross(b) == 0 && a.dot(b) < 0;
171 }
172
173 } // namespace skgpu
174
175 #endif // tessellate_Tessellation_DEFINED
176