blob: 15276b684c779329fd9b8d4aee352bf0b344b5de [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkDashPathEffect.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPathMeasure.h"
14
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000015static inline int is_even(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 return (~x) << 31;
17}
18
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000019static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase,
reed@google.comd9ee3482012-08-06 14:58:35 +000020 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.org3334c3a2011-04-20 11:39:28 +000028 }
reed@google.comd9ee3482012-08-06 14:58:35 +000029 // 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.com8a1c16f2008-12-17 15:59:43 +000035}
36
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000037SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count,
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +000038 SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 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.org3334c3a2011-04-20 11:39:28 +000046 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 SkASSERT(intervals[i] >= 0);
48 fIntervals[i] = intervals[i];
49 len += intervals[i];
50 }
51 fIntervalLength = len;
52
epoger@google.com20bf4ca2012-04-27 13:34:52 +000053 // 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.org3334c3a2011-04-20 11:39:28 +000058 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 phase = -phase;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000060 if (phase > len) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 phase = SkScalarMod(phase, len);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000062 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 phase = len - phase;
epoger@google.com20bf4ca2012-04-27 13:34:52 +000064
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.com1df888b2012-04-24 22:47:21 +000068 SkASSERT(phase <= len);
69 if (phase == len) {
70 phase = 0;
71 }
epoger@google.com20bf4ca2012-04-27 13:34:52 +000072 } else if (phase >= len) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 phase = SkScalarMod(phase, len);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000074 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkASSERT(phase >= 0 && phase < len);
epoger@google.com20bf4ca2012-04-27 13:34:52 +000076
reed@google.comd9ee3482012-08-06 14:58:35 +000077 fInitialDashLength = FindFirstInterval(intervals, phase,
78 &fInitialDashIndex, count);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
80 SkASSERT(fInitialDashLength >= 0);
81 SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000082 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 fInitialDashLength = -1; // signal bad dash intervals
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000084 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000085}
86
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000087SkDashPathEffect::~SkDashPathEffect() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 sk_free(fIntervals);
89}
90
reed@google.com4bbdeac2013-01-24 21:03:11 +000091static 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.
104static 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();
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000121
reed@google.com4bbdeac2013-01-24 21:03:11 +0000122 // 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;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000129
reed@google.com4bbdeac2013-01-24 21:03:11 +0000130 if (maxX < bounds.fLeft || minX > bounds.fRight) {
131 return false;
132 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000133
reed@google.com4bbdeac2013-01-24 21:03:11 +0000134 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 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000150
robertphillips@google.com68f670f2013-01-28 20:55:42 +0000151 SkASSERT(maxX >= minX);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000152 if (dx < 0) {
153 SkTSwap(minX, maxX);
154 }
155 pts[0].fX = minX;
156 pts[1].fX = maxX;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000157
reed@google.com4bbdeac2013-01-24 21:03:11 +0000158 dstPath->moveTo(pts[0]);
159 dstPath->lineTo(pts[1]);
160 return true;
161}
162
reed@google.com3ec68f02012-05-29 20:48:50 +0000163class SpecialLineRec {
164public:
165 bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec,
reed@google.com3ec68f02012-05-29 20:48:50 +0000166 int intervalCount, SkScalar intervalLength) {
167 if (rec->isHairlineStyle() || !src.isLine(fPts)) {
168 return false;
169 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000170
reed@google.com3ec68f02012-05-29 20:48:50 +0000171 // 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.comfbfcd562012-08-23 18:09:54 +0000175
reed@google.com4bbdeac2013-01-24 21:03:11 +0000176 SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]);
177
reed@google.com3ec68f02012-05-29 20:48:50 +0000178 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.comfbfcd562012-08-23 18:09:54 +0000191
reed@google.com3ec68f02012-05-29 20:48:50 +0000192 SkScalar ptCount = SkScalarMulDiv(pathLength,
193 SkIntToScalar(intervalCount),
194 intervalLength);
195 int n = SkScalarCeilToInt(ptCount) << 2;
196 dst->incReserve(n);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000197
reed@google.com3ec68f02012-05-29 20:48:50 +0000198 // 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
224private:
225 SkPoint fPts[2];
226 SkVector fTangent;
227 SkVector fNormal;
228 SkScalar fPathLength;
229};
230
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000231bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000232 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 // we do nothing if the src wants to be filled, or if our dashlength is 0
reed@google.comfd4be262012-05-25 01:04:12 +0000234 if (rec->isFillStyle() || fInitialDashLength < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000236 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 const SkScalar* intervals = fIntervals;
fmalita@google.com6b18d242012-12-17 16:27:34 +0000239 SkScalar dashCount = 0;
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000240 int segCount = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241
reed@google.com4bbdeac2013-01-24 21:03:11 +0000242 SkPath cullPathStorage;
243 const SkPath* srcPtr = &src;
244 if (cull_path(src, *rec, cullRect, fIntervalLength, &cullPathStorage)) {
245 srcPtr = &cullPathStorage;
246 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000247
reed@google.com3ec68f02012-05-29 20:48:50 +0000248 SpecialLineRec lineRec;
reed@google.com70375522013-01-25 14:52:11 +0000249 bool specialLine = lineRec.init(*srcPtr, dst, rec, fCount >> 1, fIntervalLength);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000250
251 SkPathMeasure meas(*srcPtr, false);
reed@google.com3ec68f02012-05-29 20:48:50 +0000252
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 do {
254 bool skipFirstSegment = meas.isClosed();
255 bool addedSegment = false;
256 SkScalar length = meas.getLength();
257 int index = fInitialDashIndex;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000258
fmalita@google.com6b18d242012-12-17 16:27:34 +0000259 // 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
fmalita@google.combfa04012012-12-12 22:13:58 +0000274 // Using double precision to avoid looping indefinitely due to single precision rounding
275 // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest.
276 double distance = 0;
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000277 double dlen = fInitialDashLength;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000279 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 SkASSERT(dlen >= 0);
281 addedSegment = false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000282 if (is_even(index) && dlen > 0 && !skipFirstSegment) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 addedSegment = true;
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000284 ++segCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000285
reed@google.com3ec68f02012-05-29 20:48:50 +0000286 if (specialLine) {
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000287 lineRec.addSegment(SkDoubleToScalar(distance),
288 SkDoubleToScalar(distance + dlen),
robertphillips@google.com441a0052012-12-14 13:55:06 +0000289 dst);
reed@google.com3ec68f02012-05-29 20:48:50 +0000290 } else {
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000291 meas.getSegment(SkDoubleToScalar(distance),
292 SkDoubleToScalar(distance + dlen),
robertphillips@google.com441a0052012-12-14 13:55:06 +0000293 dst, true);
reed@google.com3ec68f02012-05-29 20:48:50 +0000294 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 }
296 distance += dlen;
297
298 // clear this so we only respect it the first time around
299 skipFirstSegment = false;
300
301 // wrap around our intervals array if necessary
302 index += 1;
303 SkASSERT(index <= fCount);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000304 if (index == fCount) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 index = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000306 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307
308 // fetch our next dlen
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000309 dlen = intervals[index];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 }
311
312 // extend if we ended on a segment and we need to join up with the (skipped) initial segment
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000313 if (meas.isClosed() && is_even(fInitialDashIndex) &&
314 fInitialDashLength > 0) {
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000315 meas.getSegment(0, fInitialDashLength, dst, !addedSegment);
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000316 ++segCount;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000317 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 } while (meas.nextContour());
reed@google.com3ec68f02012-05-29 20:48:50 +0000319
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000320 if (segCount > 1) {
321 dst->setConvexity(SkPath::kConcave_Convexity);
322 }
323
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324 return true;
325}
326
robertphillips@google.com629ab542012-11-28 17:18:11 +0000327// Currently asPoints is more restrictive then it needs to be. In the future
328// we need to:
329// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +0000330// allow paths to be returned
robertphillips@google.com629ab542012-11-28 17:18:11 +0000331bool SkDashPathEffect::asPoints(PointData* results,
332 const SkPath& src,
333 const SkStrokeRec& rec,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000334 const SkMatrix& matrix,
335 const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000336 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
337 if (fInitialDashLength < 0 || 0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000338 return false;
339 }
340
robertphillips@google.com6d875572012-12-17 18:56:29 +0000341 // TODO: this next test could be eased up. We could allow any number of
342 // intervals as long as all the ons match and all the offs match.
343 // Additionally, they do not necessarily need to be integers.
344 // We cannot allow arbitrary intervals since we want the returned points
345 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000346 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +0000347 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
348 !SkScalarIsInt(fIntervals[0]) ||
349 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000350 return false;
351 }
352
robertphillips@google.com629ab542012-11-28 17:18:11 +0000353 SkPoint pts[2];
354
robertphillips@google.com6d875572012-12-17 18:56:29 +0000355 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000356 return false;
357 }
358
robertphillips@google.com6d875572012-12-17 18:56:29 +0000359 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +0000360 if (SkPaint::kButt_Cap != rec.getCap()) {
361 return false;
362 }
363
robertphillips@google.com6d875572012-12-17 18:56:29 +0000364 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000365 if (!matrix.rectStaysRect()) {
366 return false;
367 }
368
robertphillips@google.com6d875572012-12-17 18:56:29 +0000369 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000370
robertphillips@google.com6d875572012-12-17 18:56:29 +0000371 SkVector tangent = pts[1] - pts[0];
372 if (tangent.isZero()) {
373 return false;
374 }
375
376 tangent.scale(SkScalarInvert(length));
377
378 // TODO: make this test for horizontal & vertical lines more robust
379 bool isXAxis = true;
380 if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) {
381 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
382 } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) {
383 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
384 isXAxis = false;
385 } else if (SkPaint::kRound_Cap != rec.getCap()) {
386 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000387 return false;
388 }
389
390 if (NULL != results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000391 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000392 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000393
robertphillips@google.com6d875572012-12-17 18:56:29 +0000394 if (SkPaint::kRound_Cap == rec.getCap()) {
395 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000396 }
397
robertphillips@google.com6d875572012-12-17 18:56:29 +0000398 results->fNumPoints = 0;
399 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000400 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
401 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000402 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000403 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000404 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000405 ++results->fNumPoints; // partial first dash
406 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000407 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000408 }
409 len2 -= fIntervals[1]; // also skip first space
410 if (len2 < 0) {
411 len2 = 0;
412 }
413 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000414 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000415 }
416 }
417 int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength));
418 results->fNumPoints += numMidPoints;
419 len2 -= numMidPoints * fIntervalLength;
420 bool partialLast = false;
421 if (len2 > 0) {
422 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000423 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000424 } else {
425 ++numMidPoints;
426 ++results->fNumPoints;
427 }
428 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000429
robertphillips@google.com935ad022012-12-05 19:07:21 +0000430 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000431
robertphillips@google.com6d875572012-12-17 18:56:29 +0000432 SkScalar distance = 0;
433 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000434
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000435 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
436 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000437
robertphillips@google.com6d875572012-12-17 18:56:29 +0000438 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000439 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000440 // partial first block
441 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000442 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength));
443 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000444 SkScalar halfWidth, halfHeight;
445 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000446 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000447 halfHeight = SkScalarHalf(rec.getWidth());
448 } else {
449 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000450 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000451 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000452 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000453 // This one will not be like the others
454 results->fFirst.addRect(x - halfWidth, y - halfHeight,
455 x + halfWidth, y + halfHeight);
456 } else {
457 SkASSERT(curPt < results->fNumPoints);
458 results->fPoints[curPt].set(x, y);
459 ++curPt;
460 }
461
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000462 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000463 }
464
465 distance += fIntervals[1]; // skip over the next blank block too
466 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000467 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000468 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000469 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000470
robertphillips@google.com6d875572012-12-17 18:56:29 +0000471 if (0 != numMidPoints) {
472 distance += SkScalarHalf(fIntervals[0]);
473
474 for (int i = 0; i < numMidPoints; ++i) {
475 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance);
476 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance);
477
478 SkASSERT(curPt < results->fNumPoints);
479 results->fPoints[curPt].set(x, y);
480 ++curPt;
481
482 distance += fIntervalLength;
483 }
484
485 distance -= SkScalarHalf(fIntervals[0]);
486 }
487
488 if (partialLast) {
489 // partial final block
490 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
491 SkScalar temp = length - distance;
492 SkASSERT(temp < fIntervals[0]);
493 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp));
494 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp));
495 SkScalar halfWidth, halfHeight;
496 if (isXAxis) {
497 halfWidth = SkScalarHalf(temp);
498 halfHeight = SkScalarHalf(rec.getWidth());
499 } else {
500 halfWidth = SkScalarHalf(rec.getWidth());
501 halfHeight = SkScalarHalf(temp);
502 }
503 results->fLast.addRect(x - halfWidth, y - halfHeight,
504 x + halfWidth, y + halfHeight);
505 }
506
507 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000508 }
509
510 return true;
511}
512
robertphillips@google.comc2eae472013-10-21 12:26:10 +0000513SkFlattenable::Factory SkDashPathEffect::getFactory() const {
commit-bot@chromium.org7fc22282014-03-07 14:43:00 +0000514 return CreateProc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515}
516
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000517void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +0000518 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000519 buffer.writeInt(fInitialDashIndex);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 buffer.writeScalar(fInitialDashLength);
521 buffer.writeScalar(fIntervalLength);
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000522 // Dummy write to stay compatible with old skps. Write will be removed in follow up patch.
523 buffer.writeBool(false);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000524 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525}
526
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000527SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 return SkNEW_ARGS(SkDashPathEffect, (buffer));
529}
530
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000531SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000532 fInitialDashIndex = buffer.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 fInitialDashLength = buffer.readScalar();
534 fIntervalLength = buffer.readScalar();
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000535 buffer.readBool(); // dummy read to stay compatible with old skps
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000536
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000537 fCount = buffer.getArrayCount();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000538 size_t allocSize = sizeof(SkScalar) * fCount;
539 if (buffer.validateAvailable(allocSize)) {
540 fIntervals = (SkScalar*)sk_malloc_throw(allocSize);
541 buffer.readScalarArray(fIntervals, fCount);
542 } else {
543 fIntervals = NULL;
544 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000545}