diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /gfx/wr/webrender/src/prim_store | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gfx/wr/webrender/src/prim_store')
-rw-r--r-- | gfx/wr/webrender/src/prim_store/backdrop.rs | 98 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/borders.rs | 376 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/gradient.rs | 1031 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/image.rs | 521 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/interned.rs | 14 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/line_dec.rs | 257 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/mod.rs | 1364 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/picture.rs | 322 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/storage.rs | 134 | ||||
-rw-r--r-- | gfx/wr/webrender/src/prim_store/text_run.rs | 492 |
10 files changed, 4609 insertions, 0 deletions
diff --git a/gfx/wr/webrender/src/prim_store/backdrop.rs b/gfx/wr/webrender/src/prim_store/backdrop.rs new file mode 100644 index 0000000000..c45bf78eef --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/backdrop.rs @@ -0,0 +1,98 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::units::*; +use crate::spatial_tree::SpatialNodeIndex; +use crate::intern::{Internable, InternDebug, Handle as InternHandle}; +use crate::internal_types::LayoutPrimitiveInfo; +use crate::prim_store::{ + InternablePrimitive, PictureIndex, PrimitiveInstanceKind, PrimKey, PrimTemplate, + PrimTemplateCommonData, PrimitiveStore, RectangleKey, +}; + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, PartialEq, MallocSizeOf, Hash)] +pub struct Backdrop { + pub pic_index: PictureIndex, + pub spatial_node_index: SpatialNodeIndex, + pub border_rect: RectangleKey, +} + +impl From<Backdrop> for BackdropData { + fn from(backdrop: Backdrop) -> Self { + BackdropData { + pic_index: backdrop.pic_index, + spatial_node_index: backdrop.spatial_node_index, + border_rect: backdrop.border_rect.into(), + } + } +} + +pub type BackdropKey = PrimKey<Backdrop>; + +impl BackdropKey { + pub fn new( + info: &LayoutPrimitiveInfo, + backdrop: Backdrop, + ) -> Self { + BackdropKey { + common: info.into(), + kind: backdrop, + } + } +} + +impl InternDebug for BackdropKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, MallocSizeOf)] +pub struct BackdropData { + pub pic_index: PictureIndex, + pub spatial_node_index: SpatialNodeIndex, + pub border_rect: LayoutRect, +} + +pub type BackdropTemplate = PrimTemplate<BackdropData>; + +impl From<BackdropKey> for BackdropTemplate { + fn from(backdrop: BackdropKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(backdrop.common); + + BackdropTemplate { + common, + kind: backdrop.kind.into(), + } + } +} + +pub type BackdropDataHandle = InternHandle<Backdrop>; + +impl Internable for Backdrop { + type Key = BackdropKey; + type StoreData = BackdropTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_BACKDROPS; +} + +impl InternablePrimitive for Backdrop { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> BackdropKey { + BackdropKey::new(info, self) + } + + fn make_instance_kind( + _key: BackdropKey, + data_handle: BackdropDataHandle, + _prim_store: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + PrimitiveInstanceKind::Backdrop { + data_handle, + } + } +} diff --git a/gfx/wr/webrender/src/prim_store/borders.rs b/gfx/wr/webrender/src/prim_store/borders.rs new file mode 100644 index 0000000000..561f4a8ada --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/borders.rs @@ -0,0 +1,376 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::{NormalBorder, PremultipliedColorF, Shadow, RasterSpace}; +use api::units::*; +use crate::border::create_border_segments; +use crate::border::NormalBorderAu; +use crate::scene_building::{CreateShadow, IsVisible}; +use crate::frame_builder::{FrameBuildingState}; +use crate::gpu_cache::{GpuCache, GpuDataRequest}; +use crate::intern; +use crate::internal_types::LayoutPrimitiveInfo; +use crate::prim_store::{ + BorderSegmentInfo, BrushSegment, NinePatchDescriptor, PrimKey, + PrimTemplate, PrimTemplateCommonData, + PrimitiveInstanceKind, PrimitiveOpacity, + PrimitiveStore, InternablePrimitive, +}; +use crate::resource_cache::{ImageRequest, ResourceCache}; + +use super::storage; + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct NormalBorderPrim { + pub border: NormalBorderAu, + pub widths: LayoutSideOffsetsAu, +} + +pub type NormalBorderKey = PrimKey<NormalBorderPrim>; + +impl NormalBorderKey { + pub fn new( + info: &LayoutPrimitiveInfo, + normal_border: NormalBorderPrim, + ) -> Self { + NormalBorderKey { + common: info.into(), + kind: normal_border, + } + } +} + +impl intern::InternDebug for NormalBorderKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct NormalBorderData { + pub brush_segments: Vec<BrushSegment>, + pub border_segments: Vec<BorderSegmentInfo>, + pub border: NormalBorder, + pub widths: LayoutSideOffsets, +} + +impl NormalBorderData { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + common: &mut PrimTemplateCommonData, + frame_state: &mut FrameBuildingState, + ) { + if let Some(ref mut request) = frame_state.gpu_cache.request(&mut common.gpu_cache_handle) { + self.write_prim_gpu_blocks(request, common.prim_rect.size); + self.write_segment_gpu_blocks(request); + } + + common.opacity = PrimitiveOpacity::translucent(); + } + + fn write_prim_gpu_blocks( + &self, + request: &mut GpuDataRequest, + prim_size: LayoutSize + ) { + // Border primitives currently used for + // image borders, and run through the + // normal brush_image shader. + request.push(PremultipliedColorF::WHITE); + request.push(PremultipliedColorF::WHITE); + request.push([ + prim_size.width, + prim_size.height, + 0.0, + 0.0, + ]); + } + + fn write_segment_gpu_blocks( + &self, + request: &mut GpuDataRequest, + ) { + for segment in &self.brush_segments { + // has to match VECS_PER_SEGMENT + request.write_segment( + segment.local_rect, + segment.extra_data, + ); + } + } +} + +pub type NormalBorderTemplate = PrimTemplate<NormalBorderData>; + +impl From<NormalBorderKey> for NormalBorderTemplate { + fn from(key: NormalBorderKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(key.common); + + let mut border: NormalBorder = key.kind.border.into(); + let widths = LayoutSideOffsets::from_au(key.kind.widths); + + // FIXME(emilio): Is this the best place to do this? + border.normalize(&widths); + + let mut brush_segments = Vec::new(); + let mut border_segments = Vec::new(); + + create_border_segments( + common.prim_rect.size, + &border, + &widths, + &mut border_segments, + &mut brush_segments, + ); + + NormalBorderTemplate { + common, + kind: NormalBorderData { + brush_segments, + border_segments, + border, + widths, + } + } + } +} + +pub type NormalBorderDataHandle = intern::Handle<NormalBorderPrim>; + +impl intern::Internable for NormalBorderPrim { + type Key = NormalBorderKey; + type StoreData = NormalBorderTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_NORMAL_BORDERS; +} + +impl InternablePrimitive for NormalBorderPrim { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> NormalBorderKey { + NormalBorderKey::new( + info, + self, + ) + } + + fn make_instance_kind( + _key: NormalBorderKey, + data_handle: NormalBorderDataHandle, + _: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + PrimitiveInstanceKind::NormalBorder { + data_handle, + cache_handles: storage::Range::empty(), + } + } +} + +impl CreateShadow for NormalBorderPrim { + fn create_shadow( + &self, + shadow: &Shadow, + _: bool, + _: RasterSpace, + ) -> Self { + let border = self.border.with_color(shadow.color.into()); + NormalBorderPrim { + border, + widths: self.widths, + } + } +} + +impl IsVisible for NormalBorderPrim { + fn is_visible(&self) -> bool { + true + } +} + +//////////////////////////////////////////////////////////////////////////////// + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct ImageBorder { + #[ignore_malloc_size_of = "Arc"] + pub request: ImageRequest, + pub nine_patch: NinePatchDescriptor, +} + +pub type ImageBorderKey = PrimKey<ImageBorder>; + +impl ImageBorderKey { + pub fn new( + info: &LayoutPrimitiveInfo, + image_border: ImageBorder, + ) -> Self { + ImageBorderKey { + common: info.into(), + kind: image_border, + } + } +} + +impl intern::InternDebug for ImageBorderKey {} + + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct ImageBorderData { + #[ignore_malloc_size_of = "Arc"] + pub request: ImageRequest, + pub brush_segments: Vec<BrushSegment>, +} + +impl ImageBorderData { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + common: &mut PrimTemplateCommonData, + frame_state: &mut FrameBuildingState, + ) { + if let Some(ref mut request) = frame_state.gpu_cache.request(&mut common.gpu_cache_handle) { + self.write_prim_gpu_blocks(request, &common.prim_rect.size); + self.write_segment_gpu_blocks(request); + } + + let image_properties = frame_state + .resource_cache + .get_image_properties(self.request.key); + + common.opacity = if let Some(image_properties) = image_properties { + PrimitiveOpacity { + is_opaque: image_properties.descriptor.is_opaque(), + } + } else { + PrimitiveOpacity::opaque() + } + } + + pub fn request_resources( + &mut self, + resource_cache: &mut ResourceCache, + gpu_cache: &mut GpuCache, + ) { + resource_cache.request_image( + self.request, + gpu_cache, + ); + } + + fn write_prim_gpu_blocks( + &self, + request: &mut GpuDataRequest, + prim_size: &LayoutSize, + ) { + // Border primitives currently used for + // image borders, and run through the + // normal brush_image shader. + request.push(PremultipliedColorF::WHITE); + request.push(PremultipliedColorF::WHITE); + request.push([ + prim_size.width, + prim_size.height, + 0.0, + 0.0, + ]); + } + + fn write_segment_gpu_blocks( + &self, + request: &mut GpuDataRequest, + ) { + for segment in &self.brush_segments { + // has to match VECS_PER_SEGMENT + request.write_segment( + segment.local_rect, + segment.extra_data, + ); + } + } +} + +pub type ImageBorderTemplate = PrimTemplate<ImageBorderData>; + +impl From<ImageBorderKey> for ImageBorderTemplate { + fn from(key: ImageBorderKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(key.common); + + let brush_segments = key.kind.nine_patch.create_segments(common.prim_rect.size); + ImageBorderTemplate { + common, + kind: ImageBorderData { + request: key.kind.request, + brush_segments, + } + } + } +} + +pub type ImageBorderDataHandle = intern::Handle<ImageBorder>; + +impl intern::Internable for ImageBorder { + type Key = ImageBorderKey; + type StoreData = ImageBorderTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_IMAGE_BORDERS; +} + +impl InternablePrimitive for ImageBorder { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> ImageBorderKey { + ImageBorderKey::new( + info, + self, + ) + } + + fn make_instance_kind( + _key: ImageBorderKey, + data_handle: ImageBorderDataHandle, + _: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + PrimitiveInstanceKind::ImageBorder { + data_handle + } + } +} + +impl IsVisible for ImageBorder { + fn is_visible(&self) -> bool { + true + } +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn test_struct_sizes() { + use std::mem; + // The sizes of these structures are critical for performance on a number of + // talos stress tests. If you get a failure here on CI, there's two possibilities: + // (a) You made a structure smaller than it currently is. Great work! Update the + // test expectations and move on. + // (b) You made a structure larger. This is not necessarily a problem, but should only + // be done with care, and after checking if talos performance regresses badly. + assert_eq!(mem::size_of::<NormalBorderPrim>(), 84, "NormalBorderPrim size changed"); + assert_eq!(mem::size_of::<NormalBorderTemplate>(), 216, "NormalBorderTemplate size changed"); + assert_eq!(mem::size_of::<NormalBorderKey>(), 104, "NormalBorderKey size changed"); + assert_eq!(mem::size_of::<ImageBorder>(), 84, "ImageBorder size changed"); + assert_eq!(mem::size_of::<ImageBorderTemplate>(), 80, "ImageBorderTemplate size changed"); + assert_eq!(mem::size_of::<ImageBorderKey>(), 104, "ImageBorderKey size changed"); +} diff --git a/gfx/wr/webrender/src/prim_store/gradient.rs b/gfx/wr/webrender/src/prim_store/gradient.rs new file mode 100644 index 0000000000..da623cf212 --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/gradient.rs @@ -0,0 +1,1031 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::{ + ColorF, ColorU, ExtendMode, GradientStop, + PremultipliedColorF, LineOrientation, +}; +use api::units::{LayoutPoint, LayoutRect, LayoutSize, LayoutVector2D}; +use crate::scene_building::IsVisible; +use euclid::approxeq::ApproxEq; +use crate::frame_builder::FrameBuildingState; +use crate::gpu_cache::{GpuCacheHandle, GpuDataRequest}; +use crate::intern::{Internable, InternDebug, Handle as InternHandle}; +use crate::internal_types::LayoutPrimitiveInfo; +use crate::prim_store::{BrushSegment, CachedGradientSegment, GradientTileRange, VectorKey}; +use crate::prim_store::{PrimitiveInstanceKind, PrimitiveOpacity}; +use crate::prim_store::{PrimKeyCommonData, PrimTemplateCommonData, PrimitiveStore}; +use crate::prim_store::{NinePatchDescriptor, PointKey, SizeKey, InternablePrimitive}; +use std::{hash, ops::{Deref, DerefMut}}; +use crate::util::pack_as_float; +use crate::texture_cache::TEXTURE_REGION_DIMENSIONS; + +/// The maximum number of stops a gradient may have to use the fast path. +pub const GRADIENT_FP_STOPS: usize = 4; + +/// A hashable gradient stop that can be used in primitive keys. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Copy, Clone, MallocSizeOf, PartialEq)] +pub struct GradientStopKey { + pub offset: f32, + pub color: ColorU, +} + +impl GradientStopKey { + pub fn empty() -> Self { + GradientStopKey { + offset: 0.0, + color: ColorU::new(0, 0, 0, 0), + } + } +} + +impl Into<GradientStopKey> for GradientStop { + fn into(self) -> GradientStopKey { + GradientStopKey { + offset: self.offset, + color: self.color.into(), + } + } +} + +// Convert `stop_keys` into a vector of `GradientStop`s, which is a more +// convenient representation for the current gradient builder. Compute the +// minimum stop alpha along the way. +fn stops_and_min_alpha(stop_keys: &[GradientStopKey]) -> (Vec<GradientStop>, f32) { + let mut min_alpha: f32 = 1.0; + let stops = stop_keys.iter().map(|stop_key| { + let color: ColorF = stop_key.color.into(); + min_alpha = min_alpha.min(color.a); + + GradientStop { + offset: stop_key.offset, + color, + } + }).collect(); + + (stops, min_alpha) +} + +impl Eq for GradientStopKey {} + +impl hash::Hash for GradientStopKey { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.offset.to_bits().hash(state); + self.color.hash(state); + } +} + +/// Identifying key for a linear gradient. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, PartialEq, Hash, MallocSizeOf)] +pub struct LinearGradientKey { + pub common: PrimKeyCommonData, + pub extend_mode: ExtendMode, + pub start_point: PointKey, + pub end_point: PointKey, + pub stretch_size: SizeKey, + pub tile_spacing: SizeKey, + pub stops: Vec<GradientStopKey>, + pub reverse_stops: bool, + pub nine_patch: Option<Box<NinePatchDescriptor>>, +} + +impl LinearGradientKey { + pub fn new( + info: &LayoutPrimitiveInfo, + linear_grad: LinearGradient, + ) -> Self { + LinearGradientKey { + common: info.into(), + extend_mode: linear_grad.extend_mode, + start_point: linear_grad.start_point, + end_point: linear_grad.end_point, + stretch_size: linear_grad.stretch_size, + tile_spacing: linear_grad.tile_spacing, + stops: linear_grad.stops, + reverse_stops: linear_grad.reverse_stops, + nine_patch: linear_grad.nine_patch, + } + } +} + +impl InternDebug for LinearGradientKey {} + +#[derive(Clone, Debug, Hash, MallocSizeOf, PartialEq, Eq)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct GradientCacheKey { + pub orientation: LineOrientation, + pub start_stop_point: VectorKey, + pub stops: [GradientStopKey; GRADIENT_FP_STOPS], +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct LinearGradientTemplate { + pub common: PrimTemplateCommonData, + pub extend_mode: ExtendMode, + pub start_point: LayoutPoint, + pub end_point: LayoutPoint, + pub stretch_size: LayoutSize, + pub tile_spacing: LayoutSize, + pub stops_opacity: PrimitiveOpacity, + pub stops: Vec<GradientStop>, + pub brush_segments: Vec<BrushSegment>, + pub reverse_stops: bool, + pub stops_handle: GpuCacheHandle, + /// If true, this gradient can be drawn via the fast path + /// (cache gradient, and draw as image). + pub supports_caching: bool, +} + +impl Deref for LinearGradientTemplate { + type Target = PrimTemplateCommonData; + fn deref(&self) -> &Self::Target { + &self.common + } +} + +impl DerefMut for LinearGradientTemplate { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.common + } +} + +impl From<LinearGradientKey> for LinearGradientTemplate { + fn from(item: LinearGradientKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(item.common); + + // Check if we can draw this gradient via a fast path by caching the + // gradient in a smaller task, and drawing as an image. + // TODO(gw): Aim to reduce the constraints on fast path gradients in future, + // although this catches the vast majority of gradients on real pages. + let mut supports_caching = + // Gradient must cover entire primitive + item.tile_spacing.w + item.stretch_size.w >= common.prim_rect.size.width && + item.tile_spacing.h + item.stretch_size.h >= common.prim_rect.size.height && + // Must be a vertical or horizontal gradient + (item.start_point.x.approx_eq(&item.end_point.x) || + item.start_point.y.approx_eq(&item.end_point.y)) && + // Fast path not supported on segmented (border-image) gradients. + item.nine_patch.is_none(); + + // if we support caching and the gradient uses repeat, we might potentially + // emit a lot of quads to cover the primitive. each quad will still cover + // the entire gradient along the other axis, so the effect is linear in + // display resolution, not quadratic (unlike say a tiny background image + // tiling the display). in addition, excessive minification may lead to + // texture trashing. so use the minification as a proxy heuristic for both + // cases. + // + // note that the actual number of quads may be further increased due to + // hard-stops and/or more than GRADIENT_FP_STOPS stops per gradient. + if supports_caching && item.extend_mode == ExtendMode::Repeat { + let single_repeat_size = + if item.start_point.x.approx_eq(&item.end_point.x) { + item.end_point.y - item.start_point.y + } else { + item.end_point.x - item.start_point.x + }; + let downscaling = single_repeat_size as f32 / TEXTURE_REGION_DIMENSIONS as f32; + if downscaling < 0.1 { + // if a single copy of the gradient is this small relative to its baked + // gradient cache, we have bad texture caching and/or too many quads. + supports_caching = false; + } + } + + let (stops, min_alpha) = stops_and_min_alpha(&item.stops); + + let mut brush_segments = Vec::new(); + + if let Some(ref nine_patch) = item.nine_patch { + brush_segments = nine_patch.create_segments(common.prim_rect.size); + } + + // Save opacity of the stops for use in + // selecting which pass this gradient + // should be drawn in. + let stops_opacity = PrimitiveOpacity::from_alpha(min_alpha); + + LinearGradientTemplate { + common, + extend_mode: item.extend_mode, + start_point: item.start_point.into(), + end_point: item.end_point.into(), + stretch_size: item.stretch_size.into(), + tile_spacing: item.tile_spacing.into(), + stops_opacity, + stops, + brush_segments, + reverse_stops: item.reverse_stops, + stops_handle: GpuCacheHandle::new(), + supports_caching, + } + } +} + +fn get_gradient_opacity( + prim_rect: LayoutRect, + stretch_size: LayoutSize, + tile_spacing: LayoutSize, + stops_opacity: PrimitiveOpacity, +) -> PrimitiveOpacity { + // If the coverage of the gradient extends to or beyond + // the primitive rect, then the opacity can be determined + // by the colors of the stops. If we have tiling / spacing + // then we just assume the gradient is translucent for now. + // (In the future we could consider segmenting in some cases). + let stride = stretch_size + tile_spacing; + if stride.width >= prim_rect.size.width && stride.height >= prim_rect.size.height { + stops_opacity + } else { + PrimitiveOpacity::translucent() + } +} + +impl LinearGradientTemplate { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + frame_state: &mut FrameBuildingState, + ) { + if let Some(mut request) = + frame_state.gpu_cache.request(&mut self.common.gpu_cache_handle) { + // write_prim_gpu_blocks + request.push([ + self.start_point.x, + self.start_point.y, + self.end_point.x, + self.end_point.y, + ]); + request.push([ + pack_as_float(self.extend_mode as u32), + self.stretch_size.width, + self.stretch_size.height, + 0.0, + ]); + + // write_segment_gpu_blocks + for segment in &self.brush_segments { + // has to match VECS_PER_SEGMENT + request.write_segment( + segment.local_rect, + segment.extra_data, + ); + } + } + + if let Some(mut request) = frame_state.gpu_cache.request(&mut self.stops_handle) { + GradientGpuBlockBuilder::build( + self.reverse_stops, + &mut request, + &self.stops, + ); + } + + self.opacity = get_gradient_opacity( + self.common.prim_rect, + self.stretch_size, + self.tile_spacing, + self.stops_opacity, + ); + } +} + +pub type LinearGradientDataHandle = InternHandle<LinearGradient>; + +#[derive(Debug, MallocSizeOf)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct LinearGradient { + pub extend_mode: ExtendMode, + pub start_point: PointKey, + pub end_point: PointKey, + pub stretch_size: SizeKey, + pub tile_spacing: SizeKey, + pub stops: Vec<GradientStopKey>, + pub reverse_stops: bool, + pub nine_patch: Option<Box<NinePatchDescriptor>>, +} + +impl Internable for LinearGradient { + type Key = LinearGradientKey; + type StoreData = LinearGradientTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_LINEAR_GRADIENTS; +} + +impl InternablePrimitive for LinearGradient { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> LinearGradientKey { + LinearGradientKey::new(info, self) + } + + fn make_instance_kind( + _key: LinearGradientKey, + data_handle: LinearGradientDataHandle, + prim_store: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + let gradient_index = prim_store.linear_gradients.push(LinearGradientPrimitive { + cache_segments: Vec::new(), + visible_tiles_range: GradientTileRange::empty(), + }); + + PrimitiveInstanceKind::LinearGradient { + data_handle, + gradient_index, + } + } +} + +impl IsVisible for LinearGradient { + fn is_visible(&self) -> bool { + true + } +} + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct LinearGradientPrimitive { + pub cache_segments: Vec<CachedGradientSegment>, + pub visible_tiles_range: GradientTileRange, +} + +//////////////////////////////////////////////////////////////////////////////// + +/// Hashable radial gradient parameters, for use during prim interning. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, MallocSizeOf, PartialEq)] +pub struct RadialGradientParams { + pub start_radius: f32, + pub end_radius: f32, + pub ratio_xy: f32, +} + +impl Eq for RadialGradientParams {} + +impl hash::Hash for RadialGradientParams { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.start_radius.to_bits().hash(state); + self.end_radius.to_bits().hash(state); + self.ratio_xy.to_bits().hash(state); + } +} + +/// Identifying key for a radial gradient. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, PartialEq, Hash, MallocSizeOf)] +pub struct RadialGradientKey { + pub common: PrimKeyCommonData, + pub extend_mode: ExtendMode, + pub center: PointKey, + pub params: RadialGradientParams, + pub stretch_size: SizeKey, + pub stops: Vec<GradientStopKey>, + pub tile_spacing: SizeKey, + pub nine_patch: Option<Box<NinePatchDescriptor>>, +} + +impl RadialGradientKey { + pub fn new( + info: &LayoutPrimitiveInfo, + radial_grad: RadialGradient, + ) -> Self { + RadialGradientKey { + common: info.into(), + extend_mode: radial_grad.extend_mode, + center: radial_grad.center, + params: radial_grad.params, + stretch_size: radial_grad.stretch_size, + stops: radial_grad.stops, + tile_spacing: radial_grad.tile_spacing, + nine_patch: radial_grad.nine_patch, + } + } +} + +impl InternDebug for RadialGradientKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct RadialGradientTemplate { + pub common: PrimTemplateCommonData, + pub extend_mode: ExtendMode, + pub center: LayoutPoint, + pub params: RadialGradientParams, + pub stretch_size: LayoutSize, + pub tile_spacing: LayoutSize, + pub brush_segments: Vec<BrushSegment>, + pub stops_opacity: PrimitiveOpacity, + pub stops: Vec<GradientStop>, + pub stops_handle: GpuCacheHandle, +} + +impl Deref for RadialGradientTemplate { + type Target = PrimTemplateCommonData; + fn deref(&self) -> &Self::Target { + &self.common + } +} + +impl DerefMut for RadialGradientTemplate { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.common + } +} + +impl From<RadialGradientKey> for RadialGradientTemplate { + fn from(item: RadialGradientKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(item.common); + let mut brush_segments = Vec::new(); + + if let Some(ref nine_patch) = item.nine_patch { + brush_segments = nine_patch.create_segments(common.prim_rect.size); + } + + let (stops, min_alpha) = stops_and_min_alpha(&item.stops); + + // Save opacity of the stops for use in + // selecting which pass this gradient + // should be drawn in. + let stops_opacity = PrimitiveOpacity::from_alpha(min_alpha); + + RadialGradientTemplate { + common, + center: item.center.into(), + extend_mode: item.extend_mode, + params: item.params, + stretch_size: item.stretch_size.into(), + tile_spacing: item.tile_spacing.into(), + brush_segments, + stops_opacity, + stops, + stops_handle: GpuCacheHandle::new(), + } + } +} + +impl RadialGradientTemplate { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + frame_state: &mut FrameBuildingState, + ) { + if let Some(mut request) = + frame_state.gpu_cache.request(&mut self.common.gpu_cache_handle) { + // write_prim_gpu_blocks + request.push([ + self.center.x, + self.center.y, + self.params.start_radius, + self.params.end_radius, + ]); + request.push([ + self.params.ratio_xy, + pack_as_float(self.extend_mode as u32), + self.stretch_size.width, + self.stretch_size.height, + ]); + + // write_segment_gpu_blocks + for segment in &self.brush_segments { + // has to match VECS_PER_SEGMENT + request.write_segment( + segment.local_rect, + segment.extra_data, + ); + } + } + + if let Some(mut request) = frame_state.gpu_cache.request(&mut self.stops_handle) { + GradientGpuBlockBuilder::build( + false, + &mut request, + &self.stops, + ); + } + + self.opacity = get_gradient_opacity( + self.common.prim_rect, + self.stretch_size, + self.tile_spacing, + self.stops_opacity, + ); + } +} + +pub type RadialGradientDataHandle = InternHandle<RadialGradient>; + +#[derive(Debug, MallocSizeOf)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct RadialGradient { + pub extend_mode: ExtendMode, + pub center: PointKey, + pub params: RadialGradientParams, + pub stretch_size: SizeKey, + pub stops: Vec<GradientStopKey>, + pub tile_spacing: SizeKey, + pub nine_patch: Option<Box<NinePatchDescriptor>>, +} + +impl Internable for RadialGradient { + type Key = RadialGradientKey; + type StoreData = RadialGradientTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_RADIAL_GRADIENTS; +} + +impl InternablePrimitive for RadialGradient { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> RadialGradientKey { + RadialGradientKey::new(info, self) + } + + fn make_instance_kind( + _key: RadialGradientKey, + data_handle: RadialGradientDataHandle, + _prim_store: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + PrimitiveInstanceKind::RadialGradient { + data_handle, + visible_tiles_range: GradientTileRange::empty(), + } + } +} + +impl IsVisible for RadialGradient { + fn is_visible(&self) -> bool { + true + } +} + +//////////////////////////////////////////////////////////////////////////////// + +/// Conic gradients + +/// Hashable conic gradient parameters, for use during prim interning. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, MallocSizeOf, PartialEq)] +pub struct ConicGradientParams { + pub angle: f32, // in radians + pub start_offset: f32, + pub end_offset: f32, +} + +impl Eq for ConicGradientParams {} + +impl hash::Hash for ConicGradientParams { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.angle.to_bits().hash(state); + self.start_offset.to_bits().hash(state); + self.end_offset.to_bits().hash(state); + } +} + +/// Identifying key for a line decoration. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, PartialEq, Hash, MallocSizeOf)] +pub struct ConicGradientKey { + pub common: PrimKeyCommonData, + pub extend_mode: ExtendMode, + pub center: PointKey, + pub params: ConicGradientParams, + pub stretch_size: SizeKey, + pub stops: Vec<GradientStopKey>, + pub tile_spacing: SizeKey, + pub nine_patch: Option<Box<NinePatchDescriptor>>, +} + +impl ConicGradientKey { + pub fn new( + info: &LayoutPrimitiveInfo, + conic_grad: ConicGradient, + ) -> Self { + ConicGradientKey { + common: info.into(), + extend_mode: conic_grad.extend_mode, + center: conic_grad.center, + params: conic_grad.params, + stretch_size: conic_grad.stretch_size, + stops: conic_grad.stops, + tile_spacing: conic_grad.tile_spacing, + nine_patch: conic_grad.nine_patch, + } + } +} + +impl InternDebug for ConicGradientKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct ConicGradientTemplate { + pub common: PrimTemplateCommonData, + pub extend_mode: ExtendMode, + pub center: LayoutPoint, + pub params: ConicGradientParams, + pub stretch_size: LayoutSize, + pub tile_spacing: LayoutSize, + pub brush_segments: Vec<BrushSegment>, + pub stops_opacity: PrimitiveOpacity, + pub stops: Vec<GradientStop>, + pub stops_handle: GpuCacheHandle, +} + +impl Deref for ConicGradientTemplate { + type Target = PrimTemplateCommonData; + fn deref(&self) -> &Self::Target { + &self.common + } +} + +impl DerefMut for ConicGradientTemplate { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.common + } +} + +impl From<ConicGradientKey> for ConicGradientTemplate { + fn from(item: ConicGradientKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(item.common); + let mut brush_segments = Vec::new(); + + if let Some(ref nine_patch) = item.nine_patch { + brush_segments = nine_patch.create_segments(common.prim_rect.size); + } + + let (stops, min_alpha) = stops_and_min_alpha(&item.stops); + + // Save opacity of the stops for use in + // selecting which pass this gradient + // should be drawn in. + let stops_opacity = PrimitiveOpacity::from_alpha(min_alpha); + + ConicGradientTemplate { + common, + center: item.center.into(), + extend_mode: item.extend_mode, + params: item.params, + stretch_size: item.stretch_size.into(), + tile_spacing: item.tile_spacing.into(), + brush_segments, + stops_opacity, + stops, + stops_handle: GpuCacheHandle::new(), + } + } +} + +impl ConicGradientTemplate { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + frame_state: &mut FrameBuildingState, + ) { + if let Some(mut request) = + frame_state.gpu_cache.request(&mut self.common.gpu_cache_handle) { + // write_prim_gpu_blocks + request.push([ + self.center.x, + self.center.y, + self.params.start_offset, + self.params.end_offset, + ]); + request.push([ + self.params.angle, + pack_as_float(self.extend_mode as u32), + self.stretch_size.width, + self.stretch_size.height, + ]); + + // write_segment_gpu_blocks + for segment in &self.brush_segments { + // has to match VECS_PER_SEGMENT + request.write_segment( + segment.local_rect, + segment.extra_data, + ); + } + } + + if let Some(mut request) = frame_state.gpu_cache.request(&mut self.stops_handle) { + GradientGpuBlockBuilder::build( + false, + &mut request, + &self.stops, + ); + } + + self.opacity = get_gradient_opacity( + self.common.prim_rect, + self.stretch_size, + self.tile_spacing, + self.stops_opacity, + ); + } +} + +pub type ConicGradientDataHandle = InternHandle<ConicGradient>; + +#[derive(Debug, MallocSizeOf)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct ConicGradient { + pub extend_mode: ExtendMode, + pub center: PointKey, + pub params: ConicGradientParams, + pub stretch_size: SizeKey, + pub stops: Vec<GradientStopKey>, + pub tile_spacing: SizeKey, + pub nine_patch: Option<Box<NinePatchDescriptor>>, +} + +impl Internable for ConicGradient { + type Key = ConicGradientKey; + type StoreData = ConicGradientTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_CONIC_GRADIENTS; +} + +impl InternablePrimitive for ConicGradient { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> ConicGradientKey { + ConicGradientKey::new(info, self) + } + + fn make_instance_kind( + _key: ConicGradientKey, + data_handle: ConicGradientDataHandle, + _prim_store: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + PrimitiveInstanceKind::ConicGradient { + data_handle, + visible_tiles_range: GradientTileRange::empty(), + } + } +} + +impl IsVisible for ConicGradient { + fn is_visible(&self) -> bool { + true + } +} + +//////////////////////////////////////////////////////////////////////////////// + +// The gradient entry index for the first color stop +pub const GRADIENT_DATA_FIRST_STOP: usize = 0; +// The gradient entry index for the last color stop +pub const GRADIENT_DATA_LAST_STOP: usize = GRADIENT_DATA_SIZE - 1; + +// The start of the gradient data table +pub const GRADIENT_DATA_TABLE_BEGIN: usize = GRADIENT_DATA_FIRST_STOP + 1; +// The exclusive bound of the gradient data table +pub const GRADIENT_DATA_TABLE_END: usize = GRADIENT_DATA_LAST_STOP; +// The number of entries in the gradient data table. +pub const GRADIENT_DATA_TABLE_SIZE: usize = 128; + +// The number of entries in a gradient data: GRADIENT_DATA_TABLE_SIZE + first stop entry + last stop entry +pub const GRADIENT_DATA_SIZE: usize = GRADIENT_DATA_TABLE_SIZE + 2; + +/// An entry in a gradient data table representing a segment of the gradient +/// color space. +#[derive(Debug, Copy, Clone)] +#[repr(C)] +struct GradientDataEntry { + start_color: PremultipliedColorF, + end_color: PremultipliedColorF, +} + +impl GradientDataEntry { + fn white() -> Self { + Self { + start_color: PremultipliedColorF::WHITE, + end_color: PremultipliedColorF::WHITE, + } + } +} + +// TODO(gw): Tidy this up to be a free function / module? +struct GradientGpuBlockBuilder {} + +impl GradientGpuBlockBuilder { + /// Generate a color ramp filling the indices in [start_idx, end_idx) and interpolating + /// from start_color to end_color. + fn fill_colors( + start_idx: usize, + end_idx: usize, + start_color: &PremultipliedColorF, + end_color: &PremultipliedColorF, + entries: &mut [GradientDataEntry; GRADIENT_DATA_SIZE], + ) { + // Calculate the color difference for individual steps in the ramp. + let inv_steps = 1.0 / (end_idx - start_idx) as f32; + let step_r = (end_color.r - start_color.r) * inv_steps; + let step_g = (end_color.g - start_color.g) * inv_steps; + let step_b = (end_color.b - start_color.b) * inv_steps; + let step_a = (end_color.a - start_color.a) * inv_steps; + + let mut cur_color = *start_color; + + // Walk the ramp writing start and end colors for each entry. + for index in start_idx .. end_idx { + let entry = &mut entries[index]; + entry.start_color = cur_color; + cur_color.r += step_r; + cur_color.g += step_g; + cur_color.b += step_b; + cur_color.a += step_a; + entry.end_color = cur_color; + } + } + + /// Compute an index into the gradient entry table based on a gradient stop offset. This + /// function maps offsets from [0, 1] to indices in [GRADIENT_DATA_TABLE_BEGIN, GRADIENT_DATA_TABLE_END]. + #[inline] + fn get_index(offset: f32) -> usize { + (offset.max(0.0).min(1.0) * GRADIENT_DATA_TABLE_SIZE as f32 + + GRADIENT_DATA_TABLE_BEGIN as f32) + .round() as usize + } + + // Build the gradient data from the supplied stops, reversing them if necessary. + fn build( + reverse_stops: bool, + request: &mut GpuDataRequest, + src_stops: &[GradientStop], + ) { + // Preconditions (should be ensured by DisplayListBuilder): + // * we have at least two stops + // * first stop has offset 0.0 + // * last stop has offset 1.0 + let mut src_stops = src_stops.into_iter(); + let mut cur_color = match src_stops.next() { + Some(stop) => { + debug_assert_eq!(stop.offset, 0.0); + stop.color.premultiplied() + } + None => { + error!("Zero gradient stops found!"); + PremultipliedColorF::BLACK + } + }; + + // A table of gradient entries, with two colors per entry, that specify the start and end color + // within the segment of the gradient space represented by that entry. To lookup a gradient result, + // first the entry index is calculated to determine which two colors to interpolate between, then + // the offset within that entry bucket is used to interpolate between the two colors in that entry. + // This layout preserves hard stops, as the end color for a given entry can differ from the start + // color for the following entry, despite them being adjacent. Colors are stored within in BGRA8 + // format for texture upload. This table requires the gradient color stops to be normalized to the + // range [0, 1]. The first and last entries hold the first and last color stop colors respectively, + // while the entries in between hold the interpolated color stop values for the range [0, 1]. + let mut entries = [GradientDataEntry::white(); GRADIENT_DATA_SIZE]; + + if reverse_stops { + // Fill in the first entry (for reversed stops) with the first color stop + GradientGpuBlockBuilder::fill_colors( + GRADIENT_DATA_LAST_STOP, + GRADIENT_DATA_LAST_STOP + 1, + &cur_color, + &cur_color, + &mut entries, + ); + + // Fill in the center of the gradient table, generating a color ramp between each consecutive pair + // of gradient stops. Each iteration of a loop will fill the indices in [next_idx, cur_idx). The + // loop will then fill indices in [GRADIENT_DATA_TABLE_BEGIN, GRADIENT_DATA_TABLE_END). + let mut cur_idx = GRADIENT_DATA_TABLE_END; + for next in src_stops { + let next_color = next.color.premultiplied(); + let next_idx = Self::get_index(1.0 - next.offset); + + if next_idx < cur_idx { + GradientGpuBlockBuilder::fill_colors( + next_idx, + cur_idx, + &next_color, + &cur_color, + &mut entries, + ); + cur_idx = next_idx; + } + + cur_color = next_color; + } + if cur_idx != GRADIENT_DATA_TABLE_BEGIN { + error!("Gradient stops abruptly at {}, auto-completing to white", cur_idx); + } + + // Fill in the last entry (for reversed stops) with the last color stop + GradientGpuBlockBuilder::fill_colors( + GRADIENT_DATA_FIRST_STOP, + GRADIENT_DATA_FIRST_STOP + 1, + &cur_color, + &cur_color, + &mut entries, + ); + } else { + // Fill in the first entry with the first color stop + GradientGpuBlockBuilder::fill_colors( + GRADIENT_DATA_FIRST_STOP, + GRADIENT_DATA_FIRST_STOP + 1, + &cur_color, + &cur_color, + &mut entries, + ); + + // Fill in the center of the gradient table, generating a color ramp between each consecutive pair + // of gradient stops. Each iteration of a loop will fill the indices in [cur_idx, next_idx). The + // loop will then fill indices in [GRADIENT_DATA_TABLE_BEGIN, GRADIENT_DATA_TABLE_END). + let mut cur_idx = GRADIENT_DATA_TABLE_BEGIN; + for next in src_stops { + let next_color = next.color.premultiplied(); + let next_idx = Self::get_index(next.offset); + + if next_idx > cur_idx { + GradientGpuBlockBuilder::fill_colors( + cur_idx, + next_idx, + &cur_color, + &next_color, + &mut entries, + ); + cur_idx = next_idx; + } + + cur_color = next_color; + } + if cur_idx != GRADIENT_DATA_TABLE_END { + error!("Gradient stops abruptly at {}, auto-completing to white", cur_idx); + } + + // Fill in the last entry with the last color stop + GradientGpuBlockBuilder::fill_colors( + GRADIENT_DATA_LAST_STOP, + GRADIENT_DATA_LAST_STOP + 1, + &cur_color, + &cur_color, + &mut entries, + ); + } + + for entry in entries.iter() { + request.push(entry.start_color); + request.push(entry.end_color); + } + } +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn test_struct_sizes() { + use std::mem; + // The sizes of these structures are critical for performance on a number of + // talos stress tests. If you get a failure here on CI, there's two possibilities: + // (a) You made a structure smaller than it currently is. Great work! Update the + // test expectations and move on. + // (b) You made a structure larger. This is not necessarily a problem, but should only + // be done with care, and after checking if talos performance regresses badly. + assert_eq!(mem::size_of::<LinearGradient>(), 72, "LinearGradient size changed"); + assert_eq!(mem::size_of::<LinearGradientTemplate>(), 120, "LinearGradientTemplate size changed"); + assert_eq!(mem::size_of::<LinearGradientKey>(), 88, "LinearGradientKey size changed"); + + assert_eq!(mem::size_of::<RadialGradient>(), 72, "RadialGradient size changed"); + assert_eq!(mem::size_of::<RadialGradientTemplate>(), 128, "RadialGradientTemplate size changed"); + assert_eq!(mem::size_of::<RadialGradientKey>(), 96, "RadialGradientKey size changed"); + + assert_eq!(mem::size_of::<ConicGradient>(), 72, "ConicGradient size changed"); + assert_eq!(mem::size_of::<ConicGradientTemplate>(), 128, "ConicGradientTemplate size changed"); + assert_eq!(mem::size_of::<ConicGradientKey>(), 96, "ConicGradientKey size changed"); +} diff --git a/gfx/wr/webrender/src/prim_store/image.rs b/gfx/wr/webrender/src/prim_store/image.rs new file mode 100644 index 0000000000..8ce62abebd --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/image.rs @@ -0,0 +1,521 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::{ + AlphaType, ColorDepth, ColorF, ColorU, RasterSpace, + ImageKey as ApiImageKey, ImageRendering, + PremultipliedColorF, Shadow, YuvColorSpace, ColorRange, YuvFormat, +}; +use api::units::*; +use crate::scene_building::{CreateShadow, IsVisible}; +use crate::frame_builder::FrameBuildingState; +use crate::gpu_cache::{GpuCache, GpuDataRequest}; +use crate::intern::{Internable, InternDebug, Handle as InternHandle}; +use crate::internal_types::{LayoutPrimitiveInfo}; +use crate::picture::SurfaceIndex; +use crate::prim_store::{ + EdgeAaSegmentMask, PrimitiveInstanceKind, + PrimitiveOpacity, PrimKey, + PrimTemplate, PrimTemplateCommonData, PrimitiveStore, SegmentInstanceIndex, + SizeKey, InternablePrimitive, +}; +use crate::render_target::RenderTargetKind; +use crate::render_task::{BlitSource, RenderTask}; +use crate::render_task_cache::{ + RenderTaskCacheEntryHandle, RenderTaskCacheKey, RenderTaskCacheKeyKind, RenderTaskParent +}; +use crate::resource_cache::{ImageRequest, ResourceCache}; +use crate::util::pack_as_float; + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct VisibleImageTile { + pub tile_offset: TileOffset, + pub edge_flags: EdgeAaSegmentMask, + pub local_rect: LayoutRect, + pub local_clip_rect: LayoutRect, +} + +// Key that identifies a unique (partial) image that is being +// stored in the render task cache. +#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct ImageCacheKey { + pub request: ImageRequest, + pub texel_rect: Option<DeviceIntRect>, +} + +/// Instance specific fields for an image primitive. These are +/// currently stored in a separate array to avoid bloating the +/// size of PrimitiveInstance. In the future, we should be able +/// to remove this and store the information inline, by: +/// (a) Removing opacity collapse / binding support completely. +/// Once we have general picture caching, we don't need this. +/// (b) Change visible_tiles to use Storage in the primitive +/// scratch buffer. This will reduce the size of the +/// visible_tiles field here, and save memory allocation +/// when image tiling is used. I've left it as a Vec for +/// now to reduce the number of changes, and because image +/// tiling is very rare on real pages. +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct ImageInstance { + pub segment_instance_index: SegmentInstanceIndex, + pub tight_local_clip_rect: LayoutRect, + pub visible_tiles: Vec<VisibleImageTile>, +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, PartialEq, MallocSizeOf, Hash)] +pub struct Image { + pub key: ApiImageKey, + pub stretch_size: SizeKey, + pub tile_spacing: SizeKey, + pub color: ColorU, + pub image_rendering: ImageRendering, + pub alpha_type: AlphaType, +} + +pub type ImageKey = PrimKey<Image>; + +impl ImageKey { + pub fn new( + info: &LayoutPrimitiveInfo, + image: Image, + ) -> Self { + ImageKey { + common: info.into(), + kind: image, + } + } +} + +impl InternDebug for ImageKey {} + +// Where to find the texture data for an image primitive. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, MallocSizeOf)] +pub enum ImageSource { + // A normal image - just reference the texture cache. + Default, + // An image that is pre-rendered into the texture cache + // via a render task. + Cache { + size: DeviceIntSize, + handle: Option<RenderTaskCacheEntryHandle>, + }, +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, MallocSizeOf)] +pub struct ImageData { + pub key: ApiImageKey, + pub stretch_size: LayoutSize, + pub tile_spacing: LayoutSize, + pub color: ColorF, + pub source: ImageSource, + pub image_rendering: ImageRendering, + pub alpha_type: AlphaType, +} + +impl From<Image> for ImageData { + fn from(image: Image) -> Self { + ImageData { + key: image.key, + color: image.color.into(), + stretch_size: image.stretch_size.into(), + tile_spacing: image.tile_spacing.into(), + source: ImageSource::Default, + image_rendering: image.image_rendering, + alpha_type: image.alpha_type, + } + } +} + +impl ImageData { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + common: &mut PrimTemplateCommonData, + parent_surface: SurfaceIndex, + frame_state: &mut FrameBuildingState, + ) { + if let Some(mut request) = frame_state.gpu_cache.request(&mut common.gpu_cache_handle) { + self.write_prim_gpu_blocks(&mut request); + } + + common.opacity = { + let image_properties = frame_state + .resource_cache + .get_image_properties(self.key); + + match image_properties { + Some(image_properties) => { + let is_tiled = image_properties.tiling.is_some(); + + if self.tile_spacing != LayoutSize::zero() && !is_tiled { + self.source = ImageSource::Cache { + // Size in device-pixels we need to allocate in render task cache. + size: image_properties.descriptor.size.to_i32(), + handle: None, + }; + } + + let mut is_opaque = image_properties.descriptor.is_opaque(); + let request = ImageRequest { + key: self.key, + rendering: self.image_rendering, + tile: None, + }; + + // Every frame, for cached items, we need to request the render + // task cache item. The closure will be invoked on the first + // time through, and any time the render task output has been + // evicted from the texture cache. + match self.source { + ImageSource::Cache { ref mut size, ref mut handle } => { + let padding = DeviceIntSideOffsets::new( + 0, + (self.tile_spacing.width * size.width as f32 / self.stretch_size.width) as i32, + (self.tile_spacing.height * size.height as f32 / self.stretch_size.height) as i32, + 0, + ); + + size.width += padding.horizontal(); + size.height += padding.vertical(); + + is_opaque &= padding == DeviceIntSideOffsets::zero(); + + let image_cache_key = ImageCacheKey { + request, + texel_rect: None, + }; + let target_kind = if image_properties.descriptor.format.bytes_per_pixel() == 1 { + RenderTargetKind::Alpha + } else { + RenderTargetKind::Color + }; + + // Request a pre-rendered image task. + *handle = Some(frame_state.resource_cache.request_render_task( + RenderTaskCacheKey { + size: *size, + kind: RenderTaskCacheKeyKind::Image(image_cache_key), + }, + frame_state.gpu_cache, + frame_state.rg_builder, + None, + image_properties.descriptor.is_opaque(), + RenderTaskParent::Surface(parent_surface), + frame_state.surfaces, + |rg_builder| { + // Create a task to blit from the texture cache to + // a normal transient render task surface. This will + // copy only the sub-rect, if specified. + // TODO: figure out if/when we can do a blit instead. + let cache_to_target_task_id = RenderTask::new_scaling_with_padding( + BlitSource::Image { key: image_cache_key }, + rg_builder, + target_kind, + *size, + padding, + ); + + // Create a task to blit the rect from the child render + // task above back into the right spot in the persistent + // render target cache. + RenderTask::new_blit( + *size, + BlitSource::RenderTask { + task_id: cache_to_target_task_id, + }, + rg_builder, + ) + } + )); + } + ImageSource::Default => {} + } + + if is_opaque { + PrimitiveOpacity::from_alpha(self.color.a) + } else { + PrimitiveOpacity::translucent() + } + } + None => { + PrimitiveOpacity::opaque() + } + } + }; + } + + pub fn write_prim_gpu_blocks(&self, request: &mut GpuDataRequest) { + // Images are drawn as a white color, modulated by the total + // opacity coming from any collapsed property bindings. + // Size has to match `VECS_PER_SPECIFIC_BRUSH` from `brush_image.glsl` exactly. + request.push(self.color.premultiplied()); + request.push(PremultipliedColorF::WHITE); + request.push([ + self.stretch_size.width + self.tile_spacing.width, + self.stretch_size.height + self.tile_spacing.height, + 0.0, + 0.0, + ]); + } +} + +pub type ImageTemplate = PrimTemplate<ImageData>; + +impl From<ImageKey> for ImageTemplate { + fn from(image: ImageKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(image.common); + + ImageTemplate { + common, + kind: image.kind.into(), + } + } +} + +pub type ImageDataHandle = InternHandle<Image>; + +impl Internable for Image { + type Key = ImageKey; + type StoreData = ImageTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_IMAGES; +} + +impl InternablePrimitive for Image { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> ImageKey { + ImageKey::new(info, self) + } + + fn make_instance_kind( + _key: ImageKey, + data_handle: ImageDataHandle, + prim_store: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + // TODO(gw): Refactor this to not need a separate image + // instance (see ImageInstance struct). + let image_instance_index = prim_store.images.push(ImageInstance { + segment_instance_index: SegmentInstanceIndex::INVALID, + tight_local_clip_rect: LayoutRect::zero(), + visible_tiles: Vec::new(), + }); + + PrimitiveInstanceKind::Image { + data_handle, + image_instance_index, + is_compositor_surface: false, + } + } +} + +impl CreateShadow for Image { + fn create_shadow( + &self, + shadow: &Shadow, + _: bool, + _: RasterSpace, + ) -> Self { + Image { + tile_spacing: self.tile_spacing, + stretch_size: self.stretch_size, + key: self.key, + image_rendering: self.image_rendering, + alpha_type: self.alpha_type, + color: shadow.color.into(), + } + } +} + +impl IsVisible for Image { + fn is_visible(&self) -> bool { + true + } +} + +//////////////////////////////////////////////////////////////////////////////// + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct YuvImage { + pub color_depth: ColorDepth, + pub yuv_key: [ApiImageKey; 3], + pub format: YuvFormat, + pub color_space: YuvColorSpace, + pub color_range: ColorRange, + pub image_rendering: ImageRendering, +} + +pub type YuvImageKey = PrimKey<YuvImage>; + +impl YuvImageKey { + pub fn new( + info: &LayoutPrimitiveInfo, + yuv_image: YuvImage, + ) -> Self { + YuvImageKey { + common: info.into(), + kind: yuv_image, + } + } +} + +impl InternDebug for YuvImageKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct YuvImageData { + pub color_depth: ColorDepth, + pub yuv_key: [ApiImageKey; 3], + pub format: YuvFormat, + pub color_space: YuvColorSpace, + pub color_range: ColorRange, + pub image_rendering: ImageRendering, +} + +impl From<YuvImage> for YuvImageData { + fn from(image: YuvImage) -> Self { + YuvImageData { + color_depth: image.color_depth, + yuv_key: image.yuv_key, + format: image.format, + color_space: image.color_space, + color_range: image.color_range, + image_rendering: image.image_rendering, + } + } +} + +impl YuvImageData { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + common: &mut PrimTemplateCommonData, + frame_state: &mut FrameBuildingState, + ) { + if let Some(mut request) = frame_state.gpu_cache.request(&mut common.gpu_cache_handle) { + self.write_prim_gpu_blocks(&mut request); + }; + + // YUV images never have transparency + common.opacity = PrimitiveOpacity::opaque(); + } + + pub fn request_resources( + &mut self, + resource_cache: &mut ResourceCache, + gpu_cache: &mut GpuCache, + ) { + let channel_num = self.format.get_plane_num(); + debug_assert!(channel_num <= 3); + for channel in 0 .. channel_num { + resource_cache.request_image( + ImageRequest { + key: self.yuv_key[channel], + rendering: self.image_rendering, + tile: None, + }, + gpu_cache, + ); + } + } + + pub fn write_prim_gpu_blocks(&self, request: &mut GpuDataRequest) { + request.push([ + self.color_depth.rescaling_factor(), + pack_as_float(self.color_space as u32), + pack_as_float(self.format as u32), + 0.0 + ]); + } +} + +pub type YuvImageTemplate = PrimTemplate<YuvImageData>; + +impl From<YuvImageKey> for YuvImageTemplate { + fn from(image: YuvImageKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(image.common); + + YuvImageTemplate { + common, + kind: image.kind.into(), + } + } +} + +pub type YuvImageDataHandle = InternHandle<YuvImage>; + +impl Internable for YuvImage { + type Key = YuvImageKey; + type StoreData = YuvImageTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_YUV_IMAGES; +} + +impl InternablePrimitive for YuvImage { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> YuvImageKey { + YuvImageKey::new(info, self) + } + + fn make_instance_kind( + _key: YuvImageKey, + data_handle: YuvImageDataHandle, + _prim_store: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + PrimitiveInstanceKind::YuvImage { + data_handle, + segment_instance_index: SegmentInstanceIndex::INVALID, + is_compositor_surface: false, + } + } +} + +impl IsVisible for YuvImage { + fn is_visible(&self) -> bool { + true + } +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn test_struct_sizes() { + use std::mem; + // The sizes of these structures are critical for performance on a number of + // talos stress tests. If you get a failure here on CI, there's two possibilities: + // (a) You made a structure smaller than it currently is. Great work! Update the + // test expectations and move on. + // (b) You made a structure larger. This is not necessarily a problem, but should only + // be done with care, and after checking if talos performance regresses badly. + assert_eq!(mem::size_of::<Image>(), 32, "Image size changed"); + assert_eq!(mem::size_of::<ImageTemplate>(), 92, "ImageTemplate size changed"); + assert_eq!(mem::size_of::<ImageKey>(), 52, "ImageKey size changed"); + assert_eq!(mem::size_of::<YuvImage>(), 32, "YuvImage size changed"); + assert_eq!(mem::size_of::<YuvImageTemplate>(), 60, "YuvImageTemplate size changed"); + assert_eq!(mem::size_of::<YuvImageKey>(), 52, "YuvImageKey size changed"); +} diff --git a/gfx/wr/webrender/src/prim_store/interned.rs b/gfx/wr/webrender/src/prim_store/interned.rs new file mode 100644 index 0000000000..50337103c3 --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/interned.rs @@ -0,0 +1,14 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// list of all interned primitives to match enumerate_interners! + +pub use crate::prim_store::backdrop::Backdrop; +pub use crate::prim_store::borders::{ImageBorder, NormalBorderPrim}; +pub use crate::prim_store::image::{Image, YuvImage}; +pub use crate::prim_store::line_dec::{LineDecoration}; +pub use crate::prim_store::gradient::{LinearGradient, RadialGradient, ConicGradient}; +pub use crate::prim_store::picture::Picture; +pub use crate::prim_store::text_run::TextRun; + diff --git a/gfx/wr/webrender/src/prim_store/line_dec.rs b/gfx/wr/webrender/src/prim_store/line_dec.rs new file mode 100644 index 0000000000..e176ead4a6 --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/line_dec.rs @@ -0,0 +1,257 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::{ + ColorF, ColorU, RasterSpace, + LineOrientation, LineStyle, PremultipliedColorF, Shadow, +}; +use api::units::*; +use crate::scene_building::{CreateShadow, IsVisible}; +use crate::frame_builder::{FrameBuildingState}; +use crate::gpu_cache::GpuDataRequest; +use crate::intern; +use crate::internal_types::LayoutPrimitiveInfo; +use crate::prim_store::{ + PrimKey, PrimTemplate, PrimTemplateCommonData, + InternablePrimitive, PrimitiveStore, +}; +use crate::prim_store::PrimitiveInstanceKind; + +/// Maximum resolution in device pixels at which line decorations are rasterized. +pub const MAX_LINE_DECORATION_RESOLUTION: u32 = 4096; + +#[derive(Clone, Debug, Hash, MallocSizeOf, PartialEq, Eq)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct LineDecorationCacheKey { + pub style: LineStyle, + pub orientation: LineOrientation, + pub wavy_line_thickness: Au, + pub size: LayoutSizeAu, +} + +/// Identifying key for a line decoration. +#[derive(Clone, Debug, Hash, MallocSizeOf, PartialEq, Eq)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct LineDecoration { + // If the cache_key is Some(..) it is a line decoration + // that relies on a render task (e.g. wavy). If the + // cache key is None, it uses a fast path to draw the + // line decoration as a solid rect. + pub cache_key: Option<LineDecorationCacheKey>, + pub color: ColorU, +} + +pub type LineDecorationKey = PrimKey<LineDecoration>; + +impl LineDecorationKey { + pub fn new( + info: &LayoutPrimitiveInfo, + line_dec: LineDecoration, + ) -> Self { + LineDecorationKey { + common: info.into(), + kind: line_dec, + } + } +} + +impl intern::InternDebug for LineDecorationKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct LineDecorationData { + pub cache_key: Option<LineDecorationCacheKey>, + pub color: ColorF, +} + +impl LineDecorationData { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + common: &mut PrimTemplateCommonData, + frame_state: &mut FrameBuildingState, + ) { + if let Some(ref mut request) = frame_state.gpu_cache.request(&mut common.gpu_cache_handle) { + self.write_prim_gpu_blocks(request); + } + } + + fn write_prim_gpu_blocks( + &self, + request: &mut GpuDataRequest + ) { + match self.cache_key.as_ref() { + Some(cache_key) => { + request.push(self.color.premultiplied()); + request.push(PremultipliedColorF::WHITE); + request.push([ + cache_key.size.width.to_f32_px(), + cache_key.size.height.to_f32_px(), + 0.0, + 0.0, + ]); + } + None => { + request.push(self.color.premultiplied()); + } + } + } +} + +pub type LineDecorationTemplate = PrimTemplate<LineDecorationData>; + +impl From<LineDecorationKey> for LineDecorationTemplate { + fn from(line_dec: LineDecorationKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(line_dec.common); + LineDecorationTemplate { + common, + kind: LineDecorationData { + cache_key: line_dec.kind.cache_key, + color: line_dec.kind.color.into(), + } + } + } +} + +pub type LineDecorationDataHandle = intern::Handle<LineDecoration>; + +impl intern::Internable for LineDecoration { + type Key = LineDecorationKey; + type StoreData = LineDecorationTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_LINE_DECORATIONS; +} + +impl InternablePrimitive for LineDecoration { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> LineDecorationKey { + LineDecorationKey::new( + info, + self, + ) + } + + fn make_instance_kind( + _key: LineDecorationKey, + data_handle: LineDecorationDataHandle, + _: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + PrimitiveInstanceKind::LineDecoration { + data_handle, + cache_handle: None, + } + } +} + +impl CreateShadow for LineDecoration { + fn create_shadow( + &self, + shadow: &Shadow, + _: bool, + _: RasterSpace, + ) -> Self { + LineDecoration { + color: shadow.color.into(), + cache_key: self.cache_key.clone(), + } + } +} + +impl IsVisible for LineDecoration { + fn is_visible(&self) -> bool { + self.color.a > 0 + } +} + +/// Choose the decoration mask tile size for a given line. +/// +/// Given a line with overall size `rect_size` and the given `orientation`, +/// return the dimensions of a single mask tile for the decoration pattern +/// described by `style` and `wavy_line_thickness`. +/// +/// If `style` is `Solid`, no mask tile is necessary; return `None`. The other +/// styles each have their own characteristic periods of repetition, so for each +/// one, this function returns a `LayoutSize` with the right aspect ratio and +/// whose specific size is convenient for the `cs_line_decoration.glsl` fragment +/// shader to work with. The shader uses a local coordinate space in which the +/// tile fills a rectangle with one corner at the origin, and with the size this +/// function returns. +/// +/// The returned size is not necessarily in pixels; device scaling and other +/// concerns can still affect the actual task size. +/// +/// Regardless of whether `orientation` is `Vertical` or `Horizontal`, the +/// `width` and `height` of the returned size are always horizontal and +/// vertical, respectively. +pub fn get_line_decoration_size( + rect_size: &LayoutSize, + orientation: LineOrientation, + style: LineStyle, + wavy_line_thickness: f32, +) -> Option<LayoutSize> { + let h = match orientation { + LineOrientation::Horizontal => rect_size.height, + LineOrientation::Vertical => rect_size.width, + }; + + // TODO(gw): The formulae below are based on the existing gecko and line + // shader code. They give reasonable results for most inputs, + // but could definitely do with a detailed pass to get better + // quality on a wider range of inputs! + // See nsCSSRendering::PaintDecorationLine in Gecko. + + let (parallel, perpendicular) = match style { + LineStyle::Solid => { + return None; + } + LineStyle::Dashed => { + let dash_length = (3.0 * h).min(64.0).max(1.0); + + (2.0 * dash_length, 4.0) + } + LineStyle::Dotted => { + let diameter = h.min(64.0).max(1.0); + let period = 2.0 * diameter; + + (period, diameter) + } + LineStyle::Wavy => { + let line_thickness = wavy_line_thickness.max(1.0); + let slope_length = h - line_thickness; + let flat_length = ((line_thickness - 1.0) * 2.0).max(1.0); + let approx_period = 2.0 * (slope_length + flat_length); + + (approx_period, h) + } + }; + + Some(match orientation { + LineOrientation::Horizontal => LayoutSize::new(parallel, perpendicular), + LineOrientation::Vertical => LayoutSize::new(perpendicular, parallel), + }) +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn test_struct_sizes() { + use std::mem; + // The sizes of these structures are critical for performance on a number of + // talos stress tests. If you get a failure here on CI, there's two possibilities: + // (a) You made a structure smaller than it currently is. Great work! Update the + // test expectations and move on. + // (b) You made a structure larger. This is not necessarily a problem, but should only + // be done with care, and after checking if talos performance regresses badly. + assert_eq!(mem::size_of::<LineDecoration>(), 20, "LineDecoration size changed"); + assert_eq!(mem::size_of::<LineDecorationTemplate>(), 60, "LineDecorationTemplate size changed"); + assert_eq!(mem::size_of::<LineDecorationKey>(), 40, "LineDecorationKey size changed"); +} diff --git a/gfx/wr/webrender/src/prim_store/mod.rs b/gfx/wr/webrender/src/prim_store/mod.rs new file mode 100644 index 0000000000..ddb7517abc --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/mod.rs @@ -0,0 +1,1364 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::{BorderRadius, ClipMode, ColorF, ColorU, RasterSpace}; +use api::{ImageRendering, RepeatMode, PrimitiveFlags}; +use api::{PremultipliedColorF, PropertyBinding, Shadow}; +use api::{PrimitiveKeyKind}; +use api::units::*; +use euclid::{SideOffsets2D, Size2D}; +use malloc_size_of::MallocSizeOf; +use crate::segment::EdgeAaSegmentMask; +use crate::border::BorderSegmentCacheKey; +use crate::clip::{ClipChainId, ClipSet}; +use crate::debug_item::DebugItem; +use crate::scene_building::{CreateShadow, IsVisible}; +use crate::frame_builder::FrameBuildingState; +use crate::glyph_rasterizer::GlyphKey; +use crate::gpu_cache::{GpuCacheAddress, GpuCacheHandle, GpuDataRequest}; +use crate::gpu_types::{BrushFlags}; +use crate::intern; +use crate::picture::PicturePrimitive; +#[cfg(debug_assertions)] +use crate::render_backend::{FrameId}; +use crate::render_task_graph::RenderTaskId; +use crate::render_task_cache::RenderTaskCacheEntryHandle; +use crate::resource_cache::ImageProperties; +use crate::scene::SceneProperties; +use std::{hash, ops, u32, usize}; +#[cfg(debug_assertions)] +use std::sync::atomic::{AtomicUsize, Ordering}; +use crate::util::Recycler; +use crate::internal_types::LayoutPrimitiveInfo; +use crate::visibility::PrimitiveVisibility; + +pub mod backdrop; +pub mod borders; +pub mod gradient; +pub mod image; +pub mod line_dec; +pub mod picture; +pub mod text_run; +pub mod interned; + +mod storage; + +use backdrop::BackdropDataHandle; +use borders::{ImageBorderDataHandle, NormalBorderDataHandle}; +use gradient::{LinearGradientPrimitive, LinearGradientDataHandle, RadialGradientDataHandle, ConicGradientDataHandle}; +use image::{ImageDataHandle, ImageInstance, YuvImageDataHandle}; +use line_dec::LineDecorationDataHandle; +use picture::PictureDataHandle; +use text_run::{TextRunDataHandle, TextRunPrimitive}; + +pub const VECS_PER_SEGMENT: usize = 2; + +/// Counter for unique primitive IDs for debug tracing. +#[cfg(debug_assertions)] +static NEXT_PRIM_ID: AtomicUsize = AtomicUsize::new(0); + +#[cfg(debug_assertions)] +static PRIM_CHASE_ID: AtomicUsize = AtomicUsize::new(usize::MAX); + +#[cfg(debug_assertions)] +pub fn register_prim_chase_id(id: PrimitiveDebugId) { + PRIM_CHASE_ID.store(id.0, Ordering::SeqCst); +} + +#[cfg(not(debug_assertions))] +pub fn register_prim_chase_id(_: PrimitiveDebugId) { +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Copy, Clone, MallocSizeOf)] +pub struct PrimitiveOpacity { + pub is_opaque: bool, +} + +impl PrimitiveOpacity { + pub fn opaque() -> PrimitiveOpacity { + PrimitiveOpacity { is_opaque: true } + } + + pub fn translucent() -> PrimitiveOpacity { + PrimitiveOpacity { is_opaque: false } + } + + pub fn from_alpha(alpha: f32) -> PrimitiveOpacity { + PrimitiveOpacity { + is_opaque: alpha >= 1.0, + } + } + + pub fn combine(self, other: PrimitiveOpacity) -> PrimitiveOpacity { + PrimitiveOpacity{ + is_opaque: self.is_opaque && other.is_opaque + } + } +} + +/// For external images, it's not possible to know the +/// UV coords of the image (or the image data itself) +/// until the render thread receives the frame and issues +/// callbacks to the client application. For external +/// images that are visible, a DeferredResolve is created +/// that is stored in the frame. This allows the render +/// thread to iterate this list and update any changed +/// texture data and update the UV rect. Any filtering +/// is handled externally for NativeTexture external +/// images. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct DeferredResolve { + pub address: GpuCacheAddress, + pub image_properties: ImageProperties, + pub rendering: ImageRendering, +} + +#[derive(Debug, Copy, Clone, PartialEq)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct ClipTaskIndex(pub u16); + +impl ClipTaskIndex { + pub const INVALID: ClipTaskIndex = ClipTaskIndex(0); +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, MallocSizeOf, Ord, PartialOrd)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct PictureIndex(pub usize); + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Copy, Debug, Clone, MallocSizeOf, PartialEq)] +pub struct RectangleKey { + pub x: f32, + pub y: f32, + pub w: f32, + pub h: f32, +} + +impl RectangleKey { + pub fn intersects(&self, other: &Self) -> bool { + self.x < other.x + other.w + && other.x < self.x + self.w + && self.y < other.y + other.h + && other.y < self.y + self.h + } +} + +impl Eq for RectangleKey {} + +impl hash::Hash for RectangleKey { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.x.to_bits().hash(state); + self.y.to_bits().hash(state); + self.w.to_bits().hash(state); + self.h.to_bits().hash(state); + } +} + +impl From<RectangleKey> for LayoutRect { + fn from(key: RectangleKey) -> LayoutRect { + LayoutRect { + origin: LayoutPoint::new(key.x, key.y), + size: LayoutSize::new(key.w, key.h), + } + } +} + +impl From<RectangleKey> for WorldRect { + fn from(key: RectangleKey) -> WorldRect { + WorldRect { + origin: WorldPoint::new(key.x, key.y), + size: WorldSize::new(key.w, key.h), + } + } +} + +impl From<LayoutRect> for RectangleKey { + fn from(rect: LayoutRect) -> RectangleKey { + RectangleKey { + x: rect.origin.x, + y: rect.origin.y, + w: rect.size.width, + h: rect.size.height, + } + } +} + +impl From<PictureRect> for RectangleKey { + fn from(rect: PictureRect) -> RectangleKey { + RectangleKey { + x: rect.origin.x, + y: rect.origin.y, + w: rect.size.width, + h: rect.size.height, + } + } +} + +impl From<WorldRect> for RectangleKey { + fn from(rect: WorldRect) -> RectangleKey { + RectangleKey { + x: rect.origin.x, + y: rect.origin.y, + w: rect.size.width, + h: rect.size.height, + } + } +} + +/// A hashable SideOffset2D that can be used in primitive keys. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, MallocSizeOf, PartialEq)] +pub struct SideOffsetsKey { + pub top: f32, + pub right: f32, + pub bottom: f32, + pub left: f32, +} + +impl Eq for SideOffsetsKey {} + +impl hash::Hash for SideOffsetsKey { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.top.to_bits().hash(state); + self.right.to_bits().hash(state); + self.bottom.to_bits().hash(state); + self.left.to_bits().hash(state); + } +} + +impl From<SideOffsetsKey> for LayoutSideOffsets { + fn from(key: SideOffsetsKey) -> LayoutSideOffsets { + LayoutSideOffsets::new( + key.top, + key.right, + key.bottom, + key.left, + ) + } +} + +impl<U> From<SideOffsets2D<f32, U>> for SideOffsetsKey { + fn from(offsets: SideOffsets2D<f32, U>) -> SideOffsetsKey { + SideOffsetsKey { + top: offsets.top, + right: offsets.right, + bottom: offsets.bottom, + left: offsets.left, + } + } +} + +/// A hashable size for using as a key during primitive interning. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Copy, Debug, Clone, MallocSizeOf, PartialEq)] +pub struct SizeKey { + w: f32, + h: f32, +} + +impl Eq for SizeKey {} + +impl hash::Hash for SizeKey { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.w.to_bits().hash(state); + self.h.to_bits().hash(state); + } +} + +impl From<SizeKey> for LayoutSize { + fn from(key: SizeKey) -> LayoutSize { + LayoutSize::new(key.w, key.h) + } +} + +impl<U> From<Size2D<f32, U>> for SizeKey { + fn from(size: Size2D<f32, U>) -> SizeKey { + SizeKey { + w: size.width, + h: size.height, + } + } +} + +/// A hashable vec for using as a key during primitive interning. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Copy, Debug, Clone, MallocSizeOf, PartialEq)] +pub struct VectorKey { + pub x: f32, + pub y: f32, +} + +impl Eq for VectorKey {} + +impl hash::Hash for VectorKey { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.x.to_bits().hash(state); + self.y.to_bits().hash(state); + } +} + +impl From<VectorKey> for LayoutVector2D { + fn from(key: VectorKey) -> LayoutVector2D { + LayoutVector2D::new(key.x, key.y) + } +} + +impl From<VectorKey> for WorldVector2D { + fn from(key: VectorKey) -> WorldVector2D { + WorldVector2D::new(key.x, key.y) + } +} + +impl From<LayoutVector2D> for VectorKey { + fn from(vec: LayoutVector2D) -> VectorKey { + VectorKey { + x: vec.x, + y: vec.y, + } + } +} + +impl From<WorldVector2D> for VectorKey { + fn from(vec: WorldVector2D) -> VectorKey { + VectorKey { + x: vec.x, + y: vec.y, + } + } +} + +/// A hashable point for using as a key during primitive interning. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Copy, Clone, MallocSizeOf, PartialEq)] +pub struct PointKey { + pub x: f32, + pub y: f32, +} + +impl Eq for PointKey {} + +impl hash::Hash for PointKey { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + self.x.to_bits().hash(state); + self.y.to_bits().hash(state); + } +} + +impl From<PointKey> for LayoutPoint { + fn from(key: PointKey) -> LayoutPoint { + LayoutPoint::new(key.x, key.y) + } +} + +impl From<LayoutPoint> for PointKey { + fn from(p: LayoutPoint) -> PointKey { + PointKey { + x: p.x, + y: p.y, + } + } +} + +impl From<PicturePoint> for PointKey { + fn from(p: PicturePoint) -> PointKey { + PointKey { + x: p.x, + y: p.y, + } + } +} + +impl From<WorldPoint> for PointKey { + fn from(p: WorldPoint) -> PointKey { + PointKey { + x: p.x, + y: p.y, + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct PrimKeyCommonData { + pub flags: PrimitiveFlags, + pub prim_rect: RectangleKey, +} + +impl From<&LayoutPrimitiveInfo> for PrimKeyCommonData { + fn from(info: &LayoutPrimitiveInfo) -> Self { + PrimKeyCommonData { + flags: info.flags, + prim_rect: info.rect.into(), + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct PrimKey<T: MallocSizeOf> { + pub common: PrimKeyCommonData, + pub kind: T, +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct PrimitiveKey { + pub common: PrimKeyCommonData, + pub kind: PrimitiveKeyKind, +} + +impl PrimitiveKey { + pub fn new( + info: &LayoutPrimitiveInfo, + kind: PrimitiveKeyKind, + ) -> Self { + PrimitiveKey { + common: info.into(), + kind, + } + } +} + +impl intern::InternDebug for PrimitiveKey {} + +/// The shared information for a given primitive. This is interned and retained +/// both across frames and display lists, by comparing the matching PrimitiveKey. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub enum PrimitiveTemplateKind { + Rectangle { + color: PropertyBinding<ColorF>, + }, + Clear, +} + +impl PrimitiveTemplateKind { + /// Write any GPU blocks for the primitive template to the given request object. + pub fn write_prim_gpu_blocks( + &self, + request: &mut GpuDataRequest, + scene_properties: &SceneProperties, + ) { + match *self { + PrimitiveTemplateKind::Clear => { + // Opaque black with operator dest out + request.push(PremultipliedColorF::BLACK); + } + PrimitiveTemplateKind::Rectangle { ref color, .. } => { + request.push(scene_properties.resolve_color(color).premultiplied()) + } + } + } +} + +/// Construct the primitive template data from a primitive key. This +/// is invoked when a primitive key is created and the interner +/// doesn't currently contain a primitive with this key. +impl From<PrimitiveKeyKind> for PrimitiveTemplateKind { + fn from(kind: PrimitiveKeyKind) -> Self { + match kind { + PrimitiveKeyKind::Clear => { + PrimitiveTemplateKind::Clear + } + PrimitiveKeyKind::Rectangle { color, .. } => { + PrimitiveTemplateKind::Rectangle { + color: color.into(), + } + } + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct PrimTemplateCommonData { + pub flags: PrimitiveFlags, + pub may_need_repetition: bool, + pub prim_rect: LayoutRect, + pub opacity: PrimitiveOpacity, + /// The GPU cache handle for a primitive template. Since this structure + /// is retained across display lists by interning, this GPU cache handle + /// also remains valid, which reduces the number of updates to the GPU + /// cache when a new display list is processed. + pub gpu_cache_handle: GpuCacheHandle, +} + +impl PrimTemplateCommonData { + pub fn with_key_common(common: PrimKeyCommonData) -> Self { + PrimTemplateCommonData { + flags: common.flags, + may_need_repetition: true, + prim_rect: common.prim_rect.into(), + gpu_cache_handle: GpuCacheHandle::new(), + opacity: PrimitiveOpacity::translucent(), + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct PrimTemplate<T> { + pub common: PrimTemplateCommonData, + pub kind: T, +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct PrimitiveTemplate { + pub common: PrimTemplateCommonData, + pub kind: PrimitiveTemplateKind, +} + +impl ops::Deref for PrimitiveTemplate { + type Target = PrimTemplateCommonData; + fn deref(&self) -> &Self::Target { + &self.common + } +} + +impl ops::DerefMut for PrimitiveTemplate { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.common + } +} + +impl From<PrimitiveKey> for PrimitiveTemplate { + fn from(item: PrimitiveKey) -> Self { + PrimitiveTemplate { + common: PrimTemplateCommonData::with_key_common(item.common), + kind: item.kind.into(), + } + } +} + +impl PrimitiveTemplate { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + frame_state: &mut FrameBuildingState, + scene_properties: &SceneProperties, + ) { + if let Some(mut request) = frame_state.gpu_cache.request(&mut self.common.gpu_cache_handle) { + self.kind.write_prim_gpu_blocks(&mut request, scene_properties); + } + + self.opacity = match self.kind { + PrimitiveTemplateKind::Clear => { + PrimitiveOpacity::translucent() + } + PrimitiveTemplateKind::Rectangle { ref color, .. } => { + PrimitiveOpacity::from_alpha(scene_properties.resolve_color(color).a) + } + }; + } +} + +type PrimitiveDataHandle = intern::Handle<PrimitiveKeyKind>; + +impl intern::Internable for PrimitiveKeyKind { + type Key = PrimitiveKey; + type StoreData = PrimitiveTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_PRIMITIVES; +} + +impl InternablePrimitive for PrimitiveKeyKind { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> PrimitiveKey { + PrimitiveKey::new(info, self) + } + + fn make_instance_kind( + key: PrimitiveKey, + data_handle: PrimitiveDataHandle, + prim_store: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + match key.kind { + PrimitiveKeyKind::Clear => { + PrimitiveInstanceKind::Clear { + data_handle + } + } + PrimitiveKeyKind::Rectangle { color, .. } => { + let color_binding_index = match color { + PropertyBinding::Binding(..) => { + prim_store.color_bindings.push(color) + } + PropertyBinding::Value(..) => ColorBindingIndex::INVALID, + }; + PrimitiveInstanceKind::Rectangle { + data_handle, + segment_instance_index: SegmentInstanceIndex::INVALID, + color_binding_index, + } + } + } + } +} + +#[derive(Debug, MallocSizeOf)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct VisibleMaskImageTile { + pub tile_offset: TileOffset, + pub tile_rect: LayoutRect, +} + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct VisibleGradientTile { + pub handle: GpuCacheHandle, + pub local_rect: LayoutRect, + pub local_clip_rect: LayoutRect, +} + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct CachedGradientSegment { + pub handle: RenderTaskCacheEntryHandle, + pub local_rect: LayoutRect, +} + +/// Information about how to cache a border segment, +/// along with the current render task cache entry. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, MallocSizeOf)] +pub struct BorderSegmentInfo { + pub local_task_size: LayoutSize, + pub cache_key: BorderSegmentCacheKey, +} + +/// Represents the visibility state of a segment (wrt clip masks). +#[cfg_attr(feature = "capture", derive(Serialize))] +#[derive(Debug, Clone)] +pub enum ClipMaskKind { + /// The segment has a clip mask, specified by the render task. + Mask(RenderTaskId), + /// The segment has no clip mask. + None, + /// The segment is made invisible / clipped completely. + Clipped, +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, MallocSizeOf)] +pub struct BrushSegment { + pub local_rect: LayoutRect, + pub may_need_clip_mask: bool, + pub edge_flags: EdgeAaSegmentMask, + pub extra_data: [f32; 4], + pub brush_flags: BrushFlags, +} + +impl BrushSegment { + pub fn new( + local_rect: LayoutRect, + may_need_clip_mask: bool, + edge_flags: EdgeAaSegmentMask, + extra_data: [f32; 4], + brush_flags: BrushFlags, + ) -> Self { + Self { + local_rect, + may_need_clip_mask, + edge_flags, + extra_data, + brush_flags, + } + } +} + +#[derive(Debug, Clone)] +#[repr(C)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +struct ClipRect { + rect: LayoutRect, + mode: f32, +} + +#[derive(Debug, Clone)] +#[repr(C)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +struct ClipCorner { + rect: LayoutRect, + outer_radius_x: f32, + outer_radius_y: f32, + inner_radius_x: f32, + inner_radius_y: f32, +} + +impl ClipCorner { + fn uniform(rect: LayoutRect, outer_radius: f32, inner_radius: f32) -> ClipCorner { + ClipCorner { + rect, + outer_radius_x: outer_radius, + outer_radius_y: outer_radius, + inner_radius_x: inner_radius, + inner_radius_y: inner_radius, + } + } +} + +#[derive(Debug, Clone)] +#[repr(C)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct ClipData { + rect: ClipRect, + top_left: ClipCorner, + top_right: ClipCorner, + bottom_left: ClipCorner, + bottom_right: ClipCorner, +} + +impl ClipData { + pub fn rounded_rect(size: LayoutSize, radii: &BorderRadius, mode: ClipMode) -> ClipData { + // TODO(gw): For simplicity, keep most of the clip GPU structs the + // same as they were, even though the origin is now always + // zero, since they are in the clip's local space. In future, + // we could reduce the GPU cache size of ClipData. + let rect = LayoutRect::new( + LayoutPoint::zero(), + size, + ); + + ClipData { + rect: ClipRect { + rect, + mode: mode as u32 as f32, + }, + top_left: ClipCorner { + rect: LayoutRect::new( + LayoutPoint::new(rect.origin.x, rect.origin.y), + LayoutSize::new(radii.top_left.width, radii.top_left.height), + ), + outer_radius_x: radii.top_left.width, + outer_radius_y: radii.top_left.height, + inner_radius_x: 0.0, + inner_radius_y: 0.0, + }, + top_right: ClipCorner { + rect: LayoutRect::new( + LayoutPoint::new( + rect.origin.x + rect.size.width - radii.top_right.width, + rect.origin.y, + ), + LayoutSize::new(radii.top_right.width, radii.top_right.height), + ), + outer_radius_x: radii.top_right.width, + outer_radius_y: radii.top_right.height, + inner_radius_x: 0.0, + inner_radius_y: 0.0, + }, + bottom_left: ClipCorner { + rect: LayoutRect::new( + LayoutPoint::new( + rect.origin.x, + rect.origin.y + rect.size.height - radii.bottom_left.height, + ), + LayoutSize::new(radii.bottom_left.width, radii.bottom_left.height), + ), + outer_radius_x: radii.bottom_left.width, + outer_radius_y: radii.bottom_left.height, + inner_radius_x: 0.0, + inner_radius_y: 0.0, + }, + bottom_right: ClipCorner { + rect: LayoutRect::new( + LayoutPoint::new( + rect.origin.x + rect.size.width - radii.bottom_right.width, + rect.origin.y + rect.size.height - radii.bottom_right.height, + ), + LayoutSize::new(radii.bottom_right.width, radii.bottom_right.height), + ), + outer_radius_x: radii.bottom_right.width, + outer_radius_y: radii.bottom_right.height, + inner_radius_x: 0.0, + inner_radius_y: 0.0, + }, + } + } + + pub fn uniform(size: LayoutSize, radius: f32, mode: ClipMode) -> ClipData { + // TODO(gw): For simplicity, keep most of the clip GPU structs the + // same as they were, even though the origin is now always + // zero, since they are in the clip's local space. In future, + // we could reduce the GPU cache size of ClipData. + let rect = LayoutRect::new( + LayoutPoint::zero(), + size, + ); + + ClipData { + rect: ClipRect { + rect, + mode: mode as u32 as f32, + }, + top_left: ClipCorner::uniform( + LayoutRect::new( + LayoutPoint::new(rect.origin.x, rect.origin.y), + LayoutSize::new(radius, radius), + ), + radius, + 0.0, + ), + top_right: ClipCorner::uniform( + LayoutRect::new( + LayoutPoint::new(rect.origin.x + rect.size.width - radius, rect.origin.y), + LayoutSize::new(radius, radius), + ), + radius, + 0.0, + ), + bottom_left: ClipCorner::uniform( + LayoutRect::new( + LayoutPoint::new(rect.origin.x, rect.origin.y + rect.size.height - radius), + LayoutSize::new(radius, radius), + ), + radius, + 0.0, + ), + bottom_right: ClipCorner::uniform( + LayoutRect::new( + LayoutPoint::new( + rect.origin.x + rect.size.width - radius, + rect.origin.y + rect.size.height - radius, + ), + LayoutSize::new(radius, radius), + ), + radius, + 0.0, + ), + } + } +} + +/// A hashable descriptor for nine-patches, used by image and +/// gradient borders. +#[derive(Debug, Clone, PartialEq, Eq, Hash, MallocSizeOf)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct NinePatchDescriptor { + pub width: i32, + pub height: i32, + pub slice: DeviceIntSideOffsets, + pub fill: bool, + pub repeat_horizontal: RepeatMode, + pub repeat_vertical: RepeatMode, + pub outset: SideOffsetsKey, + pub widths: SideOffsetsKey, +} + +impl IsVisible for PrimitiveKeyKind { + // Return true if the primary primitive is visible. + // Used to trivially reject non-visible primitives. + // TODO(gw): Currently, primitives other than those + // listed here are handled before the + // add_primitive() call. In the future + // we should move the logic for all other + // primitive types to use this. + fn is_visible(&self) -> bool { + match *self { + PrimitiveKeyKind::Clear => { + true + } + PrimitiveKeyKind::Rectangle { ref color, .. } => { + match *color { + PropertyBinding::Value(value) => value.a > 0, + PropertyBinding::Binding(..) => true, + } + } + } + } +} + +impl CreateShadow for PrimitiveKeyKind { + // Create a clone of this PrimitiveContainer, applying whatever + // changes are necessary to the primitive to support rendering + // it as part of the supplied shadow. + fn create_shadow( + &self, + shadow: &Shadow, + _: bool, + _: RasterSpace, + ) -> PrimitiveKeyKind { + match *self { + PrimitiveKeyKind::Rectangle { .. } => { + PrimitiveKeyKind::Rectangle { + color: PropertyBinding::Value(shadow.color.into()), + } + } + PrimitiveKeyKind::Clear => { + panic!("bug: this prim is not supported in shadow contexts"); + } + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct PrimitiveDebugId(pub usize); + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub enum PrimitiveInstanceKind { + /// Direct reference to a Picture + Picture { + /// Handle to the common interned data for this primitive. + data_handle: PictureDataHandle, + pic_index: PictureIndex, + segment_instance_index: SegmentInstanceIndex, + }, + /// A run of glyphs, with associated font parameters. + TextRun { + /// Handle to the common interned data for this primitive. + data_handle: TextRunDataHandle, + /// Index to the per instance scratch data for this primitive. + run_index: TextRunIndex, + }, + /// A line decoration. cache_handle refers to a cached render + /// task handle, if this line decoration is not a simple solid. + LineDecoration { + /// Handle to the common interned data for this primitive. + data_handle: LineDecorationDataHandle, + // TODO(gw): For now, we need to store some information in + // the primitive instance that is created during + // prepare_prims and read during the batching pass. + // Once we unify the prepare_prims and batching to + // occur at the same time, we can remove most of + // the things we store here in the instance, and + // use them directly. This will remove cache_handle, + // but also the opacity, clip_task_id etc below. + cache_handle: Option<RenderTaskCacheEntryHandle>, + }, + NormalBorder { + /// Handle to the common interned data for this primitive. + data_handle: NormalBorderDataHandle, + cache_handles: storage::Range<RenderTaskCacheEntryHandle>, + }, + ImageBorder { + /// Handle to the common interned data for this primitive. + data_handle: ImageBorderDataHandle, + }, + Rectangle { + /// Handle to the common interned data for this primitive. + data_handle: PrimitiveDataHandle, + segment_instance_index: SegmentInstanceIndex, + color_binding_index: ColorBindingIndex, + }, + YuvImage { + /// Handle to the common interned data for this primitive. + data_handle: YuvImageDataHandle, + segment_instance_index: SegmentInstanceIndex, + is_compositor_surface: bool, + }, + Image { + /// Handle to the common interned data for this primitive. + data_handle: ImageDataHandle, + image_instance_index: ImageInstanceIndex, + is_compositor_surface: bool, + }, + LinearGradient { + /// Handle to the common interned data for this primitive. + data_handle: LinearGradientDataHandle, + gradient_index: LinearGradientIndex, + }, + RadialGradient { + /// Handle to the common interned data for this primitive. + data_handle: RadialGradientDataHandle, + visible_tiles_range: GradientTileRange, + }, + ConicGradient { + /// Handle to the common interned data for this primitive. + data_handle: ConicGradientDataHandle, + visible_tiles_range: GradientTileRange, + }, + /// Clear out a rect, used for special effects. + Clear { + /// Handle to the common interned data for this primitive. + data_handle: PrimitiveDataHandle, + }, + /// Render a portion of a specified backdrop. + Backdrop { + data_handle: BackdropDataHandle, + }, +} + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct PrimitiveInstance { + /// Identifies the kind of primitive this + /// instance is, and references to where + /// the relevant information for the primitive + /// can be found. + pub kind: PrimitiveInstanceKind, + + #[cfg(debug_assertions)] + pub id: PrimitiveDebugId, + + /// The last frame ID (of the `RenderTaskGraph`) this primitive + /// was prepared for rendering in. + #[cfg(debug_assertions)] + pub prepared_frame_id: FrameId, + + /// All information and state related to clip(s) for this primitive + pub clip_set: ClipSet, + + /// Information related to the current visibility state of this + /// primitive. + // TODO(gw): Currently built each frame, but can be retained. + // TODO(gw): Remove clipped_world_rect (use tile bounds to determine vis flags) + pub vis: PrimitiveVisibility, +} + +impl PrimitiveInstance { + pub fn new( + local_clip_rect: LayoutRect, + kind: PrimitiveInstanceKind, + clip_chain_id: ClipChainId, + ) -> Self { + PrimitiveInstance { + kind, + #[cfg(debug_assertions)] + prepared_frame_id: FrameId::INVALID, + #[cfg(debug_assertions)] + id: PrimitiveDebugId(NEXT_PRIM_ID.fetch_add(1, Ordering::Relaxed)), + vis: PrimitiveVisibility::new(), + clip_set: ClipSet { + local_clip_rect, + clip_chain_id, + }, + } + } + + // Reset any pre-frame state for this primitive. + pub fn reset(&mut self) { + self.vis.reset(); + } + + pub fn clear_visibility(&mut self) { + self.vis.reset(); + } + + #[cfg(debug_assertions)] + pub fn is_chased(&self) -> bool { + PRIM_CHASE_ID.load(Ordering::SeqCst) == self.id.0 + } + + #[cfg(not(debug_assertions))] + pub fn is_chased(&self) -> bool { + false + } + + pub fn uid(&self) -> intern::ItemUid { + match &self.kind { + PrimitiveInstanceKind::Clear { data_handle, .. } | + PrimitiveInstanceKind::Rectangle { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::Image { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::ImageBorder { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::LineDecoration { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::LinearGradient { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::NormalBorder { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::Picture { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::RadialGradient { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::ConicGradient { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::TextRun { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::YuvImage { data_handle, .. } => { + data_handle.uid() + } + PrimitiveInstanceKind::Backdrop { data_handle, .. } => { + data_handle.uid() + } + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[derive(Debug)] +pub struct SegmentedInstance { + pub gpu_cache_handle: GpuCacheHandle, + pub segments_range: SegmentsRange, +} + +pub type GlyphKeyStorage = storage::Storage<GlyphKey>; +pub type TextRunIndex = storage::Index<TextRunPrimitive>; +pub type TextRunStorage = storage::Storage<TextRunPrimitive>; +pub type ColorBindingIndex = storage::Index<PropertyBinding<ColorU>>; +pub type ColorBindingStorage = storage::Storage<PropertyBinding<ColorU>>; +pub type BorderHandleStorage = storage::Storage<RenderTaskCacheEntryHandle>; +pub type SegmentStorage = storage::Storage<BrushSegment>; +pub type SegmentsRange = storage::Range<BrushSegment>; +pub type SegmentInstanceStorage = storage::Storage<SegmentedInstance>; +pub type SegmentInstanceIndex = storage::Index<SegmentedInstance>; +pub type ImageInstanceStorage = storage::Storage<ImageInstance>; +pub type ImageInstanceIndex = storage::Index<ImageInstance>; +pub type GradientTileStorage = storage::Storage<VisibleGradientTile>; +pub type GradientTileRange = storage::Range<VisibleGradientTile>; +pub type LinearGradientIndex = storage::Index<LinearGradientPrimitive>; +pub type LinearGradientStorage = storage::Storage<LinearGradientPrimitive>; + +/// Contains various vecs of data that is used only during frame building, +/// where we want to recycle the memory each new display list, to avoid constantly +/// re-allocating and moving memory around. Written during primitive preparation, +/// and read during batching. +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct PrimitiveScratchBuffer { + /// Contains a list of clip mask instance parameters + /// per segment generated. + pub clip_mask_instances: Vec<ClipMaskKind>, + + /// List of glyphs keys that are allocated by each + /// text run instance. + pub glyph_keys: GlyphKeyStorage, + + /// List of render task handles for border segment instances + /// that have been added this frame. + pub border_cache_handles: BorderHandleStorage, + + /// A list of brush segments that have been built for this scene. + pub segments: SegmentStorage, + + /// A list of segment ranges and GPU cache handles for prim instances + /// that have opted into segment building. In future, this should be + /// removed in favor of segment building during primitive interning. + pub segment_instances: SegmentInstanceStorage, + + /// A list of visible tiles that tiled gradients use to store + /// per-tile information. + pub gradient_tiles: GradientTileStorage, + + /// List of debug display items for rendering. + pub debug_items: Vec<DebugItem>, +} + +impl Default for PrimitiveScratchBuffer { + fn default() -> Self { + PrimitiveScratchBuffer { + clip_mask_instances: Vec::new(), + glyph_keys: GlyphKeyStorage::new(0), + border_cache_handles: BorderHandleStorage::new(0), + segments: SegmentStorage::new(0), + segment_instances: SegmentInstanceStorage::new(0), + gradient_tiles: GradientTileStorage::new(0), + debug_items: Vec::new(), + } + } +} + +impl PrimitiveScratchBuffer { + pub fn recycle(&mut self, recycler: &mut Recycler) { + recycler.recycle_vec(&mut self.clip_mask_instances); + self.glyph_keys.recycle(recycler); + self.border_cache_handles.recycle(recycler); + self.segments.recycle(recycler); + self.segment_instances.recycle(recycler); + self.gradient_tiles.recycle(recycler); + recycler.recycle_vec(&mut self.debug_items); + } + + pub fn begin_frame(&mut self) { + // Clear the clip mask tasks for the beginning of the frame. Append + // a single kind representing no clip mask, at the ClipTaskIndex::INVALID + // location. + self.clip_mask_instances.clear(); + self.clip_mask_instances.push(ClipMaskKind::None); + + self.border_cache_handles.clear(); + + // TODO(gw): As in the previous code, the gradient tiles store GPU cache + // handles that are cleared (and thus invalidated + re-uploaded) + // every frame. This maintains the existing behavior, but we + // should fix this in the future to retain handles. + self.gradient_tiles.clear(); + + self.debug_items.clear(); + } + + #[allow(dead_code)] + pub fn push_debug_rect( + &mut self, + rect: DeviceRect, + outer_color: ColorF, + inner_color: ColorF, + ) { + self.debug_items.push(DebugItem::Rect { + rect, + outer_color, + inner_color, + }); + } + + #[allow(dead_code)] + pub fn push_debug_string( + &mut self, + position: DevicePoint, + color: ColorF, + msg: String, + ) { + self.debug_items.push(DebugItem::Text { + position, + color, + msg, + }); + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Clone, Debug)] +pub struct PrimitiveStoreStats { + picture_count: usize, + text_run_count: usize, + image_count: usize, + linear_gradient_count: usize, + color_binding_count: usize, +} + +impl PrimitiveStoreStats { + pub fn empty() -> Self { + PrimitiveStoreStats { + picture_count: 0, + text_run_count: 0, + image_count: 0, + linear_gradient_count: 0, + color_binding_count: 0, + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct PrimitiveStore { + pub pictures: Vec<PicturePrimitive>, + pub text_runs: TextRunStorage, + pub linear_gradients: LinearGradientStorage, + + /// A list of image instances. These are stored separately as + /// storing them inline in the instance makes the structure bigger + /// for other types. + pub images: ImageInstanceStorage, + + /// animated color bindings for this primitive. + pub color_bindings: ColorBindingStorage, +} + +impl PrimitiveStore { + pub fn new(stats: &PrimitiveStoreStats) -> PrimitiveStore { + PrimitiveStore { + pictures: Vec::with_capacity(stats.picture_count), + text_runs: TextRunStorage::new(stats.text_run_count), + images: ImageInstanceStorage::new(stats.image_count), + color_bindings: ColorBindingStorage::new(stats.color_binding_count), + linear_gradients: LinearGradientStorage::new(stats.linear_gradient_count), + } + } + + pub fn get_stats(&self) -> PrimitiveStoreStats { + PrimitiveStoreStats { + picture_count: self.pictures.len(), + text_run_count: self.text_runs.len(), + image_count: self.images.len(), + linear_gradient_count: self.linear_gradients.len(), + color_binding_count: self.color_bindings.len(), + } + } + + #[allow(unused)] + pub fn print_picture_tree(&self, root: PictureIndex) { + use crate::print_tree::PrintTree; + let mut pt = PrintTree::new("picture tree"); + self.pictures[root.0].print(&self.pictures, root, &mut pt); + } + + /// Returns the total count of primitive instances contained in pictures. + pub fn prim_count(&self) -> usize { + let mut prim_count = 0; + for pic in &self.pictures { + prim_count += pic.prim_list.prim_instances.len(); + } + prim_count + } +} + +/// Trait for primitives that are directly internable. +/// see SceneBuilder::add_primitive<P> +pub trait InternablePrimitive: intern::Internable<InternData = ()> + Sized { + /// Build a new key from self with `info`. + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> Self::Key; + + fn make_instance_kind( + key: Self::Key, + data_handle: intern::Handle<Self>, + prim_store: &mut PrimitiveStore, + reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind; +} + + +#[test] +#[cfg(target_pointer_width = "64")] +fn test_struct_sizes() { + use std::mem; + // The sizes of these structures are critical for performance on a number of + // talos stress tests. If you get a failure here on CI, there's two possibilities: + // (a) You made a structure smaller than it currently is. Great work! Update the + // test expectations and move on. + // (b) You made a structure larger. This is not necessarily a problem, but should only + // be done with care, and after checking if talos performance regresses badly. + assert_eq!(mem::size_of::<PrimitiveInstance>(), 152, "PrimitiveInstance size changed"); + assert_eq!(mem::size_of::<PrimitiveInstanceKind>(), 24, "PrimitiveInstanceKind size changed"); + assert_eq!(mem::size_of::<PrimitiveTemplate>(), 56, "PrimitiveTemplate size changed"); + assert_eq!(mem::size_of::<PrimitiveTemplateKind>(), 28, "PrimitiveTemplateKind size changed"); + assert_eq!(mem::size_of::<PrimitiveKey>(), 36, "PrimitiveKey size changed"); + assert_eq!(mem::size_of::<PrimitiveKeyKind>(), 16, "PrimitiveKeyKind size changed"); +} diff --git a/gfx/wr/webrender/src/prim_store/picture.rs b/gfx/wr/webrender/src/prim_store/picture.rs new file mode 100644 index 0000000000..d0815cdac8 --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/picture.rs @@ -0,0 +1,322 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::{ + ColorU, MixBlendMode, FilterPrimitiveInput, FilterPrimitiveKind, ColorSpace, + PropertyBinding, PropertyBindingId, CompositeOperator, +}; +use api::units::{Au, LayoutVector2D}; +use crate::scene_building::IsVisible; +use crate::filterdata::SFilterData; +use crate::intern::ItemUid; +use crate::intern::{Internable, InternDebug, Handle as InternHandle}; +use crate::internal_types::{LayoutPrimitiveInfo, Filter}; +use crate::picture::PictureCompositeMode; +use crate::prim_store::{ + PrimitiveInstanceKind, PrimitiveStore, VectorKey, + InternablePrimitive, +}; + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, MallocSizeOf, PartialEq, Hash, Eq)] +pub enum CompositeOperatorKey { + Over, + In, + Out, + Atop, + Xor, + Lighter, + Arithmetic([Au; 4]), +} + +impl From<CompositeOperator> for CompositeOperatorKey { + fn from(operator: CompositeOperator) -> Self { + match operator { + CompositeOperator::Over => CompositeOperatorKey::Over, + CompositeOperator::In => CompositeOperatorKey::In, + CompositeOperator::Out => CompositeOperatorKey::Out, + CompositeOperator::Atop => CompositeOperatorKey::Atop, + CompositeOperator::Xor => CompositeOperatorKey::Xor, + CompositeOperator::Lighter => CompositeOperatorKey::Lighter, + CompositeOperator::Arithmetic(k_vals) => { + let k_vals = [ + Au::from_f32_px(k_vals[0]), + Au::from_f32_px(k_vals[1]), + Au::from_f32_px(k_vals[2]), + Au::from_f32_px(k_vals[3]), + ]; + CompositeOperatorKey::Arithmetic(k_vals) + } + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, MallocSizeOf, PartialEq, Hash, Eq)] +pub enum FilterPrimitiveKey { + Identity(ColorSpace, FilterPrimitiveInput), + Flood(ColorSpace, ColorU), + Blend(ColorSpace, MixBlendMode, FilterPrimitiveInput, FilterPrimitiveInput), + Blur(ColorSpace, Au, Au, FilterPrimitiveInput), + Opacity(ColorSpace, Au, FilterPrimitiveInput), + ColorMatrix(ColorSpace, [Au; 20], FilterPrimitiveInput), + DropShadow(ColorSpace, (VectorKey, Au, ColorU), FilterPrimitiveInput), + ComponentTransfer(ColorSpace, FilterPrimitiveInput, Vec<SFilterData>), + Offset(ColorSpace, FilterPrimitiveInput, VectorKey), + Composite(ColorSpace, FilterPrimitiveInput, FilterPrimitiveInput, CompositeOperatorKey), +} + +/// Represents a hashable description of how a picture primitive +/// will be composited into its parent. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, MallocSizeOf, PartialEq, Hash, Eq)] +pub enum PictureCompositeKey { + // No visual compositing effect + Identity, + + // FilterOp + Blur(Au, Au), + Brightness(Au), + Contrast(Au), + Grayscale(Au), + HueRotate(Au), + Invert(Au), + Opacity(Au), + OpacityBinding(PropertyBindingId, Au), + Saturate(Au), + Sepia(Au), + DropShadows(Vec<(VectorKey, Au, ColorU)>), + ColorMatrix([Au; 20]), + SrgbToLinear, + LinearToSrgb, + ComponentTransfer(ItemUid), + Flood(ColorU), + SvgFilter(Vec<FilterPrimitiveKey>), + + // MixBlendMode + Multiply, + Screen, + Overlay, + Darken, + Lighten, + ColorDodge, + ColorBurn, + HardLight, + SoftLight, + Difference, + Exclusion, + Hue, + Saturation, + Color, + Luminosity, +} + +impl From<Option<PictureCompositeMode>> for PictureCompositeKey { + fn from(mode: Option<PictureCompositeMode>) -> Self { + match mode { + Some(PictureCompositeMode::MixBlend(mode)) => { + match mode { + MixBlendMode::Normal => PictureCompositeKey::Identity, + MixBlendMode::Multiply => PictureCompositeKey::Multiply, + MixBlendMode::Screen => PictureCompositeKey::Screen, + MixBlendMode::Overlay => PictureCompositeKey::Overlay, + MixBlendMode::Darken => PictureCompositeKey::Darken, + MixBlendMode::Lighten => PictureCompositeKey::Lighten, + MixBlendMode::ColorDodge => PictureCompositeKey::ColorDodge, + MixBlendMode::ColorBurn => PictureCompositeKey::ColorBurn, + MixBlendMode::HardLight => PictureCompositeKey::HardLight, + MixBlendMode::SoftLight => PictureCompositeKey::SoftLight, + MixBlendMode::Difference => PictureCompositeKey::Difference, + MixBlendMode::Exclusion => PictureCompositeKey::Exclusion, + MixBlendMode::Hue => PictureCompositeKey::Hue, + MixBlendMode::Saturation => PictureCompositeKey::Saturation, + MixBlendMode::Color => PictureCompositeKey::Color, + MixBlendMode::Luminosity => PictureCompositeKey::Luminosity, + } + } + Some(PictureCompositeMode::Filter(op)) => { + match op { + Filter::Blur(width, height) => + PictureCompositeKey::Blur(Au::from_f32_px(width), Au::from_f32_px(height)), + Filter::Brightness(value) => PictureCompositeKey::Brightness(Au::from_f32_px(value)), + Filter::Contrast(value) => PictureCompositeKey::Contrast(Au::from_f32_px(value)), + Filter::Grayscale(value) => PictureCompositeKey::Grayscale(Au::from_f32_px(value)), + Filter::HueRotate(value) => PictureCompositeKey::HueRotate(Au::from_f32_px(value)), + Filter::Invert(value) => PictureCompositeKey::Invert(Au::from_f32_px(value)), + Filter::Saturate(value) => PictureCompositeKey::Saturate(Au::from_f32_px(value)), + Filter::Sepia(value) => PictureCompositeKey::Sepia(Au::from_f32_px(value)), + Filter::SrgbToLinear => PictureCompositeKey::SrgbToLinear, + Filter::LinearToSrgb => PictureCompositeKey::LinearToSrgb, + Filter::Identity => PictureCompositeKey::Identity, + Filter::DropShadows(ref shadows) => { + PictureCompositeKey::DropShadows( + shadows.iter().map(|shadow| { + (shadow.offset.into(), Au::from_f32_px(shadow.blur_radius), shadow.color.into()) + }).collect() + ) + } + Filter::Opacity(binding, _) => { + match binding { + PropertyBinding::Value(value) => { + PictureCompositeKey::Opacity(Au::from_f32_px(value)) + } + PropertyBinding::Binding(key, default) => { + PictureCompositeKey::OpacityBinding(key.id, Au::from_f32_px(default)) + } + } + } + Filter::ColorMatrix(values) => { + let mut quantized_values: [Au; 20] = [Au(0); 20]; + for (value, result) in values.iter().zip(quantized_values.iter_mut()) { + *result = Au::from_f32_px(*value); + } + PictureCompositeKey::ColorMatrix(quantized_values) + } + Filter::ComponentTransfer => unreachable!(), + Filter::Flood(color) => PictureCompositeKey::Flood(color.into()), + } + } + Some(PictureCompositeMode::ComponentTransferFilter(handle)) => { + PictureCompositeKey::ComponentTransfer(handle.uid()) + } + Some(PictureCompositeMode::SvgFilter(filter_primitives, filter_data)) => { + PictureCompositeKey::SvgFilter(filter_primitives.into_iter().map(|primitive| { + match primitive.kind { + FilterPrimitiveKind::Identity(identity) => FilterPrimitiveKey::Identity(primitive.color_space, identity.input), + FilterPrimitiveKind::Blend(blend) => FilterPrimitiveKey::Blend(primitive.color_space, blend.mode, blend.input1, blend.input2), + FilterPrimitiveKind::Flood(flood) => FilterPrimitiveKey::Flood(primitive.color_space, flood.color.into()), + FilterPrimitiveKind::Blur(blur) => + FilterPrimitiveKey::Blur(primitive.color_space, Au::from_f32_px(blur.width), Au::from_f32_px(blur.height), blur.input), + FilterPrimitiveKind::Opacity(opacity) => + FilterPrimitiveKey::Opacity(primitive.color_space, Au::from_f32_px(opacity.opacity), opacity.input), + FilterPrimitiveKind::ColorMatrix(color_matrix) => { + let mut quantized_values: [Au; 20] = [Au(0); 20]; + for (value, result) in color_matrix.matrix.iter().zip(quantized_values.iter_mut()) { + *result = Au::from_f32_px(*value); + } + FilterPrimitiveKey::ColorMatrix(primitive.color_space, quantized_values, color_matrix.input) + } + FilterPrimitiveKind::DropShadow(drop_shadow) => { + FilterPrimitiveKey::DropShadow( + primitive.color_space, + ( + drop_shadow.shadow.offset.into(), + Au::from_f32_px(drop_shadow.shadow.blur_radius), + drop_shadow.shadow.color.into(), + ), + drop_shadow.input, + ) + } + FilterPrimitiveKind::ComponentTransfer(component_transfer) => + FilterPrimitiveKey::ComponentTransfer(primitive.color_space, component_transfer.input, filter_data.clone()), + FilterPrimitiveKind::Offset(info) => + FilterPrimitiveKey::Offset(primitive.color_space, info.input, info.offset.into()), + FilterPrimitiveKind::Composite(info) => + FilterPrimitiveKey::Composite(primitive.color_space, info.input1, info.input2, info.operator.into()), + } + }).collect()) + } + Some(PictureCompositeMode::Blit(_)) | + Some(PictureCompositeMode::TileCache { .. }) | + None => { + PictureCompositeKey::Identity + } + } + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct Picture { + pub composite_mode_key: PictureCompositeKey, +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct PictureKey { + pub composite_mode_key: PictureCompositeKey, +} + +impl PictureKey { + pub fn new( + pic: Picture, + ) -> Self { + PictureKey { + composite_mode_key: pic.composite_mode_key, + } + } +} + +impl InternDebug for PictureKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct PictureData; + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct PictureTemplate; + +impl From<PictureKey> for PictureTemplate { + fn from(_: PictureKey) -> Self { + PictureTemplate + } +} + +pub type PictureDataHandle = InternHandle<Picture>; + +impl Internable for Picture { + type Key = PictureKey; + type StoreData = PictureTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_PICTURES; +} + +impl InternablePrimitive for Picture { + fn into_key( + self, + _: &LayoutPrimitiveInfo, + ) -> PictureKey { + PictureKey::new(self) + } + + fn make_instance_kind( + _key: PictureKey, + _: PictureDataHandle, + _: &mut PrimitiveStore, + _reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + // Should never be hit as this method should not be + // called for pictures. + unreachable!(); + } +} + +impl IsVisible for Picture { + fn is_visible(&self) -> bool { + true + } +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn test_struct_sizes() { + use std::mem; + // The sizes of these structures are critical for performance on a number of + // talos stress tests. If you get a failure here on CI, there's two possibilities: + // (a) You made a structure smaller than it currently is. Great work! Update the + // test expectations and move on. + // (b) You made a structure larger. This is not necessarily a problem, but should only + // be done with care, and after checking if talos performance regresses badly. + assert_eq!(mem::size_of::<Picture>(), 88, "Picture size changed"); + assert_eq!(mem::size_of::<PictureTemplate>(), 0, "PictureTemplate size changed"); + assert_eq!(mem::size_of::<PictureKey>(), 88, "PictureKey size changed"); +} diff --git a/gfx/wr/webrender/src/prim_store/storage.rs b/gfx/wr/webrender/src/prim_store/storage.rs new file mode 100644 index 0000000000..a928192cd9 --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/storage.rs @@ -0,0 +1,134 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use std::{iter::Extend, ops, marker::PhantomData, u32}; +use crate::util::Recycler; + +#[derive(Debug, Hash)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct Index<T>(u32, PhantomData<T>); + +// We explicitly implement Copy + Clone instead of using #[derive(Copy, Clone)] +// because we don't want to require that T implements Clone + Copy. +impl<T> Clone for Index<T> { + fn clone(&self) -> Self { *self } +} + +impl<T> Copy for Index<T> {} + +impl<T> PartialEq for Index<T> { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl<T> Index<T> { + fn new(idx: usize) -> Self { + debug_assert!(idx < u32::max_value() as usize); + Index(idx as u32, PhantomData) + } + + pub const INVALID: Index<T> = Index(u32::MAX, PhantomData); + pub const UNUSED: Index<T> = Index(u32::MAX-1, PhantomData); +} + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct Range<T> { + pub start: Index<T>, + pub end: Index<T>, +} + +// We explicitly implement Copy + Clone instead of using #[derive(Copy, Clone)] +// because we don't want to require that T implements Clone + Copy. +impl<T> Clone for Range<T> { + fn clone(&self) -> Self { + Range { start: self.start, end: self.end } + } +} +impl<T> Copy for Range<T> {} + +impl<T> Range<T> { + /// Create an empty `Range` + pub fn empty() -> Self { + Range { + start: Index::new(0), + end: Index::new(0), + } + } + + /// Check for an empty `Range` + pub fn is_empty(self) -> bool { + self.start.0 >= self.end.0 + } +} + +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct Storage<T> { + data: Vec<T>, +} + +impl<T> Storage<T> { + pub fn new(initial_capacity: usize) -> Self { + Storage { + data: Vec::with_capacity(initial_capacity), + } + } + + pub fn len(&self) -> usize { + self.data.len() + } + + pub fn clear(&mut self) { + self.data.clear(); + } + + pub fn push(&mut self, t: T) -> Index<T> { + let index = self.data.len(); + self.data.push(t); + Index(index as u32, PhantomData) + } + + pub fn recycle(&mut self, recycler: &mut Recycler) { + recycler.recycle_vec(&mut self.data); + } + + pub fn extend<II: IntoIterator<Item=T>>(&mut self, iter: II) -> Range<T> { + let start = Index::new(self.data.len()); + self.data.extend(iter); + let end = Index::new(self.data.len()); + Range { start, end } + } +} + +impl<T> ops::Index<Index<T>> for Storage<T> { + type Output = T; + fn index(&self, index: Index<T>) -> &Self::Output { + &self.data[index.0 as usize] + } +} + +impl<T> ops::IndexMut<Index<T>> for Storage<T> { + fn index_mut(&mut self, index: Index<T>) -> &mut Self::Output { + &mut self.data[index.0 as usize] + } +} + +impl<T> ops::Index<Range<T>> for Storage<T> { + type Output = [T]; + fn index(&self, index: Range<T>) -> &Self::Output { + let start = index.start.0 as _; + let end = index.end.0 as _; + &self.data[start..end] + } +} + +impl<T> ops::IndexMut<Range<T>> for Storage<T> { + fn index_mut(&mut self, index: Range<T>) -> &mut Self::Output { + let start = index.start.0 as _; + let end = index.end.0 as _; + &mut self.data[start..end] + } +} diff --git a/gfx/wr/webrender/src/prim_store/text_run.rs b/gfx/wr/webrender/src/prim_store/text_run.rs new file mode 100644 index 0000000000..534c1ef680 --- /dev/null +++ b/gfx/wr/webrender/src/prim_store/text_run.rs @@ -0,0 +1,492 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use api::{ColorF, FontInstanceFlags, GlyphInstance, RasterSpace, Shadow}; +use api::units::{LayoutToWorldTransform, LayoutVector2D, PictureRect}; +use crate::scene_building::{CreateShadow, IsVisible}; +use crate::frame_builder::FrameBuildingState; +use crate::glyph_rasterizer::{FontInstance, FontTransform, GlyphKey, FONT_SIZE_LIMIT}; +use crate::gpu_cache::GpuCache; +use crate::intern; +use crate::internal_types::LayoutPrimitiveInfo; +use crate::picture::{SubpixelMode, SurfaceInfo}; +use crate::prim_store::{PrimitiveOpacity, PrimitiveScratchBuffer}; +use crate::prim_store::{PrimitiveStore, PrimKeyCommonData, PrimTemplateCommonData}; +use crate::renderer::{MAX_VERTEX_TEXTURE_WIDTH}; +use crate::resource_cache::{ResourceCache}; +use crate::util::{MatrixHelpers}; +use crate::prim_store::{InternablePrimitive, PrimitiveInstanceKind}; +use crate::spatial_tree::{SpatialTree, SpatialNodeIndex, ROOT_SPATIAL_NODE_INDEX}; +use crate::space::SpaceSnapper; +use crate::util::PrimaryArc; + +use std::ops; +use std::sync::Arc; + +use super::storage; + +/// A run of glyphs, with associated font information. +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash)] +pub struct TextRunKey { + pub common: PrimKeyCommonData, + pub font: FontInstance, + pub glyphs: PrimaryArc<Vec<GlyphInstance>>, + pub shadow: bool, + pub requested_raster_space: RasterSpace, +} + +impl TextRunKey { + pub fn new( + info: &LayoutPrimitiveInfo, + text_run: TextRun, + ) -> Self { + TextRunKey { + common: info.into(), + font: text_run.font, + glyphs: PrimaryArc(text_run.glyphs), + shadow: text_run.shadow, + requested_raster_space: text_run.requested_raster_space, + } + } +} + +impl intern::InternDebug for TextRunKey {} + +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +#[derive(MallocSizeOf)] +pub struct TextRunTemplate { + pub common: PrimTemplateCommonData, + pub font: FontInstance, + #[ignore_malloc_size_of = "Measured via PrimaryArc"] + pub glyphs: Arc<Vec<GlyphInstance>>, +} + +impl ops::Deref for TextRunTemplate { + type Target = PrimTemplateCommonData; + fn deref(&self) -> &Self::Target { + &self.common + } +} + +impl ops::DerefMut for TextRunTemplate { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.common + } +} + +impl From<TextRunKey> for TextRunTemplate { + fn from(item: TextRunKey) -> Self { + let common = PrimTemplateCommonData::with_key_common(item.common); + TextRunTemplate { + common, + font: item.font, + glyphs: item.glyphs.0, + } + } +} + +impl TextRunTemplate { + /// Update the GPU cache for a given primitive template. This may be called multiple + /// times per frame, by each primitive reference that refers to this interned + /// template. The initial request call to the GPU cache ensures that work is only + /// done if the cache entry is invalid (due to first use or eviction). + pub fn update( + &mut self, + frame_state: &mut FrameBuildingState, + ) { + self.write_prim_gpu_blocks(frame_state); + self.opacity = PrimitiveOpacity::translucent(); + } + + fn write_prim_gpu_blocks( + &mut self, + frame_state: &mut FrameBuildingState, + ) { + // corresponds to `fetch_glyph` in the shaders + if let Some(mut request) = frame_state.gpu_cache.request(&mut self.common.gpu_cache_handle) { + request.push(ColorF::from(self.font.color).premultiplied()); + // this is the only case where we need to provide plain color to GPU + let bg_color = ColorF::from(self.font.bg_color); + request.push([bg_color.r, bg_color.g, bg_color.b, 1.0]); + + let mut gpu_block = [0.0; 4]; + for (i, src) in self.glyphs.iter().enumerate() { + // Two glyphs are packed per GPU block. + + if (i & 1) == 0 { + gpu_block[0] = src.point.x; + gpu_block[1] = src.point.y; + } else { + gpu_block[2] = src.point.x; + gpu_block[3] = src.point.y; + request.push(gpu_block); + } + } + + // Ensure the last block is added in the case + // of an odd number of glyphs. + if (self.glyphs.len() & 1) != 0 { + request.push(gpu_block); + } + + assert!(request.current_used_block_num() <= MAX_VERTEX_TEXTURE_WIDTH); + } + } +} + +pub type TextRunDataHandle = intern::Handle<TextRun>; + +#[derive(Debug, MallocSizeOf)] +#[cfg_attr(feature = "capture", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct TextRun { + pub font: FontInstance, + #[ignore_malloc_size_of = "Measured via PrimaryArc"] + pub glyphs: Arc<Vec<GlyphInstance>>, + pub shadow: bool, + pub requested_raster_space: RasterSpace, +} + +impl intern::Internable for TextRun { + type Key = TextRunKey; + type StoreData = TextRunTemplate; + type InternData = (); + const PROFILE_COUNTER: usize = crate::profiler::INTERNED_TEXT_RUNS; +} + +impl InternablePrimitive for TextRun { + fn into_key( + self, + info: &LayoutPrimitiveInfo, + ) -> TextRunKey { + TextRunKey::new( + info, + self, + ) + } + + fn make_instance_kind( + key: TextRunKey, + data_handle: TextRunDataHandle, + prim_store: &mut PrimitiveStore, + reference_frame_relative_offset: LayoutVector2D, + ) -> PrimitiveInstanceKind { + let run_index = prim_store.text_runs.push(TextRunPrimitive { + used_font: key.font.clone(), + glyph_keys_range: storage::Range::empty(), + reference_frame_relative_offset, + snapped_reference_frame_relative_offset: reference_frame_relative_offset, + shadow: key.shadow, + raster_scale: 1.0, + requested_raster_space: key.requested_raster_space, + }); + + PrimitiveInstanceKind::TextRun{ data_handle, run_index } + } +} + +impl CreateShadow for TextRun { + fn create_shadow( + &self, + shadow: &Shadow, + blur_is_noop: bool, + current_raster_space: RasterSpace, + ) -> Self { + let mut font = FontInstance { + color: shadow.color.into(), + ..self.font.clone() + }; + if shadow.blur_radius > 0.0 { + font.disable_subpixel_aa(); + } + + let requested_raster_space = if blur_is_noop { + current_raster_space + } else { + RasterSpace::Local(1.0) + }; + + TextRun { + font, + glyphs: self.glyphs.clone(), + shadow: true, + requested_raster_space, + } + } +} + +impl IsVisible for TextRun { + fn is_visible(&self) -> bool { + self.font.color.a > 0 + } +} + +#[derive(Debug)] +#[cfg_attr(feature = "capture", derive(Serialize))] +pub struct TextRunPrimitive { + pub used_font: FontInstance, + pub glyph_keys_range: storage::Range<GlyphKey>, + pub reference_frame_relative_offset: LayoutVector2D, + pub snapped_reference_frame_relative_offset: LayoutVector2D, + pub shadow: bool, + pub raster_scale: f32, + pub requested_raster_space: RasterSpace, +} + +impl TextRunPrimitive { + pub fn update_font_instance( + &mut self, + specified_font: &FontInstance, + surface: &SurfaceInfo, + spatial_node_index: SpatialNodeIndex, + transform: &LayoutToWorldTransform, + subpixel_mode: &SubpixelMode, + raster_space: RasterSpace, + prim_rect: PictureRect, + root_scaling_factor: f32, + spatial_tree: &SpatialTree, + ) -> bool { + // If local raster space is specified, include that in the scale + // of the glyphs that get rasterized. + // TODO(gw): Once we support proper local space raster modes, this + // will implicitly be part of the device pixel ratio for + // the (cached) local space surface, and so this code + // will no longer be required. + let raster_scale = raster_space.local_scale().unwrap_or(1.0).max(0.001); + + // root_scaling_factor is used to scale very large pictures that establish + // a raster root back to something sane, thus scale the device size accordingly. + // to the shader it looks like a change in DPI which it already supports. + let dps = surface.device_pixel_scale.0 * root_scaling_factor; + let font_size = specified_font.size.to_f32_px(); + let mut device_font_size = font_size * dps * raster_scale; + + // Check there is a valid transform that doesn't exceed the font size limit. + // Ensure the font is supposed to be rasterized in screen-space. + // Only support transforms that can be coerced to simple 2D transforms. + // Add texture padding to the rasterized glyph buffer when one anticipates + // the glyph will need to be scaled when rendered. + let (use_subpixel_aa, transform_glyphs, texture_padding, oversized) = if raster_space != RasterSpace::Screen || + transform.has_perspective_component() || !transform.has_2d_inverse() + { + (false, false, true, device_font_size > FONT_SIZE_LIMIT) + } else if transform.exceeds_2d_scale((FONT_SIZE_LIMIT / device_font_size) as f64) { + (false, false, true, true) + } else { + (true, !transform.is_simple_2d_translation(), false, false) + }; + + let font_transform = if transform_glyphs { + // Get the font transform matrix (skew / scale) from the complete transform. + // Fold in the device pixel scale. + self.raster_scale = 1.0; + FontTransform::from(transform) + } else { + if oversized { + // Font sizes larger than the limit need to be scaled, thus can't use subpixels. + // In this case we adjust the font size and raster space to ensure + // we rasterize at the limit, to minimize the amount of scaling. + let limited_raster_scale = FONT_SIZE_LIMIT / (font_size * dps); + device_font_size = FONT_SIZE_LIMIT; + + // Record the raster space the text needs to be snapped in. The original raster + // scale would have been too big. + self.raster_scale = limited_raster_scale; + } else { + // Record the raster space the text needs to be snapped in. We may have changed + // from RasterSpace::Screen due to a transform with perspective or without a 2d + // inverse, or it may have been RasterSpace::Local all along. + self.raster_scale = raster_scale; + } + + // Rasterize the glyph without any transform + FontTransform::identity() + }; + + // TODO(aosmond): Snapping really ought to happen during scene building + // as much as possible. This will allow clips to be already adjusted + // based on the snapping requirements of the primitive. This may affect + // complex clips that create a different task, and when we rasterize + // glyphs without the transform (because the shader doesn't have the + // snap offsets to adjust its clip). These rects are fairly conservative + // to begin with and do not appear to be causing significant issues at + // this time. + self.snapped_reference_frame_relative_offset = if transform_glyphs { + // Don't touch the reference frame relative offset. We'll let the + // shader do the snapping in device pixels. + self.reference_frame_relative_offset + } else { + // There may be an animation, so snap the reference frame relative + // offset such that it excludes the impact, if any. + let snap_to_device = SpaceSnapper::new_with_target( + surface.raster_spatial_node_index, + spatial_node_index, + surface.device_pixel_scale, + spatial_tree, + ); + snap_to_device.snap_point(&self.reference_frame_relative_offset.to_point()).to_vector() + }; + + let mut flags = specified_font.flags; + if transform_glyphs { + flags |= FontInstanceFlags::TRANSFORM_GLYPHS; + } + if texture_padding { + flags |= FontInstanceFlags::TEXTURE_PADDING; + } + + // If the transform or device size is different, then the caller of + // this method needs to know to rebuild the glyphs. + let cache_dirty = + self.used_font.transform != font_transform || + self.used_font.size != device_font_size.into() || + self.used_font.flags != flags; + + // Construct used font instance from the specified font instance + self.used_font = FontInstance { + transform: font_transform, + size: device_font_size.into(), + flags, + ..specified_font.clone() + }; + + // If subpixel AA is disabled due to the backing surface the glyphs + // are being drawn onto, disable it (unless we are using the + // specifial subpixel mode that estimates background color). + let mut allow_subpixel = match subpixel_mode { + SubpixelMode::Allow => true, + SubpixelMode::Deny => false, + SubpixelMode::Conditional { allowed_rect, excluded_rects } => { + // Conditional mode allows subpixel AA to be enabled for this + // text run, so long as it doesn't intersect with any of the + // cutout rectangles in the list, and it's inside the allowed rect. + allowed_rect.contains_rect(&prim_rect) && + excluded_rects.iter().all(|rect| !rect.intersects(&prim_rect)) + } + }; + + // If we are using special estimated background subpixel blending, then + // we can allow it regardless of what the surface says. + allow_subpixel |= self.used_font.bg_color.a != 0; + + // If using local space glyphs, we don't want subpixel AA. + if !allow_subpixel || !use_subpixel_aa { + self.used_font.disable_subpixel_aa(); + + // Disable subpixel positioning for oversized glyphs to avoid + // thrashing the glyph cache with many subpixel variations of + // big glyph textures. A possible subpixel positioning error + // is small relative to the maximum font size and thus should + // not be very noticeable. + if oversized { + self.used_font.disable_subpixel_position(); + } + } + + cache_dirty + } + + /// Gets the raster space to use when rendering this primitive. + /// Usually this would be the requested raster space. However, if + /// the primitive's spatial node or one of its ancestors is being pinch zoomed + /// then we round it. This prevents us rasterizing glyphs for every minor + /// change in zoom level, as that would be too expensive. + fn get_raster_space_for_prim( + &self, + prim_spatial_node_index: SpatialNodeIndex, + spatial_tree: &SpatialTree, + ) -> RasterSpace { + let prim_spatial_node = &spatial_tree.spatial_nodes[prim_spatial_node_index.0 as usize]; + if prim_spatial_node.is_ancestor_or_self_zooming { + let scale_factors = spatial_tree + .get_relative_transform(prim_spatial_node_index, ROOT_SPATIAL_NODE_INDEX) + .scale_factors(); + + // Round the scale up to the nearest power of 2, but don't exceed 8. + let scale = scale_factors.0.max(scale_factors.1).min(8.0); + let rounded_up = 2.0f32.powf(scale.log2().ceil()); + + RasterSpace::Local(rounded_up) + } else { + self.requested_raster_space + } + } + + pub fn request_resources( + &mut self, + prim_offset: LayoutVector2D, + prim_rect: PictureRect, + specified_font: &FontInstance, + glyphs: &[GlyphInstance], + transform: &LayoutToWorldTransform, + surface: &SurfaceInfo, + spatial_node_index: SpatialNodeIndex, + root_scaling_factor: f32, + subpixel_mode: &SubpixelMode, + resource_cache: &mut ResourceCache, + gpu_cache: &mut GpuCache, + spatial_tree: &SpatialTree, + scratch: &mut PrimitiveScratchBuffer, + ) { + let raster_space = self.get_raster_space_for_prim( + spatial_node_index, + spatial_tree, + ); + + let cache_dirty = self.update_font_instance( + specified_font, + surface, + spatial_node_index, + transform, + subpixel_mode, + raster_space, + prim_rect, + root_scaling_factor, + spatial_tree, + ); + + if self.glyph_keys_range.is_empty() || cache_dirty { + let subpx_dir = self.used_font.get_subpx_dir(); + + let dps = surface.device_pixel_scale.0 * root_scaling_factor; + let transform = match raster_space { + RasterSpace::Local(scale) => FontTransform::new(scale * dps, 0.0, 0.0, scale * dps), + RasterSpace::Screen => self.used_font.transform.scale(dps), + }; + + self.glyph_keys_range = scratch.glyph_keys.extend( + glyphs.iter().map(|src| { + let src_point = src.point + prim_offset; + let device_offset = transform.transform(&src_point); + GlyphKey::new(src.index, device_offset, subpx_dir) + })); + } + + resource_cache.request_glyphs( + self.used_font.clone(), + &scratch.glyph_keys[self.glyph_keys_range], + gpu_cache, + ); + } +} + +/// These are linux only because FontInstancePlatformOptions varies in size by platform. +#[test] +#[cfg(target_os = "linux")] +fn test_struct_sizes() { + use std::mem; + // The sizes of these structures are critical for performance on a number of + // talos stress tests. If you get a failure here on CI, there's two possibilities: + // (a) You made a structure smaller than it currently is. Great work! Update the + // test expectations and move on. + // (b) You made a structure larger. This is not necessarily a problem, but should only + // be done with care, and after checking if talos performance regresses badly. + assert_eq!(mem::size_of::<TextRun>(), 64, "TextRun size changed"); + assert_eq!(mem::size_of::<TextRunTemplate>(), 80, "TextRunTemplate size changed"); + assert_eq!(mem::size_of::<TextRunKey>(), 80, "TextRunKey size changed"); + assert_eq!(mem::size_of::<TextRunPrimitive>(), 80, "TextRunPrimitive size changed"); +} |