Blame ext/qt/gstqtglutility.cc

Packit 1f69a5
/*
Packit 1f69a5
 * GStreamer
Packit 1f69a5
 * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
Packit 1f69a5
 *
Packit 1f69a5
 * This library is free software; you can redistribute it and/or
Packit 1f69a5
 * modify it under the terms of the GNU Library General Public
Packit 1f69a5
 * License as published by the Free Software Foundation; either
Packit 1f69a5
 * version 2 of the License, or (at your option) any later version.
Packit 1f69a5
 *
Packit 1f69a5
 * This library is distributed in the hope that it will be useful,
Packit 1f69a5
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1f69a5
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 1f69a5
 * Library General Public License for more details.
Packit 1f69a5
 *
Packit 1f69a5
 * You should have received a copy of the GNU Library General Public
Packit 1f69a5
 * License along with this library; if not, write to the
Packit 1f69a5
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 1f69a5
 * Boston, MA 02110-1301, USA.
Packit 1f69a5
 */
Packit 1f69a5
Packit 1f69a5
#ifdef HAVE_CONFIG_H
Packit 1f69a5
#include "config.h"
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
#include "gstqtglutility.h"
Packit 1f69a5
#include <QtGui/QGuiApplication>
Packit 1f69a5
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
Packit 1f69a5
#include <QX11Info>
Packit 1f69a5
#include <gst/gl/x11/gstgldisplay_x11.h>
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
Packit 1f69a5
#include <qpa/qplatformnativeinterface.h>
Packit 1f69a5
#include <gst/gl/wayland/gstgldisplay_wayland.h>
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
#if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_VIV_FB
Packit 1f69a5
#include <qpa/qplatformnativeinterface.h>
Packit 1f69a5
#include <gst/gl/viv-fb/gstgldisplay_viv_fb.h>
Packit 1f69a5
#else
Packit 1f69a5
#include <gst/gl/egl/gstegl.h>
Packit 1f69a5
#include <gst/gl/egl/gstgldisplay_egl.h>
Packit 1f69a5
#ifdef HAVE_QT_QPA_HEADER
Packit 1f69a5
#include <qpa/qplatformnativeinterface.h>
Packit 1f69a5
#endif
Packit 1f69a5
#endif
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
#define GST_CAT_DEFAULT qt_gl_utils_debug
Packit 1f69a5
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
Packit 1f69a5
Packit 1f69a5
GstGLDisplay *
Packit 1f69a5
gst_qt_get_gl_display ()
Packit 1f69a5
{
Packit 1f69a5
  GstGLDisplay *display = NULL;
Packit 1f69a5
  QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
Packit 1f69a5
  static volatile gsize _debug;
Packit 1f69a5
Packit 1f69a5
  g_assert (app != NULL);
Packit 1f69a5
Packit 1f69a5
  if (g_once_init_enter (&_debug)) {
Packit 1f69a5
    GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0,
Packit 1f69a5
        "Qt gl utility functions");
Packit 1f69a5
    g_once_init_leave (&_debug, 1);
Packit 1f69a5
  }
Packit 1f69a5
  GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
Packit 1f69a5
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
Packit 1f69a5
  if (QString::fromUtf8 ("xcb") == app->platformName())
Packit 1f69a5
    display = (GstGLDisplay *)
Packit 1f69a5
        gst_gl_display_x11_new_with_display (QX11Info::display ());
Packit 1f69a5
#endif
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
Packit 1f69a5
  if (QString::fromUtf8 ("wayland") == app->platformName()
Packit 1f69a5
        || QString::fromUtf8 ("wayland-egl") == app->platformName()){
Packit 1f69a5
    struct wl_display * wayland_display;
Packit 1f69a5
    QPlatformNativeInterface *native =
Packit 1f69a5
        QGuiApplication::platformNativeInterface();
Packit 1f69a5
    wayland_display = (struct wl_display *)
Packit 1f69a5
        native->nativeResourceForWindow("display", NULL);
Packit 1f69a5
    display = (GstGLDisplay *)
Packit 1f69a5
        gst_gl_display_wayland_new_with_display (wayland_display);
Packit 1f69a5
  }
Packit 1f69a5
#endif
Packit 1f69a5
#if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID
Packit 1f69a5
  if (QString::fromUtf8 ("android") == app->platformName()) {
Packit 1f69a5
    EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
Packit 1f69a5
    display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
Packit 1f69a5
  }
Packit 1f69a5
#elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
Packit 1f69a5
  if (QString::fromUtf8("eglfs") == app->platformName()) {
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_VIV_FB
Packit 1f69a5
    /* FIXME: Could get the display directly from Qt like this
Packit 1f69a5
      QPlatformNativeInterface *native =
Packit 1f69a5
          QGuiApplication::platformNativeInterface();
Packit 1f69a5
      EGLDisplay egl_display = (EGLDisplay)
Packit 1f69a5
          native->nativeResourceForWindow("egldisplay", NULL);
Packit 1f69a5
Packit 1f69a5
      However we seem to have no way for getting the EGLNativeDisplayType, aka
Packit 1f69a5
      native_display, via public API. As such we have to assume that display 0
Packit 1f69a5
      is always used. Only way around that is parsing the index the same way as
Packit 1f69a5
      Qt does in QEGLDeviceIntegration::fbDeviceName(), so let's do that.
Packit 1f69a5
    */
Packit 1f69a5
    const gchar *fb_dev;
Packit 1f69a5
    gint disp_idx = 0;
Packit 1f69a5
Packit 1f69a5
    fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
Packit 1f69a5
    if (fb_dev) {
Packit 1f69a5
      if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
Packit 1f69a5
        disp_idx = 0;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    display = (GstGLDisplay *) gst_gl_display_viv_fb_new (disp_idx);
Packit 1f69a5
#elif defined(HAVE_QT_QPA_HEADER)
Packit 1f69a5
    QPlatformNativeInterface *native =
Packit 1f69a5
        QGuiApplication::platformNativeInterface();
Packit 1f69a5
    EGLDisplay egl_display = (EGLDisplay)
Packit 1f69a5
        native->nativeResourceForWindow("egldisplay", NULL);
Packit 1f69a5
    if (egl_display != EGL_NO_DISPLAY)
Packit 1f69a5
      display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
Packit 1f69a5
#else
Packit 1f69a5
    EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
Packit 1f69a5
    display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
Packit 1f69a5
#endif
Packit 1f69a5
  }
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
Packit 1f69a5
  if (QString::fromUtf8 ("cocoa") == app->platformName())
Packit 1f69a5
    display = (GstGLDisplay *) gst_gl_display_new ();
Packit 1f69a5
#endif
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
Packit 1f69a5
  if (QString::fromUtf8 ("ios") == app->platformName())
Packit 1f69a5
    display = gst_gl_display_new ();
Packit 1f69a5
#endif
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
Packit 1f69a5
  if (QString::fromUtf8 ("windows") == app->platformName())
Packit 1f69a5
    display = gst_gl_display_new ();
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
  if (!display)
Packit 1f69a5
    display = gst_gl_display_new ();
Packit 1f69a5
Packit 1f69a5
  return display;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
gboolean
Packit 1f69a5
gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
Packit 1f69a5
    GstGLContext **wrap_glcontext, GstGLContext **context)
Packit 1f69a5
{
Packit 1f69a5
  GstGLPlatform platform = (GstGLPlatform) 0;
Packit 1f69a5
  GstGLAPI gl_api;
Packit 1f69a5
  guintptr gl_handle;
Packit 1f69a5
  GError *error = NULL;
Packit 1f69a5
Packit 1f69a5
  g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
Packit 1f69a5
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
Packit 1f69a5
  if (GST_IS_GL_DISPLAY_X11 (display)) {
Packit 1f69a5
#if GST_GL_HAVE_PLATFORM_GLX
Packit 1f69a5
    platform = GST_GL_PLATFORM_GLX;
Packit 1f69a5
#elif GST_GL_HAVE_PLATFORM_EGL
Packit 1f69a5
    platform = GST_GL_PLATFORM_EGL;
Packit 1f69a5
#endif
Packit 1f69a5
  }
Packit 1f69a5
#endif
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
Packit 1f69a5
  if (GST_IS_GL_DISPLAY_WAYLAND (display)) {
Packit 1f69a5
    platform = GST_GL_PLATFORM_EGL;
Packit 1f69a5
  }
Packit 1f69a5
#endif
Packit 1f69a5
#if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_VIV_FB
Packit 1f69a5
  if (GST_IS_GL_DISPLAY_VIV_FB (display)) {
Packit 1f69a5
#else
Packit 1f69a5
  if (GST_IS_GL_DISPLAY_EGL (display)) {
Packit 1f69a5
#endif
Packit 1f69a5
    platform = GST_GL_PLATFORM_EGL;
Packit 1f69a5
  }
Packit 1f69a5
#endif
Packit 1f69a5
  if (platform == 0) {
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
Packit 1f69a5
    platform = GST_GL_PLATFORM_CGL;
Packit 1f69a5
#elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
Packit 1f69a5
    platform = GST_GL_PLATFORM_EAGL;
Packit 1f69a5
#elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
Packit 1f69a5
    platform = GST_GL_PLATFORM_WGL;
Packit 1f69a5
#else
Packit 1f69a5
    GST_ERROR ("Unknown platform");
Packit 1f69a5
    return FALSE;
Packit 1f69a5
#endif
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
Packit 1f69a5
  gl_handle = gst_gl_context_get_current_gl_context (platform);
Packit 1f69a5
  if (gl_handle)
Packit 1f69a5
    *wrap_glcontext =
Packit 1f69a5
        gst_gl_context_new_wrapped (display, gl_handle,
Packit 1f69a5
        platform, gl_api);
Packit 1f69a5
Packit 1f69a5
  if (!*wrap_glcontext) {
Packit 1f69a5
    GST_ERROR ("cannot wrap qt OpenGL context");
Packit 1f69a5
    return FALSE;
Packit 1f69a5
  }
Packit 1f69a5
 
Packit 1f69a5
  (void) platform;
Packit 1f69a5
  (void) gl_api;
Packit 1f69a5
  (void) gl_handle;
Packit 1f69a5
Packit 1f69a5
  gst_gl_context_activate (*wrap_glcontext, TRUE);
Packit 1f69a5
  if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) {
Packit 1f69a5
    GST_ERROR ("failed to retrieve qt context info: %s", error->message);
Packit 1f69a5
    g_object_unref (*wrap_glcontext);
Packit 1f69a5
    *wrap_glcontext = NULL;
Packit 1f69a5
    return FALSE;
Packit 1f69a5
  } else {
Packit 1f69a5
    gst_gl_display_filter_gl_api (display, gst_gl_context_get_gl_api (*wrap_glcontext));
Packit 1f69a5
#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)  
Packit 1f69a5
    g_return_val_if_fail (context != NULL, FALSE);
Packit 1f69a5
Packit 1f69a5
    G_STMT_START {
Packit 1f69a5
      GstGLWindow *window;
Packit 1f69a5
      HDC device;
Packit 1f69a5
Packit 1f69a5
      /* If there's no wglCreateContextAttribsARB() support, then we would fallback to
Packit 1f69a5
       * wglShareLists() which will fail with ERROR_BUSY (0xaa) if either of the GL
Packit 1f69a5
       * contexts are current in any other thread.
Packit 1f69a5
       *
Packit 1f69a5
       * The workaround here is to temporarily disable Qt's GL context while we
Packit 1f69a5
       * set up our own.
Packit 1f69a5
       *
Packit 1f69a5
       * Sometimes wglCreateContextAttribsARB()
Packit 1f69a5
       * exists, but isn't functional (some Intel drivers), so it's easiest to do this
Packit 1f69a5
       * unconditionally.
Packit 1f69a5
       */
Packit 1f69a5
      *context = gst_gl_context_new (display);
Packit 1f69a5
      window = gst_gl_context_get_window (*context);
Packit 1f69a5
      device = (HDC) gst_gl_window_get_display (window);
Packit 1f69a5
Packit 1f69a5
      wglMakeCurrent (device, 0);
Packit 1f69a5
      gst_object_unref (window);
Packit 1f69a5
      if (!gst_gl_context_create (*context, *wrap_glcontext, &error)) {
Packit 1f69a5
        GST_ERROR ("%p failed to create shared GL context: %s", this, error->message);
Packit 1f69a5
        g_object_unref (*context);
Packit 1f69a5
        *context = NULL;
Packit 1f69a5
        g_object_unref (*wrap_glcontext);
Packit 1f69a5
        *wrap_glcontext = NULL;
Packit 1f69a5
        wglMakeCurrent (device, (HGLRC) gl_handle);
Packit 1f69a5
        return FALSE;
Packit 1f69a5
      }
Packit 1f69a5
      wglMakeCurrent (device, (HGLRC) gl_handle);
Packit 1f69a5
    }
Packit 1f69a5
#endif
Packit 1f69a5
    gst_gl_context_activate (*wrap_glcontext, FALSE);
Packit 1f69a5
  } G_STMT_END;
Packit 1f69a5
Packit 1f69a5
  return TRUE;
Packit 1f69a5
}