blob: db6f43085dadf88bd4b28cd128a2280d86935d92 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#ifndef SkEdge_DEFINED
11#define SkEdge_DEFINED
12
13#include "SkRect.h"
reed@google.comb6a2ba72012-08-02 16:08:51 +000014#include "SkFDot6.h"
15#include "SkMath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
reed@google.come4646db2013-02-22 14:02:58 +000017// This correctly favors the lower-pixel when y0 is on a 1/2 pixel boundary
18#define SkEdge_Compute_DY(top, y0) ((top << 6) + 32 - (y0))
reed@google.com09a029b2012-10-30 14:28:03 +000019
reed@android.com8a1c16f2008-12-17 15:59:43 +000020struct SkEdge {
21 enum Type {
22 kLine_Type,
23 kQuad_Type,
24 kCubic_Type
25 };
26
27 SkEdge* fNext;
28 SkEdge* fPrev;
29
30 SkFixed fX;
31 SkFixed fDX;
32 int32_t fFirstY;
33 int32_t fLastY;
34 int8_t fCurveCount; // only used by kQuad(+) and kCubic(-)
35 uint8_t fCurveShift; // appled to all Dx/DDx/DDDx except for fCubicDShift exception
36 uint8_t fCubicDShift; // applied to fCDx and fCDy only in cubic
37 int8_t fWinding; // 1 or -1
38
reede053ca42015-03-19 09:49:09 -070039 int setLine(const SkPoint& p0, const SkPoint& p1, const SkIRect* clip, int shiftUp);
reed@google.comb6a2ba72012-08-02 16:08:51 +000040 // call this version if you know you don't have a clip
41 inline int setLine(const SkPoint& p0, const SkPoint& p1, int shiftUp);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 inline int updateLine(SkFixed ax, SkFixed ay, SkFixed bx, SkFixed by);
43 void chopLineWithClip(const SkIRect& clip);
44
45 inline bool intersectsClip(const SkIRect& clip) const {
46 SkASSERT(fFirstY < clip.fBottom);
47 return fLastY >= clip.fTop;
48 }
49
50#ifdef SK_DEBUG
51 void dump() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 SkDebugf("edge: firstY:%d lastY:%d x:%g dx:%g w:%d\n", fFirstY, fLastY, SkFixedToFloat(fX), SkFixedToFloat(fDX), fWinding);
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 }
54
55 void validate() const {
56 SkASSERT(fPrev && fNext);
57 SkASSERT(fPrev->fNext == this);
58 SkASSERT(fNext->fPrev == this);
59
60 SkASSERT(fFirstY <= fLastY);
61 SkASSERT(SkAbs32(fWinding) == 1);
62 }
63#endif
64};
65
66struct SkQuadraticEdge : public SkEdge {
67 SkFixed fQx, fQy;
68 SkFixed fQDx, fQDy;
69 SkFixed fQDDx, fQDDy;
70 SkFixed fQLastX, fQLastY;
71
reed@android.comc07d23a2009-02-06 13:30:58 +000072 int setQuadratic(const SkPoint pts[3], int shiftUp);
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 int updateQuadratic();
74};
75
76struct SkCubicEdge : public SkEdge {
77 SkFixed fCx, fCy;
78 SkFixed fCDx, fCDy;
79 SkFixed fCDDx, fCDDy;
80 SkFixed fCDDDx, fCDDDy;
81 SkFixed fCLastX, fCLastY;
82
reede053ca42015-03-19 09:49:09 -070083 int setCubic(const SkPoint pts[4], int shiftUp);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 int updateCubic();
85};
86
reed@google.comb6a2ba72012-08-02 16:08:51 +000087int SkEdge::setLine(const SkPoint& p0, const SkPoint& p1, int shift) {
88 SkFDot6 x0, y0, x1, y1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000089
reed@google.comb6a2ba72012-08-02 16:08:51 +000090 {
george2f265282014-08-29 08:47:55 -070091#ifdef SK_RASTERIZE_EVEN_ROUNDING
92 x0 = SkScalarRoundToFDot6(p0.fX, shift);
93 y0 = SkScalarRoundToFDot6(p0.fY, shift);
94 x1 = SkScalarRoundToFDot6(p1.fX, shift);
95 y1 = SkScalarRoundToFDot6(p1.fY, shift);
96#else
reed@google.comb6a2ba72012-08-02 16:08:51 +000097 float scale = float(1 << (shift + 6));
98 x0 = int(p0.fX * scale);
99 y0 = int(p0.fY * scale);
100 x1 = int(p1.fX * scale);
101 y1 = int(p1.fY * scale);
george2f265282014-08-29 08:47:55 -0700102#endif
reed@google.comb6a2ba72012-08-02 16:08:51 +0000103 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000104
reed@google.comb6a2ba72012-08-02 16:08:51 +0000105 int winding = 1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106
reed@google.comb6a2ba72012-08-02 16:08:51 +0000107 if (y0 > y1) {
108 SkTSwap(x0, x1);
109 SkTSwap(y0, y1);
110 winding = -1;
111 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000112
reed@google.comb6a2ba72012-08-02 16:08:51 +0000113 int top = SkFDot6Round(y0);
114 int bot = SkFDot6Round(y1);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115
reed@google.comb6a2ba72012-08-02 16:08:51 +0000116 // are we a zero-height line?
117 if (top == bot) {
118 return 0;
119 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000120
reed@google.comb6a2ba72012-08-02 16:08:51 +0000121 SkFixed slope = SkFDot6Div(x1 - x0, y1 - y0);
qiankun.miao1c762162015-03-11 11:12:59 -0700122 const SkFDot6 dy = SkEdge_Compute_DY(top, y0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000123
reed@google.com09a029b2012-10-30 14:28:03 +0000124 fX = SkFDot6ToFixed(x0 + SkFixedMul(slope, dy)); // + SK_Fixed1/2
reed@google.comb6a2ba72012-08-02 16:08:51 +0000125 fDX = slope;
126 fFirstY = top;
127 fLastY = bot - 1;
128 fCurveCount = 0;
129 fWinding = SkToS8(winding);
130 fCurveShift = 0;
131 return 1;
132}
133
134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135#endif