1// Copyright (C) 2017 Klarälvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qwaylandsurfacegrabber.h"
5
6#include <QtCore/private/qobject_p.h>
7#include <QtWaylandCompositor/qwaylandsurface.h>
8#include <QtWaylandCompositor/qwaylandcompositor.h>
9#include <QtWaylandCompositor/private/qwaylandsurface_p.h>
10
11QT_BEGIN_NAMESPACE
12
13/*!
14 \class QWaylandSurfaceGrabber
15 \inmodule QtWaylandCompositor
16 \since 5.8
17 \brief The QWaylandSurfaceGrabber class allows to read the content of a QWaylandSurface.
18
19 Sometimes it is needed to get the contents of a surface, for example to provide a screenshot
20 to the user. The QWaylandSurfaceGrabber class provides a simple method to do so, without
21 having to care what type of buffer backs the surface, be it shared memory, OpenGL or something
22 else.
23*/
24
25/*!
26 \enum QWaylandSurfaceGrabber::Error
27
28 The Error enum describes the reason for a grab failure.
29
30 \value InvalidSurface The surface is null or otherwise not valid.
31 \value NoBufferAttached The client has not attached a buffer on the surface yet.
32 \value UnknownBufferType The buffer attached on the surface is of an unknown type.
33 \value RendererNotReady The compositor renderer is not ready to grab the surface content.
34 */
35
36class QWaylandSurfaceGrabberPrivate : public QObjectPrivate
37{
38 Q_DECLARE_PUBLIC(QWaylandSurfaceGrabber)
39
40 QWaylandSurface *surface = nullptr;
41};
42
43/*!
44 * Create a QWaylandSurfaceGrabber object with the given \a surface and \a parent
45 */
46QWaylandSurfaceGrabber::QWaylandSurfaceGrabber(QWaylandSurface *surface, QObject *parent)
47 : QObject(*(new QWaylandSurfaceGrabberPrivate), parent)
48{
49 Q_D(QWaylandSurfaceGrabber);
50 d->surface = surface;
51}
52
53/*!
54 * Returns the surface set on this object
55 */
56QWaylandSurface *QWaylandSurfaceGrabber::surface() const
57{
58 Q_D(const QWaylandSurfaceGrabber);
59 return d->surface;
60}
61
62/*!
63 * Grab the content of the surface set on this object.
64 * It may not be possible to do that immediately so the success and failed signals
65 * should be used to be notified of when the grab is completed.
66 */
67void QWaylandSurfaceGrabber::grab()
68{
69 Q_D(QWaylandSurfaceGrabber);
70 if (!d->surface) {
71 emit failed(error: InvalidSurface);
72 return;
73 }
74
75 QWaylandSurfacePrivate *surf = QWaylandSurfacePrivate::get(surface: d->surface);
76 QWaylandBufferRef buf = surf->bufferRef;
77 if (!buf.hasBuffer()) {
78 emit failed(error: NoBufferAttached);
79 return;
80 }
81
82 d->surface->compositor()->grabSurface(grabber: this, buffer: buf);
83}
84
85QT_END_NAMESPACE
86
87#include "moc_qwaylandsurfacegrabber.cpp"
88

source code of qtwayland/src/compositor/compositor_api/qwaylandsurfacegrabber.cpp