Move tests to top-level tests/ directory.
[profile/ivi/gstreamer-vaapi.git] / tests / test-surfaces.c
1 /*
2  *  test-surfaces.c - Test GstVaapiSurface and GstVaapiSurfacePool
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/gstvaapisurface.h>
22 #include <gst/vaapi/gstvaapisurfacepool.h>
23 #include <gst/vaapi/gstvaapidisplay_x11.h>
24
25 #define MAX_SURFACES 4
26
27 int
28 main(int argc, char *argv[])
29 {
30     GstVaapiDisplay *display;
31     GstVaapiSurface *surface;
32     GstVaapiSurface *surfaces[MAX_SURFACES];
33     GstVaapiVideoPool *pool;
34     GstCaps *caps;
35     gint i;
36
37     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
38     static const guint              width       = 320;
39     static const guint              height      = 240;
40
41     gst_init(&argc, &argv);
42
43     display = gst_vaapi_display_x11_new(NULL);
44     if (!display)
45         g_error("could not create Gst/VA display");
46
47     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
48     if (!surface)
49         g_error("could not create Gst/VA surface");
50     g_object_unref(surface);
51
52     caps = gst_caps_new_simple(
53         "video/x-vaapi-surface",
54         "width", G_TYPE_INT, width,
55         "height", G_TYPE_INT, height,
56         NULL
57     );
58     if (!caps)
59         g_error("cound not create Gst/VA surface caps");
60
61     pool = gst_vaapi_surface_pool_new(display, caps);
62     if (!pool)
63         g_error("could not create Gst/VA surface pool");
64
65     for (i = 0; i < MAX_SURFACES; i++) {
66         surface = gst_vaapi_video_pool_get_object(pool);
67         if (!surface)
68             g_error("could not allocate Gst/VA surface from pool");
69         g_print("created surface 0x%08x from pool\n",
70                 gst_vaapi_surface_get_id(surface));
71         surfaces[i] = surface;
72     }
73
74     /* Check the pool doesn't return the last free'd surface */
75     surface = g_object_ref(surfaces[1]);
76
77     for (i = 0; i < 2; i++)
78         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
79
80     for (i = 0; i < 2; i++) {
81         surfaces[i] = gst_vaapi_video_pool_get_object(pool);
82         if (!surfaces[i])
83             g_error("could not re-allocate Gst/VA surface%d from pool", i);
84         g_print("created surface 0x%08x from pool (realloc)\n",
85                 gst_vaapi_surface_get_id(surfaces[i]));
86     }
87
88     if (surface == surfaces[0])
89         g_error("Gst/VA pool doesn't queue free surfaces");
90
91     for (i = MAX_SURFACES - 1; i >= 0; i--) {
92         if (!surfaces[i])
93             continue;
94         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
95         surfaces[i] = NULL;
96     }
97
98     /* Unref in random order to check objects are correctly refcounted */
99     g_print("unref display\n");
100     g_object_unref(display);
101     gst_caps_unref(caps);
102     g_print("unref pool\n");
103     g_object_unref(pool);
104     g_print("unref surface\n");
105     g_object_unref(surface);
106     gst_deinit();
107     return 0;
108 }