Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: src/utils/SkMatrix44.cpp

Issue 23296006: Improve performance of matrix inversion. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: patch for landing Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkMatrix44.h" 8 #include "SkMatrix44.h"
9 9
10 static inline bool eq4(const SkMScalar* SK_RESTRICT a, 10 static inline bool eq4(const SkMScalar* SK_RESTRICT a,
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 } 475 }
476 return true; 476 return true;
477 } 477 }
478 if (this->isScaleTranslate()) { 478 if (this->isScaleTranslate()) {
479 if (0 == fMat[0][0] * fMat[1][1] * fMat[2][2]) { 479 if (0 == fMat[0][0] * fMat[1][1] * fMat[2][2]) {
480 return false; 480 return false;
481 } 481 }
482 if (inverse) { 482 if (inverse) {
483 sk_bzero(inverse->fMat, sizeof(inverse->fMat)); 483 sk_bzero(inverse->fMat, sizeof(inverse->fMat));
484 484
485 inverse->fMat[3][0] = -fMat[3][0] / fMat[0][0]; 485 double invXScale = 1 / fMat[0][0];
486 inverse->fMat[3][1] = -fMat[3][1] / fMat[1][1]; 486 double invYScale = 1 / fMat[1][1];
487 inverse->fMat[3][2] = -fMat[3][2] / fMat[2][2]; 487 double invZScale = 1 / fMat[2][2];
488 488
489 inverse->fMat[0][0] = 1 / fMat[0][0]; 489 inverse->fMat[3][0] = -fMat[3][0] * invXScale;
490 inverse->fMat[1][1] = 1 / fMat[1][1]; 490 inverse->fMat[3][1] = -fMat[3][1] * invYScale;
491 inverse->fMat[2][2] = 1 / fMat[2][2]; 491 inverse->fMat[3][2] = -fMat[3][2] * invZScale;
492
493 inverse->fMat[0][0] = invXScale;
494 inverse->fMat[1][1] = invYScale;
495 inverse->fMat[2][2] = invZScale;
492 inverse->fMat[3][3] = 1; 496 inverse->fMat[3][3] = 1;
493 497
494 inverse->setTypeMask(this->getType()); 498 inverse->setTypeMask(this->getType());
495 } 499 }
496 return true; 500 return true;
497 } 501 }
498 502
499 double a00 = fMat[0][0]; 503 double a00 = fMat[0][0];
500 double a01 = fMat[0][1]; 504 double a01 = fMat[0][1];
501 double a02 = fMat[0][2]; 505 double a02 = fMat[0][2];
502 double a03 = fMat[0][3]; 506 double a03 = fMat[0][3];
503 double a10 = fMat[1][0]; 507 double a10 = fMat[1][0];
504 double a11 = fMat[1][1]; 508 double a11 = fMat[1][1];
505 double a12 = fMat[1][2]; 509 double a12 = fMat[1][2];
506 double a13 = fMat[1][3]; 510 double a13 = fMat[1][3];
507 double a20 = fMat[2][0]; 511 double a20 = fMat[2][0];
508 double a21 = fMat[2][1]; 512 double a21 = fMat[2][1];
509 double a22 = fMat[2][2]; 513 double a22 = fMat[2][2];
510 double a23 = fMat[2][3]; 514 double a23 = fMat[2][3];
511 double a30 = fMat[3][0]; 515 double a30 = fMat[3][0];
512 double a31 = fMat[3][1]; 516 double a31 = fMat[3][1];
513 double a32 = fMat[3][2]; 517 double a32 = fMat[3][2];
514 double a33 = fMat[3][3]; 518 double a33 = fMat[3][3];
515 519
520 if (!(this->getType() & kPerspective_Mask)) {
521 // If we know the matrix has no perspective, then the perspective
522 // component is (0, 0, 0, 1). We can use this information to save a lot
523 // of arithmetic that would otherwise be spent to compute the inverse
524 // of a general matrix.
525
526 SkASSERT(a03 == 0);
527 SkASSERT(a13 == 0);
528 SkASSERT(a23 == 0);
529 SkASSERT(a33 == 1);
530
531 double b00 = a00 * a11 - a01 * a10;
532 double b01 = a00 * a12 - a02 * a10;
533 double b03 = a01 * a12 - a02 * a11;
534 double b06 = a20 * a31 - a21 * a30;
535 double b07 = a20 * a32 - a22 * a30;
536 double b08 = a20;
537 double b09 = a21 * a32 - a22 * a31;
538 double b10 = a21;
539 double b11 = a22;
540
541 // Calculate the determinant
542 double det = b00 * b11 - b01 * b10 + b03 * b08;
543
544 double invdet = 1.0 / det;
545 // If det is zero, we want to return false. However, we also want to ret urn false
546 // if 1/det overflows to infinity (i.e. det is denormalized). Both of th ese are
547 // handled by checking that 1/det is finite.
548 if (!sk_float_isfinite(invdet)) {
549 return false;
550 }
551 if (NULL == inverse) {
552 return true;
553 }
554
555 b00 *= invdet;
556 b01 *= invdet;
557 b03 *= invdet;
558 b06 *= invdet;
559 b07 *= invdet;
560 b08 *= invdet;
561 b09 *= invdet;
562 b10 *= invdet;
563 b11 *= invdet;
564
565 inverse->fMat[0][0] = SkDoubleToMScalar(a11 * b11 - a12 * b10);
566 inverse->fMat[0][1] = SkDoubleToMScalar(a02 * b10 - a01 * b11);
567 inverse->fMat[0][2] = SkDoubleToMScalar(b03);
568 inverse->fMat[0][3] = 0;
569 inverse->fMat[1][0] = SkDoubleToMScalar(a12 * b08 - a10 * b11);
570 inverse->fMat[1][1] = SkDoubleToMScalar(a00 * b11 - a02 * b08);
571 inverse->fMat[1][2] = SkDoubleToMScalar(-b01);
572 inverse->fMat[1][3] = 0;
573 inverse->fMat[2][0] = SkDoubleToMScalar(a10 * b10 - a11 * b08);
574 inverse->fMat[2][1] = SkDoubleToMScalar(a01 * b08 - a00 * b10);
575 inverse->fMat[2][2] = SkDoubleToMScalar(b00);
576 inverse->fMat[2][3] = 0;
577 inverse->fMat[3][0] = SkDoubleToMScalar(a11 * b07 - a10 * b09 - a12 * b0 6);
578 inverse->fMat[3][1] = SkDoubleToMScalar(a00 * b09 - a01 * b07 + a02 * b0 6);
579 inverse->fMat[3][2] = SkDoubleToMScalar(a31 * b01 - a30 * b03 - a32 * b0 0);
580 inverse->fMat[3][3] = 1;
581
582 inverse->setTypeMask(this->getType());
583 return true;
584 }
585
516 double b00 = a00 * a11 - a01 * a10; 586 double b00 = a00 * a11 - a01 * a10;
517 double b01 = a00 * a12 - a02 * a10; 587 double b01 = a00 * a12 - a02 * a10;
518 double b02 = a00 * a13 - a03 * a10; 588 double b02 = a00 * a13 - a03 * a10;
519 double b03 = a01 * a12 - a02 * a11; 589 double b03 = a01 * a12 - a02 * a11;
520 double b04 = a01 * a13 - a03 * a11; 590 double b04 = a01 * a13 - a03 * a11;
521 double b05 = a02 * a13 - a03 * a12; 591 double b05 = a02 * a13 - a03 * a12;
522 double b06 = a20 * a31 - a21 * a30; 592 double b06 = a20 * a31 - a21 * a30;
523 double b07 = a20 * a32 - a22 * a30; 593 double b07 = a20 * a32 - a22 * a30;
524 double b08 = a20 * a33 - a23 * a30; 594 double b08 = a20 * a33 - a23 * a30;
525 double b09 = a21 * a32 - a22 * a31; 595 double b09 = a21 * a32 - a22 * a31;
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 dst[SkMatrix::kMScaleX] = SkMScalarToScalar(fMat[0][0]); 917 dst[SkMatrix::kMScaleX] = SkMScalarToScalar(fMat[0][0]);
848 dst[SkMatrix::kMSkewX] = SkMScalarToScalar(fMat[1][0]); 918 dst[SkMatrix::kMSkewX] = SkMScalarToScalar(fMat[1][0]);
849 dst[SkMatrix::kMTransX] = SkMScalarToScalar(fMat[3][0]); 919 dst[SkMatrix::kMTransX] = SkMScalarToScalar(fMat[3][0]);
850 920
851 dst[SkMatrix::kMSkewY] = SkMScalarToScalar(fMat[0][1]); 921 dst[SkMatrix::kMSkewY] = SkMScalarToScalar(fMat[0][1]);
852 dst[SkMatrix::kMScaleY] = SkMScalarToScalar(fMat[1][1]); 922 dst[SkMatrix::kMScaleY] = SkMScalarToScalar(fMat[1][1]);
853 dst[SkMatrix::kMTransY] = SkMScalarToScalar(fMat[3][1]); 923 dst[SkMatrix::kMTransY] = SkMScalarToScalar(fMat[3][1]);
854 924
855 return dst; 925 return dst;
856 } 926 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698