2 * gstvaapiutils_glx.c - GLX utilties
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011 Intel Corporation
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.
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.
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
23 #define _GNU_SOURCE 1 /* RTLD_DEFAULT */
28 #include "gstvaapiutils_glx.h"
29 #include "gstvaapiutils_x11.h"
32 #include "gstvaapidebug.h"
34 /** Lookup for substring NAME in string EXT using SEP as separators */
36 find_string(const char *name, const char *ext, const char *sep)
44 end = ext + strlen(ext);
45 name_len = strlen(name);
47 n = strcspn(ext, sep);
48 if (n == name_len && strncmp(name, ext, n) == 0)
56 * gl_get_error_string:
57 * @error: an OpenGL error enumeration
59 * Retrieves the string representation the OpenGL @error.
61 * Return error: the static string representing the OpenGL @error
64 gl_get_error_string(GLenum error)
67 #define MAP(id, str) \
68 case id: return str " (" #id ")"
69 MAP(GL_NO_ERROR, "no error");
70 MAP(GL_INVALID_ENUM, "invalid enumerant");
71 MAP(GL_INVALID_VALUE, "invalid value");
72 MAP(GL_INVALID_OPERATION, "invalid operation");
73 MAP(GL_STACK_OVERFLOW, "stack overflow");
74 MAP(GL_STACK_UNDERFLOW, "stack underflow");
75 MAP(GL_OUT_OF_MEMORY, "out of memory");
76 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
77 MAP(GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
78 "invalid framebuffer operation");
89 * Purges all OpenGL errors. This function is generally useful to
90 * clear up the pending errors prior to calling gl_check_error().
95 while (glGetError() != GL_NO_ERROR)
102 * Checks whether there is any OpenGL error pending.
104 * Return value: %TRUE if an error was encountered
110 gboolean has_errors = FALSE;
112 while ((error = glGetError()) != GL_NO_ERROR) {
113 GST_DEBUG("glError: %s caught", gl_get_error_string(error));
121 * @param: the parameter name
122 * @pval: return location for the value
124 * This function is a wrapper around glGetIntegerv() that does extra
127 * Return value: %TRUE on success
130 gl_get_param(GLenum param, guint *pval)
135 glGetIntegerv(param, &val);
136 if (gl_check_error())
145 * gl_get_texture_param:
146 * @target: the target to which the texture is bound
147 * @param: the parameter name
148 * @pval: return location for the value
150 * This function is a wrapper around glGetTexLevelParameteriv() that
151 * does extra error checking.
153 * Return value: %TRUE on success
156 gl_get_texture_param(GLenum target, GLenum param, guint *pval)
161 glGetTexLevelParameteriv(target, 0, param, &val);
162 if (gl_check_error())
171 * gl_get_texture_binding:
172 * @target: a texture target
174 * Determines the texture binding type for the specified target.
176 * Return value: texture binding type for @target
179 gl_get_texture_binding(GLenum target)
185 binding = GL_TEXTURE_BINDING_1D;
188 binding = GL_TEXTURE_BINDING_2D;
191 binding = GL_TEXTURE_BINDING_3D;
193 case GL_TEXTURE_RECTANGLE_ARB:
194 binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
205 * @color: the requested RGB color
207 * Sets background color to the RGB @color. This basically is a
208 * wrapper around glClearColor().
211 gl_set_bgcolor(guint32 color)
214 ((color >> 16) & 0xff) / 255.0f,
215 ((color >> 8) & 0xff) / 255.0f,
216 ( color & 0xff) / 255.0f,
223 * @fovy: the field of view angle, in degrees, in the y direction
224 * @aspect: the aspect ratio that determines the field of view in the
225 * x direction. The aspect ratio is the ratio of x (width) to y
227 * @zNear: the distance from the viewer to the near clipping plane
229 * @zFar: the distance from the viewer to the far clipping plane
232 * Specified a viewing frustum into the world coordinate system. This
233 * basically is the Mesa implementation of gluPerspective().
236 gl_perspective(GLdouble fovy, GLdouble aspect, GLdouble near_val, GLdouble far_val)
238 GLdouble left, right, top, bottom;
241 <http://www.opengl.org/resources/faq/technical/transformations.htm> */
242 top = tan(fovy * M_PI / 360.0) * near_val;
244 left = aspect * bottom;
245 right = aspect * top;
246 glFrustum(left, right, bottom, top, near_val, far_val);
251 * @width: the requested width, in pixels
252 * @height: the requested height, in pixels
254 * Resizes the OpenGL viewport to the specified dimensions, using an
255 * orthogonal projection. (0,0) represents the top-left corner of the
259 gl_resize(guint width, guint height)
265 #define Z_CAMERA 0.869f
267 glViewport(0, 0, width, height);
268 glMatrixMode(GL_PROJECTION);
270 gl_perspective(FOVY, ASPECT, Z_NEAR, Z_FAR);
271 glMatrixMode(GL_MODELVIEW);
274 glTranslatef(-0.5f, -0.5f, -Z_CAMERA);
275 glScalef(1.0f/width, -1.0f/height, 1.0f/width);
276 glTranslatef(0.0f, -1.0f*height, 0.0f);
281 * @dpy: an X11 #Display
282 * @screen: the associated screen of @dpy
283 * @parent: the parent #GLContextState, or %NULL if none is to be used
285 * Creates a GLX context sharing textures and displays lists with
286 * @parent, if not %NULL.
288 * Return value: the newly created GLX context
291 gl_create_context(Display *dpy, int screen, GLContextState *parent)
294 GLXFBConfig *fbconfigs = NULL;
295 int fbconfig_id, val, n, n_fbconfigs;
298 static GLint fbconfig_attrs[] = {
299 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
300 GLX_RENDER_TYPE, GLX_RGBA_BIT,
301 GLX_DOUBLEBUFFER, True,
308 cs = malloc(sizeof(*cs));
313 cs->window = parent ? parent->window : None;
316 cs->swapped_buffers = FALSE;
318 if (parent && parent->context) {
319 status = glXQueryContext(
322 GLX_FBCONFIG_ID, &fbconfig_id
324 if (status != Success)
327 if (fbconfig_id == GLX_DONT_CARE)
328 goto choose_fbconfig;
330 fbconfigs = glXGetFBConfigs(dpy, screen, &n_fbconfigs);
334 /* Find out a GLXFBConfig compatible with the parent context */
335 for (n = 0; n < n_fbconfigs; n++) {
336 status = glXGetFBConfigAttrib(
339 GLX_FBCONFIG_ID, &val
341 if (status == Success && val == fbconfig_id)
344 if (n == n_fbconfigs)
349 fbconfigs = glXChooseFBConfig(
352 fbconfig_attrs, &n_fbconfigs
357 /* Select the first one */
361 cs->visual = glXGetVisualFromFBConfig(dpy, fbconfigs[n]);
362 cs->context = glXCreateNewContext(
366 parent ? parent->context : NULL,
373 gl_destroy_context(cs);
382 * gl_destroy_context:
383 * @cs: a #GLContextState
385 * Destroys the GLX context @cs
388 gl_destroy_context(GLContextState *cs)
398 if (cs->display && cs->context) {
399 if (glXGetCurrentContext() == cs->context) {
400 /* XXX: if buffers were never swapped, the application
401 will crash later with the NVIDIA driver */
402 if (!cs->swapped_buffers)
404 glXMakeCurrent(cs->display, None, NULL);
406 glXDestroyContext(cs->display, cs->context);
414 * gl_get_current_context:
415 * @cs: return location to the current #GLContextState
417 * Retrieves the current GLX context, display and drawable packed into
418 * the #GLContextState struct.
421 gl_get_current_context(GLContextState *cs)
423 cs->display = glXGetCurrentDisplay();
424 cs->window = glXGetCurrentDrawable();
425 cs->context = glXGetCurrentContext();
429 * gl_set_current_context:
430 * @new_cs: the requested new #GLContextState
431 * @old_cs: return location to the context that was previously current
433 * Makes the @new_cs GLX context the current GLX rendering context of
434 * the calling thread, replacing the previously current context if
437 * If @old_cs is non %NULL, the previously current GLX context and
438 * window are recorded.
440 * Return value: %TRUE on success
443 gl_set_current_context(GLContextState *new_cs, GLContextState *old_cs)
445 /* If display is NULL, this could be that new_cs was retrieved from
446 gl_get_current_context() with none set previously. If that case,
447 the other fields are also NULL and we don't return an error */
448 if (!new_cs->display)
449 return !new_cs->window && !new_cs->context;
452 if (old_cs == new_cs)
454 gl_get_current_context(old_cs);
455 if (old_cs->display == new_cs->display &&
456 old_cs->window == new_cs->window &&
457 old_cs->context == new_cs->context)
460 return glXMakeCurrent(new_cs->display, new_cs->window, new_cs->context);
465 * @cs: a #GLContextState
467 * Promotes the contents of the back buffer of the @win window to
468 * become the contents of the front buffer. This simply is wrapper
469 * around glXSwapBuffers().
472 gl_swap_buffers(GLContextState *cs)
474 glXSwapBuffers(cs->display, cs->window);
475 cs->swapped_buffers = TRUE;
480 * @ts: a #GLTextureState
481 * @target: the target to which the texture is bound
482 * @texture: the name of a texture
484 * Binds @texture to the specified @target, while recording the
485 * previous state in @ts.
487 * Return value: %TRUE on success
490 gl_bind_texture(GLTextureState *ts, GLenum target, GLuint texture)
496 if (glIsEnabled(target)) {
497 binding = gl_get_texture_binding(target);
500 if (!gl_get_param(binding, &ts->old_texture))
502 ts->was_enabled = TRUE;
503 ts->was_bound = texture == ts->old_texture;
510 ts->was_enabled = FALSE;
511 ts->was_bound = FALSE;
515 glBindTexture(target, texture);
516 if (gl_check_error())
523 * @ts: a #GLTextureState
525 * Rebinds the texture that was previously bound and recorded in @ts.
528 gl_unbind_texture(GLTextureState *ts)
530 if (!ts->was_bound && ts->old_texture)
531 glBindTexture(ts->target, ts->old_texture);
532 if (!ts->was_enabled)
533 glDisable(ts->target);
538 * @target: the target to which the texture is bound
539 * @format: the format of the pixel data
540 * @width: the requested width, in pixels
541 * @height: the requested height, in pixels
543 * Creates a texture with the specified dimensions and @format. The
544 * internal format will be automatically derived from @format.
546 * Return value: the newly created texture name
549 gl_create_texture(GLenum target, GLenum format, guint width, guint height)
551 GLenum internal_format;
554 guint bytes_per_component;
556 internal_format = format;
559 bytes_per_component = 1;
561 case GL_LUMINANCE_ALPHA:
562 bytes_per_component = 2;
566 internal_format = GL_RGBA;
567 bytes_per_component = 4;
570 bytes_per_component = 0;
573 g_assert(bytes_per_component > 0);
575 glGenTextures(1, &texture);
576 if (!gl_bind_texture(&ts, target, texture))
578 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
579 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
580 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
581 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
582 glPixelStorei(GL_UNPACK_ALIGNMENT, bytes_per_component);
593 gl_unbind_texture(&ts);
599 * @name: the name of the OpenGL extension function to lookup
601 * Returns the specified OpenGL extension function
603 * Return value: the OpenGL extension matching @name, or %NULL if none
606 typedef void (*GLFuncPtr)(void);
607 typedef GLFuncPtr (*GLXGetProcAddressProc)(const char *);
610 get_proc_address_default(const char *name)
615 static GLXGetProcAddressProc
616 get_proc_address_func(void)
618 GLXGetProcAddressProc get_proc_func;
621 get_proc_func = (GLXGetProcAddressProc)
622 dlsym(RTLD_DEFAULT, "glXGetProcAddress");
624 return get_proc_func;
626 get_proc_func = (GLXGetProcAddressProc)
627 dlsym(RTLD_DEFAULT, "glXGetProcAddressARB");
629 return get_proc_func;
631 return get_proc_address_default;
634 static inline GLFuncPtr
635 get_proc_address(const char *name)
637 static GLXGetProcAddressProc get_proc_func = NULL;
639 get_proc_func = get_proc_address_func();
640 return get_proc_func(name);
646 * Initializes the global #GLVTable.
648 * Return value: the #GLVTable filled in with OpenGL extensions, or
651 static GLVTable gl_vtable_static;
656 GLVTable * const gl_vtable = &gl_vtable_static;
657 const gchar *gl_extensions = (const gchar *)glGetString(GL_EXTENSIONS);
658 gboolean has_extension;
660 /* GLX_EXT_texture_from_pixmap */
661 gl_vtable->glx_create_pixmap = (PFNGLXCREATEPIXMAPPROC)
662 get_proc_address("glXCreatePixmap");
663 if (!gl_vtable->glx_create_pixmap)
665 gl_vtable->glx_destroy_pixmap = (PFNGLXDESTROYPIXMAPPROC)
666 get_proc_address("glXDestroyPixmap");
667 if (!gl_vtable->glx_destroy_pixmap)
669 gl_vtable->glx_bind_tex_image = (PFNGLXBINDTEXIMAGEEXTPROC)
670 get_proc_address("glXBindTexImageEXT");
671 if (!gl_vtable->glx_bind_tex_image)
673 gl_vtable->glx_release_tex_image = (PFNGLXRELEASETEXIMAGEEXTPROC)
674 get_proc_address("glXReleaseTexImageEXT");
675 if (!gl_vtable->glx_release_tex_image)
678 /* GL_ARB_framebuffer_object */
680 find_string("GL_ARB_framebuffer_object", gl_extensions, " ") ||
681 find_string("GL_EXT_framebuffer_object", gl_extensions, " ")
684 gl_vtable->gl_gen_framebuffers = (PFNGLGENFRAMEBUFFERSEXTPROC)
685 get_proc_address("glGenFramebuffersEXT");
686 if (!gl_vtable->gl_gen_framebuffers)
688 gl_vtable->gl_delete_framebuffers = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
689 get_proc_address("glDeleteFramebuffersEXT");
690 if (!gl_vtable->gl_delete_framebuffers)
692 gl_vtable->gl_bind_framebuffer = (PFNGLBINDFRAMEBUFFEREXTPROC)
693 get_proc_address("glBindFramebufferEXT");
694 if (!gl_vtable->gl_bind_framebuffer)
696 gl_vtable->gl_gen_renderbuffers = (PFNGLGENRENDERBUFFERSEXTPROC)
697 get_proc_address("glGenRenderbuffersEXT");
698 if (!gl_vtable->gl_gen_renderbuffers)
700 gl_vtable->gl_delete_renderbuffers = (PFNGLDELETERENDERBUFFERSEXTPROC)
701 get_proc_address("glDeleteRenderbuffersEXT");
702 if (!gl_vtable->gl_delete_renderbuffers)
704 gl_vtable->gl_bind_renderbuffer = (PFNGLBINDRENDERBUFFEREXTPROC)
705 get_proc_address("glBindRenderbufferEXT");
706 if (!gl_vtable->gl_bind_renderbuffer)
708 gl_vtable->gl_renderbuffer_storage = (PFNGLRENDERBUFFERSTORAGEEXTPROC)
709 get_proc_address("glRenderbufferStorageEXT");
710 if (!gl_vtable->gl_renderbuffer_storage)
712 gl_vtable->gl_framebuffer_renderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)
713 get_proc_address("glFramebufferRenderbufferEXT");
714 if (!gl_vtable->gl_framebuffer_renderbuffer)
716 gl_vtable->gl_framebuffer_texture_2d = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
717 get_proc_address("glFramebufferTexture2DEXT");
718 if (!gl_vtable->gl_framebuffer_texture_2d)
720 gl_vtable->gl_check_framebuffer_status = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
721 get_proc_address("glCheckFramebufferStatusEXT");
722 if (!gl_vtable->gl_check_framebuffer_status)
724 gl_vtable->has_framebuffer_object = TRUE;
727 /* GL_ARB_fragment_program */
729 find_string("GL_ARB_fragment_program", gl_extensions, " ")
732 gl_vtable->gl_gen_programs = (PFNGLGENPROGRAMSARBPROC)
733 get_proc_address("glGenProgramsARB");
734 if (!gl_vtable->gl_gen_programs)
736 gl_vtable->gl_delete_programs = (PFNGLDELETEPROGRAMSARBPROC)
737 get_proc_address("glDeleteProgramsARB");
738 if (!gl_vtable->gl_delete_programs)
740 gl_vtable->gl_bind_program = (PFNGLBINDPROGRAMARBPROC)
741 get_proc_address("glBindProgramARB");
742 if (!gl_vtable->gl_bind_program)
744 gl_vtable->gl_program_string = (PFNGLPROGRAMSTRINGARBPROC)
745 get_proc_address("glProgramStringARB");
746 if (!gl_vtable->gl_program_string)
748 gl_vtable->gl_get_program_iv = (PFNGLGETPROGRAMIVARBPROC)
749 get_proc_address("glGetProgramivARB");
750 if (!gl_vtable->gl_get_program_iv)
752 gl_vtable->gl_program_local_parameter_4fv = (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC)
753 get_proc_address("glProgramLocalParameter4fvARB");
754 if (!gl_vtable->gl_program_local_parameter_4fv)
756 gl_vtable->has_fragment_program = TRUE;
759 /* GL_ARB_multitexture */
761 find_string("GL_ARB_multitexture", gl_extensions, " ")
764 gl_vtable->gl_active_texture = (PFNGLACTIVETEXTUREPROC)
765 get_proc_address("glActiveTextureARB");
766 if (!gl_vtable->gl_active_texture)
768 gl_vtable->gl_multi_tex_coord_2f = (PFNGLMULTITEXCOORD2FPROC)
769 get_proc_address("glMultiTexCoord2fARB");
770 if (!gl_vtable->gl_multi_tex_coord_2f)
772 gl_vtable->has_multitexture = TRUE;
780 * Retrieves a VTable for OpenGL extensions.
782 * Return value: VTable for OpenGL extensions
787 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
788 static gboolean gl_vtable_init = TRUE;
789 static GLVTable *gl_vtable = NULL;
791 g_static_mutex_lock(&mutex);
792 if (gl_vtable_init) {
793 gl_vtable_init = FALSE;
794 gl_vtable = gl_init_vtable();
796 g_static_mutex_unlock(&mutex);
801 * gl_create_pixmap_object:
802 * @dpy: an X11 #Display
803 * @width: the request width, in pixels
804 * @height: the request height, in pixels
806 * Creates a #GLPixmapObject of the specified dimensions. This
807 * requires the GLX_EXT_texture_from_pixmap extension.
809 * Return value: the newly created #GLPixmapObject object
812 gl_create_pixmap_object(Display *dpy, guint width, guint height)
814 GLVTable * const gl_vtable = gl_get_vtable();
815 GLPixmapObject *pixo;
816 GLXFBConfig *fbconfig;
819 XWindowAttributes wattr;
821 int n_fbconfig_attrs;
823 int fbconfig_attrs[32] = {
824 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
825 GLX_DOUBLEBUFFER, GL_FALSE,
826 GLX_RENDER_TYPE, GLX_RGBA_BIT,
827 GLX_X_RENDERABLE, GL_TRUE,
828 GLX_Y_INVERTED_EXT, GL_TRUE,
835 int pixmap_attrs[10] = {
836 GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
837 GLX_MIPMAP_TEXTURE_EXT, GL_FALSE,
844 screen = DefaultScreen(dpy);
845 rootwin = RootWindow(dpy, screen);
847 /* XXX: this won't work for different displays */
848 if (!gl_vtable->has_texture_from_pixmap) {
849 const char *glx_extensions = glXQueryExtensionsString(dpy, screen);
852 if (!find_string("GLX_EXT_texture_from_pixmap", glx_extensions, " "))
854 gl_vtable->has_texture_from_pixmap = TRUE;
857 pixo = calloc(1, sizeof(*pixo));
863 pixo->height = height;
865 pixo->glx_pixmap = None;
866 pixo->is_bound = FALSE;
868 XGetWindowAttributes(dpy, rootwin, &wattr);
869 pixo->pixmap = XCreatePixmap(dpy, rootwin, width, height, wattr.depth);
873 /* Initialize FBConfig attributes */
874 for (attr = fbconfig_attrs; *attr != GL_NONE; attr += 2)
876 *attr++ = GLX_DEPTH_SIZE; *attr++ = wattr.depth;
877 if (wattr.depth == 32) {
878 *attr++ = GLX_ALPHA_SIZE; *attr++ = 8;
879 *attr++ = GLX_BIND_TO_TEXTURE_RGBA_EXT; *attr++ = GL_TRUE;
882 *attr++ = GLX_BIND_TO_TEXTURE_RGB_EXT; *attr++ = GL_TRUE;
886 fbconfig = glXChooseFBConfig(
889 fbconfig_attrs, &n_fbconfig_attrs
894 /* Initialize GLX Pixmap attributes */
895 for (attr = pixmap_attrs; *attr != GL_NONE; attr += 2)
897 *attr++ = GLX_TEXTURE_FORMAT_EXT;
898 if (wattr.depth == 32)
899 *attr++ = GLX_TEXTURE_FORMAT_RGBA_EXT;
901 *attr++ = GLX_TEXTURE_FORMAT_RGB_EXT;
905 pixo->glx_pixmap = gl_vtable->glx_create_pixmap(
912 if (x11_untrap_errors() != 0)
915 pixo->target = GL_TEXTURE_2D;
916 glGenTextures(1, &pixo->texture);
917 if (!gl_bind_texture(&pixo->old_texture, pixo->target, pixo->texture))
919 glTexParameteri(pixo->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
920 glTexParameteri(pixo->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
921 gl_unbind_texture(&pixo->old_texture);
925 gl_destroy_pixmap_object(pixo);
930 * gl_destroy_pixmap_object:
931 * @pixo: a #GLPixmapObject
933 * Destroys the #GLPixmapObject object.
936 gl_destroy_pixmap_object(GLPixmapObject *pixo)
938 GLVTable * const gl_vtable = gl_get_vtable();
943 gl_unbind_pixmap_object(pixo);
946 glDeleteTextures(1, &pixo->texture);
950 if (pixo->glx_pixmap) {
951 gl_vtable->glx_destroy_pixmap(pixo->dpy, pixo->glx_pixmap);
952 pixo->glx_pixmap = None;
956 XFreePixmap(pixo->dpy, pixo->pixmap);
963 * gl_bind_pixmap_object:
964 * @pixo: a #GLPixmapObject
966 * Defines a two-dimensional texture image. The texture image is taken
967 * from the @pixo pixmap and need not be copied. The texture target,
968 * format and size are derived from attributes of the @pixo pixmap.
970 * Return value: %TRUE on success
973 gl_bind_pixmap_object(GLPixmapObject *pixo)
975 GLVTable * const gl_vtable = gl_get_vtable();
980 if (!gl_bind_texture(&pixo->old_texture, pixo->target, pixo->texture))
984 gl_vtable->glx_bind_tex_image(
990 XSync(pixo->dpy, False);
991 if (x11_untrap_errors() != 0) {
992 GST_DEBUG("failed to bind pixmap");
996 pixo->is_bound = TRUE;
1001 * gl_unbind_pixmap_object:
1002 * @pixo: a #GLPixmapObject
1004 * Releases a color buffers that is being used as a texture.
1006 * Return value: %TRUE on success
1009 gl_unbind_pixmap_object(GLPixmapObject *pixo)
1011 GLVTable * const gl_vtable = gl_get_vtable();
1013 if (!pixo->is_bound)
1017 gl_vtable->glx_release_tex_image(
1022 XSync(pixo->dpy, False);
1023 if (x11_untrap_errors() != 0) {
1024 GST_DEBUG("failed to release pixmap");
1028 gl_unbind_texture(&pixo->old_texture);
1030 pixo->is_bound = FALSE;
1035 * gl_create_framebuffer_object:
1036 * @target: the target to which the texture is bound
1037 * @texture: the GL texture to hold the framebuffer
1038 * @width: the requested width, in pixels
1039 * @height: the requested height, in pixels
1041 * Creates an FBO with the specified texture and size.
1043 * Return value: the newly created #GLFramebufferObject, or %NULL if
1046 GLFramebufferObject *
1047 gl_create_framebuffer_object(
1054 GLVTable * const gl_vtable = gl_get_vtable();
1055 GLFramebufferObject *fbo;
1058 if (!gl_vtable || !gl_vtable->has_framebuffer_object)
1061 /* XXX: we only support GL_TEXTURE_2D at this time */
1062 if (target != GL_TEXTURE_2D)
1065 fbo = calloc(1, sizeof(*fbo));
1070 fbo->height = height;
1073 fbo->is_bound = FALSE;
1075 gl_get_param(GL_FRAMEBUFFER_BINDING, &fbo->old_fbo);
1076 gl_vtable->gl_gen_framebuffers(1, &fbo->fbo);
1077 gl_vtable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, fbo->fbo);
1078 gl_vtable->gl_framebuffer_texture_2d(
1080 GL_COLOR_ATTACHMENT0_EXT,
1085 status = gl_vtable->gl_check_framebuffer_status(GL_DRAW_FRAMEBUFFER_EXT);
1086 gl_vtable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, fbo->old_fbo);
1087 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
1092 gl_destroy_framebuffer_object(fbo);
1097 * gl_destroy_framebuffer_object:
1098 * @fbo: a #GLFramebufferObject
1100 * Destroys the @fbo object.
1103 gl_destroy_framebuffer_object(GLFramebufferObject *fbo)
1105 GLVTable * const gl_vtable = gl_get_vtable();
1110 gl_unbind_framebuffer_object(fbo);
1113 gl_vtable->gl_delete_framebuffers(1, &fbo->fbo);
1120 * gl_bind_framebuffer_object:
1121 * @fbo: a #GLFramebufferObject
1123 * Binds @fbo object.
1125 * Return value: %TRUE on success
1128 gl_bind_framebuffer_object(GLFramebufferObject *fbo)
1130 GLVTable * const gl_vtable = gl_get_vtable();
1131 const guint width = fbo->width;
1132 const guint height = fbo->height;
1134 const guint attribs = (GL_VIEWPORT_BIT|
1138 GL_COLOR_BUFFER_BIT);
1143 gl_get_param(GL_FRAMEBUFFER_BINDING, &fbo->old_fbo);
1144 gl_vtable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, fbo->fbo);
1145 glPushAttrib(attribs);
1146 glMatrixMode(GL_PROJECTION);
1149 glMatrixMode(GL_MODELVIEW);
1152 glViewport(0, 0, width, height);
1153 glTranslatef(-1.0f, -1.0f, 0.0f);
1154 glScalef(2.0f / width, 2.0f / height, 1.0f);
1156 fbo->is_bound = TRUE;
1161 * gl_unbind_framebuffer_object:
1162 * @fbo: a #GLFramebufferObject
1164 * Releases @fbo object.
1166 * Return value: %TRUE on success
1169 gl_unbind_framebuffer_object(GLFramebufferObject *fbo)
1171 GLVTable * const gl_vtable = gl_get_vtable();
1177 glMatrixMode(GL_PROJECTION);
1179 glMatrixMode(GL_MODELVIEW);
1181 gl_vtable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, fbo->old_fbo);
1183 fbo->is_bound = FALSE;