Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement interactive capture #55

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ License: CC0-1.0
Files: xml/*.xml
Copyright: None
License: CC0-1.0

Files: src/wayland/protocols/treeland-capture-unstable-v1.xml
Copyright: UnionTech Software Technology Co., Ltd.
License: CC0-1.0
3 changes: 3 additions & 0 deletions src/wayland/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ add_library(xdg-desktop-portal-dde-wayland SHARED
protocols/screencopy.h
protocols/screencopy.cpp
protocols/common.h
protocols/treelandcapture.h
protocols/treelandcapture.cpp
)

qt_generate_wayland_protocol_client_sources(xdg-desktop-portal-dde-wayland FILES
${WlrProtocols_PKGDATADIR}/unstable/wlr-screencopy-unstable-v1.xml
${CMAKE_CURRENT_LIST_DIR}/protocols/treeland-capture-unstable-v1.xml
)

target_include_directories(xdg-desktop-portal-dde-wayland
Expand Down
1 change: 1 addition & 0 deletions src/wayland/portalwaylandcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ PortalWaylandContext::PortalWaylandContext(QObject *parent)
: QObject(parent)
, QDBusContext()
, m_screenCopyManager(new ScreenCopyManager(this))
, m_treelandCaptureManager(new TreeLandCaptureManager(this))
{
auto screenShotPortal = new ScreenshotPortalWayland(this);
}
4 changes: 4 additions & 0 deletions src/wayland/portalwaylandcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "protocols/screencopy.h"
#include "protocols/treelandcapture.h"

#include <QDBusContext>
#include <private/qwaylanddisplay_p.h>
Expand All @@ -16,6 +17,9 @@ class PortalWaylandContext : public QObject, public QDBusContext
public:
PortalWaylandContext(QObject *parent = nullptr);
inline QPointer<ScreenCopyManager> screenCopyManager() { return m_screenCopyManager; }
inline QPointer<TreeLandCaptureManager> treelandCaptureManager() { return m_treelandCaptureManager; }

private:
ScreenCopyManager *m_screenCopyManager;
TreeLandCaptureManager *m_treelandCaptureManager;
};
217 changes: 217 additions & 0 deletions src/wayland/protocols/treeland-capture-unstable-v1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="treeland_capture_unstable_v1">
<description summary="protocol for capturing output contents or window contents">
This protocol allows authorized application to capture output contents or window
contents(useful for window streaming).
</description>

<interface name="treeland_capture_session_v1" version="1">

<enum name="cancel_reason">
<entry name="temporary" value="0" summary="temporary error, source will produce more frames"/>
<entry name="permanent" value="1" summary="fatal error, source will not produce frames"/>
<entry name="resizing" value="2" summary="temporary error, source will produce more frames"/>
</enum>

<enum name="flags" bitfield="true">
<entry name="transient" value="0x1" summary="clients should copy frame before processing"/>
</enum>

<request name="destroy" type="destructor">
<description summary="delete this object">
Unreferences the frame. This request must be called as soon as it's no longer valid.
</description>
</request>

<request name="start">
<description summary="start session">
Start session and keeps sending frame.
</description>
</request>

<event name="frame">
<description summary="supply the client with information about the frame">
Main event supplying the client with information about the frame. If the capture didn't fail, this event is always
emitted first before any other events.
When mask is provided, x and y should be offset relative to mask surface origin. Otherwise offset_x and offset_y should always
be zero.
</description>
<arg name="offset_x" type="int" summary="crop offset x"/>
<arg name="offset_y" type="int" summary="crop offset y"/>
<arg name="width" type="uint"/>
<arg name="height" type="uint"/>
<arg name="buffer_flags" type="uint"/>
<arg name="flags" type="uint" enum="treeland_capture_session_v1.flags"/>
<arg name="format" type="uint"/>
<arg name="mod_high" type="uint"/>
<arg name="mod_low" type="uint"/>
<arg name="num_objects" type="uint"/>
</event>

<event name="object">
<description summary="supply the client with object fd">
</description>
<arg name="index" type="uint"/>
<arg name="fd" type="fd"/>
<arg name="size" type="uint"/>
<arg name="offset" type="uint"/>
<arg name="stride" type="uint"/>
<arg name="plane_index" type="uint"/>
</event>

<event name="ready">
<description summary="indicates frame is available for reading">
This event is sent as soon as the frame is presented, indicating it is available for reading. This event
includes the time at which presentation happened at.
</description>
<arg name="tv_sec_hi" type="uint"/>
<arg name="tv_sec_lo" type="uint"/>
<arg name="tv_nsec" type="uint"/>
</event>

<event name="cancel">
<description summary="indicates the frame is no longer valid">
If the capture failed or if the frame is no longer valid after the "frame" event has been emitted, this
event will be used to inform the client to scrap the frame.
</description>
<arg name="reason" type="uint" enum="treeland_capture_session_v1.cancel_reason"/>
</event>

</interface>

<interface name="treeland_capture_frame_v1" version="1">
<request name="destroy" type="destructor">
<description summary="delete this object, used or not">
Destroys the context. This request can be sent at any time by the client.
</description>
</request>
<enum name="flags" bitfield="true">
<entry name="y_inverted" value="0x1" summary="contents are y-inverted"/>
</enum>

<event name="buffer">
<description summary="inform client to prepare buffer">
Inform client to prepare buffer.
</description>
<arg name="format" type="uint" enum="wl_shm.format"/>
<arg name="width" type="uint"/>
<arg name="height" type="uint"/>
<arg name="stride" type="uint"/>
</event>

<event name="buffer_done">
<description summary="all buffer formats have done">
Inform client that all buffer formats supported are emitted.
</description>
</event>

<request name="copy">
<description summary="copy capture contents">
Copy capture contents to provided buffer
</description>
<arg name="buffer" type="object" interface="wl_buffer"/>
</request>

<event name="flags">
<description summary="frame flags">
Provides flags about the frame. This event is sent once before the
"ready" event.
</description>
<arg name="flags" type="uint" enum="treeland_capture_once_context_v1.flags" summary="frame flags"/>
</event>

<event name="ready">
<description summary="buffer is copied">
Inform that buffer is ready for reading
</description>
</event>

<event name="failed">
<description summary="frame copy failed">
Inform that frame copy fails.
</description>
</event>
</interface>

<interface name="treeland_capture_context_v1" version="1">

<request name="destroy" type="destructor">
<description summary="delete this object, used or not">
Destroys the context. This request can be sent at any time by the client.
</description>
</request>

<enum name="source_type" bitfield="true">
<entry name="output" value="0x1" summary="output source type"/>
<entry name="window" value="0x2" summary="window source type"/>
<entry name="region" value="0x4" summary="region source type"/>
</enum>

<enum name="source_failure">
<description summary="source failure reason">
</description>
<entry name="selector_busy" value="1" summary="selector is occupied by other context"/>
<entry name="other" value="2" summary="other failure"/>
</enum>

<request name="select_source">
<description summary="select source interactively">
Selector is provided by compositor. Client can provide source hint to hint compositor
to provide certain kinds of source.
</description>
<arg name="source_hint" type="uint" enum="source_type"/>
<arg name="freeze" type="uint" summary="freeze compositing or not"/>
<arg name="with_cursor" type="uint" summary="whether source content contains cursor or not"/>
<arg name="mask" type="object" interface="wl_surface" allow-null="true" summary="this mask is guaranteed to be at the top most"/>
</request>

<event name="source_ready">
<description summary="notify client that source is ready">
This event supplies the client some information about the capture source, including
the capture region relative to mask and source type.
</description>
<arg name="region_x" type="int" summary="offset x of capture region relative to mask for capture contents"/>
<arg name="region_y" type="int" summary="offset y of capture region relative to mask for capture contents"/>
<arg name="region_width" type="uint" summary="width of capture region"/>
<arg name="region_height" type="uint" summary="height of capture region"/>
<arg name="source_type" type="uint" enum="source_type" summary="final capture source type"/>
</event>

<event name="source_failed">
<description summary="notify client that source selection is failed">
There could a lot of reasons but the most common one is that selector is busy
</description>
<arg name="reason" type="uint" enum="source_failure"/>
</event>

<request name="capture">
<description summary="capture one frame">
This event can be called just once. A second call might result in a protocol error cause
we just provide transient
</description>
<arg name="frame" type="new_id" interface="treeland_capture_frame_v1"/>
</request>

<request name="create_session">
<description summary="create a persistent session for capturing">
Often used by a screen recorder.
</description>
<arg name="session" type="new_id" interface="treeland_capture_session_v1"/>
</request>
</interface>

<interface name="treeland_capture_manager_v1" version="1">

<request name="destroy" type="destructor">
<description summary="destroy the capture manager">
Destroy the treeland_capture_manager_v1 object.
</description>
</request>

<request name="get_context">
<description summary="get a capture context">
</description>
<arg name="context" type="new_id" interface="treeland_capture_context_v1"/>
</request>
</interface>
</protocol>
102 changes: 102 additions & 0 deletions src/wayland/protocols/treelandcapture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "treelandcapture.h"
#include "common.h"

Q_DECLARE_LOGGING_CATEGORY(portalWaylandProtocol);
void destruct_treeland_capture_manager(TreeLandCaptureManager *manager)
{
qDeleteAll(manager->captureContexts);
manager->captureContexts.clear();
}


QPointer<TreeLandCaptureContext> TreeLandCaptureManager::getContext()
{
auto context = get_context();
auto captureContext = new TreeLandCaptureContext(context);
captureContexts.append(captureContext);
return captureContext;
}

TreeLandCaptureContext::TreeLandCaptureContext(struct ::treeland_capture_context_v1 *object)
: QObject()
, QtWayland::treeland_capture_context_v1(object)
, m_captureFrame(nullptr)
{}

void TreeLandCaptureContext::treeland_capture_context_v1_source_ready(int32_t region_x, int32_t region_y, uint32_t region_width, uint32_t region_height, uint32_t source_type)
{
Q_EMIT sourceReady(QRect(region_x, region_y, region_width, region_height), source_type);
}

void TreeLandCaptureContext::treeland_capture_context_v1_source_failed(uint32_t reason)
{
Q_EMIT sourceFailed(reason);
}

QPointer<TreeLandCaptureFrame> TreeLandCaptureContext::frame()
{
if (m_captureFrame)
return m_captureFrame;
auto capture_frame = capture();
m_captureFrame = new TreeLandCaptureFrame(capture_frame);
return m_captureFrame;
}

void TreeLandCaptureContext::selectSource(uint32_t sourceHint, bool freeze, bool withCursor, ::wl_surface *mask)
{
select_source(sourceHint, freeze, withCursor, mask);
}
void TreeLandCaptureContext::releaseCaptureFrame() {
if (m_captureFrame) {
delete m_captureFrame;
m_captureFrame = nullptr;
}
}

void TreeLandCaptureFrame::treeland_capture_frame_v1_buffer(uint32_t format, uint32_t width, uint32_t height, uint32_t stride)
{
if (stride != width * 4) {
qCDebug(portalWaylandProtocol)
<< "Receive a buffer format which is not compatible with QWaylandShmBuffer."
<< "format:" << format << "width:" << width << "height:" << height
<< "stride:" << stride;
return;
}
if (m_pendingShmBuffer)
return; // We only need one supported format
m_pendingShmBuffer = new QtWaylandClient::QWaylandShmBuffer(waylandDisplay(), QSize(width, height), QtWaylandClient::QWaylandShm::formatFrom(static_cast<::wl_shm_format>(format)));
copy(m_pendingShmBuffer->buffer());
}

void TreeLandCaptureFrame::treeland_capture_frame_v1_flags(uint32_t flags)
{
m_flags = flags;
}

void TreeLandCaptureFrame::treeland_capture_frame_v1_ready()
{
if (m_shmBuffer)
delete m_shmBuffer;
m_shmBuffer = m_pendingShmBuffer;
m_pendingShmBuffer = nullptr;
Q_EMIT ready(*m_shmBuffer->image());
}

void TreeLandCaptureFrame::treeland_capture_frame_v1_failed()
{
Q_EMIT failed();
}

void TreeLandCaptureManager::releaseCaptureContext(QPointer<TreeLandCaptureContext> context)
{
for (const auto &entry : captureContexts) {
if (entry == context.data()) {
entry->deleteLater();
captureContexts.removeOne(entry);
}
}
}
Loading
Loading