Skip to content

Truly independent web browser

LibCompositing: Count the frames of nested contexts as live content

Aliaksandr Kalenikbc3238acaf94
diff --git a/Libraries/LibCompositing/Rust/src/display_list/damage.rs b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
index d5f32e91742..4c3234ce63e 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/damage.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
@@ -14,11 +14,11 @@ use crate::display_list::builder::{
read_command, read_header,
};
use crate::display_list::commands::{
- CanvasId, ClipMode, ClipNodeIndex, CompositorScrollbar, ContextRef, DeclareMaskContent, DisplayListCommandHeader,
- DisplayListCommandRun, DisplayListCommandType, DisplayListDataSpan, DisplayListInlineClip,
- DisplayListPaintStyleType, DrawGlyphRun, DrawScaledDecodedImageFrame, EffectNodeIndex, FillPath, FillRect,
- INLINE_CLIP_ENTRY_SIZE, ImageFrameResourceId, OptionalColor, OptionalFloatRect, PaintScrollBar, PaintTextShadow,
- PathPaintKind, SpatialNodeIndex, StrokePath, VISUAL_VIEWPORT_NODE_INDEX, VideoSinkHandle,
+ CanvasId, ClipMode, ClipNodeIndex, CompositorContextId, CompositorScrollbar, ContextRef, DeclareMaskContent,
+ DisplayListCommandHeader, DisplayListCommandRun, DisplayListCommandType, DisplayListDataSpan,
+ DisplayListInlineClip, DisplayListPaintStyleType, DrawGlyphRun, DrawScaledDecodedImageFrame, EffectNodeIndex,
+ FillPath, FillRect, INLINE_CLIP_ENTRY_SIZE, ImageFrameResourceId, OptionalColor, OptionalFloatRect, PaintScrollBar,
+ PaintTextShadow, PathPaintKind, SpatialNodeIndex, StrokePath, VISUAL_VIEWPORT_NODE_INDEX, VideoSinkHandle,
};
use crate::fast_hash::FastMap;
use crate::host::FfiAnimatedContentViewportEffect;
@@ -1546,13 +1546,14 @@ pub struct DisplayListFrame<'a> {
pub scroll_offsets: &'a [FloatPoint],
}
-// The canvases, video sinks and image frames whose pixels changed since the old frame. The lists draw
-// them with commands that stay the same, so the diff cannot see the change.
+// The canvases, video sinks, image frames and frames of nested contexts whose pixels changed since the old frame.
+// The lists draw them with commands that stay the same, so the diff cannot see the change.
#[derive(Clone, Copy, Default)]
pub struct LiveContentChanges<'a> {
pub canvases: &'a [CanvasId],
pub video_sinks: &'a [VideoSinkHandle],
pub image_frames: &'a [ImageFrameResourceId],
+ pub composited_contexts: &'a [CompositorContextId],
/// What the compositor repaints whatever the lists hold, such as a blinking caret.
pub forced_damage_rect: IntRect,
pub offsets_of_records_composited_as_media_layers: &'a [u32],
@@ -1576,7 +1577,10 @@ impl DisplayListDamage {
impl LiveContentChanges<'_> {
fn is_empty(&self) -> bool {
- self.canvases.is_empty() && self.video_sinks.is_empty() && self.image_frames.is_empty()
+ self.canvases.is_empty()
+ && self.video_sinks.is_empty()
+ && self.image_frames.is_empty()
+ && self.composited_contexts.is_empty()
}
fn includes(&self, content: LiveContent) -> bool {
@@ -1584,6 +1588,7 @@ impl LiveContentChanges<'_> {
LiveContent::Canvas(canvas_id) => self.canvases.contains(&canvas_id),
LiveContent::VideoFrame(video_sink_handle) => self.video_sinks.contains(&video_sink_handle),
LiveContent::ImageFrame(image_frame_id) => self.image_frames.contains(&image_frame_id),
+ LiveContent::CompositedContext(context_id) => self.composited_contexts.contains(&context_id),
}
}
}
@@ -1805,8 +1810,8 @@ mod tests {
use crate::display_list::builder::{HEADER_SIZE, note_command};
use crate::display_list::commands::{
BackdropFilterRegion, CanvasId, CompositorMainThreadWheelEventRegion, DisplayListCommand, DisplayListGlyph,
- DisplayListResourceId, DrawCanvas, DrawIsolatedGroup, DrawVideoFrame, FillRect, FontResourceId, InlineClipKind,
- PaintNestedDisplayList, TextShadowLayer,
+ DisplayListResourceId, DrawCanvas, DrawCompositedContext, DrawIsolatedGroup, DrawVideoFrame, FillRect,
+ FontResourceId, InlineClipKind, PaintNestedDisplayList, TextShadowLayer,
};
use crate::display_list::ffi_bytes::FfiBytes;
use crate::node_slot_id::NodeSlotId;
@@ -2287,6 +2292,34 @@ mod tests {
damage_for_live_content_changes(&image, &tree, changed_video),
Some(IntRect::default())
);
+
+ let nested_context = command_bytes(
+ &DrawCompositedContext {
+ dst_rect: FloatRect::new(10.0, 10.0, 20.0, 20.0),
+ child_context_id: CompositorContextId(7),
+ scaling_mode: ScalingMode::NearestNeighbor,
+ spatial_node_index: VISUAL_VIEWPORT_NODE_INDEX,
+ snap_origin_if_untransformed: true,
+ },
+ Some(rect),
+ ContextRef::default(),
+ );
+ let changed_nested_context = LiveContentChanges {
+ composited_contexts: &[CompositorContextId(7)],
+ ..LiveContentChanges::default()
+ };
+ assert_eq!(
+ damage_for_live_content_changes(&nested_context, &tree, changed_nested_context),
+ Some(IntRect::new(9, 9, 22, 22))
+ );
+ let other_nested_context = LiveContentChanges {
+ composited_contexts: &[CompositorContextId(8)],
+ ..LiveContentChanges::default()
+ };
+ assert_eq!(
+ damage_for_live_content_changes(&nested_context, &tree, other_nested_context),
+ Some(IntRect::default())
+ );
}
#[test]
diff --git a/Libraries/LibCompositing/Rust/src/display_list/layers.rs b/Libraries/LibCompositing/Rust/src/display_list/layers.rs
index 0576d57fd1c..5180fbe6578 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/layers.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/layers.rs
@@ -45,6 +45,7 @@ fn media_name(content: LiveContent) -> &'static str {
LiveContent::Canvas(_) => "canvas",
LiveContent::VideoFrame(_) => "video",
LiveContent::ImageFrame(_) => "image",
+ LiveContent::CompositedContext(_) => "composited context",
}
}
@@ -123,7 +124,7 @@ pub struct Layer {
pub kind: LayerKind,
pub rect: IntRect,
pub record_spans: Vec<RecordSpan>,
- /// For a media layer, where the window server can show its content by itself, or why it cannot.
+ /// For a canvas or video layer, where the window server can show its content by itself, or why it cannot.
pub window_server_placement: Option<Result<WindowServerPlacement, WindowServerRejection>>,
}
@@ -503,13 +504,13 @@ impl LayersBeingPlanned {
content: LiveContent,
span: RecordSpan,
bounds: IntRect,
- window_server_placement: Result<WindowServerPlacement, WindowServerRejection>,
+ window_server_placement: Option<Result<WindowServerPlacement, WindowServerRejection>>,
) {
self.layers.push(Layer {
kind: LayerKind::Media { content },
rect: bounds,
record_spans: Vec::new(),
- window_server_placement: Some(window_server_placement),
+ window_server_placement,
});
self.media_layers_each_layer_exists_for.push(1 << media_index);
self.append_records_to_layer(self.layers.len() - 1, span);
@@ -580,7 +581,9 @@ fn media_layer_rejection(
let header = read_header(&inputs.tape[record_offset as usize..]);
if !matches!(
header.command_type,
- DisplayListCommandType::DrawCanvas | DisplayListCommandType::DrawVideoFrame
+ DisplayListCommandType::DrawCanvas
+ | DisplayListCommandType::DrawVideoFrame
+ | DisplayListCommandType::DrawCompositedContext
) {
return Some(MediaLayerRejection::DrawnInsideAnotherCommand);
}
@@ -649,6 +652,15 @@ fn media_candidates(
Ok(candidates)
}
+fn keep_canvases_and_videos_ahead_of_nested_contexts_within_layer_limit(media: &mut Vec<MediaCandidate>) {
+ if media.len() <= MAXIMUM_MEDIA_LAYER_COUNT {
+ return;
+ }
+ media.sort_by_key(|candidate| matches!(candidate.content, LiveContent::CompositedContext(_)));
+ media.truncate(MAXIMUM_MEDIA_LAYER_COUNT);
+ media.sort_by_key(|candidate| candidate.record_offset);
+}
+
fn run_ranges_holding_whole_grouping_effects(
command_runs: &[DisplayListCommandRun],
grouping_of_run: &[EffectNodeIndex],
@@ -741,8 +753,10 @@ fn place_records_in_layers(
media_iter.next_if(|(_, candidate)| candidate.record_offset == begin_offset)
{
let media_bounds = bounds.ink_device_rect(run, header.bounding_rect);
- let placement =
- window_server_placement(inputs.tree, &bounds.world, inputs.viewport_rect, run, header, payload);
+ let window_server_shows_content = !matches!(candidate.content, LiveContent::CompositedContext(_));
+ let placement = window_server_shows_content.then(|| {
+ window_server_placement(inputs.tree, &bounds.world, inputs.viewport_rect, run, header, payload)
+ });
planned.add_media_layer(media_index, candidate.content, span, media_bounds, placement);
layer_of_previous_painted_record = LayersBeingPlanned::BASE_LAYER_INDEX;
} else if !header.has_bounding_rect {
@@ -869,7 +883,7 @@ pub fn build_layer_plan(inputs: &LayerPlanInputs<'_>) -> Result<LayerPlan, Unlay
run_is_a_chunk_of_its_own,
&record_offsets_of_media_under_backdrop_filters,
)?;
- media.truncate(MAXIMUM_MEDIA_LAYER_COUNT);
+ keep_canvases_and_videos_ahead_of_nested_contexts_within_layer_limit(&mut media);
match place_records_in_layers(inputs, &bounds, &chunks, &grouping_of_run, &media) {
Ok(planned) => break planned,
Err(MediaLayersUnderBackdropFilter(media_layers)) => {
@@ -910,8 +924,8 @@ mod tests {
use super::*;
use crate::display_list::builder::{DisplayListBuilder, PendingInlineClip};
use crate::display_list::commands::{
- CanvasId, CompositorBlockingWheelEventRegion, ContextRef, DisplayListCommand, DrawCanvas, FillRect,
- SpatialNodeIndex, VISUAL_VIEWPORT_NODE_INDEX,
+ CanvasId, CompositorBlockingWheelEventRegion, CompositorContextId, ContextRef, DisplayListCommand, DrawCanvas,
+ DrawCompositedContext, FillRect, SpatialNodeIndex, VISUAL_VIEWPORT_NODE_INDEX,
};
use crate::display_list::damage::{
DisplayListDamage, DisplayListFrame, LayerPlans, LiveContentChanges, compute_display_list_damage,
@@ -925,6 +939,7 @@ mod tests {
const VIEWPORT: IntRect = IntRect::new(0, 0, 800, 600);
const CANVAS: LiveContent = LiveContent::Canvas(CanvasId(1));
+ const NESTED_CONTEXT: LiveContent = LiveContent::CompositedContext(CompositorContextId(7));
fn identity_tree() -> VisualContextTree {
VisualContextTree::create(TransformData {
@@ -979,6 +994,20 @@ mod tests {
}
}
+ fn nested_context(rect: IntRect) -> DrawCompositedContext {
+ nested_context_with_id(rect, CompositorContextId(7))
+ }
+
+ fn nested_context_with_id(rect: IntRect, child_context_id: CompositorContextId) -> DrawCompositedContext {
+ DrawCompositedContext {
+ dst_rect: rect.to_float(),
+ child_context_id,
+ scaling_mode: ScalingMode::NearestNeighbor,
+ spatial_node_index: VISUAL_VIEWPORT_NODE_INDEX,
+ snap_origin_if_untransformed: true,
+ }
+ }
+
fn canvas(rect: IntRect) -> DrawCanvas {
DrawCanvas {
dst_rect: rect,
@@ -1015,6 +1044,15 @@ mod tests {
}
fn plan(&self, tree: &VisualContextTree, scroll_offsets: &[FloatPoint]) -> Result<LayerPlan, UnlayeredReason> {
+ self.plan_with_active_media(tree, scroll_offsets, &[CANVAS])
+ }
+
+ fn plan_with_active_media(
+ &self,
+ tree: &VisualContextTree,
+ scroll_offsets: &[FloatPoint],
+ active_media: &[LiveContent],
+ ) -> Result<LayerPlan, UnlayeredReason> {
let (tape, runs) = (self.builder.bytes(), self.builder.command_runs());
let summary = summarize(tape, runs);
let effect_clips = EffectClipPlan::new(tree, runs).unwrap();
@@ -1026,7 +1064,7 @@ mod tests {
tree,
scroll_offsets,
viewport_rect: VIEWPORT,
- active_media: &[CANVAS],
+ active_media,
})
}
}
@@ -1232,6 +1270,76 @@ mod tests {
);
}
+ #[test]
+ fn a_nested_context_is_a_layer_that_the_window_server_does_not_show() {
+ let mut tree = tree_with_root_isolation_effect();
+ let root = ContextRef {
+ effect: tree.root_isolation_effect.unwrap(),
+ ..ContextRef::default()
+ };
+ let elsewhere = separate_clip(&mut tree, root);
+ let list = TestList::new()
+ .with(fill(IntRect::new(0, 0, 800, 600)), root)
+ .with(nested_context(IntRect::new(100, 100, 200, 100)), root)
+ .with(fill(IntRect::new(150, 150, 20, 20)), root)
+ .with(fill(IntRect::new(500, 400, 20, 20)), elsewhere);
+ let plan = list.plan_with_active_media(&tree, &[], &[NESTED_CONTEXT]).unwrap();
+ assert_eq!(kinds(&plan), ["base", "media", "painted"]);
+ assert_eq!(
+ plan.layers[1].kind,
+ LayerKind::Media {
+ content: NESTED_CONTEXT
+ }
+ );
+ assert_eq!(plan.layers[1].rect, IntRect::new(97, 97, 206, 106));
+ assert_eq!(plan.layers[1].window_server_placement, None);
+ assert_eq!(plan.layers[2].rect, IntRect::new(147, 147, 26, 26));
+ }
+
+ #[test]
+ fn a_canvas_keeps_its_layer_when_more_nested_contexts_than_layers_are_drawn_before_it() {
+ let tree = identity_tree();
+ let mut list = TestList::new();
+ let mut active_media = Vec::new();
+ for index in 0..=MAXIMUM_MEDIA_LAYER_COUNT {
+ let child_context_id = CompositorContextId(10 + index as u64);
+ let rect = IntRect::new(index as i32 * 60, 0, 50, 50);
+ list = list.with(nested_context_with_id(rect, child_context_id), ContextRef::default());
+ active_media.push(LiveContent::CompositedContext(child_context_id));
+ }
+ list = list.with(canvas(IntRect::new(0, 300, 200, 100)), ContextRef::default());
+ active_media.push(CANVAS);
+ let plan = list.plan_with_active_media(&tree, &[], &active_media).unwrap();
+ let media_of_layers: Vec<LiveContent> = plan
+ .layers
+ .iter()
+ .filter_map(|layer| match layer.kind {
+ LayerKind::Media { content } => Some(content),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(media_of_layers.len(), MAXIMUM_MEDIA_LAYER_COUNT);
+ assert_eq!(media_of_layers.last(), Some(&CANVAS));
+ }
+
+ #[test]
+ fn a_nested_context_under_a_group_effect_is_not_a_layer() {
+ let mut tree = tree_with_root_isolation_effect();
+ let root = ContextRef {
+ effect: tree.root_isolation_effect.unwrap(),
+ ..ContextRef::default()
+ };
+ let group = tree.append_effect_node_under(root, effects(0.5, CompositingAndBlendingOperator::Normal));
+ let list = TestList::new().with(nested_context(IntRect::new(100, 100, 200, 100)), group);
+ assert_eq!(
+ list.plan_with_active_media(&tree, &[], &[NESTED_CONTEXT]),
+ Err(UnlayeredReason::NoMediaCanBeLayer {
+ first_rejected_media: NESTED_CONTEXT,
+ rejection: MediaLayerRejection::InsideGroupEffect,
+ })
+ );
+ }
+
#[test]
fn a_canvas_between_the_runs_of_one_group_is_not_a_layer() {
let mut tree = tree_with_root_isolation_effect();
diff --git a/Libraries/LibCompositing/Rust/src/display_list/summary.rs b/Libraries/LibCompositing/Rust/src/display_list/summary.rs
index a586bd29220..b1437ce6124 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/summary.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/summary.rs
@@ -28,6 +28,7 @@ pub enum LiveContent {
Canvas(CanvasId),
VideoFrame(VideoSinkHandle),
ImageFrame(ImageFrameResourceId),
+ CompositedContext(CompositorContextId),
}
// A top-level record that draws live content, itself or in a record nested in it.
@@ -44,6 +45,7 @@ pub struct DisplayListSummary {
pub video_sink_handles: Vec<u64>,
pub display_list_ids: Vec<u64>,
pub canvas_ids: Vec<u64>,
+ pub composited_context_ids: Vec<u64>,
pub carets: Vec<IndexedRecord>,
pub live_content_records: Vec<LiveContentRecord>,
pub destination_blending_records: Vec<IndexedRecord>,
@@ -57,6 +59,7 @@ struct ReferencedIds {
video_sinks: HashSet<u64>,
display_lists: HashSet<u64>,
canvases: HashSet<u64>,
+ composited_contexts: HashSet<u64>,
}
fn live_content_of(command_type: DisplayListCommandType, payload: &[u8]) -> Option<LiveContent> {
@@ -75,6 +78,9 @@ fn live_content_of(command_type: DisplayListCommandType, payload: &[u8]) -> Opti
T::DrawTiledDecodedImageFrame => Some(LiveContent::ImageFrame(
read_command::<DrawTiledDecodedImageFrame>(payload).frame_id,
)),
+ T::DrawCompositedContext => Some(LiveContent::CompositedContext(
+ read_command::<DrawCompositedContext>(payload).child_context_id,
+ )),
_ => None,
}
}
@@ -111,6 +117,10 @@ fn note_referenced_ids(command_type: DisplayListCommandType, payload: &[u8], ids
ids.display_lists
.insert(read_command::<PaintNestedDisplayList>(payload).display_list_id.0);
}
+ T::DrawCompositedContext => {
+ ids.composited_contexts
+ .insert(read_command::<DrawCompositedContext>(payload).child_context_id.0);
+ }
T::DrawIsolatedGroup => {
let filter = read_command::<DrawIsolatedGroup>(payload).filter;
if !filter.is_empty()
@@ -124,7 +134,6 @@ fn note_referenced_ids(command_type: DisplayListCommandType, payload: &[u8], ids
T::FillRect
| T::PaintCaret
| T::DrawRepeatedTile
- | T::DrawCompositedContext
| T::PaintLinearGradient
| T::PaintRadialGradient
| T::PaintConicGradient
@@ -226,6 +235,7 @@ pub fn summarize(tape: &[u8], command_runs: &[DisplayListCommandRun]) -> Display
summary.video_sink_handles = sorted(ids.video_sinks);
summary.display_list_ids = sorted(ids.display_lists);
summary.canvas_ids = sorted(ids.canvases);
+ summary.composited_context_ids = sorted(ids.composited_contexts);
summary
}
diff --git a/Libraries/LibCompositing/Rust/src/display_list/window_server_placement.rs b/Libraries/LibCompositing/Rust/src/display_list/window_server_placement.rs
index aab4c60c148..45c0915b216 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/window_server_placement.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/window_server_placement.rs
@@ -278,7 +278,7 @@ pub fn window_server_placement(
let command = read_command::<DrawVideoFrame>(payload);
(command.dst_rect, command.scaling_mode)
}
- _ => unreachable!("only canvases and videos are drawn by media layers"),
+ _ => unreachable!("the window server shows only canvases and videos"),
};
let mut content_to_viewport = world.draw_matrix(run.context.spatial).extract_2d_affine();
// Clips in the space of the record apply before its inline transform moves the content.
diff --git a/Services/Compositor/Rust/src/presenting/damage.rs b/Services/Compositor/Rust/src/presenting/damage.rs
index a09901deb80..29a2118fa3f 100644
--- a/Services/Compositor/Rust/src/presenting/damage.rs
+++ b/Services/Compositor/Rust/src/presenting/damage.rs
@@ -370,6 +370,7 @@ impl ContextPresenting {
canvases: &changes.canvases,
video_sinks: &changes.video_sinks,
image_frames: &changes.image_frames,
+ composited_contexts: &[],
forced_damage_rect,
offsets_of_records_composited_as_media_layers: &media_record_offsets,
},
diff --git a/Services/Compositor/Rust/src/presenting/layers.rs b/Services/Compositor/Rust/src/presenting/layers.rs
index 0c3537fec18..fbb332403a4 100644
--- a/Services/Compositor/Rust/src/presenting/layers.rs
+++ b/Services/Compositor/Rust/src/presenting/layers.rs
@@ -212,7 +212,7 @@ pub fn rect_of_media_layers_showing(
let shows_changed_media = match content {
LiveMedia::Canvas(id) => canvases.contains(&id),
LiveMedia::VideoFrame(id) => video_sinks.contains(&id),
- LiveMedia::ImageFrame(_) => false,
+ LiveMedia::ImageFrame(_) | LiveMedia::CompositedContext(_) => false,
};
if shows_changed_media {
rect = rect.united(layer.rect);
@@ -347,7 +347,7 @@ impl ContextPresenting {
}
(MediaKind::Video, id.0)
}
- LiveMedia::ImageFrame(_) => return false,
+ LiveMedia::ImageFrame(_) | LiveMedia::CompositedContext(_) => return false,
};
if !env.raster.remote_layer_can_show(
env.context_id,
@@ -699,7 +699,7 @@ impl ContextPresenting {
.get(&id.0)
.is_some_and(|sink| sink.has_frame && sink.frame_has_surface),
),
- LiveMedia::ImageFrame(_) => continue,
+ LiveMedia::ImageFrame(_) | LiveMedia::CompositedContext(_) => continue,
};
if !has_content {
continue;