| 1 |
/* |
| 2 |
* Copyright 2025 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 |
#include "src/core/SkPathRawShapes.h" |
| 8 |
|
| 9 |
#include "include/core/SkPathTypes.h" |
| 10 |
#include "include/core/SkRRect.h" |
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "src/core/SkPathMakers.h" |
| 13 |
|
| 14 |
const SkPathFillType kDefFillType = SkPathFillType::kWinding; |
| 15 |
|
| 16 |
const SkPathVerb gRectVerbs[] = { |
| 17 |
SkPathVerb::kMove, |
| 18 |
SkPathVerb::kLine, |
| 19 |
SkPathVerb::kLine, |
| 20 |
SkPathVerb::kLine, |
| 21 |
SkPathVerb::kClose |
| 22 |
}; |
| 23 |
|
| 24 |
const uint8_t gRectSegMask = kLine_SkPathSegmentMask; |
| 25 |
|
| 26 |
static void set_as_rect(SkPathRaw* raw, SkSpan<SkPoint> storage, |
| 27 |
const SkRect& r, SkPathDirection dir, unsigned index) { |
| 28 |
SkASSERT(storage.size() >= 4); |
| 29 |
|
| 30 |
raw->fPoints = { storage.data(), 4 }; |
| 31 |
raw->fVerbs = gRectVerbs; |
| 32 |
raw->fConics = {}; |
| 33 |
raw->fBounds = r; |
| 34 |
raw->fFillType = kDefFillType; |
| 35 |
raw->fConvexity = SkPathDirection_ToConvexity(dir); |
| 36 |
raw->fSegmentMask = gRectSegMask; |
| 37 |
|
| 38 |
SkPath_RectPointIterator iter(r, dir, index); |
| 39 |
|
| 40 |
storage[0] = iter.current(); |
| 41 |
storage[1] = iter.next(); |
| 42 |
storage[2] = iter.next(); |
| 43 |
storage[3] = iter.next(); |
| 44 |
} |
| 45 |
|
| 46 |
////////////////// |
| 47 |
|
| 48 |
const SkPathVerb gOvalVerbs[] = { |
| 49 |
SkPathVerb::kMove, |
| 50 |
SkPathVerb::kConic, |
| 51 |
SkPathVerb::kConic, |
| 52 |
SkPathVerb::kConic, |
| 53 |
SkPathVerb::kConic, |
| 54 |
SkPathVerb::kClose |
| 55 |
}; |
| 56 |
|
| 57 |
const uint8_t gOvalSegMask = kConic_SkPathSegmentMask; |
| 58 |
|
| 59 |
const float gFourQuarterCircleConics[] = { |
| 60 |
SK_ScalarRoot2Over2, |
| 61 |
SK_ScalarRoot2Over2, |
| 62 |
SK_ScalarRoot2Over2, |
| 63 |
SK_ScalarRoot2Over2, |
| 64 |
}; |
| 65 |
|
| 66 |
static void set_as_oval(SkPathRaw* raw, SkSpan<SkPoint> storage, |
| 67 |
const SkRect& r, SkPathDirection dir, unsigned index) { |
| 68 |
SkASSERT(storage.size() >= 9); |
| 69 |
|
| 70 |
raw->fPoints = { storage.data(), 9 }; |
| 71 |
raw->fVerbs = gOvalVerbs; |
| 72 |
raw->fConics = gFourQuarterCircleConics; |
| 73 |
raw->fBounds = r; |
| 74 |
raw->fFillType = kDefFillType; |
| 75 |
raw->fConvexity = SkPathDirection_ToConvexity(dir); |
| 76 |
raw->fSegmentMask = gOvalSegMask; |
| 77 |
|
| 78 |
SkPath_OvalPointIterator ovalIter(r, dir, index); |
| 79 |
SkPath_RectPointIterator rectIter(r, dir, index + (dir == SkPathDirection::kCW ? 0 : 1)); |
| 80 |
|
| 81 |
storage[0] = ovalIter.current(); |
| 82 |
for (unsigned i = 0; i < 4; ++i) { |
| 83 |
storage[i*2 + 1] = rectIter.next(); |
| 84 |
storage[i*2 + 2] = ovalIter.next(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
///////////////////////////// |
| 89 |
|
| 90 |
const SkPathVerb gRRectVerbs_LineStart[] = { |
| 91 |
SkPathVerb::kMove, |
| 92 |
SkPathVerb::kLine, SkPathVerb::kConic, |
| 93 |
SkPathVerb::kLine, SkPathVerb::kConic, |
| 94 |
SkPathVerb::kLine, SkPathVerb::kConic, |
| 95 |
SkPathVerb::kLine, SkPathVerb::kConic, |
| 96 |
SkPathVerb::kClose |
| 97 |
}; |
| 98 |
|
| 99 |
const SkPathVerb gRRectVerbs_ConicStart[] = { |
| 100 |
SkPathVerb::kMove, |
| 101 |
SkPathVerb::kConic, SkPathVerb::kLine, |
| 102 |
SkPathVerb::kConic, SkPathVerb::kLine, |
| 103 |
SkPathVerb::kConic, SkPathVerb::kLine, |
| 104 |
SkPathVerb::kConic, // we can skip the last line |
| 105 |
SkPathVerb::kClose |
| 106 |
}; |
| 107 |
|
| 108 |
const uint8_t gRRectSegMask = kLine_SkPathSegmentMask | kConic_SkPathSegmentMask; |
| 109 |
|
| 110 |
static void set_as_rrect(SkPathRaw* raw, SkSpan<SkPoint> storage, |
| 111 |
const SkRRect& rrect, SkPathDirection dir, unsigned index) { |
| 112 |
// we start with a conic on odd indices when moving CW vs. even indices when moving CCW |
| 113 |
const bool startsWithConic = ((index & 1) == (dir == SkPathDirection::kCW)); |
| 114 |
// if we start with a conic, we end with a line, which we can skip (relying on close()) |
| 115 |
const size_t npoints = 13 - startsWithConic; |
| 116 |
const SkRect& bounds = rrect.getBounds(); |
| 117 |
|
| 118 |
SkASSERT(storage.size() >= npoints); |
| 119 |
|
| 120 |
raw->fPoints = { storage.data(), npoints }; |
| 121 |
if (startsWithConic) { |
| 122 |
raw->fVerbs = gRRectVerbs_ConicStart; |
| 123 |
} else { |
| 124 |
raw->fVerbs = gRRectVerbs_LineStart; |
| 125 |
} |
| 126 |
raw->fConics = gFourQuarterCircleConics; |
| 127 |
raw->fBounds = bounds; |
| 128 |
raw->fFillType = kDefFillType; |
| 129 |
raw->fConvexity = SkPathDirection_ToConvexity(dir); |
| 130 |
raw->fSegmentMask = gRRectSegMask; |
| 131 |
|
| 132 |
SkPath_RRectPointIterator rrectIter(rrect, dir, index); |
| 133 |
// Corner iterator indices follow the collapsed radii model, |
| 134 |
// adjusted such that the start pt is "behind" the radii start pt. |
| 135 |
const unsigned rectStartIndex = index / 2 + (dir == SkPathDirection::kCW ? 0 : 1); |
| 136 |
SkPath_RectPointIterator rectIter(bounds, dir, rectStartIndex); |
| 137 |
|
| 138 |
storage[0] = rrectIter.current(); |
| 139 |
if (startsWithConic) { |
| 140 |
for (unsigned i = 0; i < 3; ++i) { |
| 141 |
// conic points |
| 142 |
storage[i*3 + 1] = rectIter.next(); |
| 143 |
storage[i*3 + 2] = rrectIter.next(); |
| 144 |
// line point |
| 145 |
storage[i*3 + 3] = rrectIter.next(); |
| 146 |
} |
| 147 |
// last conic points |
| 148 |
storage[10] = rectIter.next(); |
| 149 |
storage[11] = rrectIter.next(); |
| 150 |
// the final line is accomplished by close() |
| 151 |
} else { |
| 152 |
for (unsigned i = 0; i < 4; ++i) { |
| 153 |
// line point |
| 154 |
storage[i*3 + 1] = rrectIter.next(); |
| 155 |
// conic points |
| 156 |
storage[i*3 + 2] = rectIter.next(); |
| 157 |
storage[i*3 + 3] = rrectIter.next(); |
| 158 |
} |
| 159 |
} |
| 160 |
// close |
| 161 |
} |
| 162 |
|
| 163 |
///////////////////////////// |
| 164 |
|
| 165 |
SkPathRawShapes::Rect::Rect(const SkRect& r, SkPathDirection dir, unsigned index) { |
| 166 |
set_as_rect(this, fStorage, r, dir, index); |
| 167 |
} |
| 168 |
|
| 169 |
SkPathRawShapes::Oval::Oval(const SkRect& r, SkPathDirection dir, unsigned index) { |
| 170 |
set_as_oval(this, fStorage, r, dir, index); |
| 171 |
} |
| 172 |
|
| 173 |
SkPathRawShapes::RRect::RRect(const SkRRect& rrect, SkPathDirection dir, unsigned index) { |
| 174 |
const SkRect& bounds = rrect.getBounds(); |
| 175 |
|
| 176 |
if (rrect.isRect() || rrect.isEmpty()) { |
| 177 |
// degenerate(rect) => radii points are collapsing |
| 178 |
set_as_rect(this, fStorage, bounds, dir, (index + 1) / 2); |
| 179 |
} else if (rrect.isOval()) { |
| 180 |
// degenerate(oval) => line points are collapsing |
| 181 |
set_as_oval(this, fStorage, bounds, dir, index / 2); |
| 182 |
} else { |
| 183 |
set_as_rrect(this, fStorage, rrect, dir, index); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
///////////////////////////// |
| 188 |
|
| 189 |
const SkPathVerb gTriangle_Verbs[] = { |
| 190 |
SkPathVerb::kMove, |
| 191 |
SkPathVerb::kLine, |
| 192 |
SkPathVerb::kLine, |
| 193 |
SkPathVerb::kClose |
| 194 |
}; |
| 195 |
|
| 196 |
static SkPathConvexity tri_to_convexity(SkSpan<const SkPoint> pts) { |
| 197 |
SkVector u = pts[1] - pts[0], |
| 198 |
v = pts[2] - pts[1]; |
| 199 |
float cross = u.fX * v.fY - u.fY * v.fX; |
| 200 |
|
| 201 |
return cross > 0 ? SkPathConvexity::kConvex_CW |
| 202 |
: (cross < 0) ? SkPathConvexity::kConvex_CCW |
| 203 |
: SkPathConvexity::kConvex_Degenerate; |
| 204 |
} |
| 205 |
|
| 206 |
SkPathRawShapes::Triangle::Triangle(SkSpan<const SkPoint> threePoints, const SkRect& bounds) |
| 207 |
: SkPathRaw{threePoints, gTriangle_Verbs, {}, bounds, |
| 208 |
SkPathFillType::kDefault, tri_to_convexity(threePoints), kLine_SkPathSegmentMask} |
| 209 |
{ |
| 210 |
SkASSERT(threePoints.size() == 3); |
| 211 |
SkASSERT(SkRect::Bounds(threePoints).value() == bounds); |
| 212 |
} |