test-surfaces.c 4.35 KB
Newer Older
gb's avatar
gb committed
1 2 3
/*
 *  test-surfaces.c - Test GstVaapiSurface and GstVaapiSurfacePool
 *
4
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5
 *  Copyright (C) 2012 Intel Corporation
gb's avatar
gb committed
6
 *
warly's avatar
warly committed
7 8 9 10
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
gb's avatar
gb committed
11
 *
warly's avatar
warly committed
12
 *  This library is distributed in the hope that it will be useful,
gb's avatar
gb committed
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
warly's avatar
warly committed
14 15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
gb's avatar
gb committed
16
 *
warly's avatar
warly committed
17 18 19 20
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
gb's avatar
gb committed
21 22 23 24
 */

#include <gst/vaapi/gstvaapisurface.h>
#include <gst/vaapi/gstvaapisurfacepool.h>
25
#include "output.h"
gb's avatar
gb committed
26 27 28

#define MAX_SURFACES 4

gb's avatar
gb committed
29 30 31
static void
gst_vaapi_object_destroy_cb(gpointer object, gpointer user_data)
{
gb's avatar
gb committed
32
    g_print("destroying GstVaapiObject %p\n", object);
gb's avatar
gb committed
33 34
}

gb's avatar
gb committed
35 36 37
int
main(int argc, char *argv[])
{
38 39 40 41 42 43 44
    GstVaapiDisplay    *display;
    GstVaapiSurface    *surface;
    GstVaapiID          surface_id;
    GstVaapiSurface    *surfaces[MAX_SURFACES];
    GstVaapiVideoPool  *pool;
    GstCaps            *caps;
    gint                i;
gb's avatar
gb committed
45 46 47 48 49

    static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
    static const guint              width       = 320;
    static const guint              height      = 240;

50 51
    if (!video_output_init(&argc, argv, NULL))
        g_error("failed to initialize video output subsystem");
gb's avatar
gb committed
52

53
    display = video_output_create_display(NULL);
gb's avatar
gb committed
54 55 56 57 58 59
    if (!display)
        g_error("could not create Gst/VA display");

    surface = gst_vaapi_surface_new(display, chroma_type, width, height);
    if (!surface)
        g_error("could not create Gst/VA surface");
60 61 62

    /* This also tests for the GstVaapiParamSpecID */
    g_object_get(G_OBJECT(surface), "id", &surface_id, NULL);
63
    if (surface_id != gst_vaapi_surface_get_id(surface))
64 65 66 67
        g_error("could not retrieve the native surface ID");
    g_print("created surface %" GST_VAAPI_ID_FORMAT "\n",
            GST_VAAPI_ID_ARGS(surface_id));

gb's avatar
gb committed
68 69 70
    g_object_unref(surface);

    caps = gst_caps_new_simple(
71 72
        GST_VAAPI_SURFACE_CAPS_NAME,
        "type", G_TYPE_STRING, "vaapi",
gb's avatar
gb committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        "width", G_TYPE_INT, width,
        "height", G_TYPE_INT, height,
        NULL
    );
    if (!caps)
        g_error("cound not create Gst/VA surface caps");

    pool = gst_vaapi_surface_pool_new(display, caps);
    if (!pool)
        g_error("could not create Gst/VA surface pool");

    for (i = 0; i < MAX_SURFACES; i++) {
        surface = gst_vaapi_video_pool_get_object(pool);
        if (!surface)
            g_error("could not allocate Gst/VA surface from pool");
88
        g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool\n",
89
                GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
gb's avatar
gb committed
90 91 92 93 94 95 96 97 98 99 100 101 102
        surfaces[i] = surface;
    }

    /* Check the pool doesn't return the last free'd surface */
    surface = g_object_ref(surfaces[1]);

    for (i = 0; i < 2; i++)
        gst_vaapi_video_pool_put_object(pool, surfaces[i]);

    for (i = 0; i < 2; i++) {
        surfaces[i] = gst_vaapi_video_pool_get_object(pool);
        if (!surfaces[i])
            g_error("could not re-allocate Gst/VA surface%d from pool", i);
103
        g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool (realloc)\n",
104
                GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surfaces[i])));
gb's avatar
gb committed
105 106 107 108 109 110 111 112 113 114 115 116
    }

    if (surface == surfaces[0])
        g_error("Gst/VA pool doesn't queue free surfaces");

    for (i = MAX_SURFACES - 1; i >= 0; i--) {
        if (!surfaces[i])
            continue;
        gst_vaapi_video_pool_put_object(pool, surfaces[i]);
        surfaces[i] = NULL;
    }

gb's avatar
gb committed
117 118 119 120 121 122
    g_signal_connect(
        G_OBJECT(surface),
        "destroy",
        G_CALLBACK(gst_vaapi_object_destroy_cb), NULL
    );

gb's avatar
gb committed
123 124 125 126 127 128 129 130
    /* Unref in random order to check objects are correctly refcounted */
    g_print("unref display\n");
    g_object_unref(display);
    gst_caps_unref(caps);
    g_print("unref pool\n");
    g_object_unref(pool);
    g_print("unref surface\n");
    g_object_unref(surface);
131
    video_output_exit();
gb's avatar
gb committed
132 133
    return 0;
}