| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef DRMEGLSERVERBUFFERINTEGRATION_H |
| 5 | #define DRMEGLSERVERBUFFERINTEGRATION_H |
| 6 | |
| 7 | #include <QtCore/QVariant> |
| 8 | #include <QtWaylandCompositor/private/qwlserverbufferintegration_p.h> |
| 9 | |
| 10 | #include "qwayland-server-drm-egl-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 |
| 24 | typedef void *EGLImageKHR; |
| 25 | typedef EGLImageKHR (EGLAPIENTRYP PFNEGLCREATEIMAGEKHRPROC) (EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list); |
| 26 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYIMAGEKHRPROC) (EGLDisplay dpy, EGLImageKHR image); |
| 27 | #endif |
| 28 | |
| 29 | #ifndef GL_OES_EGL_image |
| 30 | typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image); |
| 31 | #endif |
| 32 | #ifndef EGL_MESA_drm_image |
| 33 | typedef EGLImageKHR (EGLAPIENTRYP PFNEGLCREATEDRMIMAGEMESAPROC) (EGLDisplay dpy, const EGLint *attrib_list); |
| 34 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLEXPORTDRMIMAGEMESAPROC) (EGLDisplay dpy, EGLImageKHR image, EGLint *name, EGLint *handle, EGLint *stride); |
| 35 | #endif |
| 36 | |
| 37 | QT_BEGIN_NAMESPACE |
| 38 | |
| 39 | class DrmEglServerBufferIntegration; |
| 40 | class QImage; |
| 41 | |
| 42 | class DrmEglServerBuffer : public QtWayland::ServerBuffer, public QtWaylandServer::qt_server_buffer |
| 43 | { |
| 44 | public: |
| 45 | DrmEglServerBuffer(DrmEglServerBufferIntegration *integration, const QImage &qimage, QtWayland::ServerBuffer::Format format); |
| 46 | |
| 47 | struct ::wl_resource *resourceForClient(struct ::wl_client *) override; |
| 48 | bool bufferInUse() override; |
| 49 | QOpenGLTexture *toOpenGlTexture() override; |
| 50 | |
| 51 | private: |
| 52 | DrmEglServerBufferIntegration *m_integration = nullptr; |
| 53 | |
| 54 | EGLImageKHR m_image; |
| 55 | |
| 56 | int32_t m_name; |
| 57 | int32_t m_stride; |
| 58 | QOpenGLTexture *m_texture = nullptr; |
| 59 | QtWaylandServer::qt_drm_egl_server_buffer::format m_drm_format; |
| 60 | }; |
| 61 | |
| 62 | class DrmEglServerBufferIntegration : |
| 63 | public QtWayland::ServerBufferIntegration, |
| 64 | public QtWaylandServer::qt_drm_egl_server_buffer |
| 65 | { |
| 66 | public: |
| 67 | DrmEglServerBufferIntegration(); |
| 68 | ~DrmEglServerBufferIntegration() override; |
| 69 | |
| 70 | bool initializeHardware(QWaylandCompositor *) override; |
| 71 | |
| 72 | bool supportsFormat(QtWayland::ServerBuffer::Format format) const override; |
| 73 | QtWayland::ServerBuffer *createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format) override; |
| 74 | |
| 75 | EGLDisplay display() const { return m_egl_display; } |
| 76 | |
| 77 | inline EGLImageKHR eglCreateImageKHR(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list); |
| 78 | inline EGLBoolean eglDestroyImageKHR (EGLImageKHR image); |
| 79 | inline EGLImageKHR eglCreateDRMImageMESA (const EGLint *attrib_list); |
| 80 | inline EGLBoolean eglExportDRMImageMESA (EGLImageKHR image, EGLint *name, EGLint *handle, EGLint *stride); |
| 81 | inline void glEGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image); |
| 82 | |
| 83 | private: |
| 84 | EGLDisplay m_egl_display = EGL_NO_DISPLAY; |
| 85 | PFNEGLCREATEDRMIMAGEMESAPROC m_egl_create_drm_image; |
| 86 | PFNEGLEXPORTDRMIMAGEMESAPROC m_egl_export_drm_image; |
| 87 | PFNGLEGLIMAGETARGETTEXTURE2DOESPROC m_gl_egl_image_target_texture_2d; |
| 88 | |
| 89 | PFNEGLCREATEIMAGEKHRPROC m_egl_create_image; |
| 90 | PFNEGLDESTROYIMAGEKHRPROC m_egl_destroy_image; |
| 91 | }; |
| 92 | |
| 93 | EGLImageKHR DrmEglServerBufferIntegration::eglCreateImageKHR(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list) |
| 94 | { |
| 95 | if (!m_egl_create_image) { |
| 96 | qWarning(msg: "DrmEglServerBufferIntegration: Trying to used unresolved function eglCreateImageKHR" ); |
| 97 | return EGL_NO_IMAGE_KHR; |
| 98 | } |
| 99 | return m_egl_create_image(m_egl_display, ctx, target, buffer,attrib_list); |
| 100 | } |
| 101 | |
| 102 | EGLBoolean DrmEglServerBufferIntegration::eglDestroyImageKHR (EGLImageKHR image) |
| 103 | { |
| 104 | if (!m_egl_destroy_image) { |
| 105 | qWarning(msg: "DrmEglServerBufferIntegration: Trying to use unresolved function eglDestroyImageKHR" ); |
| 106 | return false; |
| 107 | } |
| 108 | return m_egl_destroy_image(m_egl_display, image); |
| 109 | } |
| 110 | |
| 111 | EGLImageKHR DrmEglServerBufferIntegration::eglCreateDRMImageMESA (const EGLint *attrib_list) |
| 112 | { |
| 113 | if (m_egl_create_drm_image) |
| 114 | return m_egl_create_drm_image(m_egl_display, attrib_list); |
| 115 | else |
| 116 | qWarning(msg: "DrmEglServerBufferIntegration: Trying to use unresolved function eglCreateDRMImageMESA" ); |
| 117 | return EGL_NO_IMAGE_KHR; |
| 118 | |
| 119 | } |
| 120 | |
| 121 | EGLBoolean DrmEglServerBufferIntegration::eglExportDRMImageMESA (EGLImageKHR image, EGLint *name, EGLint *handle, EGLint *stride) |
| 122 | { |
| 123 | if (m_egl_export_drm_image) |
| 124 | return m_egl_export_drm_image(m_egl_display, image, name, handle, stride); |
| 125 | else |
| 126 | qWarning(msg: "DrmEglServerBufferIntegration: Trying to use unresolved function eglExportDRMImageMESA" ); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void DrmEglServerBufferIntegration::glEGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image) |
| 131 | { |
| 132 | if (m_gl_egl_image_target_texture_2d) |
| 133 | return m_gl_egl_image_target_texture_2d(target, image); |
| 134 | else |
| 135 | qWarning(msg: "DrmEglServerBufferIntegration: Trying to use unresolved function glEGLImageTargetTexture2DOES" ); |
| 136 | } |
| 137 | QT_END_NAMESPACE |
| 138 | |
| 139 | #endif |
| 140 | |