blob: 212e2bcc79637246eb754a68c8e8218e09931e91 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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
mtklein1b249332015-07-07 12:21:21 -07008#include "SkMutex.h"
caryclark26ad22a2015-10-16 09:03:38 -07009#include "SkOpCoincidence.h"
10#include "SkOpContour.h"
jvanverth02802f62015-07-02 06:42:49 -070011#include "SkPath.h"
mtklein1b249332015-07-07 12:21:21 -070012#include "SkPathOpsDebug.h"
caryclark54359292015-03-26 07:52:43 -070013#include "SkString.h"
caryclark54359292015-03-26 07:52:43 -070014
caryclark55888e42016-07-18 10:01:36 -070015class SkCoincidentSpans;
caryclark26ad22a2015-10-16 09:03:38 -070016
caryclark54359292015-03-26 07:52:43 -070017#if DEBUG_VALIDATE
18extern bool FLAGS_runFail;
19#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +000020
caryclark624637c2015-05-11 07:21:27 -070021#if DEBUG_SORT
22int SkPathOpsDebug::gSortCountDefault = SK_MaxS32;
23int SkPathOpsDebug::gSortCount;
24#endif
25
26#if DEBUG_ACTIVE_OP
27const char* SkPathOpsDebug::kPathOpStr[] = {"diff", "sect", "union", "xor"};
28#endif
29
caryclark@google.com07393ca2013-04-08 11:47:37 +000030#if defined SK_DEBUG || !FORCE_RELEASE
31
caryclark@google.com570863f2013-09-16 15:55:01 +000032const char* SkPathOpsDebug::kLVerbStr[] = {"", "line", "quad", "cubic"};
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000033
commit-bot@chromium.orgfe41b8f2014-04-25 14:03:52 +000034int SkPathOpsDebug::gContourID = 0;
35int SkPathOpsDebug::gSegmentID = 0;
caryclark@google.com570863f2013-09-16 15:55:01 +000036
caryclark54359292015-03-26 07:52:43 -070037bool SkPathOpsDebug::ChaseContains(const SkTDArray<SkOpSpanBase* >& chaseArray,
38 const SkOpSpanBase* span) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000039 for (int index = 0; index < chaseArray.count(); ++index) {
caryclark54359292015-03-26 07:52:43 -070040 const SkOpSpanBase* entry = chaseArray[index];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000041 if (entry == span) {
42 return true;
43 }
44 }
45 return false;
46}
caryclark26ad22a2015-10-16 09:03:38 -070047#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000048
caryclark55888e42016-07-18 10:01:36 -070049#if DEBUG_COINCIDENCE_VERBOSE
caryclark26ad22a2015-10-16 09:03:38 -070050enum GlitchType {
51 kAddCorruptCoin_Glitch,
52 kAddExpandedCoin_Glitch,
caryclark55888e42016-07-18 10:01:36 -070053 kAddExpandedFail_Glitch,
54 kAddIfMissingCoin_Glitch,
caryclark26ad22a2015-10-16 09:03:38 -070055 kAddMissingCoin_Glitch,
caryclark55888e42016-07-18 10:01:36 -070056 kAddMissingExtend_Glitch,
57 kAddOrOverlap_Glitch,
caryclark26ad22a2015-10-16 09:03:38 -070058 kCollapsedCoin_Glitch,
59 kCollapsedDone_Glitch,
60 kCollapsedOppValue_Glitch,
61 kCollapsedSpan_Glitch,
62 kCollapsedWindValue_Glitch,
63 kDeletedCoin_Glitch,
64 kExpandCoin_Glitch,
65 kMarkCoinEnd_Glitch,
66 kMarkCoinInsert_Glitch,
caryclark55888e42016-07-18 10:01:36 -070067 kMarkCoinMissing_Glitch,
68 kMarkCoinStart_Glitch,
69 kMergeContained_Glitch,
caryclark26ad22a2015-10-16 09:03:38 -070070 kMissingCoin_Glitch,
71 kMissingDone_Glitch,
72 kMissingIntersection_Glitch,
73 kMoveMultiple_Glitch,
caryclark55888e42016-07-18 10:01:36 -070074 kMoveNearbyClearAll_Glitch,
75 kMoveNearbyClearAll2_Glitch,
76 kMoveNearbyMerge_Glitch,
77 kMoveNearbyMergeFinal_Glitch,
78 kMoveNearbyRelease_Glitch,
79 kMoveNearbyReleaseFinal_Glitch,
80 kReleasedSpan_Glitch,
caryclark26ad22a2015-10-16 09:03:38 -070081 kUnaligned_Glitch,
82 kUnalignedHead_Glitch,
83 kUnalignedTail_Glitch,
caryclark26ad22a2015-10-16 09:03:38 -070084};
85
caryclark55888e42016-07-18 10:01:36 -070086static const int kGlitchType_Count = kUnalignedTail_Glitch + 1;
caryclark26ad22a2015-10-16 09:03:38 -070087
88struct SpanGlitch {
89 const char* fStage;
90 const SkOpSpanBase* fBase;
91 const SkOpSpanBase* fSuspect;
caryclark26ad22a2015-10-16 09:03:38 -070092 const SkOpSegment* fSegment;
caryclark55888e42016-07-18 10:01:36 -070093 const SkOpSegment* fOppSegment;
caryclark26ad22a2015-10-16 09:03:38 -070094 const SkOpPtT* fCoinSpan;
95 const SkOpPtT* fEndSpan;
96 const SkOpPtT* fOppSpan;
97 const SkOpPtT* fOppEndSpan;
caryclark55888e42016-07-18 10:01:36 -070098 double fStartT;
99 double fEndT;
100 double fOppStartT;
101 double fOppEndT;
caryclark26ad22a2015-10-16 09:03:38 -0700102 SkPoint fPt;
103 GlitchType fType;
104};
105
106struct SkPathOpsDebug::GlitchLog {
107 SpanGlitch* recordCommon(GlitchType type, const char* stage) {
108 SpanGlitch* glitch = fGlitches.push();
109 glitch->fStage = stage;
110 glitch->fBase = nullptr;
111 glitch->fSuspect = nullptr;
caryclark26ad22a2015-10-16 09:03:38 -0700112 glitch->fSegment = nullptr;
caryclark55888e42016-07-18 10:01:36 -0700113 glitch->fOppSegment = nullptr;
caryclark26ad22a2015-10-16 09:03:38 -0700114 glitch->fCoinSpan = nullptr;
115 glitch->fEndSpan = nullptr;
116 glitch->fOppSpan = nullptr;
117 glitch->fOppEndSpan = nullptr;
caryclark55888e42016-07-18 10:01:36 -0700118 glitch->fStartT = SK_ScalarNaN;
119 glitch->fEndT = SK_ScalarNaN;
120 glitch->fOppStartT = SK_ScalarNaN;
121 glitch->fOppEndT = SK_ScalarNaN;
caryclark26ad22a2015-10-16 09:03:38 -0700122 glitch->fPt = { SK_ScalarNaN, SK_ScalarNaN };
123 glitch->fType = type;
124 return glitch;
125 }
126
127 void record(GlitchType type, const char* stage, const SkOpSpanBase* base,
128 const SkOpSpanBase* suspect = NULL) {
129 SpanGlitch* glitch = recordCommon(type, stage);
130 glitch->fBase = base;
131 glitch->fSuspect = suspect;
132 }
133
caryclark55888e42016-07-18 10:01:36 -0700134 void record(GlitchType type, const char* stage, const SkOpSpanBase* base,
135 const SkOpPtT* ptT) {
caryclark26ad22a2015-10-16 09:03:38 -0700136 SpanGlitch* glitch = recordCommon(type, stage);
caryclark55888e42016-07-18 10:01:36 -0700137 glitch->fBase = base;
138 glitch->fCoinSpan = ptT;
139 }
140
141 void record(GlitchType type, const char* stage, const SkCoincidentSpans* coin,
142 const SkCoincidentSpans* opp = NULL) {
143 SpanGlitch* glitch = recordCommon(type, stage);
144 glitch->fCoinSpan = coin->coinPtTStart();
145 glitch->fEndSpan = coin->coinPtTEnd();
146 if (opp) {
147 glitch->fOppSpan = opp->coinPtTStart();
148 glitch->fOppEndSpan = opp->coinPtTEnd();
149 }
caryclark26ad22a2015-10-16 09:03:38 -0700150 }
151
152 void record(GlitchType type, const char* stage, const SkOpSpanBase* base,
153 const SkOpSegment* seg, double t, SkPoint pt) {
154 SpanGlitch* glitch = recordCommon(type, stage);
155 glitch->fBase = base;
156 glitch->fSegment = seg;
caryclark55888e42016-07-18 10:01:36 -0700157 glitch->fStartT = t;
caryclark26ad22a2015-10-16 09:03:38 -0700158 glitch->fPt = pt;
159 }
160
161 void record(GlitchType type, const char* stage, const SkOpSpanBase* base, double t,
162 SkPoint pt) {
163 SpanGlitch* glitch = recordCommon(type, stage);
164 glitch->fBase = base;
caryclark55888e42016-07-18 10:01:36 -0700165 glitch->fStartT = t;
caryclark26ad22a2015-10-16 09:03:38 -0700166 glitch->fPt = pt;
167 }
168
169 void record(GlitchType type, const char* stage, const SkCoincidentSpans* coin,
170 const SkOpPtT* coinSpan, const SkOpPtT* endSpan) {
171 SpanGlitch* glitch = recordCommon(type, stage);
caryclark55888e42016-07-18 10:01:36 -0700172 glitch->fCoinSpan = coin->coinPtTStart();
173 glitch->fEndSpan = coin->coinPtTEnd();
caryclark26ad22a2015-10-16 09:03:38 -0700174 glitch->fEndSpan = endSpan;
caryclark55888e42016-07-18 10:01:36 -0700175 glitch->fOppSpan = coinSpan;
176 glitch->fOppEndSpan = endSpan;
caryclark26ad22a2015-10-16 09:03:38 -0700177 }
178
179 void record(GlitchType type, const char* stage, const SkCoincidentSpans* coin,
caryclark55888e42016-07-18 10:01:36 -0700180 const SkOpSpanBase* base) {
caryclark26ad22a2015-10-16 09:03:38 -0700181 SpanGlitch* glitch = recordCommon(type, stage);
caryclark55888e42016-07-18 10:01:36 -0700182 glitch->fBase = base;
183 glitch->fCoinSpan = coin->coinPtTStart();
184 glitch->fEndSpan = coin->coinPtTEnd();
caryclark26ad22a2015-10-16 09:03:38 -0700185 }
186
187 void record(GlitchType type, const char* stage, const SkOpPtT* ptTS, const SkOpPtT* ptTE,
188 const SkOpPtT* oPtTS, const SkOpPtT* oPtTE) {
189 SpanGlitch* glitch = recordCommon(type, stage);
190 glitch->fCoinSpan = ptTS;
191 glitch->fEndSpan = ptTE;
192 glitch->fOppSpan = oPtTS;
193 glitch->fOppEndSpan = oPtTE;
194 }
195
caryclark55888e42016-07-18 10:01:36 -0700196 void record(GlitchType type, const char* stage, const SkOpSegment* seg, double startT,
197 double endT, const SkOpSegment* oppSeg, double oppStartT, double oppEndT) {
198 SpanGlitch* glitch = recordCommon(type, stage);
199 glitch->fSegment = seg;
200 glitch->fStartT = startT;
201 glitch->fEndT = endT;
202 glitch->fOppSegment = oppSeg;
203 glitch->fOppStartT = oppStartT;
204 glitch->fOppEndT = oppEndT;
205 }
206
207 void record(GlitchType type, const char* stage, const SkOpSegment* seg,
208 const SkOpSpan* span) {
209 SpanGlitch* glitch = recordCommon(type, stage);
210 glitch->fSegment = seg;
211 glitch->fBase = span;
212 }
213
214 void record(GlitchType type, const char* stage, double t, const SkOpSpanBase* span) {
215 SpanGlitch* glitch = recordCommon(type, stage);
216 glitch->fStartT = t;
217 glitch->fBase = span;
218 }
219
220 void record(GlitchType type, const char* stage, const SkOpSegment* seg) {
221 SpanGlitch* glitch = recordCommon(type, stage);
222 glitch->fSegment = seg;
223 }
224
225 void record(GlitchType type, const char* stage, const SkCoincidentSpans* coin,
226 const SkOpPtT* ptT) {
227 SpanGlitch* glitch = recordCommon(type, stage);
228 glitch->fCoinSpan = coin->coinPtTStart();
229 glitch->fEndSpan = ptT;
230 }
231
caryclark26ad22a2015-10-16 09:03:38 -0700232 SkTDArray<SpanGlitch> fGlitches;
233};
caryclark55888e42016-07-18 10:01:36 -0700234#endif
caryclark26ad22a2015-10-16 09:03:38 -0700235
caryclark55888e42016-07-18 10:01:36 -0700236void SkPathOpsDebug::ShowActiveSpans(SkOpContourHead* contourList) {
237#if DEBUG_ACTIVE_SPANS
238 SkOpContour* contour = contourList;
239 do {
240 contour->debugShowActiveSpans();
241 } while ((contour = contour->next()));
242#endif
243}
244
245#if DEBUG_COINCIDENCE
caryclark26ad22a2015-10-16 09:03:38 -0700246void SkPathOpsDebug::CheckHealth(SkOpContourHead* contourList, const char* id) {
caryclark55888e42016-07-18 10:01:36 -0700247 contourList->globalState()->debugSetCheckHealth(true);
248#if DEBUG_COINCIDENCE_VERBOSE
caryclark26ad22a2015-10-16 09:03:38 -0700249 GlitchLog glitches;
250 const SkOpContour* contour = contourList;
251 const SkOpCoincidence* coincidence = contour->globalState()->coincidence();
caryclark55888e42016-07-18 10:01:36 -0700252 coincidence->debugCheckValid(id, &glitches); // don't call validate; spans may be inconsistent
caryclark26ad22a2015-10-16 09:03:38 -0700253 do {
254 contour->debugCheckHealth(id, &glitches);
caryclark55888e42016-07-18 10:01:36 -0700255 contour->debugMissingCoincidence(id, &glitches);
caryclark26ad22a2015-10-16 09:03:38 -0700256 } while ((contour = contour->next()));
caryclark55888e42016-07-18 10:01:36 -0700257 coincidence->debugRemoveCollapsed(id, &glitches);
caryclark26ad22a2015-10-16 09:03:38 -0700258 coincidence->debugAddMissing(id, &glitches);
259 coincidence->debugExpand(id, &glitches);
260 coincidence->debugAddExpanded(id, &glitches);
261 coincidence->debugMark(id, &glitches);
caryclark55888e42016-07-18 10:01:36 -0700262 coincidence->debugReorder(id, &glitches);
caryclark26ad22a2015-10-16 09:03:38 -0700263 unsigned mask = 0;
264 for (int index = 0; index < glitches.fGlitches.count(); ++index) {
265 const SpanGlitch& glitch = glitches.fGlitches[index];
266 mask |= 1 << glitch.fType;
267 }
268 for (int index = 0; index < kGlitchType_Count; ++index) {
269 SkDebugf(mask & (1 << index) ? "x" : "-");
270 }
271 SkDebugf(" %s\n", id);
caryclark55888e42016-07-18 10:01:36 -0700272 for (int index = 0; index < glitches.fGlitches.count(); ++index) {
273 const SpanGlitch& glitch = glitches.fGlitches[index];
274 SkDebugf("%02d: ", index);
275 if (glitch.fBase) {
276 SkDebugf(" base=%d", glitch.fBase->debugID());
277 }
278 if (glitch.fSuspect) {
279 SkDebugf(" base=%d", glitch.fSuspect->debugID());
280 }
281 if (glitch.fSegment) {
282 SkDebugf(" segment=%d", glitch.fSegment->debugID());
283 }
284 if (glitch.fCoinSpan) {
285 SkDebugf(" coinSpan=%d", glitch.fCoinSpan->debugID());
286 }
287 if (glitch.fEndSpan) {
288 SkDebugf(" endSpan=%d", glitch.fEndSpan->debugID());
289 }
290 if (glitch.fOppSpan) {
291 SkDebugf(" oppSpan=%d", glitch.fOppSpan->debugID());
292 }
293 if (glitch.fOppEndSpan) {
294 SkDebugf(" oppEndSpan=%d", glitch.fOppEndSpan->debugID());
295 }
296 if (!SkScalarIsNaN(glitch.fStartT)) {
297 SkDebugf(" startT=%g", glitch.fStartT);
298 }
299 if (!SkScalarIsNaN(glitch.fEndT)) {
300 SkDebugf(" endT=%g", glitch.fEndT);
301 }
302 if (glitch.fOppSegment) {
303 SkDebugf(" segment=%d", glitch.fOppSegment->debugID());
304 }
305 if (!SkScalarIsNaN(glitch.fOppStartT)) {
306 SkDebugf(" oppStartT=%g", glitch.fOppStartT);
307 }
308 if (!SkScalarIsNaN(glitch.fOppEndT)) {
309 SkDebugf(" oppEndT=%g", glitch.fOppEndT);
310 }
311 if (!SkScalarIsNaN(glitch.fPt.fX) || !SkScalarIsNaN(glitch.fPt.fY)) {
312 SkDebugf(" pt=%g,%g", glitch.fPt.fX, glitch.fPt.fY);
313 }
314 switch (glitch.fType) {
315 case kAddCorruptCoin_Glitch: SkDebugf(" AddCorruptCoin"); break;
316 case kAddExpandedCoin_Glitch: SkDebugf(" AddExpandedCoin"); break;
317 case kAddExpandedFail_Glitch: SkDebugf(" AddExpandedFail"); break;
318 case kAddIfMissingCoin_Glitch: SkDebugf(" AddIfMissingCoin"); break;
319 case kAddMissingCoin_Glitch: SkDebugf(" AddMissingCoin"); break;
320 case kAddMissingExtend_Glitch: SkDebugf(" AddMissingExtend"); break;
321 case kAddOrOverlap_Glitch: SkDebugf(" AAddOrOverlap"); break;
322 case kCollapsedCoin_Glitch: SkDebugf(" CollapsedCoin"); break;
323 case kCollapsedDone_Glitch: SkDebugf(" CollapsedDone"); break;
324 case kCollapsedOppValue_Glitch: SkDebugf(" CollapsedOppValue"); break;
325 case kCollapsedSpan_Glitch: SkDebugf(" CollapsedSpan"); break;
326 case kCollapsedWindValue_Glitch: SkDebugf(" CollapsedWindValue"); break;
327 case kDeletedCoin_Glitch: SkDebugf(" DeletedCoin"); break;
328 case kExpandCoin_Glitch: SkDebugf(" ExpandCoin"); break;
329 case kMarkCoinEnd_Glitch: SkDebugf(" MarkCoinEnd"); break;
330 case kMarkCoinInsert_Glitch: SkDebugf(" MarkCoinInsert"); break;
331 case kMarkCoinMissing_Glitch: SkDebugf(" MarkCoinMissing"); break;
332 case kMarkCoinStart_Glitch: SkDebugf(" MarkCoinStart"); break;
333 case kMergeContained_Glitch: SkDebugf(" MergeContained"); break;
334 case kMissingCoin_Glitch: SkDebugf(" MissingCoin"); break;
335 case kMissingDone_Glitch: SkDebugf(" MissingDone"); break;
336 case kMissingIntersection_Glitch: SkDebugf(" MissingIntersection"); break;
337 case kMoveMultiple_Glitch: SkDebugf(" MoveMultiple"); break;
338 case kMoveNearbyClearAll_Glitch: SkDebugf(" MoveNearbyClearAll"); break;
339 case kMoveNearbyClearAll2_Glitch: SkDebugf(" MoveNearbyClearAll2"); break;
340 case kMoveNearbyMerge_Glitch: SkDebugf(" MoveNearbyMerge"); break;
341 case kMoveNearbyMergeFinal_Glitch: SkDebugf(" MoveNearbyMergeFinal"); break;
342 case kMoveNearbyRelease_Glitch: SkDebugf(" MoveNearbyRelease"); break;
343 case kMoveNearbyReleaseFinal_Glitch: SkDebugf(" MoveNearbyReleaseFinal"); break;
344 case kReleasedSpan_Glitch: SkDebugf(" ReleasedSpan"); break;
345 case kUnaligned_Glitch: SkDebugf(" Unaligned"); break;
346 case kUnalignedHead_Glitch: SkDebugf(" UnalignedHead"); break;
347 case kUnalignedTail_Glitch: SkDebugf(" UnalignedTail"); break;
348 default: SkASSERT(0);
349 }
350 SkDebugf("\n");
351 }
352 contourList->globalState()->debugSetCheckHealth(false);
353#if DEBUG_ACTIVE_SPANS
354 SkDebugf("active after %s:\n", id);
355 ShowActiveSpans(contourList);
356#endif
357#endif
caryclark26ad22a2015-10-16 09:03:38 -0700358}
359#endif
360
361#if defined SK_DEBUG || !FORCE_RELEASE
caryclark@google.com570863f2013-09-16 15:55:01 +0000362void SkPathOpsDebug::MathematicaIze(char* str, size_t bufferLen) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000363 size_t len = strlen(str);
364 bool num = false;
365 for (size_t idx = 0; idx < len; ++idx) {
366 if (num && str[idx] == 'e') {
367 if (len + 2 >= bufferLen) {
368 return;
369 }
370 memmove(&str[idx + 2], &str[idx + 1], len - idx);
371 str[idx] = '*';
372 str[idx + 1] = '^';
373 ++len;
374 }
375 num = str[idx] >= '0' && str[idx] <= '9';
376 }
377}
378
caryclark@google.com570863f2013-09-16 15:55:01 +0000379bool SkPathOpsDebug::ValidWind(int wind) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000380 return wind > SK_MinS32 + 0xFFFF && wind < SK_MaxS32 - 0xFFFF;
381}
382
caryclark@google.com570863f2013-09-16 15:55:01 +0000383void SkPathOpsDebug::WindingPrintf(int wind) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000384 if (wind == SK_MinS32) {
385 SkDebugf("?");
386 } else {
387 SkDebugf("%d", wind);
388 }
389}
caryclark54359292015-03-26 07:52:43 -0700390#endif // defined SK_DEBUG || !FORCE_RELEASE
391
caryclark@google.coma5e55922013-05-07 18:51:31 +0000392
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000393#if DEBUG_SHOW_TEST_NAME
halcanary385fe4d2015-08-26 13:07:48 -0700394void* SkPathOpsDebug::CreateNameStr() { return new char[DEBUG_FILENAME_STRING_LENGTH]; }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000395
halcanary385fe4d2015-08-26 13:07:48 -0700396void SkPathOpsDebug::DeleteNameStr(void* v) { delete[] reinterpret_cast<char*>(v); }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000397
caryclark@google.com570863f2013-09-16 15:55:01 +0000398void SkPathOpsDebug::BumpTestName(char* test) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000399 char* num = test + strlen(test);
400 while (num[-1] >= '0' && num[-1] <= '9') {
401 --num;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000402 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000403 if (num[0] == '\0') {
404 return;
405 }
406 int dec = atoi(num);
407 if (dec == 0) {
408 return;
409 }
410 ++dec;
411 SK_SNPRINTF(num, DEBUG_FILENAME_STRING_LENGTH - (num - test), "%d", dec);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000412}
413#endif
caryclark@google.com570863f2013-09-16 15:55:01 +0000414
caryclark1049f122015-04-20 08:31:59 -0700415static void show_function_header(const char* functionName) {
416 SkDebugf("\nstatic void %s(skiatest::Reporter* reporter, const char* filename) {\n", functionName);
417 if (strcmp("skphealth_com76", functionName) == 0) {
418 SkDebugf("found it\n");
419 }
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000420}
caryclark1049f122015-04-20 08:31:59 -0700421
422static const char* gOpStrs[] = {
423 "kDifference_SkPathOp",
424 "kIntersect_SkPathOp",
425 "kUnion_SkPathOp",
caryclark55888e42016-07-18 10:01:36 -0700426 "kXOR_PathOp",
caryclark1049f122015-04-20 08:31:59 -0700427 "kReverseDifference_SkPathOp",
428};
429
caryclark03b03ca2015-04-23 09:13:37 -0700430const char* SkPathOpsDebug::OpStr(SkPathOp op) {
431 return gOpStrs[op];
432}
433
caryclark1049f122015-04-20 08:31:59 -0700434static void show_op(SkPathOp op, const char* pathOne, const char* pathTwo) {
435 SkDebugf(" testPathOp(reporter, %s, %s, %s, filename);\n", pathOne, pathTwo, gOpStrs[op]);
436 SkDebugf("}\n");
437}
438
reed086eea92016-05-04 17:12:46 -0700439SK_DECLARE_STATIC_MUTEX(gTestMutex);
caryclark1049f122015-04-20 08:31:59 -0700440
441void SkPathOpsDebug::ShowPath(const SkPath& a, const SkPath& b, SkPathOp shapeOp,
442 const char* testName) {
443 SkAutoMutexAcquire ac(gTestMutex);
444 show_function_header(testName);
445 ShowOnePath(a, "path", true);
446 ShowOnePath(b, "pathB", true);
447 show_op(shapeOp, "path", "pathB");
448}
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000449
caryclark27c8eb82015-07-06 11:38:33 -0700450#include "SkPathOpsTypes.h"
caryclark26ad22a2015-10-16 09:03:38 -0700451#include "SkIntersectionHelper.h"
452#include "SkIntersections.h"
453
454#if DEBUG_T_SECT_LOOP_COUNT
455void SkOpGlobalState::debugAddLoopCount(SkIntersections* i, const SkIntersectionHelper& wt,
456 const SkIntersectionHelper& wn) {
457 for (int index = 0; index < (int) SK_ARRAY_COUNT(fDebugLoopCount); ++index) {
458 SkIntersections::DebugLoop looper = (SkIntersections::DebugLoop) index;
459 if (fDebugLoopCount[index] >= i->debugLoopCount(looper)) {
460 continue;
461 }
462 fDebugLoopCount[index] = i->debugLoopCount(looper);
463 fDebugWorstVerb[index * 2] = wt.segment()->verb();
464 fDebugWorstVerb[index * 2 + 1] = wn.segment()->verb();
465 sk_bzero(&fDebugWorstPts[index * 8], sizeof(SkPoint) * 8);
466 memcpy(&fDebugWorstPts[index * 2 * 4], wt.pts(),
467 (SkPathOpsVerbToPoints(wt.segment()->verb()) + 1) * sizeof(SkPoint));
468 memcpy(&fDebugWorstPts[(index * 2 + 1) * 4], wn.pts(),
469 (SkPathOpsVerbToPoints(wn.segment()->verb()) + 1) * sizeof(SkPoint));
470 fDebugWorstWeight[index * 2] = wt.weight();
471 fDebugWorstWeight[index * 2 + 1] = wn.weight();
472 }
473 i->debugResetLoopCount();
474}
475
476void SkOpGlobalState::debugDoYourWorst(SkOpGlobalState* local) {
477 for (int index = 0; index < (int) SK_ARRAY_COUNT(fDebugLoopCount); ++index) {
478 if (fDebugLoopCount[index] >= local->fDebugLoopCount[index]) {
479 continue;
480 }
481 fDebugLoopCount[index] = local->fDebugLoopCount[index];
482 fDebugWorstVerb[index * 2] = local->fDebugWorstVerb[index * 2];
483 fDebugWorstVerb[index * 2 + 1] = local->fDebugWorstVerb[index * 2 + 1];
484 memcpy(&fDebugWorstPts[index * 2 * 4], &local->fDebugWorstPts[index * 2 * 4],
485 sizeof(SkPoint) * 8);
486 fDebugWorstWeight[index * 2] = local->fDebugWorstWeight[index * 2];
487 fDebugWorstWeight[index * 2 + 1] = local->fDebugWorstWeight[index * 2 + 1];
488 }
489 local->debugResetLoopCounts();
490}
491
492static void dump_curve(SkPath::Verb verb, const SkPoint& pts, float weight) {
493 if (!verb) {
494 return;
495 }
496 const char* verbs[] = { "", "line", "quad", "conic", "cubic" };
497 SkDebugf("%s: {{", verbs[verb]);
498 int ptCount = SkPathOpsVerbToPoints(verb);
499 for (int index = 0; index <= ptCount; ++index) {
500 SkDPoint::Dump((&pts)[index]);
501 if (index < ptCount - 1) {
502 SkDebugf(", ");
503 }
504 }
505 SkDebugf("}");
506 if (weight != 1) {
507 SkDebugf(", ");
508 if (weight == floorf(weight)) {
509 SkDebugf("%.0f", weight);
510 } else {
511 SkDebugf("%1.9gf", weight);
512 }
513 }
514 SkDebugf("}\n");
515}
516
517void SkOpGlobalState::debugLoopReport() {
518 const char* loops[] = { "iterations", "coinChecks", "perpCalcs" };
519 SkDebugf("\n");
520 for (int index = 0; index < (int) SK_ARRAY_COUNT(fDebugLoopCount); ++index) {
521 SkDebugf("%s: %d\n", loops[index], fDebugLoopCount[index]);
522 dump_curve(fDebugWorstVerb[index * 2], fDebugWorstPts[index * 2 * 4],
523 fDebugWorstWeight[index * 2]);
524 dump_curve(fDebugWorstVerb[index * 2 + 1], fDebugWorstPts[(index * 2 + 1) * 4],
525 fDebugWorstWeight[index * 2 + 1]);
526 }
527}
528
529void SkOpGlobalState::debugResetLoopCounts() {
530 sk_bzero(fDebugLoopCount, sizeof(fDebugLoopCount));
531 sk_bzero(fDebugWorstVerb, sizeof(fDebugWorstVerb));
532 sk_bzero(fDebugWorstPts, sizeof(fDebugWorstPts));
533 sk_bzero(fDebugWorstWeight, sizeof(fDebugWorstWeight));
534}
535#endif
caryclark27c8eb82015-07-06 11:38:33 -0700536
537#ifdef SK_DEBUG
538bool SkOpGlobalState::debugRunFail() const {
539#if DEBUG_VALIDATE
540 return FLAGS_runFail;
541#else
542 return false;
543#endif
544}
545#endif
546
caryclark26ad22a2015-10-16 09:03:38 -0700547#if DEBUG_T_SECT_LOOP_COUNT
548void SkIntersections::debugBumpLoopCount(DebugLoop index) {
549 fDebugLoopCount[index]++;
550}
551
552int SkIntersections::debugLoopCount(DebugLoop index) const {
553 return fDebugLoopCount[index];
554}
555
556void SkIntersections::debugResetLoopCount() {
557 sk_bzero(fDebugLoopCount, sizeof(fDebugLoopCount));
558}
559#endif
560
caryclark624637c2015-05-11 07:21:27 -0700561#include "SkPathOpsCubic.h"
562#include "SkPathOpsQuad.h"
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000563
caryclark624637c2015-05-11 07:21:27 -0700564SkDCubic SkDQuad::debugToCubic() const {
565 SkDCubic cubic;
566 cubic[0] = fPts[0];
567 cubic[2] = fPts[1];
568 cubic[3] = fPts[2];
569 cubic[1].fX = (cubic[0].fX + cubic[2].fX * 2) / 3;
570 cubic[1].fY = (cubic[0].fY + cubic[2].fY * 2) / 3;
571 cubic[2].fX = (cubic[3].fX + cubic[2].fX * 2) / 3;
572 cubic[2].fY = (cubic[3].fY + cubic[2].fY * 2) / 3;
573 return cubic;
caryclark54359292015-03-26 07:52:43 -0700574}
caryclark624637c2015-05-11 07:21:27 -0700575
caryclarked0935a2015-10-22 07:23:52 -0700576void SkDRect::debugInit() {
577 fLeft = fTop = fRight = fBottom = SK_ScalarNaN;
578}
579
caryclark624637c2015-05-11 07:21:27 -0700580#include "SkOpAngle.h"
caryclark624637c2015-05-11 07:21:27 -0700581#include "SkOpSegment.h"
caryclark54359292015-03-26 07:52:43 -0700582
caryclark26ad22a2015-10-16 09:03:38 -0700583#if DEBUG_COINCIDENCE
caryclark55888e42016-07-18 10:01:36 -0700584// commented-out lines keep this in sync with addT()
caryclark29b25632016-08-25 11:27:17 -0700585 const SkOpPtT* SkOpSegment::debugAddT(double t) const {
caryclark55888e42016-07-18 10:01:36 -0700586 debugValidate();
587 SkPoint pt = this->ptAtT(t);
caryclark26ad22a2015-10-16 09:03:38 -0700588 const SkOpSpanBase* span = &fHead;
caryclark55888e42016-07-18 10:01:36 -0700589 do {
590 const SkOpPtT* result = span->ptT();
caryclark29b25632016-08-25 11:27:17 -0700591 if (t == result->fT || this->match(result, this, t, pt)) {
caryclark55888e42016-07-18 10:01:36 -0700592// span->bumpSpanAdds();
caryclarkef4f32a2016-08-24 09:24:18 -0700593 return result;
caryclark26ad22a2015-10-16 09:03:38 -0700594 }
caryclark55888e42016-07-18 10:01:36 -0700595 if (t < result->fT) {
596 const SkOpSpan* prev = result->span()->prev();
caryclark29b25632016-08-25 11:27:17 -0700597 FAIL_WITH_NULL_IF(!prev);
598 // marks in global state that new op span has been allocated
599 this->globalState()->setAllocatedOpSpan();
caryclark55888e42016-07-18 10:01:36 -0700600// span->init(this, prev, t, pt);
601 this->debugValidate();
602// #if DEBUG_ADD_T
603// SkDebugf("%s insert t=%1.9g segID=%d spanID=%d\n", __FUNCTION__, t,
604// span->segment()->debugID(), span->debugID());
605// #endif
606// span->bumpSpanAdds();
caryclark55888e42016-07-18 10:01:36 -0700607 return nullptr;
caryclark26ad22a2015-10-16 09:03:38 -0700608 }
caryclark29b25632016-08-25 11:27:17 -0700609 FAIL_WITH_NULL_IF(span != &fTail);
caryclark55888e42016-07-18 10:01:36 -0700610 } while ((span = span->upCast()->next()));
611 SkASSERT(0);
caryclark29b25632016-08-25 11:27:17 -0700612 return nullptr; // we never get here, but need this to satisfy compiler
caryclark26ad22a2015-10-16 09:03:38 -0700613}
614#endif
615
616#if DEBUG_ANGLE
617void SkOpSegment::debugCheckAngleCoin() const {
618 const SkOpSpanBase* base = &fHead;
619 const SkOpSpan* span;
620 do {
621 const SkOpAngle* angle = base->fromAngle();
caryclark55888e42016-07-18 10:01:36 -0700622 if (angle && angle->debugCheckCoincidence()) {
caryclark26ad22a2015-10-16 09:03:38 -0700623 angle->debugCheckNearCoincidence();
624 }
625 if (base->final()) {
626 break;
627 }
628 span = base->upCast();
629 angle = span->toAngle();
caryclark55888e42016-07-18 10:01:36 -0700630 if (angle && angle->debugCheckCoincidence()) {
caryclark26ad22a2015-10-16 09:03:38 -0700631 angle->debugCheckNearCoincidence();
632 }
633 } while ((base = span->next()));
634}
635#endif
636
caryclark55888e42016-07-18 10:01:36 -0700637#if DEBUG_COINCIDENCE_VERBOSE
caryclark26ad22a2015-10-16 09:03:38 -0700638// this mimics the order of the checks in handle coincidence
639void SkOpSegment::debugCheckHealth(const char* id, SkPathOpsDebug::GlitchLog* glitches) const {
640 debugMoveMultiples(id, glitches);
caryclark26ad22a2015-10-16 09:03:38 -0700641 debugMoveNearby(id, glitches);
caryclark55888e42016-07-18 10:01:36 -0700642 debugMissingCoincidence(id, glitches);
caryclark26ad22a2015-10-16 09:03:38 -0700643}
644
caryclark55888e42016-07-18 10:01:36 -0700645// commented-out lines keep this in sync with clearAll()
646void SkOpSegment::debugClearAll(const char* id, SkPathOpsDebug::GlitchLog* glitches) const {
647 const SkOpSpan* span = &fHead;
648 do {
649 this->debugClearOne(span, id, glitches);
650 } while ((span = span->next()->upCastable()));
651 this->globalState()->coincidence()->debugRelease(id, glitches, this);
652}
653
654// commented-out lines keep this in sync with clearOne()
655void SkOpSegment::debugClearOne(const SkOpSpan* span, const char* id, SkPathOpsDebug::GlitchLog* glitches) const {
656 if (span->windValue()) glitches->record(kCollapsedWindValue_Glitch, id, span);
657 if (span->oppValue()) glitches->record(kCollapsedOppValue_Glitch, id, span);
658 if (!span->done()) glitches->record(kCollapsedDone_Glitch, id, span);
caryclark26ad22a2015-10-16 09:03:38 -0700659}
660#endif
661
caryclark54359292015-03-26 07:52:43 -0700662SkOpAngle* SkOpSegment::debugLastAngle() {
halcanary96fcdcc2015-08-27 07:41:13 -0700663 SkOpAngle* result = nullptr;
caryclark54359292015-03-26 07:52:43 -0700664 SkOpSpan* span = this->head();
665 do {
666 if (span->toAngle()) {
667 SkASSERT(!result);
668 result = span->toAngle();
669 }
670 } while ((span = span->next()->upCastable()));
671 SkASSERT(result);
672 return result;
673}
674
caryclark26ad22a2015-10-16 09:03:38 -0700675#if DEBUG_COINCIDENCE
caryclark55888e42016-07-18 10:01:36 -0700676// commented-out lines keep this in sync with ClearVisited
677void SkOpSegment::DebugClearVisited(const SkOpSpanBase* span) {
678 // reset visited flag back to false
679 do {
680 const SkOpPtT* ptT = span->ptT(), * stopPtT = ptT;
681 while ((ptT = ptT->next()) != stopPtT) {
682 const SkOpSegment* opp = ptT->segment();
683 opp->resetDebugVisited();
684 }
685 } while (!span->final() && (span = span->upCast()->next()));
686}
687#endif
688
689#if DEBUG_COINCIDENCE_VERBOSE
690// commented-out lines keep this in sync with missingCoincidence()
691// look for pairs of undetected coincident curves
692// assumes that segments going in have visited flag clear
693// Even though pairs of curves correct detect coincident runs, a run may be missed
694// if the coincidence is a product of multiple intersections. For instance, given
695// curves A, B, and C:
696// A-B intersect at a point 1; A-C and B-C intersect at point 2, so near
697// the end of C that the intersection is replaced with the end of C.
698// Even though A-B correctly do not detect an intersection at point 2,
699// the resulting run from point 1 to point 2 is coincident on A and B.
700void SkOpSegment::debugMissingCoincidence(const char* id, SkPathOpsDebug::GlitchLog* log) const {
caryclark26ad22a2015-10-16 09:03:38 -0700701 if (this->done()) {
702 return;
703 }
704 const SkOpSpan* prior = nullptr;
705 const SkOpSpanBase* spanBase = &fHead;
caryclark55888e42016-07-18 10:01:36 -0700706// bool result = false;
caryclark26ad22a2015-10-16 09:03:38 -0700707 do {
708 const SkOpPtT* ptT = spanBase->ptT(), * spanStopPtT = ptT;
709 SkASSERT(ptT->span() == spanBase);
710 while ((ptT = ptT->next()) != spanStopPtT) {
711 if (ptT->deleted()) {
712 continue;
713 }
caryclark55888e42016-07-18 10:01:36 -0700714 const SkOpSegment* opp = ptT->span()->segment();
caryclark26ad22a2015-10-16 09:03:38 -0700715 if (opp->done()) {
716 continue;
717 }
718 // when opp is encounted the 1st time, continue; on 2nd encounter, look for coincidence
caryclark55888e42016-07-18 10:01:36 -0700719 if (!opp->debugVisited()) {
caryclark26ad22a2015-10-16 09:03:38 -0700720 continue;
721 }
722 if (spanBase == &fHead) {
723 continue;
724 }
caryclark55888e42016-07-18 10:01:36 -0700725 if (ptT->segment() == this) {
726 continue;
727 }
caryclark26ad22a2015-10-16 09:03:38 -0700728 const SkOpSpan* span = spanBase->upCastable();
729 // FIXME?: this assumes that if the opposite segment is coincident then no more
730 // coincidence needs to be detected. This may not be true.
caryclark55888e42016-07-18 10:01:36 -0700731 if (span && span->segment() != opp && span->containsCoincidence(opp)) { // debug has additional condition since it may be called before inner duplicate points have been deleted
caryclark26ad22a2015-10-16 09:03:38 -0700732 continue;
733 }
caryclark55888e42016-07-18 10:01:36 -0700734 if (spanBase->segment() != opp && spanBase->containsCoinEnd(opp)) { // debug has additional condition since it may be called before inner duplicate points have been deleted
caryclark26ad22a2015-10-16 09:03:38 -0700735 continue;
caryclark55888e42016-07-18 10:01:36 -0700736 }
caryclark26ad22a2015-10-16 09:03:38 -0700737 const SkOpPtT* priorPtT = nullptr, * priorStopPtT;
738 // find prior span containing opp segment
739 const SkOpSegment* priorOpp = nullptr;
740 const SkOpSpan* priorTest = spanBase->prev();
741 while (!priorOpp && priorTest) {
742 priorStopPtT = priorPtT = priorTest->ptT();
743 while ((priorPtT = priorPtT->next()) != priorStopPtT) {
744 if (priorPtT->deleted()) {
745 continue;
746 }
747 SkOpSegment* segment = priorPtT->span()->segment();
748 if (segment == opp) {
749 prior = priorTest;
750 priorOpp = opp;
751 break;
752 }
753 }
754 priorTest = priorTest->prev();
755 }
756 if (!priorOpp) {
757 continue;
758 }
caryclark55888e42016-07-18 10:01:36 -0700759 if (priorPtT == ptT) {
760 continue;
761 }
caryclark26ad22a2015-10-16 09:03:38 -0700762 const SkOpPtT* oppStart = prior->ptT();
763 const SkOpPtT* oppEnd = spanBase->ptT();
764 bool swapped = priorPtT->fT > ptT->fT;
765 if (swapped) {
766 SkTSwap(priorPtT, ptT);
767 SkTSwap(oppStart, oppEnd);
768 }
caryclark55888e42016-07-18 10:01:36 -0700769 const SkOpCoincidence* coincidence = this->globalState()->coincidence();
770 const SkOpPtT* rootPriorPtT = priorPtT->span()->ptT();
771 const SkOpPtT* rootPtT = ptT->span()->ptT();
772 const SkOpPtT* rootOppStart = oppStart->span()->ptT();
773 const SkOpPtT* rootOppEnd = oppEnd->span()->ptT();
774 if (coincidence->contains(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd)) {
caryclark26ad22a2015-10-16 09:03:38 -0700775 goto swapBack;
776 }
caryclark55888e42016-07-18 10:01:36 -0700777 if (testForCoincidence(rootPriorPtT, rootPtT, prior, spanBase, opp)) {
778 // mark coincidence
779#if DEBUG_COINCIDENCE
780// SkDebugf("%s coinSpan=%d endSpan=%d oppSpan=%d oppEndSpan=%d\n", __FUNCTION__,
781// rootPriorPtT->debugID(), rootPtT->debugID(), rootOppStart->debugID(),
782// rootOppEnd->debugID());
783#endif
caryclark26ad22a2015-10-16 09:03:38 -0700784 log->record(kMissingCoin_Glitch, id, priorPtT, ptT, oppStart, oppEnd);
caryclark55888e42016-07-18 10:01:36 -0700785 // coincidences->add(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd);
786 // }
787#if DEBUG_COINCIDENCE
caryclarkd6562002016-07-27 12:02:07 -0700788// SkASSERT(coincidences->contains(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd);
caryclark55888e42016-07-18 10:01:36 -0700789#endif
790 // result = true;
caryclark26ad22a2015-10-16 09:03:38 -0700791 }
792 swapBack:
793 if (swapped) {
794 SkTSwap(priorPtT, ptT);
795 }
796 }
797 } while ((spanBase = spanBase->final() ? nullptr : spanBase->upCast()->next()));
caryclark55888e42016-07-18 10:01:36 -0700798 DebugClearVisited(&fHead);
799 return;
caryclark26ad22a2015-10-16 09:03:38 -0700800}
801
caryclark55888e42016-07-18 10:01:36 -0700802// commented-out lines keep this in sync with moveMultiples()
803// if a span has more than one intersection, merge the other segments' span as needed
caryclark26ad22a2015-10-16 09:03:38 -0700804void SkOpSegment::debugMoveMultiples(const char* id, SkPathOpsDebug::GlitchLog* glitches) const {
caryclark55888e42016-07-18 10:01:36 -0700805 debugValidate();
caryclark26ad22a2015-10-16 09:03:38 -0700806 const SkOpSpanBase* test = &fHead;
807 do {
808 int addCount = test->spanAddsCount();
809 SkASSERT(addCount >= 1);
810 if (addCount == 1) {
811 continue;
812 }
813 const SkOpPtT* startPtT = test->ptT();
814 const SkOpPtT* testPtT = startPtT;
815 do { // iterate through all spans associated with start
816 const SkOpSpanBase* oppSpan = testPtT->span();
817 if (oppSpan->spanAddsCount() == addCount) {
818 continue;
819 }
820 if (oppSpan->deleted()) {
821 continue;
822 }
823 const SkOpSegment* oppSegment = oppSpan->segment();
824 if (oppSegment == this) {
825 continue;
826 }
827 // find range of spans to consider merging
828 const SkOpSpanBase* oppPrev = oppSpan;
829 const SkOpSpanBase* oppFirst = oppSpan;
830 while ((oppPrev = oppPrev->prev())) {
831 if (!roughly_equal(oppPrev->t(), oppSpan->t())) {
832 break;
833 }
834 if (oppPrev->spanAddsCount() == addCount) {
835 continue;
836 }
837 if (oppPrev->deleted()) {
838 continue;
839 }
840 oppFirst = oppPrev;
841 }
842 const SkOpSpanBase* oppNext = oppSpan;
843 const SkOpSpanBase* oppLast = oppSpan;
844 while ((oppNext = oppNext->final() ? nullptr : oppNext->upCast()->next())) {
845 if (!roughly_equal(oppNext->t(), oppSpan->t())) {
846 break;
847 }
848 if (oppNext->spanAddsCount() == addCount) {
849 continue;
850 }
851 if (oppNext->deleted()) {
852 continue;
853 }
854 oppLast = oppNext;
855 }
856 if (oppFirst == oppLast) {
857 continue;
858 }
859 const SkOpSpanBase* oppTest = oppFirst;
860 do {
861 if (oppTest == oppSpan) {
862 continue;
863 }
864 // check to see if the candidate meets specific criteria:
865 // it contains spans of segments in test's loop but not including 'this'
866 const SkOpPtT* oppStartPtT = oppTest->ptT();
867 const SkOpPtT* oppPtT = oppStartPtT;
868 while ((oppPtT = oppPtT->next()) != oppStartPtT) {
869 const SkOpSegment* oppPtTSegment = oppPtT->segment();
870 if (oppPtTSegment == this) {
871 goto tryNextSpan;
872 }
873 const SkOpPtT* matchPtT = startPtT;
874 do {
875 if (matchPtT->segment() == oppPtTSegment) {
876 goto foundMatch;
877 }
878 } while ((matchPtT = matchPtT->next()) != startPtT);
879 goto tryNextSpan;
880 foundMatch: // merge oppTest and oppSpan
caryclark55888e42016-07-18 10:01:36 -0700881 oppSegment->debugValidate();
caryclark26ad22a2015-10-16 09:03:38 -0700882 if (oppTest == &oppSegment->fTail || oppTest == &oppSegment->fHead) {
883 SkASSERT(oppSpan != &oppSegment->fHead); // don't expect collapse
884 SkASSERT(oppSpan != &oppSegment->fTail);
885 glitches->record(kMoveMultiple_Glitch, id, oppTest, oppSpan);
886 } else {
887 glitches->record(kMoveMultiple_Glitch, id, oppSpan, oppTest);
888 }
caryclark55888e42016-07-18 10:01:36 -0700889 oppSegment->debugValidate();
caryclark26ad22a2015-10-16 09:03:38 -0700890 goto checkNextSpan;
891 }
caryclark55888e42016-07-18 10:01:36 -0700892 tryNextSpan:
caryclark26ad22a2015-10-16 09:03:38 -0700893 ;
894 } while (oppTest != oppLast && (oppTest = oppTest->upCast()->next()));
895 } while ((testPtT = testPtT->next()) != startPtT);
caryclark55888e42016-07-18 10:01:36 -0700896checkNextSpan:
caryclark26ad22a2015-10-16 09:03:38 -0700897 ;
898 } while ((test = test->final() ? nullptr : test->upCast()->next()));
caryclark55888e42016-07-18 10:01:36 -0700899 debugValidate();
900 return;
caryclark26ad22a2015-10-16 09:03:38 -0700901}
902
caryclark55888e42016-07-18 10:01:36 -0700903// commented-out lines keep this in sync with moveNearby()
904// Move nearby t values and pts so they all hang off the same span. Alignment happens later.
caryclark26ad22a2015-10-16 09:03:38 -0700905void SkOpSegment::debugMoveNearby(const char* id, SkPathOpsDebug::GlitchLog* glitches) const {
caryclark55888e42016-07-18 10:01:36 -0700906 debugValidate();
907 // release undeleted spans pointing to this seg that are linked to the primary span
908 const SkOpSpanBase* spanBase = &fHead;
caryclark26ad22a2015-10-16 09:03:38 -0700909 do {
caryclark55888e42016-07-18 10:01:36 -0700910 const SkOpPtT* ptT = spanBase->ptT();
911 const SkOpPtT* headPtT = ptT;
912 while ((ptT = ptT->next()) != headPtT) {
913 const SkOpSpanBase* test = ptT->span();
914 if (ptT->segment() == this && !ptT->deleted() && test != spanBase
915 && test->ptT() == ptT) {
916 if (test->final()) {
917 if (spanBase == &fHead) {
918 glitches->record(kMoveNearbyClearAll_Glitch, id, this);
919// return;
920 }
921 glitches->record(kMoveNearbyReleaseFinal_Glitch, id, spanBase, ptT);
922 } else if (test->prev()) {
923 glitches->record(kMoveNearbyRelease_Glitch, id, test, headPtT);
924 }
925// break;
caryclark26ad22a2015-10-16 09:03:38 -0700926 }
927 }
caryclark55888e42016-07-18 10:01:36 -0700928 spanBase = spanBase->upCast()->next();
929 } while (!spanBase->final());
930
931 // This loop looks for adjacent spans which are near by
932 spanBase = &fHead;
933 do { // iterate through all spans associated with start
934 const SkOpSpanBase* test = spanBase->upCast()->next();
935 if (this->spansNearby(spanBase, test)) {
936 if (test->final()) {
937 if (spanBase->prev()) {
938 glitches->record(kMoveNearbyMergeFinal_Glitch, id, test);
939 } else {
940 glitches->record(kMoveNearbyClearAll2_Glitch, id, this);
941 // return
942 }
943 } else {
944 glitches->record(kMoveNearbyMerge_Glitch, id, spanBase);
945 }
946 }
947 spanBase = test;
948 } while (!spanBase->final());
949 debugValidate();
caryclark26ad22a2015-10-16 09:03:38 -0700950}
951#endif
952
caryclark54359292015-03-26 07:52:43 -0700953void SkOpSegment::debugReset() {
caryclark1049f122015-04-20 08:31:59 -0700954 this->init(this->fPts, this->fWeight, this->contour(), this->verb());
caryclark54359292015-03-26 07:52:43 -0700955}
956
caryclark025b11e2016-08-25 05:21:14 -0700957#if DEBUG_COINCIDENCE_ORDER
958void SkOpSegment::debugSetCoinT(int index, SkScalar t) const {
959 if (fDebugBaseMax < 0 || fDebugBaseIndex == index) {
960 fDebugBaseIndex = index;
961 fDebugBaseMin = SkTMin(t, fDebugBaseMin);
962 fDebugBaseMax = SkTMax(t, fDebugBaseMax);
963 return;
964 }
965 SkASSERT(fDebugBaseMin >= t || t >= fDebugBaseMax);
966 if (fDebugLastMax < 0 || fDebugLastIndex == index) {
967 fDebugLastIndex = index;
968 fDebugLastMin = SkTMin(t, fDebugLastMin);
969 fDebugLastMax = SkTMax(t, fDebugLastMax);
970 return;
971 }
972 SkASSERT(fDebugLastMin >= t || t >= fDebugLastMax);
973 SkASSERT((t - fDebugBaseMin > 0) == (fDebugLastMin - fDebugBaseMin > 0));
974}
975#endif
976
caryclark54359292015-03-26 07:52:43 -0700977#if DEBUG_ACTIVE_SPANS
978void SkOpSegment::debugShowActiveSpans() const {
979 debugValidate();
980 if (done()) {
981 return;
982 }
983 int lastId = -1;
984 double lastT = -1;
985 const SkOpSpan* span = &fHead;
986 do {
987 if (span->done()) {
988 continue;
989 }
caryclark1049f122015-04-20 08:31:59 -0700990 if (lastId == this->debugID() && lastT == span->t()) {
caryclark54359292015-03-26 07:52:43 -0700991 continue;
992 }
caryclark1049f122015-04-20 08:31:59 -0700993 lastId = this->debugID();
caryclark54359292015-03-26 07:52:43 -0700994 lastT = span->t();
caryclark1049f122015-04-20 08:31:59 -0700995 SkDebugf("%s id=%d", __FUNCTION__, this->debugID());
caryclark55888e42016-07-18 10:01:36 -0700996 // since endpoints may have be adjusted, show actual computed curves
997 SkDCurve curvePart;
998 this->subDivide(span, span->next(), &curvePart);
999 const SkDPoint* pts = curvePart.fCubic.fPts;
1000 SkDebugf(" (%1.9g,%1.9g", pts[0].fX, pts[0].fY);
caryclark54359292015-03-26 07:52:43 -07001001 for (int vIndex = 1; vIndex <= SkPathOpsVerbToPoints(fVerb); ++vIndex) {
caryclark55888e42016-07-18 10:01:36 -07001002 SkDebugf(" %1.9g,%1.9g", pts[vIndex].fX, pts[vIndex].fY);
caryclark54359292015-03-26 07:52:43 -07001003 }
caryclark1049f122015-04-20 08:31:59 -07001004 if (SkPath::kConic_Verb == fVerb) {
caryclark55888e42016-07-18 10:01:36 -07001005 SkDebugf(" %1.9gf", curvePart.fConic.fWeight);
caryclark1049f122015-04-20 08:31:59 -07001006 }
caryclark55888e42016-07-18 10:01:36 -07001007 SkDebugf(") t=%1.9g tEnd=%1.9g", span->t(), span->next()->t());
caryclark54359292015-03-26 07:52:43 -07001008 if (span->windSum() == SK_MinS32) {
caryclark624637c2015-05-11 07:21:27 -07001009 SkDebugf(" windSum=?");
caryclark54359292015-03-26 07:52:43 -07001010 } else {
caryclark624637c2015-05-11 07:21:27 -07001011 SkDebugf(" windSum=%d", span->windSum());
caryclark54359292015-03-26 07:52:43 -07001012 }
caryclark624637c2015-05-11 07:21:27 -07001013 if (span->oppValue() && span->oppSum() == SK_MinS32) {
1014 SkDebugf(" oppSum=?");
1015 } else if (span->oppValue() || span->oppSum() != SK_MinS32) {
1016 SkDebugf(" oppSum=%d", span->oppSum());
1017 }
1018 SkDebugf(" windValue=%d", span->windValue());
1019 if (span->oppValue() || span->oppSum() != SK_MinS32) {
1020 SkDebugf(" oppValue=%d", span->oppValue());
1021 }
caryclark54359292015-03-26 07:52:43 -07001022 SkDebugf("\n");
1023 } while ((span = span->next()->upCastable()));
1024}
1025#endif
1026
1027#if DEBUG_MARK_DONE
1028void SkOpSegment::debugShowNewWinding(const char* fun, const SkOpSpan* span, int winding) {
1029 const SkPoint& pt = span->ptT()->fPt;
caryclark1049f122015-04-20 08:31:59 -07001030 SkDebugf("%s id=%d", fun, this->debugID());
caryclark54359292015-03-26 07:52:43 -07001031 SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY);
1032 for (int vIndex = 1; vIndex <= SkPathOpsVerbToPoints(fVerb); ++vIndex) {
1033 SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY);
1034 }
1035 SkDebugf(") t=%1.9g [%d] (%1.9g,%1.9g) tEnd=%1.9g newWindSum=",
1036 span->t(), span->debugID(), pt.fX, pt.fY, span->next()->t());
1037 if (winding == SK_MinS32) {
1038 SkDebugf("?");
1039 } else {
1040 SkDebugf("%d", winding);
1041 }
1042 SkDebugf(" windSum=");
1043 if (span->windSum() == SK_MinS32) {
1044 SkDebugf("?");
1045 } else {
1046 SkDebugf("%d", span->windSum());
1047 }
1048 SkDebugf(" windValue=%d\n", span->windValue());
1049}
1050
1051void SkOpSegment::debugShowNewWinding(const char* fun, const SkOpSpan* span, int winding,
1052 int oppWinding) {
1053 const SkPoint& pt = span->ptT()->fPt;
caryclark1049f122015-04-20 08:31:59 -07001054 SkDebugf("%s id=%d", fun, this->debugID());
caryclark54359292015-03-26 07:52:43 -07001055 SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY);
1056 for (int vIndex = 1; vIndex <= SkPathOpsVerbToPoints(fVerb); ++vIndex) {
1057 SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY);
1058 }
1059 SkDebugf(") t=%1.9g [%d] (%1.9g,%1.9g) tEnd=%1.9g newWindSum=",
1060 span->t(), span->debugID(), pt.fX, pt.fY, span->next()->t(), winding, oppWinding);
1061 if (winding == SK_MinS32) {
1062 SkDebugf("?");
1063 } else {
1064 SkDebugf("%d", winding);
1065 }
1066 SkDebugf(" newOppSum=");
1067 if (oppWinding == SK_MinS32) {
1068 SkDebugf("?");
1069 } else {
1070 SkDebugf("%d", oppWinding);
1071 }
1072 SkDebugf(" oppSum=");
1073 if (span->oppSum() == SK_MinS32) {
1074 SkDebugf("?");
1075 } else {
1076 SkDebugf("%d", span->oppSum());
1077 }
1078 SkDebugf(" windSum=");
1079 if (span->windSum() == SK_MinS32) {
1080 SkDebugf("?");
1081 } else {
1082 SkDebugf("%d", span->windSum());
1083 }
1084 SkDebugf(" windValue=%d oppValue=%d\n", span->windValue(), span->oppValue());
1085}
1086
1087#endif
1088
caryclark26ad22a2015-10-16 09:03:38 -07001089// loop looking for a pair of angle parts that are too close to be sorted
1090/* This is called after other more simple intersection and angle sorting tests have been exhausted.
1091 This should be rarely called -- the test below is thorough and time consuming.
caryclark55888e42016-07-18 10:01:36 -07001092 This checks the distance between start points; the distance between
caryclark26ad22a2015-10-16 09:03:38 -07001093*/
1094#if DEBUG_ANGLE
1095void SkOpAngle::debugCheckNearCoincidence() const {
1096 const SkOpAngle* test = this;
1097 do {
1098 const SkOpSegment* testSegment = test->segment();
1099 double testStartT = test->start()->t();
1100 SkDPoint testStartPt = testSegment->dPtAtT(testStartT);
1101 double testEndT = test->end()->t();
1102 SkDPoint testEndPt = testSegment->dPtAtT(testEndT);
1103 double testLenSq = testStartPt.distanceSquared(testEndPt);
1104 SkDebugf("%s testLenSq=%1.9g id=%d\n", __FUNCTION__, testLenSq, testSegment->debugID());
1105 double testMidT = (testStartT + testEndT) / 2;
1106 const SkOpAngle* next = test;
1107 while ((next = next->fNext) != this) {
1108 SkOpSegment* nextSegment = next->segment();
1109 double testMidDistSq = testSegment->distSq(testMidT, next);
1110 double testEndDistSq = testSegment->distSq(testEndT, next);
1111 double nextStartT = next->start()->t();
1112 SkDPoint nextStartPt = nextSegment->dPtAtT(nextStartT);
1113 double distSq = testStartPt.distanceSquared(nextStartPt);
1114 double nextEndT = next->end()->t();
1115 double nextMidT = (nextStartT + nextEndT) / 2;
1116 double nextMidDistSq = nextSegment->distSq(nextMidT, test);
1117 double nextEndDistSq = nextSegment->distSq(nextEndT, test);
1118 SkDebugf("%s distSq=%1.9g testId=%d nextId=%d\n", __FUNCTION__, distSq,
1119 testSegment->debugID(), nextSegment->debugID());
1120 SkDebugf("%s testMidDistSq=%1.9g\n", __FUNCTION__, testMidDistSq);
1121 SkDebugf("%s testEndDistSq=%1.9g\n", __FUNCTION__, testEndDistSq);
1122 SkDebugf("%s nextMidDistSq=%1.9g\n", __FUNCTION__, nextMidDistSq);
1123 SkDebugf("%s nextEndDistSq=%1.9g\n", __FUNCTION__, nextEndDistSq);
1124 SkDPoint nextEndPt = nextSegment->dPtAtT(nextEndT);
1125 double nextLenSq = nextStartPt.distanceSquared(nextEndPt);
1126 SkDebugf("%s nextLenSq=%1.9g\n", __FUNCTION__, nextLenSq);
1127 SkDebugf("\n");
1128 }
1129 test = test->fNext;
caryclark55888e42016-07-18 10:01:36 -07001130 } while (test->fNext != this);
caryclark26ad22a2015-10-16 09:03:38 -07001131}
1132#endif
1133
caryclark54359292015-03-26 07:52:43 -07001134#if DEBUG_ANGLE
1135SkString SkOpAngle::debugPart() const {
1136 SkString result;
1137 switch (this->segment()->verb()) {
1138 case SkPath::kLine_Verb:
1139 result.printf(LINE_DEBUG_STR " id=%d", LINE_DEBUG_DATA(fCurvePart),
1140 this->segment()->debugID());
1141 break;
1142 case SkPath::kQuad_Verb:
1143 result.printf(QUAD_DEBUG_STR " id=%d", QUAD_DEBUG_DATA(fCurvePart),
1144 this->segment()->debugID());
1145 break;
caryclark1049f122015-04-20 08:31:59 -07001146 case SkPath::kConic_Verb:
1147 result.printf(CONIC_DEBUG_STR " id=%d",
1148 CONIC_DEBUG_DATA(fCurvePart, fCurvePart.fConic.fWeight),
1149 this->segment()->debugID());
1150 break;
caryclark54359292015-03-26 07:52:43 -07001151 case SkPath::kCubic_Verb:
1152 result.printf(CUBIC_DEBUG_STR " id=%d", CUBIC_DEBUG_DATA(fCurvePart),
1153 this->segment()->debugID());
1154 break;
1155 default:
1156 SkASSERT(0);
mtklein1b249332015-07-07 12:21:21 -07001157 }
caryclark54359292015-03-26 07:52:43 -07001158 return result;
1159}
1160#endif
1161
caryclark624637c2015-05-11 07:21:27 -07001162#if DEBUG_SORT
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001163void SkOpAngle::debugLoop() const {
caryclarke4097e32014-06-18 07:24:19 -07001164 const SkOpAngle* first = this;
1165 const SkOpAngle* next = this;
1166 do {
1167 next->dumpOne(true);
caryclark19eb3b22014-07-18 05:08:14 -07001168 SkDebugf("\n");
caryclarke4097e32014-06-18 07:24:19 -07001169 next = next->fNext;
1170 } while (next && next != first);
caryclark54359292015-03-26 07:52:43 -07001171 next = first;
1172 do {
1173 next->debugValidate();
1174 next = next->fNext;
1175 } while (next && next != first);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001176}
1177#endif
1178
caryclark54359292015-03-26 07:52:43 -07001179void SkOpAngle::debugValidate() const {
caryclark55888e42016-07-18 10:01:36 -07001180#if DEBUG_COINCIDENCE
1181 if (this->globalState()->debugCheckHealth()) {
1182 return;
1183 }
1184#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001185#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07001186 const SkOpAngle* first = this;
1187 const SkOpAngle* next = this;
1188 int wind = 0;
1189 int opp = 0;
1190 int lastXor = -1;
1191 int lastOppXor = -1;
1192 do {
1193 if (next->unorderable()) {
1194 return;
1195 }
1196 const SkOpSpan* minSpan = next->start()->starter(next->end());
1197 if (minSpan->windValue() == SK_MinS32) {
1198 return;
1199 }
1200 bool op = next->segment()->operand();
1201 bool isXor = next->segment()->isXor();
1202 bool oppXor = next->segment()->oppXor();
1203 SkASSERT(!DEBUG_LIMIT_WIND_SUM || between(0, minSpan->windValue(), DEBUG_LIMIT_WIND_SUM));
1204 SkASSERT(!DEBUG_LIMIT_WIND_SUM
1205 || between(-DEBUG_LIMIT_WIND_SUM, minSpan->oppValue(), DEBUG_LIMIT_WIND_SUM));
1206 bool useXor = op ? oppXor : isXor;
1207 SkASSERT(lastXor == -1 || lastXor == (int) useXor);
1208 lastXor = (int) useXor;
caryclark624637c2015-05-11 07:21:27 -07001209 wind += next->debugSign() * (op ? minSpan->oppValue() : minSpan->windValue());
caryclark54359292015-03-26 07:52:43 -07001210 if (useXor) {
1211 wind &= 1;
1212 }
1213 useXor = op ? isXor : oppXor;
1214 SkASSERT(lastOppXor == -1 || lastOppXor == (int) useXor);
1215 lastOppXor = (int) useXor;
caryclark624637c2015-05-11 07:21:27 -07001216 opp += next->debugSign() * (op ? minSpan->windValue() : minSpan->oppValue());
caryclark54359292015-03-26 07:52:43 -07001217 if (useXor) {
1218 opp &= 1;
1219 }
1220 next = next->fNext;
1221 } while (next && next != first);
caryclark182b4992015-05-14 05:45:54 -07001222 SkASSERT(wind == 0 || !FLAGS_runFail);
caryclark54359292015-03-26 07:52:43 -07001223 SkASSERT(opp == 0 || !FLAGS_runFail);
1224#endif
1225}
1226
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001227void SkOpAngle::debugValidateNext() const {
caryclark54359292015-03-26 07:52:43 -07001228#if !FORCE_RELEASE
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001229 const SkOpAngle* first = this;
1230 const SkOpAngle* next = first;
1231 SkTDArray<const SkOpAngle*>(angles);
1232 do {
djsollenf2b340f2016-01-29 08:51:04 -08001233// SkASSERT_RELEASE(next->fSegment->debugContains(next));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001234 angles.push(next);
1235 next = next->next();
1236 if (next == first) {
1237 break;
1238 }
djsollenf2b340f2016-01-29 08:51:04 -08001239 SkASSERT_RELEASE(!angles.contains(next));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001240 if (!next) {
1241 return;
1242 }
1243 } while (true);
reed0dc4dd62015-03-24 13:55:33 -07001244#endif
reed0dc4dd62015-03-24 13:55:33 -07001245}
reed0dc4dd62015-03-24 13:55:33 -07001246
caryclark55888e42016-07-18 10:01:36 -07001247#ifdef SK_DEBUG
1248void SkCoincidentSpans::debugStartCheck(const SkOpSpanBase* outer, const SkOpSpanBase* over,
1249 const SkOpGlobalState* debugState) const {
1250 SkASSERT(coinPtTEnd()->span() == over || !debugState->debugRunFail());
1251 SkASSERT(oppPtTEnd()->span() == outer || !debugState->debugRunFail());
1252}
1253#endif
caryclark26ad22a2015-10-16 09:03:38 -07001254
caryclark55888e42016-07-18 10:01:36 -07001255#if DEBUG_COINCIDENCE_VERBOSE
1256/* Commented-out lines keep this in sync with expand */
1257bool SkCoincidentSpans::debugExpand(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1258 bool expanded = false;
1259 const SkOpSegment* segment = coinPtTStart()->segment();
1260 const SkOpSegment* oppSegment = oppPtTStart()->segment();
1261 do {
1262 const SkOpSpan* start = coinPtTStart()->span()->upCast();
1263 const SkOpSpan* prev = start->prev();
1264 const SkOpPtT* oppPtT;
1265 if (!prev || !(oppPtT = prev->contains(oppSegment))) {
1266 break;
1267 }
1268 double midT = (prev->t() + start->t()) / 2;
1269 if (!segment->isClose(midT, oppSegment)) {
1270 break;
1271 }
1272 if (log) log->record(kExpandCoin_Glitch, id, this, prev->ptT(), oppPtT);
1273 expanded = true;
1274 } while (false); // actual continues while expansion is possible
1275 do {
1276 const SkOpSpanBase* end = coinPtTEnd()->span();
1277 SkOpSpanBase* next = end->final() ? nullptr : end->upCast()->next();
1278 const SkOpPtT* oppPtT;
1279 if (!next || !(oppPtT = next->contains(oppSegment))) {
1280 break;
1281 }
1282 double midT = (end->t() + next->t()) / 2;
1283 if (!segment->isClose(midT, oppSegment)) {
1284 break;
1285 }
1286 if (log) log->record(kExpandCoin_Glitch, id, this, next->ptT(), oppPtT);
1287 expanded = true;
1288 } while (false); // actual continues while expansion is possible
1289 return expanded;
1290}
1291
caryclark025b11e2016-08-25 05:21:14 -07001292#undef FAIL_IF
caryclark55888e42016-07-18 10:01:36 -07001293#define FAIL_IF(cond) do { if (cond) log->record(kAddExpandedFail_Glitch, id, coin); } while (false)
1294
1295/* Commented-out lines keep this in sync with addExpanded */
1296// for each coincident pair, match the spans
1297// if the spans don't match, add the mssing pt to the segment and loop it in the opposite span
caryclark26ad22a2015-10-16 09:03:38 -07001298void SkOpCoincidence::debugAddExpanded(const char* id, SkPathOpsDebug::GlitchLog* log) const {
caryclark26ad22a2015-10-16 09:03:38 -07001299 const SkCoincidentSpans* coin = this->fHead;
1300 if (!coin) {
caryclarked0935a2015-10-22 07:23:52 -07001301 return;
1302 }
caryclark26ad22a2015-10-16 09:03:38 -07001303 do {
caryclark55888e42016-07-18 10:01:36 -07001304 const SkOpPtT* startPtT = coin->coinPtTStart();
1305 const SkOpPtT* oStartPtT = coin->oppPtTStart();
caryclark26ad22a2015-10-16 09:03:38 -07001306 SkASSERT(startPtT->contains(oStartPtT));
caryclark55888e42016-07-18 10:01:36 -07001307 SkASSERT(coin->coinPtTEnd()->contains(coin->oppPtTEnd()));
caryclark26ad22a2015-10-16 09:03:38 -07001308 const SkOpSpanBase* start = startPtT->span();
1309 const SkOpSpanBase* oStart = oStartPtT->span();
caryclark55888e42016-07-18 10:01:36 -07001310 const SkOpSpanBase* end = coin->coinPtTEnd()->span();
1311 const SkOpSpanBase* oEnd = coin->oppPtTEnd()->span();
1312 FAIL_IF(oEnd->deleted());
caryclark26ad22a2015-10-16 09:03:38 -07001313 const SkOpSpanBase* test = start->upCast()->next();
caryclark55888e42016-07-18 10:01:36 -07001314 const SkOpSpanBase* oTest = coin->flipped() ? oStart->prev() : oStart->upCast()->next();
1315 if (!oTest) {
1316 return;
1317 }
caryclark26ad22a2015-10-16 09:03:38 -07001318 while (test != end || oTest != oEnd) {
caryclark55888e42016-07-18 10:01:36 -07001319 if (!test->ptT()->contains(oTest->segment())
1320 || !oTest->ptT()->contains(start->segment())) {
caryclark26ad22a2015-10-16 09:03:38 -07001321 // use t ranges to guess which one is missing
caryclark55888e42016-07-18 10:01:36 -07001322 double startRange = coin->coinPtTEnd()->fT - startPtT->fT;
1323 FAIL_IF(!startRange);
caryclark26ad22a2015-10-16 09:03:38 -07001324 double startPart = (test->t() - startPtT->fT) / startRange;
caryclark55888e42016-07-18 10:01:36 -07001325 double oStartRange = coin->oppPtTEnd()->fT - oStartPtT->fT;
1326 FAIL_IF(!oStartRange);
caryclark26ad22a2015-10-16 09:03:38 -07001327 double oStartPart = (oTest->t() - oStartPtT->fT) / oStartRange;
caryclark55888e42016-07-18 10:01:36 -07001328 FAIL_IF(startPart == oStartPart);
1329 bool startOver = false;
1330 if (startPart < oStartPart)
1331 log->record(kAddExpandedCoin_Glitch, id, // strange debug formatting lines up with original
1332 oStartPtT->fT + oStartRange * startPart, test);
1333 else log->record(kAddExpandedCoin_Glitch, id,
1334 startPtT->fT + startRange * oStartPart, oTest);
1335 if (false) {
1336 SkASSERT(0);
1337 return;
caryclark26ad22a2015-10-16 09:03:38 -07001338 }
caryclark55888e42016-07-18 10:01:36 -07001339 if (startOver) {
1340 test = start;
1341 oTest = oStart;
caryclark26ad22a2015-10-16 09:03:38 -07001342 }
1343 }
caryclark55888e42016-07-18 10:01:36 -07001344 if (test != end) {
caryclark26ad22a2015-10-16 09:03:38 -07001345 test = test->upCast()->next();
1346 }
caryclark55888e42016-07-18 10:01:36 -07001347 if (oTest != oEnd) {
1348 oTest = coin->flipped() ? oTest->prev() : oTest->upCast()->next();
1349 if (!oTest) {
1350 return;
1351 }
caryclark26ad22a2015-10-16 09:03:38 -07001352 }
1353 }
caryclark55888e42016-07-18 10:01:36 -07001354 } while ((coin = coin->next()));
1355 return;
caryclark26ad22a2015-10-16 09:03:38 -07001356}
1357
caryclark55888e42016-07-18 10:01:36 -07001358/* Commented-out lines keep this in sync with addIfMissing() */
1359void SkOpCoincidence::debugAddIfMissing(const SkCoincidentSpans* outer, const SkOpPtT* over1s,
1360 const SkOpPtT* over1e, const char* id, SkPathOpsDebug::GlitchLog* log) const {
1361// SkASSERT(fTop);
1362 if (fTop && alreadyAdded(fTop, outer, over1s, over1e)) { // in debug, fTop may be null
1363 return;
caryclark26ad22a2015-10-16 09:03:38 -07001364 }
caryclark55888e42016-07-18 10:01:36 -07001365 if (fHead && alreadyAdded(fHead, outer, over1s, over1e)) {
1366 return;
1367 }
1368 log->record(kAddIfMissingCoin_Glitch, id, outer->coinPtTStart(), outer->coinPtTEnd(), over1s, over1e);
1369 this->debugValidate();
1370 return;
caryclark26ad22a2015-10-16 09:03:38 -07001371}
1372
caryclark55888e42016-07-18 10:01:36 -07001373/* Commented-out lines keep this in sync addIfMissing() */
1374void SkOpCoincidence::debugAddIfMissing(const SkOpPtT* over1s, const SkOpPtT* over1e,
1375 const SkOpPtT* over2s, const SkOpPtT* over2e, double tStart, double tEnd,
1376 const SkOpPtT* coinPtTStart, const SkOpPtT* coinPtTEnd,
1377 const SkOpPtT* oppPtTStart, const SkOpPtT* oppPtTEnd, const char* id, SkPathOpsDebug::GlitchLog* log) const {
caryclark26ad22a2015-10-16 09:03:38 -07001378 double coinTs, coinTe, oppTs, oppTe;
caryclark55888e42016-07-18 10:01:36 -07001379 TRange(over1s, over1e, tStart, tEnd, coinPtTStart, coinPtTEnd, &coinTs, &coinTe);
1380 TRange(over2s, over2e, tStart, tEnd, oppPtTStart, oppPtTEnd, &oppTs, &oppTe);
1381 bool swap = coinTs > coinTe;
1382 if (swap) {
1383 SkTSwap(coinTs, coinTe);
caryclark26ad22a2015-10-16 09:03:38 -07001384 }
1385 if ((over1s->fT < over1e->fT) != (over2s->fT < over2e->fT)) {
1386 SkTSwap(oppTs, oppTe);
1387 }
caryclark55888e42016-07-18 10:01:36 -07001388 if (swap) {
caryclark26ad22a2015-10-16 09:03:38 -07001389 SkTSwap(oppTs, oppTe);
1390 }
caryclark55888e42016-07-18 10:01:36 -07001391 const SkOpSegment* coinSeg = coinPtTStart->segment();
1392 const SkOpSegment* oppSeg = oppPtTStart->segment();
1393 if (coinSeg == oppSeg) {
1394 return;
caryclark26ad22a2015-10-16 09:03:38 -07001395 }
caryclark55888e42016-07-18 10:01:36 -07001396 return this->debugAddOrOverlap(coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe, id, log);
caryclark26ad22a2015-10-16 09:03:38 -07001397}
1398
caryclark55888e42016-07-18 10:01:36 -07001399/* Commented-out lines keep this in sync addOrOverlap() */
1400void SkOpCoincidence::debugAddOrOverlap(const SkOpSegment* coinSeg, const SkOpSegment* oppSeg,
1401 double coinTs, double coinTe, double oppTs, double oppTe, const char* id, SkPathOpsDebug::GlitchLog* log) const {
1402 SkTDArray<SkCoincidentSpans*> overlaps;
1403 SkASSERT(!fTop); // this is (correctly) reversed in addifMissing()
1404 if (fTop && !this->checkOverlap(fTop, coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe, &overlaps)) {
1405 return;
1406 }
1407 if (fHead && !this->checkOverlap(fHead, coinSeg, oppSeg, coinTs,
1408 coinTe, oppTs, oppTe, &overlaps)) {
1409 return;
1410 }
1411 const SkCoincidentSpans* overlap = overlaps.count() ? overlaps[0] : nullptr;
1412 for (int index = 1; index < overlaps.count(); ++index) { // combine overlaps before continuing
1413 const SkCoincidentSpans* test = overlaps[index];
1414 if (overlap->coinPtTStart()->fT > test->coinPtTStart()->fT) {
1415 log->record(kAddOrOverlap_Glitch, id, overlap, test->coinPtTStart());
1416 }
1417 if (overlap->coinPtTEnd()->fT < test->coinPtTEnd()->fT) {
1418 log->record(kAddOrOverlap_Glitch, id, overlap, test->coinPtTEnd());
1419 }
1420 if (overlap->flipped()
1421 ? overlap->oppPtTStart()->fT < test->oppPtTStart()->fT
1422 : overlap->oppPtTStart()->fT > test->oppPtTStart()->fT) {
1423 log->record(kAddOrOverlap_Glitch, id, overlap, test->oppPtTStart());
1424 }
1425 if (overlap->flipped()
1426 ? overlap->oppPtTEnd()->fT > test->oppPtTEnd()->fT
1427 : overlap->oppPtTEnd()->fT < test->oppPtTEnd()->fT) {
1428 log->record(kAddOrOverlap_Glitch, id, overlap, test->oppPtTEnd());
1429 }
1430 if (!fHead) {
1431 SkAssertResult(true);
1432 }
1433 }
1434 const SkOpPtT* cs = coinSeg->existing(coinTs, oppSeg);
1435 const SkOpPtT* ce = coinSeg->existing(coinTe, oppSeg);
1436 if (overlap && cs && ce && overlap->contains(cs, ce)) {
1437 return;
1438 }
1439 SkASSERT(cs != ce || !cs);
1440 const SkOpPtT* os = oppSeg->existing(oppTs, coinSeg);
1441 const SkOpPtT* oe = oppSeg->existing(oppTe, coinSeg);
1442 if (overlap && os && oe && overlap->contains(os, oe)) {
1443 return;
1444 }
1445 SkASSERT(true || !cs || !cs->deleted());
1446 SkASSERT(true || !os || !os->deleted());
1447 SkASSERT(true || !ce || !ce->deleted());
1448 SkASSERT(true || !oe || !oe->deleted());
1449 const SkOpPtT* csExisting = !cs ? coinSeg->existing(coinTs, nullptr) : nullptr;
1450 const SkOpPtT* ceExisting = !ce ? coinSeg->existing(coinTe, nullptr) : nullptr;
1451 if (csExisting && csExisting == ceExisting) {
1452 return;
1453 }
1454 if (csExisting && (csExisting == ce || csExisting->contains(ceExisting ? ceExisting : ce))) {
1455 return;
1456 }
1457 if (ceExisting && (ceExisting == cs || ceExisting->contains(csExisting ? csExisting : cs))) {
1458 return;
1459 }
1460 const SkOpPtT* osExisting = !os ? oppSeg->existing(oppTs, nullptr) : nullptr;
1461 const SkOpPtT* oeExisting = !oe ? oppSeg->existing(oppTe, nullptr) : nullptr;
1462 if (osExisting && osExisting == oeExisting) {
1463 return;
1464 }
1465 if (osExisting && (osExisting == oe || osExisting->contains(oeExisting ? oeExisting : oe))) {
1466 return;
1467 }
1468 if (oeExisting && (oeExisting == os || oeExisting->contains(osExisting ? osExisting : os))) {
1469 return;
1470 }
1471 bool csDeleted = false, osDeleted = false, ceDeleted = false, oeDeleted = false;
1472 this->debugValidate();
1473 if (!cs || !os) {
1474 if (!cs)
caryclark29b25632016-08-25 11:27:17 -07001475 cs = coinSeg->debugAddT(coinTs);
caryclark55888e42016-07-18 10:01:36 -07001476 if (!os)
caryclark29b25632016-08-25 11:27:17 -07001477 os = oppSeg->debugAddT(oppTs);
caryclark55888e42016-07-18 10:01:36 -07001478 if (cs && os) cs->span()->debugAddOppAndMerge(id, log, os->span(), &csDeleted, &osDeleted);
1479// cs = csWritable;
1480// os = osWritable;
1481 if ((ce && ce->deleted()) || (oe && oe->deleted())) {
1482 return;
1483 }
1484 }
1485 if (!ce || !oe) {
1486 if (!ce)
caryclark29b25632016-08-25 11:27:17 -07001487 ce = coinSeg->debugAddT(coinTe);
caryclark55888e42016-07-18 10:01:36 -07001488 if (!oe)
caryclark29b25632016-08-25 11:27:17 -07001489 oe = oppSeg->debugAddT(oppTe);
caryclark55888e42016-07-18 10:01:36 -07001490 if (ce && oe) ce->span()->debugAddOppAndMerge(id, log, oe->span(), &ceDeleted, &oeDeleted);
1491// ce = ceWritable;
1492// oe = oeWritable;
1493 }
1494 this->debugValidate();
1495 if (csDeleted || osDeleted || ceDeleted || oeDeleted) {
1496 return;
1497 }
1498 if (!cs || !ce || cs->contains(ce) || !os || !oe || os->contains(oe)) {
1499 return;
1500 }
1501// bool result = true;
1502 if (overlap) {
1503 if (overlap->coinPtTStart()->segment() == coinSeg) {
1504 log->record(kAddMissingExtend_Glitch, id, coinSeg, coinTs, coinTe, oppSeg, oppTs, oppTe);
1505 } else {
1506 if (oppTs > oppTe) {
1507 SkTSwap(coinTs, coinTe);
1508 SkTSwap(oppTs, oppTe);
1509 }
1510 log->record(kAddMissingExtend_Glitch, id, oppSeg, oppTs, oppTe, coinSeg, coinTs, coinTe);
1511 }
1512#if DEBUG_COINCIDENCE_VERBOSE
1513// if (result) {
1514// overlap->debugShow();
1515// }
1516#endif
1517 } else {
1518 log->record(kAddMissingCoin_Glitch, id, coinSeg, coinTs, coinTe, oppSeg, oppTs, oppTe);
1519#if DEBUG_COINCIDENCE_VERBOSE
1520// fHead->debugShow();
1521#endif
1522 }
1523 this->debugValidate();
1524 return;
1525}
1526
1527// Extra commented-out lines keep this in sync with addMissing()
1528/* detects overlaps of different coincident runs on same segment */
1529/* does not detect overlaps for pairs without any segments in common */
1530// returns true if caller should loop again
caryclark26ad22a2015-10-16 09:03:38 -07001531void SkOpCoincidence::debugAddMissing(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1532 const SkCoincidentSpans* outer = fHead;
1533 if (!outer) {
1534 return;
1535 }
caryclark55888e42016-07-18 10:01:36 -07001536 // bool added = false;
1537 // fTop = outer;
1538 // fHead = nullptr;
caryclark26ad22a2015-10-16 09:03:38 -07001539 do {
1540 // addifmissing can modify the list that this is walking
1541 // save head so that walker can iterate over old data unperturbed
1542 // addifmissing adds to head freely then add saved head in the end
caryclark55888e42016-07-18 10:01:36 -07001543 const SkOpSegment* outerCoin = outer->coinPtTStart()->segment();
1544 const SkOpSegment* outerOpp = outer->oppPtTStart()->segment();
1545 if (outerCoin->done() || outerOpp->done()) {
1546 continue;
1547 }
caryclark26ad22a2015-10-16 09:03:38 -07001548 const SkCoincidentSpans* inner = outer;
caryclark55888e42016-07-18 10:01:36 -07001549 while ((inner = inner->next())) {
1550 this->debugValidate();
caryclark26ad22a2015-10-16 09:03:38 -07001551 double overS, overE;
caryclark55888e42016-07-18 10:01:36 -07001552 const SkOpSegment* innerCoin = inner->coinPtTStart()->segment();
1553 const SkOpSegment* innerOpp = inner->oppPtTStart()->segment();
1554 if (innerCoin->done() || innerOpp->done()) {
1555 continue;
1556 }
1557 if (outerCoin == innerCoin) {
1558 if (outerOpp != innerOpp
1559 && this->overlap(outer->coinPtTStart(), outer->coinPtTEnd(),
1560 inner->coinPtTStart(), inner->coinPtTEnd(), &overS, &overE)) {
1561 this->debugAddIfMissing(outer->coinPtTStart(), outer->coinPtTEnd(),
1562 inner->coinPtTStart(), inner->coinPtTEnd(), overS, overE,
1563 outer->oppPtTStart(), outer->oppPtTEnd(),
1564 inner->oppPtTStart(), inner->oppPtTEnd(), id, log);
caryclark26ad22a2015-10-16 09:03:38 -07001565 }
caryclark55888e42016-07-18 10:01:36 -07001566 } else if (outerCoin == innerOpp) {
1567 if (outerOpp != innerCoin
1568 && this->overlap(outer->coinPtTStart(), outer->coinPtTEnd(),
1569 inner->oppPtTStart(), inner->oppPtTEnd(), &overS, &overE)) {
1570 this->debugAddIfMissing(outer->coinPtTStart(), outer->coinPtTEnd(),
1571 inner->oppPtTStart(), inner->oppPtTEnd(), overS, overE,
1572 outer->oppPtTStart(), outer->oppPtTEnd(),
1573 inner->coinPtTStart(), inner->coinPtTEnd(), id, log);
caryclark26ad22a2015-10-16 09:03:38 -07001574 }
caryclark55888e42016-07-18 10:01:36 -07001575 } else if (outerOpp == innerCoin) {
1576 SkASSERT(outerCoin != innerOpp);
1577 if (this->overlap(outer->oppPtTStart(), outer->oppPtTEnd(),
1578 inner->coinPtTStart(), inner->coinPtTEnd(), &overS, &overE)) {
1579 this->debugAddIfMissing(outer->oppPtTStart(), outer->oppPtTEnd(),
1580 inner->coinPtTStart(), inner->coinPtTEnd(), overS, overE,
1581 outer->coinPtTStart(), outer->coinPtTEnd(),
1582 inner->oppPtTStart(), inner->oppPtTEnd(), id, log);
caryclark26ad22a2015-10-16 09:03:38 -07001583 }
caryclark55888e42016-07-18 10:01:36 -07001584 } else if (outerOpp == innerOpp) {
1585 SkASSERT(outerCoin != innerCoin);
1586 if (this->overlap(outer->oppPtTStart(), outer->oppPtTEnd(),
1587 inner->oppPtTStart(), inner->oppPtTEnd(), &overS, &overE)) {
1588 this->debugAddIfMissing(outer->oppPtTStart(), outer->oppPtTEnd(),
1589 inner->oppPtTStart(), inner->oppPtTEnd(), overS, overE,
1590 outer->coinPtTStart(), outer->coinPtTEnd(),
1591 inner->coinPtTStart(), inner->coinPtTEnd(), id, log);
caryclark26ad22a2015-10-16 09:03:38 -07001592 }
1593 }
caryclark55888e42016-07-18 10:01:36 -07001594 this->debugValidate();
caryclark26ad22a2015-10-16 09:03:38 -07001595 }
caryclark55888e42016-07-18 10:01:36 -07001596 } while ((outer = outer->next()));
1597 // this->restoreHead();
1598 return;
caryclark26ad22a2015-10-16 09:03:38 -07001599}
1600
caryclark55888e42016-07-18 10:01:36 -07001601// Commented-out lines keep this in sync with release()
1602void SkOpCoincidence::debugRelease(const char* id, SkPathOpsDebug::GlitchLog* log, const SkOpSegment* deleted) const {
1603 const SkCoincidentSpans* coin = fHead;
1604 if (!coin) {
1605 return;
1606 }
1607 do {
1608 if (coin->coinPtTStart()->segment() == deleted
1609 || coin->coinPtTEnd()->segment() == deleted
1610 || coin->oppPtTStart()->segment() == deleted
1611 || coin->oppPtTEnd()->segment() == deleted) {
1612 log->record(kReleasedSpan_Glitch, id, coin);
1613 }
1614 } while ((coin = coin->next()));
1615}
1616
1617// Commented-out lines keep this in sync with reorder()
1618// iterate through all coincident pairs, looking for ranges greater than 1
1619// if found, see if the opposite pair can match it -- which may require
1620// reordering the ptT pairs
1621void SkOpCoincidence::debugReorder(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1622 const SkCoincidentSpans* coin = fHead;
1623 if (!coin) {
1624 return;
1625 }
1626 do {
1627 // most commonly, concidence are one span long; check for that first
1628 int intervals = coin->spanCount();
1629 if (intervals = 1) {
1630#if DEBUG_COINCIDENCE_VERBOSE
1631 // SkASSERT(!coin->debugExpand(nullptr, nullptr));
1632#endif
1633 continue;
1634 }
1635 coin->debugExpand(id, log);
1636 if (coin->spanCount() <= 0) {
1637 return;
1638 }
1639 // check to see if every span in coin has a mate in opp
1640 const SkOpSpan* start = coin->coinPtTStart()->span()->upCast();
1641 bool flipped = coin->flipped();
1642 const SkOpSpanBase* oppStartBase = coin->oppPtTStart()->span();
1643 const SkOpSpan* oppStart = flipped ? oppStartBase->prev() : oppStartBase->upCast();
1644 SkDebugf("", start, oppStart);
1645 } while ((coin = coin->next()));
1646 return;
1647}
1648
1649// Commented-out lines keep this in sync with expand()
1650// expand the range by checking adjacent spans for coincidence
caryclark26ad22a2015-10-16 09:03:38 -07001651bool SkOpCoincidence::debugExpand(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1652 const SkCoincidentSpans* coin = fHead;
1653 if (!coin) {
1654 return false;
1655 }
1656 bool expanded = false;
1657 do {
caryclark55888e42016-07-18 10:01:36 -07001658 if (coin->debugExpand(id, log)) {
1659 // check to see if multiple spans expanded so they are now identical
1660 const SkCoincidentSpans* test = fHead;
1661 do {
1662 if (coin == test) {
1663 continue;
1664 }
1665 if (coin->coinPtTStart() == test->coinPtTStart()
1666 && coin->oppPtTStart() == test->oppPtTStart()) {
1667 if (log) log->record(kExpandCoin_Glitch, id, fHead, test->coinPtTStart());
1668 break;
1669 }
1670 } while ((test = test->next()));
1671 expanded = true;
caryclark26ad22a2015-10-16 09:03:38 -07001672 }
caryclark55888e42016-07-18 10:01:36 -07001673 } while ((coin = coin->next()));
caryclark26ad22a2015-10-16 09:03:38 -07001674 return expanded;
1675}
1676
caryclark55888e42016-07-18 10:01:36 -07001677// Commented-out lines keep this in sync with removeCollapsed()
1678void SkOpCoincidence::debugRemoveCollapsed(const char* id, SkPathOpsDebug::GlitchLog* log) const {
caryclark26ad22a2015-10-16 09:03:38 -07001679 const SkCoincidentSpans* coin = fHead;
1680 if (!coin) {
1681 return;
1682 }
caryclark55888e42016-07-18 10:01:36 -07001683 // SkCoincidentSpans** priorPtr = &fHead;
caryclark26ad22a2015-10-16 09:03:38 -07001684 do {
caryclark55888e42016-07-18 10:01:36 -07001685 if (coin->coinPtTStart() == coin->coinPtTEnd()) {
1686 return;
caryclark26ad22a2015-10-16 09:03:38 -07001687 }
caryclark55888e42016-07-18 10:01:36 -07001688 if (coin->oppPtTStart() == coin->oppPtTEnd()) {
1689 return;
caryclark26ad22a2015-10-16 09:03:38 -07001690 }
caryclark55888e42016-07-18 10:01:36 -07001691 if (coin->coinPtTStart()->collapsed(coin->coinPtTEnd())) {
1692 log->record(kCollapsedCoin_Glitch, id, coin);
1693// continue;
caryclark26ad22a2015-10-16 09:03:38 -07001694 }
caryclark55888e42016-07-18 10:01:36 -07001695 if (coin->oppPtTStart()->collapsed(coin->oppPtTEnd())) {
1696 log->record(kCollapsedCoin_Glitch, id, coin, coin);
1697// continue;
caryclark26ad22a2015-10-16 09:03:38 -07001698 }
caryclark55888e42016-07-18 10:01:36 -07001699 // priorPtr = &coin->nextPtr();
1700 } while ((coin = coin->next()));
1701 return;
caryclark26ad22a2015-10-16 09:03:38 -07001702}
1703
caryclark55888e42016-07-18 10:01:36 -07001704// Commented-out lines keep this in sync with mark()
1705/* this sets up the coincidence links in the segments when the coincidence crosses multiple spans */
caryclark26ad22a2015-10-16 09:03:38 -07001706void SkOpCoincidence::debugMark(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1707 const SkCoincidentSpans* coin = fHead;
1708 if (!coin) {
1709 return;
1710 }
1711 do {
caryclark55888e42016-07-18 10:01:36 -07001712 const SkOpSpan* start = coin->coinPtTStartWritable()->span()->upCast();
1713// SkASSERT(start->deleted());
1714 const SkOpSpanBase* end = coin->coinPtTEndWritable()->span();
1715// SkASSERT(end->deleted());
1716 const SkOpSpanBase* oStart = coin->oppPtTStartWritable()->span();
1717// SkASSERT(oStart->deleted());
1718 const SkOpSpanBase* oEnd = coin->oppPtTEndWritable()->span();
1719// SkASSERT(oEnd->deleted());
1720 bool flipped = coin->flipped();
caryclark26ad22a2015-10-16 09:03:38 -07001721 if (flipped) {
1722 SkTSwap(oStart, oEnd);
1723 }
caryclark55888e42016-07-18 10:01:36 -07001724 /* coin and opp spans may not match up. Mark the ends, and then let the interior
1725 get marked as many times as the spans allow */
1726 start->debugInsertCoincidence(id, log, oStart->upCast());
1727 end->debugInsertCoinEnd(id, log, oEnd);
1728 const SkOpSegment* segment = start->segment();
1729 const SkOpSegment* oSegment = oStart->segment();
caryclark26ad22a2015-10-16 09:03:38 -07001730 const SkOpSpanBase* next = start;
1731 const SkOpSpanBase* oNext = oStart;
caryclark55888e42016-07-18 10:01:36 -07001732 while ((next = next->upCast()->next()) != end) {
1733 if (next->upCast()->debugInsertCoincidence(id, log, oSegment, flipped), false) {
1734 return;
caryclark26ad22a2015-10-16 09:03:38 -07001735 }
caryclark55888e42016-07-18 10:01:36 -07001736 }
1737 while ((oNext = oNext->upCast()->next()) != oEnd) {
1738 if (oNext->upCast()->debugInsertCoincidence(id, log, segment, flipped), false) {
1739 return;
caryclark26ad22a2015-10-16 09:03:38 -07001740 }
caryclark55888e42016-07-18 10:01:36 -07001741 }
1742 } while ((coin = coin->next()));
1743 return;
caryclark26ad22a2015-10-16 09:03:38 -07001744}
1745#endif
1746
caryclark55888e42016-07-18 10:01:36 -07001747#if DEBUG_COINCIDENCE_VERBOSE
1748// Commented-out lines keep this in sync with markCollapsed()
1749void SkOpCoincidence::debugMarkCollapsed(const char* id, SkPathOpsDebug::GlitchLog* log, const SkCoincidentSpans* coin, const SkOpPtT* test) const {
1750 while (coin) {
1751 if (coin->collapsed(test)) {
1752 if (zero_or_one(coin->coinPtTStart()->fT) && zero_or_one(coin->coinPtTEnd()->fT)) {
1753 log->record(kCollapsedCoin_Glitch, id, coin);
1754 }
1755 if (zero_or_one(coin->oppPtTStart()->fT) && zero_or_one(coin->oppPtTEnd()->fT)) {
1756 log->record(kCollapsedCoin_Glitch, id, coin);
1757 }
1758 }
1759 coin = coin->next();
caryclark624637c2015-05-11 07:21:27 -07001760 }
1761}
1762
caryclark55888e42016-07-18 10:01:36 -07001763// Commented-out lines keep this in sync with markCollapsed()
1764void SkOpCoincidence::debugMarkCollapsed(const char* id, SkPathOpsDebug::GlitchLog* log, const SkOpPtT* test) const {
1765 this->debugMarkCollapsed(id, log, fHead, test);
1766 this->debugMarkCollapsed(id, log, fTop, test);
1767}
1768#endif
1769
1770void SkCoincidentSpans::debugShow() const {
1771 SkDebugf("%s - id=%d t=%1.9g tEnd=%1.9g\n", __FUNCTION__,
1772 coinPtTStart()->segment()->debugID(),
1773 coinPtTStart()->fT, coinPtTEnd()->fT);
1774 SkDebugf("%s + id=%d t=%1.9g tEnd=%1.9g\n", __FUNCTION__,
1775 oppPtTStart()->segment()->debugID(),
1776 oppPtTStart()->fT, oppPtTEnd()->fT);
1777}
1778
1779void SkOpCoincidence::debugShowCoincidence() const {
caryclark26ad22a2015-10-16 09:03:38 -07001780#if DEBUG_COINCIDENCE
caryclark55888e42016-07-18 10:01:36 -07001781 const SkCoincidentSpans* span = fHead;
1782 while (span) {
1783 span->debugShow();
1784 span = span->next();
1785 }
1786#endif
1787}
1788
1789#if DEBUG_COINCIDENCE
1790static void DebugValidate(const SkOpSpanBase* next, const SkOpSpanBase* end,
1791 double oStart, double oEnd, const SkOpSegment* oSegment,
1792 const char* id, SkPathOpsDebug::GlitchLog* log) {
1793 SkASSERT(next != end);
1794 SkASSERT(!next->contains(end) || log);
1795 if (next->t() > end->t()) {
1796 SkTSwap(next, end);
1797 }
1798 do {
1799 const SkOpPtT* ptT = next->ptT();
1800 int index = 0;
1801 bool somethingBetween;
1802 do {
1803 ++index;
1804 ptT = ptT->next();
1805 const SkOpPtT* checkPtT = next->ptT();
1806 if (ptT == checkPtT) {
1807 break;
1808 }
1809 bool looped = false;
1810 for (int check = 0; check < index; ++check) {
1811 if ((looped = checkPtT == ptT)) {
1812 break;
1813 }
1814 checkPtT = checkPtT->next();
1815 }
1816 if (looped) {
1817 SkASSERT(0);
1818 break;
1819 }
1820 if (ptT->deleted()) {
1821 continue;
1822 }
1823 if (ptT->segment() != oSegment) {
1824 continue;
1825 }
1826 somethingBetween |= between(oStart, ptT->fT, oEnd);
1827 } while (true);
1828 SkASSERT(somethingBetween);
1829 } while (next != end && (next = next->upCast()->next()));
1830}
1831
1832static void DebugCheckOverlap(const SkCoincidentSpans* test, const SkCoincidentSpans* list,
1833 const char* id, SkPathOpsDebug::GlitchLog* log) {
1834 if (!list) {
1835 return;
1836 }
1837 const SkOpSegment* coinSeg = test->coinPtTStart()->segment();
1838 SkASSERT(coinSeg == test->coinPtTEnd()->segment());
1839 const SkOpSegment* oppSeg = test->oppPtTStart()->segment();
1840 SkASSERT(oppSeg == test->oppPtTEnd()->segment());
1841 SkASSERT(coinSeg != test->oppPtTStart()->segment());
1842 SkDEBUGCODE(double tcs = test->coinPtTStart()->fT);
1843 SkASSERT(between(0, tcs, 1));
1844 SkDEBUGCODE(double tce = test->coinPtTEnd()->fT);
1845 SkASSERT(between(0, tce, 1));
1846 SkASSERT(tcs < tce);
1847 double tos = test->oppPtTStart()->fT;
1848 SkASSERT(between(0, tos, 1));
1849 double toe = test->oppPtTEnd()->fT;
1850 SkASSERT(between(0, toe, 1));
1851 SkASSERT(tos != toe);
1852 if (tos > toe) {
1853 SkTSwap(tos, toe);
1854 }
1855 do {
1856 double lcs, lce, los, loe;
1857 if (coinSeg == list->coinPtTStart()->segment()) {
1858 if (oppSeg != list->oppPtTStart()->segment()) {
1859 continue;
1860 }
1861 lcs = list->coinPtTStart()->fT;
1862 lce = list->coinPtTEnd()->fT;
1863 los = list->oppPtTStart()->fT;
1864 loe = list->oppPtTEnd()->fT;
1865 if (los > loe) {
1866 SkTSwap(los, loe);
1867 }
1868 } else if (coinSeg == list->oppPtTStart()->segment()) {
1869 if (oppSeg != list->coinPtTStart()->segment()) {
1870 continue;
1871 }
1872 lcs = list->oppPtTStart()->fT;
1873 lce = list->oppPtTEnd()->fT;
1874 if (lcs > lce) {
1875 SkTSwap(lcs, lce);
1876 }
1877 los = list->coinPtTStart()->fT;
1878 loe = list->coinPtTEnd()->fT;
1879 } else {
1880 continue;
1881 }
1882 SkASSERT(tce < lcs || lce < tcs);
1883 SkASSERT(toe < los || loe < tos);
1884 } while ((list = list->next()));
1885}
1886
1887
1888static void DebugCheckOverlapTop(const SkCoincidentSpans* head, const SkCoincidentSpans* opt,
1889 const char* id, SkPathOpsDebug::GlitchLog* log) {
1890 // check for overlapping coincident spans
1891 const SkCoincidentSpans* test = head;
1892 while (test) {
1893 const SkCoincidentSpans* next = test->next();
1894 DebugCheckOverlap(test, next, id, log);
1895 DebugCheckOverlap(test, opt, id, log);
1896 test = next;
1897 }
1898}
1899
1900#if DEBUG_COINCIDENCE_VERBOSE
1901void SkOpCoincidence::debugCheckOverlap(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1902 DebugCheckOverlapTop(fHead, fTop, id, log);
1903 DebugCheckOverlapTop(fTop, nullptr, id, log);
1904}
1905#endif
1906
1907static void DebugValidate(const SkCoincidentSpans* head, const SkCoincidentSpans* opt,
1908 const char* id, SkPathOpsDebug::GlitchLog* log) {
1909 // look for pts inside coincident spans that are not inside the opposite spans
1910 const SkCoincidentSpans* coin = head;
1911 while (coin) {
1912 SkASSERT(SkOpCoincidence::Ordered(coin->coinPtTStart()->segment(),
1913 coin->oppPtTStart()->segment()));
1914 SkASSERT(coin->coinPtTStart()->span()->ptT() == coin->coinPtTStart());
1915 SkASSERT(coin->coinPtTEnd()->span()->ptT() == coin->coinPtTEnd());
1916 SkASSERT(coin->oppPtTStart()->span()->ptT() == coin->oppPtTStart());
1917 SkASSERT(coin->oppPtTEnd()->span()->ptT() == coin->oppPtTEnd());
1918 DebugValidate(coin->coinPtTStart()->span(), coin->coinPtTEnd()->span(),
1919 coin->oppPtTStart()->fT, coin->oppPtTEnd()->fT, coin->oppPtTStart()->segment(),
1920 id, log);
1921 DebugValidate(coin->oppPtTStart()->span(), coin->oppPtTEnd()->span(),
1922 coin->coinPtTStart()->fT, coin->coinPtTEnd()->fT, coin->coinPtTStart()->segment(),
1923 id, log);
1924 coin = coin->next();
1925 }
1926 DebugCheckOverlapTop(head, opt, id, log);
1927}
1928#endif
1929
1930void SkOpCoincidence::debugValidate() const {
1931#if DEBUG_COINCIDENCE
1932 // if (fGlobalState->debugCheckHealth()) {
1933// return;
1934// }
1935 DebugValidate(fHead, fTop, nullptr, nullptr);
1936 DebugValidate(fTop, nullptr, nullptr, nullptr);
1937#endif
1938}
1939
1940#if DEBUG_COINCIDENCE_VERBOSE
1941void SkOpCoincidence::debugCheckValid(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1942 DebugValidate(fHead, fTop, id, log);
1943 DebugValidate(fTop, nullptr, id, log);
1944}
1945#endif
1946
1947#if DEBUG_COINCIDENCE_VERBOSE
caryclark26ad22a2015-10-16 09:03:38 -07001948void SkOpContour::debugCheckHealth(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1949 const SkOpSegment* segment = &fHead;
1950 do {
1951 segment->debugCheckHealth(id, log);
1952 } while ((segment = segment->next()));
1953}
1954
caryclark55888e42016-07-18 10:01:36 -07001955// commmented-out lines keep this aligned with missingCoincidence()
1956void SkOpContour::debugMissingCoincidence(const char* id, SkPathOpsDebug::GlitchLog* log) const {
1957// SkASSERT(fCount > 0);
caryclark26ad22a2015-10-16 09:03:38 -07001958 const SkOpSegment* segment = &fHead;
caryclark55888e42016-07-18 10:01:36 -07001959// bool result = false;
caryclark26ad22a2015-10-16 09:03:38 -07001960 do {
caryclark55888e42016-07-18 10:01:36 -07001961 if (fState->angleCoincidence()) {
1962// #if DEBUG_ANGLE
1963// segment->debugCheckAngleCoin();
1964// #endif
1965 } else if (segment->debugMissingCoincidence(id, log), false) {
1966// result = true;
1967// see FIXME in missingCoincidence()
1968//
1969//
1970//
1971 // continue;
1972 }
1973 segment = segment->next();
1974 } while (segment);
1975 return;
caryclark26ad22a2015-10-16 09:03:38 -07001976}
1977#endif
1978
caryclark025b11e2016-08-25 05:21:14 -07001979#if DEBUG_COINCIDENCE_ORDER
1980void SkOpSegment::debugResetCoinT() const {
1981 fDebugBaseIndex = -1;
1982 fDebugBaseMin = 1;
1983 fDebugBaseMax = -1;
1984 fDebugLastIndex = -1;
1985 fDebugLastMin = 1;
1986 fDebugLastMax = -1;
1987}
1988#endif
1989
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001990void SkOpSegment::debugValidate() const {
caryclark025b11e2016-08-25 05:21:14 -07001991#if DEBUG_COINCIDENCE_ORDER
1992 {
1993 const SkOpSpanBase* span = &fHead;
1994 do {
1995 span->debugResetCoinT();
1996 } while (!span->final() && (span = span->upCast()->next()));
1997 span = &fHead;
1998 int index = 0;
1999 do {
2000 span->debugSetCoinT(index++);
2001 } while (!span->final() && (span = span->upCast()->next()));
2002 }
2003#endif
caryclark55888e42016-07-18 10:01:36 -07002004#if DEBUG_COINCIDENCE
2005 if (this->globalState()->debugCheckHealth()) {
2006 return;
2007 }
2008#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00002009#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07002010 const SkOpSpanBase* span = &fHead;
2011 double lastT = -1;
halcanary96fcdcc2015-08-27 07:41:13 -07002012 const SkOpSpanBase* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -07002013 int count = 0;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00002014 int done = 0;
caryclark54359292015-03-26 07:52:43 -07002015 do {
2016 if (!span->final()) {
2017 ++count;
2018 done += span->upCast()->done() ? 1 : 0;
2019 }
2020 SkASSERT(span->segment() == this);
2021 SkASSERT(!prev || prev->upCast()->next() == span);
2022 SkASSERT(!prev || prev == span->prev());
2023 prev = span;
2024 double t = span->ptT()->fT;
2025 SkASSERT(lastT < t);
2026 lastT = t;
2027 span->debugValidate();
2028 } while (!span->final() && (span = span->upCast()->next()));
2029 SkASSERT(count == fCount);
2030 SkASSERT(done == fDoneCount);
caryclark08bc8482015-04-24 09:08:57 -07002031 SkASSERT(count >= fDoneCount);
caryclark54359292015-03-26 07:52:43 -07002032 SkASSERT(span->final());
2033 span->debugValidate();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00002034#endif
caryclark54359292015-03-26 07:52:43 -07002035}
2036
caryclark55888e42016-07-18 10:01:36 -07002037#if DEBUG_COINCIDENCE_VERBOSE
2038// Commented-out lines keep this in sync with addOppAndMerge()
2039// If the added points envelop adjacent spans, merge them in.
2040void SkOpSpanBase::debugAddOppAndMerge(const char* id, SkPathOpsDebug::GlitchLog* log, const SkOpSpanBase* opp, bool* spanDeleted, bool* oppDeleted) const {
caryclark29b25632016-08-25 11:27:17 -07002041 const SkOpPtT* oppPrev = this->ptT()->debugOppPrev(opp->ptT());
2042 if (oppPrev) {
2043 this->ptT()->debugAddOpp(opp->ptT(), oppPrev);
caryclark55888e42016-07-18 10:01:36 -07002044 this->debugCheckForCollapsedCoincidence(id, log);
caryclark26ad22a2015-10-16 09:03:38 -07002045 }
caryclark55888e42016-07-18 10:01:36 -07002046 // compute bounds of points in span
2047 SkPathOpsBounds bounds;
2048 bounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMin, SK_ScalarMin);
2049 const SkOpPtT* head = this->ptT();
2050 const SkOpPtT* nextPt = head;
2051 do {
2052 bounds.add(nextPt->fPt);
2053 } while ((nextPt = nextPt->next()) != head);
2054 if (!bounds.width() && !bounds.height()) {
2055 return;
caryclark26ad22a2015-10-16 09:03:38 -07002056 }
caryclark55888e42016-07-18 10:01:36 -07002057 this->debugMergeContained(id, log, bounds, spanDeleted);
2058 opp->debugMergeContained(id, log, bounds, oppDeleted);
caryclark26ad22a2015-10-16 09:03:38 -07002059}
2060
caryclark55888e42016-07-18 10:01:36 -07002061// Commented-out lines keep this in sync with checkForCollapsedCoincidence()
2062void SkOpSpanBase::debugCheckForCollapsedCoincidence(const char* id, SkPathOpsDebug::GlitchLog* log) const {
2063 const SkOpCoincidence* coins = this->globalState()->coincidence();
2064 if (coins->isEmpty()) {
2065 return;
2066 }
2067// the insert above may have put both ends of a coincident run in the same span
2068// for each coincident ptT in loop; see if its opposite in is also in the loop
2069// this implementation is the motivation for marking that a ptT is referenced by a coincident span
2070 const SkOpPtT* head = this->ptT();
2071 const SkOpPtT* test = head;
caryclark26ad22a2015-10-16 09:03:38 -07002072 do {
caryclark55888e42016-07-18 10:01:36 -07002073 if (!test->coincident()) {
2074 continue;
caryclark26ad22a2015-10-16 09:03:38 -07002075 }
caryclark55888e42016-07-18 10:01:36 -07002076 coins->debugMarkCollapsed(id, log, test);
2077 } while ((test = test->next()) != head);
caryclark26ad22a2015-10-16 09:03:38 -07002078}
caryclark55888e42016-07-18 10:01:36 -07002079#endif
caryclark26ad22a2015-10-16 09:03:38 -07002080
caryclark54359292015-03-26 07:52:43 -07002081bool SkOpSpanBase::debugCoinEndLoopCheck() const {
2082 int loop = 0;
2083 const SkOpSpanBase* next = this;
2084 SkOpSpanBase* nextCoin;
2085 do {
2086 nextCoin = next->fCoinEnd;
2087 SkASSERT(nextCoin == this || nextCoin->fCoinEnd != nextCoin);
2088 for (int check = 1; check < loop - 1; ++check) {
2089 const SkOpSpanBase* checkCoin = this->fCoinEnd;
2090 const SkOpSpanBase* innerCoin = checkCoin;
2091 for (int inner = check + 1; inner < loop; ++inner) {
2092 innerCoin = innerCoin->fCoinEnd;
2093 if (checkCoin == innerCoin) {
2094 SkDebugf("*** bad coincident end loop ***\n");
2095 return false;
2096 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00002097 }
2098 }
caryclark54359292015-03-26 07:52:43 -07002099 ++loop;
2100 } while ((next = nextCoin) && next != this);
2101 return true;
2102}
2103
caryclark55888e42016-07-18 10:01:36 -07002104#if DEBUG_COINCIDENCE_VERBOSE
2105// Commented-out lines keep this in sync with insertCoinEnd()
2106void SkOpSpanBase::debugInsertCoinEnd(const char* id, SkPathOpsDebug::GlitchLog* log, const SkOpSpanBase* coin) const {
2107 if (containsCoinEnd(coin)) {
2108// SkASSERT(coin->containsCoinEnd(this));
2109 return;
2110 }
2111 debugValidate();
2112// SkASSERT(this != coin);
2113 log->record(kMarkCoinEnd_Glitch, id, this, coin);
2114// coin->fCoinEnd = this->fCoinEnd;
2115// this->fCoinEnd = coinNext;
2116 debugValidate();
2117}
2118
2119// Commented-out lines keep this in sync with mergeContained()
2120void SkOpSpanBase::debugMergeContained(const char* id, SkPathOpsDebug::GlitchLog* log, const SkPathOpsBounds& bounds, bool* deleted) const {
2121 // while adjacent spans' points are contained by the bounds, merge them
2122 const SkOpSpanBase* prev = this;
2123 const SkOpSegment* seg = this->segment();
2124 while ((prev = prev->prev()) && bounds.contains(prev->pt()) && !seg->ptsDisjoint(prev, this)) {
2125 if (prev->prev()) {
2126 log->record(kMergeContained_Glitch, id, this, prev);
2127 } else if (this->final()) {
2128 log->record(kMergeContained_Glitch, id, this, prev);
2129 // return;
2130 } else {
2131 log->record(kMergeContained_Glitch, id, prev, this);
caryclark26ad22a2015-10-16 09:03:38 -07002132 }
2133 }
caryclark55888e42016-07-18 10:01:36 -07002134 const SkOpSpanBase* current = this;
2135 const SkOpSpanBase* next = this;
2136 while (next->upCastable() && (next = next->upCast()->next())
2137 && bounds.contains(next->pt()) && !seg->ptsDisjoint(this, next)) {
2138 if (!current->prev() && next->final()) {
2139 log->record(kMergeContained_Glitch, id, next, current);
2140 current = next;
2141 }
2142 if (current->prev()) {
2143 log->record(kMergeContained_Glitch, id, next, current);
2144 current = next;
2145 } else {
2146 log->record(kMergeContained_Glitch, id, next, current);
2147 current = next;
2148 }
2149 }
2150#if DEBUG_COINCIDENCE
2151 // this->globalState()->coincidence()->debugValidate();
2152#endif
caryclark26ad22a2015-10-16 09:03:38 -07002153}
caryclark55888e42016-07-18 10:01:36 -07002154#endif
caryclark26ad22a2015-10-16 09:03:38 -07002155
caryclark025b11e2016-08-25 05:21:14 -07002156void SkOpSpanBase::debugResetCoinT() const {
2157#if DEBUG_COINCIDENCE_ORDER
2158 const SkOpPtT* ptT = &fPtT;
2159 do {
2160 ptT->debugResetCoinT();
2161 ptT = ptT->next();
2162 } while (ptT != &fPtT);
2163#endif
2164}
2165
2166void SkOpSpanBase::debugSetCoinT(int index) const {
2167#if DEBUG_COINCIDENCE_ORDER
2168 const SkOpPtT* ptT = &fPtT;
2169 do {
2170 if (!ptT->deleted()) {
2171 ptT->debugSetCoinT(index);
2172 }
2173 ptT = ptT->next();
2174 } while (ptT != &fPtT);
2175#endif
2176}
2177
caryclark26ad22a2015-10-16 09:03:38 -07002178const SkOpSpan* SkOpSpanBase::debugStarter(SkOpSpanBase const** endPtr) const {
2179 const SkOpSpanBase* end = *endPtr;
2180 SkASSERT(this->segment() == end->segment());
2181 const SkOpSpanBase* result;
2182 if (t() < end->t()) {
2183 result = this;
2184 } else {
2185 result = end;
2186 *endPtr = this;
2187 }
2188 return result->upCast();
2189}
2190
caryclark54359292015-03-26 07:52:43 -07002191void SkOpSpanBase::debugValidate() const {
caryclark55888e42016-07-18 10:01:36 -07002192#if DEBUG_COINCIDENCE
2193 if (this->globalState()->debugCheckHealth()) {
2194 return;
2195 }
2196#endif
caryclark54359292015-03-26 07:52:43 -07002197#if DEBUG_VALIDATE
2198 const SkOpPtT* ptT = &fPtT;
2199 SkASSERT(ptT->span() == this);
2200 do {
2201// SkASSERT(SkDPoint::RoughlyEqual(fPtT.fPt, ptT->fPt));
2202 ptT->debugValidate();
2203 ptT = ptT->next();
2204 } while (ptT != &fPtT);
2205 SkASSERT(this->debugCoinEndLoopCheck());
2206 if (!this->final()) {
2207 SkASSERT(this->upCast()->debugCoinLoopCheck());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00002208 }
caryclark54359292015-03-26 07:52:43 -07002209 if (fFromAngle) {
2210 fFromAngle->debugValidate();
2211 }
2212 if (!this->final() && this->upCast()->toAngle()) {
2213 this->upCast()->toAngle()->debugValidate();
2214 }
2215#endif
2216}
2217
2218bool SkOpSpan::debugCoinLoopCheck() const {
2219 int loop = 0;
2220 const SkOpSpan* next = this;
2221 SkOpSpan* nextCoin;
2222 do {
2223 nextCoin = next->fCoincident;
2224 SkASSERT(nextCoin == this || nextCoin->fCoincident != nextCoin);
2225 for (int check = 1; check < loop - 1; ++check) {
2226 const SkOpSpan* checkCoin = this->fCoincident;
2227 const SkOpSpan* innerCoin = checkCoin;
2228 for (int inner = check + 1; inner < loop; ++inner) {
2229 innerCoin = innerCoin->fCoincident;
2230 if (checkCoin == innerCoin) {
2231 SkDebugf("*** bad coincident loop ***\n");
2232 return false;
2233 }
2234 }
2235 }
2236 ++loop;
2237 } while ((next = nextCoin) && next != this);
2238 return true;
2239}
2240
caryclark55888e42016-07-18 10:01:36 -07002241#if DEBUG_COINCIDENCE_VERBOSE
2242// Commented-out lines keep this in sync with insertCoincidence() in header
2243void SkOpSpan::debugInsertCoincidence(const char* id, SkPathOpsDebug::GlitchLog* log, const SkOpSpan* coin) const {
2244 if (containsCoincidence(coin)) {
2245// SkASSERT(coin->containsCoincidence(this));
2246 return;
2247 }
2248 debugValidate();
2249// SkASSERT(this != coin);
2250 log->record(kMarkCoinStart_Glitch, id, this, coin);
2251// coin->fCoincident = this->fCoincident;
2252// this->fCoincident = coinNext;
2253 debugValidate();
2254}
2255
2256// Commented-out lines keep this in sync with insertCoincidence()
2257void SkOpSpan::debugInsertCoincidence(const char* id, SkPathOpsDebug::GlitchLog* log, const SkOpSegment* segment, bool flipped) const {
2258 if (this->containsCoincidence(segment)) {
2259 return;
2260 }
2261 const SkOpPtT* next = &fPtT;
2262 while ((next = next->next()) != &fPtT) {
2263 if (next->segment() == segment) {
2264 log->record(kMarkCoinInsert_Glitch, id, flipped ? next->span()->prev() : next->span());
2265 return;
2266 }
2267 }
2268#if DEBUG_COINCIDENCE
2269 log->record(kMarkCoinMissing_Glitch, id, segment, this);
2270#endif
2271}
2272#endif
2273
caryclark624637c2015-05-11 07:21:27 -07002274// called only by test code
2275int SkIntersections::debugCoincidentUsed() const {
2276 if (!fIsCoincident[0]) {
2277 SkASSERT(!fIsCoincident[1]);
2278 return 0;
2279 }
2280 int count = 0;
2281 SkDEBUGCODE(int count2 = 0;)
2282 for (int index = 0; index < fUsed; ++index) {
2283 if (fIsCoincident[0] & (1 << index)) {
2284 ++count;
2285 }
2286#ifdef SK_DEBUG
2287 if (fIsCoincident[1] & (1 << index)) {
2288 ++count2;
2289 }
2290#endif
2291 }
2292 SkASSERT(count == count2);
2293 return count;
2294}
2295
caryclark54359292015-03-26 07:52:43 -07002296#include "SkOpContour.h"
2297
caryclark55888e42016-07-18 10:01:36 -07002298// Commented-out lines keep this in sync with addOpp()
caryclark29b25632016-08-25 11:27:17 -07002299void SkOpPtT::debugAddOpp(const SkOpPtT* opp, const SkOpPtT* oppPrev) const {
2300 SkDEBUGCODE(const SkOpPtT* oldNext = this->fNext);
caryclark55888e42016-07-18 10:01:36 -07002301 SkASSERT(this != opp);
2302// this->fNext = opp;
caryclark29b25632016-08-25 11:27:17 -07002303 SkASSERT(oppPrev != oldNext);
caryclark55888e42016-07-18 10:01:36 -07002304// oppPrev->fNext = oldNext;
caryclark55888e42016-07-18 10:01:36 -07002305}
2306
caryclark26ad22a2015-10-16 09:03:38 -07002307bool SkOpPtT::debugContains(const SkOpPtT* check) const {
2308 SkASSERT(this != check);
2309 const SkOpPtT* ptT = this;
2310 int links = 0;
2311 do {
2312 ptT = ptT->next();
2313 if (ptT == check) {
2314 return true;
2315 }
2316 ++links;
2317 const SkOpPtT* test = this;
2318 for (int index = 0; index < links; ++index) {
2319 if (ptT == test) {
2320 return false;
2321 }
2322 test = test->next();
2323 }
2324 } while (true);
2325}
2326
2327const SkOpPtT* SkOpPtT::debugContains(const SkOpSegment* check) const {
2328 SkASSERT(this->segment() != check);
2329 const SkOpPtT* ptT = this;
2330 int links = 0;
2331 do {
2332 ptT = ptT->next();
2333 if (ptT->segment() == check) {
2334 return ptT;
2335 }
2336 ++links;
2337 const SkOpPtT* test = this;
2338 for (int index = 0; index < links; ++index) {
2339 if (ptT == test) {
2340 return nullptr;
2341 }
2342 test = test->next();
2343 }
2344 } while (true);
2345}
2346
caryclark54359292015-03-26 07:52:43 -07002347int SkOpPtT::debugLoopLimit(bool report) const {
2348 int loop = 0;
2349 const SkOpPtT* next = this;
2350 do {
2351 for (int check = 1; check < loop - 1; ++check) {
2352 const SkOpPtT* checkPtT = this->fNext;
2353 const SkOpPtT* innerPtT = checkPtT;
2354 for (int inner = check + 1; inner < loop; ++inner) {
2355 innerPtT = innerPtT->fNext;
2356 if (checkPtT == innerPtT) {
2357 if (report) {
2358 SkDebugf("*** bad ptT loop ***\n");
2359 }
2360 return loop;
2361 }
2362 }
2363 }
caryclark26ad22a2015-10-16 09:03:38 -07002364 // there's nothing wrong with extremely large loop counts -- but this may appear to hang
2365 // by taking a very long time to figure out that no loop entry is a duplicate
2366 // -- and it's likely that a large loop count is indicative of a bug somewhere
2367 if (++loop > 1000) {
2368 SkDebugf("*** loop count exceeds 1000 ***\n");
2369 return 1000;
2370 }
caryclark54359292015-03-26 07:52:43 -07002371 } while ((next = next->fNext) && next != this);
2372 return 0;
2373}
2374
caryclark29b25632016-08-25 11:27:17 -07002375const SkOpPtT* SkOpPtT::debugOppPrev(const SkOpPtT* opp) const {
2376 return this->oppPrev(const_cast<SkOpPtT*>(opp));
2377}
2378
caryclark025b11e2016-08-25 05:21:14 -07002379void SkOpPtT::debugResetCoinT() const {
2380#if DEBUG_COINCIDENCE_ORDER
2381 this->segment()->debugResetCoinT();
2382#endif
2383}
2384
2385void SkOpPtT::debugSetCoinT(int index) const {
2386#if DEBUG_COINCIDENCE_ORDER
2387 this->segment()->debugSetCoinT(index, fT);
2388#endif
2389}
2390
caryclark54359292015-03-26 07:52:43 -07002391void SkOpPtT::debugValidate() const {
caryclark55888e42016-07-18 10:01:36 -07002392#if DEBUG_COINCIDENCE
2393 if (this->globalState()->debugCheckHealth()) {
2394 return;
2395 }
2396#endif
caryclark54359292015-03-26 07:52:43 -07002397#if DEBUG_VALIDATE
caryclark4e1a4c92015-05-18 12:56:57 -07002398 SkOpGlobalState::Phase phase = contour()->globalState()->phase();
2399 if (phase == SkOpGlobalState::kIntersecting
2400 || phase == SkOpGlobalState::kFixWinding) {
caryclark54359292015-03-26 07:52:43 -07002401 return;
2402 }
2403 SkASSERT(fNext);
2404 SkASSERT(fNext != this);
2405 SkASSERT(fNext->fNext);
2406 SkASSERT(debugLoopLimit(false) == 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00002407#endif
2408}
caryclark1049f122015-04-20 08:31:59 -07002409
2410static void output_scalar(SkScalar num) {
2411 if (num == (int) num) {
2412 SkDebugf("%d", (int) num);
2413 } else {
2414 SkString str;
2415 str.printf("%1.9g", num);
2416 int width = (int) str.size();
2417 const char* cStr = str.c_str();
2418 while (cStr[width - 1] == '0') {
2419 --width;
2420 }
2421 str.resize(width);
2422 SkDebugf("%sf", str.c_str());
2423 }
2424}
2425
2426static void output_points(const SkPoint* pts, int count) {
2427 for (int index = 0; index < count; ++index) {
2428 output_scalar(pts[index].fX);
2429 SkDebugf(", ");
2430 output_scalar(pts[index].fY);
2431 if (index + 1 < count) {
2432 SkDebugf(", ");
2433 }
2434 }
2435}
2436
2437static void showPathContours(SkPath::RawIter& iter, const char* pathName) {
2438 uint8_t verb;
2439 SkPoint pts[4];
2440 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
2441 switch (verb) {
2442 case SkPath::kMove_Verb:
2443 SkDebugf(" %s.moveTo(", pathName);
2444 output_points(&pts[0], 1);
2445 SkDebugf(");\n");
2446 continue;
2447 case SkPath::kLine_Verb:
2448 SkDebugf(" %s.lineTo(", pathName);
2449 output_points(&pts[1], 1);
2450 SkDebugf(");\n");
2451 break;
2452 case SkPath::kQuad_Verb:
2453 SkDebugf(" %s.quadTo(", pathName);
2454 output_points(&pts[1], 2);
2455 SkDebugf(");\n");
2456 break;
2457 case SkPath::kConic_Verb:
2458 SkDebugf(" %s.conicTo(", pathName);
2459 output_points(&pts[1], 2);
2460 SkDebugf(", %1.9gf);\n", iter.conicWeight());
2461 break;
2462 case SkPath::kCubic_Verb:
2463 SkDebugf(" %s.cubicTo(", pathName);
2464 output_points(&pts[1], 3);
2465 SkDebugf(");\n");
2466 break;
2467 case SkPath::kClose_Verb:
2468 SkDebugf(" %s.close();\n", pathName);
2469 break;
2470 default:
2471 SkDEBUGFAIL("bad verb");
2472 return;
2473 }
2474 }
2475}
2476
2477static const char* gFillTypeStr[] = {
2478 "kWinding_FillType",
2479 "kEvenOdd_FillType",
2480 "kInverseWinding_FillType",
2481 "kInverseEvenOdd_FillType"
2482};
2483
2484void SkPathOpsDebug::ShowOnePath(const SkPath& path, const char* name, bool includeDeclaration) {
2485 SkPath::RawIter iter(path);
2486#define SUPPORT_RECT_CONTOUR_DETECTION 0
2487#if SUPPORT_RECT_CONTOUR_DETECTION
halcanary96fcdcc2015-08-27 07:41:13 -07002488 int rectCount = path.isRectContours() ? path.rectContours(nullptr, nullptr) : 0;
caryclark1049f122015-04-20 08:31:59 -07002489 if (rectCount > 0) {
2490 SkTDArray<SkRect> rects;
2491 SkTDArray<SkPath::Direction> directions;
2492 rects.setCount(rectCount);
2493 directions.setCount(rectCount);
2494 path.rectContours(rects.begin(), directions.begin());
2495 for (int contour = 0; contour < rectCount; ++contour) {
2496 const SkRect& rect = rects[contour];
2497 SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop,
2498 rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction
2499 ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction");
2500 }
2501 return;
2502 }
2503#endif
2504 SkPath::FillType fillType = path.getFillType();
2505 SkASSERT(fillType >= SkPath::kWinding_FillType && fillType <= SkPath::kInverseEvenOdd_FillType);
2506 if (includeDeclaration) {
2507 SkDebugf(" SkPath %s;\n", name);
2508 }
2509 SkDebugf(" %s.setFillType(SkPath::%s);\n", name, gFillTypeStr[fillType]);
2510 iter.setPath(path);
2511 showPathContours(iter, name);
2512}