Truly independent web browser
Compositor: Draw IOSurface image frames without uploading a copy
Aliaksandr Kalenik2e48f4e28078
diff --git a/Libraries/LibGfx/MetalContext.h b/Libraries/LibGfx/MetalContext.hindex 153461e9083..28045ee8741 100644--- a/Libraries/LibGfx/MetalContext.h+++ b/Libraries/LibGfx/MetalContext.h@@ -29,6 +29,7 @@ public:// The component layout a texture reads a surface's plane through, which for a planar surface differs per plane.enum class MetalTextureFormat : u8 {BGRA8,+ RGBA8,R8,RG8,R16,diff --git a/Libraries/LibGfx/MetalContext.mm b/Libraries/LibGfx/MetalContext.mmindex 07134a24be8..c94f3322a96 100644--- a/Libraries/LibGfx/MetalContext.mm+++ b/Libraries/LibGfx/MetalContext.mm@@ -39,6 +39,8 @@ static MTLPixelFormat metal_pixel_format(MetalTextureFormat format)switch (format) {case MetalTextureFormat::BGRA8:return MTLPixelFormatBGRA8Unorm;+ case MetalTextureFormat::RGBA8:+ return MTLPixelFormatRGBA8Unorm;case MetalTextureFormat::R8:return MTLPixelFormatR8Unorm;case MetalTextureFormat::RG8:@@ -66,8 +68,14 @@ public:OwnPtr<MetalTexture> create_texture_from_iosurface(Core::IOSurfaceHandle const& iosurface, MetalTextureFormat format, size_t plane) override{+ auto pixel_format = metal_pixel_format(format);+ // NB: The surface may come from another process, which need not have aligned its rows as the GPU needs them.+ auto bytes_per_row = iosurface.plane_count() > 0 ? iosurface.bytes_per_row_of_plane(plane) : iosurface.bytes_per_row();+ if (bytes_per_row % [m_device minimumLinearTextureAlignmentForPixelFormat:pixel_format] != 0)+ return {};+auto* const descriptor = [[MTLTextureDescriptor alloc] init];- descriptor.pixelFormat = metal_pixel_format(format);+ descriptor.pixelFormat = pixel_format;descriptor.width = iosurface.plane_count() > 0 ? iosurface.plane_width(plane) : iosurface.width();descriptor.height = iosurface.plane_count() > 0 ? iosurface.plane_height(plane) : iosurface.height();descriptor.storageMode = MTLStorageModeShared;diff --git a/Services/Compositor/Canvas2DCommandPlayer.cpp b/Services/Compositor/Canvas2DCommandPlayer.cppindex fc887a0726d..a97914c7c54 100644--- a/Services/Compositor/Canvas2DCommandPlayer.cpp+++ b/Services/Compositor/Canvas2DCommandPlayer.cpp@@ -14,6 +14,7 @@#include <Compositor/Canvas2DCommandPlayer.h>#include <Compositor/FontSkia.h>#include <Compositor/PaintingSurface.h>+#include <Compositor/SkiaBackendContext.h>#include <Compositor/SkiaUtils.h>#include <LibCompositing/DisplayList/Canvas2DTape.h>#include <LibCompositing/DisplayList/DisplayListCommand.h>@@ -53,6 +54,15 @@ Canvas2DCommandPlayer::Canvas2DCommandPlayer(NonnullRefPtr<Gfx::PaintingSurface>Canvas2DCommandPlayer::~Canvas2DCommandPlayer() = default;+sk_sp<SkImage> Canvas2DCommandPlayer::image_of_frame(Gfx::DecodedImageFrame const& frame) const+{+ if (auto skia_backend_context = m_surface->skia_backend_context()) {+ if (auto image = Gfx::sk_texture_image_sharing_pixels_of_decoded_image_frame(frame, *skia_backend_context))+ return image;+ }+ return Gfx::sk_image_from_decoded_image_frame(frame);+}+NonnullRefPtr<Gfx::PaintingSurface> Canvas2DCommandPlayer::surface() const{return m_surface;@@ -178,7 +188,7 @@ Optional<SkPaint> Canvas2DCommandPlayer::paint_from(Canvas2DPaint const& style,auto const* frame = m_resolvers.image_frame(style.pattern_image_id.value());if (!frame)return {};- auto image = Gfx::sk_image_from_decoded_image_frame(*frame);+ auto image = image_of_frame(*frame);if (!image)break;@@ -461,7 +471,7 @@ void Canvas2DCommandPlayer::play_command(Canvas2DDrawImageFrame const& command)auto const* frame = m_resolvers.image_frame(command.frame_id.value());if (!frame)return;- auto image = Gfx::sk_image_from_decoded_image_frame(*frame);+ auto image = image_of_frame(*frame);if (!image)return;draw_image(*image, command.dst_rect, command.src_rect, command.scaling_mode, command.compositing);diff --git a/Services/Compositor/Canvas2DCommandPlayer.h b/Services/Compositor/Canvas2DCommandPlayer.hindex 145cbd98ca6..648ef578647 100644--- a/Services/Compositor/Canvas2DCommandPlayer.h+++ b/Services/Compositor/Canvas2DCommandPlayer.h@@ -70,6 +70,7 @@ private:#undef DECLARE_PLAY_COMMANDvoid clear_rect(Gfx::FloatRect const&, Gfx::Color);+ sk_sp<SkImage> image_of_frame(Gfx::DecodedImageFrame const&) const;void draw_image(SkImage const&, Gfx::FloatRect const& dst_rect, Gfx::FloatRect const& src_rect, Gfx::ScalingMode, Compositing::Canvas2DCompositing const&);ReadonlyBytes inline_data(Compositing::DisplayListDataSpan) const;diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cppindex 0d28a73ea4e..dec99be75a6 100644--- a/Services/Compositor/DisplayListRasterCache.cpp+++ b/Services/Compositor/DisplayListRasterCache.cpp@@ -65,7 +65,8 @@ struct DisplayListRasterCache::ImageFrameImage {{}- // A raster image reads the pixels of the frame, so the frame must live as long as the image.+ // A raster image, or a texture over the IOSurface of the frame, reads the pixels of the frame, so the frame must live+ // as long as the image.Gfx::DecodedImageFrame frame;RefPtr<Gfx::SkiaBackendContext> skia_backend_context;sk_sp<SkImage> image;@@ -224,6 +225,11 @@ sk_sp<SkImageFilter> DisplayListRasterCache::backdrop_image_filter(u64 tree_strustatic sk_sp<SkImage> create_skia_image(Gfx::DecodedImageFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){+ if (skia_backend_context) {+ if (auto image = Gfx::sk_texture_image_sharing_pixels_of_decoded_image_frame(frame, *skia_backend_context))+ return image;+ }+auto raster_image = Gfx::sk_image_from_decoded_image_frame(frame);auto* gr_context = skia_backend_context ? skia_backend_context->sk_context() : nullptr;if (!gr_context)diff --git a/Services/Compositor/SkiaUtils.cpp b/Services/Compositor/SkiaUtils.cppindex 8ef188dd8cc..b9dae18c38a 100644--- a/Services/Compositor/SkiaUtils.cpp+++ b/Services/Compositor/SkiaUtils.cpp@@ -5,8 +5,10 @@*/#include <AK/Assertions.h>+#include <AK/GenericShorthands.h>#include <AK/Math.h>#include <AK/MemoryStream.h>+#include <Compositor/SkiaBackendContext.h>#include <Compositor/SkiaUtils.h>#include <LibGfx/Bitmap.h>#include <LibGfx/ColorFilterType.h>@@ -28,6 +30,16 @@#include <effects/SkPerlinNoiseShader.h>#include <effects/SkRuntimeEffect.h>+#ifdef AK_OS_MACOS+# include <LibGfx/MetalContext.h>+# include <gpu/ganesh/GrBackendSurface.h>+# include <gpu/ganesh/GrDirectContext.h>+# include <gpu/ganesh/SkImageGanesh.h>+# include <gpu/ganesh/mtl/GrMtlBackendSurface.h>+# include <gpu/ganesh/mtl/GrMtlTypes.h>+# include <ports/SkCFObject.h>+#endif+namespace Gfx {static_assert(to_underlying(PathVerb::Move) == to_underlying(SkPathVerb::kMove));@@ -473,6 +485,30 @@ sk_sp<SkImage> sk_image_from_decoded_image_frame(DecodedImageFrame const& frame)return sk_image_borrowing_pixels(frame.size(), frame.format(), frame.alpha_type(), frame.color_space(), frame.pixels().data(), frame.pitch());}+sk_sp<SkImage> sk_texture_image_sharing_pixels_of_decoded_image_frame([[maybe_unused]] DecodedImageFrame const& frame, [[maybe_unused]] SkiaBackendContext& skia_backend_context)+{+#ifdef AK_OS_MACOS+ auto const* iosurface = frame.iosurface();+ auto* gr_context = skia_backend_context.sk_context();+ if (!iosurface || !gr_context)+ return nullptr;++ auto texture_format = first_is_one_of(frame.format(), BitmapFormat::RGBA8888, BitmapFormat::RGBx8888) ? MetalTextureFormat::RGBA8 : MetalTextureFormat::BGRA8;+ auto metal_texture = skia_backend_context.metal_context().create_texture_from_iosurface(*iosurface, texture_format, 0);+ if (!metal_texture)+ return nullptr;++ // NB: The backend texture retains the Metal texture, which retains the surface.+ GrMtlTextureInfo texture_info;+ texture_info.fTexture = sk_ret_cfp(metal_texture->texture());+ auto backend_texture = GrBackendTextures::MakeMtl(frame.width(), frame.height(), skgpu::Mipmapped::kNo, texture_info);+ return SkImages::BorrowTextureFrom(gr_context, backend_texture, kTopLeft_GrSurfaceOrigin,+ to_skia_color_type(frame.format()), to_skia_alpha_type(frame.format(), frame.alpha_type()), to_skia_color_space(frame.color_space()));+#else+ return nullptr;+#endif+}+sk_sp<SkImage> sk_image_adopting_bitmap(NonnullRefPtr<Bitmap> bitmap, ColorSpace const& color_space){auto info = SkImageInfo::Make(bitmap->width(), bitmap->height(), to_skia_color_type(bitmap->format()), to_skia_alpha_type(bitmap->format(), bitmap->alpha_type()), to_skia_color_space(color_space));diff --git a/Services/Compositor/SkiaUtils.h b/Services/Compositor/SkiaUtils.hindex d94f006ed33..7c9452a645f 100644--- a/Services/Compositor/SkiaUtils.h+++ b/Services/Compositor/SkiaUtils.h@@ -11,6 +11,7 @@#include <AK/Assertions.h>#include <AK/Function.h>#include <AK/Optional.h>+#include <Compositor/Forward.h>#include <LibGfx/AffineTransform.h>#include <LibGfx/Bitmap.h>#include <LibGfx/Color.h>@@ -159,6 +160,10 @@ sk_sp<SkColorSpace> to_skia_color_space(ColorSpace const&);sk_sp<SkImage> sk_image_from_bitmap(Bitmap const& bitmap, ColorSpace const& color_space);sk_sp<SkImage> sk_image_from_decoded_image_frame(DecodedImageFrame const&);+// Samples the IOSurface of the frame where it is, so it is null for a frame without one, or when the GPU of the context+// can't sample it.+sk_sp<SkImage> sk_texture_image_sharing_pixels_of_decoded_image_frame(DecodedImageFrame const&, SkiaBackendContext&);+// Hands the source bitmap's pixel storage to the returned SkImage without copying: the image// holds a reference to the bitmap and drops it on destruction.sk_sp<SkImage> sk_image_adopting_bitmap(NonnullRefPtr<Bitmap> bitmap, ColorSpace const& color_space);