1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
/*
* Copyright 2009 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkEdgeClipper.h"
#include "SkGeometry.h"
static bool quick_reject(const SkRect& bounds, const SkRect& clip) {
return bounds.fTop >= clip.fBottom || bounds.fBottom <= clip.fTop;
}
static inline void clamp_le(SkScalar& value, SkScalar max) {
if (value > max) {
value = max;
}
}
static inline void clamp_ge(SkScalar& value, SkScalar min) {
if (value < min) {
value = min;
}
}
/* src[] must be monotonic in Y. This routine copies src into dst, and sorts
it to be increasing in Y. If it had to reverse the order of the points,
it returns true, otherwise it returns false
*/
static bool sort_increasing_Y(SkPoint dst[], const SkPoint src[], int count) {
// we need the data to be monotonically increasing in Y
if (src[0].fY > src[count - 1].fY) {
for (int i = 0; i < count; i++) {
dst[i] = src[count - i - 1];
}
return true;
} else {
memcpy(dst, src, count * sizeof(SkPoint));
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
static bool chopMonoQuadAt(SkScalar c0, SkScalar c1, SkScalar c2,
SkScalar target, SkScalar* t) {
/* Solve F(t) = y where F(t) := [0](1-t)^2 + 2[1]t(1-t) + [2]t^2
* We solve for t, using quadratic equation, hence we have to rearrange
* our cooefficents to look like At^2 + Bt + C
*/
SkScalar A = c0 - c1 - c1 + c2;
SkScalar B = 2*(c1 - c0);
SkScalar C = c0 - target;
SkScalar roots[2]; // we only expect one, but make room for 2 for safety
int count = SkFindUnitQuadRoots(A, B, C, roots);
if (count) {
*t = roots[0];
return true;
}
return false;
}
static bool chopMonoQuadAtY(SkPoint pts[3], SkScalar y, SkScalar* t) {
return chopMonoQuadAt(pts[0].fY, pts[1].fY, pts[2].fY, y, t);
}
static bool chopMonoQuadAtX(SkPoint pts[3], SkScalar x, SkScalar* t) {
return chopMonoQuadAt(pts[0].fX, pts[1].fX, pts[2].fX, x, t);
}
// Modify pts[] in place so that it is clipped in Y to the clip rect
static void chop_quad_in_Y(SkPoint pts[3], const SkRect& clip) {
SkScalar t;
SkPoint tmp[5]; // for SkChopQuadAt
// are we partially above
if (pts[0].fY < clip.fTop) {
if (chopMonoQuadAtY(pts, clip.fTop, &t)) {
// take the 2nd chopped quad
SkChopQuadAt(pts, tmp, t);
// clamp to clean up imprecise numerics in the chop
tmp[2].fY = clip.fTop;
clamp_ge(tmp[3].fY, clip.fTop);
pts[0] = tmp[2];
pts[1] = tmp[3];
} else {
// if chopMonoQuadAtY failed, then we may have hit inexact numerics
// so we just clamp against the top
for (int i = 0; i < 3; i++) {
if (pts[i].fY < clip.fTop) {
pts[i].fY = clip.fTop;
}
}
}
}
// are we partially below
if (pts[2].fY > clip.fBottom) {
if (chopMonoQuadAtY(pts, clip.fBottom, &t)) {
SkChopQuadAt(pts, tmp, t);
// clamp to clean up imprecise numerics in the chop
clamp_le(tmp[1].fY, clip.fBottom);
tmp[2].fY = clip.fBottom;
pts[1] = tmp[1];
pts[2] = tmp[2];
} else {
// if chopMonoQuadAtY failed, then we may have hit inexact numerics
// so we just clamp against the bottom
for (int i = 0; i < 3; i++) {
if (pts[i].fY > clip.fBottom) {
pts[i].fY = clip.fBottom;
}
}
}
}
}
// srcPts[] must be monotonic in X and Y
void SkEdgeClipper::clipMonoQuad(const SkPoint srcPts[3], const SkRect& clip) {
SkPoint pts[3];
bool reverse = sort_increasing_Y(pts, srcPts, 3);
// are we completely above or below
if (pts[2].fY <= clip.fTop || pts[0].fY >= clip.fBottom) {
return;
}
// Now chop so that pts is contained within clip in Y
chop_quad_in_Y(pts, clip);
if (pts[0].fX > pts[2].fX) {
SkTSwap<SkPoint>(pts[0], pts[2]);
reverse = !reverse;
}
SkASSERT(pts[0].fX <= pts[1].fX);
SkASSERT(pts[1].fX <= pts[2].fX);
// Now chop in X has needed, and record the segments
if (pts[2].fX <= clip.fLeft) { // wholly to the left
this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse);
return;
}
if (pts[0].fX >= clip.fRight) { // wholly to the right
if (!this->canCullToTheRight()) {
this->appendVLine(clip.fRight, pts[0].fY, pts[2].fY, reverse);
}
return;
}
SkScalar t;
SkPoint tmp[5]; // for SkChopQuadAt
// are we partially to the left
if (pts[0].fX < clip.fLeft) {
if (chopMonoQuadAtX(pts, clip.fLeft, &t)) {
SkChopQuadAt(pts, tmp, t);
this->appendVLine(clip.fLeft, tmp[0].fY, tmp[2].fY, reverse);
// clamp to clean up imprecise numerics in the chop
tmp[2].fX = clip.fLeft;
clamp_ge(tmp[3].fX, clip.fLeft);
pts[0] = tmp[2];
pts[1] = tmp[3];
} else {
// if chopMonoQuadAtY failed, then we may have hit inexact numerics
// so we just clamp against the left
this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse);
return;
}
}
// are we partially to the right
if (pts[2].fX > clip.fRight) {
if (chopMonoQuadAtX(pts, clip.fRight, &t)) {
SkChopQuadAt(pts, tmp, t);
// clamp to clean up imprecise numerics in the chop
clamp_le(tmp[1].fX, clip.fRight);
tmp[2].fX = clip.fRight;
this->appendQuad(tmp, reverse);
this->appendVLine(clip.fRight, tmp[2].fY, tmp[4].fY, reverse);
} else {
// if chopMonoQuadAtY failed, then we may have hit inexact numerics
// so we just clamp against the right
this->appendVLine(clip.fRight, pts[0].fY, pts[2].fY, reverse);
}
} else { // wholly inside the clip
this->appendQuad(pts, reverse);
}
}
bool SkEdgeClipper::clipQuad(const SkPoint srcPts[3], const SkRect& clip) {
fCurrPoint = fPoints;
fCurrVerb = fVerbs;
SkRect bounds;
bounds.set(srcPts, 3);
if (!quick_reject(bounds, clip)) {
SkPoint monoY[5];
int countY = SkChopQuadAtYExtrema(srcPts, monoY);
for (int y = 0; y <= countY; y++) {
SkPoint monoX[5];
int countX = SkChopQuadAtXExtrema(&monoY[y * 2], monoX);
for (int x = 0; x <= countX; x++) {
this->clipMonoQuad(&monoX[x * 2], clip);
SkASSERT(fCurrVerb - fVerbs < kMaxVerbs);
SkASSERT(fCurrPoint - fPoints <= kMaxPoints);
}
}
}
*fCurrVerb = SkPath::kDone_Verb;
fCurrPoint = fPoints;
fCurrVerb = fVerbs;
return SkPath::kDone_Verb != fVerbs[0];
}
///////////////////////////////////////////////////////////////////////////////
static SkScalar mono_cubic_closestT(const SkScalar src[], SkScalar x) {
SkScalar t = 0.5f;
SkScalar lastT;
SkScalar bestT SK_INIT_TO_AVOID_WARNING;
SkScalar step = 0.25f;
SkScalar D = src[0];
SkScalar A = src[6] + 3*(src[2] - src[4]) - D;
SkScalar B = 3*(src[4] - src[2] - src[2] + D);
SkScalar C = 3*(src[2] - D);
x -= D;
SkScalar closest = SK_ScalarMax;
do {
SkScalar loc = ((A * t + B) * t + C) * t;
SkScalar dist = SkScalarAbs(loc - x);
if (closest > dist) {
closest = dist;
bestT = t;
}
lastT = t;
t += loc < x ? step : -step;
step *= 0.5f;
} while (closest > 0.25f && lastT != t);
return bestT;
}
static void chop_mono_cubic_at_y(SkPoint src[4], SkScalar y, SkPoint dst[7]) {
if (SkChopMonoCubicAtY(src, y, dst)) {
return;
}
SkChopCubicAt(src, dst, mono_cubic_closestT(&src->fY, y));
}
// Modify pts[] in place so that it is clipped in Y to the clip rect
static void chop_cubic_in_Y(SkPoint pts[4], const SkRect& clip) {
// are we partially above
if (pts[0].fY < clip.fTop) {
SkPoint tmp[7];
chop_mono_cubic_at_y(pts, clip.fTop, tmp);
/*
* For a large range in the points, we can do a poor job of chopping, such that the t
* we computed resulted in the lower cubic still being partly above the clip.
*
* If just the first or first 2 Y values are above the fTop, we can just smash them
* down. If the first 3 Ys are above fTop, we can't smash all 3, as that can really
* distort the cubic. In this case, we take the first output (tmp[3..6] and treat it as
* a guess, and re-chop against fTop. Then we fall through to checking if we need to
* smash the first 1 or 2 Y values.
*/
if (tmp[3].fY < clip.fTop && tmp[4].fY < clip.fTop && tmp[5].fY < clip.fTop) {
SkPoint tmp2[4];
memcpy(tmp2, &tmp[3].fX, 4 * sizeof(SkPoint));
chop_mono_cubic_at_y(tmp2, clip.fTop, tmp);
}
// tmp[3, 4].fY should all be to the below clip.fTop.
// Since we can't trust the numerics of the chopper, we force those conditions now
tmp[3].fY = clip.fTop;
clamp_ge(tmp[4].fY, clip.fTop);
pts[0] = tmp[3];
pts[1] = tmp[4];
pts[2] = tmp[5];
}
// are we partially below
if (pts[3].fY > clip.fBottom) {
SkPoint tmp[7];
chop_mono_cubic_at_y(pts, clip.fBottom, tmp);
tmp[3].fY = clip.fBottom;
clamp_le(tmp[2].fY, clip.fBottom);
pts[1] = tmp[1];
pts[2] = tmp[2];
pts[3] = tmp[3];
}
}
static void chop_mono_cubic_at_x(SkPoint src[4], SkScalar x, SkPoint dst[7]) {
if (SkChopMonoCubicAtX(src, x, dst)) {
return;
}
SkChopCubicAt(src, dst, mono_cubic_closestT(&src->fX, x));
}
// srcPts[] must be monotonic in X and Y
void SkEdgeClipper::clipMonoCubic(const SkPoint src[4], const SkRect& clip) {
SkPoint pts[4];
bool reverse = sort_increasing_Y(pts, src, 4);
// are we completely above or below
if (pts[3].fY <= clip.fTop || pts[0].fY >= clip.fBottom) {
return;
}
// Now chop so that pts is contained within clip in Y
chop_cubic_in_Y(pts, clip);
if (pts[0].fX > pts[3].fX) {
SkTSwap<SkPoint>(pts[0], pts[3]);
SkTSwap<SkPoint>(pts[1], pts[2]);
reverse = !reverse;
}
// Now chop in X has needed, and record the segments
if (pts[3].fX <= clip.fLeft) { // wholly to the left
this->appendVLine(clip.fLeft, pts[0].fY, pts[3].fY, reverse);
return;
}
if (pts[0].fX >= clip.fRight) { // wholly to the right
if (!this->canCullToTheRight()) {
this->appendVLine(clip.fRight, pts[0].fY, pts[3].fY, reverse);
}
return;
}
// are we partially to the left
if (pts[0].fX < clip.fLeft) {
SkPoint tmp[7];
chop_mono_cubic_at_x(pts, clip.fLeft, tmp);
this->appendVLine(clip.fLeft, tmp[0].fY, tmp[3].fY, reverse);
// tmp[3, 4].fX should all be to the right of clip.fLeft.
// Since we can't trust the numerics of
// the chopper, we force those conditions now
tmp[3].fX = clip.fLeft;
clamp_ge(tmp[4].fX, clip.fLeft);
pts[0] = tmp[3];
pts[1] = tmp[4];
pts[2] = tmp[5];
}
// are we partially to the right
if (pts[3].fX > clip.fRight) {
SkPoint tmp[7];
chop_mono_cubic_at_x(pts, clip.fRight, tmp);
tmp[3].fX = clip.fRight;
clamp_le(tmp[2].fX, clip.fRight);
this->appendCubic(tmp, reverse);
this->appendVLine(clip.fRight, tmp[3].fY, tmp[6].fY, reverse);
} else { // wholly inside the clip
this->appendCubic(pts, reverse);
}
}
static bool quick_reject_in_y(const SkPoint pts[4], const SkRect& clip) {
Sk4s ys(pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY);
Sk4s t(clip.top());
Sk4s b(clip.bottom());
return (ys < t).allTrue() || (ys > b).allTrue();
}
bool SkEdgeClipper::clipCubic(const SkPoint srcPts[4], const SkRect& clip) {
fCurrPoint = fPoints;
fCurrVerb = fVerbs;
if (!quick_reject_in_y(srcPts, clip)) {
SkPoint monoY[10];
int countY = SkChopCubicAtYExtrema(srcPts, monoY);
for (int y = 0; y <= countY; y++) {
SkPoint monoX[10];
int countX = SkChopCubicAtXExtrema(&monoY[y * 3], monoX);
for (int x = 0; x <= countX; x++) {
this->clipMonoCubic(&monoX[x * 3], clip);
SkASSERT(fCurrVerb - fVerbs < kMaxVerbs);
SkASSERT(fCurrPoint - fPoints <= kMaxPoints);
}
}
}
*fCurrVerb = SkPath::kDone_Verb;
fCurrPoint = fPoints;
fCurrVerb = fVerbs;
return SkPath::kDone_Verb != fVerbs[0];
}
///////////////////////////////////////////////////////////////////////////////
void SkEdgeClipper::appendVLine(SkScalar x, SkScalar y0, SkScalar y1,
bool reverse) {
*fCurrVerb++ = SkPath::kLine_Verb;
if (reverse) {
SkTSwap<SkScalar>(y0, y1);
}
fCurrPoint[0].set(x, y0);
fCurrPoint[1].set(x, y1);
fCurrPoint += 2;
}
void SkEdgeClipper::appendQuad(const SkPoint pts[3], bool reverse) {
*fCurrVerb++ = SkPath::kQuad_Verb;
if (reverse) {
fCurrPoint[0] = pts[2];
fCurrPoint[2] = pts[0];
} else {
fCurrPoint[0] = pts[0];
fCurrPoint[2] = pts[2];
}
fCurrPoint[1] = pts[1];
fCurrPoint += 3;
}
void SkEdgeClipper::appendCubic(const SkPoint pts[4], bool reverse) {
*fCurrVerb++ = SkPath::kCubic_Verb;
if (reverse) {
for (int i = 0; i < 4; i++) {
fCurrPoint[i] = pts[3 - i];
}
} else {
memcpy(fCurrPoint, pts, 4 * sizeof(SkPoint));
}
fCurrPoint += 4;
}
SkPath::Verb SkEdgeClipper::next(SkPoint pts[]) {
SkPath::Verb verb = *fCurrVerb;
switch (verb) {
case SkPath::kLine_Verb:
memcpy(pts, fCurrPoint, 2 * sizeof(SkPoint));
fCurrPoint += 2;
fCurrVerb += 1;
break;
case SkPath::kQuad_Verb:
memcpy(pts, fCurrPoint, 3 * sizeof(SkPoint));
fCurrPoint += 3;
fCurrVerb += 1;
break;
case SkPath::kCubic_Verb:
memcpy(pts, fCurrPoint, 4 * sizeof(SkPoint));
fCurrPoint += 4;
fCurrVerb += 1;
break;
case SkPath::kDone_Verb:
break;
default:
SkDEBUGFAIL("unexpected verb in quadclippper2 iter");
break;
}
return verb;
}
///////////////////////////////////////////////////////////////////////////////
#ifdef SK_DEBUG
static void assert_monotonic(const SkScalar coord[], int count) {
if (coord[0] > coord[(count - 1) * 2]) {
for (int i = 1; i < count; i++) {
SkASSERT(coord[2 * (i - 1)] >= coord[i * 2]);
}
} else if (coord[0] < coord[(count - 1) * 2]) {
for (int i = 1; i < count; i++) {
SkASSERT(coord[2 * (i - 1)] <= coord[i * 2]);
}
} else {
for (int i = 1; i < count; i++) {
SkASSERT(coord[2 * (i - 1)] == coord[i * 2]);
}
}
}
void sk_assert_monotonic_y(const SkPoint pts[], int count) {
if (count > 1) {
assert_monotonic(&pts[0].fY, count);
}
}
void sk_assert_monotonic_x(const SkPoint pts[], int count) {
if (count > 1) {
assert_monotonic(&pts[0].fX, count);
}
}
#endif
|