SkDashPathEffect.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #include "include/effects/SkDashPathEffect.h"
  8. #include "include/core/SkStrokeRec.h"
  9. #include "include/private/SkTo.h"
  10. #include "src/core/SkReadBuffer.h"
  11. #include "src/core/SkWriteBuffer.h"
  12. #include "src/effects/SkDashImpl.h"
  13. #include "src/utils/SkDashPathPriv.h"
  14. #include <utility>
  15. SkDashImpl::SkDashImpl(const SkScalar intervals[], int count, SkScalar phase)
  16. : fPhase(0)
  17. , fInitialDashLength(-1)
  18. , fInitialDashIndex(0)
  19. , fIntervalLength(0) {
  20. SkASSERT(intervals);
  21. SkASSERT(count > 1 && SkIsAlign2(count));
  22. fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
  23. fCount = count;
  24. for (int i = 0; i < count; i++) {
  25. fIntervals[i] = intervals[i];
  26. }
  27. // set the internal data members
  28. SkDashPath::CalcDashParameters(phase, fIntervals, fCount,
  29. &fInitialDashLength, &fInitialDashIndex, &fIntervalLength, &fPhase);
  30. }
  31. SkDashImpl::~SkDashImpl() {
  32. sk_free(fIntervals);
  33. }
  34. bool SkDashImpl::onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
  35. const SkRect* cullRect) const {
  36. return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals, fCount,
  37. fInitialDashLength, fInitialDashIndex, fIntervalLength);
  38. }
  39. static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
  40. SkScalar radius = SkScalarHalf(rec.getWidth());
  41. if (0 == radius) {
  42. radius = SK_Scalar1; // hairlines
  43. }
  44. if (SkPaint::kMiter_Join == rec.getJoin()) {
  45. radius *= rec.getMiter();
  46. }
  47. rect->outset(radius, radius);
  48. }
  49. // Attempt to trim the line to minimally cover the cull rect (currently
  50. // only works for horizontal and vertical lines).
  51. // Return true if processing should continue; false otherwise.
  52. static bool cull_line(SkPoint* pts, const SkStrokeRec& rec,
  53. const SkMatrix& ctm, const SkRect* cullRect,
  54. const SkScalar intervalLength) {
  55. if (nullptr == cullRect) {
  56. SkASSERT(false); // Shouldn't ever occur in practice
  57. return false;
  58. }
  59. SkScalar dx = pts[1].x() - pts[0].x();
  60. SkScalar dy = pts[1].y() - pts[0].y();
  61. if ((dx && dy) || (!dx && !dy)) {
  62. return false;
  63. }
  64. SkRect bounds = *cullRect;
  65. outset_for_stroke(&bounds, rec);
  66. // cullRect is in device space while pts are in the local coordinate system
  67. // defined by the ctm. We want our answer in the local coordinate system.
  68. SkASSERT(ctm.rectStaysRect());
  69. SkMatrix inv;
  70. if (!ctm.invert(&inv)) {
  71. return false;
  72. }
  73. inv.mapRect(&bounds);
  74. if (dx) {
  75. SkASSERT(dx && !dy);
  76. SkScalar minX = pts[0].fX;
  77. SkScalar maxX = pts[1].fX;
  78. if (dx < 0) {
  79. using std::swap;
  80. swap(minX, maxX);
  81. }
  82. SkASSERT(minX < maxX);
  83. if (maxX <= bounds.fLeft || minX >= bounds.fRight) {
  84. return false;
  85. }
  86. // Now we actually perform the chop, removing the excess to the left and
  87. // right of the bounds (keeping our new line "in phase" with the dash,
  88. // hence the (mod intervalLength).
  89. if (minX < bounds.fLeft) {
  90. minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, intervalLength);
  91. }
  92. if (maxX > bounds.fRight) {
  93. maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, intervalLength);
  94. }
  95. SkASSERT(maxX > minX);
  96. if (dx < 0) {
  97. using std::swap;
  98. swap(minX, maxX);
  99. }
  100. pts[0].fX = minX;
  101. pts[1].fX = maxX;
  102. } else {
  103. SkASSERT(dy && !dx);
  104. SkScalar minY = pts[0].fY;
  105. SkScalar maxY = pts[1].fY;
  106. if (dy < 0) {
  107. using std::swap;
  108. swap(minY, maxY);
  109. }
  110. SkASSERT(minY < maxY);
  111. if (maxY <= bounds.fTop || minY >= bounds.fBottom) {
  112. return false;
  113. }
  114. // Now we actually perform the chop, removing the excess to the top and
  115. // bottom of the bounds (keeping our new line "in phase" with the dash,
  116. // hence the (mod intervalLength).
  117. if (minY < bounds.fTop) {
  118. minY = bounds.fTop - SkScalarMod(bounds.fTop - minY, intervalLength);
  119. }
  120. if (maxY > bounds.fBottom) {
  121. maxY = bounds.fBottom + SkScalarMod(maxY - bounds.fBottom, intervalLength);
  122. }
  123. SkASSERT(maxY > minY);
  124. if (dy < 0) {
  125. using std::swap;
  126. swap(minY, maxY);
  127. }
  128. pts[0].fY = minY;
  129. pts[1].fY = maxY;
  130. }
  131. return true;
  132. }
  133. // Currently asPoints is more restrictive then it needs to be. In the future
  134. // we need to:
  135. // allow kRound_Cap capping (could allow rotations in the matrix with this)
  136. // allow paths to be returned
  137. bool SkDashImpl::onAsPoints(PointData* results, const SkPath& src, const SkStrokeRec& rec,
  138. const SkMatrix& matrix, const SkRect* cullRect) const {
  139. // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
  140. if (0 >= rec.getWidth()) {
  141. return false;
  142. }
  143. // TODO: this next test could be eased up. We could allow any number of
  144. // intervals as long as all the ons match and all the offs match.
  145. // Additionally, they do not necessarily need to be integers.
  146. // We cannot allow arbitrary intervals since we want the returned points
  147. // to be uniformly sized.
  148. if (fCount != 2 ||
  149. !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
  150. !SkScalarIsInt(fIntervals[0]) ||
  151. !SkScalarIsInt(fIntervals[1])) {
  152. return false;
  153. }
  154. SkPoint pts[2];
  155. if (!src.isLine(pts)) {
  156. return false;
  157. }
  158. // TODO: this test could be eased up to allow circles
  159. if (SkPaint::kButt_Cap != rec.getCap()) {
  160. return false;
  161. }
  162. // TODO: this test could be eased up for circles. Rotations could be allowed.
  163. if (!matrix.rectStaysRect()) {
  164. return false;
  165. }
  166. // See if the line can be limited to something plausible.
  167. if (!cull_line(pts, rec, matrix, cullRect, fIntervalLength)) {
  168. return false;
  169. }
  170. SkScalar length = SkPoint::Distance(pts[1], pts[0]);
  171. SkVector tangent = pts[1] - pts[0];
  172. if (tangent.isZero()) {
  173. return false;
  174. }
  175. tangent.scale(SkScalarInvert(length));
  176. // TODO: make this test for horizontal & vertical lines more robust
  177. bool isXAxis = true;
  178. if (SkScalarNearlyEqual(SK_Scalar1, tangent.fX) ||
  179. SkScalarNearlyEqual(-SK_Scalar1, tangent.fX)) {
  180. results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
  181. } else if (SkScalarNearlyEqual(SK_Scalar1, tangent.fY) ||
  182. SkScalarNearlyEqual(-SK_Scalar1, tangent.fY)) {
  183. results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
  184. isXAxis = false;
  185. } else if (SkPaint::kRound_Cap != rec.getCap()) {
  186. // Angled lines don't have axis-aligned boxes.
  187. return false;
  188. }
  189. if (results) {
  190. results->fFlags = 0;
  191. SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
  192. if (SkPaint::kRound_Cap == rec.getCap()) {
  193. results->fFlags |= PointData::kCircles_PointFlag;
  194. }
  195. results->fNumPoints = 0;
  196. SkScalar len2 = length;
  197. if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
  198. SkASSERT(len2 >= clampedInitialDashLength);
  199. if (0 == fInitialDashIndex) {
  200. if (clampedInitialDashLength > 0) {
  201. if (clampedInitialDashLength >= fIntervals[0]) {
  202. ++results->fNumPoints; // partial first dash
  203. }
  204. len2 -= clampedInitialDashLength;
  205. }
  206. len2 -= fIntervals[1]; // also skip first space
  207. if (len2 < 0) {
  208. len2 = 0;
  209. }
  210. } else {
  211. len2 -= clampedInitialDashLength; // skip initial partial empty
  212. }
  213. }
  214. // Too many midpoints can cause results->fNumPoints to overflow or
  215. // otherwise cause the results->fPoints allocation below to OOM.
  216. // Cap it to a sane value.
  217. SkScalar numIntervals = len2 / fIntervalLength;
  218. if (!SkScalarIsFinite(numIntervals) || numIntervals > SkDashPath::kMaxDashCount) {
  219. return false;
  220. }
  221. int numMidPoints = SkScalarFloorToInt(numIntervals);
  222. results->fNumPoints += numMidPoints;
  223. len2 -= numMidPoints * fIntervalLength;
  224. bool partialLast = false;
  225. if (len2 > 0) {
  226. if (len2 < fIntervals[0]) {
  227. partialLast = true;
  228. } else {
  229. ++numMidPoints;
  230. ++results->fNumPoints;
  231. }
  232. }
  233. results->fPoints = new SkPoint[results->fNumPoints];
  234. SkScalar distance = 0;
  235. int curPt = 0;
  236. if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
  237. SkASSERT(clampedInitialDashLength <= length);
  238. if (0 == fInitialDashIndex) {
  239. if (clampedInitialDashLength > 0) {
  240. // partial first block
  241. SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
  242. SkScalar x = pts[0].fX + tangent.fX * SkScalarHalf(clampedInitialDashLength);
  243. SkScalar y = pts[0].fY + tangent.fY * SkScalarHalf(clampedInitialDashLength);
  244. SkScalar halfWidth, halfHeight;
  245. if (isXAxis) {
  246. halfWidth = SkScalarHalf(clampedInitialDashLength);
  247. halfHeight = SkScalarHalf(rec.getWidth());
  248. } else {
  249. halfWidth = SkScalarHalf(rec.getWidth());
  250. halfHeight = SkScalarHalf(clampedInitialDashLength);
  251. }
  252. if (clampedInitialDashLength < fIntervals[0]) {
  253. // This one will not be like the others
  254. results->fFirst.addRect(x - halfWidth, y - halfHeight,
  255. x + halfWidth, y + halfHeight);
  256. } else {
  257. SkASSERT(curPt < results->fNumPoints);
  258. results->fPoints[curPt].set(x, y);
  259. ++curPt;
  260. }
  261. distance += clampedInitialDashLength;
  262. }
  263. distance += fIntervals[1]; // skip over the next blank block too
  264. } else {
  265. distance += clampedInitialDashLength;
  266. }
  267. }
  268. if (0 != numMidPoints) {
  269. distance += SkScalarHalf(fIntervals[0]);
  270. for (int i = 0; i < numMidPoints; ++i) {
  271. SkScalar x = pts[0].fX + tangent.fX * distance;
  272. SkScalar y = pts[0].fY + tangent.fY * distance;
  273. SkASSERT(curPt < results->fNumPoints);
  274. results->fPoints[curPt].set(x, y);
  275. ++curPt;
  276. distance += fIntervalLength;
  277. }
  278. distance -= SkScalarHalf(fIntervals[0]);
  279. }
  280. if (partialLast) {
  281. // partial final block
  282. SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
  283. SkScalar temp = length - distance;
  284. SkASSERT(temp < fIntervals[0]);
  285. SkScalar x = pts[0].fX + tangent.fX * (distance + SkScalarHalf(temp));
  286. SkScalar y = pts[0].fY + tangent.fY * (distance + SkScalarHalf(temp));
  287. SkScalar halfWidth, halfHeight;
  288. if (isXAxis) {
  289. halfWidth = SkScalarHalf(temp);
  290. halfHeight = SkScalarHalf(rec.getWidth());
  291. } else {
  292. halfWidth = SkScalarHalf(rec.getWidth());
  293. halfHeight = SkScalarHalf(temp);
  294. }
  295. results->fLast.addRect(x - halfWidth, y - halfHeight,
  296. x + halfWidth, y + halfHeight);
  297. }
  298. SkASSERT(curPt == results->fNumPoints);
  299. }
  300. return true;
  301. }
  302. SkPathEffect::DashType SkDashImpl::onAsADash(DashInfo* info) const {
  303. if (info) {
  304. if (info->fCount >= fCount && info->fIntervals) {
  305. memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
  306. }
  307. info->fCount = fCount;
  308. info->fPhase = fPhase;
  309. }
  310. return kDash_DashType;
  311. }
  312. void SkDashImpl::flatten(SkWriteBuffer& buffer) const {
  313. buffer.writeScalar(fPhase);
  314. buffer.writeScalarArray(fIntervals, fCount);
  315. }
  316. sk_sp<SkFlattenable> SkDashImpl::CreateProc(SkReadBuffer& buffer) {
  317. const SkScalar phase = buffer.readScalar();
  318. uint32_t count = buffer.getArrayCount();
  319. // Don't allocate gigantic buffers if there's not data for them.
  320. if (!buffer.validateCanReadN<SkScalar>(count)) {
  321. return nullptr;
  322. }
  323. SkAutoSTArray<32, SkScalar> intervals(count);
  324. if (buffer.readScalarArray(intervals.get(), count)) {
  325. return SkDashPathEffect::Make(intervals.get(), SkToInt(count), phase);
  326. }
  327. return nullptr;
  328. }
  329. //////////////////////////////////////////////////////////////////////////////////////////////////
  330. sk_sp<SkPathEffect> SkDashPathEffect::Make(const SkScalar intervals[], int count, SkScalar phase) {
  331. if (!SkDashPath::ValidDashPath(phase, intervals, count)) {
  332. return nullptr;
  333. }
  334. return sk_sp<SkPathEffect>(new SkDashImpl(intervals, count, phase));
  335. }