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