Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qwlclientbuffer.cpp
Go to the documentation of this file.
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)
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>
16
17#include <QtWaylandCompositor/private/qwaylandcompositor_p.h>
18
20
21namespace QtWayland {
22
23ClientBuffer::ClientBuffer(struct ::wl_resource *buffer)
24 : m_buffer(buffer)
25{
26}
27
28
30{
31 if (m_buffer && m_committed && !m_destroyed)
33}
34
36{
38 wl_buffer_send_release(m_buffer);
39 m_committed = false;
40}
41
43{
44 m_destroyed = true;
45 m_committed = false;
46 m_buffer = nullptr;
47
48 if (!m_refCount.loadAcquire())
49 delete this;
50}
51
53{
54 m_refCount.ref();
55}
56
58{
59 if (!m_refCount.deref()) {
60 if (isCommitted() && m_buffer && !m_destroyed)
62 if (m_destroyed)
63 delete this;
64 }
65}
66
68{
69 m_damage = damage;
70 m_committed = true;
71 m_textureDirty = true;
72}
73
78
79SharedMemoryBuffer::SharedMemoryBuffer(wl_resource *bufferResource)
80 : ClientBuffer(bufferResource)
81{
82
83}
84
86{
87 if (wl_shm_buffer *shmBuffer = wl_shm_buffer_get(m_buffer)) {
88 int width = wl_shm_buffer_get_width(shmBuffer);
89 int height = wl_shm_buffer_get_height(shmBuffer);
90 return QSize(width, height);
91 }
92 return QSize();
93}
94
99
100static void shmBufferCleanup(void *data)
101{
102 auto *pool = static_cast<struct wl_shm_pool *>(data);
103 wl_shm_pool_unref(pool);
104}
105
107{
108 if (wl_shm_buffer *shmBuffer = wl_shm_buffer_get(m_buffer)) {
109 int width = wl_shm_buffer_get_width(shmBuffer);
110 int height = wl_shm_buffer_get_height(shmBuffer);
111 int bytesPerLine = wl_shm_buffer_get_stride(shmBuffer);
112
113 // TODO: try to avoid QImage::convertToFormat()
114 wl_shm_format shmFormat = wl_shm_format(wl_shm_buffer_get_format(shmBuffer));
116
117 auto *pool = wl_shm_buffer_ref_pool(shmBuffer);
118 uchar *data = static_cast<uchar *>(wl_shm_buffer_get_data(shmBuffer));
119 return QImage(data, width, height, bytesPerLine, format, &shmBufferCleanup, pool);
120 }
121
122 return QImage();
123}
124
125#if QT_CONFIG(opengl)
126QOpenGLTexture *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(image.width(), image.height());
141 if (image.hasAlphaChannel()) {
142 m_shmTexture->setFormat(QOpenGLTexture::RGBAFormat);
143 if (image.format() != QImage::Format_RGBA8888)
144 image = image.convertToFormat(QImage::Format_RGBA8888);
145 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.constBits());
146 } else {
147 m_shmTexture->setFormat(QOpenGLTexture::RGBFormat);
148 if (image.format() != QImage::Format_RGBX8888)
149 image = image.convertToFormat(QImage::Format_RGBX8888);
150 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 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
bool ref() noexcept
bool deref() noexcept
T loadAcquire() const noexcept
\inmodule QtGui
Definition qimage.h:37
Format
The following image formats are available in Qt.
Definition qimage.h:41
@ Format_RGBA8888
Definition qimage.h:59
@ Format_RGBX8888
Definition qimage.h:58
\inmodule QtGui
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
\inmodule QtCore
Definition qsize.h:25
static QImage::Format fromWaylandShmFormat(wl_shm_format format)
Origin
This enum type is used to specify the origin of a QWaylandSurface's buffer.
ClientBuffer(struct ::wl_resource *bufferResource)
virtual void setCommitted(QRegion &damage)
struct::wl_resource * m_buffer
virtual QWaylandBufferRef::BufferFormatEgl bufferFormatEgl() const
QWaylandSurface::Origin origin() const override
SharedMemoryBuffer(struct ::wl_resource *bufferResource)
QImage image() const override
QSize size() const override
Combined button and popup list for selecting options.
static void shmBufferCleanup(void *data)
Definition image.cpp:4
GLint GLsizei GLsizei height
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLint GLsizei width
GLint GLsizei GLsizei GLenum format
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define GL_UNSIGNED_BYTE
#define GL_RGBA
#define Q_UNUSED(x)
unsigned char uchar
Definition qtypes.h:32