packaging: add packaging folder and update submodule pkgs
[platform/upstream/gstreamer-vaapi.git] / tests / test-display.c
1 /*
2  *  test-display.c - Test GstVaapiDisplayX11
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6  *  Copyright (C) 2012-2013 Intel Corporation
7  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public License
11  *  as published by the Free Software Foundation; either version 2.1
12  *  of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free
21  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  *  Boston, MA 02110-1301 USA
23  */
24
25 #include "gst/vaapi/sysdeps.h"
26 #include <gst/video/video.h>
27 #if USE_DRM
28 # include <gst/vaapi/gstvaapidisplay_drm.h>
29 # include <va/va_drm.h>
30 # include <fcntl.h>
31 # include <unistd.h>
32 # ifndef DRM_DEVICE_PATH
33 # define DRM_DEVICE_PATH "/dev/dri/card0"
34 # endif
35 #endif
36 #if USE_X11
37 # include <gst/vaapi/gstvaapidisplay_x11.h>
38 #endif
39 #if USE_GLX
40 # include <gst/vaapi/gstvaapidisplay_glx.h>
41 #endif
42 #if USE_WAYLAND
43 # include <gst/vaapi/gstvaapidisplay_wayland.h>
44 #endif
45
46 #ifdef HAVE_VA_VA_GLX_H
47 # include <va/va_glx.h>
48 #endif
49
50 /* Set to 1 to check display cache works (shared VA display) */
51 #define CHECK_DISPLAY_CACHE 1
52
53 static void
54 print_value(const GValue *value, const gchar *name)
55 {
56     gchar *value_string;
57
58     value_string = g_strdup_value_contents(value);
59     if (!value_string)
60         return;
61     g_print("  %s: %s\n", name, value_string);
62     g_free(value_string);
63 }
64
65 static void
66 print_profiles(GArray *profiles, const gchar *name)
67 {
68     GstVaapiCodec codec;
69     const gchar *codec_name, *profile_name;
70     guint i;
71
72     g_print("%u %s caps\n", profiles->len, name);
73
74     for (i = 0; i < profiles->len; i++) {
75         const GstVaapiProfile profile =
76             g_array_index(profiles, GstVaapiProfile, i);
77
78         codec = gst_vaapi_profile_get_codec(profile);
79         if (!codec)
80             continue;
81
82         codec_name = gst_vaapi_codec_get_name(codec);
83         if (!codec_name)
84             continue;
85
86         profile_name = gst_vaapi_profile_get_name(profile);
87         if (!profile_name)
88             continue;
89
90         g_print("  %s: %s profile\n", codec_name, profile_name);
91     }
92 }
93
94 static void
95 print_format_yuv(const VAImageFormat *va_format)
96 {
97     const guint32 fourcc = va_format->fourcc;
98
99     g_print(" fourcc '%c%c%c%c'",
100             fourcc & 0xff,
101             (fourcc >> 8) & 0xff,
102             (fourcc >> 16) & 0xff,
103             (fourcc >> 24) & 0xff);
104 }
105
106 static void
107 print_format_rgb(const VAImageFormat *va_format)
108 {
109     g_print(" %d bits per pixel, %s endian,",
110             va_format->bits_per_pixel,
111             va_format->byte_order == VA_MSB_FIRST ? "big" : "little");
112     g_print(" %s masks", va_format->alpha_mask ? "rgba" : "rgb");
113     g_print(" 0x%08x 0x%08x 0x%08x",
114             va_format->red_mask, va_format->green_mask, va_format->blue_mask);
115     if (va_format->alpha_mask)
116         g_print(" 0x%08x", va_format->alpha_mask);
117 }
118
119 static void
120 print_formats(GArray *formats, const gchar *name)
121 {
122     guint i;
123
124     g_print("%u %s caps\n", formats->len, name);
125
126     for (i = 0; i < formats->len; i++) {
127         const GstVideoFormat format = g_array_index(formats, GstVideoFormat, i);
128         const VAImageFormat *va_format;
129
130         g_print("  %s:", gst_vaapi_video_format_to_string(format));
131
132         va_format = gst_vaapi_video_format_to_va_format(format);
133         if (!va_format)
134             g_error("could not determine VA format");
135
136         if (gst_vaapi_video_format_is_yuv(format))
137             print_format_yuv(va_format);
138         else
139             print_format_rgb(va_format);
140         g_print("\n");
141     }
142 }
143
144 typedef struct _GstVaapiDisplayProperty GstVaapiDisplayProperty;
145 struct _GstVaapiDisplayProperty {
146     const gchar *name;
147     GValue       value;
148 };
149
150 static void
151 gst_vaapi_display_property_free(GstVaapiDisplayProperty *prop)
152 {
153     if (!prop)
154         return;
155     g_value_unset(&prop->value);
156     g_slice_free(GstVaapiDisplayProperty, prop);
157 }
158
159 static GstVaapiDisplayProperty *
160 gst_vaapi_display_property_new(const gchar *name)
161 {
162     GstVaapiDisplayProperty *prop;
163
164     prop = g_slice_new0(GstVaapiDisplayProperty);
165     if (!prop)
166         return NULL;
167     prop->name = name;
168     return prop;
169 }
170
171 static void
172 free_property_cb(gpointer data, gpointer user_data)
173 {
174     gst_vaapi_display_property_free(data);
175 }
176
177 static void
178 dump_properties(GstVaapiDisplay *display)
179 {
180     GstVaapiDisplayProperty *prop;
181     GPtrArray *properties;
182     guint i;
183
184     static const gchar *g_properties[] = {
185         GST_VAAPI_DISPLAY_PROP_RENDER_MODE,
186         GST_VAAPI_DISPLAY_PROP_ROTATION,
187         GST_VAAPI_DISPLAY_PROP_HUE,
188         GST_VAAPI_DISPLAY_PROP_SATURATION,
189         GST_VAAPI_DISPLAY_PROP_BRIGHTNESS,
190         GST_VAAPI_DISPLAY_PROP_CONTRAST,
191         NULL
192     };
193
194     properties = g_ptr_array_new();
195     if (!properties)
196         return;
197
198     for (i = 0; g_properties[i] != NULL; i++) {
199         const gchar * const name = g_properties[i];
200
201         if (!gst_vaapi_display_has_property(display, name))
202             continue;
203             
204         prop = gst_vaapi_display_property_new(name);
205         if (!prop) {
206             GST_ERROR("failed to allocate GstVaapiDisplayProperty");
207             goto end;
208         }
209
210         if (!gst_vaapi_display_get_property(display, name, &prop->value)) {
211             GST_ERROR("failed to get property '%s'", name);
212             goto end;
213         }
214         g_ptr_array_add(properties, prop);
215     }
216
217     g_print("%u properties\n", properties->len);
218     for (i = 0; i < properties->len; i++) {
219         prop = g_ptr_array_index(properties, i);
220         print_value(&prop->value, prop->name);
221     }
222
223 end:
224     if (properties) {
225         g_ptr_array_foreach(properties, free_property_cb, NULL);
226         g_ptr_array_free(properties, TRUE);
227     }
228 }
229
230 static void
231 dump_info(GstVaapiDisplay *display)
232 {
233     GArray *profiles, *formats;
234
235     profiles = gst_vaapi_display_get_decode_profiles(display);
236     if (!profiles)
237         g_error("could not get VA decode profiles");
238
239     print_profiles(profiles, "decoders");
240     g_array_unref(profiles);
241
242     profiles = gst_vaapi_display_get_encode_profiles(display);
243     if (!profiles)
244         g_error("could not get VA encode profiles");
245
246     print_profiles(profiles, "encoders");
247     g_array_unref(profiles);
248
249     formats = gst_vaapi_display_get_image_formats(display);
250     if (!formats)
251         g_error("could not get VA image formats");
252
253     print_formats(formats, "image");
254     g_array_unref(formats);
255
256     formats = gst_vaapi_display_get_subpicture_formats(display);
257     if (!formats)
258         g_error("could not get VA subpicture formats");
259
260     print_formats(formats, "subpicture");
261     g_array_unref(formats);
262
263     dump_properties(display);
264 }
265
266 int
267 main(int argc, char *argv[])
268 {
269     GstVaapiDisplay *display, *display2;
270     guint width, height, par_n, par_d;
271
272     gst_init(&argc, &argv);
273
274 #if USE_DRM
275     g_print("#\n");
276     g_print("# Create display with gst_vaapi_display_drm_new()\n");
277     g_print("#\n");
278     {
279         display = gst_vaapi_display_drm_new(NULL);
280         if (!display)
281             g_error("could not create Gst/VA display");
282
283         dump_info(display);
284         gst_vaapi_display_unref(display);
285     }
286     g_print("\n");
287
288     g_print("#\n");
289     g_print("# Create display with gst_vaapi_display_drm_new_with_device()\n");
290     g_print("#\n");
291     {
292         int drm_device;
293
294         drm_device = open(DRM_DEVICE_PATH, O_RDWR|O_CLOEXEC);
295         if (drm_device < 0)
296             g_error("could not open DRM device");
297
298         display = gst_vaapi_display_drm_new_with_device(drm_device);
299         if (!display)
300             g_error("could not create Gst/VA display");
301
302         dump_info(display);
303         gst_vaapi_display_unref(display);
304         close(drm_device);
305     }
306     g_print("\n");
307
308     g_print("#\n");
309     g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayDRM()]\n");
310     g_print("#\n");
311     {
312         int drm_device;
313         VADisplay va_display;
314
315         drm_device = open(DRM_DEVICE_PATH, O_RDWR|O_CLOEXEC);
316         if (drm_device < 0)
317             g_error("could not open DRM device");
318
319         va_display = vaGetDisplayDRM(drm_device);
320         if (!va_display)
321             g_error("could not create VA display");
322
323         display = gst_vaapi_display_new_with_display(va_display);
324         if (!display)
325             g_error("could not create Gst/VA display");
326
327         dump_info(display);
328         gst_vaapi_display_unref(display);
329         close(drm_device);
330     }
331     g_print("\n");
332 #endif
333
334 #if USE_X11
335     g_print("#\n");
336     g_print("# Create display with gst_vaapi_display_x11_new()\n");
337     g_print("#\n");
338     {
339         display = gst_vaapi_display_x11_new(NULL);
340         if (!display)
341             g_error("could not create Gst/VA display");
342
343         if (CHECK_DISPLAY_CACHE) {
344             display2 = gst_vaapi_display_x11_new(NULL);
345
346             /* Check for the same X11 display */
347             g_assert(gst_vaapi_display_x11_get_display(
348                          GST_VAAPI_DISPLAY_X11(display)) ==
349                      gst_vaapi_display_x11_get_display(
350                          GST_VAAPI_DISPLAY_X11(display2)));
351
352             /* Check for the same VA display */
353             g_assert(gst_vaapi_display_get_display(display) ==
354                      gst_vaapi_display_get_display(display2));
355
356             gst_vaapi_display_unref(display2);
357
358 #if USE_GLX
359             display2 = gst_vaapi_display_glx_new(NULL);
360
361             /* Check for the different X11 display */
362             /* XXX: it is also desired to cache underlying X11 displays */
363             g_assert(gst_vaapi_display_x11_get_display(
364                          GST_VAAPI_DISPLAY_X11(display)) !=
365                      gst_vaapi_display_x11_get_display(
366                          GST_VAAPI_DISPLAY_X11(display2)));
367
368             /* Check for different VA display */
369             g_assert(gst_vaapi_display_get_display(display) !=
370                      gst_vaapi_display_get_display(display2));
371
372             gst_vaapi_display_unref(display2);
373 #endif
374         }
375
376         gst_vaapi_display_get_size(display, &width, &height);
377         g_print("Display size: %ux%u\n", width, height);
378
379         gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
380         g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);
381
382         dump_info(display);
383         gst_vaapi_display_unref(display);
384     }
385     g_print("\n");
386
387     g_print("#\n");
388     g_print("# Create display with gst_vaapi_display_x11_new_with_display()\n");
389     g_print("#\n");
390     {
391         Display *x11_display;
392
393         x11_display = XOpenDisplay(NULL);
394         if (!x11_display)
395             g_error("could not create X11 display");
396
397         display = gst_vaapi_display_x11_new_with_display(x11_display);
398         if (!display)
399             g_error("could not create Gst/VA display");
400
401         if (CHECK_DISPLAY_CACHE) {
402             display2 = gst_vaapi_display_x11_new_with_display(x11_display);
403
404             /* Check for the same VA display */
405             g_assert(gst_vaapi_display_get_display(display) ==
406                      gst_vaapi_display_get_display(display2));
407
408             gst_vaapi_display_unref(display2);
409         }
410
411         dump_info(display);
412         gst_vaapi_display_unref(display);
413         XCloseDisplay(x11_display);
414     }
415     g_print("\n");
416
417     g_print("#\n");
418     g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplay()]\n");
419     g_print("#\n");
420     {
421         Display *x11_display;
422         VADisplay va_display;
423
424         x11_display = XOpenDisplay(NULL);
425         if (!x11_display)
426             g_error("could not create X11 display");
427
428         va_display = vaGetDisplay(x11_display);
429         if (!va_display)
430             g_error("could not create VA display");
431
432         display = gst_vaapi_display_new_with_display(va_display);
433         if (!display)
434             g_error("could not create Gst/VA display");
435
436         dump_info(display);
437         gst_vaapi_display_unref(display);
438         XCloseDisplay(x11_display);
439     }
440     g_print("\n");
441 #endif
442
443 #if USE_GLX
444     g_print("#\n");
445     g_print("# Create display with gst_vaapi_display_glx_new()\n");
446     g_print("#\n");
447     {
448         display = gst_vaapi_display_glx_new(NULL);
449         if (!display)
450             g_error("could not create Gst/VA display");
451
452         if (CHECK_DISPLAY_CACHE) {
453             display2 = gst_vaapi_display_glx_new(NULL);
454
455             /* Check for the same X11 display */
456             g_assert(gst_vaapi_display_x11_get_display(
457                          GST_VAAPI_DISPLAY_X11(display)) ==
458                      gst_vaapi_display_x11_get_display(
459                          GST_VAAPI_DISPLAY_X11(display2)));
460
461             /* Check for the same VA display */
462             g_assert(gst_vaapi_display_get_display(display) ==
463                      gst_vaapi_display_get_display(display2));
464
465             gst_vaapi_display_unref(display2);
466
467             display2 = gst_vaapi_display_x11_new(NULL);
468
469             /* Check for the same X11 display */
470             g_assert(gst_vaapi_display_x11_get_display(
471                          GST_VAAPI_DISPLAY_X11(display)) ==
472                      gst_vaapi_display_x11_get_display(
473                          GST_VAAPI_DISPLAY_X11(display2)));
474
475             /* Check for the same VA display */
476             g_assert(gst_vaapi_display_get_display(display) ==
477                      gst_vaapi_display_get_display(display2));
478
479             gst_vaapi_display_unref(display2);
480         }
481
482         gst_vaapi_display_get_size(display, &width, &height);
483         g_print("Display size: %ux%u\n", width, height);
484
485         gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
486         g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);
487
488         dump_info(display);
489         gst_vaapi_display_unref(display);
490     }
491     g_print("\n");
492
493     g_print("#\n");
494     g_print("# Create display with gst_vaapi_display_glx_new_with_display()\n");
495     g_print("#\n");
496     {
497         Display *x11_display;
498
499         x11_display = XOpenDisplay(NULL);
500         if (!x11_display)
501             g_error("could not create X11 display");
502
503         display = gst_vaapi_display_glx_new_with_display(x11_display);
504         if (!display)
505             g_error("could not create Gst/VA display");
506
507         dump_info(display);
508         gst_vaapi_display_unref(display);
509         XCloseDisplay(x11_display);
510     }
511     g_print("\n");
512
513 #ifdef HAVE_VA_VA_GLX_H
514     g_print("#\n");
515     g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayGLX()]\n");
516     g_print("#\n");
517     {
518         Display *x11_display;
519         VADisplay va_display;
520
521         x11_display = XOpenDisplay(NULL);
522         if (!x11_display)
523             g_error("could not create X11 display");
524
525         va_display = vaGetDisplayGLX(x11_display);
526         if (!va_display)
527             g_error("could not create VA display");
528
529         display = gst_vaapi_display_new_with_display(va_display);
530         if (!display)
531             g_error("could not create Gst/VA display");
532
533         dump_info(display);
534         gst_vaapi_display_unref(display);
535         XCloseDisplay(x11_display);
536     }
537     g_print("\n");
538 #endif
539 #endif
540
541 #if USE_WAYLAND
542     g_print("#\n");
543     g_print("# Create display with gst_vaapi_display_wayland_new()\n");
544     g_print("#\n");
545     {
546         display = gst_vaapi_display_wayland_new(NULL);
547         if (!display)
548             g_error("could not create Gst/VA display");
549
550         gst_vaapi_display_get_size(display, &width, &height);
551         g_print("Display size: %ux%u\n", width, height);
552
553         gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
554         g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);
555
556         dump_info(display);
557         gst_vaapi_display_unref(display);
558     }
559     g_print("\n");
560 #endif
561
562     gst_deinit();
563     return 0;
564 }