Skip to content

Truly independent web browser

Compositor: Retire the backing stores of tabs hidden for a while

Aliaksandr Kalenik742ed575363b
diff --git a/Services/Compositor/BackingStoreManager.cpp b/Services/Compositor/BackingStoreManager.cpp
index b1f19c8d4fd..1fb572bc3cf 100644
--- a/Services/Compositor/BackingStoreManager.cpp
+++ b/Services/Compositor/BackingStoreManager.cpp
@@ -176,15 +176,8 @@ ErrorOr<Optional<BackingStoreManager::Publication>> BackingStoreManager::allocat
m_backing_stores.ensure_capacity(buffer_count);
if (publication_mode == PublicationMode::Unpublished) {
- for (size_t i = 0; i < buffer_count; ++i) {
- m_backing_stores.append({
- .surface = Gfx::PaintingSurface::create_with_size(allocation.size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context),
- .shared_image_buffer = nullptr,
- .window_server_surface = nullptr,
- .bitmap_id = allocation.bitmap_ids[i],
- .state = BufferState::Available,
- });
- }
+ for (size_t i = 0; i < buffer_count; ++i)
+ m_backing_stores.append(create_offscreen_backing_store(allocation.size, allocation.bitmap_ids[i], skia_backend_context));
return Optional<Publication> {};
}
@@ -259,6 +252,17 @@ ErrorOr<Optional<BackingStoreManager::Publication>> BackingStoreManager::allocat
} };
}
+BackingStoreManager::BackingStore BackingStoreManager::create_offscreen_backing_store(Gfx::IntSize size, i32 bitmap_id, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)
+{
+ return {
+ .surface = Gfx::PaintingSurface::create_with_size(size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context),
+ .shared_image_buffer = nullptr,
+ .window_server_surface = nullptr,
+ .bitmap_id = bitmap_id,
+ .state = BufferState::Available,
+ };
+}
+
BackingStoreManager::BackingStore BackingStoreManager::create_shareable_backing_store(Gfx::IntSize size, i32 bitmap_id, BufferState state, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)
{
auto shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::create(size));
@@ -285,6 +289,29 @@ ErrorOr<BackingStoreManager::BackingStore> BackingStoreManager::create_backing_s
};
}
+// GPU stores shared with the presentation are only ever allocated together, as the presentation installs them.
+bool BackingStoreManager::backing_stores_can_be_added_one_at_a_time() const
+{
+ if (m_backing_stores.is_empty())
+ return false;
+ if (m_publication_mode != PublicationMode::Published)
+ return true;
+ return m_backing_stores.first().shared_image_buffer.ptr() != nullptr;
+}
+
+ErrorOr<BackingStoreManager::BackingStore> BackingStoreManager::create_backing_store_like_the_allocated_ones(i32 bitmap_id, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)
+{
+ switch (m_publication_mode) {
+ case PublicationMode::Unpublished:
+ return create_offscreen_backing_store(m_backing_store_size, bitmap_id, skia_backend_context);
+ case PublicationMode::Published:
+ return create_shareable_backing_store(m_backing_store_size, bitmap_id, BufferState::Available, skia_backend_context);
+ case PublicationMode::ShownInRemoteLayer:
+ return create_backing_store_shown_by_window_server(m_backing_store_size, bitmap_id, skia_backend_context);
+ }
+ VERIFY_NOT_REACHED();
+}
+
// The window server lets go of a store some time after the client replaced it on screen, which at high refresh rates
// can be later than the next frame is due. Rather than hold that frame back, give it a store of its own. While the
// client has yet to take a frame, or to release a store, there is no telling what the window server holds, and the
@@ -308,14 +335,10 @@ Optional<BackingStoreManager::Publication> BackingStoreManager::add_backing_stor
}
auto bitmap_id = m_next_bitmap_id++;
- if (m_publication_mode == PublicationMode::ShownInRemoteLayer) {
- auto store = create_backing_store_shown_by_window_server(m_backing_store_size, bitmap_id, skia_backend_context);
- if (store.is_error())
- return {};
- m_backing_stores.append(store.release_value());
- } else {
- m_backing_stores.append(create_shareable_backing_store(m_backing_store_size, bitmap_id, BufferState::Available, skia_backend_context));
- }
+ auto store = create_backing_store_like_the_allocated_ones(bitmap_id, skia_backend_context);
+ if (store.is_error())
+ return {};
+ m_backing_stores.append(store.release_value());
Vector<Gfx::SharedImage> shared_images;
if (m_publication_mode == PublicationMode::Published)
@@ -352,6 +375,81 @@ Vector<i32> BackingStoreManager::retire_idle_surplus_backing_stores()
return retired_bitmap_ids;
}
+// A hidden context keeps only the store that holds its latest frame, which is what shows it until it is shown again,
+// and what a snapshot of it is taken from. Before any frame, the store reserved as the presentation's first front
+// store stays too.
+Vector<i32> BackingStoreManager::retire_backing_stores_except_the_latest_rendered()
+{
+ Vector<i32> retired_bitmap_ids;
+ if (is_rendering())
+ return retired_bitmap_ids;
+
+ for (size_t i = m_backing_stores.size(); i-- > 0;) {
+ auto const& store = m_backing_stores[i];
+ if (m_latest_rendered_store_index == i)
+ continue;
+ if (store.state != BufferState::Available || shared_surface_is_in_use(store))
+ continue;
+
+ retired_bitmap_ids.append(store.bitmap_id);
+ m_backing_stores.remove(i);
+ if (m_latest_rendered_store_index.has_value() && *m_latest_rendered_store_index > i)
+ m_latest_rendered_store_index = *m_latest_rendered_store_index - 1;
+ }
+
+ // Stores that never held a frame are allocated anew when the context is shown.
+ if (m_backing_stores.is_empty())
+ invalidate();
+ return retired_bitmap_ids;
+}
+
+// Whether a store that the hidden context does not keep is still rendered into, shown, or read by the window server,
+// and can be retired once it no longer is.
+bool BackingStoreManager::has_backing_stores_left_to_retire_while_hidden() const
+{
+ for (size_t i = 0; i < m_backing_stores.size(); ++i) {
+ if (m_latest_rendered_store_index == i)
+ continue;
+ auto const& store = m_backing_stores[i];
+ switch (store.state) {
+ case BufferState::Rendering:
+ return true;
+ case BufferState::Presented:
+ // Before any frame, a presented store is the one reserved as the presentation's first front store, which
+ // the presentation holds until it is shown a frame.
+ if (m_latest_rendered_store_index.has_value())
+ return true;
+ break;
+ case BufferState::Available:
+ if (shared_surface_is_in_use(store))
+ return true;
+ break;
+ }
+ }
+ return false;
+}
+
+// Gives a context that was shown again the stores it had before it was hidden. Answers nothing where they can't be
+// added to the ones it kept, or one could not be made, as the context then needs a whole new allocation.
+Optional<BackingStoreManager::Publication> BackingStoreManager::add_backing_stores_up_to_initial_count(RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)
+{
+ if (!backing_stores_can_be_added_one_at_a_time())
+ return {};
+
+ Publication publication;
+ while (m_backing_stores.size() < m_initial_backing_store_count) {
+ auto bitmap_id = m_next_bitmap_id++;
+ auto store = create_backing_store_like_the_allocated_ones(bitmap_id, skia_backend_context);
+ if (store.is_error())
+ return {};
+ m_backing_stores.append(store.release_value());
+ publication.bitmap_ids.append(bitmap_id);
+ if (m_publication_mode == PublicationMode::Published)
+ publication.shared_images.append(m_backing_stores.last().shared_image_buffer->export_shared_image());
+ }
+ return publication;
+}
+
bool BackingStoreManager::is_valid() const
{
return !m_backing_stores.is_empty();
diff --git a/Services/Compositor/BackingStoreManager.h b/Services/Compositor/BackingStoreManager.h
index eab6bd25bf8..6061b1ae26c 100644
--- a/Services/Compositor/BackingStoreManager.h
+++ b/Services/Compositor/BackingStoreManager.h
@@ -82,6 +82,10 @@ public:
bool has_surplus_backing_stores() const { return m_backing_stores.size() > m_initial_backing_store_count; }
Vector<i32> retire_idle_surplus_backing_stores();
+ Vector<i32> retire_backing_stores_except_the_latest_rendered();
+ bool has_backing_stores_left_to_retire_while_hidden() const;
+ Optional<Publication> add_backing_stores_up_to_initial_count(RefPtr<Gfx::SkiaBackendContext> const&);
+
private:
enum class BufferState : u8 {
Available,
@@ -101,8 +105,11 @@ private:
static constexpr size_t maximum_backing_store_count = 8;
+ static BackingStore create_offscreen_backing_store(Gfx::IntSize, i32 bitmap_id, RefPtr<Gfx::SkiaBackendContext> const&);
static BackingStore create_shareable_backing_store(Gfx::IntSize, i32 bitmap_id, BufferState, RefPtr<Gfx::SkiaBackendContext> const&);
ErrorOr<BackingStore> create_backing_store_shown_by_window_server(Gfx::IntSize, i32 bitmap_id, RefPtr<Gfx::SkiaBackendContext> const&);
+ bool backing_stores_can_be_added_one_at_a_time() const;
+ ErrorOr<BackingStore> create_backing_store_like_the_allocated_ones(i32 bitmap_id, RefPtr<Gfx::SkiaBackendContext> const&);
static bool shared_surface_is_in_use(BackingStore const&);
static bool store_can_be_rendered_into(BackingStore const&);
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index 3d26bf03087..ea8febd1465 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -1200,6 +1200,30 @@ bool CompositorState::retire_idle_surplus_stores(Web::CompositorContextId contex
return backing_stores.has_surplus_backing_stores();
}
+bool CompositorState::retire_stores_of_hidden_context(Web::CompositorContextId context_id, Optional<PresentationId> publishing_presentation)
+{
+ auto& backing_stores = presented_context(context_id).raster_targets.backing_stores();
+ auto retired_bitmap_ids = backing_stores.retire_backing_stores_except_the_latest_rendered();
+ if (auto* presentation = publishing_presentation.has_value() ? presentation_if_present(*publishing_presentation) : nullptr; presentation && !retired_bitmap_ids.is_empty()) {
+ dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Context {} retired backing stores {} while hidden", context_id, retired_bitmap_ids);
+ presentation->did_retire_backing_stores(context_id, move(retired_bitmap_ids));
+ }
+ return backing_stores.has_backing_stores_left_to_retire_while_hidden();
+}
+
+bool CompositorState::restore_stores_retired_while_hidden(Web::CompositorContextId context_id, Optional<PresentationId> publishing_presentation)
+{
+ auto& backing_stores = presented_context(context_id).raster_targets.backing_stores();
+ auto publication = backing_stores.add_backing_stores_up_to_initial_count(m_skia_backend_context);
+ if (!publication.has_value())
+ return false;
+ if (auto* presentation = publishing_presentation.has_value() ? presentation_if_present(*publishing_presentation) : nullptr; presentation && !publication->bitmap_ids.is_empty()) {
+ dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Context {} regained backing stores {} as it was shown", context_id, publication->bitmap_ids);
+ presentation->did_add_backing_stores(context_id, move(publication->bitmap_ids), move(publication->shared_images));
+ }
+ return true;
+}
+
bool CompositorState::take_registered_image_frames(Web::CompositorContextId context_id, ReadonlySpan<u64> image_frame_ids)
{
auto& context = presented_context(context_id);
@@ -1731,6 +1755,8 @@ RustFFI::FfiRasterHost CompositorState::raster_host(CompositorState& state)
.latest_rendered_store_surface = [](void* host, u64 context_id) -> void* { return state_of(host).presented_context(Context { context_id }).raster_targets.backing_stores().latest_rendered_surface().ptr(); },
.add_store_if_window_server_reads_every_released_store = [](void* host, u64 context_id, bool has_presentation, u64 presentation_id) { return state_of(host).add_store_if_window_server_reads_every_released_store(Context { context_id }, presentation_of(has_presentation, presentation_id)); },
.retire_idle_surplus_stores = [](void* host, u64 context_id, bool has_presentation, u64 presentation_id) { return state_of(host).retire_idle_surplus_stores(Context { context_id }, presentation_of(has_presentation, presentation_id)); },
+ .retire_stores_of_hidden_context = [](void* host, u64 context_id, bool has_presentation, u64 presentation_id) { return state_of(host).retire_stores_of_hidden_context(Context { context_id }, presentation_of(has_presentation, presentation_id)); },
+ .restore_stores_retired_while_hidden = [](void* host, u64 context_id, bool has_presentation, u64 presentation_id) { return state_of(host).restore_stores_retired_while_hidden(Context { context_id }, presentation_of(has_presentation, presentation_id)); },
.next_presentable_surface = [](void* host, u64 context_id, u32 pool, Gfx::IntSize size, void* like) -> void* {
auto& state = state_of(host);
auto skia_backend_context = like ? painting_surface_of(like).skia_backend_context() : state.m_skia_backend_context;
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index 76a1649b47b..a698ff57f73 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -231,6 +231,8 @@ private:
RustFFI::StoreResizeOutcome resize_stores(Web::CompositorContextId, RustFFI::FfiStoreResize const&);
bool add_store_if_window_server_reads_every_released_store(Web::CompositorContextId, Optional<PresentationId>);
bool retire_idle_surplus_stores(Web::CompositorContextId, Optional<PresentationId>);
+ bool retire_stores_of_hidden_context(Web::CompositorContextId, Optional<PresentationId>);
+ bool restore_stores_retired_while_hidden(Web::CompositorContextId, Optional<PresentationId>);
bool take_registered_image_frames(Web::CompositorContextId, ReadonlySpan<u64> image_frame_ids);
void set_drawn_image_frames(Web::CompositorContextId, ReadonlySpan<u64> image_frame_ids);
void set_drawn_video_sinks(Web::CompositorContextId, ReadonlySpan<u64> handles);
diff --git a/Services/Compositor/Rust/src/context_state/mod.rs b/Services/Compositor/Rust/src/context_state/mod.rs
index afc125466e6..99155b45cfc 100644
--- a/Services/Compositor/Rust/src/context_state/mod.rs
+++ b/Services/Compositor/Rust/src/context_state/mod.rs
@@ -645,7 +645,9 @@ impl ContextState {
}
}
// The core handles the timers of presenting.
- ContextTimer::BackingStoreShrink | ContextTimer::SurplusStoreRetirement => {}
+ ContextTimer::BackingStoreShrink
+ | ContextTimer::SurplusStoreRetirement
+ | ContextTimer::HiddenStoreRetirement => {}
}
None
}
diff --git a/Services/Compositor/Rust/src/core/mod.rs b/Services/Compositor/Rust/src/core/mod.rs
index 2e42a513eab..0b3ad2aca80 100644
--- a/Services/Compositor/Rust/src/core/mod.rs
+++ b/Services/Compositor/Rust/src/core/mod.rs
@@ -783,9 +783,20 @@ impl CompositorCore {
let event = context.borrow_mut().end_scroll_fling();
self.dispatch_scroll_fling_step(context_id, context, event, now);
}
- let became_visible = context.borrow().visibility() != visibility && visibility == ContextVisibility::Visible;
+ let visibility_changes = context.borrow().visibility() != visibility;
+ let became_visible = visibility_changes && visibility == ContextVisibility::Visible;
context.borrow_mut().set_visibility(visibility);
+ if visibility_changes
+ && visibility == ContextVisibility::Hidden
+ && let Some(presenting) = self.presenting_of(context_id)
+ {
+ presenting
+ .borrow()
+ .schedule_hidden_store_retirement(context_id, &self.host);
+ }
if became_visible {
+ self.host.cancel_timer(context_id, ContextTimer::HiddenStoreRetirement);
+ self.restore_backing_stores_retired_while_hidden(context_id, now);
self.resume_presentation_after_becoming_visible(context_id, now);
}
self.finish_entry(now);
@@ -806,6 +817,7 @@ impl CompositorCore {
self.resize_backing_stores_if_needed(context_id, now);
}
ContextTimer::SurplusStoreRetirement => self.retire_idle_surplus_backing_stores(context_id),
+ ContextTimer::HiddenStoreRetirement => self.retire_backing_stores_of_hidden_context(context_id),
_ => {
let caret_damage_rect = context.borrow_mut().timer_fired(timer, now);
if let Some(caret_damage_rect) = caret_damage_rect {
diff --git a/Services/Compositor/Rust/src/core/presenting.rs b/Services/Compositor/Rust/src/core/presenting.rs
index 6803b90cda0..b60b155b972 100644
--- a/Services/Compositor/Rust/src/core/presenting.rs
+++ b/Services/Compositor/Rust/src/core/presenting.rs
@@ -676,11 +676,19 @@ impl CompositorCore {
!capabilities.is_some_and(|capabilities| capabilities.cannot_present_gpu_images)
}
- pub(super) fn resize_backing_stores_if_needed(&self, context_id: CompositorContextId, now: MonotonicTime) {
+ pub(super) fn resize_backing_stores_if_needed(
+ &self,
+ context_id: CompositorContextId,
+ now: MonotonicTime,
+ ) -> StoreResizeOutcome {
let (Some(context), Some(presenting)) = (self.context_if_present(context_id), self.presenting_of(context_id))
else {
- return;
+ return StoreResizeOutcome::Unchanged;
};
+ // A hidden context renders nothing into its stores, so they are fitted to it once it is shown again.
+ if presenting.borrow().stores_are_retired_while_hidden() {
+ return StoreResizeOutcome::Unchanged;
+ }
let bound_presentation = context.borrow().bound_presentation();
let publication = if presenting.borrow().shown_in_remote_layer {
StorePublication::ShownInRemoteLayer
@@ -705,6 +713,7 @@ impl CompositorCore {
self.stop_showing_context_in_remote_layer(context_id, now);
}
}
+ outcome
}
pub(super) fn add_backing_store_for_pending_frame_if_needed(&self, context_id: CompositorContextId) {
@@ -753,6 +762,64 @@ impl CompositorCore {
}
}
+ pub(super) fn retire_backing_stores_of_hidden_context(&self, context_id: CompositorContextId) {
+ let (Some(context), Some(presenting)) = (self.context_if_present(context_id), self.presenting_of(context_id))
+ else {
+ return;
+ };
+ if context.borrow().visibility() == ContextVisibility::Visible {
+ return;
+ }
+ let bound_presentation = context.borrow().bound_presentation();
+ let mut presenting = presenting.borrow_mut();
+ if !presenting.presents_to_client {
+ return;
+ }
+ presenting.start_retiring_stores_while_hidden();
+ let publishing_presentation = bound_presentation
+ .filter(|_| presenting.store_publication() == StorePublication::Published)
+ .map(|presentation| presentation.0);
+ if self
+ .raster
+ .retire_stores_of_hidden_context(context_id, publishing_presentation)
+ {
+ presenting.retry_hidden_store_retirement(context_id, &self.host);
+ }
+ }
+
+ /// Gives a context that is shown again the stores it retired while it was hidden, before it presents.
+ pub(super) fn restore_backing_stores_retired_while_hidden(
+ &self,
+ context_id: CompositorContextId,
+ now: MonotonicTime,
+ ) {
+ let (Some(context), Some(presenting)) = (self.context_if_present(context_id), self.presenting_of(context_id))
+ else {
+ return;
+ };
+ if !presenting.borrow_mut().stop_retiring_stores_as_shown() {
+ return;
+ }
+ // Stores that no longer fit the context, as its viewport or how it is shown changed while it was hidden, are
+ // allocated anew.
+ if self.resize_backing_stores_if_needed(context_id, now) != StoreResizeOutcome::Unchanged {
+ return;
+ }
+ let bound_presentation = context.borrow().bound_presentation();
+ let publishing_presentation = bound_presentation
+ .filter(|_| presenting.borrow().store_publication() == StorePublication::Published)
+ .map(|presentation| presentation.0);
+ if self
+ .raster
+ .restore_stores_retired_while_hidden(context_id, publishing_presentation)
+ {
+ return;
+ }
+ let environment = self.environment(context_id, now, &NoCompositedContexts);
+ presenting.borrow_mut().invalidate_backing_stores(&environment);
+ self.resize_backing_stores_if_needed(context_id, now);
+ }
+
// A context scrolls elastically, takes presses on scrollbar tracks, and ends touchpad gestures as the presentation of
// its root context does. A platform that reports the momentum of a flick reports when the fingers lift for it.
pub(super) fn update_scroll_capabilities(&self) {
diff --git a/Services/Compositor/Rust/src/host.rs b/Services/Compositor/Rust/src/host.rs
index f2fc54718b8..fc8b5267e74 100644
--- a/Services/Compositor/Rust/src/host.rs
+++ b/Services/Compositor/Rust/src/host.rs
@@ -31,6 +31,7 @@ pub enum ContextTimer {
CaretBlink,
BackingStoreShrink,
SurplusStoreRetirement,
+ HiddenStoreRetirement,
}
#[derive(Clone, Copy, Debug, PartialEq)]
diff --git a/Services/Compositor/Rust/src/presenting/mod.rs b/Services/Compositor/Rust/src/presenting/mod.rs
index eff629c5443..e7e0fc822be 100644
--- a/Services/Compositor/Rust/src/presenting/mod.rs
+++ b/Services/Compositor/Rust/src/presenting/mod.rs
@@ -38,6 +38,13 @@ use std::rc::Rc;
const BACKING_STORE_SHRINK_DELAY: Duration = Duration::from_milliseconds(3000);
const SURPLUS_STORE_RETIREMENT_DELAY: Duration = Duration::from_milliseconds(3000);
+// A tab the user left is likely to stay in the background for long, so once it was hidden for this long, its context
+// keeps only the store that holds its latest frame. The wait keeps a user who switches back and forth between tabs from
+// paying for new stores each time.
+const TIME_HIDDEN_BEFORE_RETIRING_STORES: Duration = Duration::from_milliseconds(10_000);
+// A store that is still rendered into, shown or read when the hidden context retires its stores is retired this much
+// later.
+const HIDDEN_STORE_RETIREMENT_RETRY_DELAY: Duration = Duration::from_milliseconds(3000);
pub trait NestedContexts: CompositedContexts {
fn frame_generation_of(&self, child_context_id: CompositorContextId) -> Option<u64>;
@@ -142,6 +149,9 @@ pub struct ContextPresenting {
store_damage: StoreDamage,
damage_surface: Option<SurfaceHandle>,
surplus_store_retirement_is_scheduled: bool,
+ // The context was hidden long enough to keep only the store that holds its latest frame, and gets no new stores
+ // until it is shown again.
+ stores_are_retired_while_hidden: bool,
surface_drawn_by_parent: Option<SurfaceHandle>,
unbounded_raster_scale_of_embedding: FloatSize,
@@ -178,6 +188,7 @@ impl ContextPresenting {
store_damage: StoreDamage::default(),
damage_surface: None,
surplus_store_retirement_is_scheduled: false,
+ stores_are_retired_while_hidden: false,
surface_drawn_by_parent: None,
unbounded_raster_scale_of_embedding: FloatSize {
width: 1.0,
@@ -328,6 +339,37 @@ impl ContextPresenting {
self.surplus_store_retirement_is_scheduled = false;
}
+ pub fn schedule_hidden_store_retirement(&self, context_id: CompositorContextId, host: &Host) {
+ host.set_timer(
+ context_id,
+ ContextTimer::HiddenStoreRetirement,
+ TIME_HIDDEN_BEFORE_RETIRING_STORES,
+ );
+ }
+
+ pub fn retry_hidden_store_retirement(&self, context_id: CompositorContextId, host: &Host) {
+ host.set_timer(
+ context_id,
+ ContextTimer::HiddenStoreRetirement,
+ HIDDEN_STORE_RETIREMENT_RETRY_DELAY,
+ );
+ }
+
+ pub fn stores_are_retired_while_hidden(&self) -> bool {
+ self.stores_are_retired_while_hidden
+ }
+
+ pub fn start_retiring_stores_while_hidden(&mut self) {
+ self.stores_are_retired_while_hidden = true;
+ // The scratch surface of partial repaints is made again for the next one.
+ self.damage_surface = None;
+ }
+
+ /// Answers whether the stores were retired while the context was hidden.
+ pub fn stop_retiring_stores_as_shown(&mut self) -> bool {
+ std::mem::take(&mut self.stores_are_retired_while_hidden)
+ }
+
pub fn did_finish_gpu_present(&mut self, env: &PresentingEnvironment<'_>, bitmap_id: i32, frame_size: IntSize) {
env.raster.complete_rendering(env.context_id, bitmap_id);
self.latest_rendered_surface = env.raster.latest_rendered_store_surface(env.context_id);
diff --git a/Services/Compositor/Rust/src/raster_host.rs b/Services/Compositor/Rust/src/raster_host.rs
index 6ea3b158dcd..a2aeeee8831 100644
--- a/Services/Compositor/Rust/src/raster_host.rs
+++ b/Services/Compositor/Rust/src/raster_host.rs
@@ -247,6 +247,12 @@ pub struct FfiRasterHost {
unsafe extern "C" fn(*mut c_void, u64, bool, u64) -> bool,
/// The same arguments. Answers whether surplus stores are left.
pub retire_idle_surplus_stores: unsafe extern "C" fn(*mut c_void, u64, bool, u64) -> bool,
+ /// The same arguments. Retires every store but the one that holds the latest frame, and answers whether a store
+ /// is left that can be retired once nothing renders into, shows or reads it.
+ pub retire_stores_of_hidden_context: unsafe extern "C" fn(*mut c_void, u64, bool, u64) -> bool,
+ /// The same arguments. Answers false where the stores could not be added back to the one kept, so that the context
+ /// needs a whole new allocation.
+ pub restore_stores_retired_while_hidden: unsafe extern "C" fn(*mut c_void, u64, bool, u64) -> bool,
/// Arguments: the context, the pool, the size, and a surface the new one is made like. Null where none could be had.
pub next_presentable_surface: unsafe extern "C" fn(*mut c_void, u64, u32, IntSize, *mut c_void) -> *mut c_void,
@@ -624,6 +630,38 @@ impl RasterHost {
}
}
+ pub fn retire_stores_of_hidden_context(
+ &self,
+ context_id: CompositorContextId,
+ presentation_id: Option<u64>,
+ ) -> bool {
+ // SAFETY: The host outlives this.
+ unsafe {
+ (self.0.retire_stores_of_hidden_context)(
+ self.host(),
+ context_id.0,
+ presentation_id.is_some(),
+ presentation_id.unwrap_or_default(),
+ )
+ }
+ }
+
+ pub fn restore_stores_retired_while_hidden(
+ &self,
+ context_id: CompositorContextId,
+ presentation_id: Option<u64>,
+ ) -> bool {
+ // SAFETY: The host outlives this.
+ unsafe {
+ (self.0.restore_stores_retired_while_hidden)(
+ self.host(),
+ context_id.0,
+ presentation_id.is_some(),
+ presentation_id.unwrap_or_default(),
+ )
+ }
+ }
+
pub fn next_presentable_surface(
&self,
context_id: CompositorContextId,