1 /*
2  * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include "sysdeps.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <string.h>
30 #include <va/va.h>
31 #include "va_display.h"
32 
33 extern const VADisplayHooks va_display_hooks_android;
34 extern const VADisplayHooks va_display_hooks_wayland;
35 extern const VADisplayHooks va_display_hooks_x11;
36 extern const VADisplayHooks va_display_hooks_drm;
37 
38 static const VADisplayHooks *g_display_hooks;
39 static const VADisplayHooks *g_display_hooks_available[] = {
40 #ifdef ANDROID
41     &va_display_hooks_android,
42 #else
43 #ifdef HAVE_VA_WAYLAND
44     &va_display_hooks_wayland,
45 #endif
46 #ifdef HAVE_VA_X11
47     &va_display_hooks_x11,
48 #endif
49 #ifdef HAVE_VA_DRM
50     &va_display_hooks_drm,
51 #endif
52 #endif
53     NULL
54 };
55 
56 static const char *g_display_name;
57 
58 static const char *
get_display_name(int argc,char * argv[])59 get_display_name(int argc, char *argv[])
60 {
61     const char *display_name = NULL;
62     int i;
63 
64     for (i = 1; i < argc; i++) {
65         if (strcmp(argv[i], "--display") != 0)
66             continue;
67         argv[i] = NULL;
68 
69         if (++i < argc) {
70             display_name = argv[i];
71             argv[i] = NULL;
72         }
73     }
74     return display_name;
75 }
76 
77 static void
print_display_names(void)78 print_display_names(void)
79 {
80     const VADisplayHooks **h;
81 
82     printf("Available displays:\n");
83     for (h = g_display_hooks_available; *h != NULL; h++)
84         printf("  %s\n", (*h)->name);
85 }
86 
87 static void
sanitize_args(int * argc,char * argv[])88 sanitize_args(int *argc, char *argv[])
89 {
90     char **out_args = argv;
91     int i, n = *argc;
92 
93     for (i = 0; i < n; i++) {
94         if (argv[i])
95             *out_args++ = argv[i];
96     }
97     *out_args = NULL;
98     *argc = out_args - argv;
99 }
100 
101 void
va_init_display_args(int * argc,char * argv[])102 va_init_display_args(int *argc, char *argv[])
103 {
104     const char *display_name;
105 
106     display_name = get_display_name(*argc, argv);
107     if (display_name && strcmp(display_name, "help") == 0) {
108         print_display_names();
109         exit(0);
110     }
111     g_display_name = display_name;
112 
113     sanitize_args(argc, argv);
114 }
115 
116 VADisplay
va_open_display(void)117 va_open_display(void)
118 {
119     VADisplay va_dpy = NULL;
120     unsigned int i;
121 
122     for (i = 0; !va_dpy && g_display_hooks_available[i]; i++) {
123         g_display_hooks = g_display_hooks_available[i];
124         if (g_display_name &&
125             strcmp(g_display_name, g_display_hooks->name) != 0)
126             continue;
127         if (!g_display_hooks->open_display)
128             continue;
129         va_dpy = g_display_hooks->open_display();
130     }
131 
132     if (!va_dpy)  {
133         fprintf(stderr, "error: failed to initialize display");
134         if (g_display_name)
135             fprintf(stderr, " '%s'", g_display_name);
136         fprintf(stderr, "\n");
137         abort();
138     }
139     return va_dpy;
140 }
141 
142 void
va_close_display(VADisplay va_dpy)143 va_close_display(VADisplay va_dpy)
144 {
145     if (!va_dpy)
146         return;
147 
148     if (g_display_hooks && g_display_hooks->close_display)
149         g_display_hooks->close_display(va_dpy);
150 }
151 
152 VAStatus
va_put_surface(VADisplay va_dpy,VASurfaceID surface,const VARectangle * src_rect,const VARectangle * dst_rect)153 va_put_surface(
154     VADisplay          va_dpy,
155     VASurfaceID        surface,
156     const VARectangle *src_rect,
157     const VARectangle *dst_rect
158 )
159 {
160     if (!va_dpy)
161         return VA_STATUS_ERROR_INVALID_DISPLAY;
162 
163     if (g_display_hooks && g_display_hooks->put_surface)
164         return g_display_hooks->put_surface(va_dpy, surface, src_rect, dst_rect);
165     return VA_STATUS_ERROR_UNIMPLEMENTED;
166 }
167