Backed out 4 changesets (bug 1825722) for causing reftest failures CLOSED TREE
[gecko.git] / layout / style / nsStyleTransformMatrix.cpp
blob593bcbb39d49244c4113a3dce62a1535c48ad740
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/. */
7 /*
8 * A class used for intermediate representations of the transform and
9 * transform-like properties.
12 #include "nsStyleTransformMatrix.h"
13 #include "nsLayoutUtils.h"
14 #include "nsPresContext.h"
15 #include "mozilla/MotionPathUtils.h"
16 #include "mozilla/ServoBindings.h"
17 #include "mozilla/StaticPrefs_layout.h"
18 #include "mozilla/StyleAnimationValue.h"
19 #include "mozilla/SVGUtils.h"
20 #include "gfxMatrix.h"
21 #include "gfxQuaternion.h"
23 using namespace mozilla;
24 using namespace mozilla::gfx;
26 namespace nsStyleTransformMatrix {
28 /* Note on floating point precision: The transform matrix is an array
29 * of single precision 'float's, and so are most of the input values
30 * we get from the style system, but intermediate calculations
31 * involving angles need to be done in 'double'.
34 // Define UNIFIED_CONTINUATIONS here and in nsDisplayList.cpp
35 // to have the transform property try
36 // to transform content with continuations as one unified block instead of
37 // several smaller ones. This is currently disabled because it doesn't work
38 // correctly, since when the frames are initially being reflowed, their
39 // continuations all compute their bounding rects independently of each other
40 // and consequently get the wrong value.
41 // #define UNIFIED_CONTINUATIONS
43 static nsRect GetSVGBox(const nsIFrame* aFrame) {
44 auto computeViewBox = [&]() {
45 // Percentages in transforms resolve against the width/height of the
46 // nearest viewport (or its viewBox if one is applied), and the
47 // transform is relative to {0,0} in current user space.
48 CSSSize size = CSSSize::FromUnknownSize(SVGUtils::GetContextSize(aFrame));
49 return nsRect(-aFrame->GetPosition(), CSSPixel::ToAppUnits(size));
52 auto transformBox = aFrame->StyleDisplay()->mTransformBox;
53 if ((transformBox == StyleTransformBox::StrokeBox ||
54 transformBox == StyleTransformBox::BorderBox) &&
55 aFrame->StyleSVGReset()->HasNonScalingStroke()) {
56 // To calculate stroke bounds for an element with `non-scaling-stroke` we
57 // need to resolve its transform to its outer-svg, but to resolve that
58 // transform when it has `transform-box:stroke-box` (or `border-box`)
59 // may require its stroke bounds. There's no ideal way to break this
60 // cyclical dependency, but we break it by converting to
61 // `transform-box:fill-box` here.
62 // https://github.com/w3c/csswg-drafts/issues/9640
63 transformBox = StyleTransformBox::FillBox;
66 // For SVG elements without associated CSS layout box, the used value for
67 // content-box is fill-box and for border-box is stroke-box.
68 // https://drafts.csswg.org/css-transforms-1/#transform-box
69 switch (transformBox) {
70 case StyleTransformBox::ContentBox:
71 case StyleTransformBox::FillBox: {
72 // Percentages in transforms resolve against the SVG bbox, and the
73 // transform is relative to the top-left of the SVG bbox.
74 nsRect bboxInAppUnits = nsLayoutUtils::ComputeSVGReferenceRect(
75 const_cast<nsIFrame*>(aFrame), StyleGeometryBox::FillBox);
76 // The mRect of an SVG nsIFrame is its user space bounds *including*
77 // stroke and markers, whereas bboxInAppUnits is its user space bounds
78 // including fill only. We need to note the offset of the reference box
79 // from the frame's mRect in mX/mY.
80 return {bboxInAppUnits.x - aFrame->GetPosition().x,
81 bboxInAppUnits.y - aFrame->GetPosition().y, bboxInAppUnits.width,
82 bboxInAppUnits.height};
84 case StyleTransformBox::BorderBox:
85 if (!StaticPrefs::layout_css_transform_box_content_stroke_enabled()) {
86 // If stroke-box is disabled, we shouldn't use it and fall back to
87 // view-box.
88 return computeViewBox();
90 [[fallthrough]];
91 case StyleTransformBox::StrokeBox: {
92 // We are using SVGUtils::PathExtentsToMaxStrokeExtents() to compute the
93 // bbox contribution for stroke box (if it doesn't have simple bounds),
94 // so the |strokeBox| here may be larger than the author's expectation.
95 // Using Moz2D to compute the tighter bounding box is another way but it
96 // has some potential issues (see SVGGeometryFrame::GetBBoxContribution()
97 // for more details), and its result depends on the drawing backend. So
98 // for now we still rely on our default calcuclation for SVG geometry
99 // frame reflow code. At least this works for the shape elements which
100 // have simple bounds.
101 // FIXME: Bug 1849054. We may have to update
102 // SVGGeometryFrame::GetBBoxContribution() to get tighter stroke bounds.
103 nsRect strokeBox = nsLayoutUtils::ComputeSVGReferenceRect(
104 const_cast<nsIFrame*>(aFrame), StyleGeometryBox::StrokeBox);
105 // The |nsIFrame::mRect| includes markers, so we have to compute the
106 // offsets without markers.
107 return nsRect{strokeBox.x - aFrame->GetPosition().x,
108 strokeBox.y - aFrame->GetPosition().y, strokeBox.width,
109 strokeBox.height};
111 case StyleTransformBox::ViewBox:
112 return computeViewBox();
115 MOZ_ASSERT_UNREACHABLE("All transform box should be handled.");
116 return {};
119 void TransformReferenceBox::EnsureDimensionsAreCached() {
120 if (mIsCached) {
121 return;
124 MOZ_ASSERT(mFrame);
126 mIsCached = true;
128 if (mFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
129 mBox = GetSVGBox(mFrame);
130 return;
133 // For elements with associated CSS layout box, the used value for fill-box is
134 // content-box and for stroke-box and view-box is border-box.
135 // https://drafts.csswg.org/css-transforms-1/#transform-box
136 switch (mFrame->StyleDisplay()->mTransformBox) {
137 case StyleTransformBox::FillBox:
138 case StyleTransformBox::ContentBox: {
139 mBox = mFrame->GetContentRectRelativeToSelf();
140 return;
142 case StyleTransformBox::StrokeBox:
143 // TODO: Implement this in the following patches.
144 return;
145 case StyleTransformBox::ViewBox:
146 case StyleTransformBox::BorderBox: {
147 // If UNIFIED_CONTINUATIONS is not defined, this is simply the frame's
148 // bounding rectangle, translated to the origin. Otherwise, it is the
149 // smallest rectangle containing a frame and all of its continuations. For
150 // example, if there is a <span> element with several continuations split
151 // over several lines, this function will return the rectangle containing
152 // all of those continuations.
154 nsRect rect;
156 #ifndef UNIFIED_CONTINUATIONS
157 rect = mFrame->GetRect();
158 #else
159 // Iterate the continuation list, unioning together the bounding rects:
160 for (const nsIFrame* currFrame = mFrame->FirstContinuation();
161 currFrame != nullptr; currFrame = currFrame->GetNextContinuation()) {
162 // Get the frame rect in local coordinates, then translate back to the
163 // original coordinates:
164 rect.UnionRect(result, nsRect(currFrame->GetOffsetTo(mFrame),
165 currFrame->GetSize()));
167 #endif
169 mBox = {0, 0, rect.Width(), rect.Height()};
170 return;
175 float ProcessTranslatePart(
176 const LengthPercentage& aValue, TransformReferenceBox* aRefBox,
177 TransformReferenceBox::DimensionGetter aDimensionGetter) {
178 return aValue.ResolveToCSSPixelsWith([&] {
179 return aRefBox && !aRefBox->IsEmpty()
180 ? CSSPixel::FromAppUnits((aRefBox->*aDimensionGetter)())
181 : CSSCoord(0);
186 * Helper functions to process all the transformation function types.
188 * These take a matrix parameter to accumulate the current matrix.
191 /* Helper function to process a matrix entry. */
192 static void ProcessMatrix(Matrix4x4& aMatrix,
193 const StyleTransformOperation& aOp) {
194 const auto& matrix = aOp.AsMatrix();
195 gfxMatrix result;
197 result._11 = matrix.a;
198 result._12 = matrix.b;
199 result._21 = matrix.c;
200 result._22 = matrix.d;
201 result._31 = matrix.e;
202 result._32 = matrix.f;
204 aMatrix = result * aMatrix;
207 static void ProcessMatrix3D(Matrix4x4& aMatrix,
208 const StyleTransformOperation& aOp) {
209 Matrix4x4 temp;
211 const auto& matrix = aOp.AsMatrix3D();
213 temp._11 = matrix.m11;
214 temp._12 = matrix.m12;
215 temp._13 = matrix.m13;
216 temp._14 = matrix.m14;
217 temp._21 = matrix.m21;
218 temp._22 = matrix.m22;
219 temp._23 = matrix.m23;
220 temp._24 = matrix.m24;
221 temp._31 = matrix.m31;
222 temp._32 = matrix.m32;
223 temp._33 = matrix.m33;
224 temp._34 = matrix.m34;
226 temp._41 = matrix.m41;
227 temp._42 = matrix.m42;
228 temp._43 = matrix.m43;
229 temp._44 = matrix.m44;
231 aMatrix = temp * aMatrix;
234 // For accumulation for transform functions, |aOne| corresponds to |aB| and
235 // |aTwo| corresponds to |aA| for StyleAnimationValue::Accumulate().
236 class Accumulate {
237 public:
238 template <typename T>
239 static T operate(const T& aOne, const T& aTwo, double aCoeff) {
240 return aOne + aTwo * aCoeff;
243 static Point4D operateForPerspective(const Point4D& aOne, const Point4D& aTwo,
244 double aCoeff) {
245 return (aOne - Point4D(0, 0, 0, 1)) +
246 (aTwo - Point4D(0, 0, 0, 1)) * aCoeff + Point4D(0, 0, 0, 1);
248 static Point3D operateForScale(const Point3D& aOne, const Point3D& aTwo,
249 double aCoeff) {
250 // For scale, the identify element is 1, see AddTransformScale in
251 // StyleAnimationValue.cpp.
252 return (aOne - Point3D(1, 1, 1)) + (aTwo - Point3D(1, 1, 1)) * aCoeff +
253 Point3D(1, 1, 1);
256 static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
257 const gfxQuaternion& aTwo, double aCoeff) {
258 if (aCoeff == 0.0) {
259 return aOne.ToMatrix();
262 double theta = acos(mozilla::clamped(aTwo.w, -1.0, 1.0));
263 double scale = (theta != 0.0) ? 1.0 / sin(theta) : 0.0;
264 theta *= aCoeff;
265 scale *= sin(theta);
267 gfxQuaternion result = gfxQuaternion(scale * aTwo.x, scale * aTwo.y,
268 scale * aTwo.z, cos(theta)) *
269 aOne;
270 return result.ToMatrix();
273 static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
274 const Matrix4x4& aMatrix2,
275 double aProgress) {
276 return aMatrix1;
279 static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
280 const Matrix4x4& aMatrix2, double aCount) {
281 Matrix4x4 result;
282 Servo_MatrixTransform_Operate(MatrixTransformOperator::Accumulate,
283 &aMatrix1.components, &aMatrix2.components,
284 aCount, &result.components);
285 return result;
289 class Interpolate {
290 public:
291 template <typename T>
292 static T operate(const T& aOne, const T& aTwo, double aCoeff) {
293 return aOne + (aTwo - aOne) * aCoeff;
296 static Point4D operateForPerspective(const Point4D& aOne, const Point4D& aTwo,
297 double aCoeff) {
298 return aOne + (aTwo - aOne) * aCoeff;
301 static Point3D operateForScale(const Point3D& aOne, const Point3D& aTwo,
302 double aCoeff) {
303 return aOne + (aTwo - aOne) * aCoeff;
306 static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
307 const gfxQuaternion& aTwo, double aCoeff) {
308 return aOne.Slerp(aTwo, aCoeff).ToMatrix();
311 static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
312 const Matrix4x4& aMatrix2,
313 double aProgress) {
314 return aProgress < 0.5 ? aMatrix1 : aMatrix2;
317 static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
318 const Matrix4x4& aMatrix2, double aProgress) {
319 Matrix4x4 result;
320 Servo_MatrixTransform_Operate(MatrixTransformOperator::Interpolate,
321 &aMatrix1.components, &aMatrix2.components,
322 aProgress, &result.components);
323 return result;
327 template <typename Operator>
328 static void ProcessMatrixOperator(Matrix4x4& aMatrix,
329 const StyleTransform& aFrom,
330 const StyleTransform& aTo, float aProgress,
331 TransformReferenceBox& aRefBox) {
332 float appUnitPerCSSPixel = AppUnitsPerCSSPixel();
333 Matrix4x4 matrix1 = ReadTransforms(aFrom, aRefBox, appUnitPerCSSPixel);
334 Matrix4x4 matrix2 = ReadTransforms(aTo, aRefBox, appUnitPerCSSPixel);
335 aMatrix = Operator::operateByServo(matrix1, matrix2, aProgress) * aMatrix;
338 /* Helper function to process two matrices that we need to interpolate between
340 void ProcessInterpolateMatrix(Matrix4x4& aMatrix,
341 const StyleTransformOperation& aOp,
342 TransformReferenceBox& aRefBox) {
343 const auto& args = aOp.AsInterpolateMatrix();
344 ProcessMatrixOperator<Interpolate>(aMatrix, args.from_list, args.to_list,
345 args.progress._0, aRefBox);
348 void ProcessAccumulateMatrix(Matrix4x4& aMatrix,
349 const StyleTransformOperation& aOp,
350 TransformReferenceBox& aRefBox) {
351 const auto& args = aOp.AsAccumulateMatrix();
352 ProcessMatrixOperator<Accumulate>(aMatrix, args.from_list, args.to_list,
353 args.count, aRefBox);
356 /* Helper function to process a translatex function. */
357 static void ProcessTranslateX(Matrix4x4& aMatrix,
358 const LengthPercentage& aLength,
359 TransformReferenceBox& aRefBox) {
360 Point3D temp;
361 temp.x =
362 ProcessTranslatePart(aLength, &aRefBox, &TransformReferenceBox::Width);
363 aMatrix.PreTranslate(temp);
366 /* Helper function to process a translatey function. */
367 static void ProcessTranslateY(Matrix4x4& aMatrix,
368 const LengthPercentage& aLength,
369 TransformReferenceBox& aRefBox) {
370 Point3D temp;
371 temp.y =
372 ProcessTranslatePart(aLength, &aRefBox, &TransformReferenceBox::Height);
373 aMatrix.PreTranslate(temp);
376 static void ProcessTranslateZ(Matrix4x4& aMatrix, const Length& aLength) {
377 Point3D temp;
378 temp.z = aLength.ToCSSPixels();
379 aMatrix.PreTranslate(temp);
382 /* Helper function to process a translate function. */
383 static void ProcessTranslate(Matrix4x4& aMatrix, const LengthPercentage& aX,
384 const LengthPercentage& aY,
385 TransformReferenceBox& aRefBox) {
386 Point3D temp;
387 temp.x = ProcessTranslatePart(aX, &aRefBox, &TransformReferenceBox::Width);
388 temp.y = ProcessTranslatePart(aY, &aRefBox, &TransformReferenceBox::Height);
389 aMatrix.PreTranslate(temp);
392 static void ProcessTranslate3D(Matrix4x4& aMatrix, const LengthPercentage& aX,
393 const LengthPercentage& aY, const Length& aZ,
394 TransformReferenceBox& aRefBox) {
395 Point3D temp;
397 temp.x = ProcessTranslatePart(aX, &aRefBox, &TransformReferenceBox::Width);
398 temp.y = ProcessTranslatePart(aY, &aRefBox, &TransformReferenceBox::Height);
399 temp.z = aZ.ToCSSPixels();
401 aMatrix.PreTranslate(temp);
404 /* Helper function to set up a scale matrix. */
405 static void ProcessScaleHelper(Matrix4x4& aMatrix, float aXScale, float aYScale,
406 float aZScale) {
407 aMatrix.PreScale(aXScale, aYScale, aZScale);
410 static void ProcessScale3D(Matrix4x4& aMatrix,
411 const StyleTransformOperation& aOp) {
412 const auto& scale = aOp.AsScale3D();
413 ProcessScaleHelper(aMatrix, scale._0, scale._1, scale._2);
416 /* Helper function that, given a set of angles, constructs the appropriate
417 * skew matrix.
419 static void ProcessSkewHelper(Matrix4x4& aMatrix, const StyleAngle& aXAngle,
420 const StyleAngle& aYAngle) {
421 aMatrix.SkewXY(aXAngle.ToRadians(), aYAngle.ToRadians());
424 static void ProcessRotate3D(Matrix4x4& aMatrix, float aX, float aY, float aZ,
425 const StyleAngle& aAngle) {
426 Matrix4x4 temp;
427 temp.SetRotateAxisAngle(aX, aY, aZ, aAngle.ToRadians());
428 aMatrix = temp * aMatrix;
431 static void ProcessPerspective(
432 Matrix4x4& aMatrix,
433 const StyleGenericPerspectiveFunction<Length>& aPerspective) {
434 if (aPerspective.IsNone()) {
435 return;
437 float p = aPerspective.AsLength().ToCSSPixels();
438 if (!std::isinf(p)) {
439 aMatrix.Perspective(std::max(p, 1.0f));
443 static void MatrixForTransformFunction(Matrix4x4& aMatrix,
444 const StyleTransformOperation& aOp,
445 TransformReferenceBox& aRefBox) {
446 /* Get the keyword for the transform. */
447 switch (aOp.tag) {
448 case StyleTransformOperation::Tag::TranslateX:
449 ProcessTranslateX(aMatrix, aOp.AsTranslateX(), aRefBox);
450 break;
451 case StyleTransformOperation::Tag::TranslateY:
452 ProcessTranslateY(aMatrix, aOp.AsTranslateY(), aRefBox);
453 break;
454 case StyleTransformOperation::Tag::TranslateZ:
455 ProcessTranslateZ(aMatrix, aOp.AsTranslateZ());
456 break;
457 case StyleTransformOperation::Tag::Translate:
458 ProcessTranslate(aMatrix, aOp.AsTranslate()._0, aOp.AsTranslate()._1,
459 aRefBox);
460 break;
461 case StyleTransformOperation::Tag::Translate3D:
462 return ProcessTranslate3D(aMatrix, aOp.AsTranslate3D()._0,
463 aOp.AsTranslate3D()._1, aOp.AsTranslate3D()._2,
464 aRefBox);
465 break;
466 case StyleTransformOperation::Tag::ScaleX:
467 ProcessScaleHelper(aMatrix, aOp.AsScaleX(), 1.0f, 1.0f);
468 break;
469 case StyleTransformOperation::Tag::ScaleY:
470 ProcessScaleHelper(aMatrix, 1.0f, aOp.AsScaleY(), 1.0f);
471 break;
472 case StyleTransformOperation::Tag::ScaleZ:
473 ProcessScaleHelper(aMatrix, 1.0f, 1.0f, aOp.AsScaleZ());
474 break;
475 case StyleTransformOperation::Tag::Scale:
476 ProcessScaleHelper(aMatrix, aOp.AsScale()._0, aOp.AsScale()._1, 1.0f);
477 break;
478 case StyleTransformOperation::Tag::Scale3D:
479 ProcessScale3D(aMatrix, aOp);
480 break;
481 case StyleTransformOperation::Tag::SkewX:
482 ProcessSkewHelper(aMatrix, aOp.AsSkewX(), StyleAngle::Zero());
483 break;
484 case StyleTransformOperation::Tag::SkewY:
485 ProcessSkewHelper(aMatrix, StyleAngle::Zero(), aOp.AsSkewY());
486 break;
487 case StyleTransformOperation::Tag::Skew:
488 ProcessSkewHelper(aMatrix, aOp.AsSkew()._0, aOp.AsSkew()._1);
489 break;
490 case StyleTransformOperation::Tag::RotateX:
491 aMatrix.RotateX(aOp.AsRotateX().ToRadians());
492 break;
493 case StyleTransformOperation::Tag::RotateY:
494 aMatrix.RotateY(aOp.AsRotateY().ToRadians());
495 break;
496 case StyleTransformOperation::Tag::RotateZ:
497 aMatrix.RotateZ(aOp.AsRotateZ().ToRadians());
498 break;
499 case StyleTransformOperation::Tag::Rotate:
500 aMatrix.RotateZ(aOp.AsRotate().ToRadians());
501 break;
502 case StyleTransformOperation::Tag::Rotate3D:
503 ProcessRotate3D(aMatrix, aOp.AsRotate3D()._0, aOp.AsRotate3D()._1,
504 aOp.AsRotate3D()._2, aOp.AsRotate3D()._3);
505 break;
506 case StyleTransformOperation::Tag::Matrix:
507 ProcessMatrix(aMatrix, aOp);
508 break;
509 case StyleTransformOperation::Tag::Matrix3D:
510 ProcessMatrix3D(aMatrix, aOp);
511 break;
512 case StyleTransformOperation::Tag::InterpolateMatrix:
513 ProcessInterpolateMatrix(aMatrix, aOp, aRefBox);
514 break;
515 case StyleTransformOperation::Tag::AccumulateMatrix:
516 ProcessAccumulateMatrix(aMatrix, aOp, aRefBox);
517 break;
518 case StyleTransformOperation::Tag::Perspective:
519 ProcessPerspective(aMatrix, aOp.AsPerspective());
520 break;
521 default:
522 MOZ_ASSERT_UNREACHABLE("Unknown transform function!");
526 Matrix4x4 ReadTransforms(const StyleTransform& aTransform,
527 TransformReferenceBox& aRefBox,
528 float aAppUnitsPerMatrixUnit) {
529 Matrix4x4 result;
531 for (const StyleTransformOperation& op : aTransform.Operations()) {
532 MatrixForTransformFunction(result, op, aRefBox);
535 float scale = float(AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
536 result.PreScale(1 / scale, 1 / scale, 1 / scale);
537 result.PostScale(scale, scale, scale);
539 return result;
542 static void ProcessTranslate(Matrix4x4& aMatrix,
543 const StyleTranslate& aTranslate,
544 TransformReferenceBox& aRefBox) {
545 switch (aTranslate.tag) {
546 case StyleTranslate::Tag::None:
547 return;
548 case StyleTranslate::Tag::Translate:
549 return ProcessTranslate3D(aMatrix, aTranslate.AsTranslate()._0,
550 aTranslate.AsTranslate()._1,
551 aTranslate.AsTranslate()._2, aRefBox);
552 default:
553 MOZ_ASSERT_UNREACHABLE("Huh?");
557 static void ProcessRotate(Matrix4x4& aMatrix, const StyleRotate& aRotate) {
558 switch (aRotate.tag) {
559 case StyleRotate::Tag::None:
560 return;
561 case StyleRotate::Tag::Rotate:
562 aMatrix.RotateZ(aRotate.AsRotate().ToRadians());
563 return;
564 case StyleRotate::Tag::Rotate3D:
565 return ProcessRotate3D(aMatrix, aRotate.AsRotate3D()._0,
566 aRotate.AsRotate3D()._1, aRotate.AsRotate3D()._2,
567 aRotate.AsRotate3D()._3);
568 default:
569 MOZ_ASSERT_UNREACHABLE("Huh?");
573 static void ProcessScale(Matrix4x4& aMatrix, const StyleScale& aScale) {
574 switch (aScale.tag) {
575 case StyleScale::Tag::None:
576 return;
577 case StyleScale::Tag::Scale:
578 return ProcessScaleHelper(aMatrix, aScale.AsScale()._0,
579 aScale.AsScale()._1, aScale.AsScale()._2);
580 default:
581 MOZ_ASSERT_UNREACHABLE("Huh?");
585 Matrix4x4 ReadTransforms(const StyleTranslate& aTranslate,
586 const StyleRotate& aRotate, const StyleScale& aScale,
587 const ResolvedMotionPathData* aMotion,
588 const StyleTransform& aTransform,
589 TransformReferenceBox& aRefBox,
590 float aAppUnitsPerMatrixUnit) {
591 Matrix4x4 result;
593 ProcessTranslate(result, aTranslate, aRefBox);
594 ProcessRotate(result, aRotate);
595 ProcessScale(result, aScale);
597 if (aMotion) {
598 // Create the equivalent translate and rotate function, according to the
599 // order in spec. We combine the translate and then the rotate.
600 // https://drafts.fxtf.org/motion-1/#calculating-path-transform
602 // Besides, we have to shift the object by the delta between anchor-point
603 // and transform-origin, to make sure we rotate the object according to
604 // anchor-point.
605 result.PreTranslate(aMotion->mTranslate.x + aMotion->mShift.x,
606 aMotion->mTranslate.y + aMotion->mShift.y, 0.0);
607 if (aMotion->mRotate != 0.0) {
608 result.RotateZ(aMotion->mRotate);
610 // Shift the origin back to transform-origin.
611 result.PreTranslate(-aMotion->mShift.x, -aMotion->mShift.y, 0.0);
614 for (const StyleTransformOperation& op : aTransform.Operations()) {
615 MatrixForTransformFunction(result, op, aRefBox);
618 float scale = float(AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
619 result.PreScale(1 / scale, 1 / scale, 1 / scale);
620 result.PostScale(scale, scale, scale);
622 return result;
625 mozilla::CSSPoint Convert2DPosition(const mozilla::LengthPercentage& aX,
626 const mozilla::LengthPercentage& aY,
627 const CSSSize& aSize) {
628 return {
629 aX.ResolveToCSSPixels(aSize.width),
630 aY.ResolveToCSSPixels(aSize.height),
634 CSSPoint Convert2DPosition(const LengthPercentage& aX,
635 const LengthPercentage& aY,
636 TransformReferenceBox& aRefBox) {
637 return {
638 aX.ResolveToCSSPixelsWith(
639 [&] { return CSSPixel::FromAppUnits(aRefBox.Width()); }),
640 aY.ResolveToCSSPixelsWith(
641 [&] { return CSSPixel::FromAppUnits(aRefBox.Height()); }),
645 Point Convert2DPosition(const LengthPercentage& aX, const LengthPercentage& aY,
646 TransformReferenceBox& aRefBox,
647 int32_t aAppUnitsPerPixel) {
648 float scale = mozilla::AppUnitsPerCSSPixel() / float(aAppUnitsPerPixel);
649 CSSPoint p = Convert2DPosition(aX, aY, aRefBox);
650 return {p.x * scale, p.y * scale};
653 } // namespace nsStyleTransformMatrix