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