Skip to content

Truly independent web browser

LibGfx+Compositor: Present frames once their GPU work is scheduled

Aliaksandr Kalenikbfc4567dccdd
diff --git a/Libraries/LibGfx/MetalContext.h b/Libraries/LibGfx/MetalContext.h
index 576f7f5755e..153461e9083 100644
--- a/Libraries/LibGfx/MetalContext.h
+++ b/Libraries/LibGfx/MetalContext.h
@@ -41,6 +41,8 @@ public:
virtual void const* queue() const = 0;
virtual OwnPtr<MetalTexture> create_texture_from_iosurface(Core::IOSurfaceHandle const&, MetalTextureFormat, size_t plane) = 0;
+ // Calls back on a Metal thread.
+ virtual void when_committed_work_is_scheduled(Function<void()>&&) = 0;
virtual ~MetalContext() { }
};
diff --git a/Libraries/LibGfx/MetalContext.mm b/Libraries/LibGfx/MetalContext.mm
index 9f0b87ba866..07134a24be8 100644
--- a/Libraries/LibGfx/MetalContext.mm
+++ b/Libraries/LibGfx/MetalContext.mm
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Function.h>
#include <AK/OwnPtr.h>
#include <LibGfx/MetalContext.h>
@@ -79,6 +80,20 @@ public:
return make<MetalTextureImpl>(texture);
}
+ void when_committed_work_is_scheduled(Function<void()>&& callback) override
+ {
+ // The queue schedules its command buffers in the order they were committed.
+ @autoreleasepool {
+ auto* callback_to_invoke = new Function<void()>(move(callback));
+ id<MTLCommandBuffer> command_buffer = [m_queue commandBuffer];
+ [command_buffer addScheduledHandler:^(id<MTLCommandBuffer>) {
+ (*callback_to_invoke)();
+ delete callback_to_invoke;
+ }];
+ [command_buffer commit];
+ }
+ }
+
virtual ~MetalContextImpl() override
{
[m_queue release];
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index 04924808f3c..a4b9eaa699e 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -987,6 +987,22 @@ void CompositorState::present_frame(Web::CompositorContextId context_id, Context
});
});
context.presenter().did_submit_prepared_frame(pending_frame.viewport_rect);
+#ifdef AK_OS_MACOS
+ // The window server's reads of an IOSurface wait for GPU work that is already scheduled to write it. Handing the
+ // frame to the client before that work finishes keeps it ahead of the window server's deadline for the next refresh.
+ if (context.presents_to_client() && m_skia_backend_context) {
+ m_skia_backend_context->metal_context().when_committed_work_is_scheduled([self = NonnullRefPtr { *this }, &event_loop, pending_present]() mutable {
+ event_loop.deferred_invoke([self = move(self), pending_present] {
+ for (auto& entry : self->m_pending_async_presents) {
+ if (&entry == pending_present) {
+ self->deliver_rendered_frame(entry);
+ return;
+ }
+ }
+ });
+ });
+ }
+#endif
schedule_gpu_completion_check();
}
@@ -1195,26 +1211,27 @@ void CompositorState::did_finish_async_present(PendingAsyncPresent& pending_pres
}
VERIFY(pending_present_iterator != m_pending_async_presents.end());
- auto context_id = pending_present.context_id;
- auto viewport_rect = pending_present.viewport_rect;
- auto damage_rect = pending_present.damage_rect;
- auto bitmap_id = pending_present.bitmap_id;
- auto device_pixel_ratio = pending_present.device_pixel_ratio;
- auto was_cancelled = pending_present.was_cancelled;
+ auto finished_present = pending_present;
(void)m_pending_async_presents.remove(pending_present_iterator);
if (m_pending_async_presents.is_empty() && m_gpu_completion_timer)
m_gpu_completion_timer->stop();
- if (was_cancelled)
+ deliver_rendered_frame(finished_present);
+}
+
+void CompositorState::deliver_rendered_frame(PendingAsyncPresent& pending_present)
+{
+ if (pending_present.was_cancelled || exchange(pending_present.was_delivered, true))
return;
+ auto context_id = pending_present.context_id;
auto* context = context_if_present(context_id);
VERIFY(context);
- context->presenter().did_finish_gpu_present(bitmap_id);
+ context->presenter().did_finish_gpu_present(pending_present.bitmap_id);
if (context->presents_to_client()) {
VERIFY(m_client);
- m_client->did_present_frame(context_id, viewport_rect, damage_rect, bitmap_id, device_pixel_ratio);
+ m_client->did_present_frame(context_id, pending_present.viewport_rect, pending_present.damage_rect, pending_present.bitmap_id, pending_present.device_pixel_ratio);
}
resize_backing_stores_if_needed(context_id, *context);
if (auto parent_context_id = context->parent_context_id(); parent_context_id.has_value()) {
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index 7a3beba8e13..64b8a25616b 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -165,6 +165,7 @@ private:
i32 bitmap_id { 0 };
double device_pixel_ratio { 1.0 };
bool was_cancelled { false };
+ bool was_delivered { false };
};
ContextState* context_if_present(Web::CompositorContextId);
@@ -252,6 +253,7 @@ private:
void publish_backing_stores(Web::CompositorContextId, ContextState&, BackingStoreManager::Publication&&);
BackingStoreManager::GpuSharing gpu_sharing_for_client() const;
void did_finish_async_present(PendingAsyncPresent&);
+ void deliver_rendered_frame(PendingAsyncPresent&);
void cancel_pending_async_presents_for_context(Web::CompositorContextId);
void schedule_gpu_completion_check();
void check_gpu_completions();