blob: 1a2c635a7fa8824849d1cf4dcd81f39dcfbdca91 [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"
Mike Reed76f70622017-05-23 23:00:14 -04009#include "SkDashImpl.h"
egdaniela22ea182014-06-11 06:51:51 -070010#include "SkDashPathPriv.h"
Cary Clark4dc5a452018-05-21 11:56:57 -040011#include "SkFlattenablePriv.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
halcanary435657f2015-09-15 12:53:07 -070014#include "SkStrokeRec.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
Mike Reed76f70622017-05-23 23:00:14 -040016SkDashImpl::SkDashImpl(const SkScalar intervals[], int count, SkScalar phase)
mtklein1c577cd2014-07-09 09:54:10 -070017 : fPhase(0)
caryclarkeb75c7d2016-03-18 06:04:26 -070018 , fInitialDashLength(-1)
mtklein1c577cd2014-07-09 09:54:10 -070019 , fInitialDashIndex(0)
20 , fIntervalLength(0) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000021 SkASSERT(intervals);
cblumeeafe9d12016-09-26 04:22:59 -070022 SkASSERT(count > 1 && SkIsAlign2(count));
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000023
24 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
25 fCount = count;
26 for (int i = 0; i < count; i++) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000027 fIntervals[i] = intervals[i];
28 }
29
egdaniela22ea182014-06-11 06:51:51 -070030 // set the internal data members
mtklein1c577cd2014-07-09 09:54:10 -070031 SkDashPath::CalcDashParameters(phase, fIntervals, fCount,
32 &fInitialDashLength, &fInitialDashIndex, &fIntervalLength, &fPhase);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000033}
34
Mike Reed76f70622017-05-23 23:00:14 -040035SkDashImpl::~SkDashImpl() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 sk_free(fIntervals);
37}
38
Mike Reed76f70622017-05-23 23:00:14 -040039bool SkDashImpl::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
40 const SkRect* cullRect) const {
caryclarkeb75c7d2016-03-18 06:04:26 -070041 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals, fCount,
egdaniela22ea182014-06-11 06:51:51 -070042 fInitialDashLength, fInitialDashIndex, fIntervalLength);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043}
44
robertphillips9f2251c2014-11-04 13:33:50 -080045static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
46 SkScalar radius = SkScalarHalf(rec.getWidth());
47 if (0 == radius) {
48 radius = SK_Scalar1; // hairlines
49 }
50 if (SkPaint::kMiter_Join == rec.getJoin()) {
Mike Reed8be952a2017-02-13 20:44:33 -050051 radius *= rec.getMiter();
robertphillips9f2251c2014-11-04 13:33:50 -080052 }
53 rect->outset(radius, radius);
54}
55
mtklein88fd0fb2014-12-01 06:56:38 -080056// Attempt to trim the line to minimally cover the cull rect (currently
robertphillips9f2251c2014-11-04 13:33:50 -080057// only works for horizontal and vertical lines).
58// Return true if processing should continue; false otherwise.
59static bool cull_line(SkPoint* pts, const SkStrokeRec& rec,
60 const SkMatrix& ctm, const SkRect* cullRect,
61 const SkScalar intervalLength) {
halcanary96fcdcc2015-08-27 07:41:13 -070062 if (nullptr == cullRect) {
robertphillips9f2251c2014-11-04 13:33:50 -080063 SkASSERT(false); // Shouldn't ever occur in practice
64 return false;
65 }
66
67 SkScalar dx = pts[1].x() - pts[0].x();
68 SkScalar dy = pts[1].y() - pts[0].y();
69
egdaniel21402e32014-11-05 05:02:27 -080070 if ((dx && dy) || (!dx && !dy)) {
robertphillips9f2251c2014-11-04 13:33:50 -080071 return false;
72 }
73
74 SkRect bounds = *cullRect;
75 outset_for_stroke(&bounds, rec);
76
77 // cullRect is in device space while pts are in the local coordinate system
78 // defined by the ctm. We want our answer in the local coordinate system.
79
80 SkASSERT(ctm.rectStaysRect());
81 SkMatrix inv;
82 if (!ctm.invert(&inv)) {
83 return false;
84 }
85
86 inv.mapRect(&bounds);
87
88 if (dx) {
89 SkASSERT(dx && !dy);
90 SkScalar minX = pts[0].fX;
91 SkScalar maxX = pts[1].fX;
92
93 if (dx < 0) {
94 SkTSwap(minX, maxX);
95 }
96
97 SkASSERT(minX < maxX);
reedccc12ed2014-12-12 12:48:50 -080098 if (maxX <= bounds.fLeft || minX >= bounds.fRight) {
robertphillips9f2251c2014-11-04 13:33:50 -080099 return false;
100 }
101
102 // Now we actually perform the chop, removing the excess to the left and
103 // right of the bounds (keeping our new line "in phase" with the dash,
104 // hence the (mod intervalLength).
105
106 if (minX < bounds.fLeft) {
107 minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, intervalLength);
108 }
109 if (maxX > bounds.fRight) {
110 maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, intervalLength);
111 }
112
113 SkASSERT(maxX > minX);
114 if (dx < 0) {
115 SkTSwap(minX, maxX);
116 }
117 pts[0].fX = minX;
118 pts[1].fX = maxX;
119 } else {
120 SkASSERT(dy && !dx);
121 SkScalar minY = pts[0].fY;
122 SkScalar maxY = pts[1].fY;
123
124 if (dy < 0) {
125 SkTSwap(minY, maxY);
126 }
127
128 SkASSERT(minY < maxY);
reedccc12ed2014-12-12 12:48:50 -0800129 if (maxY <= bounds.fTop || minY >= bounds.fBottom) {
robertphillips9f2251c2014-11-04 13:33:50 -0800130 return false;
131 }
132
133 // Now we actually perform the chop, removing the excess to the top and
134 // bottom of the bounds (keeping our new line "in phase" with the dash,
135 // hence the (mod intervalLength).
136
137 if (minY < bounds.fTop) {
138 minY = bounds.fTop - SkScalarMod(bounds.fTop - minY, intervalLength);
139 }
140 if (maxY > bounds.fBottom) {
141 maxY = bounds.fBottom + SkScalarMod(maxY - bounds.fBottom, intervalLength);
142 }
143
144 SkASSERT(maxY > minY);
145 if (dy < 0) {
146 SkTSwap(minY, maxY);
147 }
148 pts[0].fY = minY;
149 pts[1].fY = maxY;
150 }
151
152 return true;
153}
154
robertphillips@google.com629ab542012-11-28 17:18:11 +0000155// Currently asPoints is more restrictive then it needs to be. In the future
156// we need to:
157// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +0000158// allow paths to be returned
Mike Reed76f70622017-05-23 23:00:14 -0400159bool SkDashImpl::asPoints(PointData* results, const SkPath& src, const SkStrokeRec& rec,
160 const SkMatrix& matrix, const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000161 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
caryclarkeb75c7d2016-03-18 06:04:26 -0700162 if (0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000163 return false;
164 }
165
robertphillips@google.com6d875572012-12-17 18:56:29 +0000166 // TODO: this next test could be eased up. We could allow any number of
167 // intervals as long as all the ons match and all the offs match.
168 // Additionally, they do not necessarily need to be integers.
169 // We cannot allow arbitrary intervals since we want the returned points
170 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000171 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +0000172 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
173 !SkScalarIsInt(fIntervals[0]) ||
174 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000175 return false;
176 }
177
robertphillips@google.com629ab542012-11-28 17:18:11 +0000178 SkPoint pts[2];
179
robertphillips@google.com6d875572012-12-17 18:56:29 +0000180 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000181 return false;
182 }
183
robertphillips@google.com6d875572012-12-17 18:56:29 +0000184 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +0000185 if (SkPaint::kButt_Cap != rec.getCap()) {
186 return false;
187 }
188
robertphillips@google.com6d875572012-12-17 18:56:29 +0000189 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000190 if (!matrix.rectStaysRect()) {
191 return false;
192 }
193
robertphillips9f2251c2014-11-04 13:33:50 -0800194 // See if the line can be limited to something plausible.
195 if (!cull_line(pts, rec, matrix, cullRect, fIntervalLength)) {
196 return false;
197 }
198
199 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000200
robertphillips@google.com6d875572012-12-17 18:56:29 +0000201 SkVector tangent = pts[1] - pts[0];
202 if (tangent.isZero()) {
203 return false;
204 }
205
206 tangent.scale(SkScalarInvert(length));
207
208 // TODO: make this test for horizontal & vertical lines more robust
209 bool isXAxis = true;
robertphillips9f2251c2014-11-04 13:33:50 -0800210 if (SkScalarNearlyEqual(SK_Scalar1, tangent.fX) ||
211 SkScalarNearlyEqual(-SK_Scalar1, tangent.fX)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000212 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
robertphillips9f2251c2014-11-04 13:33:50 -0800213 } else if (SkScalarNearlyEqual(SK_Scalar1, tangent.fY) ||
214 SkScalarNearlyEqual(-SK_Scalar1, tangent.fY)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000215 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
216 isXAxis = false;
217 } else if (SkPaint::kRound_Cap != rec.getCap()) {
218 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000219 return false;
220 }
221
bsalomon49f085d2014-09-05 13:34:00 -0700222 if (results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000223 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000224 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000225
robertphillips@google.com6d875572012-12-17 18:56:29 +0000226 if (SkPaint::kRound_Cap == rec.getCap()) {
227 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000228 }
229
robertphillips@google.com6d875572012-12-17 18:56:29 +0000230 results->fNumPoints = 0;
231 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000232 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
233 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000234 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000235 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000236 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000237 ++results->fNumPoints; // partial first dash
238 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000239 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000240 }
241 len2 -= fIntervals[1]; // also skip first space
242 if (len2 < 0) {
243 len2 = 0;
244 }
245 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000246 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000247 }
248 }
lsalzmanf41ae2f2016-07-21 09:37:59 -0700249 // Too many midpoints can cause results->fNumPoints to overflow or
250 // otherwise cause the results->fPoints allocation below to OOM.
251 // Cap it to a sane value.
252 SkScalar numIntervals = len2 / fIntervalLength;
253 if (!SkScalarIsFinite(numIntervals) || numIntervals > SkDashPath::kMaxDashCount) {
254 return false;
255 }
256 int numMidPoints = SkScalarFloorToInt(numIntervals);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000257 results->fNumPoints += numMidPoints;
258 len2 -= numMidPoints * fIntervalLength;
259 bool partialLast = false;
260 if (len2 > 0) {
261 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000262 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000263 } else {
264 ++numMidPoints;
265 ++results->fNumPoints;
266 }
267 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000268
robertphillips@google.com935ad022012-12-05 19:07:21 +0000269 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000270
robertphillips@google.com6d875572012-12-17 18:56:29 +0000271 SkScalar distance = 0;
272 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000273
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000274 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
275 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000276
robertphillips@google.com6d875572012-12-17 18:56:29 +0000277 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000278 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000279 // partial first block
280 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
Mike Reed8be952a2017-02-13 20:44:33 -0500281 SkScalar x = pts[0].fX + tangent.fX * SkScalarHalf(clampedInitialDashLength);
282 SkScalar y = pts[0].fY + tangent.fY * SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000283 SkScalar halfWidth, halfHeight;
284 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000285 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000286 halfHeight = SkScalarHalf(rec.getWidth());
287 } else {
288 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000289 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000290 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000291 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000292 // This one will not be like the others
293 results->fFirst.addRect(x - halfWidth, y - halfHeight,
294 x + halfWidth, y + halfHeight);
295 } else {
296 SkASSERT(curPt < results->fNumPoints);
297 results->fPoints[curPt].set(x, y);
298 ++curPt;
299 }
300
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000301 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000302 }
303
304 distance += fIntervals[1]; // skip over the next blank block too
305 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000306 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000307 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000308 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000309
robertphillips@google.com6d875572012-12-17 18:56:29 +0000310 if (0 != numMidPoints) {
311 distance += SkScalarHalf(fIntervals[0]);
312
313 for (int i = 0; i < numMidPoints; ++i) {
Mike Reed8be952a2017-02-13 20:44:33 -0500314 SkScalar x = pts[0].fX + tangent.fX * distance;
315 SkScalar y = pts[0].fY + tangent.fY * distance;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000316
317 SkASSERT(curPt < results->fNumPoints);
318 results->fPoints[curPt].set(x, y);
319 ++curPt;
320
321 distance += fIntervalLength;
322 }
323
324 distance -= SkScalarHalf(fIntervals[0]);
325 }
326
327 if (partialLast) {
328 // partial final block
329 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
330 SkScalar temp = length - distance;
331 SkASSERT(temp < fIntervals[0]);
Mike Reed8be952a2017-02-13 20:44:33 -0500332 SkScalar x = pts[0].fX + tangent.fX * (distance + SkScalarHalf(temp));
333 SkScalar y = pts[0].fY + tangent.fY * (distance + SkScalarHalf(temp));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000334 SkScalar halfWidth, halfHeight;
335 if (isXAxis) {
336 halfWidth = SkScalarHalf(temp);
337 halfHeight = SkScalarHalf(rec.getWidth());
338 } else {
339 halfWidth = SkScalarHalf(rec.getWidth());
340 halfHeight = SkScalarHalf(temp);
341 }
342 results->fLast.addRect(x - halfWidth, y - halfHeight,
343 x + halfWidth, y + halfHeight);
344 }
345
346 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000347 }
348
349 return true;
350}
351
Mike Reed76f70622017-05-23 23:00:14 -0400352SkPathEffect::DashType SkDashImpl::asADash(DashInfo* info) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000353 if (info) {
bsalomon49f085d2014-09-05 13:34:00 -0700354 if (info->fCount >= fCount && info->fIntervals) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000355 memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
356 }
357 info->fCount = fCount;
358 info->fPhase = fPhase;
359 }
360 return kDash_DashType;
361}
362
Mike Reed76f70622017-05-23 23:00:14 -0400363void SkDashImpl::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000364 buffer.writeScalar(fPhase);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000365 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366}
367
Mike Reed76f70622017-05-23 23:00:14 -0400368sk_sp<SkFlattenable> SkDashImpl::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700369 const SkScalar phase = buffer.readScalar();
370 uint32_t count = buffer.getArrayCount();
Adrienne Walker77e95f72018-04-24 16:41:41 -0700371
372 // Don't allocate gigantic buffers if there's not data for them.
Kevin Lubickdaebae92018-05-17 11:29:10 -0400373 if (!buffer.validateCanReadN<SkScalar>(count)) {
Adrienne Walker77e95f72018-04-24 16:41:41 -0700374 return nullptr;
375 }
376
reed9fa60da2014-08-21 07:59:51 -0700377 SkAutoSTArray<32, SkScalar> intervals(count);
378 if (buffer.readScalarArray(intervals.get(), count)) {
Mike Reed76f70622017-05-23 23:00:14 -0400379 return SkDashPathEffect::Make(intervals.get(), SkToInt(count), phase);
reed9fa60da2014-08-21 07:59:51 -0700380 }
halcanary96fcdcc2015-08-27 07:41:13 -0700381 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382}
robertphillips42dbfa82015-01-26 06:08:52 -0800383
Cary Clark32a49102018-05-20 23:15:43 +0000384void SkDashImpl::toString(SkString* str) const {
385 str->appendf("SkDashPathEffect: (");
386 str->appendf("count: %d phase %.2f intervals: (", fCount, fPhase);
387 for (int i = 0; i < fCount; ++i) {
388 str->appendf("%.2f", fIntervals[i]);
389 if (i < fCount-1) {
390 str->appendf(", ");
391 }
392 }
393 str->appendf("))");
394}
395
reedca726ab2016-02-22 12:50:25 -0800396//////////////////////////////////////////////////////////////////////////////////////////////////
397
reeda4393342016-03-18 11:22:57 -0700398sk_sp<SkPathEffect> SkDashPathEffect::Make(const SkScalar intervals[], int count, SkScalar phase) {
caryclarkeb75c7d2016-03-18 06:04:26 -0700399 if (!SkDashPath::ValidDashPath(phase, intervals, count)) {
reedca726ab2016-02-22 12:50:25 -0800400 return nullptr;
401 }
Mike Reed76f70622017-05-23 23:00:14 -0400402 return sk_sp<SkPathEffect>(new SkDashImpl(intervals, count, phase));
reedca726ab2016-02-22 12:50:25 -0800403}