1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef DMABUFSERVERBUFFERINTEGRATION_H
5#define DMABUFSERVERBUFFERINTEGRATION_H
6
7#include <QtCore/QVariant>
8#include <QtWaylandCompositor/private/qwlserverbufferintegration_p.h>
9
10#include "qwayland-server-qt-dmabuf-server-buffer.h"
11
12#include <QtGui/QWindow>
13#include <QtGui/qpa/qplatformnativeinterface.h>
14#include <QtGui/QGuiApplication>
15
16#include <QtWaylandCompositor/qwaylandcompositor.h>
17#include <QtWaylandCompositor/private/qwayland-server-server-buffer-extension.h>
18
19#include <QtCore/QDebug>
20#include <EGL/egl.h>
21#include <EGL/eglext.h>
22
23#ifndef EGL_KHR_image
24typedef void *EGLImageKHR;
25typedef EGLImageKHR (EGLAPIENTRYP PFNEGLCREATEIMAGEKHRPROC) (EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list);
26typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYIMAGEKHRPROC) (EGLDisplay dpy, EGLImageKHR image);
27#endif
28
29#ifndef GL_OES_EGL_image
30typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
31#endif
32
33#ifndef EGL_MESA_image_dma_buf_export
34typedef EGLBoolean (EGLAPIENTRYP PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC) (EGLDisplay dpy, EGLImageKHR image, int *fourcc, int *num_planes, EGLuint64KHR *modifiers);
35typedef EGLBoolean (EGLAPIENTRYP PFNEGLEXPORTDMABUFIMAGEMESAPROC) (EGLDisplay dpy, EGLImageKHR image, int *fds, EGLint *strides, EGLint *offsets);
36#endif
37
38QT_BEGIN_NAMESPACE
39
40class DmaBufServerBufferIntegration;
41class QImage;
42
43class DmaBufServerBuffer : public QtWayland::ServerBuffer, public QtWaylandServer::qt_server_buffer
44{
45public:
46 DmaBufServerBuffer(DmaBufServerBufferIntegration *integration, const QImage &qimage, QtWayland::ServerBuffer::Format format);
47 ~DmaBufServerBuffer() override;
48
49 struct ::wl_resource *resourceForClient(struct ::wl_client *) override;
50 QOpenGLTexture *toOpenGlTexture() override;
51 bool bufferInUse() override;
52
53protected:
54 void server_buffer_release(Resource *resource) override;
55
56private:
57 DmaBufServerBufferIntegration *m_integration = nullptr;
58
59 EGLImageKHR m_image;
60
61 int32_t m_offset;
62 int32_t m_stride;
63 QOpenGLTexture *m_texture = nullptr;
64 int m_fourcc_format;
65 int m_fd;
66};
67
68class DmaBufServerBufferIntegration :
69 public QtWayland::ServerBufferIntegration,
70 public QtWaylandServer::qt_dmabuf_server_buffer
71{
72public:
73 DmaBufServerBufferIntegration();
74 ~DmaBufServerBufferIntegration() override;
75
76 bool initializeHardware(QWaylandCompositor *) override;
77
78 bool supportsFormat(QtWayland::ServerBuffer::Format format) const override;
79 QtWayland::ServerBuffer *createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format) override;
80
81 EGLDisplay display() const { return m_egl_display; }
82
83 inline EGLImageKHR eglCreateImageKHR(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list);
84 inline EGLBoolean eglDestroyImageKHR(EGLImageKHR image);
85
86 inline EGLBoolean eglExportDMABUFImageQueryMESA(EGLImageKHR image,
87 int *fourcc,
88 int *num_planes,
89 EGLuint64KHR *modifiers);
90
91 inline EGLBoolean eglExportDMABUFImageMESA(EGLImageKHR image,
92 int *fds,
93 EGLint *strides,
94 EGLint *offsets);
95
96 inline void glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image);
97
98private:
99 EGLDisplay m_egl_display;
100
101 PFNEGLEXPORTDMABUFIMAGEMESAPROC m_egl_export_dmabuf_image = nullptr;
102 PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC m_egl_export_dmabuf_image_query = nullptr;
103
104 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC m_gl_egl_image_target_texture_2d = nullptr;
105 PFNEGLCREATEIMAGEKHRPROC m_egl_create_image = nullptr;
106 PFNEGLDESTROYIMAGEKHRPROC m_egl_destroy_image = nullptr;
107};
108
109EGLImageKHR DmaBufServerBufferIntegration::eglCreateImageKHR(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
110{
111 if (!m_egl_create_image) {
112 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBufferIntegration: Trying to use unresolved function eglCreateImageKHR";
113 return EGL_NO_IMAGE_KHR;
114 }
115 return m_egl_create_image(m_egl_display, ctx, target, buffer, attrib_list);
116}
117
118EGLBoolean DmaBufServerBufferIntegration::eglDestroyImageKHR(EGLImageKHR image)
119{
120 if (!m_egl_destroy_image) {
121 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBufferIntegration: Trying to use unresolved function eglDestroyImageKHR";
122 return false;
123 }
124 return m_egl_destroy_image(m_egl_display, image);
125}
126
127EGLBoolean DmaBufServerBufferIntegration::eglExportDMABUFImageQueryMESA(EGLImageKHR image,
128 int *fourcc,
129 int *num_planes,
130 EGLuint64KHR *modifiers)
131{
132 if (m_egl_export_dmabuf_image_query)
133 return m_egl_export_dmabuf_image_query(m_egl_display, image, fourcc, num_planes, modifiers);
134 else
135 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBufferIntegration: Trying to use unresolved function eglExportDMABUFImageQueryMESA";
136 return false;
137}
138
139EGLBoolean DmaBufServerBufferIntegration::eglExportDMABUFImageMESA(EGLImageKHR image,
140 int *fds,
141 EGLint *strides,
142 EGLint *offsets)
143{
144 if (m_egl_export_dmabuf_image)
145 return m_egl_export_dmabuf_image(m_egl_display, image, fds, strides, offsets);
146 else
147 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBufferIntegration: Trying to use unresolved function eglExportDMABUFImageMESA";
148 return false;
149}
150
151void DmaBufServerBufferIntegration::glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image)
152{
153 if (m_gl_egl_image_target_texture_2d)
154 return m_gl_egl_image_target_texture_2d(target, image);
155 else
156 qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBufferIntegration: Trying to use unresolved function glEGLImageTargetTexture2DOES";
157}
158QT_END_NAMESPACE
159
160#endif
161

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