1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2006 The Android Open Source Project
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/core/SkEdge.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/private/SkTo.h"
11cb93a386Sopenharmony_ci#include "src/core/SkFDot6.h"
12cb93a386Sopenharmony_ci#include "src/core/SkMathPriv.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include <utility>
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci/*
17cb93a386Sopenharmony_ci    In setLine, setQuadratic, setCubic, the first thing we do is to convert
18cb93a386Sopenharmony_ci    the points into FDot6. This is modulated by the shift parameter, which
19cb93a386Sopenharmony_ci    will either be 0, or something like 2 for antialiasing.
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci    In the float case, we want to turn the float into .6 by saying pt * 64,
22cb93a386Sopenharmony_ci    or pt * 256 for antialiasing. This is implemented as 1 << (shift + 6).
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    In the fixed case, we want to turn the fixed into .6 by saying pt >> 10,
25cb93a386Sopenharmony_ci    or pt >> 8 for antialiasing. This is implemented as pt >> (10 - shift).
26cb93a386Sopenharmony_ci*/
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_cistatic inline SkFixed SkFDot6ToFixedDiv2(SkFDot6 value) {
29cb93a386Sopenharmony_ci    // we want to return SkFDot6ToFixed(value >> 1), but we don't want to throw
30cb93a386Sopenharmony_ci    // away data in value, so just perform a modify up-shift
31cb93a386Sopenharmony_ci    return SkLeftShift(value, 16 - 6 - 1);
32cb93a386Sopenharmony_ci}
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ciint SkEdge::setLine(const SkPoint& p0, const SkPoint& p1, const SkIRect* clip,
37cb93a386Sopenharmony_ci                    int shift) {
38cb93a386Sopenharmony_ci    SkFDot6 x0, y0, x1, y1;
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci    {
41cb93a386Sopenharmony_ci#ifdef SK_RASTERIZE_EVEN_ROUNDING
42cb93a386Sopenharmony_ci        x0 = SkScalarRoundToFDot6(p0.fX, shift);
43cb93a386Sopenharmony_ci        y0 = SkScalarRoundToFDot6(p0.fY, shift);
44cb93a386Sopenharmony_ci        x1 = SkScalarRoundToFDot6(p1.fX, shift);
45cb93a386Sopenharmony_ci        y1 = SkScalarRoundToFDot6(p1.fY, shift);
46cb93a386Sopenharmony_ci#else
47cb93a386Sopenharmony_ci        float scale = float(1 << (shift + 6));
48cb93a386Sopenharmony_ci        x0 = int(p0.fX * scale);
49cb93a386Sopenharmony_ci        y0 = int(p0.fY * scale);
50cb93a386Sopenharmony_ci        x1 = int(p1.fX * scale);
51cb93a386Sopenharmony_ci        y1 = int(p1.fY * scale);
52cb93a386Sopenharmony_ci#endif
53cb93a386Sopenharmony_ci    }
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci    int winding = 1;
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci    if (y0 > y1) {
58cb93a386Sopenharmony_ci        using std::swap;
59cb93a386Sopenharmony_ci        swap(x0, x1);
60cb93a386Sopenharmony_ci        swap(y0, y1);
61cb93a386Sopenharmony_ci        winding = -1;
62cb93a386Sopenharmony_ci    }
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci    int top = SkFDot6Round(y0);
65cb93a386Sopenharmony_ci    int bot = SkFDot6Round(y1);
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    // are we a zero-height line?
68cb93a386Sopenharmony_ci    if (top == bot) {
69cb93a386Sopenharmony_ci        return 0;
70cb93a386Sopenharmony_ci    }
71cb93a386Sopenharmony_ci    // are we completely above or below the clip?
72cb93a386Sopenharmony_ci    if (clip && (top >= clip->fBottom || bot <= clip->fTop)) {
73cb93a386Sopenharmony_ci        return 0;
74cb93a386Sopenharmony_ci    }
75cb93a386Sopenharmony_ci
76cb93a386Sopenharmony_ci    SkFixed slope = SkFDot6Div(x1 - x0, y1 - y0);
77cb93a386Sopenharmony_ci    const SkFDot6 dy  = SkEdge_Compute_DY(top, y0);
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci    fX          = SkFDot6ToFixed(x0 + SkFixedMul(slope, dy));   // + SK_Fixed1/2
80cb93a386Sopenharmony_ci    fDX         = slope;
81cb93a386Sopenharmony_ci    fFirstY     = top;
82cb93a386Sopenharmony_ci    fLastY      = bot - 1;
83cb93a386Sopenharmony_ci    fCurveCount = 0;
84cb93a386Sopenharmony_ci    fWinding    = SkToS8(winding);
85cb93a386Sopenharmony_ci    fCurveShift = 0;
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_ci    if (clip) {
88cb93a386Sopenharmony_ci        this->chopLineWithClip(*clip);
89cb93a386Sopenharmony_ci    }
90cb93a386Sopenharmony_ci    return 1;
91cb93a386Sopenharmony_ci}
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci// called from a curve subclass
94cb93a386Sopenharmony_ciint SkEdge::updateLine(SkFixed x0, SkFixed y0, SkFixed x1, SkFixed y1)
95cb93a386Sopenharmony_ci{
96cb93a386Sopenharmony_ci    SkASSERT(fWinding == 1 || fWinding == -1);
97cb93a386Sopenharmony_ci    SkASSERT(fCurveCount != 0);
98cb93a386Sopenharmony_ci//    SkASSERT(fCurveShift != 0);
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ci    y0 >>= 10;
101cb93a386Sopenharmony_ci    y1 >>= 10;
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci    SkASSERT(y0 <= y1);
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ci    int top = SkFDot6Round(y0);
106cb93a386Sopenharmony_ci    int bot = SkFDot6Round(y1);
107cb93a386Sopenharmony_ci
108cb93a386Sopenharmony_ci//  SkASSERT(top >= fFirstY);
109cb93a386Sopenharmony_ci
110cb93a386Sopenharmony_ci    // are we a zero-height line?
111cb93a386Sopenharmony_ci    if (top == bot)
112cb93a386Sopenharmony_ci        return 0;
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ci    x0 >>= 10;
115cb93a386Sopenharmony_ci    x1 >>= 10;
116cb93a386Sopenharmony_ci
117cb93a386Sopenharmony_ci    SkFixed slope = SkFDot6Div(x1 - x0, y1 - y0);
118cb93a386Sopenharmony_ci    const SkFDot6 dy  = SkEdge_Compute_DY(top, y0);
119cb93a386Sopenharmony_ci
120cb93a386Sopenharmony_ci    fX          = SkFDot6ToFixed(x0 + SkFixedMul(slope, dy));   // + SK_Fixed1/2
121cb93a386Sopenharmony_ci    fDX         = slope;
122cb93a386Sopenharmony_ci    fFirstY     = top;
123cb93a386Sopenharmony_ci    fLastY      = bot - 1;
124cb93a386Sopenharmony_ci
125cb93a386Sopenharmony_ci    return 1;
126cb93a386Sopenharmony_ci}
127cb93a386Sopenharmony_ci
128cb93a386Sopenharmony_civoid SkEdge::chopLineWithClip(const SkIRect& clip)
129cb93a386Sopenharmony_ci{
130cb93a386Sopenharmony_ci    int top = fFirstY;
131cb93a386Sopenharmony_ci
132cb93a386Sopenharmony_ci    SkASSERT(top < clip.fBottom);
133cb93a386Sopenharmony_ci
134cb93a386Sopenharmony_ci    // clip the line to the top
135cb93a386Sopenharmony_ci    if (top < clip.fTop)
136cb93a386Sopenharmony_ci    {
137cb93a386Sopenharmony_ci        SkASSERT(fLastY >= clip.fTop);
138cb93a386Sopenharmony_ci        fX += fDX * (clip.fTop - top);
139cb93a386Sopenharmony_ci        fFirstY = clip.fTop;
140cb93a386Sopenharmony_ci    }
141cb93a386Sopenharmony_ci}
142cb93a386Sopenharmony_ci
143cb93a386Sopenharmony_ci///////////////////////////////////////////////////////////////////////////////
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ci/*  We store 1<<shift in a (signed) byte, so its maximum value is 1<<6 == 64.
146cb93a386Sopenharmony_ci    Note that this limits the number of lines we use to approximate a curve.
147cb93a386Sopenharmony_ci    If we need to increase this, we need to store fCurveCount in something
148cb93a386Sopenharmony_ci    larger than int8_t.
149cb93a386Sopenharmony_ci*/
150cb93a386Sopenharmony_ci#define MAX_COEFF_SHIFT     6
151cb93a386Sopenharmony_ci
152cb93a386Sopenharmony_cistatic inline SkFDot6 cheap_distance(SkFDot6 dx, SkFDot6 dy)
153cb93a386Sopenharmony_ci{
154cb93a386Sopenharmony_ci    dx = SkAbs32(dx);
155cb93a386Sopenharmony_ci    dy = SkAbs32(dy);
156cb93a386Sopenharmony_ci    // return max + min/2
157cb93a386Sopenharmony_ci    if (dx > dy)
158cb93a386Sopenharmony_ci        dx += dy >> 1;
159cb93a386Sopenharmony_ci    else
160cb93a386Sopenharmony_ci        dx = dy + (dx >> 1);
161cb93a386Sopenharmony_ci    return dx;
162cb93a386Sopenharmony_ci}
163cb93a386Sopenharmony_ci
164cb93a386Sopenharmony_cistatic inline int diff_to_shift(SkFDot6 dx, SkFDot6 dy, int shiftAA = 2)
165cb93a386Sopenharmony_ci{
166cb93a386Sopenharmony_ci    // cheap calc of distance from center of p0-p2 to the center of the curve
167cb93a386Sopenharmony_ci    SkFDot6 dist = cheap_distance(dx, dy);
168cb93a386Sopenharmony_ci
169cb93a386Sopenharmony_ci    // shift down dist (it is currently in dot6)
170cb93a386Sopenharmony_ci    // down by 3 should give us 1/8 pixel accuracy (assuming our dist is accurate...)
171cb93a386Sopenharmony_ci    // this is chosen by heuristic: make it as big as possible (to minimize segments)
172cb93a386Sopenharmony_ci    // ... but small enough so that our curves still look smooth
173cb93a386Sopenharmony_ci    // When shift > 0, we're using AA and everything is scaled up so we can
174cb93a386Sopenharmony_ci    // lower the accuracy.
175cb93a386Sopenharmony_ci    dist = (dist + (1 << 4)) >> (3 + shiftAA);
176cb93a386Sopenharmony_ci
177cb93a386Sopenharmony_ci    // each subdivision (shift value) cuts this dist (error) by 1/4
178cb93a386Sopenharmony_ci    return (32 - SkCLZ(dist)) >> 1;
179cb93a386Sopenharmony_ci}
180cb93a386Sopenharmony_ci
181cb93a386Sopenharmony_cibool SkQuadraticEdge::setQuadraticWithoutUpdate(const SkPoint pts[3], int shift) {
182cb93a386Sopenharmony_ci    SkFDot6 x0, y0, x1, y1, x2, y2;
183cb93a386Sopenharmony_ci
184cb93a386Sopenharmony_ci    {
185cb93a386Sopenharmony_ci#ifdef SK_RASTERIZE_EVEN_ROUNDING
186cb93a386Sopenharmony_ci        x0 = SkScalarRoundToFDot6(pts[0].fX, shift);
187cb93a386Sopenharmony_ci        y0 = SkScalarRoundToFDot6(pts[0].fY, shift);
188cb93a386Sopenharmony_ci        x1 = SkScalarRoundToFDot6(pts[1].fX, shift);
189cb93a386Sopenharmony_ci        y1 = SkScalarRoundToFDot6(pts[1].fY, shift);
190cb93a386Sopenharmony_ci        x2 = SkScalarRoundToFDot6(pts[2].fX, shift);
191cb93a386Sopenharmony_ci        y2 = SkScalarRoundToFDot6(pts[2].fY, shift);
192cb93a386Sopenharmony_ci#else
193cb93a386Sopenharmony_ci        float scale = float(1 << (shift + 6));
194cb93a386Sopenharmony_ci        x0 = int(pts[0].fX * scale);
195cb93a386Sopenharmony_ci        y0 = int(pts[0].fY * scale);
196cb93a386Sopenharmony_ci        x1 = int(pts[1].fX * scale);
197cb93a386Sopenharmony_ci        y1 = int(pts[1].fY * scale);
198cb93a386Sopenharmony_ci        x2 = int(pts[2].fX * scale);
199cb93a386Sopenharmony_ci        y2 = int(pts[2].fY * scale);
200cb93a386Sopenharmony_ci#endif
201cb93a386Sopenharmony_ci    }
202cb93a386Sopenharmony_ci
203cb93a386Sopenharmony_ci    int winding = 1;
204cb93a386Sopenharmony_ci    if (y0 > y2)
205cb93a386Sopenharmony_ci    {
206cb93a386Sopenharmony_ci        using std::swap;
207cb93a386Sopenharmony_ci        swap(x0, x2);
208cb93a386Sopenharmony_ci        swap(y0, y2);
209cb93a386Sopenharmony_ci        winding = -1;
210cb93a386Sopenharmony_ci    }
211cb93a386Sopenharmony_ci    SkASSERT(y0 <= y1 && y1 <= y2);
212cb93a386Sopenharmony_ci
213cb93a386Sopenharmony_ci    int top = SkFDot6Round(y0);
214cb93a386Sopenharmony_ci    int bot = SkFDot6Round(y2);
215cb93a386Sopenharmony_ci
216cb93a386Sopenharmony_ci    // are we a zero-height quad (line)?
217cb93a386Sopenharmony_ci    if (top == bot)
218cb93a386Sopenharmony_ci        return 0;
219cb93a386Sopenharmony_ci
220cb93a386Sopenharmony_ci    // compute number of steps needed (1 << shift)
221cb93a386Sopenharmony_ci    {
222cb93a386Sopenharmony_ci        SkFDot6 dx = (SkLeftShift(x1, 1) - x0 - x2) >> 2;
223cb93a386Sopenharmony_ci        SkFDot6 dy = (SkLeftShift(y1, 1) - y0 - y2) >> 2;
224cb93a386Sopenharmony_ci        // This is a little confusing:
225cb93a386Sopenharmony_ci        // before this line, shift is the scale up factor for AA;
226cb93a386Sopenharmony_ci        // after this line, shift is the fCurveShift.
227cb93a386Sopenharmony_ci        shift = diff_to_shift(dx, dy, shift);
228cb93a386Sopenharmony_ci        SkASSERT(shift >= 0);
229cb93a386Sopenharmony_ci    }
230cb93a386Sopenharmony_ci    // need at least 1 subdivision for our bias trick
231cb93a386Sopenharmony_ci    if (shift == 0) {
232cb93a386Sopenharmony_ci        shift = 1;
233cb93a386Sopenharmony_ci    } else if (shift > MAX_COEFF_SHIFT) {
234cb93a386Sopenharmony_ci        shift = MAX_COEFF_SHIFT;
235cb93a386Sopenharmony_ci    }
236cb93a386Sopenharmony_ci
237cb93a386Sopenharmony_ci    fWinding    = SkToS8(winding);
238cb93a386Sopenharmony_ci    //fCubicDShift only set for cubics
239cb93a386Sopenharmony_ci    fCurveCount = SkToS8(1 << shift);
240cb93a386Sopenharmony_ci
241cb93a386Sopenharmony_ci    /*
242cb93a386Sopenharmony_ci     *  We want to reformulate into polynomial form, to make it clear how we
243cb93a386Sopenharmony_ci     *  should forward-difference.
244cb93a386Sopenharmony_ci     *
245cb93a386Sopenharmony_ci     *  p0 (1 - t)^2 + p1 t(1 - t) + p2 t^2 ==> At^2 + Bt + C
246cb93a386Sopenharmony_ci     *
247cb93a386Sopenharmony_ci     *  A = p0 - 2p1 + p2
248cb93a386Sopenharmony_ci     *  B = 2(p1 - p0)
249cb93a386Sopenharmony_ci     *  C = p0
250cb93a386Sopenharmony_ci     *
251cb93a386Sopenharmony_ci     *  Our caller must have constrained our inputs (p0..p2) to all fit into
252cb93a386Sopenharmony_ci     *  16.16. However, as seen above, we sometimes compute values that can be
253cb93a386Sopenharmony_ci     *  larger (e.g. B = 2*(p1 - p0)). To guard against overflow, we will store
254cb93a386Sopenharmony_ci     *  A and B at 1/2 of their actual value, and just apply a 2x scale during
255cb93a386Sopenharmony_ci     *  application in updateQuadratic(). Hence we store (shift - 1) in
256cb93a386Sopenharmony_ci     *  fCurveShift.
257cb93a386Sopenharmony_ci     */
258cb93a386Sopenharmony_ci
259cb93a386Sopenharmony_ci    fCurveShift = SkToU8(shift - 1);
260cb93a386Sopenharmony_ci
261cb93a386Sopenharmony_ci    SkFixed A = SkFDot6ToFixedDiv2(x0 - x1 - x1 + x2);  // 1/2 the real value
262cb93a386Sopenharmony_ci    SkFixed B = SkFDot6ToFixed(x1 - x0);                // 1/2 the real value
263cb93a386Sopenharmony_ci
264cb93a386Sopenharmony_ci    fQx     = SkFDot6ToFixed(x0);
265cb93a386Sopenharmony_ci    fQDx    = B + (A >> shift);     // biased by shift
266cb93a386Sopenharmony_ci    fQDDx   = A >> (shift - 1);     // biased by shift
267cb93a386Sopenharmony_ci
268cb93a386Sopenharmony_ci    A = SkFDot6ToFixedDiv2(y0 - y1 - y1 + y2);  // 1/2 the real value
269cb93a386Sopenharmony_ci    B = SkFDot6ToFixed(y1 - y0);                // 1/2 the real value
270cb93a386Sopenharmony_ci
271cb93a386Sopenharmony_ci    fQy     = SkFDot6ToFixed(y0);
272cb93a386Sopenharmony_ci    fQDy    = B + (A >> shift);     // biased by shift
273cb93a386Sopenharmony_ci    fQDDy   = A >> (shift - 1);     // biased by shift
274cb93a386Sopenharmony_ci
275cb93a386Sopenharmony_ci    fQLastX = SkFDot6ToFixed(x2);
276cb93a386Sopenharmony_ci    fQLastY = SkFDot6ToFixed(y2);
277cb93a386Sopenharmony_ci
278cb93a386Sopenharmony_ci    return true;
279cb93a386Sopenharmony_ci}
280cb93a386Sopenharmony_ci
281cb93a386Sopenharmony_ciint SkQuadraticEdge::setQuadratic(const SkPoint pts[3], int shift) {
282cb93a386Sopenharmony_ci    if (!setQuadraticWithoutUpdate(pts, shift)) {
283cb93a386Sopenharmony_ci        return 0;
284cb93a386Sopenharmony_ci    }
285cb93a386Sopenharmony_ci    return this->updateQuadratic();
286cb93a386Sopenharmony_ci}
287cb93a386Sopenharmony_ci
288cb93a386Sopenharmony_ciint SkQuadraticEdge::updateQuadratic()
289cb93a386Sopenharmony_ci{
290cb93a386Sopenharmony_ci    int     success;
291cb93a386Sopenharmony_ci    int     count = fCurveCount;
292cb93a386Sopenharmony_ci    SkFixed oldx = fQx;
293cb93a386Sopenharmony_ci    SkFixed oldy = fQy;
294cb93a386Sopenharmony_ci    SkFixed dx = fQDx;
295cb93a386Sopenharmony_ci    SkFixed dy = fQDy;
296cb93a386Sopenharmony_ci    SkFixed newx, newy;
297cb93a386Sopenharmony_ci    int     shift = fCurveShift;
298cb93a386Sopenharmony_ci
299cb93a386Sopenharmony_ci    SkASSERT(count > 0);
300cb93a386Sopenharmony_ci
301cb93a386Sopenharmony_ci    do {
302cb93a386Sopenharmony_ci        if (--count > 0)
303cb93a386Sopenharmony_ci        {
304cb93a386Sopenharmony_ci            newx    = oldx + (dx >> shift);
305cb93a386Sopenharmony_ci            dx    += fQDDx;
306cb93a386Sopenharmony_ci            newy    = oldy + (dy >> shift);
307cb93a386Sopenharmony_ci            dy    += fQDDy;
308cb93a386Sopenharmony_ci        }
309cb93a386Sopenharmony_ci        else    // last segment
310cb93a386Sopenharmony_ci        {
311cb93a386Sopenharmony_ci            newx    = fQLastX;
312cb93a386Sopenharmony_ci            newy    = fQLastY;
313cb93a386Sopenharmony_ci        }
314cb93a386Sopenharmony_ci        success = this->updateLine(oldx, oldy, newx, newy);
315cb93a386Sopenharmony_ci        oldx = newx;
316cb93a386Sopenharmony_ci        oldy = newy;
317cb93a386Sopenharmony_ci    } while (count > 0 && !success);
318cb93a386Sopenharmony_ci
319cb93a386Sopenharmony_ci    fQx         = newx;
320cb93a386Sopenharmony_ci    fQy         = newy;
321cb93a386Sopenharmony_ci    fQDx        = dx;
322cb93a386Sopenharmony_ci    fQDy        = dy;
323cb93a386Sopenharmony_ci    fCurveCount = SkToS8(count);
324cb93a386Sopenharmony_ci    return success;
325cb93a386Sopenharmony_ci}
326cb93a386Sopenharmony_ci
327cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////
328cb93a386Sopenharmony_ci
329cb93a386Sopenharmony_cistatic inline int SkFDot6UpShift(SkFDot6 x, int upShift) {
330cb93a386Sopenharmony_ci    SkASSERT((SkLeftShift(x, upShift) >> upShift) == x);
331cb93a386Sopenharmony_ci    return SkLeftShift(x, upShift);
332cb93a386Sopenharmony_ci}
333cb93a386Sopenharmony_ci
334cb93a386Sopenharmony_ci/*  f(1/3) = (8a + 12b + 6c + d) / 27
335cb93a386Sopenharmony_ci    f(2/3) = (a + 6b + 12c + 8d) / 27
336cb93a386Sopenharmony_ci
337cb93a386Sopenharmony_ci    f(1/3)-b = (8a - 15b + 6c + d) / 27
338cb93a386Sopenharmony_ci    f(2/3)-c = (a + 6b - 15c + 8d) / 27
339cb93a386Sopenharmony_ci
340cb93a386Sopenharmony_ci    use 16/512 to approximate 1/27
341cb93a386Sopenharmony_ci*/
342cb93a386Sopenharmony_cistatic SkFDot6 cubic_delta_from_line(SkFDot6 a, SkFDot6 b, SkFDot6 c, SkFDot6 d)
343cb93a386Sopenharmony_ci{
344cb93a386Sopenharmony_ci    // since our parameters may be negative, we don't use << to avoid ASAN warnings
345cb93a386Sopenharmony_ci    SkFDot6 oneThird = (a*8 - b*15 + 6*c + d) * 19 >> 9;
346cb93a386Sopenharmony_ci    SkFDot6 twoThird = (a + 6*b - c*15 + d*8) * 19 >> 9;
347cb93a386Sopenharmony_ci
348cb93a386Sopenharmony_ci    return std::max(SkAbs32(oneThird), SkAbs32(twoThird));
349cb93a386Sopenharmony_ci}
350cb93a386Sopenharmony_ci
351cb93a386Sopenharmony_cibool SkCubicEdge::setCubicWithoutUpdate(const SkPoint pts[4], int shift, bool sortY) {
352cb93a386Sopenharmony_ci    SkFDot6 x0, y0, x1, y1, x2, y2, x3, y3;
353cb93a386Sopenharmony_ci
354cb93a386Sopenharmony_ci    {
355cb93a386Sopenharmony_ci#ifdef SK_RASTERIZE_EVEN_ROUNDING
356cb93a386Sopenharmony_ci        x0 = SkScalarRoundToFDot6(pts[0].fX, shift);
357cb93a386Sopenharmony_ci        y0 = SkScalarRoundToFDot6(pts[0].fY, shift);
358cb93a386Sopenharmony_ci        x1 = SkScalarRoundToFDot6(pts[1].fX, shift);
359cb93a386Sopenharmony_ci        y1 = SkScalarRoundToFDot6(pts[1].fY, shift);
360cb93a386Sopenharmony_ci        x2 = SkScalarRoundToFDot6(pts[2].fX, shift);
361cb93a386Sopenharmony_ci        y2 = SkScalarRoundToFDot6(pts[2].fY, shift);
362cb93a386Sopenharmony_ci        x3 = SkScalarRoundToFDot6(pts[3].fX, shift);
363cb93a386Sopenharmony_ci        y3 = SkScalarRoundToFDot6(pts[3].fY, shift);
364cb93a386Sopenharmony_ci#else
365cb93a386Sopenharmony_ci        float scale = float(1 << (shift + 6));
366cb93a386Sopenharmony_ci        x0 = int(pts[0].fX * scale);
367cb93a386Sopenharmony_ci        y0 = int(pts[0].fY * scale);
368cb93a386Sopenharmony_ci        x1 = int(pts[1].fX * scale);
369cb93a386Sopenharmony_ci        y1 = int(pts[1].fY * scale);
370cb93a386Sopenharmony_ci        x2 = int(pts[2].fX * scale);
371cb93a386Sopenharmony_ci        y2 = int(pts[2].fY * scale);
372cb93a386Sopenharmony_ci        x3 = int(pts[3].fX * scale);
373cb93a386Sopenharmony_ci        y3 = int(pts[3].fY * scale);
374cb93a386Sopenharmony_ci#endif
375cb93a386Sopenharmony_ci    }
376cb93a386Sopenharmony_ci
377cb93a386Sopenharmony_ci    int winding = 1;
378cb93a386Sopenharmony_ci    if (sortY && y0 > y3)
379cb93a386Sopenharmony_ci    {
380cb93a386Sopenharmony_ci        using std::swap;
381cb93a386Sopenharmony_ci        swap(x0, x3);
382cb93a386Sopenharmony_ci        swap(x1, x2);
383cb93a386Sopenharmony_ci        swap(y0, y3);
384cb93a386Sopenharmony_ci        swap(y1, y2);
385cb93a386Sopenharmony_ci        winding = -1;
386cb93a386Sopenharmony_ci    }
387cb93a386Sopenharmony_ci
388cb93a386Sopenharmony_ci    int top = SkFDot6Round(y0);
389cb93a386Sopenharmony_ci    int bot = SkFDot6Round(y3);
390cb93a386Sopenharmony_ci
391cb93a386Sopenharmony_ci    // are we a zero-height cubic (line)?
392cb93a386Sopenharmony_ci    if (sortY && top == bot)
393cb93a386Sopenharmony_ci        return 0;
394cb93a386Sopenharmony_ci
395cb93a386Sopenharmony_ci    // compute number of steps needed (1 << shift)
396cb93a386Sopenharmony_ci    {
397cb93a386Sopenharmony_ci        // Can't use (center of curve - center of baseline), since center-of-curve
398cb93a386Sopenharmony_ci        // need not be the max delta from the baseline (it could even be coincident)
399cb93a386Sopenharmony_ci        // so we try just looking at the two off-curve points
400cb93a386Sopenharmony_ci        SkFDot6 dx = cubic_delta_from_line(x0, x1, x2, x3);
401cb93a386Sopenharmony_ci        SkFDot6 dy = cubic_delta_from_line(y0, y1, y2, y3);
402cb93a386Sopenharmony_ci        // add 1 (by observation)
403cb93a386Sopenharmony_ci        shift = diff_to_shift(dx, dy) + 1;
404cb93a386Sopenharmony_ci    }
405cb93a386Sopenharmony_ci    // need at least 1 subdivision for our bias trick
406cb93a386Sopenharmony_ci    SkASSERT(shift > 0);
407cb93a386Sopenharmony_ci    if (shift > MAX_COEFF_SHIFT) {
408cb93a386Sopenharmony_ci        shift = MAX_COEFF_SHIFT;
409cb93a386Sopenharmony_ci    }
410cb93a386Sopenharmony_ci
411cb93a386Sopenharmony_ci    /*  Since our in coming data is initially shifted down by 10 (or 8 in
412cb93a386Sopenharmony_ci        antialias). That means the most we can shift up is 8. However, we
413cb93a386Sopenharmony_ci        compute coefficients with a 3*, so the safest upshift is really 6
414cb93a386Sopenharmony_ci    */
415cb93a386Sopenharmony_ci    int upShift = 6;    // largest safe value
416cb93a386Sopenharmony_ci    int downShift = shift + upShift - 10;
417cb93a386Sopenharmony_ci    if (downShift < 0) {
418cb93a386Sopenharmony_ci        downShift = 0;
419cb93a386Sopenharmony_ci        upShift = 10 - shift;
420cb93a386Sopenharmony_ci    }
421cb93a386Sopenharmony_ci
422cb93a386Sopenharmony_ci    fWinding    = SkToS8(winding);
423cb93a386Sopenharmony_ci    fCurveCount = SkToS8(SkLeftShift(-1, shift));
424cb93a386Sopenharmony_ci    fCurveShift = SkToU8(shift);
425cb93a386Sopenharmony_ci    fCubicDShift = SkToU8(downShift);
426cb93a386Sopenharmony_ci
427cb93a386Sopenharmony_ci    SkFixed B = SkFDot6UpShift(3 * (x1 - x0), upShift);
428cb93a386Sopenharmony_ci    SkFixed C = SkFDot6UpShift(3 * (x0 - x1 - x1 + x2), upShift);
429cb93a386Sopenharmony_ci    SkFixed D = SkFDot6UpShift(x3 + 3 * (x1 - x2) - x0, upShift);
430cb93a386Sopenharmony_ci
431cb93a386Sopenharmony_ci    fCx     = SkFDot6ToFixed(x0);
432cb93a386Sopenharmony_ci    fCDx    = B + (C >> shift) + (D >> 2*shift);    // biased by shift
433cb93a386Sopenharmony_ci    fCDDx   = 2*C + (3*D >> (shift - 1));           // biased by 2*shift
434cb93a386Sopenharmony_ci    fCDDDx  = 3*D >> (shift - 1);                   // biased by 2*shift
435cb93a386Sopenharmony_ci
436cb93a386Sopenharmony_ci    B = SkFDot6UpShift(3 * (y1 - y0), upShift);
437cb93a386Sopenharmony_ci    C = SkFDot6UpShift(3 * (y0 - y1 - y1 + y2), upShift);
438cb93a386Sopenharmony_ci    D = SkFDot6UpShift(y3 + 3 * (y1 - y2) - y0, upShift);
439cb93a386Sopenharmony_ci
440cb93a386Sopenharmony_ci    fCy     = SkFDot6ToFixed(y0);
441cb93a386Sopenharmony_ci    fCDy    = B + (C >> shift) + (D >> 2*shift);    // biased by shift
442cb93a386Sopenharmony_ci    fCDDy   = 2*C + (3*D >> (shift - 1));           // biased by 2*shift
443cb93a386Sopenharmony_ci    fCDDDy  = 3*D >> (shift - 1);                   // biased by 2*shift
444cb93a386Sopenharmony_ci
445cb93a386Sopenharmony_ci    fCLastX = SkFDot6ToFixed(x3);
446cb93a386Sopenharmony_ci    fCLastY = SkFDot6ToFixed(y3);
447cb93a386Sopenharmony_ci
448cb93a386Sopenharmony_ci    return true;
449cb93a386Sopenharmony_ci}
450cb93a386Sopenharmony_ci
451cb93a386Sopenharmony_ciint SkCubicEdge::setCubic(const SkPoint pts[4], int shift) {
452cb93a386Sopenharmony_ci    if (!this->setCubicWithoutUpdate(pts, shift)) {
453cb93a386Sopenharmony_ci        return 0;
454cb93a386Sopenharmony_ci    }
455cb93a386Sopenharmony_ci    return this->updateCubic();
456cb93a386Sopenharmony_ci}
457cb93a386Sopenharmony_ci
458cb93a386Sopenharmony_ciint SkCubicEdge::updateCubic()
459cb93a386Sopenharmony_ci{
460cb93a386Sopenharmony_ci    int     success;
461cb93a386Sopenharmony_ci    int     count = fCurveCount;
462cb93a386Sopenharmony_ci    SkFixed oldx = fCx;
463cb93a386Sopenharmony_ci    SkFixed oldy = fCy;
464cb93a386Sopenharmony_ci    SkFixed newx, newy;
465cb93a386Sopenharmony_ci    const int ddshift = fCurveShift;
466cb93a386Sopenharmony_ci    const int dshift = fCubicDShift;
467cb93a386Sopenharmony_ci
468cb93a386Sopenharmony_ci    SkASSERT(count < 0);
469cb93a386Sopenharmony_ci
470cb93a386Sopenharmony_ci    do {
471cb93a386Sopenharmony_ci        if (++count < 0)
472cb93a386Sopenharmony_ci        {
473cb93a386Sopenharmony_ci            newx    = oldx + (fCDx >> dshift);
474cb93a386Sopenharmony_ci            fCDx    += fCDDx >> ddshift;
475cb93a386Sopenharmony_ci            fCDDx   += fCDDDx;
476cb93a386Sopenharmony_ci
477cb93a386Sopenharmony_ci            newy    = oldy + (fCDy >> dshift);
478cb93a386Sopenharmony_ci            fCDy    += fCDDy >> ddshift;
479cb93a386Sopenharmony_ci            fCDDy   += fCDDDy;
480cb93a386Sopenharmony_ci        }
481cb93a386Sopenharmony_ci        else    // last segment
482cb93a386Sopenharmony_ci        {
483cb93a386Sopenharmony_ci        //  SkDebugf("LastX err=%d, LastY err=%d\n", (oldx + (fCDx >> shift) - fLastX), (oldy + (fCDy >> shift) - fLastY));
484cb93a386Sopenharmony_ci            newx    = fCLastX;
485cb93a386Sopenharmony_ci            newy    = fCLastY;
486cb93a386Sopenharmony_ci        }
487cb93a386Sopenharmony_ci
488cb93a386Sopenharmony_ci        // we want to say SkASSERT(oldy <= newy), but our finite fixedpoint
489cb93a386Sopenharmony_ci        // doesn't always achieve that, so we have to explicitly pin it here.
490cb93a386Sopenharmony_ci        if (newy < oldy) {
491cb93a386Sopenharmony_ci            newy = oldy;
492cb93a386Sopenharmony_ci        }
493cb93a386Sopenharmony_ci
494cb93a386Sopenharmony_ci        success = this->updateLine(oldx, oldy, newx, newy);
495cb93a386Sopenharmony_ci        oldx = newx;
496cb93a386Sopenharmony_ci        oldy = newy;
497cb93a386Sopenharmony_ci    } while (count < 0 && !success);
498cb93a386Sopenharmony_ci
499cb93a386Sopenharmony_ci    fCx         = newx;
500cb93a386Sopenharmony_ci    fCy         = newy;
501cb93a386Sopenharmony_ci    fCurveCount = SkToS8(count);
502cb93a386Sopenharmony_ci    return success;
503cb93a386Sopenharmony_ci}
504