1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "dmabufserverbufferintegration.h"
5#include <QtWaylandClient/private/qwaylanddisplay_p.h>
6#include <QDebug>
7#include <QtOpenGL/QOpenGLTexture>
8#include <QtGui/QOpenGLContext>
9
10#include <EGL/egl.h>
11#include <EGL/eglext.h>
12
13QT_BEGIN_NAMESPACE
14
15namespace QtWaylandClient {
16
17DmaBufServerBuffer::DmaBufServerBuffer(DmaBufServerBufferIntegration *integration
18 , struct ::qt_server_buffer *id
19 , int32_t fd
20 , int32_t width
21 , int32_t height
22 , int32_t stride
23 , int32_t offset
24 , int32_t fourcc_format)
25 : m_integration(integration)
26 , m_server_buffer(id)
27{
28 m_size = QSize(width, height);
29
30
31 EGLint import_attribs[] = {
32 EGL_WIDTH, width,
33 EGL_HEIGHT, height,
34 EGL_LINUX_DRM_FOURCC_EXT, fourcc_format,
35 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
36 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offset,
37 EGL_DMA_BUF_PLANE0_PITCH_EXT, stride,
38 EGL_NONE
39 };
40
41 m_image = integration->eglCreateImageKHR(
42 EGL_NO_CONTEXT,
43 EGL_LINUX_DMA_BUF_EXT,
44 buffer: (EGLClientBuffer)nullptr,
45 attrib_list: import_attribs);
46
47 int err = eglGetError();
48 qCDebug(lcQpaWayland) << "imported egl image" << m_image;
49 if (m_image == EGL_NO_IMAGE_KHR || err != EGL_SUCCESS)
50 qCWarning(lcQpaWayland) << "DmaBufServerBuffer error importing image. EGL error code" << Qt::hex << err;
51
52 qt_server_buffer_set_user_data(id, this);
53
54}
55
56DmaBufServerBuffer::~DmaBufServerBuffer()
57{
58 int err = m_integration->eglDestroyImageKHR(image: m_image);
59 if (err != EGL_SUCCESS)
60 qCWarning(lcQpaWayland) << "~DmaBufServerBuffer error destroying image" << m_image << "error code " << Qt::hex << err;
61 qt_server_buffer_release(m_server_buffer);
62 qt_server_buffer_destroy(m_server_buffer);
63}
64
65QOpenGLTexture *DmaBufServerBuffer::toOpenGlTexture()
66{
67 if (!QOpenGLContext::currentContext())
68 qCWarning(lcQpaWayland) <<"DmaBufServerBuffer: creating texture with no current context";
69
70 if (!m_texture) {
71 m_texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
72 m_texture->create();
73 }
74
75 m_texture->bind();
76 m_integration->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image: m_image);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
81 m_texture->release();
82 m_texture->setSize(width: m_size.width(), height: m_size.height());
83 return m_texture;
84}
85
86void DmaBufServerBufferIntegration::initializeEgl()
87{
88 if (m_egl_initialized)
89 return;
90 m_egl_initialized = true;
91
92 m_egl_display = eglGetDisplay(display_id: (EGLNativeDisplayType) m_display->wl_display());
93 if (m_egl_display == EGL_NO_DISPLAY) {
94 qCWarning(lcQpaWayland) << "Failed to initialize drm egl server buffer integration. Could not get egl display from wl_display.";
95 return;
96 }
97
98 const char *extensionString = eglQueryString(dpy: m_egl_display, EGL_EXTENSIONS);
99
100
101 if (!extensionString || !strstr(haystack: extensionString, needle: "EGL_KHR_image")) {
102 qCWarning(lcQpaWayland) << "Failed to initialize dmabuf server buffer integration. There is no EGL_KHR_image extension.\n";
103 return;
104 }
105 m_egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress(procname: "eglCreateImageKHR"));
106 m_egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress(procname: "eglDestroyImageKHR"));
107 if (!m_egl_create_image || !m_egl_destroy_image) {
108 qCWarning(lcQpaWayland) << "Failed to initialize dmabuf server buffer integration. Could not resolve eglCreateImageKHR or eglDestroyImageKHR";
109 return;
110 }
111
112 m_gl_egl_image_target_texture = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress(procname: "glEGLImageTargetTexture2DOES"));
113 if (!m_gl_egl_image_target_texture) {
114 qCWarning(lcQpaWayland) << "Failed to initialize dmabuf server buffer integration. Could not resolve glEGLImageTargetTexture2DOES";
115 return;
116 }
117}
118
119void DmaBufServerBufferIntegration::initialize(QWaylandDisplay *display)
120{
121 m_display = display;
122 display->addRegistryListener(listener: &wlDisplayHandleGlobal, data: this);
123}
124
125QWaylandServerBuffer *DmaBufServerBufferIntegration::serverBuffer(struct qt_server_buffer *buffer)
126{
127 return static_cast<QWaylandServerBuffer *>(qt_server_buffer_get_user_data(buffer));
128}
129
130void DmaBufServerBufferIntegration::wlDisplayHandleGlobal(void *data, ::wl_registry *registry, uint32_t id, const QString &interface, uint32_t version)
131{
132 Q_UNUSED(version);
133 if (interface == QStringLiteral("qt_dmabuf_server_buffer")) {
134 auto *integration = static_cast<DmaBufServerBufferIntegration *>(data);
135 integration->QtWayland::qt_dmabuf_server_buffer::init(registry, id, 1);
136 }
137}
138
139void DmaBufServerBufferIntegration::dmabuf_server_buffer_server_buffer_created(struct ::qt_server_buffer *id
140 , int32_t name
141 , int32_t width
142 , int32_t height
143 , int32_t stride
144 , int32_t offset
145 , int32_t format)
146{
147 (void) new DmaBufServerBuffer(this, id, name, width, height, stride, offset, format);
148}
149
150}
151
152QT_END_NAMESPACE
153

source code of qtwayland/src/hardwareintegration/client/dmabuf-server/dmabufserverbufferintegration.cpp