blob: aa3803a95c4cf99178a43dd7461fac1e60ea9f02 [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"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Reed76f70622017-05-23 23:00:14 -040010#include "SkDashImpl.h"
egdaniela22ea182014-06-11 06:51:51 -070011#include "SkDashPathPriv.h"
Cary Clark4dc5a452018-05-21 11:56:57 -040012#include "SkFlattenablePriv.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000013#include "SkReadBuffer.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040014#include "SkStrokeRec.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040015#include "SkTo.h"
16#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
Ben Wagnerf08d1d02018-06-18 15:11:00 -040018#include <utility>
19
Mike Reed76f70622017-05-23 23:00:14 -040020SkDashImpl::SkDashImpl(const SkScalar intervals[], int count, SkScalar phase)
mtklein1c577cd2014-07-09 09:54:10 -070021 : fPhase(0)
caryclarkeb75c7d2016-03-18 06:04:26 -070022 , fInitialDashLength(-1)
mtklein1c577cd2014-07-09 09:54:10 -070023 , fInitialDashIndex(0)
24 , fIntervalLength(0) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000025 SkASSERT(intervals);
cblumeeafe9d12016-09-26 04:22:59 -070026 SkASSERT(count > 1 && SkIsAlign2(count));
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000027
28 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
29 fCount = count;
30 for (int i = 0; i < count; i++) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000031 fIntervals[i] = intervals[i];
32 }
33
egdaniela22ea182014-06-11 06:51:51 -070034 // set the internal data members
mtklein1c577cd2014-07-09 09:54:10 -070035 SkDashPath::CalcDashParameters(phase, fIntervals, fCount,
36 &fInitialDashLength, &fInitialDashIndex, &fIntervalLength, &fPhase);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000037}
38
Mike Reed76f70622017-05-23 23:00:14 -040039SkDashImpl::~SkDashImpl() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 sk_free(fIntervals);
41}
42
Mike Reed76f70622017-05-23 23:00:14 -040043bool SkDashImpl::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
44 const SkRect* cullRect) const {
caryclarkeb75c7d2016-03-18 06:04:26 -070045 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals, fCount,
egdaniela22ea182014-06-11 06:51:51 -070046 fInitialDashLength, fInitialDashIndex, fIntervalLength);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047}
48
robertphillips9f2251c2014-11-04 13:33:50 -080049static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
50 SkScalar radius = SkScalarHalf(rec.getWidth());
51 if (0 == radius) {
52 radius = SK_Scalar1; // hairlines
53 }
54 if (SkPaint::kMiter_Join == rec.getJoin()) {
Mike Reed8be952a2017-02-13 20:44:33 -050055 radius *= rec.getMiter();
robertphillips9f2251c2014-11-04 13:33:50 -080056 }
57 rect->outset(radius, radius);
58}
59
mtklein88fd0fb2014-12-01 06:56:38 -080060// Attempt to trim the line to minimally cover the cull rect (currently
robertphillips9f2251c2014-11-04 13:33:50 -080061// only works for horizontal and vertical lines).
62// Return true if processing should continue; false otherwise.
63static bool cull_line(SkPoint* pts, const SkStrokeRec& rec,
64 const SkMatrix& ctm, const SkRect* cullRect,
65 const SkScalar intervalLength) {
halcanary96fcdcc2015-08-27 07:41:13 -070066 if (nullptr == cullRect) {
robertphillips9f2251c2014-11-04 13:33:50 -080067 SkASSERT(false); // Shouldn't ever occur in practice
68 return false;
69 }
70
71 SkScalar dx = pts[1].x() - pts[0].x();
72 SkScalar dy = pts[1].y() - pts[0].y();
73
egdaniel21402e32014-11-05 05:02:27 -080074 if ((dx && dy) || (!dx && !dy)) {
robertphillips9f2251c2014-11-04 13:33:50 -080075 return false;
76 }
77
78 SkRect bounds = *cullRect;
79 outset_for_stroke(&bounds, rec);
80
81 // cullRect is in device space while pts are in the local coordinate system
82 // defined by the ctm. We want our answer in the local coordinate system.
83
84 SkASSERT(ctm.rectStaysRect());
85 SkMatrix inv;
86 if (!ctm.invert(&inv)) {
87 return false;
88 }
89
90 inv.mapRect(&bounds);
91
92 if (dx) {
93 SkASSERT(dx && !dy);
94 SkScalar minX = pts[0].fX;
95 SkScalar maxX = pts[1].fX;
96
97 if (dx < 0) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040098 using std::swap;
99 swap(minX, maxX);
robertphillips9f2251c2014-11-04 13:33:50 -0800100 }
101
102 SkASSERT(minX < maxX);
reedccc12ed2014-12-12 12:48:50 -0800103 if (maxX <= bounds.fLeft || minX >= bounds.fRight) {
robertphillips9f2251c2014-11-04 13:33:50 -0800104 return false;
105 }
106
107 // Now we actually perform the chop, removing the excess to the left and
108 // right of the bounds (keeping our new line "in phase" with the dash,
109 // hence the (mod intervalLength).
110
111 if (minX < bounds.fLeft) {
112 minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, intervalLength);
113 }
114 if (maxX > bounds.fRight) {
115 maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, intervalLength);
116 }
117
118 SkASSERT(maxX > minX);
119 if (dx < 0) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400120 using std::swap;
121 swap(minX, maxX);
robertphillips9f2251c2014-11-04 13:33:50 -0800122 }
123 pts[0].fX = minX;
124 pts[1].fX = maxX;
125 } else {
126 SkASSERT(dy && !dx);
127 SkScalar minY = pts[0].fY;
128 SkScalar maxY = pts[1].fY;
129
130 if (dy < 0) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400131 using std::swap;
132 swap(minY, maxY);
robertphillips9f2251c2014-11-04 13:33:50 -0800133 }
134
135 SkASSERT(minY < maxY);
reedccc12ed2014-12-12 12:48:50 -0800136 if (maxY <= bounds.fTop || minY >= bounds.fBottom) {
robertphillips9f2251c2014-11-04 13:33:50 -0800137 return false;
138 }
139
140 // Now we actually perform the chop, removing the excess to the top and
141 // bottom of the bounds (keeping our new line "in phase" with the dash,
142 // hence the (mod intervalLength).
143
144 if (minY < bounds.fTop) {
145 minY = bounds.fTop - SkScalarMod(bounds.fTop - minY, intervalLength);
146 }
147 if (maxY > bounds.fBottom) {
148 maxY = bounds.fBottom + SkScalarMod(maxY - bounds.fBottom, intervalLength);
149 }
150
151 SkASSERT(maxY > minY);
152 if (dy < 0) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400153 using std::swap;
154 swap(minY, maxY);
robertphillips9f2251c2014-11-04 13:33:50 -0800155 }
156 pts[0].fY = minY;
157 pts[1].fY = maxY;
158 }
159
160 return true;
161}
162
robertphillips@google.com629ab542012-11-28 17:18:11 +0000163// Currently asPoints is more restrictive then it needs to be. In the future
164// we need to:
165// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +0000166// allow paths to be returned
Mike Reed76f70622017-05-23 23:00:14 -0400167bool SkDashImpl::asPoints(PointData* results, const SkPath& src, const SkStrokeRec& rec,
168 const SkMatrix& matrix, const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000169 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
caryclarkeb75c7d2016-03-18 06:04:26 -0700170 if (0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000171 return false;
172 }
173
robertphillips@google.com6d875572012-12-17 18:56:29 +0000174 // TODO: this next test could be eased up. We could allow any number of
175 // intervals as long as all the ons match and all the offs match.
176 // Additionally, they do not necessarily need to be integers.
177 // We cannot allow arbitrary intervals since we want the returned points
178 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000179 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +0000180 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
181 !SkScalarIsInt(fIntervals[0]) ||
182 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000183 return false;
184 }
185
robertphillips@google.com629ab542012-11-28 17:18:11 +0000186 SkPoint pts[2];
187
robertphillips@google.com6d875572012-12-17 18:56:29 +0000188 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000189 return false;
190 }
191
robertphillips@google.com6d875572012-12-17 18:56:29 +0000192 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +0000193 if (SkPaint::kButt_Cap != rec.getCap()) {
194 return false;
195 }
196
robertphillips@google.com6d875572012-12-17 18:56:29 +0000197 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000198 if (!matrix.rectStaysRect()) {
199 return false;
200 }
201
robertphillips9f2251c2014-11-04 13:33:50 -0800202 // See if the line can be limited to something plausible.
203 if (!cull_line(pts, rec, matrix, cullRect, fIntervalLength)) {
204 return false;
205 }
206
207 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000208
robertphillips@google.com6d875572012-12-17 18:56:29 +0000209 SkVector tangent = pts[1] - pts[0];
210 if (tangent.isZero()) {
211 return false;
212 }
213
214 tangent.scale(SkScalarInvert(length));
215
216 // TODO: make this test for horizontal & vertical lines more robust
217 bool isXAxis = true;
robertphillips9f2251c2014-11-04 13:33:50 -0800218 if (SkScalarNearlyEqual(SK_Scalar1, tangent.fX) ||
219 SkScalarNearlyEqual(-SK_Scalar1, tangent.fX)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000220 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
robertphillips9f2251c2014-11-04 13:33:50 -0800221 } else if (SkScalarNearlyEqual(SK_Scalar1, tangent.fY) ||
222 SkScalarNearlyEqual(-SK_Scalar1, tangent.fY)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000223 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
224 isXAxis = false;
225 } else if (SkPaint::kRound_Cap != rec.getCap()) {
226 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000227 return false;
228 }
229
bsalomon49f085d2014-09-05 13:34:00 -0700230 if (results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000231 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000232 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000233
robertphillips@google.com6d875572012-12-17 18:56:29 +0000234 if (SkPaint::kRound_Cap == rec.getCap()) {
235 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000236 }
237
robertphillips@google.com6d875572012-12-17 18:56:29 +0000238 results->fNumPoints = 0;
239 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000240 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
241 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000242 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000243 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000244 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000245 ++results->fNumPoints; // partial first dash
246 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000247 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000248 }
249 len2 -= fIntervals[1]; // also skip first space
250 if (len2 < 0) {
251 len2 = 0;
252 }
253 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000254 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000255 }
256 }
lsalzmanf41ae2f2016-07-21 09:37:59 -0700257 // Too many midpoints can cause results->fNumPoints to overflow or
258 // otherwise cause the results->fPoints allocation below to OOM.
259 // Cap it to a sane value.
260 SkScalar numIntervals = len2 / fIntervalLength;
261 if (!SkScalarIsFinite(numIntervals) || numIntervals > SkDashPath::kMaxDashCount) {
262 return false;
263 }
264 int numMidPoints = SkScalarFloorToInt(numIntervals);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000265 results->fNumPoints += numMidPoints;
266 len2 -= numMidPoints * fIntervalLength;
267 bool partialLast = false;
268 if (len2 > 0) {
269 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000270 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000271 } else {
272 ++numMidPoints;
273 ++results->fNumPoints;
274 }
275 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000276
robertphillips@google.com935ad022012-12-05 19:07:21 +0000277 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000278
robertphillips@google.com6d875572012-12-17 18:56:29 +0000279 SkScalar distance = 0;
280 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000281
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000282 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
283 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000284
robertphillips@google.com6d875572012-12-17 18:56:29 +0000285 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000286 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000287 // partial first block
288 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
Mike Reed8be952a2017-02-13 20:44:33 -0500289 SkScalar x = pts[0].fX + tangent.fX * SkScalarHalf(clampedInitialDashLength);
290 SkScalar y = pts[0].fY + tangent.fY * SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000291 SkScalar halfWidth, halfHeight;
292 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000293 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000294 halfHeight = SkScalarHalf(rec.getWidth());
295 } else {
296 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000297 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000298 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000299 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000300 // This one will not be like the others
301 results->fFirst.addRect(x - halfWidth, y - halfHeight,
302 x + halfWidth, y + halfHeight);
303 } else {
304 SkASSERT(curPt < results->fNumPoints);
305 results->fPoints[curPt].set(x, y);
306 ++curPt;
307 }
308
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000309 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000310 }
311
312 distance += fIntervals[1]; // skip over the next blank block too
313 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000314 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000315 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000316 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000317
robertphillips@google.com6d875572012-12-17 18:56:29 +0000318 if (0 != numMidPoints) {
319 distance += SkScalarHalf(fIntervals[0]);
320
321 for (int i = 0; i < numMidPoints; ++i) {
Mike Reed8be952a2017-02-13 20:44:33 -0500322 SkScalar x = pts[0].fX + tangent.fX * distance;
323 SkScalar y = pts[0].fY + tangent.fY * distance;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000324
325 SkASSERT(curPt < results->fNumPoints);
326 results->fPoints[curPt].set(x, y);
327 ++curPt;
328
329 distance += fIntervalLength;
330 }
331
332 distance -= SkScalarHalf(fIntervals[0]);
333 }
334
335 if (partialLast) {
336 // partial final block
337 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
338 SkScalar temp = length - distance;
339 SkASSERT(temp < fIntervals[0]);
Mike Reed8be952a2017-02-13 20:44:33 -0500340 SkScalar x = pts[0].fX + tangent.fX * (distance + SkScalarHalf(temp));
341 SkScalar y = pts[0].fY + tangent.fY * (distance + SkScalarHalf(temp));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000342 SkScalar halfWidth, halfHeight;
343 if (isXAxis) {
344 halfWidth = SkScalarHalf(temp);
345 halfHeight = SkScalarHalf(rec.getWidth());
346 } else {
347 halfWidth = SkScalarHalf(rec.getWidth());
348 halfHeight = SkScalarHalf(temp);
349 }
350 results->fLast.addRect(x - halfWidth, y - halfHeight,
351 x + halfWidth, y + halfHeight);
352 }
353
354 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000355 }
356
357 return true;
358}
359
Mike Reed76f70622017-05-23 23:00:14 -0400360SkPathEffect::DashType SkDashImpl::asADash(DashInfo* info) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000361 if (info) {
bsalomon49f085d2014-09-05 13:34:00 -0700362 if (info->fCount >= fCount && info->fIntervals) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000363 memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
364 }
365 info->fCount = fCount;
366 info->fPhase = fPhase;
367 }
368 return kDash_DashType;
369}
370
Mike Reed76f70622017-05-23 23:00:14 -0400371void SkDashImpl::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000372 buffer.writeScalar(fPhase);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000373 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374}
375
Mike Reed76f70622017-05-23 23:00:14 -0400376sk_sp<SkFlattenable> SkDashImpl::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700377 const SkScalar phase = buffer.readScalar();
378 uint32_t count = buffer.getArrayCount();
Adrienne Walker77e95f72018-04-24 16:41:41 -0700379
380 // Don't allocate gigantic buffers if there's not data for them.
Kevin Lubickdaebae92018-05-17 11:29:10 -0400381 if (!buffer.validateCanReadN<SkScalar>(count)) {
Adrienne Walker77e95f72018-04-24 16:41:41 -0700382 return nullptr;
383 }
384
reed9fa60da2014-08-21 07:59:51 -0700385 SkAutoSTArray<32, SkScalar> intervals(count);
386 if (buffer.readScalarArray(intervals.get(), count)) {
Mike Reed76f70622017-05-23 23:00:14 -0400387 return SkDashPathEffect::Make(intervals.get(), SkToInt(count), phase);
reed9fa60da2014-08-21 07:59:51 -0700388 }
halcanary96fcdcc2015-08-27 07:41:13 -0700389 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390}
robertphillips42dbfa82015-01-26 06:08:52 -0800391
reedca726ab2016-02-22 12:50:25 -0800392//////////////////////////////////////////////////////////////////////////////////////////////////
393
reeda4393342016-03-18 11:22:57 -0700394sk_sp<SkPathEffect> SkDashPathEffect::Make(const SkScalar intervals[], int count, SkScalar phase) {
caryclarkeb75c7d2016-03-18 06:04:26 -0700395 if (!SkDashPath::ValidDashPath(phase, intervals, count)) {
reedca726ab2016-02-22 12:50:25 -0800396 return nullptr;
397 }
Mike Reed76f70622017-05-23 23:00:14 -0400398 return sk_sp<SkPathEffect>(new SkDashImpl(intervals, count, phase));
reedca726ab2016-02-22 12:50:25 -0800399}