SkScan_Hairline.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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/core/SkPaint.h"
  8. #include "src/core/SkBlitter.h"
  9. #include "src/core/SkFDot6.h"
  10. #include "src/core/SkLineClipper.h"
  11. #include "src/core/SkMathPriv.h"
  12. #include "src/core/SkRasterClip.h"
  13. #include "src/core/SkScan.h"
  14. #include <utility>
  15. static void horiline(int x, int stopx, SkFixed fy, SkFixed dy,
  16. SkBlitter* blitter) {
  17. SkASSERT(x < stopx);
  18. do {
  19. blitter->blitH(x, fy >> 16, 1);
  20. fy += dy;
  21. } while (++x < stopx);
  22. }
  23. static void vertline(int y, int stopy, SkFixed fx, SkFixed dx,
  24. SkBlitter* blitter) {
  25. SkASSERT(y < stopy);
  26. do {
  27. blitter->blitH(fx >> 16, y, 1);
  28. fx += dx;
  29. } while (++y < stopy);
  30. }
  31. #ifdef SK_DEBUG
  32. static bool canConvertFDot6ToFixed(SkFDot6 x) {
  33. const int maxDot6 = SK_MaxS32 >> (16 - 6);
  34. return SkAbs32(x) <= maxDot6;
  35. }
  36. #endif
  37. void SkScan::HairLineRgn(const SkPoint array[], int arrayCount, const SkRegion* clip,
  38. SkBlitter* origBlitter) {
  39. SkBlitterClipper clipper;
  40. SkIRect clipR, ptsR;
  41. const SkScalar max = SkIntToScalar(32767);
  42. const SkRect fixedBounds = SkRect::MakeLTRB(-max, -max, max, max);
  43. SkRect clipBounds;
  44. if (clip) {
  45. clipBounds.set(clip->getBounds());
  46. }
  47. for (int i = 0; i < arrayCount - 1; ++i) {
  48. SkBlitter* blitter = origBlitter;
  49. SkPoint pts[2];
  50. // We have to pre-clip the line to fit in a SkFixed, so we just chop
  51. // the line. TODO find a way to actually draw beyond that range.
  52. if (!SkLineClipper::IntersectLine(&array[i], fixedBounds, pts)) {
  53. continue;
  54. }
  55. // Perform a clip in scalar space, so we catch huge values which might
  56. // be missed after we convert to SkFDot6 (overflow)
  57. if (clip && !SkLineClipper::IntersectLine(pts, clipBounds, pts)) {
  58. continue;
  59. }
  60. SkFDot6 x0 = SkScalarToFDot6(pts[0].fX);
  61. SkFDot6 y0 = SkScalarToFDot6(pts[0].fY);
  62. SkFDot6 x1 = SkScalarToFDot6(pts[1].fX);
  63. SkFDot6 y1 = SkScalarToFDot6(pts[1].fY);
  64. SkASSERT(canConvertFDot6ToFixed(x0));
  65. SkASSERT(canConvertFDot6ToFixed(y0));
  66. SkASSERT(canConvertFDot6ToFixed(x1));
  67. SkASSERT(canConvertFDot6ToFixed(y1));
  68. if (clip) {
  69. // now perform clipping again, as the rounding to dot6 can wiggle us
  70. // our rects are really dot6 rects, but since we've already used
  71. // lineclipper, we know they will fit in 32bits (26.6)
  72. const SkIRect& bounds = clip->getBounds();
  73. clipR.set(SkIntToFDot6(bounds.fLeft), SkIntToFDot6(bounds.fTop),
  74. SkIntToFDot6(bounds.fRight), SkIntToFDot6(bounds.fBottom));
  75. ptsR.set(x0, y0, x1, y1);
  76. ptsR.sort();
  77. // outset the right and bottom, to account for how hairlines are
  78. // actually drawn, which may hit the pixel to the right or below of
  79. // the coordinate
  80. ptsR.fRight += SK_FDot6One;
  81. ptsR.fBottom += SK_FDot6One;
  82. if (!SkIRect::Intersects(ptsR, clipR)) {
  83. continue;
  84. }
  85. if (!clip->isRect() || !clipR.contains(ptsR)) {
  86. blitter = clipper.apply(origBlitter, clip);
  87. }
  88. }
  89. SkFDot6 dx = x1 - x0;
  90. SkFDot6 dy = y1 - y0;
  91. if (SkAbs32(dx) > SkAbs32(dy)) { // mostly horizontal
  92. if (x0 > x1) { // we want to go left-to-right
  93. using std::swap;
  94. swap(x0, x1);
  95. swap(y0, y1);
  96. }
  97. int ix0 = SkFDot6Round(x0);
  98. int ix1 = SkFDot6Round(x1);
  99. if (ix0 == ix1) {// too short to draw
  100. continue;
  101. }
  102. SkFixed slope = SkFixedDiv(dy, dx);
  103. SkFixed startY = SkFDot6ToFixed(y0) + (slope * ((32 - x0) & 63) >> 6);
  104. horiline(ix0, ix1, startY, slope, blitter);
  105. } else { // mostly vertical
  106. if (y0 > y1) { // we want to go top-to-bottom
  107. using std::swap;
  108. swap(x0, x1);
  109. swap(y0, y1);
  110. }
  111. int iy0 = SkFDot6Round(y0);
  112. int iy1 = SkFDot6Round(y1);
  113. if (iy0 == iy1) { // too short to draw
  114. continue;
  115. }
  116. SkFixed slope = SkFixedDiv(dx, dy);
  117. SkFixed startX = SkFDot6ToFixed(x0) + (slope * ((32 - y0) & 63) >> 6);
  118. vertline(iy0, iy1, startX, slope, blitter);
  119. }
  120. }
  121. }
  122. // we don't just draw 4 lines, 'cause that can leave a gap in the bottom-right
  123. // and double-hit the top-left.
  124. void SkScan::HairRect(const SkRect& rect, const SkRasterClip& clip, SkBlitter* blitter) {
  125. SkAAClipBlitterWrapper wrapper;
  126. SkBlitterClipper clipper;
  127. // Create the enclosing bounds of the hairrect. i.e. we will stroke the interior of r.
  128. SkIRect r = SkIRect::MakeLTRB(SkScalarFloorToInt(rect.fLeft),
  129. SkScalarFloorToInt(rect.fTop),
  130. SkScalarFloorToInt(rect.fRight + 1),
  131. SkScalarFloorToInt(rect.fBottom + 1));
  132. // Note: r might be crazy big, if rect was huge, possibly getting pinned to max/min s32.
  133. // We need to trim it back to something reasonable before we can query its width etc.
  134. // since r.fRight - r.fLeft might wrap around to negative even if fRight > fLeft.
  135. //
  136. // We outset the clip bounds by 1 before intersecting, since r is being stroked and not filled
  137. // so we don't want to pin an edge of it to the clip. The intersect's job is mostly to just
  138. // get the actual edge values into a reasonable range (e.g. so width() can't overflow).
  139. if (!r.intersect(clip.getBounds().makeOutset(1, 1))) {
  140. return;
  141. }
  142. if (clip.quickReject(r)) {
  143. return;
  144. }
  145. if (!clip.quickContains(r)) {
  146. const SkRegion* clipRgn;
  147. if (clip.isBW()) {
  148. clipRgn = &clip.bwRgn();
  149. } else {
  150. wrapper.init(clip, blitter);
  151. clipRgn = &wrapper.getRgn();
  152. blitter = wrapper.getBlitter();
  153. }
  154. blitter = clipper.apply(blitter, clipRgn);
  155. }
  156. int width = r.width();
  157. int height = r.height();
  158. if ((width | height) == 0) {
  159. return;
  160. }
  161. if (width <= 2 || height <= 2) {
  162. blitter->blitRect(r.fLeft, r.fTop, width, height);
  163. return;
  164. }
  165. // if we get here, we know we have 4 segments to draw
  166. blitter->blitH(r.fLeft, r.fTop, width); // top
  167. blitter->blitRect(r.fLeft, r.fTop + 1, 1, height - 2); // left
  168. blitter->blitRect(r.fRight - 1, r.fTop + 1, 1, height - 2); // right
  169. blitter->blitH(r.fLeft, r.fBottom - 1, width); // bottom
  170. }
  171. ///////////////////////////////////////////////////////////////////////////////
  172. #include "include/core/SkPath.h"
  173. #include "include/private/SkNx.h"
  174. #include "src/core/SkGeometry.h"
  175. #define kMaxCubicSubdivideLevel 9
  176. #define kMaxQuadSubdivideLevel 5
  177. static uint32_t compute_int_quad_dist(const SkPoint pts[3]) {
  178. // compute the vector between the control point ([1]) and the middle of the
  179. // line connecting the start and end ([0] and [2])
  180. SkScalar dx = SkScalarHalf(pts[0].fX + pts[2].fX) - pts[1].fX;
  181. SkScalar dy = SkScalarHalf(pts[0].fY + pts[2].fY) - pts[1].fY;
  182. // we want everyone to be positive
  183. dx = SkScalarAbs(dx);
  184. dy = SkScalarAbs(dy);
  185. // convert to whole pixel values (use ceiling to be conservative).
  186. // assign to unsigned so we can safely add 1/2 of the smaller and still fit in
  187. // uint32_t, since SkScalarCeilToInt() returns 31 bits at most.
  188. uint32_t idx = SkScalarCeilToInt(dx);
  189. uint32_t idy = SkScalarCeilToInt(dy);
  190. // use the cheap approx for distance
  191. if (idx > idy) {
  192. return idx + (idy >> 1);
  193. } else {
  194. return idy + (idx >> 1);
  195. }
  196. }
  197. static void hair_quad(const SkPoint pts[3], const SkRegion* clip,
  198. SkBlitter* blitter, int level, SkScan::HairRgnProc lineproc) {
  199. SkASSERT(level <= kMaxQuadSubdivideLevel);
  200. SkQuadCoeff coeff(pts);
  201. const int lines = 1 << level;
  202. Sk2s t(0);
  203. Sk2s dt(SK_Scalar1 / lines);
  204. SkPoint tmp[(1 << kMaxQuadSubdivideLevel) + 1];
  205. SkASSERT((unsigned)lines < SK_ARRAY_COUNT(tmp));
  206. tmp[0] = pts[0];
  207. Sk2s A = coeff.fA;
  208. Sk2s B = coeff.fB;
  209. Sk2s C = coeff.fC;
  210. for (int i = 1; i < lines; ++i) {
  211. t = t + dt;
  212. ((A * t + B) * t + C).store(&tmp[i]);
  213. }
  214. tmp[lines] = pts[2];
  215. lineproc(tmp, lines + 1, clip, blitter);
  216. }
  217. static SkRect compute_nocheck_quad_bounds(const SkPoint pts[3]) {
  218. SkASSERT(SkScalarsAreFinite(&pts[0].fX, 6));
  219. Sk2s min = Sk2s::Load(pts);
  220. Sk2s max = min;
  221. for (int i = 1; i < 3; ++i) {
  222. Sk2s pair = Sk2s::Load(pts+i);
  223. min = Sk2s::Min(min, pair);
  224. max = Sk2s::Max(max, pair);
  225. }
  226. return { min[0], min[1], max[0], max[1] };
  227. }
  228. static bool is_inverted(const SkRect& r) {
  229. return r.fLeft > r.fRight || r.fTop > r.fBottom;
  230. }
  231. // Can't call SkRect::intersects, since it cares about empty, and we don't (since we tracking
  232. // something to be stroked, so empty can still draw something (e.g. horizontal line)
  233. static bool geometric_overlap(const SkRect& a, const SkRect& b) {
  234. SkASSERT(!is_inverted(a) && !is_inverted(b));
  235. return a.fLeft < b.fRight && b.fLeft < a.fRight &&
  236. a.fTop < b.fBottom && b.fTop < a.fBottom;
  237. }
  238. // Can't call SkRect::contains, since it cares about empty, and we don't (since we tracking
  239. // something to be stroked, so empty can still draw something (e.g. horizontal line)
  240. static bool geometric_contains(const SkRect& outer, const SkRect& inner) {
  241. SkASSERT(!is_inverted(outer) && !is_inverted(inner));
  242. return inner.fRight <= outer.fRight && inner.fLeft >= outer.fLeft &&
  243. inner.fBottom <= outer.fBottom && inner.fTop >= outer.fTop;
  244. }
  245. static inline void hairquad(const SkPoint pts[3], const SkRegion* clip, const SkRect* insetClip, const SkRect* outsetClip,
  246. SkBlitter* blitter, int level, SkScan::HairRgnProc lineproc) {
  247. if (insetClip) {
  248. SkASSERT(outsetClip);
  249. SkRect bounds = compute_nocheck_quad_bounds(pts);
  250. if (!geometric_overlap(*outsetClip, bounds)) {
  251. return;
  252. } else if (geometric_contains(*insetClip, bounds)) {
  253. clip = nullptr;
  254. }
  255. }
  256. hair_quad(pts, clip, blitter, level, lineproc);
  257. }
  258. static inline Sk2s abs(const Sk2s& value) {
  259. return Sk2s::Max(value, Sk2s(0)-value);
  260. }
  261. static inline SkScalar max_component(const Sk2s& value) {
  262. SkScalar components[2];
  263. value.store(components);
  264. return SkTMax(components[0], components[1]);
  265. }
  266. static inline int compute_cubic_segs(const SkPoint pts[4]) {
  267. Sk2s p0 = from_point(pts[0]);
  268. Sk2s p1 = from_point(pts[1]);
  269. Sk2s p2 = from_point(pts[2]);
  270. Sk2s p3 = from_point(pts[3]);
  271. const Sk2s oneThird(1.0f / 3.0f);
  272. const Sk2s twoThird(2.0f / 3.0f);
  273. Sk2s p13 = oneThird * p3 + twoThird * p0;
  274. Sk2s p23 = oneThird * p0 + twoThird * p3;
  275. SkScalar diff = max_component(Sk2s::Max(abs(p1 - p13), abs(p2 - p23)));
  276. SkScalar tol = SK_Scalar1 / 8;
  277. for (int i = 0; i < kMaxCubicSubdivideLevel; ++i) {
  278. if (diff < tol) {
  279. return 1 << i;
  280. }
  281. tol *= 4;
  282. }
  283. return 1 << kMaxCubicSubdivideLevel;
  284. }
  285. static bool lt_90(SkPoint p0, SkPoint pivot, SkPoint p2) {
  286. return SkVector::DotProduct(p0 - pivot, p2 - pivot) >= 0;
  287. }
  288. // The off-curve points are "inside" the limits of the on-curve pts
  289. static bool quick_cubic_niceness_check(const SkPoint pts[4]) {
  290. return lt_90(pts[1], pts[0], pts[3]) &&
  291. lt_90(pts[2], pts[0], pts[3]) &&
  292. lt_90(pts[1], pts[3], pts[0]) &&
  293. lt_90(pts[2], pts[3], pts[0]);
  294. }
  295. typedef SkNx<2, uint32_t> Sk2x32;
  296. static inline Sk2x32 sk2s_is_finite(const Sk2s& x) {
  297. const Sk2x32 exp_mask = Sk2x32(0xFF << 23);
  298. return (Sk2x32::Load(&x) & exp_mask) != exp_mask;
  299. }
  300. static void hair_cubic(const SkPoint pts[4], const SkRegion* clip, SkBlitter* blitter,
  301. SkScan::HairRgnProc lineproc) {
  302. const int lines = compute_cubic_segs(pts);
  303. SkASSERT(lines > 0);
  304. if (1 == lines) {
  305. SkPoint tmp[2] = { pts[0], pts[3] };
  306. lineproc(tmp, 2, clip, blitter);
  307. return;
  308. }
  309. SkCubicCoeff coeff(pts);
  310. const Sk2s dt(SK_Scalar1 / lines);
  311. Sk2s t(0);
  312. SkPoint tmp[(1 << kMaxCubicSubdivideLevel) + 1];
  313. SkASSERT((unsigned)lines < SK_ARRAY_COUNT(tmp));
  314. tmp[0] = pts[0];
  315. Sk2s A = coeff.fA;
  316. Sk2s B = coeff.fB;
  317. Sk2s C = coeff.fC;
  318. Sk2s D = coeff.fD;
  319. Sk2x32 is_finite(~0); // start out as true
  320. for (int i = 1; i < lines; ++i) {
  321. t = t + dt;
  322. Sk2s p = ((A * t + B) * t + C) * t + D;
  323. is_finite &= sk2s_is_finite(p);
  324. p.store(&tmp[i]);
  325. }
  326. if (is_finite.allTrue()) {
  327. tmp[lines] = pts[3];
  328. lineproc(tmp, lines + 1, clip, blitter);
  329. } // else some point(s) are non-finite, so don't draw
  330. }
  331. static SkRect compute_nocheck_cubic_bounds(const SkPoint pts[4]) {
  332. SkASSERT(SkScalarsAreFinite(&pts[0].fX, 8));
  333. Sk2s min = Sk2s::Load(pts);
  334. Sk2s max = min;
  335. for (int i = 1; i < 4; ++i) {
  336. Sk2s pair = Sk2s::Load(pts+i);
  337. min = Sk2s::Min(min, pair);
  338. max = Sk2s::Max(max, pair);
  339. }
  340. return { min[0], min[1], max[0], max[1] };
  341. }
  342. static inline void haircubic(const SkPoint pts[4], const SkRegion* clip, const SkRect* insetClip, const SkRect* outsetClip,
  343. SkBlitter* blitter, int level, SkScan::HairRgnProc lineproc) {
  344. if (insetClip) {
  345. SkASSERT(outsetClip);
  346. SkRect bounds = compute_nocheck_cubic_bounds(pts);
  347. if (!geometric_overlap(*outsetClip, bounds)) {
  348. return;
  349. } else if (geometric_contains(*insetClip, bounds)) {
  350. clip = nullptr;
  351. }
  352. }
  353. if (quick_cubic_niceness_check(pts)) {
  354. hair_cubic(pts, clip, blitter, lineproc);
  355. } else {
  356. SkPoint tmp[13];
  357. SkScalar tValues[3];
  358. int count = SkChopCubicAtMaxCurvature(pts, tmp, tValues);
  359. for (int i = 0; i < count; i++) {
  360. hair_cubic(&tmp[i * 3], clip, blitter, lineproc);
  361. }
  362. }
  363. }
  364. static int compute_quad_level(const SkPoint pts[3]) {
  365. uint32_t d = compute_int_quad_dist(pts);
  366. /* quadratics approach the line connecting their start and end points
  367. 4x closer with each subdivision, so we compute the number of
  368. subdivisions to be the minimum need to get that distance to be less
  369. than a pixel.
  370. */
  371. int level = (33 - SkCLZ(d)) >> 1;
  372. // sanity check on level (from the previous version)
  373. if (level > kMaxQuadSubdivideLevel) {
  374. level = kMaxQuadSubdivideLevel;
  375. }
  376. return level;
  377. }
  378. /* Extend the points in the direction of the starting or ending tangent by 1/2 unit to
  379. account for a round or square cap. If there's no distance between the end point and
  380. the control point, use the next control point to create a tangent. If the curve
  381. is degenerate, move the cap out 1/2 unit horizontally. */
  382. template <SkPaint::Cap capStyle>
  383. void extend_pts(SkPath::Verb prevVerb, SkPath::Verb nextVerb, SkPoint* pts, int ptCount) {
  384. SkASSERT(SkPaint::kSquare_Cap == capStyle || SkPaint::kRound_Cap == capStyle);
  385. // The area of a circle is PI*R*R. For a unit circle, R=1/2, and the cap covers half of that.
  386. const SkScalar capOutset = SkPaint::kSquare_Cap == capStyle ? 0.5f : SK_ScalarPI / 8;
  387. if (SkPath::kMove_Verb == prevVerb) {
  388. SkPoint* first = pts;
  389. SkPoint* ctrl = first;
  390. int controls = ptCount - 1;
  391. SkVector tangent;
  392. do {
  393. tangent = *first - *++ctrl;
  394. } while (tangent.isZero() && --controls > 0);
  395. if (tangent.isZero()) {
  396. tangent.set(1, 0);
  397. controls = ptCount - 1; // If all points are equal, move all but one
  398. } else {
  399. tangent.normalize();
  400. }
  401. do { // If the end point and control points are equal, loop to move them in tandem.
  402. first->fX += tangent.fX * capOutset;
  403. first->fY += tangent.fY * capOutset;
  404. ++first;
  405. } while (++controls < ptCount);
  406. }
  407. if (SkPath::kMove_Verb == nextVerb || SkPath::kDone_Verb == nextVerb
  408. || SkPath::kClose_Verb == nextVerb) {
  409. SkPoint* last = &pts[ptCount - 1];
  410. SkPoint* ctrl = last;
  411. int controls = ptCount - 1;
  412. SkVector tangent;
  413. do {
  414. tangent = *last - *--ctrl;
  415. } while (tangent.isZero() && --controls > 0);
  416. if (tangent.isZero()) {
  417. tangent.set(-1, 0);
  418. controls = ptCount - 1;
  419. } else {
  420. tangent.normalize();
  421. }
  422. do {
  423. last->fX += tangent.fX * capOutset;
  424. last->fY += tangent.fY * capOutset;
  425. --last;
  426. } while (++controls < ptCount);
  427. }
  428. }
  429. template <SkPaint::Cap capStyle>
  430. void hair_path(const SkPath& path, const SkRasterClip& rclip, SkBlitter* blitter,
  431. SkScan::HairRgnProc lineproc) {
  432. if (path.isEmpty()) {
  433. return;
  434. }
  435. SkAAClipBlitterWrapper wrap;
  436. const SkRegion* clip = nullptr;
  437. SkRect insetStorage, outsetStorage;
  438. const SkRect* insetClip = nullptr;
  439. const SkRect* outsetClip = nullptr;
  440. {
  441. const int capOut = SkPaint::kButt_Cap == capStyle ? 1 : 2;
  442. const SkIRect ibounds = path.getBounds().roundOut().makeOutset(capOut, capOut);
  443. if (rclip.quickReject(ibounds)) {
  444. return;
  445. }
  446. if (!rclip.quickContains(ibounds)) {
  447. if (rclip.isBW()) {
  448. clip = &rclip.bwRgn();
  449. } else {
  450. wrap.init(rclip, blitter);
  451. blitter = wrap.getBlitter();
  452. clip = &wrap.getRgn();
  453. }
  454. /*
  455. * We now cache two scalar rects, to use for culling per-segment (e.g. cubic).
  456. * Since we're hairlining, the "bounds" of the control points isn't necessairly the
  457. * limit of where a segment can draw (it might draw up to 1 pixel beyond in aa-hairs).
  458. *
  459. * Compute the pt-bounds per segment is easy, so we do that, and then inversely adjust
  460. * the culling bounds so we can just do a straight compare per segment.
  461. *
  462. * insetClip is use for quick-accept (i.e. the segment is not clipped), so we inset
  463. * it from the clip-bounds (since segment bounds can be off by 1).
  464. *
  465. * outsetClip is used for quick-reject (i.e. the segment is entirely outside), so we
  466. * outset it from the clip-bounds.
  467. */
  468. insetStorage.set(clip->getBounds());
  469. outsetStorage = insetStorage.makeOutset(1, 1);
  470. insetStorage.inset(1, 1);
  471. if (is_inverted(insetStorage)) {
  472. /*
  473. * our bounds checks assume the rects are never inverted. If insetting has
  474. * created that, we assume that the area is too small to safely perform a
  475. * quick-accept, so we just mark the rect as empty (so the quick-accept check
  476. * will always fail.
  477. */
  478. insetStorage.setEmpty(); // just so we don't pass an inverted rect
  479. }
  480. if (rclip.isRect()) {
  481. insetClip = &insetStorage;
  482. }
  483. outsetClip = &outsetStorage;
  484. }
  485. }
  486. SkPath::RawIter iter(path);
  487. SkPoint pts[4], firstPt, lastPt;
  488. SkPath::Verb verb, prevVerb;
  489. SkAutoConicToQuads converter;
  490. if (SkPaint::kButt_Cap != capStyle) {
  491. prevVerb = SkPath::kDone_Verb;
  492. }
  493. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
  494. switch (verb) {
  495. case SkPath::kMove_Verb:
  496. firstPt = lastPt = pts[0];
  497. break;
  498. case SkPath::kLine_Verb:
  499. if (SkPaint::kButt_Cap != capStyle) {
  500. extend_pts<capStyle>(prevVerb, iter.peek(), pts, 2);
  501. }
  502. lineproc(pts, 2, clip, blitter);
  503. lastPt = pts[1];
  504. break;
  505. case SkPath::kQuad_Verb:
  506. if (SkPaint::kButt_Cap != capStyle) {
  507. extend_pts<capStyle>(prevVerb, iter.peek(), pts, 3);
  508. }
  509. hairquad(pts, clip, insetClip, outsetClip, blitter, compute_quad_level(pts), lineproc);
  510. lastPt = pts[2];
  511. break;
  512. case SkPath::kConic_Verb: {
  513. if (SkPaint::kButt_Cap != capStyle) {
  514. extend_pts<capStyle>(prevVerb, iter.peek(), pts, 3);
  515. }
  516. // how close should the quads be to the original conic?
  517. const SkScalar tol = SK_Scalar1 / 4;
  518. const SkPoint* quadPts = converter.computeQuads(pts,
  519. iter.conicWeight(), tol);
  520. for (int i = 0; i < converter.countQuads(); ++i) {
  521. int level = compute_quad_level(quadPts);
  522. hairquad(quadPts, clip, insetClip, outsetClip, blitter, level, lineproc);
  523. quadPts += 2;
  524. }
  525. lastPt = pts[2];
  526. break;
  527. }
  528. case SkPath::kCubic_Verb: {
  529. if (SkPaint::kButt_Cap != capStyle) {
  530. extend_pts<capStyle>(prevVerb, iter.peek(), pts, 4);
  531. }
  532. haircubic(pts, clip, insetClip, outsetClip, blitter, kMaxCubicSubdivideLevel, lineproc);
  533. lastPt = pts[3];
  534. } break;
  535. case SkPath::kClose_Verb:
  536. pts[0] = lastPt;
  537. pts[1] = firstPt;
  538. if (SkPaint::kButt_Cap != capStyle && prevVerb == SkPath::kMove_Verb) {
  539. // cap moveTo/close to match svg expectations for degenerate segments
  540. extend_pts<capStyle>(prevVerb, iter.peek(), pts, 2);
  541. }
  542. lineproc(pts, 2, clip, blitter);
  543. break;
  544. case SkPath::kDone_Verb:
  545. break;
  546. }
  547. if (SkPaint::kButt_Cap != capStyle) {
  548. if (prevVerb == SkPath::kMove_Verb &&
  549. verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) {
  550. firstPt = pts[0]; // the curve moved the initial point, so close to it instead
  551. }
  552. prevVerb = verb;
  553. }
  554. }
  555. }
  556. void SkScan::HairPath(const SkPath& path, const SkRasterClip& clip, SkBlitter* blitter) {
  557. hair_path<SkPaint::kButt_Cap>(path, clip, blitter, SkScan::HairLineRgn);
  558. }
  559. void SkScan::AntiHairPath(const SkPath& path, const SkRasterClip& clip, SkBlitter* blitter) {
  560. hair_path<SkPaint::kButt_Cap>(path, clip, blitter, SkScan::AntiHairLineRgn);
  561. }
  562. void SkScan::HairSquarePath(const SkPath& path, const SkRasterClip& clip, SkBlitter* blitter) {
  563. hair_path<SkPaint::kSquare_Cap>(path, clip, blitter, SkScan::HairLineRgn);
  564. }
  565. void SkScan::AntiHairSquarePath(const SkPath& path, const SkRasterClip& clip, SkBlitter* blitter) {
  566. hair_path<SkPaint::kSquare_Cap>(path, clip, blitter, SkScan::AntiHairLineRgn);
  567. }
  568. void SkScan::HairRoundPath(const SkPath& path, const SkRasterClip& clip, SkBlitter* blitter) {
  569. hair_path<SkPaint::kRound_Cap>(path, clip, blitter, SkScan::HairLineRgn);
  570. }
  571. void SkScan::AntiHairRoundPath(const SkPath& path, const SkRasterClip& clip, SkBlitter* blitter) {
  572. hair_path<SkPaint::kRound_Cap>(path, clip, blitter, SkScan::AntiHairLineRgn);
  573. }
  574. ///////////////////////////////////////////////////////////////////////////////
  575. void SkScan::FrameRect(const SkRect& r, const SkPoint& strokeSize,
  576. const SkRasterClip& clip, SkBlitter* blitter) {
  577. SkASSERT(strokeSize.fX >= 0 && strokeSize.fY >= 0);
  578. if (strokeSize.fX < 0 || strokeSize.fY < 0) {
  579. return;
  580. }
  581. const SkScalar dx = strokeSize.fX;
  582. const SkScalar dy = strokeSize.fY;
  583. SkScalar rx = SkScalarHalf(dx);
  584. SkScalar ry = SkScalarHalf(dy);
  585. SkRect outer, tmp;
  586. outer.set(r.fLeft - rx, r.fTop - ry,
  587. r.fRight + rx, r.fBottom + ry);
  588. if (r.width() <= dx || r.height() <= dy) {
  589. SkScan::FillRect(outer, clip, blitter);
  590. return;
  591. }
  592. tmp.set(outer.fLeft, outer.fTop, outer.fRight, outer.fTop + dy);
  593. SkScan::FillRect(tmp, clip, blitter);
  594. tmp.fTop = outer.fBottom - dy;
  595. tmp.fBottom = outer.fBottom;
  596. SkScan::FillRect(tmp, clip, blitter);
  597. tmp.set(outer.fLeft, outer.fTop + dy, outer.fLeft + dx, outer.fBottom - dy);
  598. SkScan::FillRect(tmp, clip, blitter);
  599. tmp.fLeft = outer.fRight - dx;
  600. tmp.fRight = outer.fRight;
  601. SkScan::FillRect(tmp, clip, blitter);
  602. }
  603. void SkScan::HairLine(const SkPoint pts[], int count, const SkRasterClip& clip,
  604. SkBlitter* blitter) {
  605. if (clip.isBW()) {
  606. HairLineRgn(pts, count, &clip.bwRgn(), blitter);
  607. } else {
  608. const SkRegion* clipRgn = nullptr;
  609. SkRect r;
  610. r.set(pts, count);
  611. r.outset(SK_ScalarHalf, SK_ScalarHalf);
  612. SkAAClipBlitterWrapper wrap;
  613. if (!clip.quickContains(r.roundOut())) {
  614. wrap.init(clip, blitter);
  615. blitter = wrap.getBlitter();
  616. clipRgn = &wrap.getRgn();
  617. }
  618. HairLineRgn(pts, count, clipRgn, blitter);
  619. }
  620. }
  621. void SkScan::AntiHairLine(const SkPoint pts[], int count, const SkRasterClip& clip,
  622. SkBlitter* blitter) {
  623. if (clip.isBW()) {
  624. AntiHairLineRgn(pts, count, &clip.bwRgn(), blitter);
  625. } else {
  626. const SkRegion* clipRgn = nullptr;
  627. SkRect r;
  628. r.set(pts, count);
  629. SkAAClipBlitterWrapper wrap;
  630. if (!clip.quickContains(r.roundOut().makeOutset(1, 1))) {
  631. wrap.init(clip, blitter);
  632. blitter = wrap.getBlitter();
  633. clipRgn = &wrap.getRgn();
  634. }
  635. AntiHairLineRgn(pts, count, clipRgn, blitter);
  636. }
  637. }