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 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 34 | SkOpSegment* SkOpContour::undoneSegment(SkOpSpanBase** startPtr, SkOpSpanBase** endPtr) { |
| 35 | SkOpSegment* segment = &fHead; |
| 36 | do { |
| 37 | if (segment->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 38 | continue; |
| 39 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 40 | segment->undoneSpan(startPtr, endPtr); |
| 41 | return segment; |
| 42 | } while ((segment = segment->next())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 44 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 45 | |
| 46 | void SkOpContourBuilder::addConic(SkPoint pts[3], SkScalar weight) { |
| 47 | this->flush(); |
| 48 | fContour->addConic(pts, weight); |
| 49 | } |
| 50 | |
| 51 | void SkOpContourBuilder::addCubic(SkPoint pts[4]) { |
| 52 | this->flush(); |
| 53 | fContour->addCubic(pts); |
| 54 | } |
| 55 | |
| 56 | void SkOpContourBuilder::addCurve(SkPath::Verb verb, const SkPoint pts[4], SkScalar weight) { |
| 57 | if (SkPath::kLine_Verb == verb) { |
| 58 | this->addLine(pts); |
| 59 | return; |
| 60 | } |
| 61 | SkChunkAlloc* allocator = fContour->globalState()->allocator(); |
| 62 | switch (verb) { |
| 63 | case SkPath::kQuad_Verb: { |
| 64 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 3); |
| 65 | memcpy(ptStorage, pts, sizeof(SkPoint) * 3); |
| 66 | this->addQuad(ptStorage); |
| 67 | } break; |
| 68 | case SkPath::kConic_Verb: { |
| 69 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 3); |
| 70 | memcpy(ptStorage, pts, sizeof(SkPoint) * 3); |
| 71 | this->addConic(ptStorage, weight); |
| 72 | } break; |
| 73 | case SkPath::kCubic_Verb: { |
| 74 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 4); |
| 75 | memcpy(ptStorage, pts, sizeof(SkPoint) * 4); |
| 76 | this->addCubic(ptStorage); |
| 77 | } break; |
| 78 | default: |
| 79 | SkASSERT(0); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void SkOpContourBuilder::addLine(const SkPoint pts[2]) { |
| 84 | // if the previous line added is the exact opposite, eliminate both |
| 85 | if (fLastIsLine) { |
| 86 | if (fLastLine[0] == pts[1] && fLastLine[1] == pts[0]) { |
| 87 | fLastIsLine = false; |
| 88 | return; |
| 89 | } else { |
| 90 | flush(); |
| 91 | } |
| 92 | } |
| 93 | memcpy(fLastLine, pts, sizeof(fLastLine)); |
| 94 | fLastIsLine = true; |
| 95 | } |
| 96 | |
| 97 | void SkOpContourBuilder::addQuad(SkPoint pts[3]) { |
| 98 | this->flush(); |
| 99 | fContour->addQuad(pts); |
| 100 | } |
| 101 | |
| 102 | void SkOpContourBuilder::flush() { |
| 103 | if (!fLastIsLine) |
| 104 | return; |
| 105 | SkChunkAlloc* allocator = fContour->globalState()->allocator(); |
| 106 | SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 2); |
| 107 | memcpy(ptStorage, fLastLine, sizeof(fLastLine)); |
| 108 | (void) fContour->addLine(ptStorage); |
| 109 | fLastIsLine = false; |
| 110 | } |