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