Debian Codegraph @

src/core/SkAnalyticEdge.cpp

raw
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 "src/core/SkAnalyticEdge.h"
9
10 #include "include/core/SkPoint.h"
11 #include "include/private/base/SkMath.h"
12 #include "include/private/base/SkTo.h"
13 #include "src/base/SkMathPriv.h"
14 #include "src/core/SkFDot6.h"
15
16 #include <algorithm>
17 #include <cstddef>
18 #include <iterator>
19
20 static constexpr int kInverseTableSize = 1024; // SK_FDot6One * 16
21
22 static inline SkFixed quick_inverse(SkFDot6 x) {
23 static const int32_t table[] = {
24 -4096, -4100, -4104, -4108, -4112, -4116, -4120, -4124, -4128, -4132, -4136,
25 -4140, -4144, -4148, -4152, -4156, -4161, -4165, -4169, -4173, -4177, -4181,
26 -4185, -4190, -4194, -4198, -4202, -4206, -4211, -4215, -4219, -4223, -4228,
27 -4232, -4236, -4240, -4245, -4249, -4253, -4258, -4262, -4266, -4271, -4275,
28 -4279, -4284, -4288, -4293, -4297, -4301, -4306, -4310, -4315, -4319, -4324,
29 -4328, -4332, -4337, -4341, -4346, -4350, -4355, -4359, -4364, -4369, -4373,
30 -4378, -4382, -4387, -4391, -4396, -4401, -4405, -4410, -4415, -4419, -4424,
31 -4429, -4433, -4438, -4443, -4447, -4452, -4457, -4462, -4466, -4471, -4476,
32 -4481, -4485, -4490, -4495, -4500, -4505, -4510, -4514, -4519, -4524, -4529,
33 -4534, -4539, -4544, -4549, -4554, -4559, -4563, -4568, -4573, -4578, -4583,
34 -4588, -4593, -4599, -4604, -4609, -4614, -4619, -4624, -4629, -4634, -4639,
35 -4644, -4650, -4655, -4660, -4665, -4670, -4675, -4681, -4686, -4691, -4696,
36 -4702, -4707, -4712, -4718, -4723, -4728, -4733, -4739, -4744, -4750, -4755,
37 -4760, -4766, -4771, -4777, -4782, -4788, -4793, -4798, -4804, -4809, -4815,
38 -4821, -4826, -4832, -4837, -4843, -4848, -4854, -4860, -4865, -4871, -4877,
39 -4882, -4888, -4894, -4899, -4905, -4911, -4917, -4922, -4928, -4934, -4940,
40 -4946, -4951, -4957, -4963, -4969, -4975, -4981, -4987, -4993, -4999, -5005,
41 -5011, -5017, -5023, -5029, -5035, -5041, -5047, -5053, -5059, -5065, -5071,
42 -5077, -5084, -5090, -5096, -5102, -5108, -5115, -5121, -5127, -5133, -5140,
43 -5146, -5152, -5159, -5165, -5171, -5178, -5184, -5190, -5197, -5203, -5210,
44 -5216, -5223, -5229, -5236, -5242, -5249, -5256, -5262, -5269, -5275, -5282,
45 -5289, -5295, -5302, -5309, -5315, -5322, -5329, -5336, -5343, -5349, -5356,
46 -5363, -5370, -5377, -5384, -5391, -5398, -5405, -5412, -5418, -5426, -5433,
47 -5440, -5447, -5454, -5461, -5468, -5475, -5482, -5489, -5497, -5504, -5511,
48 -5518, -5526, -5533, -5540, -5548, -5555, -5562, -5570, -5577, -5584, -5592,
49 -5599, -5607, -5614, -5622, -5629, -5637, -5645, -5652, -5660, -5667, -5675,
50 -5683, -5691, -5698, -5706, -5714, -5722, -5729, -5737, -5745, -5753, -5761,
51 -5769, -5777, -5785, -5793, -5801, -5809, -5817, -5825, -5833, -5841, -5849,
52 -5857, -5866, -5874, -5882, -5890, -5899, -5907, -5915, -5924, -5932, -5940,
53 -5949, -5957, -5966, -5974, -5983, -5991, -6000, -6009, -6017, -6026, -6034,
54 -6043, -6052, -6061, -6069, -6078, -6087, -6096, -6105, -6114, -6123, -6132,
55 -6141, -6150, -6159, -6168, -6177, -6186, -6195, -6204, -6213, -6223, -6232,
56 -6241, -6250, -6260, -6269, -6278, -6288, -6297, -6307, -6316, -6326, -6335,
57 -6345, -6355, -6364, -6374, -6384, -6393, -6403, -6413, -6423, -6432, -6442,
58 -6452, -6462, -6472, -6482, -6492, -6502, -6512, -6523, -6533, -6543, -6553,
59 -6563, -6574, -6584, -6594, -6605, -6615, -6626, -6636, -6647, -6657, -6668,
60 -6678, -6689, -6700, -6710, -6721, -6732, -6743, -6754, -6765, -6775, -6786,
61 -6797, -6808, -6820, -6831, -6842, -6853, -6864, -6875, -6887, -6898, -6909,
62 -6921, -6932, -6944, -6955, -6967, -6978, -6990, -7002, -7013, -7025, -7037,
63 -7049, -7061, -7073, -7084, -7096, -7108, -7121, -7133, -7145, -7157, -7169,
64 -7182, -7194, -7206, -7219, -7231, -7244, -7256, -7269, -7281, -7294, -7307,
65 -7319, -7332, -7345, -7358, -7371, -7384, -7397, -7410, -7423, -7436, -7449,
66 -7463, -7476, -7489, -7503, -7516, -7530, -7543, -7557, -7570, -7584, -7598,
67 -7612, -7626, -7639, -7653, -7667, -7681, -7695, -7710, -7724, -7738, -7752,
68 -7767, -7781, -7796, -7810, -7825, -7839, -7854, -7869, -7884, -7898, -7913,
69 -7928, -7943, -7958, -7973, -7989, -8004, -8019, -8035, -8050, -8065, -8081,
70 -8097, -8112, -8128, -8144, -8160, -8176, -8192, -8208, -8224, -8240, -8256,
71 -8272, -8289, -8305, -8322, -8338, -8355, -8371, -8388, -8405, -8422, -8439,
72 -8456, -8473, -8490, -8507, -8525, -8542, -8559, -8577, -8594, -8612, -8630,
73 -8648, -8665, -8683, -8701, -8719, -8738, -8756, -8774, -8793, -8811, -8830,
74 -8848, -8867, -8886, -8905, -8924, -8943, -8962, -8981, -9000, -9020, -9039,
75 -9058, -9078, -9098, -9118, -9137, -9157, -9177, -9198, -9218, -9238, -9258,
76 -9279, -9300, -9320, -9341, -9362, -9383, -9404, -9425, -9446, -9467, -9489,
77 -9510, -9532, -9554, -9576, -9597, -9619, -9642, -9664, -9686, -9709, -9731,
78 -9754, -9776, -9799, -9822, -9845, -9868, -9892, -9915, -9939, -9962, -9986,
79 -10010, -10034, -10058, -10082, -10106, -10131, -10155, -10180, -10205, -10230,
80 -10255, -10280, -10305, -10330, -10356, -10381, -10407, -10433, -10459, -10485,
81 -10512, -10538, -10564, -10591, -10618, -10645, -10672, -10699, -10727, -10754,
82 -10782, -10810, -10837, -10866, -10894, -10922, -10951, -10979, -11008, -11037,
83 -11066, -11096, -11125, -11155, -11184, -11214, -11244, -11275, -11305, -11335,
84 -11366, -11397, -11428, -11459, -11491, -11522, -11554, -11586, -11618, -11650,
85 -11683, -11715, -11748, -11781, -11814, -11848, -11881, -11915, -11949, -11983,
86 -12018, -12052, -12087, -12122, -12157, -12192, -12228, -12264, -12300, -12336,
87 -12372, -12409, -12446, -12483, -12520, -12557, -12595, -12633, -12671, -12710,
88 -12748, -12787, -12826, -12865, -12905, -12945, -12985, -13025, -13066, -13107,
89 -13148, -13189, -13231, -13273, -13315, -13357, -13400, -13443, -13486, -13530,
90 -13573, -13617, -13662, -13706, -13751, -13797, -13842, -13888, -13934, -13981,
91 -14027, -14074, -14122, -14169, -14217, -14266, -14315, -14364, -14413, -14463,
92 -14513, -14563, -14614, -14665, -14716, -14768, -14820, -14873, -14926, -14979,
93 -15033, -15087, -15141, -15196, -15252, -15307, -15363, -15420, -15477, -15534,
94 -15592, -15650, -15709, -15768, -15827, -15887, -15947, -16008, -16070, -16131,
95 -16194, -16256, -16320, -16384, -16448, -16513, -16578, -16644, -16710, -16777,
96 -16844, -16912, -16980, -17050, -17119, -17189, -17260, -17331, -17403, -17476,
97 -17549, -17623, -17697, -17772, -17848, -17924, -18001, -18078, -18157, -18236,
98 -18315, -18396, -18477, -18558, -18641, -18724, -18808, -18893, -18978, -19065,
99 -19152, -19239, -19328, -19418, -19508, -19599, -19691, -19784, -19878, -19972,
100 -20068, -20164, -20262, -20360, -20460, -20560, -20661, -20763, -20867, -20971,
101 -21076, -21183, -21290, -21399, -21509, -21620, -21732, -21845, -21959, -22075,
102 -22192, -22310, -22429, -22550, -22671, -22795, -22919, -23045, -23172, -23301,
103 -23431, -23563, -23696, -23831, -23967, -24105, -24244, -24385, -24528, -24672,
104 -24818, -24966, -25115, -25266, -25420, -25575, -25731, -25890, -26051, -26214,
105 -26379, -26546, -26715, -26886, -27060, -27235, -27413, -27594, -27776, -27962,
106 -28149, -28339, -28532, -28728, -28926, -29127, -29330, -29537, -29746, -29959,
107 -30174, -30393, -30615, -30840, -31068, -31300, -31536, -31775, -32017, -32263,
108 -32513, -32768, -33026, -33288, -33554, -33825, -34100, -34379, -34663, -34952,
109 -35246, -35544, -35848, -36157, -36472, -36792, -37117, -37449, -37786, -38130,
110 -38479, -38836, -39199, -39568, -39945, -40329, -40721, -41120, -41527, -41943,
111 -42366, -42799, -43240, -43690, -44150, -44620, -45100, -45590, -46091, -46603,
112 -47127, -47662, -48210, -48770, -49344, -49932, -50533, -51150, -51781, -52428,
113 -53092, -53773, -54471, -55188, -55924, -56679, -57456, -58254, -59074, -59918,
114 -60787, -61680, -62601, -63550, -64527, -65536, -66576, -67650, -68759, -69905,
115 -71089, -72315, -73584, -74898, -76260, -77672, -79137, -80659, -82241, -83886,
116 -85598, -87381, -89240, -91180, -93206, -95325, -97541, -99864, -102300,
117 -104857, -107546, -110376, -113359, -116508, -119837, -123361, -127100, -131072,
118 -135300, -139810, -144631, -149796, -155344, -161319, -167772, -174762, -182361,
119 -190650, -199728, -209715, -220752, -233016, -246723, -262144, -279620, -299593,
120 -322638, -349525, -381300, -419430, -466033, -524288, -599186, -699050, -838860,
121 -1048576, -1398101, -2097152, -4194304, 0
122 };
123
124 static constexpr size_t kLastEntry = std::size(table) - 1;
125 SkASSERT(SkAbs32(x) <= static_cast<int32_t>(kLastEntry));
126 static_assert(kLastEntry == kInverseTableSize);
127
128 if (x > 0) {
129 return -table[kLastEntry - x];
130 } else {
131 return table[kLastEntry + x];
132 }
133 }
134
135 static inline SkFixed quick_div(SkFDot6 a, SkFDot6 b) {
136 constexpr int kMinBits = 3; // abs(b) should be at least (1 << kMinBits) for quick division
137 constexpr int kMaxBits = 31; // Number of bits available in signed int
138 // Given abs(b) <= (1 << kMinBits), the inverse of abs(b) is at most 1 << (22 - kMinBits) in
139 // SkFixed format. Hence abs(a) should be less than kMaxAbsA
140 constexpr int kMaxAbsA = 1 << (kMaxBits - (22 - kMinBits));
141 SkFDot6 abs_a = SkAbs32(a);
142 SkFDot6 abs_b = SkAbs32(b);
143 if (abs_b >= (1 << kMinBits) && abs_b < kInverseTableSize && abs_a < kMaxAbsA) {
144 SkASSERT((int64_t)a * quick_inverse(b) <= SK_MaxS32
145 && (int64_t)a * quick_inverse(b) >= SK_MinS32);
146 SkFixed ourAnswer = (a * quick_inverse(b)) >> 6;
147 SkASSERT(
148 (SkFDot6Div(a,b) == 0 && ourAnswer == 0) ||
149 SkFixedDiv(SkAbs32(SkFDot6Div(a,b) - ourAnswer), SkAbs32(SkFDot6Div(a,b))) <= 1 << 10
150 );
151 return ourAnswer;
152 }
153 return SkFDot6Div(a, b);
154 }
155
156 bool SkAnalyticEdge::setLine(const SkPoint& p0, const SkPoint& p1) {
157 // We must set X/Y using the same way (e.g., times 4, to FDot6, then to Fixed) as Quads/Cubics.
158 // Otherwise the order of the edge might be wrong due to precision limit.
159 constexpr int accuracy = kDefaultAccuracy;
160 #ifdef SK_RASTERIZE_EVEN_ROUNDING
161 SkFixed x0 = SkFDot6ToFixed(SkScalarRoundToFDot6(p0.fX, accuracy)) >> accuracy;
162 SkFixed y0 = SnapY(SkFDot6ToFixed(SkScalarRoundToFDot6(p0.fY, accuracy)) >> accuracy);
163 SkFixed x1 = SkFDot6ToFixed(SkScalarRoundToFDot6(p1.fX, accuracy)) >> accuracy;
164 SkFixed y1 = SnapY(SkFDot6ToFixed(SkScalarRoundToFDot6(p1.fY, accuracy)) >> accuracy);
165 #else
166 constexpr int multiplier = (1 << kDefaultAccuracy);
167 SkFixed x0 = SkFDot6ToFixed(SkScalarToFDot6(p0.fX * multiplier)) >> accuracy;
168 SkFixed y0 = SnapY(SkFDot6ToFixed(SkScalarToFDot6(p0.fY * multiplier)) >> accuracy);
169 SkFixed x1 = SkFDot6ToFixed(SkScalarToFDot6(p1.fX * multiplier)) >> accuracy;
170 SkFixed y1 = SnapY(SkFDot6ToFixed(SkScalarToFDot6(p1.fY * multiplier)) >> accuracy);
171 #endif
172
173 Winding winding = Winding::kCW;
174
175 if (y0 > y1) {
176 using std::swap;
177 swap(x0, x1);
178 swap(y0, y1);
179 winding = Winding::kCCW;
180 }
181
182 // are we a zero-height line?
183 SkFDot6 dy = SkFixedToFDot6(y1 - y0);
184 if (dy == 0) {
185 return false;
186 }
187 SkFDot6 dx = SkFixedToFDot6(x1 - x0);
188 SkFixed slope = quick_div(dx, dy);
189 SkFixed absSlope = SkAbs32(slope);
190
191 fX = x0;
192 fDX = slope;
193 fUpperX = x0;
194 fY = y0;
195 fUpperY = y0;
196 fLowerY = y1;
197 fDY = dx == 0 || slope == 0 ? SK_MaxS32 : absSlope < kInverseTableSize
198 ? quick_inverse(absSlope)
199 : SkAbs32(quick_div(dy, dx));
200 fEdgeType = Type::kLine;
201 fCurveCount = 0;
202 fWinding = winding;
203 fCurveShift = 0;
204
205 return true;
206 }
207
208 static SkAnalyticEdge::Winding swap_winding(SkAnalyticEdge::Winding w) {
209 return static_cast<SkAnalyticEdge::Winding>(static_cast<int8_t>(w) * -1);
210 }
211
212 // This will become a bottleneck for small ovals rendering if we call SkFixedDiv twice here.
213 // Therefore, we'll let the outter function compute the slope once and send in the value.
214 // Moreover, we'll compute fDY by quickly lookup the inverse table (if possible).
215 bool SkAnalyticEdge::updateLine(SkFixed x0, SkFixed y0, SkFixed x1, SkFixed y1, SkFixed slope) {
216 // Since we send in the slope, we can no longer snap y inside this function.
217 // If we don't send in the slope, or we do some more sophisticated snapping, this function
218 // could be a performance bottleneck.
219 SkASSERT(fWinding == Winding::kCW || fWinding == Winding::kCCW);
220 SkASSERT(fCurveCount != 0);
221
222 // We don't chop at y extrema for cubics so the y is not guaranteed to be increasing for them.
223 // In that case, we have to swap x/y and negate the winding.
224 if (y0 > y1) {
225 using std::swap;
226 swap(x0, x1);
227 swap(y0, y1);
228 fWinding = swap_winding(fWinding);
229 }
230
231 SkASSERT(y0 <= y1);
232
233 SkFDot6 dx = SkFixedToFDot6(x1 - x0);
234 SkFDot6 dy = SkFixedToFDot6(y1 - y0);
235
236 // are we a zero-height line?
237 if (dy == 0) {
238 return false;
239 }
240
241 SkASSERT(slope < SK_MaxS32);
242
243 SkFDot6 absSlope = SkAbs32(SkFixedToFDot6(slope));
244 fX = x0;
245 fDX = slope;
246 fUpperX = x0;
247 fY = y0;
248 fUpperY = y0;
249 fLowerY = y1;
250 fDY = (dx == 0 || slope == 0)
251 ? SK_MaxS32
252 : absSlope < kInverseTableSize
253 ? quick_inverse(absSlope)
254 : SkAbs32(quick_div(dy, dx));
255
256 return true;
257 }
258
259 bool SkAnalyticEdge::update(SkFixed last_y) {
260 SkASSERT(last_y >= fLowerY); // we shouldn't update edge if last_y < fLowerY
261 if (fCurveCount < 0) {
262 return static_cast<SkAnalyticCubicEdge*>(this)->updateCubic();
263 } else if (fCurveCount > 0) {
264 return static_cast<SkAnalyticQuadraticEdge*>(this)->updateQuadratic();
265 }
266 return false;
267 }
268
269 /* We store 1<<shift in a (signed) byte, so its maximum value is 1<<6 == 64.
270 Note that this limits the number of lines we use to approximate a curve.
271 If we need to increase this, we need to store fCurveCount in something
272 larger than int8_t.
273 */
274 #define MAX_COEFF_SHIFT 6
275
276 static inline SkFDot6 cheap_distance(SkFDot6 dx, SkFDot6 dy)
277 {
278 dx = SkAbs32(dx);
279 dy = SkAbs32(dy);
280 // return max + min/2
281 if (dx > dy){
282 dx += dy >> 1;
283 } else {
284 dx = dy + (dx >> 1);
285 }
286 return dx;
287 }
288
289 static inline int diff_to_shift(SkFDot6 dx, SkFDot6 dy, int shiftAA) {
290 // cheap calc of distance from center of p0-p2 to the center of the curve
291 SkFDot6 dist = cheap_distance(dx, dy);
292
293 // shift down dist (it is currently in dot6)
294 // down by 3 should give us 1/8 pixel accuracy (assuming our dist is accurate...)
295 // this is chosen by heuristic: make it as big as possible (to minimize segments)
296 // ... but small enough so that our curves still look smooth
297 // When shift > 0, we're using AA and everything is scaled up so we can
298 // lower the accuracy.
299 dist = (dist + (1 << (2 + shiftAA))) >> (3 + shiftAA);
300
301 // each subdivision (shift value) cuts this dist (error) by 1/4
302 return (32 - SkCLZ(dist)) >> 1;
303 }
304
305 /*
306 In setQuadraticWithoutUpdate, setCubicWithoutUpdate, the first thing we do is to convert
307 the points into FDot6. This is modulated by the shift parameter, which
308 will be something like 2 for antialiasing.
309
310 In the float case, we want to turn the float into .6 by saying pt * 64,
311 or pt * 256 for antialiasing. This is implemented as 1 << (shift + 6).
312
313 In the fixed case, we want to turn the fixed into .6 by saying pt >> 10,
314 or pt >> 8 for antialiasing. This is implemented as pt >> (10 - shift).
315 */
316
317 static inline SkFixed SkFDot6ToFixedDiv2(SkFDot6 value) {
318 // we want to return SkFDot6ToFixed(value >> 1), but we don't want to throw
319 // away data in value, so just perform a modify up-shift
320 return SkLeftShift(value, 16 - 6 - 1);
321 }
322
323 bool SkAnalyticQuadraticEdge::setQuadraticWithoutUpdate(const SkPoint pts[3], int shift) {
324 SkFDot6 x0, y0, x1, y1, x2, y2;
325
326 {
327 #ifdef SK_RASTERIZE_EVEN_ROUNDING
328 x0 = SkScalarRoundToFDot6(pts[0].fX, shift);
329 y0 = SkScalarRoundToFDot6(pts[0].fY, shift);
330 x1 = SkScalarRoundToFDot6(pts[1].fX, shift);
331 y1 = SkScalarRoundToFDot6(pts[1].fY, shift);
332 x2 = SkScalarRoundToFDot6(pts[2].fX, shift);
333 y2 = SkScalarRoundToFDot6(pts[2].fY, shift);
334 #else
335 float scale = float(1 << (shift + 6));
336 x0 = int(pts[0].fX * scale);
337 y0 = int(pts[0].fY * scale);
338 x1 = int(pts[1].fX * scale);
339 y1 = int(pts[1].fY * scale);
340 x2 = int(pts[2].fX * scale);
341 y2 = int(pts[2].fY * scale);
342 #endif
343 }
344
345 Winding winding = Winding::kCW;
346 if (y0 > y2)
347 {
348 using std::swap;
349 swap(x0, x2);
350 swap(y0, y2);
351 winding = Winding::kCCW;
352 }
353 SkASSERT(y0 <= y1 && y1 <= y2);
354
355 int top = SkFDot6Round(y0);
356 int bot = SkFDot6Round(y2);
357
358 // are we a zero-height quad (line)?
359 if (top == bot) {
360 return 0;
361 }
362
363 // compute number of steps needed (1 << shift)
364 {
365 SkFDot6 dx = (SkLeftShift(x1, 1) - x0 - x2) >> 2;
366 SkFDot6 dy = (SkLeftShift(y1, 1) - y0 - y2) >> 2;
367 // This is a little confusing:
368 // before this line, shift is the scale up factor for AA;
369 // after this line, shift is the fCurveShift.
370 shift = diff_to_shift(dx, dy, shift);
371 SkASSERT(shift >= 0);
372 }
373 // need at least 1 subdivision for our bias trick
374 if (shift == 0) {
375 shift = 1;
376 } else if (shift > MAX_COEFF_SHIFT) {
377 shift = MAX_COEFF_SHIFT;
378 }
379
380 fWinding = winding;
381 //fCubicDShift only set for cubics
382 fEdgeType = Type::kQuad;
383 fCurveCount = SkToS8(1 << shift);
384
385 /*
386 * We want to reformulate into polynomial form, to make it clear how we
387 * should forward-difference.
388 *
389 * p0 (1 - t)^2 + p1 t(1 - t) + p2 t^2 ==> At^2 + Bt + C
390 *
391 * A = p0 - 2p1 + p2
392 * B = 2(p1 - p0)
393 * C = p0
394 *
395 * Our caller must have constrained our inputs (p0..p2) to all fit into
396 * 16.16. However, as seen above, we sometimes compute values that can be
397 * larger (e.g. B = 2*(p1 - p0)). To guard against overflow, we will store
398 * A and B at 1/2 of their actual value, and just apply a 2x scale during
399 * application in updateQuadratic(). Hence we store (shift - 1) in
400 * fCurveShift.
401 */
402
403 fCurveShift = SkToU8(shift - 1);
404
405 SkFixed A = SkFDot6ToFixedDiv2(x0 - x1 - x1 + x2); // 1/2 the real value
406 SkFixed B = SkFDot6ToFixed(x1 - x0); // 1/2 the real value
407
408 fQx = SkFDot6ToFixed(x0);
409 fQDx = B + (A >> shift); // biased by shift
410 fQDDx = A >> (shift - 1); // biased by shift
411
412 A = SkFDot6ToFixedDiv2(y0 - y1 - y1 + y2); // 1/2 the real value
413 B = SkFDot6ToFixed(y1 - y0); // 1/2 the real value
414
415 fQy = SkFDot6ToFixed(y0);
416 fQDy = B + (A >> shift); // biased by shift
417 fQDDy = A >> (shift - 1); // biased by shift
418
419 fQLastX = SkFDot6ToFixed(x2);
420 fQLastY = SkFDot6ToFixed(y2);
421
422 return true;
423 }
424
425 bool SkAnalyticQuadraticEdge::setQuadratic(const SkPoint pts[3]) {
426 if (!setQuadraticWithoutUpdate(pts, kDefaultAccuracy)) {
427 return false;
428 }
429 fQx >>= kDefaultAccuracy;
430 fQy >>= kDefaultAccuracy;
431 fQDx >>= kDefaultAccuracy;
432 fQDy >>= kDefaultAccuracy;
433 fQDDx >>= kDefaultAccuracy;
434 fQDDy >>= kDefaultAccuracy;
435 fQLastX >>= kDefaultAccuracy;
436 fQLastY >>= kDefaultAccuracy;
437 fQy = SnapY(fQy);
438 fQLastY = SnapY(fQLastY);
439
440 fEdgeType = Type::kQuad;
441
442 fSnappedX = fQx;
443 fSnappedY = fQy;
444
445 return this->updateQuadratic();
446 }
447
448 bool SkAnalyticQuadraticEdge::updateQuadratic() {
449 int success = 0; // initialize to fail!
450 int count = fCurveCount;
451 SkFixed oldx = fQx;
452 SkFixed oldy = fQy;
453 SkFixed dx = fQDx;
454 SkFixed dy = fQDy;
455 SkFixed newx, newy, newSnappedX, newSnappedY;
456 int shift = fCurveShift;
457
458 SkASSERT(count > 0);
459
460 do {
461 SkFixed slope;
462 if (--count > 0)
463 {
464 newx = oldx + (dx >> shift);
465 newy = oldy + (dy >> shift);
466 // only snap when dy is large enough and dx/dy isn't too large
467 if (SkAbs32(dy >> shift) >= SK_Fixed1 * 2 &&
468 SkLeftShift((int64_t) SkAbs32(dy), 6) > SkAbs32(dx)) {
469 SkFDot6 diffY = SkFixedToFDot6(newy - fSnappedY);
470 slope = diffY ? quick_div(SkFixedToFDot6(newx - fSnappedX), diffY)
471 : SK_MaxS32;
472 newSnappedY = std::min<SkFixed>(fQLastY, SkFixedRoundToFixed(newy));
473 newSnappedX = newx - SkFixedMul(slope, newy - newSnappedY);
474 } else {
475 newSnappedY = std::min(fQLastY, SnapY(newy));
476 newSnappedX = newx;
477 SkFDot6 diffY = SkFixedToFDot6(newSnappedY - fSnappedY);
478 slope = diffY ? quick_div(SkFixedToFDot6(newx - fSnappedX), diffY)
479 : SK_MaxS32;
480 }
481 dx += fQDDx;
482 dy += fQDDy;
483 }
484 else // last segment
485 {
486 newx = fQLastX;
487 newy = fQLastY;
488 newSnappedY = newy;
489 newSnappedX = newx;
490 SkFDot6 diffY = SkFixedToFDot6(newy - fSnappedY);
491 slope = diffY ? quick_div(SkFixedToFDot6(newx - fSnappedX), diffY) : SK_MaxS32;
492 }
493 if (slope < SK_MaxS32) {
494 success = this->updateLine(fSnappedX, fSnappedY, newSnappedX, newSnappedY, slope);
495 }
496 oldx = newx;
497 oldy = newy;
498 } while (count > 0 && !success);
499
500 SkASSERT(newSnappedY <= fQLastY);
501
502 fQx = newx;
503 fQy = newy;
504 fQDx = dx;
505 fQDy = dy;
506 fSnappedX = newSnappedX;
507 fSnappedY = newSnappedY;
508 fCurveCount = SkToS8(count);
509 return success;
510 }
511
512 bool SkAnalyticCubicEdge::setCubic(const SkPoint pts[4]) {
513 if (!setCubicWithoutUpdate(pts, kDefaultAccuracy)) {
514 return false;
515 }
516
517 fCx >>= kDefaultAccuracy;
518 fCy >>= kDefaultAccuracy;
519 fCDx >>= kDefaultAccuracy;
520 fCDy >>= kDefaultAccuracy;
521 fCDDx >>= kDefaultAccuracy;
522 fCDDy >>= kDefaultAccuracy;
523 fCDDDx >>= kDefaultAccuracy;
524 fCDDDy >>= kDefaultAccuracy;
525 fCLastX >>= kDefaultAccuracy;
526 fCLastY >>= kDefaultAccuracy;
527 fCy = SnapY(fCy);
528 fSnappedY = fCy;
529 fCLastY = SnapY(fCLastY);
530
531 fEdgeType = Type::kCubic;
532
533 return this->updateCubic();
534 }
535
536 static inline int SkFDot6UpShift(SkFDot6 x, int upShift) {
537 SkASSERT((SkLeftShift(x, upShift) >> upShift) == x);
538 return SkLeftShift(x, upShift);
539 }
540
541 /* f(1/3) = (8a + 12b + 6c + d) / 27
542 f(2/3) = (a + 6b + 12c + 8d) / 27
543
544 f(1/3)-b = (8a - 15b + 6c + d) / 27
545 f(2/3)-c = (a + 6b - 15c + 8d) / 27
546
547 use 16/512 to approximate 1/27
548 */
549 static SkFDot6 cubic_delta_from_line(SkFDot6 a, SkFDot6 b, SkFDot6 c, SkFDot6 d)
550 {
551 // since our parameters may be negative, we don't use << to avoid ASAN warnings
552 SkFDot6 oneThird = (a*8 - b*15 + 6*c + d) * 19 >> 9;
553 SkFDot6 twoThird = (a + 6*b - c*15 + d*8) * 19 >> 9;
554
555 return std::max(SkAbs32(oneThird), SkAbs32(twoThird));
556 }
557
558 bool SkAnalyticCubicEdge::setCubicWithoutUpdate(const SkPoint pts[4], int shift) {
559 SkFDot6 x0, y0, x1, y1, x2, y2, x3, y3;
560
561 {
562 #ifdef SK_RASTERIZE_EVEN_ROUNDING
563 x0 = SkScalarRoundToFDot6(pts[0].fX, shift);
564 y0 = SkScalarRoundToFDot6(pts[0].fY, shift);
565 x1 = SkScalarRoundToFDot6(pts[1].fX, shift);
566 y1 = SkScalarRoundToFDot6(pts[1].fY, shift);
567 x2 = SkScalarRoundToFDot6(pts[2].fX, shift);
568 y2 = SkScalarRoundToFDot6(pts[2].fY, shift);
569 x3 = SkScalarRoundToFDot6(pts[3].fX, shift);
570 y3 = SkScalarRoundToFDot6(pts[3].fY, shift);
571 #else
572 float scale = float(1 << (shift + 6));
573 x0 = int(pts[0].fX * scale);
574 y0 = int(pts[0].fY * scale);
575 x1 = int(pts[1].fX * scale);
576 y1 = int(pts[1].fY * scale);
577 x2 = int(pts[2].fX * scale);
578 y2 = int(pts[2].fY * scale);
579 x3 = int(pts[3].fX * scale);
580 y3 = int(pts[3].fY * scale);
581 #endif
582 }
583
584 Winding winding = Winding::kCW;
585 if (y0 > y3)
586 {
587 using std::swap;
588 swap(x0, x3);
589 swap(x1, x2);
590 swap(y0, y3);
591 swap(y1, y2);
592 winding = Winding::kCCW;
593 }
594
595 int top = SkFDot6Round(y0);
596 int bot = SkFDot6Round(y3);
597
598 // are we a zero-height cubic (line)?
599 if (top == bot)
600 return 0;
601
602 // compute number of steps needed (1 << shift)
603 {
604 // Can't use (center of curve - center of baseline), since center-of-curve
605 // need not be the max delta from the baseline (it could even be coincident)
606 // so we try just looking at the two off-curve points
607 SkFDot6 dx = cubic_delta_from_line(x0, x1, x2, x3);
608 SkFDot6 dy = cubic_delta_from_line(y0, y1, y2, y3);
609 // add 1 (by observation)
610 shift = diff_to_shift(dx, dy, 2) + 1;
611 }
612 // need at least 1 subdivision for our bias trick
613 SkASSERT(shift > 0);
614 if (shift > MAX_COEFF_SHIFT) {
615 shift = MAX_COEFF_SHIFT;
616 }
617
618 /* Since our in coming data is initially shifted down by 10 (or 8 in
619 antialias). That means the most we can shift up is 8. However, we
620 compute coefficients with a 3*, so the safest upshift is really 6
621 */
622 int upShift = 6; // largest safe value
623 int downShift = shift + upShift - 10;
624 if (downShift < 0) {
625 downShift = 0;
626 upShift = 10 - shift;
627 }
628
629 fWinding = winding;
630 fEdgeType = Type::kCubic;
631 fCurveCount = SkToS8(SkLeftShift(-1, shift));
632 fCurveShift = SkToU8(shift);
633 fCubicDShift = SkToU8(downShift);
634
635 SkFixed B = SkFDot6UpShift(3 * (x1 - x0), upShift);
636 SkFixed C = SkFDot6UpShift(3 * (x0 - x1 - x1 + x2), upShift);
637 SkFixed D = SkFDot6UpShift(x3 + 3 * (x1 - x2) - x0, upShift);
638
639 fCx = SkFDot6ToFixed(x0);
640 fCDx = B + (C >> shift) + (D >> 2*shift); // biased by shift
641 fCDDx = 2*C + (3*D >> (shift - 1)); // biased by 2*shift
642 fCDDDx = 3*D >> (shift - 1); // biased by 2*shift
643
644 B = SkFDot6UpShift(3 * (y1 - y0), upShift);
645 C = SkFDot6UpShift(3 * (y0 - y1 - y1 + y2), upShift);
646 D = SkFDot6UpShift(y3 + 3 * (y1 - y2) - y0, upShift);
647
648 fCy = SkFDot6ToFixed(y0);
649 fCDy = B + (C >> shift) + (D >> 2*shift); // biased by shift
650 fCDDy = 2*C + (3*D >> (shift - 1)); // biased by 2*shift
651 fCDDDy = 3*D >> (shift - 1); // biased by 2*shift
652
653 fCLastX = SkFDot6ToFixed(x3);
654 fCLastY = SkFDot6ToFixed(y3);
655
656 return true;
657 }
658
659 bool SkAnalyticCubicEdge::updateCubic() {
660 int success;
661 int count = fCurveCount;
662 SkFixed oldx = fCx;
663 SkFixed oldy = fCy;
664 SkFixed newx, newy;
665 const int ddshift = fCurveShift;
666 const int dshift = fCubicDShift;
667
668 SkASSERT(count < 0);
669
670 do {
671 if (++count < 0) {
672 newx = oldx + (fCDx >> dshift);
673 fCDx += fCDDx >> ddshift;
674 fCDDx += fCDDDx;
675
676 newy = oldy + (fCDy >> dshift);
677 fCDy += fCDDy >> ddshift;
678 fCDDy += fCDDDy;
679 }
680 else { // last segment
681 newx = fCLastX;
682 newy = fCLastY;
683 }
684
685 // we want to say SkASSERT(oldy <= newy), but our finite fixedpoint
686 // doesn't always achieve that, so we have to explicitly pin it here.
687 if (newy < oldy) {
688 newy = oldy;
689 }
690
691 SkFixed newSnappedY = SnapY(newy);
692 // we want to SkASSERT(snappedNewY <= fCLastY), but our finite fixedpoint
693 // doesn't always achieve that, so we have to explicitly pin it here.
694 if (fCLastY < newSnappedY) {
695 newSnappedY = fCLastY;
696 count = 0;
697 }
698
699 SkFixed slope = SkFixedToFDot6(newSnappedY - fSnappedY) == 0
700 ? SK_MaxS32
701 : SkFDot6Div(SkFixedToFDot6(newx - oldx),
702 SkFixedToFDot6(newSnappedY - fSnappedY));
703
704 success = this->updateLine(oldx, fSnappedY, newx, newSnappedY, slope);
705
706 oldx = newx;
707 oldy = newy;
708 fSnappedY = newSnappedY;
709 } while (count < 0 && !success);
710
711 fCx = newx;
712 fCy = newy;
713 fCurveCount = SkToS8(count);
714 return success;
715 }