change encoder log output format, support GST log
[profile/ivi/gstreamer-vaapi.git] / tests / test-windows.c
1 /*
2  *  test-windows.c - Test GstVaapiWindow
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23 #include <gst/vaapi/gstvaapisurface.h>
24 #include <gst/vaapi/gstvaapiimage.h>
25 #if USE_DRM
26 # include <gst/vaapi/gstvaapidisplay_drm.h>
27 # include <gst/vaapi/gstvaapiwindow_drm.h>
28 #endif
29 #if USE_X11
30 # include <gst/vaapi/gstvaapidisplay_x11.h>
31 # include <gst/vaapi/gstvaapiwindow_x11.h>
32 #endif
33 #if USE_WAYLAND
34 # include <gst/vaapi/gstvaapidisplay_wayland.h>
35 # include <gst/vaapi/gstvaapiwindow_wayland.h>
36 #endif
37 #include "image.h"
38
39 static inline void
40 pause(void)
41 {
42     g_print("Press any key to continue...\n");
43     getchar();
44 }
45
46 static GstVaapiSurface *
47 create_test_surface(GstVaapiDisplay *display, guint width, guint height)
48 {
49     GstVaapiImage *image = NULL;
50     GstVaapiSurface *surface;
51     guint i;
52
53     static const GstVaapiChromaType  chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
54     static const GstVaapiImageFormat image_formats[] = {
55         GST_VAAPI_IMAGE_NV12,
56         GST_VAAPI_IMAGE_YV12,
57         GST_VAAPI_IMAGE_I420,
58         GST_VAAPI_IMAGE_AYUV,
59         GST_VAAPI_IMAGE_ARGB,
60         GST_VAAPI_IMAGE_BGRA,
61         GST_VAAPI_IMAGE_RGBA,
62         GST_VAAPI_IMAGE_ABGR,
63         0
64     };
65
66     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
67     if (!surface)
68         g_error("could not create Gst/VA surface");
69
70     for (i = 0; image_formats[i]; i++) {
71         const GstVaapiImageFormat format = image_formats[i];
72
73         image = image_generate(display, format, width, height);
74         if (!image)
75             break;
76         if (image_upload(image, surface))
77             break;
78     }
79     if (!image)
80         g_error("could not create Gst/VA image");
81
82     if (!gst_vaapi_surface_sync(surface))
83         g_error("could not complete image upload");
84
85     g_object_unref(image);
86     return surface;
87 }
88
89 int
90 main(int argc, char *argv[])
91 {
92     GstVaapiDisplay *display;
93     GstVaapiWindow *window;
94     GstVaapiSurface *surface;
95     guint flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
96
97     static const guint width       = 320;
98     static const guint height      = 240;
99     static const guint win_width   = 640;
100     static const guint win_height  = 480;
101
102     gst_init(&argc, &argv);
103
104 #if USE_DRM
105     display = gst_vaapi_display_drm_new(NULL);
106     if (!display)
107         g_error("could not create Gst/VA (DRM) display");
108
109     surface = create_test_surface(display, width, height);
110     if (!surface)
111         g_error("could not create Gst/VA surface");
112
113     g_print("#\n");
114     g_print("# Create window with gst_vaapi_window_drm_new()\n");
115     g_print("#\n");
116     {
117         window = gst_vaapi_window_drm_new(display, win_width, win_height);
118         if (!window)
119             g_error("could not create dummy window");
120
121         gst_vaapi_window_show(window);
122
123         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
124             g_error("could not render surface");
125
126         pause();
127         g_object_unref(window);
128     }
129
130     g_object_unref(surface);
131     g_object_unref(display);
132 #endif
133
134 #if USE_X11
135     display = gst_vaapi_display_x11_new(NULL);
136     if (!display)
137         g_error("could not create Gst/VA display");
138
139     surface = create_test_surface(display, width, height);
140     if (!surface)
141         g_error("could not create Gst/VA surface");
142
143     g_print("#\n");
144     g_print("# Create window with gst_vaapi_window_x11_new()\n");
145     g_print("#\n");
146     {
147         window = gst_vaapi_window_x11_new(display, win_width, win_height);
148         if (!window)
149             g_error("could not create window");
150
151         gst_vaapi_window_show(window);
152
153         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
154             g_error("could not render surface");
155
156         pause();
157         g_object_unref(window);
158     }
159
160     g_print("#\n");
161     g_print("# Create window with gst_vaapi_window_x11_new_with_xid()\n");
162     g_print("#\n");
163     {
164         Display * const dpy = gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(display));
165         Window rootwin, win;
166         int screen;
167         unsigned long white_pixel, black_pixel;
168
169         screen      = DefaultScreen(dpy);
170         rootwin     = RootWindow(dpy, screen);
171         white_pixel = WhitePixel(dpy, screen);
172         black_pixel = BlackPixel(dpy, screen);
173
174         win = XCreateSimpleWindow(
175             dpy,
176             rootwin,
177             0, 0, win_width, win_height,
178             0, black_pixel,
179             white_pixel
180         );
181         if (!win)
182             g_error("could not create X window");
183
184         window = gst_vaapi_window_x11_new_with_xid(display, win);
185         if (!window)
186             g_error("could not create window");
187
188         gst_vaapi_window_show(window);
189
190         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
191             g_error("could not render surface");
192
193         pause();
194         g_object_unref(window);
195         XUnmapWindow(dpy, win);
196         XDestroyWindow(dpy, win);
197     }
198
199     g_object_unref(surface);
200     g_object_unref(display);
201 #endif
202
203 #if USE_WAYLAND
204     display = gst_vaapi_display_wayland_new(NULL);
205     if (!display)
206         g_error("could not create Gst/VA (Wayland) display");
207
208     surface = create_test_surface(display, width, height);
209     if (!surface)
210         g_error("could not create Gst/VA surface");
211
212     g_print("#\n");
213     g_print("# Create window with gst_vaapi_window_wayland_new()\n");
214     g_print("#\n");
215     {
216         window = gst_vaapi_window_wayland_new(display, win_width, win_height);
217         if (!window)
218             g_error("could not create window");
219
220         gst_vaapi_window_show(window);
221
222         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
223             g_error("could not render surface");
224
225         pause();
226         g_object_unref(window);
227     }
228
229     g_object_unref(surface);
230     g_object_unref(display);
231 #endif
232
233     gst_deinit();
234     return 0;
235 }