blob: 44787a04c8eebf803f33a98fc497911e0ffc74dd [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"
Mike Reedec9d0e82021-05-21 17:42:14 -040012#include "src/core/SkPathEffectBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkReadBuffer.h"
14#include "src/core/SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
Mike Reed22234652018-02-27 09:48:39 -050016// Since we are stepping by a float, the do/while loop might go on forever (or nearly so).
17// Put in a governor to limit crash values from looping too long (and allocating too much ram).
18#define MAX_REASONABLE_ITERATIONS 100000
19
Mike Reedec9d0e82021-05-21 17:42:14 -040020class Sk1DPathEffect : public SkPathEffectBase {
Mike Reedec87dc12021-05-20 15:16:34 -040021public:
22protected:
Tyler Dennistonf8b7c1a2021-07-13 13:22:19 -040023 bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, const SkRect*,
24 const SkMatrix&) const override {
Mike Reedec87dc12021-05-20 15:16:34 -040025 SkPathMeasure meas(src, false);
26 do {
27 int governor = MAX_REASONABLE_ITERATIONS;
28 SkScalar length = meas.getLength();
29 SkScalar distance = this->begin(length);
30 while (distance < length && --governor >= 0) {
31 SkScalar delta = this->next(dst, distance, meas);
32 if (delta <= 0) {
33 break;
34 }
35 distance += delta;
reed@google.comd7a6fb92011-08-12 14:04:15 +000036 }
Mike Reedec87dc12021-05-20 15:16:34 -040037 } while (meas.nextContour());
38 return true;
39 }
40
41 /** Called at the start of each contour, returns the initial offset
42 into that contour.
43 */
44 virtual SkScalar begin(SkScalar contourLength) const = 0;
45 /** Called with the current distance along the path, with the current matrix
46 for the point/tangent at the specified distance.
47 Return the distance to travel for the next call. If return <= 0, then that
48 contour is done.
49 */
50 virtual SkScalar next(SkPath* dst, SkScalar dist, SkPathMeasure&) const = 0;
51
52private:
53 // For simplicity, assume fast bounds cannot be computed
54 bool computeFastBounds(SkRect*) const override { return false; }
55
56 using INHERITED = SkPathEffect;
57};
reed@android.com8a1c16f2008-12-17 15:59:43 +000058
reed@google.comd7a6fb92011-08-12 14:04:15 +000059///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
Mike Reedec87dc12021-05-20 15:16:34 -040061class SkPath1DPathEffectImpl : public Sk1DPathEffect {
62public:
63 SkPath1DPathEffectImpl(const SkPath& path, SkScalar advance, SkScalar phase,
64 SkPath1DPathEffect::Style style) : fPath(path) {
65 SkASSERT(advance > 0 && !path.isEmpty());
Mike Reedfc015d22018-02-24 09:51:47 -050066
Mike Reedec87dc12021-05-20 15:16:34 -040067 // Make the path thread-safe.
68 fPath.updateBoundsCache();
69 (void)fPath.getGenerationID();
Ben Wagner20054dc2019-04-10 11:16:33 -040070
Mike Reedec87dc12021-05-20 15:16:34 -040071 // cleanup their phase parameter, inverting it so that it becomes an
72 // offset along the path (to match the interpretation in PostScript)
73 if (phase < 0) {
74 phase = -phase;
75 if (phase > advance) {
76 phase = SkScalarMod(phase, advance);
77 }
78 } else {
79 if (phase > advance) {
80 phase = SkScalarMod(phase, advance);
81 }
82 phase = advance - phase;
reedca726ab2016-02-22 12:50:25 -080083 }
Mike Reedec87dc12021-05-20 15:16:34 -040084 // now catch the edge case where phase == advance (within epsilon)
85 if (phase >= advance) {
86 phase = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 }
Mike Reedec87dc12021-05-20 15:16:34 -040088 SkASSERT(phase >= 0);
reedca726ab2016-02-22 12:50:25 -080089
Mike Reedec87dc12021-05-20 15:16:34 -040090 fAdvance = advance;
91 fInitialOffset = phase;
92 fStyle = style;
reedca726ab2016-02-22 12:50:25 -080093 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
Mike Reedec87dc12021-05-20 15:16:34 -040095 bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
Tyler Dennistonf8b7c1a2021-07-13 13:22:19 -040096 const SkRect* cullRect, const SkMatrix& ctm) const override {
Mike Reedec87dc12021-05-20 15:16:34 -040097 rec->setFillStyle();
Tyler Dennistonf8b7c1a2021-07-13 13:22:19 -040098 return this->INHERITED::onFilterPath(dst, src, rec, cullRect, ctm);
Mike Reedec87dc12021-05-20 15:16:34 -040099 }
100
101 SkScalar begin(SkScalar contourLength) const override {
102 return fInitialOffset;
103 }
104
105 SkScalar next(SkPath*, SkScalar, SkPathMeasure&) const override;
106
107 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
108 SkScalar advance = buffer.readScalar();
109 SkPath path;
110 buffer.readPath(&path);
111 SkScalar phase = buffer.readScalar();
112 SkPath1DPathEffect::Style style = buffer.read32LE(SkPath1DPathEffect::kLastEnum_Style);
113 return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
114 }
115
116 void flatten(SkWriteBuffer& buffer) const override {
117 buffer.writeScalar(fAdvance);
118 buffer.writePath(fPath);
119 buffer.writeScalar(fInitialOffset);
120 buffer.writeUInt(fStyle);
121 }
122
123 Factory getFactory() const override { return CreateProc; }
124 const char* getTypeName() const override { return "SkPath1DPathEffect"; }
125
126private:
127 SkPath fPath; // copied from constructor
128 SkScalar fAdvance; // copied from constructor
129 SkScalar fInitialOffset; // computed from phase
130 SkPath1DPathEffect::Style fStyle; // copied from constructor
131
132 using INHERITED = Sk1DPathEffect;
133};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134
reed@google.comf3edf9f2012-04-12 19:44:38 +0000135static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +0000136 SkPathMeasure& meas, SkScalar dist) {
137 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 SkPoint pos;
139 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkScalar sx = src[i].fX;
142 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000143
reed@google.comf3edf9f2012-04-12 19:44:38 +0000144 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
145 return false;
146 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 SkMatrix matrix;
149 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 pt.set(sx, sy);
152 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
153 matrix.preTranslate(-sx, 0);
154 matrix.postTranslate(pos.fX, pos.fY);
155 matrix.mapPoints(&dst[i], &pt, 1);
156 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000157 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158}
159
160/* TODO
161
162Need differentially more subdivisions when the follow-path is curvy. Not sure how to
163determine that, but we need it. I guess a cheap answer is let the caller tell us,
164but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
165*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000166static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
167 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 SkPath::Iter iter(src, false);
169 SkPoint srcP[4], dstP[3];
170 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000171
172 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 switch (verb) {
174 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000175 if (morphpoints(dstP, srcP, 1, meas, dist)) {
176 dst->moveTo(dstP[0]);
177 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 break;
179 case SkPath::kLine_Verb:
180 srcP[2] = srcP[1];
181 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
182 SkScalarAve(srcP[0].fY, srcP[2].fY));
John Stiles30212b72020-06-11 17:55:07 -0400183 [[fallthrough]];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000185 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
186 dst->quadTo(dstP[0], dstP[1]);
187 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 break;
Mike Reedb50e3852018-01-29 15:27:33 -0500189 case SkPath::kConic_Verb:
190 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
191 dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
192 }
193 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000195 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
196 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
197 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 break;
199 case SkPath::kClose_Verb:
200 dst->close();
201 break;
202 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000203 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 break;
205 }
206 }
207}
208
Mike Reedec87dc12021-05-20 15:16:34 -0400209SkScalar SkPath1DPathEffectImpl::next(SkPath* dst, SkScalar distance,
210 SkPathMeasure& meas) const {
Kevin Lubick493f89e2020-09-14 08:37:35 -0400211#if defined(SK_BUILD_FOR_FUZZER)
Cary Clark472ab812018-06-19 10:47:15 -0400212 if (dst->countPoints() > 100000) {
213 return fAdvance;
214 }
215#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 switch (fStyle) {
Mike Reedec87dc12021-05-20 15:16:34 -0400217 case SkPath1DPathEffect::kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 SkPoint pos;
halcanary96fcdcc2015-08-27 07:41:13 -0700219 if (meas.getPosTan(distance, &pos, nullptr)) {
reed@google.comf3edf9f2012-04-12 19:44:38 +0000220 dst->addPath(fPath, pos.fX, pos.fY);
221 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000222 } break;
Mike Reedec87dc12021-05-20 15:16:34 -0400223 case SkPath1DPathEffect::kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000225 if (meas.getMatrix(distance, &matrix)) {
226 dst->addPath(fPath, matrix);
227 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000228 } break;
Mike Reedec87dc12021-05-20 15:16:34 -0400229 case SkPath1DPathEffect::kMorph_Style:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 morphpath(dst, fPath, meas, distance);
231 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232 }
233 return fAdvance;
234}
robertphillips42dbfa82015-01-26 06:08:52 -0800235
reedca726ab2016-02-22 12:50:25 -0800236///////////////////////////////////////////////////////////////////////////////////////////////////
237
reeda4393342016-03-18 11:22:57 -0700238sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
239 Style style) {
Mike Reedfc015d22018-02-24 09:51:47 -0500240 if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) {
reedca726ab2016-02-22 12:50:25 -0800241 return nullptr;
242 }
Mike Reedec87dc12021-05-20 15:16:34 -0400243 return sk_sp<SkPathEffect>(new SkPath1DPathEffectImpl(path, advance, phase, style));
244}
245
246void SkPath1DPathEffect::RegisterFlattenables() {
247 SK_REGISTER_FLATTENABLE(SkPath1DPathEffectImpl);
reedca726ab2016-02-22 12:50:25 -0800248}