packaging: add packaging folder and update submodule pkgs
[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  *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22
23 #include "gst/vaapi/sysdeps.h"
24 #include <gst/vaapi/gstvaapidisplay_glx.h>
25 #include <gst/vaapi/gstvaapiwindow_glx.h>
26 #include <gst/vaapi/gstvaapisurface.h>
27 #include <gst/vaapi/gstvaapiimage.h>
28 #include <gst/vaapi/gstvaapitexture.h>
29 #include "image.h"
30
31 static inline void pause(void)
32 {
33     g_print("Press any key to continue...\n");
34     getchar();
35 }
36
37 static inline guint gl_get_current_texture_2d(void)
38 {
39     GLint texture;
40     glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
41     return (guint)texture;
42 }
43
44 int
45 main(int argc, char *argv[])
46 {
47     GstVaapiDisplay    *display;
48     GstVaapiWindow     *window;
49     GstVaapiWindowGLX  *glx_window;
50     GstVaapiSurface    *surface;
51     GstVaapiImage      *image;
52     GstVaapiTexture    *textures[2];
53     GstVaapiTexture    *texture;
54     GLuint              texture_id;
55     GstVaapiRectangle   src_rect;
56     GstVaapiRectangle   dst_rect;
57     guint               flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
58
59     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
60     static const guint              width       = 320;
61     static const guint              height      = 240;
62     static const guint              win_width   = 640;
63     static const guint              win_height  = 480;
64
65     gst_init(&argc, &argv);
66
67     display = gst_vaapi_display_glx_new(NULL);
68     if (!display)
69         g_error("could not create VA display");
70
71     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
72     if (!surface)
73         g_error("could not create VA surface");
74
75     image = image_generate(display, GST_VIDEO_FORMAT_NV12, width, height);
76     if (!image)
77         g_error("could not create VA image");
78     if (!image_upload(image, surface))
79         g_error("could not upload VA image to surface");
80
81     window = gst_vaapi_window_glx_new(display, win_width, win_height);
82     if (!window)
83         g_error("could not create window");
84     glx_window = GST_VAAPI_WINDOW_GLX(window);
85
86     gst_vaapi_window_show(window);
87
88     if (!gst_vaapi_window_glx_make_current(glx_window))
89         g_error("coult not bind GL context");
90
91     g_print("#\n");
92     g_print("# Create texture with gst_vaapi_texture_new()\n");
93     g_print("#\n");
94     {
95         texture = gst_vaapi_texture_new(
96             display,
97             GL_TEXTURE_2D,
98             GL_RGBA,
99             width,
100             height
101         );
102         if (!texture)
103             g_error("could not create VA texture");
104
105         textures[0] = texture;
106         texture_id  = gst_vaapi_texture_get_id(texture);
107
108         if (!gst_vaapi_texture_put_surface(texture, surface, flags))
109             g_error("could not transfer VA surface to texture");
110
111         if (!gst_vaapi_window_glx_put_texture(glx_window, texture, NULL, NULL))
112             g_error("could not render texture into the window");
113     }
114
115     g_print("#\n");
116     g_print("# Create texture with gst_vaapi_texture_new_with_texture()\n");
117     g_print("#\n");
118     {
119         const GLenum target = GL_TEXTURE_2D;
120         const GLenum format = GL_BGRA;
121
122         glEnable(target);
123         glGenTextures(1, &texture_id);
124         glBindTexture(target, texture_id);
125         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127         glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
128         glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
129         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
130         glTexImage2D(
131             target,
132             0,
133             GL_RGBA8,
134             width, height,
135             0,
136             format,
137             GL_UNSIGNED_BYTE,
138             NULL
139         );
140         glDisable(target);
141
142         texture = gst_vaapi_texture_new_with_texture(
143             display,
144             texture_id,
145             target,
146             format
147         );
148         if (!texture)
149             g_error("could not create VA texture");
150
151         if (texture_id != gst_vaapi_texture_get_id(texture))
152             g_error("invalid texture id");
153
154         if (gl_get_current_texture_2d() != texture_id)
155             g_error("gst_vaapi_texture_new_with_texture() altered texture bindings");
156
157         textures[1] = texture;
158
159         if (!gst_vaapi_texture_put_surface(texture, surface, flags))
160             g_error("could not transfer VA surface to texture");
161
162         if (gl_get_current_texture_2d() != texture_id)
163             g_error("gst_vaapi_texture_put_surface() altered texture bindings");
164
165         src_rect.x      = 0;
166         src_rect.y      = 0;
167         src_rect.width  = width;
168         src_rect.height = height;
169
170         dst_rect.x      = win_width/2;
171         dst_rect.y      = win_height/2;
172         dst_rect.width  = win_width/2;
173         dst_rect.height = win_height/2;
174
175         if (!gst_vaapi_window_glx_put_texture(glx_window, texture, 
176                                               &src_rect, &dst_rect))
177             g_error("could not render texture into the window");
178
179         if (gl_get_current_texture_2d() != texture_id)
180             g_error("gst_vaapi_window_glx_put_texture() altered texture bindings");
181     }
182
183     gst_vaapi_window_glx_swap_buffers(glx_window);
184     pause();
185
186     gst_vaapi_texture_unref(textures[0]);
187     gst_vaapi_texture_unref(textures[1]);
188     glDeleteTextures(1, &texture_id);
189
190     gst_vaapi_window_unref(window);
191     gst_vaapi_display_unref(display);
192     gst_deinit();
193     return 0;
194 }