epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkDashPathEffect.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 10 | #include "SkDashImpl.h" |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 11 | #include "SkDashPathPriv.h" |
Cary Clark | 4dc5a45 | 2018-05-21 11:56:57 -0400 | [diff] [blame] | 12 | #include "SkFlattenablePriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
Hal Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 14 | #include "SkStrokeRec.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 15 | #include "SkTo.h" |
| 16 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 18 | #include <utility> |
| 19 | |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 20 | SkDashImpl::SkDashImpl(const SkScalar intervals[], int count, SkScalar phase) |
mtklein | 1c577cd | 2014-07-09 09:54:10 -0700 | [diff] [blame] | 21 | : fPhase(0) |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 22 | , fInitialDashLength(-1) |
mtklein | 1c577cd | 2014-07-09 09:54:10 -0700 | [diff] [blame] | 23 | , fInitialDashIndex(0) |
| 24 | , fIntervalLength(0) { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 25 | SkASSERT(intervals); |
cblume | eafe9d1 | 2016-09-26 04:22:59 -0700 | [diff] [blame] | 26 | SkASSERT(count > 1 && SkIsAlign2(count)); |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 27 | |
| 28 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count); |
| 29 | fCount = count; |
| 30 | for (int i = 0; i < count; i++) { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 31 | fIntervals[i] = intervals[i]; |
| 32 | } |
| 33 | |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 34 | // set the internal data members |
mtklein | 1c577cd | 2014-07-09 09:54:10 -0700 | [diff] [blame] | 35 | SkDashPath::CalcDashParameters(phase, fIntervals, fCount, |
| 36 | &fInitialDashLength, &fInitialDashIndex, &fIntervalLength, &fPhase); |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 39 | SkDashImpl::~SkDashImpl() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | sk_free(fIntervals); |
| 41 | } |
| 42 | |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 43 | bool SkDashImpl::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
| 44 | const SkRect* cullRect) const { |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 45 | return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals, fCount, |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 46 | fInitialDashLength, fInitialDashIndex, fIntervalLength); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | } |
| 48 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 49 | static 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 Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 55 | radius *= rec.getMiter(); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 56 | } |
| 57 | rect->outset(radius, radius); |
| 58 | } |
| 59 | |
mtklein | 88fd0fb | 2014-12-01 06:56:38 -0800 | [diff] [blame] | 60 | // Attempt to trim the line to minimally cover the cull rect (currently |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 61 | // only works for horizontal and vertical lines). |
| 62 | // Return true if processing should continue; false otherwise. |
| 63 | static bool cull_line(SkPoint* pts, const SkStrokeRec& rec, |
| 64 | const SkMatrix& ctm, const SkRect* cullRect, |
| 65 | const SkScalar intervalLength) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | if (nullptr == cullRect) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 67 | 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 | |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 74 | if ((dx && dy) || (!dx && !dy)) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 75 | 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 Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 98 | using std::swap; |
| 99 | swap(minX, maxX); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | SkASSERT(minX < maxX); |
reed | ccc12ed | 2014-12-12 12:48:50 -0800 | [diff] [blame] | 103 | if (maxX <= bounds.fLeft || minX >= bounds.fRight) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 104 | 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 Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 120 | using std::swap; |
| 121 | swap(minX, maxX); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 122 | } |
| 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 Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 131 | using std::swap; |
| 132 | swap(minY, maxY); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | SkASSERT(minY < maxY); |
reed | ccc12ed | 2014-12-12 12:48:50 -0800 | [diff] [blame] | 136 | if (maxY <= bounds.fTop || minY >= bounds.fBottom) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 137 | 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 Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame^] | 153 | using std::swap; |
| 154 | swap(minY, maxY); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 155 | } |
| 156 | pts[0].fY = minY; |
| 157 | pts[1].fY = maxY; |
| 158 | } |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 163 | // 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.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 166 | // allow paths to be returned |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 167 | bool SkDashImpl::asPoints(PointData* results, const SkPath& src, const SkStrokeRec& rec, |
| 168 | const SkMatrix& matrix, const SkRect* cullRect) const { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 169 | // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 170 | if (0 >= rec.getWidth()) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 174 | // 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.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 179 | if (fCount != 2 || |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 180 | !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) || |
| 181 | !SkScalarIsInt(fIntervals[0]) || |
| 182 | !SkScalarIsInt(fIntervals[1])) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 186 | SkPoint pts[2]; |
| 187 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 188 | if (!src.isLine(pts)) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 192 | // TODO: this test could be eased up to allow circles |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 193 | if (SkPaint::kButt_Cap != rec.getCap()) { |
| 194 | return false; |
| 195 | } |
| 196 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 197 | // TODO: this test could be eased up for circles. Rotations could be allowed. |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 198 | if (!matrix.rectStaysRect()) { |
| 199 | return false; |
| 200 | } |
| 201 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 202 | // 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.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 208 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 209 | 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; |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 218 | if (SkScalarNearlyEqual(SK_Scalar1, tangent.fX) || |
| 219 | SkScalarNearlyEqual(-SK_Scalar1, tangent.fX)) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 220 | results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth())); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 221 | } else if (SkScalarNearlyEqual(SK_Scalar1, tangent.fY) || |
| 222 | SkScalarNearlyEqual(-SK_Scalar1, tangent.fY)) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 223 | 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.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 230 | if (results) { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 231 | results->fFlags = 0; |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 232 | SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 233 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 234 | if (SkPaint::kRound_Cap == rec.getCap()) { |
| 235 | results->fFlags |= PointData::kCircles_PointFlag; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 236 | } |
| 237 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 238 | results->fNumPoints = 0; |
| 239 | SkScalar len2 = length; |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 240 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 241 | SkASSERT(len2 >= clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 242 | if (0 == fInitialDashIndex) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 243 | if (clampedInitialDashLength > 0) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 244 | if (clampedInitialDashLength >= fIntervals[0]) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 245 | ++results->fNumPoints; // partial first dash |
| 246 | } |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 247 | len2 -= clampedInitialDashLength; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 248 | } |
| 249 | len2 -= fIntervals[1]; // also skip first space |
| 250 | if (len2 < 0) { |
| 251 | len2 = 0; |
| 252 | } |
| 253 | } else { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 254 | len2 -= clampedInitialDashLength; // skip initial partial empty |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
lsalzman | f41ae2f | 2016-07-21 09:37:59 -0700 | [diff] [blame] | 257 | // 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.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 265 | results->fNumPoints += numMidPoints; |
| 266 | len2 -= numMidPoints * fIntervalLength; |
| 267 | bool partialLast = false; |
| 268 | if (len2 > 0) { |
| 269 | if (len2 < fIntervals[0]) { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 270 | partialLast = true; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 271 | } else { |
| 272 | ++numMidPoints; |
| 273 | ++results->fNumPoints; |
| 274 | } |
| 275 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 276 | |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 277 | results->fPoints = new SkPoint[results->fNumPoints]; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 278 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 279 | SkScalar distance = 0; |
| 280 | int curPt = 0; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 281 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 282 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 283 | SkASSERT(clampedInitialDashLength <= length); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 284 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 285 | if (0 == fInitialDashIndex) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 286 | if (clampedInitialDashLength > 0) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 287 | // partial first block |
| 288 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 289 | SkScalar x = pts[0].fX + tangent.fX * SkScalarHalf(clampedInitialDashLength); |
| 290 | SkScalar y = pts[0].fY + tangent.fY * SkScalarHalf(clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 291 | SkScalar halfWidth, halfHeight; |
| 292 | if (isXAxis) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 293 | halfWidth = SkScalarHalf(clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 294 | halfHeight = SkScalarHalf(rec.getWidth()); |
| 295 | } else { |
| 296 | halfWidth = SkScalarHalf(rec.getWidth()); |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 297 | halfHeight = SkScalarHalf(clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 298 | } |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 299 | if (clampedInitialDashLength < fIntervals[0]) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 300 | // 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.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 309 | distance += clampedInitialDashLength; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | distance += fIntervals[1]; // skip over the next blank block too |
| 313 | } else { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 314 | distance += clampedInitialDashLength; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 315 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 316 | } |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 317 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 318 | if (0 != numMidPoints) { |
| 319 | distance += SkScalarHalf(fIntervals[0]); |
| 320 | |
| 321 | for (int i = 0; i < numMidPoints; ++i) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 322 | SkScalar x = pts[0].fX + tangent.fX * distance; |
| 323 | SkScalar y = pts[0].fY + tangent.fY * distance; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 324 | |
| 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 Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 340 | SkScalar x = pts[0].fX + tangent.fX * (distance + SkScalarHalf(temp)); |
| 341 | SkScalar y = pts[0].fY + tangent.fY * (distance + SkScalarHalf(temp)); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 342 | 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.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return true; |
| 358 | } |
| 359 | |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 360 | SkPathEffect::DashType SkDashImpl::asADash(DashInfo* info) const { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 361 | if (info) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 362 | if (info->fCount >= fCount && info->fIntervals) { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 363 | memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar)); |
| 364 | } |
| 365 | info->fCount = fCount; |
| 366 | info->fPhase = fPhase; |
| 367 | } |
| 368 | return kDash_DashType; |
| 369 | } |
| 370 | |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 371 | void SkDashImpl::flatten(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 372 | buffer.writeScalar(fPhase); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 373 | buffer.writeScalarArray(fIntervals, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 376 | sk_sp<SkFlattenable> SkDashImpl::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 377 | const SkScalar phase = buffer.readScalar(); |
| 378 | uint32_t count = buffer.getArrayCount(); |
Adrienne Walker | 77e95f7 | 2018-04-24 16:41:41 -0700 | [diff] [blame] | 379 | |
| 380 | // Don't allocate gigantic buffers if there's not data for them. |
Kevin Lubick | daebae9 | 2018-05-17 11:29:10 -0400 | [diff] [blame] | 381 | if (!buffer.validateCanReadN<SkScalar>(count)) { |
Adrienne Walker | 77e95f7 | 2018-04-24 16:41:41 -0700 | [diff] [blame] | 382 | return nullptr; |
| 383 | } |
| 384 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 385 | SkAutoSTArray<32, SkScalar> intervals(count); |
| 386 | if (buffer.readScalarArray(intervals.get(), count)) { |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 387 | return SkDashPathEffect::Make(intervals.get(), SkToInt(count), phase); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 388 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 389 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 390 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 391 | |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 392 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 393 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 394 | sk_sp<SkPathEffect> SkDashPathEffect::Make(const SkScalar intervals[], int count, SkScalar phase) { |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 395 | if (!SkDashPath::ValidDashPath(phase, intervals, count)) { |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 396 | return nullptr; |
| 397 | } |
Mike Reed | 76f7062 | 2017-05-23 23:00:14 -0400 | [diff] [blame] | 398 | return sk_sp<SkPathEffect>(new SkDashImpl(intervals, count, phase)); |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 399 | } |