Skip to content

Truly independent web browser

5.5 KB
Raw Blame
/*
 * Copyright (c) 2026, Aliaksandr Kalenik <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGfx/Bitmap.h>
#include <LibGfx/SharedImageBuffer.h>

#ifdef USE_VULKAN_DMABUF_IMAGES
#    include <libdrm/drm_fourcc.h>
#    include <sys/mman.h>
#endif

namespace Gfx {

#ifdef AK_OS_MACOS
static constexpr auto shared_image_buffer_format = BitmapFormat::BGRA8888;
static constexpr auto shared_image_buffer_alpha_type = AlphaType::Premultiplied;

static NonnullRefPtr<Bitmap> create_bitmap_from_iosurface(Core::IOSurfaceHandle const& iosurface_handle)
{
    auto size = IntSize(static_cast<int>(iosurface_handle.width()), static_cast<int>(iosurface_handle.height()));
    auto bitmap_handle = Core::IOSurfaceHandle::from_mach_port(iosurface_handle.create_mach_port());
    return MUST(Bitmap::create_wrapper(shared_image_buffer_format, shared_image_buffer_alpha_type, size, iosurface_handle.bytes_per_row(), iosurface_handle.data(), [handle = move(bitmap_handle)] { }));
}

SharedImageBuffer::SharedImageBuffer(Core::IOSurfaceHandle&& iosurface_handle, NonnullRefPtr<Bitmap> bitmap)
    : m_iosurface_handle(move(iosurface_handle))
    , m_bitmap(move(bitmap))
{
}
#else
static constexpr auto shared_image_buffer_format = BitmapFormat::BGRA8888;
static constexpr auto shared_image_buffer_alpha_type = AlphaType::Premultiplied;
#    ifdef USE_VULKAN_DMABUF_IMAGES
static constexpr auto shared_image_buffer_drm_format = DRM_FORMAT_ARGB8888;

static NonnullRefPtr<Bitmap> create_bitmap_from_linux_dmabuf(LinuxDmaBufHandle const& dmabuf)
{
    VERIFY(dmabuf.drm_format == shared_image_buffer_drm_format);
    VERIFY(dmabuf.modifier == DRM_FORMAT_MOD_LINEAR);
    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(shared_image_buffer_format, shared_image_buffer_alpha_type, dmabuf.size, plane.pitch, data, [data, data_size] {
        VERIFY(::munmap(data, data_size) == 0);
    }));
}
#    endif

SharedImageBuffer::SharedImageBuffer(NonnullRefPtr<Bitmap> bitmap)
    : m_bitmap(move(bitmap))
{
}

#    ifdef USE_VULKAN_DMABUF_IMAGES
SharedImageBuffer::SharedImageBuffer(RefPtr<Bitmap> bitmap, LinuxDmaBufHandle&& dmabuf)
    : m_linux_dmabuf_handle(move(dmabuf))
    , m_bitmap(move(bitmap))
{
}
#    endif

#    ifdef USE_DIRECTX
SharedImageBuffer::SharedImageBuffer(WindowsD3DHandle&& d3d_handle)
    : m_windows_d3d_handle(move(d3d_handle))
{
}
#    endif
#endif

SharedImageBuffer SharedImageBuffer::create(IntSize size)
{
    return MUST(try_create(size));
}

ErrorOr<SharedImageBuffer> SharedImageBuffer::try_create(IntSize size)
{
#ifdef AK_OS_MACOS
    auto iosurface_handle = TRY(Core::IOSurfaceHandle::try_create(size.width(), size.height()));
    auto bitmap = create_bitmap_from_iosurface(iosurface_handle);
    return SharedImageBuffer(move(iosurface_handle), move(bitmap));
#else
    return SharedImageBuffer(TRY(Bitmap::create_shareable(shared_image_buffer_format, shared_image_buffer_alpha_type, size)));
#endif
}

ErrorOr<void> SharedImageBuffer::check_can_import(SharedImage const& shared_image)
{
#ifdef AK_OS_MACOS
    TRY(Core::IOSurfaceHandle::from_mach_port(shared_image.m_port));
#else
    (void)shared_image;
#endif
    return {};
}

ErrorOr<SharedImageBuffer> SharedImageBuffer::import_from_shared_image(SharedImage shared_image)
{
#ifdef AK_OS_MACOS
    auto iosurface_handle = TRY(Core::IOSurfaceHandle::from_mach_port(shared_image.m_port));
    auto bitmap = create_bitmap_from_iosurface(iosurface_handle);
    return SharedImageBuffer(move(iosurface_handle), move(bitmap));
#else
    return shared_image.m_data.visit(
        [](ShareableBitmap& shareable_bitmap) -> SharedImageBuffer {
            return SharedImageBuffer(*shareable_bitmap.bitmap());
        },
        [](LinuxDmaBufHandle& dmabuf) -> SharedImageBuffer {
#    ifdef USE_VULKAN_DMABUF_IMAGES
            // NB: Only a linear layout can be read through a mapping. The pixels of a tiled one are only for the GPU.
            if (dmabuf.modifier != DRM_FORMAT_MOD_LINEAR)
                return SharedImageBuffer(nullptr, move(dmabuf));
            auto bitmap = create_bitmap_from_linux_dmabuf(dmabuf);
            return SharedImageBuffer(move(bitmap), move(dmabuf));
#    else
            (void)dmabuf;
            VERIFY_NOT_REACHED();
#    endif
        },
        [](WindowsD3DHandle& d3d_handle) -> SharedImageBuffer {
#    ifdef USE_DIRECTX
            // NOTE: D3D12 default-heap textures cannot be mapped for CPU access, so unlike the
            //       other backing types this buffer has no bitmap view of the pixels.
            return SharedImageBuffer(move(d3d_handle));
#    else
            (void)d3d_handle;
            VERIFY_NOT_REACHED();
#    endif
        });
#endif
}

SharedImageBuffer::SharedImageBuffer(SharedImageBuffer&&) = default;

SharedImageBuffer& SharedImageBuffer::operator=(SharedImageBuffer&&) = default;

SharedImageBuffer::~SharedImageBuffer() = default;

SharedImage SharedImageBuffer::export_shared_image() const
{
#ifdef AK_OS_MACOS
    return SharedImage { m_iosurface_handle.create_mach_port() };
#else
    return SharedImage { ShareableBitmap { bitmap(), ShareableBitmap::ConstructWithKnownGoodBitmap } };
#endif
}

}