gfx/skia/trunk/src/core/SkEdge.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial