1 | // Copyright (C) 2017 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qwlbuffermanager_p.h" |
5 | #include <QWaylandCompositor> |
6 | #include <QtWaylandCompositor/private/qwaylandcompositor_p.h> |
7 | #include <QtWaylandCompositor/private/qwlclientbufferintegration_p.h> |
8 | #include <QDebug> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | namespace QtWayland { |
13 | |
14 | BufferManager::BufferManager(QWaylandCompositor *compositor) |
15 | : QObject(compositor) |
16 | , m_compositor(compositor) |
17 | { |
18 | |
19 | } |
20 | |
21 | struct buffer_manager_destroy_listener : wl_listener |
22 | { |
23 | buffer_manager_destroy_listener() |
24 | { |
25 | notify = BufferManager::destroy_listener_callback; |
26 | wl_list_init(list: &this->link); |
27 | } |
28 | |
29 | BufferManager *d = nullptr; |
30 | }; |
31 | |
32 | void BufferManager::registerBuffer(wl_resource *buffer_resource, ClientBuffer *clientBuffer) |
33 | { |
34 | m_buffers[buffer_resource] = clientBuffer; |
35 | |
36 | auto *destroy_listener = new buffer_manager_destroy_listener; |
37 | destroy_listener->d = this; |
38 | wl_resource_add_destroy_listener(resource: buffer_resource, listener: destroy_listener); |
39 | |
40 | } |
41 | |
42 | ClientBuffer *BufferManager::getBuffer(wl_resource *buffer_resource) |
43 | { |
44 | if (!buffer_resource) |
45 | return nullptr; |
46 | |
47 | auto it = m_buffers.find(key: buffer_resource); |
48 | if (it != m_buffers.end()) |
49 | return it.value(); |
50 | |
51 | ClientBuffer *newBuffer = nullptr; |
52 | |
53 | for (auto *integration : QWaylandCompositorPrivate::get(compositor: m_compositor)->clientBufferIntegrations()) { |
54 | newBuffer = integration->createBufferFor(buffer: buffer_resource); |
55 | if (newBuffer) |
56 | break; |
57 | } |
58 | |
59 | if (newBuffer) |
60 | registerBuffer(buffer_resource, clientBuffer: newBuffer); |
61 | else |
62 | qCWarning(qLcWaylandCompositorHardwareIntegration) << "Could not create buffer for resource."; |
63 | |
64 | return newBuffer; |
65 | } |
66 | |
67 | |
68 | void BufferManager::destroy_listener_callback(wl_listener *listener, void *data) |
69 | { |
70 | buffer_manager_destroy_listener *destroy_listener = static_cast<buffer_manager_destroy_listener *>(listener); |
71 | BufferManager *self = destroy_listener->d; |
72 | struct ::wl_resource *buffer = static_cast<struct ::wl_resource *>(data); |
73 | |
74 | wl_list_remove(elm: &destroy_listener->link); |
75 | delete destroy_listener; |
76 | |
77 | Q_ASSERT(self); |
78 | Q_ASSERT(buffer); |
79 | |
80 | ClientBuffer *clientBuffer = self->m_buffers.take(key: buffer); |
81 | |
82 | if (!clientBuffer) |
83 | return; |
84 | |
85 | clientBuffer->setDestroyed(); |
86 | } |
87 | |
88 | } |
89 | QT_END_NAMESPACE |
90 |