switch tests licence to LGPL v2.1+
[profile/ivi/gstreamer-vaapi.git] / tests / test-surfaces.c
1 /*
2  *  test-surfaces.c - Test GstVaapiSurface and GstVaapiSurfacePool
3  *
4  *  gstreamer-vaapi (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 <gst/vaapi/gstvaapisurface.h>
23 #include <gst/vaapi/gstvaapisurfacepool.h>
24 #include <gst/vaapi/gstvaapidisplay_x11.h>
25
26 #define MAX_SURFACES 4
27
28 static void
29 gst_vaapi_object_destroy_cb(gpointer object, gpointer user_data)
30 {
31     g_print("destroying GstVaapiObject %p\n", object);
32 }
33
34 int
35 main(int argc, char *argv[])
36 {
37     GstVaapiDisplay    *display;
38     GstVaapiSurface    *surface;
39     GstVaapiID          surface_id;
40     GstVaapiSurface    *surfaces[MAX_SURFACES];
41     GstVaapiVideoPool  *pool;
42     GstCaps            *caps;
43     gint                i;
44
45     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
46     static const guint              width       = 320;
47     static const guint              height      = 240;
48
49     gst_init(&argc, &argv);
50
51     display = gst_vaapi_display_x11_new(NULL);
52     if (!display)
53         g_error("could not create Gst/VA display");
54
55     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
56     if (!surface)
57         g_error("could not create Gst/VA surface");
58
59     /* This also tests for the GstVaapiParamSpecID */
60     g_object_get(G_OBJECT(surface), "id", &surface_id, NULL);
61     if (surface_id != gst_vaapi_surface_get_id(surface))
62         g_error("could not retrieve the native surface ID");
63     g_print("created surface %" GST_VAAPI_ID_FORMAT "\n",
64             GST_VAAPI_ID_ARGS(surface_id));
65
66     g_object_unref(surface);
67
68     caps = gst_caps_new_simple(
69         "video/x-vaapi-surface",
70         "width", G_TYPE_INT, width,
71         "height", G_TYPE_INT, height,
72         NULL
73     );
74     if (!caps)
75         g_error("cound not create Gst/VA surface caps");
76
77     pool = gst_vaapi_surface_pool_new(display, caps);
78     if (!pool)
79         g_error("could not create Gst/VA surface pool");
80
81     for (i = 0; i < MAX_SURFACES; i++) {
82         surface = gst_vaapi_video_pool_get_object(pool);
83         if (!surface)
84             g_error("could not allocate Gst/VA surface from pool");
85         g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool\n",
86                 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
87         surfaces[i] = surface;
88     }
89
90     /* Check the pool doesn't return the last free'd surface */
91     surface = g_object_ref(surfaces[1]);
92
93     for (i = 0; i < 2; i++)
94         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
95
96     for (i = 0; i < 2; i++) {
97         surfaces[i] = gst_vaapi_video_pool_get_object(pool);
98         if (!surfaces[i])
99             g_error("could not re-allocate Gst/VA surface%d from pool", i);
100         g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool (realloc)\n",
101                 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surfaces[i])));
102     }
103
104     if (surface == surfaces[0])
105         g_error("Gst/VA pool doesn't queue free surfaces");
106
107     for (i = MAX_SURFACES - 1; i >= 0; i--) {
108         if (!surfaces[i])
109             continue;
110         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
111         surfaces[i] = NULL;
112     }
113
114     g_signal_connect(
115         G_OBJECT(surface),
116         "destroy",
117         G_CALLBACK(gst_vaapi_object_destroy_cb), NULL
118     );
119
120     /* Unref in random order to check objects are correctly refcounted */
121     g_print("unref display\n");
122     g_object_unref(display);
123     gst_caps_unref(caps);
124     g_print("unref pool\n");
125     g_object_unref(pool);
126     g_print("unref surface\n");
127     g_object_unref(surface);
128     gst_deinit();
129     return 0;
130 }