Box gradients and rects in Image. r=xidorn

Gradients and rects are rare, and large.  Image is much smaller with them boxed.

This is part of of the fix for Gecko bug 1397614
<https://bugzilla.mozilla.org/show_bug.cgi?id=1397614>
This commit is contained in:
Boris Zbarsky 2017-09-09 04:31:25 -04:00
parent 91b748ec22
commit 26b39241f9
4 changed files with 65 additions and 13 deletions

View file

@ -11,20 +11,21 @@ use cssparser::serialize_identifier;
use custom_properties::SpecifiedValue;
use std::fmt;
use style_traits::ToCss;
use values::computed::ComputedValueAsSpecified;
use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue};
/// An [image].
///
/// [image]: https://drafts.csswg.org/css-images/#image-values
#[derive(Clone, PartialEq, ToComputedValue)]
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum Image<Gradient, MozImageRect, ImageUrl> {
/// A `<url()>` image.
Url(ImageUrl),
/// A `<gradient>` image.
Gradient(Gradient),
/// A `-moz-image-rect` image
Rect(MozImageRect),
/// A `<gradient>` image. Gradients are rather large, and not nearly as
/// common as urls, so we box them here to keep the size of this enum sane.
Gradient(Box<Gradient>),
/// A `-moz-image-rect` image. Also fairly large and rare.
Rect(Box<MozImageRect>),
/// A `-moz-element(# <element-id>)`
Element(Atom),
/// A paint worklet image.
@ -33,6 +34,47 @@ pub enum Image<Gradient, MozImageRect, ImageUrl> {
PaintWorklet(PaintWorklet),
}
// Can't just use derive(ToComputedValue) on Image, because when trying to do
// "impl<T> ToComputedValue for Box<T>" the Rust compiler complains that
// "impl<T> ToComputedValue for T where T: ComputedValueAsSpecified + Clone"
// aleady implements ToComputedValue for std::boxed::Box<_> and hence we have
// conflicting implementations.
impl<Gradient: ToComputedValue,
MozImageRect: ToComputedValue,
ImageUrl: ToComputedValue> ToComputedValue for Image<Gradient, MozImageRect, ImageUrl> {
type ComputedValue = Image<<Gradient as ToComputedValue>::ComputedValue,
<MozImageRect as ToComputedValue>::ComputedValue,
<ImageUrl as ToComputedValue>::ComputedValue>;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self {
Image::Url(ref url) => Image::Url(url.to_computed_value(context)),
Image::Gradient(ref gradient) =>
Image::Gradient(Box::new(gradient.to_computed_value(context))),
Image::Rect(ref rect) => Image::Rect(Box::new(rect.to_computed_value(context))),
Image::Element(ref atom) => Image::Element(atom.to_computed_value(context)),
#[cfg(feature = "servo")]
Image::PaintWorklet(ref worklet) => Image::PaintWorklet(worklet.to_computed_value(context)),
}
}
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
match *computed {
Image::Url(ref url) => Image::Url(ImageUrl::from_computed_value(url)),
Image::Gradient(ref boxed_gradient) =>
Image::Gradient(Box::new(Gradient::from_computed_value(&*boxed_gradient))),
Image::Rect(ref boxed_rect) =>
Image::Rect(Box::new(MozImageRect::from_computed_value(&*boxed_rect))),
Image::Element(ref atom) => Image::Element(Atom::from_computed_value(atom)),
#[cfg(feature = "servo")]
Image::PaintWorklet(ref worklet) =>
Image::PaintWorklet(PaintWorklet::from_computed_value(worklet)),
}
}
}
/// A CSS gradient.
/// https://drafts.csswg.org/css-images/#gradients
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]