2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkScanPriv.h"
11 #include "SkEdgeBuilder.h"
12 #include "SkGeometry.h"
14 #include "SkQuadClipper.h"
15 #include "SkRasterClip.h"
17 #include "SkTemplates.h"
20 #ifdef SK_USE_LEGACY_AA_COVERAGE
21 #define SK_USE_STD_SORT_FOR_EDGES
24 #define kEDGE_HEAD_Y SK_MinS32
25 #define kEDGE_TAIL_Y SK_MaxS32
28 static void validate_sort(const SkEdge* edge) {
31 while (edge->fFirstY != SK_MaxS32) {
33 SkASSERT(y <= edge->fFirstY);
40 #define validate_sort(edge)
43 static inline void remove_edge(SkEdge* edge) {
44 edge->fPrev->fNext = edge->fNext;
45 edge->fNext->fPrev = edge->fPrev;
48 static inline void swap_edges(SkEdge* prev, SkEdge* next) {
49 SkASSERT(prev->fNext == next && next->fPrev == prev);
51 // remove prev from the list
52 prev->fPrev->fNext = next;
53 next->fPrev = prev->fPrev;
55 // insert prev after next
56 prev->fNext = next->fNext;
57 next->fNext->fPrev = prev;
62 static void backward_insert_edge_based_on_x(SkEdge* edge SkDECLAREPARAM(int, curr_y)) {
66 SkEdge* prev = edge->fPrev;
68 // add 1 to curr_y since we may have added new edges (built from curves)
69 // that start on the next scanline
70 SkASSERT(prev && prev->fFirstY <= curr_y + 1);
75 swap_edges(prev, edge);
79 static void insert_new_edges(SkEdge* newEdge, int curr_y) {
80 SkASSERT(newEdge->fFirstY >= curr_y);
82 while (newEdge->fFirstY == curr_y) {
83 SkEdge* next = newEdge->fNext;
84 backward_insert_edge_based_on_x(newEdge SkPARAM(curr_y));
90 static void validate_edges_for_y(const SkEdge* edge, int curr_y) {
91 while (edge->fFirstY <= curr_y) {
92 SkASSERT(edge->fPrev && edge->fNext);
93 SkASSERT(edge->fPrev->fNext == edge);
94 SkASSERT(edge->fNext->fPrev == edge);
95 SkASSERT(edge->fFirstY <= edge->fLastY);
97 SkASSERT(edge->fPrev->fX <= edge->fX);
102 #define validate_edges_for_y(edge, curr_y)
105 #if defined _WIN32 && _MSC_VER >= 1300 // disable warning : local variable used without having been initialized
106 #pragma warning ( push )
107 #pragma warning ( disable : 4701 )
110 typedef void (*PrePostProc)(SkBlitter* blitter, int y, bool isStartOfScanline);
111 #define PREPOST_START true
112 #define PREPOST_END false
114 static void walk_edges(SkEdge* prevHead, SkPath::FillType fillType,
115 SkBlitter* blitter, int start_y, int stop_y,
117 validate_sort(prevHead->fNext);
119 int curr_y = start_y;
120 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
121 int windingMask = (fillType & 1) ? 1 : -1;
125 int left SK_INIT_TO_AVOID_WARNING;
126 bool in_interval = false;
127 SkEdge* currE = prevHead->fNext;
128 SkFixed prevX = prevHead->fX;
130 validate_edges_for_y(currE, curr_y);
133 proc(blitter, curr_y, PREPOST_START); // pre-proc
136 while (currE->fFirstY <= curr_y) {
137 SkASSERT(currE->fLastY >= curr_y);
139 int x = SkFixedRoundToInt(currE->fX);
140 w += currE->fWinding;
141 if ((w & windingMask) == 0) { // we finished an interval
142 SkASSERT(in_interval);
143 int width = x - left;
144 SkASSERT(width >= 0);
146 blitter->blitH(left, curr_y, width);
148 } else if (!in_interval) {
153 SkEdge* next = currE->fNext;
156 if (currE->fLastY == curr_y) { // are we done with this edge?
157 if (currE->fCurveCount < 0) {
158 if (((SkCubicEdge*)currE)->updateCubic()) {
159 SkASSERT(currE->fFirstY == curr_y + 1);
164 } else if (currE->fCurveCount > 0) {
165 if (((SkQuadraticEdge*)currE)->updateQuadratic()) {
172 SkASSERT(currE->fLastY > curr_y);
173 newX = currE->fX + currE->fDX;
176 if (newX < prevX) { // ripple currE backwards until it is x-sorted
177 backward_insert_edge_based_on_x(currE SkPARAM(curr_y));
187 proc(blitter, curr_y, PREPOST_END); // post-proc
191 if (curr_y >= stop_y) {
194 // now currE points to the first edge with a Yint larger than curr_y
195 insert_new_edges(currE, curr_y);
199 // return true if we're done with this edge
200 static bool update_edge(SkEdge* edge, int last_y) {
201 SkASSERT(edge->fLastY >= last_y);
202 if (last_y == edge->fLastY) {
203 if (edge->fCurveCount < 0) {
204 if (((SkCubicEdge*)edge)->updateCubic()) {
205 SkASSERT(edge->fFirstY == last_y + 1);
208 } else if (edge->fCurveCount > 0) {
209 if (((SkQuadraticEdge*)edge)->updateQuadratic()) {
210 SkASSERT(edge->fFirstY == last_y + 1);
219 static void walk_convex_edges(SkEdge* prevHead, SkPath::FillType,
220 SkBlitter* blitter, int start_y, int stop_y,
222 validate_sort(prevHead->fNext);
224 SkEdge* leftE = prevHead->fNext;
225 SkEdge* riteE = leftE->fNext;
226 SkEdge* currE = riteE->fNext;
229 int local_top = leftE->fFirstY;
230 SkASSERT(local_top == riteE->fFirstY);
232 // our edge choppers for curves can result in the initial edges
233 // not lining up, so we take the max.
234 int local_top = SkMax32(leftE->fFirstY, riteE->fFirstY);
236 SkASSERT(local_top >= start_y);
239 SkASSERT(leftE->fFirstY <= stop_y);
240 SkASSERT(riteE->fFirstY <= stop_y);
242 if (leftE->fX > riteE->fX || (leftE->fX == riteE->fX &&
243 leftE->fDX > riteE->fDX)) {
244 SkTSwap(leftE, riteE);
247 int local_bot = SkMin32(leftE->fLastY, riteE->fLastY);
248 local_bot = SkMin32(local_bot, stop_y - 1);
249 SkASSERT(local_top <= local_bot);
251 SkFixed left = leftE->fX;
252 SkFixed dLeft = leftE->fDX;
253 SkFixed rite = riteE->fX;
254 SkFixed dRite = riteE->fDX;
255 int count = local_bot - local_top;
256 SkASSERT(count >= 0);
257 if (0 == (dLeft | dRite)) {
258 int L = SkFixedRoundToInt(left);
259 int R = SkFixedRoundToInt(rite);
262 blitter->blitRect(L, local_top, R - L, count);
263 left += count * dLeft;
264 rite += count * dRite;
266 local_top = local_bot + 1;
269 int L = SkFixedRoundToInt(left);
270 int R = SkFixedRoundToInt(rite);
272 blitter->blitH(L, local_top, R - L);
277 } while (--count >= 0);
283 if (update_edge(leftE, local_bot)) {
284 if (currE->fFirstY >= stop_y) {
288 currE = currE->fNext;
290 if (update_edge(riteE, local_bot)) {
291 if (currE->fFirstY >= stop_y) {
295 currE = currE->fNext;
301 // check our bottom clip
302 SkASSERT(local_top == local_bot + 1);
303 if (local_top >= stop_y) {
309 ///////////////////////////////////////////////////////////////////////////////
311 // this guy overrides blitH, and will call its proxy blitter with the inverse
312 // of the spans it is given (clipped to the left/right of the cliprect)
314 // used to implement inverse filltypes on paths
316 class InverseBlitter : public SkBlitter {
318 void setBlitter(SkBlitter* blitter, const SkIRect& clip, int shift) {
320 fFirstX = clip.fLeft << shift;
321 fLastX = clip.fRight << shift;
323 void prepost(int y, bool isStart) {
327 int invWidth = fLastX - fPrevX;
329 fBlitter->blitH(fPrevX, y, invWidth);
335 virtual void blitH(int x, int y, int width) {
336 int invWidth = x - fPrevX;
338 fBlitter->blitH(fPrevX, y, invWidth);
343 // we do not expect to get called with these entrypoints
344 virtual void blitAntiH(int, int, const SkAlpha[], const int16_t runs[]) {
345 SkDEBUGFAIL("blitAntiH unexpected");
347 virtual void blitV(int x, int y, int height, SkAlpha alpha) {
348 SkDEBUGFAIL("blitV unexpected");
350 virtual void blitRect(int x, int y, int width, int height) {
351 SkDEBUGFAIL("blitRect unexpected");
353 virtual void blitMask(const SkMask&, const SkIRect& clip) {
354 SkDEBUGFAIL("blitMask unexpected");
356 virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) {
357 SkDEBUGFAIL("justAnOpaqueColor unexpected");
363 int fFirstX, fLastX, fPrevX;
366 static void PrePostInverseBlitterProc(SkBlitter* blitter, int y, bool isStart) {
367 ((InverseBlitter*)blitter)->prepost(y, isStart);
370 ///////////////////////////////////////////////////////////////////////////////
372 #if defined _WIN32 && _MSC_VER >= 1300
373 #pragma warning ( pop )
376 #ifdef SK_USE_STD_SORT_FOR_EDGES
378 static int edge_compare(const void* a, const void* b) {
379 const SkEdge* edgea = *(const SkEdge**)a;
380 const SkEdge* edgeb = *(const SkEdge**)b;
382 int valuea = edgea->fFirstY;
383 int valueb = edgeb->fFirstY;
385 if (valuea == valueb) {
390 // this overflows if valuea >>> valueb or vice-versa
391 // return valuea - valueb;
392 // do perform the slower but safe compares
393 return (valuea < valueb) ? -1 : (valuea > valueb);
397 static bool operator<(const SkEdge& a, const SkEdge& b) {
398 int valuea = a.fFirstY;
399 int valueb = b.fFirstY;
401 if (valuea == valueb) {
406 return valuea < valueb;
410 static SkEdge* sort_edges(SkEdge* list[], int count, SkEdge** last) {
411 #ifdef SK_USE_STD_SORT_FOR_EDGES
412 qsort(list, count, sizeof(SkEdge*), edge_compare);
414 SkTQSort(list, list + count - 1);
417 // now make the edges linked in sorted order
418 for (int i = 1; i < count; i++) {
419 list[i - 1]->fNext = list[i];
420 list[i]->fPrev = list[i - 1];
423 *last = list[count - 1];
427 // clipRect may be null, even though we always have a clip. This indicates that
428 // the path is contained in the clip, and so we can ignore it during the blit
430 // clipRect (if no null) has already been shifted up
432 void sk_fill_path(const SkPath& path, const SkIRect* clipRect, SkBlitter* blitter,
433 int start_y, int stop_y, int shiftEdgesUp,
434 const SkRegion& clipRgn) {
435 SkASSERT(&path && blitter);
437 SkEdgeBuilder builder;
439 int count = builder.build(path, clipRect, shiftEdgesUp);
440 SkEdge** list = builder.edgeList();
443 if (path.isInverseFillType()) {
445 * Since we are in inverse-fill, our caller has already drawn above
446 * our top (start_y) and will draw below our bottom (stop_y). Thus
447 * we need to restrict our drawing to the intersection of the clip
448 * and those two limits.
450 SkIRect rect = clipRgn.getBounds();
451 if (rect.fTop < start_y) {
454 if (rect.fBottom > stop_y) {
455 rect.fBottom = stop_y;
457 if (!rect.isEmpty()) {
458 blitter->blitRect(rect.fLeft << shiftEdgesUp,
459 rect.fTop << shiftEdgesUp,
460 rect.width() << shiftEdgesUp,
461 rect.height() << shiftEdgesUp);
468 SkEdge headEdge, tailEdge, *last;
469 // this returns the first and last edge after they're sorted into a dlink list
470 SkEdge* edge = sort_edges(list, count, &last);
472 headEdge.fPrev = NULL;
473 headEdge.fNext = edge;
474 headEdge.fFirstY = kEDGE_HEAD_Y;
475 headEdge.fX = SK_MinS32;
476 edge->fPrev = &headEdge;
478 tailEdge.fPrev = last;
479 tailEdge.fNext = NULL;
480 tailEdge.fFirstY = kEDGE_TAIL_Y;
481 last->fNext = &tailEdge;
483 // now edge is the head of the sorted linklist
485 start_y <<= shiftEdgesUp;
486 stop_y <<= shiftEdgesUp;
487 if (clipRect && start_y < clipRect->fTop) {
488 start_y = clipRect->fTop;
490 if (clipRect && stop_y > clipRect->fBottom) {
491 stop_y = clipRect->fBottom;
495 PrePostProc proc = NULL;
497 if (path.isInverseFillType()) {
498 ib.setBlitter(blitter, clipRgn.getBounds(), shiftEdgesUp);
500 proc = PrePostInverseBlitterProc;
503 if (path.isConvex() && (NULL == proc)) {
504 walk_convex_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, NULL);
506 walk_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, proc);
510 void sk_blit_above(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
511 const SkIRect& cr = clip.getBounds();
514 tmp.fLeft = cr.fLeft;
515 tmp.fRight = cr.fRight;
517 tmp.fBottom = ir.fTop;
518 if (!tmp.isEmpty()) {
519 blitter->blitRectRegion(tmp, clip);
523 void sk_blit_below(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
524 const SkIRect& cr = clip.getBounds();
527 tmp.fLeft = cr.fLeft;
528 tmp.fRight = cr.fRight;
529 tmp.fTop = ir.fBottom;
530 tmp.fBottom = cr.fBottom;
531 if (!tmp.isEmpty()) {
532 blitter->blitRectRegion(tmp, clip);
536 ///////////////////////////////////////////////////////////////////////////////
539 * If the caller is drawing an inverse-fill path, then it pass true for
540 * skipRejectTest, so we don't abort drawing just because the src bounds (ir)
541 * is outside of the clip.
543 SkScanClipper::SkScanClipper(SkBlitter* blitter, const SkRegion* clip,
544 const SkIRect& ir, bool skipRejectTest) {
545 fBlitter = NULL; // null means blit nothing
549 fClipRect = &clip->getBounds();
550 if (!skipRejectTest && !SkIRect::Intersects(*fClipRect, ir)) { // completely clipped out
554 if (clip->isRect()) {
555 if (fClipRect->contains(ir)) {
558 // only need a wrapper blitter if we're horizontally clipped
559 if (fClipRect->fLeft > ir.fLeft || fClipRect->fRight < ir.fRight) {
560 fRectBlitter.init(blitter, *fClipRect);
561 blitter = &fRectBlitter;
565 fRgnBlitter.init(blitter, clip);
566 blitter = &fRgnBlitter;
572 ///////////////////////////////////////////////////////////////////////////////
574 static bool clip_to_limit(const SkRegion& orig, SkRegion* reduced) {
575 const int32_t limit = 32767;
578 limitR.set(-limit, -limit, limit, limit);
579 if (limitR.contains(orig.getBounds())) {
582 reduced->op(orig, limitR, SkRegion::kIntersect_Op);
586 void SkScan::FillPath(const SkPath& path, const SkRegion& origClip,
587 SkBlitter* blitter) {
588 if (origClip.isEmpty()) {
592 // Our edges are fixed-point, and don't like the bounds of the clip to
593 // exceed that. Here we trim the clip just so we don't overflow later on
594 const SkRegion* clipPtr = &origClip;
596 if (clip_to_limit(origClip, &finiteClip)) {
597 if (finiteClip.isEmpty()) {
600 clipPtr = &finiteClip;
602 // don't reference "origClip" any more, just use clipPtr
605 // We deliberately call dround() instead of round(), since we can't afford to generate a
606 // bounds that is tighter than the corresponding SkEdges. The edge code basically converts
607 // the floats to fixed, and then "rounds". If we called round() instead of dround() here,
608 // we could generate the wrong ir for values like 0.4999997.
609 path.getBounds().dround(&ir);
611 if (path.isInverseFillType()) {
612 blitter->blitRegion(*clipPtr);
617 SkScanClipper clipper(blitter, clipPtr, ir, path.isInverseFillType());
619 blitter = clipper.getBlitter();
621 // we have to keep our calls to blitter in sorted order, so we
622 // must blit the above section first, then the middle, then the bottom.
623 if (path.isInverseFillType()) {
624 sk_blit_above(blitter, ir, *clipPtr);
626 sk_fill_path(path, clipper.getClipRect(), blitter, ir.fTop, ir.fBottom,
628 if (path.isInverseFillType()) {
629 sk_blit_below(blitter, ir, *clipPtr);
632 // what does it mean to not have a blitter if path.isInverseFillType???
636 void SkScan::FillPath(const SkPath& path, const SkIRect& ir,
637 SkBlitter* blitter) {
639 FillPath(path, rgn, blitter);
642 ///////////////////////////////////////////////////////////////////////////////
644 static int build_tri_edges(SkEdge edge[], const SkPoint pts[],
645 const SkIRect* clipRect, SkEdge* list[]) {
646 SkEdge** start = list;
648 if (edge->setLine(pts[0], pts[1], clipRect, 0)) {
650 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
652 if (edge->setLine(pts[1], pts[2], clipRect, 0)) {
654 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
656 if (edge->setLine(pts[2], pts[0], clipRect, 0)) {
659 return (int)(list - start);
663 static void sk_fill_triangle(const SkPoint pts[], const SkIRect* clipRect,
664 SkBlitter* blitter, const SkIRect& ir) {
665 SkASSERT(pts && blitter);
667 SkEdge edgeStorage[3];
670 int count = build_tri_edges(edgeStorage, pts, clipRect, list);
675 SkEdge headEdge, tailEdge, *last;
677 // this returns the first and last edge after they're sorted into a dlink list
678 SkEdge* edge = sort_edges(list, count, &last);
680 headEdge.fPrev = NULL;
681 headEdge.fNext = edge;
682 headEdge.fFirstY = kEDGE_HEAD_Y;
683 headEdge.fX = SK_MinS32;
684 edge->fPrev = &headEdge;
686 tailEdge.fPrev = last;
687 tailEdge.fNext = NULL;
688 tailEdge.fFirstY = kEDGE_TAIL_Y;
689 last->fNext = &tailEdge;
691 // now edge is the head of the sorted linklist
692 int stop_y = ir.fBottom;
693 if (clipRect && stop_y > clipRect->fBottom) {
694 stop_y = clipRect->fBottom;
696 int start_y = ir.fTop;
697 if (clipRect && start_y < clipRect->fTop) {
698 start_y = clipRect->fTop;
700 walk_convex_edges(&headEdge, SkPath::kEvenOdd_FillType, blitter, start_y, stop_y, NULL);
701 // walk_edges(&headEdge, SkPath::kEvenOdd_FillType, blitter, start_y, stop_y, NULL);
704 void SkScan::FillTriangle(const SkPoint pts[], const SkRasterClip& clip,
705 SkBlitter* blitter) {
706 if (clip.isEmpty()) {
714 if (ir.isEmpty() || !SkIRect::Intersects(ir, clip.getBounds())) {
718 SkAAClipBlitterWrapper wrap;
719 const SkRegion* clipRgn;
721 clipRgn = &clip.bwRgn();
723 wrap.init(clip, blitter);
724 clipRgn = &wrap.getRgn();
725 blitter = wrap.getBlitter();
728 SkScanClipper clipper(blitter, clipRgn, ir);
729 blitter = clipper.getBlitter();
730 if (NULL != blitter) {
731 sk_fill_triangle(pts, clipper.getClipRect(), blitter, ir);