summaryrefslogtreecommitdiffstats
path: root/src/compositor/extensions/qwaylandpresentationtime.cpp
blob: d47f60167682f820a40fe9bd6f730fb89cea3782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (C) 2021 LG Electronics Inc.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qwaylandpresentationtime_p.h"
#include "qwaylandpresentationtime_p_p.h"

#include <time.h>
#include <QQuickWindow>
#include <QtWaylandCompositor/QWaylandView>
#include <QtWaylandCompositor/QWaylandQuickItem>

QT_BEGIN_NAMESPACE

/*!
 * \qmltype PresentationTime
 * \instantiates QWaylandPresentationTime
 * \inqmlmodule QtWayland.Compositor.PresentationTime
 * \since 6.3
 * \brief Provides tracking the timing when a frame is presented on screen.
 *
 * The PresentationTime extension provides a way to track rendering timing
 * for a surface. Client can request feedbacks associated with a surface,
 * then compositor send events for the feedback with the time when the surface
 * is presented on-screen.
 *
 * PresentationTime corresponds to the Wayland \c wp_presentation interface.
 *
 * To provide the functionality of the presentationtime extension in a compositor, create
 * an instance of the PresentationTime component and add it to the list of extensions
 * supported by the compositor:
 *
 * Then, call sendFeedback() when a surface is presented on screen.
 * Usually, the timing can be obtained from drm page flip event.
 *
 * \qml
 * import QtWayland.Compositor.PresentationTime
 *
 * WaylandCompositor {
 *     PresentationTime {
 *         id: presentationTime
 *     }
 * }
 * \endqml
 */

/*!
 * \class QWaylandPresentationTime
 * \inmodule QtWaylandCompositor
 * \since 6.3
 * \brief The QWaylandPresentationTime class is an extension to get timing for on-screen presentation.
 *
 * The QWaylandPresentationTime extension provides a way to track rendering timing
 * for a surface. Client can request feedbacks associated with a surface,
 * then compositor send events for the feedback with the time when the surface
 * is presented on-screen.
 *
 * QWaylandPresentationTime corresponds to the Wayland \c wp_presentation interface.
 */


/*!
 * Constructs a QWaylandPresentationTime object for \a compositor.
 */
QWaylandPresentationTime::QWaylandPresentationTime(QWaylandCompositor *compositor)
    : QWaylandCompositorExtensionTemplate(compositor, *new QWaylandPresentationTimePrivate)
{

}

/*!
 * Constructs an empty QWaylandPresentationTime object.
 */
QWaylandPresentationTime::QWaylandPresentationTime()
    : QWaylandCompositorExtensionTemplate(*new QWaylandPresentationTimePrivate)
{
}

/*!
 * Initializes the extension.
 */
void QWaylandPresentationTime::initialize()
{
    Q_D(QWaylandPresentationTime);

    if (isInitialized()) {
        qWarning() << "QWaylandPresentationTime is already initialized";
        return;
    }

    QWaylandCompositor *compositor = this->compositor();
    if (compositor == nullptr) {
        qWarning() << "Failed to find QWaylandCompositor when initializing QWaylandPresentationTime";
        return;
    }

    QWaylandCompositorExtensionTemplate::initialize();

    d->init(compositor->display(), /* version */ 1);
}

QWaylandCompositor *QWaylandPresentationTime::compositor() const
{
    return qobject_cast<QWaylandCompositor *>(extensionContainer());
}

/*!
 * \qmlmethod void QWaylandCompositor::PresentationTime::sendFeedback(Window window, int sequence, int sec, int nsec)
 *
 * Interface to notify that a frame is presented on screen using \a window.
 * If your platform supports DRM events, \c page_flip_handler is the proper timing to send it.
 * The \a sequence is the refresh counter. \a sec and \a nsec hold the
 * seconds and nanoseconds parts of the presentation timestamp, respectively.
 */

/*!
 * Interface to notify that a frame is presented on screen using \a window.
 * If your platform supports DRM events, \c page_flip_handler is the proper timing to send it.
 * The \a sequence is the refresh counter. \a tv_sec and \a tv_nsec hold the
 * seconds and nanoseconds parts of the presentation timestamp, respectively.
 */
void QWaylandPresentationTime::sendFeedback(QQuickWindow *window, quint64 sequence, quint64 tv_sec, quint32 tv_nsec)
{
    if (!window)
        return;

    quint32 refresh_nsec = window->screen()->refreshRate() != 0 ? 1000000000 / window->screen()->refreshRate() : 0;

    emit presented(sequence, tv_sec, tv_nsec, refresh_nsec);
}

/*!
 * Returns the Wayland interface for the QWaylandPresentationTime.
 */
const struct wl_interface *QWaylandPresentationTime::interface()
{
    return QWaylandPresentationTimePrivate::interface();
}

/*!
 * \internal
 */
QByteArray QWaylandPresentationTime::interfaceName()
{
    return QWaylandPresentationTimePrivate::interfaceName();
}

PresentationFeedback::PresentationFeedback(QWaylandPresentationTime *pTime, QWaylandSurface *surface, struct ::wl_client *client, uint32_t id, int version)
    : wp_presentation_feedback(client, id, version)
    , m_presentationTime(pTime)
{
    setSurface(surface);
}

void PresentationFeedback::setSurface(QWaylandSurface *qwls)
{
    if (!qwls) {
        discard();
        return;
    }

    m_surface = qwls;

    connect(qwls, &QWaylandSurface::damaged, this, &PresentationFeedback::onSurfaceCommit);
    connect(qwls, &QWaylandSurface::destroyed, this, &PresentationFeedback::discard);

    QWaylandView *view = qwls ? qwls->primaryView() : nullptr;
    //The surface has not committed yet.
    if (!view) {
        connect(qwls, &QWaylandSurface::hasContentChanged, this, &PresentationFeedback::onSurfaceMapped);
        return;
    }

    maybeConnectToWindow(view);
}

void PresentationFeedback::onSurfaceCommit()
{
    // There is a new commit before sync so that discard this feedback.
    if (m_committed) {
        discard();
        return;
    }
    m_committed = true;
}

void PresentationFeedback::onSurfaceMapped()
{
    QWaylandView *view = m_surface->primaryView();
    if (!view) {
        qWarning() << "The mapped surface has no view";
        discard();
        return;
    }

    maybeConnectToWindow(view);
}

void PresentationFeedback::maybeConnectToWindow(QWaylandView *view)
{
    QWaylandQuickItem *item = view ? qobject_cast<QWaylandQuickItem *>(view->renderObject()) : nullptr;
    if (!item) {
        qWarning() << "QWaylandPresentationTime only works with QtQuick compositors" << view;
        discard();
        return;
    }

    connect(item, &QQuickItem::windowChanged, this, &PresentationFeedback::onWindowChanged);
    // wait for having window
    if (!item->window()) {
       return;
    }

    connectToWindow(item->window());
}

void PresentationFeedback::onWindowChanged()
{
    QWaylandQuickItem *item = qobject_cast<QWaylandQuickItem *>(sender());
    QQuickWindow *window = item ? item->window() : nullptr;

    if (!window) {
        qWarning() << "QWaylandPresentationTime only works with QtQuick compositors" << item;
        discard();
        /* Actually, the commit is not discarded yet. If the related item has new window,
           the commit can be presented on screen. So we can choose not to discard the feedback
           until item has new window or the surface is destroyed. */
        return;
    }

    // Check if the connected window is changed
    if (m_connectedWindow && m_connectedWindow != window)
        m_connectedWindow->disconnect(this);

    connectToWindow(window);
}

void PresentationFeedback::connectToWindow(QQuickWindow *window)
{
    if (!window) {
        discard();
        return;
    }

    m_connectedWindow = window;

    connect(window, &QQuickWindow::beforeSynchronizing, this, &PresentationFeedback::onSync);
    connect(window, &QQuickWindow::afterFrameEnd, this, &PresentationFeedback::onSwapped);
}

void PresentationFeedback::onSync()
{
    QQuickWindow *window = qobject_cast<QQuickWindow *>(sender());

    if (m_committed) {
        disconnect(m_surface, &QWaylandSurface::damaged, this, &PresentationFeedback::onSurfaceCommit);
        disconnect(window, &QQuickWindow::beforeSynchronizing, this, &PresentationFeedback::onSync);
        m_sync = true;
    }
}

void PresentationFeedback::onSwapped()
{
    QQuickWindow *window = qobject_cast<QQuickWindow *>(sender());

    if (m_sync) {
        disconnect(window, &QQuickWindow::afterFrameEnd, this, &PresentationFeedback::onSwapped);
        connect(m_presentationTime, &QWaylandPresentationTime::presented, this, &PresentationFeedback::sendPresented);
    }
}

void PresentationFeedback::discard()
{
    send_discarded();
    destroy();
}

void PresentationFeedback::sendSyncOutput()
{
    QWaylandCompositor *compositor = presentationTime()->compositor();
    if (!compositor) {
        qWarning() << "No compositor container to send sync_output";
        return;
    }

    QWaylandView *view = surface()->primaryView();
    QWaylandOutput *output = view ? view->output() : nullptr;
    struct ::wl_resource *r = output ? output->resourceForClient(QWaylandClient::fromWlClient(compositor, resource()->client())) : nullptr;

    if (r)
        send_sync_output(r);
}

void PresentationFeedback::sendPresented(quint64 sequence, quint64 tv_sec, quint32 tv_nsec, quint32 refresh_nsec)
{
    sendSyncOutput();

    send_presented(tv_sec >> 32, tv_sec, tv_nsec, refresh_nsec, sequence >> 32, sequence,
            QtWaylandServer::wp_presentation_feedback::kind_vsync
            | QtWaylandServer::wp_presentation_feedback::kind_hw_clock
            | QtWaylandServer::wp_presentation_feedback::kind_hw_completion);

    destroy();
}

void PresentationFeedback::destroy()
{
    wl_resource_destroy(resource()->handle);
}

void PresentationFeedback::wp_presentation_feedback_destroy_resource(Resource *resource)
{
    Q_UNUSED(resource);
    delete this;
}

QWaylandPresentationTimePrivate::QWaylandPresentationTimePrivate()
{
}

void QWaylandPresentationTimePrivate::wp_presentation_bind_resource(Resource *resource)
{
    send_clock_id(resource->handle, CLOCK_MONOTONIC);
}

void QWaylandPresentationTimePrivate::wp_presentation_feedback(Resource *resource, struct ::wl_resource *surface, uint32_t callback)
{
    Q_Q(QWaylandPresentationTime);

    QWaylandSurface *qwls = QWaylandSurface::fromResource(surface);
    if (!qwls)
        return;

    new PresentationFeedback(q, qwls, resource->client(), callback, /* version */ 1);
}

QT_END_NAMESPACE

#include "moc_qwaylandpresentationtime_p_p.cpp"

#include "moc_qwaylandpresentationtime_p.cpp"