summaryrefslogtreecommitdiffstats
path: root/src/hardwareintegration/compositor/wayland-egl/waylandeglclientbufferintegration.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/hardwareintegration/compositor/wayland-egl/waylandeglclientbufferintegration.cpp')
-rw-r--r--src/hardwareintegration/compositor/wayland-egl/waylandeglclientbufferintegration.cpp258
1 files changed, 258 insertions, 0 deletions
diff --git a/src/hardwareintegration/compositor/wayland-egl/waylandeglclientbufferintegration.cpp b/src/hardwareintegration/compositor/wayland-egl/waylandeglclientbufferintegration.cpp
new file mode 100644
index 000000000..a127b7bd2
--- /dev/null
+++ b/src/hardwareintegration/compositor/wayland-egl/waylandeglclientbufferintegration.cpp
@@ -0,0 +1,258 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Compositor.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "waylandeglclientbufferintegration.h"
+
+#include <QtCompositor/private/qwlcompositor_p.h>
+#include <QtCompositor/private/qwlsurface_p.h>
+#include <qpa/qplatformnativeinterface.h>
+#include <QtGui/QGuiApplication>
+#include <QtGui/QOpenGLContext>
+#include <qpa/qplatformscreen.h>
+#include <QtGui/QWindow>
+#include <QtCore/QPointer>
+
+#include <QDebug>
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+
+#ifndef EGL_WL_bind_wayland_display
+typedef EGLBoolean (EGLAPIENTRYP PFNEGLBINDWAYLANDDISPLAYWL) (EGLDisplay dpy, struct wl_display *display);
+typedef EGLBoolean (EGLAPIENTRYP PFNEGLUNBINDWAYLANDDISPLAYWL) (EGLDisplay dpy, struct wl_display *display);
+typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYWAYLANDBUFFERWL) (EGLDisplay dpy, struct wl_buffer *buffer, EGLint attribute, EGLint *value);
+#endif
+
+#ifndef EGL_KHR_image
+typedef EGLImageKHR (EGLAPIENTRYP PFNEGLCREATEIMAGEKHRPROC) (EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list);
+typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYIMAGEKHRPROC) (EGLDisplay dpy, EGLImageKHR image);
+#endif
+
+#ifndef GL_OES_EGL_image
+typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
+typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
+#endif
+
+QT_BEGIN_NAMESPACE
+
+class WaylandEglClientBufferIntegrationPrivate
+{
+public:
+ WaylandEglClientBufferIntegrationPrivate()
+ : egl_display(EGL_NO_DISPLAY)
+ , valid(false)
+ , display_bound(false)
+ , flipperConnected(false)
+ , egl_bind_wayland_display(0)
+ , egl_unbind_wayland_display(0)
+ , egl_query_wayland_buffer(0)
+ , egl_create_image(0)
+ , egl_destroy_image(0)
+ , gl_egl_image_target_texture_2d(0)
+ { }
+ EGLDisplay egl_display;
+ bool valid;
+ bool display_bound;
+ bool flipperConnected;
+ PFNEGLBINDWAYLANDDISPLAYWL egl_bind_wayland_display;
+ PFNEGLUNBINDWAYLANDDISPLAYWL egl_unbind_wayland_display;
+ PFNEGLQUERYWAYLANDBUFFERWL egl_query_wayland_buffer;
+
+ PFNEGLCREATEIMAGEKHRPROC egl_create_image;
+ PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image;
+
+ PFNGLEGLIMAGETARGETTEXTURE2DOESPROC gl_egl_image_target_texture_2d;
+};
+
+WaylandEglClientBufferIntegration::WaylandEglClientBufferIntegration()
+ : QWaylandClientBufferIntegration()
+ , d_ptr(new WaylandEglClientBufferIntegrationPrivate)
+{
+}
+
+void WaylandEglClientBufferIntegration::initializeHardware(QtWayland::Display *waylandDisplay)
+{
+ Q_D(WaylandEglClientBufferIntegration);
+
+ const bool ignoreBindDisplay = !qgetenv("QT_WAYLAND_IGNORE_BIND_DISPLAY").isEmpty();
+
+ QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface();
+ if (!nativeInterface) {
+ qWarning("Failed to initialize egl display. No native platform interface available.\n");
+ return;
+ }
+
+ d->egl_display = nativeInterface->nativeResourceForWindow("EglDisplay", m_compositor->window());
+ if (!d->egl_display) {
+ qWarning("Failed to initialize egl display. Could not get EglDisplay for window.\n");
+ return;
+ }
+
+ const char *extensionString = eglQueryString(d->egl_display, EGL_EXTENSIONS);
+ if ((!extensionString || !strstr(extensionString, "EGL_WL_bind_wayland_display")) && !ignoreBindDisplay) {
+ qWarning("Failed to initialize egl display. There is no EGL_WL_bind_wayland_display extension.\n");
+ return;
+ }
+
+ d->egl_bind_wayland_display = reinterpret_cast<PFNEGLBINDWAYLANDDISPLAYWL>(eglGetProcAddress("eglBindWaylandDisplayWL"));
+ d->egl_unbind_wayland_display = reinterpret_cast<PFNEGLUNBINDWAYLANDDISPLAYWL>(eglGetProcAddress("eglUnbindWaylandDisplayWL"));
+ if ((!d->egl_bind_wayland_display || !d->egl_unbind_wayland_display) && !ignoreBindDisplay) {
+ qWarning("Failed to initialize egl display. Could not find eglBindWaylandDisplayWL and eglUnbindWaylandDisplayWL.\n");
+ return;
+ }
+
+ d->egl_query_wayland_buffer = reinterpret_cast<PFNEGLQUERYWAYLANDBUFFERWL>(eglGetProcAddress("eglQueryWaylandBufferWL"));
+ if (!d->egl_query_wayland_buffer) {
+ qWarning("Failed to initialize egl display. Could not find eglQueryWaylandBufferWL.\n");
+ return;
+ }
+
+ d->egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR"));
+ d->egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
+ if (!d->egl_create_image || !d->egl_destroy_image) {
+ qWarning("Failed to initialize egl display. Could not find eglCreateImageKHR and eglDestroyImageKHR.\n");
+ return;
+ }
+
+ d->gl_egl_image_target_texture_2d = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
+ if (!d->gl_egl_image_target_texture_2d) {
+ qWarning("Failed to initialize egl display. Could not find glEGLImageTargetTexture2DOES.\n");
+ return;
+ }
+
+ if (d->egl_bind_wayland_display && d->egl_unbind_wayland_display) {
+ d->display_bound = d->egl_bind_wayland_display(d->egl_display, waylandDisplay->handle());
+ if (!d->display_bound && !ignoreBindDisplay) {
+ qWarning("Failed to initialize egl display. Could not bind Wayland display.\n");
+ return;
+ }
+ }
+
+ d->valid = true;
+
+ qWarning("EGL Wayland extension successfully initialized.%s\n", !d->display_bound ? " eglBindWaylandDisplayWL ignored" : "");
+}
+
+void WaylandEglClientBufferIntegration::bindTextureToBuffer(struct ::wl_resource *buffer)
+{
+ Q_D(WaylandEglClientBufferIntegration);
+ if (!d->valid) {
+ qWarning("bindTextureToBuffer() failed");
+ return;
+ }
+
+ EGLImageKHR image = d->egl_create_image(d->egl_display, EGL_NO_CONTEXT,
+ EGL_WAYLAND_BUFFER_WL,
+ buffer, NULL);
+
+ d->gl_egl_image_target_texture_2d(GL_TEXTURE_2D, image);
+
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+ d->egl_destroy_image(d->egl_display, image);
+}
+
+bool WaylandEglClientBufferIntegration::isYInverted(struct ::wl_resource *buffer) const
+{
+#if defined(EGL_WAYLAND_Y_INVERTED_WL)
+ Q_D(const WaylandEglClientBufferIntegration);
+
+ EGLint isYInverted;
+ EGLBoolean ret;
+ ret = d->egl_query_wayland_buffer(d->egl_display, buffer, EGL_WAYLAND_Y_INVERTED_WL, &isYInverted);
+
+ // Yes, this looks strange, but the specification says that EGL_FALSE return
+ // value (not supported) should be treated the same as EGL_TRUE return value
+ // and EGL_TRUE in value.
+ if (ret == EGL_FALSE || isYInverted == EGL_TRUE)
+ return true;
+#endif
+
+ return QWaylandClientBufferIntegration::isYInverted(buffer);
+}
+
+
+bool WaylandEglClientBufferIntegration::setDirectRenderSurface(QWaylandSurface *surface)
+{
+ Q_D(WaylandEglClientBufferIntegration);
+
+ QPlatformScreen *screen = QPlatformScreen::platformScreenForWindow(m_compositor->window());
+ QPlatformScreenPageFlipper *flipper = screen ? screen->pageFlipper() : 0;
+ if (flipper && !d->flipperConnected) {
+ QObject::connect(flipper, SIGNAL(bufferReleased(QPlatformScreenBuffer*)), m_compositor->handle(), SLOT(releaseBuffer(QPlatformScreenBuffer*)));
+ d->flipperConnected = true;
+ }
+ Q_UNUSED(surface);
+ return flipper;
+}
+
+void *WaylandEglClientBufferIntegration::lockNativeBuffer(struct ::wl_resource *buffer, QOpenGLContext *) const
+{
+ Q_D(const WaylandEglClientBufferIntegration);
+
+ EGLImageKHR image = d->egl_create_image(d->egl_display, EGL_NO_CONTEXT,
+ EGL_WAYLAND_BUFFER_WL,
+ buffer, NULL);
+ return image;
+}
+
+void WaylandEglClientBufferIntegration::unlockNativeBuffer(void *native_buffer, QOpenGLContext *) const
+{
+ Q_D(const WaylandEglClientBufferIntegration);
+ EGLImageKHR image = static_cast<EGLImageKHR>(native_buffer);
+
+ d->egl_destroy_image(d->egl_display, image);
+}
+
+QSize WaylandEglClientBufferIntegration::bufferSize(struct ::wl_resource *buffer) const
+{
+ Q_D(const WaylandEglClientBufferIntegration);
+
+ int width, height;
+ d->egl_query_wayland_buffer(d->egl_display, buffer, EGL_WIDTH, &width);
+ d->egl_query_wayland_buffer(d->egl_display, buffer, EGL_HEIGHT, &height);
+
+ return QSize(width, height);
+}
+
+QT_END_NAMESPACE