summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vaapi/gstvaapidisplay_wayland.c
blob: 8803c294bde58cdbef7ed1db5c3cfacebb552fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 *  gstvaapidisplay_wayland.c - VA/Wayland display abstraction
 *
 *  Copyright (C) 2012-2013 Intel Corporation
 *    Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

/**
 * SECTION:gstvaapidisplay_wayland
 * @short_description: VA/Wayland display abstraction
 */

#include "sysdeps.h"
#include "gstvaapidisplay_priv.h"
#include "gstvaapidisplay_wayland.h"
#include "gstvaapidisplay_wayland_priv.h"
#include "gstvaapiwindow_wayland.h"

#define DEBUG_VAAPI_DISPLAY 1
#include "gstvaapidebug.h"

G_DEFINE_TYPE_WITH_PRIVATE (GstVaapiDisplayWayland, gst_vaapi_display_wayland,
    GST_TYPE_VAAPI_DISPLAY);

static inline const gchar *
get_default_display_name (void)
{
  static const gchar *g_display_name;

  if (!g_display_name)
    g_display_name = getenv ("WAYLAND_DISPLAY");
  return g_display_name;
}

/* Mangle display name with our prefix */
static gboolean
set_display_name (GstVaapiDisplay * display, const gchar * display_name)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  g_free (priv->display_name);

  if (!display_name) {
    display_name = get_default_display_name ();
    if (!display_name)
      display_name = "";
  }
  priv->display_name = g_strdup (display_name);
  return priv->display_name != NULL;
}

static void
output_handle_geometry (void *data, struct wl_output *output,
    int x, int y, int physical_width, int physical_height,
    int subpixel, const char *make, const char *model, int transform)
{
  GstVaapiDisplayWaylandPrivate *const priv = data;

  priv->phys_width = physical_width;
  priv->phys_height = physical_height;
}

static void
output_handle_mode (void *data, struct wl_output *wl_output,
    uint32_t flags, int width, int height, int refresh)
{
  GstVaapiDisplayWaylandPrivate *const priv = data;

  if (flags & WL_OUTPUT_MODE_CURRENT) {
    priv->width = width;
    priv->height = height;
  }
}

static const struct wl_output_listener output_listener = {
  output_handle_geometry,
  output_handle_mode,
};

static void
handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
    uint32_t serial)
{
  xdg_wm_base_pong (xdg_wm_base, serial);
}

static const struct xdg_wm_base_listener xdg_wm_base_listener = {
  handle_xdg_wm_base_ping
};

static void
registry_handle_global (void *data,
    struct wl_registry *registry,
    uint32_t id, const char *interface, uint32_t version)
{
  GstVaapiDisplayWaylandPrivate *const priv = data;

  if (strcmp (interface, "wl_compositor") == 0)
    priv->compositor =
        wl_registry_bind (registry, id, &wl_compositor_interface, 1);
  else if (strcmp (interface, "wl_shell") == 0)
    priv->wl_shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
  else if (strcmp (interface, "xdg_wm_base") == 0) {
    priv->xdg_wm_base =
        wl_registry_bind (registry, id, &xdg_wm_base_interface, 1);
    xdg_wm_base_add_listener (priv->xdg_wm_base, &xdg_wm_base_listener, priv);
  } else if (strcmp (interface, "wl_output") == 0) {
    if (!priv->output) {
      priv->output = wl_registry_bind (registry, id, &wl_output_interface, 1);
      wl_output_add_listener (priv->output, &output_listener, priv);
    }
  }
}

static const struct wl_registry_listener registry_listener = {
  registry_handle_global,
  NULL,
};

static gboolean
gst_vaapi_display_wayland_setup (GstVaapiDisplay * display)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  wl_display_set_user_data (priv->wl_display, priv);
  priv->registry = wl_display_get_registry (priv->wl_display);
  wl_registry_add_listener (priv->registry, &registry_listener, priv);
  priv->event_fd = wl_display_get_fd (priv->wl_display);
  wl_display_roundtrip (priv->wl_display);

  if (!priv->width || !priv->height) {
    wl_display_roundtrip (priv->wl_display);
    if (!priv->width || !priv->height) {
      GST_ERROR ("failed to determine the display size");
      return FALSE;
    }
  }

  if (!priv->compositor) {
    GST_ERROR ("failed to bind compositor interface");
    return FALSE;
  }

  if (priv->xdg_wm_base)
    return TRUE;

  if (!priv->wl_shell) {
    GST_ERROR ("failed to bind wl_shell interface");
    return FALSE;
  }

  return TRUE;
}

static gboolean
gst_vaapi_display_wayland_bind_display (GstVaapiDisplay * display,
    gpointer native_display)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  priv->wl_display = native_display;
  priv->use_foreign_display = TRUE;

  /* XXX: how to get socket/display name? */
  GST_WARNING ("wayland: get display name");
  set_display_name (display, NULL);

  return gst_vaapi_display_wayland_setup (display);
}

static gboolean
gst_vaapi_display_wayland_open_display (GstVaapiDisplay * display,
    const gchar * name)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  if (!set_display_name (display, name))
    return FALSE;

  priv->wl_display = wl_display_connect (name);
  if (!priv->wl_display)
    return FALSE;
  priv->use_foreign_display = FALSE;

  return gst_vaapi_display_wayland_setup (display);
}

static void
gst_vaapi_display_wayland_close_display (GstVaapiDisplay * display)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  g_clear_pointer (&priv->output, wl_output_destroy);
  g_clear_pointer (&priv->wl_shell, wl_shell_destroy);
  g_clear_pointer (&priv->xdg_wm_base, xdg_wm_base_destroy);
  g_clear_pointer (&priv->compositor, wl_compositor_destroy);
  g_clear_pointer (&priv->registry, wl_registry_destroy);

  if (priv->wl_display) {
    if (!priv->use_foreign_display)
      wl_display_disconnect (priv->wl_display);
    priv->wl_display = NULL;
  }

  g_clear_pointer (&priv->display_name, g_free);
}

static gboolean
gst_vaapi_display_wayland_get_display_info (GstVaapiDisplay * display,
    GstVaapiDisplayInfo * info)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  info->native_display = priv->wl_display;
  info->display_name = priv->display_name;
  if (!info->va_display) {
    info->va_display = vaGetDisplayWl (priv->wl_display);
    if (!info->va_display)
      return FALSE;
  }
  return TRUE;
}

static void
gst_vaapi_display_wayland_get_size (GstVaapiDisplay * display,
    guint * pwidth, guint * pheight)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  if (!priv->output)
    return;

  if (pwidth)
    *pwidth = priv->width;

  if (pheight)
    *pheight = priv->height;
}

static void
gst_vaapi_display_wayland_get_size_mm (GstVaapiDisplay * display,
    guint * pwidth, guint * pheight)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      GST_VAAPI_DISPLAY_WAYLAND_GET_PRIVATE (display);

  if (!priv->output)
    return;

  if (pwidth)
    *pwidth = priv->phys_width;

  if (pheight)
    *pheight = priv->phys_height;
}

static GstVaapiWindow *
gst_vaapi_display_wayland_create_window (GstVaapiDisplay * display,
    GstVaapiID id, guint width, guint height)
{
  if (id != GST_VAAPI_ID_INVALID)
    return NULL;
  return gst_vaapi_window_wayland_new (display, width, height);
}

static void
gst_vaapi_display_wayland_init (GstVaapiDisplayWayland * display)
{
  GstVaapiDisplayWaylandPrivate *const priv =
      gst_vaapi_display_wayland_get_instance_private (display);

  display->priv = priv;
  priv->event_fd = -1;
}

static void
gst_vaapi_display_wayland_class_init (GstVaapiDisplayWaylandClass * klass)
{
  GstVaapiDisplayClass *const dpy_class = GST_VAAPI_DISPLAY_CLASS (klass);

  dpy_class->display_type = GST_VAAPI_DISPLAY_TYPE_WAYLAND;

  dpy_class->bind_display = gst_vaapi_display_wayland_bind_display;
  dpy_class->open_display = gst_vaapi_display_wayland_open_display;
  dpy_class->close_display = gst_vaapi_display_wayland_close_display;
  dpy_class->get_display = gst_vaapi_display_wayland_get_display_info;
  dpy_class->get_size = gst_vaapi_display_wayland_get_size;
  dpy_class->get_size_mm = gst_vaapi_display_wayland_get_size_mm;
  dpy_class->create_window = gst_vaapi_display_wayland_create_window;
}

/**
 * gst_vaapi_display_wayland_new:
 * @display_name: the Wayland display name
 *
 * Opens an Wayland #wl_display using @display_name and returns a
 * newly allocated #GstVaapiDisplay object. The Wayland display will
 * be cloed when the reference count of the object reaches zero.
 *
 * Return value: a newly allocated #GstVaapiDisplay object
 */
GstVaapiDisplay *
gst_vaapi_display_wayland_new (const gchar * display_name)
{
  GstVaapiDisplay *display;

  display = g_object_new (GST_TYPE_VAAPI_DISPLAY_WAYLAND, NULL);
  return gst_vaapi_display_config (display,
      GST_VAAPI_DISPLAY_INIT_FROM_DISPLAY_NAME, (gpointer) display_name);
}

/**
 * gst_vaapi_display_wayland_new_with_display:
 * @wl_display: an Wayland #wl_display
 *
 * Creates a #GstVaapiDisplay based on the Wayland @wl_display
 * display. The caller still owns the display and must call
 * wl_display_disconnect() when all #GstVaapiDisplay references are
 * released. Doing so too early can yield undefined behaviour.
 *
 * Return value: a newly allocated #GstVaapiDisplay object
 */
GstVaapiDisplay *
gst_vaapi_display_wayland_new_with_display (struct wl_display * wl_display)
{
  GstVaapiDisplay *display;

  g_return_val_if_fail (wl_display, NULL);

  display = g_object_new (GST_TYPE_VAAPI_DISPLAY_WAYLAND, NULL);
  return gst_vaapi_display_config (display,
      GST_VAAPI_DISPLAY_INIT_FROM_NATIVE_DISPLAY, wl_display);
}

/**
 * gst_vaapi_display_wayland_new_with_va_display:
 * @va_display: a VADisplay #va_display
 * @wl_display: an Wayland #wl_display
 *
 * Creates a #GstVaapiDisplay based on the VADisplay @va_display and
 * the Wayland @wl_display display.
 * The caller still owns the display and must call
 * wl_display_disconnect() when all #GstVaapiDisplay references are
 * released.
 *
 * Return value: a newly allocated #GstVaapiDisplay object
 */

GstVaapiDisplay *
gst_vaapi_display_wayland_new_with_va_display (VADisplay va_display,
    struct wl_display * wl_display)
{
  GstVaapiDisplay *display;
  GstVaapiDisplayInfo info = {
    .va_display = va_display,
    .native_display = wl_display,
  };

  g_return_val_if_fail (wl_display, NULL);

  display = g_object_new (GST_TYPE_VAAPI_DISPLAY_WAYLAND, NULL);
  if (!gst_vaapi_display_config (display,
          GST_VAAPI_DISPLAY_INIT_FROM_VA_DISPLAY, &info)) {
    gst_object_unref (display);
    return NULL;
  }

  return display;
}

/**
 * gst_vaapi_display_wayland_get_display:
 * @display: a #GstVaapiDisplayWayland
 *
 * Returns the underlying Wayland #wl_display that was created by
 * gst_vaapi_display_wayland_new() or that was bound from
 * gst_vaapi_display_wayland_new_with_display().
 *
 * Return value: the Wayland #wl_display attached to @display
 */
struct wl_display *
gst_vaapi_display_wayland_get_display (GstVaapiDisplayWayland * display)
{
  g_return_val_if_fail (GST_VAAPI_IS_DISPLAY_WAYLAND (display), NULL);

  return GST_VAAPI_DISPLAY_WL_DISPLAY (display);
}