Debian Codegraph @

src/pathops/SkPathOpsDebug.cpp

raw
1 /*
2 * Copyright 2013 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 #include "src/pathops/SkPathOpsDebug.h"
9
10 #include "include/core/SkPath.h"
11 #include "include/core/SkPathTypes.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkString.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/base/SkMath.h"
17 #include "include/private/base/SkMutex.h"
18 #include "src/core/SkPathPriv.h"
19 #include "src/pathops/SkIntersections.h"
20 #include "src/pathops/SkOpAngle.h"
21 #include "src/pathops/SkOpCoincidence.h"
22 #include "src/pathops/SkOpSegment.h"
23 #include "src/pathops/SkOpSpan.h"
24 #include "src/pathops/SkPathOpsConic.h"
25 #include "src/pathops/SkPathOpsCubic.h"
26 #include "src/pathops/SkPathOpsPoint.h"
27 #include "src/pathops/SkPathOpsQuad.h"
28 #include "src/pathops/SkPathOpsRect.h"
29 #include "src/pathops/SkPathOpsTypes.h"
30
31 #include <cstdint>
32 #include <cstring>
33
34 #if DEBUG_DUMP_VERIFY
35 bool SkPathOpsDebug::gDumpOp; // set to true to write op to file before a crash
36 bool SkPathOpsDebug::gVerifyOp; // set to true to compare result against regions
37 #endif
38
39 bool SkPathOpsDebug::gRunFail; // set to true to check for success on tests known to fail
40 bool SkPathOpsDebug::gVeryVerbose; // set to true to run extensive checking tests
41
42 #if DEBUG_SORT
43 int SkPathOpsDebug::gSortCountDefault = SK_MaxS32;
44 int SkPathOpsDebug::gSortCount;
45 #endif
46
47 #if DEBUG_ACTIVE_OP
48 const char* SkPathOpsDebug::kPathOpStr[] = {"diff", "sect", "union", "xor", "rdiff"};
49 #endif
50
51 #if defined SK_DEBUG || !FORCE_RELEASE
52
53 int SkPathOpsDebug::gContourID = 0;
54 int SkPathOpsDebug::gSegmentID = 0;
55
56 bool SkPathOpsDebug::ChaseContains(const SkTDArray<SkOpSpanBase*>& chaseArray,
57 const SkOpSpanBase* span) {
58 for (int index = 0; index < chaseArray.size(); ++index) {
59 const SkOpSpanBase* entry = chaseArray[index];
60 if (entry == span) {
61 return true;
62 }
63 }
64 return false;
65 }
66 #endif
67
68 #if DEBUG_ACTIVE_SPANS
69 SkString SkPathOpsDebug::gActiveSpans;
70 #endif
71
72 #if DEBUG_COIN
73
74 #include "src/pathops/SkOpContour.h"
75
76 class SkCoincidentSpans;
77
78 SkPathOpsDebug::CoinDict SkPathOpsDebug::gCoinSumChangedDict;
79 SkPathOpsDebug::CoinDict SkPathOpsDebug::gCoinSumVisitedDict;
80
81 static const int kGlitchType_Count = SkPathOpsDebug::kUnalignedTail_Glitch + 1;
82
83 struct SpanGlitch {
84 const SkOpSpanBase* fBase;
85 const SkOpSpanBase* fSuspect;
86 const SkOpSegment* fSegment;
87 const SkOpSegment* fOppSegment;
88 const SkOpPtT* fCoinSpan;
89 const SkOpPtT* fEndSpan;
90 const SkOpPtT* fOppSpan;
91 const SkOpPtT* fOppEndSpan;
92 double fStartT;
93 double fEndT;
94 double fOppStartT;
95 double fOppEndT;
96 SkPoint fPt;
97 SkPathOpsDebug::GlitchType fType;
98
99 void dumpType() const;
100 };
101
102 struct SkPathOpsDebug::GlitchLog {
103 void init(const SkOpGlobalState* state) {
104 fGlobalState = state;
105 }
106
107 SpanGlitch* recordCommon(GlitchType type) {
108 SpanGlitch* glitch = fGlitches.append();
109 glitch->fBase = nullptr;
110 glitch->fSuspect = nullptr;
111 glitch->fSegment = nullptr;
112 glitch->fOppSegment = nullptr;
113 glitch->fCoinSpan = nullptr;
114 glitch->fEndSpan = nullptr;
115 glitch->fOppSpan = nullptr;
116 glitch->fOppEndSpan = nullptr;
117 glitch->fStartT = SK_ScalarNaN;
118 glitch->fEndT = SK_ScalarNaN;
119 glitch->fOppStartT = SK_ScalarNaN;
120 glitch->fOppEndT = SK_ScalarNaN;
121 glitch->fPt = { SK_ScalarNaN, SK_ScalarNaN };
122 glitch->fType = type;
123 return glitch;
124 }
125
126 void record(GlitchType type, const SkOpSpanBase* base, const SkOpSpanBase* suspect = nullptr) {
127 SpanGlitch* glitch = recordCommon(type);
128 glitch->fBase = base;
129 glitch->fSuspect = suspect;
130 }
131
132 void record(GlitchType type, const SkOpSpanBase* base, const SkOpPtT* ptT) {
133 SpanGlitch* glitch = recordCommon(type);
134 glitch->fBase = base;
135 glitch->fCoinSpan = ptT;
136 }
137
138 void record(GlitchType type,
139 const SkCoincidentSpans* coin,
140 const SkCoincidentSpans* opp = nullptr) {
141 SpanGlitch* glitch = recordCommon(type);
142 glitch->fCoinSpan = coin->coinPtTStart();
143 glitch->fEndSpan = coin->coinPtTEnd();
144 if (opp) {
145 glitch->fOppSpan = opp->coinPtTStart();
146 glitch->fOppEndSpan = opp->coinPtTEnd();
147 }
148 }
149
150 void record(GlitchType type, const SkOpSpanBase* base,
151 const SkOpSegment* seg, double t, SkPoint pt) {
152 SpanGlitch* glitch = recordCommon(type);
153 glitch->fBase = base;
154 glitch->fSegment = seg;
155 glitch->fStartT = t;
156 glitch->fPt = pt;
157 }
158
159 void record(GlitchType type, const SkOpSpanBase* base, double t,
160 SkPoint pt) {
161 SpanGlitch* glitch = recordCommon(type);
162 glitch->fBase = base;
163 glitch->fStartT = t;
164 glitch->fPt = pt;
165 }
166
167 void record(GlitchType type, const SkCoincidentSpans* coin,
168 const SkOpPtT* coinSpan, const SkOpPtT* endSpan) {
169 SpanGlitch* glitch = recordCommon(type);
170 glitch->fCoinSpan = coin->coinPtTStart();
171 glitch->fEndSpan = coin->coinPtTEnd();
172 glitch->fEndSpan = endSpan;
173 glitch->fOppSpan = coinSpan;
174 glitch->fOppEndSpan = endSpan;
175 }
176
177 void record(GlitchType type, const SkCoincidentSpans* coin,
178 const SkOpSpanBase* base) {
179 SpanGlitch* glitch = recordCommon(type);
180 glitch->fBase = base;
181 glitch->fCoinSpan = coin->coinPtTStart();
182 glitch->fEndSpan = coin->coinPtTEnd();
183 }
184
185 void record(GlitchType type, const SkOpPtT* ptTS, const SkOpPtT* ptTE,
186 const SkOpPtT* oPtTS, const SkOpPtT* oPtTE) {
187 SpanGlitch* glitch = recordCommon(type);
188 glitch->fCoinSpan = ptTS;
189 glitch->fEndSpan = ptTE;
190 glitch->fOppSpan = oPtTS;
191 glitch->fOppEndSpan = oPtTE;
192 }
193
194 void record(GlitchType type, const SkOpSegment* seg, double startT,
195 double endT, const SkOpSegment* oppSeg, double oppStartT, double oppEndT) {
196 SpanGlitch* glitch = recordCommon(type);
197 glitch->fSegment = seg;
198 glitch->fStartT = startT;
199 glitch->fEndT = endT;
200 glitch->fOppSegment = oppSeg;
201 glitch->fOppStartT = oppStartT;
202 glitch->fOppEndT = oppEndT;
203 }
204
205 void record(GlitchType type, const SkOpSegment* seg,
206 const SkOpSpan* span) {
207 SpanGlitch* glitch = recordCommon(type);
208 glitch->fSegment = seg;
209 glitch->fBase = span;
210 }
211
212 void record(GlitchType type, double t, const SkOpSpanBase* span) {
213 SpanGlitch* glitch = recordCommon(type);
214 glitch->fStartT = t;
215 glitch->fBase = span;
216 }
217
218 void record(GlitchType type, const SkOpSegment* seg) {
219 SpanGlitch* glitch = recordCommon(type);
220 glitch->fSegment = seg;
221 }
222
223 void record(GlitchType type, const SkCoincidentSpans* coin,
224 const SkOpPtT* ptT) {
225 SpanGlitch* glitch = recordCommon(type);
226 glitch->fCoinSpan = coin->coinPtTStart();
227 glitch->fEndSpan = ptT;
228 }
229
230 SkTDArray<SpanGlitch> fGlitches;
231 const SkOpGlobalState* fGlobalState;
232 };
233
234
235 void SkPathOpsDebug::CoinDict::add(const SkPathOpsDebug::CoinDict& dict) {
236 int count = dict.fDict.size();
237 for (int index = 0; index < count; ++index) {
238 this->add(dict.fDict[index]);
239 }
240 }
241
242 void SkPathOpsDebug::CoinDict::add(const CoinDictEntry& key) {
243 int count = fDict.size();
244 for (int index = 0; index < count; ++index) {
245 CoinDictEntry* entry = &fDict[index];
246 if (entry->fIteration == key.fIteration && entry->fLineNumber == key.fLineNumber) {
247 SkASSERT(!strcmp(entry->fFunctionName, key.fFunctionName));
248 if (entry->fGlitchType == kUninitialized_Glitch) {
249 entry->fGlitchType = key.fGlitchType;
250 }
251 return;
252 }
253 }
254 *fDict.append() = key;
255 }
256
257 #endif
258
259 #if DEBUG_COIN
260 static void missing_coincidence(SkPathOpsDebug::GlitchLog* glitches,
261 const SkOpContourHead* contourList) {
262 const SkOpContour* contour = contourList;
263 // bool result = false;
264 do {
265 /* result |= */ contour->debugMissingCoincidence(glitches);
266 } while ((contour = contour->next()));
267 return;
268 }
269
270 static void move_multiples(SkPathOpsDebug::GlitchLog* glitches,
271 const SkOpContourHead* contourList) {
272 const SkOpContour* contour = contourList;
273 do {
274 contour->debugMoveMultiples(glitches);
275 } while ((contour = contour->next()));
276 return;
277 }
278
279 static void move_nearby(SkPathOpsDebug::GlitchLog* glitches, const SkOpContourHead* contourList) {
280 const SkOpContour* contour = contourList;
281 do {
282 contour->debugMoveNearby(glitches);
283 } while ((contour = contour->next()));
284 }
285
286 #endif
287
288 #if DEBUG_COIN
289 void SkOpGlobalState::debugAddToCoinChangedDict() {
290
291 #if DEBUG_COINCIDENCE
292 SkPathOpsDebug::CheckHealth(fContourHead);
293 #endif
294 // see if next coincident operation makes a change; if so, record it
295 SkPathOpsDebug::GlitchLog glitches;
296 const char* funcName = fCoinDictEntry.fFunctionName;
297 if (!strcmp("calc_angles", funcName)) {
298 //
299 } else if (!strcmp("missing_coincidence", funcName)) {
300 missing_coincidence(&glitches, fContourHead);
301 } else if (!strcmp("move_multiples", funcName)) {
302 move_multiples(&glitches, fContourHead);
303 } else if (!strcmp("move_nearby", funcName)) {
304 move_nearby(&glitches, fContourHead);
305 } else if (!strcmp("addExpanded", funcName)) {
306 fCoincidence->debugAddExpanded(&glitches);
307 } else if (!strcmp("addMissing", funcName)) {
308 bool added;
309 fCoincidence->debugAddMissing(&glitches, &added);
310 } else if (!strcmp("addEndMovedSpans", funcName)) {
311 fCoincidence->debugAddEndMovedSpans(&glitches);
312 } else if (!strcmp("correctEnds", funcName)) {
313 fCoincidence->debugCorrectEnds(&glitches);
314 } else if (!strcmp("expand", funcName)) {
315 fCoincidence->debugExpand(&glitches);
316 } else if (!strcmp("findOverlaps", funcName)) {
317 //
318 } else if (!strcmp("mark", funcName)) {
319 fCoincidence->debugMark(&glitches);
320 } else if (!strcmp("apply", funcName)) {
321 //
322 } else {
323 SkASSERT(0); // add missing case
324 }
325 if (glitches.fGlitches.size()) {
326 fCoinDictEntry.fGlitchType = glitches.fGlitches[0].fType;
327 }
328 fCoinChangedDict.add(fCoinDictEntry);
329 }
330 #endif
331
332 void SkPathOpsDebug::ShowActiveSpans(SkOpContourHead* contourList) {
333 #if DEBUG_ACTIVE_SPANS
334 SkString str;
335 SkOpContour* contour = contourList;
336 do {
337 contour->debugShowActiveSpans(&str);
338 } while ((contour = contour->next()));
339 if (!gActiveSpans.equals(str)) {
340 const char* s = str.c_str();
341 const char* end;
342 while ((end = strchr(s, '\n'))) {
343 SkDebugf("%.*s", (int) (end - s + 1), s);
344 s = end + 1;
345 }
346 gActiveSpans.set(str);
347 }
348 #endif
349 }
350
351 #if DEBUG_COINCIDENCE || DEBUG_COIN
352 void SkPathOpsDebug::CheckHealth(SkOpContourHead* contourList) {
353 #if DEBUG_COINCIDENCE
354 contourList->globalState()->debugSetCheckHealth(true);
355 #endif
356 #if DEBUG_COIN
357 GlitchLog glitches;
358 const SkOpContour* contour = contourList;
359 const SkOpCoincidence* coincidence = contour->globalState()->coincidence();
360 coincidence->debugCheckValid(&glitches); // don't call validate; spans may be inconsistent
361 do {
362 contour->debugCheckHealth(&glitches);
363 contour->debugMissingCoincidence(&glitches);
364 } while ((contour = contour->next()));
365 bool added;
366 coincidence->debugAddMissing(&glitches, &added);
367 coincidence->debugExpand(&glitches);
368 coincidence->debugAddExpanded(&glitches);
369 coincidence->debugMark(&glitches);
370 unsigned mask = 0;
371 for (int index = 0; index < glitches.fGlitches.size(); ++index) {
372 const SpanGlitch& glitch = glitches.fGlitches[index];
373 mask |= 1 << glitch.fType;
374 }
375 for (int index = 0; index < kGlitchType_Count; ++index) {
376 SkDebugf(mask & (1 << index) ? "x" : "-");
377 }
378 SkDebugf(" %s\n", contourList->globalState()->debugCoinDictEntry().fFunctionName);
379 for (int index = 0; index < glitches.fGlitches.size(); ++index) {
380 const SpanGlitch& glitch = glitches.fGlitches[index];
381 SkDebugf("%02d: ", index);
382 if (glitch.fBase) {
383 SkDebugf(" seg/base=%d/%d", glitch.fBase->segment()->debugID(),
384 glitch.fBase->debugID());
385 }
386 if (glitch.fSuspect) {
387 SkDebugf(" seg/base=%d/%d", glitch.fSuspect->segment()->debugID(),
388 glitch.fSuspect->debugID());
389 }
390 if (glitch.fSegment) {
391 SkDebugf(" segment=%d", glitch.fSegment->debugID());
392 }
393 if (glitch.fCoinSpan) {
394 SkDebugf(" coinSeg/Span/PtT=%d/%d/%d", glitch.fCoinSpan->segment()->debugID(),
395 glitch.fCoinSpan->span()->debugID(), glitch.fCoinSpan->debugID());
396 }
397 if (glitch.fEndSpan) {
398 SkDebugf(" endSpan=%d", glitch.fEndSpan->debugID());
399 }
400 if (glitch.fOppSpan) {
401 SkDebugf(" oppSeg/Span/PtT=%d/%d/%d", glitch.fOppSpan->segment()->debugID(),
402 glitch.fOppSpan->span()->debugID(), glitch.fOppSpan->debugID());
403 }
404 if (glitch.fOppEndSpan) {
405 SkDebugf(" oppEndSpan=%d", glitch.fOppEndSpan->debugID());
406 }
407 if (!SkIsNaN(glitch.fStartT)) {
408 SkDebugf(" startT=%g", glitch.fStartT);
409 }
410 if (!SkIsNaN(glitch.fEndT)) {
411 SkDebugf(" endT=%g", glitch.fEndT);
412 }
413 if (glitch.fOppSegment) {
414 SkDebugf(" segment=%d", glitch.fOppSegment->debugID());
415 }
416 if (!SkIsNaN(glitch.fOppStartT)) {
417 SkDebugf(" oppStartT=%g", glitch.fOppStartT);
418 }
419 if (!SkIsNaN(glitch.fOppEndT)) {
420 SkDebugf(" oppEndT=%g", glitch.fOppEndT);
421 }
422 if (!SkIsNaN(glitch.fPt.fX) || !SkIsNaN(glitch.fPt.fY)) {
423 SkDebugf(" pt=%g,%g", glitch.fPt.fX, glitch.fPt.fY);
424 }
425 DumpGlitchType(glitch.fType);
426 SkDebugf("\n");
427 }
428 #if DEBUG_COINCIDENCE
429 contourList->globalState()->debugSetCheckHealth(false);
430 #endif
431 #if 01 && DEBUG_ACTIVE_SPANS
432 // SkDebugf("active after %s:\n", id);
433 ShowActiveSpans(contourList);
434 #endif
435 #endif
436 }
437 #endif
438
439 #if DEBUG_COIN
440 void SkPathOpsDebug::DumpGlitchType(GlitchType glitchType) {
441 switch (glitchType) {
442 case kAddCorruptCoin_Glitch: SkDebugf(" AddCorruptCoin"); break;
443 case kAddExpandedCoin_Glitch: SkDebugf(" AddExpandedCoin"); break;
444 case kAddExpandedFail_Glitch: SkDebugf(" AddExpandedFail"); break;
445 case kAddIfCollapsed_Glitch: SkDebugf(" AddIfCollapsed"); break;
446 case kAddIfMissingCoin_Glitch: SkDebugf(" AddIfMissingCoin"); break;
447 case kAddMissingCoin_Glitch: SkDebugf(" AddMissingCoin"); break;
448 case kAddMissingExtend_Glitch: SkDebugf(" AddMissingExtend"); break;
449 case kAddOrOverlap_Glitch: SkDebugf(" AAddOrOverlap"); break;
450 case kCollapsedCoin_Glitch: SkDebugf(" CollapsedCoin"); break;
451 case kCollapsedDone_Glitch: SkDebugf(" CollapsedDone"); break;
452 case kCollapsedOppValue_Glitch: SkDebugf(" CollapsedOppValue"); break;
453 case kCollapsedSpan_Glitch: SkDebugf(" CollapsedSpan"); break;
454 case kCollapsedWindValue_Glitch: SkDebugf(" CollapsedWindValue"); break;
455 case kCorrectEnd_Glitch: SkDebugf(" CorrectEnd"); break;
456 case kDeletedCoin_Glitch: SkDebugf(" DeletedCoin"); break;
457 case kExpandCoin_Glitch: SkDebugf(" ExpandCoin"); break;
458 case kFail_Glitch: SkDebugf(" Fail"); break;
459 case kMarkCoinEnd_Glitch: SkDebugf(" MarkCoinEnd"); break;
460 case kMarkCoinInsert_Glitch: SkDebugf(" MarkCoinInsert"); break;
461 case kMarkCoinMissing_Glitch: SkDebugf(" MarkCoinMissing"); break;
462 case kMarkCoinStart_Glitch: SkDebugf(" MarkCoinStart"); break;
463 case kMergeMatches_Glitch: SkDebugf(" MergeMatches"); break;
464 case kMissingCoin_Glitch: SkDebugf(" MissingCoin"); break;
465 case kMissingDone_Glitch: SkDebugf(" MissingDone"); break;
466 case kMissingIntersection_Glitch: SkDebugf(" MissingIntersection"); break;
467 case kMoveMultiple_Glitch: SkDebugf(" MoveMultiple"); break;
468 case kMoveNearbyClearAll_Glitch: SkDebugf(" MoveNearbyClearAll"); break;
469 case kMoveNearbyClearAll2_Glitch: SkDebugf(" MoveNearbyClearAll2"); break;
470 case kMoveNearbyMerge_Glitch: SkDebugf(" MoveNearbyMerge"); break;
471 case kMoveNearbyMergeFinal_Glitch: SkDebugf(" MoveNearbyMergeFinal"); break;
472 case kMoveNearbyRelease_Glitch: SkDebugf(" MoveNearbyRelease"); break;
473 case kMoveNearbyReleaseFinal_Glitch: SkDebugf(" MoveNearbyReleaseFinal"); break;
474 case kReleasedSpan_Glitch: SkDebugf(" ReleasedSpan"); break;
475 case kReturnFalse_Glitch: SkDebugf(" ReturnFalse"); break;
476 case kUnaligned_Glitch: SkDebugf(" Unaligned"); break;
477 case kUnalignedHead_Glitch: SkDebugf(" UnalignedHead"); break;
478 case kUnalignedTail_Glitch: SkDebugf(" UnalignedTail"); break;
479 case kUninitialized_Glitch: break;
480 default: SkASSERT(0);
481 }
482 }
483 #endif
484
485 #if defined SK_DEBUG || !FORCE_RELEASE
486 void SkPathOpsDebug::MathematicaIze(char* str, size_t bufferLen) {
487 size_t len = strlen(str);
488 bool num = false;
489 for (size_t idx = 0; idx < len; ++idx) {
490 if (num && str[idx] == 'e') {
491 if (len + 2 >= bufferLen) {
492 return;
493 }
494 memmove(&str[idx + 2], &str[idx + 1], len - idx);
495 str[idx] = '*';
496 str[idx + 1] = '^';
497 ++len;
498 }
499 num = str[idx] >= '0' && str[idx] <= '9';
500 }
501 }
502
503 bool SkPathOpsDebug::ValidWind(int wind) {
504 return wind > SK_MinS32 + 0xFFFF && wind < SK_MaxS32 - 0xFFFF;
505 }
506
507 void SkPathOpsDebug::WindingPrintf(int wind) {
508 if (wind == SK_MinS32) {
509 SkDebugf("?");
510 } else {
511 SkDebugf("%d", wind);
512 }
513 }
514 #endif // defined SK_DEBUG || !FORCE_RELEASE
515
516
517 static void show_function_header(const char* functionName) {
518 SkDebugf("\nstatic void %s(skiatest::Reporter* reporter, const char* filename) {\n", functionName);
519 if (strcmp("skphealth_com76", functionName) == 0) {
520 SkDebugf("found it\n");
521 }
522 }
523
524 static const char* gOpStrs[] = {
525 "kDifference_SkPathOp",
526 "kIntersect_SkPathOp",
527 "kUnion_SkPathOp",
528 "kXOR_PathOp",
529 "kReverseDifference_SkPathOp",
530 };
531
532 const char* SkPathOpsDebug::OpStr(SkPathOp op) {
533 return gOpStrs[op];
534 }
535
536 static void show_op(SkPathOp op, const char* pathOne, const char* pathTwo) {
537 SkDebugf(" testPathOp(reporter, %s, %s, %s, filename);\n", pathOne, pathTwo, gOpStrs[op]);
538 SkDebugf("}\n");
539 }
540
541 void SkPathOpsDebug::ShowPath(const SkPath& a, const SkPath& b, SkPathOp shapeOp,
542 const char* testName) {
543 static SkMutex& mutex = *(new SkMutex);
544
545 SkAutoMutexExclusive ac(mutex);
546 show_function_header(testName);
547 ShowOnePath(a, "path", true);
548 ShowOnePath(b, "pathB", true);
549 show_op(shapeOp, "path", "pathB");
550 }
551
552 #if DEBUG_COIN
553
554 void SkOpGlobalState::debugAddToGlobalCoinDicts() {
555 static SkMutex& mutex = *(new SkMutex);
556 SkAutoMutexExclusive ac(mutex);
557 SkPathOpsDebug::gCoinSumChangedDict.add(fCoinChangedDict);
558 SkPathOpsDebug::gCoinSumVisitedDict.add(fCoinVisitedDict);
559 }
560
561 #endif
562
563 #if DEBUG_T_SECT_LOOP_COUNT
564 void SkOpGlobalState::debugAddLoopCount(SkIntersections* i, const SkIntersectionHelper& wt,
565 const SkIntersectionHelper& wn) {
566 for (int index = 0; index < (int) std::size(fDebugLoopCount); ++index) {
567 SkIntersections::DebugLoop looper = (SkIntersections::DebugLoop) index;
568 if (fDebugLoopCount[index] >= i->debugLoopCount(looper)) {
569 continue;
570 }
571 fDebugLoopCount[index] = i->debugLoopCount(looper);
572 fDebugWorstVerb[index * 2] = wt.segment()->verb();
573 fDebugWorstVerb[index * 2 + 1] = wn.segment()->verb();
574 sk_bzero(&fDebugWorstPts[index * 8], sizeof(SkPoint) * 8);
575 memcpy(&fDebugWorstPts[index * 2 * 4], wt.pts(),
576 (SkPathOpsVerbToPoints(wt.segment()->verb()) + 1) * sizeof(SkPoint));
577 memcpy(&fDebugWorstPts[(index * 2 + 1) * 4], wn.pts(),
578 (SkPathOpsVerbToPoints(wn.segment()->verb()) + 1) * sizeof(SkPoint));
579 fDebugWorstWeight[index * 2] = wt.weight();
580 fDebugWorstWeight[index * 2 + 1] = wn.weight();
581 }
582 i->debugResetLoopCount();
583 }
584
585 void SkOpGlobalState::debugDoYourWorst(SkOpGlobalState* local) {
586 for (int index = 0; index < (int) std::size(fDebugLoopCount); ++index) {
587 if (fDebugLoopCount[index] >= local->fDebugLoopCount[index]) {
588 continue;
589 }
590 fDebugLoopCount[index] = local->fDebugLoopCount[index];
591 fDebugWorstVerb[index * 2] = local->fDebugWorstVerb[index * 2];
592 fDebugWorstVerb[index * 2 + 1] = local->fDebugWorstVerb[index * 2 + 1];
593 memcpy(&fDebugWorstPts[index * 2 * 4], &local->fDebugWorstPts[index * 2 * 4],
594 sizeof(SkPoint) * 8);
595 fDebugWorstWeight[index * 2] = local->fDebugWorstWeight[index * 2];
596 fDebugWorstWeight[index * 2 + 1] = local->fDebugWorstWeight[index * 2 + 1];
597 }
598 local->debugResetLoopCounts();
599 }
600
601 static void dump_curve(SkPath::Verb verb, const SkPoint& pts, float weight) {
602 if (!verb) {
603 return;
604 }
605 const char* verbs[] = { "", "line", "quad", "conic", "cubic" };
606 SkDebugf("%s: {{", verbs[verb]);
607 int ptCount = SkPathOpsVerbToPoints(verb);
608 for (int index = 0; index <= ptCount; ++index) {
609 SkDPoint::Dump((&pts)[index]);
610 if (index < ptCount - 1) {
611 SkDebugf(", ");
612 }
613 }
614 SkDebugf("}");
615 if (weight != 1) {
616 SkDebugf(", ");
617 if (weight == floorf(weight)) {
618 SkDebugf("%.0f", weight);
619 } else {
620 SkDebugf("%1.9gf", weight);
621 }
622 }
623 SkDebugf("}\n");
624 }
625
626 void SkOpGlobalState::debugLoopReport() {
627 const char* loops[] = { "iterations", "coinChecks", "perpCalcs" };
628 SkDebugf("\n");
629 for (int index = 0; index < (int) std::size(fDebugLoopCount); ++index) {
630 SkDebugf("%s: %d\n", loops[index], fDebugLoopCount[index]);
631 dump_curve(fDebugWorstVerb[index * 2], fDebugWorstPts[index * 2 * 4],
632 fDebugWorstWeight[index * 2]);
633 dump_curve(fDebugWorstVerb[index * 2 + 1], fDebugWorstPts[(index * 2 + 1) * 4],
634 fDebugWorstWeight[index * 2 + 1]);
635 }
636 }
637
638 void SkOpGlobalState::debugResetLoopCounts() {
639 sk_bzero(fDebugLoopCount, sizeof(fDebugLoopCount));
640 sk_bzero(fDebugWorstVerb, sizeof(fDebugWorstVerb));
641 sk_bzero(fDebugWorstPts, sizeof(fDebugWorstPts));
642 sk_bzero(fDebugWorstWeight, sizeof(fDebugWorstWeight));
643 }
644 #endif
645
646 bool SkOpGlobalState::DebugRunFail() {
647 return SkPathOpsDebug::gRunFail;
648 }
649
650 // this is const so it can be called by const methods that overwise don't alter state
651 #if DEBUG_VALIDATE || DEBUG_COIN
652 void SkOpGlobalState::debugSetPhase(const char* funcName DEBUG_COIN_DECLARE_PARAMS()) const {
653 auto writable = const_cast<SkOpGlobalState*>(this);
654 #if DEBUG_VALIDATE
655 writable->setPhase(phase);
656 #endif
657 #if DEBUG_COIN
658 SkPathOpsDebug::CoinDictEntry* entry = &writable->fCoinDictEntry;
659 writable->fPreviousFuncName = entry->fFunctionName;
660 entry->fIteration = iteration;
661 entry->fLineNumber = lineNo;
662 entry->fGlitchType = SkPathOpsDebug::kUninitialized_Glitch;
663 entry->fFunctionName = funcName;
664 writable->fCoinVisitedDict.add(*entry);
665 writable->debugAddToCoinChangedDict();
666 #endif
667 }
668 #endif
669
670 #if DEBUG_T_SECT_LOOP_COUNT
671 void SkIntersections::debugBumpLoopCount(DebugLoop index) {
672 fDebugLoopCount[index]++;
673 }
674
675 int SkIntersections::debugLoopCount(DebugLoop index) const {
676 return fDebugLoopCount[index];
677 }
678
679 void SkIntersections::debugResetLoopCount() {
680 sk_bzero(fDebugLoopCount, sizeof(fDebugLoopCount));
681 }
682 #endif
683
684 SkDCubic SkDQuad::debugToCubic() const {
685 SkDCubic cubic;
686 cubic[0] = fPts[0];
687 cubic[2] = fPts[1];
688 cubic[3] = fPts[2];
689 cubic[1].fX = (cubic[0].fX + cubic[2].fX * 2) / 3;
690 cubic[1].fY = (cubic[0].fY + cubic[2].fY * 2) / 3;
691 cubic[2].fX = (cubic[3].fX + cubic[2].fX * 2) / 3;
692 cubic[2].fY = (cubic[3].fY + cubic[2].fY * 2) / 3;
693 return cubic;
694 }
695
696 void SkDQuad::debugSet(const SkDPoint* pts) {
697 memcpy(fPts, pts, sizeof(fPts));
698 SkDEBUGCODE(fDebugGlobalState = nullptr);
699 }
700
701 void SkDCubic::debugSet(const SkDPoint* pts) {
702 memcpy(fPts, pts, sizeof(fPts));
703 SkDEBUGCODE(fDebugGlobalState = nullptr);
704 }
705
706 void SkDConic::debugSet(const SkDPoint* pts, SkScalar weight) {
707 fPts.debugSet(pts);
708 fWeight = weight;
709 }
710
711 void SkDRect::debugInit() {
712 fLeft = fTop = fRight = fBottom = SK_ScalarNaN;
713 }
714
715 #if DEBUG_COIN
716 // commented-out lines keep this in sync with addT()
717 const SkOpPtT* SkOpSegment::debugAddT(double t, SkPathOpsDebug::GlitchLog* log) const {
718 debugValidate();
719 SkPoint pt = this->ptAtT(t);
720 const SkOpSpanBase* span = &fHead;
721 do {
722 const SkOpPtT* result = span->ptT();
723 if (t == result->fT || this->match(result, this, t, pt)) {
724 // span->bumpSpanAdds();
725 return result;
726 }
727 if (t < result->fT) {
728 const SkOpSpan* prev = result->span()->prev();
729 FAIL_WITH_NULL_IF(!prev, span);
730 // marks in global state that new op span has been allocated
731 this->globalState()->setAllocatedOpSpan();
732 // span->init(this, prev, t, pt);
733 this->debugValidate();
734 // #if DEBUG_ADD_T
735 // SkDebugf("%s insert t=%1.9g segID=%d spanID=%d\n", __FUNCTION__, t,
736 // span->segment()->debugID(), span->debugID());
737 // #endif
738 // span->bumpSpanAdds();
739 return nullptr;
740 }
741 FAIL_WITH_NULL_IF(span != &fTail, span);
742 } while ((span = span->upCast()->next()));
743 SkASSERT(0);
744 return nullptr; // we never get here, but need this to satisfy compiler
745 }
746 #endif
747
748 #if DEBUG_ANGLE
749 void SkOpSegment::debugCheckAngleCoin() const {
750 const SkOpSpanBase* base = &fHead;
751 const SkOpSpan* span;
752 do {
753 const SkOpAngle* angle = base->fromAngle();
754 if (angle && angle->debugCheckCoincidence()) {
755 angle->debugCheckNearCoincidence();
756 }
757 if (base->final()) {
758 break;
759 }
760 span = base->upCast();
761 angle = span->toAngle();
762 if (angle && angle->debugCheckCoincidence()) {
763 angle->debugCheckNearCoincidence();
764 }
765 } while ((base = span->next()));
766 }
767 #endif
768
769 #if DEBUG_COIN
770 // this mimics the order of the checks in handle coincidence
771 void SkOpSegment::debugCheckHealth(SkPathOpsDebug::GlitchLog* glitches) const {
772 debugMoveMultiples(glitches);
773 debugMoveNearby(glitches);
774 debugMissingCoincidence(glitches);
775 }
776
777 // commented-out lines keep this in sync with clearAll()
778 void SkOpSegment::debugClearAll(SkPathOpsDebug::GlitchLog* glitches) const {
779 const SkOpSpan* span = &fHead;
780 do {
781 this->debugClearOne(span, glitches);
782 } while ((span = span->next()->upCastable()));
783 this->globalState()->coincidence()->debugRelease(glitches, this);
784 }
785
786 // commented-out lines keep this in sync with clearOne()
787 void SkOpSegment::debugClearOne(const SkOpSpan* span, SkPathOpsDebug::GlitchLog* glitches) const {
788 if (span->windValue()) glitches->record(SkPathOpsDebug::kCollapsedWindValue_Glitch, span);
789 if (span->oppValue()) glitches->record(SkPathOpsDebug::kCollapsedOppValue_Glitch, span);
790 if (!span->done()) glitches->record(SkPathOpsDebug::kCollapsedDone_Glitch, span);
791 }
792 #endif
793
794 SkOpAngle* SkOpSegment::debugLastAngle() {
795 SkOpAngle* result = nullptr;
796 SkOpSpan* span = this->head();
797 do {
798 if (span->toAngle()) {
799 SkASSERT(!result);
800 result = span->toAngle();
801 }
802 } while ((span = span->next()->upCastable()));
803 SkASSERT(result);
804 return result;
805 }
806
807 #if DEBUG_COIN
808 // commented-out lines keep this in sync with ClearVisited
809 void SkOpSegment::DebugClearVisited(const SkOpSpanBase* span) {
810 // reset visited flag back to false
811 do {
812 const SkOpPtT* ptT = span->ptT(), * stopPtT = ptT;
813 while ((ptT = ptT->next()) != stopPtT) {
814 const SkOpSegment* opp = ptT->segment();
815 opp->resetDebugVisited();
816 }
817 } while (!span->final() && (span = span->upCast()->next()));
818 }
819 #endif
820
821 #if DEBUG_COIN
822 // commented-out lines keep this in sync with missingCoincidence()
823 // look for pairs of undetected coincident curves
824 // assumes that segments going in have visited flag clear
825 // Even though pairs of curves correct detect coincident runs, a run may be missed
826 // if the coincidence is a product of multiple intersections. For instance, given
827 // curves A, B, and C:
828 // A-B intersect at a point 1; A-C and B-C intersect at point 2, so near
829 // the end of C that the intersection is replaced with the end of C.
830 // Even though A-B correctly do not detect an intersection at point 2,
831 // the resulting run from point 1 to point 2 is coincident on A and B.
832 void SkOpSegment::debugMissingCoincidence(SkPathOpsDebug::GlitchLog* log) const {
833 if (this->done()) {
834 return;
835 }
836 const SkOpSpan* prior = nullptr;
837 const SkOpSpanBase* spanBase = &fHead;
838 // bool result = false;
839 do {
840 const SkOpPtT* ptT = spanBase->ptT(), * spanStopPtT = ptT;
841 SkASSERT(ptT->span() == spanBase);
842 while ((ptT = ptT->next()) != spanStopPtT) {
843 if (ptT->deleted()) {
844 continue;
845 }
846 const SkOpSegment* opp = ptT->span()->segment();
847 if (opp->done()) {
848 continue;
849 }
850 // when opp is encounted the 1st time, continue; on 2nd encounter, look for coincidence
851 if (!opp->debugVisited()) {
852 continue;
853 }
854 if (spanBase == &fHead) {
855 continue;
856 }
857 if (ptT->segment() == this) {
858 continue;
859 }
860 const SkOpSpan* span = spanBase->upCastable();
861 // FIXME?: this assumes that if the opposite segment is coincident then no more
862 // coincidence needs to be detected. This may not be true.
863 if (span && span->segment() != opp && span->containsCoincidence(opp)) { // debug has additional condition since it may be called before inner duplicate points have been deleted
864 continue;
865 }
866 if (spanBase->segment() != opp && spanBase->containsCoinEnd(opp)) { // debug has additional condition since it may be called before inner duplicate points have been deleted
867 continue;
868 }
869 const SkOpPtT* priorPtT = nullptr, * priorStopPtT;
870 // find prior span containing opp segment
871 const SkOpSegment* priorOpp = nullptr;
872 const SkOpSpan* priorTest = spanBase->prev();
873 while (!priorOpp && priorTest) {
874 priorStopPtT = priorPtT = priorTest->ptT();
875 while ((priorPtT = priorPtT->next()) != priorStopPtT) {
876 if (priorPtT->deleted()) {
877 continue;
878 }
879 const SkOpSegment* segment = priorPtT->span()->segment();
880 if (segment == opp) {
881 prior = priorTest;
882 priorOpp = opp;
883 break;
884 }
885 }
886 priorTest = priorTest->prev();
887 }
888 if (!priorOpp) {
889 continue;
890 }
891 if (priorPtT == ptT) {
892 continue;
893 }
894 const SkOpPtT* oppStart = prior->ptT();
895 const SkOpPtT* oppEnd = spanBase->ptT();
896 bool swapped = priorPtT->fT > ptT->fT;
897 if (swapped) {
898 using std::swap;
899 swap(priorPtT, ptT);
900 swap(oppStart, oppEnd);
901 }
902 const SkOpCoincidence* coincidence = this->globalState()->coincidence();
903 const SkOpPtT* rootPriorPtT = priorPtT->span()->ptT();
904 const SkOpPtT* rootPtT = ptT->span()->ptT();
905 const SkOpPtT* rootOppStart = oppStart->span()->ptT();
906 const SkOpPtT* rootOppEnd = oppEnd->span()->ptT();
907 if (coincidence->contains(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd)) {
908 goto swapBack;
909 }
910 if (testForCoincidence(rootPriorPtT, rootPtT, prior, spanBase, opp)) {
911 // mark coincidence
912 #if DEBUG_COINCIDENCE_VERBOSE
913 // SkDebugf("%s coinSpan=%d endSpan=%d oppSpan=%d oppEndSpan=%d\n", __FUNCTION__,
914 // rootPriorPtT->debugID(), rootPtT->debugID(), rootOppStart->debugID(),
915 // rootOppEnd->debugID());
916 #endif
917 log->record(SkPathOpsDebug::kMissingCoin_Glitch, priorPtT, ptT, oppStart, oppEnd);
918 // coincidences->add(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd);
919 // }
920 #if DEBUG_COINCIDENCE
921 // SkASSERT(coincidences->contains(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd);
922 #endif
923 // result = true;
924 }
925 swapBack:
926 if (swapped) {
927 using std::swap;
928 swap(priorPtT, ptT);
929 }
930 }
931 } while ((spanBase = spanBase->final() ? nullptr : spanBase->upCast()->next()));
932 DebugClearVisited(&fHead);
933 return;
934 }
935
936 // commented-out lines keep this in sync with moveMultiples()
937 // if a span has more than one intersection, merge the other segments' span as needed
938 void SkOpSegment::debugMoveMultiples(SkPathOpsDebug::GlitchLog* glitches) const {
939 debugValidate();
940 const SkOpSpanBase* test = &fHead;
941 do {
942 int addCount = test->spanAddsCount();
943 // SkASSERT(addCount >= 1);
944 if (addCount <= 1) {
945 continue;
946 }
947 const SkOpPtT* startPtT = test->ptT();
948 const SkOpPtT* testPtT = startPtT;
949 do { // iterate through all spans associated with start
950 const SkOpSpanBase* oppSpan = testPtT->span();
951 if (oppSpan->spanAddsCount() == addCount) {
952 continue;
953 }
954 if (oppSpan->deleted()) {
955 continue;
956 }
957 const SkOpSegment* oppSegment = oppSpan->segment();
958 if (oppSegment == this) {
959 continue;
960 }
961 // find range of spans to consider merging
962 const SkOpSpanBase* oppPrev = oppSpan;
963 const SkOpSpanBase* oppFirst = oppSpan;
964 while ((oppPrev = oppPrev->prev())) {
965 if (!roughly_equal(oppPrev->t(), oppSpan->t())) {
966 break;
967 }
968 if (oppPrev->spanAddsCount() == addCount) {
969 continue;
970 }
971 if (oppPrev->deleted()) {
972 continue;
973 }
974 oppFirst = oppPrev;
975 }
976 const SkOpSpanBase* oppNext = oppSpan;
977 const SkOpSpanBase* oppLast = oppSpan;
978 while ((oppNext = oppNext->final() ? nullptr : oppNext->upCast()->next())) {
979 if (!roughly_equal(oppNext->t(), oppSpan->t())) {
980 break;
981 }
982 if (oppNext->spanAddsCount() == addCount) {
983 continue;
984 }
985 if (oppNext->deleted()) {
986 continue;
987 }
988 oppLast = oppNext;
989 }
990 if (oppFirst == oppLast) {
991 continue;
992 }
993 const SkOpSpanBase* oppTest = oppFirst;
994 do {
995 if (oppTest == oppSpan) {
996 continue;
997 }
998 // check to see if the candidate meets specific criteria:
999 // it contains spans of segments in test's loop but not including 'this'
1000 const SkOpPtT* oppStartPtT = oppTest->ptT();
1001 const SkOpPtT* oppPtT = oppStartPtT;
1002 while ((oppPtT = oppPtT->next()) != oppStartPtT) {
1003 const SkOpSegment* oppPtTSegment = oppPtT->segment();
1004 if (oppPtTSegment == this) {
1005 goto tryNextSpan;
1006 }
1007 const SkOpPtT* matchPtT = startPtT;
1008 do {
1009 if (matchPtT->segment() == oppPtTSegment) {
1010 goto foundMatch;
1011 }
1012 } while ((matchPtT = matchPtT->next()) != startPtT);
1013 goto tryNextSpan;
1014 foundMatch: // merge oppTest and oppSpan
1015 oppSegment->debugValidate();
1016 oppTest->debugMergeMatches(glitches, oppSpan);
1017 oppTest->debugAddOpp(glitches, oppSpan);
1018 oppSegment->debugValidate();
1019 goto checkNextSpan;
1020 }
1021 tryNextSpan:
1022 ;
1023 } while (oppTest != oppLast && (oppTest = oppTest->upCast()->next()));
1024 } while ((testPtT = testPtT->next()) != startPtT);
1025 checkNextSpan:
1026 ;
1027 } while ((test = test->final() ? nullptr : test->upCast()->next()));
1028 debugValidate();
1029 return;
1030 }
1031
1032 // commented-out lines keep this in sync with moveNearby()
1033 // Move nearby t values and pts so they all hang off the same span. Alignment happens later.
1034 void SkOpSegment::debugMoveNearby(SkPathOpsDebug::GlitchLog* glitches) const {
1035 debugValidate();
1036 // release undeleted spans pointing to this seg that are linked to the primary span
1037 const SkOpSpanBase* spanBase = &fHead;
1038 do {
1039 const SkOpPtT* ptT = spanBase->ptT();
1040 const SkOpPtT* headPtT = ptT;
1041 while ((ptT = ptT->next()) != headPtT) {
1042 const SkOpSpanBase* test = ptT->span();
1043 if (ptT->segment() == this && !ptT->deleted() && test != spanBase
1044 && test->ptT() == ptT) {
1045 if (test->final()) {
1046 if (spanBase == &fHead) {
1047 glitches->record(SkPathOpsDebug::kMoveNearbyClearAll_Glitch, this);
1048 // return;
1049 }
1050 glitches->record(SkPathOpsDebug::kMoveNearbyReleaseFinal_Glitch, spanBase, ptT);
1051 } else if (test->prev()) {
1052 glitches->record(SkPathOpsDebug::kMoveNearbyRelease_Glitch, test, headPtT);
1053 }
1054 // break;
1055 }
1056 }
1057 spanBase = spanBase->upCast()->next();
1058 } while (!spanBase->final());
1059
1060 // This loop looks for adjacent spans which are near by
1061 spanBase = &fHead;
1062 do { // iterate through all spans associated with start
1063 const SkOpSpanBase* test = spanBase->upCast()->next();
1064 bool found;
1065 if (!this->spansNearby(spanBase, test, &found)) {
1066 glitches->record(SkPathOpsDebug::kMoveNearbyMergeFinal_Glitch, test);
1067 }
1068 if (found) {
1069 if (test->final()) {
1070 if (spanBase->prev()) {
1071 glitches->record(SkPathOpsDebug::kMoveNearbyMergeFinal_Glitch, test);
1072 } else {
1073 glitches->record(SkPathOpsDebug::kMoveNearbyClearAll2_Glitch, this);
1074 // return
1075 }
1076 } else {
1077 glitches->record(SkPathOpsDebug::kMoveNearbyMerge_Glitch, spanBase);
1078 }
1079 }
1080 spanBase = test;
1081 } while (!spanBase->final());
1082 debugValidate();
1083 }
1084 #endif
1085
1086 void SkOpSegment::debugReset() {
1087 this->init(this->fPts, this->fWeight, this->contour(), this->verb());
1088 }
1089
1090 #if DEBUG_COINCIDENCE_ORDER
1091 void SkOpSegment::debugSetCoinT(int index, SkScalar t) const {
1092 if (fDebugBaseMax < 0 || fDebugBaseIndex == index) {
1093 fDebugBaseIndex = index;
1094 fDebugBaseMin = std::min(t, fDebugBaseMin);
1095 fDebugBaseMax = std::max(t, fDebugBaseMax);
1096 return;
1097 }
1098 SkASSERT(fDebugBaseMin >= t || t >= fDebugBaseMax);
1099 if (fDebugLastMax < 0 || fDebugLastIndex == index) {
1100 fDebugLastIndex = index;
1101 fDebugLastMin = std::min(t, fDebugLastMin);
1102 fDebugLastMax = std::max(t, fDebugLastMax);
1103 return;
1104 }
1105 SkASSERT(fDebugLastMin >= t || t >= fDebugLastMax);
1106 SkASSERT((t - fDebugBaseMin > 0) == (fDebugLastMin - fDebugBaseMin > 0));
1107 }
1108 #endif
1109
1110 #if DEBUG_ACTIVE_SPANS
1111 void SkOpSegment::debugShowActiveSpans(SkString* str) const {
1112 debugValidate();
1113 if (done()) {
1114 return;
1115 }
1116 int lastId = -1;
1117 double lastT = -1;
1118 const SkOpSpan* span = &fHead;
1119 do {
1120 if (span->done()) {
1121 continue;
1122 }
1123 if (lastId == this->debugID() && lastT == span->t()) {
1124 continue;
1125 }
1126 lastId = this->debugID();
1127 lastT = span->t();
1128 str->appendf("%s id=%d", __FUNCTION__, this->debugID());
1129 // since endpoints may have be adjusted, show actual computed curves
1130 SkDCurve curvePart;
1131 this->subDivide(span, span->next(), &curvePart);
1132 const SkDPoint* pts = curvePart.fCubic.fPts;
1133 str->appendf(" (%1.9g,%1.9g", pts[0].fX, pts[0].fY);
1134 for (int vIndex = 1; vIndex <= SkPathOpsVerbToPoints(fVerb); ++vIndex) {
1135 str->appendf(" %1.9g,%1.9g", pts[vIndex].fX, pts[vIndex].fY);
1136 }
1137 if (SkPath::kConic_Verb == fVerb) {
1138 str->appendf(" %1.9gf", curvePart.fConic.fWeight);
1139 }
1140 str->appendf(") t=%1.9g tEnd=%1.9g", span->t(), span->next()->t());
1141 if (span->windSum() == SK_MinS32) {
1142 str->appendf(" windSum=?");
1143 } else {
1144 str->appendf(" windSum=%d", span->windSum());
1145 }
1146 if (span->oppValue() && span->oppSum() == SK_MinS32) {
1147 str->appendf(" oppSum=?");
1148 } else if (span->oppValue() || span->oppSum() != SK_MinS32) {
1149 str->appendf(" oppSum=%d", span->oppSum());
1150 }
1151 str->appendf(" windValue=%d", span->windValue());
1152 if (span->oppValue() || span->oppSum() != SK_MinS32) {
1153 str->appendf(" oppValue=%d", span->oppValue());
1154 }
1155 str->appendf("\n");
1156 } while ((span = span->next()->upCastable()));
1157 }
1158 #endif
1159
1160 #if DEBUG_MARK_DONE
1161 void SkOpSegment::debugShowNewWinding(const char* fun, const SkOpSpan* span, int winding) {
1162 const SkPoint& pt = span->ptT()->fPt;
1163 SkDebugf("%s id=%d", fun, this->debugID());
1164 SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY);
1165 for (int vIndex = 1; vIndex <= SkPathOpsVerbToPoints(fVerb); ++vIndex) {
1166 SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY);
1167 }
1168 SkDebugf(") t=%1.9g [%d] (%1.9g,%1.9g) tEnd=%1.9g newWindSum=",
1169 span->t(), span->debugID(), pt.fX, pt.fY, span->next()->t());
1170 if (winding == SK_MinS32) {
1171 SkDebugf("?");
1172 } else {
1173 SkDebugf("%d", winding);
1174 }
1175 SkDebugf(" windSum=");
1176 if (span->windSum() == SK_MinS32) {
1177 SkDebugf("?");
1178 } else {
1179 SkDebugf("%d", span->windSum());
1180 }
1181 SkDebugf(" windValue=%d\n", span->windValue());
1182 }
1183
1184 void SkOpSegment::debugShowNewWinding(const char* fun, const SkOpSpan* span, int winding,
1185 int oppWinding) {
1186 const SkPoint& pt = span->ptT()->fPt;
1187 SkDebugf("%s id=%d", fun, this->debugID());
1188 SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY);
1189 for (int vIndex = 1; vIndex <= SkPathOpsVerbToPoints(fVerb); ++vIndex) {
1190 SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY);
1191 }
1192 SkDebugf(") t=%1.9g [%d] (%1.9g,%1.9g) tEnd=%1.9g newWindSum=",
1193 span->t(), span->debugID(), pt.fX, pt.fY, span->next()->t());
1194 if (winding == SK_MinS32) {
1195 SkDebugf("?");
1196 } else {
1197 SkDebugf("%d", winding);
1198 }
1199 SkDebugf(" newOppSum=");
1200 if (oppWinding == SK_MinS32) {
1201 SkDebugf("?");
1202 } else {
1203 SkDebugf("%d", oppWinding);
1204 }
1205 SkDebugf(" oppSum=");
1206 if (span->oppSum() == SK_MinS32) {
1207 SkDebugf("?");
1208 } else {
1209 SkDebugf("%d", span->oppSum());
1210 }
1211 SkDebugf(" windSum=");
1212 if (span->windSum() == SK_MinS32) {
1213 SkDebugf("?");
1214 } else {
1215 SkDebugf("%d", span->windSum());
1216 }
1217 SkDebugf(" windValue=%d oppValue=%d\n", span->windValue(), span->oppValue());
1218 }
1219
1220 #endif
1221
1222 // loop looking for a pair of angle parts that are too close to be sorted
1223 /* This is called after other more simple intersection and angle sorting tests have been exhausted.
1224 This should be rarely called -- the test below is thorough and time consuming.
1225 This checks the distance between start points; the distance between
1226 */
1227 #if DEBUG_ANGLE
1228 void SkOpAngle::debugCheckNearCoincidence() const {
1229 const SkOpAngle* test = this;
1230 do {
1231 const SkOpSegment* testSegment = test->segment();
1232 double testStartT = test->start()->t();
1233 SkDPoint testStartPt = testSegment->dPtAtT(testStartT);
1234 double testEndT = test->end()->t();
1235 SkDPoint testEndPt = testSegment->dPtAtT(testEndT);
1236 double testLenSq = testStartPt.distanceSquared(testEndPt);
1237 SkDebugf("%s testLenSq=%1.9g id=%d\n", __FUNCTION__, testLenSq, testSegment->debugID());
1238 double testMidT = (testStartT + testEndT) / 2;
1239 const SkOpAngle* next = test;
1240 while ((next = next->fNext) != this) {
1241 SkOpSegment* nextSegment = next->segment();
1242 double testMidDistSq = testSegment->distSq(testMidT, next);
1243 double testEndDistSq = testSegment->distSq(testEndT, next);
1244 double nextStartT = next->start()->t();
1245 SkDPoint nextStartPt = nextSegment->dPtAtT(nextStartT);
1246 double distSq = testStartPt.distanceSquared(nextStartPt);
1247 double nextEndT = next->end()->t();
1248 double nextMidT = (nextStartT + nextEndT) / 2;
1249 double nextMidDistSq = nextSegment->distSq(nextMidT, test);
1250 double nextEndDistSq = nextSegment->distSq(nextEndT, test);
1251 SkDebugf("%s distSq=%1.9g testId=%d nextId=%d\n", __FUNCTION__, distSq,
1252 testSegment->debugID(), nextSegment->debugID());
1253 SkDebugf("%s testMidDistSq=%1.9g\n", __FUNCTION__, testMidDistSq);
1254 SkDebugf("%s testEndDistSq=%1.9g\n", __FUNCTION__, testEndDistSq);
1255 SkDebugf("%s nextMidDistSq=%1.9g\n", __FUNCTION__, nextMidDistSq);
1256 SkDebugf("%s nextEndDistSq=%1.9g\n", __FUNCTION__, nextEndDistSq);
1257 SkDPoint nextEndPt = nextSegment->dPtAtT(nextEndT);
1258 double nextLenSq = nextStartPt.distanceSquared(nextEndPt);
1259 SkDebugf("%s nextLenSq=%1.9g\n", __FUNCTION__, nextLenSq);
1260 SkDebugf("\n");
1261 }
1262 test = test->fNext;
1263 } while (test->fNext != this);
1264 }
1265 #endif
1266
1267 #if DEBUG_ANGLE
1268 SkString SkOpAngle::debugPart() const {
1269 SkString result;
1270 switch (this->segment()->verb()) {
1271 case SkPath::kLine_Verb:
1272 result.printf(LINE_DEBUG_STR " id=%d", LINE_DEBUG_DATA(fPart.fCurve),
1273 this->segment()->debugID());
1274 break;
1275 case SkPath::kQuad_Verb:
1276 result.printf(QUAD_DEBUG_STR " id=%d", QUAD_DEBUG_DATA(fPart.fCurve),
1277 this->segment()->debugID());
1278 break;
1279 case SkPath::kConic_Verb:
1280 result.printf(CONIC_DEBUG_STR " id=%d",
1281 CONIC_DEBUG_DATA(fPart.fCurve, fPart.fCurve.fConic.fWeight),
1282 this->segment()->debugID());
1283 break;
1284 case SkPath::kCubic_Verb:
1285 result.printf(CUBIC_DEBUG_STR " id=%d", CUBIC_DEBUG_DATA(fPart.fCurve),
1286 this->segment()->debugID());
1287 break;
1288 default:
1289 SkASSERT(0);
1290 }
1291 return result;
1292 }
1293 #endif
1294
1295 #if DEBUG_SORT
1296 void SkOpAngle::debugLoop() const {
1297 const SkOpAngle* first = this;
1298 const SkOpAngle* next = this;
1299 do {
1300 next->dumpOne(true);
1301 SkDebugf("\n");
1302 next = next->fNext;
1303 } while (next && next != first);
1304 next = first;
1305 do {
1306 next->debugValidate();
1307 next = next->fNext;
1308 } while (next && next != first);
1309 }
1310 #endif
1311
1312 void SkOpAngle::debugValidate() const {
1313 #if DEBUG_COINCIDENCE
1314 if (this->globalState()->debugCheckHealth()) {
1315 return;
1316 }
1317 #endif
1318 #if DEBUG_VALIDATE
1319 const SkOpAngle* first = this;
1320 const SkOpAngle* next = this;
1321 int wind = 0;
1322 int opp = 0;
1323 int lastXor = -1;
1324 int lastOppXor = -1;
1325 do {
1326 if (next->unorderable()) {
1327 return;
1328 }
1329 const SkOpSpan* minSpan = next->start()->starter(next->end());
1330 if (minSpan->windValue() == SK_MinS32) {
1331 return;
1332 }
1333 bool op = next->segment()->operand();
1334 bool isXor = next->segment()->isXor();
1335 bool oppXor = next->segment()->oppXor();
1336 SkASSERT(!DEBUG_LIMIT_WIND_SUM || between(0, minSpan->windValue(), DEBUG_LIMIT_WIND_SUM));
1337 SkASSERT(!DEBUG_LIMIT_WIND_SUM
1338 || between(-DEBUG_LIMIT_WIND_SUM, minSpan->oppValue(), DEBUG_LIMIT_WIND_SUM));
1339 bool useXor = op ? oppXor : isXor;
1340 SkASSERT(lastXor == -1 || lastXor == (int) useXor);
1341 lastXor = (int) useXor;
1342 wind += next->debugSign() * (op ? minSpan->oppValue() : minSpan->windValue());
1343 if (useXor) {
1344 wind &= 1;
1345 }
1346 useXor = op ? isXor : oppXor;
1347 SkASSERT(lastOppXor == -1 || lastOppXor == (int) useXor);
1348 lastOppXor = (int) useXor;
1349 opp += next->debugSign() * (op ? minSpan->windValue() : minSpan->oppValue());
1350 if (useXor) {
1351 opp &= 1;
1352 }
1353 next = next->fNext;
1354 } while (next && next != first);
1355 SkASSERT(wind == 0 || !SkPathOpsDebug::gRunFail);
1356 SkASSERT(opp == 0 || !SkPathOpsDebug::gRunFail);
1357 #endif
1358 }
1359
1360 void SkOpAngle::debugValidateNext() const {
1361 #if !FORCE_RELEASE
1362 const SkOpAngle* first = this;
1363 const SkOpAngle* next = first;
1364 SkTDArray<const SkOpAngle*> angles;
1365 do {
1366 // SkASSERT_RELEASE(next->fSegment->debugContains(next));
1367 angles.push_back(next);
1368 next = next->next();
1369 if (next == first) {
1370 break;
1371 }
1372 SkASSERT_RELEASE(!angles.contains(next));
1373 if (!next) {
1374 return;
1375 }
1376 } while (true);
1377 #endif
1378 }
1379
1380 #ifdef SK_DEBUG
1381 void SkCoincidentSpans::debugStartCheck(const SkOpSpanBase* outer, const SkOpSpanBase* over,
1382 const SkOpGlobalState* debugState) const {
1383 SkASSERT(coinPtTEnd()->span() == over || !SkOpGlobalState::DebugRunFail());
1384 SkASSERT(oppPtTEnd()->span() == outer || !SkOpGlobalState::DebugRunFail());
1385 }
1386 #endif
1387
1388 #if DEBUG_COIN
1389 // sets the span's end to the ptT referenced by the previous-next
1390 void SkCoincidentSpans::debugCorrectOneEnd(SkPathOpsDebug::GlitchLog* log,
1391 const SkOpPtT* (SkCoincidentSpans::* getEnd)() const,
1392 void (SkCoincidentSpans::*setEnd)(const SkOpPtT* ptT) const ) const {
1393 const SkOpPtT* origPtT = (this->*getEnd)();
1394 const SkOpSpanBase* origSpan = origPtT->span();
1395 const SkOpSpan* prev = origSpan->prev();
1396 const SkOpPtT* testPtT = prev ? prev->next()->ptT()
1397 : origSpan->upCast()->next()->prev()->ptT();
1398 if (origPtT != testPtT) {
1399 log->record(SkPathOpsDebug::kCorrectEnd_Glitch, this, origPtT, testPtT);
1400 }
1401 }
1402
1403
1404 /* Commented-out lines keep this in sync with correctEnds */
1405 // FIXME: member pointers have fallen out of favor and can be replaced with
1406 // an alternative approach.
1407 // makes all span ends agree with the segment's spans that define them
1408 void SkCoincidentSpans::debugCorrectEnds(SkPathOpsDebug::GlitchLog* log) const {
1409 this->debugCorrectOneEnd(log, &SkCoincidentSpans::coinPtTStart, nullptr);
1410 this->debugCorrectOneEnd(log, &SkCoincidentSpans::coinPtTEnd, nullptr);
1411 this->debugCorrectOneEnd(log, &SkCoincidentSpans::oppPtTStart, nullptr);
1412 this->debugCorrectOneEnd(log, &SkCoincidentSpans::oppPtTEnd, nullptr);
1413 }
1414
1415 /* Commented-out lines keep this in sync with expand */
1416 // expand the range by checking adjacent spans for coincidence
1417 bool SkCoincidentSpans::debugExpand(SkPathOpsDebug::GlitchLog* log) const {
1418 bool expanded = false;
1419 const SkOpSegment* segment = coinPtTStart()->segment();
1420 const SkOpSegment* oppSegment = oppPtTStart()->segment();
1421 do {
1422 const SkOpSpan* start = coinPtTStart()->span()->upCast();
1423 const SkOpSpan* prev = start->prev();
1424 const SkOpPtT* oppPtT;
1425 if (!prev || !(oppPtT = prev->contains(oppSegment))) {
1426 break;
1427 }
1428 double midT = (prev->t() + start->t()) / 2;
1429 if (!segment->isClose(midT, oppSegment)) {
1430 break;
1431 }
1432 if (log) log->record(SkPathOpsDebug::kExpandCoin_Glitch, this, prev->ptT(), oppPtT);
1433 expanded = true;
1434 } while (false); // actual continues while expansion is possible
1435 do {
1436 const SkOpSpanBase* end = coinPtTEnd()->span();
1437 SkOpSpanBase* next = end->final() ? nullptr : end->upCast()->next();
1438 if (next && next->deleted()) {
1439 break;
1440 }
1441 const SkOpPtT* oppPtT;
1442 if (!next || !(oppPtT = next->contains(oppSegment))) {
1443 break;
1444 }
1445 double midT = (end->t() + next->t()) / 2;
1446 if (!segment->isClose(midT, oppSegment)) {
1447 break;
1448 }
1449 if (log) log->record(SkPathOpsDebug::kExpandCoin_Glitch, this, next->ptT(), oppPtT);
1450 expanded = true;
1451 } while (false); // actual continues while expansion is possible
1452 return expanded;
1453 }
1454
1455 // description below
1456 void SkOpCoincidence::debugAddEndMovedSpans(SkPathOpsDebug::GlitchLog* log, const SkOpSpan* base, const SkOpSpanBase* testSpan) const {
1457 const SkOpPtT* testPtT = testSpan->ptT();
1458 const SkOpPtT* stopPtT = testPtT;
1459 const SkOpSegment* baseSeg = base->segment();
1460 while ((testPtT = testPtT->next()) != stopPtT) {
1461 const SkOpSegment* testSeg = testPtT->segment();
1462 if (testPtT->deleted()) {
1463 continue;
1464 }
1465 if (testSeg == baseSeg) {
1466 continue;
1467 }
1468 if (testPtT->span()->ptT() != testPtT) {
1469 continue;
1470 }
1471 if (this->contains(baseSeg, testSeg, testPtT->fT)) {
1472 continue;
1473 }
1474 // intersect perp with base->ptT() with testPtT->segment()
1475 SkDVector dxdy = baseSeg->dSlopeAtT(base->t());
1476 const SkPoint& pt = base->pt();
1477 SkDLine ray = {{{pt.fX, pt.fY}, {pt.fX + dxdy.fY, pt.fY - dxdy.fX}}};
1478 SkIntersections i;
1479 (*CurveIntersectRay[testSeg->verb()])(testSeg->pts(), testSeg->weight(), ray, &i);
1480 for (int index = 0; index < i.used(); ++index) {
1481 double t = i[0][index];
1482 if (!between(0, t, 1)) {
1483 continue;
1484 }
1485 SkDPoint oppPt = i.pt(index);
1486 if (!oppPt.approximatelyEqual(pt)) {
1487 continue;
1488 }
1489 SkOpSegment* writableSeg = const_cast<SkOpSegment*>(testSeg);
1490 SkOpPtT* oppStart = writableSeg->addT(t);
1491 if (oppStart == testPtT) {
1492 continue;
1493 }
1494 SkOpSpan* writableBase = const_cast<SkOpSpan*>(base);
1495 oppStart->span()->addOpp(writableBase);
1496 if (oppStart->deleted()) {
1497 continue;
1498 }
1499 SkOpSegment* coinSeg = base->segment();
1500 SkOpSegment* oppSeg = oppStart->segment();
1501 double coinTs, coinTe, oppTs, oppTe;
1502 if (Ordered(coinSeg, oppSeg)) {
1503 coinTs = base->t();
1504 coinTe = testSpan->t();
1505 oppTs = oppStart->fT;
1506 oppTe = testPtT->fT;
1507 } else {
1508 using std::swap;
1509 swap(coinSeg, oppSeg);
1510 coinTs = oppStart->fT;
1511 coinTe = testPtT->fT;
1512 oppTs = base->t();
1513 oppTe = testSpan->t();
1514 }
1515 if (coinTs > coinTe) {
1516 using std::swap;
1517 swap(coinTs, coinTe);
1518 swap(oppTs, oppTe);
1519 }
1520 bool added;
1521 this->debugAddOrOverlap(log, coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe, &added);
1522 }
1523 }
1524 return;
1525 }
1526
1527 // description below
1528 void SkOpCoincidence::debugAddEndMovedSpans(SkPathOpsDebug::GlitchLog* log, const SkOpPtT* ptT) const {
1529 FAIL_IF_COIN(!ptT->span()->upCastable(), ptT->span());
1530 const SkOpSpan* base = ptT->span()->upCast();
1531 const SkOpSpan* prev = base->prev();
1532 FAIL_IF_COIN(!prev, ptT->span());
1533 if (!prev->isCanceled()) {
1534 this->debugAddEndMovedSpans(log, base, base->prev());
1535 }
1536 if (!base->isCanceled()) {
1537 this->debugAddEndMovedSpans(log, base, base->next());
1538 }
1539 return;
1540 }
1541
1542 /* If A is coincident with B and B includes an endpoint, and A's matching point
1543 is not the endpoint (i.e., there's an implied line connecting B-end and A)
1544 then assume that the same implied line may intersect another curve close to B.
1545 Since we only care about coincidence that was undetected, look at the
1546 ptT list on B-segment adjacent to the B-end/A ptT loop (not in the loop, but
1547 next door) and see if the A matching point is close enough to form another
1548 coincident pair. If so, check for a new coincident span between B-end/A ptT loop
1549 and the adjacent ptT loop.
1550 */
1551 void SkOpCoincidence::debugAddEndMovedSpans(SkPathOpsDebug::GlitchLog* log) const {
1552 const SkCoincidentSpans* span = fHead;
1553 if (!span) {
1554 return;
1555 }
1556 // fTop = span;
1557 // fHead = nullptr;
1558 do {
1559 if (span->coinPtTStart()->fPt != span->oppPtTStart()->fPt) {
1560 FAIL_IF_COIN(1 == span->coinPtTStart()->fT, span);
1561 bool onEnd = span->coinPtTStart()->fT == 0;
1562 bool oOnEnd = zero_or_one(span->oppPtTStart()->fT);
1563 if (onEnd) {
1564 if (!oOnEnd) { // if both are on end, any nearby intersect was already found
1565 this->debugAddEndMovedSpans(log, span->oppPtTStart());
1566 }
1567 } else if (oOnEnd) {
1568 this->debugAddEndMovedSpans(log, span->coinPtTStart());
1569 }
1570 }
1571 if (span->coinPtTEnd()->fPt != span->oppPtTEnd()->fPt) {
1572 bool onEnd = span->coinPtTEnd()->fT == 1;
1573 bool oOnEnd = zero_or_one(span->oppPtTEnd()->fT);
1574 if (onEnd) {
1575 if (!oOnEnd) {
1576 this->debugAddEndMovedSpans(log, span->oppPtTEnd());
1577 }
1578 } else if (oOnEnd) {
1579 this->debugAddEndMovedSpans(log, span->coinPtTEnd());
1580 }
1581 }
1582 } while ((span = span->next()));
1583 // this->restoreHead();
1584 return;
1585 }
1586
1587 /* Commented-out lines keep this in sync with addExpanded */
1588 // for each coincident pair, match the spans
1589 // if the spans don't match, add the mssing pt to the segment and loop it in the opposite span
1590 void SkOpCoincidence::debugAddExpanded(SkPathOpsDebug::GlitchLog* log) const {
1591 // DEBUG_SET_PHASE();
1592 const SkCoincidentSpans* coin = this->fHead;
1593 if (!coin) {
1594 return;
1595 }
1596 do {
1597 const SkOpPtT* startPtT = coin->coinPtTStart();
1598 const SkOpPtT* oStartPtT = coin->oppPtTStart();
1599 double priorT = startPtT->fT;
1600 double oPriorT = oStartPtT->fT;
1601 FAIL_IF_COIN(!startPtT->contains(oStartPtT), coin);
1602 SkOPASSERT(coin->coinPtTEnd()->contains(coin->oppPtTEnd()));
1603 const SkOpSpanBase* start = startPtT->span();
1604 const SkOpSpanBase* oStart = oStartPtT->span();
1605 const SkOpSpanBase* end = coin->coinPtTEnd()->span();
1606 const SkOpSpanBase* oEnd = coin->oppPtTEnd()->span();
1607 FAIL_IF_COIN(oEnd->deleted(), coin);
1608 FAIL_IF_COIN(!start->upCastable(), coin);
1609 const SkOpSpanBase* test = start->upCast()->next();
1610 FAIL_IF_COIN(!coin->flipped() && !oStart->upCastable(), coin);
1611 const SkOpSpanBase* oTest = coin->flipped() ? oStart->prev() : oStart->upCast()->next();
1612 FAIL_IF_COIN(!oTest, coin);
1613 const SkOpSegment* seg = start->segment();
1614 const SkOpSegment* oSeg = oStart->segment();
1615 while (test != end || oTest != oEnd) {
1616 const SkOpPtT* containedOpp = test->ptT()->contains(oSeg);
1617 const SkOpPtT* containedThis = oTest->ptT()->contains(seg);
1618 if (!containedOpp || !containedThis) {
1619 // choose the ends, or the first common pt-t list shared by both
1620 double nextT, oNextT;
1621 if (containedOpp) {
1622 nextT = test->t();
1623 oNextT = containedOpp->fT;
1624 } else if (containedThis) {
1625 nextT = containedThis->fT;
1626 oNextT = oTest->t();
1627 } else {
1628 // iterate through until a pt-t list found that contains the other
1629 const SkOpSpanBase* walk = test;
1630 const SkOpPtT* walkOpp;
1631 do {
1632 FAIL_IF_COIN(!walk->upCastable(), coin);
1633 walk = walk->upCast()->next();
1634 } while (!(walkOpp = walk->ptT()->contains(oSeg))
1635 && walk != coin->coinPtTEnd()->span());
1636 FAIL_IF_COIN(!walkOpp, coin);
1637 nextT = walk->t();
1638 oNextT = walkOpp->fT;
1639 }
1640 // use t ranges to guess which one is missing
1641 double startRange = nextT - priorT;
1642 FAIL_IF_COIN(!startRange, coin);
1643 double startPart = (test->t() - priorT) / startRange;
1644 double oStartRange = oNextT - oPriorT;
1645 FAIL_IF_COIN(!oStartRange, coin);
1646 double oStartPart = (oTest->t() - oStartPtT->fT) / oStartRange;
1647 FAIL_IF_COIN(startPart == oStartPart, coin);
1648 bool addToOpp = !containedOpp && !containedThis ? startPart < oStartPart
1649 : !!containedThis;
1650 bool startOver = false;
1651 addToOpp ? log->record(SkPathOpsDebug::kAddExpandedCoin_Glitch,
1652 oPriorT + oStartRange * startPart, test)
1653 : log->record(SkPathOpsDebug::kAddExpandedCoin_Glitch,
1654 priorT + startRange * oStartPart, oTest);
1655 // FAIL_IF_COIN(!success, coin);
1656 if (startOver) {
1657 test = start;
1658 oTest = oStart;
1659 }
1660 end = coin->coinPtTEnd()->span();
1661 oEnd = coin->oppPtTEnd()->span();
1662 }
1663 if (test != end) {
1664 FAIL_IF_COIN(!test->upCastable(), coin);
1665 priorT = test->t();
1666 test = test->upCast()->next();
1667 }
1668 if (oTest != oEnd) {
1669 oPriorT = oTest->t();
1670 oTest = coin->flipped() ? oTest->prev() : oTest->upCast()->next();
1671 FAIL_IF_COIN(!oTest, coin);
1672 }
1673 }
1674 } while ((coin = coin->next()));
1675 return;
1676 }
1677
1678 /* Commented-out lines keep this in sync addIfMissing() */
1679 // note that over1s, over1e, over2s, over2e are ordered
1680 void SkOpCoincidence::debugAddIfMissing(SkPathOpsDebug::GlitchLog* log, const SkOpPtT* over1s, const SkOpPtT* over2s,
1681 double tStart, double tEnd, const SkOpSegment* coinSeg, const SkOpSegment* oppSeg, bool* added,
1682 const SkOpPtT* over1e, const SkOpPtT* over2e) const {
1683 SkASSERT(tStart < tEnd);
1684 SkASSERT(over1s->fT < over1e->fT);
1685 SkASSERT(between(over1s->fT, tStart, over1e->fT));
1686 SkASSERT(between(over1s->fT, tEnd, over1e->fT));
1687 SkASSERT(over2s->fT < over2e->fT);
1688 SkASSERT(between(over2s->fT, tStart, over2e->fT));
1689 SkASSERT(between(over2s->fT, tEnd, over2e->fT));
1690 SkASSERT(over1s->segment() == over1e->segment());
1691 SkASSERT(over2s->segment() == over2e->segment());
1692 SkASSERT(over1s->segment() == over2s->segment());
1693 SkASSERT(over1s->segment() != coinSeg);
1694 SkASSERT(over1s->segment() != oppSeg);
1695 SkASSERT(coinSeg != oppSeg);
1696 double coinTs, coinTe, oppTs, oppTe;
1697 coinTs = TRange(over1s, tStart, coinSeg SkDEBUGPARAMS(over1e));
1698 coinTe = TRange(over1s, tEnd, coinSeg SkDEBUGPARAMS(over1e));
1699 SkOpSpanBase::Collapsed result = coinSeg->collapsed(coinTs, coinTe);
1700 if (SkOpSpanBase::Collapsed::kNo != result) {
1701 return log->record(SkPathOpsDebug::kAddIfCollapsed_Glitch, coinSeg);
1702 }
1703 oppTs = TRange(over2s, tStart, oppSeg SkDEBUGPARAMS(over2e));
1704 oppTe = TRange(over2s, tEnd, oppSeg SkDEBUGPARAMS(over2e));
1705 result = oppSeg->collapsed(oppTs, oppTe);
1706 if (SkOpSpanBase::Collapsed::kNo != result) {
1707 return log->record(SkPathOpsDebug::kAddIfCollapsed_Glitch, oppSeg);
1708 }
1709 if (coinTs > coinTe) {
1710 using std::swap;
1711 swap(coinTs, coinTe);
1712 swap(oppTs, oppTe);
1713 }
1714 this->debugAddOrOverlap(log, coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe, added);
1715 return;
1716 }
1717
1718 /* Commented-out lines keep this in sync addOrOverlap() */
1719 // If this is called by addEndMovedSpans(), a returned false propogates out to an abort.
1720 // If this is called by AddIfMissing(), a returned false indicates there was nothing to add
1721 void SkOpCoincidence::debugAddOrOverlap(SkPathOpsDebug::GlitchLog* log,
1722 const SkOpSegment* coinSeg, const SkOpSegment* oppSeg,
1723 double coinTs, double coinTe, double oppTs, double oppTe, bool* added) const {
1724 SkTDArray<SkCoincidentSpans*> overlaps;
1725 SkOPASSERT(!fTop); // this is (correctly) reversed in addifMissing()
1726 if (fTop && !this->checkOverlap(fTop, coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe,
1727 &overlaps)) {
1728 return;
1729 }
1730 if (fHead && !this->checkOverlap(fHead, coinSeg, oppSeg, coinTs,
1731 coinTe, oppTs, oppTe, &overlaps)) {
1732 return;
1733 }
1734 const SkCoincidentSpans* overlap = overlaps.size() ? overlaps[0] : nullptr;
1735 for (int index = 1; index < overlaps.size(); ++index) { // combine overlaps before continuing
1736 const SkCoincidentSpans* test = overlaps[index];
1737 if (overlap->coinPtTStart()->fT > test->coinPtTStart()->fT) {
1738 log->record(SkPathOpsDebug::kAddOrOverlap_Glitch, overlap, test->coinPtTStart());
1739 }
1740 if (overlap->coinPtTEnd()->fT < test->coinPtTEnd()->fT) {
1741 log->record(SkPathOpsDebug::kAddOrOverlap_Glitch, overlap, test->coinPtTEnd());
1742 }
1743 if (overlap->flipped()
1744 ? overlap->oppPtTStart()->fT < test->oppPtTStart()->fT
1745 : overlap->oppPtTStart()->fT > test->oppPtTStart()->fT) {
1746 log->record(SkPathOpsDebug::kAddOrOverlap_Glitch, overlap, test->oppPtTStart());
1747 }
1748 if (overlap->flipped()
1749 ? overlap->oppPtTEnd()->fT > test->oppPtTEnd()->fT
1750 : overlap->oppPtTEnd()->fT < test->oppPtTEnd()->fT) {
1751 log->record(SkPathOpsDebug::kAddOrOverlap_Glitch, overlap, test->oppPtTEnd());
1752 }
1753 if (!fHead) { this->debugRelease(log, fHead, test);
1754 this->debugRelease(log, fTop, test);
1755 }
1756 }
1757 const SkOpPtT* cs = coinSeg->existing(coinTs, oppSeg);
1758 const SkOpPtT* ce = coinSeg->existing(coinTe, oppSeg);
1759 RETURN_FALSE_IF(overlap && cs && ce && overlap->contains(cs, ce), coinSeg);
1760 RETURN_FALSE_IF(cs != ce || !cs, coinSeg);
1761 const SkOpPtT* os = oppSeg->existing(oppTs, coinSeg);
1762 const SkOpPtT* oe = oppSeg->existing(oppTe, coinSeg);
1763 RETURN_FALSE_IF(overlap && os && oe && overlap->contains(os, oe), oppSeg);
1764 SkASSERT(true || !cs || !cs->deleted());
1765 SkASSERT(true || !os || !os->deleted());
1766 SkASSERT(true || !ce || !ce->deleted());
1767 SkASSERT(true || !oe || !oe->deleted());
1768 const SkOpPtT* csExisting = !cs ? coinSeg->existing(coinTs, nullptr) : nullptr;
1769 const SkOpPtT* ceExisting = !ce ? coinSeg->existing(coinTe, nullptr) : nullptr;
1770 RETURN_FALSE_IF(csExisting && csExisting == ceExisting, coinSeg);
1771 RETURN_FALSE_IF(csExisting && (csExisting == ce ||
1772 csExisting->contains(ceExisting ? ceExisting : ce)), coinSeg);
1773 RETURN_FALSE_IF(ceExisting && (ceExisting == cs ||
1774 ceExisting->contains(csExisting ? csExisting : cs)), coinSeg);
1775 const SkOpPtT* osExisting = !os ? oppSeg->existing(oppTs, nullptr) : nullptr;
1776 const SkOpPtT* oeExisting = !oe ? oppSeg->existing(oppTe, nullptr) : nullptr;
1777 RETURN_FALSE_IF(osExisting && osExisting == oeExisting, oppSeg);
1778 RETURN_FALSE_IF(osExisting && (osExisting == oe ||
1779 osExisting->contains(oeExisting ? oeExisting : oe)), oppSeg);
1780 RETURN_FALSE_IF(oeExisting && (oeExisting == os ||
1781 oeExisting->contains(osExisting ? osExisting : os)), oppSeg);
1782 bool csDeleted = false, osDeleted = false, ceDeleted = false, oeDeleted = false;
1783 this->debugValidate();
1784 if (!cs || !os) {
1785 if (!cs)
1786 cs = coinSeg->debugAddT(coinTs, log);
1787 if (!os)
1788 os = oppSeg->debugAddT(oppTs, log);
1789 // RETURN_FALSE_IF(callerAborts, !csWritable || !osWritable);
1790 if (cs && os) cs->span()->debugAddOpp(log, os->span());
1791 // cs = csWritable;
1792 // os = osWritable->active();
1793 RETURN_FALSE_IF((ce && ce->deleted()) || (oe && oe->deleted()), coinSeg);
1794 }
1795 if (!ce || !oe) {
1796 if (!ce)
1797 ce = coinSeg->debugAddT(coinTe, log);
1798 if (!oe)
1799 oe = oppSeg->debugAddT(oppTe, log);
1800 if (ce && oe) ce->span()->debugAddOpp(log, oe->span());
1801 // ce = ceWritable;
1802 // oe = oeWritable;
1803 }
1804 this->debugValidate();
1805 RETURN_FALSE_IF(csDeleted, coinSeg);
1806 RETURN_FALSE_IF(osDeleted, oppSeg);
1807 RETURN_FALSE_IF(ceDeleted, coinSeg);
1808 RETURN_FALSE_IF(oeDeleted, oppSeg);
1809 RETURN_FALSE_IF(!cs || !ce || cs == ce || cs->contains(ce) || !os || !oe || os == oe || os->contains(oe), coinSeg);
1810 bool result = true;
1811 if (overlap) {
1812 if (overlap->coinPtTStart()->segment() == coinSeg) {
1813 log->record(SkPathOpsDebug::kAddMissingExtend_Glitch, coinSeg, coinTs, coinTe, oppSeg, oppTs, oppTe);
1814 } else {
1815 if (oppTs > oppTe) {
1816 using std::swap;
1817 swap(coinTs, coinTe);
1818 swap(oppTs, oppTe);
1819 }
1820 log->record(SkPathOpsDebug::kAddMissingExtend_Glitch, oppSeg, oppTs, oppTe, coinSeg, coinTs, coinTe);
1821 }
1822 #if 0 && DEBUG_COINCIDENCE_VERBOSE
1823 if (result) {
1824 overlap->debugShow();
1825 }
1826 #endif
1827 } else {
1828 log->record(SkPathOpsDebug::kAddMissingCoin_Glitch, coinSeg, coinTs, coinTe, oppSeg, oppTs, oppTe);
1829 #if 0 && DEBUG_COINCIDENCE_VERBOSE
1830 fHead->debugShow();
1831 #endif
1832 }
1833 this->debugValidate();
1834 return (void) result;
1835 }
1836
1837 // Extra commented-out lines keep this in sync with addMissing()
1838 /* detects overlaps of different coincident runs on same segment */
1839 /* does not detect overlaps for pairs without any segments in common */
1840 // returns true if caller should loop again
1841 void SkOpCoincidence::debugAddMissing(SkPathOpsDebug::GlitchLog* log, bool* added) const {
1842 const SkCoincidentSpans* outer = fHead;
1843 *added = false;
1844 if (!outer) {
1845 return;
1846 }
1847 // fTop = outer;
1848 // fHead = nullptr;
1849 do {
1850 // addifmissing can modify the list that this is walking
1851 // save head so that walker can iterate over old data unperturbed
1852 // addifmissing adds to head freely then add saved head in the end
1853 const SkOpPtT* ocs = outer->coinPtTStart();
1854 SkASSERT(!ocs->deleted());
1855 const SkOpSegment* outerCoin = ocs->segment();
1856 SkASSERT(!outerCoin->done()); // if it's done, should have already been removed from list
1857 const SkOpPtT* oos = outer->oppPtTStart();
1858 if (oos->deleted()) {
1859 return;
1860 }
1861 const SkOpSegment* outerOpp = oos->segment();
1862 SkASSERT(!outerOpp->done());
1863 // SkOpSegment* outerCoinWritable = const_cast<SkOpSegment*>(outerCoin);
1864 // SkOpSegment* outerOppWritable = const_cast<SkOpSegment*>(outerOpp);
1865 const SkCoincidentSpans* inner = outer;
1866 while ((inner = inner->next())) {
1867 this->debugValidate();
1868 double overS, overE;
1869 const SkOpPtT* ics = inner->coinPtTStart();
1870 SkASSERT(!ics->deleted());
1871 const SkOpSegment* innerCoin = ics->segment();
1872 SkASSERT(!innerCoin->done());
1873 const SkOpPtT* ios = inner->oppPtTStart();
1874 SkASSERT(!ios->deleted());
1875 const SkOpSegment* innerOpp = ios->segment();
1876 SkASSERT(!innerOpp->done());
1877 // SkOpSegment* innerCoinWritable = const_cast<SkOpSegment*>(innerCoin);
1878 // SkOpSegment* innerOppWritable = const_cast<SkOpSegment*>(innerOpp);
1879 if (outerCoin == innerCoin) {
1880 const SkOpPtT* oce = outer->coinPtTEnd();
1881 if (oce->deleted()) {
1882 return;
1883 }
1884 const SkOpPtT* ice = inner->coinPtTEnd();
1885 SkASSERT(!ice->deleted());
1886 if (outerOpp != innerOpp && this->overlap(ocs, oce, ics, ice, &overS, &overE)) {
1887 this->debugAddIfMissing(log, ocs->starter(oce), ics->starter(ice),
1888 overS, overE, outerOpp, innerOpp, added,
1889 ocs->debugEnder(oce),
1890 ics->debugEnder(ice));
1891 }
1892 } else if (outerCoin == innerOpp) {
1893 const SkOpPtT* oce = outer->coinPtTEnd();
1894 SkASSERT(!oce->deleted());
1895 const SkOpPtT* ioe = inner->oppPtTEnd();
1896 SkASSERT(!ioe->deleted());
1897 if (outerOpp != innerCoin && this->overlap(ocs, oce, ios, ioe, &overS, &overE)) {
1898 this->debugAddIfMissing(log, ocs->starter(oce), ios->starter(ioe),
1899 overS, overE, outerOpp, innerCoin, added,
1900 ocs->debugEnder(oce),
1901 ios->debugEnder(ioe));
1902 }
1903 } else if (outerOpp == innerCoin) {
1904 const SkOpPtT* ooe = outer->oppPtTEnd();
1905 SkASSERT(!ooe->deleted());
1906 const SkOpPtT* ice = inner->coinPtTEnd();
1907 SkASSERT(!ice->deleted());
1908 SkASSERT(outerCoin != innerOpp);
1909 if (this->overlap(oos, ooe, ics, ice, &overS, &overE)) {
1910 this->debugAddIfMissing(log, oos->starter(ooe), ics->starter(ice),
1911 overS, overE, outerCoin, innerOpp, added,
1912 oos->debugEnder(ooe),
1913 ics->debugEnder(ice));
1914 }
1915 } else if (outerOpp == innerOpp) {
1916 const SkOpPtT* ooe = outer->oppPtTEnd();
1917 SkASSERT(!ooe->deleted());
1918 const SkOpPtT* ioe = inner->oppPtTEnd();
1919 if (ioe->deleted()) {
1920 return;
1921 }
1922 SkASSERT(outerCoin != innerCoin);
1923 if (this->overlap(oos, ooe, ios, ioe, &overS, &overE)) {
1924 this->debugAddIfMissing(log, oos->starter(ooe), ios->starter(ioe),
1925 overS, overE, outerCoin, innerCoin, added,
1926 oos->debugEnder(ooe),
1927 ios->debugEnder(ioe));
1928 }
1929 }
1930 this->debugValidate();
1931 }
1932 } while ((outer = outer->next()));
1933 // this->restoreHead();
1934 return;
1935 }
1936
1937 // Commented-out lines keep this in sync with release()
1938 void SkOpCoincidence::debugRelease(SkPathOpsDebug::GlitchLog* log, const SkCoincidentSpans* coin, const SkCoincidentSpans* remove) const {
1939 const SkCoincidentSpans* head = coin;
1940 const SkCoincidentSpans* prev = nullptr;
1941 const SkCoincidentSpans* next;
1942 do {
1943 next = coin->next();
1944 if (coin == remove) {
1945 if (prev) {
1946 // prev->setNext(next);
1947 } else if (head == fHead) {
1948 // fHead = next;
1949 } else {
1950 // fTop = next;
1951 }
1952 log->record(SkPathOpsDebug::kReleasedSpan_Glitch, coin);
1953 }
1954 prev = coin;
1955 } while ((coin = next));
1956 return;
1957 }
1958
1959 void SkOpCoincidence::debugRelease(SkPathOpsDebug::GlitchLog* log, const SkOpSegment* deleted) const {
1960 const SkCoincidentSpans* coin = fHead;
1961 if (!coin) {
1962 return;
1963 }
1964 do {
1965 if (coin->coinPtTStart()->segment() == deleted
1966 || coin->coinPtTEnd()->segment() == deleted
1967 || coin->oppPtTStart()->segment() == deleted
1968 || coin->oppPtTEnd()->segment() == deleted) {
1969 log->record(SkPathOpsDebug::kReleasedSpan_Glitch, coin);
1970 }
1971 } while ((coin = coin->next()));
1972 }
1973
1974 // Commented-out lines keep this in sync with expand()
1975 // expand the range by checking adjacent spans for coincidence
1976 bool SkOpCoincidence::debugExpand(SkPathOpsDebug::GlitchLog* log) const {
1977 const SkCoincidentSpans* coin = fHead;
1978 if (!coin) {
1979 return false;
1980 }
1981 bool expanded = false;
1982 do {
1983 if (coin->debugExpand(log)) {
1984 // check to see if multiple spans expanded so they are now identical
1985 const SkCoincidentSpans* test = fHead;
1986 do {
1987 if (coin == test) {
1988 continue;
1989 }
1990 if (coin->coinPtTStart() == test->coinPtTStart()
1991 && coin->oppPtTStart() == test->oppPtTStart()) {
1992 if (log) log->record(SkPathOpsDebug::kExpandCoin_Glitch, fHead, test->coinPtTStart());
1993 break;
1994 }
1995 } while ((test = test->next()));
1996 expanded = true;
1997 }
1998 } while ((coin = coin->next()));
1999 return expanded;
2000 }
2001
2002 // Commented-out lines keep this in sync with mark()
2003 /* this sets up the coincidence links in the segments when the coincidence crosses multiple spans */
2004 void SkOpCoincidence::debugMark(SkPathOpsDebug::GlitchLog* log) const {
2005 const SkCoincidentSpans* coin = fHead;
2006 if (!coin) {
2007 return;
2008 }
2009 do {
2010 FAIL_IF_COIN(!coin->coinPtTStartWritable()->span()->upCastable(), coin);
2011 const SkOpSpan* start = coin->coinPtTStartWritable()->span()->upCast();
2012 // SkASSERT(start->deleted());
2013 const SkOpSpanBase* end = coin->coinPtTEndWritable()->span();
2014 // SkASSERT(end->deleted());
2015 const SkOpSpanBase* oStart = coin->oppPtTStartWritable()->span();
2016 // SkASSERT(oStart->deleted());
2017 const SkOpSpanBase* oEnd = coin->oppPtTEndWritable()->span();
2018 // SkASSERT(oEnd->deleted());
2019 bool flipped = coin->flipped();
2020 if (flipped) {
2021 using std::swap;
2022 swap(oStart, oEnd);
2023 }
2024 /* coin and opp spans may not match up. Mark the ends, and then let the interior
2025 get marked as many times as the spans allow */
2026 start->debugInsertCoincidence(log, oStart->upCast());
2027 end->debugInsertCoinEnd(log, oEnd);
2028 const SkOpSegment* segment = start->segment();
2029 const SkOpSegment* oSegment = oStart->segment();
2030 const SkOpSpanBase* next = start;
2031 const SkOpSpanBase* oNext = oStart;
2032 bool ordered;
2033 FAIL_IF_COIN(!coin->ordered(&ordered), coin);
2034 while ((next = next->upCast()->next()) != end) {
2035 FAIL_IF_COIN(!next->upCastable(), coin);
2036 next->upCast()->debugInsertCoincidence(log, oSegment, flipped, ordered);
2037 }
2038 while ((oNext = oNext->upCast()->next()) != oEnd) {
2039 FAIL_IF_COIN(!oNext->upCastable(), coin);
2040 oNext->upCast()->debugInsertCoincidence(log, segment, flipped, ordered);
2041 }
2042 } while ((coin = coin->next()));
2043 return;
2044 }
2045 #endif // DEBUG_COIN
2046
2047 #if DEBUG_COIN
2048 // Commented-out lines keep this in sync with markCollapsed()
2049 void SkOpCoincidence::debugMarkCollapsed(SkPathOpsDebug::GlitchLog* log, const SkCoincidentSpans* coin, const SkOpPtT* test) const {
2050 const SkCoincidentSpans* head = coin;
2051 while (coin) {
2052 if (coin->collapsed(test)) {
2053 if (zero_or_one(coin->coinPtTStart()->fT) && zero_or_one(coin->coinPtTEnd()->fT)) {
2054 log->record(SkPathOpsDebug::kCollapsedCoin_Glitch, coin);
2055 }
2056 if (zero_or_one(coin->oppPtTStart()->fT) && zero_or_one(coin->oppPtTEnd()->fT)) {
2057 log->record(SkPathOpsDebug::kCollapsedCoin_Glitch, coin);
2058 }
2059 this->debugRelease(log, head, coin);
2060 }
2061 coin = coin->next();
2062 }
2063 }
2064
2065 // Commented-out lines keep this in sync with markCollapsed()
2066 void SkOpCoincidence::debugMarkCollapsed(SkPathOpsDebug::GlitchLog* log, const SkOpPtT* test) const {
2067 this->debugMarkCollapsed(log, fHead, test);
2068 this->debugMarkCollapsed(log, fTop, test);
2069 }
2070 #endif // DEBUG_COIN
2071
2072 void SkCoincidentSpans::debugShow() const {
2073 SkDebugf("coinSpan - id=%d t=%1.9g tEnd=%1.9g\n", coinPtTStart()->segment()->debugID(),
2074 coinPtTStart()->fT, coinPtTEnd()->fT);
2075 SkDebugf("coinSpan + id=%d t=%1.9g tEnd=%1.9g\n", oppPtTStart()->segment()->debugID(),
2076 oppPtTStart()->fT, oppPtTEnd()->fT);
2077 }
2078
2079 void SkOpCoincidence::debugShowCoincidence() const {
2080 #if DEBUG_COINCIDENCE
2081 const SkCoincidentSpans* span = fHead;
2082 while (span) {
2083 span->debugShow();
2084 span = span->next();
2085 }
2086 #endif // DEBUG_COINCIDENCE
2087 }
2088
2089 #if DEBUG_COIN
2090 static void DebugCheckBetween(const SkOpSpanBase* next, const SkOpSpanBase* end,
2091 double oStart, double oEnd, const SkOpSegment* oSegment,
2092 SkPathOpsDebug::GlitchLog* log) {
2093 SkASSERT(next != end);
2094 SkASSERT(!next->contains(end) || log);
2095 if (next->t() > end->t()) {
2096 using std::swap;
2097 swap(next, end);
2098 }
2099 do {
2100 const SkOpPtT* ptT = next->ptT();
2101 int index = 0;
2102 bool somethingBetween = false;
2103 do {
2104 ++index;
2105 ptT = ptT->next();
2106 const SkOpPtT* checkPtT = next->ptT();
2107 if (ptT == checkPtT) {
2108 break;
2109 }
2110 bool looped = false;
2111 for (int check = 0; check < index; ++check) {
2112 if ((looped = checkPtT == ptT)) {
2113 break;
2114 }
2115 checkPtT = checkPtT->next();
2116 }
2117 if (looped) {
2118 SkASSERT(0);
2119 break;
2120 }
2121 if (ptT->deleted()) {
2122 continue;
2123 }
2124 if (ptT->segment() != oSegment) {
2125 continue;
2126 }
2127 somethingBetween |= between(oStart, ptT->fT, oEnd);
2128 } while (true);
2129 SkASSERT(somethingBetween);
2130 } while (next != end && (next = next->upCast()->next()));
2131 }
2132
2133 static void DebugCheckOverlap(const SkCoincidentSpans* test, const SkCoincidentSpans* list,
2134 SkPathOpsDebug::GlitchLog* log) {
2135 if (!list) {
2136 return;
2137 }
2138 const SkOpSegment* coinSeg = test->coinPtTStart()->segment();
2139 SkASSERT(coinSeg == test->coinPtTEnd()->segment());
2140 const SkOpSegment* oppSeg = test->oppPtTStart()->segment();
2141 SkASSERT(oppSeg == test->oppPtTEnd()->segment());
2142 SkASSERT(coinSeg != test->oppPtTStart()->segment());
2143 SkDEBUGCODE(double tcs = test->coinPtTStart()->fT);
2144 SkASSERT(between(0, tcs, 1));
2145 SkDEBUGCODE(double tce = test->coinPtTEnd()->fT);
2146 SkASSERT(between(0, tce, 1));
2147 SkASSERT(tcs < tce);
2148 double tos = test->oppPtTStart()->fT;
2149 SkASSERT(between(0, tos, 1));
2150 double toe = test->oppPtTEnd()->fT;
2151 SkASSERT(between(0, toe, 1));
2152 SkASSERT(tos != toe);
2153 if (tos > toe) {
2154 using std::swap;
2155 swap(tos, toe);
2156 }
2157 do {
2158 double lcs, lce, los, loe;
2159 if (coinSeg == list->coinPtTStart()->segment()) {
2160 if (oppSeg != list->oppPtTStart()->segment()) {
2161 continue;
2162 }
2163 lcs = list->coinPtTStart()->fT;
2164 lce = list->coinPtTEnd()->fT;
2165 los = list->oppPtTStart()->fT;
2166 loe = list->oppPtTEnd()->fT;
2167 if (los > loe) {
2168 using std::swap;
2169 swap(los, loe);
2170 }
2171 } else if (coinSeg == list->oppPtTStart()->segment()) {
2172 if (oppSeg != list->coinPtTStart()->segment()) {
2173 continue;
2174 }
2175 lcs = list->oppPtTStart()->fT;
2176 lce = list->oppPtTEnd()->fT;
2177 if (lcs > lce) {
2178 using std::swap;
2179 swap(lcs, lce);
2180 }
2181 los = list->coinPtTStart()->fT;
2182 loe = list->coinPtTEnd()->fT;
2183 } else {
2184 continue;
2185 }
2186 SkASSERT(tce < lcs || lce < tcs);
2187 SkASSERT(toe < los || loe < tos);
2188 } while ((list = list->next()));
2189 }
2190
2191
2192 static void DebugCheckOverlapTop(const SkCoincidentSpans* head, const SkCoincidentSpans* opt,
2193 SkPathOpsDebug::GlitchLog* log) {
2194 // check for overlapping coincident spans
2195 const SkCoincidentSpans* test = head;
2196 while (test) {
2197 const SkCoincidentSpans* next = test->next();
2198 DebugCheckOverlap(test, next, log);
2199 DebugCheckOverlap(test, opt, log);
2200 test = next;
2201 }
2202 }
2203
2204 static void DebugValidate(const SkCoincidentSpans* head, const SkCoincidentSpans* opt,
2205 SkPathOpsDebug::GlitchLog* log) {
2206 // look for pts inside coincident spans that are not inside the opposite spans
2207 const SkCoincidentSpans* coin = head;
2208 while (coin) {
2209 SkASSERT(SkOpCoincidence::Ordered(coin->coinPtTStart()->segment(),
2210 coin->oppPtTStart()->segment()));
2211 SkASSERT(coin->coinPtTStart()->span()->ptT() == coin->coinPtTStart());
2212 SkASSERT(coin->coinPtTEnd()->span()->ptT() == coin->coinPtTEnd());
2213 SkASSERT(coin->oppPtTStart()->span()->ptT() == coin->oppPtTStart());
2214 SkASSERT(coin->oppPtTEnd()->span()->ptT() == coin->oppPtTEnd());
2215 coin = coin->next();
2216 }
2217 DebugCheckOverlapTop(head, opt, log);
2218 }
2219 #endif // DEBUG_COIN
2220
2221 void SkOpCoincidence::debugValidate() const {
2222 #if DEBUG_COINCIDENCE
2223 DebugValidate(fHead, fTop, nullptr);
2224 DebugValidate(fTop, nullptr, nullptr);
2225 #endif
2226 }
2227
2228 #if DEBUG_COIN
2229 static void DebugCheckBetween(const SkCoincidentSpans* head, const SkCoincidentSpans* opt,
2230 SkPathOpsDebug::GlitchLog* log) {
2231 // look for pts inside coincident spans that are not inside the opposite spans
2232 const SkCoincidentSpans* coin = head;
2233 while (coin) {
2234 DebugCheckBetween(coin->coinPtTStart()->span(), coin->coinPtTEnd()->span(),
2235 coin->oppPtTStart()->fT, coin->oppPtTEnd()->fT, coin->oppPtTStart()->segment(),
2236 log);
2237 DebugCheckBetween(coin->oppPtTStart()->span(), coin->oppPtTEnd()->span(),
2238 coin->coinPtTStart()->fT, coin->coinPtTEnd()->fT, coin->coinPtTStart()->segment(),
2239 log);
2240 coin = coin->next();
2241 }
2242 DebugCheckOverlapTop(head, opt, log);
2243 }
2244 #endif
2245
2246 void SkOpCoincidence::debugCheckBetween() const {
2247 #if DEBUG_COINCIDENCE
2248 if (fGlobalState->debugCheckHealth()) {
2249 return;
2250 }
2251 DebugCheckBetween(fHead, fTop, nullptr);
2252 DebugCheckBetween(fTop, nullptr, nullptr);
2253 #endif
2254 }
2255
2256 #if DEBUG_COIN
2257 void SkOpContour::debugCheckHealth(SkPathOpsDebug::GlitchLog* log) const {
2258 const SkOpSegment* segment = &fHead;
2259 do {
2260 segment->debugCheckHealth(log);
2261 } while ((segment = segment->next()));
2262 }
2263
2264 void SkOpCoincidence::debugCheckValid(SkPathOpsDebug::GlitchLog* log) const {
2265 #if DEBUG_VALIDATE
2266 DebugValidate(fHead, fTop, log);
2267 DebugValidate(fTop, nullptr, log);
2268 #endif
2269 }
2270
2271 void SkOpCoincidence::debugCorrectEnds(SkPathOpsDebug::GlitchLog* log) const {
2272 const SkCoincidentSpans* coin = fHead;
2273 if (!coin) {
2274 return;
2275 }
2276 do {
2277 coin->debugCorrectEnds(log);
2278 } while ((coin = coin->next()));
2279 }
2280
2281 // commmented-out lines keep this aligned with missingCoincidence()
2282 void SkOpContour::debugMissingCoincidence(SkPathOpsDebug::GlitchLog* log) const {
2283 // SkASSERT(fCount > 0);
2284 const SkOpSegment* segment = &fHead;
2285 // bool result = false;
2286 do {
2287 segment->debugMissingCoincidence(log);
2288 segment = segment->next();
2289 } while (segment);
2290 return;
2291 }
2292
2293 void SkOpContour::debugMoveMultiples(SkPathOpsDebug::GlitchLog* log) const {
2294 SkASSERT(fCount > 0);
2295 const SkOpSegment* segment = &fHead;
2296 do {
2297 segment->debugMoveMultiples(log);
2298 } while ((segment = segment->next()));
2299 return;
2300 }
2301
2302 void SkOpContour::debugMoveNearby(SkPathOpsDebug::GlitchLog* log) const {
2303 SkASSERT(fCount > 0);
2304 const SkOpSegment* segment = &fHead;
2305 do {
2306 segment->debugMoveNearby(log);
2307 } while ((segment = segment->next()));
2308 }
2309 #endif
2310
2311 #if DEBUG_COINCIDENCE_ORDER
2312 void SkOpSegment::debugResetCoinT() const {
2313 fDebugBaseIndex = -1;
2314 fDebugBaseMin = 1;
2315 fDebugBaseMax = -1;
2316 fDebugLastIndex = -1;
2317 fDebugLastMin = 1;
2318 fDebugLastMax = -1;
2319 }
2320 #endif
2321
2322 void SkOpSegment::debugValidate() const {
2323 #if DEBUG_COINCIDENCE_ORDER
2324 {
2325 const SkOpSpanBase* span = &fHead;
2326 do {
2327 span->debugResetCoinT();
2328 } while (!span->final() && (span = span->upCast()->next()));
2329 span = &fHead;
2330 int index = 0;
2331 do {
2332 span->debugSetCoinT(index++);
2333 } while (!span->final() && (span = span->upCast()->next()));
2334 }
2335 #endif
2336 #if DEBUG_COINCIDENCE
2337 if (this->globalState()->debugCheckHealth()) {
2338 return;
2339 }
2340 #endif
2341 #if DEBUG_VALIDATE
2342 const SkOpSpanBase* span = &fHead;
2343 double lastT = -1;
2344 const SkOpSpanBase* prev = nullptr;
2345 int count = 0;
2346 int done = 0;
2347 do {
2348 if (!span->final()) {
2349 ++count;
2350 done += span->upCast()->done() ? 1 : 0;
2351 }
2352 SkASSERT(span->segment() == this);
2353 SkASSERT(!prev || prev->upCast()->next() == span);
2354 SkASSERT(!prev || prev == span->prev());
2355 prev = span;
2356 double t = span->ptT()->fT;
2357 SkASSERT(lastT < t);
2358 lastT = t;
2359 span->debugValidate();
2360 } while (!span->final() && (span = span->upCast()->next()));
2361 SkASSERT(count == fCount);
2362 SkASSERT(done == fDoneCount);
2363 SkASSERT(count >= fDoneCount);
2364 SkASSERT(span->final());
2365 span->debugValidate();
2366 #endif
2367 }
2368
2369 #if DEBUG_COIN
2370
2371 // Commented-out lines keep this in sync with addOpp()
2372 void SkOpSpanBase::debugAddOpp(SkPathOpsDebug::GlitchLog* log, const SkOpSpanBase* opp) const {
2373 const SkOpPtT* oppPrev = this->ptT()->oppPrev(opp->ptT());
2374 if (!oppPrev) {
2375 return;
2376 }
2377 this->debugMergeMatches(log, opp);
2378 this->ptT()->debugAddOpp(opp->ptT(), oppPrev);
2379 this->debugCheckForCollapsedCoincidence(log);
2380 }
2381
2382 // Commented-out lines keep this in sync with checkForCollapsedCoincidence()
2383 void SkOpSpanBase::debugCheckForCollapsedCoincidence(SkPathOpsDebug::GlitchLog* log) const {
2384 const SkOpCoincidence* coins = this->globalState()->coincidence();
2385 if (coins->isEmpty()) {
2386 return;
2387 }
2388 // the insert above may have put both ends of a coincident run in the same span
2389 // for each coincident ptT in loop; see if its opposite in is also in the loop
2390 // this implementation is the motivation for marking that a ptT is referenced by a coincident span
2391 const SkOpPtT* head = this->ptT();
2392 const SkOpPtT* test = head;
2393 do {
2394 if (!test->coincident()) {
2395 continue;
2396 }
2397 coins->debugMarkCollapsed(log, test);
2398 } while ((test = test->next()) != head);
2399 }
2400 #endif
2401
2402 bool SkOpSpanBase::debugCoinEndLoopCheck() const {
2403 int loop = 0;
2404 const SkOpSpanBase* next = this;
2405 SkOpSpanBase* nextCoin;
2406 do {
2407 nextCoin = next->fCoinEnd;
2408 SkASSERT(nextCoin == this || nextCoin->fCoinEnd != nextCoin);
2409 for (int check = 1; check < loop - 1; ++check) {
2410 const SkOpSpanBase* checkCoin = this->fCoinEnd;
2411 const SkOpSpanBase* innerCoin = checkCoin;
2412 for (int inner = check + 1; inner < loop; ++inner) {
2413 innerCoin = innerCoin->fCoinEnd;
2414 if (checkCoin == innerCoin) {
2415 SkDebugf("*** bad coincident end loop ***\n");
2416 return false;
2417 }
2418 }
2419 }
2420 ++loop;
2421 } while ((next = nextCoin) && next != this);
2422 return true;
2423 }
2424
2425 #if DEBUG_COIN
2426 // Commented-out lines keep this in sync with insertCoinEnd()
2427 void SkOpSpanBase::debugInsertCoinEnd(SkPathOpsDebug::GlitchLog* log, const SkOpSpanBase* coin) const {
2428 if (containsCoinEnd(coin)) {
2429 // SkASSERT(coin->containsCoinEnd(this));
2430 return;
2431 }
2432 debugValidate();
2433 // SkASSERT(this != coin);
2434 log->record(SkPathOpsDebug::kMarkCoinEnd_Glitch, this, coin);
2435 // coin->fCoinEnd = this->fCoinEnd;
2436 // this->fCoinEnd = coinNext;
2437 debugValidate();
2438 }
2439
2440 // Commented-out lines keep this in sync with mergeMatches()
2441 // Look to see if pt-t linked list contains same segment more than once
2442 // if so, and if each pt-t is directly pointed to by spans in that segment,
2443 // merge them
2444 // keep the points, but remove spans so that the segment doesn't have 2 or more
2445 // spans pointing to the same pt-t loop at different loop elements
2446 void SkOpSpanBase::debugMergeMatches(SkPathOpsDebug::GlitchLog* log, const SkOpSpanBase* opp) const {
2447 const SkOpPtT* test = &fPtT;
2448 const SkOpPtT* testNext;
2449 const SkOpPtT* stop = test;
2450 do {
2451 testNext = test->next();
2452 if (test->deleted()) {
2453 continue;
2454 }
2455 const SkOpSpanBase* testBase = test->span();
2456 SkASSERT(testBase->ptT() == test);
2457 const SkOpSegment* segment = test->segment();
2458 if (segment->done()) {
2459 continue;
2460 }
2461 const SkOpPtT* inner = opp->ptT();
2462 const SkOpPtT* innerStop = inner;
2463 do {
2464 if (inner->segment() != segment) {
2465 continue;
2466 }
2467 if (inner->deleted()) {
2468 continue;
2469 }
2470 const SkOpSpanBase* innerBase = inner->span();
2471 SkASSERT(innerBase->ptT() == inner);
2472 // when the intersection is first detected, the span base is marked if there are
2473 // more than one point in the intersection.
2474 // if (!innerBase->hasMultipleHint() && !testBase->hasMultipleHint()) {
2475 if (!zero_or_one(inner->fT)) {
2476 log->record(SkPathOpsDebug::kMergeMatches_Glitch, innerBase, test);
2477 } else {
2478 SkASSERT(inner->fT != test->fT);
2479 if (!zero_or_one(test->fT)) {
2480 log->record(SkPathOpsDebug::kMergeMatches_Glitch, testBase, inner);
2481 } else {
2482 log->record(SkPathOpsDebug::kMergeMatches_Glitch, segment);
2483 // SkDEBUGCODE(testBase->debugSetDeleted());
2484 // test->setDeleted();
2485 // SkDEBUGCODE(innerBase->debugSetDeleted());
2486 // inner->setDeleted();
2487 }
2488 }
2489 #ifdef SK_DEBUG // assert if another undeleted entry points to segment
2490 const SkOpPtT* debugInner = inner;
2491 while ((debugInner = debugInner->next()) != innerStop) {
2492 if (debugInner->segment() != segment) {
2493 continue;
2494 }
2495 if (debugInner->deleted()) {
2496 continue;
2497 }
2498 SkOPASSERT(0);
2499 }
2500 #endif
2501 break;
2502 // }
2503 break;
2504 } while ((inner = inner->next()) != innerStop);
2505 } while ((test = testNext) != stop);
2506 this->debugCheckForCollapsedCoincidence(log);
2507 }
2508
2509 #endif
2510
2511 void SkOpSpanBase::debugResetCoinT() const {
2512 #if DEBUG_COINCIDENCE_ORDER
2513 const SkOpPtT* ptT = &fPtT;
2514 do {
2515 ptT->debugResetCoinT();
2516 ptT = ptT->next();
2517 } while (ptT != &fPtT);
2518 #endif
2519 }
2520
2521 void SkOpSpanBase::debugSetCoinT(int index) const {
2522 #if DEBUG_COINCIDENCE_ORDER
2523 const SkOpPtT* ptT = &fPtT;
2524 do {
2525 if (!ptT->deleted()) {
2526 ptT->debugSetCoinT(index);
2527 }
2528 ptT = ptT->next();
2529 } while (ptT != &fPtT);
2530 #endif
2531 }
2532
2533 const SkOpSpan* SkOpSpanBase::debugStarter(SkOpSpanBase const** endPtr) const {
2534 const SkOpSpanBase* end = *endPtr;
2535 SkASSERT(this->segment() == end->segment());
2536 const SkOpSpanBase* result;
2537 if (t() < end->t()) {
2538 result = this;
2539 } else {
2540 result = end;
2541 *endPtr = this;
2542 }
2543 return result->upCast();
2544 }
2545
2546 void SkOpSpanBase::debugValidate() const {
2547 #if DEBUG_COINCIDENCE
2548 if (this->globalState()->debugCheckHealth()) {
2549 return;
2550 }
2551 #endif
2552 #if DEBUG_VALIDATE
2553 const SkOpPtT* ptT = &fPtT;
2554 SkASSERT(ptT->span() == this);
2555 do {
2556 // SkASSERT(SkDPoint::RoughlyEqual(fPtT.fPt, ptT->fPt));
2557 ptT->debugValidate();
2558 ptT = ptT->next();
2559 } while (ptT != &fPtT);
2560 SkASSERT(this->debugCoinEndLoopCheck());
2561 if (!this->final()) {
2562 SkASSERT(this->upCast()->debugCoinLoopCheck());
2563 }
2564 if (fFromAngle) {
2565 fFromAngle->debugValidate();
2566 }
2567 if (!this->final() && this->upCast()->toAngle()) {
2568 this->upCast()->toAngle()->debugValidate();
2569 }
2570 #endif
2571 }
2572
2573 bool SkOpSpan::debugCoinLoopCheck() const {
2574 int loop = 0;
2575 const SkOpSpan* next = this;
2576 SkOpSpan* nextCoin;
2577 do {
2578 nextCoin = next->fCoincident;
2579 SkASSERT(nextCoin == this || nextCoin->fCoincident != nextCoin);
2580 for (int check = 1; check < loop - 1; ++check) {
2581 const SkOpSpan* checkCoin = this->fCoincident;
2582 const SkOpSpan* innerCoin = checkCoin;
2583 for (int inner = check + 1; inner < loop; ++inner) {
2584 innerCoin = innerCoin->fCoincident;
2585 if (checkCoin == innerCoin) {
2586 SkDebugf("*** bad coincident loop ***\n");
2587 return false;
2588 }
2589 }
2590 }
2591 ++loop;
2592 } while ((next = nextCoin) && next != this);
2593 return true;
2594 }
2595
2596 #if DEBUG_COIN
2597 // Commented-out lines keep this in sync with insertCoincidence() in header
2598 void SkOpSpan::debugInsertCoincidence(SkPathOpsDebug::GlitchLog* log, const SkOpSpan* coin) const {
2599 if (containsCoincidence(coin)) {
2600 // SkASSERT(coin->containsCoincidence(this));
2601 return;
2602 }
2603 debugValidate();
2604 // SkASSERT(this != coin);
2605 log->record(SkPathOpsDebug::kMarkCoinStart_Glitch, this, coin);
2606 // coin->fCoincident = this->fCoincident;
2607 // this->fCoincident = coinNext;
2608 debugValidate();
2609 }
2610
2611 // Commented-out lines keep this in sync with insertCoincidence()
2612 void SkOpSpan::debugInsertCoincidence(SkPathOpsDebug::GlitchLog* log, const SkOpSegment* segment, bool flipped, bool ordered) const {
2613 if (this->containsCoincidence(segment)) {
2614 return;
2615 }
2616 const SkOpPtT* next = &fPtT;
2617 while ((next = next->next()) != &fPtT) {
2618 if (next->segment() == segment) {
2619 const SkOpSpan* span;
2620 const SkOpSpanBase* base = next->span();
2621 if (!ordered) {
2622 const SkOpSpanBase* spanEnd = fNext->contains(segment)->span();
2623 const SkOpPtT* start = base->ptT()->starter(spanEnd->ptT());
2624 FAIL_IF_COIN(!start->span()->upCastable(), this);
2625 span = const_cast<SkOpSpan*>(start->span()->upCast());
2626 }
2627 else if (flipped) {
2628 span = base->prev();
2629 FAIL_IF_COIN(!span, this);
2630 }
2631 else {
2632 FAIL_IF_COIN(!base->upCastable(), this);
2633 span = base->upCast();
2634 }
2635 log->record(SkPathOpsDebug::kMarkCoinInsert_Glitch, span);
2636 return;
2637 }
2638 }
2639 log->record(SkPathOpsDebug::kMarkCoinMissing_Glitch, segment, this);
2640 return;
2641 }
2642 #endif // DEBUG_COIN
2643
2644 // called only by test code
2645 int SkIntersections::debugCoincidentUsed() const {
2646 if (!fIsCoincident[0]) {
2647 SkASSERT(!fIsCoincident[1]);
2648 return 0;
2649 }
2650 int count = 0;
2651 SkDEBUGCODE(int count2 = 0;)
2652 for (int index = 0; index < fUsed; ++index) {
2653 if (fIsCoincident[0] & (1 << index)) {
2654 ++count;
2655 }
2656 #ifdef SK_DEBUG
2657 if (fIsCoincident[1] & (1 << index)) {
2658 ++count2;
2659 }
2660 #endif
2661 }
2662 SkASSERT(count == count2);
2663 return count;
2664 }
2665
2666 // Commented-out lines keep this in sync with addOpp()
2667 void SkOpPtT::debugAddOpp(const SkOpPtT* opp, const SkOpPtT* oppPrev) const {
2668 SkDEBUGCODE(const SkOpPtT* oldNext = this->fNext);
2669 SkASSERT(this != opp);
2670 // this->fNext = opp;
2671 SkASSERT(oppPrev != oldNext);
2672 // oppPrev->fNext = oldNext;
2673 }
2674
2675 bool SkOpPtT::debugContains(const SkOpPtT* check) const {
2676 SkASSERT(this != check);
2677 const SkOpPtT* ptT = this;
2678 int links = 0;
2679 do {
2680 ptT = ptT->next();
2681 if (ptT == check) {
2682 return true;
2683 }
2684 ++links;
2685 const SkOpPtT* test = this;
2686 for (int index = 0; index < links; ++index) {
2687 if (ptT == test) {
2688 return false;
2689 }
2690 test = test->next();
2691 }
2692 } while (true);
2693 }
2694
2695 const SkOpPtT* SkOpPtT::debugContains(const SkOpSegment* check) const {
2696 SkASSERT(this->segment() != check);
2697 const SkOpPtT* ptT = this;
2698 int links = 0;
2699 do {
2700 ptT = ptT->next();
2701 if (ptT->segment() == check) {
2702 return ptT;
2703 }
2704 ++links;
2705 const SkOpPtT* test = this;
2706 for (int index = 0; index < links; ++index) {
2707 if (ptT == test) {
2708 return nullptr;
2709 }
2710 test = test->next();
2711 }
2712 } while (true);
2713 }
2714
2715 const SkOpPtT* SkOpPtT::debugEnder(const SkOpPtT* end) const {
2716 return fT < end->fT ? end : this;
2717 }
2718
2719 int SkOpPtT::debugLoopLimit(bool report) const {
2720 int loop = 0;
2721 const SkOpPtT* next = this;
2722 do {
2723 for (int check = 1; check < loop - 1; ++check) {
2724 const SkOpPtT* checkPtT = this->fNext;
2725 const SkOpPtT* innerPtT = checkPtT;
2726 for (int inner = check + 1; inner < loop; ++inner) {
2727 innerPtT = innerPtT->fNext;
2728 if (checkPtT == innerPtT) {
2729 if (report) {
2730 SkDebugf("*** bad ptT loop ***\n");
2731 }
2732 return loop;
2733 }
2734 }
2735 }
2736 // there's nothing wrong with extremely large loop counts -- but this may appear to hang
2737 // by taking a very long time to figure out that no loop entry is a duplicate
2738 // -- and it's likely that a large loop count is indicative of a bug somewhere
2739 if (++loop > 1000) {
2740 SkDebugf("*** loop count exceeds 1000 ***\n");
2741 return 1000;
2742 }
2743 } while ((next = next->fNext) && next != this);
2744 return 0;
2745 }
2746
2747 const SkOpPtT* SkOpPtT::debugOppPrev(const SkOpPtT* opp) const {
2748 return this->oppPrev(const_cast<SkOpPtT*>(opp));
2749 }
2750
2751 void SkOpPtT::debugResetCoinT() const {
2752 #if DEBUG_COINCIDENCE_ORDER
2753 this->segment()->debugResetCoinT();
2754 #endif
2755 }
2756
2757 void SkOpPtT::debugSetCoinT(int index) const {
2758 #if DEBUG_COINCIDENCE_ORDER
2759 this->segment()->debugSetCoinT(index, fT);
2760 #endif
2761 }
2762
2763 void SkOpPtT::debugValidate() const {
2764 #if DEBUG_COINCIDENCE
2765 if (this->globalState()->debugCheckHealth()) {
2766 return;
2767 }
2768 #endif
2769 #if DEBUG_VALIDATE
2770 SkOpPhase phase = contour()->globalState()->phase();
2771 if (phase == SkOpPhase::kIntersecting || phase == SkOpPhase::kFixWinding) {
2772 return;
2773 }
2774 SkASSERT(fNext);
2775 SkASSERT(fNext != this);
2776 SkASSERT(fNext->fNext);
2777 SkASSERT(debugLoopLimit(false) == 0);
2778 #endif
2779 }
2780
2781 static void output_scalar(SkScalar num) {
2782 if (num == (int) num) {
2783 SkDebugf("%d", (int) num);
2784 } else {
2785 SkString str;
2786 str.printf("%1.9g", num);
2787 int width = (int) str.size();
2788 const char* cStr = str.c_str();
2789 while (cStr[width - 1] == '0') {
2790 --width;
2791 }
2792 str.resize(width);
2793 SkDebugf("%sf", str.c_str());
2794 }
2795 }
2796
2797 static void output_points(const SkPoint* pts, int count) {
2798 for (int index = 0; index < count; ++index) {
2799 output_scalar(pts[index].fX);
2800 SkDebugf(", ");
2801 output_scalar(pts[index].fY);
2802 if (index + 1 < count) {
2803 SkDebugf(", ");
2804 }
2805 }
2806 }
2807
2808 static void showPathContours(const SkPath& path, const char* pathName) {
2809 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
2810 switch (verb) {
2811 case SkPathVerb::kMove:
2812 SkDebugf(" %s.moveTo(", pathName);
2813 output_points(&pts[0], 1);
2814 SkDebugf(");\n");
2815 continue;
2816 case SkPathVerb::kLine:
2817 SkDebugf(" %s.lineTo(", pathName);
2818 output_points(&pts[1], 1);
2819 SkDebugf(");\n");
2820 break;
2821 case SkPathVerb::kQuad:
2822 SkDebugf(" %s.quadTo(", pathName);
2823 output_points(&pts[1], 2);
2824 SkDebugf(");\n");
2825 break;
2826 case SkPathVerb::kConic:
2827 SkDebugf(" %s.conicTo(", pathName);
2828 output_points(&pts[1], 2);
2829 SkDebugf(", %1.9gf);\n", *w);
2830 break;
2831 case SkPathVerb::kCubic:
2832 SkDebugf(" %s.cubicTo(", pathName);
2833 output_points(&pts[1], 3);
2834 SkDebugf(");\n");
2835 break;
2836 case SkPathVerb::kClose:
2837 SkDebugf(" %s.close();\n", pathName);
2838 break;
2839 default:
2840 SkDEBUGFAIL("bad verb");
2841 return;
2842 }
2843 }
2844 }
2845
2846 static const char* gFillTypeStr[] = {
2847 "kWinding",
2848 "kEvenOdd",
2849 "kInverseWinding",
2850 "kInverseEvenOdd"
2851 };
2852
2853 void SkPathOpsDebug::ShowOnePath(const SkPath& path, const char* name, bool includeDeclaration) {
2854 #define SUPPORT_RECT_CONTOUR_DETECTION 0
2855 #if SUPPORT_RECT_CONTOUR_DETECTION
2856 int rectCount = path.isRectContours() ? path.rectContours(nullptr, nullptr) : 0;
2857 if (rectCount > 0) {
2858 SkTDArray<SkRect> rects;
2859 SkTDArray<SkPathDirection> directions;
2860 rects.setCount(rectCount);
2861 directions.setCount(rectCount);
2862 path.rectContours(rects.begin(), directions.begin());
2863 for (int contour = 0; contour < rectCount; ++contour) {
2864 const SkRect& rect = rects[contour];
2865 SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop,
2866 rect.fRight, rect.fBottom, directions[contour] == SkPathDirection::kCCW
2867 ? "SkPathDirection::kCCW" : "SkPathDirection::kCW");
2868 }
2869 return;
2870 }
2871 #endif
2872 SkPathFillType fillType = path.getFillType();
2873 SkASSERT(fillType >= SkPathFillType::kWinding && fillType <= SkPathFillType::kInverseEvenOdd);
2874 if (includeDeclaration) {
2875 SkDebugf(" SkPath %s;\n", name);
2876 }
2877 SkDebugf(" %s.setFillType(SkPath::%s);\n", name, gFillTypeStr[(int)fillType]);
2878 showPathContours(path, name);
2879 }
2880
2881 #if DEBUG_DUMP_VERIFY
2882 #include "include/core/SkData.h"
2883 #include "include/core/SkStream.h"
2884
2885 static void dump_path(FILE* file, const SkPath& path, bool dumpAsHex) {
2886 SkDynamicMemoryWStream wStream;
2887 path.dump(&wStream, dumpAsHex);
2888 sk_sp<SkData> data(wStream.detachAsData());
2889 fprintf(file, "%.*s\n", (int) data->size(), (char*) data->data());
2890 }
2891
2892 static int dumpID = 0;
2893
2894 void DumpOp(const SkPath& one, const SkPath& two, SkPathOp op,
2895 const char* testName) {
2896 FILE* file = sk_fopen("op_dump.txt", kWrite_SkFILE_Flag);
2897 DumpOp(file, one, two, op, testName);
2898 }
2899
2900 void DumpOp(FILE* file, const SkPath& one, const SkPath& two, SkPathOp op,
2901 const char* testName) {
2902 const char* name = testName ? testName : "op";
2903 fprintf(file,
2904 "\nstatic void %s_%d(skiatest::Reporter* reporter, const char* filename) {\n",
2905 name, ++dumpID);
2906 fprintf(file, " SkPath path;\n");
2907 fprintf(file, " path.setFillType((SkPath::FillType) %d);\n", one.getFillType());
2908 dump_path(file, one, true);
2909 fprintf(file, " SkPath path1(path);\n");
2910 fprintf(file, " path.reset();\n");
2911 fprintf(file, " path.setFillType((SkPath::FillType) %d);\n", two.getFillType());
2912 dump_path(file, two, true);
2913 fprintf(file, " SkPath path2(path);\n");
2914 fprintf(file, " testPathOp(reporter, path1, path2, (SkPathOp) %d, filename);\n", op);
2915 fprintf(file, "}\n\n");
2916 fclose(file);
2917 }
2918
2919 void DumpSimplify(const SkPath& path, const char* testName) {
2920 FILE* file = sk_fopen("simplify_dump.txt", kWrite_SkFILE_Flag);
2921 DumpSimplify(file, path, testName);
2922 }
2923
2924 void DumpSimplify(FILE* file, const SkPath& path, const char* testName) {
2925 const char* name = testName ? testName : "simplify";
2926 fprintf(file,
2927 "\nstatic void %s_%d(skiatest::Reporter* reporter, const char* filename) {\n",
2928 name, ++dumpID);
2929 fprintf(file, " SkPath path;\n");
2930 fprintf(file, " path.setFillType((SkPath::FillType) %d);\n", path.getFillType());
2931 dump_path(file, path, true);
2932 fprintf(file, " testSimplify(reporter, path, filename);\n");
2933 fprintf(file, "}\n\n");
2934 fclose(file);
2935 }
2936
2937 #include "include/core/SkBitmap.h"
2938 #include "include/core/SkCanvas.h"
2939 #include "include/core/SkPaint.h"
2940 #include "include/core/SkRegion.h"
2941
2942 const int bitWidth = 64;
2943 const int bitHeight = 64;
2944
2945 static void debug_scale_matrix(const SkPath& one, const SkPath* two, SkMatrix& scale) {
2946 SkRect larger = one.getBounds();
2947 if (two) {
2948 larger.join(two->getBounds());
2949 }
2950 SkScalar largerWidth = larger.width();
2951 if (largerWidth < 4) {
2952 largerWidth = 4;
2953 }
2954 SkScalar largerHeight = larger.height();
2955 if (largerHeight < 4) {
2956 largerHeight = 4;
2957 }
2958 SkScalar hScale = (bitWidth - 2) / largerWidth;
2959 SkScalar vScale = (bitHeight - 2) / largerHeight;
2960 scale.reset();
2961 scale.preScale(hScale, vScale);
2962 larger.fLeft *= hScale;
2963 larger.fRight *= hScale;
2964 larger.fTop *= vScale;
2965 larger.fBottom *= vScale;
2966 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
2967 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
2968 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
2969 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
2970 scale.preTranslate(dx, dy);
2971 }
2972
2973 static int debug_paths_draw_the_same(const SkPath& one, const SkPath& two, SkBitmap& bits) {
2974 if (bits.width() == 0) {
2975 bits.allocN32Pixels(bitWidth * 2, bitHeight);
2976 }
2977 SkCanvas canvas(bits);
2978 canvas.drawColor(SK_ColorWHITE);
2979 SkPaint paint;
2980 canvas.save();
2981 const SkRect& bounds1 = one.getBounds();
2982 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
2983 canvas.drawPath(one, paint);
2984 canvas.restore();
2985 canvas.save();
2986 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
2987 canvas.drawPath(two, paint);
2988 canvas.restore();
2989 int errors = 0;
2990 for (int y = 0; y < bitHeight - 1; ++y) {
2991 uint32_t* addr1 = bits.getAddr32(0, y);
2992 uint32_t* addr2 = bits.getAddr32(0, y + 1);
2993 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
2994 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
2995 for (int x = 0; x < bitWidth - 1; ++x) {
2996 // count 2x2 blocks
2997 bool err = addr1[x] != addr3[x];
2998 if (err) {
2999 errors += addr1[x + 1] != addr3[x + 1]
3000 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
3001 }
3002 }
3003 }
3004 return errors;
3005 }
3006
3007 void ReportOpFail(const SkPath& one, const SkPath& two, SkPathOp op) {
3008 SkDEBUGF("// Op did not expect failure\n");
3009 DumpOp(stderr, one, two, op, "opTest");
3010 fflush(stderr);
3011 }
3012
3013 void VerifyOp(const SkPath& one, const SkPath& two, SkPathOp op,
3014 const SkPath& result) {
3015 SkPath pathOut, scaledPathOut;
3016 SkRegion rgnA, rgnB, openClip, rgnOut;
3017 openClip.setRect({-16000, -16000, 16000, 16000});
3018 rgnA.setPath(one, openClip);
3019 rgnB.setPath(two, openClip);
3020 rgnOut.op(rgnA, rgnB, (SkRegion::Op) op);
3021 rgnOut.getBoundaryPath(&pathOut);
3022 SkMatrix scale;
3023 debug_scale_matrix(one, &two, scale);
3024 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
3025 SkPath scaledA, scaledB;
3026 scaledA.addPath(one, scale);
3027 scaledA.setFillType(one.getFillType());
3028 scaledB.addPath(two, scale);
3029 scaledB.setFillType(two.getFillType());
3030 scaledRgnA.setPath(scaledA, openClip);
3031 scaledRgnB.setPath(scaledB, openClip);
3032 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) op);
3033 scaledRgnOut.getBoundaryPath(&scaledPathOut);
3034 SkBitmap bitmap;
3035 SkPath scaledOut;
3036 scaledOut.addPath(result, scale);
3037 scaledOut.setFillType(result.getFillType());
3038 int errors = debug_paths_draw_the_same(scaledPathOut, scaledOut, bitmap);
3039 const int MAX_ERRORS = 9;
3040 if (errors > MAX_ERRORS) {
3041 fprintf(stderr, "// Op did not expect errors=%d\n", errors);
3042 DumpOp(stderr, one, two, op, "opTest");
3043 fflush(stderr);
3044 }
3045 }
3046
3047 void ReportSimplifyFail(const SkPath& path) {
3048 SkDEBUGF("// Simplify did not expect failure\n");
3049 DumpSimplify(stderr, path, "simplifyTest");
3050 fflush(stderr);
3051 }
3052
3053 void VerifySimplify(const SkPath& path, const SkPath& result) {
3054 SkPath pathOut, scaledPathOut;
3055 SkRegion rgnA, openClip, rgnOut;
3056 openClip.setRect({-16000, -16000, 16000, 16000});
3057 rgnA.setPath(path, openClip);
3058 rgnOut.getBoundaryPath(&pathOut);
3059 SkMatrix scale;
3060 debug_scale_matrix(path, nullptr, scale);
3061 SkRegion scaledRgnA;
3062 SkPath scaledA;
3063 scaledA.addPath(path, scale);
3064 scaledA.setFillType(path.getFillType());
3065 scaledRgnA.setPath(scaledA, openClip);
3066 scaledRgnA.getBoundaryPath(&scaledPathOut);
3067 SkBitmap bitmap;
3068 SkPath scaledOut;
3069 scaledOut.addPath(result, scale);
3070 scaledOut.setFillType(result.getFillType());
3071 int errors = debug_paths_draw_the_same(scaledPathOut, scaledOut, bitmap);
3072 const int MAX_ERRORS = 9;
3073 if (errors > MAX_ERRORS) {
3074 fprintf(stderr, "// Simplify did not expect errors=%d\n", errors);
3075 DumpSimplify(stderr, path, "simplifyTest");
3076 fflush(stderr);
3077 }
3078 }
3079 #endif // DEBUG_DUMP_VERIFY
3080
3081 // global path dumps for msvs Visual Studio 17 to use from Immediate Window
3082 void Dump(const SkPath& path) {
3083 path.dump();
3084 }
3085
3086 void DumpHex(const SkPath& path) {
3087 path.dumpHex();
3088 }