caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 7 | #include "SkOpContour.h" |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 8 | #include "SkOpTAllocator.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 9 | #include "SkPathWriter.h" |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 10 | #include "SkReduceOrder.h" |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 11 | #include "SkTSort.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 12 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 13 | void SkOpContour::addCurve(SkPath::Verb verb, const SkPoint pts[4], SkChunkAlloc* allocator) { |
| 14 | switch (verb) { |
| 15 | case SkPath::kLine_Verb: { |
| 16 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 2); |
| 17 | memcpy(ptStorage, pts, sizeof(SkPoint) * 2); |
| 18 | appendSegment(allocator).addLine(ptStorage, this); |
| 19 | } break; |
| 20 | case SkPath::kQuad_Verb: { |
| 21 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 3); |
| 22 | memcpy(ptStorage, pts, sizeof(SkPoint) * 3); |
| 23 | appendSegment(allocator).addQuad(ptStorage, this); |
| 24 | } break; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame^] | 25 | case SkPath::kConic_Verb: { |
| 26 | SkASSERT(0); // the original curve is a cubic, which will never reduce to a conic |
| 27 | } break; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 28 | case SkPath::kCubic_Verb: { |
| 29 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 4); |
| 30 | memcpy(ptStorage, pts, sizeof(SkPoint) * 4); |
| 31 | appendSegment(allocator).addCubic(ptStorage, this); |
| 32 | } break; |
| 33 | default: |
| 34 | SkASSERT(0); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 35 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 36 | } |
| 37 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 38 | SkOpSegment* SkOpContour::nonVerticalSegment(SkOpSpanBase** start, SkOpSpanBase** end) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 39 | int segmentCount = fSortedSegments.count(); |
| 40 | SkASSERT(segmentCount > 0); |
| 41 | for (int sortedIndex = fFirstSorted; sortedIndex < segmentCount; ++sortedIndex) { |
| 42 | SkOpSegment* testSegment = fSortedSegments[sortedIndex]; |
| 43 | if (testSegment->done()) { |
| 44 | continue; |
| 45 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 46 | SkOpSpanBase* span = testSegment->head(); |
| 47 | SkOpSpanBase* testS, * testE; |
| 48 | while (SkOpSegment::NextCandidate(span, &testS, &testE)) { |
| 49 | if (!testSegment->isVertical(testS, testE)) { |
| 50 | *start = testS; |
| 51 | *end = testE; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 52 | return testSegment; |
| 53 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 54 | span = span->upCast()->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | return NULL; |
| 58 | } |
| 59 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 60 | void SkOpContour::toPath(SkPathWriter* path) const { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 61 | const SkPoint& pt = fHead.pts()[0]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 62 | path->deferredMove(pt); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 63 | const SkOpSegment* segment = &fHead; |
| 64 | do { |
| 65 | segment->addCurveTo(segment->head(), segment->tail(), path, true); |
| 66 | } while ((segment = segment->next())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 67 | path->close(); |
| 68 | } |
| 69 | |
| 70 | void SkOpContour::topSortableSegment(const SkPoint& topLeft, SkPoint* bestXY, |
| 71 | SkOpSegment** topStart) { |
| 72 | int segmentCount = fSortedSegments.count(); |
| 73 | SkASSERT(segmentCount > 0); |
| 74 | int sortedIndex = fFirstSorted; |
| 75 | fDone = true; // may be cleared below |
| 76 | for ( ; sortedIndex < segmentCount; ++sortedIndex) { |
| 77 | SkOpSegment* testSegment = fSortedSegments[sortedIndex]; |
| 78 | if (testSegment->done()) { |
| 79 | if (sortedIndex == fFirstSorted) { |
| 80 | ++fFirstSorted; |
| 81 | } |
| 82 | continue; |
| 83 | } |
| 84 | fDone = false; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 85 | SkPoint testXY = testSegment->activeLeftTop(NULL); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 86 | if (*topStart) { |
| 87 | if (testXY.fY < topLeft.fY) { |
| 88 | continue; |
| 89 | } |
| 90 | if (testXY.fY == topLeft.fY && testXY.fX < topLeft.fX) { |
| 91 | continue; |
| 92 | } |
| 93 | if (bestXY->fY < testXY.fY) { |
| 94 | continue; |
| 95 | } |
| 96 | if (bestXY->fY == testXY.fY && bestXY->fX < testXY.fX) { |
| 97 | continue; |
| 98 | } |
| 99 | } |
| 100 | *topStart = testSegment; |
| 101 | *bestXY = testXY; |
| 102 | } |
| 103 | } |
| 104 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 105 | SkOpSegment* SkOpContour::undoneSegment(SkOpSpanBase** startPtr, SkOpSpanBase** endPtr) { |
| 106 | SkOpSegment* segment = &fHead; |
| 107 | do { |
| 108 | if (segment->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 109 | continue; |
| 110 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 111 | segment->undoneSpan(startPtr, endPtr); |
| 112 | return segment; |
| 113 | } while ((segment = segment->next())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 114 | return NULL; |
| 115 | } |