int
main(int argc, char *argv[])
{
    GstVaapiDisplay *display, *display2;
    guint width, height, par_n, par_d;

    gst_init(&argc, &argv);

#if USE_DRM
    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_drm_new()\n");
    g_print("#\n");
    {
        display = gst_vaapi_display_drm_new(NULL);
        if (!display)
            g_error("could not create Gst/VA display");

        dump_info(display);
        gst_vaapi_display_unref(display);
    }
    g_print("\n");

    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_drm_new_with_device()\n");
    g_print("#\n");
    {
        int drm_device;

        drm_device = open(DRM_DEVICE_PATH, O_RDWR|O_CLOEXEC);
        if (drm_device < 0)
            g_error("could not open DRM device");

        display = gst_vaapi_display_drm_new_with_device(drm_device);
        if (!display)
            g_error("could not create Gst/VA display");

        dump_info(display);
        gst_vaapi_display_unref(display);
        close(drm_device);
    }
    g_print("\n");

    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayDRM()]\n");
    g_print("#\n");
    {
        int drm_device;
        VADisplay va_display;

        drm_device = open(DRM_DEVICE_PATH, O_RDWR|O_CLOEXEC);
        if (drm_device < 0)
            g_error("could not open DRM device");

        va_display = vaGetDisplayDRM(drm_device);
        if (!va_display)
            g_error("could not create VA display");

        display = gst_vaapi_display_new_with_display(va_display);
        if (!display)
            g_error("could not create Gst/VA display");

        dump_info(display);
        gst_vaapi_display_unref(display);
        close(drm_device);
    }
    g_print("\n");
#endif

#if USE_X11
    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_x11_new()\n");
    g_print("#\n");
    {
        display = gst_vaapi_display_x11_new(NULL);
        if (!display)
            g_error("could not create Gst/VA display");

        if (CHECK_DISPLAY_CACHE) {
            display2 = gst_vaapi_display_x11_new(NULL);

            /* Check for the same X11 display */
            g_assert(gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display)) ==
                     gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display2)));

            /* Check for the same VA display */
            g_assert(gst_vaapi_display_get_display(display) ==
                     gst_vaapi_display_get_display(display2));

            gst_vaapi_display_unref(display2);

#if USE_GLX
            display2 = gst_vaapi_display_glx_new(NULL);

            /* Check for the different X11 display */
            /* XXX: it is also desired to cache underlying X11 displays */
            g_assert(gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display)) !=
                     gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display2)));

            /* Check for different VA display */
            g_assert(gst_vaapi_display_get_display(display) !=
                     gst_vaapi_display_get_display(display2));

            gst_vaapi_display_unref(display2);
#endif
        }

        gst_vaapi_display_get_size(display, &width, &height);
        g_print("Display size: %ux%u\n", width, height);

        gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
        g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);

        dump_info(display);
        gst_vaapi_display_unref(display);
    }
    g_print("\n");

    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_x11_new_with_display()\n");
    g_print("#\n");
    {
        Display *x11_display;

        x11_display = XOpenDisplay(NULL);
        if (!x11_display)
            g_error("could not create X11 display");

        display = gst_vaapi_display_x11_new_with_display(x11_display);
        if (!display)
            g_error("could not create Gst/VA display");

        if (CHECK_DISPLAY_CACHE) {
            display2 = gst_vaapi_display_x11_new_with_display(x11_display);

            /* Check for the same VA display */
            g_assert(gst_vaapi_display_get_display(display) ==
                     gst_vaapi_display_get_display(display2));

            gst_vaapi_display_unref(display2);
        }

        dump_info(display);
        gst_vaapi_display_unref(display);
        XCloseDisplay(x11_display);
    }
    g_print("\n");

    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplay()]\n");
    g_print("#\n");
    {
        Display *x11_display;
        VADisplay va_display;

        x11_display = XOpenDisplay(NULL);
        if (!x11_display)
            g_error("could not create X11 display");

        va_display = vaGetDisplay(x11_display);
        if (!va_display)
            g_error("could not create VA display");

        display = gst_vaapi_display_new_with_display(va_display);
        if (!display)
            g_error("could not create Gst/VA display");

        dump_info(display);
        gst_vaapi_display_unref(display);
        XCloseDisplay(x11_display);
    }
    g_print("\n");
#endif

#if USE_GLX
    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_glx_new()\n");
    g_print("#\n");
    {
        display = gst_vaapi_display_glx_new(NULL);
        if (!display)
            g_error("could not create Gst/VA display");

        if (CHECK_DISPLAY_CACHE) {
            display2 = gst_vaapi_display_glx_new(NULL);

            /* Check for the same X11 display */
            g_assert(gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display)) ==
                     gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display2)));

            /* Check for the same VA display */
            g_assert(gst_vaapi_display_get_display(display) ==
                     gst_vaapi_display_get_display(display2));

            gst_vaapi_display_unref(display2);

            display2 = gst_vaapi_display_x11_new(NULL);

            /* Check for the same X11 display */
            g_assert(gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display)) ==
                     gst_vaapi_display_x11_get_display(
                         GST_VAAPI_DISPLAY_X11(display2)));

            /* Check for the same VA display */
            g_assert(gst_vaapi_display_get_display(display) ==
                     gst_vaapi_display_get_display(display2));

            gst_vaapi_display_unref(display2);
        }

        gst_vaapi_display_get_size(display, &width, &height);
        g_print("Display size: %ux%u\n", width, height);

        gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
        g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);

        dump_info(display);
        gst_vaapi_display_unref(display);
    }
    g_print("\n");

    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_glx_new_with_display()\n");
    g_print("#\n");
    {
        Display *x11_display;

        x11_display = XOpenDisplay(NULL);
        if (!x11_display)
            g_error("could not create X11 display");

        display = gst_vaapi_display_glx_new_with_display(x11_display);
        if (!display)
            g_error("could not create Gst/VA display");

        dump_info(display);
        gst_vaapi_display_unref(display);
        XCloseDisplay(x11_display);
    }
    g_print("\n");

#ifdef HAVE_VA_VA_GLX_H
    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayGLX()]\n");
    g_print("#\n");
    {
        Display *x11_display;
        VADisplay va_display;

        x11_display = XOpenDisplay(NULL);
        if (!x11_display)
            g_error("could not create X11 display");

        va_display = vaGetDisplayGLX(x11_display);
        if (!va_display)
            g_error("could not create VA display");

        display = gst_vaapi_display_new_with_display(va_display);
        if (!display)
            g_error("could not create Gst/VA display");

        dump_info(display);
        gst_vaapi_display_unref(display);
        XCloseDisplay(x11_display);
    }
    g_print("\n");
#endif
#endif

#if USE_WAYLAND
    g_print("#\n");
    g_print("# Create display with gst_vaapi_display_wayland_new()\n");
    g_print("#\n");
    {
        display = gst_vaapi_display_wayland_new(NULL);
        if (!display)
            g_error("could not create Gst/VA display");

        gst_vaapi_display_get_size(display, &width, &height);
        g_print("Display size: %ux%u\n", width, height);

        gst_vaapi_display_get_pixel_aspect_ratio(display, &par_n, &par_d);
        g_print("Pixel aspect ratio: %u/%u\n", par_n, par_d);

        dump_info(display);
        gst_vaapi_display_unref(display);
    }
    g_print("\n");
#endif

    gst_deinit();
    return 0;
}
int
main(int argc, char *argv[])
{
    GstVaapiDisplay    *display;
    GstVaapiWindow     *window;
    GstVaapiWindowGLX  *glx_window;
    GstVaapiSurface    *surface;
    GstVaapiImage      *image;
    GstVaapiTexture    *textures[2];
    GstVaapiTexture    *texture;
    GLuint              texture_id;
    GstVaapiRectangle   src_rect;
    GstVaapiRectangle   dst_rect;
    guint               flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;

    static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
    static const guint              width       = 320;
    static const guint              height      = 240;
    static const guint              win_width   = 640;
    static const guint              win_height  = 480;

    gst_init(&argc, &argv);

    display = gst_vaapi_display_glx_new(NULL);
    if (!display)
        g_error("could not create VA display");

    surface = gst_vaapi_surface_new(display, chroma_type, width, height);
    if (!surface)
        g_error("could not create VA surface");

    image = image_generate(display, GST_VAAPI_IMAGE_NV12, width, height);
    if (!image)
        g_error("could not create VA image");
    if (!image_upload(image, surface))
        g_error("could not upload VA image to surface");

    window = gst_vaapi_window_glx_new(display, win_width, win_height);
    if (!window)
        g_error("could not create window");
    glx_window = GST_VAAPI_WINDOW_GLX(window);

    gst_vaapi_window_show(window);

    if (!gst_vaapi_window_glx_make_current(glx_window))
        g_error("coult not bind GL context");

    g_print("#\n");
    g_print("# Create texture with gst_vaapi_texture_new()\n");
    g_print("#\n");
    {
        texture = gst_vaapi_texture_new(
            display,
            GL_TEXTURE_2D,
            GL_RGBA,
            width,
            height
        );
        if (!texture)
            g_error("could not create VA texture");

        textures[0] = texture;
        texture_id  = gst_vaapi_texture_get_id(texture);

        if (!gst_vaapi_texture_put_surface(texture, surface, flags))
            g_error("could not transfer VA surface to texture");

        if (!gst_vaapi_window_glx_put_texture(glx_window, texture, NULL, NULL))
            g_error("could not render texture into the window");
    }

    g_print("#\n");
    g_print("# Create texture with gst_vaapi_texture_new_with_texture()\n");
    g_print("#\n");
    {
        const GLenum target = GL_TEXTURE_2D;
        const GLenum format = GL_BGRA;

        glEnable(target);
        glGenTextures(1, &texture_id);
        glBindTexture(target, texture_id);
        glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
        glTexImage2D(
            target,
            0,
            GL_RGBA8,
            width, height,
            0,
            format,
            GL_UNSIGNED_BYTE,
            NULL
        );
        glDisable(target);

        texture = gst_vaapi_texture_new_with_texture(
            display,
            texture_id,
            target,
            format
        );
        if (!texture)
            g_error("could not create VA texture");

        if (texture_id != gst_vaapi_texture_get_id(texture))
            g_error("invalid texture id");

        if (gl_get_current_texture_2d() != texture_id)
            g_error("gst_vaapi_texture_new_with_texture() altered texture bindings");

        textures[1] = texture;

        if (!gst_vaapi_texture_put_surface(texture, surface, flags))
            g_error("could not transfer VA surface to texture");

        if (gl_get_current_texture_2d() != texture_id)
            g_error("gst_vaapi_texture_put_surface() altered texture bindings");

        src_rect.x      = 0;
        src_rect.y      = 0;
        src_rect.width  = width;
        src_rect.height = height;

        dst_rect.x      = win_width/2;
        dst_rect.y      = win_height/2;
        dst_rect.width  = win_width/2;
        dst_rect.height = win_height/2;

        if (!gst_vaapi_window_glx_put_texture(glx_window, texture, 
                                              &src_rect, &dst_rect))
            g_error("could not render texture into the window");

        if (gl_get_current_texture_2d() != texture_id)
            g_error("gst_vaapi_window_glx_put_texture() altered texture bindings");
    }

    gst_vaapi_window_glx_swap_buffers(glx_window);
    pause();

    g_object_unref(textures[0]);
    g_object_unref(textures[1]);
    glDeleteTextures(1, &texture_id);

    g_object_unref(window);
    g_object_unref(display);
    gst_deinit();
    return 0;
}