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
dmabufserverbufferintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
10#include <drm_fourcc.h>
11#include <unistd.h>
12
13QT_BEGIN_NAMESPACE
14
15DmaBufServerBuffer::DmaBufServerBuffer(DmaBufServerBufferIntegration *integration, const QImage &qimage, QtWayland::ServerBuffer::Format format)
16 : QtWayland::ServerBuffer(qimage.size(),format)
17 , m_integration(integration)
18{
19 m_format = format;
20
21 EGLContext eglContext = eglGetCurrentContext();
22
23 m_texture = new QOpenGLTexture(qimage);
24
25 m_image = m_integration->eglCreateImageKHR(eglContext, EGL_GL_TEXTURE_2D_KHR, (EGLClientBuffer)(unsigned long)m_texture->textureId(), nullptr);
26
27 qCDebug(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer created egl image" << m_image;
28
29 int err = eglGetError();
30 if (err != EGL_SUCCESS || m_image == EGL_NO_IMAGE_KHR)
31 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer error creating EGL image" << Qt::hex << err;
32
33 // TODO: formats with more than one plane
34
35 int num_planes = 1;
36
37 if (!m_integration->eglExportDMABUFImageQueryMESA(m_image, &m_fourcc_format, &num_planes, nullptr)) {
38 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: Failed to query egl image";
39 qCDebug(qLcWaylandCompositorHardwareIntegration) << "error" << Qt::hex << eglGetError();
40 } else {
41 qCDebug(qLcWaylandCompositorHardwareIntegration) << "num_planes" << num_planes << "fourcc_format" << m_fourcc_format;
42 if (num_planes != 1) {
43 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: multi-plane formats not supported";
44 delete m_texture;
45 m_texture = nullptr;
46 m_integration->eglDestroyImageKHR(m_image);
47 m_image = EGL_NO_IMAGE_KHR;
48 return;
49 }
50 }
51
52 if (!m_integration->eglExportDMABUFImageMESA(m_image, &m_fd, &m_stride, &m_offset)) {
53 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: Failed to export egl image. Error code" << Qt::hex << eglGetError();
54 } else {
55 qCDebug(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer exported egl image: fd" << m_fd << "stride" << m_stride << "offset" << m_offset;
56 m_texture->release();
57 }
58}
59
61{
62 delete m_texture;
63
64 int err;
65 m_integration->eglDestroyImageKHR(m_image);
66 if ((err = eglGetError()) != EGL_SUCCESS)
67 qCWarning(qLcWaylandCompositorHardwareIntegration) << "~DmaBufServerBuffer: eglDestroyImageKHR error" << Qt::hex << err;
68
69 err = ::close(m_fd);
70 if (err)
71 perror("~DmaBufServerBuffer:: error closing fd");
72
73}
74
75struct ::wl_resource *DmaBufServerBuffer::resourceForClient(struct ::wl_client *client)
76{
77 auto *bufferResource = resourceMap().value(client);
78 if (!bufferResource) {
79 auto integrationResource = m_integration->resourceMap().value(client);
80 if (!integrationResource) {
81 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer::resourceForClient: Trying to get resource for ServerBuffer. But client is not bound to the qt_dmabuf_server_buffer interface";
82 return nullptr;
83 }
84 struct ::wl_resource *dmabuf_integration_resource = integrationResource->handle;
85
86 Resource *resource = add(client, 1);
87 m_integration->send_server_buffer_created(dmabuf_integration_resource, resource->handle, m_fd, m_size.width(), m_size.height(), m_stride, m_offset, m_fourcc_format);
88 return resource->handle;
89 }
90 return bufferResource->handle;
91}
92
93
95{
96 if (!m_texture) {
97 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer::toOpenGlTexture: no texture defined";
98 }
99 return m_texture;
100}
101
103{
104 return resourceMap().size() > 0;
105}
106
110
114
115bool DmaBufServerBufferIntegration::initializeHardware(QWaylandCompositor *compositor)
116{
117 Q_ASSERT(QGuiApplication::platformNativeInterface());
118
119 m_egl_display = static_cast<EGLDisplay>(QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("egldisplay"));
120 if (!m_egl_display) {
121 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Cannot initialize dmabuf server buffer integration. Missing egl display from platform plugin";
122 return false;
123 }
124
125 const char *extensionString = eglQueryString(m_egl_display, EGL_EXTENSIONS);
126 if (!extensionString || !strstr(extensionString, "EGL_KHR_image")) {
127 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. There is no EGL_KHR_image extension.";
128 return false;
129 }
130
131 m_egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR"));
132 m_egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
133 if (!m_egl_create_image || !m_egl_destroy_image) {
134 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not resolve eglCreateImageKHR or eglDestroyImageKHR";
135 return false;
136 }
137
138 m_gl_egl_image_target_texture_2d = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
139 if (!m_gl_egl_image_target_texture_2d) {
140 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find glEGLImageTargetTexture2DOES.";
141 return false;
142 }
143
144 m_egl_export_dmabuf_image_query = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC>(eglGetProcAddress("eglExportDMABUFImageQueryMESA"));
145 if (!m_egl_export_dmabuf_image_query) {
146 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find eglExportDMABUFImageQueryMESA.";
147 return false;
148 }
149
150 m_egl_export_dmabuf_image = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEMESAPROC>(eglGetProcAddress("eglExportDMABUFImageMESA"));
151 if (!m_egl_export_dmabuf_image) {
152 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find eglExportDMABUFImageMESA.";
153 return false;
154 }
155
156 QtWaylandServer::qt_dmabuf_server_buffer::init(compositor->display(), 1);
157 return true;
158}
159
160bool DmaBufServerBufferIntegration::supportsFormat(QtWayland::ServerBuffer::Format format) const
161{
162 // TODO: 8-bit format support
163 switch (format) {
164 case QtWayland::ServerBuffer::RGBA32:
165 return true;
166 case QtWayland::ServerBuffer::A8:
167 return false;
168 default:
169 return false;
170 }
171}
172
173QtWayland::ServerBuffer *DmaBufServerBufferIntegration::createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format)
174{
175 return new DmaBufServerBuffer(this, qimage, format);
176}
177
179{
180 qCDebug(qLcWaylandCompositorHardwareIntegration) << "server_buffer RELEASE resource" << resource->handle << wl_resource_get_id(resource->handle) << "for client" << resource->client();
181 wl_resource_destroy(resource->handle);
182}
183
184QT_END_NAMESPACE
bool initializeHardware(QWaylandCompositor *) override
bool supportsFormat(QtWayland::ServerBuffer::Format format) const override
EGLBoolean eglDestroyImageKHR(EGLImageKHR image)
QtWayland::ServerBuffer * createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format) override
QOpenGLTexture * toOpenGlTexture() override
void server_buffer_release(Resource *resource) override