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