Move tests to top-level tests/ directory.
[platform/upstream/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
26 static inline void pause(void)
27 {
28     g_print("Press any key to continue...\n");
29     getchar();
30 }
31
32 typedef void (*DrawRectFunc)(
33     guchar *pixels[3],
34     guint   stride[3],
35     gint    x,
36     gint    y,
37     guint   width,
38     guint   height,
39     guint32 color
40 );
41
42 static void draw_rect_NV12( // Y, UV planes
43     guchar *pixels[3],
44     guint   stride[3],
45     gint    x,
46     gint    y,
47     guint   width,
48     guint   height,
49     guint32 color
50 )
51 {
52     const guchar Y  = color >> 16;
53     const guchar Cb = color >> 8;
54     const guchar Cr = color;
55     guchar *dst;
56     guint i, j;
57
58     dst = pixels[0] + y * stride[0] + x;
59     for (j = 0; j < height; j++, dst += stride[0])
60         for (i = 0; i < width; i++)
61             dst[i] = Y;
62
63     x      /= 2;
64     y      /= 2;
65     width  /= 2;
66     height /= 2;
67
68     dst = pixels[1] + y * stride[1] + x * 2;
69     for (j = 0; j < height; j++, dst += stride[1])
70         for (i = 0; i < width; i++) {
71             dst[2*i + 0] = Cb;
72             dst[2*i + 1] = Cr;
73         }
74 }
75
76 static void draw_rect_YV12( // Y, U, V planes
77     guchar *pixels[3],
78     guint   stride[3],
79     gint    x,
80     gint    y,
81     guint   width,
82     guint   height,
83     guint32 color
84 )
85 {
86     const guchar Y  = color >> 16;
87     const guchar Cb = color >> 8;
88     const guchar Cr = color;
89     guchar *pY, *pU, *pV;
90     guint i, j;
91
92     pY = pixels[0] + y * stride[0] + x;
93     for (j = 0; j < height; j++, pY += stride[0])
94         for (i = 0; i < width; i++)
95             pY[i] = Y;
96
97     x      /= 2;
98     y      /= 2;
99     width  /= 2;
100     height /= 2;
101
102     pU = pixels[1] + y * stride[1] + x;
103     pV = pixels[2] + y * stride[2] + x;
104     for (j = 0; j < height; j++, pU += stride[1], pV += stride[2])
105         for (i = 0; i < width; i++) {
106             pU[i] = Cb;
107             pV[i] = Cr;
108         }
109 }
110
111 static gboolean draw_rgb_rects(GstVaapiImage *image)
112 {
113     GstVaapiImageFormat format = GST_VAAPI_IMAGE_FORMAT(image);
114     guint               w      = GST_VAAPI_IMAGE_WIDTH(image);
115     guint               h      = GST_VAAPI_IMAGE_HEIGHT(image);
116     guchar             *pixels[3];
117     guint               stride[3];
118     guint32             red_color, green_color, blue_color, black_color;
119     DrawRectFunc        draw_rect;
120
121     if (!gst_vaapi_image_map(image))
122         return FALSE;
123
124     switch (format) {
125     case GST_VAAPI_IMAGE_NV12:
126         draw_rect   = draw_rect_NV12;
127         pixels[0]   = gst_vaapi_image_get_plane(image, 0);
128         stride[0]   = gst_vaapi_image_get_pitch(image, 0);
129         pixels[1]   = gst_vaapi_image_get_plane(image, 1);
130         stride[1]   = gst_vaapi_image_get_pitch(image, 1);
131         goto YUV_colors;
132     case GST_VAAPI_IMAGE_YV12:
133         draw_rect   = draw_rect_YV12;
134         pixels[0]   = gst_vaapi_image_get_plane(image, 0);
135         stride[0]   = gst_vaapi_image_get_pitch(image, 0);
136         pixels[1]   = gst_vaapi_image_get_plane(image, 2);
137         stride[1]   = gst_vaapi_image_get_pitch(image, 2);
138         pixels[2]   = gst_vaapi_image_get_plane(image, 1);
139         stride[2]   = gst_vaapi_image_get_pitch(image, 1);
140         goto YUV_colors;
141     case GST_VAAPI_IMAGE_I420:
142         draw_rect   = draw_rect_YV12;
143         pixels[0]   = gst_vaapi_image_get_plane(image, 0);
144         stride[0]   = gst_vaapi_image_get_pitch(image, 0);
145         pixels[1]   = gst_vaapi_image_get_plane(image, 1);
146         stride[1]   = gst_vaapi_image_get_pitch(image, 1);
147         pixels[2]   = gst_vaapi_image_get_plane(image, 2);
148         stride[2]   = gst_vaapi_image_get_pitch(image, 2);
149     YUV_colors:
150         red_color   = 0x515af0;
151         green_color = 0x913622;
152         blue_color  = 0x29f06e;
153         black_color = 0x108080;
154         break;
155     default:
156         gst_vaapi_image_unmap(image);
157         return FALSE;
158     }
159
160     draw_rect(pixels, stride, 0,   0,   w/2, h/2, red_color);
161     draw_rect(pixels, stride, w/2, 0,   w/2, h/2, green_color);
162     draw_rect(pixels, stride, 0,   h/2, w/2, h/2, blue_color);
163     draw_rect(pixels, stride, w/2, h/2, w/2, h/2, black_color);
164
165     if (!gst_vaapi_image_unmap(image))
166         return FALSE;
167
168     return TRUE;
169 }
170
171 int
172 main(int argc, char *argv[])
173 {
174     GstVaapiDisplay *display;
175     GstVaapiWindow  *window;
176     GstVaapiSurface *surface;
177     GstVaapiImage   *image;
178     guint flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
179
180     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
181     static const guint              width       = 320;
182     static const guint              height      = 240;
183     static const guint              win_width   = 640;
184     static const guint              win_height  = 480;
185
186     gst_init(&argc, &argv);
187
188     display = gst_vaapi_display_x11_new(NULL);
189     if (!display)
190         g_error("could not create Gst/VA display");
191
192     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
193     if (!surface)
194         g_error("could not create Gst/VA surface");
195
196     image = gst_vaapi_image_new(display, GST_VAAPI_IMAGE_NV12, width, height);
197     if (!image)
198         g_error("could not create Gst/VA image");
199     if (!draw_rgb_rects(image))
200         g_error("could not draw RGB rectangels");
201
202     if (!gst_vaapi_surface_put_image(surface, image))
203         g_error("could not upload image");
204     if (!gst_vaapi_surface_sync(surface))
205         g_error("could not complete image upload");
206
207     g_print("#\n");
208     g_print("# Create window with gst_vaapi_window_x11_new()\n");
209     g_print("#\n");
210     {
211         window = gst_vaapi_window_x11_new(display, win_width, win_height);
212         if (!window)
213             g_error("could not create window");
214
215         if (!gst_vaapi_window_put_surface(window, surface, flags))
216             g_error("could not render surface");
217
218         pause();
219         g_object_unref(window);
220     }
221
222     g_print("#\n");
223     g_print("# Create window with gst_vaapi_window_x11_new_with_xid()\n");
224     g_print("#\n");
225     {
226         Display * const dpy = GST_VAAPI_DISPLAY_XDISPLAY(display);
227         Window rootwin, win;
228         int screen;
229         unsigned long white_pixel, black_pixel;
230
231         screen      = DefaultScreen(dpy);
232         rootwin     = RootWindow(dpy, screen);
233         white_pixel = WhitePixel(dpy, screen);
234         black_pixel = BlackPixel(dpy, screen);
235
236         win = XCreateSimpleWindow(
237             dpy,
238             rootwin,
239             0, 0, win_width, win_height,
240             0, black_pixel,
241             white_pixel
242         );
243         if (!win)
244             g_error("could not create X window");
245
246         XMapRaised(dpy, win);
247         XSync(dpy, False);
248
249         window = gst_vaapi_window_x11_new_with_xid(display, win);
250         if (!window)
251             g_error("could not create window");
252
253         if (!gst_vaapi_window_put_surface(window, surface, flags))
254             g_error("could not render surface");
255
256         pause();
257         g_object_unref(window);
258         XUnmapWindow(dpy, win);
259         XDestroyWindow(dpy, win);
260     }
261
262     g_object_unref(image);
263     g_object_unref(surface);
264     g_object_unref(display);
265     gst_deinit();
266     return 0;
267 }