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