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
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
5
6#include <QtOpenGL/QOpenGLTexture>
7#include <QtGui/QOpenGLContext>
8
9#include <drm_fourcc.h>
10#include <unistd.h>
11
13
15 : QtWayland::ServerBuffer(qimage.size(),format)
16 , m_integration(integration)
17{
19
20 EGLContext eglContext = eglGetCurrentContext();
21
22 m_texture = new QOpenGLTexture(qimage);
23
24 m_image = m_integration->eglCreateImageKHR(eglContext, EGL_GL_TEXTURE_2D_KHR, (EGLClientBuffer)(unsigned long)m_texture->textureId(), nullptr);
25
26 qCDebug(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer created egl image" << m_image;
27
28 int err = eglGetError();
29 if (err != EGL_SUCCESS || m_image == EGL_NO_IMAGE_KHR)
30 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer error creating EGL image" << Qt::hex << err;
31
32 // TODO: formats with more than one plane
33
34 int num_planes = 1;
35
36 if (!m_integration->eglExportDMABUFImageQueryMESA(m_image, &m_fourcc_format, &num_planes, nullptr)) {
37 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: Failed to query egl image";
38 qCDebug(qLcWaylandCompositorHardwareIntegration) << "error" << Qt::hex << eglGetError();
39 } else {
40 qCDebug(qLcWaylandCompositorHardwareIntegration) << "num_planes" << num_planes << "fourcc_format" << m_fourcc_format;
41 if (num_planes != 1) {
42 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: multi-plane formats not supported";
43 delete m_texture;
44 m_texture = nullptr;
45 m_integration->eglDestroyImageKHR(m_image);
46 m_image = EGL_NO_IMAGE_KHR;
47 return;
48 }
49 }
50
51 if (!m_integration->eglExportDMABUFImageMESA(m_image, &m_fd, &m_stride, &m_offset)) {
52 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: Failed to export egl image. Error code" << Qt::hex << eglGetError();
53 } else {
54 qCDebug(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer exported egl image: fd" << m_fd << "stride" << m_stride << "offset" << m_offset;
55 m_texture->release();
56 }
57}
58
60{
61 delete m_texture;
62
63 int err;
64 m_integration->eglDestroyImageKHR(m_image);
65 if ((err = eglGetError()) != EGL_SUCCESS)
66 qCWarning(qLcWaylandCompositorHardwareIntegration) << "~DmaBufServerBuffer: eglDestroyImageKHR error" << Qt::hex << err;
67
68 err = ::close(m_fd);
69 if (err)
70 perror("~DmaBufServerBuffer:: error closing fd");
71
72}
73
74struct ::wl_resource *DmaBufServerBuffer::resourceForClient(struct ::wl_client *client)
75{
76 auto *bufferResource = resourceMap().value(client);
77 if (!bufferResource) {
78 auto integrationResource = m_integration->resourceMap().value(client);
79 if (!integrationResource) {
80 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer::resourceForClient: Trying to get resource for ServerBuffer. But client is not bound to the qt_dmabuf_server_buffer interface";
81 return nullptr;
82 }
83 struct ::wl_resource *dmabuf_integration_resource = integrationResource->handle;
84
85 Resource *resource = add(client, 1);
86 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);
87 return resource->handle;
88 }
89 return bufferResource->handle;
90}
91
92
94{
95 if (!m_texture) {
96 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer::toOpenGlTexture: no texture defined";
97 }
98 return m_texture;
99}
100
102{
103 return resourceMap().size() > 0;
104}
105
109
113
115{
117
118 m_egl_display = static_cast<EGLDisplay>(QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("egldisplay"));
119 if (!m_egl_display) {
120 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Cannot initialize dmabuf server buffer integration. Missing egl display from platform plugin";
121 return false;
122 }
123
124 const char *extensionString = eglQueryString(m_egl_display, EGL_EXTENSIONS);
125 if (!extensionString || !strstr(extensionString, "EGL_KHR_image")) {
126 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. There is no EGL_KHR_image extension.";
127 return false;
128 }
129
130 m_egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR"));
131 m_egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
132 if (!m_egl_create_image || !m_egl_destroy_image) {
133 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not resolve eglCreateImageKHR or eglDestroyImageKHR";
134 return false;
135 }
136
137 m_gl_egl_image_target_texture_2d = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
138 if (!m_gl_egl_image_target_texture_2d) {
139 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find glEGLImageTargetTexture2DOES.";
140 return false;
141 }
142
143 m_egl_export_dmabuf_image_query = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC>(eglGetProcAddress("eglExportDMABUFImageQueryMESA"));
144 if (!m_egl_export_dmabuf_image_query) {
145 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find eglExportDMABUFImageQueryMESA.";
146 return false;
147 }
148
149 m_egl_export_dmabuf_image = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEMESAPROC>(eglGetProcAddress("eglExportDMABUFImageMESA"));
150 if (!m_egl_export_dmabuf_image) {
151 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find eglExportDMABUFImageMESA.";
152 return false;
153 }
154
155 QtWaylandServer::qt_dmabuf_server_buffer::init(compositor->display(), 1);
156 return true;
157}
158
160{
161 // TODO: 8-bit format support
162 switch (format) {
164 return true;
166 return false;
167 default:
168 return false;
169 }
170}
171
176
178{
179 qCDebug(qLcWaylandCompositorHardwareIntegration) << "server_buffer RELEASE resource" << resource->handle << wl_resource_get_id(resource->handle) << "for client" << resource->client();
180 wl_resource_destroy(resource->handle);
181}
182
EGLBoolean eglExportDMABUFImageQueryMESA(EGLImageKHR image, int *fourcc, int *num_planes, EGLuint64KHR *modifiers)
bool initializeHardware(QWaylandCompositor *) override
bool supportsFormat(QtWayland::ServerBuffer::Format format) const override
EGLImageKHR eglCreateImageKHR(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
EGLBoolean eglExportDMABUFImageMESA(EGLImageKHR image, int *fds, EGLint *strides, EGLint *offsets)
EGLBoolean eglDestroyImageKHR(EGLImageKHR image)
QtWayland::ServerBuffer * createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format) override
QOpenGLTexture * toOpenGlTexture() override
struct::wl_resource * resourceForClient(struct ::wl_client *) override
DmaBufServerBuffer(DmaBufServerBufferIntegration *integration, const QImage &qimage, QtWayland::ServerBuffer::Format format)
void server_buffer_release(Resource *resource) override
static QPlatformNativeInterface * platformNativeInterface()
\inmodule QtGui
Definition qimage.h:37
\inmodule QtGui
void release()
Unbinds this texture from the currently active texture unit.
GLuint textureId() const
Returns the name of the underlying OpenGL texture object or 0 if it has not yet been created.
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
\qmltype WaylandCompositor \instantiates QWaylandCompositor \inqmlmodule QtWayland....
EGLImageKHR int int * num_planes
Combined button and popup list for selecting options.
QTextStream & hex(QTextStream &stream)
Calls QTextStream::setIntegerBase(16) on stream and returns stream.
typedef EGLDisplay(EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC)(EGLenum platform
#define qCWarning(category,...)
#define qCDebug(category,...)
static QOpenGLCompositor * compositor
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum format
static void add(QPainterPath &path, const QWingedEdge &list, int edge, QPathEdge::Traversal traversal)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47