Factor out image utilities.
[profile/ivi/gstreamer-vaapi.git] / tests / test-windows.c
1 /*
2  *  test-windows.c - Test GstVaapiWindow
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program 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
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19  */
20
21 #include <gst/vaapi/gstvaapidisplay_x11.h>
22 #include <gst/vaapi/gstvaapiwindow_x11.h>
23 #include <gst/vaapi/gstvaapisurface.h>
24 #include <gst/vaapi/gstvaapiimage.h>
25 #include "image.h"
26
27 static inline void pause(void)
28 {
29     g_print("Press any key to continue...\n");
30     getchar();
31 }
32
33 static gboolean
34 upload_image(GstVaapiSurface *surface, GstVaapiImage *image)
35 {
36     GstVaapiDisplay *display;
37     GstVaapiImageFormat format;
38     GstVaapiSubpicture *subpicture;
39
40     display = gst_vaapi_object_get_display(GST_VAAPI_OBJECT(surface));
41     if (!display)
42         return FALSE;
43
44     format = gst_vaapi_image_get_format(image);
45     if (!format)
46         return FALSE;
47
48     if (gst_vaapi_surface_put_image(surface, image))
49         return TRUE;
50
51     g_print("could not upload %" GST_FOURCC_FORMAT" image to surface\n",
52             GST_FOURCC_ARGS(format));
53
54     if (!gst_vaapi_display_has_subpicture_format(display, format))
55         return FALSE;
56
57     g_print("trying as a subpicture\n");
58
59     subpicture = gst_vaapi_subpicture_new(image);
60     if (!subpicture)
61         g_error("could not create Gst/VA subpicture");
62
63     if (!gst_vaapi_surface_associate_subpicture(surface, subpicture,
64                                                 NULL, NULL))
65         g_error("could not associate subpicture to surface");
66
67     /* The surface holds a reference to the subpicture. This is safe */
68     g_object_unref(subpicture);
69     return TRUE;
70 }
71
72 int
73 main(int argc, char *argv[])
74 {
75     GstVaapiDisplay    *display;
76     GstVaapiWindow     *window;
77     GstVaapiSurface    *surface;
78     GstVaapiImage      *image   = NULL;
79     guint flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
80     guint i;
81
82     static const GstVaapiImageFormat image_formats[] = {
83         GST_VAAPI_IMAGE_NV12,
84         GST_VAAPI_IMAGE_YV12,
85         GST_VAAPI_IMAGE_I420,
86         GST_VAAPI_IMAGE_AYUV,
87         GST_VAAPI_IMAGE_ARGB,
88         GST_VAAPI_IMAGE_BGRA,
89         GST_VAAPI_IMAGE_RGBA,
90         GST_VAAPI_IMAGE_ABGR,
91         0
92     };
93
94     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
95     static const guint              width       = 320;
96     static const guint              height      = 240;
97     static const guint              win_width   = 640;
98     static const guint              win_height  = 480;
99
100     gst_init(&argc, &argv);
101
102     display = gst_vaapi_display_x11_new(NULL);
103     if (!display)
104         g_error("could not create Gst/VA display");
105
106     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
107     if (!surface)
108         g_error("could not create Gst/VA surface");
109
110     for (i = 0; image_formats[i]; i++) {
111         const GstVaapiImageFormat format = image_formats[i];
112
113         image = image_generate(display, format, width, height);
114         if (image) {
115             if (upload_image(surface, image))
116                 break;
117             g_object_unref(image);
118         }
119     }
120     if (!image)
121         g_error("could not create Gst/VA image");
122
123     if (!gst_vaapi_surface_sync(surface))
124         g_error("could not complete image upload");
125
126     g_print("#\n");
127     g_print("# Create window with gst_vaapi_window_x11_new()\n");
128     g_print("#\n");
129     {
130         window = gst_vaapi_window_x11_new(display, win_width, win_height);
131         if (!window)
132             g_error("could not create window");
133
134         gst_vaapi_window_show(window);
135
136         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
137             g_error("could not render surface");
138
139         pause();
140         g_object_unref(window);
141     }
142
143     g_print("#\n");
144     g_print("# Create window with gst_vaapi_window_x11_new_with_xid()\n");
145     g_print("#\n");
146     {
147         Display * const dpy = GST_VAAPI_DISPLAY_XDISPLAY(display);
148         Window rootwin, win;
149         int screen;
150         unsigned long white_pixel, black_pixel;
151
152         screen      = DefaultScreen(dpy);
153         rootwin     = RootWindow(dpy, screen);
154         white_pixel = WhitePixel(dpy, screen);
155         black_pixel = BlackPixel(dpy, screen);
156
157         win = XCreateSimpleWindow(
158             dpy,
159             rootwin,
160             0, 0, win_width, win_height,
161             0, black_pixel,
162             white_pixel
163         );
164         if (!win)
165             g_error("could not create X window");
166
167         window = gst_vaapi_window_x11_new_with_xid(display, win);
168         if (!window)
169             g_error("could not create window");
170
171         gst_vaapi_window_show(window);
172
173         if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL, flags))
174             g_error("could not render surface");
175
176         pause();
177         g_object_unref(window);
178         XUnmapWindow(dpy, win);
179         XDestroyWindow(dpy, win);
180     }
181
182     g_object_unref(image);
183     g_object_unref(surface);
184     g_object_unref(display);
185     gst_deinit();
186     return 0;
187 }