Debian Codegraph @

gst-libs/gst/wayland/gstwlwindow.c

raw
1 /* GStreamer Wayland Library
2 *
3 * Copyright (C) 2011 Intel Corporation
4 * Copyright (C) 2011 Sreerenj Balachandran <[email protected]>
5 * Copyright (C) 2014 Collabora Ltd.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "gstwlwindow.h"
28
29 #include "color-management-v1-client-protocol.h"
30 #include "color-representation-v1-client-protocol.h"
31 #include "fullscreen-shell-unstable-v1-client-protocol.h"
32 #include "single-pixel-buffer-v1-client-protocol.h"
33 #include "viewporter-client-protocol.h"
34 #include "xdg-shell-client-protocol.h"
35
36 #define GST_CAT_DEFAULT gst_wl_window_debug
37 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
38
39 typedef struct _GstWlWindowPrivate
40 {
41 GObject parent_instance;
42
43 GMutex *render_lock;
44
45 GstWlDisplay *display;
46 struct wl_surface *area_surface;
47 struct wl_surface *area_surface_wrapper;
48 struct wl_subsurface *area_subsurface;
49 struct wp_viewport *area_viewport;
50 struct wl_surface *video_surface;
51 struct wl_surface *video_surface_wrapper;
52 struct wl_subsurface *video_subsurface;
53 struct wp_viewport *video_viewport;
54 struct xdg_surface *xdg_surface;
55 struct xdg_toplevel *xdg_toplevel;
56 struct wp_color_management_surface_v1 *color_management_surface;
57 struct wp_color_representation_surface_v1 *color_representation_surface;
58 gboolean configured;
59 GCond configure_cond;
60 GMutex configure_mutex;
61
62 /* the size and position of the area_(sub)surface */
63 GstVideoRectangle render_rectangle;
64
65 /* the size and position of the video_subsurface */
66 GstVideoRectangle video_rectangle;
67
68 /* the size of the video in the buffers (unpadded) */
69 gint video_width, video_height;
70
71 /* the size of the video in the buffers (padded) */
72 gint buffer_width, buffer_height;
73
74 /* default window dimension used when the compositor does not chose a size */
75 gint default_width, default_height;
76
77 /* video width scaled according to par */
78 gint scaled_width;
79
80 /* the crop rectangle */
81 GstVideoRectangle crop;
82
83 enum wl_output_transform buffer_transform;
84
85 gboolean force_aspect_ratio;
86
87 /* when this is not set both the area_surface and the video_surface are not
88 * visible and certain steps should be skipped */
89 gboolean is_area_surface_mapped;
90
91 GMutex window_lock;
92 GstWlBuffer *next_buffer;
93 GstVideoInfo *next_video_info;
94 GstVideoMasteringDisplayInfo *next_minfo;
95 GstVideoContentLightLevel *next_linfo;
96 GstWlBuffer *staged_buffer;
97 gboolean clear_window;
98 struct wl_callback *frame_callback;
99 struct wl_callback *commit_callback;
100 } GstWlWindowPrivate;
101
102 G_DEFINE_TYPE_WITH_CODE (GstWlWindow, gst_wl_window, G_TYPE_OBJECT,
103 G_ADD_PRIVATE (GstWlWindow)
104 GST_DEBUG_CATEGORY_INIT (gst_wl_window_debug,
105 "wlwindow", 0, "wlwindow library");
106 );
107
108 enum
109 {
110 CLOSED,
111 MAP,
112 LAST_SIGNAL
113 };
114
115 static guint signals[LAST_SIGNAL] = { 0 };
116
117 static void gst_wl_window_finalize (GObject * gobject);
118
119 static void gst_wl_window_update_geometry (GstWlWindow * self);
120
121 static void gst_wl_window_update_borders (GstWlWindow * self);
122
123 static void gst_wl_window_commit_buffer (GstWlWindow * self,
124 GstWlBuffer * buffer);
125
126 static void gst_wl_window_set_colorimetry (GstWlWindow * self,
127 const GstVideoColorimetry * colorimetry,
128 const GstVideoMasteringDisplayInfo * minfo,
129 const GstVideoContentLightLevel * linfo);
130
131 static void
132 handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
133 {
134 GstWlWindow *self = data;
135
136 GST_DEBUG_OBJECT (self, "XDG toplevel got a \"close\" event.");
137 g_signal_emit (self, signals[CLOSED], 0);
138 }
139
140 static void
141 handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
142 int32_t width, int32_t height, struct wl_array *states)
143 {
144 GstWlWindow *self = data;
145 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
146 const uint32_t *state;
147
148 GST_DEBUG_OBJECT (self, "XDG toplevel got a \"configure\" event, [ %d, %d ].",
149 width, height);
150
151 wl_array_for_each (state, states) {
152 switch (*state) {
153 case XDG_TOPLEVEL_STATE_FULLSCREEN:
154 GST_DEBUG_OBJECT (self, "XDG top-level now FULLSCREEN");
155 break;
156 case XDG_TOPLEVEL_STATE_MAXIMIZED:
157 GST_DEBUG_OBJECT (self, "XDG top-level now MAXIMIXED");
158 break;
159 case XDG_TOPLEVEL_STATE_RESIZING:
160 GST_DEBUG_OBJECT (self, "XDG top-level being RESIZED");
161 break;
162 case XDG_TOPLEVEL_STATE_ACTIVATED:
163 GST_DEBUG_OBJECT (self, "XDG top-level being ACTIVATED");
164 break;
165 }
166 }
167
168 if (width <= 0 || height <= 0) {
169 width = priv->default_width;
170 height = priv->default_height;
171 }
172
173 g_mutex_lock (&priv->configure_mutex);
174 priv->configured = FALSE;
175 gst_wl_window_set_render_rectangle (self, 0, 0, width, height);
176 g_mutex_unlock (&priv->configure_mutex);
177 }
178
179 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
180 handle_xdg_toplevel_configure,
181 handle_xdg_toplevel_close,
182 };
183
184 static void
185 handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
186 uint32_t serial)
187 {
188 GstWlWindow *self = data;
189 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
190
191 xdg_surface_ack_configure (xdg_surface, serial);
192
193 g_mutex_lock (&priv->configure_mutex);
194 priv->configured = TRUE;
195 g_cond_signal (&priv->configure_cond);
196 gst_wl_window_update_geometry (self);
197 g_mutex_unlock (&priv->configure_mutex);
198 }
199
200 static const struct xdg_surface_listener xdg_surface_listener = {
201 handle_xdg_surface_configure,
202 };
203
204 static void
205 gst_wl_window_class_init (GstWlWindowClass * klass)
206 {
207 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208 gobject_class->finalize = gst_wl_window_finalize;
209
210 signals[CLOSED] = g_signal_new ("closed", G_TYPE_FROM_CLASS (gobject_class),
211 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
212 signals[MAP] = g_signal_new ("map", G_TYPE_FROM_CLASS (gobject_class),
213 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
214
215 }
216
217 static void
218 gst_wl_window_init (GstWlWindow * self)
219 {
220 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
221
222 priv->configured = TRUE;
223 g_cond_init (&priv->configure_cond);
224 g_mutex_init (&priv->configure_mutex);
225 g_mutex_init (&priv->window_lock);
226 }
227
228 static void
229 gst_wl_window_finalize (GObject * gobject)
230 {
231 GstWlWindow *self = GST_WL_WINDOW (gobject);
232 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
233
234 gst_wl_display_callback_destroy (priv->display, &priv->frame_callback);
235 gst_wl_display_callback_destroy (priv->display, &priv->commit_callback);
236 gst_wl_display_object_destroy (priv->display,
237 (gpointer *) & priv->xdg_toplevel, (GDestroyNotify) xdg_toplevel_destroy);
238 gst_wl_display_object_destroy (priv->display,
239 (gpointer *) & priv->xdg_surface, (GDestroyNotify) xdg_surface_destroy);
240
241 if (priv->staged_buffer)
242 gst_wl_buffer_unref_buffer (priv->staged_buffer);
243
244 g_cond_clear (&priv->configure_cond);
245 g_mutex_clear (&priv->configure_mutex);
246 g_mutex_clear (&priv->window_lock);
247
248 if (priv->video_viewport)
249 wp_viewport_destroy (priv->video_viewport);
250
251 if (priv->color_management_surface)
252 wp_color_management_surface_v1_destroy (priv->color_management_surface);
253
254 if (priv->color_representation_surface)
255 wp_color_representation_surface_v1_destroy
256 (priv->color_representation_surface);
257
258 wl_proxy_wrapper_destroy (priv->video_surface_wrapper);
259 wl_subsurface_destroy (priv->video_subsurface);
260 wl_surface_destroy (priv->video_surface);
261
262 if (priv->area_subsurface)
263 wl_subsurface_destroy (priv->area_subsurface);
264
265 if (priv->area_viewport)
266 wp_viewport_destroy (priv->area_viewport);
267
268 wl_proxy_wrapper_destroy (priv->area_surface_wrapper);
269 wl_surface_destroy (priv->area_surface);
270
271 g_clear_object (&priv->display);
272
273 G_OBJECT_CLASS (gst_wl_window_parent_class)->finalize (gobject);
274 }
275
276 static GstWlWindow *
277 gst_wl_window_new_internal (GstWlDisplay * display, GMutex * render_lock)
278 {
279 GstWlWindow *self;
280 GstWlWindowPrivate *priv;
281 struct wl_compositor *compositor;
282 struct wl_event_queue *event_queue;
283 struct wl_region *region;
284 struct wp_viewporter *viewporter;
285
286 self = g_object_new (GST_TYPE_WL_WINDOW, NULL);
287 priv = gst_wl_window_get_instance_private (self);
288 priv->display = g_object_ref (display);
289 priv->render_lock = render_lock;
290 g_cond_init (&priv->configure_cond);
291 priv->force_aspect_ratio = TRUE;
292
293 compositor = gst_wl_display_get_compositor (display);
294 priv->area_surface = wl_compositor_create_surface (compositor);
295 priv->video_surface = wl_compositor_create_surface (compositor);
296
297 priv->area_surface_wrapper = wl_proxy_create_wrapper (priv->area_surface);
298 priv->video_surface_wrapper = wl_proxy_create_wrapper (priv->video_surface);
299
300 event_queue = gst_wl_display_get_event_queue (display);
301 wl_proxy_set_queue ((struct wl_proxy *) priv->area_surface_wrapper,
302 event_queue);
303 wl_proxy_set_queue ((struct wl_proxy *) priv->video_surface_wrapper,
304 event_queue);
305
306 /* embed video_surface in area_surface */
307 priv->video_subsurface =
308 wl_subcompositor_get_subsurface (gst_wl_display_get_subcompositor
309 (display), priv->video_surface, priv->area_surface);
310 wl_subsurface_set_desync (priv->video_subsurface);
311
312 viewporter = gst_wl_display_get_viewporter (display);
313 if (viewporter) {
314 priv->area_viewport = wp_viewporter_get_viewport (viewporter,
315 priv->area_surface);
316 priv->video_viewport = wp_viewporter_get_viewport (viewporter,
317 priv->video_surface);
318 }
319
320 /* never accept input events on the video surface */
321 region = wl_compositor_create_region (compositor);
322 wl_surface_set_input_region (priv->video_surface, region);
323 wl_region_destroy (region);
324
325 return self;
326 }
327
328 /**
329 * gst_wl_window_ensure_fullscreen_for_output:
330 * @self: A #GstWlWindow
331 * @fullscreen: %TRUE to set fullscreen, %FALSE to unset it
332 * @output_nane: (nullable): The name of the wl_output to fullscreen to
333 *
334 * Ensure the window fullscreen state matches the desired state. If a
335 * output_name is provided, and this output exists, the window will be set to
336 * fullscreen on that screen. Otherwise the compisitor will decide.
337 *
338 * Since: 1.28
339 */
340 void
341 gst_wl_window_ensure_fullscreen_for_output (GstWlWindow * self,
342 gboolean fullscreen, const gchar * output_name)
343 {
344 GstWlWindowPrivate *priv;
345 GstWlOutput *output = NULL;
346 struct wl_output *wl_output = NULL;
347
348 g_return_if_fail (self);
349 priv = gst_wl_window_get_instance_private (self);
350
351 if (!fullscreen) {
352 xdg_toplevel_unset_fullscreen (priv->xdg_toplevel);
353 wl_display_flush (gst_wl_display_get_display (priv->display));
354 return;
355 }
356
357 if (output_name) {
358 output = gst_wl_display_get_output_by_name (priv->display, output_name);
359 if (output)
360 wl_output = gst_wl_output_get_wl_output (output);
361 else
362 GST_WARNING ("Could not find any output named '%s'", output_name);
363 }
364
365 xdg_toplevel_set_fullscreen (priv->xdg_toplevel, wl_output);
366 wl_display_flush (gst_wl_display_get_display (priv->display));
367
368 // Unref last for thread safety
369 if (output)
370 g_object_unref (output);
371 }
372
373 /**
374 * gst_wl_window_ensure_fullscreen:
375 * @self: A #GstWlWindow
376 * @fullscreen: %TRUE to set fullscreen, %FALSE to unset it
377 *
378 * Same as gst_wl_window_ensure_fullscreen_for_output() without specifying an
379 * output.
380 */
381 void
382 gst_wl_window_ensure_fullscreen (GstWlWindow * self, gboolean fullscreen)
383 {
384 gst_wl_window_ensure_fullscreen_for_output (self, fullscreen, NULL);
385 }
386
387 GstWlWindow *
388 gst_wl_window_new_toplevel_full (GstWlDisplay * display,
389 const GstVideoInfo * info, gboolean fullscreen, const gchar * output_name,
390 GMutex * render_lock)
391 {
392 GstWlWindow *self;
393 GstWlWindowPrivate *priv;
394 struct xdg_wm_base *xdg_wm_base;
395 struct zwp_fullscreen_shell_v1 *fullscreen_shell;
396
397 self = gst_wl_window_new_internal (display, render_lock);
398 priv = gst_wl_window_get_instance_private (self);
399
400 xdg_wm_base = gst_wl_display_get_xdg_wm_base (display);
401 fullscreen_shell = gst_wl_display_get_fullscreen_shell_v1 (display);
402
403 /* Check which protocol we will use (in order of preference) */
404 if (xdg_wm_base) {
405 gint64 timeout;
406
407 /* First create the XDG surface */
408 priv->xdg_surface = xdg_wm_base_get_xdg_surface (xdg_wm_base,
409 priv->area_surface);
410 if (!priv->xdg_surface) {
411 GST_ERROR_OBJECT (self, "Unable to get xdg_surface");
412 goto error;
413 }
414 xdg_surface_add_listener (priv->xdg_surface, &xdg_surface_listener, self);
415
416 /* Then the toplevel */
417 priv->xdg_toplevel = xdg_surface_get_toplevel (priv->xdg_surface);
418 if (!priv->xdg_toplevel) {
419 GST_ERROR_OBJECT (self, "Unable to get xdg_toplevel");
420 goto error;
421 }
422 xdg_toplevel_add_listener (priv->xdg_toplevel,
423 &xdg_toplevel_listener, self);
424 if (g_get_prgname ()) {
425 xdg_toplevel_set_app_id (priv->xdg_toplevel, g_get_prgname ());
426 } else {
427 xdg_toplevel_set_app_id (priv->xdg_toplevel, "org.gstreamer.wayland");
428 }
429
430 gst_wl_window_ensure_fullscreen_for_output (self, fullscreen, output_name);
431
432 /* Finally, commit the xdg_surface state as toplevel */
433 priv->configured = FALSE;
434
435 /* set the initial size to be the same as the reported video size */
436 priv->default_width =
437 gst_util_uint64_scale_int_round (info->width, info->par_n, info->par_d);
438 priv->default_height = info->height;
439 gst_wl_window_set_render_rectangle (self, 0, 0, priv->default_width,
440 priv->default_height);
441
442 GST_INFO_OBJECT (self, "Configured default rectangle to %ix%i",
443 priv->default_width, priv->default_height);
444
445 wl_surface_commit (priv->area_surface);
446 wl_display_flush (gst_wl_display_get_display (display));
447
448 g_mutex_lock (&priv->configure_mutex);
449 timeout = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND;
450 while (!priv->configured) {
451 if (!g_cond_wait_until (&priv->configure_cond, &priv->configure_mutex,
452 timeout)) {
453 GST_WARNING_OBJECT (self,
454 "The compositor did not send configure event.");
455 break;
456 }
457 }
458 g_mutex_unlock (&priv->configure_mutex);
459 } else if (fullscreen_shell) {
460 GstWlOutput *output = NULL;
461 struct wl_output *wl_output = NULL;
462 if (output_name) {
463 output = gst_wl_display_get_output_by_name (priv->display, output_name);
464 wl_output = gst_wl_output_get_wl_output (output);
465 }
466
467 zwp_fullscreen_shell_v1_present_surface (fullscreen_shell,
468 priv->area_surface, ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM,
469 wl_output);
470
471 if (output)
472 gst_object_unref (output);
473 } else {
474 GST_ERROR_OBJECT (self,
475 "Unable to use either xdg_wm_base or zwp_fullscreen_shell.");
476 goto error;
477 }
478
479
480 return self;
481
482 error:
483 g_object_unref (self);
484 return NULL;
485 }
486
487 GstWlWindow *
488 gst_wl_window_new_toplevel (GstWlDisplay * display, const GstVideoInfo * info,
489 gboolean fullscreen, GMutex * render_lock)
490 {
491 return gst_wl_window_new_toplevel_full (display, info, fullscreen, NULL,
492 render_lock);
493 }
494
495 GstWlWindow *
496 gst_wl_window_new_in_surface (GstWlDisplay * display,
497 struct wl_surface *parent, GMutex * render_lock)
498 {
499 GstWlWindow *self;
500 GstWlWindowPrivate *priv;
501 struct wl_region *region;
502
503 self = gst_wl_window_new_internal (display, render_lock);
504 priv = gst_wl_window_get_instance_private (self);
505
506 /* do not accept input events on the area surface when embedded */
507 region =
508 wl_compositor_create_region (gst_wl_display_get_compositor (display));
509 wl_surface_set_input_region (priv->area_surface, region);
510 wl_region_destroy (region);
511
512 /* embed in parent */
513 priv->area_subsurface =
514 wl_subcompositor_get_subsurface (gst_wl_display_get_subcompositor
515 (display), priv->area_surface, parent);
516 wl_subsurface_set_desync (priv->area_subsurface);
517
518 wl_surface_commit (parent);
519
520 return self;
521 }
522
523 GstWlDisplay *
524 gst_wl_window_get_display (GstWlWindow * self)
525 {
526 GstWlWindowPrivate *priv;
527
528 g_return_val_if_fail (self != NULL, NULL);
529
530 priv = gst_wl_window_get_instance_private (self);
531 return g_object_ref (priv->display);
532 }
533
534 struct wl_surface *
535 gst_wl_window_get_wl_surface (GstWlWindow * self)
536 {
537 GstWlWindowPrivate *priv;
538
539 g_return_val_if_fail (self != NULL, NULL);
540
541 priv = gst_wl_window_get_instance_private (self);
542 return priv->video_surface_wrapper;
543 }
544
545 struct wl_subsurface *
546 gst_wl_window_get_subsurface (GstWlWindow * self)
547 {
548 GstWlWindowPrivate *priv;
549
550 g_return_val_if_fail (self != NULL, NULL);
551
552 priv = gst_wl_window_get_instance_private (self);
553 return priv->area_subsurface;
554 }
555
556 gboolean
557 gst_wl_window_is_toplevel (GstWlWindow * self)
558 {
559 GstWlWindowPrivate *priv;
560
561 g_return_val_if_fail (self != NULL, FALSE);
562
563 priv = gst_wl_window_get_instance_private (self);
564 return (priv->xdg_toplevel != NULL);
565 }
566
567 static void
568 gst_wl_window_resize_video_surface (GstWlWindow * self)
569 {
570 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
571 GstVideoRectangle src = { 0, };
572 GstVideoRectangle wp_src = { 0, };
573 GstVideoRectangle dst = { 0, };
574 GstVideoRectangle res;
575
576 /* viewport coordinates will be based on the trasnformed surface */
577 wl_surface_set_buffer_transform (priv->video_surface_wrapper,
578 priv->buffer_transform);
579
580 /* adjust the width/height base on the rotation */
581 switch (priv->buffer_transform) {
582 case WL_OUTPUT_TRANSFORM_NORMAL:
583 case WL_OUTPUT_TRANSFORM_180:
584 case WL_OUTPUT_TRANSFORM_FLIPPED:
585 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
586 src.w = priv->scaled_width;
587 src.h = priv->video_height;
588 wp_src.w = priv->crop.w;
589 wp_src.h = priv->crop.h;
590 break;
591 case WL_OUTPUT_TRANSFORM_90:
592 case WL_OUTPUT_TRANSFORM_270:
593 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
594 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
595 src.w = priv->video_height;
596 src.h = priv->scaled_width;
597 wp_src.w = priv->crop.h;
598 wp_src.h = priv->crop.w;
599 break;
600 default:
601 g_assert_not_reached ();
602 }
603
604 /* apply the x/y crop based on the transformation */
605 switch (priv->buffer_transform) {
606 case WL_OUTPUT_TRANSFORM_NORMAL:
607 wp_src.x = priv->crop.x;
608 wp_src.y = priv->crop.y;
609 break;
610 case WL_OUTPUT_TRANSFORM_180:
611 wp_src.x = priv->buffer_width - (priv->crop.w + priv->crop.x);
612 wp_src.y = priv->buffer_height - (priv->crop.h + priv->crop.y);
613 break;
614 case WL_OUTPUT_TRANSFORM_FLIPPED:
615 wp_src.x = priv->buffer_width - (priv->crop.w + priv->crop.x);
616 wp_src.y = priv->crop.y;
617 break;
618 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
619 wp_src.x = priv->crop.x;
620 wp_src.y = priv->buffer_height - (priv->crop.h + priv->crop.y);
621 break;
622 case WL_OUTPUT_TRANSFORM_90:
623 wp_src.x = priv->buffer_height - (priv->crop.h + priv->crop.y);
624 wp_src.y = priv->crop.x;
625 break;
626 case WL_OUTPUT_TRANSFORM_270:
627 wp_src.x = priv->crop.y;
628 wp_src.y = priv->buffer_width - (priv->crop.w + priv->crop.x);
629 break;
630 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
631 wp_src.x = priv->buffer_height - (priv->crop.h + priv->crop.y);
632 wp_src.y = priv->buffer_width - (priv->crop.w + priv->crop.x);
633 break;
634 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
635 wp_src.x = priv->crop.y;
636 wp_src.y = priv->crop.x;
637 break;
638
639 default:
640 g_assert_not_reached ();
641 }
642
643 dst.w = priv->render_rectangle.w;
644 dst.h = priv->render_rectangle.h;
645
646 /* center the video_subsurface inside area_subsurface */
647 if (priv->video_viewport) {
648 if (!priv->force_aspect_ratio)
649 res = dst;
650 else
651 gst_video_center_rect (&src, &dst, &res, TRUE);
652 wp_viewport_set_source (priv->video_viewport, wl_fixed_from_int (wp_src.x),
653 wl_fixed_from_int (wp_src.y), wl_fixed_from_int (wp_src.w),
654 wl_fixed_from_int (wp_src.h));
655
656 /* The protocol does not allow for a size set to 0 */
657 res.w = MAX (res.w, 1);
658 res.h = MAX (res.h, 1);
659
660 wp_viewport_set_destination (priv->video_viewport, res.w, res.h);
661 } else {
662 gst_video_center_rect (&src, &dst, &res, FALSE);
663 }
664
665 wl_subsurface_set_position (priv->video_subsurface, res.x, res.y);
666
667 priv->video_rectangle = res;
668 }
669
670 static void
671 gst_wl_window_set_opaque (GstWlWindow * self, const GstVideoInfo * info)
672 {
673 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
674 struct wl_compositor *compositor;
675 struct wl_region *region;
676
677 /* Set area opaque */
678 compositor = gst_wl_display_get_compositor (priv->display);
679 region = wl_compositor_create_region (compositor);
680 wl_region_add (region, 0, 0, G_MAXINT32, G_MAXINT32);
681 wl_surface_set_opaque_region (priv->area_surface, region);
682 wl_region_destroy (region);
683
684 if (!GST_VIDEO_INFO_HAS_ALPHA (info)) {
685 /* Set video opaque */
686 region = wl_compositor_create_region (compositor);
687 wl_region_add (region, 0, 0, G_MAXINT32, G_MAXINT32);
688 wl_surface_set_opaque_region (priv->video_surface, region);
689 wl_region_destroy (region);
690 }
691 }
692
693 static void
694 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
695 {
696 GstWlWindow *self = data;
697 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
698 GstWlBuffer *next_buffer;
699
700 GST_DEBUG_OBJECT (self, "frame_redraw_cb");
701
702 wl_callback_destroy (callback);
703 priv->frame_callback = NULL;
704
705 g_mutex_lock (&priv->window_lock);
706 next_buffer = priv->next_buffer = priv->staged_buffer;
707 priv->staged_buffer = NULL;
708 g_mutex_unlock (&priv->window_lock);
709
710 if (next_buffer || priv->clear_window)
711 gst_wl_window_commit_buffer (self, next_buffer);
712
713 if (next_buffer)
714 gst_wl_buffer_unref_buffer (next_buffer);
715 }
716
717 static const struct wl_callback_listener frame_callback_listener = {
718 frame_redraw_callback
719 };
720
721 static gboolean
722 gst_wl_window_crop_rectangle_changed (GstWlWindow * self,
723 const GstVideoRectangle * pending_crop)
724 {
725 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
726
727 if (priv->crop.x == pending_crop->x
728 && priv->crop.y == pending_crop->y
729 && priv->crop.w == pending_crop->w && priv->crop.h == pending_crop->h)
730 return FALSE;
731
732 return TRUE;
733 }
734
735 static void
736 gst_wl_window_commit_buffer (GstWlWindow * self, GstWlBuffer * buffer)
737 {
738 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
739 GstVideoInfo *info = priv->next_video_info;
740 GstVideoMasteringDisplayInfo *minfo = priv->next_minfo;
741 GstVideoContentLightLevel *linfo = priv->next_linfo;
742 struct wl_callback *callback;
743 gboolean needs_layout_update = FALSE;
744 GstVideoRectangle crop = priv->crop;
745
746 if (G_UNLIKELY (info)) {
747 priv->scaled_width =
748 gst_util_uint64_scale_int_round (info->width, info->par_n, info->par_d);
749 priv->video_width = priv->buffer_width = info->width;
750 priv->video_height = priv->buffer_height = info->height;
751
752 crop.w = priv->video_width;
753 crop.h = priv->video_height;
754
755 needs_layout_update = TRUE;
756 }
757
758 if (G_LIKELY (buffer)) {
759 GstVideoMeta *vmeta = gst_wl_buffer_get_video_meta (buffer);
760 GstVideoCropMeta *cmeta = gst_wl_buffer_get_video_crop_meta (buffer);
761
762 if (vmeta && (priv->buffer_width != vmeta->width
763 || priv->buffer_height != vmeta->height)) {
764 priv->buffer_width = vmeta->width;
765 priv->buffer_height = vmeta->height;
766 needs_layout_update = TRUE;
767 }
768
769 if (cmeta) {
770 crop.x = cmeta->x;
771 crop.y = cmeta->y;
772 crop.w = cmeta->width;
773 crop.h = cmeta->height;
774 }
775 }
776
777 if (gst_wl_window_crop_rectangle_changed (self, &crop)) {
778 priv->crop = crop;
779 needs_layout_update = TRUE;
780 }
781
782 if (G_UNLIKELY (needs_layout_update)) {
783 wl_subsurface_set_sync (priv->video_subsurface);
784 gst_wl_window_resize_video_surface (self);
785 gst_wl_window_set_opaque (self, info);
786
787 gst_wl_window_set_colorimetry (self, &info->colorimetry, minfo, linfo);
788 }
789
790 if (G_LIKELY (buffer)) {
791 callback = wl_surface_frame (priv->video_surface_wrapper);
792 priv->frame_callback = callback;
793 wl_callback_add_listener (callback, &frame_callback_listener, self);
794 gst_wl_buffer_attach (buffer, priv->video_surface_wrapper);
795 wl_surface_damage_buffer (priv->video_surface_wrapper, 0, 0, G_MAXINT32,
796 G_MAXINT32);
797 wl_surface_commit (priv->video_surface_wrapper);
798
799 if (!priv->is_area_surface_mapped) {
800 gst_wl_window_update_borders (self);
801 wl_surface_commit (priv->area_surface_wrapper);
802 priv->is_area_surface_mapped = TRUE;
803 g_signal_emit (self, signals[MAP], 0);
804 }
805 } else {
806 /* clear both video and parent surfaces */
807 wl_surface_attach (priv->video_surface_wrapper, NULL, 0, 0);
808 wl_surface_commit (priv->video_surface_wrapper);
809 wl_surface_attach (priv->area_surface_wrapper, NULL, 0, 0);
810 wl_surface_commit (priv->area_surface_wrapper);
811 priv->is_area_surface_mapped = FALSE;
812 priv->clear_window = FALSE;
813 }
814
815 if (G_UNLIKELY (needs_layout_update)) {
816 /* commit also the parent (area_surface) in order to change
817 * the position of the video_subsurface */
818 wl_surface_commit (priv->area_surface_wrapper);
819 wl_subsurface_set_desync (priv->video_subsurface);
820 gst_video_info_free (priv->next_video_info);
821 priv->next_video_info = NULL;
822 g_clear_pointer (&priv->next_minfo, g_free);
823 g_clear_pointer (&priv->next_linfo, g_free);
824 }
825
826 }
827
828 static void
829 commit_callback (void *data, struct wl_callback *callback, uint32_t serial)
830 {
831 GstWlWindow *self = data;
832 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
833 GstWlBuffer *next_buffer;
834
835 wl_callback_destroy (callback);
836 priv->commit_callback = NULL;
837
838 g_mutex_lock (&priv->window_lock);
839 next_buffer = priv->next_buffer;
840 g_mutex_unlock (&priv->window_lock);
841
842 gst_wl_window_commit_buffer (self, next_buffer);
843
844 if (next_buffer)
845 gst_wl_buffer_unref_buffer (next_buffer);
846 }
847
848 static const struct wl_callback_listener commit_listener = {
849 commit_callback
850 };
851
852 gboolean
853 gst_wl_window_render (GstWlWindow * self, GstWlBuffer * buffer,
854 const GstVideoInfo * info)
855 {
856 return gst_wl_window_render_hdr (self, buffer, info, NULL, NULL);
857 }
858
859 gboolean
860 gst_wl_window_render_hdr (GstWlWindow * self, GstWlBuffer * buffer,
861 const GstVideoInfo * info, const GstVideoMasteringDisplayInfo * minfo,
862 const GstVideoContentLightLevel * linfo)
863 {
864 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
865 gboolean ret = TRUE;
866
867 if (G_LIKELY (buffer))
868 gst_wl_buffer_ref_gst_buffer (buffer);
869
870 g_mutex_lock (&priv->window_lock);
871 if (G_UNLIKELY (info)) {
872 gst_video_info_free (priv->next_video_info);
873 priv->next_video_info = gst_video_info_copy (info);
874 }
875
876 if (G_UNLIKELY (minfo)) {
877 g_clear_pointer (&priv->next_minfo, g_free);
878 priv->next_minfo = g_memdup2 (minfo, sizeof (*minfo));
879 }
880
881 if (G_UNLIKELY (linfo)) {
882 g_clear_pointer (&priv->next_linfo, g_free);
883 priv->next_linfo = g_memdup2 (linfo, sizeof (*linfo));
884 }
885
886 if (priv->next_buffer && priv->staged_buffer) {
887 GST_LOG_OBJECT (self, "buffer %p dropped (replaced)", priv->staged_buffer);
888 gst_wl_buffer_unref_buffer (priv->staged_buffer);
889 ret = FALSE;
890 }
891
892 if (!priv->next_buffer) {
893 priv->next_buffer = buffer;
894 priv->commit_callback =
895 gst_wl_display_sync (priv->display, &commit_listener, self);
896 wl_display_flush (gst_wl_display_get_display (priv->display));
897 } else {
898 priv->staged_buffer = buffer;
899 }
900 if (!buffer)
901 priv->clear_window = TRUE;
902
903 g_mutex_unlock (&priv->window_lock);
904
905 return ret;
906 }
907
908 /**
909 * gst_wl_window_flush:
910 * @self: a #GstWlWindow
911 *
912 * Releases and drops the currently staged buffer associated with the window,
913 * if one exists. This function is thread-safe and will set the staged buffer
914 * pointer to NULL after unreferencing it.
915 *
916 * Returns: %TRUE if flush successful, %FALSE otherwise.
917 *
918 * Since: 1.28
919 */
920 gboolean
921 gst_wl_window_flush (GstWlWindow * self)
922 {
923 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
924
925 g_mutex_lock (&priv->window_lock);
926 if (priv->staged_buffer) {
927 GST_LOG_OBJECT (self, "drop buffer %p", priv->staged_buffer);
928 gst_wl_buffer_unref_buffer (priv->staged_buffer);
929 priv->staged_buffer = NULL;
930 }
931 g_mutex_unlock (&priv->window_lock);
932
933 return TRUE;
934 }
935
936 /* Update the buffer used to draw black borders. When we have viewporter
937 * support, this is a scaled up 1x1 image, and without we need an black image
938 * the size of the rendering areay. */
939 static void
940 gst_wl_window_update_borders (GstWlWindow * self)
941 {
942 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
943 gint width, height;
944 GstBuffer *buf;
945 struct wl_buffer *wlbuf;
946 struct wp_single_pixel_buffer_manager_v1 *single_pixel;
947 GstWlBuffer *gwlbuf;
948
949 if (gst_wl_display_get_viewporter (priv->display)) {
950 wp_viewport_set_destination (priv->area_viewport,
951 priv->render_rectangle.w, priv->render_rectangle.h);
952
953 if (priv->is_area_surface_mapped) {
954 /* The area_surface is already visible and only needed to get resized.
955 * We don't need to attach a new buffer and are done here. */
956 return;
957 }
958 }
959
960 if (gst_wl_display_get_viewporter (priv->display)) {
961 width = height = 1;
962 } else {
963 width = priv->render_rectangle.w;
964 height = priv->render_rectangle.h;
965 }
966
967 /* draw the area_subsurface */
968 single_pixel =
969 gst_wl_display_get_single_pixel_buffer_manager_v1 (priv->display);
970 if (width == 1 && height == 1 && single_pixel) {
971 buf = gst_buffer_new_allocate (NULL, 1, NULL);
972 wlbuf =
973 wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer (single_pixel,
974 0, 0, 0, 0xffffffffU);
975 } else {
976 GstVideoFormat format;
977 GstVideoInfo info;
978 GstAllocator *alloc;
979
980 /* we want WL_SHM_FORMAT_XRGB8888 */
981 format = GST_VIDEO_FORMAT_BGRx;
982 gst_video_info_set_format (&info, format, width, height);
983 alloc = gst_shm_allocator_get ();
984
985 buf = gst_buffer_new_allocate (alloc, info.size, NULL);
986 gst_buffer_memset (buf, 0, 0, info.size);
987
988 wlbuf =
989 gst_wl_shm_memory_construct_wl_buffer (gst_buffer_peek_memory (buf, 0),
990 priv->display, &info);
991
992 g_object_unref (alloc);
993 }
994
995 gwlbuf = gst_buffer_add_wl_buffer (buf, wlbuf, priv->display);
996 gst_wl_buffer_attach (gwlbuf, priv->area_surface_wrapper);
997 wl_surface_damage_buffer (priv->area_surface_wrapper, 0, 0, G_MAXINT32,
998 G_MAXINT32);
999
1000 /* at this point, the GstWlBuffer keeps the buffer
1001 * alive and will free it on wl_buffer::release */
1002 gst_buffer_unref (buf);
1003 }
1004
1005 static void
1006 gst_wl_window_update_geometry (GstWlWindow * self)
1007 {
1008 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
1009
1010 /* position the area inside the parent - needs a parent commit to apply */
1011 if (priv->area_subsurface) {
1012 wl_subsurface_set_position (priv->area_subsurface, priv->render_rectangle.x,
1013 priv->render_rectangle.y);
1014 }
1015
1016 if (priv->is_area_surface_mapped)
1017 gst_wl_window_update_borders (self);
1018
1019 if (!priv->configured)
1020 return;
1021
1022 if (priv->scaled_width != 0) {
1023 wl_subsurface_set_sync (priv->video_subsurface);
1024 gst_wl_window_resize_video_surface (self);
1025 wl_surface_commit (priv->video_surface_wrapper);
1026 }
1027
1028 wl_surface_commit (priv->area_surface_wrapper);
1029
1030 if (priv->scaled_width != 0)
1031 wl_subsurface_set_desync (priv->video_subsurface);
1032 }
1033
1034 void
1035 gst_wl_window_set_render_rectangle (GstWlWindow * self, gint x, gint y,
1036 gint w, gint h)
1037 {
1038 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
1039
1040 if (priv->render_rectangle.x == x && priv->render_rectangle.y == y &&
1041 priv->render_rectangle.w == w && priv->render_rectangle.h == h)
1042 return;
1043
1044 priv->render_rectangle.x = x;
1045 priv->render_rectangle.y = y;
1046 priv->render_rectangle.w = w;
1047 priv->render_rectangle.h = h;
1048
1049 gst_wl_window_update_geometry (self);
1050 }
1051
1052 const GstVideoRectangle *
1053 gst_wl_window_get_render_rectangle (GstWlWindow * self)
1054 {
1055 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
1056
1057 return &priv->render_rectangle;
1058 }
1059
1060 static enum wl_output_transform
1061 output_transform_from_orientation_method (GstVideoOrientationMethod method)
1062 {
1063 switch (method) {
1064 case GST_VIDEO_ORIENTATION_IDENTITY:
1065 return WL_OUTPUT_TRANSFORM_NORMAL;
1066 case GST_VIDEO_ORIENTATION_90R:
1067 return WL_OUTPUT_TRANSFORM_90;
1068 case GST_VIDEO_ORIENTATION_180:
1069 return WL_OUTPUT_TRANSFORM_180;
1070 case GST_VIDEO_ORIENTATION_90L:
1071 return WL_OUTPUT_TRANSFORM_270;
1072 case GST_VIDEO_ORIENTATION_HORIZ:
1073 return WL_OUTPUT_TRANSFORM_FLIPPED;
1074 case GST_VIDEO_ORIENTATION_VERT:
1075 return WL_OUTPUT_TRANSFORM_FLIPPED_180;
1076 case GST_VIDEO_ORIENTATION_UL_LR:
1077 return WL_OUTPUT_TRANSFORM_FLIPPED_90;
1078 case GST_VIDEO_ORIENTATION_UR_LL:
1079 return WL_OUTPUT_TRANSFORM_FLIPPED_270;
1080 default:
1081 g_assert_not_reached ();
1082 }
1083 }
1084
1085 void
1086 gst_wl_window_set_rotate_method (GstWlWindow * self,
1087 GstVideoOrientationMethod method)
1088 {
1089 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
1090
1091 priv->buffer_transform = output_transform_from_orientation_method (method);
1092
1093 gst_wl_window_update_geometry (self);
1094 }
1095
1096 enum ImageDescriptionFeedback
1097 {
1098 IMAGE_DESCRIPTION_FEEDBACK_UNKNOWN = 0,
1099 IMAGE_DESCRIPTION_FEEDBACK_READY,
1100 IMAGE_DESCRIPTION_FEEDBACK_FAILED,
1101 };
1102
1103 static void
1104 image_description_failed (void *data,
1105 struct wp_image_description_v1 *wp_image_description_v1, uint32_t cause,
1106 const char *msg)
1107 {
1108 enum ImageDescriptionFeedback *image_description_feedback = data;
1109
1110 *image_description_feedback = IMAGE_DESCRIPTION_FEEDBACK_FAILED;
1111 }
1112
1113 static void
1114 image_description_ready (void *data,
1115 struct wp_image_description_v1 *wp_image_description_v1, uint32_t identity)
1116 {
1117 enum ImageDescriptionFeedback *image_description_feedback = data;
1118
1119 *image_description_feedback = IMAGE_DESCRIPTION_FEEDBACK_READY;
1120 }
1121
1122 static const struct wp_image_description_v1_listener description_listerer = {
1123 .failed = image_description_failed,
1124 .ready = image_description_ready,
1125 };
1126
1127 static enum wp_color_manager_v1_transfer_function
1128 gst_colorimetry_tf_to_wl (GstVideoTransferFunction tf)
1129 {
1130 switch (tf) {
1131 case GST_VIDEO_TRANSFER_SRGB:
1132 return WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_SRGB;
1133 case GST_VIDEO_TRANSFER_BT601:
1134 case GST_VIDEO_TRANSFER_BT709:
1135 case GST_VIDEO_TRANSFER_BT2020_10:
1136 return WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_BT1886;
1137 case GST_VIDEO_TRANSFER_SMPTE2084:
1138 return WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_ST2084_PQ;
1139 case GST_VIDEO_TRANSFER_ARIB_STD_B67:
1140 return WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_HLG;
1141 default:
1142 GST_WARNING ("Transfer function not handled");
1143 return 0;
1144 }
1145 }
1146
1147 static enum wp_color_manager_v1_primaries
1148 gst_colorimetry_primaries_to_wl (GstVideoColorPrimaries primaries)
1149 {
1150 switch (primaries) {
1151 case GST_VIDEO_COLOR_PRIMARIES_BT709:
1152 return WP_COLOR_MANAGER_V1_PRIMARIES_SRGB;
1153 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
1154 return WP_COLOR_MANAGER_V1_PRIMARIES_NTSC;
1155 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
1156 return WP_COLOR_MANAGER_V1_PRIMARIES_BT2020;
1157 default:
1158 GST_WARNING ("Primaries not handled");
1159 return 0;
1160 }
1161 }
1162
1163 static enum wp_color_representation_surface_v1_coefficients
1164 gst_colorimetry_matrix_to_wl (GstVideoColorMatrix matrix)
1165 {
1166 switch (matrix) {
1167 case GST_VIDEO_COLOR_MATRIX_RGB:
1168 return WP_COLOR_REPRESENTATION_SURFACE_V1_COEFFICIENTS_IDENTITY;
1169 case GST_VIDEO_COLOR_MATRIX_BT709:
1170 return WP_COLOR_REPRESENTATION_SURFACE_V1_COEFFICIENTS_BT709;
1171 case GST_VIDEO_COLOR_MATRIX_BT601:
1172 return WP_COLOR_REPRESENTATION_SURFACE_V1_COEFFICIENTS_BT601;
1173 case GST_VIDEO_COLOR_MATRIX_BT2020:
1174 return WP_COLOR_REPRESENTATION_SURFACE_V1_COEFFICIENTS_BT2020;
1175 default:
1176 GST_WARNING ("Matrix not handled");
1177 return 0;
1178 }
1179 }
1180
1181 static enum wp_color_representation_surface_v1_range
1182 gst_colorimetry_range_to_wl (GstVideoColorRange range)
1183 {
1184 switch (range) {
1185 case GST_VIDEO_COLOR_RANGE_0_255:
1186 return WP_COLOR_REPRESENTATION_SURFACE_V1_RANGE_FULL;
1187 case GST_VIDEO_COLOR_RANGE_16_235:
1188 return WP_COLOR_REPRESENTATION_SURFACE_V1_RANGE_LIMITED;
1189 default:
1190 GST_WARNING ("Range not handled");
1191 return 0;
1192 }
1193 }
1194
1195 static void
1196 gst_wl_window_set_image_description (GstWlWindow * self,
1197 const GstVideoColorimetry * colorimetry,
1198 const GstVideoMasteringDisplayInfo * minfo,
1199 const GstVideoContentLightLevel * linfo)
1200 {
1201 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
1202 struct wl_display *wl_display;
1203 struct wp_color_manager_v1 *color_manager;
1204 struct wp_color_manager_v1 *color_manager_wrapper = NULL;
1205 struct wl_event_queue *color_manager_queue = NULL;
1206 struct wp_image_description_v1 *image_description = NULL;
1207 struct wp_image_description_creator_params_v1 *params;
1208 enum ImageDescriptionFeedback image_description_feedback =
1209 IMAGE_DESCRIPTION_FEEDBACK_UNKNOWN;
1210 uint32_t wl_transfer_function;
1211 uint32_t wl_primaries;
1212
1213 if (!gst_wl_display_is_color_parametric_creator_supported (priv->display)) {
1214 GST_INFO_OBJECT (self,
1215 "Color management or parametric creator not supported");
1216 return;
1217 }
1218
1219 color_manager = gst_wl_display_get_color_manager_v1 (priv->display);
1220 if (!priv->color_management_surface) {
1221 priv->color_management_surface =
1222 wp_color_manager_v1_get_surface (color_manager,
1223 priv->video_surface_wrapper);
1224 }
1225
1226 wl_transfer_function = gst_colorimetry_tf_to_wl (colorimetry->transfer);
1227 wl_primaries = gst_colorimetry_primaries_to_wl (colorimetry->primaries);
1228
1229 if (!gst_wl_display_is_color_transfer_function_supported (priv->display,
1230 wl_transfer_function) ||
1231 !gst_wl_display_are_color_primaries_supported (priv->display,
1232 wl_primaries)) {
1233 wp_color_management_surface_v1_unset_image_description
1234 (priv->color_management_surface);
1235
1236 GST_INFO_OBJECT (self,
1237 "Can not create image description: primaries or transfer function not supported");
1238 return;
1239 }
1240
1241 color_manager_wrapper = wl_proxy_create_wrapper (color_manager);
1242 wl_display = gst_wl_display_get_display (priv->display);
1243 #ifdef HAVE_WL_EVENT_QUEUE_NAME
1244 color_manager_queue = wl_display_create_queue_with_name (wl_display,
1245 "GStreamer color manager queue");
1246 #else
1247 color_manager_queue = wl_display_create_queue (wl_display);
1248 #endif
1249 wl_proxy_set_queue ((struct wl_proxy *) color_manager_wrapper,
1250 color_manager_queue);
1251
1252 params =
1253 wp_color_manager_v1_create_parametric_creator (color_manager_wrapper);
1254
1255 wp_image_description_creator_params_v1_set_tf_named (params,
1256 wl_transfer_function);
1257 wp_image_description_creator_params_v1_set_primaries_named (params,
1258 wl_primaries);
1259
1260 if (gst_wl_display_is_color_mastering_display_supported (priv->display)
1261 && minfo) {
1262 /* first validate our luminance range */
1263 guint min_luminance = minfo->min_display_mastering_luminance / 10000;
1264 guint max_luminance =
1265 MAX (min_luminance + 1, minfo->max_display_mastering_luminance / 10000);
1266
1267 /* We need to convert from 0.00002 unit to 0.000001 */
1268 const guint f = 20;
1269 wp_image_description_creator_params_v1_set_mastering_display_primaries
1270 (params,
1271 minfo->display_primaries[0].x * f, minfo->display_primaries[0].y * f,
1272 minfo->display_primaries[1].x * f, minfo->display_primaries[1].y * f,
1273 minfo->display_primaries[2].x * f, minfo->display_primaries[2].y * f,
1274 minfo->white_point.x * f, minfo->white_point.y * f);
1275 wp_image_description_creator_params_v1_set_mastering_luminance (params,
1276 minfo->min_display_mastering_luminance, max_luminance);
1277
1278 /*
1279 * FIXME its unclear what makes a color volume exceeds the primary volume,
1280 * and how to verify it, ignoring this aspect for now, but may need to be
1281 * revisited.
1282 */
1283
1284 /* We can't set the light level if we don't know the luminance range */
1285 if (linfo) {
1286 guint maxFALL = CLAMP (linfo->max_frame_average_light_level,
1287 min_luminance + 1, max_luminance);
1288 guint maxCLL =
1289 CLAMP (linfo->max_content_light_level, maxFALL, max_luminance);
1290 wp_image_description_creator_params_v1_set_max_cll (params, maxCLL);
1291 wp_image_description_creator_params_v1_set_max_fall (params, maxFALL);
1292 }
1293 }
1294
1295 image_description = wp_image_description_creator_params_v1_create (params);
1296 wp_image_description_v1_add_listener (image_description,
1297 &description_listerer, &image_description_feedback);
1298
1299 while (image_description_feedback == IMAGE_DESCRIPTION_FEEDBACK_UNKNOWN) {
1300 if (wl_display_dispatch_queue (wl_display, color_manager_queue) == -1)
1301 break;
1302 }
1303
1304 if (image_description_feedback == IMAGE_DESCRIPTION_FEEDBACK_READY) {
1305 wp_color_management_surface_v1_set_image_description
1306 (priv->color_management_surface, image_description,
1307 WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL);
1308
1309 GST_INFO_OBJECT (self, "Successfully set parametric image description");
1310 } else {
1311 wp_color_management_surface_v1_unset_image_description
1312 (priv->color_management_surface);
1313
1314 GST_INFO_OBJECT (self, "Creating image description failed");
1315 }
1316
1317 /* Setting the image description has copy semantics */
1318 wp_image_description_v1_destroy (image_description);
1319 wl_proxy_wrapper_destroy (color_manager_wrapper);
1320 wl_event_queue_destroy (color_manager_queue);
1321 }
1322
1323 static void
1324 gst_wl_window_set_color_representation (GstWlWindow * self,
1325 const GstVideoColorimetry * colorimetry)
1326 {
1327 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
1328 struct wp_color_representation_manager_v1 *cr_manager;
1329 uint32_t wl_alpha_mode;
1330 uint32_t wl_coefficients;
1331 uint32_t wl_range;
1332 gboolean alpha_mode_supported;
1333 gboolean coefficients_supported;
1334
1335 cr_manager =
1336 gst_wl_display_get_color_representation_manager_v1 (priv->display);
1337 if (!cr_manager) {
1338 GST_INFO_OBJECT (self, "Color representation not supported");
1339 return;
1340 }
1341
1342 wl_alpha_mode = WP_COLOR_REPRESENTATION_SURFACE_V1_ALPHA_MODE_STRAIGHT;
1343 alpha_mode_supported =
1344 gst_wl_display_is_color_alpha_mode_supported (priv->display,
1345 wl_alpha_mode);
1346
1347 wl_coefficients = gst_colorimetry_matrix_to_wl (colorimetry->matrix);
1348 wl_range = gst_colorimetry_range_to_wl (colorimetry->range);
1349 coefficients_supported =
1350 gst_wl_display_are_color_coefficients_supported (priv->display,
1351 wl_coefficients, wl_range);
1352
1353 if (alpha_mode_supported || coefficients_supported) {
1354 if (!priv->color_representation_surface) {
1355 priv->color_representation_surface =
1356 wp_color_representation_manager_v1_get_surface (cr_manager,
1357 priv->video_surface_wrapper);
1358 }
1359
1360 if (alpha_mode_supported)
1361 wp_color_representation_surface_v1_set_alpha_mode
1362 (priv->color_representation_surface, wl_alpha_mode);
1363
1364 if (coefficients_supported)
1365 wp_color_representation_surface_v1_set_coefficients_and_range
1366 (priv->color_representation_surface, wl_coefficients, wl_range);
1367
1368 GST_INFO_OBJECT (self, "Successfully set color representation");
1369 } else {
1370 if (priv->color_representation_surface) {
1371 wp_color_representation_surface_v1_destroy
1372 (priv->color_representation_surface);
1373 priv->color_representation_surface = NULL;
1374 }
1375
1376 GST_INFO_OBJECT (self, "Coefficients and range not supported");
1377 }
1378 }
1379
1380 static void
1381 gst_wl_window_set_colorimetry (GstWlWindow * self,
1382 const GstVideoColorimetry * colorimetry,
1383 const GstVideoMasteringDisplayInfo * minfo,
1384 const GstVideoContentLightLevel * linfo)
1385 {
1386 GST_OBJECT_LOCK (self);
1387
1388 GST_INFO_OBJECT (self, "Trying to set colorimetry: %s",
1389 gst_video_colorimetry_to_string (colorimetry));
1390
1391 gst_wl_window_set_image_description (self, colorimetry, minfo, linfo);
1392 gst_wl_window_set_color_representation (self, colorimetry);
1393
1394 GST_OBJECT_UNLOCK (self);
1395 }
1396
1397 void
1398 gst_wl_window_set_force_aspect_ratio (GstWlWindow * self,
1399 gboolean force_aspect_ratio)
1400 {
1401 GstWlWindowPrivate *priv = gst_wl_window_get_instance_private (self);
1402
1403 priv->force_aspect_ratio = force_aspect_ratio;
1404
1405 gst_wl_window_update_geometry (self);
1406 }