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" |
| 12 | #include "src/core/SkReadBuffer.h" |
| 13 | #include "src/core/SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
Mike Reed | 2223465 | 2018-02-27 09:48:39 -0500 | [diff] [blame] | 15 | // 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 Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 19 | class Sk1DPathEffect : public SkPathEffect { |
| 20 | public: |
| 21 | protected: |
| 22 | bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, const SkRect*) const override { |
| 23 | SkPathMeasure meas(src, false); |
| 24 | do { |
| 25 | int governor = MAX_REASONABLE_ITERATIONS; |
| 26 | SkScalar length = meas.getLength(); |
| 27 | SkScalar distance = this->begin(length); |
| 28 | while (distance < length && --governor >= 0) { |
| 29 | SkScalar delta = this->next(dst, distance, meas); |
| 30 | if (delta <= 0) { |
| 31 | break; |
| 32 | } |
| 33 | distance += delta; |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 34 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 35 | } while (meas.nextContour()); |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | /** Called at the start of each contour, returns the initial offset |
| 40 | into that contour. |
| 41 | */ |
| 42 | virtual SkScalar begin(SkScalar contourLength) const = 0; |
| 43 | /** Called with the current distance along the path, with the current matrix |
| 44 | for the point/tangent at the specified distance. |
| 45 | Return the distance to travel for the next call. If return <= 0, then that |
| 46 | contour is done. |
| 47 | */ |
| 48 | virtual SkScalar next(SkPath* dst, SkScalar dist, SkPathMeasure&) const = 0; |
| 49 | |
| 50 | private: |
| 51 | // For simplicity, assume fast bounds cannot be computed |
| 52 | bool computeFastBounds(SkRect*) const override { return false; } |
| 53 | |
| 54 | using INHERITED = SkPathEffect; |
| 55 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 57 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 59 | class SkPath1DPathEffectImpl : public Sk1DPathEffect { |
| 60 | public: |
| 61 | SkPath1DPathEffectImpl(const SkPath& path, SkScalar advance, SkScalar phase, |
| 62 | SkPath1DPathEffect::Style style) : fPath(path) { |
| 63 | SkASSERT(advance > 0 && !path.isEmpty()); |
Mike Reed | fc015d2 | 2018-02-24 09:51:47 -0500 | [diff] [blame] | 64 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 65 | // Make the path thread-safe. |
| 66 | fPath.updateBoundsCache(); |
| 67 | (void)fPath.getGenerationID(); |
Ben Wagner | 20054dc | 2019-04-10 11:16:33 -0400 | [diff] [blame] | 68 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 69 | // cleanup their phase parameter, inverting it so that it becomes an |
| 70 | // offset along the path (to match the interpretation in PostScript) |
| 71 | if (phase < 0) { |
| 72 | phase = -phase; |
| 73 | if (phase > advance) { |
| 74 | phase = SkScalarMod(phase, advance); |
| 75 | } |
| 76 | } else { |
| 77 | if (phase > advance) { |
| 78 | phase = SkScalarMod(phase, advance); |
| 79 | } |
| 80 | phase = advance - phase; |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 81 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 82 | // now catch the edge case where phase == advance (within epsilon) |
| 83 | if (phase >= advance) { |
| 84 | phase = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 86 | SkASSERT(phase >= 0); |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 87 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 88 | fAdvance = advance; |
| 89 | fInitialOffset = phase; |
| 90 | fStyle = style; |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 91 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 93 | bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
| 94 | const SkRect* cullRect) const override { |
| 95 | rec->setFillStyle(); |
| 96 | return this->INHERITED::onFilterPath(dst, src, rec, cullRect); |
| 97 | } |
| 98 | |
| 99 | SkScalar begin(SkScalar contourLength) const override { |
| 100 | return fInitialOffset; |
| 101 | } |
| 102 | |
| 103 | SkScalar next(SkPath*, SkScalar, SkPathMeasure&) const override; |
| 104 | |
| 105 | static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) { |
| 106 | SkScalar advance = buffer.readScalar(); |
| 107 | SkPath path; |
| 108 | buffer.readPath(&path); |
| 109 | SkScalar phase = buffer.readScalar(); |
| 110 | SkPath1DPathEffect::Style style = buffer.read32LE(SkPath1DPathEffect::kLastEnum_Style); |
| 111 | return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr; |
| 112 | } |
| 113 | |
| 114 | void flatten(SkWriteBuffer& buffer) const override { |
| 115 | buffer.writeScalar(fAdvance); |
| 116 | buffer.writePath(fPath); |
| 117 | buffer.writeScalar(fInitialOffset); |
| 118 | buffer.writeUInt(fStyle); |
| 119 | } |
| 120 | |
| 121 | Factory getFactory() const override { return CreateProc; } |
| 122 | const char* getTypeName() const override { return "SkPath1DPathEffect"; } |
| 123 | |
| 124 | private: |
| 125 | SkPath fPath; // copied from constructor |
| 126 | SkScalar fAdvance; // copied from constructor |
| 127 | SkScalar fInitialOffset; // computed from phase |
| 128 | SkPath1DPathEffect::Style fStyle; // copied from constructor |
| 129 | |
| 130 | using INHERITED = Sk1DPathEffect; |
| 131 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 133 | static bool morphpoints(SkPoint dst[], const SkPoint src[], int count, |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 134 | SkPathMeasure& meas, SkScalar dist) { |
| 135 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | SkPoint pos; |
| 137 | SkVector tangent; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | SkScalar sx = src[i].fX; |
| 140 | SkScalar sy = src[i].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 141 | |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 142 | if (!meas.getPosTan(dist + sx, &pos, &tangent)) { |
| 143 | return false; |
| 144 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | SkMatrix matrix; |
| 147 | SkPoint pt; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | pt.set(sx, sy); |
| 150 | matrix.setSinCos(tangent.fY, tangent.fX, 0, 0); |
| 151 | matrix.preTranslate(-sx, 0); |
| 152 | matrix.postTranslate(pos.fX, pos.fY); |
| 153 | matrix.mapPoints(&dst[i], &pt, 1); |
| 154 | } |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 155 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* TODO |
| 159 | |
| 160 | Need differentially more subdivisions when the follow-path is curvy. Not sure how to |
| 161 | determine that, but we need it. I guess a cheap answer is let the caller tell us, |
| 162 | but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. |
| 163 | */ |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 164 | static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, |
| 165 | SkScalar dist) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | SkPath::Iter iter(src, false); |
| 167 | SkPoint srcP[4], dstP[3]; |
| 168 | SkPath::Verb verb; |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 169 | |
| 170 | while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | switch (verb) { |
| 172 | case SkPath::kMove_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 173 | if (morphpoints(dstP, srcP, 1, meas, dist)) { |
| 174 | dst->moveTo(dstP[0]); |
| 175 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | break; |
| 177 | case SkPath::kLine_Verb: |
| 178 | srcP[2] = srcP[1]; |
| 179 | srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX), |
| 180 | SkScalarAve(srcP[0].fY, srcP[2].fY)); |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 181 | [[fallthrough]]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | case SkPath::kQuad_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 183 | if (morphpoints(dstP, &srcP[1], 2, meas, dist)) { |
| 184 | dst->quadTo(dstP[0], dstP[1]); |
| 185 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | break; |
Mike Reed | b50e385 | 2018-01-29 15:27:33 -0500 | [diff] [blame] | 187 | case SkPath::kConic_Verb: |
| 188 | if (morphpoints(dstP, &srcP[1], 2, meas, dist)) { |
| 189 | dst->conicTo(dstP[0], dstP[1], iter.conicWeight()); |
| 190 | } |
| 191 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | case SkPath::kCubic_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 193 | if (morphpoints(dstP, &srcP[1], 3, meas, dist)) { |
| 194 | dst->cubicTo(dstP[0], dstP[1], dstP[2]); |
| 195 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | break; |
| 197 | case SkPath::kClose_Verb: |
| 198 | dst->close(); |
| 199 | break; |
| 200 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 201 | SkDEBUGFAIL("unknown verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 207 | SkScalar SkPath1DPathEffectImpl::next(SkPath* dst, SkScalar distance, |
| 208 | SkPathMeasure& meas) const { |
Kevin Lubick | 493f89e | 2020-09-14 08:37:35 -0400 | [diff] [blame] | 209 | #if defined(SK_BUILD_FOR_FUZZER) |
Cary Clark | 472ab81 | 2018-06-19 10:47:15 -0400 | [diff] [blame] | 210 | if (dst->countPoints() > 100000) { |
| 211 | return fAdvance; |
| 212 | } |
| 213 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | switch (fStyle) { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 215 | case SkPath1DPathEffect::kTranslate_Style: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | SkPoint pos; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 217 | if (meas.getPosTan(distance, &pos, nullptr)) { |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 218 | dst->addPath(fPath, pos.fX, pos.fY); |
| 219 | } |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 220 | } break; |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 221 | case SkPath1DPathEffect::kRotate_Style: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | SkMatrix matrix; |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 223 | if (meas.getMatrix(distance, &matrix)) { |
| 224 | dst->addPath(fPath, matrix); |
| 225 | } |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 226 | } break; |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 227 | case SkPath1DPathEffect::kMorph_Style: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | morphpath(dst, fPath, meas, distance); |
| 229 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | } |
| 231 | return fAdvance; |
| 232 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 233 | |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 234 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 235 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 236 | sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase, |
| 237 | Style style) { |
Mike Reed | fc015d2 | 2018-02-24 09:51:47 -0500 | [diff] [blame] | 238 | if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) { |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 239 | return nullptr; |
| 240 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame^] | 241 | return sk_sp<SkPathEffect>(new SkPath1DPathEffectImpl(path, advance, phase, style)); |
| 242 | } |
| 243 | |
| 244 | void SkPath1DPathEffect::RegisterFlattenables() { |
| 245 | SK_REGISTER_FLATTENABLE(SkPath1DPathEffectImpl); |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 246 | } |