blob: ea1659ee2bc7aa4ac06ea46efc6103af85f43acc [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"
caryclark54359292015-03-26 07:52:43 -07008#include "SkOpTAllocator.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +00009#include "SkPathWriter.h"
caryclark54359292015-03-26 07:52:43 -070010#include "SkReduceOrder.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000011#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000012
caryclark@google.com07393ca2013-04-08 11:47:37 +000013void SkOpContour::toPath(SkPathWriter* path) const {
Cary Clark0eb6ed42016-12-16 16:31:11 -050014 if (!this->count()) {
15 return;
16 }
caryclark54359292015-03-26 07:52:43 -070017 const SkOpSegment* segment = &fHead;
18 do {
caryclarkef784fb2015-10-30 12:03:06 -070019 SkAssertResult(segment->addCurveTo(segment->head(), segment->tail(), path));
caryclark54359292015-03-26 07:52:43 -070020 } while ((segment = segment->next()));
caryclarkeed356d2016-09-14 07:18:20 -070021 path->finishContour();
22 path->assemble();
caryclark@google.com07393ca2013-04-08 11:47:37 +000023}
24
caryclark5b5ddd72015-05-18 05:12:56 -070025void SkOpContour::toReversePath(SkPathWriter* path) const {
caryclark5b5ddd72015-05-18 05:12:56 -070026 const SkOpSegment* segment = fTail;
27 do {
caryclarkef784fb2015-10-30 12:03:06 -070028 SkAssertResult(segment->addCurveTo(segment->tail(), segment->head(), path));
caryclark5b5ddd72015-05-18 05:12:56 -070029 } while ((segment = segment->prev()));
caryclarkeed356d2016-09-14 07:18:20 -070030 path->finishContour();
31 path->assemble();
caryclark5b5ddd72015-05-18 05:12:56 -070032}
33
Cary Clarkab2d73b2016-12-16 17:17:25 -050034SkOpSpan* SkOpContour::undoneSpan() {
35 SkOpSegment* testSegment = &fHead;
36 bool allDone = true;
caryclark54359292015-03-26 07:52:43 -070037 do {
Cary Clarkab2d73b2016-12-16 17:17:25 -050038 if (testSegment->done()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000039 continue;
40 }
Cary Clarkab2d73b2016-12-16 17:17:25 -050041 allDone = false;
42 return testSegment->undoneSpan();
43 } while ((testSegment = testSegment->next()));
44 if (allDone) {
45 fDone = true;
46 }
halcanary96fcdcc2015-08-27 07:41:13 -070047 return nullptr;
caryclark@google.com07393ca2013-04-08 11:47:37 +000048}
Cary Clarkff114282016-12-14 11:56:16 -050049
50void SkOpContourBuilder::addConic(SkPoint pts[3], SkScalar weight) {
51 this->flush();
52 fContour->addConic(pts, weight);
53}
54
55void SkOpContourBuilder::addCubic(SkPoint pts[4]) {
56 this->flush();
57 fContour->addCubic(pts);
58}
59
60void 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 Derbyc3cc5fa2017-03-07 11:11:47 -050065 SkArenaAlloc* allocator = fContour->globalState()->allocator();
Cary Clarkff114282016-12-14 11:56:16 -050066 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
87void 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
101void SkOpContourBuilder::addQuad(SkPoint pts[3]) {
102 this->flush();
103 fContour->addQuad(pts);
104}
105
106void SkOpContourBuilder::flush() {
107 if (!fLastIsLine)
108 return;
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500109 SkArenaAlloc* allocator = fContour->globalState()->allocator();
Cary Clarkff114282016-12-14 11:56:16 -0500110 SkPoint* ptStorage = SkOpTAllocator<SkPoint>::AllocateArray(allocator, 2);
111 memcpy(ptStorage, fLastLine, sizeof(fLastLine));
112 (void) fContour->addLine(ptStorage);
113 fLastIsLine = false;
114}