Skip to content

Truly independent web browser

20.5 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Debug.h>
#include <AK/Format.h>
#include <LibGfx/SharedImageBuffer.h>
#include <UI/Qt/WaylandPageSurface.h>
#include <UI/Qt/WaylandRegistry.h>
#include <UI/Qt/WaylandWindow.h>

#include <QGuiApplication>
#include <QImage>
#include <QPlatformSurfaceEvent>
#include <QTimer>
#include <QWindow>

#include <libdrm/drm_fourcc.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wayland-client.h>

// NB: The generated listener functions cast the const away from the listeners they pass to libwayland.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#include <linux-dmabuf-v1-client-protocol.h>
#include <viewporter-client-protocol.h>
#pragma GCC diagnostic pop

namespace Ladybird {

// NB: The page is opaque. Telling the compositor so lets it skip what is behind the page, or scan the frame out.
static u32 opaque_drm_format(u32 drm_format)
{
    if (drm_format == DRM_FORMAT_ARGB8888)
        return DRM_FORMAT_XRGB8888;
    if (drm_format == DRM_FORMAT_ABGR8888)
        return DRM_FORMAT_XBGR8888;
    return drm_format;
}

OwnPtr<WaylandPageSurface> WaylandPageSurface::create()
{
    static Optional<Globals> const globals = []() -> Optional<Globals> {
        auto* application = wayland_application();
        auto* compositor = application ? application->compositor() : nullptr;
        if (!compositor)
            return {};

        auto registry = WaylandRegistry::create();
        if (!registry)
            return {};

        Globals globals {
            .compositor = compositor,
            .subcompositor = registry->bind<wl_subcompositor>(wl_subcompositor_interface, 1),
            .viewporter = registry->bind<wp_viewporter>(wp_viewporter_interface, 1),
            .dmabuf = registry->bind<zwp_linux_dmabuf_v1>(zwp_linux_dmabuf_v1_interface, 3),
            .shm = registry->bind<wl_shm>(wl_shm_interface, 1),
        };
        for (void* proxy : { static_cast<void*>(globals.subcompositor), static_cast<void*>(globals.viewporter), static_cast<void*>(globals.dmabuf), static_cast<void*>(globals.shm) }) {
            if (proxy)
                WaylandRegistry::move_to_qt_queue(proxy);
        }
        if (!globals.subcompositor || !globals.viewporter || !globals.dmabuf || !globals.shm)
            return {};
        return globals;
    }();

    if (!globals.has_value())
        return nullptr;
    return adopt_own(*new WaylandPageSurface(*globals));
}

WaylandPageSurface::WaylandPageSurface(Globals globals)
    : m_globals(globals)
{
    m_surface = wl_compositor_create_surface(m_globals.compositor);
    m_viewport = wp_viewporter_get_viewport(m_globals.viewporter, m_surface);
    m_overlay_surface = wl_compositor_create_surface(m_globals.compositor);
    m_overlay_viewport = wp_viewporter_get_viewport(m_globals.viewporter, m_overlay_surface);

    // NB: Input goes to the browser window underneath, where Qt delivers it to the view.
    auto* empty_region = wl_compositor_create_region(m_globals.compositor);
    wl_surface_set_input_region(m_surface, empty_region);
    wl_surface_set_input_region(m_overlay_surface, empty_region);
    wl_region_destroy(empty_region);
}

WaylandPageSurface::~WaylandPageSurface()
{
    on_backing_store_released = nullptr;
    on_ready_to_present = nullptr;
    destroy_subsurface();

    for (auto& buffer : m_buffers) {
        if (buffer->state != BufferState::Creating) {
            destroy_buffer(*buffer);
            continue;
        }
        // NB: The listener of the request to create the buffer now owns this record, and destroys it with the answer.
        buffer->surface = nullptr;
        buffer->shared_image_buffer = nullptr;
        (void)buffer.leak_ptr();
    }

    for (auto* buffer : m_overlay_buffers)
        wl_buffer_destroy(buffer);
    wp_viewport_destroy(m_overlay_viewport);
    wl_surface_destroy(m_overlay_surface);
    wp_viewport_destroy(m_viewport);
    wl_surface_destroy(m_surface);
}

bool WaylandPageSurface::eventFilter(QObject* watched, QEvent* event)
{
    if (watched != m_parent)
        return false;

    if (event->type() == QEvent::PlatformSurface) {
        if (static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) {
            destroy_subsurface();
            return false;
        }
    } else if (event->type() != QEvent::Show && event->type() != QEvent::Expose) {
        return false;
    }

    // Qt can replace the wl_surface without recreating the platform window. Recheck after the native window has
    // processed the event, so a page can attach its last frame again even if it has nothing new to present.
    QTimer::singleShot(0, this, [this] {
        if (!m_parent || !m_parent->isVisible())
            return;

        auto* parent_surface = wayland_surface_for_window(*m_parent);
        if (is_attached_to(parent_surface))
            return;

        destroy_subsurface();

        if (parent_surface && on_ready_to_present)
            on_ready_to_present();
    });

    return false;
}

bool WaylandPageSurface::ensure_subsurface(QWindow& parent)
{
    auto* parent_surface = wayland_surface_for_window(parent);
    if (m_parent == &parent && is_attached_to(parent_surface))
        return true;

    destroy_subsurface();

    if (m_parent != &parent) {
        if (m_parent)
            m_parent->removeEventFilter(this);

        m_parent = &parent;
        parent.installEventFilter(this);
    }

    if (!parent_surface)
        return false;

    m_subsurface = wl_subcompositor_get_subsurface(m_globals.subcompositor, m_surface, parent_surface);
    // NB: A desynchronized subsurface shows each frame as soon as it is committed, without waiting for the browser
    //     window to commit too.
    if (m_is_synchronized)
        wl_subsurface_set_sync(m_subsurface);
    else
        wl_subsurface_set_desync(m_subsurface);
    // NB: A new subsurface goes on top of its siblings, so the overlay is above the page. It stays synchronized, so
    //     that what it shows and where it shows it change together.
    m_overlay_subsurface = wl_subcompositor_get_subsurface(m_globals.subcompositor, m_overlay_surface, parent_surface);
    m_parent_surface = parent_surface;
    m_parent_surface_id = wl_proxy_get_id(reinterpret_cast<wl_proxy*>(parent_surface));
    return true;
}

bool WaylandPageSurface::is_attached_to(wl_surface* parent_surface) const
{
    // A replacement proxy can reuse the address of the old one.
    return parent_surface && parent_surface == m_parent_surface
        && wl_proxy_get_id(reinterpret_cast<wl_proxy*>(parent_surface)) == m_parent_surface_id;
}

void WaylandPageSurface::destroy_subsurface()
{
    if (m_subsurface) {
        wl_subsurface_destroy(m_subsurface);
        m_subsurface = nullptr;
    }
    if (m_overlay_subsurface) {
        wl_subsurface_destroy(m_overlay_subsurface);
        m_overlay_subsurface = nullptr;
    }
    m_parent_surface = nullptr;
    m_parent_surface_id = 0;
    m_position.clear();
    hide();
    hide_overlay();
}

WaylandPageSurface::Buffer* WaylandPageSurface::buffer_for(Frame const& frame)
{
    if (frame.dmabuf.objects.is_empty() || frame.dmabuf.planes.is_empty())
        return nullptr;
    for (auto const& plane : frame.dmabuf.planes) {
        if (plane.object_index >= frame.dmabuf.objects.size())
            return nullptr;
    }

    // NB: A backing store can be freed and another one allocated at the same address, so the dma-buf itself is what
    //     identifies the buffer.
    struct stat object_stat {};
    if (fstat(frame.dmabuf.objects[0].fd(), &object_stat) < 0)
        return nullptr;

    for (size_t i = 0; i < m_buffers.size(); ++i) {
        auto& buffer = *m_buffers[i];
        if (buffer.shared_image_buffer != &frame.shared_image_buffer)
            continue;
        if (buffer.device == object_stat.st_dev && buffer.inode == object_stat.st_ino) {
            // NB: A view keeps showing a frame after its backing stores are gone, and it no longer has an ID for it
            //     then. The compositor's release of the buffer is still the release of the backing store it held.
            if (frame.bitmap_id >= 0 || !buffer.is_held_by_compositor) {
                buffer.page_id = frame.page_id;
                buffer.bitmap_id = frame.bitmap_id;
            }
            return &buffer;
        }
        retire_buffer(i);
        break;
    }

    auto buffer = make<Buffer>();
    buffer->surface = this;
    buffer->shared_image_buffer = &frame.shared_image_buffer;
    buffer->device = object_stat.st_dev;
    buffer->inode = object_stat.st_ino;
    buffer->page_id = frame.page_id;
    buffer->bitmap_id = frame.bitmap_id;
    buffer->drm_format = opaque_drm_format(frame.dmabuf.drm_format);
    buffer->modifier = frame.dmabuf.modifier;

    buffer->params = zwp_linux_dmabuf_v1_create_params(m_globals.dmabuf);
    for (size_t plane_index = 0; plane_index < frame.dmabuf.planes.size(); ++plane_index) {
        auto const& plane = frame.dmabuf.planes[plane_index];
        zwp_linux_buffer_params_v1_add(buffer->params, frame.dmabuf.objects[plane.object_index].fd(), plane_index, plane.offset, plane.pitch,
            static_cast<u32>(frame.dmabuf.modifier >> 32), static_cast<u32>(frame.dmabuf.modifier & 0xffffffff));
    }
    static zwp_linux_buffer_params_v1_listener const params_listener {
        .created = buffer_created,
        .failed = buffer_failed,
    };
    zwp_linux_buffer_params_v1_add_listener(buffer->params, &params_listener, buffer.ptr());
    zwp_linux_buffer_params_v1_create(buffer->params, frame.dmabuf.size.width(), frame.dmabuf.size.height(), buffer->drm_format, 0);

    m_buffers.append(move(buffer));
    return m_buffers.last().ptr();
}

void WaylandPageSurface::retire_buffer(size_t index)
{
    // NB: The surface shows nothing at all while its buffer is destroyed, so one on screen lives until it is released.
    auto& buffer = *m_buffers[index];
    // NB: The compositor answers the request to create a buffer with a new object, and libwayland drops the answer to a
    //     request that was destroyed. The connection breaks at the next new object then, so a buffer that is still being
    //     created lives until the answer too.
    if (buffer.is_held_by_compositor || buffer.state == BufferState::Creating) {
        buffer.shared_image_buffer = nullptr;
        return;
    }
    destroy_buffer(buffer);
    m_buffers.remove(index);
}

void WaylandPageSurface::forget_buffers_except(ReadonlySpan<Gfx::SharedImageBuffer const*> backing_stores)
{
    for (size_t i = m_buffers.size(); i-- > 0;) {
        auto const* backing_store = m_buffers[i]->shared_image_buffer;
        if (!backing_store || !backing_stores.contains_slow(backing_store))
            retire_buffer(i);
    }
}

void WaylandPageSurface::destroy_buffer(Buffer& buffer)
{
    VERIFY(!buffer.is_held_by_compositor);
    VERIFY(buffer.state != BufferState::Creating);
    if (buffer.buffer) {
        wl_buffer_destroy(buffer.buffer);
        buffer.buffer = nullptr;
    }
}

void WaylandPageSurface::retire_buffers(Function<Function<void()>(HeldBackingStore const&)> make_release_callback)
{
    on_ready_to_present = nullptr;
    hide();
    for (size_t i = m_buffers.size(); i-- > 0;) {
        if (!m_buffers[i]->is_held_by_compositor)
            continue;
        auto buffer = m_buffers.take(i);
        VERIFY(buffer->buffer && !buffer->params);
        buffer->on_retired_buffer_released = make_release_callback({ buffer->page_id, buffer->bitmap_id, buffer->device, buffer->inode });
        buffer->surface = nullptr;
        buffer->shared_image_buffer = nullptr;
        // NB: The wl_buffer's listener now owns this record. Destroying the surface or the view does not make the
        //     backing store reusable: only wl_buffer.release does. The listener destroys the record after that event.
        (void)buffer.leak_ptr();
    }
}

// A buffer whose surface or backing store went away while it was being created.
bool WaylandPageSurface::forget_buffer_nobody_waits_for(Buffer& buffer)
{
    if (!buffer.surface) {
        auto abandoned_buffer = adopt_own(buffer);
        if (abandoned_buffer->buffer)
            wl_buffer_destroy(abandoned_buffer->buffer);
        return true;
    }
    if (buffer.shared_image_buffer)
        return false;
    auto& surface = *buffer.surface;
    surface.retire_buffer(*surface.m_buffers.find_first_index_if([&](auto const& candidate) { return candidate.ptr() == &buffer; }));
    return true;
}

void WaylandPageSurface::buffer_created(void* data, zwp_linux_buffer_params_v1*, wl_buffer* wayland_buffer)
{
    auto& buffer = *static_cast<Buffer*>(data);
    zwp_linux_buffer_params_v1_destroy(buffer.params);
    buffer.params = nullptr;
    buffer.buffer = wayland_buffer;
    buffer.state = BufferState::Ready;
    if (forget_buffer_nobody_waits_for(buffer))
        return;

    static wl_buffer_listener const buffer_listener {
        .release = buffer_released,
    };
    wl_buffer_add_listener(wayland_buffer, &buffer_listener, &buffer);

    if (auto& callback = buffer.surface->on_ready_to_present)
        callback();
}

void WaylandPageSurface::buffer_failed(void* data, zwp_linux_buffer_params_v1*)
{
    auto& buffer = *static_cast<Buffer*>(data);
    zwp_linux_buffer_params_v1_destroy(buffer.params);
    buffer.params = nullptr;
    buffer.state = BufferState::Failed;
    dbgln("WaylandPageSurface: The compositor cannot show a page frame in a dma-buf of format {:#x} with modifier {:#x}", buffer.drm_format, buffer.modifier);
    if (forget_buffer_nobody_waits_for(buffer))
        return;

    if (auto& callback = buffer.surface->on_ready_to_present)
        callback();
}

void WaylandPageSurface::buffer_released(void* data, wl_buffer*)
{
    auto& buffer = *static_cast<Buffer*>(data);
    if (!buffer.surface) {
        auto retired_buffer = adopt_own(buffer);
        retired_buffer->is_held_by_compositor = false;
        wl_buffer_destroy(retired_buffer->buffer);
        retired_buffer->buffer = nullptr;
        if (retired_buffer->on_retired_buffer_released)
            retired_buffer->on_retired_buffer_released();
        return;
    }
    auto& surface = *buffer.surface;
    buffer.is_held_by_compositor = false;
    auto page_id = buffer.page_id;
    auto bitmap_id = buffer.bitmap_id;

    // NB: A buffer whose backing store is gone was only kept for as long as it was on screen.
    if (!buffer.shared_image_buffer)
        surface.retire_buffer(*surface.m_buffers.find_first_index_if([&](auto const& candidate) { return candidate.ptr() == &buffer; }));

    // NB: A new Compositor process reuses the IDs of backing stores, so another buffer on screen can carry the same
    //     ones. Its own release reports the backing store once it is the last.
    if (surface.holds_backing_store(page_id, bitmap_id))
        return;

    if (auto& callback = surface.on_backing_store_released)
        callback(page_id, bitmap_id);
}

bool WaylandPageSurface::would_change_placement(Frame const& frame) const
{
    return m_position != frame.position || m_presented_source != frame.source || m_presented_destination != frame.destination;
}

WaylandPageSurface::PresentResult WaylandPageSurface::present(QWindow& parent, Frame const& frame)
{
    // NB: The source has to lie within the buffer, or the compositor disconnects us.
    auto source = frame.source.intersected(QRect { 0, 0, frame.dmabuf.size.width(), frame.dmabuf.size.height() });
    if (source.isEmpty() || frame.destination.isEmpty()) {
        hide();
        return PresentResult::Presented;
    }

    auto* buffer = buffer_for(frame);
    if (!buffer)
        return PresentResult::Failed;
    if (buffer->state == BufferState::Creating)
        return PresentResult::Pending;
    if (buffer->state == BufferState::Failed)
        return PresentResult::Failed;

    if (!ensure_subsurface(parent))
        return PresentResult::Pending;

    // NB: The browser window's layout places the view several times for one resize, mostly at the same place.
    if (!frame.has_new_content && m_is_attached && m_presented_buffer == buffer && !would_change_placement(frame)) {
        m_position_needs_parent_commit = false;
        return PresentResult::Presented;
    }

    // NB: The position belongs to the browser window's state, and takes effect when it commits next.
    m_position_needs_parent_commit = m_position != frame.position;
    if (m_position_needs_parent_commit) {
        wl_subsurface_set_position(m_subsurface, frame.position.x(), frame.position.y());
        m_position = frame.position;
    }

    wp_viewport_set_source(m_viewport, wl_fixed_from_int(source.x()), wl_fixed_from_int(source.y()), wl_fixed_from_int(source.width()), wl_fixed_from_int(source.height()));
    wp_viewport_set_destination(m_viewport, frame.destination.width(), frame.destination.height());

    auto* opaque_region = wl_compositor_create_region(m_globals.compositor);
    wl_region_add(opaque_region, 0, 0, frame.destination.width(), frame.destination.height());
    wl_surface_set_opaque_region(m_surface, opaque_region);
    wl_region_destroy(opaque_region);

    wl_surface_attach(m_surface, buffer->buffer, 0, 0);
    wl_surface_damage_buffer(m_surface, 0, 0, INT32_MAX, INT32_MAX);
    wl_surface_commit(m_surface);
    ++m_commit_count;
    buffer->is_held_by_compositor = true;
    m_is_attached = true;
    m_presented_buffer = buffer;
    m_presented_source = frame.source;
    m_presented_destination = frame.destination;
    return PresentResult::Presented;
}

void WaylandPageSurface::hide()
{
    if (!m_is_attached)
        return;
    wl_surface_attach(m_surface, nullptr, 0, 0);
    wl_surface_commit(m_surface);
    ++m_commit_count;
    m_is_attached = false;
}

void WaylandPageSurface::set_synchronized(bool synchronized)
{
    if (m_is_synchronized == synchronized)
        return;
    m_is_synchronized = synchronized;
    if (!m_subsurface)
        return;
    // NB: The compositor applies what the surface committed while synchronized as soon as it is desynchronized, so
    //     that has to wait until the parent has committed.
    if (synchronized)
        wl_subsurface_set_sync(m_subsurface);
    else
        wl_subsurface_set_desync(m_subsurface);
}

bool WaylandPageSurface::show_overlay(QWindow& parent, QImage const& image, QPoint position)
{
    if (image.isNull() || !ensure_subsurface(parent))
        return false;

    // NB: Wayland's ARGB8888 is Qt's premultiplied ARGB32 on a little-endian machine.
    auto pixels = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    auto size = static_cast<size_t>(pixels.sizeInBytes());
    auto fd = memfd_create("ladybird-overlay", MFD_CLOEXEC);
    if (fd < 0)
        return false;
    auto* memory = ftruncate(fd, static_cast<off_t>(size)) == 0 ? mmap(nullptr, size, PROT_WRITE, MAP_SHARED, fd, 0) : MAP_FAILED;
    if (memory == MAP_FAILED) {
        ::close(fd);
        return false;
    }
    memcpy(memory, pixels.constBits(), size);
    munmap(memory, size);

    auto* pool = wl_shm_create_pool(m_globals.shm, fd, static_cast<i32>(size));
    auto* buffer = wl_shm_pool_create_buffer(pool, 0, pixels.width(), pixels.height(), static_cast<i32>(pixels.bytesPerLine()), WL_SHM_FORMAT_ARGB8888);
    wl_shm_pool_destroy(pool);
    ::close(fd);
    static wl_buffer_listener const buffer_listener {
        .release = overlay_buffer_released,
    };
    wl_buffer_add_listener(buffer, &buffer_listener, this);
    m_overlay_buffers.append(buffer);

    auto logical_size = image.deviceIndependentSize().toSize();
    wl_subsurface_set_position(m_overlay_subsurface, position.x(), position.y());
    wp_viewport_set_destination(m_overlay_viewport, logical_size.width(), logical_size.height());
    wl_surface_attach(m_overlay_surface, buffer, 0, 0);
    wl_surface_damage_buffer(m_overlay_surface, 0, 0, INT32_MAX, INT32_MAX);
    wl_surface_commit(m_overlay_surface);
    m_overlay_is_attached = true;
    return true;
}

void WaylandPageSurface::hide_overlay()
{
    if (!m_overlay_is_attached)
        return;
    wl_surface_attach(m_overlay_surface, nullptr, 0, 0);
    wl_surface_commit(m_overlay_surface);
    m_overlay_is_attached = false;
}

void WaylandPageSurface::overlay_buffer_released(void* data, wl_buffer* buffer)
{
    auto& surface = *static_cast<WaylandPageSurface*>(data);
    surface.m_overlay_buffers.remove_first_matching([&](auto* candidate) { return candidate == buffer; });
    wl_buffer_destroy(buffer);
}

bool WaylandPageSurface::holds_backing_store(u64 page_id, i32 bitmap_id) const
{
    for (auto const& buffer : m_buffers) {
        if (buffer->page_id == page_id && buffer->bitmap_id == bitmap_id && buffer->is_held_by_compositor)
            return true;
    }
    return false;
}

}