mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
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:
parent
6cb3b7e254
commit
080b3f8d1a
2 changed files with 83 additions and 65 deletions
|
@ -891,25 +891,8 @@ impl Animate for ComputedTransform {
|
||||||
match (this_remainder, other_remainder) {
|
match (this_remainder, other_remainder) {
|
||||||
// If there is a remainder from *both* lists we must have had mismatched functions.
|
// If there is a remainder from *both* lists we must have had mismatched functions.
|
||||||
// => Add the remainders to a suitable ___Matrix function.
|
// => Add the remainders to a suitable ___Matrix function.
|
||||||
(Some(this_remainder), Some(other_remainder)) => match procedure {
|
(Some(this_remainder), Some(other_remainder)) => {
|
||||||
Procedure::Add => {
|
result.push(TransformOperation::animate_mismatched_transforms(this_remainder, other_remainder, procedure)?);
|
||||||
debug_assert!(false, "Should have already dealt with add by the point");
|
|
||||||
return Err(());
|
|
||||||
},
|
|
||||||
Procedure::Interpolate { progress } => {
|
|
||||||
result.push(TransformOperation::InterpolateMatrix {
|
|
||||||
from_list: Transform(this_remainder.to_vec().into()),
|
|
||||||
to_list: Transform(other_remainder.to_vec().into()),
|
|
||||||
progress: Percentage(progress as f32),
|
|
||||||
});
|
|
||||||
},
|
|
||||||
Procedure::Accumulate { count } => {
|
|
||||||
result.push(TransformOperation::AccumulateMatrix {
|
|
||||||
from_list: Transform(this_remainder.to_vec().into()),
|
|
||||||
to_list: Transform(other_remainder.to_vec().into()),
|
|
||||||
count: cmp::min(count, i32::max_value() as u64) as i32,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
// If there is a remainder from just one list, then one list must be shorter but
|
// If there is a remainder from just one list, then one list must be shorter but
|
||||||
// completely match the type of the corresponding functions in the longer list.
|
// completely match the type of the corresponding functions in the longer list.
|
||||||
|
@ -923,36 +906,19 @@ impl Animate for ComputedTransform {
|
||||||
let identity = transform.to_animated_zero().unwrap();
|
let identity = transform.to_animated_zero().unwrap();
|
||||||
|
|
||||||
match transform {
|
match transform {
|
||||||
// We can't interpolate/accumulate ___Matrix types directly with a
|
|
||||||
// matrix. Instead we need to wrap it in another ___Matrix type.
|
|
||||||
TransformOperation::AccumulateMatrix { .. } |
|
TransformOperation::AccumulateMatrix { .. } |
|
||||||
TransformOperation::InterpolateMatrix { .. } => {
|
TransformOperation::InterpolateMatrix { .. } => {
|
||||||
let transform_list = Transform(vec![transform.clone()].into());
|
let (from, to) = if fill_right {
|
||||||
let identity_list = Transform(vec![identity].into());
|
(transform, &identity)
|
||||||
let (from_list, to_list) = if fill_right {
|
|
||||||
(transform_list, identity_list)
|
|
||||||
} else {
|
} else {
|
||||||
(identity_list, transform_list)
|
(&identity, transform)
|
||||||
};
|
};
|
||||||
|
|
||||||
match procedure {
|
TransformOperation::animate_mismatched_transforms(
|
||||||
Procedure::Add => Err(()),
|
&[from.clone()],
|
||||||
Procedure::Interpolate { progress } => {
|
&[to.clone()],
|
||||||
Ok(TransformOperation::InterpolateMatrix {
|
procedure,
|
||||||
from_list,
|
)
|
||||||
to_list,
|
|
||||||
progress: Percentage(progress as f32),
|
|
||||||
})
|
|
||||||
},
|
|
||||||
Procedure::Accumulate { count } => {
|
|
||||||
Ok(TransformOperation::AccumulateMatrix {
|
|
||||||
from_list,
|
|
||||||
to_list,
|
|
||||||
count: cmp::min(count, i32::max_value() as u64)
|
|
||||||
as i32,
|
|
||||||
})
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
let (lhs, rhs) = if fill_right {
|
let (lhs, rhs) = if fill_right {
|
||||||
|
@ -981,9 +947,13 @@ impl ComputeSquaredDistance for ComputedTransform {
|
||||||
|
|
||||||
// Roll back to matrix interpolation if there is any Err(()) in the
|
// Roll back to matrix interpolation if there is any Err(()) in the
|
||||||
// transform lists, such as mismatched transform functions.
|
// transform lists, such as mismatched transform functions.
|
||||||
|
//
|
||||||
|
// FIXME: Using a zero size here seems a bit sketchy but matches the
|
||||||
|
// previous behavior.
|
||||||
if squared_dist.is_err() {
|
if squared_dist.is_err() {
|
||||||
let matrix1: Matrix3D = self.to_transform_3d_matrix(None)?.0.into();
|
let rect = euclid::Rect::zero();
|
||||||
let matrix2: Matrix3D = other.to_transform_3d_matrix(None)?.0.into();
|
let matrix1: Matrix3D = self.to_transform_3d_matrix(Some(&rect))?.0.into();
|
||||||
|
let matrix2: Matrix3D = other.to_transform_3d_matrix(Some(&rect))?.0.into();
|
||||||
return matrix1.compute_squared_distance(&matrix2);
|
return matrix1.compute_squared_distance(&matrix2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1141,6 +1111,52 @@ impl Animate for ComputedTransformOperation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ComputedTransformOperation {
|
||||||
|
/// If there are no size dependencies, we try to animate in-place, to avoid
|
||||||
|
/// creating deeply nested Interpolate* operations.
|
||||||
|
fn try_animate_mismatched_transforms_in_place(
|
||||||
|
left: &[Self],
|
||||||
|
right: &[Self],
|
||||||
|
procedure: Procedure,
|
||||||
|
) -> Result<Self, ()> {
|
||||||
|
let (left, _left_3d) = Transform::components_to_transform_3d_matrix(left, None)?;
|
||||||
|
let (right, _right_3d) = Transform::components_to_transform_3d_matrix(right, None)?;
|
||||||
|
ComputedTransformOperation::Matrix3D(left.into()).animate(&ComputedTransformOperation::Matrix3D(right.into()), procedure)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn animate_mismatched_transforms(
|
||||||
|
left: &[Self],
|
||||||
|
right: &[Self],
|
||||||
|
procedure: Procedure,
|
||||||
|
) -> Result<Self, ()> {
|
||||||
|
if let Ok(op) = Self::try_animate_mismatched_transforms_in_place(left, right, procedure) {
|
||||||
|
return Ok(op);
|
||||||
|
}
|
||||||
|
let from_list = Transform(left.to_vec().into());
|
||||||
|
let to_list = Transform(right.to_vec().into());
|
||||||
|
Ok(match procedure {
|
||||||
|
Procedure::Add => {
|
||||||
|
debug_assert!(false, "Addition should've been handled earlier");
|
||||||
|
return Err(())
|
||||||
|
},
|
||||||
|
Procedure::Interpolate { progress } => {
|
||||||
|
Self::InterpolateMatrix {
|
||||||
|
from_list,
|
||||||
|
to_list,
|
||||||
|
progress: Percentage(progress as f32),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Procedure::Accumulate { count } => {
|
||||||
|
Self::AccumulateMatrix {
|
||||||
|
from_list,
|
||||||
|
to_list,
|
||||||
|
count: cmp::min(count, i32::max_value() as u64) as i32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This might not be the most useful definition of distance. It might be better, for example,
|
// This might not be the most useful definition of distance. It might be better, for example,
|
||||||
// to trace the distance travelled by a point as its transform is interpolated between the two
|
// to trace the distance travelled by a point as its transform is interpolated between the two
|
||||||
// lists. That, however, proves to be quite complicated so we take a simple approach for now.
|
// lists. That, however, proves to be quite complicated so we take a simple approach for now.
|
||||||
|
|
|
@ -404,15 +404,7 @@ impl ToAbsoluteLength for ComputedLength {
|
||||||
impl ToAbsoluteLength for ComputedLengthPercentage {
|
impl ToAbsoluteLength for ComputedLengthPercentage {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_pixel_length(&self, containing_len: Option<ComputedLength>) -> Result<CSSFloat, ()> {
|
fn to_pixel_length(&self, containing_len: Option<ComputedLength>) -> Result<CSSFloat, ()> {
|
||||||
match containing_len {
|
Ok(self.maybe_percentage_relative_to(containing_len).ok_or(())?.px())
|
||||||
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()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -572,12 +564,21 @@ impl<T> Transform<T> {
|
||||||
|
|
||||||
impl<T: ToMatrix> Transform<T> {
|
impl<T: ToMatrix> Transform<T> {
|
||||||
/// Return the equivalent 3d matrix of this transform list.
|
/// Return the equivalent 3d matrix of this transform list.
|
||||||
|
///
|
||||||
/// We return a pair: the first one is the transform matrix, and the second one
|
/// 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.
|
/// indicates if there is any 3d transform function in this transform list.
|
||||||
#[cfg_attr(rustfmt, rustfmt_skip)]
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
||||||
pub fn to_transform_3d_matrix(
|
pub fn to_transform_3d_matrix(
|
||||||
&self,
|
&self,
|
||||||
reference_box: Option<&Rect<ComputedLength>>
|
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), ()> {
|
) -> Result<(Transform3D<CSSFloat>, bool), ()> {
|
||||||
let cast_3d_transform = |m: Transform3D<f64>| -> Transform3D<CSSFloat> {
|
let cast_3d_transform = |m: Transform3D<f64>| -> Transform3D<CSSFloat> {
|
||||||
use std::{f32, f64};
|
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))
|
Ok((cast_3d_transform(m), is_3d))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as Transform::to_transform_3d_matrix but a f64 version.
|
/// Same as Transform::to_transform_3d_matrix but a f64 version.
|
||||||
pub fn to_transform_3d_matrix_f64(
|
fn components_to_transform_3d_matrix_f64(
|
||||||
&self,
|
ops: &[T],
|
||||||
reference_box: Option<&Rect<ComputedLength>>,
|
reference_box: Option<&Rect<ComputedLength>>,
|
||||||
) -> Result<(Transform3D<f64>, bool), ()> {
|
) -> Result<(Transform3D<f64>, bool), ()> {
|
||||||
// We intentionally use Transform3D<f64> during computation to avoid error propagation
|
// We intentionally use Transform3D<f64> during computation to avoid
|
||||||
// because using f32 to compute triangle functions (e.g. in rotation()) is not
|
// error propagation because using f32 to compute triangle functions
|
||||||
// accurate enough. In Gecko, we also use "double" to compute the triangle functions.
|
// (e.g. in rotation()) is not accurate enough. In Gecko, we also use
|
||||||
// Therefore, let's use Transform3D<f64> during matrix computation and cast it into f32
|
// "double" to compute the triangle functions. Therefore, let's use
|
||||||
// in the end.
|
// Transform3D<f64> during matrix computation and cast it into f32 in
|
||||||
|
// the end.
|
||||||
let mut transform = Transform3D::<f64>::identity();
|
let mut transform = Transform3D::<f64>::identity();
|
||||||
let mut contain_3d = false;
|
let mut contain_3d = false;
|
||||||
|
|
||||||
for operation in &*self.0 {
|
for operation in ops {
|
||||||
let matrix = operation.to_3d_matrix(reference_box)?;
|
let matrix = operation.to_3d_matrix(reference_box)?;
|
||||||
contain_3d |= operation.is_3d();
|
contain_3d = contain_3d || operation.is_3d();
|
||||||
transform = matrix.then(&transform);
|
transform = matrix.then(&transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue