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/SkColor.h"
9 #include "include/core/SkPath.h"
10 #include "include/core/SkPathTypes.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRegion.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/base/SkFixed.h"
17 #include "include/private/base/SkFloatingPoint.h"
18 #include "include/private/base/SkMacros.h"
19 #include "include/private/base/SkMath.h"
20 #include "include/private/base/SkPoint_impl.h"
21 #include "include/private/base/SkSafe32.h"
22 #include "src/base/SkTSort.h"
23 #include "src/core/SkBlitter.h"
24 #include "src/core/SkEdge.h"
25 #include "src/core/SkEdgeBuilder.h"
26 #include "src/core/SkFDot6.h"
27 #include "src/core/SkRasterClip.h"
28 #include "src/core/SkRectPriv.h"
29 #include "src/core/SkScan.h"
30 #include "src/core/SkScanPriv.h"
31
32 #include <algorithm>
33 #include <cmath>
34 #include <cstdint>
35
36 struct SkMask;
37
38 #define kEDGE_HEAD_Y SK_MinS32
39 #define kEDGE_TAIL_Y SK_MaxS32
40
41 #ifdef SK_DEBUG
validate_sort(const SkEdge * edge)42 static void validate_sort(const SkEdge* edge) {
43 int y = kEDGE_HEAD_Y;
44
45 while (edge->fFirstY != SK_MaxS32) {
46 edge->validate();
47 SkASSERT(y <= edge->fFirstY);
48
49 y = edge->fFirstY;
50 edge = edge->fNext;
51 }
52 }
53 #else
54 #define validate_sort(edge)
55 #endif
56
insert_new_edges(SkEdge * newEdge,int curr_y)57 static void insert_new_edges(SkEdge* newEdge, int curr_y) {
58 if (newEdge->fFirstY != curr_y) {
59 return;
60 }
61 SkEdge* prev = newEdge->fPrev;
62 if (prev->fX <= newEdge->fX) {
63 return;
64 }
65 // find first x pos to insert
66 SkEdge* start = backward_insert_start(prev, newEdge->fX);
67 // insert the lot, fixing up the links as we go
68 do {
69 SkEdge* next = newEdge->fNext;
70 do {
71 if (start->fNext == newEdge) {
72 goto nextEdge;
73 }
74 SkEdge* after = start->fNext;
75 if (after->fX >= newEdge->fX) {
76 break;
77 }
78 start = after;
79 } while (true);
80 remove_edge(newEdge);
81 insert_edge_after(newEdge, start);
82 nextEdge:
83 start = newEdge;
84 newEdge = next;
85 } while (newEdge->fFirstY == curr_y);
86 }
87
88 #ifdef SK_DEBUG
validate_edges_for_y(const SkEdge * edge,int curr_y)89 static void validate_edges_for_y(const SkEdge* edge, int curr_y) {
90 while (edge->fFirstY <= curr_y) {
91 SkASSERT(edge->fPrev && edge->fNext);
92 SkASSERT(edge->fPrev->fNext == edge);
93 SkASSERT(edge->fNext->fPrev == edge);
94 SkASSERT(edge->fFirstY <= edge->fLastY);
95
96 SkASSERT(edge->fPrev->fX <= edge->fX);
97 edge = edge->fNext;
98 }
99 }
100 #else
101 #define validate_edges_for_y(edge, curr_y)
102 #endif
103
104 #if defined _WIN32 // disable warning : local variable used without having been initialized
105 #pragma warning ( push )
106 #pragma warning ( disable : 4701 )
107 #endif
108
109 typedef void (*PrePostProc)(SkBlitter* blitter, int y, bool isStartOfScanline);
110 #define PREPOST_START true
111 #define PREPOST_END false
112
walk_edges(SkEdge * prevHead,SkPathFillType fillType,SkBlitter * blitter,int start_y,int stop_y,PrePostProc proc,int rightClip)113 static void walk_edges(SkEdge* prevHead, SkPathFillType fillType,
114 SkBlitter* blitter, int start_y, int stop_y,
115 PrePostProc proc, int rightClip) {
116 validate_sort(prevHead->fNext);
117
118 int curr_y = start_y;
119 int windingMask = SkPathFillType_IsEvenOdd(fillType) ? 1 : -1;
120
121 for (;;) {
122 int w = 0;
123 int left SK_INIT_TO_AVOID_WARNING;
124 SkEdge* currE = prevHead->fNext;
125 SkFixed prevX = prevHead->fX;
126
127 validate_edges_for_y(currE, curr_y);
128
129 if (proc) {
130 proc(blitter, curr_y, PREPOST_START); // pre-proc
131 }
132
133 while (currE->fFirstY <= curr_y) {
134 SkASSERT(currE->fLastY >= curr_y);
135
136 int x = SkFixedRoundToInt(currE->fX);
137
138 if ((w & windingMask) == 0) { // we're starting interval
139 left = x;
140 }
141
142 w += static_cast<int>(currE->fWinding);
143
144 if ((w & windingMask) == 0) { // we finished an interval
145 int width = x - left;
146 SkASSERT(width >= 0);
147 if (width > 0) {
148 blitter->blitH(left, curr_y, width);
149 }
150 }
151
152 SkEdge* next = currE->fNext;
153 SkFixed newX;
154
155 if (currE->fLastY == curr_y) { // are we done with this edge?
156 if (currE->fCurveCount > 0) {
157 if (((SkQuadraticEdge*)currE)->updateQuadratic()) {
158 newX = currE->fX;
159 goto NEXT_X;
160 }
161 } else if (currE->fCurveCount < 0) {
162 if (((SkCubicEdge*)currE)->updateCubic()) {
163 SkASSERT(currE->fFirstY == curr_y + 1);
164
165 newX = currE->fX;
166 goto NEXT_X;
167 }
168 }
169 remove_edge(currE);
170 } else {
171 SkASSERT(currE->fLastY > curr_y);
172 newX = currE->fX + currE->fDX;
173 currE->fX = newX;
174 NEXT_X:
175 if (newX < prevX) { // ripple currE backwards until it is x-sorted
176 backward_insert_edge_based_on_x(currE);
177 } else {
178 prevX = newX;
179 }
180 }
181 currE = next;
182 SkASSERT(currE);
183 }
184
185 if ((w & windingMask) != 0) { // was our right-edge culled away?
186 int width = rightClip - left;
187 if (width > 0) {
188 blitter->blitH(left, curr_y, width);
189 }
190 }
191
192 if (proc) {
193 proc(blitter, curr_y, PREPOST_END); // post-proc
194 }
195
196 curr_y += 1;
197 if (curr_y >= stop_y) {
198 break;
199 }
200 // now currE points to the first edge with a Yint larger than curr_y
201 insert_new_edges(currE, curr_y);
202 }
203 }
204
205 // return true if we're NOT done with this edge
update_edge(SkEdge * edge,int last_y)206 static bool update_edge(SkEdge* edge, int last_y) {
207 SkASSERT(edge->fLastY >= last_y);
208 if (last_y == edge->fLastY) {
209 if (edge->fCurveCount < 0) {
210 if (((SkCubicEdge*)edge)->updateCubic()) {
211 SkASSERT(edge->fFirstY == last_y + 1);
212 return true;
213 }
214 } else if (edge->fCurveCount > 0) {
215 if (((SkQuadraticEdge*)edge)->updateQuadratic()) {
216 SkASSERT(edge->fFirstY == last_y + 1);
217 return true;
218 }
219 }
220 return false;
221 }
222 return true;
223 }
224
225 // Unexpected conditions for which we need to return
226 #define ASSERT_RETURN(cond) \
227 do { \
228 if (!(cond)) { \
229 SkDEBUGFAILF("assert(%s)", #cond); \
230 return; \
231 } \
232 } while (0)
233
234 // Needs Y to only change once (looser than convex in X)
walk_simple_edges(SkEdge * prevHead,SkBlitter * blitter,int start_y,int stop_y)235 static void walk_simple_edges(SkEdge* prevHead, SkBlitter* blitter, int start_y, int stop_y) {
236 validate_sort(prevHead->fNext);
237
238 SkEdge* leftE = prevHead->fNext;
239 SkEdge* riteE = leftE->fNext;
240 SkEdge* currE = riteE->fNext;
241
242 // our edge choppers for curves can result in the initial edges
243 // not lining up, so we take the max.
244 int local_top = std::max(leftE->fFirstY, riteE->fFirstY);
245 ASSERT_RETURN(local_top >= start_y);
246
247 while (local_top < stop_y) {
248 SkASSERT(leftE->fFirstY <= stop_y);
249 SkASSERT(riteE->fFirstY <= stop_y);
250
251 int local_bot = std::min(leftE->fLastY, riteE->fLastY);
252 local_bot = std::min(local_bot, stop_y - 1);
253 ASSERT_RETURN(local_top <= local_bot);
254
255 SkFixed left = leftE->fX;
256 SkFixed dLeft = leftE->fDX;
257 SkFixed rite = riteE->fX;
258 SkFixed dRite = riteE->fDX;
259 int count = local_bot - local_top;
260 ASSERT_RETURN(count >= 0);
261
262 if (0 == (dLeft | dRite)) {
263 int L = SkFixedRoundToInt(left);
264 int R = SkFixedRoundToInt(rite);
265 if (L > R) {
266 std::swap(L, R);
267 }
268 if (L < R) {
269 count += 1;
270 blitter->blitRect(L, local_top, R - L, count);
271 }
272 local_top = local_bot + 1;
273 } else {
274 do {
275 int L = SkFixedRoundToInt(left);
276 int R = SkFixedRoundToInt(rite);
277 if (L > R) {
278 std::swap(L, R);
279 }
280 if (L < R) {
281 blitter->blitH(L, local_top, R - L);
282 }
283 // Either/both of these might overflow, since we perform this step even if
284 // (later) we determine that we are done with the edge, and so the computed
285 // left or rite edge will not be used (see update_edge). Use this helper to
286 // silence UBSAN when we perform the add.
287 left = Sk32_can_overflow_add(left, dLeft);
288 rite = Sk32_can_overflow_add(rite, dRite);
289 local_top += 1;
290 } while (--count >= 0);
291 }
292
293 leftE->fX = left;
294 riteE->fX = rite;
295
296 if (!update_edge(leftE, local_bot)) {
297 if (currE->fFirstY >= stop_y) {
298 return; // we're done
299 }
300 leftE = currE;
301 currE = currE->fNext;
302 ASSERT_RETURN(leftE->fFirstY == local_top);
303 }
304 if (!update_edge(riteE, local_bot)) {
305 if (currE->fFirstY >= stop_y) {
306 return; // we're done
307 }
308 riteE = currE;
309 currE = currE->fNext;
310 ASSERT_RETURN(riteE->fFirstY == local_top);
311 }
312 }
313 }
314
315 ///////////////////////////////////////////////////////////////////////////////
316
317 // this overrides blitH, and will call its proxy blitter with the inverse
318 // of the spans it is given (clipped to the left/right of the cliprect)
319 //
320 // used to implement inverse filltypes on paths
321 //
322 class InverseBlitter : public SkBlitter {
323 public:
setBlitter(SkBlitter * blitter,const SkIRect & clip,int shift)324 void setBlitter(SkBlitter* blitter, const SkIRect& clip, int shift) {
325 fBlitter = blitter;
326 fFirstX = clip.fLeft << shift;
327 fLastX = clip.fRight << shift;
328 }
prepost(int y,bool isStart)329 void prepost(int y, bool isStart) {
330 if (isStart) {
331 fPrevX = fFirstX;
332 } else {
333 int invWidth = fLastX - fPrevX;
334 if (invWidth > 0) {
335 fBlitter->blitH(fPrevX, y, invWidth);
336 }
337 }
338 }
339
340 // overrides
blitH(int x,int y,int width)341 void blitH(int x, int y, int width) override {
342 int invWidth = x - fPrevX;
343 if (invWidth > 0) {
344 fBlitter->blitH(fPrevX, y, invWidth);
345 }
346 fPrevX = x + width;
347 }
348
349 // we do not expect to get called with these entrypoints
blitAntiH(int,int,const SkAlpha[],const int16_t runs[])350 void blitAntiH(int, int, const SkAlpha[], const int16_t runs[]) override {
351 SkDEBUGFAIL("blitAntiH unexpected");
352 }
blitV(int x,int y,int height,SkAlpha alpha)353 void blitV(int x, int y, int height, SkAlpha alpha) override {
354 SkDEBUGFAIL("blitV unexpected");
355 }
blitRect(int x,int y,int width,int height)356 void blitRect(int x, int y, int width, int height) override {
357 SkDEBUGFAIL("blitRect unexpected");
358 }
blitMask(const SkMask &,const SkIRect & clip)359 void blitMask(const SkMask&, const SkIRect& clip) override {
360 SkDEBUGFAIL("blitMask unexpected");
361 }
362
363 private:
364 SkBlitter* fBlitter;
365 int fFirstX, fLastX, fPrevX;
366 };
367
PrePostInverseBlitterProc(SkBlitter * blitter,int y,bool isStart)368 static void PrePostInverseBlitterProc(SkBlitter* blitter, int y, bool isStart) {
369 ((InverseBlitter*)blitter)->prepost(y, isStart);
370 }
371
372 ///////////////////////////////////////////////////////////////////////////////
373
374 #if defined _WIN32
375 #pragma warning ( pop )
376 #endif
377
compare_edges(const SkEdge * a,const SkEdge * b)378 static bool compare_edges(const SkEdge* a, const SkEdge* b) {
379 if (a->fFirstY != b->fFirstY) {
380 return a->fFirstY < b->fFirstY;
381 }
382
383 return a->fX < b->fX;
384 }
385
sort_edges(SkEdge * list[],int count,SkEdge ** last)386 static SkEdge* sort_edges(SkEdge* list[], int count, SkEdge** last) {
387 SkTQSort(list, list + count, compare_edges);
388
389 // now make the edges linked in sorted order
390 for (int i = 1; i < count; i++) {
391 list[i - 1]->fNext = list[i];
392 list[i]->fPrev = list[i - 1];
393 }
394
395 *last = list[count - 1];
396 return list[0];
397 }
398
399 // clipRect has not been shifted up
sk_fill_path(const SkPath & path,const SkIRect & clipRect,SkBlitter * blitter,int start_y,int stop_y,int shiftEdgesUp,bool pathContainedInClip)400 void sk_fill_path(const SkPath& path, const SkIRect& clipRect, SkBlitter* blitter,
401 int start_y, int stop_y, int shiftEdgesUp, bool pathContainedInClip) {
402 SkASSERT(blitter);
403
404 SkIRect shiftedClip = clipRect;
405 shiftedClip.fLeft = SkLeftShift(shiftedClip.fLeft, shiftEdgesUp);
406 shiftedClip.fRight = SkLeftShift(shiftedClip.fRight, shiftEdgesUp);
407 shiftedClip.fTop = SkLeftShift(shiftedClip.fTop, shiftEdgesUp);
408 shiftedClip.fBottom = SkLeftShift(shiftedClip.fBottom, shiftEdgesUp);
409
410 SkBasicEdgeBuilder builder(shiftEdgesUp);
411 int count = builder.buildEdges(path, pathContainedInClip ? nullptr : &shiftedClip);
412 SkEdge** list = builder.edgeList();
413
414 if (0 == count) {
415 if (path.isInverseFillType()) {
416 /*
417 * Since we are in inverse-fill, our caller has already drawn above
418 * our top (start_y) and will draw below our bottom (stop_y). Thus
419 * we need to restrict our drawing to the intersection of the clip
420 * and those two limits.
421 */
422 SkIRect rect = clipRect;
423 if (rect.fTop < start_y) {
424 rect.fTop = start_y;
425 }
426 if (rect.fBottom > stop_y) {
427 rect.fBottom = stop_y;
428 }
429 if (!rect.isEmpty()) {
430 blitter->blitRect(rect.fLeft << shiftEdgesUp,
431 rect.fTop << shiftEdgesUp,
432 rect.width() << shiftEdgesUp,
433 rect.height() << shiftEdgesUp);
434 }
435 }
436 return;
437 }
438
439 SkEdge headEdge, tailEdge, *last;
440 // this returns the first and last edge after they're sorted into a dlink list
441 SkEdge* edge = sort_edges(list, count, &last);
442
443 headEdge.fPrev = nullptr;
444 headEdge.fNext = edge;
445 headEdge.fFirstY = kEDGE_HEAD_Y;
446 headEdge.fX = SK_MinS32;
447 edge->fPrev = &headEdge;
448
449 tailEdge.fPrev = last;
450 tailEdge.fNext = nullptr;
451 tailEdge.fFirstY = kEDGE_TAIL_Y;
452 last->fNext = &tailEdge;
453
454 // now edge is the head of the sorted linklist
455
456 start_y = SkLeftShift(start_y, shiftEdgesUp);
457 stop_y = SkLeftShift(stop_y, shiftEdgesUp);
458 if (!pathContainedInClip && start_y < shiftedClip.fTop) {
459 start_y = shiftedClip.fTop;
460 }
461 if (!pathContainedInClip && stop_y > shiftedClip.fBottom) {
462 stop_y = shiftedClip.fBottom;
463 }
464
465 InverseBlitter ib;
466 PrePostProc proc = nullptr;
467
468 if (path.isInverseFillType()) {
469 ib.setBlitter(blitter, clipRect, shiftEdgesUp);
470 blitter = &ib;
471 proc = PrePostInverseBlitterProc;
472 }
473
474 // count >= 2 is required as the convex walker does not handle missing right edges
475 if (path.isConvex() && (nullptr == proc) && count >= 2) {
476 walk_simple_edges(&headEdge, blitter, start_y, stop_y);
477 } else {
478 walk_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, proc,
479 shiftedClip.right());
480 }
481 }
482
sk_blit_above(SkBlitter * blitter,const SkIRect & ir,const SkRegion & clip)483 void sk_blit_above(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
484 const SkIRect& cr = clip.getBounds();
485 SkIRect tmp;
486
487 tmp.fLeft = cr.fLeft;
488 tmp.fRight = cr.fRight;
489 tmp.fTop = cr.fTop;
490 tmp.fBottom = ir.fTop;
491 if (!tmp.isEmpty()) {
492 blitter->blitRectRegion(tmp, clip);
493 }
494 }
495
sk_blit_below(SkBlitter * blitter,const SkIRect & ir,const SkRegion & clip)496 void sk_blit_below(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
497 const SkIRect& cr = clip.getBounds();
498 SkIRect tmp;
499
500 tmp.fLeft = cr.fLeft;
501 tmp.fRight = cr.fRight;
502 tmp.fTop = ir.fBottom;
503 tmp.fBottom = cr.fBottom;
504 if (!tmp.isEmpty()) {
505 blitter->blitRectRegion(tmp, clip);
506 }
507 }
508
509 ///////////////////////////////////////////////////////////////////////////////
510
511 /**
512 * If the caller is drawing an inverse-fill path, then it pass true for
513 * skipRejectTest, so we don't abort drawing just because the src bounds (ir)
514 * is outside of the clip.
515 */
SkScanClipper(SkBlitter * blitter,const SkRegion * clip,const SkIRect & ir,bool skipRejectTest,bool irPreClipped)516 SkScanClipper::SkScanClipper(SkBlitter* blitter, const SkRegion* clip,
517 const SkIRect& ir, bool skipRejectTest, bool irPreClipped) {
518 fBlitter = nullptr; // null means blit nothing
519 fClipRect = nullptr;
520
521 if (clip) {
522 fClipRect = &clip->getBounds();
523 if (!skipRejectTest && !SkIRect::Intersects(*fClipRect, ir)) { // completely clipped out
524 return;
525 }
526
527 if (clip->isRect()) {
528 if (!irPreClipped && fClipRect->contains(ir)) {
529 #ifdef SK_DEBUG
530 fRectClipCheckBlitter.init(blitter, *fClipRect);
531 blitter = &fRectClipCheckBlitter;
532 #endif
533 fClipRect = nullptr;
534 } else {
535 // only need a wrapper blitter if we're horizontally clipped
536 if (irPreClipped ||
537 fClipRect->fLeft > ir.fLeft || fClipRect->fRight < ir.fRight) {
538 fRectBlitter.init(blitter, *fClipRect);
539 blitter = &fRectBlitter;
540 } else {
541 #ifdef SK_DEBUG
542 fRectClipCheckBlitter.init(blitter, *fClipRect);
543 blitter = &fRectClipCheckBlitter;
544 #endif
545 }
546 }
547 } else {
548 fRgnBlitter.init(blitter, clip);
549 blitter = &fRgnBlitter;
550 }
551 }
552 fBlitter = blitter;
553 }
554
555 ///////////////////////////////////////////////////////////////////////////////
556
clip_to_limit(const SkRegion & orig,SkRegion * reduced)557 static bool clip_to_limit(const SkRegion& orig, SkRegion* reduced) {
558 // need to limit coordinates such that the width/height of our rect can be represented
559 // in SkFixed (16.16). See skbug.com/7998
560 const int32_t limit = 32767 >> 1;
561
562 SkIRect limitR;
563 limitR.setLTRB(-limit, -limit, limit, limit);
564 if (limitR.contains(orig.getBounds())) {
565 return false;
566 }
567 reduced->op(orig, limitR, SkRegion::kIntersect_Op);
568 return true;
569 }
570
571 // Bias used for conservative rounding of float rects to int rects, to nudge the irects a little
572 // larger, so we don't "think" a path's bounds are inside a clip, when (due to numeric drift in
573 // the scan-converter) we might walk beyond the predicted limits.
574 //
575 // This value has been determined trial and error: pick the smallest value (after the 0.5) that
576 // fixes any problematic cases (e.g. crbug.com/844457)
577 // NOTE: cubics appear to be the main reason for needing this slop. If we could (perhaps) have a
578 // more accurate walker for cubics, we may be able to reduce this fudge factor.
579 static const double kConservativeRoundBias = 0.5 + 1.5 / SK_FDot6One;
580
581 /**
582 * Round the value down. This is used to round the top and left of a rectangle,
583 * and corresponds to the way the scan converter treats the top and left edges.
584 * It has a slight bias to make the "rounded" int smaller than a normal round, to create a more
585 * conservative int-bounds (larger) from a float rect.
586 */
round_down_to_int(SkScalar x)587 static inline int round_down_to_int(SkScalar x) {
588 double xx = x;
589 xx -= kConservativeRoundBias;
590 return sk_double_saturate2int(ceil(xx));
591 }
592
593 /**
594 * Round the value up. This is used to round the right and bottom of a rectangle.
595 * It has a slight bias to make the "rounded" int smaller than a normal round, to create a more
596 * conservative int-bounds (larger) from a float rect.
597 */
round_up_to_int(SkScalar x)598 static inline int round_up_to_int(SkScalar x) {
599 double xx = x;
600 xx += kConservativeRoundBias;
601 return sk_double_saturate2int(floor(xx));
602 }
603
604 /*
605 * Conservative rounding function, which effectively nudges the int-rect to be slightly larger
606 * than SkRect::round() might have produced. This is a safety-net for the scan-converter, which
607 * inspects the returned int-rect, and may disable clipping (for speed) if it thinks all of the
608 * edges will fit inside the clip's bounds. The scan-converter introduces slight numeric errors
609 * due to accumulated += of the slope, so this function is used to return a conservatively large
610 * int-bounds, and thus we will only disable clipping if we're sure the edges will stay in-bounds.
611 */
conservative_round_to_int(const SkRect & src)612 static SkIRect conservative_round_to_int(const SkRect& src) {
613 return {
614 round_down_to_int(src.fLeft),
615 round_down_to_int(src.fTop),
616 round_up_to_int(src.fRight),
617 round_up_to_int(src.fBottom),
618 };
619 }
620
FillPath(const SkPath & path,const SkRegion & origClip,SkBlitter * blitter)621 void SkScan::FillPath(const SkPath& path, const SkRegion& origClip,
622 SkBlitter* blitter) {
623 if (origClip.isEmpty()) {
624 return;
625 }
626
627 // Our edges are fixed-point, and don't like the bounds of the clip to
628 // exceed that. Here we trim the clip just so we don't overflow later on
629 const SkRegion* clipPtr = &origClip;
630 SkRegion finiteClip;
631 if (clip_to_limit(origClip, &finiteClip)) {
632 if (finiteClip.isEmpty()) {
633 return;
634 }
635 clipPtr = &finiteClip;
636 }
637 // don't reference "origClip" any more, just use clipPtr
638
639
640 SkRect bounds = path.getBounds();
641 bool irPreClipped = false;
642 if (!SkRectPriv::MakeLargeS32().contains(bounds)) {
643 if (!bounds.intersect(SkRectPriv::MakeLargeS32())) {
644 bounds.setEmpty();
645 }
646 irPreClipped = true;
647 }
648
649 SkIRect ir = conservative_round_to_int(bounds);
650 if (ir.isEmpty()) {
651 if (path.isInverseFillType()) {
652 blitter->blitRegion(*clipPtr);
653 }
654 return;
655 }
656
657 SkScanClipper clipper(blitter, clipPtr, ir, path.isInverseFillType(), irPreClipped);
658
659 blitter = clipper.getBlitter();
660 if (blitter) {
661 // we have to keep our calls to blitter in sorted order, so we
662 // must blit the above section first, then the middle, then the bottom.
663 if (path.isInverseFillType()) {
664 sk_blit_above(blitter, ir, *clipPtr);
665 }
666 SkASSERT(clipper.getClipRect() == nullptr ||
667 *clipper.getClipRect() == clipPtr->getBounds());
668 sk_fill_path(path, clipPtr->getBounds(), blitter, ir.fTop, ir.fBottom,
669 0, clipper.getClipRect() == nullptr);
670 if (path.isInverseFillType()) {
671 sk_blit_below(blitter, ir, *clipPtr);
672 }
673 } else {
674 // what does it mean to not have a blitter if path.isInverseFillType???
675 }
676 }
677
FillPath(const SkPath & path,const SkIRect & ir,SkBlitter * blitter)678 void SkScan::FillPath(const SkPath& path, const SkIRect& ir,
679 SkBlitter* blitter) {
680 SkRegion rgn(ir);
681 FillPath(path, rgn, blitter);
682 }
683
PathRequiresTiling(const SkIRect & bounds)684 bool SkScan::PathRequiresTiling(const SkIRect& bounds) {
685 SkRegion out; // ignored
686 return clip_to_limit(SkRegion(bounds), &out);
687 }
688
689 ///////////////////////////////////////////////////////////////////////////////
690
build_tri_edges(SkEdge edge[],const SkPoint pts[],const SkIRect * clipRect,SkEdge * list[])691 static int build_tri_edges(SkEdge edge[], const SkPoint pts[],
692 const SkIRect* clipRect, SkEdge* list[]) {
693 SkEdge** start = list;
694
695 if (edge->setLine(pts[0], pts[1], clipRect, 0)) {
696 *list++ = edge;
697 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
698 }
699 if (edge->setLine(pts[1], pts[2], clipRect, 0)) {
700 *list++ = edge;
701 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
702 }
703 if (edge->setLine(pts[2], pts[0], clipRect, 0)) {
704 *list++ = edge;
705 }
706 return (int)(list - start);
707 }
708
709
sk_fill_triangle(const SkPoint pts[],const SkIRect * clipRect,SkBlitter * blitter,const SkIRect & ir)710 static void sk_fill_triangle(const SkPoint pts[], const SkIRect* clipRect,
711 SkBlitter* blitter, const SkIRect& ir) {
712 SkASSERT(pts && blitter);
713
714 SkEdge edgeStorage[3];
715 SkEdge* list[3];
716
717 int count = build_tri_edges(edgeStorage, pts, clipRect, list);
718 if (count < 2) {
719 return;
720 }
721
722 SkEdge headEdge, tailEdge, *last;
723
724 // this returns the first and last edge after they're sorted into a dlink list
725 SkEdge* edge = sort_edges(list, count, &last);
726
727 headEdge.fPrev = nullptr;
728 headEdge.fNext = edge;
729 headEdge.fFirstY = kEDGE_HEAD_Y;
730 headEdge.fX = SK_MinS32;
731 edge->fPrev = &headEdge;
732
733 tailEdge.fPrev = last;
734 tailEdge.fNext = nullptr;
735 tailEdge.fFirstY = kEDGE_TAIL_Y;
736 last->fNext = &tailEdge;
737
738 // now edge is the head of the sorted linklist
739 int stop_y = ir.fBottom;
740 if (clipRect && stop_y > clipRect->fBottom) {
741 stop_y = clipRect->fBottom;
742 }
743 int start_y = ir.fTop;
744 if (clipRect && start_y < clipRect->fTop) {
745 start_y = clipRect->fTop;
746 }
747 walk_simple_edges(&headEdge, blitter, start_y, stop_y);
748 }
749
FillTriangle(const SkPoint pts[],const SkRasterClip & clip,SkBlitter * blitter)750 void SkScan::FillTriangle(const SkPoint pts[], const SkRasterClip& clip,
751 SkBlitter* blitter) {
752 if (clip.isEmpty()) {
753 return;
754 }
755
756 SkRect r;
757 r.setBounds(pts, 3);
758 // If r is too large (larger than can easily fit in SkFixed) then we need perform geometric
759 // clipping. This is a bit of work, so we just call the general FillPath() to handle it.
760 // Use FixedMax/2 as the limit so we can subtract two edges and still store that in Fixed.
761 const SkScalar limit = SK_MaxS16 >> 1;
762 if (!SkRect::MakeLTRB(-limit, -limit, limit, limit).contains(r)) {
763 SkPath path;
764 path.addPoly(pts, 3, false);
765 FillPath(path, clip, blitter);
766 return;
767 }
768
769 SkIRect ir = conservative_round_to_int(r);
770 if (ir.isEmpty() || !SkIRect::Intersects(ir, clip.getBounds())) {
771 return;
772 }
773
774 SkAAClipBlitterWrapper wrap;
775 const SkRegion* clipRgn;
776 if (clip.isBW()) {
777 clipRgn = &clip.bwRgn();
778 } else {
779 wrap.init(clip, blitter);
780 clipRgn = &wrap.getRgn();
781 blitter = wrap.getBlitter();
782 }
783
784 SkScanClipper clipper(blitter, clipRgn, ir);
785 blitter = clipper.getBlitter();
786 if (blitter) {
787 sk_fill_triangle(pts, clipper.getClipRect(), blitter, ir);
788 }
789 }
790