style: Avoid generating InterpolateMatrix operations if there are no size dependencies

The issue here is that we end up with a transition between mismatched
transform lists that ends up generating an InterpolateMatrix {}
operation. So far so good, but we end up interpolating that a lot of
times and generating an unboundedly-deep operation list.

This implementas an optimization that flattens them to a single matrix
when possible (when there's no dependencies on the containing box).

This is similar to:

  https://chromium.googlesource.com/chromium/src.git/+/2b89cc4df436e672ef9cf940d1c0dc73fef82a4a

We fix the to_pixel_length() behavior for LenghtPercentage to be
correct (and update callers to preserve behavior).

Differential Revision: https://phabricator.services.mozilla.com/D134784
This commit is contained in:
Emilio Cobos Álvarez 2023-06-06 23:34:11 +02:00 committed by Oriol Brufau
parent 6cb3b7e254
commit 080b3f8d1a
2 changed files with 83 additions and 65 deletions

View file

@ -404,15 +404,7 @@ impl ToAbsoluteLength for ComputedLength {
impl ToAbsoluteLength for ComputedLengthPercentage {
#[inline]
fn to_pixel_length(&self, containing_len: Option<ComputedLength>) -> Result<CSSFloat, ()> {
match containing_len {
Some(relative_len) => Ok(self.resolve(relative_len).px()),
// If we don't have reference box, we cannot resolve the used value,
// so only retrieve the length part. This will be used for computing
// distance without any layout info.
//
// FIXME(emilio): This looks wrong.
None => Ok(self.resolve(Zero::zero()).px()),
}
Ok(self.maybe_percentage_relative_to(containing_len).ok_or(())?.px())
}
}
@ -572,12 +564,21 @@ impl<T> Transform<T> {
impl<T: ToMatrix> Transform<T> {
/// Return the equivalent 3d matrix of this transform list.
///
/// We return a pair: the first one is the transform matrix, and the second one
/// indicates if there is any 3d transform function in this transform list.
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn to_transform_3d_matrix(
&self,
reference_box: Option<&Rect<ComputedLength>>
) -> Result<(Transform3D<CSSFloat>, bool), ()> {
Self::components_to_transform_3d_matrix(&self.0, reference_box)
}
/// Converts a series of components to a 3d matrix.
pub fn components_to_transform_3d_matrix(
ops: &[T],
reference_box: Option<&Rect<ComputedLength>>
) -> Result<(Transform3D<CSSFloat>, bool), ()> {
let cast_3d_transform = |m: Transform3D<f64>| -> Transform3D<CSSFloat> {
use std::{f32, f64};
@ -590,26 +591,27 @@ impl<T: ToMatrix> Transform<T> {
)
};
let (m, is_3d) = self.to_transform_3d_matrix_f64(reference_box)?;
let (m, is_3d) = Self::components_to_transform_3d_matrix_f64(ops, reference_box)?;
Ok((cast_3d_transform(m), is_3d))
}
/// Same as Transform::to_transform_3d_matrix but a f64 version.
pub fn to_transform_3d_matrix_f64(
&self,
fn components_to_transform_3d_matrix_f64(
ops: &[T],
reference_box: Option<&Rect<ComputedLength>>,
) -> Result<(Transform3D<f64>, bool), ()> {
// We intentionally use Transform3D<f64> during computation to avoid error propagation
// because using f32 to compute triangle functions (e.g. in rotation()) is not
// accurate enough. In Gecko, we also use "double" to compute the triangle functions.
// Therefore, let's use Transform3D<f64> during matrix computation and cast it into f32
// in the end.
// We intentionally use Transform3D<f64> during computation to avoid
// error propagation because using f32 to compute triangle functions
// (e.g. in rotation()) is not accurate enough. In Gecko, we also use
// "double" to compute the triangle functions. Therefore, let's use
// Transform3D<f64> during matrix computation and cast it into f32 in
// the end.
let mut transform = Transform3D::<f64>::identity();
let mut contain_3d = false;
for operation in &*self.0 {
for operation in ops {
let matrix = operation.to_3d_matrix(reference_box)?;
contain_3d |= operation.is_3d();
contain_3d = contain_3d || operation.is_3d();
transform = matrix.then(&transform);
}