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 | |
| 8 | #include "SkDashPathPriv.h" |
| 9 | #include "SkPathMeasure.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 10 | #include "SkStrokeRec.h" |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 11 | |
| 12 | static inline int is_even(int x) { |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 13 | return !(x & 1); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | static SkScalar find_first_interval(const SkScalar intervals[], SkScalar phase, |
| 17 | int32_t* index, int count) { |
| 18 | for (int i = 0; i < count; ++i) { |
caryclark | d3cfd94 | 2016-03-17 05:33:28 -0700 | [diff] [blame] | 19 | SkScalar gap = intervals[i]; |
| 20 | if (phase > gap || (phase == gap && gap)) { |
| 21 | phase -= gap; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 22 | } else { |
| 23 | *index = i; |
caryclark | d3cfd94 | 2016-03-17 05:33:28 -0700 | [diff] [blame] | 24 | return gap - phase; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | // If we get here, phase "appears" to be larger than our length. This |
| 28 | // shouldn't happen with perfect precision, but we can accumulate errors |
| 29 | // during the initial length computation (rounding can make our sum be too |
| 30 | // big or too small. In that event, we just have to eat the error here. |
| 31 | *index = 0; |
| 32 | return intervals[0]; |
| 33 | } |
| 34 | |
| 35 | void SkDashPath::CalcDashParameters(SkScalar phase, const SkScalar intervals[], int32_t count, |
| 36 | SkScalar* initialDashLength, int32_t* initialDashIndex, |
| 37 | SkScalar* intervalLength, SkScalar* adjustedPhase) { |
| 38 | SkScalar len = 0; |
| 39 | for (int i = 0; i < count; i++) { |
| 40 | len += intervals[i]; |
| 41 | } |
| 42 | *intervalLength = len; |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 43 | // Adjust phase to be between 0 and len, "flipping" phase if negative. |
| 44 | // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 |
| 45 | if (adjustedPhase) { |
| 46 | if (phase < 0) { |
| 47 | phase = -phase; |
| 48 | if (phase > len) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 49 | phase = SkScalarMod(phase, len); |
| 50 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 51 | phase = len - phase; |
| 52 | |
| 53 | // Due to finite precision, it's possible that phase == len, |
| 54 | // even after the subtract (if len >>> phase), so fix that here. |
| 55 | // This fixes http://crbug.com/124652 . |
| 56 | SkASSERT(phase <= len); |
| 57 | if (phase == len) { |
| 58 | phase = 0; |
| 59 | } |
| 60 | } else if (phase >= len) { |
| 61 | phase = SkScalarMod(phase, len); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 62 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 63 | *adjustedPhase = phase; |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 64 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 65 | SkASSERT(phase >= 0 && phase < len); |
| 66 | |
| 67 | *initialDashLength = find_first_interval(intervals, phase, |
| 68 | initialDashIndex, count); |
| 69 | |
| 70 | SkASSERT(*initialDashLength >= 0); |
| 71 | SkASSERT(*initialDashIndex >= 0 && *initialDashIndex < count); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) { |
| 75 | SkScalar radius = SkScalarHalf(rec.getWidth()); |
| 76 | if (0 == radius) { |
| 77 | radius = SK_Scalar1; // hairlines |
| 78 | } |
| 79 | if (SkPaint::kMiter_Join == rec.getJoin()) { |
| 80 | radius = SkScalarMul(radius, rec.getMiter()); |
| 81 | } |
| 82 | rect->outset(radius, radius); |
| 83 | } |
| 84 | |
| 85 | // Only handles lines for now. If returns true, dstPath is the new (smaller) |
| 86 | // path. If returns false, then dstPath parameter is ignored. |
| 87 | static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec, |
| 88 | const SkRect* cullRect, SkScalar intervalLength, |
| 89 | SkPath* dstPath) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 90 | if (nullptr == cullRect) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
| 94 | SkPoint pts[2]; |
| 95 | if (!srcPath.isLine(pts)) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | SkRect bounds = *cullRect; |
| 100 | outset_for_stroke(&bounds, rec); |
| 101 | |
| 102 | SkScalar dx = pts[1].x() - pts[0].x(); |
| 103 | SkScalar dy = pts[1].y() - pts[0].y(); |
| 104 | |
| 105 | // just do horizontal lines for now (lazy) |
| 106 | if (dy) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | SkScalar minX = pts[0].fX; |
| 111 | SkScalar maxX = pts[1].fX; |
| 112 | |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 113 | if (dx < 0) { |
| 114 | SkTSwap(minX, maxX); |
| 115 | } |
| 116 | |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 117 | SkASSERT(minX <= maxX); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 118 | if (maxX < bounds.fLeft || minX > bounds.fRight) { |
| 119 | return false; |
| 120 | } |
| 121 | |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 122 | // Now we actually perform the chop, removing the excess to the left and |
| 123 | // right of the bounds (keeping our new line "in phase" with the dash, |
| 124 | // hence the (mod intervalLength). |
| 125 | |
| 126 | if (minX < bounds.fLeft) { |
| 127 | minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, |
| 128 | intervalLength); |
| 129 | } |
| 130 | if (maxX > bounds.fRight) { |
| 131 | maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, |
| 132 | intervalLength); |
| 133 | } |
| 134 | |
| 135 | SkASSERT(maxX >= minX); |
| 136 | if (dx < 0) { |
| 137 | SkTSwap(minX, maxX); |
| 138 | } |
| 139 | pts[0].fX = minX; |
| 140 | pts[1].fX = maxX; |
| 141 | |
| 142 | dstPath->moveTo(pts[0]); |
| 143 | dstPath->lineTo(pts[1]); |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | class SpecialLineRec { |
| 148 | public: |
| 149 | bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec, |
| 150 | int intervalCount, SkScalar intervalLength) { |
| 151 | if (rec->isHairlineStyle() || !src.isLine(fPts)) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | // can relax this in the future, if we handle square and round caps |
| 156 | if (SkPaint::kButt_Cap != rec->getCap()) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]); |
| 161 | |
| 162 | fTangent = fPts[1] - fPts[0]; |
| 163 | if (fTangent.isZero()) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | fPathLength = pathLength; |
| 168 | fTangent.scale(SkScalarInvert(pathLength)); |
| 169 | fTangent.rotateCCW(&fNormal); |
| 170 | fNormal.scale(SkScalarHalf(rec->getWidth())); |
| 171 | |
| 172 | // now estimate how many quads will be added to the path |
| 173 | // resulting segments = pathLen * intervalCount / intervalLen |
| 174 | // resulting points = 4 * segments |
| 175 | |
| 176 | SkScalar ptCount = SkScalarMulDiv(pathLength, |
| 177 | SkIntToScalar(intervalCount), |
| 178 | intervalLength); |
lsalzman | f41ae2f | 2016-07-21 09:37:59 -0700 | [diff] [blame] | 179 | ptCount = SkTMin(ptCount, SkDashPath::kMaxDashCount); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 180 | int n = SkScalarCeilToInt(ptCount) << 2; |
| 181 | dst->incReserve(n); |
| 182 | |
| 183 | // we will take care of the stroking |
| 184 | rec->setFillStyle(); |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const { |
lsalzman | 0adbd3e | 2016-08-08 13:40:27 -0700 | [diff] [blame] | 189 | SkASSERT(d0 <= fPathLength); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 190 | // clamp the segment to our length |
| 191 | if (d1 > fPathLength) { |
| 192 | d1 = fPathLength; |
| 193 | } |
| 194 | |
| 195 | SkScalar x0 = fPts[0].fX + SkScalarMul(fTangent.fX, d0); |
| 196 | SkScalar x1 = fPts[0].fX + SkScalarMul(fTangent.fX, d1); |
| 197 | SkScalar y0 = fPts[0].fY + SkScalarMul(fTangent.fY, d0); |
| 198 | SkScalar y1 = fPts[0].fY + SkScalarMul(fTangent.fY, d1); |
| 199 | |
| 200 | SkPoint pts[4]; |
| 201 | pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo |
| 202 | pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo |
| 203 | pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo |
| 204 | pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo |
| 205 | |
| 206 | path->addPoly(pts, SK_ARRAY_COUNT(pts), false); |
| 207 | } |
| 208 | |
| 209 | private: |
| 210 | SkPoint fPts[2]; |
| 211 | SkVector fTangent; |
| 212 | SkVector fNormal; |
| 213 | SkScalar fPathLength; |
| 214 | }; |
| 215 | |
| 216 | |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 217 | bool SkDashPath::InternalFilter(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 218 | const SkRect* cullRect, const SkScalar aIntervals[], |
| 219 | int32_t count, SkScalar initialDashLength, int32_t initialDashIndex, |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 220 | SkScalar intervalLength, |
| 221 | StrokeRecApplication strokeRecApplication) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 222 | |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 223 | // we do nothing if the src wants to be filled |
bsalomon | a058786 | 2016-06-09 06:03:38 -0700 | [diff] [blame] | 224 | SkStrokeRec::Style style = rec->getStyle(); |
| 225 | if (SkStrokeRec::kFill_Style == style || SkStrokeRec::kStrokeAndFill_Style == style) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | const SkScalar* intervals = aIntervals; |
| 230 | SkScalar dashCount = 0; |
| 231 | int segCount = 0; |
| 232 | |
| 233 | SkPath cullPathStorage; |
| 234 | const SkPath* srcPtr = &src; |
| 235 | if (cull_path(src, *rec, cullRect, intervalLength, &cullPathStorage)) { |
| 236 | srcPtr = &cullPathStorage; |
| 237 | } |
| 238 | |
| 239 | SpecialLineRec lineRec; |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 240 | bool specialLine = (StrokeRecApplication::kAllow == strokeRecApplication) && |
| 241 | lineRec.init(*srcPtr, dst, rec, count >> 1, intervalLength); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 242 | |
caryclark | 1a7eb26 | 2016-01-21 07:07:02 -0800 | [diff] [blame] | 243 | SkPathMeasure meas(*srcPtr, false, rec->getResScale()); |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 244 | |
| 245 | do { |
| 246 | bool skipFirstSegment = meas.isClosed(); |
| 247 | bool addedSegment = false; |
| 248 | SkScalar length = meas.getLength(); |
| 249 | int index = initialDashIndex; |
| 250 | |
| 251 | // Since the path length / dash length ratio may be arbitrarily large, we can exert |
| 252 | // significant memory pressure while attempting to build the filtered path. To avoid this, |
| 253 | // we simply give up dashing beyond a certain threshold. |
| 254 | // |
| 255 | // The original bug report (http://crbug.com/165432) is based on a path yielding more than |
| 256 | // 90 million dash segments and crashing the memory allocator. A limit of 1 million |
| 257 | // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the |
| 258 | // maximum dash memory overhead at roughly 17MB per path. |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 259 | dashCount += length * (count >> 1) / intervalLength; |
| 260 | if (dashCount > kMaxDashCount) { |
| 261 | dst->reset(); |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | // Using double precision to avoid looping indefinitely due to single precision rounding |
| 266 | // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest. |
| 267 | double distance = 0; |
| 268 | double dlen = initialDashLength; |
| 269 | |
| 270 | while (distance < length) { |
| 271 | SkASSERT(dlen >= 0); |
| 272 | addedSegment = false; |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 273 | if (is_even(index) && !skipFirstSegment) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 274 | addedSegment = true; |
| 275 | ++segCount; |
| 276 | |
| 277 | if (specialLine) { |
| 278 | lineRec.addSegment(SkDoubleToScalar(distance), |
| 279 | SkDoubleToScalar(distance + dlen), |
| 280 | dst); |
| 281 | } else { |
| 282 | meas.getSegment(SkDoubleToScalar(distance), |
| 283 | SkDoubleToScalar(distance + dlen), |
| 284 | dst, true); |
| 285 | } |
| 286 | } |
| 287 | distance += dlen; |
| 288 | |
| 289 | // clear this so we only respect it the first time around |
| 290 | skipFirstSegment = false; |
| 291 | |
| 292 | // wrap around our intervals array if necessary |
| 293 | index += 1; |
| 294 | SkASSERT(index <= count); |
| 295 | if (index == count) { |
| 296 | index = 0; |
| 297 | } |
| 298 | |
| 299 | // fetch our next dlen |
| 300 | dlen = intervals[index]; |
| 301 | } |
| 302 | |
| 303 | // extend if we ended on a segment and we need to join up with the (skipped) initial segment |
| 304 | if (meas.isClosed() && is_even(initialDashIndex) && |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 305 | initialDashLength >= 0) { |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 306 | meas.getSegment(0, initialDashLength, dst, !addedSegment); |
| 307 | ++segCount; |
| 308 | } |
| 309 | } while (meas.nextContour()); |
| 310 | |
| 311 | if (segCount > 1) { |
| 312 | dst->setConvexity(SkPath::kConcave_Convexity); |
| 313 | } |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | bool SkDashPath::FilterDashPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
| 319 | const SkRect* cullRect, const SkPathEffect::DashInfo& info) { |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 320 | if (!ValidDashPath(info.fPhase, info.fIntervals, info.fCount)) { |
| 321 | return false; |
| 322 | } |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 323 | SkScalar initialDashLength = 0; |
| 324 | int32_t initialDashIndex = 0; |
| 325 | SkScalar intervalLength = 0; |
| 326 | CalcDashParameters(info.fPhase, info.fIntervals, info.fCount, |
| 327 | &initialDashLength, &initialDashIndex, &intervalLength); |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 328 | return InternalFilter(dst, src, rec, cullRect, info.fIntervals, info.fCount, initialDashLength, |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 329 | initialDashIndex, intervalLength); |
| 330 | } |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 331 | |
| 332 | bool SkDashPath::ValidDashPath(SkScalar phase, const SkScalar intervals[], int32_t count) { |
| 333 | if (count < 2 || !SkIsAlign2(count)) { |
| 334 | return false; |
| 335 | } |
| 336 | SkScalar length = 0; |
| 337 | for (int i = 0; i < count; i++) { |
| 338 | if (intervals[i] < 0) { |
| 339 | return false; |
| 340 | } |
| 341 | length += intervals[i]; |
| 342 | } |
| 343 | // watch out for values that might make us go out of bounds |
| 344 | return length > 0 && SkScalarIsFinite(phase) && SkScalarIsFinite(length); |
| 345 | } |