gfx/skia/trunk/src/core/SkCubicClipper.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 2009 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 "SkCubicClipper.h"
michael@0 11 #include "SkGeometry.h"
michael@0 12
michael@0 13 SkCubicClipper::SkCubicClipper() {
michael@0 14 fClip.setEmpty();
michael@0 15 }
michael@0 16
michael@0 17 void SkCubicClipper::setClip(const SkIRect& clip) {
michael@0 18 // conver to scalars, since that's where we'll see the points
michael@0 19 fClip.set(clip);
michael@0 20 }
michael@0 21
michael@0 22
michael@0 23 static bool chopMonoCubicAtY(SkPoint pts[4], SkScalar y, SkScalar* t) {
michael@0 24 SkScalar ycrv[4];
michael@0 25 ycrv[0] = pts[0].fY - y;
michael@0 26 ycrv[1] = pts[1].fY - y;
michael@0 27 ycrv[2] = pts[2].fY - y;
michael@0 28 ycrv[3] = pts[3].fY - y;
michael@0 29
michael@0 30 #ifdef NEWTON_RAPHSON // Quadratic convergence, typically <= 3 iterations.
michael@0 31 // Initial guess.
michael@0 32 // TODO(turk): Check for zero denominator? Shouldn't happen unless the curve
michael@0 33 // is not only monotonic but degenerate.
michael@0 34 SkScalar t1 = ycrv[0] / (ycrv[0] - ycrv[3]);
michael@0 35
michael@0 36 // Newton's iterations.
michael@0 37 const SkScalar tol = SK_Scalar1 / 16384; // This leaves 2 fixed noise bits.
michael@0 38 SkScalar t0;
michael@0 39 const int maxiters = 5;
michael@0 40 int iters = 0;
michael@0 41 bool converged;
michael@0 42 do {
michael@0 43 t0 = t1;
michael@0 44 SkScalar y01 = SkScalarInterp(ycrv[0], ycrv[1], t0);
michael@0 45 SkScalar y12 = SkScalarInterp(ycrv[1], ycrv[2], t0);
michael@0 46 SkScalar y23 = SkScalarInterp(ycrv[2], ycrv[3], t0);
michael@0 47 SkScalar y012 = SkScalarInterp(y01, y12, t0);
michael@0 48 SkScalar y123 = SkScalarInterp(y12, y23, t0);
michael@0 49 SkScalar y0123 = SkScalarInterp(y012, y123, t0);
michael@0 50 SkScalar yder = (y123 - y012) * 3;
michael@0 51 // TODO(turk): check for yder==0: horizontal.
michael@0 52 t1 -= y0123 / yder;
michael@0 53 converged = SkScalarAbs(t1 - t0) <= tol; // NaN-safe
michael@0 54 ++iters;
michael@0 55 } while (!converged && (iters < maxiters));
michael@0 56 *t = t1; // Return the result.
michael@0 57
michael@0 58 // The result might be valid, even if outside of the range [0, 1], but
michael@0 59 // we never evaluate a Bezier outside this interval, so we return false.
michael@0 60 if (t1 < 0 || t1 > SK_Scalar1)
michael@0 61 return false; // This shouldn't happen, but check anyway.
michael@0 62 return converged;
michael@0 63
michael@0 64 #else // BISECTION // Linear convergence, typically 16 iterations.
michael@0 65
michael@0 66 // Check that the endpoints straddle zero.
michael@0 67 SkScalar tNeg, tPos; // Negative and positive function parameters.
michael@0 68 if (ycrv[0] < 0) {
michael@0 69 if (ycrv[3] < 0)
michael@0 70 return false;
michael@0 71 tNeg = 0;
michael@0 72 tPos = SK_Scalar1;
michael@0 73 } else if (ycrv[0] > 0) {
michael@0 74 if (ycrv[3] > 0)
michael@0 75 return false;
michael@0 76 tNeg = SK_Scalar1;
michael@0 77 tPos = 0;
michael@0 78 } else {
michael@0 79 *t = 0;
michael@0 80 return true;
michael@0 81 }
michael@0 82
michael@0 83 const SkScalar tol = SK_Scalar1 / 65536; // 1 for fixed, 1e-5 for float.
michael@0 84 int iters = 0;
michael@0 85 do {
michael@0 86 SkScalar tMid = (tPos + tNeg) / 2;
michael@0 87 SkScalar y01 = SkScalarInterp(ycrv[0], ycrv[1], tMid);
michael@0 88 SkScalar y12 = SkScalarInterp(ycrv[1], ycrv[2], tMid);
michael@0 89 SkScalar y23 = SkScalarInterp(ycrv[2], ycrv[3], tMid);
michael@0 90 SkScalar y012 = SkScalarInterp(y01, y12, tMid);
michael@0 91 SkScalar y123 = SkScalarInterp(y12, y23, tMid);
michael@0 92 SkScalar y0123 = SkScalarInterp(y012, y123, tMid);
michael@0 93 if (y0123 == 0) {
michael@0 94 *t = tMid;
michael@0 95 return true;
michael@0 96 }
michael@0 97 if (y0123 < 0) tNeg = tMid;
michael@0 98 else tPos = tMid;
michael@0 99 ++iters;
michael@0 100 } while (!(SkScalarAbs(tPos - tNeg) <= tol)); // Nan-safe
michael@0 101
michael@0 102 *t = (tNeg + tPos) / 2;
michael@0 103 return true;
michael@0 104 #endif // BISECTION
michael@0 105 }
michael@0 106
michael@0 107
michael@0 108 bool SkCubicClipper::clipCubic(const SkPoint srcPts[4], SkPoint dst[4]) {
michael@0 109 bool reverse;
michael@0 110
michael@0 111 // we need the data to be monotonically descending in Y
michael@0 112 if (srcPts[0].fY > srcPts[3].fY) {
michael@0 113 dst[0] = srcPts[3];
michael@0 114 dst[1] = srcPts[2];
michael@0 115 dst[2] = srcPts[1];
michael@0 116 dst[3] = srcPts[0];
michael@0 117 reverse = true;
michael@0 118 } else {
michael@0 119 memcpy(dst, srcPts, 4 * sizeof(SkPoint));
michael@0 120 reverse = false;
michael@0 121 }
michael@0 122
michael@0 123 // are we completely above or below
michael@0 124 const SkScalar ctop = fClip.fTop;
michael@0 125 const SkScalar cbot = fClip.fBottom;
michael@0 126 if (dst[3].fY <= ctop || dst[0].fY >= cbot) {
michael@0 127 return false;
michael@0 128 }
michael@0 129
michael@0 130 SkScalar t;
michael@0 131 SkPoint tmp[7]; // for SkChopCubicAt
michael@0 132
michael@0 133 // are we partially above
michael@0 134 if (dst[0].fY < ctop && chopMonoCubicAtY(dst, ctop, &t)) {
michael@0 135 SkChopCubicAt(dst, tmp, t);
michael@0 136 dst[0] = tmp[3];
michael@0 137 dst[1] = tmp[4];
michael@0 138 dst[2] = tmp[5];
michael@0 139 }
michael@0 140
michael@0 141 // are we partially below
michael@0 142 if (dst[3].fY > cbot && chopMonoCubicAtY(dst, cbot, &t)) {
michael@0 143 SkChopCubicAt(dst, tmp, t);
michael@0 144 dst[1] = tmp[1];
michael@0 145 dst[2] = tmp[2];
michael@0 146 dst[3] = tmp[3];
michael@0 147 }
michael@0 148
michael@0 149 if (reverse) {
michael@0 150 SkTSwap<SkPoint>(dst[0], dst[3]);
michael@0 151 SkTSwap<SkPoint>(dst[1], dst[2]);
michael@0 152 }
michael@0 153 return true;
michael@0 154 }

mercurial