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