tests: image: fix string representation for GstVideoFormat.
[platform/upstream/gstreamer-vaapi.git] / tests / test-textures.c
1 /*
2  *  test-textures.c - Test GstVaapiTexture
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/sysdeps.h"
23 #include <gst/vaapi/gstvaapidisplay_glx.h>
24 #include <gst/vaapi/gstvaapiwindow_glx.h>
25 #include <gst/vaapi/gstvaapisurface.h>
26 #include <gst/vaapi/gstvaapiimage.h>
27 #include <gst/vaapi/gstvaapitexture.h>
28 #include "image.h"
29
30 static inline void pause(void)
31 {
32     g_print("Press any key to continue...\n");
33     getchar();
34 }
35
36 static inline guint gl_get_current_texture_2d(void)
37 {
38     GLint texture;
39     glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
40     return (guint)texture;
41 }
42
43 int
44 main(int argc, char *argv[])
45 {
46     GstVaapiDisplay    *display;
47     GstVaapiWindow     *window;
48     GstVaapiWindowGLX  *glx_window;
49     GstVaapiSurface    *surface;
50     GstVaapiImage      *image;
51     GstVaapiTexture    *textures[2];
52     GstVaapiTexture    *texture;
53     GLuint              texture_id;
54     GstVaapiRectangle   src_rect;
55     GstVaapiRectangle   dst_rect;
56     guint               flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
57
58     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
59     static const guint              width       = 320;
60     static const guint              height      = 240;
61     static const guint              win_width   = 640;
62     static const guint              win_height  = 480;
63
64     gst_init(&argc, &argv);
65
66     display = gst_vaapi_display_glx_new(NULL);
67     if (!display)
68         g_error("could not create VA display");
69
70     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
71     if (!surface)
72         g_error("could not create VA surface");
73
74     image = image_generate(display, GST_VIDEO_FORMAT_NV12, width, height);
75     if (!image)
76         g_error("could not create VA image");
77     if (!image_upload(image, surface))
78         g_error("could not upload VA image to surface");
79
80     window = gst_vaapi_window_glx_new(display, win_width, win_height);
81     if (!window)
82         g_error("could not create window");
83     glx_window = GST_VAAPI_WINDOW_GLX(window);
84
85     gst_vaapi_window_show(window);
86
87     if (!gst_vaapi_window_glx_make_current(glx_window))
88         g_error("coult not bind GL context");
89
90     g_print("#\n");
91     g_print("# Create texture with gst_vaapi_texture_new()\n");
92     g_print("#\n");
93     {
94         texture = gst_vaapi_texture_new(
95             display,
96             GL_TEXTURE_2D,
97             GL_RGBA,
98             width,
99             height
100         );
101         if (!texture)
102             g_error("could not create VA texture");
103
104         textures[0] = texture;
105         texture_id  = gst_vaapi_texture_get_id(texture);
106
107         if (!gst_vaapi_texture_put_surface(texture, surface, flags))
108             g_error("could not transfer VA surface to texture");
109
110         if (!gst_vaapi_window_glx_put_texture(glx_window, texture, NULL, NULL))
111             g_error("could not render texture into the window");
112     }
113
114     g_print("#\n");
115     g_print("# Create texture with gst_vaapi_texture_new_with_texture()\n");
116     g_print("#\n");
117     {
118         const GLenum target = GL_TEXTURE_2D;
119         const GLenum format = GL_BGRA;
120
121         glEnable(target);
122         glGenTextures(1, &texture_id);
123         glBindTexture(target, texture_id);
124         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
126         glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
127         glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
128         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
129         glTexImage2D(
130             target,
131             0,
132             GL_RGBA8,
133             width, height,
134             0,
135             format,
136             GL_UNSIGNED_BYTE,
137             NULL
138         );
139         glDisable(target);
140
141         texture = gst_vaapi_texture_new_with_texture(
142             display,
143             texture_id,
144             target,
145             format
146         );
147         if (!texture)
148             g_error("could not create VA texture");
149
150         if (texture_id != gst_vaapi_texture_get_id(texture))
151             g_error("invalid texture id");
152
153         if (gl_get_current_texture_2d() != texture_id)
154             g_error("gst_vaapi_texture_new_with_texture() altered texture bindings");
155
156         textures[1] = texture;
157
158         if (!gst_vaapi_texture_put_surface(texture, surface, flags))
159             g_error("could not transfer VA surface to texture");
160
161         if (gl_get_current_texture_2d() != texture_id)
162             g_error("gst_vaapi_texture_put_surface() altered texture bindings");
163
164         src_rect.x      = 0;
165         src_rect.y      = 0;
166         src_rect.width  = width;
167         src_rect.height = height;
168
169         dst_rect.x      = win_width/2;
170         dst_rect.y      = win_height/2;
171         dst_rect.width  = win_width/2;
172         dst_rect.height = win_height/2;
173
174         if (!gst_vaapi_window_glx_put_texture(glx_window, texture, 
175                                               &src_rect, &dst_rect))
176             g_error("could not render texture into the window");
177
178         if (gl_get_current_texture_2d() != texture_id)
179             g_error("gst_vaapi_window_glx_put_texture() altered texture bindings");
180     }
181
182     gst_vaapi_window_glx_swap_buffers(glx_window);
183     pause();
184
185     gst_vaapi_texture_unref(textures[0]);
186     gst_vaapi_texture_unref(textures[1]);
187     glDeleteTextures(1, &texture_id);
188
189     gst_vaapi_window_unref(window);
190     gst_vaapi_display_unref(display);
191     gst_deinit();
192     return 0;
193 }