Bug 1272549 - Part 6: Use enum class for shear in decomposition functions. r=birtles
authorBoris Chiou <boris.chiou@gmail.com>
Wed, 12 Oct 2016 12:36:58 +0800
changeset 319298 be30e778b634a57d93bf49979a60d1bdd8ac8c83
parent 319297 92a5d32ed2da14b0b00826139827f3c605f44b66
child 319299 00e8df3987f90a27f85232eb38cdfe920ff7dad9
push id30869
push userphilringnalda@gmail.com
push dateWed, 26 Oct 2016 04:57:48 +0000
treeherdermozilla-central@9471b3c49b2c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbirtles
bugs1272549
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1272549 - Part 6: Use enum class for shear in decomposition functions. r=birtles MozReview-Commit-ID: 4exovhbjHI3
layout/style/StyleAnimationValue.cpp
layout/style/nsStyleTransformMatrix.cpp
layout/style/nsStyleTransformMatrix.h
--- a/layout/style/StyleAnimationValue.cpp
+++ b/layout/style/StyleAnimationValue.cpp
@@ -34,16 +34,17 @@
 #include "nsIFrame.h"
 #include "gfx2DGlue.h"
 
 using namespace mozilla;
 using namespace mozilla::css;
 using namespace mozilla::gfx;
 using nsStyleTransformMatrix::Decompose2DMatrix;
 using nsStyleTransformMatrix::Decompose3DMatrix;
+using nsStyleTransformMatrix::ShearType;
 
 // HELPER METHODS
 // --------------
 /*
  * Given two units, this method returns a common unit that they can both be
  * converted into, if possible.  This is intended to facilitate
  * interpolation, distance-computation, and addition between "similar" units.
  *
@@ -1604,26 +1605,31 @@ T InterpolateNumerically(const T& aOne, 
 /* static */ Matrix4x4
 StyleAnimationValue::InterpolateTransformMatrix(const Matrix4x4 &aMatrix1,
                                                 const Matrix4x4 &aMatrix2,
                                                 double aProgress)
 {
   // Decompose both matrices
 
   // TODO: What do we do if one of these returns false (singular matrix)
-
   Point3D scale1(1, 1, 1), translate1;
   Point4D perspective1(0, 0, 0, 1);
   gfxQuaternion rotate1;
-  float shear1[3] = { 0.0f, 0.0f, 0.0f};
+  nsStyleTransformMatrix::ShearArray shear1;
+  for (auto&& s : shear1) {
+    s = 0.0f;
+  }
 
   Point3D scale2(1, 1, 1), translate2;
   Point4D perspective2(0, 0, 0, 1);
   gfxQuaternion rotate2;
-  float shear2[3] = { 0.0f, 0.0f, 0.0f};
+  nsStyleTransformMatrix::ShearArray shear2;
+  for (auto&& s : shear2) {
+    s = 0.0f;
+  }
 
   Matrix matrix2d1, matrix2d2;
   if (aMatrix1.Is2D(&matrix2d1) && aMatrix2.Is2D(&matrix2d2)) {
     Decompose2DMatrix(matrix2d1, scale1, shear1, rotate1, translate1);
     Decompose2DMatrix(matrix2d2, scale2, shear2, rotate2, translate2);
   } else {
     Decompose3DMatrix(aMatrix1, scale1, shear1,
                       rotate1, translate1, perspective1);
@@ -1646,29 +1652,35 @@ StyleAnimationValue::InterpolateTransfor
   Matrix4x4 rotate = q3.ToMatrix();
   if (!rotate.IsIdentity()) {
       result = rotate * result;
   }
 
   // TODO: Would it be better to interpolate these as angles?
   //       How do we convert back to angles?
   float yzshear =
-    InterpolateNumerically(shear1[YZSHEAR], shear2[YZSHEAR], aProgress);
+    InterpolateNumerically(shear1[ShearType::YZSHEAR],
+                           shear2[ShearType::YZSHEAR],
+                           aProgress);
   if (yzshear != 0.0) {
     result.SkewYZ(yzshear);
   }
 
   float xzshear =
-    InterpolateNumerically(shear1[XZSHEAR], shear2[XZSHEAR], aProgress);
+    InterpolateNumerically(shear1[ShearType::XZSHEAR],
+                           shear2[ShearType::XZSHEAR],
+                           aProgress);
   if (xzshear != 0.0) {
     result.SkewXZ(xzshear);
   }
 
   float xyshear =
-    InterpolateNumerically(shear1[XYSHEAR], shear2[XYSHEAR], aProgress);
+    InterpolateNumerically(shear1[ShearType::XYSHEAR],
+                           shear2[ShearType::XYSHEAR],
+                           aProgress);
   if (xyshear != 0.0) {
     result.SkewXY(xyshear);
   }
 
   Point3D scale =
     InterpolateNumerically(scale1, scale2, aProgress);
   if (scale != Point3D(1.0, 1.0, 1.0)) {
     result.PreScale(scale.x, scale.y, scale.z);
--- a/layout/style/nsStyleTransformMatrix.cpp
+++ b/layout/style/nsStyleTransformMatrix.cpp
@@ -871,17 +871,17 @@ ReadTransforms(const nsCSSValueList* aLi
 
 /*
  * Decompose2DMatrix implements the above decomposition algorithm.
  */
 
 bool
 Decompose2DMatrix(const Matrix& aMatrix,
                   Point3D& aScale,
-                  float aShear[3],
+                  ShearArray& aShear,
                   gfxQuaternion& aRotate,
                   Point3D& aTranslate)
 {
   float A = aMatrix._11,
         B = aMatrix._12,
         C = aMatrix._21,
         D = aMatrix._22;
   if (A * D == B * C) {
@@ -911,17 +911,17 @@ Decompose2DMatrix(const Matrix& aMatrix,
     C = -C;
     D = -D;
     XYshear = -XYshear;
     scaleX = -scaleX;
   }
 
   float rotate = atan2f(B, A);
   aRotate = gfxQuaternion(0, 0, sin(rotate/2), cos(rotate/2));
-  aShear[XYSHEAR] = XYshear;
+  aShear[ShearType::XYSHEAR] = XYshear;
   aScale.x = scaleX;
   aScale.y = scaleY;
   aTranslate.x = aMatrix._31;
   aTranslate.y = aMatrix._32;
   return true;
 }
 
 /**
@@ -932,17 +932,17 @@ Decompose2DMatrix(const Matrix& aMatrix,
  * This, in turn, refers to the unmatrix program in Graphics Gems,
  * available from http://tog.acm.org/resources/GraphicsGems/ , and in
  * particular as the file GraphicsGems/gemsii/unmatrix.c
  * in http://tog.acm.org/resources/GraphicsGems/AllGems.tar.gz
  */
 bool
 Decompose3DMatrix(const Matrix4x4& aMatrix,
                   Point3D& aScale,
-                  float aShear[3],
+                  ShearArray& aShear,
                   gfxQuaternion& aRotate,
                   Point3D& aTranslate,
                   Point4D& aPerspective)
 {
   Matrix4x4 local = aMatrix;
 
   if (local[3][3] == 0) {
     return false;
@@ -989,36 +989,36 @@ Decompose3DMatrix(const Matrix4x4& aMatr
 
   /* Now get scale and shear. */
 
   /* Compute X scale factor and normalize first row. */
   aScale.x = local[0].Length();
   local[0] /= aScale.x;
 
   /* Compute XY shear factor and make 2nd local orthogonal to 1st. */
-  aShear[XYSHEAR] = local[0].DotProduct(local[1]);
-  local[1] -= local[0] * aShear[XYSHEAR];
+  aShear[ShearType::XYSHEAR] = local[0].DotProduct(local[1]);
+  local[1] -= local[0] * aShear[ShearType::XYSHEAR];
 
   /* Now, compute Y scale and normalize 2nd local. */
   aScale.y = local[1].Length();
   local[1] /= aScale.y;
-  aShear[XYSHEAR] /= aScale.y;
+  aShear[ShearType::XYSHEAR] /= aScale.y;
 
   /* Compute XZ and YZ shears, make 3rd local orthogonal */
-  aShear[XZSHEAR] = local[0].DotProduct(local[2]);
-  local[2] -= local[0] * aShear[XZSHEAR];
-  aShear[YZSHEAR] = local[1].DotProduct(local[2]);
-  local[2] -= local[1] * aShear[YZSHEAR];
+  aShear[ShearType::XZSHEAR] = local[0].DotProduct(local[2]);
+  local[2] -= local[0] * aShear[ShearType::XZSHEAR];
+  aShear[ShearType::YZSHEAR] = local[1].DotProduct(local[2]);
+  local[2] -= local[1] * aShear[ShearType::YZSHEAR];
 
   /* Next, get Z scale and normalize 3rd local. */
   aScale.z = local[2].Length();
   local[2] /= aScale.z;
 
-  aShear[XZSHEAR] /= aScale.z;
-  aShear[YZSHEAR] /= aScale.z;
+  aShear[ShearType::XZSHEAR] /= aScale.z;
+  aShear[ShearType::YZSHEAR] /= aScale.z;
 
   /**
    * At this point, the matrix (in locals) is orthonormal.
    * Check for a coordinate system flip.  If the determinant
    * is -1, then negate the matrix and the scaling factors.
    */
   if (local[0].DotProduct(local[1].CrossProduct(local[2])) < 0) {
     aScale *= -1;
--- a/layout/style/nsStyleTransformMatrix.h
+++ b/layout/style/nsStyleTransformMatrix.h
@@ -5,16 +5,17 @@
 
 /*
  * A class representing three matrices that can be used for style transforms.
  */
 
 #ifndef nsStyleTransformMatrix_h_
 #define nsStyleTransformMatrix_h_
 
+#include "mozilla/EnumeratedArray.h"
 #include "nsCSSValue.h"
 
 class nsIFrame;
 class nsStyleContext;
 class nsPresContext;
 struct gfxQuaternion;
 struct nsRect;
 namespace mozilla {
@@ -169,32 +170,37 @@ namespace nsStyleTransformMatrix {
                                          nsStyleContext* aContext,
                                          nsPresContext* aPresContext,
                                          mozilla::RuleNodeCacheConditions& aConditions,
                                          TransformReferenceBox& aBounds,
                                          float aAppUnitsPerMatrixUnit,
                                          bool* aContains3dTransform);
 
   // Shear type for decomposition.
-  #define XYSHEAR 0
-  #define XZSHEAR 1
-  #define YZSHEAR 2
+  enum class ShearType {
+    XYSHEAR,
+    XZSHEAR,
+    YZSHEAR,
+    Count
+  };
+  using ShearArray =
+    mozilla::EnumeratedArray<ShearType, ShearType::Count, float>;
 
   /*
    * Implements the 2d transform matrix decomposition algorithm.
    */
   bool Decompose2DMatrix(const mozilla::gfx::Matrix& aMatrix,
                          mozilla::gfx::Point3D& aScale,
-                         float aShear[3],
+                         ShearArray& aShear,
                          gfxQuaternion& aRotate,
                          mozilla::gfx::Point3D& aTranslate);
   /*
    * Implements the 3d transform matrix decomposition algorithm.
    */
   bool Decompose3DMatrix(const mozilla::gfx::Matrix4x4& aMatrix,
                          mozilla::gfx::Point3D& aScale,
-                         float aShear[3],
+                         ShearArray& aShear,
                          gfxQuaternion& aRotate,
                          mozilla::gfx::Point3D& aTranslate,
                          mozilla::gfx::Point4D& aPerspective);
 } // namespace nsStyleTransformMatrix
 
 #endif