1 | // Copyright (C) 2017 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qwlclientbuffer_p.h" |
5 | |
6 | #if QT_CONFIG(opengl) |
7 | #include "hardware_integration/qwlclientbufferintegration_p.h" |
8 | #include <qpa/qplatformopenglcontext.h> |
9 | #include <QOpenGLTexture> |
10 | #endif |
11 | |
12 | #include <QtCore/QDebug> |
13 | |
14 | #include <QtWaylandCompositor/private/wayland-wayland-server-protocol.h> |
15 | #include "qwaylandsharedmemoryformathelper_p.h" |
16 | |
17 | #include <QtWaylandCompositor/private/qwaylandcompositor_p.h> |
18 | |
19 | QT_BEGIN_NAMESPACE |
20 | |
21 | namespace QtWayland { |
22 | |
23 | ClientBuffer::ClientBuffer(struct ::wl_resource *buffer) |
24 | : m_buffer(buffer) |
25 | { |
26 | } |
27 | |
28 | |
29 | ClientBuffer::~ClientBuffer() |
30 | { |
31 | if (m_buffer && m_committed && !m_destroyed) |
32 | sendRelease(); |
33 | } |
34 | |
35 | void ClientBuffer::sendRelease() |
36 | { |
37 | Q_ASSERT(m_buffer); |
38 | wl_buffer_send_release(m_buffer); |
39 | m_committed = false; |
40 | } |
41 | |
42 | void ClientBuffer::setDestroyed() |
43 | { |
44 | m_destroyed = true; |
45 | m_committed = false; |
46 | m_buffer = nullptr; |
47 | |
48 | if (!m_refCount.loadAcquire()) |
49 | delete this; |
50 | } |
51 | |
52 | void ClientBuffer::ref() |
53 | { |
54 | m_refCount.ref(); |
55 | } |
56 | |
57 | void ClientBuffer::deref() |
58 | { |
59 | if (!m_refCount.deref()) { |
60 | if (isCommitted() && m_buffer && !m_destroyed) |
61 | sendRelease(); |
62 | if (m_destroyed) |
63 | delete this; |
64 | } |
65 | } |
66 | |
67 | void ClientBuffer::setCommitted(QRegion &damage) |
68 | { |
69 | m_damage = damage; |
70 | m_committed = true; |
71 | m_textureDirty = true; |
72 | } |
73 | |
74 | QWaylandBufferRef::BufferFormatEgl ClientBuffer::bufferFormatEgl() const |
75 | { |
76 | return QWaylandBufferRef::BufferFormatEgl_Null; |
77 | } |
78 | |
79 | SharedMemoryBuffer::SharedMemoryBuffer(wl_resource *bufferResource) |
80 | : ClientBuffer(bufferResource) |
81 | { |
82 | |
83 | } |
84 | |
85 | QSize SharedMemoryBuffer::size() const |
86 | { |
87 | if (wl_shm_buffer *shmBuffer = wl_shm_buffer_get(resource: m_buffer)) { |
88 | int width = wl_shm_buffer_get_width(buffer: shmBuffer); |
89 | int height = wl_shm_buffer_get_height(buffer: shmBuffer); |
90 | return QSize(width, height); |
91 | } |
92 | return QSize(); |
93 | } |
94 | |
95 | QWaylandSurface::Origin SharedMemoryBuffer::origin() const |
96 | { |
97 | return QWaylandSurface::OriginTopLeft; |
98 | } |
99 | |
100 | static void shmBufferCleanup(void *data) |
101 | { |
102 | auto *pool = static_cast<struct wl_shm_pool *>(data); |
103 | wl_shm_pool_unref(pool); |
104 | } |
105 | |
106 | QImage SharedMemoryBuffer::image() const |
107 | { |
108 | if (wl_shm_buffer *shmBuffer = wl_shm_buffer_get(resource: m_buffer)) { |
109 | int width = wl_shm_buffer_get_width(buffer: shmBuffer); |
110 | int height = wl_shm_buffer_get_height(buffer: shmBuffer); |
111 | int bytesPerLine = wl_shm_buffer_get_stride(buffer: shmBuffer); |
112 | |
113 | // TODO: try to avoid QImage::convertToFormat() |
114 | wl_shm_format shmFormat = wl_shm_format(wl_shm_buffer_get_format(shmBuffer)); |
115 | QImage::Format format = QWaylandSharedMemoryFormatHelper::fromWaylandShmFormat(shmFormat); |
116 | |
117 | auto *pool = wl_shm_buffer_ref_pool(buffer: shmBuffer); |
118 | uchar *data = static_cast<uchar *>(wl_shm_buffer_get_data(buffer: shmBuffer)); |
119 | return QImage(data, width, height, bytesPerLine, format, &shmBufferCleanup, pool); |
120 | } |
121 | |
122 | return QImage(); |
123 | } |
124 | |
125 | #if QT_CONFIG(opengl) |
126 | QOpenGLTexture *SharedMemoryBuffer::toOpenGlTexture(int plane) |
127 | { |
128 | Q_UNUSED(plane); |
129 | if (isSharedMemory()) { |
130 | if (!m_shmTexture) { |
131 | m_shmTexture = new QOpenGLTexture(QOpenGLTexture::Target2D); |
132 | m_shmTexture->create(); |
133 | } |
134 | if (m_textureDirty) { |
135 | m_textureDirty = false; |
136 | m_shmTexture->bind(); |
137 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
138 | // TODO: partial texture upload |
139 | QImage image = this->image(); |
140 | m_shmTexture->setSize(width: image.width(), height: image.height()); |
141 | if (image.hasAlphaChannel()) { |
142 | m_shmTexture->setFormat(QOpenGLTexture::RGBAFormat); |
143 | if (image.format() != QImage::Format_RGBA8888) |
144 | image = image.convertToFormat(f: QImage::Format_RGBA8888); |
145 | glTexImage2D(GL_TEXTURE_2D, level: 0, GL_RGBA, width: image.width(), height: image.height(), border: 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels: image.constBits()); |
146 | } else { |
147 | m_shmTexture->setFormat(QOpenGLTexture::RGBFormat); |
148 | if (image.format() != QImage::Format_RGBX8888) |
149 | image = image.convertToFormat(f: QImage::Format_RGBX8888); |
150 | glTexImage2D(GL_TEXTURE_2D, level: 0, GL_RGB, width: image.width(), height: image.height(), border: 0, GL_RGB, GL_UNSIGNED_BYTE, pixels: image.constBits()); |
151 | } |
152 | //we can release the buffer after uploading, since we have a copy |
153 | if (isCommitted()) |
154 | sendRelease(); |
155 | } |
156 | return m_shmTexture; |
157 | } |
158 | return nullptr; |
159 | } |
160 | #endif |
161 | |
162 | } |
163 | |
164 | QT_END_NAMESPACE |
165 | |