1// Copyright (C) 2016 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 "drmeglserverbufferintegration.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
12QT_BEGIN_NAMESPACE
13
14namespace QtWaylandClient {
15
16DrmServerBuffer::DrmServerBuffer(DrmEglServerBufferIntegration *integration
17 , int32_t name
18 , int32_t width
19 , int32_t height
20 , int32_t stride
21 , int32_t format)
22 : m_integration(integration)
23{
24 m_size = QSize(width, height);
25 EGLint egl_format;
26 int32_t format_stride;
27 switch (format) {
28 case QtWayland::qt_drm_egl_server_buffer::format_RGBA32:
29 m_format = QWaylandServerBuffer::RGBA32;
30 egl_format = EGL_DRM_BUFFER_FORMAT_ARGB32_MESA;
31 format_stride = stride / 4;
32 break;
33#ifdef EGL_DRM_BUFFER_FORMAT_A8_MESA
34 case QtWayland::qt_drm_egl_server_buffer::format_A8:
35 m_format = QWaylandServerBuffer::A8;
36 egl_format = EGL_DRM_BUFFER_FORMAT_A8_MESA;
37 format_stride = stride;
38 break;
39#endif
40 default:
41 qWarning(msg: "DrmServerBuffer: unknown format");
42 m_format = QWaylandServerBuffer::RGBA32;
43 egl_format = EGL_DRM_BUFFER_FORMAT_ARGB32_MESA;
44 format_stride = stride / 4;
45 break;
46 }
47 EGLint attribs[] = {
48 EGL_WIDTH, width,
49 EGL_HEIGHT, height,
50 EGL_DRM_BUFFER_STRIDE_MESA, format_stride,
51 EGL_DRM_BUFFER_FORMAT_MESA, egl_format,
52 EGL_NONE
53 };
54
55 qintptr name_pointer = name;
56 m_image = integration->eglCreateImageKHR(EGL_NO_CONTEXT, EGL_DRM_BUFFER_MESA, buffer: (EGLClientBuffer) name_pointer, attrib_list: attribs);
57
58}
59
60DrmServerBuffer::~DrmServerBuffer()
61{
62 m_integration->eglDestroyImageKHR(image: m_image);
63}
64
65QOpenGLTexture *DrmServerBuffer::toOpenGlTexture()
66{
67 if (!QOpenGLContext::currentContext())
68 qWarning(msg: "DrmServerBuffer: 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 DrmEglServerBufferIntegration::initializeEgl()
87{
88 if (m_egl_initialized)
89 return;
90 m_egl_initialized = true;
91
92#if QT_CONFIG(egl_extension_platform_wayland)
93 m_egl_display = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_EXT, native_display: m_display->wl_display(), attrib_list: nullptr);
94#else
95 m_egl_display = eglGetDisplay((EGLNativeDisplayType) m_display->wl_display());
96#endif
97 if (m_egl_display == EGL_NO_DISPLAY) {
98 qWarning(msg: "Failed to initialize drm egl server buffer integration. Could not get egl display from wl_display.");
99 return;
100 }
101
102 const char *extensionString = eglQueryString(dpy: m_egl_display, EGL_EXTENSIONS);
103 if (!extensionString || !strstr(haystack: extensionString, needle: "EGL_KHR_image")) {
104 qWarning(msg: "Failed to initialize drm egl server buffer integration. There is no EGL_KHR_image extension.\n");
105 return;
106 }
107 m_egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress(procname: "eglCreateImageKHR"));
108 m_egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress(procname: "eglDestroyImageKHR"));
109 if (!m_egl_create_image || !m_egl_destroy_image) {
110 qWarning(msg: "Failed to initialize drm egl server buffer integration. Could not resolve eglCreateImageKHR or eglDestroyImageKHR");
111 return;
112 }
113
114 m_gl_egl_image_target_texture = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress(procname: "glEGLImageTargetTexture2DOES"));
115 if (!m_gl_egl_image_target_texture) {
116 qWarning(msg: "Failed to initialize drm egl server buffer integration. Could not resolve glEGLImageTargetTexture2DOES");
117 return;
118 }
119 m_egl_initialized = true;
120}
121
122void DrmEglServerBufferIntegration::initialize(QWaylandDisplay *display)
123{
124 m_display = display;
125 display->addRegistryListener(listener: &wlDisplayHandleGlobal, data: this);
126}
127
128QWaylandServerBuffer *DrmEglServerBufferIntegration::serverBuffer(struct qt_server_buffer *buffer)
129{
130 return static_cast<QWaylandServerBuffer *>(qt_server_buffer_get_user_data(buffer));
131}
132
133void DrmEglServerBufferIntegration::wlDisplayHandleGlobal(void *data, ::wl_registry *registry, uint32_t id, const QString &interface, uint32_t version)
134{
135 Q_UNUSED(version);
136 if (interface == QStringLiteral("qt_drm_egl_server_buffer")) {
137 auto *integration = static_cast<DrmEglServerBufferIntegration *>(data);
138 integration->QtWayland::qt_drm_egl_server_buffer::init(registry, id, 1);
139 }
140}
141
142void DrmEglServerBufferIntegration::drm_egl_server_buffer_server_buffer_created(struct ::qt_server_buffer *id
143 , int32_t name
144 , int32_t width
145 , int32_t height
146 , int32_t stride
147 , int32_t format)
148{
149 DrmServerBuffer *server_buffer = new DrmServerBuffer(this, name, width, height, stride, format);
150 qt_server_buffer_set_user_data(id, server_buffer);
151}
152
153}
154
155QT_END_NAMESPACE
156

source code of qtwayland/src/hardwareintegration/client/drm-egl-server/drmeglserverbufferintegration.cpp