| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the plugins of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU Lesser General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 |
** packaging of this file. Please review the following information to |
| 22 |
** ensure the GNU Lesser General Public License version 3 requirements |
| 23 |
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 |
** |
| 25 |
** GNU General Public License Usage |
| 26 |
** Alternatively, this file may be used under the terms of the GNU |
| 27 |
** General Public License version 2.0 or (at your option) the GNU General |
| 28 |
** Public license version 3 or any later version approved by the KDE Free |
| 29 |
** Qt Foundation. The licenses are as published by the Free Software |
| 30 |
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 |
** included in the packaging of this file. Please review the following |
| 32 |
** information to ensure the GNU General Public License requirements will |
| 33 |
** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 |
** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 |
** |
| 36 |
** $QT_END_LICENSE$ |
| 37 |
** |
| 38 |
****************************************************************************/ |
| 39 |
|
| 40 |
#include "dmabufserverbufferintegration.h" |
| 41 |
#include <QtWaylandClient/private/qwaylanddisplay_p.h> |
| 42 |
#include <QDebug> |
| 43 |
#include <QtGui/QOpenGLContext> |
| 44 |
#include <QtGui/QOpenGLTexture> |
| 45 |
|
| 46 |
#include <EGL/egl.h> |
| 47 |
#include <EGL/eglext.h> |
| 48 |
|
| 49 |
QT_BEGIN_NAMESPACE |
| 50 |
|
| 51 |
namespace QtWaylandClient { |
| 52 |
|
| 53 |
DmaBufServerBuffer::DmaBufServerBuffer(DmaBufServerBufferIntegration *integration |
| 54 |
, struct ::qt_server_buffer *id |
| 55 |
, int32_t fd |
| 56 |
, int32_t width |
| 57 |
, int32_t height |
| 58 |
, int32_t stride |
| 59 |
, int32_t offset |
| 60 |
, int32_t fourcc_format) |
| 61 |
: m_integration(integration) |
| 62 |
, m_server_buffer(id) |
| 63 |
{ |
| 64 |
m_size = QSize(width, height); |
| 65 |
|
| 66 |
|
| 67 |
EGLint import_attribs[] = { |
| 68 |
EGL_WIDTH, width, |
| 69 |
EGL_HEIGHT, height, |
| 70 |
EGL_LINUX_DRM_FOURCC_EXT, fourcc_format, |
| 71 |
EGL_DMA_BUF_PLANE0_FD_EXT, fd, |
| 72 |
EGL_DMA_BUF_PLANE0_OFFSET_EXT, offset, |
| 73 |
EGL_DMA_BUF_PLANE0_PITCH_EXT, stride, |
| 74 |
EGL_NONE |
| 75 |
}; |
| 76 |
|
| 77 |
m_image = integration->eglCreateImageKHR( |
| 78 |
EGL_NO_CONTEXT, |
| 79 |
EGL_LINUX_DMA_BUF_EXT, |
| 80 |
(EGLClientBuffer)nullptr, |
| 81 |
import_attribs); |
| 82 |
|
| 83 |
int err = eglGetError(); |
| 84 |
qCDebug(lcQpaWayland) << "imported egl image" << m_image; |
| 85 |
if (m_image == EGL_NO_IMAGE_KHR || err != EGL_SUCCESS) |
| 86 |
qCWarning(lcQpaWayland) << "DmaBufServerBuffer error importing image. EGL error code" << Qt::hex << err; |
| 87 |
|
| 88 |
qt_server_buffer_set_user_data(id, this); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
DmaBufServerBuffer::~DmaBufServerBuffer() |
| 93 |
{ |
| 94 |
int err = m_integration->eglDestroyImageKHR(m_image); |
| 95 |
if (err != EGL_SUCCESS) |
| 96 |
qCWarning(lcQpaWayland) << "~DmaBufServerBuffer error destroying image" << m_image << "error code " << Qt::hex << err; |
| 97 |
qt_server_buffer_release(m_server_buffer); |
| 98 |
qt_server_buffer_destroy(m_server_buffer); |
| 99 |
} |
| 100 |
|
| 101 |
QOpenGLTexture *DmaBufServerBuffer::toOpenGlTexture() |
| 102 |
{ |
| 103 |
if (!QOpenGLContext::currentContext()) |
| 104 |
qCWarning(lcQpaWayland) <<"DmaBufServerBuffer: creating texture with no current context"; |
| 105 |
|
| 106 |
if (!m_texture) { |
| 107 |
m_texture = new QOpenGLTexture(QOpenGLTexture::Target2D); |
| 108 |
m_texture->create(); |
| 109 |
} |
| 110 |
|
| 111 |
m_texture->bind(); |
| 112 |
m_integration->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_image); |
| 113 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 114 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 115 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 116 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 117 |
m_texture->release(); |
| 118 |
m_texture->setSize(m_size.width(), m_size.height()); |
| 119 |
return m_texture; |
| 120 |
} |
| 121 |
|
| 122 |
void DmaBufServerBufferIntegration::initializeEgl() |
| 123 |
{ |
| 124 |
if (m_egl_initialized) |
| 125 |
return; |
| 126 |
m_egl_initialized = true; |
| 127 |
|
| 128 |
m_egl_display = eglGetDisplay((EGLNativeDisplayType) m_display->wl_display()); |
| 129 |
if (m_egl_display == EGL_NO_DISPLAY) { |
| 130 |
qCWarning(lcQpaWayland) << "Failed to initialize drm egl server buffer integration. Could not get egl display from wl_display."; |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
const char *extensionString = eglQueryString(m_egl_display, EGL_EXTENSIONS); |
| 135 |
|
| 136 |
|
| 137 |
if (!extensionString || !strstr(extensionString, "EGL_KHR_image")) { |
| 138 |
qCWarning(lcQpaWayland) << "Failed to initialize dmabuf server buffer integration. There is no EGL_KHR_image extension.\n"; |
| 139 |
return; |
| 140 |
} |
| 141 |
m_egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR")); |
| 142 |
m_egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR")); |
| 143 |
if (!m_egl_create_image || !m_egl_destroy_image) { |
| 144 |
qCWarning(lcQpaWayland) << "Failed to initialize dmabuf server buffer integration. Could not resolve eglCreateImageKHR or eglDestroyImageKHR"; |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
m_gl_egl_image_target_texture = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES")); |
| 149 |
if (!m_gl_egl_image_target_texture) { |
| 150 |
qCWarning(lcQpaWayland) << "Failed to initialize dmabuf server buffer integration. Could not resolve glEGLImageTargetTexture2DOES"; |
| 151 |
return; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void DmaBufServerBufferIntegration::initialize(QWaylandDisplay *display) |
| 156 |
{ |
| 157 |
m_display = display; |
| 158 |
display->addRegistryListener(&wlDisplayHandleGlobal, this); |
| 159 |
} |
| 160 |
|
| 161 |
QWaylandServerBuffer *DmaBufServerBufferIntegration::serverBuffer(struct qt_server_buffer *buffer) |
| 162 |
{ |
| 163 |
return static_cast<QWaylandServerBuffer *>(qt_server_buffer_get_user_data(buffer)); |
| 164 |
} |
| 165 |
|
| 166 |
void DmaBufServerBufferIntegration::wlDisplayHandleGlobal(void *data, ::wl_registry *registry, uint32_t id, const QString &interface, uint32_t version) |
| 167 |
{ |
| 168 |
Q_UNUSED(version); |
| 169 |
if (interface == QStringLiteral("qt_dmabuf_server_buffer")) { |
| 170 |
auto *integration = static_cast<DmaBufServerBufferIntegration *>(data); |
| 171 |
integration->QtWayland::qt_dmabuf_server_buffer::init(registry, id, 1); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void DmaBufServerBufferIntegration::dmabuf_server_buffer_server_buffer_created(struct ::qt_server_buffer *id |
| 176 |
, int32_t name |
| 177 |
, int32_t width |
| 178 |
, int32_t height |
| 179 |
, int32_t stride |
| 180 |
, int32_t offset |
| 181 |
, int32_t format) |
| 182 |
{ |
| 183 |
(void) new DmaBufServerBuffer(this, id, name, width, height, stride, offset, format); |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
QT_END_NAMESPACE |