Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
shmserverbufferintegration.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// Qt-Security score:critical reason:network-protocol
4
6
7#include <QtOpenGL/QOpenGLTexture>
8#include <QtGui/QOpenGLContext>
9#include <QtCore/QSharedMemory>
10
11#include <QtCore/QDebug>
12
14
15ShmServerBuffer::ShmServerBuffer(ShmServerBufferIntegration *integration, const QImage &qimage, QtWayland::ServerBuffer::Format format)
16 : QtWayland::ServerBuffer(qimage.size(),format)
17 , m_integration(integration)
18 , m_width(qimage.width())
19 , m_height(qimage.height())
20 , m_bpl(qimage.bytesPerLine())
21{
22 m_format = format;
23 switch (m_format) {
24 case RGBA32:
25 m_shm_format = QtWaylandServer::qt_shm_emulation_server_buffer::format_RGBA32;
26 break;
27 case A8:
28 m_shm_format = QtWaylandServer::qt_shm_emulation_server_buffer::format_A8;
29 break;
30 default:
31 qWarning("ShmServerBuffer: unsupported format");
32 m_shm_format = QtWaylandServer::qt_shm_emulation_server_buffer::format_RGBA32;
33 break;
34 }
35
36 QString key = "qt_shm_emulation_" + QString::number(qimage.cacheKey());
37 // ### Use proper native keys the next time we can break protocol compatibility
38 QT_IGNORE_DEPRECATIONS(m_shm = new QSharedMemory(key);)
39 qsizetype shm_size = qimage.sizeInBytes();
40 bool ok = m_shm->create(shm_size) && m_shm->lock();
41 if (ok) {
42 memcpy(m_shm->data(), qimage.constBits(), shm_size);
43 m_shm->unlock();
44 } else {
45 qWarning() << "Could not create shared memory" << key << shm_size;
46 }
47}
48
50{
51 delete m_shm;
52}
53
54struct ::wl_resource *ShmServerBuffer::resourceForClient(struct ::wl_client *client)
55{
56 auto *bufferResource = resourceMap().value(client);
57 if (!bufferResource) {
58 auto integrationResource = m_integration->resourceMap().value(client);
59 if (!integrationResource) {
60 qWarning("ShmServerBuffer::resourceForClient: Trying to get resource for ServerBuffer. But client is not bound to the shm_emulation interface");
61 return nullptr;
62 }
63 struct ::wl_resource *shm_integration_resource = integrationResource->handle;
64 Resource *resource = add(client, 1);
65 QT_IGNORE_DEPRECATIONS(const QString shmKey = m_shm->key();)
66 m_integration->send_server_buffer_created(shm_integration_resource, resource->handle, shmKey, m_width, m_height, m_bpl, m_shm_format);
67 return resource->handle;
68 }
69 return bufferResource->handle;
70}
71
73{
74 return resourceMap().size() > 0;
75}
76
78{
79 if (!m_texture) {
80 qWarning("ShmServerBuffer::toOpenGlTexture: no texture defined");
81 }
82 return m_texture;
83}
84
88
92
93bool ShmServerBufferIntegration::initializeHardware(QWaylandCompositor *compositor)
94{
95 Q_ASSERT(QGuiApplication::platformNativeInterface());
96
97 QtWaylandServer::qt_shm_emulation_server_buffer::init(compositor->display(), 1);
98 return true;
99}
100
101bool ShmServerBufferIntegration::supportsFormat(QtWayland::ServerBuffer::Format format) const
102{
103 switch (format) {
104 case QtWayland::ServerBuffer::RGBA32:
105 return true;
106 case QtWayland::ServerBuffer::A8:
107 return true;
108 default:
109 return false;
110 }
111}
112
113QtWayland::ServerBuffer *ShmServerBufferIntegration::createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format)
114{
115 return new ShmServerBuffer(this, qimage, format);
116}
117
118QT_END_NAMESPACE
bool initializeHardware(QWaylandCompositor *) override
bool supportsFormat(QtWayland::ServerBuffer::Format format) const override
QtWayland::ServerBuffer * createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format) override
QOpenGLTexture * toOpenGlTexture() override
Combined button and popup list for selecting options.