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