eglimage: Add compatibility define for DRM_FORMAT_NV24
[platform/upstream/gstreamer.git] / gst-libs / gst / gl / wayland / gstgldisplay_wayland.c
1 /*
2  * GStreamer
3  * Copyright (C) 2013 Matthew Waters <ystreet00@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstgldisplay_wayland.h"
26 #include "gstgldisplay_wayland_private.h"
27
28 GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
29 #define GST_CAT_DEFAULT gst_gl_display_debug
30
31 /* We can't define these in the public struct, or we'd break ABI */
32 typedef struct _GstGLDisplayWaylandPrivate
33 {
34   struct xdg_wm_base *xdg_wm_base;
35 } GstGLDisplayWaylandPrivate;
36
37 G_DEFINE_TYPE_WITH_PRIVATE (GstGLDisplayWayland, gst_gl_display_wayland,
38     GST_TYPE_GL_DISPLAY);
39
40 static void gst_gl_display_wayland_finalize (GObject * object);
41 static guintptr gst_gl_display_wayland_get_handle (GstGLDisplay * display);
42
43 static void
44 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
45     uint32_t serial)
46 {
47   xdg_wm_base_pong (xdg_wm_base, serial);
48 }
49
50 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
51   handle_xdg_wm_base_ping
52 };
53
54 static void
55 registry_handle_global (void *data, struct wl_registry *registry,
56     uint32_t name, const char *interface, uint32_t version)
57 {
58   GstGLDisplayWayland *display = data;
59   GstGLDisplayWaylandPrivate *priv =
60       gst_gl_display_wayland_get_instance_private (display);
61
62   GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
63
64   GST_TRACE_OBJECT (display, "registry_handle_global with registry %p, "
65       "interface %s, version %u", registry, interface, version);
66
67   if (g_strcmp0 (interface, "wl_compositor") == 0) {
68     display->compositor =
69         wl_registry_bind (registry, name, &wl_compositor_interface, 1);
70   } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
71     display->subcompositor =
72         wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
73   } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
74     priv->xdg_wm_base =
75         wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
76     xdg_wm_base_add_listener (priv->xdg_wm_base, &xdg_wm_base_listener,
77         display);
78   } else if (g_strcmp0 (interface, "wl_shell") == 0) {
79     display->wl_shell =
80         wl_registry_bind (registry, name, &wl_shell_interface, 1);
81   }
82 }
83
84 static const struct wl_registry_listener registry_listener = {
85   registry_handle_global
86 };
87
88 static void
89 _connect_listeners (GstGLDisplayWayland * display)
90 {
91   display->registry = wl_display_get_registry (display->display);
92   wl_registry_add_listener (display->registry, &registry_listener, display);
93
94   wl_display_roundtrip (display->display);
95 }
96
97 static void
98 gst_gl_display_wayland_class_init (GstGLDisplayWaylandClass * klass)
99 {
100   GST_GL_DISPLAY_CLASS (klass)->get_handle =
101       GST_DEBUG_FUNCPTR (gst_gl_display_wayland_get_handle);
102
103   G_OBJECT_CLASS (klass)->finalize = gst_gl_display_wayland_finalize;
104 }
105
106 static void
107 gst_gl_display_wayland_init (GstGLDisplayWayland * display_wayland)
108 {
109   GstGLDisplay *display = (GstGLDisplay *) display_wayland;
110
111   display->type = GST_GL_DISPLAY_TYPE_WAYLAND;
112   display_wayland->foreign_display = FALSE;
113 }
114
115 static void
116 gst_gl_display_wayland_finalize (GObject * object)
117 {
118   GstGLDisplayWayland *display_wayland = GST_GL_DISPLAY_WAYLAND (object);
119   GstGLDisplayWaylandPrivate *priv =
120       gst_gl_display_wayland_get_instance_private (display_wayland);
121
122   g_clear_pointer (&display_wayland->wl_shell, wl_shell_destroy);
123   g_clear_pointer (&priv->xdg_wm_base, xdg_wm_base_destroy);
124
125   /* Cause eglTerminate() to occur before wl_display_disconnect()
126    * https://bugzilla.gnome.org/show_bug.cgi?id=787293 */
127   g_object_set_data (object, "gst.gl.display.egl", NULL);
128
129   if (!display_wayland->foreign_display && display_wayland->display) {
130     wl_display_flush (display_wayland->display);
131     wl_display_disconnect (display_wayland->display);
132   }
133
134   G_OBJECT_CLASS (gst_gl_display_wayland_parent_class)->finalize (object);
135 }
136
137 /**
138  * gst_gl_display_wayland_new:
139  * @name: (allow-none): a display name
140  *
141  * Create a new #GstGLDisplayWayland from the wayland display name.  See wl_display_connect()
142  * for details on what is a valid name.
143  *
144  * Returns: (transfer full): a new #GstGLDisplayWayland or %NULL
145  */
146 GstGLDisplayWayland *
147 gst_gl_display_wayland_new (const gchar * name)
148 {
149   GstGLDisplayWayland *ret;
150
151   GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
152
153   ret = g_object_new (GST_TYPE_GL_DISPLAY_WAYLAND, NULL);
154   gst_object_ref_sink (ret);
155   ret->display = wl_display_connect (name);
156
157   if (!ret->display) {
158     if (name != NULL) {
159       GST_ERROR ("Failed to open Wayland display connection with name \'%s\'",
160           name);
161     } else {
162       GST_INFO ("Failed to open Wayland display connection.");
163     }
164     gst_object_unref (ret);
165     return NULL;
166   }
167
168   _connect_listeners (ret);
169
170   return ret;
171 }
172
173 /**
174  * gst_gl_display_wayland_new_with_display:
175  * @display: an existing, wayland display
176  *
177  * Creates a new display connection from a wl_display Display.
178  *
179  * Returns: (transfer full): a new #GstGLDisplayWayland
180  */
181 GstGLDisplayWayland *
182 gst_gl_display_wayland_new_with_display (struct wl_display * display)
183 {
184   GstGLDisplayWayland *ret;
185
186   g_return_val_if_fail (display != NULL, NULL);
187
188   GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
189
190   ret = g_object_new (GST_TYPE_GL_DISPLAY_WAYLAND, NULL);
191   gst_object_ref_sink (ret);
192
193   ret->display = display;
194   ret->foreign_display = TRUE;
195
196   _connect_listeners (ret);
197
198   return ret;
199 }
200
201 static guintptr
202 gst_gl_display_wayland_get_handle (GstGLDisplay * display)
203 {
204   return (guintptr) GST_GL_DISPLAY_WAYLAND (display)->display;
205 }
206
207 struct xdg_wm_base *
208 gst_gl_display_wayland_get_xdg_wm_base (GstGLDisplayWayland * display)
209 {
210   GstGLDisplayWaylandPrivate *priv =
211       gst_gl_display_wayland_get_instance_private (display);
212
213   return priv->xdg_wm_base;
214 }