Skip to content

Truly independent web browser

Compositor: Repaint only where a nested context's new frame shows

Aliaksandr Kalenik2a4317fa7ef4
diff --git a/Services/Compositor/Rust/src/core/presenting.rs b/Services/Compositor/Rust/src/core/presenting.rs
index 2ef6522a2a1..dfaa5f7784a 100644
--- a/Services/Compositor/Rust/src/core/presenting.rs
+++ b/Services/Compositor/Rust/src/core/presenting.rs
@@ -9,7 +9,7 @@
use super::CompositorCore;
use crate::context_state::ContextState;
use crate::host::{PendingFrame, PresentScheduling, PresentationId};
-use crate::presenting::{ContextPresenting, PresentingEnvironment};
+use crate::presenting::{ContextPresenting, NestedContexts, PresentingEnvironment};
use crate::raster_host::{
FfiRemoteLayerFrame, FrameHandOff, StorePublication, StoreResizeOutcome, SublayersHandle, SurfaceHandle,
};
@@ -86,6 +86,17 @@ struct ContextsNestedIn<'a> {
now: MonotonicTime,
}
+impl NestedContexts for ContextsNestedIn<'_> {
+ fn frame_generation_of(&self, child_context_id: CompositorContextId) -> Option<u64> {
+ let child_state = self.core.context_if_present(child_context_id)?;
+ if child_state.try_borrow().ok()?.parent_context_id() != Some(self.parent_context_id) {
+ return None;
+ }
+ let child_presenting = self.core.presenting_of(child_context_id)?.try_borrow().ok()?;
+ Some(child_presenting.frame_generation())
+ }
+}
+
impl CompositedContexts for ContextsNestedIn<'_> {
fn resolve(
&self,
@@ -112,7 +123,7 @@ impl CompositorCore {
&'a self,
context_id: CompositorContextId,
now: MonotonicTime,
- composited_contexts: &'a dyn CompositedContexts,
+ nested_contexts: &'a dyn NestedContexts,
) -> PresentingEnvironment<'a> {
PresentingEnvironment {
context_id,
@@ -121,7 +132,7 @@ impl CompositorCore {
now,
has_gpu: self.gpu.has_gpu,
window_server_may_show_high_dynamic_range: self.window_server_may_show_high_dynamic_range.get(),
- composited_contexts,
+ nested_contexts,
}
}
@@ -264,6 +275,16 @@ impl CompositorCore {
}
}
+ fn present_changes_to_current_frame(&self, context_id: CompositorContextId, now: MonotonicTime) {
+ let Some(presenting) = self.presenting_of(context_id) else {
+ return;
+ };
+ let frame_rect = presenting.borrow().frame_rect_to_repaint();
+ if let Some(frame_rect) = frame_rect {
+ self.schedule_present_frame(context_id, PendingFrame::repainting_changes(frame_rect), now);
+ }
+ }
+
pub(super) fn schedule_pending_present_frame(&self, context_id: CompositorContextId, now: MonotonicTime) {
let Some(presenting) = self.presenting_of(context_id) else {
return;
@@ -276,7 +297,7 @@ impl CompositorCore {
.context_if_present(context_id)
.and_then(|context| context.borrow().parent_context_id())
{
- self.present_current_frame(parent, now);
+ self.present_changes_to_current_frame(parent, now);
}
self.schedule_animation_frames_if_needed(context_id, now);
return;
diff --git a/Services/Compositor/Rust/src/nested_display_lists.rs b/Services/Compositor/Rust/src/nested_display_lists.rs
index cfe8b9b4e8d..499588c6a0d 100644
--- a/Services/Compositor/Rust/src/nested_display_lists.rs
+++ b/Services/Compositor/Rust/src/nested_display_lists.rs
@@ -148,11 +148,15 @@ impl NestedDisplayListStore {
}
}
- /// The canvases that the lists a list nests draw, at any depth.
- pub fn canvas_ids_of_nested_display_lists(&self, storage: &DisplayListStorage) -> HashSet<u64> {
- let mut canvas_ids = HashSet::new();
- self.for_each_summary_of_nested_display_lists(storage, |summary| canvas_ids.extend(&summary.canvas_ids));
- canvas_ids
+ /// The ids that the lists a list nests draw, at any depth, as `ids_of` reads them from the summary of each.
+ pub fn ids_drawn_by_nested_display_lists(
+ &self,
+ storage: &DisplayListStorage,
+ ids_of: impl Fn(&DisplayListSummary) -> &[u64],
+ ) -> HashSet<u64> {
+ let mut ids = HashSet::new();
+ self.for_each_summary_of_nested_display_lists(storage, |summary| ids.extend(ids_of(summary)));
+ ids
}
/// The handles of the video sinks that a list and the lists it nests draw, at any depth.
diff --git a/Services/Compositor/Rust/src/presenting/damage.rs b/Services/Compositor/Rust/src/presenting/damage.rs
index 29a2118fa3f..7d84fca2582 100644
--- a/Services/Compositor/Rust/src/presenting/damage.rs
+++ b/Services/Compositor/Rust/src/presenting/damage.rs
@@ -12,9 +12,11 @@ use crate::frame_inputs::FrameInputs;
use crate::host::PendingFrame;
use crate::nested_display_lists::NestedDisplayListStore;
use crate::raster_host::LiveContent;
-use libcompositing_rust::display_list::commands::{CanvasId, ImageFrameResourceId, VideoSinkHandle};
+use libcompositing_rust::display_list::commands::{
+ CanvasId, CompositorContextId, ImageFrameResourceId, VideoSinkHandle,
+};
use libcompositing_rust::display_list::damage::{DisplayListDamage, LayerPlans, LiveContentChanges};
-use libcompositing_rust::display_list::summary::LiveContent as LiveMedia;
+use libcompositing_rust::display_list::summary::{DisplayListSummary, LiveContent as LiveMedia};
use libcompositing_rust::scrolling::DisplayList;
use libgfx_rust::{IntRect, IntSize};
use std::collections::{HashMap, HashSet};
@@ -30,6 +32,7 @@ pub struct PaintedFrame {
canvas_content_generations: HashMap<u64, u64>,
video_frame_ids: HashMap<u64, Option<u64>>,
image_frame_pixel_generations: HashMap<u64, u64>,
+ nested_context_frame_generations: HashMap<u64, Option<u64>>,
}
impl PaintedFrame {
@@ -74,12 +77,13 @@ impl StoreDamage {
}
}
-// The canvases, video sinks and image frames whose pixels changed since the frame painted last.
+// The canvases, video sinks, image frames and nested contexts whose pixels changed since the frame painted last.
#[derive(Default)]
pub struct LiveContentChangesSinceLastPaint {
pub canvases: Vec<CanvasId>,
pub video_sinks: Vec<VideoSinkHandle>,
pub image_frames: Vec<ImageFrameResourceId>,
+ pub nested_contexts: Vec<CompositorContextId>,
pub media_updates: Vec<MediaUpdate>,
pub repaints_everything: bool,
}
@@ -90,17 +94,31 @@ pub struct FrameDamage {
pub live_media_changed: bool,
}
-// The canvases that the list draws, and those that the lists it nests draw.
-fn canvases_drawn_by(display_list: &DisplayList, nested_display_lists: &NestedDisplayListStore) -> Vec<u64> {
- let mut canvas_ids = display_list.storage().summary().canvas_ids.clone();
- let canvas_ids_of_list: HashSet<u64> = canvas_ids.iter().copied().collect();
- canvas_ids.extend(
+// The ids that the list draws, and those that the lists it nests draw, as `ids_of` reads them from a summary.
+fn ids_drawn_by(
+ display_list: &DisplayList,
+ nested_display_lists: &NestedDisplayListStore,
+ ids_of: impl Fn(&DisplayListSummary) -> &[u64],
+) -> Vec<u64> {
+ let mut ids = ids_of(display_list.storage().summary()).to_vec();
+ let ids_of_list: HashSet<u64> = ids.iter().copied().collect();
+ ids.extend(
nested_display_lists
- .canvas_ids_of_nested_display_lists(display_list.storage())
+ .ids_drawn_by_nested_display_lists(display_list.storage(), &ids_of)
.into_iter()
- .filter(|id| !canvas_ids_of_list.contains(id)),
+ .filter(|id| !ids_of_list.contains(id)),
);
- canvas_ids
+ ids
+}
+
+fn canvases_drawn_by(display_list: &DisplayList, nested_display_lists: &NestedDisplayListStore) -> Vec<u64> {
+ ids_drawn_by(display_list, nested_display_lists, |summary| &summary.canvas_ids)
+}
+
+fn nested_contexts_drawn_by(display_list: &DisplayList, nested_display_lists: &NestedDisplayListStore) -> Vec<u64> {
+ ids_drawn_by(display_list, nested_display_lists, |summary| {
+ &summary.composited_context_ids
+ })
}
impl ContextPresenting {
@@ -127,6 +145,7 @@ impl ContextPresenting {
pub(super) fn remember_painted_frame(
&mut self,
+ env: &PresentingEnvironment<'_>,
viewport_rect: IntRect,
display_list: &Rc<DisplayList>,
nested_display_lists: &Rc<NestedDisplayListStore>,
@@ -150,6 +169,10 @@ impl ContextPresenting {
.map(|sink| (sink.handle, sink.has_frame.then_some(sink.frame_id)))
.collect();
let image_frame_pixel_generations = live_content.image_frames.clone();
+ let nested_context_frame_generations = nested_contexts_drawn_by(display_list, nested_display_lists)
+ .into_iter()
+ .map(|id| (id, env.nested_contexts.frame_generation_of(CompositorContextId(id))))
+ .collect();
self.painted_frame = Some(PaintedFrame {
display_list: display_list.clone(),
nested_display_lists: nested_display_lists.clone(),
@@ -161,11 +184,13 @@ impl ContextPresenting {
canvas_content_generations,
video_frame_ids,
image_frame_pixel_generations,
+ nested_context_frame_generations,
});
}
fn live_content_changes_since_last_paint(
&self,
+ env: &PresentingEnvironment<'_>,
viewport_size: IntSize,
display_list: &DisplayList,
live_content: &LiveContent,
@@ -233,6 +258,17 @@ impl ContextPresenting {
}
changes.image_frames.push(ImageFrameResourceId(image_frame_id));
}
+ let nested_context_ids_of_list: HashSet<u64> = summary.composited_context_ids.iter().copied().collect();
+ for (&nested_context_id, &frame_generation) in &painted_frame.nested_context_frame_generations {
+ let nested_context = CompositorContextId(nested_context_id);
+ if env.nested_contexts.frame_generation_of(nested_context) == frame_generation {
+ continue;
+ }
+ if !nested_context_ids_of_list.contains(&nested_context_id) {
+ changes.repaints_everything = true;
+ }
+ changes.nested_contexts.push(nested_context);
+ }
changes
}
@@ -249,7 +285,7 @@ impl ContextPresenting {
};
let viewport_rect = rect_of_size(viewport_size);
let forced_damage_rect = pending_frame.forced_damage_rect.intersected(viewport_rect);
- let changes = self.live_content_changes_since_last_paint(viewport_size, frame.display_list, live_content);
+ let changes = self.live_content_changes_since_last_paint(env, viewport_size, frame.display_list, live_content);
self.layers.media_activity.note_updates(&changes.media_updates, env.now);
let mut layers = self.layer_update_for(env, frame, viewport_rect, live_content);
@@ -370,7 +406,7 @@ impl ContextPresenting {
canvases: &changes.canvases,
video_sinks: &changes.video_sinks,
image_frames: &changes.image_frames,
- composited_contexts: &[],
+ composited_contexts: &changes.nested_contexts,
forced_damage_rect,
offsets_of_records_composited_as_media_layers: &media_record_offsets,
},
diff --git a/Services/Compositor/Rust/src/presenting/mod.rs b/Services/Compositor/Rust/src/presenting/mod.rs
index 7345a1a8bf8..4f810b3f5c5 100644
--- a/Services/Compositor/Rust/src/presenting/mod.rs
+++ b/Services/Compositor/Rust/src/presenting/mod.rs
@@ -23,7 +23,8 @@ use damage::{PaintedFrame, StoreDamage};
use layers::ContextLayers;
use libcompositing_rust::display_list::commands::CompositorContextId;
use libcompositing_rust::display_list::replay::{
- CompositedContexts, ReplayFrame, ReplaySelection, ResolvedCompositedContext, replay_display_list,
+ CompositedContexts, NoCompositedContexts, ReplayFrame, ReplaySelection, ResolvedCompositedContext,
+ replay_display_list,
};
use libcompositing_rust::host::RecordSpan;
use libcompositing_rust::scrolling::DisplayList;
@@ -35,6 +36,16 @@ use std::rc::Rc;
const BACKING_STORE_SHRINK_DELAY: Duration = Duration::from_milliseconds(3000);
const SURPLUS_STORE_RETIREMENT_DELAY: Duration = Duration::from_milliseconds(3000);
+pub trait NestedContexts: CompositedContexts {
+ fn frame_generation_of(&self, child_context_id: CompositorContextId) -> Option<u64>;
+}
+
+impl NestedContexts for NoCompositedContexts {
+ fn frame_generation_of(&self, _child_context_id: CompositorContextId) -> Option<u64> {
+ None
+ }
+}
+
/// What presenting a frame of a context works with besides the context's own state.
pub struct PresentingEnvironment<'a> {
pub context_id: CompositorContextId,
@@ -43,7 +54,7 @@ pub struct PresentingEnvironment<'a> {
pub now: MonotonicTime,
pub has_gpu: bool,
pub window_server_may_show_high_dynamic_range: bool,
- pub composited_contexts: &'a dyn CompositedContexts,
+ pub nested_contexts: &'a dyn NestedContexts,
}
/// A frame drawn into a store, or for a frame that changes only what the window server shows above the frame on
@@ -117,6 +128,7 @@ pub struct ContextPresenting {
pending_present_frame: Option<PendingFrame>,
pending_present_frame_scheduled: bool,
presented_frame: Option<IntRect>,
+ frame_generation: u64,
store_publication: StorePublication,
frame_without_store_in_flight: bool,
@@ -154,6 +166,7 @@ impl ContextPresenting {
pending_present_frame: None,
pending_present_frame_scheduled: false,
presented_frame: None,
+ frame_generation: 0,
store_publication: StorePublication::Unpublished,
frame_without_store_in_flight: false,
latest_rendered_surface: None,
@@ -392,7 +405,12 @@ impl ContextPresenting {
})
}
+ pub fn frame_generation(&self) -> u64 {
+ self.frame_generation
+ }
+
pub fn queue_present_frame(&mut self, pending_frame: PendingFrame) {
+ self.frame_generation += 1;
let Some(queued) = &mut self.pending_present_frame else {
self.pending_present_frame = Some(pending_frame);
return;
@@ -555,6 +573,7 @@ impl ContextPresenting {
frame_damage.layers.shown_by_window_server && frame_damage.live_media_changed;
self.commit_layer_update(env, frame_damage.layers);
self.remember_painted_frame(
+ env,
pending_frame.viewport_rect,
display_list,
nested_display_lists,
@@ -589,6 +608,7 @@ impl ContextPresenting {
}
self.commit_layer_update(env, frame_damage.layers);
self.remember_painted_frame(
+ env,
pending_frame.viewport_rect,
display_list,
nested_display_lists,
@@ -645,6 +665,7 @@ impl ContextPresenting {
let _ = self.paint_frame(env, &surface, &frame, repaint_rect, &mut frame_damage.layers);
self.commit_layer_update(env, frame_damage.layers);
self.remember_painted_frame(
+ env,
pending_frame.viewport_rect,
display_list,
nested_display_lists,
@@ -701,7 +722,7 @@ impl ContextPresenting {
frame_inputs: &painted_frame.frame_inputs,
};
let snapshot_env = PresentingEnvironment {
- composited_contexts: &libcompositing_rust::display_list::replay::NoCompositedContexts,
+ nested_contexts: &NoCompositedContexts,
..*env
};
self.composite_layers_above_base(&snapshot_env, &composited, &frame, source_rect);
@@ -844,7 +865,7 @@ pub(crate) fn replay_into(
scrollbars: &scrollbars,
time_ns: frame_inputs.painted_at.nanoseconds(),
nested_display_lists: frame.nested_display_lists.as_ref(),
- composited_contexts: env.composited_contexts,
+ composited_contexts: env.nested_contexts,
};
let storage = display_list.storage();
let tree = frame_inputs.visual_context_tree.as_arc();