Skip to content

Truly independent web browser

LibGfx: Allow our dma-buf handles to carry multiple planes

Zaggy1024e700a2ef7cd4
diff --git a/Libraries/LibGfx/CMakeLists.txt b/Libraries/LibGfx/CMakeLists.txt
index 1373f954862..2a4582cb9ea 100644
--- a/Libraries/LibGfx/CMakeLists.txt
+++ b/Libraries/LibGfx/CMakeLists.txt
@@ -28,6 +28,7 @@ set(SOURCES
FontCascadeList.cpp
GradientPainting.cpp
Gradients.cpp
+ LinuxDmaBufHandle.cpp
Matrix4x4.cpp
DecodedImageFrame.cpp
RustProcessState.cpp
diff --git a/Libraries/LibGfx/LinuxDmaBufHandle.cpp b/Libraries/LibGfx/LinuxDmaBufHandle.cpp
new file mode 100644
index 00000000000..64ddc9a1060
--- /dev/null
+++ b/Libraries/LibGfx/LinuxDmaBufHandle.cpp
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGfx/LinuxDmaBufHandle.h>
+#include <LibIPC/Decoder.h>
+#include <LibIPC/Encoder.h>
+
+namespace IPC {
+
+template<>
+ErrorOr<void> encode(Encoder& encoder, Gfx::LinuxDmaBufHandle const& dma_buf)
+{
+ TRY(encoder.encode(dma_buf.size));
+ TRY(encoder.encode(dma_buf.drm_format));
+ TRY(encoder.encode(dma_buf.modifier));
+ TRY(encoder.encode(dma_buf.planes.size()));
+ for (auto const& plane : dma_buf.planes) {
+ TRY(encoder.encode(plane.object_index));
+ TRY(encoder.encode(plane.offset));
+ TRY(encoder.encode(plane.pitch));
+ }
+ TRY(encoder.encode(dma_buf.objects.size()));
+ for (auto const& object : dma_buf.objects)
+ TRY(encoder.encode(TRY(IPC::File::clone_fd(object.fd()))));
+ return {};
+}
+
+template<>
+ErrorOr<Gfx::LinuxDmaBufHandle> decode(Decoder& decoder)
+{
+ Gfx::LinuxDmaBufHandle dma_buf;
+ dma_buf.size = TRY(decoder.decode<Gfx::IntSize>());
+ dma_buf.drm_format = TRY(decoder.decode<u32>());
+ dma_buf.modifier = TRY(decoder.decode<u64>());
+
+ auto plane_count = TRY(decoder.decode<size_t>());
+ if (plane_count == 0 || plane_count > Gfx::LinuxDmaBufHandle::MAXIMUM_PLANE_COUNT)
+ return Error::from_string_literal("dma-buf image has an unusable number of planes");
+ for (size_t plane_index = 0; plane_index < plane_count; plane_index++) {
+ Gfx::LinuxDmaBufPlane plane;
+ plane.object_index = TRY(decoder.decode<u32>());
+ plane.offset = TRY(decoder.decode<u32>());
+ plane.pitch = TRY(decoder.decode<u32>());
+ dma_buf.planes.unchecked_append(plane);
+ }
+
+ auto object_count = TRY(decoder.decode<size_t>());
+ if (object_count == 0 || object_count > Gfx::LinuxDmaBufHandle::MAXIMUM_PLANE_COUNT)
+ return Error::from_string_literal("dma-buf image has an unusable number of objects");
+ for (size_t object_index = 0; object_index < object_count; object_index++)
+ dma_buf.objects.unchecked_append(TRY(decoder.decode<IPC::File>()));
+
+ for (auto const& plane : dma_buf.planes) {
+ if (plane.object_index >= dma_buf.objects.size())
+ return Error::from_string_literal("dma-buf image plane refers to an object it does not have");
+ }
+ return dma_buf;
+}
+
+}
diff --git a/Libraries/LibGfx/LinuxDmaBufHandle.h b/Libraries/LibGfx/LinuxDmaBufHandle.h
new file mode 100644
index 00000000000..db366ed9fee
--- /dev/null
+++ b/Libraries/LibGfx/LinuxDmaBufHandle.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Vector.h>
+#include <LibGfx/Size.h>
+#include <LibIPC/File.h>
+#include <LibIPC/Forward.h>
+
+namespace Gfx {
+
+struct LinuxDmaBufPlane {
+ u32 object_index { 0 };
+ u32 offset { 0 };
+ u32 pitch { 0 };
+};
+
+// An image in one or more dma-buf objects. The format and modifier say how many planes it has and how each lays out
+// its pixels, and each plane lies in one of the objects.
+struct LinuxDmaBufHandle {
+ static constexpr size_t MAXIMUM_PLANE_COUNT = 4;
+
+ IntSize size;
+ u32 drm_format { 0 };
+ u64 modifier { 0 };
+ Vector<IPC::File, MAXIMUM_PLANE_COUNT> objects;
+ Vector<LinuxDmaBufPlane, MAXIMUM_PLANE_COUNT> planes;
+};
+
+}
+
+namespace IPC {
+
+template<>
+ErrorOr<void> encode(Encoder&, Gfx::LinuxDmaBufHandle const&);
+
+template<>
+ErrorOr<Gfx::LinuxDmaBufHandle> decode(Decoder&);
+
+}
diff --git a/Libraries/LibGfx/SharedImage.cpp b/Libraries/LibGfx/SharedImage.cpp
index ef8ddc7b729..923086b95db 100644
--- a/Libraries/LibGfx/SharedImage.cpp
+++ b/Libraries/LibGfx/SharedImage.cpp
@@ -39,9 +39,6 @@ SharedImage::SharedImage(WindowsD3DHandle&& d3d_handle)
}
# ifdef USE_VULKAN_DMABUF_IMAGES
-static constexpr auto shared_image_bitmap_format = BitmapFormat::BGRA8888;
-static constexpr auto shared_image_alpha_type = AlphaType::Premultiplied;
-
SharedImage duplicate_shared_image(VulkanImage const& vulkan_image)
{
return SharedImage { duplicate_linux_dmabuf_handle(vulkan_image) };
@@ -52,15 +49,13 @@ LinuxDmaBufHandle duplicate_linux_dmabuf_handle(VulkanImage const& vulkan_image)
VERIFY(vulkan_image.info.format == VK_FORMAT_B8G8R8A8_UNORM);
auto fd = vulkan_image.get_dma_buf_fd();
VERIFY(fd >= 0);
- return LinuxDmaBufHandle {
- .bitmap_format = shared_image_bitmap_format,
- .alpha_type = shared_image_alpha_type,
- .size = IntSize(static_cast<int>(vulkan_image.info.extent.width), static_cast<int>(vulkan_image.info.extent.height)),
- .drm_format = vk_format_to_drm_format(vulkan_image.info.format),
- .pitch = static_cast<size_t>(vulkan_image.info.row_pitch),
- .modifier = vulkan_image.info.modifier,
- .file = IPC::File::adopt_fd(fd),
- };
+ LinuxDmaBufHandle dma_buf;
+ dma_buf.size = IntSize(static_cast<int>(vulkan_image.info.extent.width), static_cast<int>(vulkan_image.info.extent.height));
+ dma_buf.drm_format = vk_format_to_drm_format(vulkan_image.info.format);
+ dma_buf.modifier = vulkan_image.info.modifier;
+ dma_buf.objects.append(IPC::File::adopt_fd(fd));
+ dma_buf.planes.append({ .object_index = 0, .offset = 0, .pitch = static_cast<u32>(vulkan_image.info.row_pitch) });
+ return dma_buf;
}
# endif
@@ -91,33 +86,6 @@ enum class SharedImageBackingType : u8 {
WindowsD3DTexture,
};
-template<>
-ErrorOr<void> encode(Encoder& encoder, Gfx::LinuxDmaBufHandle const& dmabuf)
-{
- TRY(encoder.encode(dmabuf.bitmap_format));
- TRY(encoder.encode(dmabuf.alpha_type));
- TRY(encoder.encode(dmabuf.size));
- TRY(encoder.encode(dmabuf.drm_format));
- TRY(encoder.encode(dmabuf.pitch));
- TRY(encoder.encode(dmabuf.modifier));
- TRY(encoder.encode(TRY(IPC::File::clone_fd(dmabuf.file.fd()))));
- return {};
-}
-
-template<>
-ErrorOr<Gfx::LinuxDmaBufHandle> decode(Decoder& decoder)
-{
- return Gfx::LinuxDmaBufHandle {
- .bitmap_format = TRY(decoder.decode<Gfx::BitmapFormat>()),
- .alpha_type = TRY(decoder.decode<Gfx::AlphaType>()),
- .size = TRY(decoder.decode<Gfx::IntSize>()),
- .drm_format = TRY(decoder.decode<u32>()),
- .pitch = TRY(decoder.decode<size_t>()),
- .modifier = TRY(decoder.decode<u64>()),
- .file = TRY(decoder.decode<IPC::File>()),
- };
-}
-
template<>
ErrorOr<void> encode(Encoder& encoder, Gfx::WindowsD3DHandle const& d3d_handle)
{
diff --git a/Libraries/LibGfx/SharedImage.h b/Libraries/LibGfx/SharedImage.h
index d05e33aef04..bead78452b1 100644
--- a/Libraries/LibGfx/SharedImage.h
+++ b/Libraries/LibGfx/SharedImage.h
@@ -11,6 +11,7 @@
#include <AK/Variant.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Forward.h>
+#include <LibGfx/LinuxDmaBufHandle.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibIPC/File.h>
#include <LibIPC/Forward.h>
@@ -24,16 +25,6 @@ namespace Gfx {
class SharedImageBuffer;
#ifndef AK_OS_MACOS
-struct LinuxDmaBufHandle {
- BitmapFormat bitmap_format;
- AlphaType alpha_type;
- IntSize size;
- u32 drm_format;
- size_t pitch;
- u64 modifier;
- IPC::File file;
-};
-
// NOTE: The texture behind the handle is always DXGI_FORMAT_R8G8B8A8_UNORM with premultiplied alpha.
struct WindowsD3DHandle {
IntSize size;
@@ -90,12 +81,6 @@ private:
namespace IPC {
#ifndef AK_OS_MACOS
-template<>
-ErrorOr<void> encode(Encoder&, Gfx::LinuxDmaBufHandle const&);
-
-template<>
-ErrorOr<Gfx::LinuxDmaBufHandle> decode(Decoder&);
-
template<>
ErrorOr<void> encode(Encoder&, Gfx::WindowsD3DHandle const&);
diff --git a/Libraries/LibGfx/SharedImageBuffer.cpp b/Libraries/LibGfx/SharedImageBuffer.cpp
index 222159de7a3..78cffc63d10 100644
--- a/Libraries/LibGfx/SharedImageBuffer.cpp
+++ b/Libraries/LibGfx/SharedImageBuffer.cpp
@@ -38,14 +38,17 @@ static constexpr auto shared_image_buffer_drm_format = DRM_FORMAT_ARGB8888;
static NonnullRefPtr<Bitmap> create_bitmap_from_linux_dmabuf(LinuxDmaBufHandle const& dmabuf)
{
- VERIFY(dmabuf.bitmap_format == shared_image_buffer_format);
- VERIFY(dmabuf.alpha_type == shared_image_buffer_alpha_type);
VERIFY(dmabuf.drm_format == shared_image_buffer_drm_format);
VERIFY(dmabuf.modifier == DRM_FORMAT_MOD_LINEAR);
- auto data_size = Bitmap::size_in_bytes(dmabuf.pitch, dmabuf.size.height());
- auto* data = ::mmap(nullptr, data_size, PROT_READ, MAP_SHARED, dmabuf.file.fd(), 0);
+ VERIFY(dmabuf.objects.size() == 1);
+ VERIFY(dmabuf.planes.size() == 1);
+ auto const& plane = dmabuf.planes.first();
+ VERIFY(plane.object_index == 0);
+ VERIFY(plane.offset == 0);
+ auto data_size = Bitmap::size_in_bytes(plane.pitch, dmabuf.size.height());
+ auto* data = ::mmap(nullptr, data_size, PROT_READ, MAP_SHARED, dmabuf.objects.first().fd(), 0);
VERIFY(data != MAP_FAILED);
- return MUST(Bitmap::create_wrapper(dmabuf.bitmap_format, dmabuf.alpha_type, dmabuf.size, dmabuf.pitch, data, [data, data_size] {
+ return MUST(Bitmap::create_wrapper(shared_image_buffer_format, shared_image_buffer_alpha_type, dmabuf.size, plane.pitch, data, [data, data_size] {
VERIFY(::munmap(data, data_size) == 0);
}));
}
diff --git a/UI/Qt/WebContentViewLinux.cpp b/UI/Qt/WebContentViewLinux.cpp
index 25cf345d5b2..c9bd59ea5ea 100644
--- a/UI/Qt/WebContentViewLinux.cpp
+++ b/UI/Qt/WebContentViewLinux.cpp
@@ -486,8 +486,11 @@ struct WebContentView::VulkanRenderer {
static Optional<DmaBufIdentity> dmabuf_identity_for(Gfx::LinuxDmaBufHandle const& dmabuf)
{
+ if (dmabuf.objects.size() != 1 || dmabuf.planes.size() != 1 || dmabuf.planes.first().offset != 0)
+ return {};
+
struct stat statbuf {};
- if (fstat(dmabuf.file.fd(), &statbuf) < 0)
+ if (fstat(dmabuf.objects.first().fd(), &statbuf) < 0)
return {};
return DmaBufIdentity {
@@ -495,7 +498,7 @@ struct WebContentView::VulkanRenderer {
.inode = static_cast<u64>(statbuf.st_ino),
.size = dmabuf.size,
.drm_format = dmabuf.drm_format,
- .pitch = dmabuf.pitch,
+ .pitch = dmabuf.planes.first().pitch,
.modifier = dmabuf.modifier,
};
}
@@ -576,7 +579,7 @@ struct WebContentView::VulkanRenderer {
VkImageSubresource subresource { VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT, 0, 0 };
VkSubresourceLayout subresource_layout {};
vkGetImageSubresourceLayout(device, image, &subresource, &subresource_layout);
- if (subresource_layout.rowPitch != dmabuf.pitch) {
+ if (subresource_layout.rowPitch != dmabuf.planes.first().pitch) {
vkDestroyImage(device, image, nullptr);
return {};
}
@@ -589,7 +592,7 @@ struct WebContentView::VulkanRenderer {
.pNext = nullptr,
.memoryTypeBits = 0,
};
- if (get_memory_fd_properties(device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, dmabuf.file.fd(), &memory_fd_properties) != VK_SUCCESS) {
+ if (get_memory_fd_properties(device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, dmabuf.objects.first().fd(), &memory_fd_properties) != VK_SUCCESS) {
vkDestroyImage(device, image, nullptr);
return {};
}
@@ -600,7 +603,7 @@ struct WebContentView::VulkanRenderer {
return {};
}
- int imported_fd = fcntl(dmabuf.file.fd(), F_DUPFD_CLOEXEC, 0);
+ int imported_fd = fcntl(dmabuf.objects.first().fd(), F_DUPFD_CLOEXEC, 0);
if (imported_fd < 0) {
vkDestroyImage(device, image, nullptr);
return {};