Debian Codegraph @

src/core/SkScan_Hairline.cpp

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