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@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 13 | void SkOpContour::toPath(SkPathWriter* path) const { |
Cary Clark | 0eb6ed4 | 2016-12-16 16:31:11 -0500 | [diff] [blame] | 14 | if (!this->count()) { |
| 15 | return; |
| 16 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 17 | const SkOpSegment* segment = &fHead; |
| 18 | do { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 19 | SkAssertResult(segment->addCurveTo(segment->head(), segment->tail(), path)); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 20 | } while ((segment = segment->next())); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 21 | path->finishContour(); |
| 22 | path->assemble(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 23 | } |
| 24 | |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 25 | void SkOpContour::toReversePath(SkPathWriter* path) const { |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 26 | const SkOpSegment* segment = fTail; |
| 27 | do { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 28 | SkAssertResult(segment->addCurveTo(segment->tail(), segment->head(), path)); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 29 | } while ((segment = segment->prev())); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 30 | path->finishContour(); |
| 31 | path->assemble(); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Cary Clark | ab2d73b | 2016-12-16 17:17:25 -0500 | [diff] [blame] | 34 | SkOpSpan* SkOpContour::undoneSpan() { |
| 35 | SkOpSegment* testSegment = &fHead; |
| 36 | bool allDone = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 37 | do { |
Cary Clark | ab2d73b | 2016-12-16 17:17:25 -0500 | [diff] [blame] | 38 | if (testSegment->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 39 | continue; |
| 40 | } |
Cary Clark | ab2d73b | 2016-12-16 17:17:25 -0500 | [diff] [blame] | 41 | allDone = false; |
| 42 | return testSegment->undoneSpan(); |
| 43 | } while ((testSegment = testSegment->next())); |
| 44 | if (allDone) { |
| 45 | fDone = true; |
| 46 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 48 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 49 | |
| 50 | void SkOpContourBuilder::addConic(SkPoint pts[3], SkScalar weight) { |
| 51 | this->flush(); |
| 52 | fContour->addConic(pts, weight); |
| 53 | } |
| 54 | |
| 55 | void SkOpContourBuilder::addCubic(SkPoint pts[4]) { |
| 56 | this->flush(); |
| 57 | fContour->addCubic(pts); |
| 58 | } |
| 59 | |
| 60 | void SkOpContourBuilder::addCurve(SkPath::Verb verb, const SkPoint pts[4], SkScalar weight) { |
| 61 | if (SkPath::kLine_Verb == verb) { |
| 62 | this->addLine(pts); |
| 63 | return; |
| 64 | } |
Herb Derby | c3cc5fa | 2017-03-07 11:11:47 -0500 | [diff] [blame^] | 65 | SkArenaAlloc* allocator = fContour->globalState()->allocator(); |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 66 | switch (verb) { |
| 67 | case SkPath::kQuad_Verb: { |
| 68 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 3); |
| 69 | memcpy(ptStorage, pts, sizeof(SkPoint) * 3); |
| 70 | this->addQuad(ptStorage); |
| 71 | } break; |
| 72 | case SkPath::kConic_Verb: { |
| 73 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 3); |
| 74 | memcpy(ptStorage, pts, sizeof(SkPoint) * 3); |
| 75 | this->addConic(ptStorage, weight); |
| 76 | } break; |
| 77 | case SkPath::kCubic_Verb: { |
| 78 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 4); |
| 79 | memcpy(ptStorage, pts, sizeof(SkPoint) * 4); |
| 80 | this->addCubic(ptStorage); |
| 81 | } break; |
| 82 | default: |
| 83 | SkASSERT(0); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void SkOpContourBuilder::addLine(const SkPoint pts[2]) { |
| 88 | // if the previous line added is the exact opposite, eliminate both |
| 89 | if (fLastIsLine) { |
| 90 | if (fLastLine[0] == pts[1] && fLastLine[1] == pts[0]) { |
| 91 | fLastIsLine = false; |
| 92 | return; |
| 93 | } else { |
| 94 | flush(); |
| 95 | } |
| 96 | } |
| 97 | memcpy(fLastLine, pts, sizeof(fLastLine)); |
| 98 | fLastIsLine = true; |
| 99 | } |
| 100 | |
| 101 | void SkOpContourBuilder::addQuad(SkPoint pts[3]) { |
| 102 | this->flush(); |
| 103 | fContour->addQuad(pts); |
| 104 | } |
| 105 | |
| 106 | void SkOpContourBuilder::flush() { |
| 107 | if (!fLastIsLine) |
| 108 | return; |
Herb Derby | c3cc5fa | 2017-03-07 11:11:47 -0500 | [diff] [blame^] | 109 | SkArenaAlloc* allocator = fContour->globalState()->allocator(); |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 110 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 2); |
| 111 | memcpy(ptStorage, fLastLine, sizeof(fLastLine)); |
| 112 | (void) fContour->addLine(ptStorage); |
| 113 | fLastIsLine = false; |
| 114 | } |