epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkDashPathEffect.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame^] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkPathMeasure.h" |
| 14 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 15 | static inline int is_even(int x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | return (~x) << 31; |
| 17 | } |
| 18 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 19 | static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase, |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 20 | int32_t* index, int count) { |
| 21 | for (int i = 0; i < count; ++i) { |
| 22 | if (phase > intervals[i]) { |
| 23 | phase -= intervals[i]; |
| 24 | } else { |
| 25 | *index = i; |
| 26 | return intervals[i] - phase; |
| 27 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 28 | } |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 29 | // If we get here, phase "appears" to be larger than our length. This |
| 30 | // shouldn't happen with perfect precision, but we can accumulate errors |
| 31 | // during the initial length computation (rounding can make our sum be too |
| 32 | // big or too small. In that event, we just have to eat the error here. |
| 33 | *index = 0; |
| 34 | return intervals[0]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | } |
| 36 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 37 | SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, |
| 38 | SkScalar phase, bool scaleToFit) |
| 39 | : fScaleToFit(scaleToFit) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | SkASSERT(intervals); |
| 41 | SkASSERT(count > 1 && SkAlign2(count) == count); |
| 42 | |
| 43 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count); |
| 44 | fCount = count; |
| 45 | |
| 46 | SkScalar len = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 47 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | SkASSERT(intervals[i] >= 0); |
| 49 | fIntervals[i] = intervals[i]; |
| 50 | len += intervals[i]; |
| 51 | } |
| 52 | fIntervalLength = len; |
| 53 | |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 54 | // watch out for values that might make us go out of bounds |
| 55 | if ((len > 0) && SkScalarIsFinite(phase) && SkScalarIsFinite(len)) { |
| 56 | |
| 57 | // Adjust phase to be between 0 and len, "flipping" phase if negative. |
| 58 | // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 59 | if (phase < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | phase = -phase; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 61 | if (phase > len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 63 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | phase = len - phase; |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 65 | |
| 66 | // Due to finite precision, it's possible that phase == len, |
| 67 | // even after the subtract (if len >>> phase), so fix that here. |
| 68 | // This fixes http://crbug.com/124652 . |
reed@google.com | 1df888b | 2012-04-24 22:47:21 +0000 | [diff] [blame] | 69 | SkASSERT(phase <= len); |
| 70 | if (phase == len) { |
| 71 | phase = 0; |
| 72 | } |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 73 | } else if (phase >= len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 75 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | SkASSERT(phase >= 0 && phase < len); |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 77 | |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 78 | fInitialDashLength = FindFirstInterval(intervals, phase, |
| 79 | &fInitialDashIndex, count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | |
| 81 | SkASSERT(fInitialDashLength >= 0); |
| 82 | SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 83 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | fInitialDashLength = -1; // signal bad dash intervals |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 85 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | } |
| 87 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 88 | SkDashPathEffect::~SkDashPathEffect() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | sk_free(fIntervals); |
| 90 | } |
| 91 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 92 | static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) { |
| 93 | SkScalar radius = SkScalarHalf(rec.getWidth()); |
| 94 | if (0 == radius) { |
| 95 | radius = SK_Scalar1; // hairlines |
| 96 | } |
| 97 | if (SkPaint::kMiter_Join == rec.getJoin()) { |
| 98 | radius = SkScalarMul(radius, rec.getMiter()); |
| 99 | } |
| 100 | rect->outset(radius, radius); |
| 101 | } |
| 102 | |
| 103 | // Only handles lines for now. If returns true, dstPath is the new (smaller) |
| 104 | // path. If returns false, then dstPath parameter is ignored. |
| 105 | static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec, |
| 106 | const SkRect* cullRect, SkScalar intervalLength, |
| 107 | SkPath* dstPath) { |
| 108 | if (NULL == cullRect) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | SkPoint pts[2]; |
| 113 | if (!srcPath.isLine(pts)) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | SkRect bounds = *cullRect; |
| 118 | outset_for_stroke(&bounds, rec); |
| 119 | |
| 120 | SkScalar dx = pts[1].x() - pts[0].x(); |
| 121 | SkScalar dy = pts[1].y() - pts[0].y(); |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 122 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 123 | // just do horizontal lines for now (lazy) |
| 124 | if (dy) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | SkScalar minX = pts[0].fX; |
| 129 | SkScalar maxX = pts[1].fX; |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 130 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 131 | if (maxX < bounds.fLeft || minX > bounds.fRight) { |
| 132 | return false; |
| 133 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 134 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 135 | if (dx < 0) { |
| 136 | SkTSwap(minX, maxX); |
| 137 | } |
| 138 | |
| 139 | // Now we actually perform the chop, removing the excess to the left and |
| 140 | // right of the bounds (keeping our new line "in phase" with the dash, |
| 141 | // hence the (mod intervalLength). |
| 142 | |
| 143 | if (minX < bounds.fLeft) { |
| 144 | minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, |
| 145 | intervalLength); |
| 146 | } |
| 147 | if (maxX > bounds.fRight) { |
| 148 | maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, |
| 149 | intervalLength); |
| 150 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 151 | |
robertphillips@google.com | 68f670f | 2013-01-28 20:55:42 +0000 | [diff] [blame] | 152 | SkASSERT(maxX >= minX); |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 153 | if (dx < 0) { |
| 154 | SkTSwap(minX, maxX); |
| 155 | } |
| 156 | pts[0].fX = minX; |
| 157 | pts[1].fX = maxX; |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 158 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 159 | dstPath->moveTo(pts[0]); |
| 160 | dstPath->lineTo(pts[1]); |
| 161 | return true; |
| 162 | } |
| 163 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 164 | class SpecialLineRec { |
| 165 | public: |
| 166 | bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec, |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 167 | int intervalCount, SkScalar intervalLength) { |
| 168 | if (rec->isHairlineStyle() || !src.isLine(fPts)) { |
| 169 | return false; |
| 170 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 171 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 172 | // can relax this in the future, if we handle square and round caps |
| 173 | if (SkPaint::kButt_Cap != rec->getCap()) { |
| 174 | return false; |
| 175 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 176 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 177 | SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]); |
| 178 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 179 | fTangent = fPts[1] - fPts[0]; |
| 180 | if (fTangent.isZero()) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | fPathLength = pathLength; |
| 185 | fTangent.scale(SkScalarInvert(pathLength)); |
| 186 | fTangent.rotateCCW(&fNormal); |
| 187 | fNormal.scale(SkScalarHalf(rec->getWidth())); |
| 188 | |
| 189 | // now estimate how many quads will be added to the path |
| 190 | // resulting segments = pathLen * intervalCount / intervalLen |
| 191 | // resulting points = 4 * segments |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 192 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 193 | SkScalar ptCount = SkScalarMulDiv(pathLength, |
| 194 | SkIntToScalar(intervalCount), |
| 195 | intervalLength); |
| 196 | int n = SkScalarCeilToInt(ptCount) << 2; |
| 197 | dst->incReserve(n); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 198 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 199 | // we will take care of the stroking |
| 200 | rec->setFillStyle(); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const { |
| 205 | SkASSERT(d0 < fPathLength); |
| 206 | // clamp the segment to our length |
| 207 | if (d1 > fPathLength) { |
| 208 | d1 = fPathLength; |
| 209 | } |
| 210 | |
| 211 | SkScalar x0 = fPts[0].fX + SkScalarMul(fTangent.fX, d0); |
| 212 | SkScalar x1 = fPts[0].fX + SkScalarMul(fTangent.fX, d1); |
| 213 | SkScalar y0 = fPts[0].fY + SkScalarMul(fTangent.fY, d0); |
| 214 | SkScalar y1 = fPts[0].fY + SkScalarMul(fTangent.fY, d1); |
| 215 | |
| 216 | SkPoint pts[4]; |
| 217 | pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo |
| 218 | pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo |
| 219 | pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo |
| 220 | pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo |
| 221 | |
| 222 | path->addPoly(pts, SK_ARRAY_COUNT(pts), false); |
| 223 | } |
| 224 | |
| 225 | private: |
| 226 | SkPoint fPts[2]; |
| 227 | SkVector fTangent; |
| 228 | SkVector fNormal; |
| 229 | SkScalar fPathLength; |
| 230 | }; |
| 231 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 232 | bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 233 | SkStrokeRec* rec, const SkRect* cullRect) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | // we do nothing if the src wants to be filled, or if our dashlength is 0 |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 235 | if (rec->isFillStyle() || fInitialDashLength < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 237 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | const SkScalar* intervals = fIntervals; |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 240 | SkScalar dashCount = 0; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 241 | int segCount = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 243 | SkPath cullPathStorage; |
| 244 | const SkPath* srcPtr = &src; |
| 245 | if (cull_path(src, *rec, cullRect, fIntervalLength, &cullPathStorage)) { |
| 246 | srcPtr = &cullPathStorage; |
| 247 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 248 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 249 | SpecialLineRec lineRec; |
reed@google.com | 7037552 | 2013-01-25 14:52:11 +0000 | [diff] [blame] | 250 | bool specialLine = lineRec.init(*srcPtr, dst, rec, fCount >> 1, fIntervalLength); |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 251 | |
| 252 | SkPathMeasure meas(*srcPtr, false); |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 253 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | do { |
| 255 | bool skipFirstSegment = meas.isClosed(); |
| 256 | bool addedSegment = false; |
| 257 | SkScalar length = meas.getLength(); |
| 258 | int index = fInitialDashIndex; |
| 259 | SkScalar scale = SK_Scalar1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 260 | |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 261 | // Since the path length / dash length ratio may be arbitrarily large, we can exert |
| 262 | // significant memory pressure while attempting to build the filtered path. To avoid this, |
| 263 | // we simply give up dashing beyond a certain threshold. |
| 264 | // |
| 265 | // The original bug report (http://crbug.com/165432) is based on a path yielding more than |
| 266 | // 90 million dash segments and crashing the memory allocator. A limit of 1 million |
| 267 | // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the |
| 268 | // maximum dash memory overhead at roughly 17MB per path. |
| 269 | static const SkScalar kMaxDashCount = 1000000; |
| 270 | dashCount += length * (fCount >> 1) / fIntervalLength; |
| 271 | if (dashCount > kMaxDashCount) { |
| 272 | dst->reset(); |
| 273 | return false; |
| 274 | } |
| 275 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 276 | if (fScaleToFit) { |
| 277 | if (fIntervalLength >= length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 278 | scale = SkScalarDiv(length, fIntervalLength); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 279 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | SkScalar div = SkScalarDiv(length, fIntervalLength); |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 281 | int n = SkScalarFloorToInt(div); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | scale = SkScalarDiv(length, n * fIntervalLength); |
| 283 | } |
| 284 | } |
| 285 | |
fmalita@google.com | bfa0401 | 2012-12-12 22:13:58 +0000 | [diff] [blame] | 286 | // Using double precision to avoid looping indefinitely due to single precision rounding |
| 287 | // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest. |
| 288 | double distance = 0; |
| 289 | double dlen = SkScalarMul(fInitialDashLength, scale); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 291 | while (distance < length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | SkASSERT(dlen >= 0); |
| 293 | addedSegment = false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 294 | if (is_even(index) && dlen > 0 && !skipFirstSegment) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | addedSegment = true; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 296 | ++segCount; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 297 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 298 | if (specialLine) { |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 299 | lineRec.addSegment(SkDoubleToScalar(distance), |
| 300 | SkDoubleToScalar(distance + dlen), |
robertphillips@google.com | 441a005 | 2012-12-14 13:55:06 +0000 | [diff] [blame] | 301 | dst); |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 302 | } else { |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 303 | meas.getSegment(SkDoubleToScalar(distance), |
| 304 | SkDoubleToScalar(distance + dlen), |
robertphillips@google.com | 441a005 | 2012-12-14 13:55:06 +0000 | [diff] [blame] | 305 | dst, true); |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 306 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 307 | } |
| 308 | distance += dlen; |
| 309 | |
| 310 | // clear this so we only respect it the first time around |
| 311 | skipFirstSegment = false; |
| 312 | |
| 313 | // wrap around our intervals array if necessary |
| 314 | index += 1; |
| 315 | SkASSERT(index <= fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 316 | if (index == fCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | index = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 318 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | |
| 320 | // fetch our next dlen |
| 321 | dlen = SkScalarMul(intervals[index], scale); |
| 322 | } |
| 323 | |
| 324 | // extend if we ended on a segment and we need to join up with the (skipped) initial segment |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 325 | if (meas.isClosed() && is_even(fInitialDashIndex) && |
| 326 | fInitialDashLength > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 327 | meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 328 | ++segCount; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 329 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | } while (meas.nextContour()); |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 331 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 332 | if (segCount > 1) { |
| 333 | dst->setConvexity(SkPath::kConcave_Convexity); |
| 334 | } |
| 335 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 339 | // Currently asPoints is more restrictive then it needs to be. In the future |
| 340 | // we need to: |
| 341 | // allow kRound_Cap capping (could allow rotations in the matrix with this) |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 342 | // allow paths to be returned |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 343 | bool SkDashPathEffect::asPoints(PointData* results, |
| 344 | const SkPath& src, |
| 345 | const SkStrokeRec& rec, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 346 | const SkMatrix& matrix, |
| 347 | const SkRect* cullRect) const { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 348 | // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out |
| 349 | if (fInitialDashLength < 0 || 0 >= rec.getWidth()) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 353 | // TODO: this next test could be eased up. We could allow any number of |
| 354 | // intervals as long as all the ons match and all the offs match. |
| 355 | // Additionally, they do not necessarily need to be integers. |
| 356 | // We cannot allow arbitrary intervals since we want the returned points |
| 357 | // to be uniformly sized. |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 358 | if (fCount != 2 || |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 359 | !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) || |
| 360 | !SkScalarIsInt(fIntervals[0]) || |
| 361 | !SkScalarIsInt(fIntervals[1])) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 365 | // TODO: this next test could be eased up. The rescaling should not impact |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 366 | // the equality of the ons & offs. However, we would need to remove the |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 367 | // integer intervals restriction first |
| 368 | if (fScaleToFit) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
| 372 | SkPoint pts[2]; |
| 373 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 374 | if (!src.isLine(pts)) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 378 | // TODO: this test could be eased up to allow circles |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 379 | if (SkPaint::kButt_Cap != rec.getCap()) { |
| 380 | return false; |
| 381 | } |
| 382 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 383 | // TODO: this test could be eased up for circles. Rotations could be allowed. |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 384 | if (!matrix.rectStaysRect()) { |
| 385 | return false; |
| 386 | } |
| 387 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 388 | SkScalar length = SkPoint::Distance(pts[1], pts[0]); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 389 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 390 | SkVector tangent = pts[1] - pts[0]; |
| 391 | if (tangent.isZero()) { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | tangent.scale(SkScalarInvert(length)); |
| 396 | |
| 397 | // TODO: make this test for horizontal & vertical lines more robust |
| 398 | bool isXAxis = true; |
| 399 | if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) { |
| 400 | results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth())); |
| 401 | } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) { |
| 402 | results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0])); |
| 403 | isXAxis = false; |
| 404 | } else if (SkPaint::kRound_Cap != rec.getCap()) { |
| 405 | // Angled lines don't have axis-aligned boxes. |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | |
| 409 | if (NULL != results) { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 410 | results->fFlags = 0; |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 411 | SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 412 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 413 | if (SkPaint::kRound_Cap == rec.getCap()) { |
| 414 | results->fFlags |= PointData::kCircles_PointFlag; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 415 | } |
| 416 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 417 | results->fNumPoints = 0; |
| 418 | SkScalar len2 = length; |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 419 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 420 | SkASSERT(len2 >= clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 421 | if (0 == fInitialDashIndex) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 422 | if (clampedInitialDashLength > 0) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 423 | if (clampedInitialDashLength >= fIntervals[0]) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 424 | ++results->fNumPoints; // partial first dash |
| 425 | } |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 426 | len2 -= clampedInitialDashLength; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 427 | } |
| 428 | len2 -= fIntervals[1]; // also skip first space |
| 429 | if (len2 < 0) { |
| 430 | len2 = 0; |
| 431 | } |
| 432 | } else { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 433 | len2 -= clampedInitialDashLength; // skip initial partial empty |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength)); |
| 437 | results->fNumPoints += numMidPoints; |
| 438 | len2 -= numMidPoints * fIntervalLength; |
| 439 | bool partialLast = false; |
| 440 | if (len2 > 0) { |
| 441 | if (len2 < fIntervals[0]) { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 442 | partialLast = true; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 443 | } else { |
| 444 | ++numMidPoints; |
| 445 | ++results->fNumPoints; |
| 446 | } |
| 447 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 448 | |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 449 | results->fPoints = new SkPoint[results->fNumPoints]; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 450 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 451 | SkScalar distance = 0; |
| 452 | int curPt = 0; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 453 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 454 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 455 | SkASSERT(clampedInitialDashLength <= length); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 456 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 457 | if (0 == fInitialDashIndex) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 458 | if (clampedInitialDashLength > 0) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 459 | // partial first block |
| 460 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 461 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength)); |
| 462 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength)); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 463 | SkScalar halfWidth, halfHeight; |
| 464 | if (isXAxis) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 465 | halfWidth = SkScalarHalf(clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 466 | halfHeight = SkScalarHalf(rec.getWidth()); |
| 467 | } else { |
| 468 | halfWidth = SkScalarHalf(rec.getWidth()); |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 469 | halfHeight = SkScalarHalf(clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 470 | } |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 471 | if (clampedInitialDashLength < fIntervals[0]) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 472 | // This one will not be like the others |
| 473 | results->fFirst.addRect(x - halfWidth, y - halfHeight, |
| 474 | x + halfWidth, y + halfHeight); |
| 475 | } else { |
| 476 | SkASSERT(curPt < results->fNumPoints); |
| 477 | results->fPoints[curPt].set(x, y); |
| 478 | ++curPt; |
| 479 | } |
| 480 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 481 | distance += clampedInitialDashLength; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | distance += fIntervals[1]; // skip over the next blank block too |
| 485 | } else { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 486 | distance += clampedInitialDashLength; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 487 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 488 | } |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 489 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 490 | if (0 != numMidPoints) { |
| 491 | distance += SkScalarHalf(fIntervals[0]); |
| 492 | |
| 493 | for (int i = 0; i < numMidPoints; ++i) { |
| 494 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance); |
| 495 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance); |
| 496 | |
| 497 | SkASSERT(curPt < results->fNumPoints); |
| 498 | results->fPoints[curPt].set(x, y); |
| 499 | ++curPt; |
| 500 | |
| 501 | distance += fIntervalLength; |
| 502 | } |
| 503 | |
| 504 | distance -= SkScalarHalf(fIntervals[0]); |
| 505 | } |
| 506 | |
| 507 | if (partialLast) { |
| 508 | // partial final block |
| 509 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
| 510 | SkScalar temp = length - distance; |
| 511 | SkASSERT(temp < fIntervals[0]); |
| 512 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp)); |
| 513 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp)); |
| 514 | SkScalar halfWidth, halfHeight; |
| 515 | if (isXAxis) { |
| 516 | halfWidth = SkScalarHalf(temp); |
| 517 | halfHeight = SkScalarHalf(rec.getWidth()); |
| 518 | } else { |
| 519 | halfWidth = SkScalarHalf(rec.getWidth()); |
| 520 | halfHeight = SkScalarHalf(temp); |
| 521 | } |
| 522 | results->fLast.addRect(x - halfWidth, y - halfHeight, |
| 523 | x + halfWidth, y + halfHeight); |
| 524 | } |
| 525 | |
| 526 | SkASSERT(curPt == results->fNumPoints); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return true; |
| 530 | } |
| 531 | |
robertphillips@google.com | c2eae47 | 2013-10-21 12:26:10 +0000 | [diff] [blame] | 532 | SkFlattenable::Factory SkDashPathEffect::getFactory() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | return fInitialDashLength < 0 ? NULL : CreateProc; |
| 534 | } |
| 535 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame^] | 536 | void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | SkASSERT(fInitialDashLength >= 0); |
| 538 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 539 | this->INHERITED::flatten(buffer); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 540 | buffer.writeInt(fInitialDashIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 541 | buffer.writeScalar(fInitialDashLength); |
| 542 | buffer.writeScalar(fIntervalLength); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 543 | buffer.writeBool(fScaleToFit); |
| 544 | buffer.writeScalarArray(fIntervals, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 545 | } |
| 546 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame^] | 547 | SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 548 | return SkNEW_ARGS(SkDashPathEffect, (buffer)); |
| 549 | } |
| 550 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame^] | 551 | SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 552 | fInitialDashIndex = buffer.readInt(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 553 | fInitialDashLength = buffer.readScalar(); |
| 554 | fIntervalLength = buffer.readScalar(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 555 | fScaleToFit = buffer.readBool(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 556 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 557 | fCount = buffer.getArrayCount(); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 558 | size_t allocSize = sizeof(SkScalar) * fCount; |
| 559 | if (buffer.validateAvailable(allocSize)) { |
| 560 | fIntervals = (SkScalar*)sk_malloc_throw(allocSize); |
| 561 | buffer.readScalarArray(fIntervals, fCount); |
| 562 | } else { |
| 563 | fIntervals = NULL; |
| 564 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 565 | } |