Skip to content

Truly independent web browser

Compositor: Paint a nested context only when its parent draws it

Aliaksandr Kalenikb1e1625b2af9
diff --git a/Services/Compositor/Rust/src/core/presenting.rs b/Services/Compositor/Rust/src/core/presenting.rs
index ba062823b0c..2ef6522a2a1 100644
--- a/Services/Compositor/Rust/src/core/presenting.rs
+++ b/Services/Compositor/Rust/src/core/presenting.rs
@@ -125,6 +125,29 @@ impl CompositorCore {
}
}
+ fn nesting_depth_of(&self, context_id: CompositorContextId) -> usize {
+ let mut depth = 0;
+ let mut current = context_id;
+ while depth < self.contexts.len() {
+ let Some(parent) = self
+ .context_if_present(current)
+ .and_then(|context| context.borrow().parent_context_id())
+ .filter(|parent| self.context_if_present(*parent).is_some())
+ else {
+ break;
+ };
+ depth += 1;
+ current = parent;
+ }
+ depth
+ }
+
+ fn context_ids_nested_before_their_parents(&self) -> Vec<CompositorContextId> {
+ let mut context_ids: Vec<CompositorContextId> = self.contexts.keys().copied().collect();
+ context_ids.sort_by_cached_key(|&context_id| std::cmp::Reverse(self.nesting_depth_of(context_id)));
+ context_ids
+ }
+
fn root_context_id_of(&self, context_id: CompositorContextId) -> CompositorContextId {
let mut current = context_id;
for _ in 0..=self.contexts.len() {
@@ -339,20 +362,7 @@ impl CompositorCore {
}
// A context nested in itself, through others, is already being painted.
let mut child_presenting = self.presenting_of(child_context_id)?.try_borrow_mut().ok()?;
- let is_rendering = self.raster.store_state(child_context_id).is_rendering;
- if child_presenting.update_composited_raster_transform(is_rendering, destination_rect, canvas_matrix) {
- let environment = self.environment(child_context_id, now, &NoCompositedContexts);
- let outcome = child_presenting.resize_backing_stores_if_needed(
- &environment,
- StorePublication::Unpublished,
- false,
- None,
- );
- assert!(matches!(
- outcome,
- StoreResizeOutcome::Unchanged | StoreResizeOutcome::ReallocatedOffscreen
- ));
- }
+ child_presenting.update_composited_raster_transform(destination_rect, canvas_matrix);
if child_presenting.needs_rasterization() {
let (display_list, nested_display_lists, frame_inputs) = {
let mut child_state = child_state.try_borrow_mut().ok()?;
@@ -368,7 +378,7 @@ impl CompositorCore {
now,
};
let environment = self.environment(child_context_id, now, &nested_contexts);
- child_presenting.present_synchronously(
+ child_presenting.paint_frame_drawn_by_parent(
&environment,
display_list.as_ref(),
&nested_display_lists,
@@ -525,13 +535,9 @@ impl CompositorCore {
}
presenting.presents_to_client
};
- let (bound_presentation, page_id, parent_context_id) = {
+ let (bound_presentation, page_id) = {
let context = context.borrow();
- (
- context.bound_presentation(),
- context.page_id(),
- context.parent_context_id(),
- )
+ (context.bound_presentation(), context.page_id())
};
let presentation = if delivery.publish_to_presentation {
bound_presentation
@@ -564,9 +570,6 @@ impl CompositorCore {
);
}
self.resize_backing_stores_if_needed(context_id, now);
- if let Some(parent_context_id) = parent_context_id {
- self.present_current_frame(parent_context_id, now);
- }
self.schedule_pending_present_frame_if_unblocked(context_id, now);
}
@@ -876,7 +879,7 @@ impl CompositorCore {
};
let has_frame = self.presenting_of(context_id).is_some_and(|presenting| {
let presenting = presenting.borrow();
- presenting.has_latest_rendered_surface() || presenting.needs_rasterization()
+ presenting.has_surface_drawn_by_parent() || presenting.needs_rasterization()
});
if has_frame {
self.present_current_frame(parent_context_id, now);
@@ -980,7 +983,7 @@ impl CompositorCore {
for context_id in self.raster.update_video_sinks() {
self.present_changed_live_content(context_id, now);
}
- let context_ids: Vec<CompositorContextId> = self.contexts.keys().copied().collect();
+ let context_ids = self.context_ids_nested_before_their_parents();
// Keep this display's vsync ticking while any sink painted on it may require updates.
for &context_id in &context_ids {
@@ -1060,6 +1063,23 @@ impl CompositorCore {
if !tick.presents_on_display {
continue;
}
+ if !presenting.borrow().presents_to_client {
+ let viewport_rect = presenting.borrow().viewport_rect_of_current_frame();
+ if tick.needs_animation_frames
+ && let Some(viewport_rect) = viewport_rect
+ {
+ presenting
+ .borrow_mut()
+ .queue_present_frame(PendingFrame::repainting_changes(viewport_rect));
+ }
+ let has_pending_present_frame = presenting.borrow().has_pending_present_frame();
+ if has_pending_present_frame {
+ self.schedule_pending_present_frame(context_id, now);
+ } else if tick.needs_animation_frames {
+ self.schedule_animation_frames_if_needed(context_id, now);
+ }
+ continue;
+ }
self.add_backing_store_for_pending_frame_if_needed(context_id);
let pending_frame = presenting
diff --git a/Services/Compositor/Rust/src/presenting/mod.rs b/Services/Compositor/Rust/src/presenting/mod.rs
index dd7aa26d5f8..7345a1a8bf8 100644
--- a/Services/Compositor/Rust/src/presenting/mod.rs
+++ b/Services/Compositor/Rust/src/presenting/mod.rs
@@ -64,8 +64,8 @@ pub(crate) struct FrameToPaint<'a> {
pub frame_inputs: &'a FrameInputs,
}
-// Bounds the embedding scale of a nested context's raster, so that the two backing stores together use at most 128 MiB,
-// even when an embedding transform magnifies a large iframe.
+// Bounds the embedding scale of a nested context's raster, so that its surface uses at most 64 MiB, even when an
+// embedding transform magnifies a large iframe.
fn bounded_composited_raster_scale(mut scale: FloatSize, viewport_size: IntSize) -> FloatSize {
if viewport_size.width <= 0 || viewport_size.height <= 0 {
return FloatSize {
@@ -128,6 +128,7 @@ pub struct ContextPresenting {
damage_surface: Option<SurfaceHandle>,
surplus_store_retirement_is_scheduled: bool,
+ surface_drawn_by_parent: Option<SurfaceHandle>,
unbounded_raster_scale_of_embedding: FloatSize,
raster_scale: FloatSize,
raster_translation: FloatPoint,
@@ -160,6 +161,7 @@ impl ContextPresenting {
store_damage: StoreDamage::default(),
damage_surface: None,
surplus_store_retirement_is_scheduled: false,
+ surface_drawn_by_parent: None,
unbounded_raster_scale_of_embedding: FloatSize {
width: 1.0,
height: 1.0,
@@ -224,24 +226,16 @@ impl ContextPresenting {
pub fn resize_backing_stores_if_needed(
&mut self,
env: &PresentingEnvironment<'_>,
- mut publication: StorePublication,
+ publication: StorePublication,
gpu_sharing_allowed: bool,
presentation_id: Option<u64>,
) -> StoreResizeOutcome {
- if env.raster.store_state(env.context_id).is_rendering {
+ if !self.presents_to_client {
+ self.fit_surface_drawn_by_parent_to_viewport(env);
return StoreResizeOutcome::Unchanged;
}
- if !self.presents_to_client {
- // A resize can arrive before the parent's next replay supplies the new embedding transform. Bound the
- // existing embedding's scale for the new viewport before allocating backing stores. Bounding the already
- // bounded scale instead can round it differently, which would discard the raster on every replay.
- let scale = bounded_composited_raster_scale(self.unbounded_raster_scale_of_embedding, self.viewport_size);
- if self.raster_scale != scale {
- self.raster_scale = scale;
- self.latest_rendered_surface = None;
- self.painted_frame = None;
- }
- publication = StorePublication::Unpublished;
+ if env.raster.store_state(env.context_id).is_rendering {
+ return StoreResizeOutcome::Unchanged;
}
let outcome = env.raster.resize_stores(
env.context_id,
@@ -263,6 +257,25 @@ impl ContextPresenting {
outcome
}
+ // A resize can arrive before the parent's next replay supplies the new embedding transform, so the existing
+ // embedding's scale is bounded for the new viewport. Bounding the already bounded scale instead can round it
+ // differently, which would discard the raster on every replay.
+ fn fit_surface_drawn_by_parent_to_viewport(&mut self, env: &PresentingEnvironment<'_>) {
+ let scale = bounded_composited_raster_scale(self.unbounded_raster_scale_of_embedding, self.viewport_size);
+ if self.raster_scale != scale {
+ self.raster_scale = scale;
+ self.painted_frame = None;
+ }
+ let raster_size = self.raster_size();
+ if self
+ .surface_drawn_by_parent
+ .as_ref()
+ .is_some_and(|surface| env.raster.surface_size(surface) != raster_size)
+ {
+ self.surface_drawn_by_parent = None;
+ }
+ }
+
pub fn invalidate_backing_stores(&mut self, env: &PresentingEnvironment<'_>) {
env.raster.invalidate_stores(env.context_id);
self.frame_without_store_in_flight = false;
@@ -306,19 +319,14 @@ impl ContextPresenting {
self.frame_without_store_in_flight = false;
}
- pub fn has_latest_rendered_surface(&self) -> bool {
- self.latest_rendered_surface.is_some()
+ pub fn has_surface_drawn_by_parent(&self) -> bool {
+ self.surface_drawn_by_parent.is_some()
}
- /// Answers whether the raster has to be made again, at the scale and phase the embedding now draws it at.
- pub fn update_composited_raster_transform(
- &mut self,
- is_rendering: bool,
- destination_rect: FloatRect,
- transform: &FloatMatrix4x4,
- ) -> bool {
- if self.presents_to_client || is_rendering || self.viewport_size.is_empty() || destination_rect.is_empty() {
- return false;
+ /// Makes the next paint redo the raster where the embedding now draws it at another scale or phase.
+ pub fn update_composited_raster_transform(&mut self, destination_rect: FloatRect, transform: &FloatMatrix4x4) {
+ if self.presents_to_client || self.viewport_size.is_empty() || destination_rect.is_empty() {
+ return;
}
let m = &transform.elements;
// Display list coordinates already include device scale and page zoom. Only the embedding transform and
@@ -331,7 +339,7 @@ impl ContextPresenting {
scale_y = ak::math::hypot(m[0][1], m[1][1]) * destination_rect.height / self.viewport_size.height as f32;
}
if !scale_x.is_finite() || !scale_y.is_finite() {
- return false;
+ return;
}
self.unbounded_raster_scale_of_embedding = FloatSize {
width: scale_x,
@@ -346,7 +354,7 @@ impl ContextPresenting {
let mut x = m[0][0] * destination_rect.x + m[0][3];
let mut y = m[1][1] * destination_rect.y + m[1][3];
if !x.is_finite() || !y.is_finite() {
- return false;
+ return;
}
// Mirrored placements need the opposite fractional phase so the remaining image transform still
// lands on integer device pixels.
@@ -363,17 +371,15 @@ impl ContextPresenting {
}
if self.raster_scale == scale && self.raster_translation == translation {
- return false;
+ return;
}
self.raster_scale = scale;
self.raster_translation = translation;
- self.latest_rendered_surface = None;
self.painted_frame = None;
- true
}
pub fn composited_surface(&self) -> Option<ResolvedCompositedContext> {
- let surface = self.latest_rendered_surface.as_ref()?;
+ let surface = self.surface_drawn_by_parent.as_ref()?;
let painted_frame = self.painted_frame.as_ref()?;
Some(ResolvedCompositedContext {
surface: surface.as_ptr(),
@@ -435,9 +441,14 @@ impl ContextPresenting {
self.pending_present_frame.take()
}
+ pub fn has_pending_present_frame(&self) -> bool {
+ self.pending_present_frame.is_some()
+ }
+
pub fn needs_rasterization(&self) -> bool {
self.pending_present_frame.is_some()
- || (self.latest_rendered_surface.is_none() && self.presented_frame.is_some())
+ || (self.presented_frame.is_some()
+ && (self.surface_drawn_by_parent.is_none() || self.painted_frame.is_none()))
}
pub fn frame_rect_to_repaint(&self) -> Option<IntRect> {
@@ -593,28 +604,23 @@ impl ContextPresenting {
})
}
- /// Paints the pending frame, or the frame presented last, into a store at once, for a context that its parent draws.
- pub fn present_synchronously(
+ /// Paints the pending frame, or the frame presented last, into the surface of a context that its parent draws.
+ pub fn paint_frame_drawn_by_parent(
&mut self,
env: &PresentingEnvironment<'_>,
display_list: Option<&Rc<DisplayList>>,
nested_display_lists: &Rc<NestedDisplayListStore>,
frame_inputs: Option<FrameInputs>,
- ) -> bool {
- if !self.can_render_frame(display_list, env.raster, env.context_id) {
- return false;
- }
- let display_list = display_list.expect("a frame that can render has a display list");
+ ) {
+ let Some(display_list) = display_list else {
+ return;
+ };
let frame_inputs = frame_inputs.expect("a context with a display list has frame inputs");
- // Don't race an async present already in flight for this context; its own completion will update the output.
- if self.is_present_blocked(env.raster, env.context_id) {
- return false;
- }
let pending_frame = match self.pending_present_frame {
Some(pending_frame) => pending_frame,
None => match self.presented_frame {
Some(presented_frame) => PendingFrame::repainting_everything(presented_frame),
- None => return false,
+ None => return,
},
};
let live_content = self.live_content(env, display_list, nested_display_lists);
@@ -624,13 +630,19 @@ impl ContextPresenting {
frame_inputs: &frame_inputs,
};
let mut frame_damage = self.frame_damage_for(env, &pending_frame, &frame, &live_content);
- let raster_damage_rect = self.raster_damage_rect(frame_damage.rect);
- let Some((bitmap_id, store)) = env.raster.acquire_store(env.context_id) else {
- return false;
+ let raster_size = self.raster_size();
+ let (surface, repaint_rect) = match self.surface_drawn_by_parent.take() {
+ Some(surface) if env.raster.surface_size(&surface) == raster_size => {
+ (surface, self.raster_damage_rect(frame_damage.rect))
+ }
+ _ => {
+ let Some(surface) = env.raster.create_surface(raster_size, None) else {
+ return;
+ };
+ (surface, rect_of_size(raster_size))
+ }
};
- let store_rect = rect_of_size(env.raster.surface_size(&store));
- let repaint_rect = self.store_damage.take(bitmap_id, raster_damage_rect, store_rect);
- let _ = self.paint_frame(env, &store, &frame, repaint_rect, &mut frame_damage.layers);
+ 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(
pending_frame.viewport_rect,
@@ -639,17 +651,11 @@ impl ContextPresenting {
frame_inputs,
&live_content,
);
- env.raster.flush(&store);
- env.raster.complete_rendering(env.context_id, bitmap_id);
- self.latest_rendered_surface = env.raster.latest_rendered_store_surface(env.context_id);
- self.size_of_latest_rendered_frame = IntSize {
- width: pending_frame.viewport_rect.width,
- height: pending_frame.viewport_rect.height,
- };
+ env.raster.flush(&surface);
+ self.surface_drawn_by_parent = Some(surface);
self.presented_frame = Some(pending_frame.viewport_rect);
self.pending_present_frame = None;
self.pending_present_frame_scheduled = false;
- true
}
/// Paints a screenshot into the target, without the overlays of the browser and at the raster's own scale.