1 | // Copyright (C) 2018 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qwaylandquickhardwarelayer_p.h" |
5 | |
6 | #include <QtWaylandCompositor/private/qwlhardwarelayerintegration_p.h> |
7 | #include <QtWaylandCompositor/private/qwlhardwarelayerintegrationfactory_p.h> |
8 | |
9 | #include <QtCore/private/qobject_p.h> |
10 | #include <QMatrix4x4> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | class QWaylandQuickHardwareLayerPrivate : public QObjectPrivate |
15 | { |
16 | Q_DECLARE_PUBLIC(QWaylandQuickHardwareLayer) |
17 | public: |
18 | QtWayland::HardwareLayerIntegration *layerIntegration(); |
19 | QWaylandQuickItem *m_waylandItem = nullptr; |
20 | int m_stackingLevel = 0; |
21 | QMatrix4x4 m_matrixFromRenderThread; |
22 | static QtWayland::HardwareLayerIntegration *s_hardwareLayerIntegration; |
23 | }; |
24 | |
25 | QtWayland::HardwareLayerIntegration *QWaylandQuickHardwareLayerPrivate::s_hardwareLayerIntegration = nullptr; |
26 | |
27 | QtWayland::HardwareLayerIntegration *QWaylandQuickHardwareLayerPrivate::layerIntegration() |
28 | { |
29 | if (!s_hardwareLayerIntegration) { |
30 | QStringList keys = QtWayland::HardwareLayerIntegrationFactory::keys(); |
31 | |
32 | QString environmentKey = QString::fromLocal8Bit(ba: qgetenv(varName: "QT_WAYLAND_HARDWARE_LAYER_INTEGRATION").constData()); |
33 | if (!environmentKey.isEmpty()) { |
34 | if (keys.contains(str: environmentKey)) { |
35 | s_hardwareLayerIntegration = QtWayland::HardwareLayerIntegrationFactory::create(name: environmentKey, args: QStringList()); |
36 | } else { |
37 | qWarning() << "Unknown hardware layer integration:"<< environmentKey |
38 | << "Valid layer integrations are"<< keys; |
39 | } |
40 | } else if (!keys.isEmpty()) { |
41 | s_hardwareLayerIntegration = QtWayland::HardwareLayerIntegrationFactory::create(name: keys.first(), args: QStringList()); |
42 | } else { |
43 | qWarning() << "No wayland hardware layer integrations found"; |
44 | } |
45 | } |
46 | |
47 | return s_hardwareLayerIntegration; |
48 | } |
49 | |
50 | /*! |
51 | * \qmltype WaylandHardwareLayer |
52 | * \inqmlmodule QtWayland.Compositor |
53 | * \preliminary |
54 | * \brief Makes a parent WaylandQuickItem use hardware layers for rendering. |
55 | * |
56 | * This item needs to be a descendant of a WaylandQuickItem or a derivative, |
57 | * (i.e. ShellSurfaceItem or similar) |
58 | * |
59 | * The Surface of the parent WaylandQuickItem will be drawn in a hardware specific way instead |
60 | * of the regular way using the QtQuick scene graph. On some platforms, the WaylandQuickItem's |
61 | * current buffer and the scene graph can be blended in a separate step. This makes it possible for |
62 | * clients to update continuously without triggering a full redraw of the compositor scene graph for |
63 | * each frame. |
64 | * |
65 | * The preferred hardware layer integration may be overridden by setting the |
66 | * QT_WAYLAND_HARDWARE_LAYER_INTEGRATION environment variable. |
67 | */ |
68 | |
69 | QWaylandQuickHardwareLayer::QWaylandQuickHardwareLayer(QObject *parent) |
70 | : QObject(*new QWaylandQuickHardwareLayerPrivate(), parent) |
71 | { |
72 | } |
73 | |
74 | QWaylandQuickHardwareLayer::~QWaylandQuickHardwareLayer() |
75 | { |
76 | Q_D(QWaylandQuickHardwareLayer); |
77 | if (d->layerIntegration()) |
78 | d->layerIntegration()->remove(this); |
79 | } |
80 | |
81 | /*! |
82 | * \qmlproperty int QtWayland.Compositor::WaylandHardwareLayer::stackingLevel |
83 | * |
84 | * This property holds the stacking level of this hardware layer relative to other hardware layers, |
85 | * and can be used to sort hardware layers. I.e. a layer with a higher level is rendered on top of |
86 | * one with a lower level. |
87 | * |
88 | * Layers with level 0 will be drawn in an implementation defined order on top of the compositor |
89 | * scene graph. |
90 | * |
91 | * Layers with a level below 0 are drawn beneath the compositor scene graph, if supported by the |
92 | * hardware layer integration. |
93 | */ |
94 | int QWaylandQuickHardwareLayer::stackingLevel() const |
95 | { |
96 | Q_D(const QWaylandQuickHardwareLayer); |
97 | return d->m_stackingLevel; |
98 | } |
99 | |
100 | void QWaylandQuickHardwareLayer::setStackingLevel(int level) |
101 | { |
102 | Q_D(QWaylandQuickHardwareLayer); |
103 | if (level == d->m_stackingLevel) |
104 | return; |
105 | |
106 | d->m_stackingLevel = level; |
107 | emit stackingLevelChanged(); |
108 | } |
109 | |
110 | QWaylandQuickItem *QWaylandQuickHardwareLayer::waylandItem() const |
111 | { |
112 | Q_D(const QWaylandQuickHardwareLayer); |
113 | return d->m_waylandItem; |
114 | } |
115 | |
116 | void QWaylandQuickHardwareLayer::classBegin() |
117 | { |
118 | Q_D(QWaylandQuickHardwareLayer); |
119 | for (QObject *p = parent(); p != nullptr; p = p->parent()) { |
120 | if (auto *waylandItem = qobject_cast<QWaylandQuickItem *>(object: p)) { |
121 | d->m_waylandItem = waylandItem; |
122 | break; |
123 | } |
124 | } |
125 | } |
126 | |
127 | void QWaylandQuickHardwareLayer::componentComplete() |
128 | { |
129 | Q_D(QWaylandQuickHardwareLayer); |
130 | Q_ASSERT(d->m_waylandItem); |
131 | if (auto integration = d->layerIntegration()) |
132 | integration->add(this); |
133 | else |
134 | qWarning() << "No hardware layer integration. WaylandHarwareLayer has no effect."; |
135 | } |
136 | |
137 | void QWaylandQuickHardwareLayer::setSceneGraphPainting(bool enable) |
138 | { |
139 | waylandItem()->setPaintEnabled(enable); |
140 | } |
141 | |
142 | // This should be called if QWaylandQuickHardwareLayer used as a native instance, not a qml component. |
143 | void QWaylandQuickHardwareLayer::initialize() |
144 | { |
145 | classBegin(); |
146 | componentComplete(); |
147 | } |
148 | |
149 | QT_END_NAMESPACE |
150 | |
151 | #include "moc_qwaylandquickhardwarelayer_p.cpp" |
152 |