blob: cb2e7fbbbe02598c207c32f857c881c82c486f5c [file] [log] [blame]
caryclark54359292015-03-26 07:52:43 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SkOpCoincidence.h"
8#include "SkOpContour.h"
9#include "SkOpSegment.h"
10#include "SkPathWriter.h"
11
12bool SkOpPtT::alias() const {
13 return this->span()->ptT() != this;
14}
15
caryclark30b9fdd2016-08-31 14:36:29 -070016const SkOpPtT* SkOpPtT::active() const {
17 if (!fDeleted) {
18 return this;
19 }
20 const SkOpPtT* ptT = this;
21 const SkOpPtT* stopPtT = ptT;
22 while ((ptT = ptT->next()) != stopPtT) {
23 if (ptT->fSpan == fSpan && !ptT->fDeleted) {
24 return ptT;
25 }
26 }
Cary Clark74b42902018-03-09 07:38:47 -050027 return nullptr; // should never return deleted; caller must abort
caryclark30b9fdd2016-08-31 14:36:29 -070028}
29
caryclark27c8eb82015-07-06 11:38:33 -070030bool SkOpPtT::contains(const SkOpPtT* check) const {
caryclarke7bb5b22016-09-22 05:20:07 -070031 SkOPASSERT(this != check);
caryclark27c8eb82015-07-06 11:38:33 -070032 const SkOpPtT* ptT = this;
33 const SkOpPtT* stopPtT = ptT;
34 while ((ptT = ptT->next()) != stopPtT) {
35 if (ptT == check) {
36 return true;
37 }
38 }
39 return false;
40}
41
caryclark55888e42016-07-18 10:01:36 -070042bool SkOpPtT::contains(const SkOpSegment* segment, const SkPoint& pt) const {
43 SkASSERT(this->segment() != segment);
44 const SkOpPtT* ptT = this;
caryclark27c8eb82015-07-06 11:38:33 -070045 const SkOpPtT* stopPtT = ptT;
46 while ((ptT = ptT->next()) != stopPtT) {
caryclark55888e42016-07-18 10:01:36 -070047 if (ptT->fPt == pt && ptT->segment() == segment) {
48 return true;
49 }
50 }
51 return false;
52}
53
54bool SkOpPtT::contains(const SkOpSegment* segment, double t) const {
55 const SkOpPtT* ptT = this;
56 const SkOpPtT* stopPtT = ptT;
57 while ((ptT = ptT->next()) != stopPtT) {
58 if (ptT->fT == t && ptT->segment() == segment) {
59 return true;
60 }
61 }
62 return false;
63}
64
65const SkOpPtT* SkOpPtT::contains(const SkOpSegment* check) const {
66 SkASSERT(this->segment() != check);
67 const SkOpPtT* ptT = this;
68 const SkOpPtT* stopPtT = ptT;
69 while ((ptT = ptT->next()) != stopPtT) {
70 if (ptT->segment() == check && !ptT->deleted()) {
caryclark27c8eb82015-07-06 11:38:33 -070071 return ptT;
72 }
73 }
halcanary96fcdcc2015-08-27 07:41:13 -070074 return nullptr;
caryclark27c8eb82015-07-06 11:38:33 -070075}
76
caryclark54359292015-03-26 07:52:43 -070077SkOpContour* SkOpPtT::contour() const {
78 return segment()->contour();
79}
80
caryclark55888e42016-07-18 10:01:36 -070081const SkOpPtT* SkOpPtT::find(const SkOpSegment* segment) const {
82 const SkOpPtT* ptT = this;
caryclark27c8eb82015-07-06 11:38:33 -070083 const SkOpPtT* stopPtT = ptT;
84 do {
caryclark55888e42016-07-18 10:01:36 -070085 if (ptT->segment() == segment && !ptT->deleted()) {
caryclark27c8eb82015-07-06 11:38:33 -070086 return ptT;
87 }
88 ptT = ptT->fNext;
89 } while (stopPtT != ptT);
caryclark55888e42016-07-18 10:01:36 -070090// SkASSERT(0);
halcanary96fcdcc2015-08-27 07:41:13 -070091 return nullptr;
caryclark27c8eb82015-07-06 11:38:33 -070092}
93
caryclark54359292015-03-26 07:52:43 -070094SkOpGlobalState* SkOpPtT::globalState() const {
caryclark55888e42016-07-18 10:01:36 -070095 return contour()->globalState();
caryclark54359292015-03-26 07:52:43 -070096}
97
98void SkOpPtT::init(SkOpSpanBase* span, double t, const SkPoint& pt, bool duplicate) {
99 fT = t;
100 fPt = pt;
101 fSpan = span;
102 fNext = this;
103 fDuplicatePt = duplicate;
104 fDeleted = false;
caryclark55888e42016-07-18 10:01:36 -0700105 fCoincident = false;
caryclark1049f122015-04-20 08:31:59 -0700106 SkDEBUGCODE(fID = span->globalState()->nextPtTID());
caryclark54359292015-03-26 07:52:43 -0700107}
108
109bool SkOpPtT::onEnd() const {
110 const SkOpSpanBase* span = this->span();
111 if (span->ptT() != this) {
112 return false;
113 }
114 const SkOpSegment* segment = this->segment();
115 return span == segment->head() || span == segment->tail();
116}
117
caryclark55888e42016-07-18 10:01:36 -0700118bool SkOpPtT::ptAlreadySeen(const SkOpPtT* check) const {
119 while (this != check) {
120 if (this->fPt == check->fPt) {
121 return true;
122 }
123 check = check->fNext;
124 }
125 return false;
126}
127
caryclark54359292015-03-26 07:52:43 -0700128SkOpPtT* SkOpPtT::prev() {
129 SkOpPtT* result = this;
130 SkOpPtT* next = this;
131 while ((next = next->fNext) != this) {
132 result = next;
133 }
134 SkASSERT(result->fNext == this);
135 return result;
136}
137
caryclark54359292015-03-26 07:52:43 -0700138const SkOpSegment* SkOpPtT::segment() const {
139 return span()->segment();
140}
141
142SkOpSegment* SkOpPtT::segment() {
143 return span()->segment();
144}
145
caryclark55888e42016-07-18 10:01:36 -0700146void SkOpPtT::setDeleted() {
147 SkASSERT(this->span()->debugDeleted() || this->span()->ptT() != this);
caryclark15976282016-07-21 05:48:43 -0700148 SkOPASSERT(!fDeleted);
caryclark55888e42016-07-18 10:01:36 -0700149 fDeleted = true;
150}
151
caryclark30b9fdd2016-08-31 14:36:29 -0700152void SkOpSpanBase::addOpp(SkOpSpanBase* opp) {
caryclark29b25632016-08-25 11:27:17 -0700153 SkOpPtT* oppPrev = this->ptT()->oppPrev(opp->ptT());
caryclark30b9fdd2016-08-31 14:36:29 -0700154 if (!oppPrev) {
caryclark54359292015-03-26 07:52:43 -0700155 return;
156 }
caryclark30b9fdd2016-08-31 14:36:29 -0700157 this->mergeMatches(opp);
158 this->ptT()->addOpp(opp->ptT(), oppPrev);
159 this->checkForCollapsedCoincidence();
caryclark55888e42016-07-18 10:01:36 -0700160}
161
Cary Clarkba610292018-06-19 09:47:15 -0400162SkOpSpanBase::Collapsed SkOpSpanBase::collapsed(double s, double e) const {
caryclark30b9fdd2016-08-31 14:36:29 -0700163 const SkOpPtT* start = &fPtT;
Cary Clarkba610292018-06-19 09:47:15 -0400164 const SkOpPtT* startNext = nullptr;
caryclark30b9fdd2016-08-31 14:36:29 -0700165 const SkOpPtT* walk = start;
166 double min = walk->fT;
167 double max = min;
168 const SkOpSegment* segment = this->segment();
169 while ((walk = walk->next()) != start) {
Cary Clarkba610292018-06-19 09:47:15 -0400170 if (walk == startNext) {
171 return Collapsed::kError;
172 }
caryclark30b9fdd2016-08-31 14:36:29 -0700173 if (walk->segment() != segment) {
174 continue;
175 }
176 min = SkTMin(min, walk->fT);
177 max = SkTMax(max, walk->fT);
178 if (between(min, s, max) && between(min, e, max)) {
Cary Clarkba610292018-06-19 09:47:15 -0400179 return Collapsed::kYes;
caryclark30b9fdd2016-08-31 14:36:29 -0700180 }
Cary Clarkba610292018-06-19 09:47:15 -0400181 startNext = start->next();
caryclark30b9fdd2016-08-31 14:36:29 -0700182 }
Cary Clarkba610292018-06-19 09:47:15 -0400183 return Collapsed::kNo;
caryclark30b9fdd2016-08-31 14:36:29 -0700184}
185
caryclark54359292015-03-26 07:52:43 -0700186bool SkOpSpanBase::contains(const SkOpSpanBase* span) const {
187 const SkOpPtT* start = &fPtT;
188 const SkOpPtT* check = &span->fPtT;
caryclarkef4f32a2016-08-24 09:24:18 -0700189 SkOPASSERT(start != check);
caryclark54359292015-03-26 07:52:43 -0700190 const SkOpPtT* walk = start;
191 while ((walk = walk->next()) != start) {
192 if (walk == check) {
193 return true;
194 }
195 }
196 return false;
197}
198
caryclark55888e42016-07-18 10:01:36 -0700199const SkOpPtT* SkOpSpanBase::contains(const SkOpSegment* segment) const {
200 const SkOpPtT* start = &fPtT;
201 const SkOpPtT* walk = start;
caryclark54359292015-03-26 07:52:43 -0700202 while ((walk = walk->next()) != start) {
caryclark55888e42016-07-18 10:01:36 -0700203 if (walk->deleted()) {
204 continue;
205 }
206 if (walk->segment() == segment && walk->span()->ptT() == walk) {
caryclark54359292015-03-26 07:52:43 -0700207 return walk;
208 }
209 }
halcanary96fcdcc2015-08-27 07:41:13 -0700210 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700211}
212
213bool SkOpSpanBase::containsCoinEnd(const SkOpSegment* segment) const {
214 SkASSERT(this->segment() != segment);
215 const SkOpSpanBase* next = this;
216 while ((next = next->fCoinEnd) != this) {
217 if (next->segment() == segment) {
218 return true;
219 }
220 }
221 return false;
222}
223
224SkOpContour* SkOpSpanBase::contour() const {
225 return segment()->contour();
226}
227
228SkOpGlobalState* SkOpSpanBase::globalState() const {
caryclark55888e42016-07-18 10:01:36 -0700229 return contour()->globalState();
caryclark54359292015-03-26 07:52:43 -0700230}
231
232void SkOpSpanBase::initBase(SkOpSegment* segment, SkOpSpan* prev, double t, const SkPoint& pt) {
233 fSegment = segment;
234 fPtT.init(this, t, pt, false);
235 fCoinEnd = this;
halcanary96fcdcc2015-08-27 07:41:13 -0700236 fFromAngle = nullptr;
caryclark54359292015-03-26 07:52:43 -0700237 fPrev = prev;
caryclark08bc8482015-04-24 09:08:57 -0700238 fSpanAdds = 0;
caryclark54359292015-03-26 07:52:43 -0700239 fAligned = true;
240 fChased = false;
caryclark1049f122015-04-20 08:31:59 -0700241 SkDEBUGCODE(fCount = 1);
242 SkDEBUGCODE(fID = globalState()->nextSpanID());
caryclark30b9fdd2016-08-31 14:36:29 -0700243 SkDEBUGCODE(fDebugDeleted = false);
caryclark54359292015-03-26 07:52:43 -0700244}
245
246// this pair of spans share a common t value or point; merge them and eliminate duplicates
247// this does not compute the best t or pt value; this merely moves all data into a single list
248void SkOpSpanBase::merge(SkOpSpan* span) {
249 SkOpPtT* spanPtT = span->ptT();
250 SkASSERT(this->t() != spanPtT->fT);
251 SkASSERT(!zero_or_one(spanPtT->fT));
mtklein18300a32016-03-16 13:53:35 -0700252 span->release(this->ptT());
caryclark55888e42016-07-18 10:01:36 -0700253 if (this->contains(span)) {
caryclark30b9fdd2016-08-31 14:36:29 -0700254 SkOPASSERT(0); // check to see if this ever happens -- should have been found earlier
caryclark55888e42016-07-18 10:01:36 -0700255 return; // merge is already in the ptT loop
256 }
caryclark54359292015-03-26 07:52:43 -0700257 SkOpPtT* remainder = spanPtT->next();
caryclark55888e42016-07-18 10:01:36 -0700258 this->ptT()->insert(spanPtT);
caryclark54359292015-03-26 07:52:43 -0700259 while (remainder != spanPtT) {
260 SkOpPtT* next = remainder->next();
261 SkOpPtT* compare = spanPtT->next();
262 while (compare != spanPtT) {
263 SkOpPtT* nextC = compare->next();
264 if (nextC->span() == remainder->span() && nextC->fT == remainder->fT) {
265 goto tryNextRemainder;
266 }
267 compare = nextC;
268 }
269 spanPtT->insert(remainder);
270tryNextRemainder:
271 remainder = next;
272 }
caryclark08bc8482015-04-24 09:08:57 -0700273 fSpanAdds += span->fSpanAdds;
caryclark30b9fdd2016-08-31 14:36:29 -0700274}
275
caryclark55888e42016-07-18 10:01:36 -0700276// please keep in sync with debugCheckForCollapsedCoincidence()
277void SkOpSpanBase::checkForCollapsedCoincidence() {
278 SkOpCoincidence* coins = this->globalState()->coincidence();
279 if (coins->isEmpty()) {
280 return;
281 }
282// the insert above may have put both ends of a coincident run in the same span
283// for each coincident ptT in loop; see if its opposite in is also in the loop
284// this implementation is the motivation for marking that a ptT is referenced by a coincident span
285 SkOpPtT* head = this->ptT();
286 SkOpPtT* test = head;
287 do {
288 if (!test->coincident()) {
289 continue;
290 }
291 coins->markCollapsed(test);
292 } while ((test = test->next()) != head);
caryclark30b9fdd2016-08-31 14:36:29 -0700293 coins->releaseDeleted();
294}
295
296// please keep in sync with debugMergeMatches()
297// Look to see if pt-t linked list contains same segment more than once
298// if so, and if each pt-t is directly pointed to by spans in that segment,
299// merge them
300// keep the points, but remove spans so that the segment doesn't have 2 or more
301// spans pointing to the same pt-t loop at different loop elements
302void SkOpSpanBase::mergeMatches(SkOpSpanBase* opp) {
303 SkOpPtT* test = &fPtT;
304 SkOpPtT* testNext;
305 const SkOpPtT* stop = test;
306 do {
307 testNext = test->next();
308 if (test->deleted()) {
309 continue;
310 }
311 SkOpSpanBase* testBase = test->span();
312 SkASSERT(testBase->ptT() == test);
313 SkOpSegment* segment = test->segment();
314 if (segment->done()) {
315 continue;
316 }
317 SkOpPtT* inner = opp->ptT();
318 const SkOpPtT* innerStop = inner;
319 do {
320 if (inner->segment() != segment) {
321 continue;
322 }
323 if (inner->deleted()) {
324 continue;
325 }
326 SkOpSpanBase* innerBase = inner->span();
327 SkASSERT(innerBase->ptT() == inner);
Ben Wagner63fd7602017-10-09 15:45:33 -0400328 // when the intersection is first detected, the span base is marked if there are
caryclark30b9fdd2016-08-31 14:36:29 -0700329 // more than one point in the intersection.
330 if (!zero_or_one(inner->fT)) {
331 innerBase->upCast()->release(test);
332 } else {
caryclarkb393a492016-09-07 08:21:09 -0700333 SkOPASSERT(inner->fT != test->fT);
caryclark30b9fdd2016-08-31 14:36:29 -0700334 if (!zero_or_one(test->fT)) {
335 testBase->upCast()->release(inner);
336 } else {
337 segment->markAllDone(); // mark segment as collapsed
338 SkDEBUGCODE(testBase->debugSetDeleted());
339 test->setDeleted();
340 SkDEBUGCODE(innerBase->debugSetDeleted());
341 inner->setDeleted();
342 }
343 }
344#ifdef SK_DEBUG // assert if another undeleted entry points to segment
345 const SkOpPtT* debugInner = inner;
346 while ((debugInner = debugInner->next()) != innerStop) {
347 if (debugInner->segment() != segment) {
348 continue;
349 }
350 if (debugInner->deleted()) {
351 continue;
352 }
353 SkOPASSERT(0);
354 }
355#endif
356 break;
357 } while ((inner = inner->next()) != innerStop);
358 } while ((test = testNext) != stop);
359 this->checkForCollapsedCoincidence();
caryclark54359292015-03-26 07:52:43 -0700360}
361
caryclarkbca19f72015-05-13 08:23:48 -0700362int SkOpSpan::computeWindSum() {
363 SkOpGlobalState* globals = this->globalState();
364 SkOpContour* contourHead = globals->contourHead();
365 int windTry = 0;
366 while (!this->sortableTop(contourHead) && ++windTry < SkOpGlobalState::kMaxWindingTries) {
367 ;
368 }
369 return this->windSum();
370}
371
caryclark54359292015-03-26 07:52:43 -0700372bool SkOpSpan::containsCoincidence(const SkOpSegment* segment) const {
373 SkASSERT(this->segment() != segment);
374 const SkOpSpan* next = fCoincident;
375 do {
376 if (next->segment() == segment) {
377 return true;
378 }
379 } while ((next = next->fCoincident) != this);
380 return false;
381}
382
caryclark55888e42016-07-18 10:01:36 -0700383void SkOpSpan::init(SkOpSegment* segment, SkOpSpan* prev, double t, const SkPoint& pt) {
384 SkASSERT(t != 1);
385 initBase(segment, prev, t, pt);
386 fCoincident = this;
387 fToAngle = nullptr;
388 fWindSum = fOppSum = SK_MinS32;
389 fWindValue = 1;
390 fOppValue = 0;
391 fTopTTry = 0;
392 fChased = fDone = false;
393 segment->bumpCount();
394 fAlreadyAdded = false;
395}
396
397// Please keep this in sync with debugInsertCoincidence()
caryclark81a478c2016-09-09 09:37:57 -0700398bool SkOpSpan::insertCoincidence(const SkOpSegment* segment, bool flipped, bool ordered) {
caryclark55888e42016-07-18 10:01:36 -0700399 if (this->containsCoincidence(segment)) {
400 return true;
401 }
402 SkOpPtT* next = &fPtT;
403 while ((next = next->next()) != &fPtT) {
404 if (next->segment() == segment) {
caryclark1493b972016-07-19 11:29:14 -0700405 SkOpSpan* span;
caryclark81a478c2016-09-09 09:37:57 -0700406 SkOpSpanBase* base = next->span();
407 if (!ordered) {
caryclarke6522ea2016-10-17 07:54:33 -0700408 const SkOpPtT* spanEndPtT = fNext->contains(segment);
409 FAIL_IF(!spanEndPtT);
410 const SkOpSpanBase* spanEnd = spanEndPtT->span();
caryclark81a478c2016-09-09 09:37:57 -0700411 const SkOpPtT* start = base->ptT()->starter(spanEnd->ptT());
caryclark45f04b82016-09-21 08:46:56 -0700412 FAIL_IF(!start->span()->upCastable());
caryclark81a478c2016-09-09 09:37:57 -0700413 span = const_cast<SkOpSpan*>(start->span()->upCast());
414 } else if (flipped) {
415 span = base->prev();
416 FAIL_IF(!span);
caryclark1493b972016-07-19 11:29:14 -0700417 } else {
caryclark81a478c2016-09-09 09:37:57 -0700418 FAIL_IF(!base->upCastable());
caryclark1493b972016-07-19 11:29:14 -0700419 span = base->upCast();
caryclark55888e42016-07-18 10:01:36 -0700420 }
421 this->insertCoincidence(span);
422 return true;
423 }
424 }
425#if DEBUG_COINCIDENCE
426 SkASSERT(0); // FIXME? if we get here, the span is missing its opposite segment...
427#endif
428 return true;
429}
430
431void SkOpSpan::release(const SkOpPtT* kept) {
caryclark30b9fdd2016-08-31 14:36:29 -0700432 SkDEBUGCODE(fDebugDeleted = true);
caryclarkb393a492016-09-07 08:21:09 -0700433 SkOPASSERT(kept->span() != this);
caryclark54359292015-03-26 07:52:43 -0700434 SkASSERT(!final());
435 SkOpSpan* prev = this->prev();
436 SkASSERT(prev);
437 SkOpSpanBase* next = this->next();
438 SkASSERT(next);
439 prev->setNext(next);
440 next->setPrev(prev);
mtklein18300a32016-03-16 13:53:35 -0700441 this->segment()->release(this);
caryclark218f21a2015-06-29 11:41:52 -0700442 SkOpCoincidence* coincidence = this->globalState()->coincidence();
443 if (coincidence) {
444 coincidence->fixUp(this->ptT(), kept);
445 }
caryclark54359292015-03-26 07:52:43 -0700446 this->ptT()->setDeleted();
caryclark55888e42016-07-18 10:01:36 -0700447 SkOpPtT* stopPtT = this->ptT();
448 SkOpPtT* testPtT = stopPtT;
449 const SkOpSpanBase* keptSpan = kept->span();
450 do {
451 if (this == testPtT->span()) {
452 testPtT->setSpan(keptSpan);
453 }
454 } while ((testPtT = testPtT->next()) != stopPtT);
caryclark54359292015-03-26 07:52:43 -0700455}
456
457void SkOpSpan::setOppSum(int oppSum) {
458 SkASSERT(!final());
459 if (fOppSum != SK_MinS32 && fOppSum != oppSum) {
460 this->globalState()->setWindingFailed();
461 return;
462 }
bungeman60e0fee2015-08-26 05:15:46 -0700463 SkASSERT(!DEBUG_LIMIT_WIND_SUM || SkTAbs(oppSum) <= DEBUG_LIMIT_WIND_SUM);
caryclark54359292015-03-26 07:52:43 -0700464 fOppSum = oppSum;
465}
caryclark624637c2015-05-11 07:21:27 -0700466
467void SkOpSpan::setWindSum(int windSum) {
468 SkASSERT(!final());
469 if (fWindSum != SK_MinS32 && fWindSum != windSum) {
470 this->globalState()->setWindingFailed();
471 return;
472 }
bungeman60e0fee2015-08-26 05:15:46 -0700473 SkASSERT(!DEBUG_LIMIT_WIND_SUM || SkTAbs(windSum) <= DEBUG_LIMIT_WIND_SUM);
caryclark624637c2015-05-11 07:21:27 -0700474 fWindSum = windSum;
475}