Debian Codegraph @

src/pathops/SkPathWriter.cpp

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright 2012 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 #include "src/pathops/SkPathWriter.h"
8
9 #include "include/core/SkTypes.h"
10 #include "include/private/base/SkMath.h"
11 #include "src/base/SkTSort.h"
12 #include "src/core/SkPathPriv.h"
13 #include "src/pathops/SkOpSegment.h"
14 #include "src/pathops/SkOpSpan.h"
15 #include "src/pathops/SkPathOpsDebug.h"
16 #include "src/pathops/SkPathOpsTypes.h"
17
18 using namespace skia_private;
19
20 SkPathWriter::SkPathWriter(SkPathFillType ft) : fBuilder(ft) {
21 init();
22 }
23
24 void SkPathWriter::close() {
25 if (fCurrent.isEmpty()) {
26 return;
27 }
28 SkASSERT(this->isClosed());
29 #if DEBUG_PATH_CONSTRUCTION
30 SkDebugf("path.close();\n");
31 #endif
32 fCurrent.close();
33 if (auto raw = SkPathPriv::Raw(fCurrent, SkResolveConvexity::kNo)) {
34 fBuilder.addRaw(*raw);
35 }
36 init();
37 }
38
39 void SkPathWriter::conicTo(const SkPoint& pt1, const SkOpPtT* pt2, SkScalar weight) {
40 SkPoint pt2pt = this->update(pt2);
41 #if DEBUG_PATH_CONSTRUCTION
42 SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n",
43 pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY, weight);
44 #endif
45 fCurrent.conicTo(pt1, pt2pt, weight);
46 }
47
48 void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkOpPtT* pt3) {
49 SkPoint pt3pt = this->update(pt3);
50 #if DEBUG_PATH_CONSTRUCTION
51 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
52 pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3pt.fX, pt3pt.fY);
53 #endif
54 fCurrent.cubicTo(pt1, pt2, pt3pt);
55 }
56
57 bool SkPathWriter::deferredLine(const SkOpPtT* pt) {
•58 SkASSERT(pt);
59 SkASSERT(fFirstPtT);
60 SkASSERT(fDefer[0]);
61 if (fDefer[0] == pt) {
62 // FIXME: why we're adding a degenerate line? Caller should have preflighted this.
63 return true;
64 }
65 if (pt->contains(fDefer[0])) {
66 // FIXME: why we're adding a degenerate line?
67 return true;
68 }
69 if (this->matchedLast(pt)) {
70 return false;
71 }
72 if (fDefer[1] && this->changedSlopes(pt)) {
73 this->lineTo();
74 fDefer[0] = fDefer[1];
75 }
76 fDefer[1] = pt;
77 return true;
78 }
79
80 void SkPathWriter::deferredMove(const SkOpPtT* pt) {
•81 SkASSERT(pt);
82 if (!fDefer[1]) {
83 fFirstPtT = fDefer[0] = pt;
84 return;
85 }
86 SkASSERT(fDefer[0]);
87 if (!this->matchedLast(pt)) {
88 this->finishContour();
89 fFirstPtT = fDefer[0] = pt;
90 }
91 }
92
93 void SkPathWriter::finishContour() {
94 if (!this->matchedLast(fDefer[0])) {
95 if (!fDefer[1]) {
96 return;
97 }
98 this->lineTo();
99 }
100 if (fCurrent.isEmpty()) {
101 return;
102 }
103 if (this->isClosed()) {
104 this->close();
105 } else {
106 SkASSERT(fDefer[1]);
107 fEndPtTs.push_back(fFirstPtT);
108 fEndPtTs.push_back(fDefer[1]);
109 fPartials.push_back(fCurrent);
110 this->init();
111 }
112 }
113
114 void SkPathWriter::init() {
115 fCurrent.reset();
116 fFirstPtT = fDefer[0] = fDefer[1] = nullptr;
117 }
118
119 bool SkPathWriter::isClosed() const {
120 return this->matchedLast(fFirstPtT);
121 }
122
123 void SkPathWriter::lineTo() {
124 if (fCurrent.isEmpty()) {
125 this->moveTo();
126 }
127 #if DEBUG_PATH_CONSTRUCTION
128 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1]->fPt.fX, fDefer[1]->fPt.fY);
129 #endif
130 fCurrent.lineTo(fDefer[1]->fPt);
131 }
132
133 bool SkPathWriter::matchedLast(const SkOpPtT* test) const {
134 if (test == fDefer[1]) {
135 return true;
136 }
137 if (!test) {
138 return false;
139 }
140 if (!fDefer[1]) {
141 return false;
142 }
143 return test->contains(fDefer[1]);
144 }
145
146 void SkPathWriter::moveTo() {
147 #if DEBUG_PATH_CONSTRUCTION
148 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fFirstPtT->fPt.fX, fFirstPtT->fPt.fY);
149 #endif
150 fCurrent.moveTo(fFirstPtT->fPt);
151 }
152
153 void SkPathWriter::quadTo(const SkPoint& pt1, const SkOpPtT* pt2) {
154 SkPoint pt2pt = this->update(pt2);
155 #if DEBUG_PATH_CONSTRUCTION
156 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
157 pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY);
158 #endif
159 fCurrent.quadTo(pt1, pt2pt);
160 }
161
162 // if last point to be written matches the current path's first point, alter the
163 // last to avoid writing a degenerate lineTo when the path is closed
164 SkPoint SkPathWriter::update(const SkOpPtT* pt) {
•165 SkASSERT(pt);
166 if (!fDefer[1]) {
167 this->moveTo();
168 } else if (!this->matchedLast(fDefer[0])) {
169 this->lineTo();
170 }
171 SkPoint result = pt->fPt;
172 if (fFirstPtT && result != fFirstPtT->fPt && fFirstPtT->contains(pt)) {
173 result = fFirstPtT->fPt;
174 }
175 fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line
176 return result;
177 }
178
179 bool SkPathWriter::someAssemblyRequired() {
180 this->finishContour();
181 return !fEndPtTs.empty();
182 }
183
184 bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const {
•185 SkASSERT(ptT);
186 if (matchedLast(fDefer[0])) {
187 return false;
188 }
189 SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt;
190 SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt;
191 return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX;
192 }
193
194 class DistanceLessThan {
195 public:
196 DistanceLessThan(double* distances) : fDistances(distances) { }
197 double* fDistances;
198 bool operator()(const int one, const int two) const {
199 return fDistances[one] < fDistances[two];
200 }
201 };
202
203 /*
204 check start and end of each contour
205 if not the same, record them
206 match them up
207 connect closest
208 reassemble contour pieces into new path
209 */
210 void SkPathWriter::assemble() {
211 if (!this->someAssemblyRequired()) {
212 return;
213 }
214 #if DEBUG_PATH_CONSTRUCTION
215 SkDebugf("%s\n", __FUNCTION__);
216 #endif
217 SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours
218 int endCount = fEndPtTs.size(); // all starts and ends
219 SkASSERT(endCount > 0);
•220 SkASSERT(endCount == (int)fPartials.size() * 2);
221
222 // Limit the number of partial contours to avoid O(N^2) complexity and integer overflows.
223 // 10,000 partial contours results in 20,000 ends and ~200,000,000 distance entries.
224 constexpr int kMaxPartialContours = 10000;
225 if (endCount > kMaxPartialContours * 2) {
226 return;
227 }
228
229 #if DEBUG_ASSEMBLE
230 for (int index = 0; index < endCount; index += 2) {
231 const SkOpPtT* eStart = runs[index];
232 const SkOpPtT* eEnd = runs[index + 1];
233 SkASSERT(eStart != eEnd);
234 SkASSERT(!eStart->contains(eEnd));
235 SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__,
236 eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY);
237 }
238 #endif
239 // lengthen any partial contour adjacent to a simple segment
240 for (int pIndex = 0; pIndex < endCount; pIndex++) {
241 SkOpPtT* opPtT = const_cast<SkOpPtT*>(runs[pIndex]);
242 SkPathWriter partWriter(SkPathFillType::kDefault);
243 do {
244 if (!zero_or_one(opPtT->fT)) {
245 break;
246 }
247 SkOpSpanBase* opSpanBase = opPtT->span();
248 SkOpSpanBase* start = opPtT->fT ? opSpanBase->prev() : opSpanBase->upCast()->next();
249 int step = opPtT->fT ? 1 : -1;
250 const SkOpSegment* opSegment = opSpanBase->segment();
251 const SkOpSegment* nextSegment = opSegment->isSimple(&start, &step);
252 if (!nextSegment) {
253 break;
254 }
255 SkOpSpanBase* opSpanEnd = start->t() ? start->prev() : start->upCast()->next();
256 if (start->starter(opSpanEnd)->alreadyAdded()) {
257 break;
258 }
259 nextSegment->addCurveTo(start, opSpanEnd, &partWriter);
260 opPtT = opSpanEnd->ptT();
261 SkOpPtT** runsPtr = const_cast<SkOpPtT**>(&runs[pIndex]);
262 *runsPtr = opPtT;
263 } while (true);
264 partWriter.finishContour();
265 const TArray<SkPathBuilder>& partPartials = partWriter.partials();
266 if (partPartials.empty()) {
267 continue;
268 }
269 // if pIndex is even, reverse and prepend to fPartials; otherwise, append
270 SkPathBuilder& partial = const_cast<SkPathBuilder&>(fPartials[pIndex >> 1]);
271 const SkPath part = partPartials[0].snapshot();
272 if (pIndex & 1) {
273 partial.addPath(part, SkPath::kExtend_AddPathMode);
274 } else {
275 SkPathBuilder reverse;
276 SkPathPriv::ReverseAddPath(&reverse, part);
277 reverse.addPath(partial.detach(), SkPath::kExtend_AddPathMode);
278 partial = reverse;
279 }
280 }
281 SkTDArray<int> sLink, eLink;
282 int linkCount = endCount / 2; // number of partial contours
283 sLink.append(linkCount);
284 eLink.append(linkCount);
285 int rIndex, iIndex;
286 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
287 sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
288 }
289 const int entries = endCount * (endCount - 1) / 2; // folded triangle
290 STArray<8, double, true> distances(entries);
291 STArray<8, int, true> sortedDist(entries);
292 STArray<8, int, true> distLookup(entries);
293 int rRow = 0;
294 int dIndex = 0;
295 for (rIndex = 0; rIndex < endCount - 1; ++rIndex) {
296 const SkOpPtT* oPtT = runs[rIndex];
297 for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) {
298 const SkOpPtT* iPtT = runs[iIndex];
299 double dx = iPtT->fPt.fX - oPtT->fPt.fX;
300 double dy = iPtT->fPt.fY - oPtT->fPt.fY;
301 double dist = dx * dx + dy * dy;
302 distLookup.push_back(rRow + iIndex);
303 distances.push_back(dist); // oStart distance from iStart
304 sortedDist.push_back(dIndex++);
305 }
306 rRow += endCount;
307 }
308 SkASSERT(dIndex == entries);
309 SkTQSort<int>(sortedDist.begin(), sortedDist.end(), DistanceLessThan(distances.begin()));
310 int remaining = linkCount; // number of start/end pairs
311 for (rIndex = 0; rIndex < entries; ++rIndex) {
312 int pair = sortedDist[rIndex];
313 pair = distLookup[pair];
314 int row = pair / endCount;
315 int col = pair - row * endCount;
316 int ndxOne = row >> 1;
317 bool endOne = row & 1;
318 int* linkOne = endOne ? eLink.begin() : sLink.begin();
319 if (linkOne[ndxOne] != SK_MaxS32) {
320 continue;
321 }
322 int ndxTwo = col >> 1;
323 bool endTwo = col & 1;
324 int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
325 if (linkTwo[ndxTwo] != SK_MaxS32) {
326 continue;
327 }
328 SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
329 bool flip = endOne == endTwo;
330 linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
331 linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
332 if (!--remaining) {
333 break;
334 }
335 }
336 SkASSERT(!remaining);
337 #if DEBUG_ASSEMBLE
338 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
339 int s = sLink[rIndex];
340 int e = eLink[rIndex];
341 SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
342 s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
343 }
344 #endif
345 rIndex = 0;
346 do {
347 bool forward = true;
348 bool first = true;
349 int sIndex = sLink[rIndex];
350 SkASSERT(sIndex != SK_MaxS32);
351 sLink[rIndex] = SK_MaxS32;
352 int eIndex;
353 if (sIndex < 0) {
354 eIndex = sLink[~sIndex];
355 sLink[~sIndex] = SK_MaxS32;
356 } else {
357 eIndex = eLink[sIndex];
358 eLink[sIndex] = SK_MaxS32;
359 }
360 SkASSERT(eIndex != SK_MaxS32);
361 #if DEBUG_ASSEMBLE
362 SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
363 sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
364 eIndex < 0 ? ~eIndex : eIndex);
365 #endif
366 do {
367 SkPath contour = fPartials[rIndex].snapshot();
368 if (!first) {
369 auto prior = fBuilder.getLastPt();
370 if (!prior) {
371 return;
372 }
373 SkSpan<const SkPoint> contourPts = contour.points();
374 SkPoint next;
375 if (forward) {
376 next = contourPts.empty() ? SkPoint{0, 0} : contourPts.front();
377 } else {
•378 if (contourPts.empty()) {
379 SkDEBUGFAIL("unexpected empty contour");
380 return;
381 }
382 next = contourPts.back();
383 }
384 if (*prior != next) {
385 /* TODO: if there is a gap between open path written so far and path to come,
386 connect by following segments from one to the other, rather than introducing
387 a diagonal to connect the two.
388 */
389 }
390 }
391 if (forward) {
392 fBuilder.addPath(contour,
393 first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode);
394 } else {
395 SkASSERT(!first);
396 SkPathPriv::ReversePathTo(&fBuilder, contour);
397 }
398 if (first) {
399 first = false;
400 }
401 #if DEBUG_ASSEMBLE
402 SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
403 eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
404 sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
405 #endif
406 if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
407 fBuilder.close();
408 break;
409 }
410 if (forward) {
411 eIndex = eLink[rIndex];
412 SkASSERT(eIndex != SK_MaxS32);
413 eLink[rIndex] = SK_MaxS32;
414 if (eIndex >= 0) {
415 SkASSERT(sLink[eIndex] == rIndex);
416 sLink[eIndex] = SK_MaxS32;
417 } else {
418 SkASSERT(eLink[~eIndex] == ~rIndex);
419 eLink[~eIndex] = SK_MaxS32;
420 }
421 } else {
422 eIndex = sLink[rIndex];
423 SkASSERT(eIndex != SK_MaxS32);
424 sLink[rIndex] = SK_MaxS32;
425 if (eIndex >= 0) {
426 SkASSERT(eLink[eIndex] == rIndex);
427 eLink[eIndex] = SK_MaxS32;
428 } else {
429 SkASSERT(sLink[~eIndex] == ~rIndex);
430 sLink[~eIndex] = SK_MaxS32;
431 }
432 }
433 rIndex = eIndex;
434 if (rIndex < 0) {
435 forward ^= 1;
436 rIndex = ~rIndex;
437 }
438 } while (true);
439 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
440 if (sLink[rIndex] != SK_MaxS32) {
441 break;
442 }
443 }
444 } while (rIndex < linkCount);
445 #if DEBUG_ASSEMBLE
446 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
447 SkASSERT(sLink[rIndex] == SK_MaxS32);
448 SkASSERT(eLink[rIndex] == SK_MaxS32);
449 }
450 #endif
451 }