Truly independent web browser
Compositor: Present canvases from surfaces the window server can show
Aliaksandr Kalenik70f0918777f6
diff --git a/Services/Compositor/CMakeLists.txt b/Services/Compositor/CMakeLists.txtindex f6825127a8b..cd21a65b07d 100644--- a/Services/Compositor/CMakeLists.txt+++ b/Services/Compositor/CMakeLists.txt@@ -21,6 +21,7 @@ set(SOURCESHostWebGLContext.cppOpenGLContext.cppPaintingSurface.cpp+ PresentableSurface.cppPausedDebuggerOverlay.cppPictureInPictureControls.cppRasterizeDisplayList.cppdiff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cppindex ccde3e910a0..368f8eb3b15 100644--- a/Services/Compositor/CanvasHost.cpp+++ b/Services/Compositor/CanvasHost.cpp@@ -115,14 +115,11 @@ static void copy_surface_contents(Gfx::PaintingSurface& source, Gfx::PaintingSurdestination.copy_from_surface(source);}-static ErrorOr<NonnullRefPtr<Gfx::PaintingSurface>> create_presented_canvas_surface(Gfx::PaintingSurface& source, RefPtr<Gfx::SkiaBackendContext> skia_backend_context)+// The pool keeps a surface that the window server shows, or that a frame takes to it, as it is.+static ErrorOr<NonnullRefPtr<PresentableSurface>> present_into_surface_of_pool(PresentableSurfacePool& pool, Gfx::PaintingSurface& source, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){- auto surface = TRY(Gfx::PaintingSurface::try_create_with_size(- source.size(),- Gfx::BitmapFormat::BGRA8888,- Gfx::AlphaType::Premultiplied,- move(skia_backend_context)));- copy_surface_contents(source, *surface);+ auto surface = TRY(pool.surface_for_next_content(source.size(), skia_backend_context));+ copy_surface_contents(source, surface->painting_surface());return surface;}@@ -141,16 +138,18 @@ bool CanvasHost::create_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize// NB: A canvas that draws in memory still presents onto the GPU, if there is one. A present then uploads it once,// instead of every composited frame uploading a fresh snapshot of it.- auto presented_surface_or_error = create_presented_canvas_surface(command_player->surface(), m_skia_backend_context);+ PresentableSurfacePool presented_surfaces;+ auto presented_surface_or_error = present_into_surface_of_pool(presented_surfaces, command_player->surface(), m_skia_backend_context);if (presented_surface_or_error.is_error()) {dbgln("Unable to create the presented surface of a {}x{} 2D canvas context: {}", size.width(), size.height(), presented_surface_or_error.error());return false;}auto presented_surface = presented_surface_or_error.release_value();- m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, presented_surface);+ m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, *presented_surface);Canvas2DContext context {.command_player = command_player.release_nonnull(),- .presented_surface = move(presented_surface),+ .presented_surfaces = move(presented_surfaces),+ .presented_surface = presented_surface->painting_surface(),};m_contexts.set(canvas_id, move(context));return true;@@ -184,8 +183,15 @@ CanvasHost::Context* CanvasHost::context(Compositing::CanvasId canvas_id)void CanvasHost::present_canvas_2d_context(Compositing::CanvasId canvas_id, Canvas2DContext& context){- copy_surface_contents(context.command_player->surface(), context.presented_surface);- m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, context.presented_surface);+ auto presented_surface_or_error = present_into_surface_of_pool(context.presented_surfaces, context.command_player->surface(), m_skia_backend_context);+ // The canvas goes on showing what it presented before, and presents again with its next commands.+ if (presented_surface_or_error.is_error()) {+ dbgln("Unable to present a 2D canvas context: {}", presented_surface_or_error.error());+ return;+ }+ auto presented_surface = presented_surface_or_error.release_value();+ m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, *presented_surface);+ context.presented_surface = presented_surface->painting_surface();context.has_uncommitted_commands = false;}@@ -342,8 +348,12 @@ void CanvasHost::present_webgl_canvas(Compositing::CanvasId canvas_id, bool presauto* context = this->context(canvas_id);VERIFY(context);- auto surface = MUST(as_webgl(*context).prepare_for_compositing(preserve_drawing_buffer));- m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, move(surface));+ auto& webgl_context = as_webgl(*context);+ auto surface = MUST(webgl_context.prepare_for_compositing(preserve_drawing_buffer));+ if (auto presentable_surface = webgl_context.presented_presentable_surface(); presentable_surface && &presentable_surface->painting_surface() == surface.ptr())+ m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, *presentable_surface);+ else+ m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, move(surface));}void CanvasHost::clear_webgl_drawing_buffer(Compositing::CanvasId canvas_id)diff --git a/Services/Compositor/CanvasHost.h b/Services/Compositor/CanvasHost.hindex b95af5ec37d..bd53e7e8a80 100644--- a/Services/Compositor/CanvasHost.h+++ b/Services/Compositor/CanvasHost.h@@ -21,6 +21,7 @@#include <AK/Vector.h>#include <Compositor/Forward.h>#include <Compositor/HostWebGLContext.h>+#include <Compositor/PresentableSurface.h>#include <LibCompositing/DisplayList/DisplayListResourceIds.h>#include <LibCompositing/Forward.h>#include <LibCompositing/Types.h>@@ -87,6 +88,7 @@ public:private:struct Canvas2DContext {NonnullOwnPtr<Canvas2DCommandPlayer> command_player;+ PresentableSurfacePool presented_surfaces;NonnullRefPtr<Gfx::PaintingSurface> presented_surface;bool has_uncommitted_commands { false };};diff --git a/Services/Compositor/CanvasSurfaceRegistry.h b/Services/Compositor/CanvasSurfaceRegistry.hindex cdc8d5ebc21..cbd4a54557b 100644--- a/Services/Compositor/CanvasSurfaceRegistry.h+++ b/Services/Compositor/CanvasSurfaceRegistry.h@@ -9,7 +9,9 @@#include <AK/HashMap.h>#include <AK/Noncopyable.h>#include <AK/NonnullRefPtr.h>+#include <AK/WeakPtr.h>#include <Compositor/PaintingSurface.h>+#include <Compositor/PresentableSurface.h>#include <LibCompositing/DisplayList/DisplayListResourceIds.h>namespace Compositor {@@ -44,9 +46,13 @@ public:void set_canvas_surface(Compositing::CanvasSurfaceOwnerId owner_id, Compositing::CanvasId id, NonnullRefPtr<Gfx::PaintingSurface> surface){- if (auto existing = m_entries.get(id); existing.has_value())- VERIFY(existing->owner_id == owner_id);- m_entries.set(id, { .owner_id = owner_id, .surface = move(surface), .content_generation = ++m_last_content_generation });+ set_entry(owner_id, id, move(surface), {});+ }++ // The canvas shows the content of a surface that the window server can show as it is.+ void set_canvas_surface(Compositing::CanvasSurfaceOwnerId owner_id, Compositing::CanvasId id, PresentableSurface& surface)+ {+ set_entry(owner_id, id, surface.painting_surface(), surface.make_weak_ptr());}void remove_canvas_surface(Compositing::CanvasId id)@@ -66,13 +72,28 @@ public:return entry ? entry->content_generation : 0;}+ // Null for a canvas whose content the window server cannot show as it is. Whoever holds the surface keeps it busy.+ RefPtr<PresentableSurface> presentable_canvas_surface(Compositing::CanvasSurfaceOwnerId owner_id, Compositing::CanvasId id) const+ {+ auto const* entry = entry_of(owner_id, id);+ return entry ? entry->presentable_surface.strong_ref() : nullptr;+ }+private:struct Entry {Compositing::CanvasSurfaceOwnerId owner_id;NonnullRefPtr<Gfx::PaintingSurface> surface;+ WeakPtr<PresentableSurface> presentable_surface;u64 content_generation { 0 };};+ void set_entry(Compositing::CanvasSurfaceOwnerId owner_id, Compositing::CanvasId id, NonnullRefPtr<Gfx::PaintingSurface> surface, WeakPtr<PresentableSurface> presentable_surface)+ {+ if (auto existing = m_entries.get(id); existing.has_value())+ VERIFY(existing->owner_id == owner_id);+ m_entries.set(id, { .owner_id = owner_id, .surface = move(surface), .presentable_surface = move(presentable_surface), .content_generation = ++m_last_content_generation });+ }+Entry const* entry_of(Compositing::CanvasSurfaceOwnerId owner_id, Compositing::CanvasId id) const{auto it = m_entries.find(id);@@ -98,6 +119,7 @@ public:Gfx::PaintingSurface const* canvas_surface(Compositing::CanvasId id) const { return m_registry->canvas_surface(m_owner_id, id); }u64 canvas_content_generation(Compositing::CanvasId id) const { return m_registry->canvas_content_generation(m_owner_id, id); }+ RefPtr<PresentableSurface> presentable_canvas_surface(Compositing::CanvasId id) const { return m_registry->presentable_canvas_surface(m_owner_id, id); }private:CanvasSurfaceRegistry const* m_registry { nullptr };diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 6e7f12b641a..f7eaab108e9 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -395,7 +395,7 @@ CompositorState::PlaceholderCanvasAllocation CompositorState::allocate_placehold{auto canvas_id = m_canvas_surface_registry.allocate_canvas_id();auto secret = get_random<u64>();- m_placeholder_canvases.set(canvas_id, { .owner = &client, .secret = secret, .surface = nullptr, .size = {}, .origin_clean = true });+ m_placeholder_canvases.set(canvas_id, { .owner = &client, .secret = secret, .presented_surfaces = {}, .surface = nullptr, .size = {}, .origin_clean = true });return { canvas_id, secret };}@@ -416,10 +416,15 @@ void CompositorState::commit_placeholder_canvas(Compositing::CanvasId canvas_id,auto& placeholder = it->value;if (source_surface) {- if (!placeholder.surface || placeholder.surface->size() != source_surface->size())- placeholder.surface = Gfx::PaintingSurface::create_with_size(source_surface->size(), Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, m_skia_backend_context);- placeholder.surface->copy_from_surface(*source_surface);- m_canvas_surface_registry.set_canvas_surface(placeholder.owner->canvas_surface_owner_id(), canvas_id, *placeholder.surface);+ auto presented_surface_or_error = placeholder.presented_surfaces.surface_for_next_content(source_surface->size(), m_skia_backend_context);+ if (presented_surface_or_error.is_error()) {+ dbgln("Unable to commit a placeholder canvas: {}", presented_surface_or_error.error());+ return;+ }+ auto presented_surface = presented_surface_or_error.release_value();+ presented_surface->painting_surface().copy_from_surface(*source_surface);+ placeholder.surface = presented_surface->painting_surface();+ m_canvas_surface_registry.set_canvas_surface(placeholder.owner->canvas_surface_owner_id(), canvas_id, *presented_surface);} else {placeholder.surface = nullptr;m_canvas_surface_registry.remove_canvas_surface(canvas_id);diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.hindex a8a687bfc3a..794d053df89 100644--- a/Services/Compositor/CompositorState.h+++ b/Services/Compositor/CompositorState.h@@ -375,6 +375,7 @@ private:struct PlaceholderCanvas {CompositorStateWebContentClient* owner { nullptr };u64 secret { 0 };+ PresentableSurfacePool presented_surfaces;RefPtr<Gfx::PaintingSurface> surface;Optional<Gfx::IntSize> size;bool origin_clean { true };diff --git a/Services/Compositor/HostWebGLContext.h b/Services/Compositor/HostWebGLContext.hindex 9476635c948..41d3bc1334a 100644--- a/Services/Compositor/HostWebGLContext.h+++ b/Services/Compositor/HostWebGLContext.h@@ -55,6 +55,7 @@ public:ErrorOr<NonnullRefPtr<Gfx::PaintingSurface>> prepare_for_compositing(bool preserve_drawing_buffer);void clear_drawing_buffer();RefPtr<Gfx::PaintingSurface> presented_surface();+ RefPtr<PresentableSurface> presented_presentable_surface() { return m_gl_context->presented_presentable_surface(); }OpenGLContext& gl_context() { return *m_gl_context; }diff --git a/Services/Compositor/OpenGLContext.cpp b/Services/Compositor/OpenGLContext.cppindex 902c0d31de0..c73a517b85e 100644--- a/Services/Compositor/OpenGLContext.cpp+++ b/Services/Compositor/OpenGLContext.cpp@@ -58,6 +58,7 @@ struct OpenGLContext::ColorBuffer {RefPtr<Gfx::PaintingSurface> painting_surface;#ifdef AK_OS_MACOSOwnPtr<Gfx::SharedImageBuffer> shared_image_buffer;+ RefPtr<PresentableSurface> presentable_surface;#endifEGLSurface surface { EGL_NO_SURFACE };#if defined(USE_VULKAN_DMABUF_IMAGES) || defined(USE_DIRECTX)@@ -84,9 +85,11 @@ struct OpenGLContext::Impl {// A shared drawing buffer is composited straight from GPU memory, so presenting moves the default framebuffer// to another color buffer instead of letting the page draw over the frame being composited. Besides the// presented buffer and the one being drawn, a third holds the frame presented before, which a raster submitted- // before the latest present may still be reading on the GPU. The CPU painting surface is a copy made when+ // before the latest present may still be reading on the GPU. A fourth is allocated only while the window server+ // shows the buffers itself and still reads the other two. The CPU painting surface is a copy made when// presenting, so it only uses the first.- Array<ColorBuffer, 3> color_buffers {};+ static constexpr size_t usual_color_buffer_count = 3;+ Array<ColorBuffer, usual_color_buffer_count + 1> color_buffers {};// What Skia draws for an upload into one of the page's textures, at the size of the latest upload.ColorBuffer upload_color_buffer {};Gfx::IntSize upload_color_buffer_size {};@@ -169,6 +172,7 @@ void OpenGLContext::free_color_buffer([[maybe_unused]] ColorBuffer& color_buffercolor_buffer.painting_surface = nullptr;# ifdef AK_OS_MACOScolor_buffer.shared_image_buffer = nullptr;+ color_buffer.presentable_surface = nullptr;# endif#endif}@@ -437,6 +441,7 @@ void OpenGLContext::allocate_iosurface_color_buffer(ColorBuffer& color_buffer, G{color_buffer.shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::create(size));color_buffer.painting_surface = Gfx::PaintingSurface::create_from_shared_image_buffer(*color_buffer.shared_image_buffer, *m_skia_backend_context, origin);+ color_buffer.presentable_surface = PresentableSurface::create_over_iosurface(*color_buffer.painting_surface, *color_buffer.shared_image_buffer, origin);EGLint const surface_attributes[] = {EGL_WIDTH,@@ -808,7 +813,7 @@ void OpenGLContext::present_drawing_buffer([[maybe_unused]] bool preserve_drawinauto& presented_color_buffer = m_impl->color_buffers[presented_index];presented_color_buffer.painting_surface->notify_content_will_change();- auto next_index = (presented_index + 1) % m_impl->color_buffers.size();+ auto next_index = next_color_buffer_index_after(presented_index);auto& next_color_buffer = m_impl->color_buffers[next_index];// NB: Without a second color buffer the page keeps drawing into the presented one, which a frame composited// while it draws can catch half drawn.@@ -827,6 +832,32 @@ void OpenGLContext::present_drawing_buffer([[maybe_unused]] bool preserve_drawin}#ifdef ENABLE_WEBGL+// The buffer after the presented one, unless the window server still reads it or a frame on its way there holds it.+// Then the other usual buffer, and when that one is busy too, the spare.+size_t OpenGLContext::next_color_buffer_index_after(size_t presented_index) const+{+ auto& color_buffers = m_impl->color_buffers;+ auto is_busy = [&](size_t index) {+# ifdef AK_OS_MACOS+ auto const& presentable_surface = color_buffers[index].presentable_surface;+ return presentable_surface && presentable_surface->is_busy();+# else+ (void)index;+ return false;+# endif+ };+ auto spare_index = Impl::usual_color_buffer_count;+ auto buffer_count = color_buffers[spare_index].is_allocated() ? color_buffers.size() : Impl::usual_color_buffer_count;+ for (size_t step = 1; step < buffer_count; ++step) {+ auto index = (presented_index + step) % buffer_count;+ if (!is_busy(index))+ return index;+ }+ if (!color_buffers[spare_index].is_allocated())+ return spare_index;+ return (presented_index + 1) % buffer_count;+}+bool OpenGLContext::allocate_color_buffer(ColorBuffer& color_buffer){return allocate_shared_color_buffer(color_buffer, m_size, Gfx::PaintingSurface::Origin::BottomLeft);@@ -968,6 +999,21 @@ RefPtr<Gfx::PaintingSurface> OpenGLContext::presented_surface()return m_impl->color_buffers[*m_impl->presented_color_buffer_index].painting_surface;}+RefPtr<PresentableSurface> OpenGLContext::presented_presentable_surface()+{+#ifdef AK_OS_MACOS+ if (!m_impl->presented_color_buffer_index.has_value() || m_impl->uses_cpu_painting_surface)+ return nullptr;+ // When presenting could not move the page onto another buffer, it goes on drawing into the presented one, which+ // the window server would then show half drawn.+ if (*m_impl->presented_color_buffer_index == m_impl->drawing_color_buffer_index)+ return nullptr;+ return m_impl->color_buffers[*m_impl->presented_color_buffer_index].presentable_surface;+#else+ return nullptr;+#endif+}+u32 OpenGLContext::default_renderbuffer() const{return m_impl->depth_buffer;diff --git a/Services/Compositor/OpenGLContext.h b/Services/Compositor/OpenGLContext.hindex 8a20a025ba5..655ace7d8b5 100644--- a/Services/Compositor/OpenGLContext.h+++ b/Services/Compositor/OpenGLContext.h@@ -12,6 +12,7 @@#include <AK/RefPtr.h>#include <AK/Vector.h>#include <Compositor/PaintingSurface.h>+#include <Compositor/PresentableSurface.h>#include <LibCompositing/WebGL/GLFunctions.h>#include <LibGfx/Forward.h>#include <LibGfx/Size.h>@@ -58,6 +59,8 @@ public:RefPtr<Gfx::PaintingSurface> drawing_buffer_surface();RefPtr<Gfx::PaintingSurface> presented_surface();+ // Null where the window server cannot show the presented buffer as it is.+ RefPtr<PresentableSurface> presented_presentable_surface();u32 default_framebuffer() const;u32 default_renderbuffer() const;@@ -88,6 +91,7 @@ private:void free_surface_resources();void free_color_buffer(ColorBuffer&);bool allocate_color_buffer(ColorBuffer&);+ size_t next_color_buffer_index_after(size_t presented_index) const;bool allocate_shared_color_buffer(ColorBuffer&, Gfx::IntSize, Gfx::PaintingSurface::Origin);void attach_color_buffer_to_default_framebuffer(ColorBuffer const&);u32 color_buffer_texture_target() const;diff --git a/Services/Compositor/PresentableSurface.cpp b/Services/Compositor/PresentableSurface.cppnew file mode 100644index 00000000000..84b114080fb--- /dev/null+++ b/Services/Compositor/PresentableSurface.cpp@@ -0,0 +1,135 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <Compositor/PresentableSurface.h>+#include <Compositor/SkiaBackendContext.h>+#include <LibGfx/SharedImageBuffer.h>++namespace Compositor {++PresentableSurface::PresentableSurface(NonnullRefPtr<Gfx::PaintingSurface> painting_surface, Gfx::PaintingSurface::Origin origin)+ : m_painting_surface(move(painting_surface))+ , m_origin(origin)+{+}++PresentableSurface::~PresentableSurface()+{+ VERIFY(m_lease_count == 0);+}++PresentableSurface::Lease::Lease(PresentableSurface& surface)+ : m_surface(surface)+{+ ++surface.m_lease_count;+}++PresentableSurface::Lease::Lease(Lease const& other)+ : m_surface(other.m_surface)+{+ if (m_surface)+ ++m_surface->m_lease_count;+}++PresentableSurface::Lease::Lease(Lease&& other)+ : m_surface(move(other.m_surface))+{+}++PresentableSurface::Lease& PresentableSurface::Lease::operator=(Lease const& other)+{+ if (this == &other)+ return *this;+ release();+ m_surface = other.m_surface;+ if (m_surface)+ ++m_surface->m_lease_count;+ return *this;+}++PresentableSurface::Lease& PresentableSurface::Lease::operator=(Lease&& other)+{+ if (this == &other)+ return *this;+ release();+ m_surface = move(other.m_surface);+ return *this;+}++PresentableSurface::Lease::~Lease()+{+ release();+}++void PresentableSurface::Lease::release()+{+ if (!m_surface)+ return;+ VERIFY(m_surface->m_lease_count > 0);+ --m_surface->m_lease_count;+ m_surface = nullptr;+}++#ifdef AK_OS_MACOS+NonnullRefPtr<PresentableSurface> PresentableSurface::create_over_iosurface(NonnullRefPtr<Gfx::PaintingSurface> painting_surface, Gfx::SharedImageBuffer const& shared_image_buffer, Gfx::PaintingSurface::Origin origin)+{+ auto surface = adopt_ref(*new PresentableSurface(move(painting_surface), origin));+ surface->m_iosurface = Core::IOSurfaceHandle::from_ref(shared_image_buffer.iosurface_handle().core_foundation_pointer());+ return surface;+}+#endif++bool PresentableSurface::is_read_by_window_server() const+{+#ifdef AK_OS_MACOS+ return m_was_handed_to_window_server && m_iosurface.has_value() && m_iosurface->is_in_use();+#else+ return false;+#endif+}++ErrorOr<NonnullRefPtr<PresentableSurface>> PresentableSurfacePool::create_surface(Gfx::IntSize size, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+{+#ifdef AK_OS_MACOS+ if (skia_backend_context) {+ auto shared_image_buffer = make<Gfx::SharedImageBuffer>(TRY(Gfx::SharedImageBuffer::try_create(size)));+ auto painting_surface = Gfx::PaintingSurface::create_from_shared_image_buffer(*shared_image_buffer, *skia_backend_context);+ auto surface = PresentableSurface::create_over_iosurface(move(painting_surface), *shared_image_buffer, Gfx::PaintingSurface::Origin::TopLeft);+ surface->m_owned_shared_image_buffer = move(shared_image_buffer);+ return surface;+ }+#endif+ auto painting_surface = TRY(Gfx::PaintingSurface::try_create_with_size(size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context));+ return adopt_ref(*new PresentableSurface(move(painting_surface), Gfx::PaintingSurface::Origin::TopLeft));+}++ErrorOr<NonnullRefPtr<PresentableSurface>> PresentableSurfacePool::surface_for_next_content(Gfx::IntSize size, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+{+ auto fits = [&](PresentableSurface const& surface) {+ return surface.size() == size && surface.painting_surface().skia_backend_context() == skia_backend_context;+ };+ m_surfaces.remove_all_matching([&](auto const& surface) {+ return !fits(*surface) && !surface->is_busy();+ });++ auto take = [&](size_t index) {+ auto surface = m_surfaces.take(index);+ m_surfaces.append(surface);+ return surface;+ };+ if (!m_surfaces.is_empty() && fits(*m_surfaces.last()) && !m_surfaces.last()->is_busy())+ return m_surfaces.last();+ for (size_t index = 0; index < m_surfaces.size(); ++index) {+ if (fits(*m_surfaces[index]) && !m_surfaces[index]->is_busy())+ return take(index);+ }+ if (m_surfaces.size() >= maximum_surface_count)+ return Error::from_string_literal("Every surface of the pool is busy");+ m_surfaces.append(TRY(create_surface(size, skia_backend_context)));+ return m_surfaces.last();+}++}diff --git a/Services/Compositor/PresentableSurface.h b/Services/Compositor/PresentableSurface.hnew file mode 100644index 00000000000..1f56714b76e--- /dev/null+++ b/Services/Compositor/PresentableSurface.h@@ -0,0 +1,119 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Noncopyable.h>+#include <AK/NonnullRefPtr.h>+#include <AK/Optional.h>+#include <AK/OwnPtr.h>+#include <AK/RefCounted.h>+#include <AK/Vector.h>+#include <AK/Weakable.h>+#include <Compositor/PaintingSurface.h>+#include <LibGfx/Forward.h>+#include <LibGfx/Size.h>++#ifdef AK_OS_MACOS+# include <LibCore/IOSurface.h>+#endif++namespace Compositor {++// A surface whose content the window server can show as it is, instead of after it is composited into a frame. While a+// lease of it is held, as by a frame on its way to the window server and by the layer that shows it, or while the+// window server still reads it, the surface is busy, and its content must stay as it is.+class PresentableSurface final+ : public RefCounted<PresentableSurface>+ , public Weakable<PresentableSurface> {+ AK_MAKE_NONCOPYABLE(PresentableSurface);+ AK_MAKE_NONMOVABLE(PresentableSurface);++public:+ // Keeps the content of the surface as it is for as long as it is held. Holding the surface itself does not.+ class Lease {+ public:+ Lease(Lease const&);+ Lease(Lease&&);+ Lease& operator=(Lease const&);+ Lease& operator=(Lease&&);+ ~Lease();++ PresentableSurface& surface() const { return *m_surface; }++ private:+ friend class PresentableSurface;++ explicit Lease(PresentableSurface&);+ void release();++ RefPtr<PresentableSurface> m_surface;+ };++#ifdef AK_OS_MACOS+ // Wraps a surface that draws into the IOSurface, which something else owns.+ static NonnullRefPtr<PresentableSurface> create_over_iosurface(NonnullRefPtr<Gfx::PaintingSurface>, Gfx::SharedImageBuffer const&, Gfx::PaintingSurface::Origin);+#endif+ ~PresentableSurface();++ Gfx::PaintingSurface& painting_surface() const { return *m_painting_surface; }+ Gfx::IntSize size() const { return m_painting_surface->size(); }+ bool content_is_upside_down() const { return m_origin == Gfx::PaintingSurface::Origin::BottomLeft; }++#ifdef AK_OS_MACOS+ // Null for a surface that draws into memory the window server cannot read.+ Core::IOSurfaceHandle const* iosurface() const { return m_iosurface.has_value() ? &m_iosurface.value() : nullptr; }+#endif++ // GPU work in flight on a surface counts as use too, so only a surface handed to the window server is checked.+ void did_hand_to_window_server() { m_was_handed_to_window_server = true; }+ bool is_read_by_window_server() const;+ Lease lease() { return Lease { *this }; }+ bool is_busy() const { return m_lease_count > 0 || is_read_by_window_server(); }++private:+ friend class PresentableSurfacePool;++ PresentableSurface(NonnullRefPtr<Gfx::PaintingSurface>, Gfx::PaintingSurface::Origin);++ NonnullRefPtr<Gfx::PaintingSurface> m_painting_surface;+ Gfx::PaintingSurface::Origin m_origin { Gfx::PaintingSurface::Origin::TopLeft };+ bool m_was_handed_to_window_server { false };+ size_t m_lease_count { 0 };+#ifdef AK_OS_MACOS+ OwnPtr<Gfx::SharedImageBuffer> m_owned_shared_image_buffer;+ Optional<Core::IOSurfaceHandle> m_iosurface;+#endif+};++// The surfaces that one piece of content, such as a canvas, is presented from in turn. The content goes into its+// latest surface again unless that one is busy, so content that is only ever composited into frames keeps a single+// surface.+class PresentableSurfacePool {+ AK_MAKE_NONCOPYABLE(PresentableSurfacePool);+ AK_MAKE_DEFAULT_MOVABLE(PresentableSurfacePool);++public:+ // A surface is only added while every other one is busy, so a pool holds about as many as are on their way to the+ // window server or still read by it at once, which is a few. The limit only stops a pool whose surfaces never stop+ // being busy from growing without end.+ static constexpr size_t maximum_surface_count = 8;++ PresentableSurfacePool() = default;+ ~PresentableSurfacePool() = default;++ // A surface of the size for the next content, which is never one that is busy. Fails when every surface is busy and+ // the pool is full, or when no surface can be allocated, and the content then has to wait for a later turn.+ ErrorOr<NonnullRefPtr<PresentableSurface>> surface_for_next_content(Gfx::IntSize, RefPtr<Gfx::SkiaBackendContext> const&);++private:+ static ErrorOr<NonnullRefPtr<PresentableSurface>> create_surface(Gfx::IntSize, RefPtr<Gfx::SkiaBackendContext> const&);++ // In the order the content went into them, the latest last.+ Vector<NonnullRefPtr<PresentableSurface>> m_surfaces;+};++}