diff --git a/Libraries/LibGfx/Forward.h b/Libraries/LibGfx/Forward.h index 612a9720d6a..37e5998a7f6 100644 --- a/Libraries/LibGfx/Forward.h +++ b/Libraries/LibGfx/Forward.h @@ -28,6 +28,7 @@ class Palette; class PaletteImpl; class Path; class ShareableBitmap; +class SkiaBackendContext; struct SystemTheme; template diff --git a/Libraries/LibGfx/MetalContext.h b/Libraries/LibGfx/MetalContext.h index efe338b0c97..31ebac57bf1 100644 --- a/Libraries/LibGfx/MetalContext.h +++ b/Libraries/LibGfx/MetalContext.h @@ -11,6 +11,8 @@ static_assert(false, "This file must only be used for macOS"); #endif #include +#include +#include #include namespace Gfx { @@ -24,7 +26,7 @@ public: virtual ~MetalTexture() {}; }; -class MetalContext { +class MetalContext : public RefCounted { public: virtual void const* device() const = 0; virtual void const* queue() const = 0; @@ -34,6 +36,6 @@ public: virtual ~MetalContext() {}; }; -OwnPtr get_metal_context(); +RefPtr get_metal_context(); } diff --git a/Libraries/LibGfx/MetalContext.mm b/Libraries/LibGfx/MetalContext.mm index 225685b7109..6127d64d5c9 100644 --- a/Libraries/LibGfx/MetalContext.mm +++ b/Libraries/LibGfx/MetalContext.mm @@ -67,7 +67,7 @@ private: id m_queue; }; -OwnPtr get_metal_context() +RefPtr get_metal_context() { auto device = MTLCreateSystemDefaultDevice(); if (!device) { @@ -82,7 +82,7 @@ OwnPtr get_metal_context() return {}; } - return make(device, queue); + return adopt_ref(*new MetalContextImpl(device, queue)); } } diff --git a/Libraries/LibGfx/PaintingSurface.cpp b/Libraries/LibGfx/PaintingSurface.cpp index bd0d265adbc..c23b524678f 100644 --- a/Libraries/LibGfx/PaintingSurface.cpp +++ b/Libraries/LibGfx/PaintingSurface.cpp @@ -56,13 +56,14 @@ NonnullRefPtr PaintingSurface::wrap_bitmap(Bitmap& bitmap) } #ifdef AK_OS_MACOS -NonnullRefPtr PaintingSurface::wrap_metal_surface(Gfx::MetalTexture& metal_texture, RefPtr context) +NonnullRefPtr PaintingSurface::wrap_iosurface(Core::IOSurfaceHandle const& iosurface_handle, RefPtr context) { - IntSize const size { metal_texture.width(), metal_texture.height() }; + auto metal_texture = context->metal_context().create_texture_from_iosurface(iosurface_handle); + IntSize const size { metal_texture->width(), metal_texture->height() }; auto image_info = SkImageInfo::Make(size.width(), size.height(), kBGRA_8888_SkColorType, kPremul_SkAlphaType); GrMtlTextureInfo mtl_info; - mtl_info.fTexture = sk_ret_cfp(metal_texture.texture()); - auto backend_render_target = GrBackendRenderTargets::MakeMtl(metal_texture.width(), metal_texture.height(), mtl_info); + mtl_info.fTexture = sk_ret_cfp(metal_texture->texture()); + auto backend_render_target = GrBackendRenderTargets::MakeMtl(metal_texture->width(), metal_texture->height(), mtl_info); auto surface = SkSurfaces::WrapBackendRenderTarget(context->sk_context(), backend_render_target, kTopLeft_GrSurfaceOrigin, kBGRA_8888_SkColorType, nullptr, nullptr); return adopt_ref(*new PaintingSurface(make(size, surface, nullptr, context))); } diff --git a/Libraries/LibGfx/PaintingSurface.h b/Libraries/LibGfx/PaintingSurface.h index 9c489567850..ae25b5fb9c2 100644 --- a/Libraries/LibGfx/PaintingSurface.h +++ b/Libraries/LibGfx/PaintingSurface.h @@ -28,7 +28,7 @@ public: static NonnullRefPtr wrap_bitmap(Bitmap&); #ifdef AK_OS_MACOS - static NonnullRefPtr wrap_metal_surface(Gfx::MetalTexture&, RefPtr); + static NonnullRefPtr wrap_iosurface(Core::IOSurfaceHandle const&, RefPtr); #endif void read_into_bitmap(Bitmap&); diff --git a/Libraries/LibGfx/SkiaBackendContext.cpp b/Libraries/LibGfx/SkiaBackendContext.cpp index 1bb41469a52..776d3f37051 100644 --- a/Libraries/LibGfx/SkiaBackendContext.cpp +++ b/Libraries/LibGfx/SkiaBackendContext.cpp @@ -53,6 +53,8 @@ public: GrDirectContext* sk_context() const override { return m_context.get(); } + MetalContext& metal_context() override { VERIFY_NOT_REACHED(); } + private: sk_sp m_context; NonnullOwnPtr m_extensions; @@ -89,8 +91,9 @@ class SkiaMetalBackendContext final : public SkiaBackendContext { AK_MAKE_NONMOVABLE(SkiaMetalBackendContext); public: - SkiaMetalBackendContext(sk_sp context) + SkiaMetalBackendContext(sk_sp context, MetalContext& metal_context) : m_context(move(context)) + , m_metal_context(move(metal_context)) { } @@ -105,17 +108,20 @@ public: GrDirectContext* sk_context() const override { return m_context.get(); } + MetalContext& metal_context() override { return m_metal_context; } + private: sk_sp m_context; + NonnullRefPtr m_metal_context; }; -RefPtr SkiaBackendContext::create_metal_context(Gfx::MetalContext const& metal_context) +RefPtr SkiaBackendContext::create_metal_context(MetalContext& metal_context) { GrMtlBackendContext backend_context; - backend_context.fDevice.retain((GrMTLHandle)metal_context.device()); - backend_context.fQueue.retain((GrMTLHandle)metal_context.queue()); + backend_context.fDevice.retain(metal_context.device()); + backend_context.fQueue.retain(metal_context.queue()); sk_sp ctx = GrDirectContexts::MakeMetal(backend_context); - return adopt_ref(*new SkiaMetalBackendContext(ctx)); + return adopt_ref(*new SkiaMetalBackendContext(ctx, metal_context)); } #endif diff --git a/Libraries/LibGfx/SkiaBackendContext.h b/Libraries/LibGfx/SkiaBackendContext.h index 0a4b93c556d..e78af755838 100644 --- a/Libraries/LibGfx/SkiaBackendContext.h +++ b/Libraries/LibGfx/SkiaBackendContext.h @@ -22,6 +22,8 @@ class SkSurface; namespace Gfx { +class MetalContext; + class SkiaBackendContext : public RefCounted { AK_MAKE_NONCOPYABLE(SkiaBackendContext); AK_MAKE_NONMOVABLE(SkiaBackendContext); @@ -32,7 +34,7 @@ public: #endif #ifdef AK_OS_MACOS - static RefPtr create_metal_context(Gfx::MetalContext const&); + static RefPtr create_metal_context(MetalContext&); #endif SkiaBackendContext() {}; @@ -40,6 +42,8 @@ public: virtual void flush_and_submit(SkSurface*) {}; virtual GrDirectContext* sk_context() const = 0; + + virtual MetalContext& metal_context() = 0; }; } diff --git a/Libraries/LibWeb/HTML/TraversableNavigable.cpp b/Libraries/LibWeb/HTML/TraversableNavigable.cpp index 75b5c18322b..9dd5d2b9f70 100644 --- a/Libraries/LibWeb/HTML/TraversableNavigable.cpp +++ b/Libraries/LibWeb/HTML/TraversableNavigable.cpp @@ -1408,8 +1408,7 @@ void TraversableNavigable::paint(DevicePixelRect const& content_rect, Painting:: #ifdef AK_OS_MACOS if (m_metal_context && m_skia_backend_context && is(target)) { auto& iosurface_backing_store = static_cast(target); - auto texture = m_metal_context->create_texture_from_iosurface(iosurface_backing_store.iosurface_handle()); - auto painting_surface = Gfx::PaintingSurface::wrap_metal_surface(*texture, m_skia_backend_context); + auto painting_surface = Gfx::PaintingSurface::wrap_iosurface(iosurface_backing_store.iosurface_handle(), *m_skia_backend_context); Painting::DisplayListPlayerSkia player(*m_skia_backend_context, painting_surface); player.execute(*display_list); return; diff --git a/Libraries/LibWeb/HTML/TraversableNavigable.h b/Libraries/LibWeb/HTML/TraversableNavigable.h index 5df8ac385a9..bb00de11a7a 100644 --- a/Libraries/LibWeb/HTML/TraversableNavigable.h +++ b/Libraries/LibWeb/HTML/TraversableNavigable.h @@ -149,7 +149,7 @@ private: RefPtr m_skia_backend_context; #ifdef AK_OS_MACOS - OwnPtr m_metal_context; + RefPtr m_metal_context; #endif };