blob: f7de7ab735480098efaaa42b66f5b7e9783b2c54 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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.com07393ca2013-04-08 11:47:37 +00007#include "SkOpContour.h"
8#include "SkPathWriter.h"
caryclark54359292015-03-26 07:52:43 -07009#include "SkReduceOrder.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000010#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000011
caryclark@google.com07393ca2013-04-08 11:47:37 +000012void SkOpContour::toPath(SkPathWriter* path) const {
Cary Clark0eb6ed42016-12-16 16:31:11 -050013 if (!this->count()) {
14 return;
15 }
caryclark54359292015-03-26 07:52:43 -070016 const SkOpSegment* segment = &fHead;
17 do {
caryclarkef784fb2015-10-30 12:03:06 -070018 SkAssertResult(segment->addCurveTo(segment->head(), segment->tail(), path));
caryclark54359292015-03-26 07:52:43 -070019 } while ((segment = segment->next()));
caryclarkeed356d2016-09-14 07:18:20 -070020 path->finishContour();
21 path->assemble();
caryclark@google.com07393ca2013-04-08 11:47:37 +000022}
23
caryclark5b5ddd72015-05-18 05:12:56 -070024void SkOpContour::toReversePath(SkPathWriter* path) const {
caryclark5b5ddd72015-05-18 05:12:56 -070025 const SkOpSegment* segment = fTail;
26 do {
caryclarkef784fb2015-10-30 12:03:06 -070027 SkAssertResult(segment->addCurveTo(segment->tail(), segment->head(), path));
caryclark5b5ddd72015-05-18 05:12:56 -070028 } while ((segment = segment->prev()));
caryclarkeed356d2016-09-14 07:18:20 -070029 path->finishContour();
30 path->assemble();
caryclark5b5ddd72015-05-18 05:12:56 -070031}
32
Cary Clarkab2d73b2016-12-16 17:17:25 -050033SkOpSpan* SkOpContour::undoneSpan() {
34 SkOpSegment* testSegment = &fHead;
caryclark54359292015-03-26 07:52:43 -070035 do {
Cary Clarkab2d73b2016-12-16 17:17:25 -050036 if (testSegment->done()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000037 continue;
38 }
Cary Clarkab2d73b2016-12-16 17:17:25 -050039 return testSegment->undoneSpan();
40 } while ((testSegment = testSegment->next()));
Mike Klein1d746202018-01-25 17:32:51 -050041 fDone = true;
halcanary96fcdcc2015-08-27 07:41:13 -070042 return nullptr;
caryclark@google.com07393ca2013-04-08 11:47:37 +000043}
Cary Clarkff114282016-12-14 11:56:16 -050044
45void SkOpContourBuilder::addConic(SkPoint pts[3], SkScalar weight) {
46 this->flush();
47 fContour->addConic(pts, weight);
48}
49
50void SkOpContourBuilder::addCubic(SkPoint pts[4]) {
51 this->flush();
52 fContour->addCubic(pts);
53}
54
55void 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 Derbyc3cc5fa2017-03-07 11:11:47 -050060 SkArenaAlloc* allocator = fContour->globalState()->allocator();
Cary Clarkff114282016-12-14 11:56:16 -050061 switch (verb) {
62 case SkPath::kQuad_Verb: {
Herb Derbyecc364c2017-04-19 15:09:48 -040063 SkPoint* ptStorage = allocator->makeArrayDefault<SkPoint>(3);
Cary Clarkff114282016-12-14 11:56:16 -050064 memcpy(ptStorage, pts, sizeof(SkPoint) * 3);
65 this->addQuad(ptStorage);
66 } break;
67 case SkPath::kConic_Verb: {
Herb Derbyecc364c2017-04-19 15:09:48 -040068 SkPoint* ptStorage = allocator->makeArrayDefault<SkPoint>(3);
Cary Clarkff114282016-12-14 11:56:16 -050069 memcpy(ptStorage, pts, sizeof(SkPoint) * 3);
70 this->addConic(ptStorage, weight);
71 } break;
72 case SkPath::kCubic_Verb: {
Herb Derbyecc364c2017-04-19 15:09:48 -040073 SkPoint* ptStorage = allocator->makeArrayDefault<SkPoint>(4);
Cary Clarkff114282016-12-14 11:56:16 -050074 memcpy(ptStorage, pts, sizeof(SkPoint) * 4);
75 this->addCubic(ptStorage);
76 } break;
77 default:
78 SkASSERT(0);
79 }
80}
81
82void 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
96void SkOpContourBuilder::addQuad(SkPoint pts[3]) {
97 this->flush();
98 fContour->addQuad(pts);
99}
100
101void SkOpContourBuilder::flush() {
102 if (!fLastIsLine)
103 return;
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500104 SkArenaAlloc* allocator = fContour->globalState()->allocator();
Herb Derbyecc364c2017-04-19 15:09:48 -0400105 SkPoint* ptStorage = allocator->makeArrayDefault<SkPoint>(2);
Cary Clarkff114282016-12-14 11:56:16 -0500106 memcpy(ptStorage, fLastLine, sizeof(fLastLine));
107 (void) fContour->addLine(ptStorage);
108 fLastIsLine = false;
109}