gfx/skia/trunk/include/core/SkScalar.h

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 * Copyright 2006 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #ifndef SkScalar_DEFINED
michael@0 9 #define SkScalar_DEFINED
michael@0 10
michael@0 11 #include "SkFixed.h"
michael@0 12 #include "SkFloatingPoint.h"
michael@0 13
michael@0 14 //#define SK_SUPPORT_DEPRECATED_SCALARROUND
michael@0 15
michael@0 16 typedef float SkScalar;
michael@0 17
michael@0 18 /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
michael@0 19 */
michael@0 20 #define SK_Scalar1 (1.0f)
michael@0 21 /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
michael@0 22 */
michael@0 23 #define SK_ScalarHalf (0.5f)
michael@0 24 /** SK_ScalarInfinity is defined to be infinity as an SkScalar
michael@0 25 */
michael@0 26 #define SK_ScalarInfinity SK_FloatInfinity
michael@0 27 /** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar
michael@0 28 */
michael@0 29 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
michael@0 30 /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
michael@0 31 */
michael@0 32 #define SK_ScalarMax (3.402823466e+38f)
michael@0 33 /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
michael@0 34 */
michael@0 35 #define SK_ScalarMin (-SK_ScalarMax)
michael@0 36 /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
michael@0 37 */
michael@0 38 #define SK_ScalarNaN SK_FloatNaN
michael@0 39 /** SkScalarIsNaN(n) returns true if argument is not a number
michael@0 40 */
michael@0 41 static inline bool SkScalarIsNaN(float x) { return x != x; }
michael@0 42
michael@0 43 /** Returns true if x is not NaN and not infinite */
michael@0 44 static inline bool SkScalarIsFinite(float x) {
michael@0 45 // We rely on the following behavior of infinities and nans
michael@0 46 // 0 * finite --> 0
michael@0 47 // 0 * infinity --> NaN
michael@0 48 // 0 * NaN --> NaN
michael@0 49 float prod = x * 0;
michael@0 50 // At this point, prod will either be NaN or 0
michael@0 51 // Therefore we can return (prod == prod) or (0 == prod).
michael@0 52 return prod == prod;
michael@0 53 }
michael@0 54
michael@0 55 /** SkIntToScalar(n) returns its integer argument as an SkScalar
michael@0 56 */
michael@0 57 #define SkIntToScalar(n) ((float)(n))
michael@0 58 /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
michael@0 59 */
michael@0 60 #define SkFixedToScalar(x) SkFixedToFloat(x)
michael@0 61 /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
michael@0 62 */
michael@0 63 #define SkScalarToFixed(x) SkFloatToFixed(x)
michael@0 64
michael@0 65 #define SkScalarToFloat(n) (n)
michael@0 66 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED
michael@0 67 #define SkFloatToScalar(n) (n)
michael@0 68 #endif
michael@0 69
michael@0 70 #define SkScalarToDouble(n) (double)(n)
michael@0 71 #define SkDoubleToScalar(n) (float)(n)
michael@0 72
michael@0 73 /** SkScalarFraction(x) returns the signed fractional part of the argument
michael@0 74 */
michael@0 75 #define SkScalarFraction(x) sk_float_mod(x, 1.0f)
michael@0 76
michael@0 77 #define SkScalarFloorToScalar(x) sk_float_floor(x)
michael@0 78 #define SkScalarCeilToScalar(x) sk_float_ceil(x)
michael@0 79 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f)
michael@0 80
michael@0 81 #define SkScalarFloorToInt(x) sk_float_floor2int(x)
michael@0 82 #define SkScalarCeilToInt(x) sk_float_ceil2int(x)
michael@0 83 #define SkScalarRoundToInt(x) sk_float_round2int(x)
michael@0 84 #define SkScalarTruncToInt(x) static_cast<int>(x)
michael@0 85
michael@0 86 /** Returns the absolute value of the specified SkScalar
michael@0 87 */
michael@0 88 #define SkScalarAbs(x) sk_float_abs(x)
michael@0 89 /** Return x with the sign of y
michael@0 90 */
michael@0 91 #define SkScalarCopySign(x, y) sk_float_copysign(x, y)
michael@0 92 /** Returns the value pinned between 0 and max inclusive
michael@0 93 */
michael@0 94 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
michael@0 95 return x < 0 ? 0 : x > max ? max : x;
michael@0 96 }
michael@0 97 /** Returns the value pinned between min and max inclusive
michael@0 98 */
michael@0 99 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
michael@0 100 return x < min ? min : x > max ? max : x;
michael@0 101 }
michael@0 102 /** Returns the specified SkScalar squared (x*x)
michael@0 103 */
michael@0 104 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
michael@0 105 /** Returns the product of two SkScalars
michael@0 106 */
michael@0 107 #define SkScalarMul(a, b) ((float)(a) * (b))
michael@0 108 /** Returns the product of two SkScalars plus a third SkScalar
michael@0 109 */
michael@0 110 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
michael@0 111 /** Returns the quotient of two SkScalars (a/b)
michael@0 112 */
michael@0 113 #define SkScalarDiv(a, b) ((float)(a) / (b))
michael@0 114 /** Returns the mod of two SkScalars (a mod b)
michael@0 115 */
michael@0 116 #define SkScalarMod(x,y) sk_float_mod(x,y)
michael@0 117 /** Returns the product of the first two arguments, divided by the third argument
michael@0 118 */
michael@0 119 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
michael@0 120 /** Returns the multiplicative inverse of the SkScalar (1/x)
michael@0 121 */
michael@0 122 #define SkScalarInvert(x) (SK_Scalar1 / (x))
michael@0 123 #define SkScalarFastInvert(x) (SK_Scalar1 / (x))
michael@0 124 /** Returns the square root of the SkScalar
michael@0 125 */
michael@0 126 #define SkScalarSqrt(x) sk_float_sqrt(x)
michael@0 127 /** Returns b to the e
michael@0 128 */
michael@0 129 #define SkScalarPow(b, e) sk_float_pow(b, e)
michael@0 130 /** Returns the average of two SkScalars (a+b)/2
michael@0 131 */
michael@0 132 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f)
michael@0 133 /** Returns one half of the specified SkScalar
michael@0 134 */
michael@0 135 #define SkScalarHalf(a) ((a) * 0.5f)
michael@0 136
michael@0 137 #define SK_ScalarSqrt2 1.41421356f
michael@0 138 #define SK_ScalarPI 3.14159265f
michael@0 139 #define SK_ScalarTanPIOver8 0.414213562f
michael@0 140 #define SK_ScalarRoot2Over2 0.707106781f
michael@0 141
michael@0 142 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
michael@0 143 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
michael@0 144 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
michael@0 145 #define SkScalarSin(radians) (float)sk_float_sin(radians)
michael@0 146 #define SkScalarCos(radians) (float)sk_float_cos(radians)
michael@0 147 #define SkScalarTan(radians) (float)sk_float_tan(radians)
michael@0 148 #define SkScalarASin(val) (float)sk_float_asin(val)
michael@0 149 #define SkScalarACos(val) (float)sk_float_acos(val)
michael@0 150 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
michael@0 151 #define SkScalarExp(x) (float)sk_float_exp(x)
michael@0 152 #define SkScalarLog(x) (float)sk_float_log(x)
michael@0 153
michael@0 154 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
michael@0 155 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
michael@0 156
michael@0 157 static inline bool SkScalarIsInt(SkScalar x) {
michael@0 158 return x == (float)(int)x;
michael@0 159 }
michael@0 160
michael@0 161 // DEPRECATED : use ToInt or ToScalar variant
michael@0 162 #ifdef SK_SUPPORT_DEPRECATED_SCALARROUND
michael@0 163 # define SkScalarFloor(x) SkScalarFloorToInt(x)
michael@0 164 # define SkScalarCeil(x) SkScalarCeilToInt(x)
michael@0 165 # define SkScalarRound(x) SkScalarRoundToInt(x)
michael@0 166 #endif
michael@0 167
michael@0 168 /**
michael@0 169 * Returns -1 || 0 || 1 depending on the sign of value:
michael@0 170 * -1 if x < 0
michael@0 171 * 0 if x == 0
michael@0 172 * 1 if x > 0
michael@0 173 */
michael@0 174 static inline int SkScalarSignAsInt(SkScalar x) {
michael@0 175 return x < 0 ? -1 : (x > 0);
michael@0 176 }
michael@0 177
michael@0 178 // Scalar result version of above
michael@0 179 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
michael@0 180 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
michael@0 181 }
michael@0 182
michael@0 183 #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
michael@0 184
michael@0 185 static inline bool SkScalarNearlyZero(SkScalar x,
michael@0 186 SkScalar tolerance = SK_ScalarNearlyZero) {
michael@0 187 SkASSERT(tolerance >= 0);
michael@0 188 return SkScalarAbs(x) <= tolerance;
michael@0 189 }
michael@0 190
michael@0 191 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
michael@0 192 SkScalar tolerance = SK_ScalarNearlyZero) {
michael@0 193 SkASSERT(tolerance >= 0);
michael@0 194 return SkScalarAbs(x-y) <= tolerance;
michael@0 195 }
michael@0 196
michael@0 197 /** Linearly interpolate between A and B, based on t.
michael@0 198 If t is 0, return A
michael@0 199 If t is 1, return B
michael@0 200 else interpolate.
michael@0 201 t must be [0..SK_Scalar1]
michael@0 202 */
michael@0 203 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
michael@0 204 SkASSERT(t >= 0 && t <= SK_Scalar1);
michael@0 205 return A + (B - A) * t;
michael@0 206 }
michael@0 207
michael@0 208 /** Interpolate along the function described by (keys[length], values[length])
michael@0 209 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
michael@0 210 clamp to the min or max value. This function was inspired by a desire
michael@0 211 to change the multiplier for thickness in fakeBold; therefore it assumes
michael@0 212 the number of pairs (length) will be small, and a linear search is used.
michael@0 213 Repeated keys are allowed for discontinuous functions (so long as keys is
michael@0 214 monotonically increasing), and if key is the value of a repeated scalar in
michael@0 215 keys, the first one will be used. However, that may change if a binary
michael@0 216 search is used.
michael@0 217 */
michael@0 218 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
michael@0 219 const SkScalar values[], int length);
michael@0 220
michael@0 221 /*
michael@0 222 * Helper to compare an array of scalars.
michael@0 223 */
michael@0 224 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
michael@0 225 SkASSERT(n >= 0);
michael@0 226 for (int i = 0; i < n; ++i) {
michael@0 227 if (a[i] != b[i]) {
michael@0 228 return false;
michael@0 229 }
michael@0 230 }
michael@0 231 return true;
michael@0 232 }
michael@0 233
michael@0 234 #endif

mercurial