egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkPathMeasure.h" |
| 9 | #include "include/core/SkStrokeRec.h" |
Mike Reed | 6052c0b | 2020-08-28 11:49:56 -0400 | [diff] [blame] | 10 | #include "src/core/SkPathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkPointPriv.h" |
| 12 | #include "src/utils/SkDashPathPriv.h" |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 13 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 16 | static inline int is_even(int x) { |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 17 | return !(x & 1); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | static SkScalar find_first_interval(const SkScalar intervals[], SkScalar phase, |
| 21 | int32_t* index, int count) { |
| 22 | for (int i = 0; i < count; ++i) { |
caryclark | d3cfd94 | 2016-03-17 05:33:28 -0700 | [diff] [blame] | 23 | SkScalar gap = intervals[i]; |
| 24 | if (phase > gap || (phase == gap && gap)) { |
| 25 | phase -= gap; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 26 | } else { |
| 27 | *index = i; |
caryclark | d3cfd94 | 2016-03-17 05:33:28 -0700 | [diff] [blame] | 28 | return gap - phase; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | // If we get here, phase "appears" to be larger than our length. This |
| 32 | // shouldn't happen with perfect precision, but we can accumulate errors |
| 33 | // during the initial length computation (rounding can make our sum be too |
| 34 | // big or too small. In that event, we just have to eat the error here. |
| 35 | *index = 0; |
| 36 | return intervals[0]; |
| 37 | } |
| 38 | |
| 39 | void SkDashPath::CalcDashParameters(SkScalar phase, const SkScalar intervals[], int32_t count, |
| 40 | SkScalar* initialDashLength, int32_t* initialDashIndex, |
| 41 | SkScalar* intervalLength, SkScalar* adjustedPhase) { |
| 42 | SkScalar len = 0; |
| 43 | for (int i = 0; i < count; i++) { |
| 44 | len += intervals[i]; |
| 45 | } |
| 46 | *intervalLength = len; |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 47 | // Adjust phase to be between 0 and len, "flipping" phase if negative. |
| 48 | // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 |
| 49 | if (adjustedPhase) { |
| 50 | if (phase < 0) { |
| 51 | phase = -phase; |
| 52 | if (phase > len) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 53 | phase = SkScalarMod(phase, len); |
| 54 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 55 | phase = len - phase; |
| 56 | |
| 57 | // Due to finite precision, it's possible that phase == len, |
| 58 | // even after the subtract (if len >>> phase), so fix that here. |
| 59 | // This fixes http://crbug.com/124652 . |
| 60 | SkASSERT(phase <= len); |
| 61 | if (phase == len) { |
| 62 | phase = 0; |
| 63 | } |
| 64 | } else if (phase >= len) { |
| 65 | phase = SkScalarMod(phase, len); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 66 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 67 | *adjustedPhase = phase; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 68 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 69 | SkASSERT(phase >= 0 && phase < len); |
| 70 | |
| 71 | *initialDashLength = find_first_interval(intervals, phase, |
| 72 | initialDashIndex, count); |
| 73 | |
| 74 | SkASSERT(*initialDashLength >= 0); |
| 75 | SkASSERT(*initialDashIndex >= 0 && *initialDashIndex < count); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) { |
| 79 | SkScalar radius = SkScalarHalf(rec.getWidth()); |
| 80 | if (0 == radius) { |
| 81 | radius = SK_Scalar1; // hairlines |
| 82 | } |
| 83 | if (SkPaint::kMiter_Join == rec.getJoin()) { |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 84 | radius *= rec.getMiter(); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 85 | } |
| 86 | rect->outset(radius, radius); |
| 87 | } |
| 88 | |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 89 | // If line is zero-length, bump out the end by a tiny amount |
| 90 | // to draw endcaps. The bump factor is sized so that |
| 91 | // SkPoint::Distance() computes a non-zero length. |
| 92 | // Offsets SK_ScalarNearlyZero or smaller create empty paths when Iter measures length. |
| 93 | // Large values are scaled by SK_ScalarNearlyZero so significant bits change. |
| 94 | static void adjust_zero_length_line(SkPoint pts[2]) { |
| 95 | SkASSERT(pts[0] == pts[1]); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 96 | pts[1].fX += std::max(1.001f, pts[1].fX) * SK_ScalarNearlyZero; |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static bool clip_line(SkPoint pts[2], const SkRect& bounds, SkScalar intervalLength, |
| 100 | SkScalar priorPhase) { |
| 101 | SkVector dxy = pts[1] - pts[0]; |
| 102 | |
| 103 | // only horizontal or vertical lines |
| 104 | if (dxy.fX && dxy.fY) { |
| 105 | return false; |
| 106 | } |
| 107 | int xyOffset = SkToBool(dxy.fY); // 0 to adjust horizontal, 1 to adjust vertical |
| 108 | |
| 109 | SkScalar minXY = (&pts[0].fX)[xyOffset]; |
| 110 | SkScalar maxXY = (&pts[1].fX)[xyOffset]; |
| 111 | bool swapped = maxXY < minXY; |
| 112 | if (swapped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 113 | using std::swap; |
| 114 | swap(minXY, maxXY); |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | SkASSERT(minXY <= maxXY); |
| 118 | SkScalar leftTop = (&bounds.fLeft)[xyOffset]; |
| 119 | SkScalar rightBottom = (&bounds.fRight)[xyOffset]; |
| 120 | if (maxXY < leftTop || minXY > rightBottom) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // Now we actually perform the chop, removing the excess to the left/top and |
| 125 | // right/bottom of the bounds (keeping our new line "in phase" with the dash, |
| 126 | // hence the (mod intervalLength). |
| 127 | |
| 128 | if (minXY < leftTop) { |
| 129 | minXY = leftTop - SkScalarMod(leftTop - minXY, intervalLength); |
| 130 | if (!swapped) { |
| 131 | minXY -= priorPhase; // for rectangles, adjust by prior phase |
| 132 | } |
| 133 | } |
| 134 | if (maxXY > rightBottom) { |
| 135 | maxXY = rightBottom + SkScalarMod(maxXY - rightBottom, intervalLength); |
| 136 | if (swapped) { |
| 137 | maxXY += priorPhase; // for rectangles, adjust by prior phase |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | SkASSERT(maxXY >= minXY); |
| 142 | if (swapped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 143 | using std::swap; |
| 144 | swap(minXY, maxXY); |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 145 | } |
| 146 | (&pts[0].fX)[xyOffset] = minXY; |
| 147 | (&pts[1].fX)[xyOffset] = maxXY; |
| 148 | |
| 149 | if (minXY == maxXY) { |
| 150 | adjust_zero_length_line(pts); |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
Mike Klein | e2556a4 | 2019-08-12 12:09:28 -0400 | [diff] [blame] | 155 | // Handles only lines and rects. |
| 156 | // If cull_path() returns true, dstPath is the new smaller path, |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 157 | // otherwise dstPath may have been changed but you should ignore it. |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 158 | static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec, |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 159 | const SkRect* cullRect, SkScalar intervalLength, SkPath* dstPath) { |
| 160 | if (!cullRect) { |
| 161 | SkPoint pts[2]; |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 162 | if (srcPath.isLine(pts) && pts[0] == pts[1]) { |
| 163 | adjust_zero_length_line(pts); |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 164 | dstPath->moveTo(pts[0]); |
| 165 | dstPath->lineTo(pts[1]); |
| 166 | return true; |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 167 | } |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 168 | return false; |
| 169 | } |
Mike Klein | e2556a4 | 2019-08-12 12:09:28 -0400 | [diff] [blame] | 170 | |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 171 | SkRect bounds; |
| 172 | bounds = *cullRect; |
| 173 | outset_for_stroke(&bounds, rec); |
Mike Klein | e2556a4 | 2019-08-12 12:09:28 -0400 | [diff] [blame] | 174 | |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 175 | { |
| 176 | SkPoint pts[2]; |
| 177 | if (srcPath.isLine(pts)) { |
| 178 | if (clip_line(pts, bounds, intervalLength, 0)) { |
| 179 | dstPath->moveTo(pts[0]); |
| 180 | dstPath->lineTo(pts[1]); |
| 181 | return true; |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 182 | } |
Greg Daniel | 9838b49 | 2017-12-21 14:55:00 +0000 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | } |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 186 | |
| 187 | if (srcPath.isRect(nullptr)) { |
| 188 | // We'll break the rect into four lines, culling each separately. |
| 189 | SkPath::Iter iter(srcPath, false); |
| 190 | |
| 191 | SkPoint pts[4]; // Rects are all moveTo and lineTo, so we'll only use pts[0] and pts[1]. |
| 192 | SkAssertResult(SkPath::kMove_Verb == iter.next(pts)); |
| 193 | |
| 194 | SkScalar accum = 0; // Sum of unculled edge lengths to keep the phase correct. |
| 195 | while (iter.next(pts) == SkPath::kLine_Verb) { |
| 196 | // Notice this vector v and accum work with the original unclipped length. |
| 197 | SkVector v = pts[1] - pts[0]; |
| 198 | |
Mike Klein | fb83927 | 2019-08-12 15:38:58 -0400 | [diff] [blame] | 199 | if (clip_line(pts, bounds, intervalLength, SkScalarMod(accum, intervalLength))) { |
| 200 | // pts[0] may have just been changed by clip_line(). |
| 201 | // If that's not where we ended the previous lineTo(), we need to moveTo() there. |
| 202 | SkPoint last; |
| 203 | if (!dstPath->getLastPt(&last) || last != pts[0]) { |
| 204 | dstPath->moveTo(pts[0]); |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 205 | } |
Mike Klein | fb83927 | 2019-08-12 15:38:58 -0400 | [diff] [blame] | 206 | dstPath->lineTo(pts[1]); |
Mike Klein | 0bbb3c1 | 2019-08-12 15:19:41 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // We either just traveled v.fX horizontally or v.fY vertically. |
| 210 | SkASSERT(v.fX == 0 || v.fY == 0); |
| 211 | accum += SkScalarAbs(v.fX + v.fY); |
| 212 | } |
| 213 | return !dstPath->isEmpty(); |
| 214 | } |
| 215 | |
| 216 | return false; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | class SpecialLineRec { |
| 220 | public: |
| 221 | bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec, |
| 222 | int intervalCount, SkScalar intervalLength) { |
| 223 | if (rec->isHairlineStyle() || !src.isLine(fPts)) { |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | // can relax this in the future, if we handle square and round caps |
| 228 | if (SkPaint::kButt_Cap != rec->getCap()) { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]); |
| 233 | |
| 234 | fTangent = fPts[1] - fPts[0]; |
| 235 | if (fTangent.isZero()) { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | fPathLength = pathLength; |
| 240 | fTangent.scale(SkScalarInvert(pathLength)); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 241 | SkPointPriv::RotateCCW(fTangent, &fNormal); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 242 | fNormal.scale(SkScalarHalf(rec->getWidth())); |
| 243 | |
| 244 | // now estimate how many quads will be added to the path |
| 245 | // resulting segments = pathLen * intervalCount / intervalLen |
| 246 | // resulting points = 4 * segments |
| 247 | |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 248 | SkScalar ptCount = pathLength * intervalCount / (float)intervalLength; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 249 | ptCount = std::min(ptCount, SkDashPath::kMaxDashCount); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 250 | int n = SkScalarCeilToInt(ptCount) << 2; |
| 251 | dst->incReserve(n); |
| 252 | |
| 253 | // we will take care of the stroking |
| 254 | rec->setFillStyle(); |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const { |
lsalzman | 0adbd3e | 2016-08-08 13:40:27 -0700 | [diff] [blame] | 259 | SkASSERT(d0 <= fPathLength); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 260 | // clamp the segment to our length |
| 261 | if (d1 > fPathLength) { |
| 262 | d1 = fPathLength; |
| 263 | } |
| 264 | |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 265 | SkScalar x0 = fPts[0].fX + fTangent.fX * d0; |
| 266 | SkScalar x1 = fPts[0].fX + fTangent.fX * d1; |
| 267 | SkScalar y0 = fPts[0].fY + fTangent.fY * d0; |
| 268 | SkScalar y1 = fPts[0].fY + fTangent.fY * d1; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 269 | |
| 270 | SkPoint pts[4]; |
| 271 | pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo |
| 272 | pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo |
| 273 | pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo |
| 274 | pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo |
| 275 | |
| 276 | path->addPoly(pts, SK_ARRAY_COUNT(pts), false); |
| 277 | } |
| 278 | |
| 279 | private: |
| 280 | SkPoint fPts[2]; |
| 281 | SkVector fTangent; |
| 282 | SkVector fNormal; |
| 283 | SkScalar fPathLength; |
| 284 | }; |
| 285 | |
| 286 | |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 287 | bool SkDashPath::InternalFilter(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 288 | const SkRect* cullRect, const SkScalar aIntervals[], |
| 289 | int32_t count, SkScalar initialDashLength, int32_t initialDashIndex, |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 290 | SkScalar intervalLength, |
| 291 | StrokeRecApplication strokeRecApplication) { |
Mike Reed | 4c65144 | 2018-08-23 12:47:59 -0400 | [diff] [blame] | 292 | // we must always have an even number of intervals |
| 293 | SkASSERT(is_even(count)); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 294 | |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 295 | // we do nothing if the src wants to be filled |
bsalomon | a058786 | 2016-06-09 06:03:38 -0700 | [diff] [blame] | 296 | SkStrokeRec::Style style = rec->getStyle(); |
| 297 | if (SkStrokeRec::kFill_Style == style || SkStrokeRec::kStrokeAndFill_Style == style) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 298 | return false; |
| 299 | } |
| 300 | |
| 301 | const SkScalar* intervals = aIntervals; |
| 302 | SkScalar dashCount = 0; |
| 303 | int segCount = 0; |
| 304 | |
| 305 | SkPath cullPathStorage; |
| 306 | const SkPath* srcPtr = &src; |
| 307 | if (cull_path(src, *rec, cullRect, intervalLength, &cullPathStorage)) { |
Cary Clark | 4fb229d | 2017-12-22 08:10:09 -0500 | [diff] [blame] | 308 | // if rect is closed, starts in a dash, and ends in a dash, add the initial join |
| 309 | // potentially a better fix is described here: bug.skia.org/7445 |
| 310 | if (src.isRect(nullptr) && src.isLastContourClosed() && is_even(initialDashIndex)) { |
| 311 | SkScalar pathLength = SkPathMeasure(src, false, rec->getResScale()).getLength(); |
| 312 | SkScalar endPhase = SkScalarMod(pathLength + initialDashLength, intervalLength); |
| 313 | int index = 0; |
| 314 | while (endPhase > intervals[index]) { |
| 315 | endPhase -= intervals[index++]; |
| 316 | SkASSERT(index <= count); |
Mike Reed | 4c65144 | 2018-08-23 12:47:59 -0400 | [diff] [blame] | 317 | if (index == count) { |
| 318 | // We have run out of intervals. endPhase "should" never get to this point, |
| 319 | // but it could if the subtracts underflowed. Hence we will pin it as if it |
| 320 | // perfectly ran through the intervals. |
| 321 | // See crbug.com/875494 (and skbug.com/8274) |
| 322 | endPhase = 0; |
| 323 | break; |
| 324 | } |
Cary Clark | 4fb229d | 2017-12-22 08:10:09 -0500 | [diff] [blame] | 325 | } |
| 326 | // if dash ends inside "on", or ends at beginning of "off" |
| 327 | if (is_even(index) == (endPhase > 0)) { |
| 328 | SkPoint midPoint = src.getPoint(0); |
| 329 | // get vector at end of rect |
| 330 | int last = src.countPoints() - 1; |
| 331 | while (midPoint == src.getPoint(last)) { |
| 332 | --last; |
| 333 | SkASSERT(last >= 0); |
| 334 | } |
| 335 | // get vector at start of rect |
| 336 | int next = 1; |
| 337 | while (midPoint == src.getPoint(next)) { |
| 338 | ++next; |
| 339 | SkASSERT(next < last); |
| 340 | } |
| 341 | SkVector v = midPoint - src.getPoint(last); |
| 342 | const SkScalar kTinyOffset = SK_ScalarNearlyZero; |
| 343 | // scale vector to make start of tiny right angle |
| 344 | v *= kTinyOffset; |
| 345 | cullPathStorage.moveTo(midPoint - v); |
| 346 | cullPathStorage.lineTo(midPoint); |
| 347 | v = midPoint - src.getPoint(next); |
| 348 | // scale vector to make end of tiny right angle |
| 349 | v *= kTinyOffset; |
| 350 | cullPathStorage.lineTo(midPoint - v); |
| 351 | } |
| 352 | } |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 353 | srcPtr = &cullPathStorage; |
| 354 | } |
| 355 | |
| 356 | SpecialLineRec lineRec; |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 357 | bool specialLine = (StrokeRecApplication::kAllow == strokeRecApplication) && |
| 358 | lineRec.init(*srcPtr, dst, rec, count >> 1, intervalLength); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 359 | |
caryclark | 1a7eb26 | 2016-01-21 07:07:02 -0800 | [diff] [blame] | 360 | SkPathMeasure meas(*srcPtr, false, rec->getResScale()); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 361 | |
| 362 | do { |
| 363 | bool skipFirstSegment = meas.isClosed(); |
| 364 | bool addedSegment = false; |
| 365 | SkScalar length = meas.getLength(); |
| 366 | int index = initialDashIndex; |
| 367 | |
| 368 | // Since the path length / dash length ratio may be arbitrarily large, we can exert |
| 369 | // significant memory pressure while attempting to build the filtered path. To avoid this, |
| 370 | // we simply give up dashing beyond a certain threshold. |
| 371 | // |
| 372 | // The original bug report (http://crbug.com/165432) is based on a path yielding more than |
| 373 | // 90 million dash segments and crashing the memory allocator. A limit of 1 million |
| 374 | // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the |
| 375 | // maximum dash memory overhead at roughly 17MB per path. |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 376 | dashCount += length * (count >> 1) / intervalLength; |
| 377 | if (dashCount > kMaxDashCount) { |
| 378 | dst->reset(); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | // Using double precision to avoid looping indefinitely due to single precision rounding |
| 383 | // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest. |
| 384 | double distance = 0; |
| 385 | double dlen = initialDashLength; |
| 386 | |
| 387 | while (distance < length) { |
| 388 | SkASSERT(dlen >= 0); |
| 389 | addedSegment = false; |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 390 | if (is_even(index) && !skipFirstSegment) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 391 | addedSegment = true; |
| 392 | ++segCount; |
| 393 | |
| 394 | if (specialLine) { |
| 395 | lineRec.addSegment(SkDoubleToScalar(distance), |
| 396 | SkDoubleToScalar(distance + dlen), |
| 397 | dst); |
| 398 | } else { |
| 399 | meas.getSegment(SkDoubleToScalar(distance), |
| 400 | SkDoubleToScalar(distance + dlen), |
| 401 | dst, true); |
| 402 | } |
| 403 | } |
| 404 | distance += dlen; |
| 405 | |
| 406 | // clear this so we only respect it the first time around |
| 407 | skipFirstSegment = false; |
| 408 | |
| 409 | // wrap around our intervals array if necessary |
| 410 | index += 1; |
| 411 | SkASSERT(index <= count); |
| 412 | if (index == count) { |
| 413 | index = 0; |
| 414 | } |
| 415 | |
| 416 | // fetch our next dlen |
| 417 | dlen = intervals[index]; |
| 418 | } |
| 419 | |
| 420 | // extend if we ended on a segment and we need to join up with the (skipped) initial segment |
| 421 | if (meas.isClosed() && is_even(initialDashIndex) && |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 422 | initialDashLength >= 0) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 423 | meas.getSegment(0, initialDashLength, dst, !addedSegment); |
| 424 | ++segCount; |
| 425 | } |
| 426 | } while (meas.nextContour()); |
| 427 | |
Mike Reed | 6052c0b | 2020-08-28 11:49:56 -0400 | [diff] [blame] | 428 | // TODO: do we still need this? |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 429 | if (segCount > 1) { |
Mike Reed | 3872c98 | 2020-08-29 17:46:51 -0400 | [diff] [blame^] | 430 | SkPathPriv::SetConvexity(*dst, SkPathConvexity::kConcave); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | bool SkDashPath::FilterDashPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
| 437 | const SkRect* cullRect, const SkPathEffect::DashInfo& info) { |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 438 | if (!ValidDashPath(info.fPhase, info.fIntervals, info.fCount)) { |
| 439 | return false; |
| 440 | } |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 441 | SkScalar initialDashLength = 0; |
| 442 | int32_t initialDashIndex = 0; |
| 443 | SkScalar intervalLength = 0; |
| 444 | CalcDashParameters(info.fPhase, info.fIntervals, info.fCount, |
| 445 | &initialDashLength, &initialDashIndex, &intervalLength); |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 446 | return InternalFilter(dst, src, rec, cullRect, info.fIntervals, info.fCount, initialDashLength, |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 447 | initialDashIndex, intervalLength); |
| 448 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 449 | |
| 450 | bool SkDashPath::ValidDashPath(SkScalar phase, const SkScalar intervals[], int32_t count) { |
| 451 | if (count < 2 || !SkIsAlign2(count)) { |
| 452 | return false; |
| 453 | } |
| 454 | SkScalar length = 0; |
| 455 | for (int i = 0; i < count; i++) { |
| 456 | if (intervals[i] < 0) { |
| 457 | return false; |
| 458 | } |
| 459 | length += intervals[i]; |
| 460 | } |
| 461 | // watch out for values that might make us go out of bounds |
| 462 | return length > 0 && SkScalarIsFinite(phase) && SkScalarIsFinite(length); |
| 463 | } |