blob: 360179d6631fc3397ac28de97f8fb0f215d97f2c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkPathMeasure.h"
10#include "include/core/SkStrokeRec.h"
11#include "include/effects/Sk1DPathEffect.h"
12#include "src/core/SkReadBuffer.h"
13#include "src/core/SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
Mike Reed22234652018-02-27 09:48:39 -050015// Since we are stepping by a float, the do/while loop might go on forever (or nearly so).
16// Put in a governor to limit crash values from looping too long (and allocating too much ram).
17#define MAX_REASONABLE_ITERATIONS 100000
18
Mike Reed6d10f8b2018-08-16 13:22:16 -040019bool Sk1DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
20 SkStrokeRec*, const SkRect*) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 SkPathMeasure meas(src, false);
22 do {
Mike Reed22234652018-02-27 09:48:39 -050023 int governor = MAX_REASONABLE_ITERATIONS;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 SkScalar length = meas.getLength();
25 SkScalar distance = this->begin(length);
Mike Reed22234652018-02-27 09:48:39 -050026 while (distance < length && --governor >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 SkScalar delta = this->next(dst, distance, meas);
reed@google.comd7a6fb92011-08-12 14:04:15 +000028 if (delta <= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 break;
reed@google.comd7a6fb92011-08-12 14:04:15 +000030 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 distance += delta;
32 }
33 } while (meas.nextContour());
34 return true;
35}
36
reed@google.comd7a6fb92011-08-12 14:04:15 +000037///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
Mike Reedfc015d22018-02-24 09:51:47 -050039SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, SkScalar phase,
40 Style style) : fPath(path) {
reedca726ab2016-02-22 12:50:25 -080041 SkASSERT(advance > 0 && !path.isEmpty());
Mike Reedfc015d22018-02-24 09:51:47 -050042 SkASSERT((unsigned)style <= kMorph_Style);
43
Ben Wagner20054dc2019-04-10 11:16:33 -040044 // Make the path thread-safe.
45 fPath.updateBoundsCache();
46 (void)fPath.getGenerationID();
47
reedca726ab2016-02-22 12:50:25 -080048 // cleanup their phase parameter, inverting it so that it becomes an
49 // offset along the path (to match the interpretation in PostScript)
50 if (phase < 0) {
51 phase = -phase;
52 if (phase > advance) {
53 phase = SkScalarMod(phase, advance);
54 }
reed@google.comd7a6fb92011-08-12 14:04:15 +000055 } else {
reedca726ab2016-02-22 12:50:25 -080056 if (phase > advance) {
57 phase = SkScalarMod(phase, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 }
reedca726ab2016-02-22 12:50:25 -080059 phase = advance - phase;
reed653db512016-02-22 06:34:47 -080060 }
reedca726ab2016-02-22 12:50:25 -080061 // now catch the edge case where phase == advance (within epsilon)
62 if (phase >= advance) {
63 phase = 0;
64 }
65 SkASSERT(phase >= 0);
66
67 fAdvance = advance;
68 fInitialOffset = phase;
69
70 if ((unsigned)style > kMorph_Style) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -040071 SkDEBUGF("SkPath1DPathEffect style enum out of range %d\n", style);
reedca726ab2016-02-22 12:50:25 -080072 }
73 fStyle = style;
reed@android.com8a1c16f2008-12-17 15:59:43 +000074}
75
Mike Reed6d10f8b2018-08-16 13:22:16 -040076bool SkPath1DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
77 SkStrokeRec* rec, const SkRect* cullRect) const {
Mike Reedfc015d22018-02-24 09:51:47 -050078 rec->setFillStyle();
Mike Reed6d10f8b2018-08-16 13:22:16 -040079 return this->INHERITED::onFilterPath(dst, src, rec, cullRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080}
81
reed@google.comf3edf9f2012-04-12 19:44:38 +000082static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000083 SkPathMeasure& meas, SkScalar dist) {
84 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkPoint pos;
86 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 SkScalar sx = src[i].fX;
89 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
reed@google.comf3edf9f2012-04-12 19:44:38 +000091 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
92 return false;
93 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 SkMatrix matrix;
96 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 pt.set(sx, sy);
99 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
100 matrix.preTranslate(-sx, 0);
101 matrix.postTranslate(pos.fX, pos.fY);
102 matrix.mapPoints(&dst[i], &pt, 1);
103 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000104 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105}
106
107/* TODO
108
109Need differentially more subdivisions when the follow-path is curvy. Not sure how to
110determine that, but we need it. I guess a cheap answer is let the caller tell us,
111but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
112*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000113static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
114 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 SkPath::Iter iter(src, false);
116 SkPoint srcP[4], dstP[3];
117 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000118
119 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 switch (verb) {
121 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000122 if (morphpoints(dstP, srcP, 1, meas, dist)) {
123 dst->moveTo(dstP[0]);
124 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 break;
126 case SkPath::kLine_Verb:
127 srcP[2] = srcP[1];
128 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
129 SkScalarAve(srcP[0].fY, srcP[2].fY));
130 // fall through to quad
131 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000132 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
133 dst->quadTo(dstP[0], dstP[1]);
134 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 break;
Mike Reedb50e3852018-01-29 15:27:33 -0500136 case SkPath::kConic_Verb:
137 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
138 dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
139 }
140 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000142 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
143 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
144 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 break;
146 case SkPath::kClose_Verb:
147 dst->close();
148 break;
149 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000150 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 break;
152 }
153 }
154}
155
reed@google.com548a1f32012-12-18 16:12:09 +0000156SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 return fInitialOffset;
158}
159
reed60c9b582016-04-03 09:11:13 -0700160sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700161 SkScalar advance = buffer.readScalar();
Mike Reedfc015d22018-02-24 09:51:47 -0500162 SkPath path;
163 buffer.readPath(&path);
164 SkScalar phase = buffer.readScalar();
165 Style style = buffer.read32LE(kLastEnum_Style);
166 return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
reed9fa60da2014-08-21 07:59:51 -0700167}
168
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000169void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 buffer.writeScalar(fAdvance);
Mike Reedfc015d22018-02-24 09:51:47 -0500171 buffer.writePath(fPath);
172 buffer.writeScalar(fInitialOffset);
173 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174}
175
reed@google.comd7a6fb92011-08-12 14:04:15 +0000176SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
reed@google.com548a1f32012-12-18 16:12:09 +0000177 SkPathMeasure& meas) const {
Cary Clark472ab812018-06-19 10:47:15 -0400178#if defined(IS_FUZZING_WITH_LIBFUZZER)
179 if (dst->countPoints() > 100000) {
180 return fAdvance;
181 }
182#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000184 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 SkPoint pos;
halcanary96fcdcc2015-08-27 07:41:13 -0700186 if (meas.getPosTan(distance, &pos, nullptr)) {
reed@google.comf3edf9f2012-04-12 19:44:38 +0000187 dst->addPath(fPath, pos.fX, pos.fY);
188 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000189 } break;
190 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000192 if (meas.getMatrix(distance, &matrix)) {
193 dst->addPath(fPath, matrix);
194 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000195 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 case kMorph_Style:
197 morphpath(dst, fPath, meas, distance);
198 break;
199 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000200 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 break;
202 }
203 return fAdvance;
204}
robertphillips42dbfa82015-01-26 06:08:52 -0800205
reedca726ab2016-02-22 12:50:25 -0800206///////////////////////////////////////////////////////////////////////////////////////////////////
207
reeda4393342016-03-18 11:22:57 -0700208sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
209 Style style) {
Mike Reedfc015d22018-02-24 09:51:47 -0500210 if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) {
reedca726ab2016-02-22 12:50:25 -0800211 return nullptr;
212 }
reeda4393342016-03-18 11:22:57 -0700213 return sk_sp<SkPathEffect>(new SkPath1DPathEffect(path, advance, phase, style));
reedca726ab2016-02-22 12:50:25 -0800214}