1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the plugins of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include "qwaylandquickhardwarelayer_p.h" |
31 | |
32 | #include <QtWaylandCompositor/private/qwlhardwarelayerintegration_p.h> |
33 | #include <QtWaylandCompositor/private/qwlhardwarelayerintegrationfactory_p.h> |
34 | |
35 | #include <QtCore/private/qobject_p.h> |
36 | #include <QMatrix4x4> |
37 | |
38 | QT_BEGIN_NAMESPACE |
39 | |
40 | class QWaylandQuickHardwareLayerPrivate : public QObjectPrivate |
41 | { |
42 | Q_DECLARE_PUBLIC(QWaylandQuickHardwareLayer) |
43 | public: |
44 | QtWayland::HardwareLayerIntegration *layerIntegration(); |
45 | QWaylandQuickItem *m_waylandItem = nullptr; |
46 | int m_stackingLevel = 0; |
47 | QMatrix4x4 m_matrixFromRenderThread; |
48 | static QtWayland::HardwareLayerIntegration *s_hardwareLayerIntegration; |
49 | }; |
50 | |
51 | QtWayland::HardwareLayerIntegration *QWaylandQuickHardwareLayerPrivate::s_hardwareLayerIntegration = nullptr; |
52 | |
53 | QtWayland::HardwareLayerIntegration *QWaylandQuickHardwareLayerPrivate::layerIntegration() |
54 | { |
55 | if (!s_hardwareLayerIntegration) { |
56 | QStringList keys = QtWayland::HardwareLayerIntegrationFactory::keys(); |
57 | |
58 | QString environmentKey = QString::fromLocal8Bit(str: qgetenv(varName: "QT_WAYLAND_HARDWARE_LAYER_INTEGRATION" ).constData()); |
59 | if (!environmentKey.isEmpty()) { |
60 | if (keys.contains(str: environmentKey)) { |
61 | s_hardwareLayerIntegration = QtWayland::HardwareLayerIntegrationFactory::create(name: environmentKey, args: QStringList()); |
62 | } else { |
63 | qWarning() << "Unknown hardware layer integration:" << environmentKey |
64 | << "Valid layer integrations are" << keys; |
65 | } |
66 | } else if (!keys.isEmpty()) { |
67 | s_hardwareLayerIntegration = QtWayland::HardwareLayerIntegrationFactory::create(name: keys.first(), args: QStringList()); |
68 | } else { |
69 | qWarning() << "No wayland hardware layer integrations found" ; |
70 | } |
71 | } |
72 | |
73 | return s_hardwareLayerIntegration; |
74 | } |
75 | |
76 | /*! |
77 | * \qmltype WaylandHardwareLayer |
78 | * \inqmlmodule QtWayland.Compositor |
79 | * \preliminary |
80 | * \brief Makes a parent WaylandQuickItem use hardware layers for rendering. |
81 | * |
82 | * This item needs to be a descendant of a WaylandQuickItem or a derivative, |
83 | * (i.e. ShellSurfaceItem or similar) |
84 | * |
85 | * The Surface of the parent WaylandQuickItem will be drawn in a hardware specific way instead |
86 | * of the regular way using the QtQuick scene graph. On some platforms, the WaylandQuickItem's |
87 | * current buffer and the scene graph can be blended in a separate step. This makes it possible for |
88 | * clients to update continuously without triggering a full redraw of the compositor scene graph for |
89 | * each frame. |
90 | * |
91 | * The preferred hardware layer integration may be overridden by setting the |
92 | * QT_WAYLAND_HARDWARE_LAYER_INTEGRATION environment variable. |
93 | */ |
94 | |
95 | QWaylandQuickHardwareLayer::QWaylandQuickHardwareLayer(QObject *parent) |
96 | : QObject(*new QWaylandQuickHardwareLayerPrivate(), parent) |
97 | { |
98 | } |
99 | |
100 | QWaylandQuickHardwareLayer::~QWaylandQuickHardwareLayer() |
101 | { |
102 | Q_D(QWaylandQuickHardwareLayer); |
103 | if (d->layerIntegration()) |
104 | d->layerIntegration()->remove(this); |
105 | } |
106 | |
107 | /*! |
108 | * \qmlproperty int QtWaylandCompositor::WaylandHardwareLayer::stackingLevel |
109 | * |
110 | * This property holds the stacking level of this hardware layer relative to other hardware layers, |
111 | * and can be used to sort hardware layers. I.e. a layer with a higher level is rendered on top of |
112 | * one with a lower level. |
113 | * |
114 | * Layers with level 0 will be drawn in an implementation defined order on top of the compositor |
115 | * scene graph. |
116 | * |
117 | * Layers with a level below 0 are drawn beneath the compositor scene graph, if supported by the |
118 | * hardware layer integration. |
119 | */ |
120 | int QWaylandQuickHardwareLayer::stackingLevel() const |
121 | { |
122 | Q_D(const QWaylandQuickHardwareLayer); |
123 | return d->m_stackingLevel; |
124 | } |
125 | |
126 | void QWaylandQuickHardwareLayer::setStackingLevel(int level) |
127 | { |
128 | Q_D(QWaylandQuickHardwareLayer); |
129 | if (level == d->m_stackingLevel) |
130 | return; |
131 | |
132 | d->m_stackingLevel = level; |
133 | emit stackingLevelChanged(); |
134 | } |
135 | |
136 | QWaylandQuickItem *QWaylandQuickHardwareLayer::waylandItem() const |
137 | { |
138 | Q_D(const QWaylandQuickHardwareLayer); |
139 | return d->m_waylandItem; |
140 | } |
141 | |
142 | void QWaylandQuickHardwareLayer::classBegin() |
143 | { |
144 | Q_D(QWaylandQuickHardwareLayer); |
145 | for (QObject *p = parent(); p != nullptr; p = p->parent()) { |
146 | if (auto *waylandItem = qobject_cast<QWaylandQuickItem *>(object: p)) { |
147 | d->m_waylandItem = waylandItem; |
148 | break; |
149 | } |
150 | } |
151 | } |
152 | |
153 | void QWaylandQuickHardwareLayer::componentComplete() |
154 | { |
155 | Q_D(QWaylandQuickHardwareLayer); |
156 | Q_ASSERT(d->m_waylandItem); |
157 | if (auto integration = d->layerIntegration()) |
158 | integration->add(this); |
159 | else |
160 | qWarning() << "No hardware layer integration. WaylandHarwareLayer has no effect." ; |
161 | } |
162 | |
163 | void QWaylandQuickHardwareLayer::disableSceneGraphPainting() |
164 | { |
165 | waylandItem()->setPaintEnabled(false); |
166 | } |
167 | |
168 | QT_END_NAMESPACE |
169 | |