blob: cc35baa8fc1469d1916aa5cf25bc8d234f3cf9c1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkEdge_DEFINED
9#define SkEdge_DEFINED
10
reed@google.comb6a2ba72012-08-02 16:08:51 +000011#include "SkFDot6.h"
12#include "SkMath.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040013#include "SkRect.h"
14#include "SkTo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
Ben Wagnerf08d1d02018-06-18 15:11:00 -040016#include <utility>
17
reed@google.come4646db2013-02-22 14:02:58 +000018// This correctly favors the lower-pixel when y0 is on a 1/2 pixel boundary
caryclark3127c992015-12-09 12:02:30 -080019#define SkEdge_Compute_DY(top, y0) (SkLeftShift(top, 6) + 32 - (y0))
reed@google.com09a029b2012-10-30 14:28:03 +000020
reed@android.com8a1c16f2008-12-17 15:59:43 +000021struct SkEdge {
22 enum Type {
23 kLine_Type,
24 kQuad_Type,
25 kCubic_Type
26 };
27
28 SkEdge* fNext;
29 SkEdge* fPrev;
30
31 SkFixed fX;
32 SkFixed fDX;
33 int32_t fFirstY;
34 int32_t fLastY;
35 int8_t fCurveCount; // only used by kQuad(+) and kCubic(-)
36 uint8_t fCurveShift; // appled to all Dx/DDx/DDDx except for fCubicDShift exception
37 uint8_t fCubicDShift; // applied to fCDx and fCDy only in cubic
38 int8_t fWinding; // 1 or -1
39
reede053ca42015-03-19 09:49:09 -070040 int setLine(const SkPoint& p0, const SkPoint& p1, const SkIRect* clip, int shiftUp);
reed@google.comb6a2ba72012-08-02 16:08:51 +000041 // call this version if you know you don't have a clip
42 inline int setLine(const SkPoint& p0, const SkPoint& p1, int shiftUp);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 inline int updateLine(SkFixed ax, SkFixed ay, SkFixed bx, SkFixed by);
44 void chopLineWithClip(const SkIRect& clip);
45
46 inline bool intersectsClip(const SkIRect& clip) const {
47 SkASSERT(fFirstY < clip.fBottom);
48 return fLastY >= clip.fTop;
49 }
50
51#ifdef SK_DEBUG
52 void dump() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 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 +000054 }
55
56 void validate() const {
57 SkASSERT(fPrev && fNext);
58 SkASSERT(fPrev->fNext == this);
59 SkASSERT(fNext->fPrev == this);
60
61 SkASSERT(fFirstY <= fLastY);
62 SkASSERT(SkAbs32(fWinding) == 1);
63 }
64#endif
65};
66
67struct SkQuadraticEdge : public SkEdge {
68 SkFixed fQx, fQy;
69 SkFixed fQDx, fQDy;
70 SkFixed fQDDx, fQDDy;
71 SkFixed fQLastX, fQLastY;
72
liyuqian38911a72016-10-04 11:23:22 -070073 bool setQuadraticWithoutUpdate(const SkPoint pts[3], int shiftUp);
reed@android.comc07d23a2009-02-06 13:30:58 +000074 int setQuadratic(const SkPoint pts[3], int shiftUp);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 int updateQuadratic();
76};
77
78struct SkCubicEdge : public SkEdge {
79 SkFixed fCx, fCy;
80 SkFixed fCDx, fCDy;
81 SkFixed fCDDx, fCDDy;
82 SkFixed fCDDDx, fCDDDy;
83 SkFixed fCLastX, fCLastY;
84
Yuqian Li5eb8fc52017-08-08 14:00:52 -040085 bool setCubicWithoutUpdate(const SkPoint pts[4], int shiftUp, bool sortY = true);
reede053ca42015-03-19 09:49:09 -070086 int setCubic(const SkPoint pts[4], int shiftUp);
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 int updateCubic();
88};
89
reed@google.comb6a2ba72012-08-02 16:08:51 +000090int SkEdge::setLine(const SkPoint& p0, const SkPoint& p1, int shift) {
91 SkFDot6 x0, y0, x1, y1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000092
reed@google.comb6a2ba72012-08-02 16:08:51 +000093 {
george2f265282014-08-29 08:47:55 -070094#ifdef SK_RASTERIZE_EVEN_ROUNDING
95 x0 = SkScalarRoundToFDot6(p0.fX, shift);
96 y0 = SkScalarRoundToFDot6(p0.fY, shift);
97 x1 = SkScalarRoundToFDot6(p1.fX, shift);
98 y1 = SkScalarRoundToFDot6(p1.fY, shift);
99#else
reed@google.comb6a2ba72012-08-02 16:08:51 +0000100 float scale = float(1 << (shift + 6));
101 x0 = int(p0.fX * scale);
102 y0 = int(p0.fY * scale);
103 x1 = int(p1.fX * scale);
104 y1 = int(p1.fY * scale);
george2f265282014-08-29 08:47:55 -0700105#endif
reed@google.comb6a2ba72012-08-02 16:08:51 +0000106 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000107
reed@google.comb6a2ba72012-08-02 16:08:51 +0000108 int winding = 1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109
reed@google.comb6a2ba72012-08-02 16:08:51 +0000110 if (y0 > y1) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400111 using std::swap;
112 swap(x0, x1);
113 swap(y0, y1);
reed@google.comb6a2ba72012-08-02 16:08:51 +0000114 winding = -1;
115 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000116
reed@google.comb6a2ba72012-08-02 16:08:51 +0000117 int top = SkFDot6Round(y0);
118 int bot = SkFDot6Round(y1);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000119
reed@google.comb6a2ba72012-08-02 16:08:51 +0000120 // are we a zero-height line?
121 if (top == bot) {
122 return 0;
123 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000124
reed@google.comb6a2ba72012-08-02 16:08:51 +0000125 SkFixed slope = SkFDot6Div(x1 - x0, y1 - y0);
qiankun.miao1c762162015-03-11 11:12:59 -0700126 const SkFDot6 dy = SkEdge_Compute_DY(top, y0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000127
reed@google.com09a029b2012-10-30 14:28:03 +0000128 fX = SkFDot6ToFixed(x0 + SkFixedMul(slope, dy)); // + SK_Fixed1/2
reed@google.comb6a2ba72012-08-02 16:08:51 +0000129 fDX = slope;
130 fFirstY = top;
131 fLastY = bot - 1;
132 fCurveCount = 0;
133 fWinding = SkToS8(winding);
134 fCurveShift = 0;
135 return 1;
136}
137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138#endif