| 1 | // Copyright (C) 2018 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 |  | 
| 4 | #include "dmabufserverbufferintegration.h" | 
| 5 |  | 
| 6 | #include <QtOpenGL/QOpenGLTexture> | 
| 7 | #include <QtGui/QOpenGLContext> | 
| 8 |  | 
| 9 | #include <drm_fourcc.h> | 
| 10 | #include <unistd.h> | 
| 11 |  | 
| 12 | QT_BEGIN_NAMESPACE | 
| 13 |  | 
| 14 | DmaBufServerBuffer::DmaBufServerBuffer(DmaBufServerBufferIntegration *integration, const QImage &qimage, QtWayland::ServerBuffer::Format format) | 
| 15 |     : QtWayland::ServerBuffer(qimage.size(),format) | 
| 16 |     , m_integration(integration) | 
| 17 | { | 
| 18 |     m_format = format; | 
| 19 |  | 
| 20 |     EGLContext eglContext = eglGetCurrentContext(); | 
| 21 |  | 
| 22 |     m_texture = new QOpenGLTexture(qimage); | 
| 23 |  | 
| 24 |     m_image  = m_integration->eglCreateImageKHR(ctx: eglContext, EGL_GL_TEXTURE_2D_KHR, buffer: (EGLClientBuffer)(unsigned long)m_texture->textureId(), attrib_list: nullptr); | 
| 25 |  | 
| 26 |     qCDebug(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer created egl image"  << m_image; | 
| 27 |  | 
| 28 |     int err = eglGetError(); | 
| 29 |     if (err != EGL_SUCCESS || m_image == EGL_NO_IMAGE_KHR) | 
| 30 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer error creating EGL image"  << Qt::hex << err; | 
| 31 |  | 
| 32 |     // TODO: formats with more than one plane | 
| 33 |  | 
| 34 |     int num_planes = 1; | 
| 35 |  | 
| 36 |     if (!m_integration->eglExportDMABUFImageQueryMESA(image: m_image, fourcc: &m_fourcc_format, num_planes: &num_planes, modifiers: nullptr)) { | 
| 37 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: Failed to query egl image" ; | 
| 38 |         qCDebug(qLcWaylandCompositorHardwareIntegration) << "error"  << Qt::hex << eglGetError(); | 
| 39 |     } else { | 
| 40 |         qCDebug(qLcWaylandCompositorHardwareIntegration) << "num_planes"  << num_planes << "fourcc_format"  << m_fourcc_format; | 
| 41 |         if (num_planes != 1) { | 
| 42 |             qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: multi-plane formats not supported" ; | 
| 43 |             delete m_texture; | 
| 44 |             m_texture = nullptr; | 
| 45 |             m_integration->eglDestroyImageKHR(image: m_image); | 
| 46 |             m_image = EGL_NO_IMAGE_KHR; | 
| 47 |             return; | 
| 48 |         } | 
| 49 |     } | 
| 50 |  | 
| 51 |     if (!m_integration->eglExportDMABUFImageMESA(image: m_image, fds: &m_fd, strides: &m_stride, offsets: &m_offset)) { | 
| 52 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer: Failed to export egl image. Error code"  << Qt::hex << eglGetError(); | 
| 53 |     } else { | 
| 54 |         qCDebug(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer exported egl image: fd"  << m_fd << "stride"  << m_stride << "offset"  << m_offset; | 
| 55 |         m_texture->release(); | 
| 56 |     } | 
| 57 | } | 
| 58 |  | 
| 59 | DmaBufServerBuffer::~DmaBufServerBuffer() | 
| 60 | { | 
| 61 |     delete m_texture; | 
| 62 |  | 
| 63 |     int err; | 
| 64 |     m_integration->eglDestroyImageKHR(image: m_image); | 
| 65 |     if ((err = eglGetError()) != EGL_SUCCESS) | 
| 66 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "~DmaBufServerBuffer: eglDestroyImageKHR error"  << Qt::hex << err; | 
| 67 |  | 
| 68 |     err = ::close(fd: m_fd); | 
| 69 |     if (err) | 
| 70 |         perror(s: "~DmaBufServerBuffer:: error closing fd" ); | 
| 71 |  | 
| 72 | } | 
| 73 |  | 
| 74 | struct ::wl_resource *DmaBufServerBuffer::resourceForClient(struct ::wl_client *client) | 
| 75 | { | 
| 76 |     auto *bufferResource = resourceMap().value(client); | 
| 77 |     if (!bufferResource) { | 
| 78 |         auto integrationResource = m_integration->resourceMap().value(client); | 
| 79 |         if (!integrationResource) { | 
| 80 |             qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer::resourceForClient: Trying to get resource for ServerBuffer. But client is not bound to the qt_dmabuf_server_buffer interface" ; | 
| 81 |             return nullptr; | 
| 82 |         } | 
| 83 |         struct ::wl_resource *dmabuf_integration_resource = integrationResource->handle; | 
| 84 |  | 
| 85 |         Resource *resource = add(client, 1); | 
| 86 |         m_integration->send_server_buffer_created(dmabuf_integration_resource, resource->handle, m_fd, m_size.width(), m_size.height(), m_stride, m_offset, m_fourcc_format); | 
| 87 |         return resource->handle; | 
| 88 |     } | 
| 89 |     return bufferResource->handle; | 
| 90 | } | 
| 91 |  | 
| 92 |  | 
| 93 | QOpenGLTexture *DmaBufServerBuffer::toOpenGlTexture() | 
| 94 | { | 
| 95 |     if (!m_texture) { | 
| 96 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "DmaBufServerBuffer::toOpenGlTexture: no texture defined" ; | 
| 97 |     } | 
| 98 |     return m_texture; | 
| 99 | } | 
| 100 |  | 
| 101 | bool DmaBufServerBuffer::bufferInUse() | 
| 102 | { | 
| 103 |     return resourceMap().size() > 0; | 
| 104 | } | 
| 105 |  | 
| 106 | DmaBufServerBufferIntegration::DmaBufServerBufferIntegration() | 
| 107 | { | 
| 108 | } | 
| 109 |  | 
| 110 | DmaBufServerBufferIntegration::~DmaBufServerBufferIntegration() | 
| 111 | { | 
| 112 | } | 
| 113 |  | 
| 114 | bool DmaBufServerBufferIntegration::initializeHardware(QWaylandCompositor *compositor) | 
| 115 | { | 
| 116 |     Q_ASSERT(QGuiApplication::platformNativeInterface()); | 
| 117 |  | 
| 118 |     m_egl_display = static_cast<EGLDisplay>(QGuiApplication::platformNativeInterface()->nativeResourceForIntegration(resource: "egldisplay" )); | 
| 119 |     if (!m_egl_display) { | 
| 120 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "Cannot initialize dmabuf server buffer integration. Missing egl display from platform plugin" ; | 
| 121 |         return false; | 
| 122 |     } | 
| 123 |  | 
| 124 |     const char *extensionString = eglQueryString(dpy: m_egl_display, EGL_EXTENSIONS); | 
| 125 |     if (!extensionString || !strstr(haystack: extensionString, needle: "EGL_KHR_image" )) { | 
| 126 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. There is no EGL_KHR_image extension." ; | 
| 127 |         return false; | 
| 128 |     } | 
| 129 |  | 
| 130 |     m_egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress(procname: "eglCreateImageKHR" )); | 
| 131 |     m_egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress(procname: "eglDestroyImageKHR" )); | 
| 132 |     if (!m_egl_create_image || !m_egl_destroy_image) { | 
| 133 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not resolve eglCreateImageKHR or eglDestroyImageKHR" ; | 
| 134 |         return false; | 
| 135 |     } | 
| 136 |  | 
| 137 |     m_gl_egl_image_target_texture_2d = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress(procname: "glEGLImageTargetTexture2DOES" )); | 
| 138 |     if (!m_gl_egl_image_target_texture_2d) { | 
| 139 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find glEGLImageTargetTexture2DOES." ; | 
| 140 |         return false; | 
| 141 |     } | 
| 142 |  | 
| 143 |     m_egl_export_dmabuf_image_query = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC>(eglGetProcAddress(procname: "eglExportDMABUFImageQueryMESA" )); | 
| 144 |     if (!m_egl_export_dmabuf_image_query) { | 
| 145 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find eglExportDMABUFImageQueryMESA." ; | 
| 146 |         return false; | 
| 147 |     } | 
| 148 |  | 
| 149 |     m_egl_export_dmabuf_image = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEMESAPROC>(eglGetProcAddress(procname: "eglExportDMABUFImageMESA" )); | 
| 150 |     if (!m_egl_export_dmabuf_image) { | 
| 151 |         qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize dmabuf server buffer integration. Could not find eglExportDMABUFImageMESA." ; | 
| 152 |         return false; | 
| 153 |     } | 
| 154 |  | 
| 155 |     QtWaylandServer::qt_dmabuf_server_buffer::init(compositor->display(), 1); | 
| 156 |     return true; | 
| 157 | } | 
| 158 |  | 
| 159 | bool DmaBufServerBufferIntegration::supportsFormat(QtWayland::ServerBuffer::Format format) const | 
| 160 | { | 
| 161 |     // TODO: 8-bit format support | 
| 162 |     switch (format) { | 
| 163 |     case QtWayland::ServerBuffer::RGBA32: | 
| 164 |         return true; | 
| 165 |     case QtWayland::ServerBuffer::A8: | 
| 166 |         return false; | 
| 167 |     default: | 
| 168 |         return false; | 
| 169 |     } | 
| 170 | } | 
| 171 |  | 
| 172 | QtWayland::ServerBuffer *DmaBufServerBufferIntegration::createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format) | 
| 173 | { | 
| 174 |     return new DmaBufServerBuffer(this, qimage, format); | 
| 175 | } | 
| 176 |  | 
| 177 | void DmaBufServerBuffer::server_buffer_release(Resource *resource) | 
| 178 | { | 
| 179 |     qCDebug(qLcWaylandCompositorHardwareIntegration) << "server_buffer RELEASE resource"  << resource->handle << wl_resource_get_id(resource->handle) << "for client"  << resource->client(); | 
| 180 |     wl_resource_destroy(resource->handle); | 
| 181 | } | 
| 182 |  | 
| 183 | QT_END_NAMESPACE | 
| 184 |  |