blob: f9a56d0ff41fe129a6af8cb15c0dded767be211c [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"
egdaniela22ea182014-06-11 06:51:51 -07009
10#include "SkDashPathPriv.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
mtklein1c577cd2014-07-09 09:54:10 -070014SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, SkScalar phase)
15 : fPhase(0)
16 , fInitialDashLength(0)
17 , fInitialDashIndex(0)
18 , fIntervalLength(0) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000019 SkASSERT(intervals);
20 SkASSERT(count > 1 && SkAlign2(count) == count);
21
22 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
23 fCount = count;
24 for (int i = 0; i < count; i++) {
25 SkASSERT(intervals[i] >= 0);
26 fIntervals[i] = intervals[i];
27 }
28
egdaniela22ea182014-06-11 06:51:51 -070029 // set the internal data members
mtklein1c577cd2014-07-09 09:54:10 -070030 SkDashPath::CalcDashParameters(phase, fIntervals, fCount,
31 &fInitialDashLength, &fInitialDashIndex, &fIntervalLength, &fPhase);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000032}
33
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000034SkDashPathEffect::~SkDashPathEffect() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 sk_free(fIntervals);
36}
37
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000038bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000039 SkStrokeRec* rec, const SkRect* cullRect) const {
egdaniela22ea182014-06-11 06:51:51 -070040 return SkDashPath::FilterDashPath(dst, src, rec, cullRect, fIntervals, fCount,
41 fInitialDashLength, fInitialDashIndex, fIntervalLength);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042}
43
robertphillips@google.com629ab542012-11-28 17:18:11 +000044// Currently asPoints is more restrictive then it needs to be. In the future
45// we need to:
46// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +000047// allow paths to be returned
robertphillips@google.com629ab542012-11-28 17:18:11 +000048bool SkDashPathEffect::asPoints(PointData* results,
49 const SkPath& src,
50 const SkStrokeRec& rec,
reed@google.com4bbdeac2013-01-24 21:03:11 +000051 const SkMatrix& matrix,
52 const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +000053 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
54 if (fInitialDashLength < 0 || 0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +000055 return false;
56 }
57
robertphillips@google.com6d875572012-12-17 18:56:29 +000058 // TODO: this next test could be eased up. We could allow any number of
59 // intervals as long as all the ons match and all the offs match.
60 // Additionally, they do not necessarily need to be integers.
61 // We cannot allow arbitrary intervals since we want the returned points
62 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000063 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +000064 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
65 !SkScalarIsInt(fIntervals[0]) ||
66 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +000067 return false;
68 }
69
robertphillips@google.com629ab542012-11-28 17:18:11 +000070 SkPoint pts[2];
71
robertphillips@google.com6d875572012-12-17 18:56:29 +000072 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +000073 return false;
74 }
75
robertphillips@google.com6d875572012-12-17 18:56:29 +000076 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +000077 if (SkPaint::kButt_Cap != rec.getCap()) {
78 return false;
79 }
80
robertphillips@google.com6d875572012-12-17 18:56:29 +000081 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +000082 if (!matrix.rectStaysRect()) {
83 return false;
84 }
85
robertphillips@google.com6d875572012-12-17 18:56:29 +000086 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +000087
robertphillips@google.com6d875572012-12-17 18:56:29 +000088 SkVector tangent = pts[1] - pts[0];
89 if (tangent.isZero()) {
90 return false;
91 }
92
93 tangent.scale(SkScalarInvert(length));
94
95 // TODO: make this test for horizontal & vertical lines more robust
96 bool isXAxis = true;
97 if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) {
98 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
99 } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) {
100 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
101 isXAxis = false;
102 } else if (SkPaint::kRound_Cap != rec.getCap()) {
103 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000104 return false;
105 }
106
bsalomon49f085d2014-09-05 13:34:00 -0700107 if (results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000108 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000109 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000110
robertphillips@google.com6d875572012-12-17 18:56:29 +0000111 if (SkPaint::kRound_Cap == rec.getCap()) {
112 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000113 }
114
robertphillips@google.com6d875572012-12-17 18:56:29 +0000115 results->fNumPoints = 0;
116 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000117 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
118 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000119 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000120 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000121 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000122 ++results->fNumPoints; // partial first dash
123 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000124 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000125 }
126 len2 -= fIntervals[1]; // also skip first space
127 if (len2 < 0) {
128 len2 = 0;
129 }
130 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000131 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000132 }
133 }
134 int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength));
135 results->fNumPoints += numMidPoints;
136 len2 -= numMidPoints * fIntervalLength;
137 bool partialLast = false;
138 if (len2 > 0) {
139 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000140 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000141 } else {
142 ++numMidPoints;
143 ++results->fNumPoints;
144 }
145 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000146
robertphillips@google.com935ad022012-12-05 19:07:21 +0000147 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000148
robertphillips@google.com6d875572012-12-17 18:56:29 +0000149 SkScalar distance = 0;
150 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000151
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000152 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
153 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000154
robertphillips@google.com6d875572012-12-17 18:56:29 +0000155 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000156 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000157 // partial first block
158 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000159 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength));
160 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000161 SkScalar halfWidth, halfHeight;
162 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000163 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000164 halfHeight = SkScalarHalf(rec.getWidth());
165 } else {
166 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000167 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000168 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000169 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000170 // This one will not be like the others
171 results->fFirst.addRect(x - halfWidth, y - halfHeight,
172 x + halfWidth, y + halfHeight);
173 } else {
174 SkASSERT(curPt < results->fNumPoints);
175 results->fPoints[curPt].set(x, y);
176 ++curPt;
177 }
178
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000179 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000180 }
181
182 distance += fIntervals[1]; // skip over the next blank block too
183 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000184 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000185 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000186 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000187
robertphillips@google.com6d875572012-12-17 18:56:29 +0000188 if (0 != numMidPoints) {
189 distance += SkScalarHalf(fIntervals[0]);
190
191 for (int i = 0; i < numMidPoints; ++i) {
192 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance);
193 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance);
194
195 SkASSERT(curPt < results->fNumPoints);
196 results->fPoints[curPt].set(x, y);
197 ++curPt;
198
199 distance += fIntervalLength;
200 }
201
202 distance -= SkScalarHalf(fIntervals[0]);
203 }
204
205 if (partialLast) {
206 // partial final block
207 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
208 SkScalar temp = length - distance;
209 SkASSERT(temp < fIntervals[0]);
210 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp));
211 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp));
212 SkScalar halfWidth, halfHeight;
213 if (isXAxis) {
214 halfWidth = SkScalarHalf(temp);
215 halfHeight = SkScalarHalf(rec.getWidth());
216 } else {
217 halfWidth = SkScalarHalf(rec.getWidth());
218 halfHeight = SkScalarHalf(temp);
219 }
220 results->fLast.addRect(x - halfWidth, y - halfHeight,
221 x + halfWidth, y + halfHeight);
222 }
223
224 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000225 }
226
227 return true;
228}
229
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000230SkPathEffect::DashType SkDashPathEffect::asADash(DashInfo* info) const {
231 if (info) {
bsalomon49f085d2014-09-05 13:34:00 -0700232 if (info->fCount >= fCount && info->fIntervals) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000233 memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
234 }
235 info->fCount = fCount;
236 info->fPhase = fPhase;
237 }
238 return kDash_DashType;
239}
240
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000241void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000242 buffer.writeScalar(fPhase);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000243 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244}
245
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000246SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700247 const SkScalar phase = buffer.readScalar();
248 uint32_t count = buffer.getArrayCount();
249 SkAutoSTArray<32, SkScalar> intervals(count);
250 if (buffer.readScalarArray(intervals.get(), count)) {
251 return Create(intervals.get(), SkToInt(count), phase);
252 }
253 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254}
255
reed9fa60da2014-08-21 07:59:51 -0700256#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
mtklein1c577cd2014-07-09 09:54:10 -0700257SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer)
258 : INHERITED(buffer)
259 , fPhase(0)
260 , fInitialDashLength(0)
261 , fInitialDashIndex(0)
262 , fIntervalLength(0) {
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000263 bool useOldPic = buffer.isVersionLT(SkReadBuffer::kDashWritesPhaseIntervals_Version);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000264 if (useOldPic) {
265 fInitialDashIndex = buffer.readInt();
266 fInitialDashLength = buffer.readScalar();
267 fIntervalLength = buffer.readScalar();
268 buffer.readBool(); // Dummy for old ScalarToFit field
269 } else {
270 fPhase = buffer.readScalar();
271 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000273 fCount = buffer.getArrayCount();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000274 size_t allocSize = sizeof(SkScalar) * fCount;
275 if (buffer.validateAvailable(allocSize)) {
276 fIntervals = (SkScalar*)sk_malloc_throw(allocSize);
277 buffer.readScalarArray(fIntervals, fCount);
278 } else {
279 fIntervals = NULL;
280 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000281
282 if (useOldPic) {
283 fPhase = 0;
commit-bot@chromium.org6b3eebc2014-05-13 14:45:29 +0000284 if (fInitialDashLength != -1) { // Signal for bad dash interval
285 for (int i = 0; i < fInitialDashIndex; ++i) {
286 fPhase += fIntervals[i];
287 }
288 fPhase += fIntervals[fInitialDashIndex] - fInitialDashLength;
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000289 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000290 } else {
egdaniela22ea182014-06-11 06:51:51 -0700291 // set the internal data members, fPhase should have been between 0 and intervalLength
292 // when written to buffer so no need to adjust it
mtklein1c577cd2014-07-09 09:54:10 -0700293 SkDashPath::CalcDashParameters(fPhase, fIntervals, fCount,
294 &fInitialDashLength, &fInitialDashIndex, &fIntervalLength);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000295 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296}
reed9fa60da2014-08-21 07:59:51 -0700297#endif
298