6#include <QtWaylandCompositor/QWaylandCompositor>
7#include <QtWaylandCompositor/private/qwltextureorphanage_p.h>
8#include <qpa/qplatformnativeinterface.h>
9#include <QtOpenGL/QOpenGLTexture>
10#include <QtGui/QGuiApplication>
11#include <QtGui/QOpenGLContext>
12#include <QtGui/QOffscreenSurface>
13#include <qpa/qplatformscreen.h>
14#include <QtGui/QWindow>
15#include <QtCore/QPointer>
19#include <QMutexLocker>
20#include <QVarLengthArray>
21#include <QtCore/private/qcore_unix_p.h>
22#include <QtGui/private/qeglstreamconvenience_p.h>
24#ifndef GL_TEXTURE_EXTERNAL_OES
25#define GL_TEXTURE_EXTERNAL_OES 0x8D65
28#ifndef EGL_WAYLAND_BUFFER_WL
29#define EGL_WAYLAND_BUFFER_WL 0x31D5
32#ifndef EGL_WAYLAND_EGLSTREAM_WL
33#define EGL_WAYLAND_EGLSTREAM_WL 0x334B
36#ifndef EGL_WAYLAND_PLANE_WL
37#define EGL_WAYLAND_PLANE_WL 0x31D6
40#ifndef EGL_WAYLAND_Y_INVERTED_WL
41#define EGL_WAYLAND_Y_INVERTED_WL 0x31DB
44#ifndef EGL_TEXTURE_RGB
45#define EGL_TEXTURE_RGB 0x305D
48#ifndef EGL_TEXTURE_RGBA
49#define EGL_TEXTURE_RGBA 0x305E
52#ifndef EGL_TEXTURE_EXTERNAL_WL
53#define EGL_TEXTURE_EXTERNAL_WL 0x31DA
56#ifndef EGL_TEXTURE_Y_U_V_WL
57#define EGL_TEXTURE_Y_U_V_WL 0x31D7
60#ifndef EGL_TEXTURE_Y_UV_WL
61#define EGL_TEXTURE_Y_UV_WL 0x31D8
64#ifndef EGL_TEXTURE_Y_XUXV_WL
65#define EGL_TEXTURE_Y_XUXV_WL 0x31D9
68#ifndef EGL_PLATFORM_X11_KHR
69#define EGL_PLATFORM_X11_KHR 0x31D5
75#ifndef EGL_WL_bind_wayland_display
76typedef EGLBoolean (EGLAPIENTRYP PFNEGLBINDWAYLANDDISPLAYWL) (EGLDisplay dpy,
struct wl_display *display);
77typedef EGLBoolean (EGLAPIENTRYP PFNEGLUNBINDWAYLANDDISPLAYWL) (EGLDisplay dpy,
struct wl_display *display);
82typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYIMAGEKHRPROC) (EGLDisplay dpy, EGLImageKHR image);
85#ifndef GL_OES_EGL_image
95#define MYERRCODE(x) case x: return #x;
150 bool initEglStream(WaylandEglClientBuffer *buffer,
struct ::wl_resource *bufferHandle);
172 return integration ? integration->d_ptr.data() :
nullptr;
184 if (egl_query_wayland_buffer(egl_display, buffer->waylandBufferHandle(), EGL_TEXTURE_FORMAT, &format))
185 initEglTexture(buffer, format);
193 if (!gl_egl_image_target_texture_2d)
194 gl_egl_image_target_texture_2d =
reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress(
"glEGLImageTargetTexture2DOES"));
196 if (!gl_egl_image_target_texture_2d) {
197 qCWarning(qLcWaylandCompositorHardwareIntegration)
198 <<
"BindTextureToBuffer() failed. Could not find glEGLImageTargetTexture2DOES.";
202 BufferState &state = *buffer->d;
203 state.egl_format = format;
204 state.eglMode = BufferState::ModeEGLImage;
208 EGLBoolean ret = egl_query_wayland_buffer(egl_display, buffer->waylandBufferHandle(),
EGL_WAYLAND_Y_INVERTED_WL, &isYInverted);
212 state.isYInverted = (ret == EGL_FALSE || isYInverted == EGL_TRUE);
235 for (
int i = 0; i < planes; i++) {
237#ifdef EGL_EXT_protected_content
238 if (buffer->isProtected()) {
239 attribs[2] = EGL_PROTECTED_CONTENT_EXT;
240 attribs[3] = EGL_TRUE;
241 attribs[4] = EGL_NONE;
244 EGLImageKHR image = egl_create_image(egl_display,
247 buffer->waylandBufferHandle(),
250 if (image == EGL_NO_IMAGE_KHR) {
251 qCWarning(qLcWaylandCompositorHardwareIntegration)
252 <<
"Failed to create EGL image for plane" << i;
255 state.egl_images << image;
257 QMutexLocker locker(&state.texturesLock);
258 state.textures[i] =
nullptr;
264 bool localContextNeeded =
false;
265 if (!QOpenGLContext::currentContext()) {
266 if (!localContext && QOpenGLContext::globalShareContext()) {
267 localContext =
new QOpenGLContext;
268 localContext->setShareContext(QOpenGLContext::globalShareContext());
269 localContext->create();
272 if (!offscreenSurface) {
273 offscreenSurface =
new QOffscreenSurface;
274 offscreenSurface->setFormat(localContext->format());
275 offscreenSurface->create();
277 localContext->makeCurrent(offscreenSurface);
278 localContextNeeded =
true;
281 return localContextNeeded;
287 QMutexLocker locker(&bs->texturesLock);
289 bs->textures[plane] = texture;
290 bs->texturesContext[plane] = QOpenGLContext::currentContext();
292 Q_ASSERT(bs->texturesContext[plane] !=
nullptr);
294 qCDebug(qLcWaylandCompositorHardwareIntegration)
296 <<
"(egl) creating a cleanup-lambda for QOpenGLContext::aboutToBeDestroyed!"
297 <<
", texture: " << bs->textures[plane]
298 <<
", ctx: " << (
void*)bs->texturesContext[plane];
300 bs->texturesAboutToBeDestroyedConnection[plane] =
301 QObject::connect(bs->texturesContext[plane], &QOpenGLContext::aboutToBeDestroyed,
302 bs->texturesContext[plane], [bs, plane]() {
304 QMutexLocker locker(&bs->texturesLock);
308 if (bs->textures[plane] ==
nullptr)
311 delete bs->textures[plane];
313 qCDebug(qLcWaylandCompositorHardwareIntegration)
315 <<
"texture deleted due to QOpenGLContext::aboutToBeDestroyed!"
316 <<
"Pointer (now dead) was:" << (
void*)(bs->textures[plane])
317 <<
" Associated context (about to die too) is: " << (
void*)(bs->texturesContext[plane]);
319 bs->textures[plane] =
nullptr;
320 bs->texturesContext[plane] =
nullptr;
322 QObject::disconnect(bs->texturesAboutToBeDestroyedConnection[plane]);
323 bs->texturesAboutToBeDestroyedConnection[plane] = QMetaObject::Connection();
325 }, Qt::DirectConnection);
330 BufferState &state = *buffer->d;
332 state.isYInverted =
false;
334 EGLNativeFileDescriptorKHR streamFd = EGL_NO_FILE_DESCRIPTOR_KHR;
337 state.egl_stream = funcs->create_stream_from_file_descriptor(egl_display, streamFd);
340 EGLAttrib stream_attribs[] = {
344 state.egl_stream = funcs->create_stream_attrib_nv(egl_display, stream_attribs);
347 if (state.egl_stream == EGL_NO_STREAM_KHR) {
348 qCWarning(qLcWaylandCompositorHardwareIntegration,
"%s:%d: eglCreateStreamFromFileDescriptorKHR failed: 0x%x", Q_FUNC_INFO, __LINE__, eglGetError());
351 state.eglMode = BufferState::ModeEGLStream;
353 if (!QOpenGLContext::currentContext()) {
354 qCWarning(qLcWaylandCompositorHardwareIntegration)
355 <<
"EglClientBufferIntegration: creating texture with no current context";
361 setupBufferAndCleanup(buffer->d, texture, 0);
363 qCDebug(qLcWaylandCompositorHardwareIntegration)
364 <<
" NEW texture! It's pointer and ctx pointer: "
365 << (
void*)state.textures[0] <<
"; " << (
void*)state.texturesContext[0];
369 auto newStream = funcs->stream_consumer_gltexture(egl_display, state.egl_stream);
371 EGLint code = eglGetError();
372 qCWarning(qLcWaylandCompositorHardwareIntegration) <<
"Could not initialize EGLStream:" << egl_error_string(code) << Qt::hex << (
long)code;
373 funcs->destroy_stream(egl_display, state.egl_stream);
374 state.egl_stream = EGL_NO_STREAM_KHR;
382 bool usingLocalContext = ensureContext();
384 if (buffer->d->eglMode == BufferState::ModeUninitialized) {
385 bool streamOK = initEglStream(buffer, bufferHandle);
390 BufferState &state = *buffer->d;
391 auto texture = state.textures[0];
396 funcs->query_stream(egl_display, state.egl_stream, EGL_STREAM_STATE_KHR, &stream_state);
398 if (stream_state == EGL_STREAM_STATE_NEW_FRAME_AVAILABLE_KHR) {
399 if (funcs->stream_consumer_acquire(egl_display, state.egl_stream) != EGL_TRUE)
400 qCWarning(qLcWaylandCompositorHardwareIntegration,
401 "%s:%d: eglStreamConsumerAcquireKHR failed: 0x%x", Q_FUNC_INFO, __LINE__,
405 if (usingLocalContext)
406 localContext->doneCurrent();
409WaylandEglClientBufferIntegration::WaylandEglClientBufferIntegration()
410 : d_ptr(
new WaylandEglClientBufferIntegrationPrivate)
414WaylandEglClientBufferIntegration::~WaylandEglClientBufferIntegration()
416 Q_D(WaylandEglClientBufferIntegration);
417 if (d->egl_unbind_wayland_display && d->display_bound) {
418 Q_ASSERT(d->wlDisplay);
419 if (!d->egl_unbind_wayland_display(d->egl_display, d->wlDisplay))
420 qCWarning(qLcWaylandCompositorHardwareIntegration) <<
"eglUnbindWaylandDisplayWL failed";
424void WaylandEglClientBufferIntegration::initializeHardware(
struct wl_display *display)
426 Q_D(WaylandEglClientBufferIntegration);
428 const bool ignoreBindDisplay = !qgetenv(
"QT_WAYLAND_IGNORE_BIND_DISPLAY").isEmpty();
430 QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface();
431 if (!nativeInterface) {
432 qCWarning(qLcWaylandCompositorHardwareIntegration)
433 <<
"Failed to initialize EGL display. No native platform interface available.";
437 d->egl_display = nativeInterface->nativeResourceForIntegration(
"EglDisplay");
438 if (!d->egl_display) {
439 qCWarning(qLcWaylandCompositorHardwareIntegration)
440 <<
"Failed to initialize EGL display. Could not get EglDisplay for window.";
444 const char *extensionString = eglQueryString(d->egl_display, EGL_EXTENSIONS);
445 if ((!extensionString || !strstr(extensionString,
"EGL_WL_bind_wayland_display")) && !ignoreBindDisplay) {
446 qCWarning(qLcWaylandCompositorHardwareIntegration)
447 <<
"Failed to initialize EGL display. There is no EGL_WL_bind_wayland_display extension.";
451 d->egl_bind_wayland_display =
reinterpret_cast<PFNEGLBINDWAYLANDDISPLAYWL>(eglGetProcAddress(
"eglBindWaylandDisplayWL"));
452 d->egl_unbind_wayland_display =
reinterpret_cast<PFNEGLUNBINDWAYLANDDISPLAYWL>(eglGetProcAddress(
"eglUnbindWaylandDisplayWL"));
453 if ((!d->egl_bind_wayland_display || !d->egl_unbind_wayland_display) && !ignoreBindDisplay) {
454 qCWarning(qLcWaylandCompositorHardwareIntegration)
455 <<
"Failed to initialize EGL display. Could not find eglBindWaylandDisplayWL and eglUnbindWaylandDisplayWL.";
459 d->egl_query_wayland_buffer =
reinterpret_cast<PFNEGLQUERYWAYLANDBUFFERWL_compat>(eglGetProcAddress(
"eglQueryWaylandBufferWL"));
460 if (!d->egl_query_wayland_buffer) {
461 qCWarning(qLcWaylandCompositorHardwareIntegration)
462 <<
"Failed to initialize EGL display. Could not find eglQueryWaylandBufferWL.";
466 d->egl_create_image =
reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress(
"eglCreateImageKHR"));
467 d->egl_destroy_image =
reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress(
"eglDestroyImageKHR"));
468 if (!d->egl_create_image || !d->egl_destroy_image) {
469 qCWarning(qLcWaylandCompositorHardwareIntegration)
470 <<
"Failed to initialize EGL display. Could not find eglCreateImageKHR and eglDestroyImageKHR.";
474 if (d->egl_bind_wayland_display && d->egl_unbind_wayland_display) {
475 d->display_bound = d->egl_bind_wayland_display(d->egl_display, display);
476 if (!d->display_bound)
477 qCDebug(qLcWaylandCompositorHardwareIntegration) <<
"Wayland display already bound by other client buffer integration.";
478 d->wlDisplay = display;
481 d->funcs =
new QEGLStreamConvenience;
482 d->funcs->initialize(d->egl_display);
485QtWayland::ClientBuffer *WaylandEglClientBufferIntegration::createBufferFor(wl_resource *buffer)
487 Q_D(WaylandEglClientBufferIntegration);
489 bool q = d->egl_query_wayland_buffer(d->egl_display, buffer, EGL_WIDTH, &w);
492 return new WaylandEglClientBuffer(
this, buffer);
495WaylandEglClientBuffer::WaylandEglClientBuffer(WaylandEglClientBufferIntegration *integration, wl_resource *buffer)
496 : ClientBuffer(buffer)
497 , m_integration(integration)
499 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
501 if (buffer && !wl_shm_buffer_get(buffer)) {
502 EGLint width, height;
503 p->egl_query_wayland_buffer(p->egl_display, buffer, EGL_WIDTH, &width);
504 p->egl_query_wayland_buffer(p->egl_display, buffer, EGL_HEIGHT, &height);
505 d->size = QSize(width, height);
512WaylandEglClientBuffer::~WaylandEglClientBuffer()
514 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
517 for (
auto image : d->egl_images)
518 p->egl_destroy_image(p->egl_display, image);
521 p->funcs->destroy_stream(p->egl_display, d->egl_stream);
526 QMutexLocker locker(&d->texturesLock);
528 for (
int i=0; i<3; i++) {
529 if (d->textures[i] !=
nullptr) {
531 qCDebug(qLcWaylandCompositorHardwareIntegration)
532 << Q_FUNC_INFO <<
" handing over texture!"
533 << (
void*)d->textures[i] <<
"; " << (
void*)d->texturesContext[i]
534 <<
" ... current context might be the same: " << QOpenGLContext::currentContext();
536 QtWayland::QWaylandTextureOrphanage::instance()->admitTexture(
537 d->textures[i], d->texturesContext[i]);
538 d->textures[i] =
nullptr;
539 d->texturesContext[i] =
nullptr;
540 QObject::disconnect(d->texturesAboutToBeDestroyedConnection[i]);
541 d->texturesAboutToBeDestroyedConnection[i] = QMetaObject::Connection();
552 return QWaylandBufferRef::BufferFormatEgl_RGB;
554 return QWaylandBufferRef::BufferFormatEgl_RGBA;
556 return QWaylandBufferRef::BufferFormatEgl_EXTERNAL_OES;
558 return QWaylandBufferRef::BufferFormatEgl_Y_UV;
560 return QWaylandBufferRef::BufferFormatEgl_Y_U_V;
562 return QWaylandBufferRef::BufferFormatEgl_Y_XUXV;
565 return QWaylandBufferRef::BufferFormatEgl_RGBA;
571 return QOpenGLTexture::RGBFormat;
573 return QOpenGLTexture::RGBAFormat;
575 return QOpenGLTexture::NoFormat;
579QWaylandBufferRef::BufferFormatEgl WaylandEglClientBuffer::bufferFormatEgl()
const
581 return formatFromEglFormat(d->egl_format);
584QOpenGLTexture *WaylandEglClientBuffer::toOpenGlTexture(
int plane)
586 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
588 QtWayland::QWaylandTextureOrphanage::instance()->deleteTextures();
593 auto texture = d->textures[plane];
594 if (d->eglMode == BufferState::ModeEGLStream)
600 texture =
new QOpenGLTexture(target);
601 texture->setFormat(openGLFormatFromEglFormat(d->egl_format));
602 texture->setSize(d->size.width(), d->size.height());
604 p->setupBufferAndCleanup(
this->d, texture, plane);
607 if (m_textureDirty) {
608 m_textureDirty =
false;
610 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
611 p->gl_egl_image_target_texture_2d(target, d->egl_images[plane]);
612#ifdef GL_EXT_protected_textures
614 glTexParameteri(target, GL_TEXTURE_PROTECTED_EXT, GL_TRUE);
620void WaylandEglClientBuffer::setCommitted(QRegion &damage)
622 ClientBuffer::setCommitted(damage);
623 if (d->eglMode == BufferState::ModeEGLStream || d->eglMode == BufferState::ModeUninitialized) {
624 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
625 p->handleEglstreamTexture(
this, waylandBufferHandle());
629bool WaylandEglClientBuffer::isProtected()
631 if (m_integration && m_buffer)
632 return m_integration->isProtected(m_buffer);
638QWaylandSurface::Origin WaylandEglClientBuffer::origin()
const
640 return d->isYInverted ? QWaylandSurface::OriginTopLeft : QWaylandSurface::OriginBottomLeft;
643quintptr WaylandEglClientBuffer::lockNativeBuffer()
645 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
647 if (d->egl_stream != EGL_NO_STREAM_KHR)
650 EGLImageKHR image = p->egl_create_image(p->egl_display, EGL_NO_CONTEXT,
653 return reinterpret_cast<quintptr>(image);
656void WaylandEglClientBuffer::unlockNativeBuffer(quintptr native_buffer)
const
661 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
663 EGLImageKHR image =
reinterpret_cast<EGLImageKHR>(native_buffer);
664 p->egl_destroy_image(p->egl_display, image);
667QSize WaylandEglClientBuffer::size()
const
void handleEglstreamTexture(WaylandEglClientBuffer *buffer, wl_resource *bufferHandle)
QOpenGLContext * localContext
static WaylandEglClientBufferIntegrationPrivate * get(WaylandEglClientBufferIntegration *integration)
PFNEGLCREATEIMAGEKHRPROC egl_create_image
PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC gl_egl_image_target_texture_2d
void initEglTexture(WaylandEglClientBuffer *buffer, EGLint format)
bool initEglStream(WaylandEglClientBuffer *buffer, struct ::wl_resource *bufferHandle)
PFNEGLUNBINDWAYLANDDISPLAYWL egl_unbind_wayland_display
QOffscreenSurface * offscreenSurface
void initBuffer(WaylandEglClientBuffer *buffer)
void registerBuffer(struct ::wl_resource *buffer, BufferState state)
void setupBufferAndCleanup(BufferState *bs, QOpenGLTexture *texture, int plane)
PFNEGLBINDWAYLANDDISPLAYWL egl_bind_wayland_display
WaylandEglClientBufferIntegrationPrivate()
QEGLStreamConvenience * funcs
PFNEGLQUERYWAYLANDBUFFERWL_compat egl_query_wayland_buffer
#define GL_TEXTURE_EXTERNAL_OES
QVarLengthArray< EGLImageKHR, 3 > egl_images
QMetaObject::Connection texturesAboutToBeDestroyedConnection[3]
QOpenGLContext * texturesContext[3]
QOpenGLTexture * textures[3]
static QOpenGLTexture::TextureFormat openGLFormatFromEglFormat(EGLint format)
#define EGL_TEXTURE_Y_U_V_WL
#define EGL_TEXTURE_EXTERNAL_WL
typedef void(GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)(GLenum target
struct wl_resource EGLint EGLint * value
EGLContext EGLenum EGLClientBuffer const EGLint * attrib_list
typedef EGLBoolean(EGLAPIENTRYP PFNEGLQUERYWAYLANDBUFFERWL_compat)(EGLDisplay dpy
#define EGL_WAYLAND_BUFFER_WL
#define EGL_TEXTURE_Y_XUXV_WL
#define EGL_WAYLAND_Y_INVERTED_WL
#define EGL_WAYLAND_PLANE_WL
typedef EGLImageKHR(EGLAPIENTRYP PFNEGLCREATEIMAGEKHRPROC)(EGLDisplay dpy
static QT_BEGIN_NAMESPACE const char * egl_error_string(EGLint code)
static QWaylandBufferRef::BufferFormatEgl formatFromEglFormat(EGLint format)
#define EGL_TEXTURE_Y_UV_WL
#define EGL_WAYLAND_EGLSTREAM_WL
struct wl_resource * buffer
struct wl_resource EGLint attribute