Skip to content

Truly independent web browser

Compositor: Show canvases above the frame on Windows

Aliaksandr Kalenike4b578c2479b
diff --git a/Services/Compositor/DirectCompositionWindowServer.cpp b/Services/Compositor/DirectCompositionWindowServer.cpp
index fb8b88c8093..c869790c4e7 100644
--- a/Services/Compositor/DirectCompositionWindowServer.cpp
+++ b/Services/Compositor/DirectCompositionWindowServer.cpp
@@ -6,14 +6,18 @@
#include <AK/Array.h>
#include <AK/Error.h>
+#include <AK/HashMap.h>
+#include <AK/HashTable.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <Compositor/DirectCompositionWindowServer.h>
#include <Compositor/PaintingSurface.h>
#include <Compositor/SkiaBackendContext.h>
#include <LibCompositing/FrameFitting.h>
+#include <LibGfx/AffineTransform.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Direct3DContext.h>
+#include <LibGfx/Rect.h>
#include <LibThreading/Thread.h>
#include <core/SkCanvas.h>
@@ -308,7 +312,7 @@ public:
virtual ~DirectCompositionRemoteLayer() override;
virtual Compositing::RemoteLayerHostHandle host_handle() const override;
- virtual bool can_show(WindowServerPlacement const&, Content const&, double) const override { return false; }
+ virtual bool can_show(WindowServerPlacement const&, Content const&, double device_pixel_ratio) const override;
virtual void show_frame(Frame) override;
virtual void place_frame(Optional<Gfx::IntSize> fit_within) override;
@@ -321,8 +325,21 @@ private:
{
}
+ struct ShownSublayer {
+ ComPtr<IDCompositionVisual2> clip_visual;
+ ComPtr<IDCompositionRectangleClip> clip;
+ ComPtr<IDCompositionVisual2> rounded_clip_visual;
+ ComPtr<IDCompositionRectangleClip> rounded_clip;
+ ComPtr<IDCompositionVisual2> content_visual;
+ OwnPtr<CompositionSwapChain> swap_chain;
+ Optional<PresentableSurface::Lease> content;
+ };
+
DirectCompositionWindowServer::Impl& window_server_impl() const { return *m_window_server->m_impl; }
+ ErrorOr<OwnPtr<CompositionSwapChain>> create_swap_chain(Gfx::IntSize, DXGI_ALPHA_MODE) const;
void lay_out_frame();
+ void show_sublayers(Vector<RemoteSublayer>&);
+ ErrorOr<void> update_shown_sublayer(ShownSublayer&, RemoteSublayer&);
void show_changes_unless_held();
NonnullRefPtr<DirectCompositionWindowServer> m_window_server;
@@ -331,6 +348,7 @@ private:
ComPtr<IDCompositionVisual2> m_root_visual;
ComPtr<IDCompositionVisual2> m_frame_visual;
OwnPtr<CompositionSwapChain> m_frame_swap_chain;
+ HashMap<u64, ShownSublayer> m_shown_sublayers;
Gfx::IntSize m_painted_size;
Optional<Gfx::IntSize> m_fit_within;
Gfx::IntSize m_window_size;
@@ -369,6 +387,7 @@ DirectCompositionRemoteLayer::~DirectCompositionRemoteLayer()
if (m_target)
m_target->SetRoot(nullptr);
m_target = nullptr;
+ m_shown_sublayers.clear();
m_frame_visual = nullptr;
m_root_visual = nullptr;
m_frame_swap_chain = nullptr;
@@ -382,6 +401,22 @@ Compositing::RemoteLayerHostHandle DirectCompositionRemoteLayer::host_handle() c
return { .kind = Compositing::RemoteLayerHostKind::Win32ChildWindow, .id = static_cast<u64>(reinterpret_cast<uintptr_t>(m_window)) };
}
+bool DirectCompositionRemoteLayer::can_show(WindowServerPlacement const&, Content const& content, double) const
+{
+ return content.visit(
+ [&](NonnullRefPtr<PresentableSurface> const& surface) {
+ return surface->window_server_surface() != nullptr && surface->painting_surface().skia_backend_context() == window_server_impl().skia_backend_context.ptr();
+ },
+ [](NonnullRefPtr<Media::VideoFrame> const&) {
+ return false;
+ });
+}
+
+ErrorOr<OwnPtr<CompositionSwapChain>> DirectCompositionRemoteLayer::create_swap_chain(Gfx::IntSize size, DXGI_ALPHA_MODE alpha_mode) const
+{
+ return TRY(CompositionSwapChain::create(window_server_impl().skia_backend_context, *window_server_impl().dxgi_factory.Get(), size, alpha_mode));
+}
+
void DirectCompositionRemoteLayer::show_frame(Frame frame)
{
if (frame.store.has_value()) {
@@ -390,7 +425,7 @@ void DirectCompositionRemoteLayer::show_frame(Frame frame)
if (painted_size.is_empty())
return;
if (!m_frame_swap_chain) {
- auto swap_chain = CompositionSwapChain::create(window_server_impl().skia_backend_context, *window_server_impl().dxgi_factory.Get(), painted_size, DXGI_ALPHA_MODE_IGNORE);
+ auto swap_chain = create_swap_chain(painted_size, DXGI_ALPHA_MODE_IGNORE);
if (swap_chain.is_error()) {
dbgln("Compositor: Unable to create the swap chain of a remote layer: {}", swap_chain.error());
return;
@@ -408,9 +443,136 @@ void DirectCompositionRemoteLayer::show_frame(Frame frame)
return;
m_fit_within = frame.fit_within;
lay_out_frame();
+ show_sublayers(frame.sublayers);
show_changes_unless_held();
}
+void DirectCompositionRemoteLayer::show_sublayers(Vector<RemoteSublayer>& sublayers)
+{
+ auto& device = *window_server_impl().device.Get();
+ HashTable<u64> shown_identities;
+ m_frame_visual->RemoveAllVisuals();
+ IDCompositionVisual2* visual_below = nullptr;
+ for (auto& sublayer : sublayers) {
+ auto& shown_sublayer = m_shown_sublayers.ensure(sublayer.identity, [] { return ShownSublayer {}; });
+ if (!shown_sublayer.clip_visual) {
+ if (FAILED(device.CreateVisual(&shown_sublayer.clip_visual)) || FAILED(device.CreateRectangleClip(&shown_sublayer.clip))
+ || FAILED(device.CreateVisual(&shown_sublayer.content_visual))) {
+ dbgln("Compositor: Unable to create the visuals of a sublayer");
+ m_shown_sublayers.remove(sublayer.identity);
+ continue;
+ }
+ shown_sublayer.clip_visual->SetClip(shown_sublayer.clip.Get());
+ shown_sublayer.clip_visual->AddVisual(shown_sublayer.content_visual.Get(), FALSE, nullptr);
+ }
+ if (auto result = update_shown_sublayer(shown_sublayer, sublayer); result.is_error())
+ dbgln("Compositor: Unable to show a sublayer: {}", result.error());
+ m_frame_visual->AddVisual(shown_sublayer.clip_visual.Get(), TRUE, visual_below);
+ visual_below = shown_sublayer.clip_visual.Get();
+ shown_identities.set(sublayer.identity);
+ }
+ m_shown_sublayers.remove_all_matching([&](u64 identity, auto&) { return !shown_identities.contains(identity); });
+}
+
+static D2D_MATRIX_3X2_F to_direct2d_matrix(Gfx::AffineTransform const& transform)
+{
+ D2D_MATRIX_3X2_F matrix {};
+ matrix._11 = transform.a();
+ matrix._12 = transform.b();
+ matrix._21 = transform.c();
+ matrix._22 = transform.d();
+ matrix._31 = transform.e();
+ matrix._32 = transform.f();
+ return matrix;
+}
+
+static void set_rectangle(IDCompositionRectangleClip& clip, Gfx::FloatRect rect)
+{
+ clip.SetLeft(rect.left());
+ clip.SetTop(rect.top());
+ clip.SetRight(rect.right());
+ clip.SetBottom(rect.bottom());
+}
+
+static DCOMPOSITION_BITMAP_INTERPOLATION_MODE interpolation_mode_for_scaling_mode(Gfx::ScalingMode scaling_mode)
+{
+ switch (scaling_mode) {
+ case Gfx::ScalingMode::None:
+ case Gfx::ScalingMode::NearestNeighbor:
+ return DCOMPOSITION_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
+ case Gfx::ScalingMode::Bilinear:
+ case Gfx::ScalingMode::BilinearMipmap:
+ return DCOMPOSITION_BITMAP_INTERPOLATION_MODE_LINEAR;
+ }
+ VERIFY_NOT_REACHED();
+}
+
+ErrorOr<void> DirectCompositionRemoteLayer::update_shown_sublayer(ShownSublayer& shown_sublayer, RemoteSublayer& sublayer)
+{
+ auto& device = *window_server_impl().device.Get();
+ set_rectangle(*shown_sublayer.clip.Get(), sublayer.clip_rect);
+
+ if (sublayer.rounded_clip.has_value()) {
+ auto const& rounded_clip = *sublayer.rounded_clip;
+ if (!shown_sublayer.rounded_clip_visual) {
+ if (auto result = device.CreateVisual(&shown_sublayer.rounded_clip_visual); FAILED(result))
+ return Error::from_windows_error(result);
+ if (auto result = device.CreateRectangleClip(&shown_sublayer.rounded_clip); FAILED(result))
+ return Error::from_windows_error(result);
+ shown_sublayer.rounded_clip_visual->SetClip(shown_sublayer.rounded_clip.Get());
+ shown_sublayer.clip_visual->RemoveAllVisuals();
+ shown_sublayer.rounded_clip_visual->AddVisual(shown_sublayer.content_visual.Get(), FALSE, nullptr);
+ shown_sublayer.clip_visual->AddVisual(shown_sublayer.rounded_clip_visual.Get(), FALSE, nullptr);
+ }
+ auto& clip = *shown_sublayer.rounded_clip.Get();
+ set_rectangle(clip, rounded_clip.rect);
+ auto radius_if = [&](bool rounds) { return rounds ? rounded_clip.radius : 0.0f; };
+ clip.SetTopLeftRadiusX(radius_if(rounded_clip.rounds_top_left));
+ clip.SetTopLeftRadiusY(radius_if(rounded_clip.rounds_top_left));
+ clip.SetTopRightRadiusX(radius_if(rounded_clip.rounds_top_right));
+ clip.SetTopRightRadiusY(radius_if(rounded_clip.rounds_top_right));
+ clip.SetBottomRightRadiusX(radius_if(rounded_clip.rounds_bottom_right));
+ clip.SetBottomRightRadiusY(radius_if(rounded_clip.rounds_bottom_right));
+ clip.SetBottomLeftRadiusX(radius_if(rounded_clip.rounds_bottom_left));
+ clip.SetBottomLeftRadiusY(radius_if(rounded_clip.rounds_bottom_left));
+ } else if (shown_sublayer.rounded_clip_visual) {
+ shown_sublayer.clip_visual->RemoveAllVisuals();
+ shown_sublayer.rounded_clip_visual->RemoveAllVisuals();
+ shown_sublayer.clip_visual->AddVisual(shown_sublayer.content_visual.Get(), FALSE, nullptr);
+ shown_sublayer.rounded_clip_visual = nullptr;
+ shown_sublayer.rounded_clip = nullptr;
+ }
+
+ auto* lease = sublayer.content.get_pointer<PresentableSurface::Lease>();
+ if (!lease)
+ return Error::from_string_literal("Only surfaces can be shown as sublayers");
+ auto& surface = lease->surface();
+ auto surface_size = surface.size();
+ if (surface_size.is_empty())
+ return {};
+
+ auto content_rect = sublayer.content_rect;
+ auto transform = sublayer.content_to_frame;
+ transform.translate(content_rect.x(), content_rect.y());
+ transform.scale(static_cast<float>(content_rect.width()) / surface_size.width(), static_cast<float>(content_rect.height()) / surface_size.height());
+ auto& content_visual = *shown_sublayer.content_visual.Get();
+ content_visual.SetTransform(to_direct2d_matrix(transform));
+ content_visual.SetClip(D2D_RECT_F { 0, 0, static_cast<float>(surface_size.width()), static_cast<float>(surface_size.height()) });
+ content_visual.SetBitmapInterpolationMode(interpolation_mode_for_scaling_mode(sublayer.scaling_mode));
+ content_visual.SetBorderMode(sublayer.content_to_frame.is_identity_or_translation_or_scale() ? DCOMPOSITION_BORDER_MODE_HARD : DCOMPOSITION_BORDER_MODE_SOFT);
+
+ if (shown_sublayer.content.has_value() && &shown_sublayer.content->surface() == &surface)
+ return {};
+ if (!shown_sublayer.swap_chain) {
+ shown_sublayer.swap_chain = TRY(create_swap_chain(surface_size, DXGI_ALPHA_MODE_PREMULTIPLIED));
+ content_visual.SetContent(shown_sublayer.swap_chain->content());
+ }
+ TRY(shown_sublayer.swap_chain->draw(surface.painting_surface(), surface_size));
+ shown_sublayer.content = move(*lease);
+ m_has_changes_to_show = true;
+ return {};
+}
+
void DirectCompositionRemoteLayer::place_frame(Optional<Gfx::IntSize> fit_within)
{
m_fit_within = fit_within;
@@ -460,6 +622,10 @@ void DirectCompositionRemoteLayer::show_changes()
}
if (m_frame_swap_chain)
m_frame_swap_chain->show_drawing();
+ for (auto& [identity, shown_sublayer] : m_shown_sublayers) {
+ if (shown_sublayer.swap_chain)
+ shown_sublayer.swap_chain->show_drawing();
+ }
}
RefPtr<DirectCompositionWindowServer> DirectCompositionWindowServer::try_create()
@@ -499,12 +665,22 @@ OwnPtr<RemoteLayer> DirectCompositionWindowServer::try_create_remote_layer(u64)
return DirectCompositionRemoteLayer::try_create(*this);
}
-ErrorOr<NonnullRefPtr<PresentableSurface>> DirectCompositionWindowServer::try_create_presentable_surface(Gfx::IntSize size, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)
+static ErrorOr<NonnullRefPtr<PresentableSurface>> create_presentable_surface(Gfx::IntSize size, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)
{
auto painting_surface = TRY(Gfx::PaintingSurface::try_create_with_size(size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context));
return PresentableSurface::create(move(painting_surface), Gfx::PaintingSurface::Origin::TopLeft, make<DirectCompositionSurface>());
}
+ErrorOr<NonnullRefPtr<PresentableSurface>> DirectCompositionWindowServer::try_create_presentable_surface_on_gpu(Gfx::IntSize size, Gfx::SkiaBackendContext& skia_backend_context)
+{
+ return create_presentable_surface(size, skia_backend_context);
+}
+
+ErrorOr<NonnullRefPtr<PresentableSurface>> DirectCompositionWindowServer::try_create_presentable_surface(Gfx::IntSize size, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)
+{
+ return create_presentable_surface(size, skia_backend_context);
+}
+
void DirectCompositionWindowServer::hold_changes_on_screen()
{
m_holds_changes_off_screen = true;
diff --git a/Services/Compositor/DirectCompositionWindowServer.h b/Services/Compositor/DirectCompositionWindowServer.h
index 6b7aaa963dd..4cba4650e8c 100644
--- a/Services/Compositor/DirectCompositionWindowServer.h
+++ b/Services/Compositor/DirectCompositionWindowServer.h
@@ -29,6 +29,7 @@ public:
class DirectCompositionWindowServer final : public WindowServer {
public:
static RefPtr<DirectCompositionWindowServer> try_create();
+ static ErrorOr<NonnullRefPtr<PresentableSurface>> try_create_presentable_surface_on_gpu(Gfx::IntSize, Gfx::SkiaBackendContext&);
virtual ~DirectCompositionWindowServer() override;
virtual OwnPtr<RemoteLayer> try_create_remote_layer(u64 remote_layer_id) override;
diff --git a/Services/Compositor/OpenGLContext.cpp b/Services/Compositor/OpenGLContext.cpp
index 462e5b3c8cc..44bdba2c54e 100644
--- a/Services/Compositor/OpenGLContext.cpp
+++ b/Services/Compositor/OpenGLContext.cpp
@@ -35,6 +35,8 @@ extern "C" {
#include <Compositor/SkiaUtils.h>
#ifdef AK_OS_MACOS
# include <Compositor/CoreAnimationWindowServer.h>
+#elif defined(USE_DIRECTX)
+# include <Compositor/DirectCompositionWindowServer.h>
#endif
#include <core/SkCanvas.h>
@@ -61,6 +63,8 @@ struct OpenGLContext::ColorBuffer {
RefPtr<Gfx::PaintingSurface> painting_surface;
#ifdef AK_OS_MACOS
OwnPtr<Gfx::SharedImageBuffer> shared_image_buffer;
+#endif
+#if defined(AK_OS_MACOS) || defined(USE_DIRECTX)
RefPtr<PresentableSurface> presentable_surface;
#endif
EGLSurface surface { EGL_NO_SURFACE };
@@ -175,6 +179,8 @@ void OpenGLContext::free_color_buffer([[maybe_unused]] ColorBuffer& color_buffer
color_buffer.painting_surface = nullptr;
# ifdef AK_OS_MACOS
color_buffer.shared_image_buffer = nullptr;
+# endif
+# if defined(AK_OS_MACOS) || defined(USE_DIRECTX)
color_buffer.presentable_surface = nullptr;
# endif
#endif
@@ -525,6 +531,7 @@ bool OpenGLContext::allocate_d3d_color_buffer(ColorBuffer& color_buffer, Gfx::In
return false;
}
color_buffer.painting_surface = painting_surface.release_value();
+ color_buffer.presentable_surface = PresentableSurface::create(*color_buffer.painting_surface, origin, make<DirectCompositionSurface>());
create_texture_from_egl_image(color_buffer);
return true;
@@ -841,7 +848,7 @@ size_t OpenGLContext::next_color_buffer_index_after(size_t presented_index) cons
{
auto& color_buffers = m_impl->color_buffers;
auto is_busy = [&](size_t index) {
-# ifdef AK_OS_MACOS
+# if defined(AK_OS_MACOS) || defined(USE_DIRECTX)
auto const& presentable_surface = color_buffers[index].presentable_surface;
return presentable_surface && presentable_surface->is_busy();
# else
@@ -1004,7 +1011,7 @@ RefPtr<Gfx::PaintingSurface> OpenGLContext::presented_surface()
RefPtr<PresentableSurface> OpenGLContext::presented_presentable_surface()
{
-#ifdef AK_OS_MACOS
+#if defined(AK_OS_MACOS) || defined(USE_DIRECTX)
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
diff --git a/Services/Compositor/WindowServer.cpp b/Services/Compositor/WindowServer.cpp
index abbbcfe70fa..c226d2852f5 100644
--- a/Services/Compositor/WindowServer.cpp
+++ b/Services/Compositor/WindowServer.cpp
@@ -29,6 +29,8 @@ ErrorOr<RefPtr<PresentableSurface>> WindowServer::try_create_presentable_surface
{
#ifdef AK_OS_MACOS
return RefPtr<PresentableSurface> { TRY(CoreAnimationWindowServer::try_create_presentable_surface_over_new_iosurface(size, skia_backend_context)) };
+#elif defined(AK_OS_WINDOWS) && defined(USE_DIRECTX)
+ return RefPtr<PresentableSurface> { TRY(DirectCompositionWindowServer::try_create_presentable_surface_on_gpu(size, skia_backend_context)) };
#else
return nullptr;
#endif