blob: 4fd4185e06285efb5845423a288f0c449d084d2b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkDashPathEffect.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00009#include "SkReadBuffer.h"
10#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkPathMeasure.h"
12
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000013static inline int is_even(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014 return (~x) << 31;
15}
16
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000017static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase,
reed@google.comd9ee3482012-08-06 14:58:35 +000018 int32_t* index, int count) {
19 for (int i = 0; i < count; ++i) {
20 if (phase > intervals[i]) {
21 phase -= intervals[i];
22 } else {
23 *index = i;
24 return intervals[i] - phase;
25 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000026 }
reed@google.comd9ee3482012-08-06 14:58:35 +000027 // If we get here, phase "appears" to be larger than our length. This
28 // shouldn't happen with perfect precision, but we can accumulate errors
29 // during the initial length computation (rounding can make our sum be too
30 // big or too small. In that event, we just have to eat the error here.
31 *index = 0;
32 return intervals[0];
reed@android.com8a1c16f2008-12-17 15:59:43 +000033}
34
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000035void SkDashPathEffect::setInternalMembers(SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkScalar len = 0;
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000037 for (int i = 0; i < fCount; i++) {
38 len += fIntervals[i];
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 }
40 fIntervalLength = len;
41
epoger@google.com20bf4ca2012-04-27 13:34:52 +000042 // watch out for values that might make us go out of bounds
43 if ((len > 0) && SkScalarIsFinite(phase) && SkScalarIsFinite(len)) {
44
45 // Adjust phase to be between 0 and len, "flipping" phase if negative.
46 // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000047 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 phase = -phase;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000049 if (phase > len) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 phase = SkScalarMod(phase, len);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000051 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 phase = len - phase;
epoger@google.com20bf4ca2012-04-27 13:34:52 +000053
54 // Due to finite precision, it's possible that phase == len,
55 // even after the subtract (if len >>> phase), so fix that here.
56 // This fixes http://crbug.com/124652 .
reed@google.com1df888b2012-04-24 22:47:21 +000057 SkASSERT(phase <= len);
58 if (phase == len) {
59 phase = 0;
60 }
epoger@google.com20bf4ca2012-04-27 13:34:52 +000061 } else if (phase >= len) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 phase = SkScalarMod(phase, len);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000063 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 SkASSERT(phase >= 0 && phase < len);
epoger@google.com20bf4ca2012-04-27 13:34:52 +000065
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000066 fPhase = phase;
67
68 fInitialDashLength = FindFirstInterval(fIntervals, fPhase,
69 &fInitialDashIndex, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
71 SkASSERT(fInitialDashLength >= 0);
72 SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000073 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 fInitialDashLength = -1; // signal bad dash intervals
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000075 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000076}
77
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000078SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count,
79 SkScalar phase) {
80 SkASSERT(intervals);
81 SkASSERT(count > 1 && SkAlign2(count) == count);
82
83 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
84 fCount = count;
85 for (int i = 0; i < count; i++) {
86 SkASSERT(intervals[i] >= 0);
87 fIntervals[i] = intervals[i];
88 }
89
90 this->setInternalMembers(phase);
91}
92
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000093SkDashPathEffect::~SkDashPathEffect() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 sk_free(fIntervals);
95}
96
reed@google.com4bbdeac2013-01-24 21:03:11 +000097static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
98 SkScalar radius = SkScalarHalf(rec.getWidth());
99 if (0 == radius) {
100 radius = SK_Scalar1; // hairlines
101 }
102 if (SkPaint::kMiter_Join == rec.getJoin()) {
103 radius = SkScalarMul(radius, rec.getMiter());
104 }
105 rect->outset(radius, radius);
106}
107
108// Only handles lines for now. If returns true, dstPath is the new (smaller)
109// path. If returns false, then dstPath parameter is ignored.
110static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec,
111 const SkRect* cullRect, SkScalar intervalLength,
112 SkPath* dstPath) {
113 if (NULL == cullRect) {
114 return false;
115 }
116
117 SkPoint pts[2];
118 if (!srcPath.isLine(pts)) {
119 return false;
120 }
121
122 SkRect bounds = *cullRect;
123 outset_for_stroke(&bounds, rec);
124
125 SkScalar dx = pts[1].x() - pts[0].x();
126 SkScalar dy = pts[1].y() - pts[0].y();
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000127
reed@google.com4bbdeac2013-01-24 21:03:11 +0000128 // just do horizontal lines for now (lazy)
129 if (dy) {
130 return false;
131 }
132
133 SkScalar minX = pts[0].fX;
134 SkScalar maxX = pts[1].fX;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000135
reed@google.com4bbdeac2013-01-24 21:03:11 +0000136 if (maxX < bounds.fLeft || minX > bounds.fRight) {
137 return false;
138 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000139
reed@google.com4bbdeac2013-01-24 21:03:11 +0000140 if (dx < 0) {
141 SkTSwap(minX, maxX);
142 }
143
144 // Now we actually perform the chop, removing the excess to the left and
145 // right of the bounds (keeping our new line "in phase" with the dash,
146 // hence the (mod intervalLength).
147
148 if (minX < bounds.fLeft) {
149 minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX,
150 intervalLength);
151 }
152 if (maxX > bounds.fRight) {
153 maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight,
154 intervalLength);
155 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000156
robertphillips@google.com68f670f2013-01-28 20:55:42 +0000157 SkASSERT(maxX >= minX);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000158 if (dx < 0) {
159 SkTSwap(minX, maxX);
160 }
161 pts[0].fX = minX;
162 pts[1].fX = maxX;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000163
reed@google.com4bbdeac2013-01-24 21:03:11 +0000164 dstPath->moveTo(pts[0]);
165 dstPath->lineTo(pts[1]);
166 return true;
167}
168
reed@google.com3ec68f02012-05-29 20:48:50 +0000169class SpecialLineRec {
170public:
171 bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec,
reed@google.com3ec68f02012-05-29 20:48:50 +0000172 int intervalCount, SkScalar intervalLength) {
173 if (rec->isHairlineStyle() || !src.isLine(fPts)) {
174 return false;
175 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000176
reed@google.com3ec68f02012-05-29 20:48:50 +0000177 // can relax this in the future, if we handle square and round caps
178 if (SkPaint::kButt_Cap != rec->getCap()) {
179 return false;
180 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000181
reed@google.com4bbdeac2013-01-24 21:03:11 +0000182 SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]);
183
reed@google.com3ec68f02012-05-29 20:48:50 +0000184 fTangent = fPts[1] - fPts[0];
185 if (fTangent.isZero()) {
186 return false;
187 }
188
189 fPathLength = pathLength;
190 fTangent.scale(SkScalarInvert(pathLength));
191 fTangent.rotateCCW(&fNormal);
192 fNormal.scale(SkScalarHalf(rec->getWidth()));
193
194 // now estimate how many quads will be added to the path
195 // resulting segments = pathLen * intervalCount / intervalLen
196 // resulting points = 4 * segments
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000197
reed@google.com3ec68f02012-05-29 20:48:50 +0000198 SkScalar ptCount = SkScalarMulDiv(pathLength,
199 SkIntToScalar(intervalCount),
200 intervalLength);
201 int n = SkScalarCeilToInt(ptCount) << 2;
202 dst->incReserve(n);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000203
reed@google.com3ec68f02012-05-29 20:48:50 +0000204 // we will take care of the stroking
205 rec->setFillStyle();
206 return true;
207 }
208
209 void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const {
210 SkASSERT(d0 < fPathLength);
211 // clamp the segment to our length
212 if (d1 > fPathLength) {
213 d1 = fPathLength;
214 }
215
216 SkScalar x0 = fPts[0].fX + SkScalarMul(fTangent.fX, d0);
217 SkScalar x1 = fPts[0].fX + SkScalarMul(fTangent.fX, d1);
218 SkScalar y0 = fPts[0].fY + SkScalarMul(fTangent.fY, d0);
219 SkScalar y1 = fPts[0].fY + SkScalarMul(fTangent.fY, d1);
220
221 SkPoint pts[4];
222 pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo
223 pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo
224 pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo
225 pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo
226
227 path->addPoly(pts, SK_ARRAY_COUNT(pts), false);
228 }
229
230private:
231 SkPoint fPts[2];
232 SkVector fTangent;
233 SkVector fNormal;
234 SkScalar fPathLength;
235};
236
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000237bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000238 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 // we do nothing if the src wants to be filled, or if our dashlength is 0
reed@google.comfd4be262012-05-25 01:04:12 +0000240 if (rec->isFillStyle() || fInitialDashLength < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000242 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 const SkScalar* intervals = fIntervals;
fmalita@google.com6b18d242012-12-17 16:27:34 +0000245 SkScalar dashCount = 0;
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000246 int segCount = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247
reed@google.com4bbdeac2013-01-24 21:03:11 +0000248 SkPath cullPathStorage;
249 const SkPath* srcPtr = &src;
250 if (cull_path(src, *rec, cullRect, fIntervalLength, &cullPathStorage)) {
251 srcPtr = &cullPathStorage;
252 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000253
reed@google.com3ec68f02012-05-29 20:48:50 +0000254 SpecialLineRec lineRec;
reed@google.com70375522013-01-25 14:52:11 +0000255 bool specialLine = lineRec.init(*srcPtr, dst, rec, fCount >> 1, fIntervalLength);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000256
257 SkPathMeasure meas(*srcPtr, false);
reed@google.com3ec68f02012-05-29 20:48:50 +0000258
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 do {
260 bool skipFirstSegment = meas.isClosed();
261 bool addedSegment = false;
262 SkScalar length = meas.getLength();
263 int index = fInitialDashIndex;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000264
fmalita@google.com6b18d242012-12-17 16:27:34 +0000265 // Since the path length / dash length ratio may be arbitrarily large, we can exert
266 // significant memory pressure while attempting to build the filtered path. To avoid this,
267 // we simply give up dashing beyond a certain threshold.
268 //
269 // The original bug report (http://crbug.com/165432) is based on a path yielding more than
270 // 90 million dash segments and crashing the memory allocator. A limit of 1 million
271 // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the
272 // maximum dash memory overhead at roughly 17MB per path.
273 static const SkScalar kMaxDashCount = 1000000;
274 dashCount += length * (fCount >> 1) / fIntervalLength;
275 if (dashCount > kMaxDashCount) {
276 dst->reset();
277 return false;
278 }
279
fmalita@google.combfa04012012-12-12 22:13:58 +0000280 // Using double precision to avoid looping indefinitely due to single precision rounding
281 // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest.
282 double distance = 0;
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000283 double dlen = fInitialDashLength;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000285 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 SkASSERT(dlen >= 0);
287 addedSegment = false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000288 if (is_even(index) && dlen > 0 && !skipFirstSegment) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 addedSegment = true;
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000290 ++segCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000291
reed@google.com3ec68f02012-05-29 20:48:50 +0000292 if (specialLine) {
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000293 lineRec.addSegment(SkDoubleToScalar(distance),
294 SkDoubleToScalar(distance + dlen),
robertphillips@google.com441a0052012-12-14 13:55:06 +0000295 dst);
reed@google.com3ec68f02012-05-29 20:48:50 +0000296 } else {
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000297 meas.getSegment(SkDoubleToScalar(distance),
298 SkDoubleToScalar(distance + dlen),
robertphillips@google.com441a0052012-12-14 13:55:06 +0000299 dst, true);
reed@google.com3ec68f02012-05-29 20:48:50 +0000300 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 }
302 distance += dlen;
303
304 // clear this so we only respect it the first time around
305 skipFirstSegment = false;
306
307 // wrap around our intervals array if necessary
308 index += 1;
309 SkASSERT(index <= fCount);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000310 if (index == fCount) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 index = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000312 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313
314 // fetch our next dlen
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000315 dlen = intervals[index];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 }
317
318 // 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 +0000319 if (meas.isClosed() && is_even(fInitialDashIndex) &&
320 fInitialDashLength > 0) {
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000321 meas.getSegment(0, fInitialDashLength, dst, !addedSegment);
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000322 ++segCount;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000323 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324 } while (meas.nextContour());
reed@google.com3ec68f02012-05-29 20:48:50 +0000325
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000326 if (segCount > 1) {
327 dst->setConvexity(SkPath::kConcave_Convexity);
328 }
329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 return true;
331}
332
robertphillips@google.com629ab542012-11-28 17:18:11 +0000333// Currently asPoints is more restrictive then it needs to be. In the future
334// we need to:
335// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +0000336// allow paths to be returned
robertphillips@google.com629ab542012-11-28 17:18:11 +0000337bool SkDashPathEffect::asPoints(PointData* results,
338 const SkPath& src,
339 const SkStrokeRec& rec,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000340 const SkMatrix& matrix,
341 const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000342 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
343 if (fInitialDashLength < 0 || 0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000344 return false;
345 }
346
robertphillips@google.com6d875572012-12-17 18:56:29 +0000347 // TODO: this next test could be eased up. We could allow any number of
348 // intervals as long as all the ons match and all the offs match.
349 // Additionally, they do not necessarily need to be integers.
350 // We cannot allow arbitrary intervals since we want the returned points
351 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000352 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +0000353 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
354 !SkScalarIsInt(fIntervals[0]) ||
355 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000356 return false;
357 }
358
robertphillips@google.com629ab542012-11-28 17:18:11 +0000359 SkPoint pts[2];
360
robertphillips@google.com6d875572012-12-17 18:56:29 +0000361 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000362 return false;
363 }
364
robertphillips@google.com6d875572012-12-17 18:56:29 +0000365 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +0000366 if (SkPaint::kButt_Cap != rec.getCap()) {
367 return false;
368 }
369
robertphillips@google.com6d875572012-12-17 18:56:29 +0000370 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000371 if (!matrix.rectStaysRect()) {
372 return false;
373 }
374
robertphillips@google.com6d875572012-12-17 18:56:29 +0000375 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000376
robertphillips@google.com6d875572012-12-17 18:56:29 +0000377 SkVector tangent = pts[1] - pts[0];
378 if (tangent.isZero()) {
379 return false;
380 }
381
382 tangent.scale(SkScalarInvert(length));
383
384 // TODO: make this test for horizontal & vertical lines more robust
385 bool isXAxis = true;
386 if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) {
387 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
388 } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) {
389 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
390 isXAxis = false;
391 } else if (SkPaint::kRound_Cap != rec.getCap()) {
392 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000393 return false;
394 }
395
396 if (NULL != results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000397 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000398 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000399
robertphillips@google.com6d875572012-12-17 18:56:29 +0000400 if (SkPaint::kRound_Cap == rec.getCap()) {
401 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000402 }
403
robertphillips@google.com6d875572012-12-17 18:56:29 +0000404 results->fNumPoints = 0;
405 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000406 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
407 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000408 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000409 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000410 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000411 ++results->fNumPoints; // partial first dash
412 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000413 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000414 }
415 len2 -= fIntervals[1]; // also skip first space
416 if (len2 < 0) {
417 len2 = 0;
418 }
419 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000420 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000421 }
422 }
423 int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength));
424 results->fNumPoints += numMidPoints;
425 len2 -= numMidPoints * fIntervalLength;
426 bool partialLast = false;
427 if (len2 > 0) {
428 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000429 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000430 } else {
431 ++numMidPoints;
432 ++results->fNumPoints;
433 }
434 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000435
robertphillips@google.com935ad022012-12-05 19:07:21 +0000436 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000437
robertphillips@google.com6d875572012-12-17 18:56:29 +0000438 SkScalar distance = 0;
439 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000440
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000441 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
442 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000443
robertphillips@google.com6d875572012-12-17 18:56:29 +0000444 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000445 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000446 // partial first block
447 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000448 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength));
449 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000450 SkScalar halfWidth, halfHeight;
451 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000452 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000453 halfHeight = SkScalarHalf(rec.getWidth());
454 } else {
455 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000456 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000457 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000458 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000459 // This one will not be like the others
460 results->fFirst.addRect(x - halfWidth, y - halfHeight,
461 x + halfWidth, y + halfHeight);
462 } else {
463 SkASSERT(curPt < results->fNumPoints);
464 results->fPoints[curPt].set(x, y);
465 ++curPt;
466 }
467
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000468 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000469 }
470
471 distance += fIntervals[1]; // skip over the next blank block too
472 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000473 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000474 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000475 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000476
robertphillips@google.com6d875572012-12-17 18:56:29 +0000477 if (0 != numMidPoints) {
478 distance += SkScalarHalf(fIntervals[0]);
479
480 for (int i = 0; i < numMidPoints; ++i) {
481 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance);
482 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance);
483
484 SkASSERT(curPt < results->fNumPoints);
485 results->fPoints[curPt].set(x, y);
486 ++curPt;
487
488 distance += fIntervalLength;
489 }
490
491 distance -= SkScalarHalf(fIntervals[0]);
492 }
493
494 if (partialLast) {
495 // partial final block
496 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
497 SkScalar temp = length - distance;
498 SkASSERT(temp < fIntervals[0]);
499 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp));
500 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp));
501 SkScalar halfWidth, halfHeight;
502 if (isXAxis) {
503 halfWidth = SkScalarHalf(temp);
504 halfHeight = SkScalarHalf(rec.getWidth());
505 } else {
506 halfWidth = SkScalarHalf(rec.getWidth());
507 halfHeight = SkScalarHalf(temp);
508 }
509 results->fLast.addRect(x - halfWidth, y - halfHeight,
510 x + halfWidth, y + halfHeight);
511 }
512
513 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000514 }
515
516 return true;
517}
518
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000519SkPathEffect::DashType SkDashPathEffect::asADash(DashInfo* info) const {
520 if (info) {
521 if (info->fCount >= fCount && NULL != info->fIntervals) {
522 memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
523 }
524 info->fCount = fCount;
525 info->fPhase = fPhase;
526 }
527 return kDash_DashType;
528}
529
robertphillips@google.comc2eae472013-10-21 12:26:10 +0000530SkFlattenable::Factory SkDashPathEffect::getFactory() const {
commit-bot@chromium.org7fc22282014-03-07 14:43:00 +0000531 return CreateProc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532}
533
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000534void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +0000535 this->INHERITED::flatten(buffer);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000536 buffer.writeScalar(fPhase);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000537 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538}
539
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000540SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000541 return SkNEW_ARGS(SkDashPathEffect, (buffer));
542}
543
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000544SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000545 bool useOldPic = buffer.isVersionLT(SkReadBuffer::kDashWritesPhaseIntervals_Version);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000546 if (useOldPic) {
547 fInitialDashIndex = buffer.readInt();
548 fInitialDashLength = buffer.readScalar();
549 fIntervalLength = buffer.readScalar();
550 buffer.readBool(); // Dummy for old ScalarToFit field
551 } else {
552 fPhase = buffer.readScalar();
553 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000554
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000555 fCount = buffer.getArrayCount();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000556 size_t allocSize = sizeof(SkScalar) * fCount;
557 if (buffer.validateAvailable(allocSize)) {
558 fIntervals = (SkScalar*)sk_malloc_throw(allocSize);
559 buffer.readScalarArray(fIntervals, fCount);
560 } else {
561 fIntervals = NULL;
562 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000563
564 if (useOldPic) {
565 fPhase = 0;
commit-bot@chromium.org6b3eebc2014-05-13 14:45:29 +0000566 if (fInitialDashLength != -1) { // Signal for bad dash interval
567 for (int i = 0; i < fInitialDashIndex; ++i) {
568 fPhase += fIntervals[i];
569 }
570 fPhase += fIntervals[fInitialDashIndex] - fInitialDashLength;
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000571 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000572 } else {
573 this->setInternalMembers(fPhase);
574 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000575}