Skip to content
Open
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
43 changes: 43 additions & 0 deletions .agents/skills/treeland-protocol-test/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,49 @@ void protocol_test_setup(WaylandServer *server)
- 客户端代码只允许 C、`wayland-client` 与 scanner 生成的 C API;不要 include Qt 或 Treeland 的 C++ API。
- `invoke_on_server_thread()` 只用于读取生产状态,或注入没有对应客户端请求的定向 fixture 激励。绝不用它伪造请求结果。

### 像素测试的 fixture 条件

- 框架设置 `TREELAND_DISABLE_WALLPAPER=1`。使用保留 output buffer 的软件渲染路径时,
需要固定的不透明场景背景来重绘窗口移走后暴露的像素;否则旧窗口残影和阴影累积会
污染整屏对比。先在没有被测操作时验证重复渲染稳定,再比较操作前后,不能通过
屏蔽区域或像素容差来掩盖未定位的差异。
- `add_headless_output()` 会立即启用输出,可能使框架先于生产 `OutputManager` 将其加入
layout。需要真实渲染且发生重复 output/cursor layer 注册时,只向已有 headless backend
添加输出,让生产路径完成注册和启用;参考
`tests/protocols/wayland-ext-image-copy-capture-v1/toplevel-setup.cpp`。
- 检查捕获/离屏渲染是否影响屏幕时,先回读已提交 buffer,再单独验证下一次重绘;
不要在取证前强制重绘,否则会覆盖共享 QSG 节点状态污染等瞬态问题。动画场景用
capture renderer 的 `beforeRendering` / `afterRendering` 比较 live 节点状态,
不用跨帧固定矩阵误判正常移动。
- 整图比较需要固定光标/悬停等动态内容。指针夹具可在取基线前隐藏光标,仍保留真实
seat/QtQuick 输入路径;不要屏蔽差异像素。移动/还原后同时比较完整捕获图像和实际
output buffer,GPU 或物理 scanout 未实测时明确记录,不能用 pixman 通过代替。
- renderer 的 preferred read format 不一定有 QImage 映射;明确选择支持的读回格式并
检查分配和读回结果,禁止把空图像地址交给 `wlr_texture_read_pixels()`。夹具窗口引用
用 `QPointer`,主动销毁前暂停“不应变化”的整屏比较,避免异步 frame 回调访问已销毁窗口。

### 捕获 / 录屏类协议的资源生命周期

- 服务端对象的所有者必须是请求它的客户端资源加上 session,而不是被捕获的窗口。
`accept` 只创建 client resource;离屏视口/副本子树在第一个 session 的 `start()` 里建,
最后一个 `stop()` 里同步释放,最后一个 client resource 也消失后再销毁包装对象。
窗口失效只负责 `finish()` → `stopped`,不能作为唯一的回收路径。
- 清理必须延迟到下一轮事件循环:`stop()` 和 client resource 的 destroy listener 都跑在
wlroots 的 `session_destroy()` 里,此时直接 `finish()` 会重入同一个 session 的销毁
(`session_send_stopped()` 用到已释放的 resource)。
- 用 fixture 直接计数离屏视口(`findChildren<WOutputViewport*>()` + `offscreen()`),
断言:创建 source 后为 0;同一 source 的首个 session 创建 1 条管线,增加 session
不再增加管线;销毁最后一个 session 后回到 0;
会话期间提前销毁 source resource 仍为 1 且继续能捕获到 frame。
- `request_frame` 的 `schedule_frame` 来自 session damage 是否非空,不能将它当作
“已有待处理请求”的标志。按需渲染的离屏视口对每次请求都应主动调度,并自行记录
待完成的渲染,不能因为 damage 为空而永远等不到下一个 frame。
- disabled output 不发送 frame,借用其时钟的离屏捕获需要独立的排队渲染兜底。
用真实 power-management 请求覆盖:先发 capture 再在同一批请求中关屏、关屏后
提交新内容并再次捕获、复电和取消排队请求;关闭原输出后重建会话,应解析窗口当前
可用的输出。fixture 只负责添加第二块输出、移动窗口以及读取生产状态,不替代关屏
或捕获请求;同时断言视口复用和兜底不提交真实输出。

## 测试形态与证据
- 每个语义用例都要有名字。发出请求后用 roundtrip、指定事件、信号、模型变化或 future 完成来同步;不要用随意 sleep 或跨用例的状态机。
- 命名为 `dispatch` 的用例只能证明可分发,不能证明业务语义覆盖。
Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ qt_add_qml_module(libtreeland
session/sessionprelaunch.cpp
session/sessionprelaunch.h
session/sessionprotocols.h
session/toplevelcapturesource.cpp
session/toplevelcapturesource.h
session/toplevelcaptureviewport.cpp
session/toplevelcaptureviewport.h
session/sessioncomponent.cpp
session/sessioncomponent.h
session/sessionworkspace.cpp
Expand Down Expand Up @@ -326,6 +330,7 @@ qt_add_qml_module(libtreeland
core/qml/WindowMenu.qml
core/qml/TaskBar.qml
core/qml/SurfaceContent.qml
core/qml/CaptureSurfaceContent.qml
core/qml/XdgShadow.qml
core/qml/Border.qml
core/qml/OutputMenuBar.qml
Expand Down
9 changes: 9 additions & 0 deletions src/core/qml/CaptureSurfaceContent.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (C) 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

import QtQuick

SurfaceContent {
wrapper: surface?.shellSurface ? (surface.parent?.sourceSurface ?? null) : null
syncSurfaceSizeRatio: false
}
5 changes: 3 additions & 2 deletions src/core/qml/SurfaceContent.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Item {

required property SurfaceItem surface
// surface?.parent maybe is a `SubsurfaceContainer`
readonly property SurfaceWrapper wrapper: surface?.parent as SurfaceWrapper
property SurfaceWrapper wrapper: surface?.parent as SurfaceWrapper
property bool syncSurfaceSizeRatio: true
readonly property XWaylandSurface xwaylandSurface: wrapper?.shellSurface as XWaylandSurface
readonly property bool hasClipShape: xwaylandSurface?.hasClipShape ?? false
readonly property real cornerRadius: wrapper?.frameGeometry.radius ?? 0
Expand Down Expand Up @@ -87,7 +88,7 @@ Item {
onDevicePixelRatioChanged: {
// The wrapper outlives a destroyed shell surface during its close
// animation, so there is nothing to keep in sync in that case.
if (wrapper && wrapper.shellSurface) {
if (root.syncSurfaceSizeRatio && wrapper && wrapper.shellSurface) {
wrapper.updateSurfaceSizeRatio()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/qmlengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
, titleBarComponent(this, "Treeland", "TitleBar")
, decorationComponent(this, "Treeland", "Decoration")
, windowMenuComponent(this, "Treeland", "WindowMenu")
, borderComponent(this, "Treeland", "Border")
, taskBarComponent(this, "Treeland", "TaskBar")
, surfaceContent(this, "Treeland", "SurfaceContent")
, captureSurfaceContent(this, "Treeland", "CaptureSurfaceContent")
, xdgShadowComponent(this, "Treeland", "XdgShadow")
, taskSwitchComponent(this, "Treeland", "TaskSwitcher")
, geometryAnimationComponent(this, "Treeland", "GeometryAnimation")
Expand Down Expand Up @@ -119,14 +121,12 @@
return obj;
}

QQuickItem *QmlEngine::createBorder(SurfaceWrapper *surface, QQuickItem *parent)
QQuickItem *QmlEngine::createBorder(QQuickItem *parent)

Check warning on line 124 in src/core/qmlengine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'createBorder' is never used.
{
return createComponent(borderComponent,
parent,
{ { "surface", QVariant::fromValue(surface) } });
return createComponent(borderComponent, parent);
}

QQuickItem *QmlEngine::createTaskBar(Output *output, QQuickItem *parent)

Check warning on line 129 in src/core/qmlengine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'createTaskBar' is never used.
{
return createComponent(taskBarComponent, parent, { { "output", QVariant::fromValue(output) } });
}
Expand Down
8 changes: 7 additions & 1 deletion src/core/qmlengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
QQuickItem *createTitleBar(SurfaceWrapper *surface, QQuickItem *parent);
QQuickItem *createDecoration(SurfaceWrapper *surface, QQuickItem *parent);
QObject *createWindowMenu(QObject *parent);
QQuickItem *createBorder(SurfaceWrapper *surface, QQuickItem *parent);
QQuickItem *createBorder(QQuickItem *parent);
QQuickItem *createTaskBar(Output *output, QQuickItem *parent);
QQuickItem *createXdgShadow(QQuickItem *parent);
QQuickItem *createTaskSwitcher(Output *output, QQuickItem *parent, DesktopEnvironment *desktop);
Expand Down Expand Up @@ -84,6 +84,11 @@
return &surfaceContent;
}

QQmlComponent *captureSurfaceContentComponent()

Check warning on line 87 in src/core/qmlengine.h

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'captureSurfaceContentComponent' is never used.
{
return &captureSurfaceContent;
}

private:
QObject *const dtkInWindowBlurFileSelector;
QQmlComponent titleBarComponent;
Expand All @@ -92,6 +97,7 @@
QQmlComponent borderComponent;
QQmlComponent taskBarComponent;
QQmlComponent surfaceContent;
QQmlComponent captureSurfaceContent;
QQmlComponent xdgShadowComponent;
QQmlComponent taskSwitchComponent;
QQmlComponent geometryAnimationComponent;
Expand Down
40 changes: 6 additions & 34 deletions src/session/sessionprotocols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include "sessionprotocols.h"
#include "session.h"
#include "sessionworkspace.h"
#include "toplevelcapturesource.h"
#include "core/waylandserver.h"

Check warning on line 8 in src/session/sessionprotocols.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "core/waylandserver.h" not found.
#include "core/rootsurfacecontainer.h"

Check warning on line 9 in src/session/sessionprotocols.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "core/rootsurfacecontainer.h" not found.
#include "surface/surfacewrapper.h"

Check warning on line 10 in src/session/sessionprotocols.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "surface/surfacewrapper.h" not found.
#include "output/output.h"
#include "common/treelandlogging.h"
#include "modules/appearance/appearanceinterfacev1.h"
Expand All @@ -17,11 +18,10 @@

#include <wlr_all.h>

#include <wextforeigntoplevellistv1.h>

Check warning on line 21 in src/session/sessionprotocols.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <wextforeigntoplevellistv1.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <wextimagecapturesourcev1impl.h>

Check warning on line 22 in src/session/sessionprotocols.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <wextimagecapturesourcev1impl.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <wsurfaceitem.h>
#include <wsocket.h>

Check warning on line 23 in src/session/sessionprotocols.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <wsocket.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <wscoplistener.h>

Check warning on line 24 in src/session/sessionprotocols.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <wscoplistener.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <wxdgshell.h>
#include <wlayershell.h>
#include <wxdgdecorationmanager.h>
Expand Down Expand Up @@ -119,40 +119,12 @@
return;
}

WSurfaceItem *surfaceItem = surfaceWrapper->surfaceItem();
if (!surfaceItem) {
qCWarning(lcTlCapture) << "Could not get WSurfaceItem from SurfaceWrapper";
wlr_ext_foreign_toplevel_image_capture_source_manager_v1_request_accept(request, nullptr);
return;
}

WSurfaceItemContent *surfaceContent = surfaceItem->findItemContent();
if (!surfaceContent) {
qCWarning(lcTlCapture) << "Could not find WSurfaceItemContent";
wlr_ext_foreign_toplevel_image_capture_source_manager_v1_request_accept(request, nullptr);
return;
}

qCDebug(lcTlCapture) << "Found WSurfaceItemContent for capture:"
<< "size=" << surfaceContent->size()
<< "implicitSize=" << QSizeF(surfaceContent->implicitWidth(), surfaceContent->implicitHeight())
<< "isTextureProvider=" << surfaceContent->isTextureProvider();

auto *output = surfaceWrapper->ownsOutput() ? surfaceWrapper->ownsOutput()->output() : nullptr;
if (!output) {
qCWarning(lcTlCapture) << "Could not get WOutput from SurfaceWrapper";
wlr_ext_foreign_toplevel_image_capture_source_manager_v1_request_accept(request, nullptr);
return;
}

auto *imageCaptureSource = new WExtImageCaptureSourceV1Impl(surfaceContent, output);

imageCaptureSource->setParent(surfaceWrapper);

bool success = wlr_ext_foreign_toplevel_image_capture_source_manager_v1_request_accept(
request, imageCaptureSource->handle());
// Validate and allocate the capture pipeline when the first session starts.
auto *imageCaptureSource = new ToplevelCaptureSource(surfaceWrapper);
connect(surfaceWrapper, &SurfaceWrapper::aboutToBeInvalidated,
imageCaptureSource, &WExtImageCaptureSourceV1Impl::invalidate);

if (!success) {
if (!imageCaptureSource->accept(request)) {
qCWarning(lcTlCapture) << "Failed to accept foreign toplevel image capture request";
delete imageCaptureSource;
}
Expand Down
23 changes: 23 additions & 0 deletions src/session/toplevelcapturesource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "toplevelcapturesource.h"
#include "toplevelcaptureviewport.h"

#include "output/output.h"
#include "surface/surfacewrapper.h"

ToplevelCaptureSource::ToplevelCaptureSource(SurfaceWrapper *wrapper)
: WExtImageCaptureSourceV1Impl(wrapper)
, m_wrapper(wrapper)
{
}

WOutputViewport *ToplevelCaptureSource::createViewport()
{
// Resolve the output per session: the window may have moved while no
// client was capturing it.
if (!m_wrapper || !m_wrapper->surfaceItem() || !m_wrapper->ownsOutput())
Comment thread
glyvut marked this conversation as resolved.
Resolved
return nullptr;
return new ToplevelCaptureViewport(m_wrapper, m_wrapper->ownsOutput()->output());
Comment thread
glyvut marked this conversation as resolved.
Resolved
}
25 changes: 25 additions & 0 deletions src/session/toplevelcapturesource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#pragma once

#include <wextimagecapturesourcev1impl.h>

#include <QPointer>

WAYLIB_SERVER_USE_NAMESPACE

class SurfaceWrapper;

// Per-request toplevel source with a viewport shared by its capture sessions.
class ToplevelCaptureSource final : public WExtImageCaptureSourceV1Impl
{
public:
explicit ToplevelCaptureSource(SurfaceWrapper *wrapper);

protected:
WOutputViewport *createViewport() override;

private:
QPointer<SurfaceWrapper> m_wrapper;
};
Loading
Loading