6ef4ae9b3e59415599692f63f6b55c39848db535
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidisplay.c
1 /*
2  *  gstvaapidisplay.c - VA display abstraction
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 2011-2012 Intel Corporation
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  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  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser 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 /**
24  * SECTION:gstvaapidisplay
25  * @short_description: VA display abstraction
26  */
27
28 #include "sysdeps.h"
29 #include <string.h>
30 #include "gstvaapiutils.h"
31 #include "gstvaapidisplay.h"
32 #include "gstvaapidisplay_priv.h"
33 #include "gstvaapiworkarounds.h"
34
35 #define DEBUG 1
36 #include "gstvaapidebug.h"
37
38 GST_DEBUG_CATEGORY(gst_debug_vaapi);
39
40 G_DEFINE_TYPE(GstVaapiDisplay, gst_vaapi_display, G_TYPE_OBJECT);
41
42 typedef struct _GstVaapiConfig GstVaapiConfig;
43 struct _GstVaapiConfig {
44     GstVaapiProfile     profile;
45     GstVaapiEntrypoint  entrypoint;
46 };
47
48 typedef struct _GstVaapiProperty GstVaapiProperty;
49 struct _GstVaapiProperty {
50     const gchar        *name;
51     VADisplayAttribute  attribute;
52 };
53
54 /* XXX: export property names when the API is stable enough */
55 #define GST_VAAPI_DISPLAY_PROP_RENDER_MODE      "render-mode"
56 #define GST_VAAPI_DISPLAY_PROP_ROTATION         "rotation"
57
58 enum {
59     PROP_0,
60
61     PROP_DISPLAY,
62     PROP_DISPLAY_TYPE,
63     PROP_WIDTH,
64     PROP_HEIGHT
65 };
66
67 static GstVaapiDisplayCache *g_display_cache = NULL;
68
69 static inline GstVaapiDisplayCache *
70 get_display_cache(void)
71 {
72     if (!g_display_cache)
73         g_display_cache = gst_vaapi_display_cache_new();
74     return g_display_cache;
75 }
76
77 GstVaapiDisplayCache *
78 gst_vaapi_display_get_cache(void)
79 {
80     return get_display_cache();
81 }
82
83 static void
84 free_display_cache(void)
85 {
86     if (!g_display_cache)
87         return;
88     if (gst_vaapi_display_cache_get_size(g_display_cache) > 0)
89         return;
90     gst_vaapi_display_cache_free(g_display_cache);
91     g_display_cache = NULL;
92 }
93
94 /* GstVaapiDisplayType enumerations */
95 GType
96 gst_vaapi_display_type_get_type(void)
97 {
98     static GType g_type = 0;
99
100     static const GEnumValue display_types[] = {
101         { GST_VAAPI_DISPLAY_TYPE_ANY,
102           "Auto detection", "any" },
103 #if USE_X11
104         { GST_VAAPI_DISPLAY_TYPE_X11,
105           "VA/X11 display", "x11" },
106 #endif
107 #if USE_GLX
108         { GST_VAAPI_DISPLAY_TYPE_GLX,
109           "VA/GLX display", "glx" },
110 #endif
111 #if USE_WAYLAND
112         { GST_VAAPI_DISPLAY_TYPE_WAYLAND,
113           "VA/Wayland display", "wayland" },
114 #endif
115 #if USE_DRM
116         { GST_VAAPI_DISPLAY_TYPE_DRM,
117           "VA/DRM display", "drm" },
118 #endif
119         { 0, NULL, NULL },
120     };
121
122     if (!g_type)
123         g_type = g_enum_register_static("GstVaapiDisplayType", display_types);
124     return g_type;
125 }
126
127 /* Append GstVaapiImageFormat to formats array */
128 static inline void
129 append_format(GArray *formats, GstVaapiImageFormat format)
130 {
131     g_array_append_val(formats, format);
132 }
133
134 /* Append VAImageFormats to formats array */
135 static void
136 append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
137 {
138     GstVaapiImageFormat format;
139     gboolean has_YV12 = FALSE;
140     gboolean has_I420 = FALSE;
141     guint i;
142
143     for (i = 0; i < n; i++) {
144         const VAImageFormat * const va_format = &va_formats[i];
145
146         format = gst_vaapi_image_format(va_format);
147         if (!format) {
148             GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
149                       GST_FOURCC_ARGS(va_format->fourcc));
150             continue;
151         }
152
153         switch (format) {
154         case GST_VAAPI_IMAGE_YV12:
155             has_YV12 = TRUE;
156             break;
157         case GST_VAAPI_IMAGE_I420:
158             has_I420 = TRUE;
159             break;
160         default:
161             break;
162         }
163         append_format(formats, format);
164     }
165
166     /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
167        supported by the underlying driver */
168     if (has_YV12 && !has_I420)
169         append_format(formats, GST_VAAPI_IMAGE_I420);
170     else if (has_I420 && !has_YV12)
171         append_format(formats, GST_VAAPI_IMAGE_YV12);
172 }
173
174 /* Sort image formats. Prefer YUV formats first */
175 static gint
176 compare_yuv_formats(gconstpointer a, gconstpointer b)
177 {
178     const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
179     const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
180
181     const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
182     const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);
183
184     if (is_fmt1_yuv != is_fmt2_yuv)
185         return is_fmt1_yuv ? -1 : 1;
186
187     return ((gint)gst_vaapi_image_format_get_score(fmt1) -
188             (gint)gst_vaapi_image_format_get_score(fmt2));
189 }
190
191 /* Sort subpicture formats. Prefer RGB formats first */
192 static gint
193 compare_rgb_formats(gconstpointer a, gconstpointer b)
194 {
195     const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
196     const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
197
198     const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
199     const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);
200
201     if (is_fmt1_rgb != is_fmt2_rgb)
202         return is_fmt1_rgb ? -1 : 1;
203
204     return ((gint)gst_vaapi_image_format_get_score(fmt1) -
205             (gint)gst_vaapi_image_format_get_score(fmt2));
206 }
207
208 /* Check if configs array contains profile at entrypoint */
209 static inline gboolean
210 find_config(
211     GArray             *configs,
212     GstVaapiProfile     profile,
213     GstVaapiEntrypoint  entrypoint
214 )
215 {
216     GstVaapiConfig *config;
217     guint i;
218
219     if (!configs)
220         return FALSE;
221
222     for (i = 0; i < configs->len; i++) {
223         config = &g_array_index(configs, GstVaapiConfig, i);
224         if (config->profile == profile && config->entrypoint == entrypoint)
225             return TRUE;
226     }
227     return FALSE;
228 }
229
230 /* HACK: append H.263 Baseline profile if MPEG-4:2 Simple profile is supported */
231 static void
232 append_h263_config(GArray *configs)
233 {
234     GstVaapiConfig *config, tmp_config;
235     GstVaapiConfig *mpeg4_simple_config = NULL;
236     GstVaapiConfig *h263_baseline_config = NULL;
237     guint i;
238
239     if (!WORKAROUND_H263_BASELINE_DECODE_PROFILE)
240         return;
241
242     if (!configs)
243         return;
244
245     for (i = 0; i < configs->len; i++) {
246         config = &g_array_index(configs, GstVaapiConfig, i);
247         if (config->profile == GST_VAAPI_PROFILE_MPEG4_SIMPLE)
248             mpeg4_simple_config = config;
249         else if (config->profile == GST_VAAPI_PROFILE_H263_BASELINE)
250             h263_baseline_config = config;
251     }
252
253     if (mpeg4_simple_config && !h263_baseline_config) {
254         tmp_config = *mpeg4_simple_config;
255         tmp_config.profile = GST_VAAPI_PROFILE_H263_BASELINE;
256         g_array_append_val(configs, tmp_config);
257     }
258 }
259
260 /* Convert configs array to profiles as GstCaps */
261 static GstCaps *
262 get_profile_caps(GArray *configs)
263 {
264     GstVaapiConfig *config;
265     GstCaps *out_caps, *caps;
266     guint i;
267
268     if (!configs)
269         return NULL;
270
271     out_caps = gst_caps_new_empty();
272     if (!out_caps)
273         return NULL;
274
275     for (i = 0; i < configs->len; i++) {
276         config = &g_array_index(configs, GstVaapiConfig, i);
277         caps   = gst_vaapi_profile_get_caps(config->profile);
278         if (caps)
279             gst_caps_merge(out_caps, caps);
280     }
281     return out_caps;
282 }
283
284 /* Check if formats array contains format */
285 static inline gboolean
286 find_format(GArray *formats, GstVaapiImageFormat format)
287 {
288     guint i;
289
290     for (i = 0; i < formats->len; i++)
291         if (g_array_index(formats, GstVaapiImageFormat, i) == format)
292             return TRUE;
293     return FALSE;
294 }
295
296 /* Convert formats array to GstCaps */
297 static GstCaps *
298 get_format_caps(GArray *formats)
299 {
300     GstVaapiImageFormat format;
301     GstCaps *out_caps, *caps;
302     guint i;
303
304     out_caps = gst_caps_new_empty();
305     if (!out_caps)
306         return NULL;
307
308     for (i = 0; i < formats->len; i++) {
309         format = g_array_index(formats, GstVaapiImageFormat, i);
310         caps   = gst_vaapi_image_format_get_caps(format);
311         if (caps)
312             gst_caps_append(out_caps, caps);
313     }
314     return out_caps;
315 }
316
317 /* Find display attribute */
318 static const GstVaapiProperty *
319 find_property(GArray *properties, const gchar *name)
320 {
321     GstVaapiProperty *prop;
322     guint i;
323
324     if (!name)
325         return NULL;
326
327     for (i = 0; i < properties->len; i++) {
328         prop = &g_array_index(properties, GstVaapiProperty, i);
329         if (strcmp(prop->name, name) == 0)
330             return prop;
331     }
332     return NULL;
333 }
334
335 #if 0
336 static const GstVaapiProperty *
337 find_property_by_type(GArray *properties, VADisplayAttribType type)
338 {
339     GstVaapiProperty *prop;
340     guint i;
341
342     for (i = 0; i < properties->len; i++) {
343         prop = &g_array_index(properties, GstVaapiProperty, i);
344         if (prop->attribute.type == type)
345             return prop;
346     }
347     return NULL;
348 }
349 #endif
350
351 static void
352 gst_vaapi_display_calculate_pixel_aspect_ratio(GstVaapiDisplay *display)
353 {
354     GstVaapiDisplayPrivate * const priv = display->priv;
355     gdouble ratio, delta;
356     gint i, index;
357
358     static const gint par[][2] = {
359         {1, 1},         /* regular screen            */
360         {16, 15},       /* PAL TV                    */
361         {11, 10},       /* 525 line Rec.601 video    */
362         {54, 59},       /* 625 line Rec.601 video    */
363         {64, 45},       /* 1280x1024 on 16:9 display */
364         {5, 3},         /* 1280x1024 on  4:3 display */
365         {4, 3}          /*  800x600  on 16:9 display */
366     };
367
368     /* First, calculate the "real" ratio based on the X values;
369      * which is the "physical" w/h divided by the w/h in pixels of the
370      * display */
371     if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
372         ratio = 1.0;
373     else
374         ratio = (gdouble)(priv->width_mm * priv->height) /
375             (priv->height_mm * priv->width);
376     GST_DEBUG("calculated pixel aspect ratio: %f", ratio);
377
378     /* Now, find the one from par[][2] with the lowest delta to the real one */
379 #define DELTA(idx) (ABS(ratio - ((gdouble)par[idx][0] / par[idx][1])))
380     delta = DELTA(0);
381     index = 0;
382
383     for (i = 1; i < G_N_ELEMENTS(par); i++) {
384         const gdouble this_delta = DELTA(i);
385         if (this_delta < delta) {
386             index = i;
387             delta = this_delta;
388         }
389     }
390 #undef DELTA
391
392     priv->par_n = par[index][0];
393     priv->par_d = par[index][1];
394 }
395
396 static void
397 gst_vaapi_display_destroy(GstVaapiDisplay *display)
398 {
399     GstVaapiDisplayPrivate * const priv = display->priv;
400
401     if (priv->decoders) {
402         g_array_free(priv->decoders, TRUE);
403         priv->decoders = NULL;
404     }
405
406     if (priv->encoders) {
407         g_array_free(priv->encoders, TRUE);
408         priv->encoders = NULL;
409     }
410
411     if (priv->image_formats) {
412         g_array_free(priv->image_formats, TRUE);
413         priv->image_formats = NULL;
414     }
415
416     if (priv->subpicture_formats) {
417         g_array_free(priv->subpicture_formats, TRUE);
418         priv->subpicture_formats = NULL;
419     }
420
421     if (priv->properties) {
422         g_array_free(priv->properties, TRUE);
423         priv->properties = NULL;
424     }
425
426     if (priv->display) {
427         if (!priv->parent)
428             vaTerminate(priv->display);
429         priv->display = NULL;
430     }
431
432     if (priv->create_display) {
433         GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
434         if (klass->close_display)
435             klass->close_display(display);
436     }
437
438     g_clear_object(&priv->parent);
439
440     if (g_display_cache) {
441         gst_vaapi_display_cache_remove(get_display_cache(), display);
442         free_display_cache();
443     }
444 }
445
446 static gboolean
447 gst_vaapi_display_create(GstVaapiDisplay *display)
448 {
449     GstVaapiDisplayPrivate * const priv = display->priv;
450     GstVaapiDisplayCache *cache;
451     gboolean            has_errors      = TRUE;
452     VADisplayAttribute *display_attrs   = NULL;
453     VAProfile          *profiles        = NULL;
454     VAEntrypoint       *entrypoints     = NULL;
455     VAImageFormat      *formats         = NULL;
456     unsigned int       *flags           = NULL;
457     gint                i, j, n, num_entrypoints, major_version, minor_version;
458     VAStatus            status;
459     GstVaapiDisplayInfo info;
460     const GstVaapiDisplayInfo *cached_info = NULL;
461
462     memset(&info, 0, sizeof(info));
463     info.display = display;
464     info.display_type = priv->display_type;
465
466     if (priv->display)
467         info.va_display = priv->display;
468     else if (priv->create_display) {
469         GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
470         if (klass->open_display && !klass->open_display(display))
471             return FALSE;
472         if (!klass->get_display || !klass->get_display(display, &info))
473             return FALSE;
474         priv->display = info.va_display;
475         priv->display_type = info.display_type;
476         if (klass->get_size)
477             klass->get_size(display, &priv->width, &priv->height);
478         if (klass->get_size_mm)
479             klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
480         gst_vaapi_display_calculate_pixel_aspect_ratio(display);
481     }
482     if (!priv->display)
483         return FALSE;
484
485     cache = get_display_cache();
486     if (!cache)
487         return FALSE;
488     cached_info = gst_vaapi_display_cache_lookup_by_va_display(
489         cache,
490         info.va_display
491     );
492     if (cached_info) {
493         g_clear_object(&priv->parent);
494         priv->parent = g_object_ref(cached_info->display);
495         priv->display_type = cached_info->display_type;
496     }
497
498     if (!priv->parent) {
499         status = vaInitialize(priv->display, &major_version, &minor_version);
500         if (!vaapi_check_status(status, "vaInitialize()"))
501             goto end;
502         GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
503     }
504
505     /* VA profiles */
506     profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
507     if (!profiles)
508         goto end;
509     entrypoints = g_new(VAEntrypoint, vaMaxNumEntrypoints(priv->display));
510     if (!entrypoints)
511         goto end;
512     status = vaQueryConfigProfiles(priv->display, profiles, &n);
513     if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
514         goto end;
515
516     GST_DEBUG("%d profiles", n);
517     for (i = 0; i < n; i++) {
518 #if VA_CHECK_VERSION(0,34,0)
519         /* Introduced in VA/VPP API */
520         if (profiles[i] == VAProfileNone)
521             continue;
522 #endif
523         GST_DEBUG("  %s", string_of_VAProfile(profiles[i]));
524     }
525
526     priv->decoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
527     if (!priv->decoders)
528         goto end;
529     priv->encoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
530     if (!priv->encoders)
531         goto end;
532
533     for (i = 0; i < n; i++) {
534         GstVaapiConfig config;
535
536         config.profile = gst_vaapi_profile(profiles[i]);
537         if (!config.profile)
538             continue;
539
540         status = vaQueryConfigEntrypoints(
541             priv->display,
542             profiles[i],
543             entrypoints, &num_entrypoints
544         );
545         if (!vaapi_check_status(status, "vaQueryConfigEntrypoints()"))
546             continue;
547
548         for (j = 0; j < num_entrypoints; j++) {
549             config.entrypoint = gst_vaapi_entrypoint(entrypoints[j]);
550             switch (config.entrypoint) {
551             case GST_VAAPI_ENTRYPOINT_VLD:
552             case GST_VAAPI_ENTRYPOINT_IDCT:
553             case GST_VAAPI_ENTRYPOINT_MOCO:
554                 g_array_append_val(priv->decoders, config);
555                 break;
556             case GST_VAAPI_ENTRYPOINT_SLICE_ENCODE:
557                 g_array_append_val(priv->encoders, config);
558                 break;
559             }
560         }
561     }
562     append_h263_config(priv->decoders);
563
564     /* VA display attributes */
565     display_attrs =
566         g_new(VADisplayAttribute, vaMaxNumDisplayAttributes(priv->display));
567     if (!display_attrs)
568         goto end;
569
570     n = 0; /* XXX: workaround old GMA500 bug */
571     status = vaQueryDisplayAttributes(priv->display, display_attrs, &n);
572     if (!vaapi_check_status(status, "vaQueryDisplayAttributes()"))
573         goto end;
574
575     priv->properties = g_array_new(FALSE, FALSE, sizeof(GstVaapiProperty));
576     if (!priv->properties)
577         goto end;
578
579     GST_DEBUG("%d display attributes", n);
580     for (i = 0; i < n; i++) {
581         VADisplayAttribute * const attr = &display_attrs[i];
582         GstVaapiProperty prop;
583
584         GST_DEBUG("  %s", string_of_VADisplayAttributeType(attr->type));
585
586         switch (attr->type) {
587 #if !VA_CHECK_VERSION(0,34,0)
588         case VADisplayAttribDirectSurface:
589             prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
590             break;
591 #endif
592         case VADisplayAttribRenderMode:
593             prop.name = GST_VAAPI_DISPLAY_PROP_RENDER_MODE;
594             break;
595         case VADisplayAttribRotation:
596             prop.name = GST_VAAPI_DISPLAY_PROP_ROTATION;
597             break;
598         default:
599             prop.name = NULL;
600             break;
601         }
602         if (!prop.name)
603             continue;
604         prop.attribute = *attr;
605         g_array_append_val(priv->properties, prop);
606     }
607
608     /* VA image formats */
609     formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
610     if (!formats)
611         goto end;
612     status = vaQueryImageFormats(priv->display, formats, &n);
613     if (!vaapi_check_status(status, "vaQueryImageFormats()"))
614         goto end;
615
616     GST_DEBUG("%d image formats", n);
617     for (i = 0; i < n; i++)
618         GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
619
620     priv->image_formats =
621         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
622     if (!priv->image_formats)
623         goto end;
624     append_formats(priv->image_formats, formats, n);
625     g_array_sort(priv->image_formats, compare_yuv_formats);
626
627     /* VA subpicture formats */
628     n = vaMaxNumSubpictureFormats(priv->display);
629     formats = g_renew(VAImageFormat, formats, n);
630     flags   = g_new(guint, n);
631     if (!formats || !flags)
632         goto end;
633     status = vaQuerySubpictureFormats(priv->display, formats, flags, (guint *)&n);
634     if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
635         goto end;
636
637     GST_DEBUG("%d subpicture formats", n);
638     for (i = 0; i < n; i++)
639         GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
640
641     priv->subpicture_formats =
642         g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
643     if (!priv->subpicture_formats)
644         goto end;
645     append_formats(priv->subpicture_formats, formats, n);
646     g_array_sort(priv->subpicture_formats, compare_rgb_formats);
647
648     if (!cached_info) {
649         if (!gst_vaapi_display_cache_add(cache, &info))
650             goto end;
651     }
652
653     has_errors = FALSE;
654 end:
655     g_free(display_attrs);
656     g_free(profiles);
657     g_free(entrypoints);
658     g_free(formats);
659     g_free(flags);
660     return !has_errors;
661 }
662
663 static void
664 gst_vaapi_display_lock_default(GstVaapiDisplay *display)
665 {
666     GstVaapiDisplayPrivate *priv = display->priv;
667
668     if (priv->parent)
669         priv = priv->parent->priv;
670     g_static_rec_mutex_lock(&priv->mutex);
671 }
672
673 static void
674 gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
675 {
676     GstVaapiDisplayPrivate *priv = display->priv;
677
678     if (priv->parent)
679         priv = priv->parent->priv;
680     g_static_rec_mutex_unlock(&priv->mutex);
681 }
682
683 static void
684 gst_vaapi_display_finalize(GObject *object)
685 {
686     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
687
688     gst_vaapi_display_destroy(display);
689
690     g_static_rec_mutex_free(&display->priv->mutex);
691
692     G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
693 }
694
695 static void
696 gst_vaapi_display_set_property(
697     GObject      *object,
698     guint         prop_id,
699     const GValue *value,
700     GParamSpec   *pspec
701 )
702 {
703     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
704
705     switch (prop_id) {
706     case PROP_DISPLAY:
707         display->priv->display = g_value_get_pointer(value);
708         break;
709     case PROP_DISPLAY_TYPE:
710         display->priv->display_type = g_value_get_enum(value);
711         break;
712     default:
713         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
714         break;
715     }
716 }
717
718 static void
719 gst_vaapi_display_get_property(
720     GObject    *object,
721     guint       prop_id,
722     GValue     *value,
723     GParamSpec *pspec
724 )
725 {
726     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
727
728     switch (prop_id) {
729     case PROP_DISPLAY:
730         g_value_set_pointer(value, gst_vaapi_display_get_display(display));
731         break;
732     case PROP_DISPLAY_TYPE:
733         g_value_set_enum(value, gst_vaapi_display_get_display_type(display));
734         break;
735     case PROP_WIDTH:
736         g_value_set_uint(value, gst_vaapi_display_get_width(display));
737         break;
738     case PROP_HEIGHT:
739         g_value_set_uint(value, gst_vaapi_display_get_height(display));
740         break;
741     default:
742         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
743         break;
744     }
745 }
746
747 static void
748 gst_vaapi_display_constructed(GObject *object)
749 {
750     GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
751     GObjectClass *parent_class;
752
753     display->priv->create_display = display->priv->display == NULL;
754     if (!gst_vaapi_display_create(display))
755         gst_vaapi_display_destroy(display);
756
757     parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
758     if (parent_class->constructed)
759         parent_class->constructed(object);
760 }
761
762 static void
763 gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
764 {
765     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
766     GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
767
768     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");
769
770     g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));
771
772     object_class->finalize      = gst_vaapi_display_finalize;
773     object_class->set_property  = gst_vaapi_display_set_property;
774     object_class->get_property  = gst_vaapi_display_get_property;
775     object_class->constructed   = gst_vaapi_display_constructed;
776
777     dpy_class->lock             = gst_vaapi_display_lock_default;
778     dpy_class->unlock           = gst_vaapi_display_unlock_default;
779
780     g_object_class_install_property
781         (object_class,
782          PROP_DISPLAY,
783          g_param_spec_pointer("display",
784                               "VA display",
785                               "VA display",
786                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
787
788     g_object_class_install_property
789         (object_class,
790          PROP_DISPLAY_TYPE,
791          g_param_spec_enum("display-type",
792                            "VA display type",
793                            "VA display type",
794                            GST_VAAPI_TYPE_DISPLAY_TYPE,
795                            GST_VAAPI_DISPLAY_TYPE_ANY,
796                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
797
798     g_object_class_install_property
799         (object_class,
800          PROP_WIDTH,
801          g_param_spec_uint("width",
802                            "Width",
803                            "The display width",
804                            1, G_MAXUINT32, 1,
805                            G_PARAM_READABLE));
806
807     g_object_class_install_property
808         (object_class,
809          PROP_HEIGHT,
810          g_param_spec_uint("height",
811                            "height",
812                            "The display height",
813                            1, G_MAXUINT32, 1,
814                            G_PARAM_READABLE));
815 }
816
817 static void
818 gst_vaapi_display_init(GstVaapiDisplay *display)
819 {
820     GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);
821
822     display->priv               = priv;
823     priv->parent                = NULL;
824     priv->display_type          = GST_VAAPI_DISPLAY_TYPE_ANY;
825     priv->display               = NULL;
826     priv->width                 = 0;
827     priv->height                = 0;
828     priv->width_mm              = 0;
829     priv->height_mm             = 0;
830     priv->par_n                 = 1;
831     priv->par_d                 = 1;
832     priv->decoders              = NULL;
833     priv->encoders              = NULL;
834     priv->image_formats         = NULL;
835     priv->subpicture_formats    = NULL;
836     priv->properties            = NULL;
837     priv->create_display        = TRUE;
838
839     g_static_rec_mutex_init(&priv->mutex);
840 }
841
842 /**
843  * gst_vaapi_display_new_with_display:
844  * @va_display: a #VADisplay
845  *
846  * Creates a new #GstVaapiDisplay, using @va_display as the VA
847  * display.
848  *
849  * Return value: the newly created #GstVaapiDisplay object
850  */
851 GstVaapiDisplay *
852 gst_vaapi_display_new_with_display(VADisplay va_display)
853 {
854     GstVaapiDisplayCache * const cache = get_display_cache();
855     const GstVaapiDisplayInfo *info;
856
857     g_return_val_if_fail(va_display != NULL, NULL);
858     g_return_val_if_fail(cache != NULL, NULL);
859
860     info = gst_vaapi_display_cache_lookup_by_va_display(cache, va_display);
861     if (info)
862         return g_object_ref(info->display);
863
864     return g_object_new(GST_VAAPI_TYPE_DISPLAY,
865                         "display", va_display,
866                         NULL);
867 }
868
869 /**
870  * gst_vaapi_display_lock:
871  * @display: a #GstVaapiDisplay
872  *
873  * Locks @display. If @display is already locked by another thread,
874  * the current thread will block until @display is unlocked by the
875  * other thread.
876  */
877 void
878 gst_vaapi_display_lock(GstVaapiDisplay *display)
879 {
880     GstVaapiDisplayClass *klass;
881
882     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
883
884     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
885     if (klass->lock)
886         klass->lock(display);
887 }
888
889 /**
890  * gst_vaapi_display_unlock:
891  * @display: a #GstVaapiDisplay
892  *
893  * Unlocks @display. If another thread is blocked in a
894  * gst_vaapi_display_lock() call for @display, it will be woken and
895  * can lock @display itself.
896  */
897 void
898 gst_vaapi_display_unlock(GstVaapiDisplay *display)
899 {
900     GstVaapiDisplayClass *klass;
901
902     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
903
904     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
905     if (klass->unlock)
906         klass->unlock(display);
907 }
908
909 /**
910  * gst_vaapi_display_sync:
911  * @display: a #GstVaapiDisplay
912  *
913  * Flushes any requests queued for the windowing system and waits until
914  * all requests have been handled. This is often used for making sure
915  * that the display is synchronized with the current state of the program.
916  *
917  * This is most useful for X11. On windowing systems where requests are
918  * handled synchronously, this function will do nothing.
919  */
920 void
921 gst_vaapi_display_sync(GstVaapiDisplay *display)
922 {
923     GstVaapiDisplayClass *klass;
924
925     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
926
927     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
928     if (klass->sync)
929         klass->sync(display);
930     else if (klass->flush)
931         klass->flush(display);
932 }
933
934 /**
935  * gst_vaapi_display_flush:
936  * @display: a #GstVaapiDisplay
937  *
938  * Flushes any requests queued for the windowing system.
939  *
940  * This is most useful for X11. On windowing systems where requests
941  * are handled synchronously, this function will do nothing.
942  */
943 void
944 gst_vaapi_display_flush(GstVaapiDisplay *display)
945 {
946     GstVaapiDisplayClass *klass;
947
948     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
949
950     klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
951     if (klass->flush)
952         klass->flush(display);
953 }
954
955 /**
956  * gst_vaapi_display_get_display:
957  * @display: a #GstVaapiDisplay
958  *
959  * Returns the #GstVaapiDisplayType bound to @display.
960  *
961  * Return value: the #GstVaapiDisplayType
962  */
963 GstVaapiDisplayType
964 gst_vaapi_display_get_display_type(GstVaapiDisplay *display)
965 {
966     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display),
967                          GST_VAAPI_DISPLAY_TYPE_ANY);
968
969     return display->priv->display_type;
970 }
971
972 /**
973  * gst_vaapi_display_get_display:
974  * @display: a #GstVaapiDisplay
975  *
976  * Returns the #VADisplay bound to @display.
977  *
978  * Return value: the #VADisplay
979  */
980 VADisplay
981 gst_vaapi_display_get_display(GstVaapiDisplay *display)
982 {
983     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
984
985     return display->priv->display;
986 }
987
988 /**
989  * gst_vaapi_display_get_width:
990  * @display: a #GstVaapiDisplay
991  *
992  * Retrieves the width of a #GstVaapiDisplay.
993  *
994  * Return value: the width of the @display, in pixels
995  */
996 guint
997 gst_vaapi_display_get_width(GstVaapiDisplay *display)
998 {
999     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
1000
1001     return display->priv->width;
1002 }
1003
1004 /**
1005  * gst_vaapi_display_get_height:
1006  * @display: a #GstVaapiDisplay
1007  *
1008  * Retrieves the height of a #GstVaapiDisplay
1009  *
1010  * Return value: the height of the @display, in pixels
1011  */
1012 guint
1013 gst_vaapi_display_get_height(GstVaapiDisplay *display)
1014 {
1015     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);
1016
1017     return display->priv->height;
1018 }
1019
1020 /**
1021  * gst_vaapi_display_get_size:
1022  * @display: a #GstVaapiDisplay
1023  * @pwidth: return location for the width, or %NULL
1024  * @pheight: return location for the height, or %NULL
1025  *
1026  * Retrieves the dimensions of a #GstVaapiDisplay.
1027  */
1028 void
1029 gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
1030 {
1031     g_return_if_fail(GST_VAAPI_DISPLAY(display));
1032
1033     if (pwidth)
1034         *pwidth = display->priv->width;
1035
1036     if (pheight)
1037         *pheight = display->priv->height;
1038 }
1039
1040 /**
1041  * gst_vaapi_display_get_pixel_aspect_ratio:
1042  * @display: a #GstVaapiDisplay
1043  * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
1044  * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
1045  *
1046  * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
1047  */
1048 void
1049 gst_vaapi_display_get_pixel_aspect_ratio(
1050     GstVaapiDisplay *display,
1051     guint           *par_n,
1052     guint           *par_d
1053 )
1054 {
1055     g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));
1056
1057     if (par_n)
1058         *par_n = display->priv->par_n;
1059
1060     if (par_d)
1061         *par_d = display->priv->par_d;
1062 }
1063
1064 /**
1065  * gst_vaapi_display_get_decode_caps:
1066  * @display: a #GstVaapiDisplay
1067  *
1068  * Gets the supported profiles for decoding as #GstCaps capabilities.
1069  *
1070  * Return value: a newly allocated #GstCaps object, possibly empty
1071  */
1072 GstCaps *
1073 gst_vaapi_display_get_decode_caps(GstVaapiDisplay *display)
1074 {
1075     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1076
1077     return get_profile_caps(display->priv->decoders);
1078 }
1079
1080 /**
1081  * gst_vaapi_display_has_decoder:
1082  * @display: a #GstVaapiDisplay
1083  * @profile: a #VAProfile
1084  * @entrypoint: a #GstVaaiEntrypoint
1085  *
1086  * Returns whether VA @display supports @profile for decoding at the
1087  * specified @entrypoint.
1088  *
1089  * Return value: %TRUE if VA @display supports @profile for decoding.
1090  */
1091 gboolean
1092 gst_vaapi_display_has_decoder(
1093     GstVaapiDisplay    *display,
1094     GstVaapiProfile     profile,
1095     GstVaapiEntrypoint  entrypoint
1096 )
1097 {
1098     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1099
1100     return find_config(display->priv->decoders, profile, entrypoint);
1101 }
1102
1103 /**
1104  * gst_vaapi_display_get_encode_caps:
1105  * @display: a #GstVaapiDisplay
1106  *
1107  * Gets the supported profiles for decoding as #GstCaps capabilities.
1108  *
1109  * Return value: a newly allocated #GstCaps object, possibly empty
1110  */
1111 GstCaps *
1112 gst_vaapi_display_get_encode_caps(GstVaapiDisplay *display)
1113 {
1114     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1115
1116     return get_profile_caps(display->priv->encoders);
1117 }
1118
1119 /**
1120  * gst_vaapi_display_has_encoder:
1121  * @display: a #GstVaapiDisplay
1122  * @profile: a #VAProfile
1123  * @entrypoint: a #GstVaapiEntrypoint
1124  *
1125  * Returns whether VA @display supports @profile for encoding at the
1126  * specified @entrypoint.
1127  *
1128  * Return value: %TRUE if VA @display supports @profile for encoding.
1129  */
1130 gboolean
1131 gst_vaapi_display_has_encoder(
1132     GstVaapiDisplay    *display,
1133     GstVaapiProfile     profile,
1134     GstVaapiEntrypoint  entrypoint
1135 )
1136 {
1137     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1138
1139     return find_config(display->priv->encoders, profile, entrypoint);
1140 }
1141
1142 /**
1143  * gst_vaapi_display_get_image_caps:
1144  * @display: a #GstVaapiDisplay
1145  *
1146  * Gets the supported image formats for gst_vaapi_surface_get_image()
1147  * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
1148  *
1149  * Note that this method does not necessarily map image formats
1150  * returned by vaQueryImageFormats(). The set of capabilities can be
1151  * stripped down, if gstreamer-vaapi does not support the format, or
1152  * expanded to cover compatible formats not exposed by the underlying
1153  * driver. e.g. I420 can be supported even if the driver only exposes
1154  * YV12.
1155  *
1156  * Return value: a newly allocated #GstCaps object, possibly empty
1157  */
1158 GstCaps *
1159 gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
1160 {
1161     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1162
1163     return get_format_caps(display->priv->image_formats);
1164 }
1165
1166 /**
1167  * gst_vaapi_display_has_image_format:
1168  * @display: a #GstVaapiDisplay
1169  * @format: a #GstVaapiFormat
1170  *
1171  * Returns whether VA @display supports @format image format.
1172  *
1173  * Return value: %TRUE if VA @display supports @format image format
1174  */
1175 gboolean
1176 gst_vaapi_display_has_image_format(
1177     GstVaapiDisplay    *display,
1178     GstVaapiImageFormat format
1179 )
1180 {
1181     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1182     g_return_val_if_fail(format, FALSE);
1183
1184     if (find_format(display->priv->image_formats, format))
1185         return TRUE;
1186
1187     /* XXX: try subpicture formats since some drivers could report a
1188      * set of VA image formats that is not a superset of the set of VA
1189      * subpicture formats
1190      */
1191     return find_format(display->priv->subpicture_formats, format);
1192 }
1193
1194 /**
1195  * gst_vaapi_display_get_subpicture_caps:
1196  * @display: a #GstVaapiDisplay
1197  *
1198  * Gets the supported subpicture formats as #GstCaps capabilities.
1199  *
1200  * Note that this method does not necessarily map subpicture formats
1201  * returned by vaQuerySubpictureFormats(). The set of capabilities can
1202  * be stripped down if gstreamer-vaapi does not support the
1203  * format. e.g. this is the case for paletted formats like IA44.
1204  *
1205  * Return value: a newly allocated #GstCaps object, possibly empty
1206  */
1207 GstCaps *
1208 gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
1209 {
1210     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1211
1212     return get_format_caps(display->priv->subpicture_formats);
1213 }
1214
1215 /**
1216  * gst_vaapi_display_has_subpicture_format:
1217  * @display: a #GstVaapiDisplay
1218  * @format: a #GstVaapiFormat
1219  *
1220  * Returns whether VA @display supports @format subpicture format.
1221  *
1222  * Return value: %TRUE if VA @display supports @format subpicture format
1223  */
1224 gboolean
1225 gst_vaapi_display_has_subpicture_format(
1226     GstVaapiDisplay    *display,
1227     GstVaapiImageFormat format
1228 )
1229 {
1230     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1231     g_return_val_if_fail(format, FALSE);
1232
1233     return find_format(display->priv->subpicture_formats, format);
1234 }
1235
1236 /**
1237  * gst_vaapi_display_has_property:
1238  * @display: a #GstVaapiDisplay
1239  * @name: the property name to check
1240  *
1241  * Returns whether VA @display supports the requested property. The
1242  * check is performed against the property @name. So, the client
1243  * application may perform this check only once and cache this
1244  * information.
1245  *
1246  * Return value: %TRUE if VA @display supports property @name
1247  */
1248 gboolean
1249 gst_vaapi_display_has_property(GstVaapiDisplay *display, const gchar *name)
1250 {
1251     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1252     g_return_val_if_fail(name, FALSE);
1253
1254     return find_property(display->priv->properties, name) != NULL;
1255 }
1256
1257 static gboolean
1258 get_attribute(GstVaapiDisplay *display, VADisplayAttribType type, gint *value)
1259 {
1260     VADisplayAttribute attr;
1261     VAStatus status;
1262
1263     attr.type  = type;
1264     attr.flags = VA_DISPLAY_ATTRIB_GETTABLE;
1265     status = vaGetDisplayAttributes(display->priv->display, &attr, 1);
1266     if (!vaapi_check_status(status, "vaGetDisplayAttributes()"))
1267         return FALSE;
1268     *value = attr.value;
1269     return TRUE;
1270 }
1271
1272 static gboolean
1273 set_attribute(GstVaapiDisplay *display, VADisplayAttribType type, gint value)
1274 {
1275     VADisplayAttribute attr;
1276     VAStatus status;
1277
1278     attr.type  = type;
1279     attr.value = value;
1280     attr.flags = VA_DISPLAY_ATTRIB_SETTABLE;
1281     status = vaSetDisplayAttributes(display->priv->display, &attr, 1);
1282     if (!vaapi_check_status(status, "vaSetDisplayAttributes()"))
1283         return FALSE;
1284     return TRUE;
1285 }
1286
1287 static gboolean
1288 get_render_mode_VADisplayAttribRenderMode(
1289     GstVaapiDisplay    *display,
1290     GstVaapiRenderMode *pmode
1291 )
1292 {
1293     gint modes, devices;
1294
1295     if (!get_attribute(display, VADisplayAttribRenderDevice, &devices))
1296         return FALSE;
1297     if (!devices)
1298         return FALSE;
1299     if (!get_attribute(display, VADisplayAttribRenderMode, &modes))
1300         return FALSE;
1301
1302     /* Favor "overlay" mode since it is the most restrictive one */
1303     if (modes & (VA_RENDER_MODE_LOCAL_OVERLAY|VA_RENDER_MODE_EXTERNAL_OVERLAY))
1304         *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1305     else
1306         *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1307     return TRUE;
1308 }
1309
1310 static gboolean
1311 get_render_mode_VADisplayAttribDirectSurface(
1312     GstVaapiDisplay    *display,
1313     GstVaapiRenderMode *pmode
1314 )
1315 {
1316 #if VA_CHECK_VERSION(0,34,0)
1317     /* VADisplayAttribDirectsurface was removed in VA-API >= 0.34.0 */
1318     return FALSE;
1319 #else
1320     gint direct_surface;
1321
1322     if (!get_attribute(display, VADisplayAttribDirectSurface, &direct_surface))
1323         return FALSE;
1324     if (direct_surface)
1325         *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1326     else
1327         *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1328     return TRUE;
1329 #endif
1330 }
1331
1332 static gboolean
1333 get_render_mode_default(
1334     GstVaapiDisplay    *display,
1335     GstVaapiRenderMode *pmode
1336 )
1337 {
1338     switch (display->priv->display_type) {
1339 #if USE_WAYLAND
1340     case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1341         /* wl_buffer mapped from VA surface through vaGetSurfaceBufferWl() */
1342         *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1343         break;
1344 #endif
1345 #if USE_DRM
1346     case GST_VAAPI_DISPLAY_TYPE_DRM:
1347         /* vaGetSurfaceBufferDRM() returns the underlying DRM buffer handle */
1348         *pmode = GST_VAAPI_RENDER_MODE_OVERLAY;
1349         break;
1350 #endif
1351     default:
1352         /* This includes VA/X11 and VA/GLX modes */
1353         *pmode = GST_VAAPI_RENDER_MODE_TEXTURE;
1354         break;
1355     }
1356     return TRUE;
1357 }
1358
1359 /**
1360  * gst_vaapi_display_get_render_mode:
1361  * @display: a #GstVaapiDisplay
1362  * @pmode: return location for the VA @display rendering mode
1363  *
1364  * Returns the current VA @display rendering mode.
1365  *
1366  * Return value: %TRUE if VA @display rendering mode could be determined
1367  */
1368 gboolean
1369 gst_vaapi_display_get_render_mode(
1370     GstVaapiDisplay    *display,
1371     GstVaapiRenderMode *pmode
1372 )
1373 {
1374     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1375
1376     /* Try with render-mode attribute */
1377     if (get_render_mode_VADisplayAttribRenderMode(display, pmode))
1378         return TRUE;
1379
1380     /* Try with direct-surface attribute */
1381     if (get_render_mode_VADisplayAttribDirectSurface(display, pmode))
1382         return TRUE;
1383
1384     /* Default: determine from the display type */
1385     return get_render_mode_default(display, pmode);
1386 }
1387
1388 /**
1389  * gst_vaapi_display_set_render_mode:
1390  * @display: a #GstVaapiDisplay
1391  * @mode: the #GstVaapiRenderMode to set
1392  *
1393  * Sets the VA @display rendering mode to the supplied @mode. This
1394  * function returns %FALSE if the rendering mode could not be set,
1395  * e.g. run-time switching rendering mode is not supported.
1396  *
1397  * Return value: %TRUE if VA @display rendering @mode could be changed
1398  *   to the requested value
1399  */
1400 gboolean
1401 gst_vaapi_display_set_render_mode(
1402     GstVaapiDisplay   *display,
1403     GstVaapiRenderMode mode
1404 )
1405 {
1406     gint modes, devices;
1407
1408     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1409
1410     if (!get_attribute(display, VADisplayAttribRenderDevice, &devices))
1411         return FALSE;
1412
1413     modes = 0;
1414     switch (mode) {
1415     case GST_VAAPI_RENDER_MODE_OVERLAY:
1416         if (devices & VA_RENDER_DEVICE_LOCAL)
1417             modes |= VA_RENDER_MODE_LOCAL_OVERLAY;
1418         if (devices & VA_RENDER_DEVICE_EXTERNAL)
1419             modes |= VA_RENDER_MODE_EXTERNAL_OVERLAY;
1420         break;
1421     case GST_VAAPI_RENDER_MODE_TEXTURE:
1422         if (devices & VA_RENDER_DEVICE_LOCAL)
1423             modes |= VA_RENDER_MODE_LOCAL_GPU;
1424         if (devices & VA_RENDER_DEVICE_EXTERNAL)
1425             modes |= VA_RENDER_MODE_EXTERNAL_GPU;
1426         break;
1427     }
1428     if (!modes)
1429         return FALSE;
1430     if (!set_attribute(display, VADisplayAttribRenderMode, modes))
1431         return FALSE;
1432     return TRUE;
1433 }
1434
1435 /**
1436  * gst_vaapi_display_get_rotation:
1437  * @display: a #GstVaapiDisplay
1438  *
1439  * Returns the current VA @display rotation angle. If the VA driver
1440  * does not support "rotation" display attribute, then the display is
1441  * assumed to be un-rotated.
1442  *
1443  * Return value: the current #GstVaapiRotation value
1444  */
1445 GstVaapiRotation
1446 gst_vaapi_display_get_rotation(GstVaapiDisplay *display)
1447 {
1448     gint value;
1449
1450     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), GST_VAAPI_ROTATION_0);
1451
1452     if (!get_attribute(display, VADisplayAttribRotation, &value))
1453         value = VA_ROTATION_NONE;
1454     return to_GstVaapiRotation(value);
1455 }
1456
1457 /**
1458  * gst_vaapi_display_set_rotation:
1459  * @display: a #GstVaapiDisplay
1460  * @rotation: the #GstVaapiRotation value to set
1461  *
1462  * Sets the VA @display rotation angle to the supplied @rotation
1463  * value. This function returns %FALSE if the rotation angle could not
1464  * be set, e.g. the VA driver does not allow to change the display
1465  * rotation angle.
1466  *
1467  * Return value: %TRUE if VA @display rotation angle could be changed
1468  *   to the requested value
1469  */
1470 gboolean
1471 gst_vaapi_display_set_rotation(
1472     GstVaapiDisplay *display,
1473     GstVaapiRotation rotation
1474 )
1475 {
1476     guint value;
1477
1478     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1479
1480     value = from_GstVaapiRotation(rotation);
1481     if (!set_attribute(display, VADisplayAttribRotation, value))
1482         return FALSE;
1483     return TRUE;
1484 }