2 * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "va_glx_private.h"
27 #include "va_glx_impl.h"
35 static void va_glx_error_message(const char *format, ...)
38 va_start(args, format);
39 fprintf(stderr, "libva-glx error: ");
40 vfprintf(stderr, format, args);
45 static int x11_error_code = 0;
46 static int (*old_error_handler)(Display *, XErrorEvent *);
48 static int error_handler(Display *dpy, XErrorEvent *error)
50 x11_error_code = error->error_code;
54 static void x11_trap_errors(void)
57 old_error_handler = XSetErrorHandler(error_handler);
60 static int x11_untrap_errors(void)
62 XSetErrorHandler(old_error_handler);
63 return x11_error_code;
66 // Returns a string representation of an OpenGL error
67 static const char *gl_get_error_string(GLenum error)
74 { GL_NO_ERROR, "no error" },
75 { GL_INVALID_ENUM, "invalid enumerant" },
76 { GL_INVALID_VALUE, "invalid value" },
77 { GL_INVALID_OPERATION, "invalid operation" },
78 { GL_STACK_OVERFLOW, "stack overflow" },
79 { GL_STACK_UNDERFLOW, "stack underflow" },
80 { GL_OUT_OF_MEMORY, "out of memory" },
81 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
82 { GL_INVALID_FRAMEBUFFER_OPERATION_EXT, "invalid framebuffer operation" },
88 for (i = 0; gl_errors[i].str; i++) {
89 if (gl_errors[i].val == error)
90 return gl_errors[i].str;
95 static inline int gl_do_check_error(int report)
99 while ((error = glGetError()) != GL_NO_ERROR) {
101 va_glx_error_message("glError: %s caught\n",
102 gl_get_error_string(error));
108 static inline void gl_purge_errors(void)
110 gl_do_check_error(0);
113 static inline int gl_check_error(void)
115 return gl_do_check_error(1);
118 // glGetTexLevelParameteriv() wrapper
119 static int gl_get_texture_param(GLenum param, unsigned int *pval)
124 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, param, &val);
125 if (gl_check_error())
132 // Returns the OpenGL VTable
133 static inline VAOpenGLVTableP gl_get_vtable(VADriverContextP ctx)
135 return &VA_DRIVER_CONTEXT_GLX(ctx)->gl_vtable;
138 // Lookup for a GLX function
139 typedef void (*GLFuncPtr)(void);
140 typedef GLFuncPtr (*GLXGetProcAddressProc)(const char *);
142 static GLFuncPtr get_proc_address_default(const char *name)
147 static GLXGetProcAddressProc get_proc_address_func(void)
149 GLXGetProcAddressProc get_proc_func;
152 get_proc_func = (GLXGetProcAddressProc)
153 dlsym(RTLD_DEFAULT, "glXGetProcAddress");
155 return get_proc_func;
157 get_proc_func = (GLXGetProcAddressProc)
158 dlsym(RTLD_DEFAULT, "glXGetProcAddressARB");
160 return get_proc_func;
162 return get_proc_address_default;
165 static inline GLFuncPtr get_proc_address(const char *name)
167 static GLXGetProcAddressProc get_proc_func = NULL;
169 get_proc_func = get_proc_address_func();
170 return get_proc_func(name);
173 // Check for GLX extensions (TFP, FBO)
174 static int check_extension(const char *name, const char *ext)
182 end = ext + strlen(ext);
183 name_len = strlen(name);
185 n = strcspn(ext, " ");
186 if (n == name_len && strncmp(name, ext, n) == 0)
193 static int check_tfp_extensions(VADriverContextP ctx)
195 const char *gl_extensions;
196 const char *glx_extensions;
198 gl_extensions = (const char *)glGetString(GL_EXTENSIONS);
199 if (!check_extension("GL_ARB_texture_non_power_of_two", gl_extensions))
202 glx_extensions = glXQueryExtensionsString(ctx->native_dpy, ctx->x11_screen);
203 if (!check_extension("GLX_EXT_texture_from_pixmap", glx_extensions))
208 static int check_fbo_extensions(VADriverContextP ctx)
210 const char *gl_extensions;
212 gl_extensions = (const char *)glGetString(GL_EXTENSIONS);
213 if (check_extension("GL_ARB_framebuffer_object", gl_extensions))
215 if (check_extension("GL_EXT_framebuffer_object", gl_extensions))
220 // Load GLX extensions
221 static int load_tfp_extensions(VADriverContextP ctx)
223 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
225 pOpenGLVTable->glx_create_pixmap = (PFNGLXCREATEPIXMAPPROC)
226 get_proc_address("glXCreatePixmap");
227 if (!pOpenGLVTable->glx_create_pixmap)
229 pOpenGLVTable->glx_destroy_pixmap = (PFNGLXDESTROYPIXMAPPROC)
230 get_proc_address("glXDestroyPixmap");
231 if (!pOpenGLVTable->glx_destroy_pixmap)
233 pOpenGLVTable->glx_bind_tex_image = (PFNGLXBINDTEXIMAGEEXTPROC)
234 get_proc_address("glXBindTexImageEXT");
235 if (!pOpenGLVTable->glx_bind_tex_image)
237 pOpenGLVTable->glx_release_tex_image = (PFNGLXRELEASETEXIMAGEEXTPROC)
238 get_proc_address("glXReleaseTexImageEXT");
239 if (!pOpenGLVTable->glx_release_tex_image)
244 static int load_fbo_extensions(VADriverContextP ctx)
246 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
248 pOpenGLVTable->gl_gen_framebuffers = (PFNGLGENFRAMEBUFFERSEXTPROC)
249 get_proc_address("glGenFramebuffersEXT");
250 if (!pOpenGLVTable->gl_gen_framebuffers)
252 pOpenGLVTable->gl_delete_framebuffers = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
253 get_proc_address("glDeleteFramebuffersEXT");
254 if (!pOpenGLVTable->gl_delete_framebuffers)
256 pOpenGLVTable->gl_bind_framebuffer = (PFNGLBINDFRAMEBUFFEREXTPROC)
257 get_proc_address("glBindFramebufferEXT");
258 if (!pOpenGLVTable->gl_bind_framebuffer)
260 pOpenGLVTable->gl_gen_renderbuffers = (PFNGLGENRENDERBUFFERSEXTPROC)
261 get_proc_address("glGenRenderbuffersEXT");
262 if (!pOpenGLVTable->gl_gen_renderbuffers)
264 pOpenGLVTable->gl_delete_renderbuffers = (PFNGLDELETERENDERBUFFERSEXTPROC)
265 get_proc_address("glDeleteRenderbuffersEXT");
266 if (!pOpenGLVTable->gl_delete_renderbuffers)
268 pOpenGLVTable->gl_bind_renderbuffer = (PFNGLBINDRENDERBUFFEREXTPROC)
269 get_proc_address("glBindRenderbufferEXT");
270 if (!pOpenGLVTable->gl_bind_renderbuffer)
272 pOpenGLVTable->gl_renderbuffer_storage = (PFNGLRENDERBUFFERSTORAGEEXTPROC)
273 get_proc_address("glRenderbufferStorageEXT");
274 if (!pOpenGLVTable->gl_renderbuffer_storage)
276 pOpenGLVTable->gl_framebuffer_renderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)
277 get_proc_address("glFramebufferRenderbufferEXT");
278 if (!pOpenGLVTable->gl_framebuffer_renderbuffer)
280 pOpenGLVTable->gl_framebuffer_texture_2d = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
281 get_proc_address("glFramebufferTexture2DEXT");
282 if (!pOpenGLVTable->gl_framebuffer_texture_2d)
284 pOpenGLVTable->gl_check_framebuffer_status = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
285 get_proc_address("glCheckFramebufferStatusEXT");
286 if (!pOpenGLVTable->gl_check_framebuffer_status)
292 /* ========================================================================= */
293 /* === VA/GLX helpers === */
294 /* ========================================================================= */
296 // OpenGL context state
297 typedef struct OpenGLContextState *OpenGLContextStateP;
299 struct OpenGLContextState {
306 gl_destroy_context(OpenGLContextStateP cs)
311 if (cs->display && cs->context) {
312 if (glXGetCurrentContext() == cs->context)
313 glXMakeCurrent(cs->display, None, NULL);
314 glXDestroyContext(cs->display, cs->context);
321 static OpenGLContextStateP
322 gl_create_context(VADriverContextP ctx, OpenGLContextStateP parent)
324 OpenGLContextStateP cs;
325 GLXFBConfig *fbconfigs = NULL;
326 int fbconfig_id, val, n, n_fbconfigs;
329 static GLint fbconfig_attrs[] = {
330 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
331 GLX_RENDER_TYPE, GLX_RGBA_BIT,
332 GLX_DOUBLEBUFFER, True,
339 cs = malloc(sizeof(*cs));
344 cs->display = parent->display;
345 cs->window = parent->window;
348 cs->display = ctx->native_dpy;
353 if (parent && parent->context) {
354 status = glXQueryContext(
357 GLX_FBCONFIG_ID, &fbconfig_id
359 if (status != Success)
362 if (fbconfig_id == GLX_DONT_CARE)
363 goto choose_fbconfig;
365 fbconfigs = glXGetFBConfigs(
367 DefaultScreen(parent->display),
373 /* Find out a GLXFBConfig compatible with the parent context */
374 for (n = 0; n < n_fbconfigs; n++) {
375 status = glXGetFBConfigAttrib(
378 GLX_FBCONFIG_ID, &val
380 if (status == Success && val == fbconfig_id)
383 if (n == n_fbconfigs)
388 fbconfigs = glXChooseFBConfig(
391 fbconfig_attrs, &n_fbconfigs
396 /* Select the first one */
400 cs->context = glXCreateNewContext(
404 parent ? parent->context : NULL,
411 gl_destroy_context(cs);
419 static void gl_get_current_context(OpenGLContextStateP cs)
421 cs->display = glXGetCurrentDisplay();
422 cs->window = glXGetCurrentDrawable();
423 cs->context = glXGetCurrentContext();
427 gl_set_current_context(OpenGLContextStateP new_cs, OpenGLContextStateP old_cs)
429 /* If display is NULL, this could be that new_cs was retrieved from
430 gl_get_current_context() with none set previously. If that case,
431 the other fields are also NULL and we don't return an error */
432 if (!new_cs->display)
433 return !new_cs->window && !new_cs->context;
436 if (old_cs == new_cs)
438 gl_get_current_context(old_cs);
439 if (old_cs->display == new_cs->display &&
440 old_cs->window == new_cs->window &&
441 old_cs->context == new_cs->context)
444 return glXMakeCurrent(new_cs->display, new_cs->window, new_cs->context);
447 /** Unique VASurfaceGLX identifier */
448 #define VA_SURFACE_GLX_MAGIC VA_FOURCC('V','A','G','L')
450 struct VASurfaceGLX {
451 uint32_t magic; ///< Magic number identifying a VASurfaceGLX
452 GLenum target; ///< GL target to which the texture is bound
453 GLuint texture; ///< GL texture
454 VASurfaceID surface; ///< Associated VA surface
457 OpenGLContextStateP gl_context;
461 GLXPixmap glx_pixmap;
465 // Create Pixmaps for GLX texture-from-pixmap extension
466 static int create_tfp_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
468 VAOpenGLVTableP const pOpenGLVTable = gl_get_vtable(ctx);
469 const unsigned int width = pSurfaceGLX->width;
470 const unsigned int height = pSurfaceGLX->height;
471 Pixmap pixmap = None;
472 GLXFBConfig *fbconfig = NULL;
473 GLXPixmap glx_pixmap = None;
475 XWindowAttributes wattr;
477 int n_fbconfig_attrs;
479 root_window = RootWindow(ctx->native_dpy, ctx->x11_screen);
480 XGetWindowAttributes(ctx->native_dpy, root_window, &wattr);
481 if (wattr.depth != 24 && wattr.depth != 32)
483 pixmap = XCreatePixmap(
492 pSurfaceGLX->pixmap = pixmap;
494 int fbconfig_attrs[32] = {
495 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
496 GLX_DOUBLEBUFFER, GL_TRUE,
497 GLX_RENDER_TYPE, GLX_RGBA_BIT,
498 GLX_X_RENDERABLE, GL_TRUE,
499 GLX_Y_INVERTED_EXT, GL_TRUE,
505 for (attrib = fbconfig_attrs; *attrib != GL_NONE; attrib += 2)
507 if (wattr.depth == 32) {
508 *attrib++ = GLX_ALPHA_SIZE; *attrib++ = 8;
509 *attrib++ = GLX_BIND_TO_TEXTURE_RGBA_EXT; *attrib++ = GL_TRUE;
512 *attrib++ = GLX_BIND_TO_TEXTURE_RGB_EXT; *attrib++ = GL_TRUE;
516 fbconfig = glXChooseFBConfig(
525 int pixmap_attrs[10] = {
526 GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
527 GLX_MIPMAP_TEXTURE_EXT, GL_FALSE,
530 for (attrib = pixmap_attrs; *attrib != GL_NONE; attrib += 2)
532 *attrib++ = GLX_TEXTURE_FORMAT_EXT;
533 if (wattr.depth == 32)
534 *attrib++ = GLX_TEXTURE_FORMAT_RGBA_EXT;
536 *attrib++ = GLX_TEXTURE_FORMAT_RGB_EXT;
540 glx_pixmap = pOpenGLVTable->glx_create_pixmap(
547 if (x11_untrap_errors() != 0)
549 pSurfaceGLX->glx_pixmap = glx_pixmap;
551 glGenTextures(1, &pSurfaceGLX->pix_texture);
552 glBindTexture(GL_TEXTURE_2D, pSurfaceGLX->pix_texture);
553 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
554 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
558 // Destroy Pixmaps used for TFP
559 static void destroy_tfp_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
561 VAOpenGLVTableP const pOpenGLVTable = gl_get_vtable(ctx);
563 if (pSurfaceGLX->pix_texture) {
564 glDeleteTextures(1, &pSurfaceGLX->pix_texture);
565 pSurfaceGLX->pix_texture = 0;
568 if (pSurfaceGLX->glx_pixmap) {
569 pOpenGLVTable->glx_destroy_pixmap(ctx->native_dpy, pSurfaceGLX->glx_pixmap);
570 pSurfaceGLX->glx_pixmap = None;
573 if (pSurfaceGLX->pixmap) {
574 XFreePixmap(ctx->native_dpy, pSurfaceGLX->pixmap);
575 pSurfaceGLX->pixmap = None;
579 // Bind GLX Pixmap to texture
580 static int bind_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
582 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
584 if (pSurfaceGLX->is_bound)
587 glBindTexture(GL_TEXTURE_2D, pSurfaceGLX->pix_texture);
590 pOpenGLVTable->glx_bind_tex_image(
592 pSurfaceGLX->glx_pixmap,
596 XSync(ctx->native_dpy, False);
597 if (x11_untrap_errors() != 0) {
598 va_glx_error_message("failed to bind pixmap\n");
602 pSurfaceGLX->is_bound = 1;
606 // Release GLX Pixmap from texture
607 static int unbind_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
609 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
611 if (!pSurfaceGLX->is_bound)
615 pOpenGLVTable->glx_release_tex_image(
617 pSurfaceGLX->glx_pixmap,
620 XSync(ctx->native_dpy, False);
621 if (x11_untrap_errors() != 0) {
622 va_glx_error_message("failed to release pixmap\n");
626 glBindTexture(GL_TEXTURE_2D, 0);
628 pSurfaceGLX->is_bound = 0;
632 // Render GLX Pixmap to texture
633 static void render_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
635 const unsigned int w = pSurfaceGLX->width;
636 const unsigned int h = pSurfaceGLX->height;
638 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
641 glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0);
642 glTexCoord2f(0.0f, 1.0f); glVertex2i(0, h);
643 glTexCoord2f(1.0f, 1.0f); glVertex2i(w, h);
644 glTexCoord2f(1.0f, 0.0f); glVertex2i(w, 0);
649 // Create offscreen surface
650 static int create_fbo_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
652 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
656 pOpenGLVTable->gl_gen_framebuffers(1, &fbo);
657 pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, fbo);
658 pOpenGLVTable->gl_framebuffer_texture_2d(
660 GL_COLOR_ATTACHMENT0_EXT,
662 pSurfaceGLX->texture,
666 status = pOpenGLVTable->gl_check_framebuffer_status(GL_DRAW_FRAMEBUFFER_EXT);
667 pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, 0);
668 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
671 pSurfaceGLX->fbo = fbo;
675 // Destroy offscreen surface
676 static void destroy_fbo_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
678 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
680 if (pSurfaceGLX->fbo) {
681 pOpenGLVTable->gl_delete_framebuffers(1, &pSurfaceGLX->fbo);
682 pSurfaceGLX->fbo = 0;
686 // Setup matrices to match the FBO texture dimensions
687 static void fbo_enter(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
689 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
690 const unsigned int width = pSurfaceGLX->width;
691 const unsigned int height = pSurfaceGLX->height;
693 pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, pSurfaceGLX->fbo);
694 glPushAttrib(GL_VIEWPORT_BIT);
695 glMatrixMode(GL_PROJECTION);
698 glMatrixMode(GL_MODELVIEW);
701 glViewport(0, 0, width, height);
702 glTranslatef(-1.0f, -1.0f, 0.0f);
703 glScalef(2.0f / width, 2.0f / height, 1.0f);
706 // Restore original OpenGL matrices
707 static void fbo_leave(VADriverContextP ctx)
709 VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
712 glMatrixMode(GL_PROJECTION);
714 glMatrixMode(GL_MODELVIEW);
716 pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, 0);
719 // Check internal texture format is supported
720 static int is_supported_internal_format(GLenum format)
722 /* XXX: we don't support other textures than RGBA */
732 // Destroy VA/GLX surface
734 destroy_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
736 unbind_pixmap(ctx, pSurfaceGLX);
737 destroy_fbo_surface(ctx, pSurfaceGLX);
738 destroy_tfp_surface(ctx, pSurfaceGLX);
742 // Create VA/GLX surface
744 create_surface(VADriverContextP ctx, GLenum target, GLuint texture)
746 VASurfaceGLXP pSurfaceGLX = NULL;
747 unsigned int internal_format, border_width, width, height;
750 pSurfaceGLX = malloc(sizeof(*pSurfaceGLX));
754 pSurfaceGLX->magic = VA_SURFACE_GLX_MAGIC;
755 pSurfaceGLX->target = target;
756 pSurfaceGLX->texture = texture;
757 pSurfaceGLX->surface = VA_INVALID_SURFACE;
758 pSurfaceGLX->gl_context = NULL;
759 pSurfaceGLX->is_bound = 0;
760 pSurfaceGLX->pixmap = None;
761 pSurfaceGLX->pix_texture = 0;
762 pSurfaceGLX->glx_pixmap = None;
763 pSurfaceGLX->fbo = 0;
766 glBindTexture(target, texture);
767 if (!gl_get_texture_param(GL_TEXTURE_INTERNAL_FORMAT, &internal_format))
769 if (!is_supported_internal_format(internal_format))
772 /* Check texture dimensions */
773 if (!gl_get_texture_param(GL_TEXTURE_BORDER, &border_width))
775 if (!gl_get_texture_param(GL_TEXTURE_WIDTH, &width))
777 if (!gl_get_texture_param(GL_TEXTURE_HEIGHT, &height))
780 width -= 2 * border_width;
781 height -= 2 * border_width;
782 if (width == 0 || height == 0)
785 pSurfaceGLX->width = width;
786 pSurfaceGLX->height = height;
788 /* Create TFP objects */
789 if (!create_tfp_surface(ctx, pSurfaceGLX))
792 /* Create FBO objects */
793 if (!create_fbo_surface(ctx, pSurfaceGLX))
798 if (is_error && pSurfaceGLX) {
799 destroy_surface(ctx, pSurfaceGLX);
806 /* ========================================================================= */
807 /* === VA/GLX implementation from the driver (fordward calls) === */
808 /* ========================================================================= */
810 #define INVOKE(ctx, func, args) do { \
811 VADriverVTableGLXP vtable = (ctx)->vtable_glx; \
812 if (!vtable->va##func##GLX) \
813 return VA_STATUS_ERROR_UNIMPLEMENTED; \
815 VAStatus status = vtable->va##func##GLX args; \
816 if (status != VA_STATUS_SUCCESS) \
821 vaCreateSurfaceGLX_impl_driver(
822 VADriverContextP ctx,
828 INVOKE(ctx, CreateSurface, (ctx, target, texture, gl_surface));
829 return VA_STATUS_SUCCESS;
833 vaDestroySurfaceGLX_impl_driver(VADriverContextP ctx, void *gl_surface)
835 INVOKE(ctx, DestroySurface, (ctx, gl_surface));
836 return VA_STATUS_SUCCESS;
840 vaCopySurfaceGLX_impl_driver(
841 VADriverContextP ctx,
847 INVOKE(ctx, CopySurface, (ctx, gl_surface, surface, flags));
848 return VA_STATUS_SUCCESS;
854 /* ========================================================================= */
855 /* === VA/GLX implementation from libVA (generic and suboptimal path) === */
856 /* ========================================================================= */
858 #define INIT_SURFACE(surface, surface_arg) do { \
859 surface = (VASurfaceGLXP)(surface_arg); \
860 if (!check_surface(surface)) \
861 return VA_STATUS_ERROR_INVALID_SURFACE; \
864 // Check VASurfaceGLX is valid
865 static inline int check_surface(VASurfaceGLXP pSurfaceGLX)
867 return pSurfaceGLX && pSurfaceGLX->magic == VA_SURFACE_GLX_MAGIC;
871 vaCreateSurfaceGLX_impl_libva(
872 VADriverContextP ctx,
878 VASurfaceGLXP pSurfaceGLX;
879 struct OpenGLContextState old_cs, *new_cs;
881 gl_get_current_context(&old_cs);
882 new_cs = gl_create_context(ctx, &old_cs);
885 if (!gl_set_current_context(new_cs, NULL))
888 pSurfaceGLX = create_surface(ctx, target, texture);
892 pSurfaceGLX->gl_context = new_cs;
893 *gl_surface = pSurfaceGLX;
895 gl_set_current_context(&old_cs, NULL);
896 return VA_STATUS_SUCCESS;
900 gl_destroy_context(new_cs);
902 return VA_STATUS_ERROR_ALLOCATION_FAILED;
906 vaDestroySurfaceGLX_impl_libva(VADriverContextP ctx, void *gl_surface)
908 VASurfaceGLXP pSurfaceGLX;
909 struct OpenGLContextState old_cs, *new_cs;
911 INIT_SURFACE(pSurfaceGLX, gl_surface);
913 new_cs = pSurfaceGLX->gl_context;
914 if (!gl_set_current_context(new_cs, &old_cs))
915 return VA_STATUS_ERROR_OPERATION_FAILED;
917 destroy_surface(ctx, pSurfaceGLX);
919 gl_destroy_context(new_cs);
920 gl_set_current_context(&old_cs, NULL);
921 return VA_STATUS_SUCCESS;
924 static inline VAStatus
925 deassociate_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
927 if (!unbind_pixmap(ctx, pSurfaceGLX))
928 return VA_STATUS_ERROR_OPERATION_FAILED;
930 pSurfaceGLX->surface = VA_INVALID_SURFACE;
931 return VA_STATUS_SUCCESS;
936 VADriverContextP ctx,
937 VASurfaceGLXP pSurfaceGLX,
944 /* XXX: optimise case where we are associating the same VA surface
945 as before an no changed occurred to it */
946 status = deassociate_surface(ctx, pSurfaceGLX);
947 if (status != VA_STATUS_SUCCESS)
951 status = ctx->vtable->vaPutSurface(
954 (void *)pSurfaceGLX->pixmap,
955 0, 0, pSurfaceGLX->width, pSurfaceGLX->height,
956 0, 0, pSurfaceGLX->width, pSurfaceGLX->height,
960 XSync(ctx->native_dpy, False);
961 if (x11_untrap_errors() != 0)
962 return VA_STATUS_ERROR_OPERATION_FAILED;
963 if (status != VA_STATUS_SUCCESS)
966 pSurfaceGLX->surface = surface;
967 return VA_STATUS_SUCCESS;
970 static inline VAStatus
971 sync_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
973 if (pSurfaceGLX->surface == VA_INVALID_SURFACE)
974 return VA_STATUS_ERROR_INVALID_SURFACE;
976 return ctx->vtable->vaSyncSurface(ctx, pSurfaceGLX->surface);
979 static inline VAStatus
980 begin_render_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
984 status = sync_surface(ctx, pSurfaceGLX);
985 if (status != VA_STATUS_SUCCESS)
988 if (!bind_pixmap(ctx, pSurfaceGLX))
989 return VA_STATUS_ERROR_OPERATION_FAILED;
991 return VA_STATUS_SUCCESS;
994 static inline VAStatus
995 end_render_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
997 if (!unbind_pixmap(ctx, pSurfaceGLX))
998 return VA_STATUS_ERROR_OPERATION_FAILED;
1000 return VA_STATUS_SUCCESS;
1005 VADriverContextP ctx,
1006 VASurfaceGLXP pSurfaceGLX,
1007 VASurfaceID surface,
1013 /* Associate VA surface */
1014 status = associate_surface(ctx, pSurfaceGLX, surface, flags);
1015 if (status != VA_STATUS_SUCCESS)
1019 fbo_enter(ctx, pSurfaceGLX);
1020 status = begin_render_surface(ctx, pSurfaceGLX);
1021 if (status == VA_STATUS_SUCCESS) {
1022 render_pixmap(ctx, pSurfaceGLX);
1023 status = end_render_surface(ctx, pSurfaceGLX);
1026 if (status != VA_STATUS_SUCCESS)
1029 return deassociate_surface(ctx, pSurfaceGLX);
1033 vaCopySurfaceGLX_impl_libva(
1034 VADriverContextP ctx,
1036 VASurfaceID surface,
1040 VASurfaceGLXP pSurfaceGLX;
1042 struct OpenGLContextState old_cs;
1044 INIT_SURFACE(pSurfaceGLX, gl_surface);
1046 if (!gl_set_current_context(pSurfaceGLX->gl_context, &old_cs))
1047 return VA_STATUS_ERROR_OPERATION_FAILED;
1049 status = copy_surface(ctx, pSurfaceGLX, surface, flags);
1051 gl_set_current_context(&old_cs, NULL);
1058 /* ========================================================================= */
1059 /* === Private VA/GLX vtable initialization === */
1060 /* ========================================================================= */
1062 // Initialize GLX driver context
1063 VAStatus va_glx_init_context(VADriverContextP ctx)
1065 VADriverContextGLXP glx_ctx = VA_DRIVER_CONTEXT_GLX(ctx);
1066 VADriverVTableGLXP vtable = &glx_ctx->vtable;
1067 int glx_major, glx_minor;
1069 if (glx_ctx->is_initialized)
1070 return VA_STATUS_SUCCESS;
1072 if (ctx->vtable_glx && ctx->vtable_glx->vaCopySurfaceGLX) {
1073 vtable->vaCreateSurfaceGLX = vaCreateSurfaceGLX_impl_driver;
1074 vtable->vaDestroySurfaceGLX = vaDestroySurfaceGLX_impl_driver;
1075 vtable->vaCopySurfaceGLX = vaCopySurfaceGLX_impl_driver;
1078 vtable->vaCreateSurfaceGLX = vaCreateSurfaceGLX_impl_libva;
1079 vtable->vaDestroySurfaceGLX = vaDestroySurfaceGLX_impl_libva;
1080 vtable->vaCopySurfaceGLX = vaCopySurfaceGLX_impl_libva;
1082 if (!glXQueryVersion(ctx->native_dpy, &glx_major, &glx_minor))
1083 return VA_STATUS_ERROR_UNIMPLEMENTED;
1085 if (!check_tfp_extensions(ctx) || !load_tfp_extensions(ctx))
1086 return VA_STATUS_ERROR_UNIMPLEMENTED;
1088 if (!check_fbo_extensions(ctx) || !load_fbo_extensions(ctx))
1089 return VA_STATUS_ERROR_UNIMPLEMENTED;
1092 glx_ctx->is_initialized = 1;
1093 return VA_STATUS_SUCCESS;