Debian Codegraph @

src/core/SkEdgeClipper.cpp

raw
1 /*
2 * Copyright 2009 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 "src/core/SkEdgeClipper.h"
9
10 #include "include/core/SkRect.h"
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkMacros.h"
13 #include "src/core/SkGeometry.h"
14 #include "src/core/SkLineClipper.h"
15 #include "src/core/SkPathPriv.h"
16
17 #include <algorithm>
18 #include <cstring>
19
20 static bool quick_reject(const SkRect& bounds, const SkRect& clip) {
21 return bounds.fTop >= clip.fBottom || bounds.fBottom <= clip.fTop;
22 }
23
24 static inline void clamp_le(SkScalar& value, SkScalar max) {
25 if (value > max) {
26 value = max;
27 }
28 }
29
30 static inline void clamp_ge(SkScalar& value, SkScalar min) {
31 if (value < min) {
32 value = min;
33 }
34 }
35
36 /* src[] must be monotonic in Y. This routine copies src into dst, and sorts
37 it to be increasing in Y. If it had to reverse the order of the points,
38 it returns true, otherwise it returns false
39 */
40 static bool sort_increasing_Y(SkPoint dst[], const SkPoint src[], int count) {
41 // we need the data to be monotonically increasing in Y
42 if (src[0].fY > src[count - 1].fY) {
43 for (int i = 0; i < count; i++) {
44 dst[i] = src[count - i - 1];
45 }
46 return true;
47 } else {
48 memcpy(dst, src, count * sizeof(SkPoint));
49 return false;
50 }
51 }
52
53 bool SkEdgeClipper::clipLine(SkPoint p0, SkPoint p1, const SkRect& clip) {
54 fCurrPoint = fPoints;
55 fCurrVerb = fVerbs;
56
57 SkPoint lines[SkLineClipper::kMaxPoints];
58 const SkPoint pts[] = { p0, p1 };
59 int lineCount = SkLineClipper::ClipLine(pts, clip, lines, fCanCullToTheRight);
60 for (int i = 0; i < lineCount; i++) {
61 this->appendLine(lines[i], lines[i + 1]);
62 }
63
64 fCurrVerbStop = fCurrVerb;
65 fCurrPoint = fPoints;
66 fCurrVerb = fVerbs;
67 return fCurrVerbStop != fCurrVerb;
68 }
69
70 ///////////////////////////////////////////////////////////////////////////////
71
72 static bool chopMonoQuadAt(SkScalar c0, SkScalar c1, SkScalar c2,
73 SkScalar target, SkScalar* t) {
74 /* Solve F(t) = y where F(t) := [0](1-t)^2 + 2[1]t(1-t) + [2]t^2
75 * We solve for t, using quadratic equation, hence we have to rearrange
76 * our cooefficents to look like At^2 + Bt + C
77 */
78 SkScalar A = c0 - c1 - c1 + c2;
79 SkScalar B = 2*(c1 - c0);
80 SkScalar C = c0 - target;
81
82 SkScalar roots[2]; // we only expect one, but make room for 2 for safety
83 int count = SkFindUnitQuadRoots(A, B, C, roots);
84 if (count) {
85 *t = roots[0];
86 return true;
87 }
88 return false;
89 }
90
91 static bool chopMonoQuadAtY(SkPoint pts[3], SkScalar y, SkScalar* t) {
92 return chopMonoQuadAt(pts[0].fY, pts[1].fY, pts[2].fY, y, t);
93 }
94
95 static bool chopMonoQuadAtX(SkPoint pts[3], SkScalar x, SkScalar* t) {
96 return chopMonoQuadAt(pts[0].fX, pts[1].fX, pts[2].fX, x, t);
97 }
98
99 // Modify pts[] in place so that it is clipped in Y to the clip rect
100 static void chop_quad_in_Y(SkPoint pts[3], const SkRect& clip) {
101 SkScalar t;
102 SkPoint tmp[5]; // for SkChopQuadAt
103
104 // are we partially above
105 if (pts[0].fY < clip.fTop) {
106 if (chopMonoQuadAtY(pts, clip.fTop, &t)) {
107 // take the 2nd chopped quad
108 SkChopQuadAt(pts, tmp, t);
109 // clamp to clean up imprecise numerics in the chop
110 tmp[2].fY = clip.fTop;
111 clamp_ge(tmp[3].fY, clip.fTop);
112
113 pts[0] = tmp[2];
114 pts[1] = tmp[3];
115 } else {
116 // if chopMonoQuadAtY failed, then we may have hit inexact numerics
117 // so we just clamp against the top
118 for (int i = 0; i < 3; i++) {
119 if (pts[i].fY < clip.fTop) {
120 pts[i].fY = clip.fTop;
121 }
122 }
123 }
124 }
125
126 // are we partially below
127 if (pts[2].fY > clip.fBottom) {
128 if (chopMonoQuadAtY(pts, clip.fBottom, &t)) {
129 SkChopQuadAt(pts, tmp, t);
130 // clamp to clean up imprecise numerics in the chop
131 clamp_le(tmp[1].fY, clip.fBottom);
132 tmp[2].fY = clip.fBottom;
133
134 pts[1] = tmp[1];
135 pts[2] = tmp[2];
136 } else {
137 // if chopMonoQuadAtY failed, then we may have hit inexact numerics
138 // so we just clamp against the bottom
139 for (int i = 0; i < 3; i++) {
140 if (pts[i].fY > clip.fBottom) {
141 pts[i].fY = clip.fBottom;
142 }
143 }
144 }
145 }
146 }
147
148 // srcPts[] must be monotonic in X and Y
149 void SkEdgeClipper::clipMonoQuad(const SkPoint srcPts[3], const SkRect& clip) {
150 SkPoint pts[3];
151 bool reverse = sort_increasing_Y(pts, srcPts, 3);
152
153 // are we completely above or below
154 if (pts[2].fY <= clip.fTop || pts[0].fY >= clip.fBottom) {
155 return;
156 }
157
158 // Now chop so that pts is contained within clip in Y
159 chop_quad_in_Y(pts, clip);
160
161 if (pts[0].fX > pts[2].fX) {
162 using std::swap;
163 swap(pts[0], pts[2]);
164 reverse = !reverse;
165 }
166 SkASSERT(pts[0].fX <= pts[1].fX);
167 SkASSERT(pts[1].fX <= pts[2].fX);
168
169 // Now chop in X has needed, and record the segments
170
171 if (pts[2].fX <= clip.fLeft) { // wholly to the left
172 this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse);
173 return;
174 }
175 if (pts[0].fX >= clip.fRight) { // wholly to the right
176 if (!this->canCullToTheRight()) {
177 this->appendVLine(clip.fRight, pts[0].fY, pts[2].fY, reverse);
178 }
179 return;
180 }
181
182 SkScalar t;
183 SkPoint tmp[5]; // for SkChopQuadAt
184
185 // are we partially to the left
186 if (pts[0].fX < clip.fLeft) {
187 if (chopMonoQuadAtX(pts, clip.fLeft, &t)) {
188 SkChopQuadAt(pts, tmp, t);
189 this->appendVLine(clip.fLeft, tmp[0].fY, tmp[2].fY, reverse);
190 // clamp to clean up imprecise numerics in the chop
191 tmp[2].fX = clip.fLeft;
192 clamp_ge(tmp[3].fX, clip.fLeft);
193
194 pts[0] = tmp[2];
195 pts[1] = tmp[3];
196 } else {
197 // if chopMonoQuadAtY failed, then we may have hit inexact numerics
198 // so we just clamp against the left
199 this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse);
200 return;
201 }
202 }
203
204 // are we partially to the right
205 if (pts[2].fX > clip.fRight) {
206 if (chopMonoQuadAtX(pts, clip.fRight, &t)) {
207 SkChopQuadAt(pts, tmp, t);
208 // clamp to clean up imprecise numerics in the chop
209 clamp_le(tmp[1].fX, clip.fRight);
210 tmp[2].fX = clip.fRight;
211
212 this->appendQuad(tmp, reverse);
213 this->appendVLine(clip.fRight, tmp[2].fY, tmp[4].fY, reverse);
214 } else {
215 // if chopMonoQuadAtY failed, then we may have hit inexact numerics
216 // so we just clamp against the right
217 pts[1].fX = std::min(pts[1].fX, clip.fRight);
218 pts[2].fX = std::min(pts[2].fX, clip.fRight);
219 this->appendQuad(pts, reverse);
220 }
221 } else { // wholly inside the clip
222 this->appendQuad(pts, reverse);
223 }
224 }
225
226 bool SkEdgeClipper::clipQuad(const SkPoint srcPts[3], const SkRect& clip) {
227 fCurrPoint = fPoints;
228 fCurrVerb = fVerbs;
229
230 const SkRect bounds = SkRect::BoundsOrEmpty({srcPts, 3});
231
232 if (!quick_reject(bounds, clip)) {
233 SkPoint monoY[5];
234 int countY = SkChopQuadAtYExtrema(srcPts, monoY);
235 for (int y = 0; y <= countY; y++) {
236 SkPoint monoX[5];
237 int countX = SkChopQuadAtXExtrema(&monoY[y * 2], monoX);
238 for (int x = 0; x <= countX; x++) {
239 this->clipMonoQuad(&monoX[x * 2], clip);
240 SkASSERT(fCurrVerb - fVerbs < kMaxVerbs);
241 SkASSERT(fCurrPoint - fPoints <= kMaxPoints);
242 }
243 }
244 }
245
246 fCurrVerbStop = fCurrVerb;
247 fCurrPoint = fPoints;
248 fCurrVerb = fVerbs;
249 return fCurrVerbStop != fCurrVerb;
250 }
251
252 ///////////////////////////////////////////////////////////////////////////////
253
254 static SkScalar mono_cubic_closestT(const SkScalar src[], SkScalar x) {
255 SkScalar t = 0.5f;
256 SkScalar lastT;
257 SkScalar bestT SK_INIT_TO_AVOID_WARNING;
258 SkScalar step = 0.25f;
259 SkScalar D = src[0];
260 SkScalar A = src[6] + 3*(src[2] - src[4]) - D;
261 SkScalar B = 3*(src[4] - src[2] - src[2] + D);
262 SkScalar C = 3*(src[2] - D);
263 x -= D;
264 SkScalar closest = SK_ScalarMax;
265 do {
266 SkScalar loc = ((A * t + B) * t + C) * t;
267 SkScalar dist = SkScalarAbs(loc - x);
268 if (closest > dist) {
269 closest = dist;
270 bestT = t;
271 }
272 lastT = t;
273 t += loc < x ? step : -step;
274 step *= 0.5f;
275 } while (closest > 0.25f && lastT != t);
276 return bestT;
277 }
278
279 static void chop_mono_cubic_at_y(SkPoint src[4], SkScalar y, SkPoint dst[7]) {
280 if (SkChopMonoCubicAtY(src, y, dst)) {
281 return;
282 }
283 SkChopCubicAt(src, dst, mono_cubic_closestT(&src->fY, y));
284 }
285
286 // Modify pts[] in place so that it is clipped in Y to the clip rect
287 static void chop_cubic_in_Y(SkPoint pts[4], const SkRect& clip) {
288
289 // are we partially above
290 if (pts[0].fY < clip.fTop) {
291 SkPoint tmp[7];
292 chop_mono_cubic_at_y(pts, clip.fTop, tmp);
293
294 /*
295 * For a large range in the points, we can do a poor job of chopping, such that the t
296 * we computed resulted in the lower cubic still being partly above the clip.
297 *
298 * If just the first or first 2 Y values are above the fTop, we can just smash them
299 * down. If the first 3 Ys are above fTop, we can't smash all 3, as that can really
300 * distort the cubic. In this case, we take the first output (tmp[3..6] and treat it as
301 * a guess, and re-chop against fTop. Then we fall through to checking if we need to
302 * smash the first 1 or 2 Y values.
303 */
304 if (tmp[3].fY < clip.fTop && tmp[4].fY < clip.fTop && tmp[5].fY < clip.fTop) {
305 SkPoint tmp2[4];
306 memcpy(tmp2, &tmp[3].fX, 4 * sizeof(SkPoint));
307 chop_mono_cubic_at_y(tmp2, clip.fTop, tmp);
308 }
309
310 // tmp[3, 4].fY should all be to the below clip.fTop.
311 // Since we can't trust the numerics of the chopper, we force those conditions now
312 tmp[3].fY = clip.fTop;
313 clamp_ge(tmp[4].fY, clip.fTop);
314
315 pts[0] = tmp[3];
316 pts[1] = tmp[4];
317 pts[2] = tmp[5];
318 }
319
320 // are we partially below
321 if (pts[3].fY > clip.fBottom) {
322 SkPoint tmp[7];
323 chop_mono_cubic_at_y(pts, clip.fBottom, tmp);
324 tmp[3].fY = clip.fBottom;
325 clamp_le(tmp[2].fY, clip.fBottom);
326
327 pts[1] = tmp[1];
328 pts[2] = tmp[2];
329 pts[3] = tmp[3];
330 }
331 }
332
333 static void chop_mono_cubic_at_x(SkPoint src[4], SkScalar x, SkPoint dst[7]) {
334 if (SkChopMonoCubicAtX(src, x, dst)) {
335 return;
336 }
337 SkChopCubicAt(src, dst, mono_cubic_closestT(&src->fX, x));
338 }
339
340 // srcPts[] must be monotonic in X and Y
341 void SkEdgeClipper::clipMonoCubic(const SkPoint src[4], const SkRect& clip) {
342 SkPoint pts[4];
343 bool reverse = sort_increasing_Y(pts, src, 4);
344
345 // are we completely above or below
346 if (pts[3].fY <= clip.fTop || pts[0].fY >= clip.fBottom) {
347 return;
348 }
349
350 // Now chop so that pts is contained within clip in Y
351 chop_cubic_in_Y(pts, clip);
352
353 if (pts[0].fX > pts[3].fX) {
354 using std::swap;
355 swap(pts[0], pts[3]);
356 swap(pts[1], pts[2]);
357 reverse = !reverse;
358 }
359
360 // Now chop in X has needed, and record the segments
361
362 if (pts[3].fX <= clip.fLeft) { // wholly to the left
363 this->appendVLine(clip.fLeft, pts[0].fY, pts[3].fY, reverse);
364 return;
365 }
366 if (pts[0].fX >= clip.fRight) { // wholly to the right
367 if (!this->canCullToTheRight()) {
368 this->appendVLine(clip.fRight, pts[0].fY, pts[3].fY, reverse);
369 }
370 return;
371 }
372
373 // are we partially to the left
374 if (pts[0].fX < clip.fLeft) {
375 SkPoint tmp[7];
376 chop_mono_cubic_at_x(pts, clip.fLeft, tmp);
377 this->appendVLine(clip.fLeft, tmp[0].fY, tmp[3].fY, reverse);
378
379 // tmp[3, 4].fX should all be to the right of clip.fLeft.
380 // Since we can't trust the numerics of
381 // the chopper, we force those conditions now
382 tmp[3].fX = clip.fLeft;
383 clamp_ge(tmp[4].fX, clip.fLeft);
384
385 pts[0] = tmp[3];
386 pts[1] = tmp[4];
387 pts[2] = tmp[5];
388 }
389
390 // are we partially to the right
391 if (pts[3].fX > clip.fRight) {
392 SkPoint tmp[7];
393 chop_mono_cubic_at_x(pts, clip.fRight, tmp);
394 tmp[3].fX = clip.fRight;
395 clamp_le(tmp[2].fX, clip.fRight);
396
397 this->appendCubic(tmp, reverse);
398 this->appendVLine(clip.fRight, tmp[3].fY, tmp[6].fY, reverse);
399 } else { // wholly inside the clip
400 this->appendCubic(pts, reverse);
401 }
402 }
403
404 static SkRect compute_cubic_bounds(const SkPoint pts[4]) {
405 return SkRect::BoundsOrEmpty({pts, 4});
406 }
407
408 static bool too_big_for_reliable_float_math(const SkRect& r) {
409 // limit set as the largest float value for which we can still reliably compute things like
410 // - chopping at XY extrema
411 // - chopping at Y or X values for clipping
412 //
413 // Current value chosen just by experiment. Larger (and still succeeds) is always better.
414 //
415 const SkScalar limit = 1 << 22;
416 return r.fLeft < -limit || r.fTop < -limit || r.fRight > limit || r.fBottom > limit;
417 }
418
419 bool SkEdgeClipper::clipCubic(const SkPoint srcPts[4], const SkRect& clip) {
420 fCurrPoint = fPoints;
421 fCurrVerb = fVerbs;
422
423 const SkRect bounds = compute_cubic_bounds(srcPts);
424 // check if we're clipped out vertically
425 if (bounds.fBottom > clip.fTop && bounds.fTop < clip.fBottom) {
426 if (too_big_for_reliable_float_math(bounds)) {
427 // can't safely clip the cubic, so we give up and draw a line (which we can safely clip)
428 //
429 // If we rewrote chopcubicat*extrema and chopmonocubic using doubles, we could very
430 // likely always handle the cubic safely, but (it seems) at a big loss in speed, so
431 // we'd only want to take that alternate impl if needed. Perhaps a TODO to try it.
432 //
433 return this->clipLine(srcPts[0], srcPts[3], clip);
434 } else {
435 SkPoint monoY[10];
436 int countY = SkChopCubicAtYExtrema(srcPts, monoY);
437 for (int y = 0; y <= countY; y++) {
438 SkPoint monoX[10];
439 int countX = SkChopCubicAtXExtrema(&monoY[y * 3], monoX);
440 for (int x = 0; x <= countX; x++) {
441 this->clipMonoCubic(&monoX[x * 3], clip);
442 SkASSERT(fCurrVerb - fVerbs < kMaxVerbs);
443 SkASSERT(fCurrPoint - fPoints <= kMaxPoints);
444 }
445 }
446 }
447 }
448
449 fCurrVerbStop = fCurrVerb;
450 fCurrPoint = fPoints;
451 fCurrVerb = fVerbs;
452 return fCurrVerbStop != fCurrVerb;
453 }
454
455 ///////////////////////////////////////////////////////////////////////////////
456
457 void SkEdgeClipper::appendLine(SkPoint p0, SkPoint p1) {
458 *fCurrVerb++ = SkPathVerb::kLine;
459 fCurrPoint[0] = p0;
460 fCurrPoint[1] = p1;
461 fCurrPoint += 2;
462 }
463
464 void SkEdgeClipper::appendVLine(SkScalar x, SkScalar y0, SkScalar y1, bool reverse) {
465 *fCurrVerb++ = SkPathVerb::kLine;
466
467 if (reverse) {
468 using std::swap;
469 swap(y0, y1);
470 }
471 fCurrPoint[0].set(x, y0);
472 fCurrPoint[1].set(x, y1);
473 fCurrPoint += 2;
474 }
475
476 void SkEdgeClipper::appendQuad(const SkPoint pts[3], bool reverse) {
477 *fCurrVerb++ = SkPathVerb::kQuad;
478
479 if (reverse) {
480 fCurrPoint[0] = pts[2];
481 fCurrPoint[2] = pts[0];
482 } else {
483 fCurrPoint[0] = pts[0];
484 fCurrPoint[2] = pts[2];
485 }
486 fCurrPoint[1] = pts[1];
487 fCurrPoint += 3;
488 }
489
490 void SkEdgeClipper::appendCubic(const SkPoint pts[4], bool reverse) {
491 *fCurrVerb++ = SkPathVerb::kCubic;
492
493 if (reverse) {
494 for (int i = 0; i < 4; i++) {
495 fCurrPoint[i] = pts[3 - i];
496 }
497 } else {
498 memcpy(fCurrPoint, pts, 4 * sizeof(SkPoint));
499 }
500 fCurrPoint += 4;
501 }
502
503 std::optional<SkPathVerb> SkEdgeClipper::next(SkPoint pts[]) {
504 SkASSERT(fCurrVerb <= fCurrVerbStop);
505 if (fCurrVerb >= fCurrVerbStop) {
506 return {};
507 }
508
509 auto verb = *fCurrVerb++;
510 switch (verb) {
511 case SkPathVerb::kLine:
512 memcpy(pts, fCurrPoint, 2 * sizeof(SkPoint));
513 fCurrPoint += 2;
514 break;
515 case SkPathVerb::kQuad:
516 memcpy(pts, fCurrPoint, 3 * sizeof(SkPoint));
517 fCurrPoint += 3;
518 break;
519 case SkPathVerb::kCubic:
520 memcpy(pts, fCurrPoint, 4 * sizeof(SkPoint));
521 fCurrPoint += 4;
522 break;
523 default:
524 SkDEBUGFAIL("unexpected verb in quadclippper2 iter");
525 break;
526 }
527 return verb;
528 }
529
530 ///////////////////////////////////////////////////////////////////////////////
531
532 #ifdef SK_DEBUG
533 static void assert_monotonic(const SkScalar coord[], int count) {
534 if (coord[0] > coord[(count - 1) * 2]) {
535 for (int i = 1; i < count; i++) {
536 SkASSERT(coord[2 * (i - 1)] >= coord[i * 2]);
537 }
538 } else if (coord[0] < coord[(count - 1) * 2]) {
539 for (int i = 1; i < count; i++) {
540 SkASSERT(coord[2 * (i - 1)] <= coord[i * 2]);
541 }
542 } else {
543 for (int i = 1; i < count; i++) {
544 SkASSERT(coord[2 * (i - 1)] == coord[i * 2]);
545 }
546 }
547 }
548
549 void sk_assert_monotonic_y(const SkPoint pts[], int count) {
550 if (count > 1) {
551 assert_monotonic(&pts[0].fY, count);
552 }
553 }
554
555 void sk_assert_monotonic_x(const SkPoint pts[], int count) {
556 if (count > 1) {
557 assert_monotonic(&pts[0].fX, count);
558 }
559 }
560 #endif
561
562 void SkEdgeClipper::ClipPath(const SkPathRaw& raw, const SkRect& clip, bool canCullToTheRight,
563 void (*consume)(SkEdgeClipper*, bool newCtr, void* ctx), void* ctx) {
564 SkAutoConicToQuads quadder;
565 constexpr float kConicTol = 0.25f;
566
567 SkPathEdgeIter iter(raw);
568 SkEdgeClipper clipper(canCullToTheRight);
569
570 while (auto e = iter.next()) {
571 switch (e.fEdge) {
572 case SkPathEdgeIter::Edge::kLine:
573 if (clipper.clipLine(e.fPts[0], e.fPts[1], clip)) {
574 consume(&clipper, e.fIsNewContour, ctx);
575 }
576 break;
577 case SkPathEdgeIter::Edge::kQuad:
578 if (clipper.clipQuad(e.fPts, clip)) {
579 consume(&clipper, e.fIsNewContour, ctx);
580 }
581 break;
582 case SkPathEdgeIter::Edge::kConic: {
583 const SkPoint* quadPts =
584 quadder.computeQuads(e.fPts, iter.conicWeight(), kConicTol);
585 for (int i = 0; i < quadder.countQuads(); ++i) {
586 if (clipper.clipQuad(quadPts, clip)) {
587 consume(&clipper, e.fIsNewContour, ctx);
588 }
589 quadPts += 2;
590 }
591 } break;
592 case SkPathEdgeIter::Edge::kCubic:
593 if (clipper.clipCubic(e.fPts, clip)) {
594 consume(&clipper, e.fIsNewContour, ctx);
595 }
596 break;
597 default:
598 SkDEBUGFAIL("Unknown edge type");
599 break;
600 }
601 }
602 }