Skip to content

Truly independent web browser

Compositor: Composite every nested context as a layer of its parent

Aliaksandr Kalenik4fff851b1f0a
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
index 0e2a4379132..0670bec4e0f 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
@@ -320,7 +320,7 @@ fn dump_commands(
}
}
-// A list that draws no canvas or video gets no section.
+// A list that draws no canvas, video or nested context gets no section.
fn dump_layers_if_every_canvas_and_video_kept_updating(
output: &mut String,
tape: &[u8],
@@ -330,16 +330,14 @@ fn dump_layers_if_every_canvas_and_video_kept_updating(
device_viewport_rect: IntRect,
) -> Option<LayerPlan> {
let summary = summarize(tape, command_runs);
- let mut canvases_and_videos = Vec::new();
+ let mut active_media = Vec::new();
for live_content_record in &summary.live_content_records {
let content = live_content_record.content;
- if matches!(content, LiveContent::Canvas(_) | LiveContent::VideoFrame(_))
- && !canvases_and_videos.contains(&content)
- {
- canvases_and_videos.push(content);
+ if !matches!(content, LiveContent::ImageFrame(_)) && !active_media.contains(&content) {
+ active_media.push(content);
}
}
- if canvases_and_videos.is_empty() {
+ if active_media.is_empty() {
return None;
}
let effect_clips =
@@ -352,7 +350,7 @@ fn dump_layers_if_every_canvas_and_video_kept_updating(
tree,
scroll_offsets,
viewport_rect: device_viewport_rect,
- active_media: &canvases_and_videos,
+ active_media: &active_media,
});
match layer_plan {
Ok(layer_plan) => {
diff --git a/Services/Compositor/Rust/src/presenting/damage.rs b/Services/Compositor/Rust/src/presenting/damage.rs
index 7d84fca2582..da577cbad1d 100644
--- a/Services/Compositor/Rust/src/presenting/damage.rs
+++ b/Services/Compositor/Rust/src/presenting/damage.rs
@@ -88,6 +88,17 @@ pub struct LiveContentChangesSinceLastPaint {
pub repaints_everything: bool,
}
+impl LiveContentChangesSinceLastPaint {
+ pub fn changed(&self, content: LiveMedia) -> bool {
+ match content {
+ LiveMedia::Canvas(id) => self.canvases.contains(&id),
+ LiveMedia::VideoFrame(id) => self.video_sinks.contains(&id),
+ LiveMedia::ImageFrame(id) => self.image_frames.contains(&id),
+ LiveMedia::CompositedContext(id) => self.nested_contexts.contains(&id),
+ }
+ }
+}
+
pub struct FrameDamage {
pub rect: IntRect,
pub layers: LayerUpdate,
@@ -363,10 +374,8 @@ impl ContextPresenting {
if let Some(plan) = layers.plan(&self.layers)
&& !layers.shown_by_window_server
{
- damage_rect = damage_rect.united(
- super::layers::rect_of_media_layers_showing(plan, &changes.canvases, &changes.video_sinks)
- .intersected(viewport_rect),
- );
+ damage_rect = damage_rect
+ .united(super::layers::rect_of_media_layers_showing_changes(plan, &changes).intersected(viewport_rect));
}
FrameDamage {
rect: damage_rect,
diff --git a/Services/Compositor/Rust/src/presenting/layers.rs b/Services/Compositor/Rust/src/presenting/layers.rs
index 440582c7a03..605b4551af5 100644
--- a/Services/Compositor/Rust/src/presenting/layers.rs
+++ b/Services/Compositor/Rust/src/presenting/layers.rs
@@ -4,9 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-//! The layers a frame is painted in while canvases and videos update often, and what of them the window server shows
-//! above the frame by itself.
+//! The layers a frame is painted in while canvases and videos update often or it draws nested contexts, and what of
+//! them the window server shows above the frame by itself.
+use super::damage::LiveContentChangesSinceLastPaint;
use super::{
ContextPresenting, FrameToPaint, PresentingEnvironment, paint_overlays, rect_of_size, replay_into, replaying_only,
};
@@ -15,7 +16,7 @@ use crate::raster_host::{
FfiSublayer, FfiSublayerPlacement, LiveContent, MediaKind, PresentableSurfaceHandle, SublayerContentKind,
SurfaceBlend, SurfaceHandle,
};
-use libcompositing_rust::display_list::commands::{CanvasId, VideoSinkHandle};
+use libcompositing_rust::display_list::commands::VideoSinkHandle;
use libcompositing_rust::display_list::layers::{LayerKind, LayerPlan, LayerPlanInputs, build_layer_plan};
use libcompositing_rust::display_list::summary::LiveContent as LiveMedia;
use libcompositing_rust::display_list::window_server_placement::WindowServerPlacement;
@@ -146,6 +147,19 @@ impl ContextLayers {
self.window_server_shows_media
}
+ fn plan_has_nested_context_layers(&self) -> bool {
+ self.plan.as_ref().is_some_and(|plan| {
+ plan.layers.iter().any(|layer| {
+ matches!(
+ layer.kind,
+ LayerKind::Media {
+ content: LiveMedia::CompositedContext(_)
+ }
+ )
+ })
+ })
+ }
+
pub fn drop_layers(&mut self, env: &PresentingEnvironment<'_>) {
// NB: The drop timer runs only while there is a plan.
if self.plan.is_none() && self.surfaces.is_empty() && self.painted_layer_presentations.is_empty() {
@@ -164,7 +178,7 @@ impl ContextLayers {
pub fn drop_timer_fired(&mut self, env: &PresentingEnvironment<'_>) {
// While the window server shows the media, the layers cost nothing, and the frame on screen still needs their
// plan, as for a snapshot. The next frame without a plan puts the media back into the store.
- if self.plan.is_none() || self.window_server_shows_media {
+ if self.plan.is_none() || self.window_server_shows_media || self.plan_has_nested_context_layers() {
return;
}
self.drop_layers(env);
@@ -199,22 +213,12 @@ impl LayerUpdate {
}
}
-pub fn rect_of_media_layers_showing(
- plan: &LayerPlan,
- canvases: &[CanvasId],
- video_sinks: &[VideoSinkHandle],
-) -> IntRect {
+pub fn rect_of_media_layers_showing_changes(plan: &LayerPlan, changes: &LiveContentChangesSinceLastPaint) -> IntRect {
let mut rect = IntRect::default();
for layer in &plan.layers {
- let LayerKind::Media { content } = layer.kind else {
- continue;
- };
- let shows_changed_media = match content {
- LiveMedia::Canvas(id) => canvases.contains(&id),
- LiveMedia::VideoFrame(id) => video_sinks.contains(&id),
- LiveMedia::ImageFrame(_) | LiveMedia::CompositedContext(_) => false,
- };
- if shows_changed_media {
+ if let LayerKind::Media { content } = layer.kind
+ && changes.changed(content)
+ {
rect = rect.united(layer.rect);
}
}
@@ -285,6 +289,12 @@ impl ContextPresenting {
}
}
}
+ for live_content_record in &frame.display_list.storage().summary().live_content_records {
+ let content = live_content_record.content;
+ if matches!(content, LiveMedia::CompositedContext(_)) && !layers.active_media.contains(&content) {
+ layers.active_media.push(content);
+ }
+ }
if layers.active_media.is_empty() {
return layers;
}
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/nested-navigable-wheel-admission.txt b/Tests/LibWeb/Text/expected/async-scrolling/nested-navigable-wheel-admission.txt
index 51117ba1c61..b9856a6ea33 100644
--- a/Tests/LibWeb/Text/expected/async-scrolling/nested-navigable-wheel-admission.txt
+++ b/Tests/LibWeb/Text/expected/async-scrolling/nested-navigable-wheel-admission.txt
@@ -13,12 +13,16 @@ AccumulatedVisualContext Tree:
ScrollNodes:
ScrollNode scroll_node_index=s1 parent_scroll_node_index=s0 scrollport_rect=[0,0 800x600] min_scroll_offset=[0,0] max_scroll_offset=[0,1413] is_viewport=true
+Layers:
+ [L0] base rect=[0,0 800x600]
+ [L1] composited context rect=[37,37 166x126]
+
DisplayList:
-CompositorWheelHitTestTarget@s0/e0 target_scroll_node_index=s1 rect=[0,0 800x600]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[0,0 800x2013]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[0,0 800x2000]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[0,0 800x2000]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[40,40 160x120]
-DrawCompositedContext@s1/e0 dst_rect=[40,40 160x120] inline_clips=[clip=[40,40 160x120]]
-CompositorNestedContextWheelRegion@s1/e0 rect=[40,40 160x120]
+L0 CompositorWheelHitTestTarget@s0/e0 target_scroll_node_index=s1 rect=[0,0 800x600]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[0,0 800x2013]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[0,0 800x2000]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[0,0 800x2000]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s1 rect=[40,40 160x120]
+L1 DrawCompositedContext@s1/e0 dst_rect=[40,40 160x120] inline_clips=[clip=[40,40 160x120]]
+L0 CompositorNestedContextWheelRegion@s1/e0 rect=[40,40 160x120]
diff --git a/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-and-canvas.txt b/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-and-canvas.txt
new file mode 100644
index 00000000000..8e668a0bc83
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-and-canvas.txt
@@ -0,0 +1,24 @@
+AccumulatedVisualContext Tree:
+ spatial:
+ [s0] transform=[1,0,0,1,0,0] origin=(0,0) (Viewport<#document>)
+ [s1] scroll (Viewport<#document>)
+ effects:
+ [e0 in s0] effects=[]
+
+Layers:
+ [L0] base rect=[0,0 800x600]
+ [L1] composited context rect=[17,17 206x126]
+ [L2] canvas rect=[297,17 206x126] shown by the window server: clip=[0,0 800x600]
+ [L3] painted rect=[97,57 306x46]
+
+DisplayList:
+L0 CompositorWheelHitTestTarget@s0/e0 target_scroll_node_index=s0 rect=[0,0 800x600]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x13]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[20,20 200x120]
+L1 DrawCompositedContext@s1/e0 dst_rect=[20,20 200x120] inline_clips=[clip=[20,20 200x120]]
+L0 CompositorNestedContextWheelRegion@s1/e0 rect=[20,20 200x120]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[300,20 200x120]
+L2 DrawCanvas@s1/e0 dst_rect=[300,20 200x120]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[100,60 300x40]
+L3 FillRect@s1/e0 rect=[100,60 300x40] color=rgba(255, 255, 255, 0.5)
+
diff --git a/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-in-opacity-group.txt b/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-in-opacity-group.txt
new file mode 100644
index 00000000000..09a7fcdff8e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-in-opacity-group.txt
@@ -0,0 +1,19 @@
+AccumulatedVisualContext Tree:
+ spatial:
+ [s0] transform=[1,0,0,1,0,0] origin=(0,0) (Viewport<#document>)
+ [s1] scroll (Viewport<#document>)
+ effects:
+ [e0 in s0] effects=[]
+ [e1 in s1] effects=[opacity=0.5] (BlockContainer<DIV>#group)
+
+Layers: none (composited context inside a group effect)
+
+DisplayList:
+CompositorWheelHitTestTarget@s0/e0 target_scroll_node_index=s0 rect=[0,0 800x600]
+CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x133]
+CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x120]
+CompositorWheelHitTestTarget@s1/e1 target_scroll_node_index=s0 rect=[0,0 800x120]
+CompositorWheelHitTestTarget@s1/e1 target_scroll_node_index=s0 rect=[0,0 200x120]
+DrawCompositedContext@s1/e1 dst_rect=[0,0 200x120] inline_clips=[clip=[0,0 200x120]]
+CompositorNestedContextWheelRegion@s1/e1 rect=[0,0 200x120]
+
diff --git a/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-under-content.txt b/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-under-content.txt
new file mode 100644
index 00000000000..27936f69cbf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/display_list/compositing-layers-iframe-under-content.txt
@@ -0,0 +1,23 @@
+AccumulatedVisualContext Tree:
+ spatial:
+ [s0] transform=[1,0,0,1,0,0] origin=(0,0) (Viewport<#document>)
+ [s1] scroll (Viewport<#document>)
+ effects:
+ [e0 in s0] effects=[]
+
+Layers:
+ [L0] base rect=[0,0 800x600]
+ [L1] composited context rect=[17,17 206x126]
+ [L2] painted rect=[97,57 206x46]
+
+DisplayList:
+L0 CompositorWheelHitTestTarget@s0/e0 target_scroll_node_index=s0 rect=[0,0 800x600]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x13]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[20,20 200x120]
+L1 DrawCompositedContext@s1/e0 dst_rect=[20,20 200x120] inline_clips=[clip=[20,20 200x120]]
+L0 CompositorNestedContextWheelRegion@s1/e0 rect=[20,20 200x120]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[100,60 200x40]
+L2 FillRect@s1/e0 rect=[100,60 200x40] color=rgba(255, 255, 255, 0.5)
+L2 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[400,300 50x20]
+L0 FillRect@s1/e0 rect=[400,300 50x20] color=rgb(0, 128, 0)
+
diff --git a/Tests/LibWeb/Text/expected/display_list/iframe-compositor-surface.txt b/Tests/LibWeb/Text/expected/display_list/iframe-compositor-surface.txt
index 8ca70cf2cd1..727a6c36f4c 100644
--- a/Tests/LibWeb/Text/expected/display_list/iframe-compositor-surface.txt
+++ b/Tests/LibWeb/Text/expected/display_list/iframe-compositor-surface.txt
@@ -5,12 +5,16 @@ AccumulatedVisualContext Tree:
effects:
[e0 in s0] effects=[]
+Layers:
+ [L0] base rect=[0,0 800x600]
+ [L1] composited context rect=[0,0 123x83]
+
DisplayList:
-CompositorWheelHitTestTarget@s0/e0 target_scroll_node_index=s0 rect=[0,0 800x600]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x93]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x80]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x80]
-CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 120x80]
-DrawCompositedContext@s1/e0 dst_rect=[0,0 120x80] inline_clips=[clip=[0,0 120x80]]
-CompositorNestedContextWheelRegion@s1/e0 rect=[0,0 120x80]
+L0 CompositorWheelHitTestTarget@s0/e0 target_scroll_node_index=s0 rect=[0,0 800x600]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x93]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x80]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 800x80]
+L0 CompositorWheelHitTestTarget@s1/e0 target_scroll_node_index=s0 rect=[0,0 120x80]
+L1 DrawCompositedContext@s1/e0 dst_rect=[0,0 120x80] inline_clips=[clip=[0,0 120x80]]
+L0 CompositorNestedContextWheelRegion@s1/e0 rect=[0,0 120x80]
diff --git a/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-and-canvas.html b/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-and-canvas.html
new file mode 100644
index 00000000000..66df1a02670
--- /dev/null
+++ b/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-and-canvas.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ iframe { position: absolute; left: 20px; top: 20px; width: 200px; height: 120px; border: 0; }
+ canvas { position: absolute; left: 300px; top: 20px; }
+ #over-both { position: absolute; left: 100px; top: 60px; width: 300px; height: 40px; background: rgba(255, 255, 255, 0.5); }
+</style>
+<iframe id="frame" srcdoc="<body style='margin: 0; background: blue'></body>"></iframe>
+<canvas id="canvas" width="200" height="120"></canvas>
+<div id="over-both"></div>
+<script>
+ promiseTest(async () => {
+ await new Promise(resolve => {
+ // The initial about:blank document is complete too, before the srcdoc document replaces it.
+ if (frame.contentDocument && frame.contentDocument.URL === "about:srcdoc" && frame.contentDocument.readyState === "complete") {
+ resolve();
+ return;
+ }
+ frame.onload = resolve;
+ });
+ const context = canvas.getContext("2d");
+ context.fillStyle = "blue";
+ context.fillRect(0, 0, canvas.width, canvas.height);
+ await animationFrame();
+
+ println(internals.dumpDisplayList());
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-in-opacity-group.html b/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-in-opacity-group.html
new file mode 100644
index 00000000000..32e79535fa9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-in-opacity-group.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ #group { opacity: 0.5; }
+ iframe { width: 200px; height: 120px; border: 0; }
+</style>
+<div id="group"><iframe id="frame" srcdoc="<body style='margin: 0; background: blue'></body>"></iframe></div>
+<script>
+ promiseTest(async () => {
+ await new Promise(resolve => {
+ // The initial about:blank document is complete too, before the srcdoc document replaces it.
+ if (frame.contentDocument && frame.contentDocument.URL === "about:srcdoc" && frame.contentDocument.readyState === "complete") {
+ resolve();
+ return;
+ }
+ frame.onload = resolve;
+ });
+ await animationFrame();
+
+ println(internals.dumpDisplayList());
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-under-content.html b/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-under-content.html
new file mode 100644
index 00000000000..e19091f5d18
--- /dev/null
+++ b/Tests/LibWeb/Text/input/display_list/compositing-layers-iframe-under-content.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ iframe { position: absolute; left: 20px; top: 20px; width: 200px; height: 120px; border: 0; }
+ #over-iframe { position: absolute; left: 100px; top: 60px; width: 200px; height: 40px; background: rgba(255, 255, 255, 0.5); }
+ #elsewhere { position: absolute; left: 400px; top: 300px; width: 50px; height: 20px; background: green; }
+</style>
+<iframe id="frame" srcdoc="<body style='margin: 0; background: blue'></body>"></iframe>
+<div id="over-iframe"></div>
+<div id="elsewhere"></div>
+<script>
+ promiseTest(async () => {
+ await new Promise(resolve => {
+ // The initial about:blank document is complete too, before the srcdoc document replaces it.
+ if (frame.contentDocument && frame.contentDocument.URL === "about:srcdoc" && frame.contentDocument.readyState === "complete") {
+ resolve();
+ return;
+ }
+ frame.onload = resolve;
+ });
+ await animationFrame();
+
+ println(internals.dumpDisplayList());
+ });
+</script>