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#ifndef DMABUFSERVERBUFFERINTEGRATION_H
5#define DMABUFSERVERBUFFERINTEGRATION_H
6
7#include <QtCore/QVariant>
8#include <QtWaylandClient/private/qwayland-wayland.h>
9#include "qwayland-qt-dmabuf-server-buffer.h"
10#include <QtWaylandClient/private/qwaylandserverbufferintegration_p.h>
11
12#include "dmabufserverbufferintegration.h"
13#include <QtWaylandClient/private/qwaylanddisplay_p.h>
14#include <QtCore/QTextStream>
15
16#include <EGL/egl.h>
17#include <EGL/eglext.h>
18#ifndef EGL_KHR_image
19typedef void *EGLImageKHR;
20typedef EGLImageKHR (EGLAPIENTRYP PFNEGLCREATEIMAGEKHRPROC) (EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list);
21typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYIMAGEKHRPROC) (EGLDisplay dpy, EGLImageKHR image);
22#endif
23
24#ifndef GL_OES_EGL_image
25typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
26#endif
27
28QT_BEGIN_NAMESPACE
29
30namespace QtWaylandClient {
31
32class DmaBufServerBufferIntegration;
33
34class DmaBufServerBuffer : public QWaylandServerBuffer
35{
36public:
37 DmaBufServerBuffer(DmaBufServerBufferIntegration *integration, struct ::qt_server_buffer *id, int32_t fd,
38 int32_t width, int32_t height, int32_t stride, int32_t offset, int32_t fourcc_format);
39 ~DmaBufServerBuffer() override;
40 QOpenGLTexture* toOpenGlTexture() override;
41private:
42 DmaBufServerBufferIntegration *m_integration = nullptr;
43 EGLImageKHR m_image = EGL_NO_IMAGE_KHR;
44 QOpenGLTexture *m_texture = nullptr;
45 struct ::qt_server_buffer *m_server_buffer = nullptr;
46};
47
48class DmaBufServerBufferIntegration
49 : public QWaylandServerBufferIntegration
50 , public QtWayland::qt_dmabuf_server_buffer
51{
52public:
53 void initialize(QWaylandDisplay *display) override;
54
55 QWaylandServerBuffer *serverBuffer(struct qt_server_buffer *buffer) override;
56
57 inline EGLImageKHR eglCreateImageKHR(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list);
58 inline EGLBoolean eglDestroyImageKHR(EGLImageKHR image);
59 inline void glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image);
60protected:
61 void dmabuf_server_buffer_server_buffer_created(struct ::qt_server_buffer *id, int32_t fd,
62 int32_t width, int32_t height, int32_t stride,
63 int32_t offset, int32_t fourcc_format) override;
64
65private:
66 static void wlDisplayHandleGlobal(void *data, struct ::wl_registry *registry, uint32_t id,
67 const QString &interface, uint32_t version);
68 void initializeEgl();
69
70 PFNEGLCREATEIMAGEKHRPROC m_egl_create_image = nullptr;
71 PFNEGLDESTROYIMAGEKHRPROC m_egl_destroy_image = nullptr;
72 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC m_gl_egl_image_target_texture = nullptr;
73 QWaylandDisplay *m_display = nullptr;
74 EGLDisplay m_egl_display = EGL_NO_DISPLAY;
75 bool m_egl_initialized = false;
76};
77
78EGLImageKHR DmaBufServerBufferIntegration::eglCreateImageKHR(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
79{
80 if (!m_egl_initialized)
81 initializeEgl();
82 if (!m_egl_create_image) {
83 qCWarning(lcQpaWayland) << "DmaBufServerBufferIntegration: Trying to use unresolved function eglCreateImageKHR";
84 return EGL_NO_IMAGE_KHR;
85 }
86 return m_egl_create_image(m_egl_display, ctx, target, buffer, attrib_list);
87}
88
89EGLBoolean DmaBufServerBufferIntegration::eglDestroyImageKHR(EGLImageKHR image)
90{
91 if (!m_egl_destroy_image) {
92 qCWarning(lcQpaWayland) << "DmaBufServerBufferIntegration: Trying to use unresolved function eglDestroyImageKHR";
93 return false;
94 }
95 return m_egl_destroy_image(m_egl_display, image);
96}
97
98void DmaBufServerBufferIntegration::glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image)
99{
100 if (!m_gl_egl_image_target_texture) {
101 qCWarning(lcQpaWayland) << "DmaBufServerBufferIntegration: Trying to use unresolved function glEGLImageTargetTexture2DOES";
102 return;
103 }
104 m_gl_egl_image_target_texture(target, image);
105}
106
107}
108
109QT_END_NAMESPACE
110
111#endif
112

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