7#include <QtWaylandCompositor/QWaylandCompositor>
8#include <QtWaylandCompositor/private/qwltextureorphanage_p.h>
9#include <qpa/qplatformnativeinterface.h>
10#include <QtOpenGL/QOpenGLTexture>
11#include <QtGui/QGuiApplication>
12#include <QtGui/QOpenGLContext>
13#include <QtGui/QOffscreenSurface>
14#include <qpa/qplatformscreen.h>
15#include <QtGui/QWindow>
16#include <QtCore/QPointer>
20#include <QMutexLocker>
21#include <QVarLengthArray>
22#include <QtCore/private/qcore_unix_p.h>
23#include <QtGui/private/qeglstreamconvenience_p.h>
25#ifndef GL_TEXTURE_EXTERNAL_OES
26#define GL_TEXTURE_EXTERNAL_OES 0x8D65
29#ifndef EGL_WAYLAND_BUFFER_WL
30#define EGL_WAYLAND_BUFFER_WL 0x31D5
33#ifndef EGL_WAYLAND_EGLSTREAM_WL
34#define EGL_WAYLAND_EGLSTREAM_WL 0x334B
37#ifndef EGL_WAYLAND_PLANE_WL
38#define EGL_WAYLAND_PLANE_WL 0x31D6
41#ifndef EGL_WAYLAND_Y_INVERTED_WL
42#define EGL_WAYLAND_Y_INVERTED_WL 0x31DB
45#ifndef EGL_TEXTURE_RGB
46#define EGL_TEXTURE_RGB 0x305D
49#ifndef EGL_TEXTURE_RGBA
50#define EGL_TEXTURE_RGBA 0x305E
53#ifndef EGL_TEXTURE_EXTERNAL_WL
54#define EGL_TEXTURE_EXTERNAL_WL 0x31DA
57#ifndef EGL_TEXTURE_Y_U_V_WL
58#define EGL_TEXTURE_Y_U_V_WL 0x31D7
61#ifndef EGL_TEXTURE_Y_UV_WL
62#define EGL_TEXTURE_Y_UV_WL 0x31D8
65#ifndef EGL_TEXTURE_Y_XUXV_WL
66#define EGL_TEXTURE_Y_XUXV_WL 0x31D9
69#ifndef EGL_PLATFORM_X11_KHR
70#define EGL_PLATFORM_X11_KHR 0x31D5
76#ifndef EGL_WL_bind_wayland_display
77typedef EGLBoolean (EGLAPIENTRYP PFNEGLBINDWAYLANDDISPLAYWL) (EGLDisplay dpy,
struct wl_display *display);
78typedef EGLBoolean (EGLAPIENTRYP PFNEGLUNBINDWAYLANDDISPLAYWL) (EGLDisplay dpy,
struct wl_display *display);
83typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYIMAGEKHRPROC) (EGLDisplay dpy, EGLImageKHR image);
86#ifndef GL_OES_EGL_image
96#define MYERRCODE(x) case x: return #x;
151 bool initEglStream(WaylandEglClientBuffer *buffer,
struct ::wl_resource *bufferHandle);
173 return integration ? integration->d_ptr.data() :
nullptr;
185 if (egl_query_wayland_buffer(egl_display, buffer->waylandBufferHandle(), EGL_TEXTURE_FORMAT, &format))
186 initEglTexture(buffer, format);
194 if (!gl_egl_image_target_texture_2d)
195 gl_egl_image_target_texture_2d =
reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress(
"glEGLImageTargetTexture2DOES"));
197 if (!gl_egl_image_target_texture_2d) {
198 qCWarning(qLcWaylandCompositorHardwareIntegration)
199 <<
"BindTextureToBuffer() failed. Could not find glEGLImageTargetTexture2DOES.";
203 BufferState &state = *buffer->d;
204 state.egl_format = format;
205 state.eglMode = BufferState::ModeEGLImage;
209 EGLBoolean ret = egl_query_wayland_buffer(egl_display, buffer->waylandBufferHandle(),
EGL_WAYLAND_Y_INVERTED_WL, &isYInverted);
213 state.isYInverted = (ret == EGL_FALSE || isYInverted == EGL_TRUE);
236 for (
int i = 0; i < planes; i++) {
238#ifdef EGL_EXT_protected_content
239 if (buffer->isProtected()) {
240 attribs[2] = EGL_PROTECTED_CONTENT_EXT;
241 attribs[3] = EGL_TRUE;
242 attribs[4] = EGL_NONE;
245 EGLImageKHR image = egl_create_image(egl_display,
248 buffer->waylandBufferHandle(),
251 if (image == EGL_NO_IMAGE_KHR) {
252 qCWarning(qLcWaylandCompositorHardwareIntegration)
253 <<
"Failed to create EGL image for plane" << i;
256 state.egl_images << image;
258 QMutexLocker locker(&state.texturesLock);
259 state.textures[i] =
nullptr;
265 bool localContextNeeded =
false;
266 if (!QOpenGLContext::currentContext()) {
267 if (!localContext && QOpenGLContext::globalShareContext()) {
268 localContext =
new QOpenGLContext;
269 localContext->setShareContext(QOpenGLContext::globalShareContext());
270 localContext->create();
273 if (!offscreenSurface) {
274 offscreenSurface =
new QOffscreenSurface;
275 offscreenSurface->setFormat(localContext->format());
276 offscreenSurface->create();
278 localContext->makeCurrent(offscreenSurface);
279 localContextNeeded =
true;
282 return localContextNeeded;
288 QMutexLocker locker(&bs->texturesLock);
290 bs->textures[plane] = texture;
291 bs->texturesContext[plane] = QOpenGLContext::currentContext();
293 Q_ASSERT(bs->texturesContext[plane] !=
nullptr);
295 qCDebug(qLcWaylandCompositorHardwareIntegration)
297 <<
"(egl) creating a cleanup-lambda for QOpenGLContext::aboutToBeDestroyed!"
298 <<
", texture: " << bs->textures[plane]
299 <<
", ctx: " << (
void*)bs->texturesContext[plane];
301 bs->texturesAboutToBeDestroyedConnection[plane] =
302 QObject::connect(bs->texturesContext[plane], &QOpenGLContext::aboutToBeDestroyed,
303 bs->texturesContext[plane], [bs, plane]() {
305 QMutexLocker locker(&bs->texturesLock);
309 if (bs->textures[plane] ==
nullptr)
312 delete bs->textures[plane];
314 qCDebug(qLcWaylandCompositorHardwareIntegration)
316 <<
"texture deleted due to QOpenGLContext::aboutToBeDestroyed!"
317 <<
"Pointer (now dead) was:" << (
void*)(bs->textures[plane])
318 <<
" Associated context (about to die too) is: " << (
void*)(bs->texturesContext[plane]);
320 bs->textures[plane] =
nullptr;
321 bs->texturesContext[plane] =
nullptr;
323 QObject::disconnect(bs->texturesAboutToBeDestroyedConnection[plane]);
324 bs->texturesAboutToBeDestroyedConnection[plane] = QMetaObject::Connection();
326 }, Qt::DirectConnection);
331 BufferState &state = *buffer->d;
333 state.isYInverted =
false;
335 EGLNativeFileDescriptorKHR streamFd = EGL_NO_FILE_DESCRIPTOR_KHR;
338 state.egl_stream = funcs->create_stream_from_file_descriptor(egl_display, streamFd);
341 EGLAttrib stream_attribs[] = {
345 state.egl_stream = funcs->create_stream_attrib_nv(egl_display, stream_attribs);
348 if (state.egl_stream == EGL_NO_STREAM_KHR) {
349 qCWarning(qLcWaylandCompositorHardwareIntegration,
"%s:%d: eglCreateStreamFromFileDescriptorKHR failed: 0x%x", Q_FUNC_INFO, __LINE__, eglGetError());
352 state.eglMode = BufferState::ModeEGLStream;
354 if (!QOpenGLContext::currentContext()) {
355 qCWarning(qLcWaylandCompositorHardwareIntegration)
356 <<
"EglClientBufferIntegration: creating texture with no current context";
362 setupBufferAndCleanup(buffer->d, texture, 0);
364 qCDebug(qLcWaylandCompositorHardwareIntegration)
365 <<
" NEW texture! It's pointer and ctx pointer: "
366 << (
void*)state.textures[0] <<
"; " << (
void*)state.texturesContext[0];
370 auto newStream = funcs->stream_consumer_gltexture(egl_display, state.egl_stream);
372 EGLint code = eglGetError();
373 qCWarning(qLcWaylandCompositorHardwareIntegration) <<
"Could not initialize EGLStream:" << egl_error_string(code) << Qt::hex << (
long)code;
374 funcs->destroy_stream(egl_display, state.egl_stream);
375 state.egl_stream = EGL_NO_STREAM_KHR;
383 bool usingLocalContext = ensureContext();
385 if (buffer->d->eglMode == BufferState::ModeUninitialized) {
386 bool streamOK = initEglStream(buffer, bufferHandle);
391 BufferState &state = *buffer->d;
392 auto texture = state.textures[0];
397 funcs->query_stream(egl_display, state.egl_stream, EGL_STREAM_STATE_KHR, &stream_state);
399 if (stream_state == EGL_STREAM_STATE_NEW_FRAME_AVAILABLE_KHR) {
400 if (funcs->stream_consumer_acquire(egl_display, state.egl_stream) != EGL_TRUE)
401 qCWarning(qLcWaylandCompositorHardwareIntegration,
402 "%s:%d: eglStreamConsumerAcquireKHR failed: 0x%x", Q_FUNC_INFO, __LINE__,
406 if (usingLocalContext)
407 localContext->doneCurrent();
410WaylandEglClientBufferIntegration::WaylandEglClientBufferIntegration()
411 : d_ptr(
new WaylandEglClientBufferIntegrationPrivate)
415WaylandEglClientBufferIntegration::~WaylandEglClientBufferIntegration()
417 Q_D(WaylandEglClientBufferIntegration);
418 if (d->egl_unbind_wayland_display && d->display_bound) {
419 Q_ASSERT(d->wlDisplay);
420 if (!d->egl_unbind_wayland_display(d->egl_display, d->wlDisplay))
421 qCWarning(qLcWaylandCompositorHardwareIntegration) <<
"eglUnbindWaylandDisplayWL failed";
425void WaylandEglClientBufferIntegration::initializeHardware(
struct wl_display *display)
427 Q_D(WaylandEglClientBufferIntegration);
429 const bool ignoreBindDisplay = !qgetenv(
"QT_WAYLAND_IGNORE_BIND_DISPLAY").isEmpty();
431 QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface();
432 if (!nativeInterface) {
433 qCWarning(qLcWaylandCompositorHardwareIntegration)
434 <<
"Failed to initialize EGL display. No native platform interface available.";
438 d->egl_display = nativeInterface->nativeResourceForIntegration(
"EglDisplay");
439 if (!d->egl_display) {
440 qCWarning(qLcWaylandCompositorHardwareIntegration)
441 <<
"Failed to initialize EGL display. Could not get EglDisplay for window.";
445 const char *extensionString = eglQueryString(d->egl_display, EGL_EXTENSIONS);
446 if ((!extensionString || !strstr(extensionString,
"EGL_WL_bind_wayland_display")) && !ignoreBindDisplay) {
447 qCWarning(qLcWaylandCompositorHardwareIntegration)
448 <<
"Failed to initialize EGL display. There is no EGL_WL_bind_wayland_display extension.";
452 d->egl_bind_wayland_display =
reinterpret_cast<PFNEGLBINDWAYLANDDISPLAYWL>(eglGetProcAddress(
"eglBindWaylandDisplayWL"));
453 d->egl_unbind_wayland_display =
reinterpret_cast<PFNEGLUNBINDWAYLANDDISPLAYWL>(eglGetProcAddress(
"eglUnbindWaylandDisplayWL"));
454 if ((!d->egl_bind_wayland_display || !d->egl_unbind_wayland_display) && !ignoreBindDisplay) {
455 qCWarning(qLcWaylandCompositorHardwareIntegration)
456 <<
"Failed to initialize EGL display. Could not find eglBindWaylandDisplayWL and eglUnbindWaylandDisplayWL.";
460 d->egl_query_wayland_buffer =
reinterpret_cast<PFNEGLQUERYWAYLANDBUFFERWL_compat>(eglGetProcAddress(
"eglQueryWaylandBufferWL"));
461 if (!d->egl_query_wayland_buffer) {
462 qCWarning(qLcWaylandCompositorHardwareIntegration)
463 <<
"Failed to initialize EGL display. Could not find eglQueryWaylandBufferWL.";
467 d->egl_create_image =
reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress(
"eglCreateImageKHR"));
468 d->egl_destroy_image =
reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress(
"eglDestroyImageKHR"));
469 if (!d->egl_create_image || !d->egl_destroy_image) {
470 qCWarning(qLcWaylandCompositorHardwareIntegration)
471 <<
"Failed to initialize EGL display. Could not find eglCreateImageKHR and eglDestroyImageKHR.";
475 if (d->egl_bind_wayland_display && d->egl_unbind_wayland_display) {
476 d->display_bound = d->egl_bind_wayland_display(d->egl_display, display);
477 if (!d->display_bound)
478 qCDebug(qLcWaylandCompositorHardwareIntegration) <<
"Wayland display already bound by other client buffer integration.";
479 d->wlDisplay = display;
482 d->funcs =
new QEGLStreamConvenience;
483 d->funcs->initialize(d->egl_display);
486QtWayland::ClientBuffer *WaylandEglClientBufferIntegration::createBufferFor(wl_resource *buffer)
488 Q_D(WaylandEglClientBufferIntegration);
490 bool q = d->egl_query_wayland_buffer(d->egl_display, buffer, EGL_WIDTH, &w);
493 return new WaylandEglClientBuffer(
this, buffer);
496WaylandEglClientBuffer::WaylandEglClientBuffer(WaylandEglClientBufferIntegration *integration, wl_resource *buffer)
497 : ClientBuffer(buffer)
498 , m_integration(integration)
500 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
502 if (buffer && !wl_shm_buffer_get(buffer)) {
503 EGLint width, height;
504 p->egl_query_wayland_buffer(p->egl_display, buffer, EGL_WIDTH, &width);
505 p->egl_query_wayland_buffer(p->egl_display, buffer, EGL_HEIGHT, &height);
506 d->size = QSize(width, height);
513WaylandEglClientBuffer::~WaylandEglClientBuffer()
515 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
518 for (
auto image : d->egl_images)
519 p->egl_destroy_image(p->egl_display, image);
522 p->funcs->destroy_stream(p->egl_display, d->egl_stream);
527 QMutexLocker locker(&d->texturesLock);
529 for (
int i=0; i<3; i++) {
530 if (d->textures[i] !=
nullptr) {
532 qCDebug(qLcWaylandCompositorHardwareIntegration)
533 << Q_FUNC_INFO <<
" handing over texture!"
534 << (
void*)d->textures[i] <<
"; " << (
void*)d->texturesContext[i]
535 <<
" ... current context might be the same: " << QOpenGLContext::currentContext();
537 QtWayland::QWaylandTextureOrphanage::instance()->admitTexture(
538 d->textures[i], d->texturesContext[i]);
539 d->textures[i] =
nullptr;
540 d->texturesContext[i] =
nullptr;
541 QObject::disconnect(d->texturesAboutToBeDestroyedConnection[i]);
542 d->texturesAboutToBeDestroyedConnection[i] = QMetaObject::Connection();
553 return QWaylandBufferRef::BufferFormatEgl_RGB;
555 return QWaylandBufferRef::BufferFormatEgl_RGBA;
557 return QWaylandBufferRef::BufferFormatEgl_EXTERNAL_OES;
559 return QWaylandBufferRef::BufferFormatEgl_Y_UV;
561 return QWaylandBufferRef::BufferFormatEgl_Y_U_V;
563 return QWaylandBufferRef::BufferFormatEgl_Y_XUXV;
566 return QWaylandBufferRef::BufferFormatEgl_RGBA;
572 return QOpenGLTexture::RGBFormat;
574 return QOpenGLTexture::RGBAFormat;
576 return QOpenGLTexture::NoFormat;
580QWaylandBufferRef::BufferFormatEgl WaylandEglClientBuffer::bufferFormatEgl()
const
582 return formatFromEglFormat(d->egl_format);
585QOpenGLTexture *WaylandEglClientBuffer::toOpenGlTexture(
int plane)
587 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
589 QtWayland::QWaylandTextureOrphanage::instance()->deleteTextures();
594 auto texture = d->textures[plane];
595 if (d->eglMode == BufferState::ModeEGLStream)
601 texture =
new QOpenGLTexture(target);
602 texture->setFormat(openGLFormatFromEglFormat(d->egl_format));
603 texture->setSize(d->size.width(), d->size.height());
605 p->setupBufferAndCleanup(
this->d, texture, plane);
608 if (m_textureDirty) {
609 m_textureDirty =
false;
611 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
612 p->gl_egl_image_target_texture_2d(target, d->egl_images[plane]);
613#ifdef GL_EXT_protected_textures
615 glTexParameteri(target, GL_TEXTURE_PROTECTED_EXT, GL_TRUE);
621void WaylandEglClientBuffer::setCommitted(QRegion &damage)
623 ClientBuffer::setCommitted(damage);
624 if (d->eglMode == BufferState::ModeEGLStream || d->eglMode == BufferState::ModeUninitialized) {
625 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
626 p->handleEglstreamTexture(
this, waylandBufferHandle());
630bool WaylandEglClientBuffer::isProtected()
632 if (m_integration && m_buffer)
633 return m_integration->isProtected(m_buffer);
639QWaylandSurface::Origin WaylandEglClientBuffer::origin()
const
641 return d->isYInverted ? QWaylandSurface::OriginTopLeft : QWaylandSurface::OriginBottomLeft;
644quintptr WaylandEglClientBuffer::lockNativeBuffer()
646 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
648 if (d->egl_stream != EGL_NO_STREAM_KHR)
651 EGLImageKHR image = p->egl_create_image(p->egl_display, EGL_NO_CONTEXT,
654 return reinterpret_cast<quintptr>(image);
657void WaylandEglClientBuffer::unlockNativeBuffer(quintptr native_buffer)
const
662 auto *p = WaylandEglClientBufferIntegrationPrivate::get(m_integration);
664 EGLImageKHR image =
reinterpret_cast<EGLImageKHR>(native_buffer);
665 p->egl_destroy_image(p->egl_display, image);
668QSize 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