tests: image: fix string representation for GstVideoFormat.
[platform/upstream/gstreamer-vaapi.git] / tests / test-windows.c
1 /*
2  *  test-windows.c - Test GstVaapiWindow
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 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 #include "gst/vaapi/sysdeps.h"
24 #include <gst/vaapi/gstvaapisurface.h>
25 #include <gst/vaapi/gstvaapiimage.h>
26 #if USE_DRM
27 # include <gst/vaapi/gstvaapidisplay_drm.h>
28 # include <gst/vaapi/gstvaapiwindow_drm.h>
29 #endif
30 #if USE_X11
31 # include <gst/vaapi/gstvaapidisplay_x11.h>
32 # include <gst/vaapi/gstvaapiwindow_x11.h>
33 #endif
34 #if USE_WAYLAND
35 # include <gst/vaapi/gstvaapidisplay_wayland.h>
36 # include <gst/vaapi/gstvaapiwindow_wayland.h>
37 #endif
38 #include "image.h"
39
40 static inline void
41 pause(void)
42 {
43     g_print("Press any key to continue...\n");
44     getchar();
45 }
46
47 static GstVaapiSurface *
48 create_test_surface(GstVaapiDisplay *display, guint width, guint height)
49 {
50     GstVaapiImage *image = NULL;
51     GstVaapiSurface *surface;
52     guint i;
53
54     static const GstVaapiChromaType  chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
55     static const GstVideoFormat image_formats[] = {
56         GST_VIDEO_FORMAT_NV12,
57         GST_VIDEO_FORMAT_YV12,
58         GST_VIDEO_FORMAT_I420,
59         GST_VIDEO_FORMAT_AYUV,
60         GST_VIDEO_FORMAT_ARGB,
61         GST_VIDEO_FORMAT_BGRA,
62         GST_VIDEO_FORMAT_RGBA,
63         GST_VIDEO_FORMAT_ABGR,
64         GST_VIDEO_FORMAT_UNKNOWN
65     };
66
67     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
68     if (!surface)
69         g_error("could not create Gst/VA surface");
70
71     for (i = 0; image_formats[i] != GST_VIDEO_FORMAT_UNKNOWN; i++) {
72         const GstVideoFormat format = image_formats[i];
73
74         image = image_generate(display, format, width, height);
75         if (!image)
76             break;
77         if (image_upload(image, surface))
78             break;
79     }
80     if (!image)
81         g_error("could not create Gst/VA image");
82
83     if (!gst_vaapi_surface_sync(surface))
84         g_error("could not complete image upload");
85
86     gst_vaapi_object_unref(image);
87     return surface;
88 }
89
90 int
91 main(int argc, char *argv[])
92 {
93     GstVaapiDisplay *display;
94     GstVaapiWindow *window;
95     GstVaapiSurface *surface;
96     guint flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
97
98     static const guint width       = 320;
99     static const guint height      = 240;
100     static const guint win_width   = 640;
101     static const guint win_height  = 480;
102
103     gst_init(&argc, &argv);
104
105 #if USE_DRM
106     display = gst_vaapi_display_drm_new(NULL);
107     if (!display)
108         g_error("could not create Gst/VA (DRM) display");
109
110     surface = create_test_surface(display, width, height);
111     if (!surface)
112         g_error("could not create Gst/VA surface");
113
114     g_print("#\n");
115     g_print("# Create window with gst_vaapi_window_drm_new()\n");
116     g_print("#\n");
117     {
118         window = gst_vaapi_window_drm_new(display, win_width, win_height);
119         if (!window)
120             g_error("could not create dummy window");
121
122         gst_vaapi_window_show(window);
123
124         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
125             g_error("could not render surface");
126
127         pause();
128         gst_vaapi_window_unref(window);
129     }
130
131     gst_vaapi_object_unref(surface);
132     gst_vaapi_display_unref(display);
133 #endif
134
135 #if USE_X11
136     display = gst_vaapi_display_x11_new(NULL);
137     if (!display)
138         g_error("could not create Gst/VA display");
139
140     surface = create_test_surface(display, width, height);
141     if (!surface)
142         g_error("could not create Gst/VA surface");
143
144     g_print("#\n");
145     g_print("# Create window with gst_vaapi_window_x11_new()\n");
146     g_print("#\n");
147     {
148         window = gst_vaapi_window_x11_new(display, win_width, win_height);
149         if (!window)
150             g_error("could not create window");
151
152         gst_vaapi_window_show(window);
153
154         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
155             g_error("could not render surface");
156
157         pause();
158         gst_vaapi_window_unref(window);
159     }
160
161     g_print("#\n");
162     g_print("# Create window with gst_vaapi_window_x11_new_with_xid()\n");
163     g_print("#\n");
164     {
165         Display * const dpy = gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(display));
166         Window rootwin, win;
167         int screen;
168         unsigned long white_pixel, black_pixel;
169
170         screen      = DefaultScreen(dpy);
171         rootwin     = RootWindow(dpy, screen);
172         white_pixel = WhitePixel(dpy, screen);
173         black_pixel = BlackPixel(dpy, screen);
174
175         win = XCreateSimpleWindow(
176             dpy,
177             rootwin,
178             0, 0, win_width, win_height,
179             0, black_pixel,
180             white_pixel
181         );
182         if (!win)
183             g_error("could not create X window");
184
185         window = gst_vaapi_window_x11_new_with_xid(display, win);
186         if (!window)
187             g_error("could not create window");
188
189         gst_vaapi_window_show(window);
190
191         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
192             g_error("could not render surface");
193
194         pause();
195         gst_vaapi_window_unref(window);
196         XUnmapWindow(dpy, win);
197         XDestroyWindow(dpy, win);
198     }
199
200     gst_vaapi_object_unref(surface);
201     gst_vaapi_display_unref(display);
202 #endif
203
204 #if USE_WAYLAND
205     display = gst_vaapi_display_wayland_new(NULL);
206     if (!display)
207         g_error("could not create Gst/VA (Wayland) display");
208
209     surface = create_test_surface(display, width, height);
210     if (!surface)
211         g_error("could not create Gst/VA surface");
212
213     g_print("#\n");
214     g_print("# Create window with gst_vaapi_window_wayland_new()\n");
215     g_print("#\n");
216     {
217         window = gst_vaapi_window_wayland_new(display, win_width, win_height);
218         if (!window)
219             g_error("could not create window");
220
221         gst_vaapi_window_show(window);
222
223         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
224             g_error("could not render surface");
225
226         pause();
227         gst_vaapi_window_unref(window);
228     }
229
230     gst_vaapi_object_unref(surface);
231     gst_vaapi_display_unref(display);
232 #endif
233
234     gst_deinit();
235     return 0;
236 }