Skip to content

Truly independent web browser

LibCompositing+LibWeb+Compositor: Sweep unused SVG image rasters

Aliaksandr Kalenik4b2399517d4e
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp
index dcf4013ea35..70fef1afaab 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp
+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp
@@ -30,8 +30,7 @@ struct DisplayListStoredImageFrameResource {
bool DisplayListResourceSet::is_empty() const
{
return fonts.is_empty()
- && image_frames.is_empty()
- && vector_images.is_empty();
+ && image_frames.is_empty();
}
void DisplayListResourceSet::include(DisplayListResourceSet const& other)
@@ -40,8 +39,6 @@ void DisplayListResourceSet::include(DisplayListResourceSet const& other)
fonts.set(id, AK::HashSetExistingEntryBehavior::Keep);
for (auto id : other.image_frames)
image_frames.set(id, AK::HashSetExistingEntryBehavior::Keep);
- for (auto id : other.vector_images)
- vector_images.set(id, AK::HashSetExistingEntryBehavior::Keep);
}
DisplayListResourceStorage::DisplayListResourceStorage() = default;
@@ -163,10 +160,6 @@ DisplayListResourceTransaction DisplayListResourceStorage::create_transaction(
if (!current.image_frames.contains(id))
transaction.image_frame_ids_to_remove.append(id);
}
- for (auto id : previous.vector_images) {
- if (!current.vector_images.contains(id))
- transaction.vector_image_ids_to_remove.append(id);
- }
return transaction;
}
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h
index a3543dfeb02..3ebd1082e99 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h
+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h
@@ -35,7 +35,6 @@ struct COMPOSITING_API DisplayListResourceSet {
HashTable<FontResourceId> fonts;
HashTable<ImageFrameResourceId> image_frames;
- HashTable<DisplayListResourceId> vector_images;
};
struct DisplayListFontResource {
@@ -65,7 +64,6 @@ struct DisplayListResourceTransaction {
Vector<FontResourceId> font_ids_to_remove;
Vector<ImageFrameResourceId> image_frame_ids_to_remove;
- Vector<DisplayListResourceId> vector_image_ids_to_remove;
};
class COMPOSITING_API DisplayListResourceStorage {
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp
index 824d8c68010..511b5ec5bbe 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp
+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp
@@ -77,7 +77,6 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::DisplayListResourceTransacti
TRY(encoder.encode(transaction.image_frames));
TRY(encoder.encode(transaction.font_ids_to_remove));
TRY(encoder.encode(transaction.image_frame_ids_to_remove));
- TRY(encoder.encode(transaction.vector_image_ids_to_remove));
return {};
}
@@ -92,7 +91,6 @@ ErrorOr<Compositing::DisplayListResourceTransaction> decode(Decoder& decoder)
.image_frames = move(image_frames),
.font_ids_to_remove = TRY(decoder.decode<Vector<Compositing::FontResourceId>>()),
.image_frame_ids_to_remove = TRY(decoder.decode<Vector<Compositing::ImageFrameResourceId>>()),
- .vector_image_ids_to_remove = TRY(decoder.decode<Vector<Compositing::DisplayListResourceId>>()),
};
}
diff --git a/Libraries/LibCompositing/Rust/src/ipc_types.rs b/Libraries/LibCompositing/Rust/src/ipc_types.rs
index 9502fddd3c6..c94a8749b90 100644
--- a/Libraries/LibCompositing/Rust/src/ipc_types.rs
+++ b/Libraries/LibCompositing/Rust/src/ipc_types.rs
@@ -194,15 +194,11 @@ impl Decode for SharedDisplayList {
pub struct Compositing_DisplayListResourceTransaction {
pub font_ids_to_remove: Vec<u64>,
pub image_frame_ids_to_remove: Vec<u64>,
- /// The SVG images that no display list of the context draws any longer, whose rasters the compositor drops.
- pub vector_image_ids_to_remove: Vec<u64>,
}
impl Compositing_DisplayListResourceTransaction {
pub fn removes_anything(&self) -> bool {
- !self.font_ids_to_remove.is_empty()
- || !self.image_frame_ids_to_remove.is_empty()
- || !self.vector_image_ids_to_remove.is_empty()
+ !self.font_ids_to_remove.is_empty() || !self.image_frame_ids_to_remove.is_empty()
}
}
@@ -212,8 +208,7 @@ impl Encode for Compositing_DisplayListResourceTransaction {
encoder.encode_size(0)?;
encoder.encode_size(0)?;
encoder.encode(&self.font_ids_to_remove)?;
- encoder.encode(&self.image_frame_ids_to_remove)?;
- encoder.encode(&self.vector_image_ids_to_remove)
+ encoder.encode(&self.image_frame_ids_to_remove)
}
}
@@ -229,7 +224,6 @@ impl Decode for Compositing_DisplayListResourceTransaction {
Ok(Self {
font_ids_to_remove: decoder.decode()?,
image_frame_ids_to_remove: decoder.decode()?,
- vector_image_ids_to_remove: decoder.decode()?,
})
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list_resources.rs b/Libraries/LibWeb/Rust/src/painting/display_list_resources.rs
index e9ef161c843..ae67398e3be 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list_resources.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list_resources.rs
@@ -76,17 +76,15 @@ impl DisplayListResources {
ResourceIds {
fonts: self.fonts.keys().copied().collect(),
image_frames: self.image_frames.keys().copied().collect(),
- vector_images: self.vector_images.keys().copied().collect(),
}
}
}
-/// The ids of resources a receiver holds, and of the SVG images it may keep rasters of.
+/// The ids of resources a receiver holds.
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub(crate) struct ResourceIds {
pub(crate) fonts: HashSet<u64>,
pub(crate) image_frames: HashSet<u64>,
- pub(crate) vector_images: HashSet<u64>,
}
/// The ids of the resources the commands of `display_list` name.
diff --git a/Libraries/LibWeb/Rust/src/painting/presentation.rs b/Libraries/LibWeb/Rust/src/painting/presentation.rs
index ab4e171d544..e86204ba40e 100644
--- a/Libraries/LibWeb/Rust/src/painting/presentation.rs
+++ b/Libraries/LibWeb/Rust/src/painting/presentation.rs
@@ -370,7 +370,6 @@ impl PresenterState {
|held: &HashSet<u64>, kept: &HashSet<u64>| -> Vec<u64> { held.difference(kept).copied().collect() };
update.transaction.font_ids_to_remove = removed(&held.fonts, &kept_ids.fonts);
update.transaction.image_frame_ids_to_remove = removed(&held.image_frames, &kept_ids.image_frames);
- update.transaction.vector_image_ids_to_remove = removed(&held.vector_images, &kept_ids.vector_images);
self.compositor_resource_ids = kept_ids;
update
}
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index 4a95ced683f..d1af5ef07c9 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -1200,13 +1200,7 @@ void CompositorState::apply_resource_changes(Web::CompositorContextId context_id
transaction.font_ids_to_remove.append(Compositing::FontResourceId { id });
for (auto id : changes.image_frame_ids_to_remove)
transaction.image_frame_ids_to_remove.append(Compositing::ImageFrameResourceId { id });
- auto& raster_targets = context.raster_targets;
- raster_targets.apply_resource_transaction(move(transaction));
- // Only the rasters of the SVG images that the context's display lists draw are kept here.
- Compositing::DisplayListResourceSet removed_vector_images;
- for (auto id : changes.vector_image_ids_to_remove)
- removed_vector_images.vector_images.set(Compositing::DisplayListResourceId { id });
- raster_targets.raster_cache().evict(removed_vector_images);
+ context.raster_targets.apply_resource_transaction(move(transaction));
show_animated_image_frames_in(context);
}
@@ -1216,9 +1210,14 @@ void CompositorState::set_drawn_video_sinks(Web::CompositorContextId context_id,
update_unpainted_video_update_scheduling();
}
+void CompositorState::set_drawn_vector_images(Web::CompositorContextId context_id, ReadonlySpan<u64> vector_image_ids)
+{
+ presented_context(context_id).raster_targets.raster_cache().forget_vector_images_other_than(vector_image_ids);
+}
+
bool CompositorState::stage_rasterization(CompositorStateWebContentClient& client, u64 token, Compositing::DisplayListResourceTransaction&& resources, Gfx::ShareableBitmap target_bitmap)
{
- if (!resources.font_ids_to_remove.is_empty() || !resources.image_frame_ids_to_remove.is_empty() || !resources.vector_image_ids_to_remove.is_empty())
+ if (!resources.font_ids_to_remove.is_empty() || !resources.image_frame_ids_to_remove.is_empty())
return false;
// WebContent waits for each rasterization it stages, so it has only the one in flight per thread that rasterizes.
static constexpr size_t maximum_staged_rasterizations_per_client = 16;
@@ -1768,6 +1767,7 @@ RustFFI::FfiRasterHost CompositorState::raster_host(CompositorState& state)
.unmap_shared_memory = [](void* mapping) { delete static_cast<SharedMemoryMapping*>(mapping); },
.apply_resource_changes = [](void* host, u64 context_id, RustFFI::FfiResourceChanges const* changes) { state_of(host).apply_resource_changes(Context { context_id }, *changes); },
.set_drawn_video_sinks = [](void* host, u64 context_id, ReadonlySpan<u64> handles) { state_of(host).set_drawn_video_sinks(Context { context_id }, handles); },
+ .set_drawn_vector_images = [](void* host, u64 context_id, ReadonlySpan<u64> vector_image_ids) { state_of(host).set_drawn_vector_images(Context { context_id }, vector_image_ids); },
.staged_rasterization_has_resources = [](void* host, u64 client, u64 token, ReadonlySpan<u64> font_ids, ReadonlySpan<u64> image_frame_ids) { return state_of(host).staged_rasterization_has_resources(WebContentClientId { client }, token, font_ids, image_frame_ids); },
.begin_staged_rasterization = [](void* host, u64 client, u64 token, Compositing::RustFFI::FfiRasterPainter* painter) { return state_of(host).begin_staged_rasterization(WebContentClientId { client }, token, *painter); },
.finish_staged_rasterization = [](void* host, u64 client, u64 token) { state_of(host).finish_staged_rasterization(WebContentClientId { client }, token); },
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index 61ef1044321..cd5a870a370 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -231,6 +231,7 @@ private:
bool retire_idle_surplus_stores(Web::CompositorContextId, Optional<PresentationId>);
void apply_resource_changes(Web::CompositorContextId, RustFFI::FfiResourceChanges const&);
void set_drawn_video_sinks(Web::CompositorContextId, ReadonlySpan<u64> handles);
+ void set_drawn_vector_images(Web::CompositorContextId, ReadonlySpan<u64> vector_image_ids);
struct StagedRasterization;
StagedRasterization* staged_rasterization(WebContentClientId, u64 token);
bool staged_rasterization_has_resources(WebContentClientId, u64 token, ReadonlySpan<u64> font_ids, ReadonlySpan<u64> image_frame_ids);
diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cpp
index d9e3bb7c713..ea8fd55b7fd 100644
--- a/Services/Compositor/DisplayListRasterCache.cpp
+++ b/Services/Compositor/DisplayListRasterCache.cpp
@@ -195,8 +195,6 @@ void DisplayListRasterCache::evict(DisplayListResourceSet const& removed_resourc
m_layer_image_filter_cache = nullptr;
for (auto id : removed_resources.image_frames)
m_image_frame_images.remove(id.value());
- for (auto id : removed_resources.vector_images)
- m_vector_image_rasters.remove(id.value());
if (!removed_resources.fonts.is_empty()) {
m_text_blobs.remove_all_matching([&](auto const& key, auto const& text_blob) {
if (!removed_resources.fonts.contains(FontResourceId { key.font_id }))
@@ -281,6 +279,16 @@ void DisplayListRasterCache::forget_video_sink(Media::VideoSinkHandle handle)
m_video_sink_images.remove(handle.value());
}
+void DisplayListRasterCache::forget_vector_images_other_than(ReadonlySpan<u64> drawn_vector_image_ids)
+{
+ if (m_vector_image_rasters.is_empty())
+ return;
+ HashTable<u64> drawn;
+ for (auto id : drawn_vector_image_ids)
+ drawn.set(id);
+ m_vector_image_rasters.remove_all_matching([&](auto id, auto const&) { return !drawn.contains(id); });
+}
+
sk_sp<SkImage> DisplayListRasterCache::image_for_video_sink(Media::VideoSinkHandle handle, Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context, Gfx::IntSize& visible_size)
{
auto& cached_image = *m_video_sink_images.ensure(handle.value(), [] { return make<VideoSinkImage>(); });
diff --git a/Services/Compositor/DisplayListRasterCache.h b/Services/Compositor/DisplayListRasterCache.h
index 2f19e80bcc8..03f2e25be61 100644
--- a/Services/Compositor/DisplayListRasterCache.h
+++ b/Services/Compositor/DisplayListRasterCache.h
@@ -78,6 +78,8 @@ public:
void evict(Compositing::DisplayListResourceSet const& removed_resources);
// Drops the image made from the frame of a video sink that is no longer drawn.
void forget_video_sink(Media::VideoSinkHandle);
+ // Drops the rasters of every SVG image but these, which are the ones that the display list of the context draws.
+ void forget_vector_images_other_than(ReadonlySpan<u64> drawn_vector_image_ids);
sk_sp<SkImage> image_for_image_frame(Compositing::DisplayListResourceStorage const&, Compositing::ImageFrameResourceId, RefPtr<Gfx::SkiaBackendContext> const&);
// The image of the frame that the video sink shows. The image can be larger than the frame, whose visible size is
diff --git a/Services/Compositor/Rust/src/context_state/mod.rs b/Services/Compositor/Rust/src/context_state/mod.rs
index d7c93047ad6..ad64e6473c4 100644
--- a/Services/Compositor/Rust/src/context_state/mod.rs
+++ b/Services/Compositor/Rust/src/context_state/mod.rs
@@ -466,6 +466,13 @@ impl ContextState {
None => Vec::new(),
}
}
+ /// The ids of the SVG images that the display list draws.
+ pub fn vector_image_ids_drawn(&self) -> Vec<u64> {
+ match self.scene.display_list() {
+ Some(display_list) => display_list.storage().summary().vector_image_ids.clone(),
+ None => Vec::new(),
+ }
+ }
pub fn context_id(&self) -> CompositorContextId {
self.context_id
}
diff --git a/Services/Compositor/Rust/src/core/mod.rs b/Services/Compositor/Rust/src/core/mod.rs
index fa0b25d213e..818ed7b847a 100644
--- a/Services/Compositor/Rust/src/core/mod.rs
+++ b/Services/Compositor/Rust/src/core/mod.rs
@@ -691,7 +691,7 @@ impl CompositorCore {
}
/// Removes the resources that a display list or a visual context tree that the context took no longer draws, and
- /// tells the host which video sinks the context draws with them.
+ /// tells the host which video sinks and SVG images the context draws with them.
pub fn apply_resource_transaction(
&self,
context_id: CompositorContextId,
@@ -708,12 +708,13 @@ impl CompositorCore {
&FfiResourceChanges {
font_ids_to_remove: ak::Span::new(&transaction.font_ids_to_remove),
image_frame_ids_to_remove: ak::Span::new(&transaction.image_frame_ids_to_remove),
- vector_image_ids_to_remove: ak::Span::new(&transaction.vector_image_ids_to_remove),
},
);
}
let video_sink_handles = context.borrow().video_sink_handles_drawn();
self.raster.set_drawn_video_sinks(context_id, &video_sink_handles);
+ let vector_image_ids = context.borrow().vector_image_ids_drawn();
+ self.raster.set_drawn_vector_images(context_id, &vector_image_ids);
}
// Returns false for a tree that does not fit the installed display list, of which only the scroll state is taken.
diff --git a/Services/Compositor/Rust/src/raster_host.rs b/Services/Compositor/Rust/src/raster_host.rs
index 0f337b9d616..7b3a18627c4 100644
--- a/Services/Compositor/Rust/src/raster_host.rs
+++ b/Services/Compositor/Rust/src/raster_host.rs
@@ -119,7 +119,6 @@ pub struct FfiVideoSinkContent {
pub struct FfiResourceChanges<'a> {
pub font_ids_to_remove: ak::Span<'a, u64>,
pub image_frame_ids_to_remove: ak::Span<'a, u64>,
- pub vector_image_ids_to_remove: ak::Span<'a, u64>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -309,6 +308,8 @@ pub struct FfiRasterHost {
pub apply_resource_changes: unsafe extern "C" fn(*mut c_void, u64, *const FfiResourceChanges<'_>),
/// Arguments: the context, and the handles of the video sinks that its display list draws.
pub set_drawn_video_sinks: unsafe extern "C" fn(*mut c_void, u64, ak::Span<'_, u64>),
+ /// Arguments: the context, and the ids of the SVG images that its display list draws, whose rasters the host keeps.
+ pub set_drawn_vector_images: unsafe extern "C" fn(*mut c_void, u64, ak::Span<'_, u64>),
/// Arguments: the client, the token it staged the rasterization under, and the fonts and image frames the display
/// list draws. Answers whether the rasterization is staged with all of them. A staged rasterization draws no video
/// frames, so the sinks are not asked about.
@@ -851,6 +852,11 @@ impl RasterHost {
unsafe { (self.0.set_drawn_video_sinks)(self.host(), context_id.0, ak::Span::new(video_sink_handles)) };
}
+ pub fn set_drawn_vector_images(&self, context_id: CompositorContextId, vector_image_ids: &[u64]) {
+ // SAFETY: The host outlives this, and reads the ids during the call.
+ unsafe { (self.0.set_drawn_vector_images)(self.host(), context_id.0, ak::Span::new(vector_image_ids)) };
+ }
+
pub fn staged_rasterization_has_resources(
&self,
client: u64,