Tizen 2.0 Release
[framework/multimedia/gstreamer-vaapi.git] / tests / test-surfaces.c
1 /*
2  *  test-surfaces.c - Test GstVaapiSurface and GstVaapiSurfacePool
3  *
4  *  Copyright (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 "output.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     if (!video_output_init(&argc, argv, NULL))
50         g_error("failed to initialize video output subsystem");
51
52     display = video_output_create_display(NULL);
53     if (!display)
54         g_error("could not create Gst/VA display");
55
56     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
57     if (!surface)
58         g_error("could not create Gst/VA surface");
59
60     /* This also tests for the GstVaapiParamSpecID */
61     g_object_get(G_OBJECT(surface), "id", &surface_id, NULL);
62     if (surface_id != gst_vaapi_surface_get_id(surface))
63         g_error("could not retrieve the native surface ID");
64     g_print("created surface %" GST_VAAPI_ID_FORMAT "\n",
65             GST_VAAPI_ID_ARGS(surface_id));
66
67     g_object_unref(surface);
68
69     caps = gst_caps_new_simple(
70         GST_VAAPI_SURFACE_CAPS_NAME,
71         "type", G_TYPE_STRING, "vaapi",
72         "width", G_TYPE_INT, width,
73         "height", G_TYPE_INT, height,
74         NULL
75     );
76     if (!caps)
77         g_error("cound not create Gst/VA surface caps");
78
79     pool = gst_vaapi_surface_pool_new(display, caps);
80     if (!pool)
81         g_error("could not create Gst/VA surface pool");
82
83     for (i = 0; i < MAX_SURFACES; i++) {
84         surface = gst_vaapi_video_pool_get_object(pool);
85         if (!surface)
86             g_error("could not allocate Gst/VA surface from pool");
87         g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool\n",
88                 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
89         surfaces[i] = surface;
90     }
91
92     /* Check the pool doesn't return the last free'd surface */
93     surface = g_object_ref(surfaces[1]);
94
95     for (i = 0; i < 2; i++)
96         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
97
98     for (i = 0; i < 2; i++) {
99         surfaces[i] = gst_vaapi_video_pool_get_object(pool);
100         if (!surfaces[i])
101             g_error("could not re-allocate Gst/VA surface%d from pool", i);
102         g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool (realloc)\n",
103                 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surfaces[i])));
104     }
105
106     if (surface == surfaces[0])
107         g_error("Gst/VA pool doesn't queue free surfaces");
108
109     for (i = MAX_SURFACES - 1; i >= 0; i--) {
110         if (!surfaces[i])
111             continue;
112         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
113         surfaces[i] = NULL;
114     }
115
116     g_signal_connect(
117         G_OBJECT(surface),
118         "destroy",
119         G_CALLBACK(gst_vaapi_object_destroy_cb), NULL
120     );
121
122     /* Unref in random order to check objects are correctly refcounted */
123     g_print("unref display\n");
124     g_object_unref(display);
125     gst_caps_unref(caps);
126     g_print("unref pool\n");
127     g_object_unref(pool);
128     g_print("unref surface\n");
129     g_object_unref(surface);
130     video_output_exit();
131     return 0;
132 }