epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkPathMeasure.h" |
| 10 | #include "include/core/SkStrokeRec.h" |
| 11 | #include "include/effects/Sk1DPathEffect.h" |
Mike Reed | ec9d0e8 | 2021-05-21 17:42:14 -0400 | [diff] [blame] | 12 | #include "src/core/SkPathEffectBase.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkReadBuffer.h" |
| 14 | #include "src/core/SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
Mike Reed | 2223465 | 2018-02-27 09:48:39 -0500 | [diff] [blame] | 16 | // 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 Reed | ec9d0e8 | 2021-05-21 17:42:14 -0400 | [diff] [blame] | 20 | class Sk1DPathEffect : public SkPathEffectBase { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 21 | public: |
| 22 | protected: |
Tyler Denniston | f8b7c1a | 2021-07-13 13:22:19 -0400 | [diff] [blame] | 23 | bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, const SkRect*, |
| 24 | const SkMatrix&) const override { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 25 | 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.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 36 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 37 | } 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 | |
| 52 | private: |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 59 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 61 | class SkPath1DPathEffectImpl : public Sk1DPathEffect { |
| 62 | public: |
| 63 | SkPath1DPathEffectImpl(const SkPath& path, SkScalar advance, SkScalar phase, |
| 64 | SkPath1DPathEffect::Style style) : fPath(path) { |
| 65 | SkASSERT(advance > 0 && !path.isEmpty()); |
Mike Reed | fc015d2 | 2018-02-24 09:51:47 -0500 | [diff] [blame] | 66 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 67 | // Make the path thread-safe. |
| 68 | fPath.updateBoundsCache(); |
| 69 | (void)fPath.getGenerationID(); |
Ben Wagner | 20054dc | 2019-04-10 11:16:33 -0400 | [diff] [blame] | 70 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 71 | // 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; |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 83 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 84 | // now catch the edge case where phase == advance (within epsilon) |
| 85 | if (phase >= advance) { |
| 86 | phase = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 88 | SkASSERT(phase >= 0); |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 89 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 90 | fAdvance = advance; |
| 91 | fInitialOffset = phase; |
| 92 | fStyle = style; |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 93 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 95 | bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
Tyler Denniston | f8b7c1a | 2021-07-13 13:22:19 -0400 | [diff] [blame] | 96 | const SkRect* cullRect, const SkMatrix& ctm) const override { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 97 | rec->setFillStyle(); |
Tyler Denniston | f8b7c1a | 2021-07-13 13:22:19 -0400 | [diff] [blame] | 98 | return this->INHERITED::onFilterPath(dst, src, rec, cullRect, ctm); |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 99 | } |
| 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 | |
| 126 | private: |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 135 | static bool morphpoints(SkPoint dst[], const SkPoint src[], int count, |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 136 | SkPathMeasure& meas, SkScalar dist) { |
| 137 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | SkPoint pos; |
| 139 | SkVector tangent; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | SkScalar sx = src[i].fX; |
| 142 | SkScalar sy = src[i].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 143 | |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 144 | if (!meas.getPosTan(dist + sx, &pos, &tangent)) { |
| 145 | return false; |
| 146 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 147 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | SkMatrix matrix; |
| 149 | SkPoint pt; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | 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.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 157 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* TODO |
| 161 | |
| 162 | Need differentially more subdivisions when the follow-path is curvy. Not sure how to |
| 163 | determine that, but we need it. I guess a cheap answer is let the caller tell us, |
| 164 | but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. |
| 165 | */ |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 166 | static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, |
| 167 | SkScalar dist) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | SkPath::Iter iter(src, false); |
| 169 | SkPoint srcP[4], dstP[3]; |
| 170 | SkPath::Verb verb; |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 171 | |
| 172 | while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | switch (verb) { |
| 174 | case SkPath::kMove_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 175 | if (morphpoints(dstP, srcP, 1, meas, dist)) { |
| 176 | dst->moveTo(dstP[0]); |
| 177 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | 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 Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 183 | [[fallthrough]]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | case SkPath::kQuad_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 185 | if (morphpoints(dstP, &srcP[1], 2, meas, dist)) { |
| 186 | dst->quadTo(dstP[0], dstP[1]); |
| 187 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | break; |
Mike Reed | b50e385 | 2018-01-29 15:27:33 -0500 | [diff] [blame] | 189 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | case SkPath::kCubic_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 195 | if (morphpoints(dstP, &srcP[1], 3, meas, dist)) { |
| 196 | dst->cubicTo(dstP[0], dstP[1], dstP[2]); |
| 197 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | break; |
| 199 | case SkPath::kClose_Verb: |
| 200 | dst->close(); |
| 201 | break; |
| 202 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 203 | SkDEBUGFAIL("unknown verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 209 | SkScalar SkPath1DPathEffectImpl::next(SkPath* dst, SkScalar distance, |
| 210 | SkPathMeasure& meas) const { |
Kevin Lubick | 493f89e | 2020-09-14 08:37:35 -0400 | [diff] [blame] | 211 | #if defined(SK_BUILD_FOR_FUZZER) |
Cary Clark | 472ab81 | 2018-06-19 10:47:15 -0400 | [diff] [blame] | 212 | if (dst->countPoints() > 100000) { |
| 213 | return fAdvance; |
| 214 | } |
| 215 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | switch (fStyle) { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 217 | case SkPath1DPathEffect::kTranslate_Style: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | SkPoint pos; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 219 | if (meas.getPosTan(distance, &pos, nullptr)) { |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 220 | dst->addPath(fPath, pos.fX, pos.fY); |
| 221 | } |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 222 | } break; |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 223 | case SkPath1DPathEffect::kRotate_Style: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | SkMatrix matrix; |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 225 | if (meas.getMatrix(distance, &matrix)) { |
| 226 | dst->addPath(fPath, matrix); |
| 227 | } |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 228 | } break; |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 229 | case SkPath1DPathEffect::kMorph_Style: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | morphpath(dst, fPath, meas, distance); |
| 231 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 232 | } |
| 233 | return fAdvance; |
| 234 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 235 | |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 236 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 237 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 238 | sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase, |
| 239 | Style style) { |
Mike Reed | fc015d2 | 2018-02-24 09:51:47 -0500 | [diff] [blame] | 240 | if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) { |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 241 | return nullptr; |
| 242 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 243 | return sk_sp<SkPathEffect>(new SkPath1DPathEffectImpl(path, advance, phase, style)); |
| 244 | } |
| 245 | |
| 246 | void SkPath1DPathEffect::RegisterFlattenables() { |
| 247 | SK_REGISTER_FLATTENABLE(SkPath1DPathEffectImpl); |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 248 | } |