static GstVaapiDisplay *
gst_vaapi_create_display_from_egl (GstGLDisplay * gl_display,
    GstGLContext * gl_context, GstVaapiDisplayType display_type,
    gpointer native_display)
{
  GstVaapiDisplay *display = NULL;
#if USE_EGL
  GstGLAPI gl_api;
  gint gles_version;
  guintptr egl_handler;

  gl_api = gst_gl_context_get_gl_api (gl_context);
  gles_version = gst_vaapi_get_gles_version_from_gl_api (gl_api);
  if (gles_version == -1)
    return NULL;

  egl_handler = gst_vaapi_get_egl_handle_from_gl_display (gl_display);
  if (egl_handler != 0) {
    gpointer native_display_egl = GSIZE_TO_POINTER (egl_handler);
    display = gst_vaapi_display_egl_new_with_native_display (native_display_egl,
        display_type, gles_version);
  }

  if (!display) {
    GstVaapiDisplay *wrapped_display;

    wrapped_display =
        gst_vaapi_create_display_from_handle (display_type, native_display);
    if (wrapped_display) {
      display = gst_vaapi_display_egl_new (wrapped_display, gles_version);
      gst_object_unref (wrapped_display);
    }
  }

  if (display) {
    gst_vaapi_display_egl_set_gl_context (GST_VAAPI_DISPLAY_EGL (display),
        GSIZE_TO_POINTER (gst_gl_context_get_gl_context (gl_context)));
  }
#endif
  return display;
}
gboolean
gst_vaapi_display_egl_set_current_display (GstVaapiDisplayEGL * display)
{
  EglDisplay *egl_display;

  g_return_val_if_fail (GST_VAAPI_IS_DISPLAY_EGL (display), FALSE);

  if (G_UNLIKELY (eglGetCurrentDisplay () == EGL_NO_DISPLAY))
    return TRUE;
  if (G_LIKELY (display->egl_display->base.handle.p == eglGetCurrentDisplay ()))
    return TRUE;

  egl_display = egl_display_new_wrapped (eglGetCurrentDisplay ());
  if (!egl_display)
    return FALSE;
  egl_object_replace (&display->egl_display, egl_display);
  egl_object_unref (egl_display);
  if (!gst_vaapi_display_egl_set_gl_context (display, eglGetCurrentContext ()))
    return FALSE;

  return TRUE;
}
static GstVaapiDisplay *
gst_vaapi_create_display_from_gl_context (GstObject * gl_context_object)
{
#if USE_GST_GL_HELPERS
  GstGLContext *const gl_context = GST_GL_CONTEXT (gl_context_object);
  GstGLDisplay *const gl_display = gst_gl_context_get_display (gl_context);
  gpointer native_display =
      GSIZE_TO_POINTER (gst_gl_display_get_handle (gl_display));
  GstVaapiDisplay *display, *out_display;
  GstVaapiDisplayType display_type;

  switch (gst_gl_display_get_handle_type (gl_display)) {
#if USE_X11
    case GST_GL_DISPLAY_TYPE_X11:
      display_type = GST_VAAPI_DISPLAY_TYPE_X11;
      break;
#endif
#if USE_WAYLAND
    case GST_GL_DISPLAY_TYPE_WAYLAND:
      display_type = GST_VAAPI_DISPLAY_TYPE_WAYLAND;
      break;
#endif
    case GST_GL_DISPLAY_TYPE_ANY:{
      /* Derive from the active window */
      GstGLWindow *const gl_window = gst_gl_context_get_window (gl_context);
      const gchar *const gl_window_type = g_getenv ("GST_GL_WINDOW");

      display_type = GST_VAAPI_DISPLAY_TYPE_ANY;
      if (!gl_window)
        break;
      native_display = GSIZE_TO_POINTER (gst_gl_window_get_display (gl_window));

      if (gl_window_type) {
#if USE_X11
        if (!display_type && g_strcmp0 (gl_window_type, "x11") == 0)
          display_type = GST_VAAPI_DISPLAY_TYPE_X11;
#endif
#if USE_WAYLAND
        if (!display_type && g_strcmp0 (gl_window_type, "wayland") == 0)
          display_type = GST_VAAPI_DISPLAY_TYPE_WAYLAND;
#endif
      } else {
#if USE_X11
        if (!display_type && GST_GL_HAVE_WINDOW_X11)
          display_type = GST_VAAPI_DISPLAY_TYPE_X11;
#endif
#if USE_WAYLAND
        if (!display_type && GST_GL_HAVE_WINDOW_WAYLAND)
          display_type = GST_VAAPI_DISPLAY_TYPE_WAYLAND;
#endif
      }
      break;
    }
    default:
      display_type = GST_VAAPI_DISPLAY_TYPE_ANY;
      break;
  }
  if (!display_type)
    return NULL;

  display = gst_vaapi_create_display_from_handle (display_type, native_display);
  if (!display)
    return NULL;

  switch (gst_gl_context_get_gl_platform (gl_context)) {
#if USE_EGL
    case GST_GL_PLATFORM_EGL:{
      guint gles_version;

      switch (gst_gl_context_get_gl_api (gl_context)) {
        case GST_GL_API_GLES1:
          gles_version = 1;
          goto create_egl_display;
        case GST_GL_API_GLES2:
          gles_version = 2;
          goto create_egl_display;
        case GST_GL_API_OPENGL:
        case GST_GL_API_OPENGL3:
          gles_version = 0;
        create_egl_display:
          out_display = gst_vaapi_display_egl_new (display, gles_version);
          break;
        default:
          out_display = NULL;
          break;
      }
      if (!out_display)
        return NULL;
      gst_vaapi_display_egl_set_gl_context (GST_VAAPI_DISPLAY_EGL (out_display),
          GSIZE_TO_POINTER (gst_gl_context_get_gl_context (gl_context)));
      break;
    }
#endif
    default:
      out_display = gst_vaapi_display_ref (display);
      break;
  }
  gst_vaapi_display_unref (display);
  return out_display;
#endif
  GST_ERROR ("unsupported GStreamer version %s", GST_API_VERSION_S);
  return NULL;
}