Truly independent web browser
LibGfx: Hold image frame pixels in IOSurfaces on macOS
Aliaksandr Kalenikcbf6237cc352
diff --git a/Libraries/LibGfx/DecodedImageFrame.cpp b/Libraries/LibGfx/DecodedImageFrame.cppindex 980ed7a425b..c9ef4d536eb 100644--- a/Libraries/LibGfx/DecodedImageFrame.cpp+++ b/Libraries/LibGfx/DecodedImageFrame.cpp@@ -6,6 +6,7 @@#include <AK/Checked.h>#include <LibGfx/DecodedImageFrame.h>+#include <LibIPC/Attachment.h>#include <LibIPC/Decoder.h>#include <LibIPC/Encoder.h>#include <LibIPC/File.h>@@ -57,20 +58,29 @@ ErrorOr<DecodedImageFrame> DecodedImageFrame::create_over_shared_memory(Core::Anreturn DecodedImageFrame { move(shared_memory), offset_of_pixels, size, format, alpha_type, move(color_space) };}+#ifdef AK_OS_MACOS+ErrorOr<DecodedImageFrame> DecodedImageFrame::create_over_iosurface(Core::IOSurfaceHandle iosurface, IntSize size, BitmapFormat format, AlphaType alpha_type, ColorSpace color_space)+{+ TRY(Bitmap::validate_iosurface_holds_pixels_of(iosurface, format, size));+ auto shared_iosurface = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SharedIOSurface(move(iosurface))));+ return DecodedImageFrame { move(shared_iosurface), size, format, alpha_type, move(color_space) };+}+#endif+ErrorOr<DecodedImageFrame> DecodedImageFrame::create(Bitmap const& bitmap, ColorSpace color_space){+#ifdef AK_OS_MACOS+ if (auto const* iosurface = bitmap.iosurface())+ return create_over_iosurface(Core::IOSurfaceHandle::from_ref(iosurface->core_foundation_pointer()), bitmap.size(), bitmap.format(), bitmap.alpha_type(), move(color_space));+#endifif (bitmap.anonymous_buffer().is_valid())return create_over_shared_memory(bitmap.anonymous_buffer(), 0, bitmap.size(), bitmap.format(), bitmap.alpha_type(), move(color_space));if (bitmap.size().is_empty())return Error::from_string_literal("Gfx::DecodedImageFrame size is invalid");- auto pitch = Bitmap::minimum_pitch(bitmap.width(), bitmap.format());- auto shared_memory = TRY(Core::AnonymousBuffer::create_with_size(Bitmap::size_in_bytes(pitch, bitmap.height())));- auto* pixels = shared_memory.data<u8>();- for (int y = 0; y < bitmap.height(); ++y)- __builtin_memcpy(pixels + static_cast<size_t>(y) * pitch, bitmap.scanline_u8(y), pitch);- return create_over_shared_memory(move(shared_memory), 0, bitmap.size(), bitmap.format(), bitmap.alpha_type(), move(color_space));+ auto bitmap_in_shareable_memory = TRY(bitmap.to_bitmap_shareable_with_gpu_access());+ return create(*bitmap_in_shareable_memory, move(color_space));}ErrorOr<DecodedImageFrame> DecodedImageFrame::create_with_id(u64 id, Bitmap const& bitmap, ColorSpace color_space)@@ -82,6 +92,10 @@ ErrorOr<DecodedImageFrame> DecodedImageFrame::create_with_id(u64 id, Bitmap consNonnullRefPtr<Bitmap const> DecodedImageFrame::bitmap_sharing_pixels() const{+#ifdef AK_OS_MACOS+ if (m_iosurface)+ return MUST(Bitmap::create_wrapper(m_format, m_alpha_type, m_size, m_pitch, const_cast<u8*>(m_pixels), [iosurface = m_iosurface] { }));+#endifif (m_offset_of_pixels == 0)return MUST(Bitmap::create_with_anonymous_buffer(m_format, m_alpha_type, m_shared_memory, m_size));return MUST(Bitmap::create_wrapper(m_format, m_alpha_type, m_size, pitch(), const_cast<u8*>(pixels().data()), [shared_memory = m_shared_memory] { }));@@ -91,11 +105,30 @@ NonnullRefPtr<Bitmap const> DecodedImageFrame::bitmap_sharing_pixels() constnamespace IPC {-template<>-ErrorOr<void> encode(Encoder& encoder, Gfx::DecodedImageFrame const& frame)+enum class DecodedImageFramePixelStorage : u8 {+ SharedMemory,+ IOSurface,+};++static ErrorOr<void> encode_pixel_storage(Encoder& encoder, Gfx::DecodedImageFrame const& frame){+#ifdef AK_OS_MACOS+ if (auto const* iosurface = frame.iosurface()) {+ TRY(encoder.encode(to_underlying(DecodedImageFramePixelStorage::IOSurface)));+ TRY(encoder.append_attachment(Attachment::from_mach_port(iosurface->create_mach_port(), Core::MachPort::MessageRight::MoveSend)));+ return {};+ }+#endif+ TRY(encoder.encode(to_underlying(DecodedImageFramePixelStorage::SharedMemory)));TRY(encoder.encode(TRY(IPC::File::clone_fd(frame.shared_memory().fd()))));TRY(encoder.encode(static_cast<u64>(frame.offset_of_pixels())));+ return {};+}++template<>+ErrorOr<void> encode(Encoder& encoder, Gfx::DecodedImageFrame const& frame)+{+ TRY(encode_pixel_storage(encoder, frame));TRY(encoder.encode(frame.size()));TRY(encoder.encode(static_cast<u32>(frame.format())));TRY(encoder.encode(static_cast<u32>(frame.alpha_type())));@@ -106,8 +139,27 @@ ErrorOr<void> encode(Encoder& encoder, Gfx::DecodedImageFrame const& frame)template<>ErrorOr<Gfx::DecodedImageFrame> decode(Decoder& decoder){- auto shared_memory_file = TRY(decoder.decode<IPC::File>());- auto offset_of_pixels = TRY(decoder.decode<u64>());+ Optional<IPC::File> shared_memory_file;+ u64 offset_of_pixels = 0;+#ifdef AK_OS_MACOS+ Optional<Core::IOSurfaceHandle> iosurface;+#endif+ auto pixel_storage = TRY(decoder.decode<UnderlyingType<DecodedImageFramePixelStorage>>());+ if (pixel_storage == to_underlying(DecodedImageFramePixelStorage::IOSurface)) {+#ifdef AK_OS_MACOS+ auto attachment = TRY(decoder.attachments().try_dequeue());+ if (attachment.message_right() != Core::MachPort::MessageRight::MoveSend)+ return Error::from_string_literal("IPC: Gfx::DecodedImageFrame IOSurface attachment is not a moved send right");+ iosurface = TRY(Core::IOSurfaceHandle::from_mach_port(attachment.release_mach_port()));+#else+ return Error::from_string_literal("IPC: This platform has no Gfx::DecodedImageFrame IOSurfaces to decode");+#endif+ } else if (pixel_storage == to_underlying(DecodedImageFramePixelStorage::SharedMemory)) {+ shared_memory_file = TRY(decoder.decode<IPC::File>());+ offset_of_pixels = TRY(decoder.decode<u64>());+ } else {+ return Error::from_string_literal("IPC: Invalid Gfx::DecodedImageFrame pixel storage");+ }auto size = TRY(decoder.decode<Gfx::IntSize>());if (size.is_empty())@@ -127,12 +179,17 @@ ErrorOr<Gfx::DecodedImageFrame> decode(Decoder& decoder)auto color_space = TRY(decoder.decode<Gfx::ColorSpace>());+#ifdef AK_OS_MACOS+ if (iosurface.has_value())+ return Gfx::DecodedImageFrame::create_over_iosurface(iosurface.release_value(), size, format, alpha_type, move(color_space));+#endif+Checked<u64> end_of_pixels = offset_of_pixels;end_of_pixels += Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width(), format), size.height());if (end_of_pixels.has_overflow())return Error::from_string_literal("IPC: Gfx::DecodedImageFrame pixels overflow");- auto shared_memory = TRY(Core::AnonymousBuffer::create_from_anon_fd(shared_memory_file.take_fd(), static_cast<size_t>(end_of_pixels.value())));+ auto shared_memory = TRY(Core::AnonymousBuffer::create_from_anon_fd(shared_memory_file->take_fd(), static_cast<size_t>(end_of_pixels.value())));TRY(shared_memory.validate_backing_size());return Gfx::DecodedImageFrame::create_over_shared_memory(move(shared_memory), static_cast<size_t>(offset_of_pixels), size, format, alpha_type, move(color_space));}diff --git a/Libraries/LibGfx/DecodedImageFrame.h b/Libraries/LibGfx/DecodedImageFrame.hindex d839f6fa94d..aa360350219 100644--- a/Libraries/LibGfx/DecodedImageFrame.h+++ b/Libraries/LibGfx/DecodedImageFrame.h@@ -7,6 +7,7 @@#pragma once#include <AK/Atomic.h>+#include <AK/AtomicRefCounted.h>#include <AK/Error.h>#include <AK/NonnullRefPtr.h>#include <AK/Span.h>@@ -19,6 +20,10 @@#include <LibGfx/Rect.h>#include <LibIPC/Forward.h>+#ifdef AK_OS_MACOS+# include <LibCore/IOSurface.h>+#endif+namespace Gfx {class DecodedImageFrame final {@@ -27,6 +32,9 @@ public:static ErrorOr<DecodedImageFrame> create(Bitmap const&, ColorSpace = {});static ErrorOr<DecodedImageFrame> create_over_shared_memory(Core::AnonymousBuffer shared_memory, size_t offset_of_pixels, IntSize, BitmapFormat, AlphaType, ColorSpace = {});+#ifdef AK_OS_MACOS+ static ErrorOr<DecodedImageFrame> create_over_iosurface(Core::IOSurfaceHandle, IntSize, BitmapFormat, AlphaType, ColorSpace = {});+#endif// For the same pixels decoded again, which caches keyed by the id of a frame may still hold.static ErrorOr<DecodedImageFrame> create_with_id(u64 id, Bitmap const&, ColorSpace = {});@@ -58,21 +66,46 @@ public:size_t pitch() const { return m_pitch; }size_t size_in_bytes() const { return Bitmap::size_in_bytes(m_pitch, m_size.height()); }- ReadonlyBytes pixels() const { return { m_shared_memory.data<u8>() + m_offset_of_pixels, size_in_bytes() }; }+ ReadonlyBytes pixels() const { return { m_pixels, size_in_bytes() }; }+ // Invalid for a frame whose pixels are in an IOSurface.Core::AnonymousBuffer const& shared_memory() const { return m_shared_memory; }size_t offset_of_pixels() const { return m_offset_of_pixels; }- bool has_same_pixels_as(DecodedImageFrame const& other) const { return pixels().data() == other.pixels().data(); }- bool pixels_are_held_elsewhere() const { return m_shared_memory.has_other_holders(); }+#ifdef AK_OS_MACOS+ Core::IOSurfaceHandle const* iosurface() const { return m_iosurface ? &m_iosurface->handle : nullptr; }+#endif++ bool has_same_pixels_as(DecodedImageFrame const& other) const { return m_pixels == other.m_pixels; }+ bool pixels_are_held_elsewhere() const+ {+#ifdef AK_OS_MACOS+ if (m_iosurface)+ return m_iosurface->ref_count() > 1;+#endif+ return m_shared_memory.has_other_holders();+ }NonnullRefPtr<Bitmap const> bitmap_sharing_pixels() const;private:+#ifdef AK_OS_MACOS+ // Copies of a frame share one of these, so that whether anything else holds the pixels is a matter of counting.+ struct SharedIOSurface : public AtomicRefCounted<SharedIOSurface> {+ explicit SharedIOSurface(Core::IOSurfaceHandle handle)+ : handle(move(handle))+ {+ }++ Core::IOSurfaceHandle handle;+ };+#endif+DecodedImageFrame(Core::AnonymousBuffer shared_memory, size_t offset_of_pixels, IntSize size, BitmapFormat format, AlphaType alpha_type, ColorSpace color_space): m_id(next_id()), m_shared_memory(move(shared_memory)), m_offset_of_pixels(offset_of_pixels)+ , m_pixels(m_shared_memory.data<u8>() + offset_of_pixels), m_pitch(Bitmap::minimum_pitch(size.width(), format)), m_size(size), m_format(format)@@ -81,6 +114,20 @@ private:{}+#ifdef AK_OS_MACOS+ DecodedImageFrame(NonnullRefPtr<SharedIOSurface> iosurface, IntSize size, BitmapFormat format, AlphaType alpha_type, ColorSpace color_space)+ : m_id(next_id())+ , m_iosurface(move(iosurface))+ , m_pixels(static_cast<u8 const*>(m_iosurface->handle.data()))+ , m_pitch(m_iosurface->handle.bytes_per_row())+ , m_size(size)+ , m_format(format)+ , m_alpha_type(alpha_type)+ , m_color_space(move(color_space))+ {+ }+#endif+static u64 next_id(){static Atomic<u64> s_next_id { 1 };@@ -90,6 +137,10 @@ private:u64 m_id { 0 };Core::AnonymousBuffer m_shared_memory;size_t m_offset_of_pixels { 0 };+#ifdef AK_OS_MACOS+ RefPtr<SharedIOSurface> m_iosurface;+#endif+ u8 const* m_pixels { nullptr };size_t m_pitch { 0 };IntSize m_size;BitmapFormat m_format { BitmapFormat::Invalid };diff --git a/Libraries/LibGfx/Rust/src/image_frame.rs b/Libraries/LibGfx/Rust/src/image_frame.rsindex a31940d0724..646d617e1a0 100644--- a/Libraries/LibGfx/Rust/src/image_frame.rs+++ b/Libraries/LibGfx/Rust/src/image_frame.rs@@ -52,8 +52,8 @@ struct ImageFrameEntry {}// SAFETY: The entry owns an immutable Gfx::DecodedImageFrame copy. The shared-// memory of its pixels is atomically reference counted, and neither the frame-// nor its color space is mutated while the entry is live.+// memory or IOSurface of its pixels is atomically reference counted, and+// neither the frame nor its color space is mutated while the entry is live.unsafe impl Send for ImageFrameEntry {}// SAFETY: See the Send implementation above.unsafe impl Sync for ImageFrameEntry {}