Truly independent web browser
LibCompositing+LibWeb+Compositor: Remove DisplayListResourceStorage
Aliaksandr Kalenikab283be69ae5
diff --git a/Libraries/LibCompositing/CMakeLists.txt b/Libraries/LibCompositing/CMakeLists.txtindex 59ccc1b9532..b97bab66aa2 100644--- a/Libraries/LibCompositing/CMakeLists.txt+++ b/Libraries/LibCompositing/CMakeLists.txt@@ -3,8 +3,8 @@ include(libcompositing_generators)set(SOURCESDisplayList/Canvas2DCommandStream.cppDisplayList/Canvas2DTape.cpp- DisplayList/DisplayListResourceStorage.cppDisplayList/DisplayListResourceEncoding.cpp+ DisplayList/DisplayListResources.cppDisplayList/RegisteredImageFrames.cppDisplayList/RegisteredTypefaces.cppDisplayList/RustFFIAssertions.cpp@@ -100,7 +100,7 @@ if(NOT BUILD_SHARED_LIBS)# POST_BUILD steps don't track inputs, so the bridge files depend on the Rust archive: when it# changes, LibCompositing re-archives and the merge picks up the fresh objects.- foreach(bridge IN ITEMS DisplayList/DisplayListResourceStorage.cpp Scrolling/SmoothScrollAnimation.cpp)+ foreach(bridge IN ITEMS DisplayList/RegisteredImageFrames.cpp Scrolling/SmoothScrollAnimation.cpp)set_property(SOURCE ${bridge} APPEND PROPERTY OBJECT_DEPENDS ${_libcompositing_rust_lib})endforeach()endif()diff --git a/Libraries/LibCompositing/DisplayList/Canvas2DCommandStream.h b/Libraries/LibCompositing/DisplayList/Canvas2DCommandStream.hindex bbceafcaee7..1a63fd0f4f2 100644--- a/Libraries/LibCompositing/DisplayList/Canvas2DCommandStream.h+++ b/Libraries/LibCompositing/DisplayList/Canvas2DCommandStream.h@@ -11,7 +11,7 @@#include <AK/RefCounted.h>#include <AK/Vector.h>#include <LibCompositing/DisplayList/DisplayListResourceIds.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Export.h>#include <LibCompositing/Video/VideoFrameHold.h>#include <LibGfx/DecodedImageFrame.h>diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceEncoding.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceEncoding.cppindex 684b3f24abf..c43fd26c984 100644--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceEncoding.cpp+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceEncoding.cpp@@ -5,7 +5,7 @@*/#include <AK/Math.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibGfx/Font/Font.h>#include <LibGfx/Font/Typeface.h>#include <LibIPC/Decoder.h>diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cppdeleted file mode 100644index b0f7501e8e3..00000000000--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp+++ /dev/null@@ -1,197 +0,0 @@-/*- * Copyright (c) 2026, Aliaksandr Kalenik <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include <AK/Function.h>-#include <AK/Math.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>-#include <LibCompositing/DisplayList/RegisteredTypefaces.h>-#include <LibCompositing/RustFFI.h>-#include <LibGfx/Bitmap.h>-#include <LibGfx/Font/Font.h>--namespace Compositing {--struct DisplayListStoredImageFrameResource {- AK_ALLOC_WITH_KMALLOC;-- explicit DisplayListStoredImageFrameResource(Gfx::DecodedImageFrame frame)- : frame(move(frame))- {- }-- Gfx::DecodedImageFrame frame;- // Classifying costs a walk over the pixels and the answer never changes for a given frame — so it's worked out on- // first use, and kept.- mutable Optional<bool> force_dark_should_filter;-};--bool DisplayListResourceSet::is_empty() const-{- return fonts.is_empty()- && image_frames.is_empty();-}--void DisplayListResourceSet::include(DisplayListResourceSet const& other)-{- for (auto id : other.fonts)- fonts.set(id, AK::HashSetExistingEntryBehavior::Keep);- for (auto id : other.image_frames)- image_frames.set(id, AK::HashSetExistingEntryBehavior::Keep);-}--DisplayListResourceStorage::DisplayListResourceStorage() = default;-DisplayListResourceStorage::DisplayListResourceStorage(DisplayListResourceStorage&&) = default;-DisplayListResourceStorage& DisplayListResourceStorage::operator=(DisplayListResourceStorage&&) = default;-DisplayListResourceStorage::~DisplayListResourceStorage() = default;--DisplayListFontDescription DisplayListFontDescription::of(Gfx::Font const& font)-{- return {- .id = FontResourceId { font.id() },- .typeface_id = font.typeface().id(),- .point_size = font.point_size(),- .variation_settings = font.variation_settings(),- .synthesis = font.synthesis(),- .is_invisible = font.is_invisible(),- };-}--FontResourceId DisplayListResourceStorage::add_font(Gfx::Font const& font)-{- auto id = font.id();- m_fonts.ensure(id, [&]() -> NonnullRefPtr<Gfx::Font const> { return font; });- return { id };-}--// Coarse by design: the verdict only must tell line art from photos; a large image shouldn't pay per-pixel to be asked.-static constexpr size_t max_sampled_pixels = 1000;--static bool classify_image_frame_for_force_dark(Gfx::DecodedImageFrame const& frame)-{- auto bitmap = frame.bitmap_sharing_pixels();- auto size = bitmap->size();- if (size.is_empty())- return false;-- auto total = static_cast<double>(size.width()) * static_cast<double>(size.height());- auto step = max(1, static_cast<int>(AK::ceil(AK::sqrt(total / static_cast<double>(max_sampled_pixels)))));-- Vector<u32> opaque_samples;- size_t transparent_count = 0;- size_t sampled_count = 0;- for (int y = 0; y < size.height(); y += step) {- for (int x = 0; x < size.width(); x += step) {- auto color = bitmap->get_pixel(x, y);- sampled_count++;- // A pixel this sheer says something about the image's shape rather than its palette — so it's counted- // toward transparency, but kept out of the palette samples.- if (color.alpha() < 128) {- transparent_count++;- continue;- }- opaque_samples.append(color.value());- }- }- if (sampled_count == 0)- return false;-- auto transparency_ratio = static_cast<float>(transparent_count) / static_cast<float>(sampled_count);- return Compositing::RustFFI::ladybird_web_force_dark_should_filter_image(- opaque_samples.data(), opaque_samples.size(), transparency_ratio);-}--bool DisplayListResourceStorage::image_frame_should_force_dark(ImageFrameResourceId id) const-{- auto stored = m_image_frames.get(id.value());- if (!stored.has_value())- return false;- auto const& resource = *stored.value();- if (!resource.force_dark_should_filter.has_value())- resource.force_dark_should_filter = classify_image_frame_for_force_dark(resource.frame);- return resource.force_dark_should_filter.value();-}--ImageFrameResourceId DisplayListResourceStorage::add_image_frame(Gfx::DecodedImageFrame const& frame)-{- auto id = frame.id();- m_image_frames.ensure(id, [&] { return make<DisplayListStoredImageFrameResource>(frame); });- return { id };-}--void DisplayListResourceStorage::set_font(FontResourceId id, NonnullRefPtr<Gfx::Font const> font)-{- m_fonts.set(id.value(), move(font));-}--void DisplayListResourceStorage::set_image_frame(ImageFrameResourceId id, Gfx::DecodedImageFrame frame)-{- m_image_frames.set(id.value(), make<DisplayListStoredImageFrameResource>(move(frame)));-}--void DisplayListResourceStorage::replace_image_frame_pixels(ImageFrameResourceId id, Gfx::DecodedImageFrame frame)-{- auto stored = m_image_frames.get(id.value());- VERIFY(stored.has_value());- // NB: Whether force-dark filters the image stays as it was, so that it does not flip between the frames of one- // image.- stored.value()->frame = move(frame);- m_image_frame_pixel_generations.set(id.value(), m_next_image_frame_pixel_generation++);-}--Gfx::DecodedImageFrame const& DisplayListResourceStorage::image_frame(ImageFrameResourceId id) const-{- return m_image_frames.get(id.value()).value()->frame;-}--bool DisplayListResourceStorage::add_described_fonts_made_of_registered_typefaces(ReadonlySpan<DisplayListFontDescription> descriptions, RegisteredTypefaces const& registered_typefaces)-{- bool every_typeface_is_registered = true;- for (auto const& description : descriptions) {- // NB: Font IDs are immutable. Keep the typeface that Skia's glyph caches were built for.- if (has_font(description.id))- continue;- auto font = registered_typefaces.font_described_by(description);- if (!font) {- every_typeface_is_registered = false;- continue;- }- set_font(description.id, font.release_nonnull());- }- return every_typeface_is_registered;-}--DisplayListResourceSet DisplayListResourceStorage::retain_only_fonts(ReadonlySpan<u64> font_ids)-{- HashTable<u64> retained_font_ids;- for (auto id : font_ids)- retained_font_ids.set(id);- DisplayListResourceSet removed_resources;- m_fonts.remove_all_matching([&](auto id, auto const&) {- if (retained_font_ids.contains(id))- return false;- removed_resources.fonts.set(FontResourceId { id });- return true;- });- return removed_resources;-}--DisplayListResourceSet DisplayListResourceStorage::retain_only_image_frames(ReadonlySpan<u64> image_frame_ids)-{- HashTable<u64> retained_image_frame_ids;- for (auto id : image_frame_ids)- retained_image_frame_ids.set(id);- DisplayListResourceSet removed_resources;- m_image_frames.remove_all_matching([&](auto id, auto const&) {- if (retained_image_frame_ids.contains(id))- return false;- m_image_frame_pixel_generations.remove(id);- removed_resources.image_frames.set(ImageFrameResourceId { id });- return true;- });- return removed_resources;-}--}diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.hdeleted file mode 100644index fdcdf49622f..00000000000--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h+++ /dev/null@@ -1,135 +0,0 @@-/*- * Copyright (c) 2026, Aliaksandr Kalenik <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/ByteBuffer.h>-#include <AK/Error.h>-#include <AK/Forward.h>-#include <AK/HashMap.h>-#include <AK/HashTable.h>-#include <AK/Noncopyable.h>-#include <AK/NonnullOwnPtr.h>-#include <AK/NonnullRefPtr.h>-#include <AK/RefPtr.h>-#include <AK/Span.h>-#include <AK/Time.h>-#include <AK/Variant.h>-#include <AK/Vector.h>-#include <LibCompositing/DisplayList/DisplayListResourceIds.h>-#include <LibCompositing/Export.h>-#include <LibCompositing/Forward.h>-#include <LibGfx/DecodedImageFrame.h>-#include <LibGfx/Font/Font.h>-#include <LibGfx/Font/FontVariationSettings.h>-#include <LibGfx/Font/Typeface.h>-#include <LibGfx/Forward.h>-#include <LibIPC/Forward.h>--namespace Compositing {--struct COMPOSITING_API DisplayListResourceSet {- bool is_empty() const;- void include(DisplayListResourceSet const&);-- HashTable<FontResourceId> fonts;- HashTable<ImageFrameResourceId> image_frames;-};--struct DisplayListFontResource {- FontResourceId id;- NonnullRefPtr<Gfx::Font const> font;-};--struct COMPOSITING_API DisplayListFontDescription {- static DisplayListFontDescription of(Gfx::Font const&);-- FontResourceId id;- u64 typeface_id { 0 };- float point_size { 0 };- Gfx::FontVariationSettings variation_settings;- Gfx::FontSynthesis synthesis;- bool is_invisible { false };-};--struct TypefaceRegistration {- u64 typeface_id { 0 };- NonnullRefPtr<Gfx::Typeface const> typeface;-};--struct DisplayListImageFrameResource {- ImageFrameResourceId id;- Gfx::DecodedImageFrame frame;-};--// The -webkit-font-smoothing values the recorder writes as a byte, in the order the CSS enum declares them.-enum class FontSmoothing : u8 {- Auto,- None,- Antialiased,- SubpixelAntialiased,-};--struct DisplayListStoredImageFrameResource;--class COMPOSITING_API DisplayListResourceStorage {- AK_MAKE_NONCOPYABLE(DisplayListResourceStorage);--public:- DisplayListResourceStorage();- DisplayListResourceStorage(DisplayListResourceStorage&&);- DisplayListResourceStorage& operator=(DisplayListResourceStorage&&);- ~DisplayListResourceStorage();-- FontResourceId add_font(Gfx::Font const&);- ImageFrameResourceId add_image_frame(Gfx::DecodedImageFrame const&);- void set_font(FontResourceId, NonnullRefPtr<Gfx::Font const>);- void set_image_frame(ImageFrameResourceId, Gfx::DecodedImageFrame);- [[nodiscard]] bool add_described_fonts_made_of_registered_typefaces(ReadonlySpan<DisplayListFontDescription>, RegisteredTypefaces const&);- // Returns the fonts that were removed.- DisplayListResourceSet retain_only_fonts(ReadonlySpan<u64> font_ids);- // Returns the image frames that were removed.- DisplayListResourceSet retain_only_image_frames(ReadonlySpan<u64> image_frame_ids);- // Shows other pixels under an id the display lists keep drawing, as an animated image moves to its next frame.- void replace_image_frame_pixels(ImageFrameResourceId, Gfx::DecodedImageFrame);- // A stamp for each image frame that has had its pixels replaced. No two replacements share a stamp, even across the- // removal of a frame, so an equal stamp always means the same pixels.- HashMap<u64, u64> const& image_frame_pixel_generations() const { return m_image_frame_pixel_generations; }-- bool has_font(FontResourceId id) const { return m_fonts.contains(id.value()); }- bool has_image_frame(ImageFrameResourceId id) const { return m_image_frames.contains(id.value()); }- Gfx::Font const& font(FontResourceId id) const { return *m_fonts.get(id.value()).value(); }- Gfx::DecodedImageFrame const& image_frame(ImageFrameResourceId) const;- // Whether force-dark should invert this image, worked out once and cached.- bool image_frame_should_force_dark(ImageFrameResourceId) const;--private:- HashMap<u64, NonnullRefPtr<Gfx::Font const>> m_fonts;- HashMap<u64, NonnullOwnPtr<DisplayListStoredImageFrameResource>> m_image_frames;- HashMap<u64, u64> m_image_frame_pixel_generations;- u64 m_next_image_frame_pixel_generation { 1 };-};--}--namespace IPC {--template<>-COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::DisplayListFontDescription const&);-template<>-COMPOSITING_API ErrorOr<Compositing::DisplayListFontDescription> decode(Decoder&);--template<>-COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::TypefaceRegistration const&);-template<>-COMPOSITING_API ErrorOr<Compositing::TypefaceRegistration> decode(Decoder&);--template<>-COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::DisplayListImageFrameResource const&);-template<>-COMPOSITING_API ErrorOr<Compositing::DisplayListImageFrameResource> decode(Decoder&);--}diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResources.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResources.cppnew file mode 100644index 00000000000..195ae111139--- /dev/null+++ b/Libraries/LibCompositing/DisplayList/DisplayListResources.cpp@@ -0,0 +1,24 @@+/*+ * Copyright (c) 2026, Aliaksandr Kalenik <[email protected]>+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCompositing/DisplayList/DisplayListResources.h>+#include <LibGfx/Font/Font.h>++namespace Compositing {++DisplayListFontDescription DisplayListFontDescription::of(Gfx::Font const& font)+{+ return {+ .id = FontResourceId { font.id() },+ .typeface_id = font.typeface().id(),+ .point_size = font.point_size(),+ .variation_settings = font.variation_settings(),+ .synthesis = font.synthesis(),+ .is_invisible = font.is_invisible(),+ };+}++}diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResources.h b/Libraries/LibCompositing/DisplayList/DisplayListResources.hnew file mode 100644index 00000000000..271ce0374af--- /dev/null+++ b/Libraries/LibCompositing/DisplayList/DisplayListResources.h@@ -0,0 +1,77 @@+/*+ * Copyright (c) 2026, Aliaksandr Kalenik <[email protected]>+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Error.h>+#include <AK/Forward.h>+#include <AK/NonnullRefPtr.h>+#include <LibCompositing/DisplayList/DisplayListResourceIds.h>+#include <LibCompositing/Export.h>+#include <LibCompositing/Forward.h>+#include <LibGfx/DecodedImageFrame.h>+#include <LibGfx/Font/Font.h>+#include <LibGfx/Font/FontVariationSettings.h>+#include <LibGfx/Font/Typeface.h>+#include <LibGfx/Forward.h>+#include <LibIPC/Forward.h>++namespace Compositing {++struct DisplayListFontResource {+ FontResourceId id;+ NonnullRefPtr<Gfx::Font const> font;+};++struct COMPOSITING_API DisplayListFontDescription {+ static DisplayListFontDescription of(Gfx::Font const&);++ FontResourceId id;+ u64 typeface_id { 0 };+ float point_size { 0 };+ Gfx::FontVariationSettings variation_settings;+ Gfx::FontSynthesis synthesis;+ bool is_invisible { false };+};++struct TypefaceRegistration {+ u64 typeface_id { 0 };+ NonnullRefPtr<Gfx::Typeface const> typeface;+};++struct DisplayListImageFrameResource {+ ImageFrameResourceId id;+ Gfx::DecodedImageFrame frame;+};++// The -webkit-font-smoothing values the recorder writes as a byte, in the order the CSS enum declares them.+enum class FontSmoothing : u8 {+ Auto,+ None,+ Antialiased,+ SubpixelAntialiased,+};++}++namespace IPC {++template<>+COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::DisplayListFontDescription const&);+template<>+COMPOSITING_API ErrorOr<Compositing::DisplayListFontDescription> decode(Decoder&);++template<>+COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::TypefaceRegistration const&);+template<>+COMPOSITING_API ErrorOr<Compositing::TypefaceRegistration> decode(Decoder&);++template<>+COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::DisplayListImageFrameResource const&);+template<>+COMPOSITING_API ErrorOr<Compositing::DisplayListImageFrameResource> decode(Decoder&);++}diff --git a/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.cpp b/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.cppindex 9e50f36a670..699400ba679 100644--- a/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.cpp+++ b/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.cpp@@ -4,13 +4,77 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Math.h>#include <LibCompositing/DisplayList/RegisteredImageFrames.h>+#include <LibCompositing/RustFFI.h>+#include <LibGfx/Bitmap.h>namespace Compositing {+// Coarse by design: the verdict only must tell line art from photos; a large image shouldn't pay per-pixel to be asked.+static constexpr size_t max_sampled_pixels = 1000;++static bool classify_image_frame_for_force_dark(Gfx::DecodedImageFrame const& frame)+{+ auto bitmap = frame.bitmap_sharing_pixels();+ auto size = bitmap->size();+ if (size.is_empty())+ return false;++ auto total = static_cast<double>(size.width()) * static_cast<double>(size.height());+ auto step = max(1, static_cast<int>(AK::ceil(AK::sqrt(total / static_cast<double>(max_sampled_pixels)))));++ Vector<u32> opaque_samples;+ size_t transparent_count = 0;+ size_t sampled_count = 0;+ for (int y = 0; y < size.height(); y += step) {+ for (int x = 0; x < size.width(); x += step) {+ auto color = bitmap->get_pixel(x, y);+ sampled_count++;+ // A pixel this sheer says something about the image's shape rather than its palette — so it's counted+ // toward transparency, but kept out of the palette samples.+ if (color.alpha() < 128) {+ transparent_count++;+ continue;+ }+ opaque_samples.append(color.value());+ }+ }+ if (sampled_count == 0)+ return false;++ auto transparency_ratio = static_cast<float>(transparent_count) / static_cast<float>(sampled_count);+ return Compositing::RustFFI::ladybird_web_force_dark_should_filter_image(+ opaque_samples.data(), opaque_samples.size(), transparency_ratio);+}++static u64 s_next_generation_of_replaced_pixels = 1;++RegisteredImageFrame::RegisteredImageFrame(Gfx::DecodedImageFrame frame)+ : m_frame(move(frame))+{+}++bool RegisteredImageFrame::should_force_dark() const+{+ // Classifying costs a walk over the pixels and the answer never changes for a given frame — so it's worked out on+ // first use, and kept.+ if (!m_should_force_dark.has_value())+ m_should_force_dark = classify_image_frame_for_force_dark(m_frame);+ return m_should_force_dark.value();+}++void RegisteredImageFrame::replace_pixels(Gfx::DecodedImageFrame frame)+{+ // NB: Whether force-dark filters the image stays as it was, so that it does not flip between the frames of one+ // image.+ m_frame = move(frame);+ m_generation_of_replaced_pixels = s_next_generation_of_replaced_pixels++;+}+void RegisteredImageFrames::register_image_frame(DisplayListImageFrameResource resource){- m_image_frames_by_id.set(resource.id.value(), move(resource.frame));+ m_image_frames_by_id.set(resource.id.value(), make_ref_counted<RegisteredImageFrame>(move(resource.frame)));}void RegisteredImageFrames::unregister_image_frame(ImageFrameResourceId id)@@ -18,12 +82,12 @@ void RegisteredImageFrames::unregister_image_frame(ImageFrameResourceId id)m_image_frames_by_id.remove(id.value());}-Gfx::DecodedImageFrame const* RegisteredImageFrames::image_frame(ImageFrameResourceId id) const+RefPtr<RegisteredImageFrame> RegisteredImageFrames::image_frame(ImageFrameResourceId id) const{auto image_frame = m_image_frames_by_id.find(id.value());if (image_frame == m_image_frames_by_id.end())return nullptr;- return &image_frame->value;+ return image_frame->value;}}diff --git a/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.h b/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.hindex f6fcd4d0848..50cfb8a86b3 100644--- a/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.h+++ b/Libraries/LibCompositing/DisplayList/RegisteredImageFrames.h@@ -7,21 +7,50 @@#pragma once#include <AK/HashMap.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <AK/NonnullRefPtr.h>+#include <AK/Optional.h>+#include <AK/RefCounted.h>+#include <AK/RefPtr.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Export.h>#include <LibGfx/DecodedImageFrame.h>namespace Compositing {+// An image frame that a client registered. Whatever draws it holds it, so it outlasts its registration for as long as+// it is drawn.+class COMPOSITING_API RegisteredImageFrame final : public RefCounted<RegisteredImageFrame> {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit RegisteredImageFrame(Gfx::DecodedImageFrame);++ Gfx::DecodedImageFrame const& frame() const { return m_frame; }+ // Whether force-dark should invert this image, worked out once and cached.+ bool should_force_dark() const;++ // Shows other pixels under the id the display lists keep drawing, as an animated image moves to its next frame.+ void replace_pixels(Gfx::DecodedImageFrame);+ // A stamp for the pixels that replaced the ones the frame had before, or none for a frame that still has the+ // pixels it was registered with. No two replacements share a stamp, so an equal stamp always means the same+ // pixels.+ Optional<u64> generation_of_replaced_pixels() const { return m_generation_of_replaced_pixels; }++private:+ Gfx::DecodedImageFrame m_frame;+ mutable Optional<bool> m_should_force_dark;+ Optional<u64> m_generation_of_replaced_pixels;+};+class COMPOSITING_API RegisteredImageFrames {public:void register_image_frame(DisplayListImageFrameResource);void unregister_image_frame(ImageFrameResourceId);- Gfx::DecodedImageFrame const* image_frame(ImageFrameResourceId) const;+ RefPtr<RegisteredImageFrame> image_frame(ImageFrameResourceId) const;private:- HashMap<u64, Gfx::DecodedImageFrame> m_image_frames_by_id;+ HashMap<u64, NonnullRefPtr<RegisteredImageFrame>> m_image_frames_by_id;};}diff --git a/Libraries/LibCompositing/DisplayList/RegisteredTypefaces.h b/Libraries/LibCompositing/DisplayList/RegisteredTypefaces.hindex 4469aab51ef..bf48686e0f5 100644--- a/Libraries/LibCompositing/DisplayList/RegisteredTypefaces.h+++ b/Libraries/LibCompositing/DisplayList/RegisteredTypefaces.h@@ -9,7 +9,7 @@#include <AK/HashMap.h>#include <AK/NonnullRefPtr.h>#include <AK/RefPtr.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Export.h>#include <LibGfx/Forward.h>diff --git a/Libraries/LibCompositing/Forward.h b/Libraries/LibCompositing/Forward.hindex dc44d5421a1..d9768df6cc7 100644--- a/Libraries/LibCompositing/Forward.h+++ b/Libraries/LibCompositing/Forward.h@@ -22,10 +22,9 @@ struct UIOverlays;class Canvas2DCommandStream;struct Canvas2DCommandStreamSegment;struct DisplayListGlyph;-class DisplayListResourceStorage;+class RegisteredImageFrame;class RegisteredImageFrames;class RegisteredTypefaces;-struct DisplayListResourceSet;enum class CompositorScrollNodeKind : u8;}diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.h b/Libraries/LibWeb/Compositor/CompositorConnection.hindex e7a2a085f5e..a3b9e7d071a 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.h+++ b/Libraries/LibWeb/Compositor/CompositorConnection.h@@ -14,7 +14,7 @@#include <Compositor/CompositorWebContentClientEndpoint.h>#include <Compositor/CompositorWebContentServerEndpoint.h>#include <LibCompositing/DisplayList/Canvas2DCommandStream.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Types.h>#include <LibCompositing/WebGL/Types.h>#include <LibGfx/Forward.h>diff --git a/Libraries/LibWeb/Compositor/CompositorHost.h b/Libraries/LibWeb/Compositor/CompositorHost.hindex da3fd3033d5..7cceae38475 100644--- a/Libraries/LibWeb/Compositor/CompositorHost.h+++ b/Libraries/LibWeb/Compositor/CompositorHost.h@@ -15,7 +15,7 @@#include <AK/Types.h>#include <AK/Vector.h>#include <AK/Weakable.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Types.h>#include <LibCompositing/Video/VideoFrameHold.h>#include <LibGfx/Bitmap.h>diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex e570c910b31..e3e302c66ad 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -16,7 +16,7 @@#include <AK/Tuple.h>#include <AK/Utf16String.h>#include <AK/Utf16View.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Scrolling/SmoothScrollAnimation.h>#include <LibCore/Forward.h>#include <LibCore/StatsLog.h>diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 82776ec1b7f..548e3f66d3a 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -13,7 +13,7 @@#include <AK/Utf16String.h>#include <AK/Utf16StringBuilder.h>#include <LibAudio/ClientConnection.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCore/EventLoop.h>#include <LibCore/System.h>#include <LibCore/TimeZone.h>diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex e61ffe90f21..fb648df6ce0 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -11,7 +11,7 @@#include <AK/Base64.h>#include <AK/Debug.h>#include <AK/Math.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibGC/Heap.h>#include <LibGC/RootHashTable.h>#include <LibGC/RootVector.h>diff --git a/Libraries/LibWeb/Painting/DocumentPaintState.h b/Libraries/LibWeb/Painting/DocumentPaintState.hindex 4496c6008fd..59610b459f8 100644--- a/Libraries/LibWeb/Painting/DocumentPaintState.h+++ b/Libraries/LibWeb/Painting/DocumentPaintState.h@@ -11,7 +11,7 @@#include <AK/RefPtr.h>#include <AK/Time.h>#include <AK/Vector.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibGC/Ptr.h>#include <LibWeb/Export.h>#include <LibWeb/Forward.h>diff --git a/Services/Compositor/CMakeLists.txt b/Services/Compositor/CMakeLists.txtindex 74b3195919a..11833a423de 100644--- a/Services/Compositor/CMakeLists.txt+++ b/Services/Compositor/CMakeLists.txt@@ -10,6 +10,7 @@ set(SOURCESContextRasterTargets.cppDisplayListPlayerSkia.cppDisplayListRasterCache.cpp+ DrawnResources.cppFontSkia.cppConnectionFromClient.cppConnectionFromPresentation.cppdiff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cppindex e9a99cfe22d..4e4115f8f2c 100644--- a/Services/Compositor/CanvasHost.cpp+++ b/Services/Compositor/CanvasHost.cpp@@ -74,14 +74,17 @@ OwnPtr<Canvas2DCommandPlayer> CanvasHost::create_2d_command_player(Gfx::IntSizereturn m_canvas_surface_registry.canvas_surface(m_canvas_surface_owner_id, Compositing::CanvasId { canvas_id });};auto font_resolver = [this](u64 font_id) -> Gfx::Font const* {- if (!m_text_resources.has_font(Compositing::FontResourceId { font_id }))+ if (!m_fonts_of_latest_stream_batch.contains(Compositing::FontResourceId { font_id }))return nullptr;- return &m_text_resources.font(Compositing::FontResourceId { font_id });+ return &m_fonts_of_latest_stream_batch.font(Compositing::FontResourceId { font_id });};auto image_frame_resolver = [this](u64 frame_id) -> Gfx::DecodedImageFrame const* {if (!m_image_frames_registered_while_stream_batch_plays)return nullptr;- return m_image_frames_registered_while_stream_batch_plays->image_frame(Compositing::ImageFrameResourceId { frame_id });+ auto image_frame = m_image_frames_registered_while_stream_batch_plays->image_frame(Compositing::ImageFrameResourceId { frame_id });+ if (!image_frame)+ return nullptr;+ return &image_frame->frame();};auto video_frame_image_resolver = [this, skia_backend_context_of_canvas](u64 video_sink_handle, u64 generation) {return held_video_frame_image(Media::VideoSinkHandle { video_sink_handle }, generation, skia_backend_context_of_canvas);@@ -196,13 +199,13 @@ void CanvasHost::present_canvas_2d_context(Compositing::CanvasId canvas_id, Canvbool CanvasHost::execute_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStreamSegment> const& segments, Vector<Compositing::DisplayListFontDescription> const& fonts, Compositing::RegisteredTypefaces const& registered_typefaces, Compositing::RegisteredImageFrames const& registered_image_frames){- if (!m_text_resources.add_described_fonts_made_of_registered_typefaces(fonts, registered_typefaces))+ if (!m_fonts_of_latest_stream_batch.add_described_fonts_made_of_registered_typefaces(fonts, registered_typefaces))return false;Vector<u64> font_ids_of_batch;font_ids_of_batch.ensure_capacity(fonts.size());for (auto const& font : fonts)font_ids_of_batch.unchecked_append(font.id.value());- (void)m_text_resources.retain_only_fonts(font_ids_of_batch);+ (void)m_fonts_of_latest_stream_batch.retain_only(font_ids_of_batch);TemporaryChange image_frames_registered_while_stream_batch_plays { m_image_frames_registered_while_stream_batch_plays, ®istered_image_frames };Vector<NonnullRefPtr<Gfx::PaintingSurface>> surfaces_that_sample_video_frames;for (auto const& segment : segments) {diff --git a/Services/Compositor/CanvasHost.h b/Services/Compositor/CanvasHost.hindex 8c91cb0e565..07a4282022d 100644--- a/Services/Compositor/CanvasHost.h+++ b/Services/Compositor/CanvasHost.h@@ -6,7 +6,7 @@#pragma once-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <AK/ByteBuffer.h>#include <AK/Error.h>@@ -19,6 +19,7 @@#include <AK/String.h>#include <AK/Variant.h>#include <AK/Vector.h>+#include <Compositor/DrawnResources.h>#include <Compositor/Forward.h>#include <Compositor/HostWebGLContext.h>#include <Compositor/PresentableSurface.h>@@ -106,7 +107,7 @@ private:CanvasSurfaceRegistry& m_canvas_surface_registry;Compositing::CanvasSurfaceOwnerId m_canvas_surface_owner_id;HashMap<Compositing::CanvasId, Context> m_contexts;- Compositing::DisplayListResourceStorage m_text_resources;+ DrawnFonts m_fonts_of_latest_stream_batch;Compositing::RegisteredImageFrames const* m_image_frames_registered_while_stream_batch_plays { nullptr };VideoFrameHolds const& m_video_frame_holds;diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 46126a74fc8..fe84630057a 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -486,7 +486,7 @@ void CompositorState::stop_presenting_to_client(Web::CompositorContextId contextbool CompositorState::describe_display_list_fonts(Web::CompositorContextId context_id, ReadonlySpan<Compositing::DisplayListFontDescription> fonts, Compositing::RegisteredTypefaces const& registered_typefaces){- return presented_context(context_id).raster_targets.resource_storage().add_described_fonts_made_of_registered_typefaces(fonts, registered_typefaces);+ return presented_context(context_id).raster_targets.fonts().add_described_fonts_made_of_registered_typefaces(fonts, registered_typefaces);}CompositorState::VideoSinkState* CompositorState::video_sink_state(CompositorStateWebContentClient& client, Media::VideoSinkHandle handle)@@ -661,7 +661,7 @@ void CompositorState::animated_image_decoder_lost(CompositorStateWebContentClienbool CompositorState::animated_image_is_drawn(CompositorStateWebContentClient* client, Compositing::ImageFrameResourceId id){for (auto& [context_id, context] : m_presented_contexts) {- if (context->web_content_client.web_content_client_id() == client->web_content_client_id() && context->raster_targets.resource_storage().has_image_frame(id) && m_core.context_is_effectively_visible(context_id))+ if (context->web_content_client.web_content_client_id() == client->web_content_client_id() && context->raster_targets.image_frames().contains(id) && m_core.context_is_effectively_visible(context_id))return true;}return false;@@ -670,10 +670,13 @@ bool CompositorState::animated_image_is_drawn(CompositorStateWebContentClient* cvoid CompositorState::show_animated_image_frame(CompositorStateWebContentClient* client, Compositing::ImageFrameResourceId id, Gfx::DecodedImageFrame const& frame){for (auto& [context_id, context] : m_presented_contexts) {- auto& resource_storage = context->raster_targets.resource_storage();- if (context->web_content_client.web_content_client_id() != client->web_content_client_id() || !resource_storage.has_image_frame(id))+ auto& image_frames = context->raster_targets.image_frames();+ if (context->web_content_client.web_content_client_id() != client->web_content_client_id() || !image_frames.contains(id))continue;- resource_storage.replace_image_frame_pixels(id, frame);+ // NB: Contexts that draw the image frame hold the same one, unless it was registered anew since one took it.+ auto& image_frame = image_frames.image_frame(id);+ if (!image_frame.frame().has_same_pixels_as(frame))+ image_frame.replace_pixels(frame);m_core.context_content_changed(context_id);}}@@ -686,8 +689,9 @@ void CompositorState::show_animated_image_frames_in(PresentedContext& context)for (auto& image_entry : images->value) {Compositing::ImageFrameResourceId id { image_entry.key };auto const& frame = image_entry.value->shown_frame();- if (frame.has_value() && context.raster_targets.resource_storage().has_image_frame(id) && !context.raster_targets.resource_storage().image_frame(id).has_same_pixels_as(*frame))- context.raster_targets.resource_storage().replace_image_frame_pixels(id, *frame);+ auto& image_frames = context.raster_targets.image_frames();+ if (frame.has_value() && image_frames.contains(id) && !image_frames.image_frame(id).frame().has_same_pixels_as(*frame))+ image_frames.image_frame(id).replace_pixels(*frame);}// An image that nothing drew has been waiting for a context to draw it.if (!images->value.is_empty())@@ -1108,11 +1112,11 @@ Compositing::RustFFI::FfiRasterPainter CompositorState::begin_painting(Web::Compauto& player = *m_display_list_players[m_painting_depth++];switch (resources) {case RustFFI::PaintingResources::Page:- return player.begin_painting(surface, raster_targets.resource_storage(), raster_targets.raster_cache(), &raster_targets.canvas_surfaces(), [this, &client = context.web_content_client](Media::VideoSinkHandle handle) {+ return player.begin_painting(surface, raster_targets.fonts(), &raster_targets.image_frames(), raster_targets.raster_cache(), &raster_targets.canvas_surfaces(), [this, &client = context.web_content_client](Media::VideoSinkHandle handle) {return current_video_frame(client, handle);});case RustFFI::PaintingResources::Overlays:- return player.begin_painting(surface, raster_targets.overlay_resource_storage(), raster_targets.overlay_raster_cache(), nullptr, nullptr);+ return player.begin_painting(surface, raster_targets.overlay_fonts(), nullptr, raster_targets.overlay_raster_cache(), nullptr, nullptr);}VERIFY_NOT_REACHED();}@@ -1194,23 +1198,10 @@ bool CompositorState::retire_idle_surplus_stores(Web::CompositorContextId contexbool CompositorState::take_registered_image_frames(Web::CompositorContextId context_id, ReadonlySpan<u64> image_frame_ids){auto& context = presented_context(context_id);- auto& resource_storage = context.raster_targets.resource_storage();- auto const& registered_image_frames = context.web_content_client.registered_image_frames();- bool holds_every_image_frame = true;- bool took_an_image_frame = false;- for (auto id : image_frame_ids) {- Compositing::ImageFrameResourceId image_frame_id { id };- if (resource_storage.has_image_frame(image_frame_id))- continue;- auto const* image_frame = registered_image_frames.image_frame(image_frame_id);- if (!image_frame) {- holds_every_image_frame = false;- continue;- }- resource_storage.set_image_frame(image_frame_id, *image_frame);- took_an_image_frame = true;- }- if (took_an_image_frame)+ auto& image_frames = context.raster_targets.image_frames();+ auto image_frame_count_before = image_frames.size();+ bool holds_every_image_frame = image_frames.take_registered(image_frame_ids, context.web_content_client.registered_image_frames());+ if (image_frames.size() != image_frame_count_before)show_animated_image_frames_in(context);return holds_every_image_frame;}@@ -1238,9 +1229,9 @@ void CompositorState::set_drawn_fonts(Web::CompositorContextId context_id, Readobool CompositorState::context_holds_fonts(Web::CompositorContextId context_id, ReadonlySpan<u64> font_ids){- auto const& resource_storage = presented_context(context_id).raster_targets.resource_storage();+ auto const& fonts = presented_context(context_id).raster_targets.fonts();for (auto id : font_ids) {- if (!resource_storage.has_font(Compositing::FontResourceId { id }))+ if (!fonts.contains(Compositing::FontResourceId { id }))return false;}return true;@@ -1254,14 +1245,14 @@ bool CompositorState::stage_rasterization(CompositorStateWebContentClient& clienif (staged.contains(token) || staged.size() >= maximum_staged_rasterizations_per_client)return false;auto rasterization = make<StagedRasterization>();- if (!rasterization->resource_storage.add_described_fonts_made_of_registered_typefaces(fonts, registered_typefaces))+ if (!rasterization->fonts.add_described_fonts_made_of_registered_typefaces(fonts, registered_typefaces))+ return false;+ Vector<u64> ids_of_image_frames;+ ids_of_image_frames.ensure_capacity(image_frame_ids.size());+ for (auto image_frame_id : image_frame_ids)+ ids_of_image_frames.unchecked_append(image_frame_id.value());+ if (!rasterization->image_frames.take_registered(ids_of_image_frames, client.registered_image_frames()))return false;- for (auto image_frame_id : image_frame_ids) {- auto const* image_frame = client.registered_image_frames().image_frame(image_frame_id);- if (!image_frame)- return false;- rasterization->resource_storage.set_image_frame(image_frame_id, *image_frame);- }rasterization->target_bitmap = move(target_bitmap);staged.set(token, move(rasterization));return true;@@ -1283,13 +1274,12 @@ bool CompositorState::staged_rasterization_has_resources(WebContentClientId clieauto* rasterization = staged_rasterization(client, token);if (!rasterization)return false;- auto const& resource_storage = rasterization->resource_storage;for (auto id : font_ids) {- if (!resource_storage.has_font(Compositing::FontResourceId { id }))+ if (!rasterization->fonts.contains(Compositing::FontResourceId { id }))return false;}for (auto id : image_frame_ids) {- if (!resource_storage.has_image_frame(Compositing::ImageFrameResourceId { id }))+ if (!rasterization->image_frames.contains(Compositing::ImageFrameResourceId { id }))return false;}return true;@@ -1315,7 +1305,7 @@ bool CompositorState::begin_staged_rasterization(WebContentClientId client, u64__builtin_memset(target.scanline_u8(0), 0, target.size_in_bytes());rasterization->target_surface = Gfx::PaintingSurface::wrap_bitmap(target);rasterization->player = make<DisplayListPlayerSkia>(RefPtr<Gfx::SkiaBackendContext> {});- painter = rasterization->player->begin_painting(*rasterization->target_surface, rasterization->resource_storage, rasterization->raster_cache, nullptr, nullptr);+ painter = rasterization->player->begin_painting(*rasterization->target_surface, rasterization->fonts, &rasterization->image_frames, rasterization->raster_cache, nullptr, nullptr);return true;}@@ -1343,7 +1333,6 @@ void CompositorState::report_live_content(Web::CompositorContextId context_id, R{auto& context = presented_context(context_id);auto& raster_targets = context.raster_targets;- auto const& resource_storage = raster_targets.resource_storage();Vector<RustFFI::FfiCanvasContent> canvases;canvases.ensure_capacity(canvas_ids.size());@@ -1368,9 +1357,10 @@ void CompositorState::report_live_content(Web::CompositorContextId context_id, R});}Vector<RustFFI::FfiImageFrameContent> image_frames;- image_frames.ensure_capacity(resource_storage.image_frame_pixel_generations().size());- for (auto const& [id, pixel_generation] : resource_storage.image_frame_pixel_generations())- image_frames.unchecked_append({ .id = id, .pixel_generation = pixel_generation });+ raster_targets.image_frames().for_each([&](auto id, auto const& image_frame) {+ if (auto pixel_generation = image_frame.generation_of_replaced_pixels(); pixel_generation.has_value())+ image_frames.append({ .id = id.value(), .pixel_generation = *pixel_generation });+ });RustFFI::FfiLiveContent content {.canvases = canvases.span(),diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.hindex 9d13353da6e..bc087afd48c 100644--- a/Services/Compositor/CompositorState.h+++ b/Services/Compositor/CompositorState.h@@ -21,7 +21,7 @@#include <Compositor/SkiaBackendContext.h>#include <Compositor/VSyncScheduler.h>#include <Compositor/WindowServer.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Forward.h>#include <LibCompositing/Types.h>#include <LibCore/Forward.h>@@ -279,7 +279,8 @@ private:StagedRasterization() = default;- Compositing::DisplayListResourceStorage resource_storage;+ DrawnFonts fonts;+ DrawnImageFrames image_frames;DisplayListRasterCache raster_cache;Gfx::ShareableBitmap target_bitmap;RefPtr<Gfx::PaintingSurface> target_surface;diff --git a/Services/Compositor/CompositorWebContentServer.ipc b/Services/Compositor/CompositorWebContentServer.ipcindex 460df293637..184dee90db2 100644--- a/Services/Compositor/CompositorWebContentServer.ipc+++ b/Services/Compositor/CompositorWebContentServer.ipc@@ -1,7 +1,7 @@#include <AK/NonnullRefPtr.h>#include <AK/Optional.h>#include <LibCompositing/DisplayList/Canvas2DCommandStream.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/Forward.h>#include <LibCompositing/Types.h>#include <LibCompositing/Video/VideoFramePresentationPage.h>diff --git a/Services/Compositor/ConnectionFromWebContent.h b/Services/Compositor/ConnectionFromWebContent.hindex b6e3f508b6c..ed9bc625847 100644--- a/Services/Compositor/ConnectionFromWebContent.h+++ b/Services/Compositor/ConnectionFromWebContent.h@@ -15,7 +15,7 @@#include <Compositor/RenderClockConnection.h>#include <Compositor/VideoFrameHolds.h>#include <LibCompositing/DisplayList/DisplayListCommand.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/DisplayList/RegisteredImageFrames.h>#include <LibCompositing/DisplayList/RegisteredTypefaces.h>#include <LibCompositing/WebGL/Types.h>diff --git a/Services/Compositor/ContextRasterTargets.cpp b/Services/Compositor/ContextRasterTargets.cppindex f3b213d9cc2..93e5ef8ddad 100644--- a/Services/Compositor/ContextRasterTargets.cpp+++ b/Services/Compositor/ContextRasterTargets.cpp@@ -21,12 +21,12 @@ ContextRasterTargets::ContextRasterTargets(OwnedCanvasSurfaces canvas_surfaces)void ContextRasterTargets::retain_only_fonts(ReadonlySpan<u64> font_ids){- m_raster_cache.evict(m_resource_storage.retain_only_fonts(font_ids));+ m_raster_cache.forget_fonts(m_fonts.retain_only(font_ids));}void ContextRasterTargets::retain_only_image_frames(ReadonlySpan<u64> image_frame_ids){- m_raster_cache.evict(m_resource_storage.retain_only_image_frames(image_frame_ids));+ m_raster_cache.forget_image_frames(m_image_frames.retain_only(image_frame_ids));}Optional<OverlayLabel> ContextRasterTargets::shape_overlay_label(Optional<StringView> font_family, double device_pixel_ratio, StringView text)@@ -58,7 +58,7 @@ Optional<OverlayLabel> ContextRasterTargets::shape_overlay_label(Optional<Stringlabel.ascent = font_metrics.fAscent;label.descent = font_metrics.fDescent;label.font_ascent = font->pixel_metrics().ascent;- label.font_id = m_overlay_resource_storage.add_font(*font).value();+ label.font_id = m_overlay_fonts.add(*font).value();return label;}diff --git a/Services/Compositor/ContextRasterTargets.h b/Services/Compositor/ContextRasterTargets.hindex e2579b0144f..18f17da3e7b 100644--- a/Services/Compositor/ContextRasterTargets.h+++ b/Services/Compositor/ContextRasterTargets.h@@ -17,8 +17,8 @@#include <Compositor/CanvasSurfaceRegistry.h>#include <Compositor/CompositorCoreHost.h>#include <Compositor/DisplayListRasterCache.h>+#include <Compositor/DrawnResources.h>#include <Compositor/PresentableSurface.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>#include <LibGfx/Size.h>#include <LibMedia/VideoSinkHandle.h>@@ -40,17 +40,19 @@ class ContextRasterTargets {public:explicit ContextRasterTargets(OwnedCanvasSurfaces);- Compositing::DisplayListResourceStorage& resource_storage() { return m_resource_storage; }- Compositing::DisplayListResourceStorage const& resource_storage() const { return m_resource_storage; }+ DrawnFonts& fonts() { return m_fonts; }+ DrawnFonts const& fonts() const { return m_fonts; }+ DrawnImageFrames& image_frames() { return m_image_frames; }+ DrawnImageFrames const& image_frames() const { return m_image_frames; }void retain_only_fonts(ReadonlySpan<u64> font_ids);void retain_only_image_frames(ReadonlySpan<u64> image_frame_ids);DisplayListRasterCache& raster_cache() { return m_raster_cache; }OwnedCanvasSurfaces const& canvas_surfaces() const { return m_canvas_surfaces; }- // The resources that the overlays draw with, apart from those of the page.- Compositing::DisplayListResourceStorage& overlay_resource_storage() { return m_overlay_resource_storage; }+ // The fonts that the overlays draw with, apart from those of the page.+ DrawnFonts const& overlay_fonts() const { return m_overlay_fonts; }DisplayListRasterCache& overlay_raster_cache() { return m_overlay_raster_cache; }- // Shapes a label of the overlays, in a font that the overlays' resources then hold.+ // Shapes a label of the overlays, in a font that the overlays' fonts then hold.Optional<OverlayLabel> shape_overlay_label(Optional<StringView> font_family, double device_pixel_ratio, StringView text);BackingStoreManager& backing_stores() { return m_backing_stores; }@@ -66,10 +68,11 @@ public:bool draws_video_sink(Media::VideoSinkHandle handle) const { return m_drawn_video_sinks.contains(handle); }private:- Compositing::DisplayListResourceStorage m_resource_storage;+ DrawnFonts m_fonts;+ DrawnImageFrames m_image_frames;DisplayListRasterCache m_raster_cache;OwnedCanvasSurfaces m_canvas_surfaces;- Compositing::DisplayListResourceStorage m_overlay_resource_storage;+ DrawnFonts m_overlay_fonts;DisplayListRasterCache m_overlay_raster_cache;BackingStoreManager m_backing_stores;HashMap<u32, PresentableSurfacePool> m_presentable_surface_pools;diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cppindex 0eab39fc857..3c50f2e076f 100644--- a/Services/Compositor/DisplayListPlayerSkia.cpp+++ b/Services/Compositor/DisplayListPlayerSkia.cpp@@ -128,11 +128,12 @@ struct DisplayListPlayerSkia::PainterCallbacks {}};-RustFFI::FfiRasterPainter DisplayListPlayerSkia::begin_painting(Gfx::PaintingSurface& surface, DisplayListResourceStorage const& resource_storage, DisplayListRasterCache& raster_cache, OwnedCanvasSurfaces const* canvas_surfaces, CurrentVideoFrame current_video_frame)+RustFFI::FfiRasterPainter DisplayListPlayerSkia::begin_painting(Gfx::PaintingSurface& surface, DrawnFonts const& fonts, DrawnImageFrames const* image_frames, DisplayListRasterCache& raster_cache, OwnedCanvasSurfaces const* canvas_surfaces, CurrentVideoFrame current_video_frame){VERIFY(m_surfaces.is_empty());m_surfaces.append(surface);- m_resource_storage = &resource_storage;+ m_fonts = &fonts;+ m_image_frames = image_frames;m_raster_cache = &raster_cache;m_canvas_surfaces = canvas_surfaces;m_current_video_frame = move(current_video_frame);@@ -146,7 +147,8 @@ void DisplayListPlayerSkia::end_painting()VERIFY(m_raster_state->vector_images_in_progress.is_empty());m_raster_state->tile_raster_for_next_command = nullptr;m_surfaces.clear();- m_resource_storage = nullptr;+ m_fonts = nullptr;+ m_image_frames = nullptr;m_raster_cache = nullptr;m_canvas_surfaces = nullptr;m_current_video_frame = nullptr;@@ -323,7 +325,7 @@ void DisplayListPlayerSkia::play_command(DrawGlyphRun const& command)if (glyphs.is_empty())return;- auto blob = raster_cache().text_blob(resource_storage(), command.font_id, command.scale, glyphs, command.font_smoothing);+ auto blob = raster_cache().text_blob(font(command.font_id), command.font_id, command.scale, glyphs, command.font_smoothing);if (!blob)return;@@ -493,7 +495,7 @@ sk_sp<SkColorFilter> force_dark_image_color_filter()void DisplayListPlayerSkia::play_command(DrawScaledDecodedImageFrame const& command){- auto image = raster_cache().image_for_image_frame(resource_storage(), command.frame_id, m_skia_backend_context);+ auto image = raster_cache().image_for_image_frame(image_frame(command.frame_id).frame(), command.frame_id, m_skia_backend_context);if (!image)return;@@ -501,7 +503,7 @@ void DisplayListPlayerSkia::play_command(DrawScaledDecodedImageFrame const& commauto& canvas = surface().canvas();SkPaint paint;paint.setAntiAlias(true);- auto should_force_dark = command.apply_force_dark && resource_storage().image_frame_should_force_dark(command.frame_id);+ auto should_force_dark = command.apply_force_dark && image_frame(command.frame_id).should_force_dark();if (command.isolated_backdrop_color.has_value()) {auto src_rect = command.src_rect.value_or(Gfx::FloatRect { 0, 0, static_cast<float>(image->width()), static_cast<float>(image->height()) });SkMatrix matrix;@@ -532,8 +534,8 @@ void DisplayListPlayerSkia::play_command(DrawScaledDecodedImageFrame const& commvoid DisplayListPlayerSkia::play_command(DrawRepeatedDecodedImageFrame const& command){- auto const& frame = resource_storage().image_frame(command.frame_id);- auto image = raster_cache().image_for_image_frame(resource_storage(), command.frame_id, m_skia_backend_context);+ auto const& frame = image_frame(command.frame_id).frame();+ auto image = raster_cache().image_for_image_frame(image_frame(command.frame_id).frame(), command.frame_id, m_skia_backend_context);if (!image)return;@@ -550,7 +552,7 @@ void DisplayListPlayerSkia::play_command(DrawRepeatedDecodedImageFrame const& coSkPaint paint;paint.setAntiAlias(true);- auto should_force_dark = command.apply_force_dark && resource_storage().image_frame_should_force_dark(command.frame_id);+ auto should_force_dark = command.apply_force_dark && image_frame(command.frame_id).should_force_dark();if (command.isolated_backdrop_color.has_value()) {// The backdrop arrives already resolved by the recorder, so only the image gets the filter; filtering the// blended result would run the backdrop through the image transform on top of its own.@@ -602,7 +604,7 @@ static void paint_repeated_image(SkCanvas& canvas, SkImage& image, Gfx::FloatRecvoid DisplayListPlayerSkia::play_command(DrawTiledDecodedImageFrame const& command){- auto image = raster_cache().image_for_image_frame(resource_storage(), command.frame_id, m_skia_backend_context);+ auto image = raster_cache().image_for_image_frame(image_frame(command.frame_id).frame(), command.frame_id, m_skia_backend_context);if (!image)return;@@ -618,7 +620,7 @@ void DisplayListPlayerSkia::play_command(DrawTiledDecodedImageFrame const& commaauto* tile_canvas = tile_recorder.beginRecording(tile_bounds);SkPaint tile_paint;tile_paint.setAntiAlias(true);- if (command.apply_force_dark && resource_storage().image_frame_should_force_dark(command.frame_id))+ if (command.apply_force_dark && image_frame(command.frame_id).should_force_dark())tile_paint.setColorFilter(force_dark_image_color_filter());tile_canvas->drawImageRect(image.get(),@@ -938,7 +940,7 @@ void DisplayListPlayerSkia::play_command(PaintTextShadow const& command)if (glyphs.is_empty() || layers.is_empty())return;- auto blob = raster_cache().text_blob(resource_storage(), command.font_id, command.scale, glyphs, command.font_smoothing);+ auto blob = raster_cache().text_blob(font(command.font_id), command.font_id, command.scale, glyphs, command.font_smoothing);if (!blob)return;@@ -1677,7 +1679,7 @@ sk_sp<SkImageFilter> DisplayListPlayerSkia::layer_image_filter(ReplayLayer constsk_sp<SkImageFilter> DisplayListPlayerSkia::image_filter_from_bytes(ReadonlyBytes filter_bytes, Optional<Gfx::FloatRect> blur_edge){- return Gfx::to_skia_image_filter(filter_bytes, [&](u64 image_id) -> Gfx::DecodedImageFrame const& { return resource_storage().image_frame(ImageFrameResourceId { image_id }); }, blur_edge);+ return Gfx::to_skia_image_filter(filter_bytes, [&](u64 image_id) -> Gfx::DecodedImageFrame const& { return image_frame(ImageFrameResourceId { image_id }).frame(); }, blur_edge);}// A mask that applies to a box's filtered backdrop starts its layer with that backdrop, read from thediff --git a/Services/Compositor/DisplayListPlayerSkia.h b/Services/Compositor/DisplayListPlayerSkia.hindex 90f461453e7..a2efb90f659 100644--- a/Services/Compositor/DisplayListPlayerSkia.h+++ b/Services/Compositor/DisplayListPlayerSkia.h@@ -12,9 +12,10 @@#include <AK/Optional.h>#include <AK/RefPtr.h>#include <AK/Vector.h>+#include <Compositor/DrawnResources.h>#include <Compositor/Forward.h>#include <LibCompositing/DisplayList/DisplayListCommand.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibCompositing/RustFFI.h>#include <LibGfx/Forward.h>#include <LibGfx/Rect.h>@@ -51,9 +52,10 @@ public:// Answers the frame that a video sink shows now, which is null for a sink that shows none.using CurrentVideoFrame = Function<RefPtr<Media::VideoFrame>(Media::VideoSinkHandle)>;- // The painter paints into `surface`, with the resources of `resource_storage` and the caches of `raster_cache`,- // until end_painting(). It draws no canvas without `canvas_surfaces`, and no video without `current_video_frame`.- Compositing::RustFFI::FfiRasterPainter begin_painting(Gfx::PaintingSurface&, Compositing::DisplayListResourceStorage const&, DisplayListRasterCache&, OwnedCanvasSurfaces const*, CurrentVideoFrame);+ // The painter paints into `surface`, with `fonts`, `image_frames` and the caches of `raster_cache`, until+ // end_painting(). It draws no image frame without `image_frames`, no canvas without `canvas_surfaces`, and no video+ // without `current_video_frame`.+ Compositing::RustFFI::FfiRasterPainter begin_painting(Gfx::PaintingSurface&, DrawnFonts const&, DrawnImageFrames const*, DisplayListRasterCache&, OwnedCanvasSurfaces const*, CurrentVideoFrame);void end_painting();static void flush(Gfx::PaintingSurface&);@@ -63,7 +65,8 @@ private:struct PainterCallbacks;Gfx::PaintingSurface& surface() const;- Compositing::DisplayListResourceStorage const& resource_storage() const { return *m_resource_storage; }+ Gfx::Font const& font(Compositing::FontResourceId id) const { return m_fonts->font(id); }+ Compositing::RegisteredImageFrame const& image_frame(Compositing::ImageFrameResourceId id) const { return m_image_frames->image_frame(id); }OwnedCanvasSurfaces const* canvas_surfaces() const { return m_canvas_surfaces; }DisplayListRasterCache& raster_cache() const { return *m_raster_cache; }@@ -128,7 +131,8 @@ private:// The surface painted into last, which a tile or vector image raster stacks on top of the target.Vector<NonnullRefPtr<Gfx::PaintingSurface>, 4> m_surfaces;- Compositing::DisplayListResourceStorage const* m_resource_storage { nullptr };+ DrawnFonts const* m_fonts { nullptr };+ DrawnImageFrames const* m_image_frames { nullptr };DisplayListRasterCache* m_raster_cache { nullptr };OwnedCanvasSurfaces const* m_canvas_surfaces { nullptr };CurrentVideoFrame m_current_video_frame;diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cppindex ea8fd55b7fd..f0a60f934b4 100644--- a/Services/Compositor/DisplayListRasterCache.cpp+++ b/Services/Compositor/DisplayListRasterCache.cpp@@ -6,6 +6,7 @@#include <AK/BitCast.h>#include <AK/ByteBuffer.h>+#include <AK/HashTable.h>#include <AK/ScopeGuard.h>#include <Compositor/DisplayListRasterCache.h>#include <Compositor/FontSkia.h>@@ -13,7 +14,7 @@#include <Compositor/SkiaUtils.h>#include <Compositor/VideoFrameSkia.h>#include <LibCompositing/DisplayList/DisplayListCommand.h>-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>#include <LibGfx/Bitmap.h>#include <LibGfx/ColorSpace.h>#include <LibGfx/Font/Font.h>@@ -188,21 +189,28 @@ struct DisplayListRasterCache::ClipPath {DisplayListRasterCache::DisplayListRasterCache() = default;DisplayListRasterCache::~DisplayListRasterCache() = default;-void DisplayListRasterCache::evict(DisplayListResourceSet const& removed_resources)+void DisplayListRasterCache::forget_fonts(ReadonlySpan<u64> font_ids){- // Filters retain images resolved from this storage, including resources whose IDs were replaced.- if (!removed_resources.image_frames.is_empty())+ if (font_ids.is_empty())+ return;+ HashTable<u64> forgotten_font_ids;+ for (auto id : font_ids)+ forgotten_font_ids.set(id);+ m_text_blobs.remove_all_matching([&](auto const& key, auto const& text_blob) {+ if (!forgotten_font_ids.contains(key.font_id))+ return false;+ m_text_blob_cache_bytes -= text_blob->byte_size;+ return true;+ });+}++void DisplayListRasterCache::forget_image_frames(ReadonlySpan<u64> image_frame_ids)+{+ // Filters retain the images of the image frames that they were made with.+ if (!image_frame_ids.is_empty())m_layer_image_filter_cache = nullptr;- for (auto id : removed_resources.image_frames)- m_image_frame_images.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 }))- return false;- m_text_blob_cache_bytes -= text_blob->byte_size;- return true;- });- }+ for (auto id : image_frame_ids)+ m_image_frame_images.remove(id);}DisplayListRasterCache::LayerImageFilterCache& DisplayListRasterCache::ensure_layer_image_filter_cache()@@ -258,9 +266,8 @@ static sk_sp<SkImage> create_skia_image(Gfx::DecodedImageFrame const& frame, Refreturn raster_image;}-sk_sp<SkImage> DisplayListRasterCache::image_for_image_frame(DisplayListResourceStorage const& resource_storage, ImageFrameResourceId id, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+sk_sp<SkImage> DisplayListRasterCache::image_for_image_frame(Gfx::DecodedImageFrame const& frame, ImageFrameResourceId id, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){- auto const& frame = resource_storage.image_frame(id);if (auto cached = m_image_frame_images.get(id.value()); cached.has_value()) {auto const& image = *cached.value();// A frame replaced under the same id has pixels of its own, so this also catches a replacement that was@@ -487,7 +494,7 @@ static sk_sp<SkTextBlob> make_text_blob(Gfx::Font const& font, float scale, Readreturn builder.make();}-sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(DisplayListResourceStorage const& resource_storage, FontResourceId font_id, float scale, ReadonlySpan<DisplayListGlyph> glyphs, u8 font_smoothing)+sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(Gfx::Font const& font, FontResourceId font_id, float scale, ReadonlySpan<DisplayListGlyph> glyphs, u8 font_smoothing){constexpr size_t max_text_blob_cache_bytes = 16 * MiB;constexpr auto text_blob_idle_duration = AK::Duration::from_milliseconds(250);@@ -506,7 +513,7 @@ sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(DisplayListResourceStorage cm_text_blobs.remove(cached);}- auto blob = make_text_blob(resource_storage.font(font_id), scale, glyphs, font_smoothing);+ auto blob = make_text_blob(font, scale, glyphs, font_smoothing);if (!blob)return nullptr;diff --git a/Services/Compositor/DisplayListRasterCache.h b/Services/Compositor/DisplayListRasterCache.hindex 03f2e25be61..9a97fcc49d0 100644--- a/Services/Compositor/DisplayListRasterCache.h+++ b/Services/Compositor/DisplayListRasterCache.h@@ -64,8 +64,8 @@ struct Traits<Compositor::TextBlobCacheKey> : public DefaultTraits<Compositor::Tnamespace Compositor {-// The Skia images and text blobs that the player makes from the resources of one storage. They are kept until the-// storage removes the resource they were made from.+// The Skia images and text blobs that the player makes from the fonts and the image frames that one display list, or+// what else is painted, draws. They are kept until it stops drawing what they were made from.class DisplayListRasterCache {AK_MAKE_NONCOPYABLE(DisplayListRasterCache);AK_MAKE_NONMOVABLE(DisplayListRasterCache);@@ -74,14 +74,16 @@ public:DisplayListRasterCache();~DisplayListRasterCache();- // Drops everything made from these resources, which the storage removed or replaced.- void evict(Compositing::DisplayListResourceSet const& removed_resources);+ // Drops everything made from these fonts, which are no longer drawn.+ void forget_fonts(ReadonlySpan<u64> font_ids);+ // Drops everything made from these image frames, which are no longer drawn.+ void forget_image_frames(ReadonlySpan<u64> image_frame_ids);// 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&);+ sk_sp<SkImage> image_for_image_frame(Gfx::DecodedImageFrame 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// the part of it to draw.sk_sp<SkImage> image_for_video_sink(Media::VideoSinkHandle, Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&, Gfx::IntSize& visible_size);@@ -94,7 +96,7 @@ public:bool reserve_vector_image_raster(Compositing::DisplayListResourceId, RefPtr<Gfx::SkiaBackendContext> const&, Gfx::IntRect rect_in_recorded_space);// Keeps the raster that was reserved, unless it could not be made.sk_sp<SkImage> finish_vector_image_raster(Compositing::DisplayListResourceId, RefPtr<Gfx::SkiaBackendContext> const&, Gfx::IntRect rect_in_recorded_space, sk_sp<SkImage>);- sk_sp<SkTextBlob> text_blob(Compositing::DisplayListResourceStorage const&, Compositing::FontResourceId, float scale, ReadonlySpan<Compositing::DisplayListGlyph>, u8 font_smoothing);+ sk_sp<SkTextBlob> text_blob(Gfx::Font const&, Compositing::FontResourceId, float scale, ReadonlySpan<Compositing::DisplayListGlyph>, u8 font_smoothing);sk_sp<SkImageFilter> layer_image_filter(Compositing::ReplayLayer const&, Function<sk_sp<SkImageFilter>()> const& create);sk_sp<SkImageFilter> backdrop_image_filter(Compositing::ReplayLayer const&, Optional<Gfx::FloatRect> blur_edge, bool limited_to_region, Function<sk_sp<SkImageFilter>()> const& create);diff --git a/Services/Compositor/DrawnResources.cpp b/Services/Compositor/DrawnResources.cppnew file mode 100644index 00000000000..53c1c38b563--- /dev/null+++ b/Services/Compositor/DrawnResources.cpp@@ -0,0 +1,82 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/HashTable.h>+#include <Compositor/DrawnResources.h>+#include <LibCompositing/DisplayList/RegisteredTypefaces.h>+#include <LibGfx/Font/Font.h>++namespace Compositor {++template<typename Value>+static Vector<u64> retain_only_the_ids(HashMap<u64, Value>& values_by_id, ReadonlySpan<u64> ids)+{+ HashTable<u64> retained_ids;+ for (auto id : ids)+ retained_ids.set(id);+ Vector<u64> removed_ids;+ values_by_id.remove_all_matching([&](auto id, auto const&) {+ if (retained_ids.contains(id))+ return false;+ removed_ids.append(id);+ return true;+ });+ return removed_ids;+}++bool DrawnFonts::add_described_fonts_made_of_registered_typefaces(ReadonlySpan<Compositing::DisplayListFontDescription> descriptions, Compositing::RegisteredTypefaces const& registered_typefaces)+{+ bool every_typeface_is_registered = true;+ for (auto const& description : descriptions) {+ // NB: Font IDs are immutable. Keep the typeface that Skia's glyph caches were built for.+ if (contains(description.id))+ continue;+ auto font = registered_typefaces.font_described_by(description);+ if (!font) {+ every_typeface_is_registered = false;+ continue;+ }+ m_fonts_by_id.set(description.id.value(), font.release_nonnull());+ }+ return every_typeface_is_registered;+}++Compositing::FontResourceId DrawnFonts::add(Gfx::Font const& font)+{+ auto id = font.id();+ m_fonts_by_id.ensure(id, [&]() -> NonnullRefPtr<Gfx::Font const> { return font; });+ return { id };+}++Vector<u64> DrawnFonts::retain_only(ReadonlySpan<u64> font_ids)+{+ return retain_only_the_ids(m_fonts_by_id, font_ids);+}++bool DrawnImageFrames::take_registered(ReadonlySpan<u64> image_frame_ids, Compositing::RegisteredImageFrames const& registered_image_frames)+{+ Vector<NonnullRefPtr<Compositing::RegisteredImageFrame>> image_frames_to_hold;+ Vector<u64> ids_of_image_frames_to_hold;+ for (auto id : image_frame_ids) {+ if (m_image_frames_by_id.contains(id))+ continue;+ auto image_frame = registered_image_frames.image_frame(Compositing::ImageFrameResourceId { id });+ if (!image_frame)+ return false;+ image_frames_to_hold.append(image_frame.release_nonnull());+ ids_of_image_frames_to_hold.append(id);+ }+ for (size_t i = 0; i < image_frames_to_hold.size(); ++i)+ m_image_frames_by_id.set(ids_of_image_frames_to_hold[i], move(image_frames_to_hold[i]));+ return true;+}++Vector<u64> DrawnImageFrames::retain_only(ReadonlySpan<u64> image_frame_ids)+{+ return retain_only_the_ids(m_image_frames_by_id, image_frame_ids);+}++}diff --git a/Services/Compositor/DrawnResources.h b/Services/Compositor/DrawnResources.hnew file mode 100644index 00000000000..f8d3c2c4113--- /dev/null+++ b/Services/Compositor/DrawnResources.h@@ -0,0 +1,63 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/HashMap.h>+#include <AK/NonnullRefPtr.h>+#include <AK/Span.h>+#include <AK/Vector.h>+#include <LibCompositing/DisplayList/DisplayListResources.h>+#include <LibCompositing/DisplayList/RegisteredImageFrames.h>+#include <LibCompositing/Forward.h>+#include <LibGfx/Forward.h>++namespace Compositor {++// The fonts that a display list, the overlays, a batch of a canvas 2D stream or a staged rasterization draws, each+// under the id that its commands name it by.+class DrawnFonts {+public:+ [[nodiscard]] bool add_described_fonts_made_of_registered_typefaces(ReadonlySpan<Compositing::DisplayListFontDescription>, Compositing::RegisteredTypefaces const&);+ Compositing::FontResourceId add(Gfx::Font const&);++ bool contains(Compositing::FontResourceId id) const { return m_fonts_by_id.contains(id.value()); }+ Gfx::Font const& font(Compositing::FontResourceId id) const { return *m_fonts_by_id.get(id.value()).value(); }++ // Returns the ids of the fonts that were removed.+ Vector<u64> retain_only(ReadonlySpan<u64> font_ids);++private:+ HashMap<u64, NonnullRefPtr<Gfx::Font const>> m_fonts_by_id;+};++// The registered image frames that a display list and its visual context tree, or a staged rasterization, draw.+class DrawnImageFrames {+public:+ // Holds the image frame registered under each id that it holds none for, and answers whether it then holds one+ // for every id. It takes none where one of them is missing.+ [[nodiscard]] bool take_registered(ReadonlySpan<u64> image_frame_ids, Compositing::RegisteredImageFrames const&);++ size_t size() const { return m_image_frames_by_id.size(); }+ bool contains(Compositing::ImageFrameResourceId id) const { return m_image_frames_by_id.contains(id.value()); }+ Compositing::RegisteredImageFrame const& image_frame(Compositing::ImageFrameResourceId id) const { return *m_image_frames_by_id.get(id.value()).value(); }+ Compositing::RegisteredImageFrame& image_frame(Compositing::ImageFrameResourceId id) { return *m_image_frames_by_id.get(id.value()).value(); }++ template<typename Callback>+ void for_each(Callback callback) const+ {+ for (auto const& [id, image_frame] : m_image_frames_by_id)+ callback(Compositing::ImageFrameResourceId { id }, *image_frame);+ }++ // Returns the ids of the image frames that were removed.+ Vector<u64> retain_only(ReadonlySpan<u64> image_frame_ids);++private:+ HashMap<u64, NonnullRefPtr<Compositing::RegisteredImageFrame>> m_image_frames_by_id;+};++}