Truly independent web browser
Compositor: Let the window server allocate the surfaces it shows
Aliaksandr Kalenik962e781c4dee
diff --git a/Services/Compositor/BackingStoreManager.cpp b/Services/Compositor/BackingStoreManager.cppindex 69232c94e30..a7c4afc3991 100644--- a/Services/Compositor/BackingStoreManager.cpp+++ b/Services/Compositor/BackingStoreManager.cpp@@ -159,12 +159,13 @@ Optional<BackingStoreManager::Allocation> BackingStoreManager::resize_backing_streturn {};}-Optional<BackingStoreManager::Publication> BackingStoreManager::allocate_backing_stores(Allocation const& allocation, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context, PublicationMode publication_mode, [[maybe_unused]] GpuSharing gpu_sharing)+Optional<BackingStoreManager::Publication> BackingStoreManager::allocate_backing_stores(Allocation const& allocation, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context, PublicationMode publication_mode, [[maybe_unused]] GpuSharing gpu_sharing, RefPtr<WindowServer> window_server_showing_stores){m_backing_stores.clear();m_rendering_store_index.clear();m_latest_rendered_store_index.clear();m_publication_mode = publication_mode;+ m_window_server_showing_stores = publication_mode == PublicationMode::ShownInRemoteLayer ? move(window_server_showing_stores) : nullptr;if (Gfx::Bitmap::size_would_overflow(Gfx::BitmapFormat::BGRA8888, allocation.size))return {};@@ -179,6 +180,7 @@ Optional<BackingStoreManager::Publication> BackingStoreManager::allocate_backingm_backing_stores.append({.surface = Gfx::PaintingSurface::create_with_size(allocation.size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context),.shared_image_buffer = nullptr,+ .window_server_surface = nullptr,.bitmap_id = allocation.bitmap_ids[i],.state = BufferState::Available,.accumulated_damage = { {}, allocation.size },@@ -188,8 +190,15 @@ Optional<BackingStoreManager::Publication> BackingStoreManager::allocate_backing}if (publication_mode == PublicationMode::ShownInRemoteLayer) {- for (size_t i = 0; i < buffer_count; ++i)- m_backing_stores.append(create_shareable_backing_store(allocation.size, allocation.bitmap_ids[i], BufferState::Available, skia_backend_context));+ for (size_t i = 0; i < buffer_count; ++i) {+ auto store = create_backing_store_shown_by_window_server(allocation.size, allocation.bitmap_ids[i], skia_backend_context);+ if (store.is_error()) {+ dbgln("Failed to allocate a backing store that the window server can show: {}", store.error());+ m_backing_stores.clear();+ return {};+ }+ m_backing_stores.append(store.release_value());+ }return Publication { .bitmap_ids = allocation.bitmap_ids, .shared_images = {} };}@@ -220,6 +229,7 @@ Optional<BackingStoreManager::Publication> BackingStoreManager::allocate_backingm_backing_stores.append({.surface = move(store.surface),.shared_image_buffer = nullptr,+ .window_server_surface = nullptr,.bitmap_id = allocation.bitmap_ids[i],.state = initial_buffer_state(i),.accumulated_damage = { {}, allocation.size },@@ -258,12 +268,27 @@ BackingStoreManager::BackingStore BackingStoreManager::create_shareable_backing_return {.surface = move(surface),.shared_image_buffer = move(shared_image_buffer),+ .window_server_surface = nullptr,.bitmap_id = bitmap_id,.state = state,.accumulated_damage = { {}, size },};}+ErrorOr<BackingStoreManager::BackingStore> BackingStoreManager::create_backing_store_shown_by_window_server(Gfx::IntSize size, i32 bitmap_id, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+{+ VERIFY(m_window_server_showing_stores);+ auto surface = TRY(m_window_server_showing_stores->try_create_presentable_surface(size, skia_backend_context));+ return BackingStore {+ .surface = surface->painting_surface(),+ .shared_image_buffer = nullptr,+ .window_server_surface = move(surface),+ .bitmap_id = bitmap_id,+ .state = BufferState::Available,+ .accumulated_damage = { {}, size },+ };+}+// The window server lets go of a store some time after the client replaced it on screen, which at high refresh rates// can be later than the next frame is due. Rather than hold that frame back, give it a store of its own. While the// client has yet to take a frame, or to release a store, there is no telling what the window server holds, and the@@ -277,7 +302,7 @@ Optional<BackingStoreManager::Publication> BackingStoreManager::add_backing_storfor (size_t i = 0; i < m_backing_stores.size(); ++i) {auto const& store = m_backing_stores[i];- if (!store.shared_image_buffer)+ if (!store.shared_image_buffer && !store.window_server_surface)return {};bool client_displays_store = i == *m_latest_rendered_store_index;if (client_displays_store && store.state != BufferState::Presented)@@ -287,7 +312,14 @@ Optional<BackingStoreManager::Publication> BackingStoreManager::add_backing_stor}auto bitmap_id = m_next_bitmap_id++;- m_backing_stores.append(create_shareable_backing_store(m_backing_store_size, bitmap_id, BufferState::Available, skia_backend_context));+ if (m_publication_mode == PublicationMode::ShownInRemoteLayer) {+ auto store = create_backing_store_shown_by_window_server(m_backing_store_size, bitmap_id, skia_backend_context);+ if (store.is_error())+ return {};+ m_backing_stores.append(store.release_value());+ } else {+ m_backing_stores.append(create_shareable_backing_store(m_backing_store_size, bitmap_id, BufferState::Available, skia_backend_context));+ }Vector<Gfx::SharedImage> shared_images;if (m_publication_mode == PublicationMode::Published)@@ -331,6 +363,8 @@ bool BackingStoreManager::is_valid() constbool BackingStoreManager::shared_surface_is_in_use(BackingStore const& store){+ if (store.window_server_surface)+ return store.window_server_surface->is_read_by_window_server();#ifdef AK_OS_MACOS// The send right exported to the UI counts as use as well, so a freshly published store reads as in use until// the UI has imported it.@@ -422,11 +456,11 @@ RefPtr<Gfx::PaintingSurface> BackingStoreManager::latest_rendered_surface() consreturn m_backing_stores[*m_latest_rendered_store_index].surface;}-Gfx::SharedImageBuffer const* BackingStoreManager::shared_image_buffer_of_latest_rendered_store() const+PresentableSurface* BackingStoreManager::window_server_surface_of_latest_rendered_store() const{if (!m_latest_rendered_store_index.has_value())return nullptr;- return m_backing_stores[*m_latest_rendered_store_index].shared_image_buffer.ptr();+ return m_backing_stores[*m_latest_rendered_store_index].window_server_surface.ptr();}}diff --git a/Services/Compositor/BackingStoreManager.h b/Services/Compositor/BackingStoreManager.hindex 473d4299061..761de8c58c3 100644--- a/Services/Compositor/BackingStoreManager.h+++ b/Services/Compositor/BackingStoreManager.h@@ -13,6 +13,8 @@#include <AK/Types.h>#include <AK/Vector.h>#include <Compositor/Forward.h>+#include <Compositor/PresentableSurface.h>+#include <Compositor/WindowServer.h>#include <LibCompositing/Types.h>#include <LibGfx/Forward.h>#include <LibGfx/SharedImage.h>@@ -62,7 +64,7 @@ public:Optional<Allocation> resize_backing_stores_if_needed(Gfx::IntSize viewport_size, Compositing::WindowResizingInProgress, PublicationMode);- Optional<Publication> allocate_backing_stores(Allocation const&, RefPtr<Gfx::SkiaBackendContext> const&, PublicationMode, GpuSharing);+ Optional<Publication> allocate_backing_stores(Allocation const&, RefPtr<Gfx::SkiaBackendContext> const&, PublicationMode, GpuSharing, RefPtr<WindowServer> window_server_showing_stores);void invalidate() { m_allocated_size = {}; }@@ -74,7 +76,7 @@ public:bool release_buffer(i32 bitmap_id);void release_all_presented_buffers();RefPtr<Gfx::PaintingSurface> latest_rendered_surface() const;- Gfx::SharedImageBuffer const* shared_image_buffer_of_latest_rendered_store() const;+ PresentableSurface* window_server_surface_of_latest_rendered_store() const;PublicationMode publication_mode() const { return m_publication_mode; }Optional<Publication> add_backing_store_if_window_server_still_reads_every_released_store(RefPtr<Gfx::SkiaBackendContext> const&);@@ -91,6 +93,7 @@ private:struct BackingStore {RefPtr<Gfx::PaintingSurface> surface;OwnPtr<Gfx::SharedImageBuffer> shared_image_buffer;+ RefPtr<PresentableSurface> window_server_surface;i32 bitmap_id { -1 };BufferState state { BufferState::Available };Gfx::IntRect accumulated_damage;@@ -101,6 +104,7 @@ private:static constexpr size_t maximum_backing_store_count = 8;static BackingStore create_shareable_backing_store(Gfx::IntSize, i32 bitmap_id, BufferState, RefPtr<Gfx::SkiaBackendContext> const&);+ ErrorOr<BackingStore> create_backing_store_shown_by_window_server(Gfx::IntSize, i32 bitmap_id, RefPtr<Gfx::SkiaBackendContext> const&);static bool shared_surface_is_in_use(BackingStore const&);static bool store_can_be_rendered_into(BackingStore const&);@@ -111,6 +115,7 @@ private:Gfx::IntSize m_backing_store_size;PublicationMode m_publication_mode { PublicationMode::Unpublished };+ RefPtr<WindowServer> m_window_server_showing_stores;size_t m_initial_backing_store_count { 0 };Vector<BackingStore> m_backing_stores;Optional<size_t> m_rendering_store_index;diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 16446bb21b3..26bceb1dc3f 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -1650,12 +1650,12 @@ void CompositorState::show_frame_in_remote_layer(Web::CompositorContextId contexVERIFY(context.remote_layer_id.has_value());auto remote_layer = m_remote_layers.find(*context.remote_layer_id);VERIFY(remote_layer != m_remote_layers.end());- auto const* shared_image_buffer = pending_present.keeps_shown_store ? nullptr : context.presenter.shared_image_buffer_of_latest_rendered_store();- VERIFY(shared_image_buffer || pending_present.keeps_shown_store);+ auto* store = pending_present.keeps_shown_store ? nullptr : context.presenter.window_server_surface_of_latest_rendered_store();+ VERIFY(store || pending_present.keeps_shown_store);auto frame_size = pending_present.viewport_rect.size();remote_layer->value.layer->show_frame({- .store = shared_image_buffer,+ .store = store ? Optional<PresentableSurface::Lease> { store->lease() } : OptionalNone {},.painted_size = frame_size,.device_pixel_ratio = pending_present.device_pixel_ratio,.fit_within = remote_layer->value.fit_within,@@ -1807,19 +1807,21 @@ CompositedContextSurface CompositorState::resolve_composited_context(Web::Composvoid CompositorState::resize_backing_stores_if_needed(Web::CompositorContextId context_id, PresentedContext& context){auto publication_mode = BackingStoreManager::PublicationMode::Unpublished;- if (context.remote_layer_id.has_value())+ RemoteLayerState const* remote_layer_showing_context = nullptr;+ if (context.remote_layer_id.has_value()) {+ auto remote_layer = m_remote_layers.find(*context.remote_layer_id);+ VERIFY(remote_layer != m_remote_layers.end());+ remote_layer_showing_context = &remote_layer->value;publication_mode = BackingStoreManager::PublicationMode::ShownInRemoteLayer;- else if (presentation_for_context(context_id))+ } else if (presentation_for_context(context_id)) {publication_mode = BackingStoreManager::PublicationMode::Published;+ }// The window server reads what it shows above the store straight from GPU memory.- RemoteLayer const* remote_layer_that_may_show_media = nullptr;- if (publication_mode == BackingStoreManager::PublicationMode::ShownInRemoteLayer && m_skia_backend_context && !context.presents_from_cpu_memory) {- if (auto remote_layer = m_remote_layers.find(*context.remote_layer_id); remote_layer != m_remote_layers.end())- remote_layer_that_may_show_media = remote_layer->value.layer.ptr();- }- context.presenter.set_remote_layer_that_may_show_media(remote_layer_that_may_show_media);- if (auto publication = context.presenter.resize_backing_stores_if_needed(m_skia_backend_context, gpu_sharing_for_context(context_id), publication_mode); publication.has_value()) {+ auto window_server_may_show_media = remote_layer_showing_context && m_skia_backend_context && !context.presents_from_cpu_memory;+ context.presenter.set_remote_layer_that_may_show_media(window_server_may_show_media ? remote_layer_showing_context->layer.ptr() : nullptr);+ auto window_server_showing_stores = remote_layer_showing_context ? RefPtr<WindowServer> { remote_layer_showing_context->window_server } : nullptr;+ if (auto publication = context.presenter.resize_backing_stores_if_needed(m_skia_backend_context, gpu_sharing_for_context(context_id), publication_mode, move(window_server_showing_stores)); publication.has_value()) {if (publication_mode == BackingStoreManager::PublicationMode::Published)publish_backing_stores(context_id, context, publication.release_value());present_current_frame(context_id, context);diff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cppindex fd731e35ae1..eaf7544b987 100644--- a/Services/Compositor/ContextPresenter.cpp+++ b/Services/Compositor/ContextPresenter.cpp@@ -129,7 +129,7 @@ Optional<Gfx::IntRect> ContextPresenter::viewport_rect_for_ui_overlay() constreturn viewport_rect;}-Optional<BackingStoreManager::Publication> ContextPresenter::resize_backing_stores_if_needed(RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context, BackingStoreManager::GpuSharing gpu_sharing, BackingStoreManager::PublicationMode publication_mode)+Optional<BackingStoreManager::Publication> ContextPresenter::resize_backing_stores_if_needed(RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context, BackingStoreManager::GpuSharing gpu_sharing, BackingStoreManager::PublicationMode publication_mode, RefPtr<WindowServer> window_server_showing_stores){if (m_backing_store_manager.is_rendering())return {};@@ -151,7 +151,7 @@ Optional<BackingStoreManager::Publication> ContextPresenter::resize_backing_storreturn {};m_latest_rendered_surface = nullptr;m_last_rasterized_frame.clear();- return m_backing_store_manager.allocate_backing_stores(*allocation, skia_backend_context, publication_mode, gpu_sharing);+ return m_backing_store_manager.allocate_backing_stores(*allocation, skia_backend_context, publication_mode, gpu_sharing, move(window_server_showing_stores));}void ContextPresenter::invalidate_backing_stores()diff --git a/Services/Compositor/ContextPresenter.h b/Services/Compositor/ContextPresenter.hindex 7afcafcf82e..b91ca3551b9 100644--- a/Services/Compositor/ContextPresenter.h+++ b/Services/Compositor/ContextPresenter.h@@ -94,7 +94,7 @@ public:bool ui_overlay_fades_need_frame() const { return m_picture_in_picture_controls_fade.is_fading(); }Optional<PendingFrame> advance_ui_overlay_fades(MonotonicTime frame_time);- Optional<BackingStoreManager::Publication> resize_backing_stores_if_needed(RefPtr<Gfx::SkiaBackendContext> const&, BackingStoreManager::GpuSharing, BackingStoreManager::PublicationMode);+ Optional<BackingStoreManager::Publication> resize_backing_stores_if_needed(RefPtr<Gfx::SkiaBackendContext> const&, BackingStoreManager::GpuSharing, BackingStoreManager::PublicationMode, RefPtr<WindowServer> window_server_showing_stores = {});void invalidate_backing_stores();Optional<BackingStoreManager::Publication> add_backing_store_for_pending_frame_if_needed(RefPtr<Gfx::SkiaBackendContext> const&);bool has_surplus_backing_stores() const { return m_backing_store_manager.has_surplus_backing_stores(); }@@ -104,7 +104,7 @@ public:void acknowledge_all_presented_bitmaps() { m_backing_store_manager.release_all_presented_buffers(); }void did_finish_gpu_present(i32 bitmap_id, Gfx::IntSize frame_size);RefPtr<Gfx::PaintingSurface> latest_rendered_surface() const { return m_latest_rendered_surface; }- Gfx::SharedImageBuffer const* shared_image_buffer_of_latest_rendered_store() const { return m_backing_store_manager.shared_image_buffer_of_latest_rendered_store(); }+ PresentableSurface* window_server_surface_of_latest_rendered_store() const { return m_backing_store_manager.window_server_surface_of_latest_rendered_store(); }BackingStoreManager::PublicationMode backing_store_publication_mode() const { return m_backing_store_manager.publication_mode(); }bool update_composited_raster_transform(Gfx::FloatRect destination_rect, Gfx::FloatMatrix4x4 const& canvas_transform);diff --git a/Services/Compositor/CoreAnimationWindowServer.h b/Services/Compositor/CoreAnimationWindowServer.hindex cadcf45d1b6..0f5fa666e0d 100644--- a/Services/Compositor/CoreAnimationWindowServer.h+++ b/Services/Compositor/CoreAnimationWindowServer.h@@ -18,17 +18,36 @@#include <Compositor/RemoteLayer.h>#include <Compositor/WindowServer.h>#include <LibCore/Forward.h>+#include <LibCore/IOSurface.h>+#include <LibGfx/Forward.h>#include <LibGfx/Size.h>#include <LibMediaCodecs/VideoFrame.h>namespace Compositor {+class CoreAnimationSurface final : public WindowServerSurface {+public:+ // Over an IOSurface that something else owns.+ explicit CoreAnimationSurface(Gfx::SharedImageBuffer const&);+ explicit CoreAnimationSurface(NonnullOwnPtr<Gfx::SharedImageBuffer>);+ virtual ~CoreAnimationSurface() override;++ Core::IOSurfaceHandle const& iosurface() const { return m_iosurface; }+ virtual bool may_be_read_by_window_server() const override { return m_iosurface.is_in_use(); }++private:+ OwnPtr<Gfx::SharedImageBuffer> m_owned_shared_image_buffer;+ Core::IOSurfaceHandle m_iosurface;+};+class CoreAnimationWindowServer final : public WindowServer {public:// None where the layer trees that another process shows by the ID of their context are not available.static RefPtr<CoreAnimationWindowServer> try_create();+ static ErrorOr<NonnullRefPtr<PresentableSurface>> try_create_presentable_surface_over_new_iosurface(Gfx::IntSize, Gfx::SkiaBackendContext&);virtual OwnPtr<RemoteLayer> try_create_remote_layer(u64 remote_layer_id) override;+ virtual ErrorOr<NonnullRefPtr<PresentableSurface>> try_create_presentable_surface(Gfx::IntSize, RefPtr<Gfx::SkiaBackendContext> const&) override;virtual FrameHandOff frame_hand_off() const override { return FrameHandOff::OnceGpuWorkIsScheduled; }virtual void hold_changes_on_screen() override;virtual void release_changes_held_off_screen() override;diff --git a/Services/Compositor/CoreAnimationWindowServer.mm b/Services/Compositor/CoreAnimationWindowServer.mmindex 2262a3b8ff6..0cfb4f2bb56 100644--- a/Services/Compositor/CoreAnimationWindowServer.mm+++ b/Services/Compositor/CoreAnimationWindowServer.mm@@ -108,6 +108,35 @@ OwnPtr<RemoteLayer> CoreAnimationWindowServer::try_create_remote_layer(u64)return CoreAnimationRemoteLayer::try_create();}+CoreAnimationSurface::CoreAnimationSurface(Gfx::SharedImageBuffer const& shared_image_buffer)+ : m_iosurface(Core::IOSurfaceHandle::from_ref(shared_image_buffer.iosurface_handle().core_foundation_pointer()))+{+}++CoreAnimationSurface::CoreAnimationSurface(NonnullOwnPtr<Gfx::SharedImageBuffer> shared_image_buffer)+ : m_owned_shared_image_buffer(move(shared_image_buffer))+ , m_iosurface(Core::IOSurfaceHandle::from_ref(m_owned_shared_image_buffer->iosurface_handle().core_foundation_pointer()))+{+}++CoreAnimationSurface::~CoreAnimationSurface() = default;++ErrorOr<NonnullRefPtr<PresentableSurface>> CoreAnimationWindowServer::try_create_presentable_surface_over_new_iosurface(Gfx::IntSize size, Gfx::SkiaBackendContext& 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);+ return PresentableSurface::create(move(painting_surface), Gfx::PaintingSurface::Origin::TopLeft, make<CoreAnimationSurface>(move(shared_image_buffer)));+}++ErrorOr<NonnullRefPtr<PresentableSurface>> CoreAnimationWindowServer::try_create_presentable_surface(Gfx::IntSize size, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+{+ if (skia_backend_context)+ return try_create_presentable_surface_over_new_iosurface(size, *skia_backend_context);+ auto shared_image_buffer = make<Gfx::SharedImageBuffer>(TRY(Gfx::SharedImageBuffer::try_create(size)));+ auto painting_surface = Gfx::PaintingSurface::wrap_bitmap(*shared_image_buffer->bitmap());+ return PresentableSurface::create(move(painting_surface), Gfx::PaintingSurface::Origin::TopLeft, make<CoreAnimationSurface>(move(shared_image_buffer)));+}+OwnPtr<CoreAnimationRemoteLayer> CoreAnimationRemoteLayer::try_create(){auto* connection_id_function = main_window_server_connection_id_function();@@ -170,7 +199,7 @@ bool CoreAnimationRemoteLayer::can_show(WindowServerPlacement const&, Content co{return content.visit([](NonnullRefPtr<PresentableSurface> const& surface) {- return surface->iosurface() != nullptr;+ return surface->window_server_surface() != nullptr;},[](NonnullRefPtr<Media::VideoFrame> const& frame) {// A frame that a decoder left in memory has no surface that the window server could read.@@ -224,16 +253,28 @@ static CGRect unit_rect_of_surface_part(CALayer* layer, Gfx::IntSize part_size,: CGRectMake(0, 1 - unit_height, unit_width, unit_height);}+static Core::IOSurfaceHandle const& iosurface_of(PresentableSurface const& surface)+{+ auto const* window_server_surface = surface.window_server_surface();+ VERIFY(window_server_surface);+ return static_cast<CoreAnimationSurface const*>(window_server_surface)->iosurface();+}+void CoreAnimationRemoteLayer::show_frame(Frame frame){- auto const* buffer = frame.store;+ Core::IOSurfaceHandle const* buffer = nullptr;+ if (frame.store.has_value()) {+ auto& store = frame.store->surface();+ buffer = &iosurface_of(store);+ store.did_hand_to_window_server();+ }auto painted_size = frame.painted_size;auto device_pixel_ratio = frame.device_pixel_ratio;auto fit_within = frame.fit_within;auto& sublayers = frame.sublayers;Gfx::IntSize surface_size;if (buffer) {- auto const& iosurface = buffer->iosurface_handle();+ auto const& iosurface = *buffer;surface_size = { static_cast<int>(iosurface.width()), static_cast<int>(iosurface.height()) };// A store can be larger than the frame painted into it, as while the window is resized.painted_size = { min(painted_size.width(), surface_size.width()), min(painted_size.height(), surface_size.height()) };@@ -253,7 +294,7 @@ void CoreAnimationRemoteLayer::show_frame(Frame frame)}lay_out_frame(fit_within);if (buffer)- content_layer.contents = (__bridge id)buffer->iosurface_handle().core_foundation_pointer();+ content_layer.contents = (__bridge id)buffer->core_foundation_pointer();show_sublayers(sublayers);});release_video_frames_window_server_let_go_of();@@ -580,12 +621,11 @@ void CoreAnimationRemoteLayer::show_content(ShownSublayer& shown_sublayer, Remotif (shown_lease && &shown_lease->surface() == &lease.surface())return;auto& surface = lease.surface();- auto const* iosurface = surface.iosurface();- VERIFY(iosurface);+ auto const& iosurface = iosurface_of(surface);surface.did_hand_to_window_server();content_layer.contentsRect = CGRectMake(0, 0, 1, 1);content_layer.opaque = NO;- content_layer.contents = (__bridge id)iosurface->core_foundation_pointer();+ content_layer.contents = (__bridge id)iosurface.core_foundation_pointer();shown_sublayer.content = move(lease);});}diff --git a/Services/Compositor/OpenGLContext.cpp b/Services/Compositor/OpenGLContext.cppindex c73a517b85e..462e5b3c8cc 100644--- a/Services/Compositor/OpenGLContext.cpp+++ b/Services/Compositor/OpenGLContext.cpp@@ -33,6 +33,9 @@ extern "C" {#include <Compositor/OpenGLContext.h>#include <Compositor/SkiaBackendContext.h>#include <Compositor/SkiaUtils.h>+#ifdef AK_OS_MACOS+# include <Compositor/CoreAnimationWindowServer.h>+#endif#include <core/SkCanvas.h>#include <core/SkImage.h>@@ -441,7 +444,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);+ color_buffer.presentable_surface = PresentableSurface::create(*color_buffer.painting_surface, origin, make<CoreAnimationSurface>(*color_buffer.shared_image_buffer));EGLint const surface_attributes[] = {EGL_WIDTH,diff --git a/Services/Compositor/PresentableSurface.cpp b/Services/Compositor/PresentableSurface.cppindex 84b114080fb..ddd4604edb3 100644--- a/Services/Compositor/PresentableSurface.cpp+++ b/Services/Compositor/PresentableSurface.cpp@@ -6,13 +6,20 @@#include <Compositor/PresentableSurface.h>#include <Compositor/SkiaBackendContext.h>-#include <LibGfx/SharedImageBuffer.h>+#include <Compositor/WindowServer.h>+#include <LibGfx/Bitmap.h>namespace Compositor {-PresentableSurface::PresentableSurface(NonnullRefPtr<Gfx::PaintingSurface> painting_surface, Gfx::PaintingSurface::Origin origin)+NonnullRefPtr<PresentableSurface> PresentableSurface::create(NonnullRefPtr<Gfx::PaintingSurface> painting_surface, Gfx::PaintingSurface::Origin origin, OwnPtr<WindowServerSurface> window_server_surface)+{+ return adopt_ref(*new PresentableSurface(move(painting_surface), origin, move(window_server_surface)));+}++PresentableSurface::PresentableSurface(NonnullRefPtr<Gfx::PaintingSurface> painting_surface, Gfx::PaintingSurface::Origin origin, OwnPtr<WindowServerSurface> window_server_surface): m_painting_surface(move(painting_surface)), m_origin(origin)+ , m_window_server_surface(move(window_server_surface)){}@@ -73,37 +80,19 @@ void PresentableSurface::Lease::release()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+ return m_was_handed_to_window_server && m_window_server_surface && m_window_server_surface->may_be_read_by_window_server();}ErrorOr<NonnullRefPtr<PresentableSurface>> PresentableSurfacePool::create_surface(Gfx::IntSize size, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){-#ifdef AK_OS_MACOSif (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;+ if (auto surface = TRY(WindowServer::try_create_presentable_surface_for_sublayers(size, *skia_backend_context)))+ return surface.release_nonnull();}-#endifauto 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));+ return PresentableSurface::create(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)diff --git a/Services/Compositor/PresentableSurface.h b/Services/Compositor/PresentableSurface.hindex 1f56714b76e..8f4a0a27007 100644--- a/Services/Compositor/PresentableSurface.h+++ b/Services/Compositor/PresentableSurface.h@@ -13,16 +13,22 @@#include <AK/RefCounted.h>#include <AK/Vector.h>#include <AK/Weakable.h>+#include <AK/kmalloc.h>#include <Compositor/PaintingSurface.h>#include <LibGfx/Forward.h>#include <LibGfx/Size.h>-#ifdef AK_OS_MACOS-# include <LibCore/IOSurface.h>-#endif-namespace Compositor {+class WindowServerSurface {+public:+ AK_ALLOC_WITH_KMALLOC;++ virtual ~WindowServerSurface() = default;++ virtual bool may_be_read_by_window_server() const = 0;+};+// 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.@@ -53,20 +59,15 @@ public: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+ static NonnullRefPtr<PresentableSurface> create(NonnullRefPtr<Gfx::PaintingSurface>, Gfx::PaintingSurface::Origin, OwnPtr<WindowServerSurface> = {});~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+ WindowServerSurface const* window_server_surface() const { return m_window_server_surface.ptr(); }// 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; }@@ -77,16 +78,13 @@ public:private:friend class PresentableSurfacePool;- PresentableSurface(NonnullRefPtr<Gfx::PaintingSurface>, Gfx::PaintingSurface::Origin);+ PresentableSurface(NonnullRefPtr<Gfx::PaintingSurface>, Gfx::PaintingSurface::Origin, OwnPtr<WindowServerSurface>);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+ OwnPtr<WindowServerSurface> m_window_server_surface;};// The surfaces that one piece of content, such as a canvas, is presented from in turn. The content goes into itsdiff --git a/Services/Compositor/RemoteLayer.h b/Services/Compositor/RemoteLayer.hindex dc353bd9a76..60d0dea2714 100644--- a/Services/Compositor/RemoteLayer.h+++ b/Services/Compositor/RemoteLayer.h@@ -40,7 +40,7 @@ public:struct Frame {// Without a store, the frame shown before stays, and only the sublayers change.- Gfx::SharedImageBuffer const* store { nullptr };+ Optional<PresentableSurface::Lease> store;Gfx::IntSize painted_size;double device_pixel_ratio { 1.0 };Optional<Gfx::IntSize> fit_within;diff --git a/Services/Compositor/WindowServer.cpp b/Services/Compositor/WindowServer.cppindex 5e6527901a1..9d0532fc7a7 100644--- a/Services/Compositor/WindowServer.cpp+++ b/Services/Compositor/WindowServer.cpp@@ -21,6 +21,15 @@ RefPtr<WindowServer> WindowServer::try_create_for_presentation()#endif}+ErrorOr<RefPtr<PresentableSurface>> WindowServer::try_create_presentable_surface_for_sublayers([[maybe_unused]] Gfx::IntSize size, [[maybe_unused]] Gfx::SkiaBackendContext& skia_backend_context)+{+#ifdef AK_OS_MACOS+ return RefPtr<PresentableSurface> { TRY(CoreAnimationWindowServer::try_create_presentable_surface_over_new_iosurface(size, skia_backend_context)) };+#else+ return nullptr;+#endif+}+WindowServer::FrameHandOff WindowServer::frame_hand_off_through_presentation(){#ifdef AK_OS_MACOSdiff --git a/Services/Compositor/WindowServer.h b/Services/Compositor/WindowServer.hindex 78dd4efbbfd..35e7bf93611 100644--- a/Services/Compositor/WindowServer.h+++ b/Services/Compositor/WindowServer.h@@ -6,14 +6,18 @@#pragma once+#include <AK/Error.h>#include <AK/Noncopyable.h>+#include <AK/NonnullRefPtr.h>#include <AK/OwnPtr.h>#include <AK/RefCounted.h>#include <AK/RefPtr.h>#include <AK/Types.h>#include <AK/Weakable.h>#include <AK/kmalloc.h>+#include <Compositor/PresentableSurface.h>#include <Compositor/RemoteLayer.h>+#include <LibGfx/Size.h>namespace Compositor {@@ -32,6 +36,9 @@ public:virtual ~WindowServer() = default;virtual OwnPtr<RemoteLayer> try_create_remote_layer(u64 remote_layer_id) = 0;+ virtual ErrorOr<NonnullRefPtr<PresentableSurface>> try_create_presentable_surface(Gfx::IntSize, RefPtr<Gfx::SkiaBackendContext> const&) = 0;+ // None where no window server shows surfaces above the frames.+ static ErrorOr<RefPtr<PresentableSurface>> try_create_presentable_surface_for_sublayers(Gfx::IntSize, Gfx::SkiaBackendContext&);enum class FrameHandOff : u8 {OnceGpuWorkIsScheduled,