1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /*
8  * A class used for intermediate representations of the -moz-transform property.
9  */
10 
11 #include "nsStyleTransformMatrix.h"
12 #include "nsCSSValue.h"
13 #include "nsLayoutUtils.h"
14 #include "nsPresContext.h"
15 #ifdef MOZ_OLD_STYLE
16 #include "nsRuleNode.h"
17 #endif
18 #include "nsSVGUtils.h"
19 #include "nsCSSKeywords.h"
20 #include "mozilla/RuleNodeCacheConditions.h"
21 #include "mozilla/ServoBindings.h"
22 #include "mozilla/StyleAnimationValue.h"
23 #include "gfxMatrix.h"
24 #include "gfxQuaternion.h"
25 
26 using namespace mozilla;
27 using namespace mozilla::gfx;
28 
29 namespace nsStyleTransformMatrix {
30 
31 /* Note on floating point precision: The transform matrix is an array
32  * of single precision 'float's, and so are most of the input values
33  * we get from the style system, but intermediate calculations
34  * involving angles need to be done in 'double'.
35  */
36 
37 // Define UNIFIED_CONTINUATIONS here and in nsDisplayList.cpp
38 // to have the transform property try
39 // to transform content with continuations as one unified block instead of
40 // several smaller ones.  This is currently disabled because it doesn't work
41 // correctly, since when the frames are initially being reflowed, their
42 // continuations all compute their bounding rects independently of each other
43 // and consequently get the wrong value.
44 //#define UNIFIED_CONTINUATIONS
45 
EnsureDimensionsAreCached()46 void TransformReferenceBox::EnsureDimensionsAreCached() {
47   if (mIsCached) {
48     return;
49   }
50 
51   MOZ_ASSERT(mFrame);
52 
53   mIsCached = true;
54 
55   if (mFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT) {
56     if (!nsLayoutUtils::SVGTransformBoxEnabled()) {
57       mX = -mFrame->GetPosition().x;
58       mY = -mFrame->GetPosition().y;
59       Size contextSize = nsSVGUtils::GetContextSize(mFrame);
60       mWidth = nsPresContext::CSSPixelsToAppUnits(contextSize.width);
61       mHeight = nsPresContext::CSSPixelsToAppUnits(contextSize.height);
62     } else if (mFrame->StyleDisplay()->mTransformBox ==
63                StyleGeometryBox::FillBox) {
64       // Percentages in transforms resolve against the SVG bbox, and the
65       // transform is relative to the top-left of the SVG bbox.
66       nsRect bboxInAppUnits = nsLayoutUtils::ComputeGeometryBox(
67           const_cast<nsIFrame*>(mFrame), StyleGeometryBox::FillBox);
68       // The mRect of an SVG nsIFrame is its user space bounds *including*
69       // stroke and markers, whereas bboxInAppUnits is its user space bounds
70       // including fill only.  We need to note the offset of the reference box
71       // from the frame's mRect in mX/mY.
72       mX = bboxInAppUnits.x - mFrame->GetPosition().x;
73       mY = bboxInAppUnits.y - mFrame->GetPosition().y;
74       mWidth = bboxInAppUnits.width;
75       mHeight = bboxInAppUnits.height;
76     } else {
77       // The value 'border-box' is treated as 'view-box' for SVG content.
78       MOZ_ASSERT(
79           mFrame->StyleDisplay()->mTransformBox == StyleGeometryBox::ViewBox ||
80               mFrame->StyleDisplay()->mTransformBox ==
81                   StyleGeometryBox::BorderBox,
82           "Unexpected value for 'transform-box'");
83       // Percentages in transforms resolve against the width/height of the
84       // nearest viewport (or its viewBox if one is applied), and the
85       // transform is relative to {0,0} in current user space.
86       mX = -mFrame->GetPosition().x;
87       mY = -mFrame->GetPosition().y;
88       Size contextSize = nsSVGUtils::GetContextSize(mFrame);
89       mWidth = nsPresContext::CSSPixelsToAppUnits(contextSize.width);
90       mHeight = nsPresContext::CSSPixelsToAppUnits(contextSize.height);
91     }
92     return;
93   }
94 
95   // If UNIFIED_CONTINUATIONS is not defined, this is simply the frame's
96   // bounding rectangle, translated to the origin.  Otherwise, it is the
97   // smallest rectangle containing a frame and all of its continuations.  For
98   // example, if there is a <span> element with several continuations split
99   // over several lines, this function will return the rectangle containing all
100   // of those continuations.
101 
102   nsRect rect;
103 
104 #ifndef UNIFIED_CONTINUATIONS
105   rect = mFrame->GetRect();
106 #else
107   // Iterate the continuation list, unioning together the bounding rects:
108   for (const nsIFrame* currFrame = mFrame->FirstContinuation();
109        currFrame != nullptr; currFrame = currFrame->GetNextContinuation()) {
110     // Get the frame rect in local coordinates, then translate back to the
111     // original coordinates:
112     rect.UnionRect(
113         result, nsRect(currFrame->GetOffsetTo(mFrame), currFrame->GetSize()));
114   }
115 #endif
116 
117   mX = 0;
118   mY = 0;
119   mWidth = rect.Width();
120   mHeight = rect.Height();
121 }
122 
Init(const nsSize & aDimensions)123 void TransformReferenceBox::Init(const nsSize& aDimensions) {
124   MOZ_ASSERT(!mFrame && !mIsCached);
125 
126   mX = 0;
127   mY = 0;
128   mWidth = aDimensions.width;
129   mHeight = aDimensions.height;
130   mIsCached = true;
131 }
132 
ProcessTranslatePart(const nsCSSValue & aValue,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox * aRefBox,TransformReferenceBox::DimensionGetter aDimensionGetter)133 float ProcessTranslatePart(
134     const nsCSSValue& aValue, GeckoStyleContext* aContext,
135     nsPresContext* aPresContext, RuleNodeCacheConditions& aConditions,
136     TransformReferenceBox* aRefBox,
137     TransformReferenceBox::DimensionGetter aDimensionGetter) {
138   nscoord offset = 0;
139   float percent = 0.0f;
140 
141   if (aValue.GetUnit() == eCSSUnit_Percent) {
142     percent = aValue.GetPercentValue();
143   } else if (aValue.GetUnit() == eCSSUnit_Pixel ||
144              aValue.GetUnit() == eCSSUnit_Number) {
145     // Handle this here (even though nsRuleNode::CalcLength handles it
146     // fine) so that callers are allowed to pass a null style context
147     // and pres context to SetToTransformFunction if they know (as
148     // StyleAnimationValue does) that all lengths within the transform
149     // function have already been computed to pixels and percents.
150     //
151     // Raw numbers are treated as being pixels.
152     //
153     // Don't convert to aValue to AppUnits here to avoid precision issues.
154     return aValue.GetFloatValue();
155   } else if (aValue.IsCalcUnit()) {
156     if (aContext) {
157 #ifdef MOZ_OLD_STYLE
158       // Gecko backend
159       nsRuleNode::ComputedCalc result = nsRuleNode::SpecifiedCalcToComputedCalc(
160           aValue, aContext, aPresContext, aConditions);
161       percent = result.mPercent;
162       offset = result.mLength;
163 #else
164       MOZ_CRASH("old style system disabled");
165 #endif
166     } else {
167       // Servo backend. We can retrieve the Calc value directly because it has
168       // been computed from Servo side and set by nsCSSValue::SetCalcValue().
169       // We don't use nsRuleNode::SpecifiedCalcToComputedCalc() because it
170       // asserts for null context and we always pass null context for Servo
171       // backend.
172       nsStyleCoord::CalcValue calc = aValue.GetCalcValue();
173       percent = calc.mPercent;
174       offset = calc.mLength;
175     }
176   } else {
177   // Note: The unit of nsCSSValue passed from Servo side would be number,
178   //       pixel, percent, or eCSSUnit_Calc, so it is impossible to go into
179   //       this branch.
180 #ifdef MOZ_OLD_STYLE
181     MOZ_ASSERT(aContext, "We need a valid context to compute the length");
182     offset =
183         nsRuleNode::CalcLength(aValue, aContext, aPresContext, aConditions);
184 #else
185     MOZ_CRASH("unexpected unit in ProcessTranslatePart");
186 #endif
187   }
188 
189   float translation =
190       NSAppUnitsToFloatPixels(offset, nsPresContext::AppUnitsPerCSSPixel());
191   // We want to avoid calling aDimensionGetter if there's no percentage to be
192   // resolved (for performance reasons - see TransformReferenceBox).
193   if (percent != 0.0f && aRefBox && !aRefBox->IsEmpty()) {
194     translation +=
195         percent * NSAppUnitsToFloatPixels((aRefBox->*aDimensionGetter)(),
196                                           nsPresContext::AppUnitsPerCSSPixel());
197   }
198   return translation;
199 }
200 
201 /**
202  * Helper functions to process all the transformation function types.
203  *
204  * These take a matrix parameter to accumulate the current matrix.
205  */
206 
207 /* Helper function to process a matrix entry. */
ProcessMatrix(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox)208 static void ProcessMatrix(Matrix4x4& aMatrix, const nsCSSValue::Array* aData,
209                           GeckoStyleContext* aContext,
210                           nsPresContext* aPresContext,
211                           RuleNodeCacheConditions& aConditions,
212                           TransformReferenceBox& aRefBox) {
213   NS_PRECONDITION(aData->Count() == 7, "Invalid array!");
214 
215   gfxMatrix result;
216 
217   /* Take the first four elements out of the array as floats and store
218    * them.
219    */
220   result._11 = aData->Item(1).GetFloatValue();
221   result._12 = aData->Item(2).GetFloatValue();
222   result._21 = aData->Item(3).GetFloatValue();
223   result._22 = aData->Item(4).GetFloatValue();
224 
225   /* The last two elements have their length parts stored in aDelta
226    * and their percent parts stored in aX[0] and aY[1].
227    */
228   result._31 =
229       ProcessTranslatePart(aData->Item(5), aContext, aPresContext, aConditions,
230                            &aRefBox, &TransformReferenceBox::Width);
231   result._32 =
232       ProcessTranslatePart(aData->Item(6), aContext, aPresContext, aConditions,
233                            &aRefBox, &TransformReferenceBox::Height);
234 
235   aMatrix = result * aMatrix;
236 }
237 
ProcessMatrix3D(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox)238 static void ProcessMatrix3D(Matrix4x4& aMatrix, const nsCSSValue::Array* aData,
239                             GeckoStyleContext* aContext,
240                             nsPresContext* aPresContext,
241                             RuleNodeCacheConditions& aConditions,
242                             TransformReferenceBox& aRefBox) {
243   NS_PRECONDITION(aData->Count() == 17, "Invalid array!");
244 
245   Matrix4x4 temp;
246 
247   temp._11 = aData->Item(1).GetFloatValue();
248   temp._12 = aData->Item(2).GetFloatValue();
249   temp._13 = aData->Item(3).GetFloatValue();
250   temp._14 = aData->Item(4).GetFloatValue();
251   temp._21 = aData->Item(5).GetFloatValue();
252   temp._22 = aData->Item(6).GetFloatValue();
253   temp._23 = aData->Item(7).GetFloatValue();
254   temp._24 = aData->Item(8).GetFloatValue();
255   temp._31 = aData->Item(9).GetFloatValue();
256   temp._32 = aData->Item(10).GetFloatValue();
257   temp._33 = aData->Item(11).GetFloatValue();
258   temp._34 = aData->Item(12).GetFloatValue();
259   temp._44 = aData->Item(16).GetFloatValue();
260 
261   temp._41 =
262       ProcessTranslatePart(aData->Item(13), aContext, aPresContext, aConditions,
263                            &aRefBox, &TransformReferenceBox::Width);
264   temp._42 =
265       ProcessTranslatePart(aData->Item(14), aContext, aPresContext, aConditions,
266                            &aRefBox, &TransformReferenceBox::Height);
267   temp._43 = ProcessTranslatePart(aData->Item(15), aContext, aPresContext,
268                                   aConditions, nullptr);
269 
270   aMatrix = temp * aMatrix;
271 }
272 
273 // For accumulation for transform functions, |aOne| corresponds to |aB| and
274 // |aTwo| corresponds to |aA| for StyleAnimationValue::Accumulate().
275 class Accumulate {
276  public:
277   template <typename T>
operate(const T & aOne,const T & aTwo,double aCoeff)278   static T operate(const T& aOne, const T& aTwo, double aCoeff) {
279     return aOne + aTwo * aCoeff;
280   }
281 
operateForPerspective(const Point4D & aOne,const Point4D & aTwo,double aCoeff)282   static Point4D operateForPerspective(const Point4D& aOne, const Point4D& aTwo,
283                                        double aCoeff) {
284     return (aOne - Point4D(0, 0, 0, 1)) +
285            (aTwo - Point4D(0, 0, 0, 1)) * aCoeff + Point4D(0, 0, 0, 1);
286   }
operateForScale(const Point3D & aOne,const Point3D & aTwo,double aCoeff)287   static Point3D operateForScale(const Point3D& aOne, const Point3D& aTwo,
288                                  double aCoeff) {
289     // For scale, the identify element is 1, see AddTransformScale in
290     // StyleAnimationValue.cpp.
291     return (aOne - Point3D(1, 1, 1)) + (aTwo - Point3D(1, 1, 1)) * aCoeff +
292            Point3D(1, 1, 1);
293   }
294 
operateForRotate(const gfxQuaternion & aOne,const gfxQuaternion & aTwo,double aCoeff)295   static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
296                                     const gfxQuaternion& aTwo, double aCoeff) {
297     if (aCoeff == 0.0) {
298       return aOne.ToMatrix();
299     }
300 
301     double theta = acos(mozilla::clamped(aTwo.w, -1.0, 1.0));
302     double scale = (theta != 0.0) ? 1.0 / sin(theta) : 0.0;
303     theta *= aCoeff;
304     scale *= sin(theta);
305 
306     gfxQuaternion result = gfxQuaternion(scale * aTwo.x, scale * aTwo.y,
307                                          scale * aTwo.z, cos(theta)) *
308                            aOne;
309     return result.ToMatrix();
310   }
311 
operateForFallback(const Matrix4x4 & aMatrix1,const Matrix4x4 & aMatrix2,double aProgress)312   static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
313                                       const Matrix4x4& aMatrix2,
314                                       double aProgress) {
315     return aMatrix1;
316   }
317 
operateByServo(const Matrix4x4 & aMatrix1,const Matrix4x4 & aMatrix2,double aCount)318   static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
319                                   const Matrix4x4& aMatrix2, double aCount) {
320     Matrix4x4 result;
321     Servo_MatrixTransform_Operate(MatrixTransformOperator::Accumulate,
322                                   &aMatrix1.components, &aMatrix2.components,
323                                   aCount, &result.components);
324     return result;
325   }
326 };
327 
328 class Interpolate {
329  public:
330   template <typename T>
operate(const T & aOne,const T & aTwo,double aCoeff)331   static T operate(const T& aOne, const T& aTwo, double aCoeff) {
332     return aOne + (aTwo - aOne) * aCoeff;
333   }
334 
operateForPerspective(const Point4D & aOne,const Point4D & aTwo,double aCoeff)335   static Point4D operateForPerspective(const Point4D& aOne, const Point4D& aTwo,
336                                        double aCoeff) {
337     return aOne + (aTwo - aOne) * aCoeff;
338   }
339 
operateForScale(const Point3D & aOne,const Point3D & aTwo,double aCoeff)340   static Point3D operateForScale(const Point3D& aOne, const Point3D& aTwo,
341                                  double aCoeff) {
342     return aOne + (aTwo - aOne) * aCoeff;
343   }
344 
operateForRotate(const gfxQuaternion & aOne,const gfxQuaternion & aTwo,double aCoeff)345   static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
346                                     const gfxQuaternion& aTwo, double aCoeff) {
347     return aOne.Slerp(aTwo, aCoeff).ToMatrix();
348   }
349 
operateForFallback(const Matrix4x4 & aMatrix1,const Matrix4x4 & aMatrix2,double aProgress)350   static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
351                                       const Matrix4x4& aMatrix2,
352                                       double aProgress) {
353     return aProgress < 0.5 ? aMatrix1 : aMatrix2;
354   }
355 
operateByServo(const Matrix4x4 & aMatrix1,const Matrix4x4 & aMatrix2,double aProgress)356   static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
357                                   const Matrix4x4& aMatrix2, double aProgress) {
358     Matrix4x4 result;
359     Servo_MatrixTransform_Operate(MatrixTransformOperator::Interpolate,
360                                   &aMatrix1.components, &aMatrix2.components,
361                                   aProgress, &result.components);
362     return result;
363   }
364 };
365 
366 /**
367  * Calculate 2 matrices by decomposing them with Operator.
368  *
369  * @param aMatrix1   First matrix, using CSS pixel units.
370  * @param aMatrix2   Second matrix, using CSS pixel units.
371  * @param aProgress  Coefficient for the Operator.
372  */
373 template <typename Operator>
OperateTransformMatrix(const Matrix4x4 & aMatrix1,const Matrix4x4 & aMatrix2,double aProgress)374 static Matrix4x4 OperateTransformMatrix(const Matrix4x4& aMatrix1,
375                                         const Matrix4x4& aMatrix2,
376                                         double aProgress) {
377   // Decompose both matrices
378 
379   Point3D scale1(1, 1, 1), translate1;
380   Point4D perspective1(0, 0, 0, 1);
381   gfxQuaternion rotate1;
382   nsStyleTransformMatrix::ShearArray shear1{0.0f, 0.0f, 0.0f};
383 
384   Point3D scale2(1, 1, 1), translate2;
385   Point4D perspective2(0, 0, 0, 1);
386   gfxQuaternion rotate2;
387   nsStyleTransformMatrix::ShearArray shear2{0.0f, 0.0f, 0.0f};
388 
389   // Check if both matrices are decomposable.
390   bool wasDecomposed;
391   Matrix matrix2d1, matrix2d2;
392   if (aMatrix1.Is2D(&matrix2d1) && aMatrix2.Is2D(&matrix2d2)) {
393     wasDecomposed =
394         Decompose2DMatrix(matrix2d1, scale1, shear1, rotate1, translate1) &&
395         Decompose2DMatrix(matrix2d2, scale2, shear2, rotate2, translate2);
396   } else {
397     wasDecomposed = Decompose3DMatrix(aMatrix1, scale1, shear1, rotate1,
398                                       translate1, perspective1) &&
399                     Decompose3DMatrix(aMatrix2, scale2, shear2, rotate2,
400                                       translate2, perspective2);
401   }
402 
403   // Fallback to discrete operation if one of the matrices is not decomposable.
404   if (!wasDecomposed) {
405     return Operator::operateForFallback(aMatrix1, aMatrix2, aProgress);
406   }
407 
408   Matrix4x4 result;
409 
410   // Operate each of the pieces in response to |Operator|.
411   Point4D perspective =
412       Operator::operateForPerspective(perspective1, perspective2, aProgress);
413   result.SetTransposedVector(3, perspective);
414 
415   Point3D translate = Operator::operate(translate1, translate2, aProgress);
416   result.PreTranslate(translate.x, translate.y, translate.z);
417 
418   Matrix4x4 rotate = Operator::operateForRotate(rotate1, rotate2, aProgress);
419   if (!rotate.IsIdentity()) {
420     result = rotate * result;
421   }
422 
423   // TODO: Would it be better to operate these as angles?
424   //       How do we convert back to angles?
425   float yzshear = Operator::operate(shear1[ShearType::YZSHEAR],
426                                     shear2[ShearType::YZSHEAR], aProgress);
427   if (yzshear != 0.0) {
428     result.SkewYZ(yzshear);
429   }
430 
431   float xzshear = Operator::operate(shear1[ShearType::XZSHEAR],
432                                     shear2[ShearType::XZSHEAR], aProgress);
433   if (xzshear != 0.0) {
434     result.SkewXZ(xzshear);
435   }
436 
437   float xyshear = Operator::operate(shear1[ShearType::XYSHEAR],
438                                     shear2[ShearType::XYSHEAR], aProgress);
439   if (xyshear != 0.0) {
440     result.SkewXY(xyshear);
441   }
442 
443   Point3D scale = Operator::operateForScale(scale1, scale2, aProgress);
444   if (scale != Point3D(1.0, 1.0, 1.0)) {
445     result.PreScale(scale.x, scale.y, scale.z);
446   }
447 
448   return result;
449 }
450 
451 template <typename Operator>
OperateTransformMatrixByServo(const Matrix4x4 & aMatrix1,const Matrix4x4 & aMatrix2,double aProgress)452 static Matrix4x4 OperateTransformMatrixByServo(const Matrix4x4& aMatrix1,
453                                                const Matrix4x4& aMatrix2,
454                                                double aProgress) {
455   return Operator::operateByServo(aMatrix1, aMatrix2, aProgress);
456 }
457 
458 template <typename Operator>
ProcessMatrixOperator(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox,bool * aContains3dTransform)459 static void ProcessMatrixOperator(Matrix4x4& aMatrix,
460                                   const nsCSSValue::Array* aData,
461                                   GeckoStyleContext* aContext,
462                                   nsPresContext* aPresContext,
463                                   RuleNodeCacheConditions& aConditions,
464                                   TransformReferenceBox& aRefBox,
465                                   bool* aContains3dTransform) {
466   NS_PRECONDITION(aData->Count() == 4, "Invalid array!");
467 
468   auto readTransform = [&](const nsCSSValue& aValue) -> Matrix4x4 {
469     const nsCSSValueList* list = nullptr;
470     switch (aValue.GetUnit()) {
471       case eCSSUnit_List:
472         // For Gecko style backend.
473         list = aValue.GetListValue();
474         break;
475       case eCSSUnit_SharedList:
476         // For Servo style backend. The transform lists of interpolatematrix
477         // are not created on the main thread (i.e. during parallel traversal),
478         // and nsCSSValueList_heap is not thread safe. Therefore, we use
479         // nsCSSValueSharedList as a workaround.
480         list = aValue.GetSharedListValue()->mHead;
481         break;
482       default:
483         list = nullptr;
484     }
485 
486     Matrix4x4 matrix;
487     if (!list) {
488       return matrix;
489     }
490 
491     float appUnitPerCSSPixel = nsPresContext::AppUnitsPerCSSPixel();
492     matrix = nsStyleTransformMatrix::ReadTransforms(list,
493 #ifdef MOZ_OLD_STYLE
494                                                     aContext,
495 #else
496                                                     nullptr,
497 #endif
498                                                     aPresContext, aConditions,
499                                                     aRefBox, appUnitPerCSSPixel,
500                                                     aContains3dTransform);
501     return matrix;
502   };
503 
504   Matrix4x4 matrix1 = readTransform(aData->Item(1));
505   Matrix4x4 matrix2 = readTransform(aData->Item(2));
506   double progress = aData->Item(3).GetPercentValue();
507 
508   // We cannot use GeckoStyleContext to check if we use Servo backend because
509   // it could be null in Gecko. Instead, use the unit of the nsCSSValue because
510   // we use eCSSUnit_SharedList for Servo backend.
511   if (aData->Item(1).GetUnit() == eCSSUnit_SharedList) {
512     aMatrix =
513         OperateTransformMatrixByServo<Operator>(matrix1, matrix2, progress) *
514         aMatrix;
515     return;
516   }
517 
518   aMatrix =
519       OperateTransformMatrix<Operator>(matrix1, matrix2, progress) * aMatrix;
520 }
521 
522 /* Helper function to process two matrices that we need to interpolate between
523  */
ProcessInterpolateMatrix(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox,bool * aContains3dTransform)524 void ProcessInterpolateMatrix(Matrix4x4& aMatrix,
525                               const nsCSSValue::Array* aData,
526                               GeckoStyleContext* aContext,
527                               nsPresContext* aPresContext,
528                               RuleNodeCacheConditions& aConditions,
529                               TransformReferenceBox& aRefBox,
530                               bool* aContains3dTransform) {
531   ProcessMatrixOperator<Interpolate>(aMatrix, aData, aContext, aPresContext,
532                                      aConditions, aRefBox,
533                                      aContains3dTransform);
534 }
535 
ProcessAccumulateMatrix(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox,bool * aContains3dTransform)536 void ProcessAccumulateMatrix(Matrix4x4& aMatrix, const nsCSSValue::Array* aData,
537                              GeckoStyleContext* aContext,
538                              nsPresContext* aPresContext,
539                              RuleNodeCacheConditions& aConditions,
540                              TransformReferenceBox& aRefBox,
541                              bool* aContains3dTransform) {
542   ProcessMatrixOperator<Accumulate>(aMatrix, aData, aContext, aPresContext,
543                                     aConditions, aRefBox, aContains3dTransform);
544 }
545 
546 /* Helper function to process a translatex function. */
ProcessTranslateX(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox)547 static void ProcessTranslateX(Matrix4x4& aMatrix,
548                               const nsCSSValue::Array* aData,
549                               GeckoStyleContext* aContext,
550                               nsPresContext* aPresContext,
551                               RuleNodeCacheConditions& aConditions,
552                               TransformReferenceBox& aRefBox) {
553   NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
554 
555   Point3D temp;
556 
557   temp.x =
558       ProcessTranslatePart(aData->Item(1), aContext, aPresContext, aConditions,
559                            &aRefBox, &TransformReferenceBox::Width);
560   aMatrix.PreTranslate(temp);
561 }
562 
563 /* Helper function to process a translatey function. */
ProcessTranslateY(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox)564 static void ProcessTranslateY(Matrix4x4& aMatrix,
565                               const nsCSSValue::Array* aData,
566                               GeckoStyleContext* aContext,
567                               nsPresContext* aPresContext,
568                               RuleNodeCacheConditions& aConditions,
569                               TransformReferenceBox& aRefBox) {
570   NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
571 
572   Point3D temp;
573 
574   temp.y =
575       ProcessTranslatePart(aData->Item(1), aContext, aPresContext, aConditions,
576                            &aRefBox, &TransformReferenceBox::Height);
577   aMatrix.PreTranslate(temp);
578 }
579 
ProcessTranslateZ(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions)580 static void ProcessTranslateZ(Matrix4x4& aMatrix,
581                               const nsCSSValue::Array* aData,
582                               GeckoStyleContext* aContext,
583                               nsPresContext* aPresContext,
584                               RuleNodeCacheConditions& aConditions) {
585   NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
586 
587   Point3D temp;
588 
589   temp.z = ProcessTranslatePart(aData->Item(1), aContext, aPresContext,
590                                 aConditions, nullptr);
591   aMatrix.PreTranslate(temp);
592 }
593 
594 /* Helper function to process a translate function. */
ProcessTranslate(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox)595 static void ProcessTranslate(Matrix4x4& aMatrix, const nsCSSValue::Array* aData,
596                              GeckoStyleContext* aContext,
597                              nsPresContext* aPresContext,
598                              RuleNodeCacheConditions& aConditions,
599                              TransformReferenceBox& aRefBox) {
600   NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Invalid array!");
601 
602   Point3D temp;
603 
604   temp.x =
605       ProcessTranslatePart(aData->Item(1), aContext, aPresContext, aConditions,
606                            &aRefBox, &TransformReferenceBox::Width);
607 
608   /* If we read in a Y component, set it appropriately */
609   if (aData->Count() == 3) {
610     temp.y = ProcessTranslatePart(aData->Item(2), aContext, aPresContext,
611                                   aConditions, &aRefBox,
612                                   &TransformReferenceBox::Height);
613   }
614   aMatrix.PreTranslate(temp);
615 }
616 
ProcessTranslate3D(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox)617 static void ProcessTranslate3D(Matrix4x4& aMatrix,
618                                const nsCSSValue::Array* aData,
619                                GeckoStyleContext* aContext,
620                                nsPresContext* aPresContext,
621                                RuleNodeCacheConditions& aConditions,
622                                TransformReferenceBox& aRefBox) {
623   NS_PRECONDITION(aData->Count() == 4, "Invalid array!");
624 
625   Point3D temp;
626 
627   temp.x =
628       ProcessTranslatePart(aData->Item(1), aContext, aPresContext, aConditions,
629                            &aRefBox, &TransformReferenceBox::Width);
630 
631   temp.y =
632       ProcessTranslatePart(aData->Item(2), aContext, aPresContext, aConditions,
633                            &aRefBox, &TransformReferenceBox::Height);
634 
635   temp.z = ProcessTranslatePart(aData->Item(3), aContext, aPresContext,
636                                 aConditions, nullptr);
637 
638   aMatrix.PreTranslate(temp);
639 }
640 
641 /* Helper function to set up a scale matrix. */
ProcessScaleHelper(Matrix4x4 & aMatrix,float aXScale,float aYScale,float aZScale)642 static void ProcessScaleHelper(Matrix4x4& aMatrix, float aXScale, float aYScale,
643                                float aZScale) {
644   aMatrix.PreScale(aXScale, aYScale, aZScale);
645 }
646 
647 /* Process a scalex function. */
ProcessScaleX(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)648 static void ProcessScaleX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
649   NS_PRECONDITION(aData->Count() == 2, "Bad array!");
650   ProcessScaleHelper(aMatrix, aData->Item(1).GetFloatValue(), 1.0f, 1.0f);
651 }
652 
653 /* Process a scaley function. */
ProcessScaleY(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)654 static void ProcessScaleY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
655   NS_PRECONDITION(aData->Count() == 2, "Bad array!");
656   ProcessScaleHelper(aMatrix, 1.0f, aData->Item(1).GetFloatValue(), 1.0f);
657 }
658 
ProcessScaleZ(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)659 static void ProcessScaleZ(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
660   NS_PRECONDITION(aData->Count() == 2, "Bad array!");
661   ProcessScaleHelper(aMatrix, 1.0f, 1.0f, aData->Item(1).GetFloatValue());
662 }
663 
ProcessScale3D(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)664 static void ProcessScale3D(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
665   NS_PRECONDITION(aData->Count() == 4, "Bad array!");
666   ProcessScaleHelper(aMatrix, aData->Item(1).GetFloatValue(),
667                      aData->Item(2).GetFloatValue(),
668                      aData->Item(3).GetFloatValue());
669 }
670 
671 /* Process a scale function. */
ProcessScale(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)672 static void ProcessScale(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
673   NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
674   /* We either have one element or two.  If we have one, it's for both X and Y.
675    * Otherwise it's one for each.
676    */
677   const nsCSSValue& scaleX = aData->Item(1);
678   const nsCSSValue& scaleY = (aData->Count() == 2 ? scaleX : aData->Item(2));
679 
680   ProcessScaleHelper(aMatrix, scaleX.GetFloatValue(), scaleY.GetFloatValue(),
681                      1.0f);
682 }
683 
684 /* Helper function that, given a set of angles, constructs the appropriate
685  * skew matrix.
686  */
ProcessSkewHelper(Matrix4x4 & aMatrix,double aXAngle,double aYAngle)687 static void ProcessSkewHelper(Matrix4x4& aMatrix, double aXAngle,
688                               double aYAngle) {
689   aMatrix.SkewXY(aXAngle, aYAngle);
690 }
691 
692 /* Function that converts a skewx transform into a matrix. */
ProcessSkewX(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)693 static void ProcessSkewX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
694   NS_ASSERTION(aData->Count() == 2, "Bad array!");
695   ProcessSkewHelper(aMatrix, aData->Item(1).GetAngleValueInRadians(), 0.0);
696 }
697 
698 /* Function that converts a skewy transform into a matrix. */
ProcessSkewY(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)699 static void ProcessSkewY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
700   NS_ASSERTION(aData->Count() == 2, "Bad array!");
701   ProcessSkewHelper(aMatrix, 0.0, aData->Item(1).GetAngleValueInRadians());
702 }
703 
704 /* Function that converts a skew transform into a matrix. */
ProcessSkew(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)705 static void ProcessSkew(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
706   NS_ASSERTION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
707 
708   double xSkew = aData->Item(1).GetAngleValueInRadians();
709   double ySkew =
710       (aData->Count() == 2 ? 0.0 : aData->Item(2).GetAngleValueInRadians());
711 
712   ProcessSkewHelper(aMatrix, xSkew, ySkew);
713 }
714 
715 /* Function that converts a rotate transform into a matrix. */
ProcessRotateZ(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)716 static void ProcessRotateZ(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
717   NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
718   double theta = aData->Item(1).GetAngleValueInRadians();
719   aMatrix.RotateZ(theta);
720 }
721 
ProcessRotateX(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)722 static void ProcessRotateX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
723   NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
724   double theta = aData->Item(1).GetAngleValueInRadians();
725   aMatrix.RotateX(theta);
726 }
727 
ProcessRotateY(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)728 static void ProcessRotateY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData) {
729   NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
730   double theta = aData->Item(1).GetAngleValueInRadians();
731   aMatrix.RotateY(theta);
732 }
733 
ProcessRotate3D(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData)734 static void ProcessRotate3D(Matrix4x4& aMatrix,
735                             const nsCSSValue::Array* aData) {
736   NS_PRECONDITION(aData->Count() == 5, "Invalid array!");
737 
738   double theta = aData->Item(4).GetAngleValueInRadians();
739   float x = aData->Item(1).GetFloatValue();
740   float y = aData->Item(2).GetFloatValue();
741   float z = aData->Item(3).GetFloatValue();
742 
743   Matrix4x4 temp;
744   temp.SetRotateAxisAngle(x, y, z, theta);
745 
746   aMatrix = temp * aMatrix;
747 }
748 
ProcessPerspective(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions)749 static void ProcessPerspective(Matrix4x4& aMatrix,
750                                const nsCSSValue::Array* aData,
751                                GeckoStyleContext* aContext,
752                                nsPresContext* aPresContext,
753                                RuleNodeCacheConditions& aConditions) {
754   NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
755 
756   float depth = ProcessTranslatePart(aData->Item(1), aContext, aPresContext,
757                                      aConditions, nullptr);
758   ApplyPerspectiveToMatrix(aMatrix, depth);
759 }
760 
761 /**
762  * SetToTransformFunction is essentially a giant switch statement that fans
763  * out to many smaller helper functions.
764  */
MatrixForTransformFunction(Matrix4x4 & aMatrix,const nsCSSValue::Array * aData,GeckoStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox,bool * aContains3dTransform)765 static void MatrixForTransformFunction(Matrix4x4& aMatrix,
766                                        const nsCSSValue::Array* aData,
767                                        GeckoStyleContext* aContext,
768                                        nsPresContext* aPresContext,
769                                        RuleNodeCacheConditions& aConditions,
770                                        TransformReferenceBox& aRefBox,
771                                        bool* aContains3dTransform) {
772   MOZ_ASSERT(aContains3dTransform);
773   NS_PRECONDITION(aData, "Why did you want to get data from a null array?");
774   // It's OK if aContext and aPresContext are null if the caller already
775   // knows that all length units have been converted to pixels (as
776   // StyleAnimationValue does).
777 
778   /* Get the keyword for the transform. */
779   switch (TransformFunctionOf(aData)) {
780     case eCSSKeyword_translatex:
781       ProcessTranslateX(aMatrix, aData, aContext, aPresContext, aConditions,
782                         aRefBox);
783       break;
784     case eCSSKeyword_translatey:
785       ProcessTranslateY(aMatrix, aData, aContext, aPresContext, aConditions,
786                         aRefBox);
787       break;
788     case eCSSKeyword_translatez:
789       *aContains3dTransform = true;
790       ProcessTranslateZ(aMatrix, aData, aContext, aPresContext, aConditions);
791       break;
792     case eCSSKeyword_translate:
793       ProcessTranslate(aMatrix, aData, aContext, aPresContext, aConditions,
794                        aRefBox);
795       break;
796     case eCSSKeyword_translate3d:
797       *aContains3dTransform = true;
798       ProcessTranslate3D(aMatrix, aData, aContext, aPresContext, aConditions,
799                          aRefBox);
800       break;
801     case eCSSKeyword_scalex:
802       ProcessScaleX(aMatrix, aData);
803       break;
804     case eCSSKeyword_scaley:
805       ProcessScaleY(aMatrix, aData);
806       break;
807     case eCSSKeyword_scalez:
808       *aContains3dTransform = true;
809       ProcessScaleZ(aMatrix, aData);
810       break;
811     case eCSSKeyword_scale:
812       ProcessScale(aMatrix, aData);
813       break;
814     case eCSSKeyword_scale3d:
815       *aContains3dTransform = true;
816       ProcessScale3D(aMatrix, aData);
817       break;
818     case eCSSKeyword_skewx:
819       ProcessSkewX(aMatrix, aData);
820       break;
821     case eCSSKeyword_skewy:
822       ProcessSkewY(aMatrix, aData);
823       break;
824     case eCSSKeyword_skew:
825       ProcessSkew(aMatrix, aData);
826       break;
827     case eCSSKeyword_rotatex:
828       *aContains3dTransform = true;
829       ProcessRotateX(aMatrix, aData);
830       break;
831     case eCSSKeyword_rotatey:
832       *aContains3dTransform = true;
833       ProcessRotateY(aMatrix, aData);
834       break;
835     case eCSSKeyword_rotatez:
836       *aContains3dTransform = true;
837       MOZ_FALLTHROUGH;
838     case eCSSKeyword_rotate:
839       ProcessRotateZ(aMatrix, aData);
840       break;
841     case eCSSKeyword_rotate3d:
842       *aContains3dTransform = true;
843       ProcessRotate3D(aMatrix, aData);
844       break;
845     case eCSSKeyword_matrix:
846       ProcessMatrix(aMatrix, aData, aContext, aPresContext, aConditions,
847                     aRefBox);
848       break;
849     case eCSSKeyword_matrix3d:
850       *aContains3dTransform = true;
851       ProcessMatrix3D(aMatrix, aData, aContext, aPresContext, aConditions,
852                       aRefBox);
853       break;
854     case eCSSKeyword_interpolatematrix:
855       ProcessMatrixOperator<Interpolate>(aMatrix, aData, aContext, aPresContext,
856                                          aConditions, aRefBox,
857                                          aContains3dTransform);
858       break;
859     case eCSSKeyword_accumulatematrix:
860       ProcessMatrixOperator<Accumulate>(aMatrix, aData, aContext, aPresContext,
861                                         aConditions, aRefBox,
862                                         aContains3dTransform);
863       break;
864     case eCSSKeyword_perspective:
865       *aContains3dTransform = true;
866       ProcessPerspective(aMatrix, aData, aContext, aPresContext, aConditions);
867       break;
868     default:
869       NS_NOTREACHED("Unknown transform function!");
870   }
871 }
872 
873 /**
874  * Return the transform function, as an nsCSSKeyword, for the given
875  * nsCSSValue::Array from a transform list.
876  */
TransformFunctionOf(const nsCSSValue::Array * aData)877 nsCSSKeyword TransformFunctionOf(const nsCSSValue::Array* aData) {
878   MOZ_ASSERT(aData->Item(0).GetUnit() == eCSSUnit_Enumerated);
879   return aData->Item(0).GetKeywordValue();
880 }
881 
SetIdentityMatrix(nsCSSValue::Array * aMatrix)882 void SetIdentityMatrix(nsCSSValue::Array* aMatrix) {
883   MOZ_ASSERT(aMatrix, "aMatrix should be non-null");
884 
885   nsCSSKeyword tfunc = TransformFunctionOf(aMatrix);
886   MOZ_ASSERT(tfunc == eCSSKeyword_matrix || tfunc == eCSSKeyword_matrix3d,
887              "Only accept matrix and matrix3d");
888 
889   if (tfunc == eCSSKeyword_matrix) {
890     MOZ_ASSERT(aMatrix->Count() == 7, "Invalid matrix");
891     Matrix m;
892     for (size_t i = 0; i < 6; ++i) {
893       aMatrix->Item(i + 1).SetFloatValue(m.components[i], eCSSUnit_Number);
894     }
895     return;
896   }
897 
898   MOZ_ASSERT(aMatrix->Count() == 17, "Invalid matrix3d");
899   Matrix4x4 m;
900   for (size_t i = 0; i < 16; ++i) {
901     aMatrix->Item(i + 1).SetFloatValue(m.components[i], eCSSUnit_Number);
902   }
903 }
904 
ReadTransforms(const nsCSSValueList * aList,nsStyleContext * aContext,nsPresContext * aPresContext,RuleNodeCacheConditions & aConditions,TransformReferenceBox & aRefBox,float aAppUnitsPerMatrixUnit,bool * aContains3dTransform)905 Matrix4x4 ReadTransforms(const nsCSSValueList* aList, nsStyleContext* aContext,
906                          nsPresContext* aPresContext,
907                          RuleNodeCacheConditions& aConditions,
908                          TransformReferenceBox& aRefBox,
909                          float aAppUnitsPerMatrixUnit,
910                          bool* aContains3dTransform) {
911   Matrix4x4 result;
912   GeckoStyleContext* contextIfGecko =
913 #ifdef MOZ_OLD_STYLE
914       aContext ? aContext->GetAsGecko() : nullptr;
915 #else
916       nullptr;
917 #endif
918 
919   for (const nsCSSValueList* curr = aList; curr != nullptr;
920        curr = curr->mNext) {
921     const nsCSSValue& currElem = curr->mValue;
922     if (currElem.GetUnit() != eCSSUnit_Function) {
923       NS_ASSERTION(currElem.GetUnit() == eCSSUnit_None && !aList->mNext,
924                    "stream should either be a list of functions or a "
925                    "lone None");
926       continue;
927     }
928     NS_ASSERTION(currElem.GetArrayValue()->Count() >= 1,
929                  "Incoming function is too short!");
930 
931     /* Read in a single transform matrix. */
932     MatrixForTransformFunction(result, currElem.GetArrayValue(), contextIfGecko,
933                                aPresContext, aConditions, aRefBox,
934                                aContains3dTransform);
935   }
936 
937   float scale =
938       float(nsPresContext::AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
939   result.PreScale(1 / scale, 1 / scale, 1 / scale);
940   result.PostScale(scale, scale, scale);
941 
942   return result;
943 }
944 
Convert2DPosition(nsStyleCoord const (& aValue)[2],TransformReferenceBox & aRefBox,int32_t aAppUnitsPerDevPixel)945 Point Convert2DPosition(nsStyleCoord const (&aValue)[2],
946                         TransformReferenceBox& aRefBox,
947                         int32_t aAppUnitsPerDevPixel) {
948   float position[2];
949   nsStyleTransformMatrix::TransformReferenceBox::DimensionGetter
950       dimensionGetter[] = {
951           &nsStyleTransformMatrix::TransformReferenceBox::Width,
952           &nsStyleTransformMatrix::TransformReferenceBox::Height};
953   for (uint8_t index = 0; index < 2; ++index) {
954     const nsStyleCoord& value = aValue[index];
955     if (value.GetUnit() == eStyleUnit_Calc) {
956       const nsStyleCoord::Calc* calc = value.GetCalcValue();
957       position[index] =
958           NSAppUnitsToFloatPixels((aRefBox.*dimensionGetter[index])(),
959                                   aAppUnitsPerDevPixel) *
960               calc->mPercent +
961           NSAppUnitsToFloatPixels(calc->mLength, aAppUnitsPerDevPixel);
962     } else if (value.GetUnit() == eStyleUnit_Percent) {
963       position[index] =
964           NSAppUnitsToFloatPixels((aRefBox.*dimensionGetter[index])(),
965                                   aAppUnitsPerDevPixel) *
966           value.GetPercentValue();
967     } else {
968       MOZ_ASSERT(value.GetUnit() == eStyleUnit_Coord, "unexpected unit");
969       position[index] =
970           NSAppUnitsToFloatPixels(value.GetCoordValue(), aAppUnitsPerDevPixel);
971     }
972   }
973 
974   return Point(position[0], position[1]);
975 }
976 
977 /*
978  * The relevant section of the transitions specification:
979  * http://dev.w3.org/csswg/css3-transitions/#animation-of-property-types-
980  * defers all of the details to the 2-D and 3-D transforms specifications.
981  * For the 2-D transforms specification (all that's relevant for us, right
982  * now), the relevant section is:
983  * http://dev.w3.org/csswg/css3-2d-transforms/#animation
984  * This, in turn, refers to the unmatrix program in Graphics Gems,
985  * available from http://tog.acm.org/resources/GraphicsGems/ , and in
986  * particular as the file GraphicsGems/gemsii/unmatrix.c
987  * in http://tog.acm.org/resources/GraphicsGems/AllGems.tar.gz
988  *
989  * The unmatrix reference is for general 3-D transform matrices (any of the
990  * 16 components can have any value).
991  *
992  * For CSS 2-D transforms, we have a 2-D matrix with the bottom row constant:
993  *
994  * [ A C E ]
995  * [ B D F ]
996  * [ 0 0 1 ]
997  *
998  * For that case, I believe the algorithm in unmatrix reduces to:
999  *
1000  *  (1) If A * D - B * C == 0, the matrix is singular.  Fail.
1001  *
1002  *  (2) Set translation components (Tx and Ty) to the translation parts of
1003  *      the matrix (E and F) and then ignore them for the rest of the time.
1004  *      (For us, E and F each actually consist of three constants:  a
1005  *      length, a multiplier for the width, and a multiplier for the
1006  *      height.  This actually requires its own decomposition, but I'll
1007  *      keep that separate.)
1008  *
1009  *  (3) Let the X scale (Sx) be sqrt(A^2 + B^2).  Then divide both A and B
1010  *      by it.
1011  *
1012  *  (4) Let the XY shear (K) be A * C + B * D.  From C, subtract A times
1013  *      the XY shear.  From D, subtract B times the XY shear.
1014  *
1015  *  (5) Let the Y scale (Sy) be sqrt(C^2 + D^2).  Divide C, D, and the XY
1016  *      shear (K) by it.
1017  *
1018  *  (6) At this point, A * D - B * C is either 1 or -1.  If it is -1,
1019  *      negate the XY shear (K), the X scale (Sx), and A, B, C, and D.
1020  *      (Alternatively, we could negate the XY shear (K) and the Y scale
1021  *      (Sy).)
1022  *
1023  *  (7) Let the rotation be R = atan2(B, A).
1024  *
1025  * Then the resulting decomposed transformation is:
1026  *
1027  *   translate(Tx, Ty) rotate(R) skewX(atan(K)) scale(Sx, Sy)
1028  *
1029  * An interesting result of this is that all of the simple transform
1030  * functions (i.e., all functions other than matrix()), in isolation,
1031  * decompose back to themselves except for:
1032  *   'skewY(φ)', which is 'matrix(1, tan(φ), 0, 1, 0, 0)', which decomposes
1033  *   to 'rotate(φ) skewX(φ) scale(sec(φ), cos(φ))' since (ignoring the
1034  *   alternate sign possibilities that would get fixed in step 6):
1035  *     In step 3, the X scale factor is sqrt(1+tan²(φ)) = sqrt(sec²(φ)) =
1036  * sec(φ). Thus, after step 3, A = 1/sec(φ) = cos(φ) and B = tan(φ) / sec(φ) =
1037  * sin(φ). In step 4, the XY shear is sin(φ). Thus, after step 4, C =
1038  * -cos(φ)sin(φ) and D = 1 - sin²(φ) = cos²(φ). Thus, in step 5, the Y scale
1039  * is sqrt(cos²(φ)(sin²(φ) + cos²(φ)) = cos(φ). Thus, after step 5, C =
1040  * -sin(φ), D = cos(φ), and the XY shear is tan(φ). Thus, in step 6, A * D -
1041  * B * C = cos²(φ) + sin²(φ) = 1. In step 7, the rotation is thus φ.
1042  *
1043  *   skew(θ, φ), which is matrix(1, tan(φ), tan(θ), 1, 0, 0), which decomposes
1044  *   to 'rotate(φ) skewX(θ + φ) scale(sec(φ), cos(φ))' since (ignoring
1045  *   the alternate sign possibilities that would get fixed in step 6):
1046  *     In step 3, the X scale factor is sqrt(1+tan²(φ)) = sqrt(sec²(φ)) =
1047  * sec(φ). Thus, after step 3, A = 1/sec(φ) = cos(φ) and B = tan(φ) / sec(φ) =
1048  * sin(φ). In step 4, the XY shear is cos(φ)tan(θ) + sin(φ). Thus, after
1049  * step 4, C = tan(θ) - cos(φ)(cos(φ)tan(θ) + sin(φ)) = tan(θ)sin²(φ) -
1050  * cos(φ)sin(φ) D = 1 - sin(φ)(cos(φ)tan(θ) + sin(φ)) = cos²(φ) -
1051  * sin(φ)cos(φ)tan(θ) Thus, in step 5, the Y scale is sqrt(C² + D²) =
1052  *     sqrt(tan²(θ)(sin⁴(φ) + sin²(φ)cos²(φ)) -
1053  *          2 tan(θ)(sin³(φ)cos(φ) + sin(φ)cos³(φ)) +
1054  *          (sin²(φ)cos²(φ) + cos⁴(φ))) =
1055  *     sqrt(tan²(θ)sin²(φ) - 2 tan(θ)sin(φ)cos(φ) + cos²(φ)) =
1056  *     cos(φ) - tan(θ)sin(φ) (taking the negative of the obvious solution so
1057  *     we avoid flipping in step 6).
1058  *     After step 5, C = -sin(φ) and D = cos(φ), and the XY shear is
1059  *     (cos(φ)tan(θ) + sin(φ)) / (cos(φ) - tan(θ)sin(φ)) =
1060  *     (dividing both numerator and denominator by cos(φ))
1061  *     (tan(θ) + tan(φ)) / (1 - tan(θ)tan(φ)) = tan(θ + φ).
1062  *     (See http://en.wikipedia.org/wiki/List_of_trigonometric_identities .)
1063  *     Thus, in step 6, A * D - B * C = cos²(φ) + sin²(φ) = 1.
1064  *     In step 7, the rotation is thus φ.
1065  *
1066  *     To check this result, we can multiply things back together:
1067  *
1068  *     [ cos(φ) -sin(φ) ] [ 1 tan(θ + φ) ] [ sec(φ)    0   ]
1069  *     [ sin(φ)  cos(φ) ] [ 0      1     ] [   0    cos(φ) ]
1070  *
1071  *     [ cos(φ)      cos(φ)tan(θ + φ) - sin(φ) ] [ sec(φ)    0   ]
1072  *     [ sin(φ)      sin(φ)tan(θ + φ) + cos(φ) ] [   0    cos(φ) ]
1073  *
1074  *     but since tan(θ + φ) = (tan(θ) + tan(φ)) / (1 - tan(θ)tan(φ)),
1075  *     cos(φ)tan(θ + φ) - sin(φ)
1076  *      = cos(φ)(tan(θ) + tan(φ)) - sin(φ) + sin(φ)tan(θ)tan(φ)
1077  *      = cos(φ)tan(θ) + sin(φ) - sin(φ) + sin(φ)tan(θ)tan(φ)
1078  *      = cos(φ)tan(θ) + sin(φ)tan(θ)tan(φ)
1079  *      = tan(θ) (cos(φ) + sin(φ)tan(φ))
1080  *      = tan(θ) sec(φ) (cos²(φ) + sin²(φ))
1081  *      = tan(θ) sec(φ)
1082  *     and
1083  *     sin(φ)tan(θ + φ) + cos(φ)
1084  *      = sin(φ)(tan(θ) + tan(φ)) + cos(φ) - cos(φ)tan(θ)tan(φ)
1085  *      = tan(θ) (sin(φ) - sin(φ)) + sin(φ)tan(φ) + cos(φ)
1086  *      = sec(φ) (sin²(φ) + cos²(φ))
1087  *      = sec(φ)
1088  *     so the above is:
1089  *     [ cos(φ)  tan(θ) sec(φ) ] [ sec(φ)    0   ]
1090  *     [ sin(φ)     sec(φ)     ] [   0    cos(φ) ]
1091  *
1092  *     [    1   tan(θ) ]
1093  *     [ tan(φ)    1   ]
1094  */
1095 
1096 /*
1097  * Decompose2DMatrix implements the above decomposition algorithm.
1098  */
1099 
Decompose2DMatrix(const Matrix & aMatrix,Point3D & aScale,ShearArray & aShear,gfxQuaternion & aRotate,Point3D & aTranslate)1100 bool Decompose2DMatrix(const Matrix& aMatrix, Point3D& aScale,
1101                        ShearArray& aShear, gfxQuaternion& aRotate,
1102                        Point3D& aTranslate) {
1103   float A = aMatrix._11, B = aMatrix._12, C = aMatrix._21, D = aMatrix._22;
1104   if (A * D == B * C) {
1105     // singular matrix
1106     return false;
1107   }
1108 
1109   float scaleX = sqrt(A * A + B * B);
1110   A /= scaleX;
1111   B /= scaleX;
1112 
1113   float XYshear = A * C + B * D;
1114   C -= A * XYshear;
1115   D -= B * XYshear;
1116 
1117   float scaleY = sqrt(C * C + D * D);
1118   C /= scaleY;
1119   D /= scaleY;
1120   XYshear /= scaleY;
1121 
1122   float determinant = A * D - B * C;
1123   // Determinant should now be 1 or -1.
1124   if (0.99 > Abs(determinant) || Abs(determinant) > 1.01) {
1125     return false;
1126   }
1127 
1128   if (determinant < 0) {
1129     A = -A;
1130     B = -B;
1131     C = -C;
1132     D = -D;
1133     XYshear = -XYshear;
1134     scaleX = -scaleX;
1135   }
1136 
1137   float rotate = atan2f(B, A);
1138   aRotate = gfxQuaternion(0, 0, sin(rotate / 2), cos(rotate / 2));
1139   aShear[ShearType::XYSHEAR] = XYshear;
1140   aScale.x = scaleX;
1141   aScale.y = scaleY;
1142   aTranslate.x = aMatrix._31;
1143   aTranslate.y = aMatrix._32;
1144   return true;
1145 }
1146 
1147 /**
1148  * Implementation of the unmatrix algorithm, specified by:
1149  *
1150  * http://dev.w3.org/csswg/css3-2d-transforms/#unmatrix
1151  *
1152  * This, in turn, refers to the unmatrix program in Graphics Gems,
1153  * available from http://tog.acm.org/resources/GraphicsGems/ , and in
1154  * particular as the file GraphicsGems/gemsii/unmatrix.c
1155  * in http://tog.acm.org/resources/GraphicsGems/AllGems.tar.gz
1156  */
Decompose3DMatrix(const Matrix4x4 & aMatrix,Point3D & aScale,ShearArray & aShear,gfxQuaternion & aRotate,Point3D & aTranslate,Point4D & aPerspective)1157 bool Decompose3DMatrix(const Matrix4x4& aMatrix, Point3D& aScale,
1158                        ShearArray& aShear, gfxQuaternion& aRotate,
1159                        Point3D& aTranslate, Point4D& aPerspective) {
1160   Matrix4x4 local = aMatrix;
1161 
1162   if (local[3][3] == 0) {
1163     return false;
1164   }
1165   /* Normalize the matrix */
1166   local.Normalize();
1167 
1168   /**
1169    * perspective is used to solve for perspective, but it also provides
1170    * an easy way to test for singularity of the upper 3x3 component.
1171    */
1172   Matrix4x4 perspective = local;
1173   Point4D empty(0, 0, 0, 1);
1174   perspective.SetTransposedVector(3, empty);
1175 
1176   if (perspective.Determinant() == 0.0) {
1177     return false;
1178   }
1179 
1180   /* First, isolate perspective. */
1181   if (local[0][3] != 0 || local[1][3] != 0 || local[2][3] != 0) {
1182     /* aPerspective is the right hand side of the equation. */
1183     aPerspective = local.TransposedVector(3);
1184 
1185     /**
1186      * Solve the equation by inverting perspective and multiplying
1187      * aPerspective by the inverse.
1188      */
1189     perspective.Invert();
1190     aPerspective = perspective.TransposeTransform4D(aPerspective);
1191 
1192     /* Clear the perspective partition */
1193     local.SetTransposedVector(3, empty);
1194   } else {
1195     aPerspective = Point4D(0, 0, 0, 1);
1196   }
1197 
1198   /* Next take care of translation */
1199   for (int i = 0; i < 3; i++) {
1200     aTranslate[i] = local[3][i];
1201     local[3][i] = 0;
1202   }
1203 
1204   /* Now get scale and shear. */
1205 
1206   /* Compute X scale factor and normalize first row. */
1207   aScale.x = local[0].Length();
1208   local[0] /= aScale.x;
1209 
1210   /* Compute XY shear factor and make 2nd local orthogonal to 1st. */
1211   aShear[ShearType::XYSHEAR] = local[0].DotProduct(local[1]);
1212   local[1] -= local[0] * aShear[ShearType::XYSHEAR];
1213 
1214   /* Now, compute Y scale and normalize 2nd local. */
1215   aScale.y = local[1].Length();
1216   local[1] /= aScale.y;
1217   aShear[ShearType::XYSHEAR] /= aScale.y;
1218 
1219   /* Compute XZ and YZ shears, make 3rd local orthogonal */
1220   aShear[ShearType::XZSHEAR] = local[0].DotProduct(local[2]);
1221   local[2] -= local[0] * aShear[ShearType::XZSHEAR];
1222   aShear[ShearType::YZSHEAR] = local[1].DotProduct(local[2]);
1223   local[2] -= local[1] * aShear[ShearType::YZSHEAR];
1224 
1225   /* Next, get Z scale and normalize 3rd local. */
1226   aScale.z = local[2].Length();
1227   local[2] /= aScale.z;
1228 
1229   aShear[ShearType::XZSHEAR] /= aScale.z;
1230   aShear[ShearType::YZSHEAR] /= aScale.z;
1231 
1232   /**
1233    * At this point, the matrix (in locals) is orthonormal.
1234    * Check for a coordinate system flip.  If the determinant
1235    * is -1, then negate the matrix and the scaling factors.
1236    */
1237   if (local[0].DotProduct(local[1].CrossProduct(local[2])) < 0) {
1238     aScale *= -1;
1239     for (int i = 0; i < 3; i++) {
1240       local[i] *= -1;
1241     }
1242   }
1243 
1244   /* Now, get the rotations out */
1245   aRotate = gfxQuaternion(local);
1246 
1247   return true;
1248 }
1249 
CSSValueArrayTo2DMatrix(nsCSSValue::Array * aArray)1250 Matrix CSSValueArrayTo2DMatrix(nsCSSValue::Array* aArray) {
1251   MOZ_ASSERT(aArray && TransformFunctionOf(aArray) == eCSSKeyword_matrix &&
1252              aArray->Count() == 7);
1253   Matrix m(aArray->Item(1).GetFloatValue(), aArray->Item(2).GetFloatValue(),
1254            aArray->Item(3).GetFloatValue(), aArray->Item(4).GetFloatValue(),
1255            aArray->Item(5).GetFloatValue(), aArray->Item(6).GetFloatValue());
1256   return m;
1257 }
1258 
CSSValueArrayTo3DMatrix(nsCSSValue::Array * aArray)1259 Matrix4x4 CSSValueArrayTo3DMatrix(nsCSSValue::Array* aArray) {
1260   MOZ_ASSERT(aArray && TransformFunctionOf(aArray) == eCSSKeyword_matrix3d &&
1261              aArray->Count() == 17);
1262   gfx::Float array[16];
1263   for (size_t i = 0; i < 16; ++i) {
1264     array[i] = aArray->Item(i + 1).GetFloatValue();
1265   }
1266   Matrix4x4 m(array);
1267   return m;
1268 }
1269 
GetScaleValue(const nsCSSValueSharedList * aList,const nsIFrame * aForFrame)1270 Size GetScaleValue(const nsCSSValueSharedList* aList,
1271                    const nsIFrame* aForFrame) {
1272   MOZ_ASSERT(aList && aList->mHead);
1273   MOZ_ASSERT(aForFrame);
1274 
1275   RuleNodeCacheConditions dontCare;
1276   bool dontCareBool;
1277   TransformReferenceBox refBox(aForFrame);
1278   Matrix4x4 transform = ReadTransforms(
1279       aList->mHead, aForFrame->StyleContext(), aForFrame->PresContext(),
1280       dontCare, refBox, aForFrame->PresContext()->AppUnitsPerDevPixel(),
1281       &dontCareBool);
1282   Matrix transform2d;
1283   bool canDraw2D = transform.CanDraw2D(&transform2d);
1284   if (!canDraw2D) {
1285     return Size();
1286   }
1287 
1288   return transform2d.ScaleFactors(true);
1289 }
1290 
1291 }  // namespace nsStyleTransformMatrix
1292