| 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/SkEdge.h" |
| 9 |
|
| 10 |
#include "include/core/SkRect.h" |
| 11 |
#include "include/private/base/SkDebug.h" |
| 12 |
#include "include/private/base/SkSafe32.h" |
| 13 |
#include "include/private/base/SkTo.h" |
| 14 |
#include "src/base/SkMathPriv.h" |
| 15 |
#include "src/core/SkFDot6.h" |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
/* |
| 21 |
In setLine, setQuadratic, setCubic, the first thing we do is to convert |
| 22 |
the points into FDot6. This is modulated by the shift parameter, which |
| 23 |
will either be 0, or something like 2 for antialiasing. |
| 24 |
|
| 25 |
In the float case, we want to turn the float into .6 by saying pt * 64, |
| 26 |
or pt * 256 for antialiasing. This is implemented as 1 << (shift + 6). |
| 27 |
|
| 28 |
In the fixed case, we want to turn the fixed into .6 by saying pt >> 10, |
| 29 |
or pt >> 8 for antialiasing. This is implemented as pt >> (10 - shift). |
| 30 |
*/ |
| 31 |
|
| 32 |
static inline SkFixed SkFDot6ToFixedDiv2(SkFDot6 value) { |
| 33 |
// we want to return SkFDot6ToFixed(value >> 1), but we don't want to throw |
| 34 |
// away data in value, so just perform a modify up-shift |
| 35 |
return SkLeftShift(value, 16 - 6 - 1); |
| 36 |
} |
| 37 |
|
| 38 |
///////////////////////////////////////////////////////////////////////// |
| 39 |
|
| 40 |
#if defined(SK_DEBUG) |
| 41 |
void SkEdge::dump() const { |
| 42 |
SkASSERT(fSegmentCount == 0); |
| 43 |
SkDebugf("line edge: firstY:%d lastY:%d x:%g dx/dy:%g\n" |
| 44 |
"\twinding:%d curveShift:%u\n", |
| 45 |
fFirstY, |
| 46 |
fLastY, |
| 47 |
SkFixedToFloat(fX), |
| 48 |
SkFixedToFloat(fDxDy), |
| 49 |
static_cast<int8_t>(fWinding), |
| 50 |
fCurveShift); |
| 51 |
} |
| 52 |
|
| 53 |
void SkQuadraticEdge::dump() const { |
| 54 |
SkDebugf("quad edge; %u segment(s) left: firstY:%d lastY:%d x:%g dx/dy:%g\n" |
| 55 |
"\tqx:%g qy:%g dqx:%g dqy:%g ddqx:%g ddqy:%g qLastX:%g qLastY:%g\n" |
| 56 |
"\twinding:%d curveShift:%u\n", |
| 57 |
fSegmentCount, |
| 58 |
fFirstY, |
| 59 |
fLastY, |
| 60 |
SkFixedToFloat(fX), |
| 61 |
SkFixedToFloat(fDxDy), |
| 62 |
SkFixedToFloat(fQx), |
| 63 |
SkFixedToFloat(fQy), |
| 64 |
SkFixedToFloat(fQDxDt), |
| 65 |
SkFixedToFloat(fQDyDt), |
| 66 |
SkFixedToFloat(fQD2xDt2), |
| 67 |
SkFixedToFloat(fQD2yDt2), |
| 68 |
SkFixedToFloat(fQLastX), |
| 69 |
SkFixedToFloat(fQLastY), |
| 70 |
static_cast<int8_t>(fWinding), |
| 71 |
fCurveShift); |
| 72 |
} |
| 73 |
|
| 74 |
void SkCubicEdge::dump() const { |
| 75 |
SkDebugf("cube edge; %u segment(s) left: firstY:%d lastY:%d x:%g dx/dy:%g\n" |
| 76 |
"qx:%g qy:%g dcx:%g dcy:%g ddcx:%g ddcy:%g dddcx:%g dddcy:%g cLastX:%g cLastY:%g\n" |
| 77 |
"\twinding:%d curveShift:%u dShift:%u\n", |
| 78 |
fSegmentCount, |
| 79 |
fFirstY, |
| 80 |
fLastY, |
| 81 |
SkFixedToFloat(fX), |
| 82 |
SkFixedToFloat(fDxDy), |
| 83 |
SkFixedToFloat(fCx), |
| 84 |
SkFixedToFloat(fCy), |
| 85 |
SkFixedToFloat(fCDxDt), |
| 86 |
SkFixedToFloat(fCDyDt), |
| 87 |
SkFixedToFloat(fCD2xDt2), |
| 88 |
SkFixedToFloat(fCD2yDt2), |
| 89 |
SkFixedToFloat(fCD3xDt3), |
| 90 |
SkFixedToFloat(fCD3yDt3), |
| 91 |
SkFixedToFloat(fCLastX), |
| 92 |
SkFixedToFloat(fCLastY), |
| 93 |
static_cast<int8_t>(fWinding), |
| 94 |
fCurveShift, |
| 95 |
fCubicDShift); |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
bool SkEdge::setLine(const SkPoint& p0, const SkPoint& p1, const SkIRect* clip) { |
| 100 |
SkFDot6 x0, y0, x1, y1; |
| 101 |
|
| 102 |
#ifdef SK_RASTERIZE_EVEN_ROUNDING |
| 103 |
x0 = SkScalarRoundToFDot6(p0.fX, 0); |
| 104 |
y0 = SkScalarRoundToFDot6(p0.fY, 0); |
| 105 |
x1 = SkScalarRoundToFDot6(p1.fX, 0); |
| 106 |
y1 = SkScalarRoundToFDot6(p1.fY, 0); |
| 107 |
#else |
| 108 |
x0 = SkFloatToFDot6(p0.fX); |
| 109 |
y0 = SkFloatToFDot6(p0.fY); |
| 110 |
x1 = SkFloatToFDot6(p1.fX); |
| 111 |
y1 = SkFloatToFDot6(p1.fY); |
| 112 |
#endif |
| 113 |
|
| 114 |
Winding winding = Winding::kCW; |
| 115 |
if (y0 > y1) { |
| 116 |
std::swap(x0, x1); |
| 117 |
std::swap(y0, y1); |
| 118 |
winding = Winding::kCCW; |
| 119 |
} |
| 120 |
|
| 121 |
int top = SkFDot6Round(y0); |
| 122 |
int bot = SkFDot6Round(y1); |
| 123 |
|
| 124 |
// are we a zero-height line? |
| 125 |
if (top == bot) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
// are we completely above or below the clip? |
| 129 |
if (clip && (top >= clip->fBottom || bot <= clip->fTop)) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
SkFixed slope = SkFDot6Div(x1 - x0, y1 - y0); |
| 134 |
const SkFDot6 dy = SkEdge_Compute_DY(top, y0); |
| 135 |
|
| 136 |
// Note that SkFixedMul(SkFixed, SkFDot6) produces results in SkFDot6 |
| 137 |
fX = SkFDot6ToFixed(x0 + SkFixedMul(slope, dy)); |
| 138 |
fDxDy = slope; |
| 139 |
fFirstY = top; |
| 140 |
fLastY = bot - 1; |
| 141 |
fEdgeType = Type::kLine; |
| 142 |
fSegmentCount = 0; |
| 143 |
fWinding = winding; |
| 144 |
fCurveShift = 0; |
| 145 |
|
| 146 |
if (clip) { |
| 147 |
this->chopLineWithClip(*clip); |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
bool SkEdge::nextSegment() { |
| 153 |
SkDEBUGFAILF("Shouldn't be asking a linear edge to go to the next curve."); |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
// Draws a line between the provided points and then calculates the slope and starting |
| 158 |
// x value to line up with the closest pixel center. Updates the fields in the SkEdge |
| 159 |
// base class appropriately. Returns false if this edge would start and stop in the |
| 160 |
// same row. |
| 161 |
bool SkEdge::updateLine(SkFixed xStart, SkFixed yStart, SkFixed xEnd, SkFixed yEnd) { |
| 162 |
SkASSERT(fWinding == Winding::kCW || fWinding == Winding::kCCW); |
| 163 |
SkASSERT(fSegmentCount != 0); |
| 164 |
|
| 165 |
const SkFDot6 y0 = SkFixedToFDot6(yStart); |
| 166 |
const SkFDot6 y1 = SkFixedToFDot6(yEnd); |
| 167 |
|
| 168 |
SkASSERT(y0 <= y1); |
| 169 |
|
| 170 |
const int top = SkFDot6Round(y0); |
| 171 |
const int bot = SkFDot6Round(y1); |
| 172 |
|
| 173 |
// are we a zero-height line? |
| 174 |
if (top == bot) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
const SkFDot6 x0 = SkFixedToFDot6(xStart); |
| 179 |
const SkFDot6 x1 = SkFixedToFDot6(xEnd); |
| 180 |
|
| 181 |
SkFixed slope = SkFDot6Div(x1 - x0, y1 - y0); |
| 182 |
const SkFDot6 dy = SkEdge_Compute_DY(top, y0); |
| 183 |
|
| 184 |
// We could do this math in fixed point, but it would potentially require some |
| 185 |
// rebaselining https://codereview.chromium.org/960353005/#msg6 |
| 186 |
// Note that SkFixedMul(SkFixed, SkFDot6) produces results in SkFDot6 |
| 187 |
fX = SkFDot6ToFixed(x0 + SkFixedMul(slope, dy)); |
| 188 |
fDxDy = slope; |
| 189 |
fFirstY = top; |
| 190 |
fLastY = bot - 1; |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
void SkEdge::chopLineWithClip(const SkIRect& clip) |
| 196 |
{ |
| 197 |
int top = fFirstY; |
| 198 |
|
| 199 |
SkASSERT(top < clip.fBottom); |
| 200 |
|
| 201 |
// clip the line to the top |
| 202 |
if (top < clip.fTop) |
| 203 |
{ |
| 204 |
SkASSERT(fLastY >= clip.fTop); |
| 205 |
fX += fDxDy * (clip.fTop - top); |
| 206 |
fFirstY = clip.fTop; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/////////////////////////////////////////////////////////////////////////////// |
| 211 |
|
| 212 |
/* This limits the number of lines we use to approximate a curve. |
| 213 |
If we need to increase this, we need to store fSegmentCount in a larger data type. |
| 214 |
TODO(kjlubick): now that this is in an unsigned byte, we could go up to 7 |
| 215 |
*/ |
| 216 |
#define MAX_COEFF_SHIFT 6 |
| 217 |
|
| 218 |
// Approximate the distance from (0,0) to (dx, dy). |
| 219 |
// When dx and dy are about the same |
| 220 |
// sqrt(dx^2 + dy^2) => sqrt(2dx^2) => dx sqrt(2) = 1.41 * dx |
| 221 |
// When dx >> dy |
| 222 |
// sqrt(dx^2 + dy^2) => sqrt(dx^2) => dx |
| 223 |
// So this is a reasonable approximation |
| 224 |
static inline SkFDot6 cheap_distance(SkFDot6 dx, SkFDot6 dy) { |
| 225 |
dx = SkAbs32(dx); |
| 226 |
dy = SkAbs32(dy); |
| 227 |
// return max + min/2 |
| 228 |
if (dx > dy) { |
| 229 |
return dx + (dy / 2); |
| 230 |
} |
| 231 |
return dy + (dx / 2); |
| 232 |
} |
| 233 |
|
| 234 |
static inline int diff_to_shift(SkFDot6 dx, SkFDot6 dy, int accuracy) { |
| 235 |
// cheap calc of distance from center of p0-p2 to the center of the curve |
| 236 |
SkFDot6 dist = cheap_distance(dx, dy); |
| 237 |
|
| 238 |
// shift down dist (it is currently in dot6) |
| 239 |
// down by 3 should give us 1/8 pixel accuracy (assuming our dist is accurate...) |
| 240 |
// this is chosen by heuristic: make it as big as possible (to minimize segments) |
| 241 |
// ... but small enough so that our curves still look smooth |
| 242 |
// When shift > 0, we're using AA and everything is scaled up so we can |
| 243 |
// lower the accuracy. |
| 244 |
// For cubics still, we have shift > 0. TODO(kjlubick) can we align cubics and quads? |
| 245 |
dist = (dist + (1 << (2 + accuracy))) >> (3 + accuracy); |
| 246 |
|
| 247 |
// each subdivision (shift value) cuts this dist (error) by 1/4 |
| 248 |
return (32 - SkCLZ(dist)) >> 1; |
| 249 |
} |
| 250 |
|
| 251 |
bool SkQuadraticEdge::setQuadratic(const SkPoint pts[3]) { |
| 252 |
SkFDot6 x0, y0, x1, y1, x2, y2; |
| 253 |
|
| 254 |
#if defined(SK_RASTERIZE_EVEN_ROUNDING) |
| 255 |
x0 = SkScalarRoundToFDot6(pts[0].fX, 0); |
| 256 |
y0 = SkScalarRoundToFDot6(pts[0].fY, 0); |
| 257 |
x1 = SkScalarRoundToFDot6(pts[1].fX, 0); |
| 258 |
y1 = SkScalarRoundToFDot6(pts[1].fY, 0); |
| 259 |
x2 = SkScalarRoundToFDot6(pts[2].fX, 0); |
| 260 |
y2 = SkScalarRoundToFDot6(pts[2].fY, 0); |
| 261 |
#else |
| 262 |
x0 = SkFloatToFDot6(pts[0].fX); |
| 263 |
y0 = SkFloatToFDot6(pts[0].fY); |
| 264 |
x1 = SkFloatToFDot6(pts[1].fX); |
| 265 |
y1 = SkFloatToFDot6(pts[1].fY); |
| 266 |
x2 = SkFloatToFDot6(pts[2].fX); |
| 267 |
y2 = SkFloatToFDot6(pts[2].fY); |
| 268 |
#endif |
| 269 |
|
| 270 |
Winding winding = Winding::kCW; |
| 271 |
if (y0 > y2) { |
| 272 |
std::swap(x0, x2); |
| 273 |
std::swap(y0, y2); |
| 274 |
winding = Winding::kCCW; |
| 275 |
} |
| 276 |
SkASSERTF(y0 <= y1 && y1 <= y2, "curve must be monotonic"); |
| 277 |
|
| 278 |
const int top = SkFDot6Round(y0); |
| 279 |
const int bot = SkFDot6Round(y2); |
| 280 |
|
| 281 |
// are we a zero-height quad (line)? |
| 282 |
if (top == bot) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
// compute number of steps needed (2^shift) based on the distance between |
| 287 |
// this curve at the half-way point (t=0.5) and the midpoint of a straight |
| 288 |
// line between p0 and p2. |
| 289 |
// B(1/2) = p0 (1-t)^2 + 2 p1 t(1-t) + p2 t^2; t = 1/2 |
| 290 |
// = p0 (1/2)^2 + 2 p1 (1/2)(1/2) + p2 (1/2)^2 |
| 291 |
// = 1/4 (p0 + 2 p1 + p2) |
| 292 |
// Midpoint of p0 and p2 is M(p0, p2) = (p2 + p0) / 2 |
| 293 |
// Subtracting the two terms to get the vector representing the difference |
| 294 |
// distance = B(1/2) - M(p0, p2) |
| 295 |
// = 1/4 (p0 + 2 p1 + p2) - (p2 + p0) / 2 |
| 296 |
// = 1/4 (p0 + 2 p1 + p2) - (2 p2 + 2 p0) / 4 |
| 297 |
// = 1/4 (-p0 + 2 p1 - p2) |
| 298 |
SkFDot6 deltaX = (2*x1 - x0 - x2) >> 2; |
| 299 |
SkFDot6 deltaY = (2*y1 - y0 - y2) >> 2; |
| 300 |
// We pass those points into this function which will find the total distance |
| 301 |
// and use a heuristic to reduce the error to some threshold. |
| 302 |
int shift = diff_to_shift(deltaX, deltaY, 0); |
| 303 |
SkASSERT(shift >= 0); |
| 304 |
|
| 305 |
// We need at least 2 line segments for us to be able to save the derivatives as |
| 306 |
// half their values to avoid overflow. |
| 307 |
if (shift == 0) { |
| 308 |
shift = 1; |
| 309 |
} else if (shift > MAX_COEFF_SHIFT) { |
| 310 |
shift = MAX_COEFF_SHIFT; |
| 311 |
} |
| 312 |
|
| 313 |
fWinding = winding; |
| 314 |
fEdgeType = Type::kQuad; |
| 315 |
fSegmentCount = SkToU8(1 << shift); |
| 316 |
|
| 317 |
/* |
| 318 |
* By re-arranging the Bezier curve in polynomial form, it is easier to |
| 319 |
* find the derivatives and forward-differentiate from one segment to the next. |
| 320 |
* |
| 321 |
* p0 (1-t)^2 + 2 p1 t(1-t) + p2 t^2 ==> At^2 + Bt + C |
| 322 |
* |
| 323 |
* A = p0 - 2p1 + p2 |
| 324 |
* B = 2(p1 - p0) |
| 325 |
* C = p0 |
| 326 |
* |
| 327 |
* Our caller must have constrained our inputs (p0..p2) to all fit into |
| 328 |
* 16.16. However, as seen above, we sometimes compute values that can be |
| 329 |
* larger (e.g. B = 2*(p1 - p0)). To guard against overflow, we will store |
| 330 |
* A and B at 1/2 of their actual value, and just apply a 2x scale during |
| 331 |
* application in nextSegment(). Hence we store (shift - 1) in |
| 332 |
* fCurveShift. |
| 333 |
*/ |
| 334 |
|
| 335 |
fCurveShift = SkToU8(shift - 1); |
| 336 |
// TODO(kjlubick): Can we use SkVx and calculate both X and Y at once? |
| 337 |
|
| 338 |
// The extra 1/2 factor avoids overflow |
| 339 |
SkFixed A_half = SkFDot6ToFixedDiv2(x0 - x1 - x1 + x2); |
| 340 |
SkFixed B_half = SkFDot6ToFixed(x1 - x0); |
| 341 |
|
| 342 |
// We want to calculate the slope at the midpoint of our first segment. This means evaluating |
| 343 |
// dx/dt = 2A*t + B |
| 344 |
// dx^2/dt^2 = 2A |
| 345 |
// at t = 1/N * 1/2 |
| 346 |
// There's an extra 1/2 on the whole expression to avoid overflows (as above). |
| 347 |
// 1/2 ( 2A*t + B) => 1/2 (2A*1/2N + B) => A/2*1/N + B/2 => A/2 * 1/2^shift + B/2 |
| 348 |
fQDxDt = B_half + (A_half >> shift); |
| 349 |
// The second derivatives are constant, so we can pre-multiply them by 1/N to save having |
| 350 |
// to do it in nextSegment(). Since A_half was already calculated we can use a smaller shift. |
| 351 |
// 1/2 (2A * 1/N) => A * 1/N => A * 1/2^shift => A/2 * 1/2^(shift-1) |
| 352 |
fQD2xDt2 = A_half >> (shift - 1); |
| 353 |
|
| 354 |
A_half = SkFDot6ToFixedDiv2(y0 - y1 - y1 + y2); |
| 355 |
B_half = SkFDot6ToFixed(y1 - y0); |
| 356 |
|
| 357 |
fQDyDt = B_half + (A_half >> shift); |
| 358 |
fQD2yDt2 = A_half >> (shift - 1); |
| 359 |
|
| 360 |
fQx = SkFDot6ToFixed(x0); |
| 361 |
fQy = SkFDot6ToFixed(y0); |
| 362 |
fQLastX = SkFDot6ToFixed(x2); |
| 363 |
fQLastY = SkFDot6ToFixed(y2); |
| 364 |
|
| 365 |
return this->nextSegment(); |
| 366 |
} |
| 367 |
|
| 368 |
bool SkQuadraticEdge::nextSegment() { |
| 369 |
bool success; |
| 370 |
int count = fSegmentCount; |
| 371 |
SkFixed oldx = fQx; |
| 372 |
SkFixed oldy = fQy; |
| 373 |
SkFixed dx = fQDxDt; |
| 374 |
SkFixed dy = fQDyDt; |
| 375 |
SkFixed newx, newy; |
| 376 |
int shift = fCurveShift; |
| 377 |
|
| 378 |
SkASSERT(count > 0); |
| 379 |
|
| 380 |
do { |
| 381 |
if (--count > 0) { |
| 382 |
newx = oldx + (dx >> shift); |
| 383 |
dx += fQD2xDt2; |
| 384 |
newy = oldy + (dy >> shift); |
| 385 |
dy += fQD2yDt2; |
| 386 |
} |
| 387 |
else // last segment |
| 388 |
{ |
| 389 |
newx = fQLastX; |
| 390 |
newy = fQLastY; |
| 391 |
} |
| 392 |
success = this->updateLine(oldx, oldy, newx, newy); |
| 393 |
oldx = newx; |
| 394 |
oldy = newy; |
| 395 |
} while (count > 0 && !success); |
| 396 |
|
| 397 |
fQx = newx; |
| 398 |
fQy = newy; |
| 399 |
fQDxDt = dx; |
| 400 |
fQDyDt = dy; |
| 401 |
fSegmentCount = SkToU8(count); |
| 402 |
return success; |
| 403 |
} |
| 404 |
|
| 405 |
///////////////////////////////////////////////////////////////////////// |
| 406 |
|
| 407 |
static inline int SkFDot6UpShift(SkFDot6 x, int upShift) { |
| 408 |
SkASSERT((SkLeftShift(x, upShift) >> upShift) == x); |
| 409 |
return SkLeftShift(x, upShift); |
| 410 |
} |
| 411 |
|
| 412 |
/* f(1/3) = (8a + 12b + 6c + d) / 27 |
| 413 |
f(2/3) = (a + 6b + 12c + 8d) / 27 |
| 414 |
|
| 415 |
f(1/3)-b = (8a - 15b + 6c + d) / 27 |
| 416 |
f(2/3)-c = (a + 6b - 15c + 8d) / 27 |
| 417 |
|
| 418 |
use 16/512 to approximate 1/27 |
| 419 |
*/ |
| 420 |
static SkFDot6 cubic_delta_from_line(SkFDot6 a, SkFDot6 b, SkFDot6 c, SkFDot6 d) |
| 421 |
{ |
| 422 |
// since our parameters may be negative, we don't use << to avoid ASAN warnings |
| 423 |
SkFDot6 oneThird = (a*8 - b*15 + 6*c + d) * 19 >> 9; |
| 424 |
SkFDot6 twoThird = (a + 6*b - c*15 + d*8) * 19 >> 9; |
| 425 |
|
| 426 |
return std::max(SkAbs32(oneThird), SkAbs32(twoThird)); |
| 427 |
} |
| 428 |
|
| 429 |
bool SkCubicEdge::setCubic(const SkPoint pts[4]) { |
| 430 |
SkFDot6 x0, y0, x1, y1, x2, y2, x3, y3; |
| 431 |
|
| 432 |
#if defined(SK_RASTERIZE_EVEN_ROUNDING) |
| 433 |
x0 = SkScalarRoundToFDot6(pts[0].fX, 0); |
| 434 |
y0 = SkScalarRoundToFDot6(pts[0].fY, 0); |
| 435 |
x1 = SkScalarRoundToFDot6(pts[1].fX, 0); |
| 436 |
y1 = SkScalarRoundToFDot6(pts[1].fY, 0); |
| 437 |
x2 = SkScalarRoundToFDot6(pts[2].fX, 0); |
| 438 |
y2 = SkScalarRoundToFDot6(pts[2].fY, 0); |
| 439 |
x3 = SkScalarRoundToFDot6(pts[3].fX, 0); |
| 440 |
y3 = SkScalarRoundToFDot6(pts[3].fY, 0); |
| 441 |
#else |
| 442 |
x0 = SkFloatToFDot6(pts[0].fX); |
| 443 |
y0 = SkFloatToFDot6(pts[0].fY); |
| 444 |
x1 = SkFloatToFDot6(pts[1].fX); |
| 445 |
y1 = SkFloatToFDot6(pts[1].fY); |
| 446 |
x2 = SkFloatToFDot6(pts[2].fX); |
| 447 |
y2 = SkFloatToFDot6(pts[2].fY); |
| 448 |
x3 = SkFloatToFDot6(pts[3].fX); |
| 449 |
y3 = SkFloatToFDot6(pts[3].fY); |
| 450 |
#endif |
| 451 |
|
| 452 |
Winding winding = Winding::kCW; |
| 453 |
if (y0 > y3) { |
| 454 |
std::swap(x0, x3); |
| 455 |
std::swap(x1, x2); |
| 456 |
std::swap(y0, y3); |
| 457 |
std::swap(y1, y2); |
| 458 |
winding = Winding::kCCW; |
| 459 |
} |
| 460 |
|
| 461 |
int top = SkFDot6Round(y0); |
| 462 |
int bot = SkFDot6Round(y3); |
| 463 |
|
| 464 |
// are we a zero-height cubic (line)? |
| 465 |
if (top == bot) { |
| 466 |
return false; |
| 467 |
} |
| 468 |
|
| 469 |
// compute number of steps needed (1 << shift) |
| 470 |
// Can't use (center of curve - center of baseline), since center-of-curve |
| 471 |
// need not be the max delta from the baseline (it could even be coincident) |
| 472 |
// so we try just looking at the two off-curve points |
| 473 |
SkFDot6 dx = cubic_delta_from_line(x0, x1, x2, x3); |
| 474 |
SkFDot6 dy = cubic_delta_from_line(y0, y1, y2, y3); |
| 475 |
// add 1 (by observation) |
| 476 |
int shift = diff_to_shift(dx, dy, 2) + 1; |
| 477 |
// need at least 1 subdivision for our bias trick |
| 478 |
SkASSERT(shift > 0); |
| 479 |
if (shift > MAX_COEFF_SHIFT) { |
| 480 |
shift = MAX_COEFF_SHIFT; |
| 481 |
} |
| 482 |
|
| 483 |
/* Since our in coming data is initially shifted down by 10 (or 8 in |
| 484 |
antialias). That means the most we can shift up is 8. However, we |
| 485 |
compute coefficients with a 3*, so the safest upshift is really 6 |
| 486 |
*/ |
| 487 |
int upShift = 6; // largest safe value |
| 488 |
int downShift = shift + upShift - 10; |
| 489 |
if (downShift < 0) { |
| 490 |
downShift = 0; |
| 491 |
upShift = 10 - shift; |
| 492 |
} |
| 493 |
|
| 494 |
fWinding = winding; |
| 495 |
fEdgeType = Type::kCubic; |
| 496 |
fSegmentCount = SkToU8(SkLeftShift(1, shift)); |
| 497 |
fCurveShift = SkToU8(shift); |
| 498 |
fCubicDShift = SkToU8(downShift); |
| 499 |
|
| 500 |
/* |
| 501 |
* By re-arranging the Bezier curve in polynomial form, it is easier to |
| 502 |
* find the derivatives and forward-differentiate from one segment to the next. |
| 503 |
* |
| 504 |
* p0 (1-t)^3 + 3 p1 t(1-t)^2 + 3 p2 t^2 (1-t) + p3 t^3 ==> At^3 + Bt^2 + Ct + D |
| 505 |
* Where A = -p0 + 3p1 + -3p2 + p3 |
| 506 |
* B = 3p0 - 6p1 + 3p2 |
| 507 |
* C = -3p0 + 3p1 |
| 508 |
* D = p0 |
| 509 |
*/ |
| 510 |
// TODO(kjlubick): Can we use SkVx and calculate both X and Y at once? |
| 511 |
|
| 512 |
SkFixed A = SkFDot6UpShift(x3 + 3 * (x1 - x2) - x0, upShift); |
| 513 |
SkFixed B = SkFDot6UpShift(3 * (x0 - 2*x1 + x2), upShift); |
| 514 |
SkFixed C = SkFDot6UpShift(3 * (x1 - x0), upShift); |
| 515 |
|
| 516 |
// We want to calculate the slope at the midpoint of our first segment. This means evaluating |
| 517 |
// dx/dt = 3A*t^2 + 2B*t + C |
| 518 |
// dx^2/dt^2 = 6A * t + 2B |
| 519 |
// dx^3/dt^3 = 6A |
| 520 |
// at t = 1/N * 1/2 |
| 521 |
// TODO(kjlubick): I'm not sure these cubic approximations are being done correctly. Some |
| 522 |
// coefficients seem to be missing. Maybe it's being done not at the midpoint |
| 523 |
// of the first line but just...somewhere in there? |
| 524 |
fCDxDt = (A >> 2*shift) + (B >> shift) + C; |
| 525 |
fCD2xDt2 = (3*A >> (shift - 1)) + 2*B; |
| 526 |
|
| 527 |
// This may be attempting to precompute the third derivative times 1/N |
| 528 |
// 6A / N => 6A / 2^shift => 3A / 2^(shift-1) |
| 529 |
fCD3xDt3 = 3*A >> (shift - 1); |
| 530 |
|
| 531 |
A = SkFDot6UpShift(y3 + 3 * (y1 - y2) - y0, upShift); |
| 532 |
B = SkFDot6UpShift(3 * (y0 - 2*y1 + y2), upShift); |
| 533 |
C = SkFDot6UpShift(3 * (y1 - y0), upShift); |
| 534 |
|
| 535 |
fCDyDt = (A >> 2*shift) + (B >> shift) + C; |
| 536 |
fCD2yDt2 = (3*A >> (shift - 1)) + 2*B; |
| 537 |
fCD3yDt3 = 3*A >> (shift - 1); |
| 538 |
|
| 539 |
fCx = SkFDot6ToFixed(x0); |
| 540 |
fCy = SkFDot6ToFixed(y0); |
| 541 |
fCLastX = SkFDot6ToFixed(x3); |
| 542 |
fCLastY = SkFDot6ToFixed(y3); |
| 543 |
|
| 544 |
return this->nextSegment(); |
| 545 |
} |
| 546 |
|
| 547 |
bool SkCubicEdge::nextSegment() { |
| 548 |
bool success; |
| 549 |
int count = fSegmentCount; |
| 550 |
SkFixed oldx = fCx; |
| 551 |
SkFixed oldy = fCy; |
| 552 |
SkFixed newx, newy; |
| 553 |
const int ddshift = fCurveShift; |
| 554 |
const int dshift = fCubicDShift; |
| 555 |
|
| 556 |
SkASSERT(count > 0); |
| 557 |
|
| 558 |
do { |
| 559 |
if (--count > 0) |
| 560 |
{ |
| 561 |
newx = oldx + (fCDxDt >> dshift); |
| 562 |
fCDxDt += fCD2xDt2 >> ddshift; |
| 563 |
fCD2xDt2 += fCD3xDt3; |
| 564 |
|
| 565 |
newy = oldy + (fCDyDt >> dshift); |
| 566 |
fCDyDt += fCD2yDt2 >> ddshift; |
| 567 |
fCD2yDt2 += fCD3yDt3; |
| 568 |
} |
| 569 |
else // last segment |
| 570 |
{ |
| 571 |
newx = fCLastX; |
| 572 |
newy = fCLastY; |
| 573 |
} |
| 574 |
|
| 575 |
// we want to say SkASSERT(oldy <= newy), but our finite fixedpoint |
| 576 |
// doesn't always achieve that, so we have to explicitly pin it here. |
| 577 |
if (newy < oldy) { |
| 578 |
newy = oldy; |
| 579 |
} |
| 580 |
|
| 581 |
success = this->updateLine(oldx, oldy, newx, newy); |
| 582 |
oldx = newx; |
| 583 |
oldy = newy; |
| 584 |
} while (count > 0 && !success); |
| 585 |
|
| 586 |
fCx = newx; |
| 587 |
fCy = newy; |
| 588 |
fSegmentCount = SkToU8(count); |
| 589 |
return success; |
| 590 |
} |