blob: 0d2783eba257b35bef23ca56e7d71c8a51094bfc [file] [log] [blame]
egdaniela22ea182014-06-11 06:51:51 -07001/*
2 * Copyright 2014 Google Inc.
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
8#include "SkDashPathPriv.h"
9#include "SkPathMeasure.h"
halcanary435657f2015-09-15 12:53:07 -070010#include "SkStrokeRec.h"
egdaniela22ea182014-06-11 06:51:51 -070011
12static inline int is_even(int x) {
caryclark3127c992015-12-09 12:02:30 -080013 return !(x & 1);
egdaniela22ea182014-06-11 06:51:51 -070014}
15
16static SkScalar find_first_interval(const SkScalar intervals[], SkScalar phase,
17 int32_t* index, int count) {
18 for (int i = 0; i < count; ++i) {
caryclarkd3cfd942016-03-17 05:33:28 -070019 SkScalar gap = intervals[i];
20 if (phase > gap || (phase == gap && gap)) {
21 phase -= gap;
egdaniela22ea182014-06-11 06:51:51 -070022 } else {
23 *index = i;
caryclarkd3cfd942016-03-17 05:33:28 -070024 return gap - phase;
egdaniela22ea182014-06-11 06:51:51 -070025 }
26 }
27 // If we get here, phase "appears" to be larger than our length. This
28 // shouldn't happen with perfect precision, but we can accumulate errors
29 // during the initial length computation (rounding can make our sum be too
30 // big or too small. In that event, we just have to eat the error here.
31 *index = 0;
32 return intervals[0];
33}
34
35void SkDashPath::CalcDashParameters(SkScalar phase, const SkScalar intervals[], int32_t count,
36 SkScalar* initialDashLength, int32_t* initialDashIndex,
37 SkScalar* intervalLength, SkScalar* adjustedPhase) {
38 SkScalar len = 0;
39 for (int i = 0; i < count; i++) {
40 len += intervals[i];
41 }
42 *intervalLength = len;
43
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
49 if (adjustedPhase) {
50 if (phase < 0) {
51 phase = -phase;
52 if (phase > len) {
53 phase = SkScalarMod(phase, len);
54 }
55 phase = len - phase;
56
57 // Due to finite precision, it's possible that phase == len,
58 // even after the subtract (if len >>> phase), so fix that here.
59 // This fixes http://crbug.com/124652 .
60 SkASSERT(phase <= len);
61 if (phase == len) {
62 phase = 0;
63 }
64 } else if (phase >= len) {
65 phase = SkScalarMod(phase, len);
66 }
67 *adjustedPhase = phase;
68 }
69 SkASSERT(phase >= 0 && phase < len);
70
71 *initialDashLength = find_first_interval(intervals, phase,
72 initialDashIndex, count);
73
74 SkASSERT(*initialDashLength >= 0);
75 SkASSERT(*initialDashIndex >= 0 && *initialDashIndex < count);
76 } else {
77 *initialDashLength = -1; // signal bad dash intervals
78 }
79}
80
81static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
82 SkScalar radius = SkScalarHalf(rec.getWidth());
83 if (0 == radius) {
84 radius = SK_Scalar1; // hairlines
85 }
86 if (SkPaint::kMiter_Join == rec.getJoin()) {
87 radius = SkScalarMul(radius, rec.getMiter());
88 }
89 rect->outset(radius, radius);
90}
91
92// Only handles lines for now. If returns true, dstPath is the new (smaller)
93// path. If returns false, then dstPath parameter is ignored.
94static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec,
95 const SkRect* cullRect, SkScalar intervalLength,
96 SkPath* dstPath) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 if (nullptr == cullRect) {
egdaniela22ea182014-06-11 06:51:51 -070098 return false;
99 }
100
101 SkPoint pts[2];
102 if (!srcPath.isLine(pts)) {
103 return false;
104 }
105
106 SkRect bounds = *cullRect;
107 outset_for_stroke(&bounds, rec);
108
109 SkScalar dx = pts[1].x() - pts[0].x();
110 SkScalar dy = pts[1].y() - pts[0].y();
111
112 // just do horizontal lines for now (lazy)
113 if (dy) {
114 return false;
115 }
116
117 SkScalar minX = pts[0].fX;
118 SkScalar maxX = pts[1].fX;
119
egdaniela22ea182014-06-11 06:51:51 -0700120 if (dx < 0) {
121 SkTSwap(minX, maxX);
122 }
123
egdaniel21402e32014-11-05 05:02:27 -0800124 SkASSERT(minX <= maxX);
robertphillips9f2251c2014-11-04 13:33:50 -0800125 if (maxX < bounds.fLeft || minX > bounds.fRight) {
126 return false;
127 }
128
egdaniela22ea182014-06-11 06:51:51 -0700129 // Now we actually perform the chop, removing the excess to the left and
130 // right of the bounds (keeping our new line "in phase" with the dash,
131 // hence the (mod intervalLength).
132
133 if (minX < bounds.fLeft) {
134 minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX,
135 intervalLength);
136 }
137 if (maxX > bounds.fRight) {
138 maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight,
139 intervalLength);
140 }
141
142 SkASSERT(maxX >= minX);
143 if (dx < 0) {
144 SkTSwap(minX, maxX);
145 }
146 pts[0].fX = minX;
147 pts[1].fX = maxX;
148
149 dstPath->moveTo(pts[0]);
150 dstPath->lineTo(pts[1]);
151 return true;
152}
153
154class SpecialLineRec {
155public:
156 bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec,
157 int intervalCount, SkScalar intervalLength) {
158 if (rec->isHairlineStyle() || !src.isLine(fPts)) {
159 return false;
160 }
161
162 // can relax this in the future, if we handle square and round caps
163 if (SkPaint::kButt_Cap != rec->getCap()) {
164 return false;
165 }
166
167 SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]);
168
169 fTangent = fPts[1] - fPts[0];
170 if (fTangent.isZero()) {
171 return false;
172 }
173
174 fPathLength = pathLength;
175 fTangent.scale(SkScalarInvert(pathLength));
176 fTangent.rotateCCW(&fNormal);
177 fNormal.scale(SkScalarHalf(rec->getWidth()));
178
179 // now estimate how many quads will be added to the path
180 // resulting segments = pathLen * intervalCount / intervalLen
181 // resulting points = 4 * segments
182
183 SkScalar ptCount = SkScalarMulDiv(pathLength,
184 SkIntToScalar(intervalCount),
185 intervalLength);
186 int n = SkScalarCeilToInt(ptCount) << 2;
187 dst->incReserve(n);
188
189 // we will take care of the stroking
190 rec->setFillStyle();
191 return true;
192 }
193
194 void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const {
195 SkASSERT(d0 < fPathLength);
196 // clamp the segment to our length
197 if (d1 > fPathLength) {
198 d1 = fPathLength;
199 }
200
201 SkScalar x0 = fPts[0].fX + SkScalarMul(fTangent.fX, d0);
202 SkScalar x1 = fPts[0].fX + SkScalarMul(fTangent.fX, d1);
203 SkScalar y0 = fPts[0].fY + SkScalarMul(fTangent.fY, d0);
204 SkScalar y1 = fPts[0].fY + SkScalarMul(fTangent.fY, d1);
205
206 SkPoint pts[4];
207 pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo
208 pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo
209 pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo
210 pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo
211
212 path->addPoly(pts, SK_ARRAY_COUNT(pts), false);
213 }
214
215private:
216 SkPoint fPts[2];
217 SkVector fTangent;
218 SkVector fNormal;
219 SkScalar fPathLength;
220};
221
222
223bool SkDashPath::FilterDashPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
224 const SkRect* cullRect, const SkScalar aIntervals[],
225 int32_t count, SkScalar initialDashLength, int32_t initialDashIndex,
226 SkScalar intervalLength) {
227
228 // we do nothing if the src wants to be filled, or if our dashlength is 0
229 if (rec->isFillStyle() || initialDashLength < 0) {
230 return false;
231 }
232
233 const SkScalar* intervals = aIntervals;
234 SkScalar dashCount = 0;
235 int segCount = 0;
236
237 SkPath cullPathStorage;
238 const SkPath* srcPtr = &src;
239 if (cull_path(src, *rec, cullRect, intervalLength, &cullPathStorage)) {
240 srcPtr = &cullPathStorage;
241 }
242
243 SpecialLineRec lineRec;
244 bool specialLine = lineRec.init(*srcPtr, dst, rec, count >> 1, intervalLength);
245
caryclark1a7eb262016-01-21 07:07:02 -0800246 SkPathMeasure meas(*srcPtr, false, rec->getResScale());
egdaniela22ea182014-06-11 06:51:51 -0700247
248 do {
249 bool skipFirstSegment = meas.isClosed();
250 bool addedSegment = false;
251 SkScalar length = meas.getLength();
252 int index = initialDashIndex;
253
254 // Since the path length / dash length ratio may be arbitrarily large, we can exert
255 // significant memory pressure while attempting to build the filtered path. To avoid this,
256 // we simply give up dashing beyond a certain threshold.
257 //
258 // The original bug report (http://crbug.com/165432) is based on a path yielding more than
259 // 90 million dash segments and crashing the memory allocator. A limit of 1 million
260 // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the
261 // maximum dash memory overhead at roughly 17MB per path.
262 static const SkScalar kMaxDashCount = 1000000;
263 dashCount += length * (count >> 1) / intervalLength;
264 if (dashCount > kMaxDashCount) {
265 dst->reset();
266 return false;
267 }
268
269 // Using double precision to avoid looping indefinitely due to single precision rounding
270 // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest.
271 double distance = 0;
272 double dlen = initialDashLength;
273
274 while (distance < length) {
275 SkASSERT(dlen >= 0);
276 addedSegment = false;
caryclark5cb00a92015-08-26 09:04:55 -0700277 if (is_even(index) && !skipFirstSegment) {
egdaniela22ea182014-06-11 06:51:51 -0700278 addedSegment = true;
279 ++segCount;
280
281 if (specialLine) {
282 lineRec.addSegment(SkDoubleToScalar(distance),
283 SkDoubleToScalar(distance + dlen),
284 dst);
285 } else {
286 meas.getSegment(SkDoubleToScalar(distance),
287 SkDoubleToScalar(distance + dlen),
288 dst, true);
289 }
290 }
291 distance += dlen;
292
293 // clear this so we only respect it the first time around
294 skipFirstSegment = false;
295
296 // wrap around our intervals array if necessary
297 index += 1;
298 SkASSERT(index <= count);
299 if (index == count) {
300 index = 0;
301 }
302
303 // fetch our next dlen
304 dlen = intervals[index];
305 }
306
307 // extend if we ended on a segment and we need to join up with the (skipped) initial segment
308 if (meas.isClosed() && is_even(initialDashIndex) &&
309 initialDashLength > 0) {
310 meas.getSegment(0, initialDashLength, dst, !addedSegment);
311 ++segCount;
312 }
313 } while (meas.nextContour());
314
315 if (segCount > 1) {
316 dst->setConvexity(SkPath::kConcave_Convexity);
317 }
318
319 return true;
320}
321
322bool SkDashPath::FilterDashPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
323 const SkRect* cullRect, const SkPathEffect::DashInfo& info) {
324 SkScalar initialDashLength = 0;
325 int32_t initialDashIndex = 0;
326 SkScalar intervalLength = 0;
327 CalcDashParameters(info.fPhase, info.fIntervals, info.fCount,
328 &initialDashLength, &initialDashIndex, &intervalLength);
329 return FilterDashPath(dst, src, rec, cullRect, info.fIntervals, info.fCount, initialDashLength,
330 initialDashIndex, intervalLength);
331}