Sat, 03 Jan 2015 20:18:00 +0100
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 2011 Google Inc. |
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 | #include "SkMatrix44.h" |
michael@0 | 9 | |
michael@0 | 10 | static inline bool eq4(const SkMScalar* SK_RESTRICT a, |
michael@0 | 11 | const SkMScalar* SK_RESTRICT b) { |
michael@0 | 12 | return (a[0] == b[0]) & (a[1] == b[1]) & (a[2] == b[2]) & (a[3] == b[3]); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | bool SkMatrix44::operator==(const SkMatrix44& other) const { |
michael@0 | 16 | if (this == &other) { |
michael@0 | 17 | return true; |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | if (this->isTriviallyIdentity() && other.isTriviallyIdentity()) { |
michael@0 | 21 | return true; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | const SkMScalar* SK_RESTRICT a = &fMat[0][0]; |
michael@0 | 25 | const SkMScalar* SK_RESTRICT b = &other.fMat[0][0]; |
michael@0 | 26 | |
michael@0 | 27 | #if 0 |
michael@0 | 28 | for (int i = 0; i < 16; ++i) { |
michael@0 | 29 | if (a[i] != b[i]) { |
michael@0 | 30 | return false; |
michael@0 | 31 | } |
michael@0 | 32 | } |
michael@0 | 33 | return true; |
michael@0 | 34 | #else |
michael@0 | 35 | // to reduce branch instructions, we compare 4 at a time. |
michael@0 | 36 | // see bench/Matrix44Bench.cpp for test. |
michael@0 | 37 | if (!eq4(&a[0], &b[0])) { |
michael@0 | 38 | return false; |
michael@0 | 39 | } |
michael@0 | 40 | if (!eq4(&a[4], &b[4])) { |
michael@0 | 41 | return false; |
michael@0 | 42 | } |
michael@0 | 43 | if (!eq4(&a[8], &b[8])) { |
michael@0 | 44 | return false; |
michael@0 | 45 | } |
michael@0 | 46 | return eq4(&a[12], &b[12]); |
michael@0 | 47 | #endif |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 51 | |
michael@0 | 52 | int SkMatrix44::computeTypeMask() const { |
michael@0 | 53 | unsigned mask = 0; |
michael@0 | 54 | |
michael@0 | 55 | if (0 != perspX() || 0 != perspY() || 0 != perspZ() || 1 != fMat[3][3]) { |
michael@0 | 56 | return kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | if (0 != transX() || 0 != transY() || 0 != transZ()) { |
michael@0 | 60 | mask |= kTranslate_Mask; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | if (1 != scaleX() || 1 != scaleY() || 1 != scaleZ()) { |
michael@0 | 64 | mask |= kScale_Mask; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | if (0 != fMat[1][0] || 0 != fMat[0][1] || 0 != fMat[0][2] || |
michael@0 | 68 | 0 != fMat[2][0] || 0 != fMat[1][2] || 0 != fMat[2][1]) { |
michael@0 | 69 | mask |= kAffine_Mask; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | return mask; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 76 | |
michael@0 | 77 | void SkMatrix44::asColMajorf(float dst[]) const { |
michael@0 | 78 | const SkMScalar* src = &fMat[0][0]; |
michael@0 | 79 | #ifdef SK_MSCALAR_IS_DOUBLE |
michael@0 | 80 | for (int i = 0; i < 16; ++i) { |
michael@0 | 81 | dst[i] = SkMScalarToFloat(src[i]); |
michael@0 | 82 | } |
michael@0 | 83 | #elif defined SK_MSCALAR_IS_FLOAT |
michael@0 | 84 | memcpy(dst, src, 16 * sizeof(float)); |
michael@0 | 85 | #endif |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | void SkMatrix44::asColMajord(double dst[]) const { |
michael@0 | 89 | const SkMScalar* src = &fMat[0][0]; |
michael@0 | 90 | #ifdef SK_MSCALAR_IS_DOUBLE |
michael@0 | 91 | memcpy(dst, src, 16 * sizeof(double)); |
michael@0 | 92 | #elif defined SK_MSCALAR_IS_FLOAT |
michael@0 | 93 | for (int i = 0; i < 16; ++i) { |
michael@0 | 94 | dst[i] = SkMScalarToDouble(src[i]); |
michael@0 | 95 | } |
michael@0 | 96 | #endif |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | void SkMatrix44::asRowMajorf(float dst[]) const { |
michael@0 | 100 | const SkMScalar* src = &fMat[0][0]; |
michael@0 | 101 | for (int i = 0; i < 4; ++i) { |
michael@0 | 102 | dst[0] = SkMScalarToFloat(src[0]); |
michael@0 | 103 | dst[4] = SkMScalarToFloat(src[1]); |
michael@0 | 104 | dst[8] = SkMScalarToFloat(src[2]); |
michael@0 | 105 | dst[12] = SkMScalarToFloat(src[3]); |
michael@0 | 106 | src += 4; |
michael@0 | 107 | dst += 1; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | void SkMatrix44::asRowMajord(double dst[]) const { |
michael@0 | 112 | const SkMScalar* src = &fMat[0][0]; |
michael@0 | 113 | for (int i = 0; i < 4; ++i) { |
michael@0 | 114 | dst[0] = SkMScalarToDouble(src[0]); |
michael@0 | 115 | dst[4] = SkMScalarToDouble(src[1]); |
michael@0 | 116 | dst[8] = SkMScalarToDouble(src[2]); |
michael@0 | 117 | dst[12] = SkMScalarToDouble(src[3]); |
michael@0 | 118 | src += 4; |
michael@0 | 119 | dst += 1; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | void SkMatrix44::setColMajorf(const float src[]) { |
michael@0 | 124 | SkMScalar* dst = &fMat[0][0]; |
michael@0 | 125 | #ifdef SK_MSCALAR_IS_DOUBLE |
michael@0 | 126 | for (int i = 0; i < 16; ++i) { |
michael@0 | 127 | dst[i] = SkMScalarToFloat(src[i]); |
michael@0 | 128 | } |
michael@0 | 129 | #elif defined SK_MSCALAR_IS_FLOAT |
michael@0 | 130 | memcpy(dst, src, 16 * sizeof(float)); |
michael@0 | 131 | #endif |
michael@0 | 132 | |
michael@0 | 133 | this->dirtyTypeMask(); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | void SkMatrix44::setColMajord(const double src[]) { |
michael@0 | 137 | SkMScalar* dst = &fMat[0][0]; |
michael@0 | 138 | #ifdef SK_MSCALAR_IS_DOUBLE |
michael@0 | 139 | memcpy(dst, src, 16 * sizeof(double)); |
michael@0 | 140 | #elif defined SK_MSCALAR_IS_FLOAT |
michael@0 | 141 | for (int i = 0; i < 16; ++i) { |
michael@0 | 142 | dst[i] = SkDoubleToMScalar(src[i]); |
michael@0 | 143 | } |
michael@0 | 144 | #endif |
michael@0 | 145 | |
michael@0 | 146 | this->dirtyTypeMask(); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | void SkMatrix44::setRowMajorf(const float src[]) { |
michael@0 | 150 | SkMScalar* dst = &fMat[0][0]; |
michael@0 | 151 | for (int i = 0; i < 4; ++i) { |
michael@0 | 152 | dst[0] = SkMScalarToFloat(src[0]); |
michael@0 | 153 | dst[4] = SkMScalarToFloat(src[1]); |
michael@0 | 154 | dst[8] = SkMScalarToFloat(src[2]); |
michael@0 | 155 | dst[12] = SkMScalarToFloat(src[3]); |
michael@0 | 156 | src += 4; |
michael@0 | 157 | dst += 1; |
michael@0 | 158 | } |
michael@0 | 159 | this->dirtyTypeMask(); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | void SkMatrix44::setRowMajord(const double src[]) { |
michael@0 | 163 | SkMScalar* dst = &fMat[0][0]; |
michael@0 | 164 | for (int i = 0; i < 4; ++i) { |
michael@0 | 165 | dst[0] = SkDoubleToMScalar(src[0]); |
michael@0 | 166 | dst[4] = SkDoubleToMScalar(src[1]); |
michael@0 | 167 | dst[8] = SkDoubleToMScalar(src[2]); |
michael@0 | 168 | dst[12] = SkDoubleToMScalar(src[3]); |
michael@0 | 169 | src += 4; |
michael@0 | 170 | dst += 1; |
michael@0 | 171 | } |
michael@0 | 172 | this->dirtyTypeMask(); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 176 | |
michael@0 | 177 | const SkMatrix44& SkMatrix44::I() { |
michael@0 | 178 | static const SkMatrix44 gIdentity44(kIdentity_Constructor); |
michael@0 | 179 | return gIdentity44; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | void SkMatrix44::setIdentity() { |
michael@0 | 183 | fMat[0][0] = 1; |
michael@0 | 184 | fMat[0][1] = 0; |
michael@0 | 185 | fMat[0][2] = 0; |
michael@0 | 186 | fMat[0][3] = 0; |
michael@0 | 187 | fMat[1][0] = 0; |
michael@0 | 188 | fMat[1][1] = 1; |
michael@0 | 189 | fMat[1][2] = 0; |
michael@0 | 190 | fMat[1][3] = 0; |
michael@0 | 191 | fMat[2][0] = 0; |
michael@0 | 192 | fMat[2][1] = 0; |
michael@0 | 193 | fMat[2][2] = 1; |
michael@0 | 194 | fMat[2][3] = 0; |
michael@0 | 195 | fMat[3][0] = 0; |
michael@0 | 196 | fMat[3][1] = 0; |
michael@0 | 197 | fMat[3][2] = 0; |
michael@0 | 198 | fMat[3][3] = 1; |
michael@0 | 199 | this->setTypeMask(kIdentity_Mask); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | void SkMatrix44::set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02, |
michael@0 | 203 | SkMScalar m10, SkMScalar m11, SkMScalar m12, |
michael@0 | 204 | SkMScalar m20, SkMScalar m21, SkMScalar m22) { |
michael@0 | 205 | fMat[0][0] = m00; fMat[0][1] = m01; fMat[0][2] = m02; fMat[0][3] = 0; |
michael@0 | 206 | fMat[1][0] = m10; fMat[1][1] = m11; fMat[1][2] = m12; fMat[1][3] = 0; |
michael@0 | 207 | fMat[2][0] = m20; fMat[2][1] = m21; fMat[2][2] = m22; fMat[2][3] = 0; |
michael@0 | 208 | fMat[3][0] = 0; fMat[3][1] = 0; fMat[3][2] = 0; fMat[3][3] = 1; |
michael@0 | 209 | this->dirtyTypeMask(); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 213 | |
michael@0 | 214 | void SkMatrix44::setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz) { |
michael@0 | 215 | this->setIdentity(); |
michael@0 | 216 | |
michael@0 | 217 | if (!dx && !dy && !dz) { |
michael@0 | 218 | return; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | fMat[3][0] = dx; |
michael@0 | 222 | fMat[3][1] = dy; |
michael@0 | 223 | fMat[3][2] = dz; |
michael@0 | 224 | this->setTypeMask(kTranslate_Mask); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | void SkMatrix44::preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz) { |
michael@0 | 228 | if (!dx && !dy && !dz) { |
michael@0 | 229 | return; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | for (int i = 0; i < 4; ++i) { |
michael@0 | 233 | fMat[3][i] = fMat[0][i] * dx + fMat[1][i] * dy + fMat[2][i] * dz + fMat[3][i]; |
michael@0 | 234 | } |
michael@0 | 235 | this->dirtyTypeMask(); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | void SkMatrix44::postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz) { |
michael@0 | 239 | if (!dx && !dy && !dz) { |
michael@0 | 240 | return; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | if (this->getType() & kPerspective_Mask) { |
michael@0 | 244 | for (int i = 0; i < 4; ++i) { |
michael@0 | 245 | fMat[i][0] += fMat[i][3] * dx; |
michael@0 | 246 | fMat[i][1] += fMat[i][3] * dy; |
michael@0 | 247 | fMat[i][2] += fMat[i][3] * dz; |
michael@0 | 248 | } |
michael@0 | 249 | } else { |
michael@0 | 250 | fMat[3][0] += dx; |
michael@0 | 251 | fMat[3][1] += dy; |
michael@0 | 252 | fMat[3][2] += dz; |
michael@0 | 253 | this->dirtyTypeMask(); |
michael@0 | 254 | } |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 258 | |
michael@0 | 259 | void SkMatrix44::setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz) { |
michael@0 | 260 | this->setIdentity(); |
michael@0 | 261 | |
michael@0 | 262 | if (1 == sx && 1 == sy && 1 == sz) { |
michael@0 | 263 | return; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | fMat[0][0] = sx; |
michael@0 | 267 | fMat[1][1] = sy; |
michael@0 | 268 | fMat[2][2] = sz; |
michael@0 | 269 | this->setTypeMask(kScale_Mask); |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | void SkMatrix44::preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz) { |
michael@0 | 273 | if (1 == sx && 1 == sy && 1 == sz) { |
michael@0 | 274 | return; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | // The implementation matrix * pureScale can be shortcut |
michael@0 | 278 | // by knowing that pureScale components effectively scale |
michael@0 | 279 | // the columns of the original matrix. |
michael@0 | 280 | for (int i = 0; i < 4; i++) { |
michael@0 | 281 | fMat[0][i] *= sx; |
michael@0 | 282 | fMat[1][i] *= sy; |
michael@0 | 283 | fMat[2][i] *= sz; |
michael@0 | 284 | } |
michael@0 | 285 | this->dirtyTypeMask(); |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | void SkMatrix44::postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz) { |
michael@0 | 289 | if (1 == sx && 1 == sy && 1 == sz) { |
michael@0 | 290 | return; |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | for (int i = 0; i < 4; i++) { |
michael@0 | 294 | fMat[i][0] *= sx; |
michael@0 | 295 | fMat[i][1] *= sy; |
michael@0 | 296 | fMat[i][2] *= sz; |
michael@0 | 297 | } |
michael@0 | 298 | this->dirtyTypeMask(); |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 302 | |
michael@0 | 303 | void SkMatrix44::setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z, |
michael@0 | 304 | SkMScalar radians) { |
michael@0 | 305 | double len2 = (double)x * x + (double)y * y + (double)z * z; |
michael@0 | 306 | if (1 != len2) { |
michael@0 | 307 | if (0 == len2) { |
michael@0 | 308 | this->setIdentity(); |
michael@0 | 309 | return; |
michael@0 | 310 | } |
michael@0 | 311 | double scale = 1 / sqrt(len2); |
michael@0 | 312 | x = SkDoubleToMScalar(x * scale); |
michael@0 | 313 | y = SkDoubleToMScalar(y * scale); |
michael@0 | 314 | z = SkDoubleToMScalar(z * scale); |
michael@0 | 315 | } |
michael@0 | 316 | this->setRotateAboutUnit(x, y, z, radians); |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | void SkMatrix44::setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z, |
michael@0 | 320 | SkMScalar radians) { |
michael@0 | 321 | double c = cos(radians); |
michael@0 | 322 | double s = sin(radians); |
michael@0 | 323 | double C = 1 - c; |
michael@0 | 324 | double xs = x * s; |
michael@0 | 325 | double ys = y * s; |
michael@0 | 326 | double zs = z * s; |
michael@0 | 327 | double xC = x * C; |
michael@0 | 328 | double yC = y * C; |
michael@0 | 329 | double zC = z * C; |
michael@0 | 330 | double xyC = x * yC; |
michael@0 | 331 | double yzC = y * zC; |
michael@0 | 332 | double zxC = z * xC; |
michael@0 | 333 | |
michael@0 | 334 | // if you're looking at wikipedia, remember that we're column major. |
michael@0 | 335 | this->set3x3(SkDoubleToMScalar(x * xC + c), // scale x |
michael@0 | 336 | SkDoubleToMScalar(xyC + zs), // skew x |
michael@0 | 337 | SkDoubleToMScalar(zxC - ys), // trans x |
michael@0 | 338 | |
michael@0 | 339 | SkDoubleToMScalar(xyC - zs), // skew y |
michael@0 | 340 | SkDoubleToMScalar(y * yC + c), // scale y |
michael@0 | 341 | SkDoubleToMScalar(yzC + xs), // trans y |
michael@0 | 342 | |
michael@0 | 343 | SkDoubleToMScalar(zxC + ys), // persp x |
michael@0 | 344 | SkDoubleToMScalar(yzC - xs), // persp y |
michael@0 | 345 | SkDoubleToMScalar(z * zC + c)); // persp 2 |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 349 | |
michael@0 | 350 | static bool bits_isonly(int value, int mask) { |
michael@0 | 351 | return 0 == (value & ~mask); |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | void SkMatrix44::setConcat(const SkMatrix44& a, const SkMatrix44& b) { |
michael@0 | 355 | const SkMatrix44::TypeMask a_mask = a.getType(); |
michael@0 | 356 | const SkMatrix44::TypeMask b_mask = b.getType(); |
michael@0 | 357 | |
michael@0 | 358 | if (kIdentity_Mask == a_mask) { |
michael@0 | 359 | *this = b; |
michael@0 | 360 | return; |
michael@0 | 361 | } |
michael@0 | 362 | if (kIdentity_Mask == b_mask) { |
michael@0 | 363 | *this = a; |
michael@0 | 364 | return; |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | bool useStorage = (this == &a || this == &b); |
michael@0 | 368 | SkMScalar storage[16]; |
michael@0 | 369 | SkMScalar* result = useStorage ? storage : &fMat[0][0]; |
michael@0 | 370 | |
michael@0 | 371 | // Both matrices are at most scale+translate |
michael@0 | 372 | if (bits_isonly(a_mask | b_mask, kScale_Mask | kTranslate_Mask)) { |
michael@0 | 373 | result[0] = a.fMat[0][0] * b.fMat[0][0]; |
michael@0 | 374 | result[1] = result[2] = result[3] = result[4] = 0; |
michael@0 | 375 | result[5] = a.fMat[1][1] * b.fMat[1][1]; |
michael@0 | 376 | result[6] = result[7] = result[8] = result[9] = 0; |
michael@0 | 377 | result[10] = a.fMat[2][2] * b.fMat[2][2]; |
michael@0 | 378 | result[11] = 0; |
michael@0 | 379 | result[12] = a.fMat[0][0] * b.fMat[3][0] + a.fMat[3][0]; |
michael@0 | 380 | result[13] = a.fMat[1][1] * b.fMat[3][1] + a.fMat[3][1]; |
michael@0 | 381 | result[14] = a.fMat[2][2] * b.fMat[3][2] + a.fMat[3][2]; |
michael@0 | 382 | result[15] = 1; |
michael@0 | 383 | } else { |
michael@0 | 384 | for (int j = 0; j < 4; j++) { |
michael@0 | 385 | for (int i = 0; i < 4; i++) { |
michael@0 | 386 | double value = 0; |
michael@0 | 387 | for (int k = 0; k < 4; k++) { |
michael@0 | 388 | value += SkMScalarToDouble(a.fMat[k][i]) * b.fMat[j][k]; |
michael@0 | 389 | } |
michael@0 | 390 | *result++ = SkDoubleToMScalar(value); |
michael@0 | 391 | } |
michael@0 | 392 | } |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | if (useStorage) { |
michael@0 | 396 | memcpy(fMat, storage, sizeof(storage)); |
michael@0 | 397 | } |
michael@0 | 398 | this->dirtyTypeMask(); |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 402 | |
michael@0 | 403 | /** We always perform the calculation in doubles, to avoid prematurely losing |
michael@0 | 404 | precision along the way. This relies on the compiler automatically |
michael@0 | 405 | promoting our SkMScalar values to double (if needed). |
michael@0 | 406 | */ |
michael@0 | 407 | double SkMatrix44::determinant() const { |
michael@0 | 408 | if (this->isIdentity()) { |
michael@0 | 409 | return 1; |
michael@0 | 410 | } |
michael@0 | 411 | if (this->isScaleTranslate()) { |
michael@0 | 412 | return fMat[0][0] * fMat[1][1] * fMat[2][2] * fMat[3][3]; |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | double a00 = fMat[0][0]; |
michael@0 | 416 | double a01 = fMat[0][1]; |
michael@0 | 417 | double a02 = fMat[0][2]; |
michael@0 | 418 | double a03 = fMat[0][3]; |
michael@0 | 419 | double a10 = fMat[1][0]; |
michael@0 | 420 | double a11 = fMat[1][1]; |
michael@0 | 421 | double a12 = fMat[1][2]; |
michael@0 | 422 | double a13 = fMat[1][3]; |
michael@0 | 423 | double a20 = fMat[2][0]; |
michael@0 | 424 | double a21 = fMat[2][1]; |
michael@0 | 425 | double a22 = fMat[2][2]; |
michael@0 | 426 | double a23 = fMat[2][3]; |
michael@0 | 427 | double a30 = fMat[3][0]; |
michael@0 | 428 | double a31 = fMat[3][1]; |
michael@0 | 429 | double a32 = fMat[3][2]; |
michael@0 | 430 | double a33 = fMat[3][3]; |
michael@0 | 431 | |
michael@0 | 432 | double b00 = a00 * a11 - a01 * a10; |
michael@0 | 433 | double b01 = a00 * a12 - a02 * a10; |
michael@0 | 434 | double b02 = a00 * a13 - a03 * a10; |
michael@0 | 435 | double b03 = a01 * a12 - a02 * a11; |
michael@0 | 436 | double b04 = a01 * a13 - a03 * a11; |
michael@0 | 437 | double b05 = a02 * a13 - a03 * a12; |
michael@0 | 438 | double b06 = a20 * a31 - a21 * a30; |
michael@0 | 439 | double b07 = a20 * a32 - a22 * a30; |
michael@0 | 440 | double b08 = a20 * a33 - a23 * a30; |
michael@0 | 441 | double b09 = a21 * a32 - a22 * a31; |
michael@0 | 442 | double b10 = a21 * a33 - a23 * a31; |
michael@0 | 443 | double b11 = a22 * a33 - a23 * a32; |
michael@0 | 444 | |
michael@0 | 445 | // Calculate the determinant |
michael@0 | 446 | return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 450 | |
michael@0 | 451 | bool SkMatrix44::invert(SkMatrix44* inverse) const { |
michael@0 | 452 | if (this->isIdentity()) { |
michael@0 | 453 | if (inverse) { |
michael@0 | 454 | inverse->setIdentity(); |
michael@0 | 455 | } |
michael@0 | 456 | return true; |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | if (this->isTranslate()) { |
michael@0 | 460 | if (inverse) { |
michael@0 | 461 | inverse->setTranslate(-fMat[3][0], -fMat[3][1], -fMat[3][2]); |
michael@0 | 462 | } |
michael@0 | 463 | return true; |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | if (this->isScaleTranslate()) { |
michael@0 | 467 | if (0 == fMat[0][0] * fMat[1][1] * fMat[2][2]) { |
michael@0 | 468 | return false; |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | if (inverse) { |
michael@0 | 472 | double invXScale = 1 / fMat[0][0]; |
michael@0 | 473 | double invYScale = 1 / fMat[1][1]; |
michael@0 | 474 | double invZScale = 1 / fMat[2][2]; |
michael@0 | 475 | |
michael@0 | 476 | inverse->fMat[0][0] = invXScale; |
michael@0 | 477 | inverse->fMat[0][1] = 0; |
michael@0 | 478 | inverse->fMat[0][2] = 0; |
michael@0 | 479 | inverse->fMat[0][3] = 0; |
michael@0 | 480 | |
michael@0 | 481 | inverse->fMat[1][0] = 0; |
michael@0 | 482 | inverse->fMat[1][1] = invYScale; |
michael@0 | 483 | inverse->fMat[1][2] = 0; |
michael@0 | 484 | inverse->fMat[1][3] = 0; |
michael@0 | 485 | |
michael@0 | 486 | inverse->fMat[2][0] = 0; |
michael@0 | 487 | inverse->fMat[2][1] = 0; |
michael@0 | 488 | inverse->fMat[2][2] = invZScale; |
michael@0 | 489 | inverse->fMat[2][3] = 0; |
michael@0 | 490 | |
michael@0 | 491 | inverse->fMat[3][0] = -fMat[3][0] * invXScale; |
michael@0 | 492 | inverse->fMat[3][1] = -fMat[3][1] * invYScale; |
michael@0 | 493 | inverse->fMat[3][2] = -fMat[3][2] * invZScale; |
michael@0 | 494 | inverse->fMat[3][3] = 1; |
michael@0 | 495 | |
michael@0 | 496 | inverse->setTypeMask(this->getType()); |
michael@0 | 497 | } |
michael@0 | 498 | |
michael@0 | 499 | return true; |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | double a00 = fMat[0][0]; |
michael@0 | 503 | double a01 = fMat[0][1]; |
michael@0 | 504 | double a02 = fMat[0][2]; |
michael@0 | 505 | double a03 = fMat[0][3]; |
michael@0 | 506 | double a10 = fMat[1][0]; |
michael@0 | 507 | double a11 = fMat[1][1]; |
michael@0 | 508 | double a12 = fMat[1][2]; |
michael@0 | 509 | double a13 = fMat[1][3]; |
michael@0 | 510 | double a20 = fMat[2][0]; |
michael@0 | 511 | double a21 = fMat[2][1]; |
michael@0 | 512 | double a22 = fMat[2][2]; |
michael@0 | 513 | double a23 = fMat[2][3]; |
michael@0 | 514 | double a30 = fMat[3][0]; |
michael@0 | 515 | double a31 = fMat[3][1]; |
michael@0 | 516 | double a32 = fMat[3][2]; |
michael@0 | 517 | double a33 = fMat[3][3]; |
michael@0 | 518 | |
michael@0 | 519 | if (!(this->getType() & kPerspective_Mask)) { |
michael@0 | 520 | // If we know the matrix has no perspective, then the perspective |
michael@0 | 521 | // component is (0, 0, 0, 1). We can use this information to save a lot |
michael@0 | 522 | // of arithmetic that would otherwise be spent to compute the inverse |
michael@0 | 523 | // of a general matrix. |
michael@0 | 524 | |
michael@0 | 525 | SkASSERT(a03 == 0); |
michael@0 | 526 | SkASSERT(a13 == 0); |
michael@0 | 527 | SkASSERT(a23 == 0); |
michael@0 | 528 | SkASSERT(a33 == 1); |
michael@0 | 529 | |
michael@0 | 530 | double b00 = a00 * a11 - a01 * a10; |
michael@0 | 531 | double b01 = a00 * a12 - a02 * a10; |
michael@0 | 532 | double b03 = a01 * a12 - a02 * a11; |
michael@0 | 533 | double b06 = a20 * a31 - a21 * a30; |
michael@0 | 534 | double b07 = a20 * a32 - a22 * a30; |
michael@0 | 535 | double b08 = a20; |
michael@0 | 536 | double b09 = a21 * a32 - a22 * a31; |
michael@0 | 537 | double b10 = a21; |
michael@0 | 538 | double b11 = a22; |
michael@0 | 539 | |
michael@0 | 540 | // Calculate the determinant |
michael@0 | 541 | double det = b00 * b11 - b01 * b10 + b03 * b08; |
michael@0 | 542 | |
michael@0 | 543 | double invdet = 1.0 / det; |
michael@0 | 544 | // If det is zero, we want to return false. However, we also want to return false |
michael@0 | 545 | // if 1/det overflows to infinity (i.e. det is denormalized). Both of these are |
michael@0 | 546 | // handled by checking that 1/det is finite. |
michael@0 | 547 | if (!sk_float_isfinite(invdet)) { |
michael@0 | 548 | return false; |
michael@0 | 549 | } |
michael@0 | 550 | if (NULL == inverse) { |
michael@0 | 551 | return true; |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | b00 *= invdet; |
michael@0 | 555 | b01 *= invdet; |
michael@0 | 556 | b03 *= invdet; |
michael@0 | 557 | b06 *= invdet; |
michael@0 | 558 | b07 *= invdet; |
michael@0 | 559 | b08 *= invdet; |
michael@0 | 560 | b09 *= invdet; |
michael@0 | 561 | b10 *= invdet; |
michael@0 | 562 | b11 *= invdet; |
michael@0 | 563 | |
michael@0 | 564 | inverse->fMat[0][0] = SkDoubleToMScalar(a11 * b11 - a12 * b10); |
michael@0 | 565 | inverse->fMat[0][1] = SkDoubleToMScalar(a02 * b10 - a01 * b11); |
michael@0 | 566 | inverse->fMat[0][2] = SkDoubleToMScalar(b03); |
michael@0 | 567 | inverse->fMat[0][3] = 0; |
michael@0 | 568 | inverse->fMat[1][0] = SkDoubleToMScalar(a12 * b08 - a10 * b11); |
michael@0 | 569 | inverse->fMat[1][1] = SkDoubleToMScalar(a00 * b11 - a02 * b08); |
michael@0 | 570 | inverse->fMat[1][2] = SkDoubleToMScalar(-b01); |
michael@0 | 571 | inverse->fMat[1][3] = 0; |
michael@0 | 572 | inverse->fMat[2][0] = SkDoubleToMScalar(a10 * b10 - a11 * b08); |
michael@0 | 573 | inverse->fMat[2][1] = SkDoubleToMScalar(a01 * b08 - a00 * b10); |
michael@0 | 574 | inverse->fMat[2][2] = SkDoubleToMScalar(b00); |
michael@0 | 575 | inverse->fMat[2][3] = 0; |
michael@0 | 576 | inverse->fMat[3][0] = SkDoubleToMScalar(a11 * b07 - a10 * b09 - a12 * b06); |
michael@0 | 577 | inverse->fMat[3][1] = SkDoubleToMScalar(a00 * b09 - a01 * b07 + a02 * b06); |
michael@0 | 578 | inverse->fMat[3][2] = SkDoubleToMScalar(a31 * b01 - a30 * b03 - a32 * b00); |
michael@0 | 579 | inverse->fMat[3][3] = 1; |
michael@0 | 580 | |
michael@0 | 581 | inverse->setTypeMask(this->getType()); |
michael@0 | 582 | return true; |
michael@0 | 583 | } |
michael@0 | 584 | |
michael@0 | 585 | double b00 = a00 * a11 - a01 * a10; |
michael@0 | 586 | double b01 = a00 * a12 - a02 * a10; |
michael@0 | 587 | double b02 = a00 * a13 - a03 * a10; |
michael@0 | 588 | double b03 = a01 * a12 - a02 * a11; |
michael@0 | 589 | double b04 = a01 * a13 - a03 * a11; |
michael@0 | 590 | double b05 = a02 * a13 - a03 * a12; |
michael@0 | 591 | double b06 = a20 * a31 - a21 * a30; |
michael@0 | 592 | double b07 = a20 * a32 - a22 * a30; |
michael@0 | 593 | double b08 = a20 * a33 - a23 * a30; |
michael@0 | 594 | double b09 = a21 * a32 - a22 * a31; |
michael@0 | 595 | double b10 = a21 * a33 - a23 * a31; |
michael@0 | 596 | double b11 = a22 * a33 - a23 * a32; |
michael@0 | 597 | |
michael@0 | 598 | // Calculate the determinant |
michael@0 | 599 | double det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; |
michael@0 | 600 | |
michael@0 | 601 | double invdet = 1.0 / det; |
michael@0 | 602 | // If det is zero, we want to return false. However, we also want to return false |
michael@0 | 603 | // if 1/det overflows to infinity (i.e. det is denormalized). Both of these are |
michael@0 | 604 | // handled by checking that 1/det is finite. |
michael@0 | 605 | if (!sk_float_isfinite(invdet)) { |
michael@0 | 606 | return false; |
michael@0 | 607 | } |
michael@0 | 608 | if (NULL == inverse) { |
michael@0 | 609 | return true; |
michael@0 | 610 | } |
michael@0 | 611 | |
michael@0 | 612 | b00 *= invdet; |
michael@0 | 613 | b01 *= invdet; |
michael@0 | 614 | b02 *= invdet; |
michael@0 | 615 | b03 *= invdet; |
michael@0 | 616 | b04 *= invdet; |
michael@0 | 617 | b05 *= invdet; |
michael@0 | 618 | b06 *= invdet; |
michael@0 | 619 | b07 *= invdet; |
michael@0 | 620 | b08 *= invdet; |
michael@0 | 621 | b09 *= invdet; |
michael@0 | 622 | b10 *= invdet; |
michael@0 | 623 | b11 *= invdet; |
michael@0 | 624 | |
michael@0 | 625 | inverse->fMat[0][0] = SkDoubleToMScalar(a11 * b11 - a12 * b10 + a13 * b09); |
michael@0 | 626 | inverse->fMat[0][1] = SkDoubleToMScalar(a02 * b10 - a01 * b11 - a03 * b09); |
michael@0 | 627 | inverse->fMat[0][2] = SkDoubleToMScalar(a31 * b05 - a32 * b04 + a33 * b03); |
michael@0 | 628 | inverse->fMat[0][3] = SkDoubleToMScalar(a22 * b04 - a21 * b05 - a23 * b03); |
michael@0 | 629 | inverse->fMat[1][0] = SkDoubleToMScalar(a12 * b08 - a10 * b11 - a13 * b07); |
michael@0 | 630 | inverse->fMat[1][1] = SkDoubleToMScalar(a00 * b11 - a02 * b08 + a03 * b07); |
michael@0 | 631 | inverse->fMat[1][2] = SkDoubleToMScalar(a32 * b02 - a30 * b05 - a33 * b01); |
michael@0 | 632 | inverse->fMat[1][3] = SkDoubleToMScalar(a20 * b05 - a22 * b02 + a23 * b01); |
michael@0 | 633 | inverse->fMat[2][0] = SkDoubleToMScalar(a10 * b10 - a11 * b08 + a13 * b06); |
michael@0 | 634 | inverse->fMat[2][1] = SkDoubleToMScalar(a01 * b08 - a00 * b10 - a03 * b06); |
michael@0 | 635 | inverse->fMat[2][2] = SkDoubleToMScalar(a30 * b04 - a31 * b02 + a33 * b00); |
michael@0 | 636 | inverse->fMat[2][3] = SkDoubleToMScalar(a21 * b02 - a20 * b04 - a23 * b00); |
michael@0 | 637 | inverse->fMat[3][0] = SkDoubleToMScalar(a11 * b07 - a10 * b09 - a12 * b06); |
michael@0 | 638 | inverse->fMat[3][1] = SkDoubleToMScalar(a00 * b09 - a01 * b07 + a02 * b06); |
michael@0 | 639 | inverse->fMat[3][2] = SkDoubleToMScalar(a31 * b01 - a30 * b03 - a32 * b00); |
michael@0 | 640 | inverse->fMat[3][3] = SkDoubleToMScalar(a20 * b03 - a21 * b01 + a22 * b00); |
michael@0 | 641 | inverse->dirtyTypeMask(); |
michael@0 | 642 | |
michael@0 | 643 | return true; |
michael@0 | 644 | } |
michael@0 | 645 | |
michael@0 | 646 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 647 | |
michael@0 | 648 | void SkMatrix44::transpose() { |
michael@0 | 649 | SkTSwap(fMat[0][1], fMat[1][0]); |
michael@0 | 650 | SkTSwap(fMat[0][2], fMat[2][0]); |
michael@0 | 651 | SkTSwap(fMat[0][3], fMat[3][0]); |
michael@0 | 652 | SkTSwap(fMat[1][2], fMat[2][1]); |
michael@0 | 653 | SkTSwap(fMat[1][3], fMat[3][1]); |
michael@0 | 654 | SkTSwap(fMat[2][3], fMat[3][2]); |
michael@0 | 655 | |
michael@0 | 656 | if (!this->isTriviallyIdentity()) { |
michael@0 | 657 | this->dirtyTypeMask(); |
michael@0 | 658 | } |
michael@0 | 659 | } |
michael@0 | 660 | |
michael@0 | 661 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 662 | |
michael@0 | 663 | void SkMatrix44::mapScalars(const SkScalar src[4], SkScalar dst[4]) const { |
michael@0 | 664 | SkScalar storage[4]; |
michael@0 | 665 | SkScalar* result = (src == dst) ? storage : dst; |
michael@0 | 666 | |
michael@0 | 667 | for (int i = 0; i < 4; i++) { |
michael@0 | 668 | SkMScalar value = 0; |
michael@0 | 669 | for (int j = 0; j < 4; j++) { |
michael@0 | 670 | value += fMat[j][i] * src[j]; |
michael@0 | 671 | } |
michael@0 | 672 | result[i] = SkMScalarToScalar(value); |
michael@0 | 673 | } |
michael@0 | 674 | |
michael@0 | 675 | if (storage == result) { |
michael@0 | 676 | memcpy(dst, storage, sizeof(storage)); |
michael@0 | 677 | } |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | #ifdef SK_MSCALAR_IS_DOUBLE |
michael@0 | 681 | |
michael@0 | 682 | void SkMatrix44::mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const { |
michael@0 | 683 | SkMScalar storage[4]; |
michael@0 | 684 | SkMScalar* result = (src == dst) ? storage : dst; |
michael@0 | 685 | |
michael@0 | 686 | for (int i = 0; i < 4; i++) { |
michael@0 | 687 | SkMScalar value = 0; |
michael@0 | 688 | for (int j = 0; j < 4; j++) { |
michael@0 | 689 | value += fMat[j][i] * src[j]; |
michael@0 | 690 | } |
michael@0 | 691 | result[i] = value; |
michael@0 | 692 | } |
michael@0 | 693 | |
michael@0 | 694 | if (storage == result) { |
michael@0 | 695 | memcpy(dst, storage, sizeof(storage)); |
michael@0 | 696 | } |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | #endif |
michael@0 | 700 | |
michael@0 | 701 | typedef void (*Map2Procf)(const SkMScalar mat[][4], const float src2[], int count, float dst4[]); |
michael@0 | 702 | typedef void (*Map2Procd)(const SkMScalar mat[][4], const double src2[], int count, double dst4[]); |
michael@0 | 703 | |
michael@0 | 704 | static void map2_if(const SkMScalar mat[][4], const float* SK_RESTRICT src2, |
michael@0 | 705 | int count, float* SK_RESTRICT dst4) { |
michael@0 | 706 | for (int i = 0; i < count; ++i) { |
michael@0 | 707 | dst4[0] = src2[0]; |
michael@0 | 708 | dst4[1] = src2[1]; |
michael@0 | 709 | dst4[2] = 0; |
michael@0 | 710 | dst4[3] = 1; |
michael@0 | 711 | src2 += 2; |
michael@0 | 712 | dst4 += 4; |
michael@0 | 713 | } |
michael@0 | 714 | } |
michael@0 | 715 | |
michael@0 | 716 | static void map2_id(const SkMScalar mat[][4], const double* SK_RESTRICT src2, |
michael@0 | 717 | int count, double* SK_RESTRICT dst4) { |
michael@0 | 718 | for (int i = 0; i < count; ++i) { |
michael@0 | 719 | dst4[0] = src2[0]; |
michael@0 | 720 | dst4[1] = src2[1]; |
michael@0 | 721 | dst4[2] = 0; |
michael@0 | 722 | dst4[3] = 1; |
michael@0 | 723 | src2 += 2; |
michael@0 | 724 | dst4 += 4; |
michael@0 | 725 | } |
michael@0 | 726 | } |
michael@0 | 727 | |
michael@0 | 728 | static void map2_tf(const SkMScalar mat[][4], const float* SK_RESTRICT src2, |
michael@0 | 729 | int count, float* SK_RESTRICT dst4) { |
michael@0 | 730 | const float mat30 = SkMScalarToFloat(mat[3][0]); |
michael@0 | 731 | const float mat31 = SkMScalarToFloat(mat[3][1]); |
michael@0 | 732 | const float mat32 = SkMScalarToFloat(mat[3][2]); |
michael@0 | 733 | for (int n = 0; n < count; ++n) { |
michael@0 | 734 | dst4[0] = src2[0] + mat30; |
michael@0 | 735 | dst4[1] = src2[1] + mat31; |
michael@0 | 736 | dst4[2] = mat32; |
michael@0 | 737 | dst4[3] = 1; |
michael@0 | 738 | src2 += 2; |
michael@0 | 739 | dst4 += 4; |
michael@0 | 740 | } |
michael@0 | 741 | } |
michael@0 | 742 | |
michael@0 | 743 | static void map2_td(const SkMScalar mat[][4], const double* SK_RESTRICT src2, |
michael@0 | 744 | int count, double* SK_RESTRICT dst4) { |
michael@0 | 745 | for (int n = 0; n < count; ++n) { |
michael@0 | 746 | dst4[0] = src2[0] + mat[3][0]; |
michael@0 | 747 | dst4[1] = src2[1] + mat[3][1]; |
michael@0 | 748 | dst4[2] = mat[3][2]; |
michael@0 | 749 | dst4[3] = 1; |
michael@0 | 750 | src2 += 2; |
michael@0 | 751 | dst4 += 4; |
michael@0 | 752 | } |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | static void map2_sf(const SkMScalar mat[][4], const float* SK_RESTRICT src2, |
michael@0 | 756 | int count, float* SK_RESTRICT dst4) { |
michael@0 | 757 | const float mat32 = SkMScalarToFloat(mat[3][2]); |
michael@0 | 758 | for (int n = 0; n < count; ++n) { |
michael@0 | 759 | dst4[0] = SkMScalarToFloat(mat[0][0] * src2[0] + mat[3][0]); |
michael@0 | 760 | dst4[1] = SkMScalarToFloat(mat[1][1] * src2[1] + mat[3][1]); |
michael@0 | 761 | dst4[2] = mat32; |
michael@0 | 762 | dst4[3] = 1; |
michael@0 | 763 | src2 += 2; |
michael@0 | 764 | dst4 += 4; |
michael@0 | 765 | } |
michael@0 | 766 | } |
michael@0 | 767 | |
michael@0 | 768 | static void map2_sd(const SkMScalar mat[][4], const double* SK_RESTRICT src2, |
michael@0 | 769 | int count, double* SK_RESTRICT dst4) { |
michael@0 | 770 | for (int n = 0; n < count; ++n) { |
michael@0 | 771 | dst4[0] = mat[0][0] * src2[0] + mat[3][0]; |
michael@0 | 772 | dst4[1] = mat[1][1] * src2[1] + mat[3][1]; |
michael@0 | 773 | dst4[2] = mat[3][2]; |
michael@0 | 774 | dst4[3] = 1; |
michael@0 | 775 | src2 += 2; |
michael@0 | 776 | dst4 += 4; |
michael@0 | 777 | } |
michael@0 | 778 | } |
michael@0 | 779 | |
michael@0 | 780 | static void map2_af(const SkMScalar mat[][4], const float* SK_RESTRICT src2, |
michael@0 | 781 | int count, float* SK_RESTRICT dst4) { |
michael@0 | 782 | SkMScalar r; |
michael@0 | 783 | for (int n = 0; n < count; ++n) { |
michael@0 | 784 | SkMScalar sx = SkFloatToMScalar(src2[0]); |
michael@0 | 785 | SkMScalar sy = SkFloatToMScalar(src2[1]); |
michael@0 | 786 | r = mat[0][0] * sx + mat[1][0] * sy + mat[3][0]; |
michael@0 | 787 | dst4[0] = SkMScalarToFloat(r); |
michael@0 | 788 | r = mat[0][1] * sx + mat[1][1] * sy + mat[3][1]; |
michael@0 | 789 | dst4[1] = SkMScalarToFloat(r); |
michael@0 | 790 | r = mat[0][2] * sx + mat[1][2] * sy + mat[3][2]; |
michael@0 | 791 | dst4[2] = SkMScalarToFloat(r); |
michael@0 | 792 | dst4[3] = 1; |
michael@0 | 793 | src2 += 2; |
michael@0 | 794 | dst4 += 4; |
michael@0 | 795 | } |
michael@0 | 796 | } |
michael@0 | 797 | |
michael@0 | 798 | static void map2_ad(const SkMScalar mat[][4], const double* SK_RESTRICT src2, |
michael@0 | 799 | int count, double* SK_RESTRICT dst4) { |
michael@0 | 800 | for (int n = 0; n < count; ++n) { |
michael@0 | 801 | double sx = src2[0]; |
michael@0 | 802 | double sy = src2[1]; |
michael@0 | 803 | dst4[0] = mat[0][0] * sx + mat[1][0] * sy + mat[3][0]; |
michael@0 | 804 | dst4[1] = mat[0][1] * sx + mat[1][1] * sy + mat[3][1]; |
michael@0 | 805 | dst4[2] = mat[0][2] * sx + mat[1][2] * sy + mat[3][2]; |
michael@0 | 806 | dst4[3] = 1; |
michael@0 | 807 | src2 += 2; |
michael@0 | 808 | dst4 += 4; |
michael@0 | 809 | } |
michael@0 | 810 | } |
michael@0 | 811 | |
michael@0 | 812 | static void map2_pf(const SkMScalar mat[][4], const float* SK_RESTRICT src2, |
michael@0 | 813 | int count, float* SK_RESTRICT dst4) { |
michael@0 | 814 | SkMScalar r; |
michael@0 | 815 | for (int n = 0; n < count; ++n) { |
michael@0 | 816 | SkMScalar sx = SkFloatToMScalar(src2[0]); |
michael@0 | 817 | SkMScalar sy = SkFloatToMScalar(src2[1]); |
michael@0 | 818 | for (int i = 0; i < 4; i++) { |
michael@0 | 819 | r = mat[0][i] * sx + mat[1][i] * sy + mat[3][i]; |
michael@0 | 820 | dst4[i] = SkMScalarToFloat(r); |
michael@0 | 821 | } |
michael@0 | 822 | src2 += 2; |
michael@0 | 823 | dst4 += 4; |
michael@0 | 824 | } |
michael@0 | 825 | } |
michael@0 | 826 | |
michael@0 | 827 | static void map2_pd(const SkMScalar mat[][4], const double* SK_RESTRICT src2, |
michael@0 | 828 | int count, double* SK_RESTRICT dst4) { |
michael@0 | 829 | for (int n = 0; n < count; ++n) { |
michael@0 | 830 | double sx = src2[0]; |
michael@0 | 831 | double sy = src2[1]; |
michael@0 | 832 | for (int i = 0; i < 4; i++) { |
michael@0 | 833 | dst4[i] = mat[0][i] * sx + mat[1][i] * sy + mat[3][i]; |
michael@0 | 834 | } |
michael@0 | 835 | src2 += 2; |
michael@0 | 836 | dst4 += 4; |
michael@0 | 837 | } |
michael@0 | 838 | } |
michael@0 | 839 | |
michael@0 | 840 | void SkMatrix44::map2(const float src2[], int count, float dst4[]) const { |
michael@0 | 841 | static const Map2Procf gProc[] = { |
michael@0 | 842 | map2_if, map2_tf, map2_sf, map2_sf, map2_af, map2_af, map2_af, map2_af |
michael@0 | 843 | }; |
michael@0 | 844 | |
michael@0 | 845 | TypeMask mask = this->getType(); |
michael@0 | 846 | Map2Procf proc = (mask & kPerspective_Mask) ? map2_pf : gProc[mask]; |
michael@0 | 847 | proc(fMat, src2, count, dst4); |
michael@0 | 848 | } |
michael@0 | 849 | |
michael@0 | 850 | void SkMatrix44::map2(const double src2[], int count, double dst4[]) const { |
michael@0 | 851 | static const Map2Procd gProc[] = { |
michael@0 | 852 | map2_id, map2_td, map2_sd, map2_sd, map2_ad, map2_ad, map2_ad, map2_ad |
michael@0 | 853 | }; |
michael@0 | 854 | |
michael@0 | 855 | TypeMask mask = this->getType(); |
michael@0 | 856 | Map2Procd proc = (mask & kPerspective_Mask) ? map2_pd : gProc[mask]; |
michael@0 | 857 | proc(fMat, src2, count, dst4); |
michael@0 | 858 | } |
michael@0 | 859 | |
michael@0 | 860 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 861 | |
michael@0 | 862 | void SkMatrix44::dump() const { |
michael@0 | 863 | static const char* format = |
michael@0 | 864 | "[%g %g %g %g][%g %g %g %g][%g %g %g %g][%g %g %g %g]\n"; |
michael@0 | 865 | #if 0 |
michael@0 | 866 | SkDebugf(format, |
michael@0 | 867 | fMat[0][0], fMat[1][0], fMat[2][0], fMat[3][0], |
michael@0 | 868 | fMat[0][1], fMat[1][1], fMat[2][1], fMat[3][1], |
michael@0 | 869 | fMat[0][2], fMat[1][2], fMat[2][2], fMat[3][2], |
michael@0 | 870 | fMat[0][3], fMat[1][3], fMat[2][3], fMat[3][3]); |
michael@0 | 871 | #else |
michael@0 | 872 | SkDebugf(format, |
michael@0 | 873 | fMat[0][0], fMat[0][1], fMat[0][2], fMat[0][3], |
michael@0 | 874 | fMat[1][0], fMat[1][1], fMat[1][2], fMat[1][3], |
michael@0 | 875 | fMat[2][0], fMat[2][1], fMat[2][2], fMat[2][3], |
michael@0 | 876 | fMat[3][0], fMat[3][1], fMat[3][2], fMat[3][3]); |
michael@0 | 877 | #endif |
michael@0 | 878 | } |
michael@0 | 879 | |
michael@0 | 880 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 881 | |
michael@0 | 882 | static void initFromMatrix(SkMScalar dst[4][4], const SkMatrix& src) { |
michael@0 | 883 | dst[0][0] = SkScalarToMScalar(src[SkMatrix::kMScaleX]); |
michael@0 | 884 | dst[1][0] = SkScalarToMScalar(src[SkMatrix::kMSkewX]); |
michael@0 | 885 | dst[2][0] = 0; |
michael@0 | 886 | dst[3][0] = SkScalarToMScalar(src[SkMatrix::kMTransX]); |
michael@0 | 887 | dst[0][1] = SkScalarToMScalar(src[SkMatrix::kMSkewY]); |
michael@0 | 888 | dst[1][1] = SkScalarToMScalar(src[SkMatrix::kMScaleY]); |
michael@0 | 889 | dst[2][1] = 0; |
michael@0 | 890 | dst[3][1] = SkScalarToMScalar(src[SkMatrix::kMTransY]); |
michael@0 | 891 | dst[0][2] = 0; |
michael@0 | 892 | dst[1][2] = 0; |
michael@0 | 893 | dst[2][2] = 1; |
michael@0 | 894 | dst[3][2] = 0; |
michael@0 | 895 | dst[0][3] = SkScalarToMScalar(src[SkMatrix::kMPersp0]); |
michael@0 | 896 | dst[1][3] = SkScalarToMScalar(src[SkMatrix::kMPersp1]); |
michael@0 | 897 | dst[2][3] = 0; |
michael@0 | 898 | dst[3][3] = SkScalarToMScalar(src[SkMatrix::kMPersp2]); |
michael@0 | 899 | } |
michael@0 | 900 | |
michael@0 | 901 | SkMatrix44::SkMatrix44(const SkMatrix& src) { |
michael@0 | 902 | initFromMatrix(fMat, src); |
michael@0 | 903 | } |
michael@0 | 904 | |
michael@0 | 905 | SkMatrix44& SkMatrix44::operator=(const SkMatrix& src) { |
michael@0 | 906 | initFromMatrix(fMat, src); |
michael@0 | 907 | |
michael@0 | 908 | if (src.isIdentity()) { |
michael@0 | 909 | this->setTypeMask(kIdentity_Mask); |
michael@0 | 910 | } else { |
michael@0 | 911 | this->dirtyTypeMask(); |
michael@0 | 912 | } |
michael@0 | 913 | return *this; |
michael@0 | 914 | } |
michael@0 | 915 | |
michael@0 | 916 | SkMatrix44::operator SkMatrix() const { |
michael@0 | 917 | SkMatrix dst; |
michael@0 | 918 | |
michael@0 | 919 | dst[SkMatrix::kMScaleX] = SkMScalarToScalar(fMat[0][0]); |
michael@0 | 920 | dst[SkMatrix::kMSkewX] = SkMScalarToScalar(fMat[1][0]); |
michael@0 | 921 | dst[SkMatrix::kMTransX] = SkMScalarToScalar(fMat[3][0]); |
michael@0 | 922 | |
michael@0 | 923 | dst[SkMatrix::kMSkewY] = SkMScalarToScalar(fMat[0][1]); |
michael@0 | 924 | dst[SkMatrix::kMScaleY] = SkMScalarToScalar(fMat[1][1]); |
michael@0 | 925 | dst[SkMatrix::kMTransY] = SkMScalarToScalar(fMat[3][1]); |
michael@0 | 926 | |
michael@0 | 927 | dst[SkMatrix::kMPersp0] = SkMScalarToScalar(fMat[0][3]); |
michael@0 | 928 | dst[SkMatrix::kMPersp1] = SkMScalarToScalar(fMat[1][3]); |
michael@0 | 929 | dst[SkMatrix::kMPersp2] = SkMScalarToScalar(fMat[3][3]); |
michael@0 | 930 | |
michael@0 | 931 | return dst; |
michael@0 | 932 | } |