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