| 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 "include/core/SkMatrix.h" |
| 9 |
|
| 10 |
#include "include/core/SkPathBuilder.h" |
| 11 |
#include "include/core/SkPoint3.h" |
| 12 |
#include "include/core/SkRSXform.h" |
| 13 |
#include "include/core/SkSamplingOptions.h" |
| 14 |
#include "include/core/SkSize.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/private/base/SkDebug.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| 18 |
#include "include/private/base/SkMalloc.h" |
| 19 |
#include "include/private/base/SkMath.h" |
| 20 |
#include "include/private/base/SkTo.h" |
| 21 |
#include "src/base/SkFloatBits.h" |
| 22 |
#include "src/base/SkVx.h" |
| 23 |
#include "src/core/SkMatrixPriv.h" |
| 24 |
#include "src/core/SkMatrixUtils.h" |
| 25 |
#include "src/core/SkSamplingPriv.h" |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <cmath> |
| 29 |
|
| 30 |
template <typename S, typename T> int min_count(SkSpan<S> a, SkSpan<T> b) { |
| 31 |
return SkToInt(std::min(a.size(), b.size())); |
| 32 |
} |
| 33 |
|
| 34 |
void SkMatrix::doNormalizePerspective() { |
| 35 |
// If the bottom row of the matrix is [0, 0, not_one], we will treat the matrix as if it |
| 36 |
// is in perspective, even though it stills behaves like its affine. If we divide everything |
| 37 |
// by the not_one value, then it will behave the same, but will be treated as affine, |
| 38 |
// and therefore faster (e.g. clients can forward-difference calculations). |
| 39 |
// |
| 40 |
if (0 == fMat[SkMatrix::kMPersp0] && 0 == fMat[SkMatrix::kMPersp1]) { |
| 41 |
SkScalar p2 = fMat[SkMatrix::kMPersp2]; |
| 42 |
if (p2 != 0 && p2 != 1) { |
| 43 |
double inv = 1.0 / p2; |
| 44 |
for (int i = 0; i < 6; ++i) { |
| 45 |
fMat[i] = SkDoubleToScalar(fMat[i] * inv); |
| 46 |
} |
| 47 |
fMat[SkMatrix::kMPersp2] = 1; |
| 48 |
} |
| 49 |
this->setTypeMask(kUnknown_Mask); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
SkMatrix& SkMatrix::reset() { *this = SkMatrix(); return *this; } |
| 54 |
|
| 55 |
SkMatrix& SkMatrix::set9(const SkScalar buffer[9]) { |
| 56 |
memcpy(fMat, buffer, 9 * sizeof(SkScalar)); |
| 57 |
this->setTypeMask(kUnknown_Mask); |
| 58 |
return *this; |
| 59 |
} |
| 60 |
|
| 61 |
SkMatrix& SkMatrix::setAffine(const SkScalar buffer[6]) { |
| 62 |
fMat[kMScaleX] = buffer[kAScaleX]; |
| 63 |
fMat[kMSkewX] = buffer[kASkewX]; |
| 64 |
fMat[kMTransX] = buffer[kATransX]; |
| 65 |
fMat[kMSkewY] = buffer[kASkewY]; |
| 66 |
fMat[kMScaleY] = buffer[kAScaleY]; |
| 67 |
fMat[kMTransY] = buffer[kATransY]; |
| 68 |
fMat[kMPersp0] = 0; |
| 69 |
fMat[kMPersp1] = 0; |
| 70 |
fMat[kMPersp2] = 1; |
| 71 |
this->setTypeMask(kUnknown_Mask); |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
// this aligns with the masks, so we can compute a mask from a variable 0/1 |
| 76 |
enum { |
| 77 |
kTranslate_Shift, |
| 78 |
kScale_Shift, |
| 79 |
kAffine_Shift, |
| 80 |
kPerspective_Shift, |
| 81 |
kRectStaysRect_Shift |
| 82 |
}; |
| 83 |
|
| 84 |
static const int32_t kScalar1Int = 0x3f800000; |
| 85 |
|
| 86 |
uint8_t SkMatrix::computePerspectiveTypeMask() const { |
| 87 |
// Benchmarking suggests that replacing this set of SkScalarAs2sCompliment |
| 88 |
// is a win, but replacing those below is not. We don't yet understand |
| 89 |
// that result. |
| 90 |
if (fMat[kMPersp0] != 0 || fMat[kMPersp1] != 0 || fMat[kMPersp2] != 1) { |
| 91 |
// If this is a perspective transform, we return true for all other |
| 92 |
// transform flags - this does not disable any optimizations, respects |
| 93 |
// the rule that the type mask must be conservative, and speeds up |
| 94 |
// type mask computation. |
| 95 |
return SkToU8(kORableMasks); |
| 96 |
} |
| 97 |
|
| 98 |
return SkToU8(kOnlyPerspectiveValid_Mask | kUnknown_Mask); |
| 99 |
} |
| 100 |
|
| 101 |
uint8_t SkMatrix::computeTypeMask() const { |
| 102 |
unsigned mask = 0; |
| 103 |
|
| 104 |
if (fMat[kMPersp0] != 0 || fMat[kMPersp1] != 0 || fMat[kMPersp2] != 1) { |
| 105 |
// Once it is determined that that this is a perspective transform, |
| 106 |
// all other flags are moot as far as optimizations are concerned. |
| 107 |
return SkToU8(kORableMasks); |
| 108 |
} |
| 109 |
|
| 110 |
if (fMat[kMTransX] != 0 || fMat[kMTransY] != 0) { |
| 111 |
mask |= kTranslate_Mask; |
| 112 |
} |
| 113 |
|
| 114 |
int m00 = SkScalarAs2sCompliment(fMat[SkMatrix::kMScaleX]); |
| 115 |
int m01 = SkScalarAs2sCompliment(fMat[SkMatrix::kMSkewX]); |
| 116 |
int m10 = SkScalarAs2sCompliment(fMat[SkMatrix::kMSkewY]); |
| 117 |
int m11 = SkScalarAs2sCompliment(fMat[SkMatrix::kMScaleY]); |
| 118 |
|
| 119 |
if (m01 | m10) { |
| 120 |
// The skew components may be scale-inducing, unless we are dealing |
| 121 |
// with a pure rotation. Testing for a pure rotation is expensive, |
| 122 |
// so we opt for being conservative by always setting the scale bit. |
| 123 |
// along with affine. |
| 124 |
// By doing this, we are also ensuring that matrices have the same |
| 125 |
// type masks as their inverses. |
| 126 |
mask |= kAffine_Mask | kScale_Mask; |
| 127 |
|
| 128 |
// For rectStaysRect, in the affine case, we only need check that |
| 129 |
// the primary diagonal is all zeros and that the secondary diagonal |
| 130 |
// is all non-zero. |
| 131 |
|
| 132 |
// map non-zero to 1 |
| 133 |
m01 = m01 != 0; |
| 134 |
m10 = m10 != 0; |
| 135 |
|
| 136 |
int dp0 = 0 == (m00 | m11) ; // true if both are 0 |
| 137 |
int ds1 = m01 & m10; // true if both are 1 |
| 138 |
|
| 139 |
mask |= (dp0 & ds1) << kRectStaysRect_Shift; |
| 140 |
} else { |
| 141 |
// Only test for scale explicitly if not affine, since affine sets the |
| 142 |
// scale bit. |
| 143 |
if ((m00 ^ kScalar1Int) | (m11 ^ kScalar1Int)) { |
| 144 |
mask |= kScale_Mask; |
| 145 |
} |
| 146 |
|
| 147 |
// Not affine, therefore we already know secondary diagonal is |
| 148 |
// all zeros, so we just need to check that primary diagonal is |
| 149 |
// all non-zero. |
| 150 |
|
| 151 |
// map non-zero to 1 |
| 152 |
m00 = m00 != 0; |
| 153 |
m11 = m11 != 0; |
| 154 |
|
| 155 |
// record if the (p)rimary diagonal is all non-zero |
| 156 |
mask |= (m00 & m11) << kRectStaysRect_Shift; |
| 157 |
} |
| 158 |
|
| 159 |
return SkToU8(mask); |
| 160 |
} |
| 161 |
|
| 162 |
/////////////////////////////////////////////////////////////////////////////// |
| 163 |
|
| 164 |
bool operator==(const SkMatrix& a, const SkMatrix& b) { |
| 165 |
const SkScalar* SK_RESTRICT ma = a.fMat; |
| 166 |
const SkScalar* SK_RESTRICT mb = b.fMat; |
| 167 |
|
| 168 |
return ma[0] == mb[0] && ma[1] == mb[1] && ma[2] == mb[2] && |
| 169 |
ma[3] == mb[3] && ma[4] == mb[4] && ma[5] == mb[5] && |
| 170 |
ma[6] == mb[6] && ma[7] == mb[7] && ma[8] == mb[8]; |
| 171 |
} |
| 172 |
|
| 173 |
/////////////////////////////////////////////////////////////////////////////// |
| 174 |
|
| 175 |
// helper function to determine if upper-left 2x2 of matrix is degenerate |
| 176 |
static inline bool is_degenerate_2x2(SkScalar scaleX, SkScalar skewX, |
| 177 |
SkScalar skewY, SkScalar scaleY) { |
| 178 |
SkScalar perp_dot = scaleX*scaleY - skewX*skewY; |
| 179 |
return SkScalarNearlyZero(perp_dot, SK_ScalarNearlyZero*SK_ScalarNearlyZero); |
| 180 |
} |
| 181 |
|
| 182 |
/////////////////////////////////////////////////////////////////////////////// |
| 183 |
|
| 184 |
bool SkMatrix::isSimilarity(SkScalar tol) const { |
| 185 |
// if identity or translate matrix |
| 186 |
TypeMask mask = this->getType(); |
| 187 |
if (mask <= kTranslate_Mask) { |
| 188 |
return true; |
| 189 |
} |
| 190 |
if (mask & kPerspective_Mask) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
SkScalar mx = fMat[kMScaleX]; |
| 195 |
SkScalar my = fMat[kMScaleY]; |
| 196 |
// if no skew, can just compare scale factors |
| 197 |
if (!(mask & kAffine_Mask)) { |
| 198 |
return !SkScalarNearlyZero(mx) && SkScalarNearlyEqual(SkScalarAbs(mx), SkScalarAbs(my)); |
| 199 |
} |
| 200 |
SkScalar sx = fMat[kMSkewX]; |
| 201 |
SkScalar sy = fMat[kMSkewY]; |
| 202 |
|
| 203 |
if (is_degenerate_2x2(mx, sx, sy, my)) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
// upper 2x2 is rotation/reflection + uniform scale if basis vectors |
| 208 |
// are 90 degree rotations of each other |
| 209 |
return (SkScalarNearlyEqual(mx, my, tol) && SkScalarNearlyEqual(sx, -sy, tol)) |
| 210 |
|| (SkScalarNearlyEqual(mx, -my, tol) && SkScalarNearlyEqual(sx, sy, tol)); |
| 211 |
} |
| 212 |
|
| 213 |
bool SkMatrix::preservesRightAngles(SkScalar tol) const { |
| 214 |
TypeMask mask = this->getType(); |
| 215 |
|
| 216 |
if (mask <= kTranslate_Mask) { |
| 217 |
// identity, translate and/or scale |
| 218 |
return true; |
| 219 |
} |
| 220 |
if (mask & kPerspective_Mask) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
SkASSERT(mask & (kAffine_Mask | kScale_Mask)); |
| 225 |
|
| 226 |
SkScalar mx = fMat[kMScaleX]; |
| 227 |
SkScalar my = fMat[kMScaleY]; |
| 228 |
SkScalar sx = fMat[kMSkewX]; |
| 229 |
SkScalar sy = fMat[kMSkewY]; |
| 230 |
|
| 231 |
if (is_degenerate_2x2(mx, sx, sy, my)) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
// upper 2x2 is scale + rotation/reflection if basis vectors are orthogonal |
| 236 |
SkVector vec[2]; |
| 237 |
vec[0].set(mx, sy); |
| 238 |
vec[1].set(sx, my); |
| 239 |
|
| 240 |
return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)); |
| 241 |
} |
| 242 |
|
| 243 |
/////////////////////////////////////////////////////////////////////////////// |
| 244 |
|
| 245 |
static inline SkScalar sdot(SkScalar a, SkScalar b, SkScalar c, SkScalar d) { |
| 246 |
return a * b + c * d; |
| 247 |
} |
| 248 |
|
| 249 |
static inline SkScalar sdot(SkScalar a, SkScalar b, SkScalar c, SkScalar d, |
| 250 |
SkScalar e, SkScalar f) { |
| 251 |
return a * b + c * d + e * f; |
| 252 |
} |
| 253 |
|
| 254 |
static inline SkScalar scross(SkScalar a, SkScalar b, SkScalar c, SkScalar d) { |
| 255 |
return a * b - c * d; |
| 256 |
} |
| 257 |
|
| 258 |
SkMatrix& SkMatrix::setTranslate(SkScalar dx, SkScalar dy) { |
| 259 |
*this = SkMatrix(1, 0, dx, |
| 260 |
0, 1, dy, |
| 261 |
0, 0, 1, |
| 262 |
(dx != 0 || dy != 0) ? kTranslate_Mask | kRectStaysRect_Mask |
| 263 |
: kIdentity_Mask | kRectStaysRect_Mask); |
| 264 |
return *this; |
| 265 |
} |
| 266 |
|
| 267 |
SkMatrix& SkMatrix::preTranslate(SkScalar dx, SkScalar dy) { |
| 268 |
const unsigned mask = this->getType(); |
| 269 |
|
| 270 |
if (mask <= kTranslate_Mask) { |
| 271 |
fMat[kMTransX] += dx; |
| 272 |
fMat[kMTransY] += dy; |
| 273 |
} else if (mask & kPerspective_Mask) { |
| 274 |
SkMatrix m; |
| 275 |
m.setTranslate(dx, dy); |
| 276 |
return this->preConcat(m); |
| 277 |
} else { |
| 278 |
fMat[kMTransX] += sdot(fMat[kMScaleX], dx, fMat[kMSkewX], dy); |
| 279 |
fMat[kMTransY] += sdot(fMat[kMSkewY], dx, fMat[kMScaleY], dy); |
| 280 |
} |
| 281 |
this->updateTranslateMask(); |
| 282 |
return *this; |
| 283 |
} |
| 284 |
|
| 285 |
SkMatrix& SkMatrix::postTranslate(SkScalar dx, SkScalar dy) { |
| 286 |
if (this->hasPerspective()) { |
| 287 |
SkMatrix m; |
| 288 |
m.setTranslate(dx, dy); |
| 289 |
this->postConcat(m); |
| 290 |
} else { |
| 291 |
fMat[kMTransX] += dx; |
| 292 |
fMat[kMTransY] += dy; |
| 293 |
this->updateTranslateMask(); |
| 294 |
} |
| 295 |
return *this; |
| 296 |
} |
| 297 |
|
| 298 |
/////////////////////////////////////////////////////////////////////////////// |
| 299 |
|
| 300 |
SkMatrix& SkMatrix::setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 301 |
if (1 == sx && 1 == sy) { |
| 302 |
this->reset(); |
| 303 |
} else { |
| 304 |
this->setScaleTranslate(sx, sy, px - sx * px, py - sy * py); |
| 305 |
} |
| 306 |
return *this; |
| 307 |
} |
| 308 |
|
| 309 |
SkMatrix& SkMatrix::setScale(SkScalar sx, SkScalar sy) { |
| 310 |
auto rectMask = (sx == 0 || sy == 0) ? 0 : kRectStaysRect_Mask; |
| 311 |
*this = SkMatrix(sx, 0, 0, |
| 312 |
0, sy, 0, |
| 313 |
0, 0, 1, |
| 314 |
(sx == 1 && sy == 1) ? kIdentity_Mask | rectMask |
| 315 |
: kScale_Mask | rectMask); |
| 316 |
return *this; |
| 317 |
} |
| 318 |
|
| 319 |
SkMatrix& SkMatrix::preScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 320 |
if (1 == sx && 1 == sy) { |
| 321 |
return *this; |
| 322 |
} |
| 323 |
|
| 324 |
SkMatrix m; |
| 325 |
m.setScale(sx, sy, px, py); |
| 326 |
return this->preConcat(m); |
| 327 |
} |
| 328 |
|
| 329 |
SkMatrix& SkMatrix::preScale(SkScalar sx, SkScalar sy) { |
| 330 |
if (1 == sx && 1 == sy) { |
| 331 |
return *this; |
| 332 |
} |
| 333 |
|
| 334 |
// the assumption is that these multiplies are very cheap, and that |
| 335 |
// a full concat and/or just computing the matrix type is more expensive. |
| 336 |
// Also, the fixed-point case checks for overflow, but the float doesn't, |
| 337 |
// so we can get away with these blind multiplies. |
| 338 |
|
| 339 |
fMat[kMScaleX] *= sx; |
| 340 |
fMat[kMSkewY] *= sx; |
| 341 |
fMat[kMPersp0] *= sx; |
| 342 |
|
| 343 |
fMat[kMSkewX] *= sy; |
| 344 |
fMat[kMScaleY] *= sy; |
| 345 |
fMat[kMPersp1] *= sy; |
| 346 |
|
| 347 |
// Attempt to simplify our type when applying an inverse scale. |
| 348 |
// TODO: The persp/affine preconditions are in place to keep the mask consistent with |
| 349 |
// what computeTypeMask() would produce (persp/skew always implies kScale). |
| 350 |
// We should investigate whether these flag dependencies are truly needed. |
| 351 |
if (fMat[kMScaleX] == 1 && fMat[kMScaleY] == 1 |
| 352 |
&& !(fTypeMask & (kPerspective_Mask | kAffine_Mask))) { |
| 353 |
this->clearTypeMask(kScale_Mask); |
| 354 |
} else { |
| 355 |
this->orTypeMask(kScale_Mask); |
| 356 |
// Remove kRectStaysRect if the preScale factors were 0 |
| 357 |
if (!sx || !sy) { |
| 358 |
this->clearTypeMask(kRectStaysRect_Mask); |
| 359 |
} |
| 360 |
} |
| 361 |
return *this; |
| 362 |
} |
| 363 |
|
| 364 |
SkMatrix& SkMatrix::postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 365 |
if (1 == sx && 1 == sy) { |
| 366 |
return *this; |
| 367 |
} |
| 368 |
SkMatrix m; |
| 369 |
m.setScale(sx, sy, px, py); |
| 370 |
return this->postConcat(m); |
| 371 |
} |
| 372 |
|
| 373 |
SkMatrix& SkMatrix::postScale(SkScalar sx, SkScalar sy) { |
| 374 |
if (1 == sx && 1 == sy) { |
| 375 |
return *this; |
| 376 |
} |
| 377 |
SkMatrix m; |
| 378 |
m.setScale(sx, sy); |
| 379 |
return this->postConcat(m); |
| 380 |
} |
| 381 |
|
| 382 |
// this perhaps can go away, if we have a fract/high-precision way to |
| 383 |
// scale matrices |
| 384 |
bool SkMatrix::postIDiv(int divx, int divy) { |
| 385 |
if (divx == 0 || divy == 0) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
const float invX = 1.f / divx; |
| 390 |
const float invY = 1.f / divy; |
| 391 |
|
| 392 |
fMat[kMScaleX] *= invX; |
| 393 |
fMat[kMSkewX] *= invX; |
| 394 |
fMat[kMTransX] *= invX; |
| 395 |
|
| 396 |
fMat[kMScaleY] *= invY; |
| 397 |
fMat[kMSkewY] *= invY; |
| 398 |
fMat[kMTransY] *= invY; |
| 399 |
|
| 400 |
this->setTypeMask(kUnknown_Mask); |
| 401 |
return true; |
| 402 |
} |
| 403 |
|
| 404 |
//////////////////////////////////////////////////////////////////////////////////// |
| 405 |
|
| 406 |
SkMatrix& SkMatrix::setSinCos(SkScalar sinV, SkScalar cosV, SkScalar px, SkScalar py) { |
| 407 |
const SkScalar oneMinusCosV = 1 - cosV; |
| 408 |
|
| 409 |
fMat[kMScaleX] = cosV; |
| 410 |
fMat[kMSkewX] = -sinV; |
| 411 |
fMat[kMTransX] = sdot(sinV, py, oneMinusCosV, px); |
| 412 |
|
| 413 |
fMat[kMSkewY] = sinV; |
| 414 |
fMat[kMScaleY] = cosV; |
| 415 |
fMat[kMTransY] = sdot(-sinV, px, oneMinusCosV, py); |
| 416 |
|
| 417 |
fMat[kMPersp0] = fMat[kMPersp1] = 0; |
| 418 |
fMat[kMPersp2] = 1; |
| 419 |
|
| 420 |
this->setTypeMask(kUnknown_Mask | kOnlyPerspectiveValid_Mask); |
| 421 |
return *this; |
| 422 |
} |
| 423 |
|
| 424 |
SkMatrix& SkMatrix::setRSXform(const SkRSXform& xform) { |
| 425 |
fMat[kMScaleX] = xform.fSCos; |
| 426 |
fMat[kMSkewX] = -xform.fSSin; |
| 427 |
fMat[kMTransX] = xform.fTx; |
| 428 |
|
| 429 |
fMat[kMSkewY] = xform.fSSin; |
| 430 |
fMat[kMScaleY] = xform.fSCos; |
| 431 |
fMat[kMTransY] = xform.fTy; |
| 432 |
|
| 433 |
fMat[kMPersp0] = fMat[kMPersp1] = 0; |
| 434 |
fMat[kMPersp2] = 1; |
| 435 |
|
| 436 |
this->setTypeMask(kUnknown_Mask | kOnlyPerspectiveValid_Mask); |
| 437 |
return *this; |
| 438 |
} |
| 439 |
|
| 440 |
SkMatrix& SkMatrix::setSinCos(SkScalar sinV, SkScalar cosV) { |
| 441 |
fMat[kMScaleX] = cosV; |
| 442 |
fMat[kMSkewX] = -sinV; |
| 443 |
fMat[kMTransX] = 0; |
| 444 |
|
| 445 |
fMat[kMSkewY] = sinV; |
| 446 |
fMat[kMScaleY] = cosV; |
| 447 |
fMat[kMTransY] = 0; |
| 448 |
|
| 449 |
fMat[kMPersp0] = fMat[kMPersp1] = 0; |
| 450 |
fMat[kMPersp2] = 1; |
| 451 |
|
| 452 |
this->setTypeMask(kUnknown_Mask | kOnlyPerspectiveValid_Mask); |
| 453 |
return *this; |
| 454 |
} |
| 455 |
|
| 456 |
SkMatrix& SkMatrix::setRotate(SkScalar degrees, SkScalar px, SkScalar py) { |
| 457 |
SkScalar rad = SkDegreesToRadians(degrees); |
| 458 |
return this->setSinCos(SkScalarSinSnapToZero(rad), SkScalarCosSnapToZero(rad), px, py); |
| 459 |
} |
| 460 |
|
| 461 |
SkMatrix& SkMatrix::setRotate(SkScalar degrees) { |
| 462 |
SkScalar rad = SkDegreesToRadians(degrees); |
| 463 |
return this->setSinCos(SkScalarSinSnapToZero(rad), SkScalarCosSnapToZero(rad)); |
| 464 |
} |
| 465 |
|
| 466 |
SkMatrix& SkMatrix::preRotate(SkScalar degrees, SkScalar px, SkScalar py) { |
| 467 |
SkMatrix m; |
| 468 |
m.setRotate(degrees, px, py); |
| 469 |
return this->preConcat(m); |
| 470 |
} |
| 471 |
|
| 472 |
SkMatrix& SkMatrix::preRotate(SkScalar degrees) { |
| 473 |
SkMatrix m; |
| 474 |
m.setRotate(degrees); |
| 475 |
return this->preConcat(m); |
| 476 |
} |
| 477 |
|
| 478 |
SkMatrix& SkMatrix::postRotate(SkScalar degrees, SkScalar px, SkScalar py) { |
| 479 |
SkMatrix m; |
| 480 |
m.setRotate(degrees, px, py); |
| 481 |
return this->postConcat(m); |
| 482 |
} |
| 483 |
|
| 484 |
SkMatrix& SkMatrix::postRotate(SkScalar degrees) { |
| 485 |
SkMatrix m; |
| 486 |
m.setRotate(degrees); |
| 487 |
return this->postConcat(m); |
| 488 |
} |
| 489 |
|
| 490 |
//////////////////////////////////////////////////////////////////////////////////// |
| 491 |
|
| 492 |
SkMatrix& SkMatrix::setSkew(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 493 |
*this = SkMatrix(1, sx, -sx * py, |
| 494 |
sy, 1, -sy * px, |
| 495 |
0, 0, 1, |
| 496 |
kUnknown_Mask | kOnlyPerspectiveValid_Mask); |
| 497 |
return *this; |
| 498 |
} |
| 499 |
|
| 500 |
SkMatrix& SkMatrix::setSkew(SkScalar sx, SkScalar sy) { |
| 501 |
fMat[kMScaleX] = 1; |
| 502 |
fMat[kMSkewX] = sx; |
| 503 |
fMat[kMTransX] = 0; |
| 504 |
|
| 505 |
fMat[kMSkewY] = sy; |
| 506 |
fMat[kMScaleY] = 1; |
| 507 |
fMat[kMTransY] = 0; |
| 508 |
|
| 509 |
fMat[kMPersp0] = fMat[kMPersp1] = 0; |
| 510 |
fMat[kMPersp2] = 1; |
| 511 |
|
| 512 |
this->setTypeMask(kUnknown_Mask | kOnlyPerspectiveValid_Mask); |
| 513 |
return *this; |
| 514 |
} |
| 515 |
|
| 516 |
SkMatrix& SkMatrix::preSkew(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 517 |
SkMatrix m; |
| 518 |
m.setSkew(sx, sy, px, py); |
| 519 |
return this->preConcat(m); |
| 520 |
} |
| 521 |
|
| 522 |
SkMatrix& SkMatrix::preSkew(SkScalar sx, SkScalar sy) { |
| 523 |
SkMatrix m; |
| 524 |
m.setSkew(sx, sy); |
| 525 |
return this->preConcat(m); |
| 526 |
} |
| 527 |
|
| 528 |
SkMatrix& SkMatrix::postSkew(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 529 |
SkMatrix m; |
| 530 |
m.setSkew(sx, sy, px, py); |
| 531 |
return this->postConcat(m); |
| 532 |
} |
| 533 |
|
| 534 |
SkMatrix& SkMatrix::postSkew(SkScalar sx, SkScalar sy) { |
| 535 |
SkMatrix m; |
| 536 |
m.setSkew(sx, sy); |
| 537 |
return this->postConcat(m); |
| 538 |
} |
| 539 |
|
| 540 |
/////////////////////////////////////////////////////////////////////////////// |
| 541 |
|
| 542 |
SkMatrix SkMatrix::ScaleTranslate(float sx, float sy, float tx, float ty) { |
| 543 |
uint8_t mask = 0; |
| 544 |
if (sx != 1 || sy != 1) { |
| 545 |
mask |= SkMatrix::kScale_Mask; |
| 546 |
} |
| 547 |
if (tx != 0.0f || ty != 0.0f) { |
| 548 |
mask |= SkMatrix::kTranslate_Mask; |
| 549 |
} |
| 550 |
if (sx != 0 && sy != 0) { |
| 551 |
mask |= SkMatrix::kRectStaysRect_Mask; |
| 552 |
} |
| 553 |
return SkMatrix(sx, 0, tx, |
| 554 |
0, sy, ty, |
| 555 |
0, 0, 1, |
| 556 |
mask); |
| 557 |
} |
| 558 |
|
| 559 |
std::optional<SkMatrix> SkMatrix::Rect2Rect(const SkRect& src, const SkRect& dst, ScaleToFit stf) { |
| 560 |
if (src.isEmpty()) { |
| 561 |
return {}; |
| 562 |
} |
| 563 |
|
| 564 |
SkScalar tx, sx = sk_ieee_float_divide(dst.width(), src.width()); |
| 565 |
SkScalar ty, sy = sk_ieee_float_divide(dst.height(), src.height()); |
| 566 |
bool xLarger = false; |
| 567 |
|
| 568 |
if (stf != kFill_ScaleToFit) { |
| 569 |
if (sx > sy) { |
| 570 |
xLarger = true; |
| 571 |
sx = sy; |
| 572 |
} else { |
| 573 |
sy = sx; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
tx = dst.fLeft - src.fLeft * sx; |
| 578 |
ty = dst.fTop - src.fTop * sy; |
| 579 |
if (stf == kCenter_ScaleToFit || stf == kEnd_ScaleToFit) { |
| 580 |
SkScalar diff; |
| 581 |
|
| 582 |
if (xLarger) { |
| 583 |
diff = dst.width() - src.width() * sy; |
| 584 |
} else { |
| 585 |
diff = dst.height() - src.height() * sy; |
| 586 |
} |
| 587 |
|
| 588 |
if (stf == kCenter_ScaleToFit) { |
| 589 |
diff = SkScalarHalf(diff); |
| 590 |
} |
| 591 |
|
| 592 |
if (xLarger) { |
| 593 |
tx += diff; |
| 594 |
} else { |
| 595 |
ty += diff; |
| 596 |
} |
| 597 |
} |
| 598 |
return ScaleTranslate(sx, sy, tx, ty); |
| 599 |
} |
| 600 |
|
| 601 |
/////////////////////////////////////////////////////////////////////////////// |
| 602 |
|
| 603 |
static inline float muladdmul(float a, float b, float c, float d) { |
| 604 |
return sk_double_to_float((double)a * b + (double)c * d); |
| 605 |
} |
| 606 |
|
| 607 |
static inline float rowcol3(const float row[], const float col[]) { |
| 608 |
return row[0] * col[0] + row[1] * col[3] + row[2] * col[6]; |
| 609 |
} |
| 610 |
|
| 611 |
static bool only_scale_and_translate(unsigned mask) { |
| 612 |
return 0 == (mask & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)); |
| 613 |
} |
| 614 |
|
| 615 |
SkMatrix& SkMatrix::setConcat(const SkMatrix& a, const SkMatrix& b) { |
| 616 |
TypeMask aType = a.getType(); |
| 617 |
TypeMask bType = b.getType(); |
| 618 |
|
| 619 |
if (a.isTriviallyIdentity()) { |
| 620 |
*this = b; |
| 621 |
} else if (b.isTriviallyIdentity()) { |
| 622 |
*this = a; |
| 623 |
} else if (only_scale_and_translate(aType | bType)) { |
| 624 |
this->setScaleTranslate(a.fMat[kMScaleX] * b.fMat[kMScaleX], |
| 625 |
a.fMat[kMScaleY] * b.fMat[kMScaleY], |
| 626 |
a.fMat[kMScaleX] * b.fMat[kMTransX] + a.fMat[kMTransX], |
| 627 |
a.fMat[kMScaleY] * b.fMat[kMTransY] + a.fMat[kMTransY]); |
| 628 |
} else { |
| 629 |
SkMatrix tmp; |
| 630 |
|
| 631 |
if ((aType | bType) & kPerspective_Mask) { |
| 632 |
tmp.fMat[kMScaleX] = rowcol3(&a.fMat[0], &b.fMat[0]); |
| 633 |
tmp.fMat[kMSkewX] = rowcol3(&a.fMat[0], &b.fMat[1]); |
| 634 |
tmp.fMat[kMTransX] = rowcol3(&a.fMat[0], &b.fMat[2]); |
| 635 |
tmp.fMat[kMSkewY] = rowcol3(&a.fMat[3], &b.fMat[0]); |
| 636 |
tmp.fMat[kMScaleY] = rowcol3(&a.fMat[3], &b.fMat[1]); |
| 637 |
tmp.fMat[kMTransY] = rowcol3(&a.fMat[3], &b.fMat[2]); |
| 638 |
tmp.fMat[kMPersp0] = rowcol3(&a.fMat[6], &b.fMat[0]); |
| 639 |
tmp.fMat[kMPersp1] = rowcol3(&a.fMat[6], &b.fMat[1]); |
| 640 |
tmp.fMat[kMPersp2] = rowcol3(&a.fMat[6], &b.fMat[2]); |
| 641 |
|
| 642 |
tmp.setTypeMask(kUnknown_Mask); |
| 643 |
} else { |
| 644 |
tmp.fMat[kMScaleX] = muladdmul(a.fMat[kMScaleX], |
| 645 |
b.fMat[kMScaleX], |
| 646 |
a.fMat[kMSkewX], |
| 647 |
b.fMat[kMSkewY]); |
| 648 |
|
| 649 |
tmp.fMat[kMSkewX] = muladdmul(a.fMat[kMScaleX], |
| 650 |
b.fMat[kMSkewX], |
| 651 |
a.fMat[kMSkewX], |
| 652 |
b.fMat[kMScaleY]); |
| 653 |
|
| 654 |
tmp.fMat[kMTransX] = muladdmul(a.fMat[kMScaleX], |
| 655 |
b.fMat[kMTransX], |
| 656 |
a.fMat[kMSkewX], |
| 657 |
b.fMat[kMTransY]) + a.fMat[kMTransX]; |
| 658 |
|
| 659 |
tmp.fMat[kMSkewY] = muladdmul(a.fMat[kMSkewY], |
| 660 |
b.fMat[kMScaleX], |
| 661 |
a.fMat[kMScaleY], |
| 662 |
b.fMat[kMSkewY]); |
| 663 |
|
| 664 |
tmp.fMat[kMScaleY] = muladdmul(a.fMat[kMSkewY], |
| 665 |
b.fMat[kMSkewX], |
| 666 |
a.fMat[kMScaleY], |
| 667 |
b.fMat[kMScaleY]); |
| 668 |
|
| 669 |
tmp.fMat[kMTransY] = muladdmul(a.fMat[kMSkewY], |
| 670 |
b.fMat[kMTransX], |
| 671 |
a.fMat[kMScaleY], |
| 672 |
b.fMat[kMTransY]) + a.fMat[kMTransY]; |
| 673 |
|
| 674 |
tmp.fMat[kMPersp0] = 0; |
| 675 |
tmp.fMat[kMPersp1] = 0; |
| 676 |
tmp.fMat[kMPersp2] = 1; |
| 677 |
//SkDebugf("Concat mat non-persp type: %d\n", tmp.getType()); |
| 678 |
//SkASSERT(!(tmp.getType() & kPerspective_Mask)); |
| 679 |
tmp.setTypeMask(kUnknown_Mask | kOnlyPerspectiveValid_Mask); |
| 680 |
} |
| 681 |
*this = tmp; |
| 682 |
} |
| 683 |
return *this; |
| 684 |
} |
| 685 |
|
| 686 |
SkMatrix& SkMatrix::preConcat(const SkMatrix& mat) { |
| 687 |
// check for identity first, so we don't do a needless copy of ourselves |
| 688 |
// to ourselves inside setConcat() |
| 689 |
if(!mat.isIdentity()) { |
| 690 |
this->setConcat(*this, mat); |
| 691 |
} |
| 692 |
return *this; |
| 693 |
} |
| 694 |
|
| 695 |
SkMatrix& SkMatrix::postConcat(const SkMatrix& mat) { |
| 696 |
// check for identity first, so we don't do a needless copy of ourselves |
| 697 |
// to ourselves inside setConcat() |
| 698 |
if (!mat.isIdentity()) { |
| 699 |
this->setConcat(mat, *this); |
| 700 |
} |
| 701 |
return *this; |
| 702 |
} |
| 703 |
|
| 704 |
/////////////////////////////////////////////////////////////////////////////// |
| 705 |
|
| 706 |
/* Matrix inversion is very expensive, but also the place where keeping |
| 707 |
precision may be most important (here and matrix concat). Hence to avoid |
| 708 |
bitmap blitting artifacts when walking the inverse, we use doubles for |
| 709 |
the intermediate math, even though we know that is more expensive. |
| 710 |
*/ |
| 711 |
|
| 712 |
static inline SkScalar scross_dscale(SkScalar a, SkScalar b, |
| 713 |
SkScalar c, SkScalar d, double scale) { |
| 714 |
return SkDoubleToScalar(scross(a, b, c, d) * scale); |
| 715 |
} |
| 716 |
|
| 717 |
static inline double dcross(double a, double b, double c, double d) { |
| 718 |
return a * b - c * d; |
| 719 |
} |
| 720 |
|
| 721 |
static inline SkScalar dcross_dscale(double a, double b, |
| 722 |
double c, double d, double scale) { |
| 723 |
return SkDoubleToScalar(dcross(a, b, c, d) * scale); |
| 724 |
} |
| 725 |
|
| 726 |
static double sk_determinant(const float mat[9], int isPerspective) { |
| 727 |
if (isPerspective) { |
| 728 |
return mat[SkMatrix::kMScaleX] * |
| 729 |
dcross(mat[SkMatrix::kMScaleY], mat[SkMatrix::kMPersp2], |
| 730 |
mat[SkMatrix::kMTransY], mat[SkMatrix::kMPersp1]) |
| 731 |
+ |
| 732 |
mat[SkMatrix::kMSkewX] * |
| 733 |
dcross(mat[SkMatrix::kMTransY], mat[SkMatrix::kMPersp0], |
| 734 |
mat[SkMatrix::kMSkewY], mat[SkMatrix::kMPersp2]) |
| 735 |
+ |
| 736 |
mat[SkMatrix::kMTransX] * |
| 737 |
dcross(mat[SkMatrix::kMSkewY], mat[SkMatrix::kMPersp1], |
| 738 |
mat[SkMatrix::kMScaleY], mat[SkMatrix::kMPersp0]); |
| 739 |
} else { |
| 740 |
return dcross(mat[SkMatrix::kMScaleX], mat[SkMatrix::kMScaleY], |
| 741 |
mat[SkMatrix::kMSkewX], mat[SkMatrix::kMSkewY]); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
static double sk_inv_determinant(const float mat[9], int isPerspective) { |
| 746 |
double det = sk_determinant(mat, isPerspective); |
| 747 |
|
| 748 |
// Since the determinant is on the order of the cube of the matrix members, |
| 749 |
// compare to the cube of the default nearly-zero constant (although an |
| 750 |
// estimate of the condition number would be better if it wasn't so expensive). |
| 751 |
if (SkScalarNearlyZero(sk_double_to_float(det), |
| 752 |
SK_ScalarNearlyZero * SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { |
| 753 |
return 0; |
| 754 |
} |
| 755 |
return 1.0 / det; |
| 756 |
} |
| 757 |
|
| 758 |
void SkMatrix::SetAffineIdentity(SkScalar affine[6]) { |
| 759 |
affine[kAScaleX] = 1; |
| 760 |
affine[kASkewY] = 0; |
| 761 |
affine[kASkewX] = 0; |
| 762 |
affine[kAScaleY] = 1; |
| 763 |
affine[kATransX] = 0; |
| 764 |
affine[kATransY] = 0; |
| 765 |
} |
| 766 |
|
| 767 |
bool SkMatrix::asAffine(SkScalar affine[6]) const { |
| 768 |
if (this->hasPerspective()) { |
| 769 |
return false; |
| 770 |
} |
| 771 |
if (affine) { |
| 772 |
affine[kAScaleX] = this->fMat[kMScaleX]; |
| 773 |
affine[kASkewY] = this->fMat[kMSkewY]; |
| 774 |
affine[kASkewX] = this->fMat[kMSkewX]; |
| 775 |
affine[kAScaleY] = this->fMat[kMScaleY]; |
| 776 |
affine[kATransX] = this->fMat[kMTransX]; |
| 777 |
affine[kATransY] = this->fMat[kMTransY]; |
| 778 |
} |
| 779 |
return true; |
| 780 |
} |
| 781 |
|
| 782 |
void SkMatrix::mapPoints(SkSpan<SkPoint> dst, SkSpan<const SkPoint> src) const { |
| 783 |
const auto count = min_count(dst, src); |
| 784 |
this->getMapPtsProc()(*this, dst.data(), src.data(), count); |
| 785 |
} |
| 786 |
|
| 787 |
void SkMatrix::ComputeInv(SkScalar dst[9], const SkScalar src[9], double invDet, bool isPersp) { |
| 788 |
SkASSERT(src != dst); |
| 789 |
SkASSERT(src && dst); |
| 790 |
|
| 791 |
if (isPersp) { |
| 792 |
dst[kMScaleX] = scross_dscale(src[kMScaleY], src[kMPersp2], src[kMTransY], src[kMPersp1], invDet); |
| 793 |
dst[kMSkewX] = scross_dscale(src[kMTransX], src[kMPersp1], src[kMSkewX], src[kMPersp2], invDet); |
| 794 |
dst[kMTransX] = scross_dscale(src[kMSkewX], src[kMTransY], src[kMTransX], src[kMScaleY], invDet); |
| 795 |
|
| 796 |
dst[kMSkewY] = scross_dscale(src[kMTransY], src[kMPersp0], src[kMSkewY], src[kMPersp2], invDet); |
| 797 |
dst[kMScaleY] = scross_dscale(src[kMScaleX], src[kMPersp2], src[kMTransX], src[kMPersp0], invDet); |
| 798 |
dst[kMTransY] = scross_dscale(src[kMTransX], src[kMSkewY], src[kMScaleX], src[kMTransY], invDet); |
| 799 |
|
| 800 |
dst[kMPersp0] = scross_dscale(src[kMSkewY], src[kMPersp1], src[kMScaleY], src[kMPersp0], invDet); |
| 801 |
dst[kMPersp1] = scross_dscale(src[kMSkewX], src[kMPersp0], src[kMScaleX], src[kMPersp1], invDet); |
| 802 |
dst[kMPersp2] = scross_dscale(src[kMScaleX], src[kMScaleY], src[kMSkewX], src[kMSkewY], invDet); |
| 803 |
} else { // not perspective |
| 804 |
dst[kMScaleX] = SkDoubleToScalar(src[kMScaleY] * invDet); |
| 805 |
dst[kMSkewX] = SkDoubleToScalar(-src[kMSkewX] * invDet); |
| 806 |
dst[kMTransX] = dcross_dscale(src[kMSkewX], src[kMTransY], src[kMScaleY], src[kMTransX], invDet); |
| 807 |
|
| 808 |
dst[kMSkewY] = SkDoubleToScalar(-src[kMSkewY] * invDet); |
| 809 |
dst[kMScaleY] = SkDoubleToScalar(src[kMScaleX] * invDet); |
| 810 |
dst[kMTransY] = dcross_dscale(src[kMSkewY], src[kMTransX], src[kMScaleX], src[kMTransY], invDet); |
| 811 |
|
| 812 |
dst[kMPersp0] = 0; |
| 813 |
dst[kMPersp1] = 0; |
| 814 |
dst[kMPersp2] = 1; |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
std::optional<SkMatrix> SkMatrix::invert() const { |
| 819 |
TypeMask mask = this->getType(); |
| 820 |
|
| 821 |
if (mask == kIdentity_Mask) { |
| 822 |
return *this; |
| 823 |
} |
| 824 |
|
| 825 |
// Optimized invert for only scale and/or translation matrices. |
| 826 |
if (0 == (mask & ~(kScale_Mask | kTranslate_Mask))) { |
| 827 |
if (mask & kScale_Mask) { |
| 828 |
// Scale + (optional) Translate |
| 829 |
SkScalar invSX = sk_ieee_float_divide(1.f, fMat[kMScaleX]); |
| 830 |
SkScalar invSY = sk_ieee_float_divide(1.f, fMat[kMScaleY]); |
| 831 |
// Denormalized (non-zero) scale factors will overflow when inverted, in which case |
| 832 |
// the inverse matrix would not be finite, so return false. |
| 833 |
if (!SkIsFinite(invSX, invSY)) { |
| 834 |
return {}; |
| 835 |
} |
| 836 |
SkScalar invTX = -fMat[kMTransX] * invSX; |
| 837 |
SkScalar invTY = -fMat[kMTransY] * invSY; |
| 838 |
// Make sure inverse translation didn't overflow/underflow after dividing by scale. |
| 839 |
// Also catches cases where the original matrix's translation values are not finite. |
| 840 |
if (!SkIsFinite(invTX, invTY)) { |
| 841 |
return {}; |
| 842 |
} |
| 843 |
|
| 844 |
SkMatrix inv; |
| 845 |
inv.fMat[kMSkewX] = inv.fMat[kMSkewY] = |
| 846 |
inv.fMat[kMPersp0] = inv.fMat[kMPersp1] = 0; |
| 847 |
|
| 848 |
inv.fMat[kMScaleX] = invSX; |
| 849 |
inv.fMat[kMScaleY] = invSY; |
| 850 |
inv.fMat[kMPersp2] = 1; |
| 851 |
inv.fMat[kMTransX] = invTX; |
| 852 |
inv.fMat[kMTransY] = invTY; |
| 853 |
|
| 854 |
inv.setTypeMask(mask | kRectStaysRect_Mask); |
| 855 |
return inv; |
| 856 |
} |
| 857 |
|
| 858 |
// Translate-only |
| 859 |
if (!SkIsFinite(fMat[kMTransX], fMat[kMTransY])) { |
| 860 |
// Translation components aren't finite, so inverse isn't possible |
| 861 |
return {}; |
| 862 |
} |
| 863 |
|
| 864 |
return SkMatrix::Translate(-fMat[kMTransX], -fMat[kMTransY]); |
| 865 |
} |
| 866 |
|
| 867 |
int isPersp = mask & kPerspective_Mask; |
| 868 |
double invDet = sk_inv_determinant(fMat, isPersp); |
| 869 |
|
| 870 |
if (invDet == 0) { // underflow |
| 871 |
return {}; |
| 872 |
} |
| 873 |
|
| 874 |
SkMatrix inv; |
| 875 |
ComputeInv(inv.fMat, fMat, invDet, isPersp); |
| 876 |
if (!inv.isFinite()) { |
| 877 |
return {}; |
| 878 |
} |
| 879 |
inv.setTypeMask(fTypeMask); |
| 880 |
return inv; |
| 881 |
} |
| 882 |
|
| 883 |
SkPoint SkMatrix::mapPointPerspective(SkPoint p) const { |
| 884 |
SkScalar x = sdot(p.fX, fMat[kMScaleX], p.fY, fMat[kMSkewX]) + fMat[kMTransX]; |
| 885 |
SkScalar y = sdot(p.fX, fMat[kMSkewY], p.fY, fMat[kMScaleY]) + fMat[kMTransY]; |
| 886 |
SkScalar z = sdot(p.fX, fMat[kMPersp0], p.fY, fMat[kMPersp1]) + fMat[kMPersp2]; |
| 887 |
if (z) { |
| 888 |
z = 1 / z; |
| 889 |
} |
| 890 |
return {x * z, y * z}; |
| 891 |
} |
| 892 |
|
| 893 |
/////////////////////////////////////////////////////////////////////////////// |
| 894 |
|
| 895 |
void SkMatrix::Identity_pts(const SkMatrix& m, SkPoint dst[], const SkPoint src[], int count) { |
| 896 |
SkASSERT(m.getType() == 0); |
| 897 |
|
| 898 |
if (dst != src && count > 0) { |
| 899 |
memcpy(dst, src, count * sizeof(SkPoint)); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
void SkMatrix::Trans_pts(const SkMatrix& m, SkPoint dst[], const SkPoint src[], int count) { |
| 904 |
SkASSERT(m.getType() <= SkMatrix::kTranslate_Mask); |
| 905 |
if (count > 0) { |
| 906 |
SkScalar tx = m.getTranslateX(); |
| 907 |
SkScalar ty = m.getTranslateY(); |
| 908 |
if (count & 1) { |
| 909 |
dst->fX = src->fX + tx; |
| 910 |
dst->fY = src->fY + ty; |
| 911 |
src += 1; |
| 912 |
dst += 1; |
| 913 |
} |
| 914 |
skvx::float4 trans4(tx, ty, tx, ty); |
| 915 |
count >>= 1; |
| 916 |
if (count & 1) { |
| 917 |
(skvx::float4::Load(src) + trans4).store(dst); |
| 918 |
src += 2; |
| 919 |
dst += 2; |
| 920 |
} |
| 921 |
count >>= 1; |
| 922 |
for (int i = 0; i < count; ++i) { |
| 923 |
(skvx::float4::Load(src+0) + trans4).store(dst+0); |
| 924 |
(skvx::float4::Load(src+2) + trans4).store(dst+2); |
| 925 |
src += 4; |
| 926 |
dst += 4; |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
void SkMatrix::Scale_pts(const SkMatrix& m, SkPoint dst[], const SkPoint src[], int count) { |
| 932 |
SkASSERT(m.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)); |
| 933 |
if (count > 0) { |
| 934 |
SkScalar tx = m.getTranslateX(); |
| 935 |
SkScalar ty = m.getTranslateY(); |
| 936 |
SkScalar sx = m.getScaleX(); |
| 937 |
SkScalar sy = m.getScaleY(); |
| 938 |
skvx::float4 trans4(tx, ty, tx, ty); |
| 939 |
skvx::float4 scale4(sx, sy, sx, sy); |
| 940 |
if (count & 1) { |
| 941 |
skvx::float4 p(src->fX, src->fY, 0, 0); |
| 942 |
p = p * scale4 + trans4; |
| 943 |
dst->fX = p[0]; |
| 944 |
dst->fY = p[1]; |
| 945 |
src += 1; |
| 946 |
dst += 1; |
| 947 |
} |
| 948 |
count >>= 1; |
| 949 |
if (count & 1) { |
| 950 |
(skvx::float4::Load(src) * scale4 + trans4).store(dst); |
| 951 |
src += 2; |
| 952 |
dst += 2; |
| 953 |
} |
| 954 |
count >>= 1; |
| 955 |
for (int i = 0; i < count; ++i) { |
| 956 |
(skvx::float4::Load(src+0) * scale4 + trans4).store(dst+0); |
| 957 |
(skvx::float4::Load(src+2) * scale4 + trans4).store(dst+2); |
| 958 |
src += 4; |
| 959 |
dst += 4; |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
void SkMatrix::Persp_pts(const SkMatrix& m, SkPoint dst[], |
| 965 |
const SkPoint src[], int count) { |
| 966 |
SkASSERT(m.hasPerspective()); |
| 967 |
|
| 968 |
if (count > 0) { |
| 969 |
do { |
| 970 |
SkScalar sy = src->fY; |
| 971 |
SkScalar sx = src->fX; |
| 972 |
src += 1; |
| 973 |
|
| 974 |
SkScalar x = sdot(sx, m.fMat[kMScaleX], sy, m.fMat[kMSkewX]) + m.fMat[kMTransX]; |
| 975 |
SkScalar y = sdot(sx, m.fMat[kMSkewY], sy, m.fMat[kMScaleY]) + m.fMat[kMTransY]; |
| 976 |
SkScalar z = sdot(sx, m.fMat[kMPersp0], sy, m.fMat[kMPersp1]) + m.fMat[kMPersp2]; |
| 977 |
if (z) { |
| 978 |
z = 1 / z; |
| 979 |
} |
| 980 |
|
| 981 |
dst->fY = y * z; |
| 982 |
dst->fX = x * z; |
| 983 |
dst += 1; |
| 984 |
} while (--count); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
void SkMatrix::Affine_vpts(const SkMatrix& m, SkPoint dst[], const SkPoint src[], int count) { |
| 989 |
SkASSERT(m.getType() != SkMatrix::kPerspective_Mask); |
| 990 |
if (count > 0) { |
| 991 |
SkScalar tx = m.getTranslateX(); |
| 992 |
SkScalar ty = m.getTranslateY(); |
| 993 |
SkScalar sx = m.getScaleX(); |
| 994 |
SkScalar sy = m.getScaleY(); |
| 995 |
SkScalar kx = m.getSkewX(); |
| 996 |
SkScalar ky = m.getSkewY(); |
| 997 |
skvx::float4 trans4(tx, ty, tx, ty); |
| 998 |
skvx::float4 scale4(sx, sy, sx, sy); |
| 999 |
skvx::float4 skew4(kx, ky, kx, ky); // applied to swizzle of src4 |
| 1000 |
bool trailingElement = (count & 1); |
| 1001 |
count >>= 1; |
| 1002 |
skvx::float4 src4; |
| 1003 |
for (int i = 0; i < count; ++i) { |
| 1004 |
src4 = skvx::float4::Load(src); |
| 1005 |
skvx::float4 swz4 = skvx::shuffle<1,0,3,2>(src4); // y0 x0, y1 x1 |
| 1006 |
(src4 * scale4 + swz4 * skew4 + trans4).store(dst); |
| 1007 |
src += 2; |
| 1008 |
dst += 2; |
| 1009 |
} |
| 1010 |
if (trailingElement) { |
| 1011 |
// We use the same logic here to ensure that the math stays consistent throughout, even |
| 1012 |
// though the high float2 is ignored. |
| 1013 |
src4.lo = skvx::float2::Load(src); |
| 1014 |
skvx::float4 swz4 = skvx::shuffle<1,0,3,2>(src4); // y0 x0, y1 x1 |
| 1015 |
(src4 * scale4 + swz4 * skew4 + trans4).lo.store(dst); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
const SkMatrix::MapPtsProc SkMatrix::gMapPtsProcs[] = { |
| 1021 |
SkMatrix::Identity_pts, SkMatrix::Trans_pts, |
| 1022 |
SkMatrix::Scale_pts, SkMatrix::Scale_pts, |
| 1023 |
SkMatrix::Affine_vpts, SkMatrix::Affine_vpts, |
| 1024 |
SkMatrix::Affine_vpts, SkMatrix::Affine_vpts, |
| 1025 |
// repeat the persp proc 8 times |
| 1026 |
SkMatrix::Persp_pts, SkMatrix::Persp_pts, |
| 1027 |
SkMatrix::Persp_pts, SkMatrix::Persp_pts, |
| 1028 |
SkMatrix::Persp_pts, SkMatrix::Persp_pts, |
| 1029 |
SkMatrix::Persp_pts, SkMatrix::Persp_pts |
| 1030 |
}; |
| 1031 |
|
| 1032 |
/////////////////////////////////////////////////////////////////////////////// |
| 1033 |
|
| 1034 |
void SkMatrixPriv::MapHomogeneousPointsWithStride(const SkMatrix& mx, SkPoint3 dst[], |
| 1035 |
size_t dstStride, const SkPoint3 src[], |
| 1036 |
size_t srcStride, int count) { |
| 1037 |
SkASSERT((dst && src && count > 0) || 0 == count); |
| 1038 |
// no partial overlap |
| 1039 |
SkASSERT(src == dst || &dst[count] <= &src[0] || &src[count] <= &dst[0]); |
| 1040 |
|
| 1041 |
if (count > 0) { |
| 1042 |
if (mx.isIdentity()) { |
| 1043 |
if (src != dst) { |
| 1044 |
if (srcStride == sizeof(SkPoint3) && dstStride == sizeof(SkPoint3)) { |
| 1045 |
memcpy(dst, src, count * sizeof(SkPoint3)); |
| 1046 |
} else { |
| 1047 |
for (int i = 0; i < count; ++i) { |
| 1048 |
*dst = *src; |
| 1049 |
dst = reinterpret_cast<SkPoint3*>(reinterpret_cast<char*>(dst) + dstStride); |
| 1050 |
src = reinterpret_cast<const SkPoint3*>(reinterpret_cast<const char*>(src) + |
| 1051 |
srcStride); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
} |
| 1055 |
return; |
| 1056 |
} |
| 1057 |
do { |
| 1058 |
SkScalar sx = src->fX; |
| 1059 |
SkScalar sy = src->fY; |
| 1060 |
SkScalar sw = src->fZ; |
| 1061 |
src = reinterpret_cast<const SkPoint3*>(reinterpret_cast<const char*>(src) + srcStride); |
| 1062 |
const SkScalar* mat = mx.fMat; |
| 1063 |
typedef SkMatrix M; |
| 1064 |
SkScalar x = sdot(sx, mat[M::kMScaleX], sy, mat[M::kMSkewX], sw, mat[M::kMTransX]); |
| 1065 |
SkScalar y = sdot(sx, mat[M::kMSkewY], sy, mat[M::kMScaleY], sw, mat[M::kMTransY]); |
| 1066 |
SkScalar w = sdot(sx, mat[M::kMPersp0], sy, mat[M::kMPersp1], sw, mat[M::kMPersp2]); |
| 1067 |
|
| 1068 |
dst->set(x, y, w); |
| 1069 |
dst = reinterpret_cast<SkPoint3*>(reinterpret_cast<char*>(dst) + dstStride); |
| 1070 |
} while (--count); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
void SkMatrix::mapHomogeneousPoints(SkSpan<SkPoint3> dst, SkSpan<const SkPoint3> src) const { |
| 1075 |
const auto count = min_count(dst, src); |
| 1076 |
SkMatrixPriv::MapHomogeneousPointsWithStride(*this, dst.data(), sizeof(SkPoint3), src.data(), |
| 1077 |
sizeof(SkPoint3), count); |
| 1078 |
} |
| 1079 |
|
| 1080 |
void SkMatrix::mapPointsToHomogeneous(SkSpan<SkPoint3> dst, SkSpan<const SkPoint> src) const { |
| 1081 |
const auto count = min_count(dst, src); |
| 1082 |
|
| 1083 |
if (this->isIdentity()) { |
| 1084 |
for (int i = 0; i < count; ++i) { |
| 1085 |
dst[i] = { src[i].fX, src[i].fY, 1 }; |
| 1086 |
} |
| 1087 |
} else if (this->hasPerspective()) { |
| 1088 |
for (int i = 0; i < count; ++i) { |
| 1089 |
dst[i] = { |
| 1090 |
fMat[0] * src[i].fX + fMat[1] * src[i].fY + fMat[2], |
| 1091 |
fMat[3] * src[i].fX + fMat[4] * src[i].fY + fMat[5], |
| 1092 |
fMat[6] * src[i].fX + fMat[7] * src[i].fY + fMat[8], |
| 1093 |
}; |
| 1094 |
} |
| 1095 |
} else { // affine |
| 1096 |
for (int i = 0; i < count; ++i) { |
| 1097 |
dst[i] = { |
| 1098 |
fMat[0] * src[i].fX + fMat[1] * src[i].fY + fMat[2], |
| 1099 |
fMat[3] * src[i].fX + fMat[4] * src[i].fY + fMat[5], |
| 1100 |
1, |
| 1101 |
}; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
/////////////////////////////////////////////////////////////////////////////// |
| 1107 |
|
| 1108 |
void SkMatrix::mapVectors(SkSpan<SkPoint> dst, SkSpan<const SkPoint> src) const { |
| 1109 |
if (this->hasPerspective()) { |
| 1110 |
const SkPoint origin = this->mapPointPerspective({0, 0}); |
| 1111 |
|
| 1112 |
for (int i = min_count(dst, src) - 1; i >= 0; --i) { |
| 1113 |
dst[i] = this->mapPointPerspective(src[i]) - origin; |
| 1114 |
} |
| 1115 |
} else { |
| 1116 |
SkMatrix tmp = *this; |
| 1117 |
|
| 1118 |
tmp.fMat[kMTransX] = tmp.fMat[kMTransY] = 0; |
| 1119 |
tmp.clearTypeMask(kTranslate_Mask); |
| 1120 |
tmp.mapPoints(dst, src); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
static skvx::float4 sort_as_rect(const skvx::float4& ltrb) { |
| 1125 |
skvx::float4 rblt(ltrb[2], ltrb[3], ltrb[0], ltrb[1]); |
| 1126 |
auto min = skvx::min(ltrb, rblt); |
| 1127 |
auto max = skvx::max(ltrb, rblt); |
| 1128 |
// We can extract either pair [0,1] or [2,3] from min and max and be correct, but on |
| 1129 |
// ARM this sequence generates the fastest (a single instruction). |
| 1130 |
return skvx::float4(min[2], min[3], max[0], max[1]); |
| 1131 |
} |
| 1132 |
|
| 1133 |
void SkMatrix::mapRectScaleTranslate(SkRect* dst, const SkRect& src) const { |
| 1134 |
SkASSERT(dst); |
| 1135 |
SkASSERT(this->isScaleTranslate()); |
| 1136 |
|
| 1137 |
SkScalar sx = fMat[kMScaleX]; |
| 1138 |
SkScalar sy = fMat[kMScaleY]; |
| 1139 |
SkScalar tx = fMat[kMTransX]; |
| 1140 |
SkScalar ty = fMat[kMTransY]; |
| 1141 |
skvx::float4 scale(sx, sy, sx, sy); |
| 1142 |
skvx::float4 trans(tx, ty, tx, ty); |
| 1143 |
sort_as_rect(skvx::float4::Load(&src.fLeft) * scale + trans).store(&dst->fLeft); |
| 1144 |
} |
| 1145 |
|
| 1146 |
bool SkMatrix::mapRect(SkRect* dst, const SkRect& src) const { |
| 1147 |
SkASSERT(dst); |
| 1148 |
|
| 1149 |
if (this->getType() <= kTranslate_Mask) { |
| 1150 |
SkScalar tx = fMat[kMTransX]; |
| 1151 |
SkScalar ty = fMat[kMTransY]; |
| 1152 |
skvx::float4 trans(tx, ty, tx, ty); |
| 1153 |
sort_as_rect(skvx::float4::Load(&src.fLeft) + trans).store(&dst->fLeft); |
| 1154 |
return true; |
| 1155 |
} |
| 1156 |
if (this->isScaleTranslate()) { |
| 1157 |
this->mapRectScaleTranslate(dst, src); |
| 1158 |
return true; |
| 1159 |
} else if (this->hasPerspective()) { |
| 1160 |
SkPathBuilder builder; |
| 1161 |
builder.addRect(src); |
| 1162 |
builder.transform(*this); |
| 1163 |
*dst = builder.computeBounds(); |
| 1164 |
return false; |
| 1165 |
} else { |
| 1166 |
std::array<SkPoint, 4> quad = src.toQuad(); |
| 1167 |
this->mapPoints(quad); |
| 1168 |
dst->setBoundsNoCheck(quad); |
| 1169 |
return this->rectStaysRect(); // might still return true if rotated by 90, etc. |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
SkScalar SkMatrix::mapRadius(SkScalar radius) const { |
| 1174 |
SkVector vec[2]; |
| 1175 |
|
| 1176 |
vec[0].set(radius, 0); |
| 1177 |
vec[1].set(0, radius); |
| 1178 |
this->mapVectors(vec); |
| 1179 |
|
| 1180 |
SkScalar d0 = vec[0].length(); |
| 1181 |
SkScalar d1 = vec[1].length(); |
| 1182 |
|
| 1183 |
// return geometric mean |
| 1184 |
return SkScalarSqrt(d0 * d1); |
| 1185 |
} |
| 1186 |
|
| 1187 |
/////////////////////////////////////////////////////////////////////////////// |
| 1188 |
#if 0 |
| 1189 |
// if its nearly zero (just made up 26, perhaps it should be bigger or smaller) |
| 1190 |
#define PerspNearlyZero(x) SkScalarNearlyZero(x, (1.0f / (1 << 26))) |
| 1191 |
|
| 1192 |
bool SkMatrix::isFixedStepInX() const { |
| 1193 |
return PerspNearlyZero(fMat[kMPersp0]); |
| 1194 |
} |
| 1195 |
|
| 1196 |
SkVector SkMatrix::fixedStepInX(SkScalar y) const { |
| 1197 |
SkASSERT(PerspNearlyZero(fMat[kMPersp0])); |
| 1198 |
if (PerspNearlyZero(fMat[kMPersp1]) && |
| 1199 |
PerspNearlyZero(fMat[kMPersp2] - 1)) { |
| 1200 |
return SkVector::Make(fMat[kMScaleX], fMat[kMSkewY]); |
| 1201 |
} else { |
| 1202 |
SkScalar z = y * fMat[kMPersp1] + fMat[kMPersp2]; |
| 1203 |
return SkVector::Make(fMat[kMScaleX] / z, fMat[kMSkewY] / z); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
#endif |
| 1207 |
|
| 1208 |
/////////////////////////////////////////////////////////////////////////////// |
| 1209 |
|
| 1210 |
static inline bool checkForZero(float x) { |
| 1211 |
return x*x == 0; |
| 1212 |
} |
| 1213 |
|
| 1214 |
bool SkMatrix::Poly2Proc(const SkPoint srcPt[], SkMatrix* dst) { |
| 1215 |
dst->fMat[kMScaleX] = srcPt[1].fY - srcPt[0].fY; |
| 1216 |
dst->fMat[kMSkewY] = srcPt[0].fX - srcPt[1].fX; |
| 1217 |
dst->fMat[kMPersp0] = 0; |
| 1218 |
|
| 1219 |
dst->fMat[kMSkewX] = srcPt[1].fX - srcPt[0].fX; |
| 1220 |
dst->fMat[kMScaleY] = srcPt[1].fY - srcPt[0].fY; |
| 1221 |
dst->fMat[kMPersp1] = 0; |
| 1222 |
|
| 1223 |
dst->fMat[kMTransX] = srcPt[0].fX; |
| 1224 |
dst->fMat[kMTransY] = srcPt[0].fY; |
| 1225 |
dst->fMat[kMPersp2] = 1; |
| 1226 |
dst->setTypeMask(kUnknown_Mask); |
| 1227 |
return true; |
| 1228 |
} |
| 1229 |
|
| 1230 |
bool SkMatrix::Poly3Proc(const SkPoint srcPt[], SkMatrix* dst) { |
| 1231 |
dst->fMat[kMScaleX] = srcPt[2].fX - srcPt[0].fX; |
| 1232 |
dst->fMat[kMSkewY] = srcPt[2].fY - srcPt[0].fY; |
| 1233 |
dst->fMat[kMPersp0] = 0; |
| 1234 |
|
| 1235 |
dst->fMat[kMSkewX] = srcPt[1].fX - srcPt[0].fX; |
| 1236 |
dst->fMat[kMScaleY] = srcPt[1].fY - srcPt[0].fY; |
| 1237 |
dst->fMat[kMPersp1] = 0; |
| 1238 |
|
| 1239 |
dst->fMat[kMTransX] = srcPt[0].fX; |
| 1240 |
dst->fMat[kMTransY] = srcPt[0].fY; |
| 1241 |
dst->fMat[kMPersp2] = 1; |
| 1242 |
dst->setTypeMask(kUnknown_Mask); |
| 1243 |
return true; |
| 1244 |
} |
| 1245 |
|
| 1246 |
bool SkMatrix::Poly4Proc(const SkPoint srcPt[], SkMatrix* dst) { |
| 1247 |
float a1, a2; |
| 1248 |
float x0, y0, x1, y1, x2, y2; |
| 1249 |
|
| 1250 |
x0 = srcPt[2].fX - srcPt[0].fX; |
| 1251 |
y0 = srcPt[2].fY - srcPt[0].fY; |
| 1252 |
x1 = srcPt[2].fX - srcPt[1].fX; |
| 1253 |
y1 = srcPt[2].fY - srcPt[1].fY; |
| 1254 |
x2 = srcPt[2].fX - srcPt[3].fX; |
| 1255 |
y2 = srcPt[2].fY - srcPt[3].fY; |
| 1256 |
|
| 1257 |
/* check if abs(x2) > abs(y2) */ |
| 1258 |
if ( x2 > 0 ? y2 > 0 ? x2 > y2 : x2 > -y2 : y2 > 0 ? -x2 > y2 : x2 < y2) { |
| 1259 |
float denom = sk_ieee_float_divide(x1 * y2, x2) - y1; |
| 1260 |
if (checkForZero(denom)) { |
| 1261 |
return false; |
| 1262 |
} |
| 1263 |
a1 = (((x0 - x1) * y2 / x2) - y0 + y1) / denom; |
| 1264 |
} else { |
| 1265 |
float denom = x1 - sk_ieee_float_divide(y1 * x2, y2); |
| 1266 |
if (checkForZero(denom)) { |
| 1267 |
return false; |
| 1268 |
} |
| 1269 |
a1 = (x0 - x1 - sk_ieee_float_divide((y0 - y1) * x2, y2)) / denom; |
| 1270 |
} |
| 1271 |
|
| 1272 |
/* check if abs(x1) > abs(y1) */ |
| 1273 |
if ( x1 > 0 ? y1 > 0 ? x1 > y1 : x1 > -y1 : y1 > 0 ? -x1 > y1 : x1 < y1) { |
| 1274 |
float denom = y2 - sk_ieee_float_divide(x2 * y1, x1); |
| 1275 |
if (checkForZero(denom)) { |
| 1276 |
return false; |
| 1277 |
} |
| 1278 |
a2 = (y0 - y2 - sk_ieee_float_divide((x0 - x2) * y1, x1)) / denom; |
| 1279 |
} else { |
| 1280 |
float denom = sk_ieee_float_divide(y2 * x1, y1) - x2; |
| 1281 |
if (checkForZero(denom)) { |
| 1282 |
return false; |
| 1283 |
} |
| 1284 |
a2 = (sk_ieee_float_divide((y0 - y2) * x1, y1) - x0 + x2) / denom; |
| 1285 |
} |
| 1286 |
|
| 1287 |
dst->fMat[kMScaleX] = a2 * srcPt[3].fX + srcPt[3].fX - srcPt[0].fX; |
| 1288 |
dst->fMat[kMSkewY] = a2 * srcPt[3].fY + srcPt[3].fY - srcPt[0].fY; |
| 1289 |
dst->fMat[kMPersp0] = a2; |
| 1290 |
|
| 1291 |
dst->fMat[kMSkewX] = a1 * srcPt[1].fX + srcPt[1].fX - srcPt[0].fX; |
| 1292 |
dst->fMat[kMScaleY] = a1 * srcPt[1].fY + srcPt[1].fY - srcPt[0].fY; |
| 1293 |
dst->fMat[kMPersp1] = a1; |
| 1294 |
|
| 1295 |
dst->fMat[kMTransX] = srcPt[0].fX; |
| 1296 |
dst->fMat[kMTransY] = srcPt[0].fY; |
| 1297 |
dst->fMat[kMPersp2] = 1; |
| 1298 |
dst->setTypeMask(kUnknown_Mask); |
| 1299 |
return true; |
| 1300 |
} |
| 1301 |
|
| 1302 |
typedef bool (*PolyMapProc)(const SkPoint[], SkMatrix*); |
| 1303 |
|
| 1304 |
/* Originally adapted from Rob Johnson's original sample code in QuickDraw GX |
| 1305 |
*/ |
| 1306 |
std::optional<SkMatrix> SkMatrix::PolyToPoly(SkSpan<const SkPoint> src, SkSpan<const SkPoint> dst) { |
| 1307 |
if (src.size() != dst.size() || src.size() > 4) { |
| 1308 |
return {}; |
| 1309 |
} |
| 1310 |
|
| 1311 |
const PolyMapProc gPolyMapProcs[] = { |
| 1312 |
SkMatrix::Poly2Proc, SkMatrix::Poly3Proc, SkMatrix::Poly4Proc |
| 1313 |
}; |
| 1314 |
|
| 1315 |
switch (src.size()) { |
| 1316 |
case 0: return SkMatrix::I(); |
| 1317 |
case 1: return SkMatrix::Translate(dst[0] - src[0]); |
| 1318 |
case 2: [[fallthrough]]; |
| 1319 |
case 3: [[fallthrough]]; |
| 1320 |
case 4: { |
| 1321 |
PolyMapProc proc = gPolyMapProcs[src.size() - 2]; |
| 1322 |
|
| 1323 |
SkMatrix tempMap; |
| 1324 |
if (!proc(src.data(), &tempMap)) { |
| 1325 |
return {}; |
| 1326 |
} |
| 1327 |
auto inverse = tempMap.invert(); |
| 1328 |
if (!inverse) { |
| 1329 |
return {}; |
| 1330 |
} |
| 1331 |
if (!proc(dst.data(), &tempMap)) { |
| 1332 |
return {}; |
| 1333 |
} |
| 1334 |
return tempMap * inverse.value(); |
| 1335 |
} |
| 1336 |
} |
| 1337 |
SkUNREACHABLE; |
| 1338 |
} |
| 1339 |
|
| 1340 |
/////////////////////////////////////////////////////////////////////////////// |
| 1341 |
|
| 1342 |
enum MinMaxOrBoth { |
| 1343 |
kMin_MinMaxOrBoth, |
| 1344 |
kMax_MinMaxOrBoth, |
| 1345 |
kBoth_MinMaxOrBoth |
| 1346 |
}; |
| 1347 |
|
| 1348 |
template <MinMaxOrBoth MIN_MAX_OR_BOTH> bool get_scale_factor(SkMatrix::TypeMask typeMask, |
| 1349 |
const SkScalar m[9], |
| 1350 |
SkScalar results[/*1 or 2*/]) { |
| 1351 |
if (typeMask & SkMatrix::kPerspective_Mask) { |
| 1352 |
return false; |
| 1353 |
} |
| 1354 |
if (SkMatrix::kIdentity_Mask == typeMask) { |
| 1355 |
results[0] = SK_Scalar1; |
| 1356 |
if (kBoth_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1357 |
results[1] = SK_Scalar1; |
| 1358 |
} |
| 1359 |
return true; |
| 1360 |
} |
| 1361 |
if (!(typeMask & SkMatrix::kAffine_Mask)) { |
| 1362 |
if (kMin_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1363 |
results[0] = std::min(SkScalarAbs(m[SkMatrix::kMScaleX]), |
| 1364 |
SkScalarAbs(m[SkMatrix::kMScaleY])); |
| 1365 |
} else if (kMax_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1366 |
results[0] = std::max(SkScalarAbs(m[SkMatrix::kMScaleX]), |
| 1367 |
SkScalarAbs(m[SkMatrix::kMScaleY])); |
| 1368 |
} else { |
| 1369 |
results[0] = SkScalarAbs(m[SkMatrix::kMScaleX]); |
| 1370 |
results[1] = SkScalarAbs(m[SkMatrix::kMScaleY]); |
| 1371 |
if (results[0] > results[1]) { |
| 1372 |
using std::swap; |
| 1373 |
swap(results[0], results[1]); |
| 1374 |
} |
| 1375 |
} |
| 1376 |
return true; |
| 1377 |
} |
| 1378 |
// ignore the translation part of the matrix, just look at 2x2 portion. |
| 1379 |
// compute singular values, take largest or smallest abs value. |
| 1380 |
// [a b; b c] = A^T*A |
| 1381 |
SkScalar a = sdot(m[SkMatrix::kMScaleX], m[SkMatrix::kMScaleX], |
| 1382 |
m[SkMatrix::kMSkewY], m[SkMatrix::kMSkewY]); |
| 1383 |
SkScalar b = sdot(m[SkMatrix::kMScaleX], m[SkMatrix::kMSkewX], |
| 1384 |
m[SkMatrix::kMScaleY], m[SkMatrix::kMSkewY]); |
| 1385 |
SkScalar c = sdot(m[SkMatrix::kMSkewX], m[SkMatrix::kMSkewX], |
| 1386 |
m[SkMatrix::kMScaleY], m[SkMatrix::kMScaleY]); |
| 1387 |
// eigenvalues of A^T*A are the squared singular values of A. |
| 1388 |
// characteristic equation is det((A^T*A) - l*I) = 0 |
| 1389 |
// l^2 - (a + c)l + (ac-b^2) |
| 1390 |
// solve using quadratic equation (divisor is non-zero since l^2 has 1 coeff |
| 1391 |
// and roots are guaranteed to be pos and real). |
| 1392 |
SkScalar bSqd = b * b; |
| 1393 |
// if upper left 2x2 is orthogonal save some math |
| 1394 |
if (bSqd <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) { |
| 1395 |
if (kMin_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1396 |
results[0] = std::min(a, c); |
| 1397 |
} else if (kMax_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1398 |
results[0] = std::max(a, c); |
| 1399 |
} else { |
| 1400 |
results[0] = a; |
| 1401 |
results[1] = c; |
| 1402 |
if (results[0] > results[1]) { |
| 1403 |
using std::swap; |
| 1404 |
swap(results[0], results[1]); |
| 1405 |
} |
| 1406 |
} |
| 1407 |
} else { |
| 1408 |
SkScalar aminusc = a - c; |
| 1409 |
SkScalar apluscdiv2 = SkScalarHalf(a + c); |
| 1410 |
SkScalar x = SkScalarHalf(SkScalarSqrt(aminusc * aminusc + 4 * bSqd)); |
| 1411 |
if (kMin_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1412 |
results[0] = apluscdiv2 - x; |
| 1413 |
} else if (kMax_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1414 |
results[0] = apluscdiv2 + x; |
| 1415 |
} else { |
| 1416 |
results[0] = apluscdiv2 - x; |
| 1417 |
results[1] = apluscdiv2 + x; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
if (!SkIsFinite(results[0])) { |
| 1421 |
return false; |
| 1422 |
} |
| 1423 |
// Due to the floating point inaccuracy, there might be an error in a, b, c |
| 1424 |
// calculated by sdot, further deepened by subsequent arithmetic operations |
| 1425 |
// on them. Therefore, we allow and cap the nearly-zero negative values. |
| 1426 |
if (results[0] < 0) { |
| 1427 |
results[0] = 0; |
| 1428 |
} |
| 1429 |
results[0] = SkScalarSqrt(results[0]); |
| 1430 |
if (kBoth_MinMaxOrBoth == MIN_MAX_OR_BOTH) { |
| 1431 |
if (!SkIsFinite(results[1])) { |
| 1432 |
return false; |
| 1433 |
} |
| 1434 |
if (results[1] < 0) { |
| 1435 |
results[1] = 0; |
| 1436 |
} |
| 1437 |
results[1] = SkScalarSqrt(results[1]); |
| 1438 |
} |
| 1439 |
return true; |
| 1440 |
} |
| 1441 |
|
| 1442 |
SkScalar SkMatrix::getMinScale() const { |
| 1443 |
SkScalar factor; |
| 1444 |
if (get_scale_factor<kMin_MinMaxOrBoth>(this->getType(), fMat, &factor)) { |
| 1445 |
return factor; |
| 1446 |
} else { |
| 1447 |
return -1; |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
SkScalar SkMatrix::getMaxScale() const { |
| 1452 |
SkScalar factor; |
| 1453 |
if (get_scale_factor<kMax_MinMaxOrBoth>(this->getType(), fMat, &factor)) { |
| 1454 |
return factor; |
| 1455 |
} else { |
| 1456 |
return -1; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
bool SkMatrix::getMinMaxScales(SkScalar scaleFactors[2]) const { |
| 1461 |
return get_scale_factor<kBoth_MinMaxOrBoth>(this->getType(), fMat, scaleFactors); |
| 1462 |
} |
| 1463 |
|
| 1464 |
const SkMatrix& SkMatrix::I() { |
| 1465 |
static constexpr SkMatrix identity; |
| 1466 |
SkASSERT(identity.isIdentity()); |
| 1467 |
return identity; |
| 1468 |
} |
| 1469 |
|
| 1470 |
const SkMatrix& SkMatrix::InvalidMatrix() { |
| 1471 |
static constexpr SkMatrix invalid(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, |
| 1472 |
SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, |
| 1473 |
SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, |
| 1474 |
kTranslate_Mask | kScale_Mask | |
| 1475 |
kAffine_Mask | kPerspective_Mask); |
| 1476 |
return invalid; |
| 1477 |
} |
| 1478 |
|
| 1479 |
bool SkMatrix::decomposeScale(SkSize* scale, SkMatrix* remaining) const { |
| 1480 |
if (this->hasPerspective()) { |
| 1481 |
return false; |
| 1482 |
} |
| 1483 |
|
| 1484 |
const SkScalar sx = SkVector::Length(this->getScaleX(), this->getSkewY()); |
| 1485 |
const SkScalar sy = SkVector::Length(this->getSkewX(), this->getScaleY()); |
| 1486 |
if (!SkIsFinite(sx, sy) || |
| 1487 |
SkScalarNearlyZero(sx) || SkScalarNearlyZero(sy)) { |
| 1488 |
return false; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if (scale) { |
| 1492 |
scale->set(sx, sy); |
| 1493 |
} |
| 1494 |
if (remaining) { |
| 1495 |
*remaining = *this; |
| 1496 |
remaining->preScale(SkScalarInvert(sx), SkScalarInvert(sy)); |
| 1497 |
} |
| 1498 |
return true; |
| 1499 |
} |
| 1500 |
|
| 1501 |
/////////////////////////////////////////////////////////////////////////////// |
| 1502 |
|
| 1503 |
size_t SkMatrix::writeToMemory(void* buffer) const { |
| 1504 |
// TODO write less for simple matrices |
| 1505 |
static const size_t sizeInMemory = 9 * sizeof(SkScalar); |
| 1506 |
if (buffer) { |
| 1507 |
memcpy(buffer, fMat, sizeInMemory); |
| 1508 |
} |
| 1509 |
return sizeInMemory; |
| 1510 |
} |
| 1511 |
|
| 1512 |
size_t SkMatrix::readFromMemory(const void* buffer, size_t length) { |
| 1513 |
static const size_t sizeInMemory = 9 * sizeof(SkScalar); |
| 1514 |
if (length < sizeInMemory) { |
| 1515 |
return 0; |
| 1516 |
} |
| 1517 |
memcpy(fMat, buffer, sizeInMemory); |
| 1518 |
this->setTypeMask(kUnknown_Mask); |
| 1519 |
// Figure out the type now so that we're thread-safe |
| 1520 |
(void)this->getType(); |
| 1521 |
return sizeInMemory; |
| 1522 |
} |
| 1523 |
|
| 1524 |
void SkMatrix::dump() const { |
| 1525 |
SkString str; |
| 1526 |
str.appendf("[%8.4f %8.4f %8.4f][%8.4f %8.4f %8.4f][%8.4f %8.4f %8.4f]", |
| 1527 |
fMat[0], fMat[1], fMat[2], fMat[3], fMat[4], fMat[5], |
| 1528 |
fMat[6], fMat[7], fMat[8]); |
| 1529 |
SkDebugf("%s\n", str.c_str()); |
| 1530 |
} |
| 1531 |
|
| 1532 |
/////////////////////////////////////////////////////////////////////////////// |
| 1533 |
|
| 1534 |
bool SkTreatAsSprite(const SkMatrix& mat, const SkISize& size, const SkSamplingOptions& sampling, |
| 1535 |
bool isAntiAlias) { |
| 1536 |
if (!SkSamplingPriv::NoChangeWithIdentityMatrix(sampling)) { |
| 1537 |
return false; |
| 1538 |
} |
| 1539 |
|
| 1540 |
// Our path aa is 2-bits, and our rect aa is 8, so we could use 8, |
| 1541 |
// but in practice 4 seems enough (still looks smooth) and allows |
| 1542 |
// more slightly fractional cases to fall into the fast (sprite) case. |
| 1543 |
static const unsigned kAntiAliasSubpixelBits = 4; |
| 1544 |
|
| 1545 |
const unsigned subpixelBits = isAntiAlias ? kAntiAliasSubpixelBits : 0; |
| 1546 |
|
| 1547 |
// quick reject on affine or perspective |
| 1548 |
if (mat.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { |
| 1549 |
return false; |
| 1550 |
} |
| 1551 |
|
| 1552 |
// We don't want to snap to pixels if we're asking for linear filtering with |
| 1553 |
// a subpixel translation. (b/41322892). |
| 1554 |
// This mirrors `tweak_sampling` in SkImageShader.cpp |
| 1555 |
if (sampling.filter == SkFilterMode::kLinear && |
| 1556 |
(mat.getTranslateX() != (int)mat.getTranslateX() || |
| 1557 |
mat.getTranslateY() != (int)mat.getTranslateY())) { |
| 1558 |
return false; |
| 1559 |
} |
| 1560 |
|
| 1561 |
// quick success check |
| 1562 |
if (!subpixelBits && !(mat.getType() & ~SkMatrix::kTranslate_Mask)) { |
| 1563 |
return true; |
| 1564 |
} |
| 1565 |
|
| 1566 |
// mapRect supports negative scales, so we eliminate those first |
| 1567 |
if (mat.getScaleX() < 0 || mat.getScaleY() < 0) { |
| 1568 |
return false; |
| 1569 |
} |
| 1570 |
|
| 1571 |
SkRect dst; |
| 1572 |
SkIRect isrc = SkIRect::MakeSize(size); |
| 1573 |
|
| 1574 |
{ |
| 1575 |
SkRect src; |
| 1576 |
src.set(isrc); |
| 1577 |
mat.mapRect(&dst, src); |
| 1578 |
} |
| 1579 |
|
| 1580 |
// just apply the translate to isrc |
| 1581 |
isrc.offset(SkScalarRoundToInt(mat.getTranslateX()), |
| 1582 |
SkScalarRoundToInt(mat.getTranslateY())); |
| 1583 |
|
| 1584 |
if (subpixelBits) { |
| 1585 |
isrc.fLeft = SkLeftShift(isrc.fLeft, subpixelBits); |
| 1586 |
isrc.fTop = SkLeftShift(isrc.fTop, subpixelBits); |
| 1587 |
isrc.fRight = SkLeftShift(isrc.fRight, subpixelBits); |
| 1588 |
isrc.fBottom = SkLeftShift(isrc.fBottom, subpixelBits); |
| 1589 |
|
| 1590 |
const float scale = 1 << subpixelBits; |
| 1591 |
dst.fLeft *= scale; |
| 1592 |
dst.fTop *= scale; |
| 1593 |
dst.fRight *= scale; |
| 1594 |
dst.fBottom *= scale; |
| 1595 |
} |
| 1596 |
|
| 1597 |
SkIRect idst; |
| 1598 |
dst.round(&idst); |
| 1599 |
return isrc == idst; |
| 1600 |
} |
| 1601 |
|
| 1602 |
// A square matrix M can be decomposed (via polar decomposition) into two matrices -- |
| 1603 |
// an orthogonal matrix Q and a symmetric matrix S. In turn we can decompose S into U*W*U^T, |
| 1604 |
// where U is another orthogonal matrix and W is a scale matrix. These can be recombined |
| 1605 |
// to give M = (Q*U)*W*U^T, i.e., the product of two orthogonal matrices and a scale matrix. |
| 1606 |
// |
| 1607 |
// The one wrinkle is that traditionally Q may contain a reflection -- the |
| 1608 |
// calculation has been rejiggered to put that reflection into W. |
| 1609 |
bool SkDecomposeUpper2x2(const SkMatrix& matrix, |
| 1610 |
SkPoint* rotation1, |
| 1611 |
SkPoint* scale, |
| 1612 |
SkPoint* rotation2) { |
| 1613 |
|
| 1614 |
SkScalar A = matrix[SkMatrix::kMScaleX]; |
| 1615 |
SkScalar B = matrix[SkMatrix::kMSkewX]; |
| 1616 |
SkScalar C = matrix[SkMatrix::kMSkewY]; |
| 1617 |
SkScalar D = matrix[SkMatrix::kMScaleY]; |
| 1618 |
|
| 1619 |
if (is_degenerate_2x2(A, B, C, D)) { |
| 1620 |
return false; |
| 1621 |
} |
| 1622 |
|
| 1623 |
double w1, w2; |
| 1624 |
SkScalar cos1, sin1; |
| 1625 |
SkScalar cos2, sin2; |
| 1626 |
|
| 1627 |
// do polar decomposition (M = Q*S) |
| 1628 |
SkScalar cosQ, sinQ; |
| 1629 |
double Sa, Sb, Sd; |
| 1630 |
// if M is already symmetric (i.e., M = I*S) |
| 1631 |
if (SkScalarNearlyEqual(B, C)) { |
| 1632 |
cosQ = 1; |
| 1633 |
sinQ = 0; |
| 1634 |
|
| 1635 |
Sa = A; |
| 1636 |
Sb = B; |
| 1637 |
Sd = D; |
| 1638 |
} else { |
| 1639 |
cosQ = A + D; |
| 1640 |
sinQ = C - B; |
| 1641 |
SkScalar reciplen = SkScalarInvert(SkScalarSqrt(cosQ*cosQ + sinQ*sinQ)); |
| 1642 |
cosQ *= reciplen; |
| 1643 |
sinQ *= reciplen; |
| 1644 |
|
| 1645 |
// S = Q^-1*M |
| 1646 |
// we don't calc Sc since it's symmetric |
| 1647 |
Sa = A*cosQ + C*sinQ; |
| 1648 |
Sb = B*cosQ + D*sinQ; |
| 1649 |
Sd = -B*sinQ + D*cosQ; |
| 1650 |
} |
| 1651 |
|
| 1652 |
// Now we need to compute eigenvalues of S (our scale factors) |
| 1653 |
// and eigenvectors (bases for our rotation) |
| 1654 |
// From this, should be able to reconstruct S as U*W*U^T |
| 1655 |
if (SkScalarNearlyZero(SkDoubleToScalar(Sb))) { |
| 1656 |
// already diagonalized |
| 1657 |
cos1 = 1; |
| 1658 |
sin1 = 0; |
| 1659 |
w1 = Sa; |
| 1660 |
w2 = Sd; |
| 1661 |
cos2 = cosQ; |
| 1662 |
sin2 = sinQ; |
| 1663 |
} else { |
| 1664 |
double diff = Sa - Sd; |
| 1665 |
double discriminant = sqrt(diff*diff + 4.0*Sb*Sb); |
| 1666 |
double trace = Sa + Sd; |
| 1667 |
if (diff > 0) { |
| 1668 |
w1 = 0.5*(trace + discriminant); |
| 1669 |
w2 = 0.5*(trace - discriminant); |
| 1670 |
} else { |
| 1671 |
w1 = 0.5*(trace - discriminant); |
| 1672 |
w2 = 0.5*(trace + discriminant); |
| 1673 |
} |
| 1674 |
|
| 1675 |
cos1 = SkDoubleToScalar(Sb); sin1 = SkDoubleToScalar(w1 - Sa); |
| 1676 |
SkScalar reciplen = SkScalarInvert(SkScalarSqrt(cos1*cos1 + sin1*sin1)); |
| 1677 |
cos1 *= reciplen; |
| 1678 |
sin1 *= reciplen; |
| 1679 |
|
| 1680 |
// rotation 2 is composition of Q and U |
| 1681 |
cos2 = cos1*cosQ - sin1*sinQ; |
| 1682 |
sin2 = sin1*cosQ + cos1*sinQ; |
| 1683 |
|
| 1684 |
// rotation 1 is U^T |
| 1685 |
sin1 = -sin1; |
| 1686 |
} |
| 1687 |
|
| 1688 |
if (scale) { |
| 1689 |
scale->fX = SkDoubleToScalar(w1); |
| 1690 |
scale->fY = SkDoubleToScalar(w2); |
| 1691 |
} |
| 1692 |
if (rotation1) { |
| 1693 |
rotation1->fX = cos1; |
| 1694 |
rotation1->fY = sin1; |
| 1695 |
} |
| 1696 |
if (rotation2) { |
| 1697 |
rotation2->fX = cos2; |
| 1698 |
rotation2->fY = sin2; |
| 1699 |
} |
| 1700 |
|
| 1701 |
return true; |
| 1702 |
} |
| 1703 |
|
| 1704 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1705 |
|
| 1706 |
SkScalar SkMatrixPriv::DifferentialAreaScale(const SkMatrix& m, const SkPoint& p) { |
| 1707 |
// [m00 m01 m02] [f(u,v)] |
| 1708 |
// Assuming M = [m10 m11 m12], define the projected p'(u,v) = [g(u,v)] where |
| 1709 |
// [m20 m12 m22] |
| 1710 |
// [x] [u] |
| 1711 |
// f(u,v) = x(u,v) / w(u,v), g(u,v) = y(u,v) / w(u,v) and [y] = M*[v] |
| 1712 |
// [w] [1] |
| 1713 |
// |
| 1714 |
// Then the differential scale factor between p = (u,v) and p' is |det J|, |
| 1715 |
// where J is the Jacobian for p': [df/du dg/du] |
| 1716 |
// [df/dv dg/dv] |
| 1717 |
// and df/du = (w*dx/du - x*dw/du)/w^2, dg/du = (w*dy/du - y*dw/du)/w^2 |
| 1718 |
// df/dv = (w*dx/dv - x*dw/dv)/w^2, dg/dv = (w*dy/dv - y*dw/dv)/w^2 |
| 1719 |
// |
| 1720 |
// From here, |det J| can be rewritten as |det J'/w^3|, where |
| 1721 |
// [x y w ] [x y w ] |
| 1722 |
// J' = [dx/du dy/du dw/du] = [m00 m10 m20] |
| 1723 |
// [dx/dv dy/dv dw/dv] [m01 m11 m21] |
| 1724 |
SkPoint3 xyw = m.mapPointToHomogeneous(p); |
| 1725 |
|
| 1726 |
if (xyw.fZ < SK_ScalarNearlyZero) { |
| 1727 |
// Reaching the discontinuity of xy/w and where the point would clip to w >= 0 |
| 1728 |
return SK_ScalarInfinity; |
| 1729 |
} |
| 1730 |
SkMatrix jacobian = SkMatrix::MakeAll(xyw.fX, xyw.fY, xyw.fZ, |
| 1731 |
m.getScaleX(), m.getSkewY(), m.getPerspX(), |
| 1732 |
m.getSkewX(), m.getScaleY(), m.getPerspY()); |
| 1733 |
|
| 1734 |
double denom = 1.0 / xyw.fZ; // 1/w |
| 1735 |
denom = denom * denom * denom; // 1/w^3 |
| 1736 |
return SkScalarAbs(SkDoubleToScalar(sk_determinant(jacobian.fMat, true) * denom)); |
| 1737 |
} |
| 1738 |
|
| 1739 |
bool SkMatrixPriv::NearlyAffine(const SkMatrix& m, |
| 1740 |
const SkRect& bounds, |
| 1741 |
SkScalar tolerance) { |
| 1742 |
if (!m.hasPerspective()) { |
| 1743 |
return true; |
| 1744 |
} |
| 1745 |
|
| 1746 |
// The idea here is that we are computing the differential area scale at each corner, |
| 1747 |
// and comparing them with some tolerance value. If they are similar, then we can say |
| 1748 |
// that the transformation is nearly affine. |
| 1749 |
|
| 1750 |
// We can map the four points simultaneously. |
| 1751 |
SkPoint3 xyw[4]; |
| 1752 |
m.mapPointsToHomogeneous(xyw, bounds.toQuad()); |
| 1753 |
|
| 1754 |
// Since the Jacobian is a 3x3 matrix, the determinant is a scalar triple product, |
| 1755 |
// and the initial cross product is constant across all four points. |
| 1756 |
SkPoint3 v1{m.getScaleX(), m.getSkewY(), m.getPerspX()}; |
| 1757 |
SkPoint3 v2{m.getSkewX(), m.getScaleY(), m.getPerspY()}; |
| 1758 |
SkPoint3 detCrossProd = v1.cross(v2); |
| 1759 |
|
| 1760 |
// Start with the calculations at P0. |
| 1761 |
if (xyw[0].fZ < SK_ScalarNearlyZero) { |
| 1762 |
// Reaching the discontinuity of xy/w and where the point would clip to w >= 0 |
| 1763 |
return false; |
| 1764 |
} |
| 1765 |
|
| 1766 |
// Performing a dot product with the pre-w divide transformed point completes |
| 1767 |
// the scalar triple product and the determinant calculation. |
| 1768 |
double det = detCrossProd.dot(xyw[0]); |
| 1769 |
// From that we can compute the differential area scale at P0. |
| 1770 |
double denom = 1.0 / xyw[0].fZ; // 1/w |
| 1771 |
denom = denom * denom * denom; // 1/w^3 |
| 1772 |
SkScalar a0 = SkScalarAbs(SkDoubleToScalar(det*denom)); |
| 1773 |
|
| 1774 |
// Now we compare P0's scale with that at the other three points |
| 1775 |
tolerance *= tolerance; // squared tolerance since we're comparing area |
| 1776 |
for (int i = 1; i < 4; ++i) { |
| 1777 |
if (xyw[i].fZ < SK_ScalarNearlyZero) { |
| 1778 |
// Reaching the discontinuity of xy/w and where the point would clip to w >= 0 |
| 1779 |
return false; |
| 1780 |
} |
| 1781 |
|
| 1782 |
det = detCrossProd.dot(xyw[i]); // completing scalar triple product |
| 1783 |
denom = 1.0 / xyw[i].fZ; // 1/w |
| 1784 |
denom = denom * denom * denom; // 1/w^3 |
| 1785 |
SkScalar a = SkScalarAbs(SkDoubleToScalar(det*denom)); |
| 1786 |
if (!SkScalarNearlyEqual(a0, a, tolerance)) { |
| 1787 |
return false; |
| 1788 |
} |
| 1789 |
} |
| 1790 |
|
| 1791 |
return true; |
| 1792 |
} |
| 1793 |
|
| 1794 |
SkScalar SkMatrixPriv::ComputeResScaleForStroking(const SkMatrix& matrix) { |
| 1795 |
// Not sure how to handle perspective differently, so we just don't try (yet) |
| 1796 |
SkScalar sx = SkPoint::Length(matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewY]); |
| 1797 |
SkScalar sy = SkPoint::Length(matrix[SkMatrix::kMSkewX], matrix[SkMatrix::kMScaleY]); |
| 1798 |
if (SkIsFinite(sx, sy)) { |
| 1799 |
SkScalar scale = std::max(sx, sy); |
| 1800 |
if (scale > 0) { |
| 1801 |
return scale; |
| 1802 |
} |
| 1803 |
} |
| 1804 |
return 1; |
| 1805 |
} |