e181c9a0a82257bc9ca0540ca2e393c916413fdf
[platform/upstream/libva.git] / va / glx / va_glx_impl.c
1 /*
2  * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
3  *
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:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
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.
23  */
24
25 #define _GNU_SOURCE 1
26 #include "va_glx_private.h"
27 #include "va_glx_impl.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <dlfcn.h>
34
35 static void va_glx_error_message(const char *format, ...)
36 {
37     va_list args;
38     va_start(args, format);
39     fprintf(stderr, "libva-glx error: ");
40     vfprintf(stderr, format, args);
41     va_end(args);
42 }
43
44 // X error trap
45 static int x11_error_code = 0;
46 static int (*old_error_handler)(Display *, XErrorEvent *);
47
48 static int error_handler(Display *dpy, XErrorEvent *error)
49 {
50     x11_error_code = error->error_code;
51     return 0;
52 }
53
54 static void x11_trap_errors(void)
55 {
56     x11_error_code    = 0;
57     old_error_handler = XSetErrorHandler(error_handler);
58 }
59
60 static int x11_untrap_errors(void)
61 {
62     XSetErrorHandler(old_error_handler);
63     return x11_error_code;
64 }
65
66 // Returns a string representation of an OpenGL error
67 static const char *gl_get_error_string(GLenum error)
68 {
69     static const struct {
70         GLenum val;
71         const char *str;
72     }
73     gl_errors[] = {
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" },
83 #endif
84         { ~0, NULL }
85     };
86
87     int i;
88     for (i = 0; gl_errors[i].str; i++) {
89         if (gl_errors[i].val == error)
90             return gl_errors[i].str;
91     }
92     return "unknown";
93 }
94
95 static inline int gl_do_check_error(int report)
96 {
97     GLenum error;
98     int is_error = 0;
99     while ((error = glGetError()) != GL_NO_ERROR) {
100         if (report)
101             va_glx_error_message("glError: %s caught\n",
102                                  gl_get_error_string(error));
103         is_error = 1;
104     }
105     return is_error;
106 }
107
108 static inline void gl_purge_errors(void)
109 {
110     gl_do_check_error(0);
111 }
112
113 static inline int gl_check_error(void)
114 {
115     return gl_do_check_error(1);
116 }
117
118 // glGetTexLevelParameteriv() wrapper
119 static int gl_get_texture_param(GLenum param, unsigned int *pval)
120 {
121     GLint val;
122
123     gl_purge_errors();
124     glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, param, &val);
125     if (gl_check_error())
126         return 0;
127     if (pval)
128         *pval = val;
129     return 1;
130 }
131
132 // Returns the OpenGL VTable
133 static inline VAOpenGLVTableP gl_get_vtable(VADriverContextP ctx)
134 {
135     return &VA_DRIVER_CONTEXT_GLX(ctx)->gl_vtable;
136 }
137
138 // Lookup for a GLX function
139 typedef void (*GLFuncPtr)(void);
140 typedef GLFuncPtr (*GLXGetProcAddressProc)(const char *);
141
142 static GLFuncPtr get_proc_address_default(const char *name)
143 {
144     return NULL;
145 }
146
147 static GLXGetProcAddressProc get_proc_address_func(void)
148 {
149     GLXGetProcAddressProc get_proc_func;
150
151     dlerror();
152     get_proc_func = (GLXGetProcAddressProc)
153         dlsym(RTLD_DEFAULT, "glXGetProcAddress");
154     if (!dlerror())
155         return get_proc_func;
156
157     get_proc_func = (GLXGetProcAddressProc)
158         dlsym(RTLD_DEFAULT, "glXGetProcAddressARB");
159     if (!dlerror())
160         return get_proc_func;
161
162     return get_proc_address_default;
163 }
164
165 static inline GLFuncPtr get_proc_address(const char *name)
166 {
167     static GLXGetProcAddressProc get_proc_func = NULL;
168     if (!get_proc_func)
169         get_proc_func = get_proc_address_func();
170     return get_proc_func(name);
171 }
172
173 // Check for GLX extensions (TFP, FBO)
174 static int check_extension(const char *name, const char *ext)
175 {
176     const char *end;
177     int name_len, n;
178
179     if (!name || !ext)
180         return 0;
181
182     end = ext + strlen(ext);
183     name_len = strlen(name);
184     while (ext < end) {
185         n = strcspn(ext, " ");
186         if (n == name_len && strncmp(name, ext, n) == 0)
187             return 1;
188         ext += (n + 1);
189     }
190     return 0;
191 }
192
193 static int check_tfp_extensions(VADriverContextP ctx)
194 {
195     const char *gl_extensions;
196     const char *glx_extensions;
197
198     gl_extensions = (const char *)glGetString(GL_EXTENSIONS);
199     if (!check_extension("GL_ARB_texture_non_power_of_two", gl_extensions))
200         return 0;
201
202     glx_extensions = glXQueryExtensionsString(ctx->native_dpy, ctx->x11_screen);
203     if (!check_extension("GLX_EXT_texture_from_pixmap", glx_extensions))
204         return 0;
205     return 1;
206 }
207
208 static int check_fbo_extensions(VADriverContextP ctx)
209 {
210     const char *gl_extensions;
211
212     gl_extensions = (const char *)glGetString(GL_EXTENSIONS);
213     if (check_extension("GL_ARB_framebuffer_object", gl_extensions))
214         return 1;
215     if (check_extension("GL_EXT_framebuffer_object", gl_extensions))
216         return 1;
217     return 0;
218 }
219
220 // Load GLX extensions
221 static int load_tfp_extensions(VADriverContextP ctx)
222 {
223     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
224
225     pOpenGLVTable->glx_create_pixmap = (PFNGLXCREATEPIXMAPPROC)
226         get_proc_address("glXCreatePixmap");
227     if (!pOpenGLVTable->glx_create_pixmap)
228         return 0;
229     pOpenGLVTable->glx_destroy_pixmap = (PFNGLXDESTROYPIXMAPPROC)
230         get_proc_address("glXDestroyPixmap");
231     if (!pOpenGLVTable->glx_destroy_pixmap)
232         return 0;
233     pOpenGLVTable->glx_bind_tex_image = (PFNGLXBINDTEXIMAGEEXTPROC)
234         get_proc_address("glXBindTexImageEXT");
235     if (!pOpenGLVTable->glx_bind_tex_image)
236         return 0;
237     pOpenGLVTable->glx_release_tex_image = (PFNGLXRELEASETEXIMAGEEXTPROC)
238         get_proc_address("glXReleaseTexImageEXT");
239     if (!pOpenGLVTable->glx_release_tex_image)
240         return 0;
241     return 1;
242 }
243
244 static int load_fbo_extensions(VADriverContextP ctx)
245 {
246     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
247
248     pOpenGLVTable->gl_gen_framebuffers = (PFNGLGENFRAMEBUFFERSEXTPROC)
249         get_proc_address("glGenFramebuffersEXT");
250     if (!pOpenGLVTable->gl_gen_framebuffers)
251         return 0;
252     pOpenGLVTable->gl_delete_framebuffers = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
253         get_proc_address("glDeleteFramebuffersEXT");
254     if (!pOpenGLVTable->gl_delete_framebuffers)
255         return 0;
256     pOpenGLVTable->gl_bind_framebuffer = (PFNGLBINDFRAMEBUFFEREXTPROC)
257         get_proc_address("glBindFramebufferEXT");
258     if (!pOpenGLVTable->gl_bind_framebuffer)
259         return 0;
260     pOpenGLVTable->gl_gen_renderbuffers = (PFNGLGENRENDERBUFFERSEXTPROC)
261         get_proc_address("glGenRenderbuffersEXT");
262     if (!pOpenGLVTable->gl_gen_renderbuffers)
263         return 0;
264     pOpenGLVTable->gl_delete_renderbuffers = (PFNGLDELETERENDERBUFFERSEXTPROC)
265         get_proc_address("glDeleteRenderbuffersEXT");
266     if (!pOpenGLVTable->gl_delete_renderbuffers)
267         return 0;
268     pOpenGLVTable->gl_bind_renderbuffer = (PFNGLBINDRENDERBUFFEREXTPROC)
269         get_proc_address("glBindRenderbufferEXT");
270     if (!pOpenGLVTable->gl_bind_renderbuffer)
271         return 0;
272     pOpenGLVTable->gl_renderbuffer_storage = (PFNGLRENDERBUFFERSTORAGEEXTPROC)
273         get_proc_address("glRenderbufferStorageEXT");
274     if (!pOpenGLVTable->gl_renderbuffer_storage)
275         return 0;
276     pOpenGLVTable->gl_framebuffer_renderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)
277         get_proc_address("glFramebufferRenderbufferEXT");
278     if (!pOpenGLVTable->gl_framebuffer_renderbuffer)
279         return 0;
280     pOpenGLVTable->gl_framebuffer_texture_2d = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
281         get_proc_address("glFramebufferTexture2DEXT");
282     if (!pOpenGLVTable->gl_framebuffer_texture_2d)
283         return 0;
284     pOpenGLVTable->gl_check_framebuffer_status = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
285         get_proc_address("glCheckFramebufferStatusEXT");
286     if (!pOpenGLVTable->gl_check_framebuffer_status)
287         return 0;
288     return 1;
289 }
290
291
292 /* ========================================================================= */
293 /* === VA/GLX helpers                                                    === */
294 /* ========================================================================= */
295
296 // OpenGL context state
297 typedef struct OpenGLContextState *OpenGLContextStateP;
298
299 struct OpenGLContextState {
300     Display     *display;
301     Window       window;
302     GLXContext   context;
303 };
304
305 static void
306 gl_destroy_context(OpenGLContextStateP cs)
307 {
308     if (!cs)
309         return;
310
311     if (cs->display && cs->context) {
312         if (glXGetCurrentContext() == cs->context)
313             glXMakeCurrent(cs->display, None, NULL);
314         glXDestroyContext(cs->display, cs->context);
315         cs->display = NULL;
316         cs->context = NULL;
317     }
318     free(cs);
319 }
320
321 static OpenGLContextStateP
322 gl_create_context(VADriverContextP ctx, OpenGLContextStateP parent)
323 {
324     OpenGLContextStateP cs;
325     GLXFBConfig *fbconfigs = NULL;
326     int fbconfig_id, val, n, n_fbconfigs;
327     Status status;
328
329     static GLint fbconfig_attrs[] = {
330         GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
331         GLX_RENDER_TYPE,   GLX_RGBA_BIT,
332         GLX_DOUBLEBUFFER,  True,
333         GLX_RED_SIZE,      8,
334         GLX_GREEN_SIZE,    8, 
335         GLX_BLUE_SIZE,     8,
336         None
337     };
338
339     cs = malloc(sizeof(*cs));
340     if (!cs)
341         goto error;
342
343     if (parent) {
344         cs->display = parent->display;
345         cs->window  = parent->window;
346     }
347     else {
348         cs->display = ctx->native_dpy;
349         cs->window  = None;
350     }
351     cs->context = NULL;
352
353     if (parent && parent->context) {
354         status = glXQueryContext(
355             parent->display,
356             parent->context,
357             GLX_FBCONFIG_ID, &fbconfig_id
358         );
359         if (status != Success)
360             goto error;
361
362         if (fbconfig_id == GLX_DONT_CARE)
363             goto choose_fbconfig;
364
365         fbconfigs = glXGetFBConfigs(
366             parent->display,
367             DefaultScreen(parent->display),
368             &n_fbconfigs
369         );
370         if (!fbconfigs)
371             goto error;
372
373         /* Find out a GLXFBConfig compatible with the parent context */
374         for (n = 0; n < n_fbconfigs; n++) {
375             status = glXGetFBConfigAttrib(
376                 cs->display,
377                 fbconfigs[n],
378                 GLX_FBCONFIG_ID, &val
379             );
380             if (status == Success && val == fbconfig_id)
381                 break;
382         }
383         if (n == n_fbconfigs)
384             goto error;
385     }
386     else {
387     choose_fbconfig:
388         fbconfigs = glXChooseFBConfig(
389             ctx->native_dpy,
390             ctx->x11_screen,
391             fbconfig_attrs, &n_fbconfigs
392         );
393         if (!fbconfigs)
394             goto error;
395
396         /* Select the first one */
397         n = 0;
398     }
399
400     cs->context = glXCreateNewContext(
401         cs->display,
402         fbconfigs[n],
403         GLX_RGBA_TYPE,
404         parent ? parent->context : NULL,
405         True
406     );
407     if (cs->context)
408         goto end;
409
410 error:
411     gl_destroy_context(cs);
412     cs = NULL;
413 end:
414     if (fbconfigs)
415         XFree(fbconfigs);
416     return cs;
417 }
418
419 static void gl_get_current_context(OpenGLContextStateP cs)
420 {
421     cs->display = glXGetCurrentDisplay();
422     cs->window  = glXGetCurrentDrawable();
423     cs->context = glXGetCurrentContext();
424 }
425
426 static int
427 gl_set_current_context(OpenGLContextStateP new_cs, OpenGLContextStateP old_cs)
428 {
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;
434
435     if (old_cs) {
436         if (old_cs == new_cs)
437             return 1;
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)
442             return 1;
443     }
444     return glXMakeCurrent(new_cs->display, new_cs->window, new_cs->context);
445 }
446
447 /** Unique VASurfaceGLX identifier */
448 #define VA_SURFACE_GLX_MAGIC VA_FOURCC('V','A','G','L')
449
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
455     unsigned int        width;
456     unsigned int        height;
457     OpenGLContextStateP gl_context;
458     int                 is_bound;
459     Pixmap              pixmap;
460     GLuint              pix_texture;
461     GLXPixmap           glx_pixmap;
462     GLuint              fbo;
463 };
464
465 // Create Pixmaps for GLX texture-from-pixmap extension
466 static int create_tfp_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
467 {
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;
474     Window                root_window;
475     XWindowAttributes     wattr;
476     int                  *attrib;
477     int                   n_fbconfig_attrs;
478
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)
482         return 0;
483     pixmap = XCreatePixmap(
484         ctx->native_dpy,
485         root_window,
486         width,
487         height,
488         wattr.depth
489     );
490     if (!pixmap)
491         return 0;
492     pSurfaceGLX->pixmap = pixmap;
493
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,
500         GLX_RED_SIZE,           8,
501         GLX_GREEN_SIZE,         8,
502         GLX_BLUE_SIZE,          8,
503         GL_NONE,
504     };
505     for (attrib = fbconfig_attrs; *attrib != GL_NONE; attrib += 2)
506         ;
507     if (wattr.depth == 32) {
508     *attrib++ = GLX_ALPHA_SIZE;                 *attrib++ = 8;
509     *attrib++ = GLX_BIND_TO_TEXTURE_RGBA_EXT;   *attrib++ = GL_TRUE;
510     }
511     else {
512     *attrib++ = GLX_BIND_TO_TEXTURE_RGB_EXT;    *attrib++ = GL_TRUE;
513     }
514     *attrib++ = GL_NONE;
515
516     fbconfig = glXChooseFBConfig(
517         ctx->native_dpy,
518         ctx->x11_screen,
519         fbconfig_attrs,
520         &n_fbconfig_attrs
521     );
522     if (!fbconfig)
523         return 0;
524
525     int pixmap_attrs[10] = {
526         GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
527         GLX_MIPMAP_TEXTURE_EXT, GL_FALSE,
528         GL_NONE,
529     };
530     for (attrib = pixmap_attrs; *attrib != GL_NONE; attrib += 2)
531         ;
532     *attrib++ = GLX_TEXTURE_FORMAT_EXT;
533     if (wattr.depth == 32)
534     *attrib++ = GLX_TEXTURE_FORMAT_RGBA_EXT;
535     else
536     *attrib++ = GLX_TEXTURE_FORMAT_RGB_EXT;
537     *attrib++ = GL_NONE;
538
539     x11_trap_errors();
540     glx_pixmap = pOpenGLVTable->glx_create_pixmap(
541         ctx->native_dpy,
542         fbconfig[0],
543         pixmap,
544         pixmap_attrs
545     );
546     free(fbconfig);
547     if (x11_untrap_errors() != 0)
548         return 0;
549     pSurfaceGLX->glx_pixmap = glx_pixmap;
550
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);
555     return 1;
556 }
557
558 // Destroy Pixmaps used for TFP
559 static void destroy_tfp_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
560 {
561     VAOpenGLVTableP const pOpenGLVTable = gl_get_vtable(ctx);
562
563     if (pSurfaceGLX->pix_texture) {
564         glDeleteTextures(1, &pSurfaceGLX->pix_texture);
565         pSurfaceGLX->pix_texture = 0;
566     }
567
568     if (pSurfaceGLX->glx_pixmap) {
569         pOpenGLVTable->glx_destroy_pixmap(ctx->native_dpy, pSurfaceGLX->glx_pixmap);
570         pSurfaceGLX->glx_pixmap = None;
571     }
572
573     if (pSurfaceGLX->pixmap) {
574         XFreePixmap(ctx->native_dpy, pSurfaceGLX->pixmap);
575         pSurfaceGLX->pixmap = None;
576     }
577 }
578
579 // Bind GLX Pixmap to texture
580 static int bind_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
581 {
582     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
583
584     if (pSurfaceGLX->is_bound)
585         return 1;
586
587     glBindTexture(GL_TEXTURE_2D, pSurfaceGLX->pix_texture);
588
589     x11_trap_errors();
590     pOpenGLVTable->glx_bind_tex_image(
591         ctx->native_dpy,
592         pSurfaceGLX->glx_pixmap,
593         GLX_FRONT_LEFT_EXT,
594         NULL
595     );
596     XSync(ctx->native_dpy, False);
597     if (x11_untrap_errors() != 0) {
598         va_glx_error_message("failed to bind pixmap\n");
599         return 0;
600     }
601
602     pSurfaceGLX->is_bound = 1;
603     return 1;
604 }
605
606 // Release GLX Pixmap from texture
607 static int unbind_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
608 {
609     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
610
611     if (!pSurfaceGLX->is_bound)
612         return 1;
613
614     x11_trap_errors();
615     pOpenGLVTable->glx_release_tex_image(
616         ctx->native_dpy,
617         pSurfaceGLX->glx_pixmap,
618         GLX_FRONT_LEFT_EXT
619     );
620     XSync(ctx->native_dpy, False);
621     if (x11_untrap_errors() != 0) {
622         va_glx_error_message("failed to release pixmap\n");
623         return 0;
624     }
625
626     glBindTexture(GL_TEXTURE_2D, 0);
627
628     pSurfaceGLX->is_bound = 0;
629     return 1;
630 }
631
632 // Render GLX Pixmap to texture
633 static void render_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
634 {
635     const unsigned int w = pSurfaceGLX->width;
636     const unsigned int h = pSurfaceGLX->height;
637
638     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
639     glBegin(GL_QUADS);
640     {
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);
645     }
646     glEnd();
647 }
648
649 // Create offscreen surface
650 static int create_fbo_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
651 {
652     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
653     GLuint fbo;
654     GLenum status;
655
656     pOpenGLVTable->gl_gen_framebuffers(1, &fbo);
657     pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, fbo);
658     pOpenGLVTable->gl_framebuffer_texture_2d(
659         GL_FRAMEBUFFER_EXT,
660         GL_COLOR_ATTACHMENT0_EXT,
661         GL_TEXTURE_2D,
662         pSurfaceGLX->texture,
663         0
664     );
665
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)
669         return 0;
670
671     pSurfaceGLX->fbo = fbo;
672     return 1;
673 }
674
675 // Destroy offscreen surface
676 static void destroy_fbo_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
677 {
678     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
679
680     if (pSurfaceGLX->fbo) {
681         pOpenGLVTable->gl_delete_framebuffers(1, &pSurfaceGLX->fbo);
682         pSurfaceGLX->fbo = 0;
683     }
684 }
685
686 // Setup matrices to match the FBO texture dimensions
687 static void fbo_enter(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
688 {
689     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
690     const unsigned int width  = pSurfaceGLX->width;
691     const unsigned int height = pSurfaceGLX->height;
692
693     pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, pSurfaceGLX->fbo);
694     glPushAttrib(GL_VIEWPORT_BIT);
695     glMatrixMode(GL_PROJECTION);
696     glPushMatrix();
697     glLoadIdentity();
698     glMatrixMode(GL_MODELVIEW);
699     glPushMatrix();
700     glLoadIdentity();
701     glViewport(0, 0, width, height);
702     glTranslatef(-1.0f, -1.0f, 0.0f);
703     glScalef(2.0f / width, 2.0f / height, 1.0f);
704 }
705
706 // Restore original OpenGL matrices
707 static void fbo_leave(VADriverContextP ctx)
708 {
709     VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
710
711     glPopAttrib();
712     glMatrixMode(GL_PROJECTION);
713     glPopMatrix();
714     glMatrixMode(GL_MODELVIEW);
715     glPopMatrix();
716     pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, 0);
717 }
718
719 // Check internal texture format is supported
720 static int is_supported_internal_format(GLenum format)
721 {
722     /* XXX: we don't support other textures than RGBA */
723     switch (format) {
724     case 4:
725     case GL_RGBA:
726     case GL_RGBA8:
727         return 1;
728     }
729     return 0;
730 }
731
732 // Destroy VA/GLX surface
733 static void
734 destroy_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
735 {
736     unbind_pixmap(ctx, pSurfaceGLX);
737     destroy_fbo_surface(ctx, pSurfaceGLX);
738     destroy_tfp_surface(ctx, pSurfaceGLX);
739     free(pSurfaceGLX);
740 }
741
742 // Create VA/GLX surface
743 static VASurfaceGLXP
744 create_surface(VADriverContextP ctx, GLenum target, GLuint texture)
745 {
746     VASurfaceGLXP pSurfaceGLX = NULL;
747     unsigned int internal_format, border_width, width, height;
748     int is_error = 1;
749
750     pSurfaceGLX = malloc(sizeof(*pSurfaceGLX));
751     if (!pSurfaceGLX)
752         goto end;
753
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;
764
765     glEnable(target);
766     glBindTexture(target, texture);
767     if (!gl_get_texture_param(GL_TEXTURE_INTERNAL_FORMAT, &internal_format))
768         goto end;
769     if (!is_supported_internal_format(internal_format))
770         goto end;
771
772     /* Check texture dimensions */
773     if (!gl_get_texture_param(GL_TEXTURE_BORDER, &border_width))
774         goto end;
775     if (!gl_get_texture_param(GL_TEXTURE_WIDTH, &width))
776         goto end;
777     if (!gl_get_texture_param(GL_TEXTURE_HEIGHT, &height))
778         goto end;
779
780     width  -= 2 * border_width;
781     height -= 2 * border_width;
782     if (width == 0 || height == 0)
783         goto end;
784
785     pSurfaceGLX->width  = width;
786     pSurfaceGLX->height = height;
787
788     /* Create TFP objects */
789     if (!create_tfp_surface(ctx, pSurfaceGLX))
790         goto end;
791
792     /* Create FBO objects */
793     if (!create_fbo_surface(ctx, pSurfaceGLX))
794         goto end;
795
796     is_error = 0;
797 end:
798     if (is_error && pSurfaceGLX) {
799         destroy_surface(ctx, pSurfaceGLX);
800         pSurfaceGLX = NULL;
801     }
802     return pSurfaceGLX;
803 }
804
805
806 /* ========================================================================= */
807 /* === VA/GLX implementation from the driver (fordward calls)            === */
808 /* ========================================================================= */
809
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;       \
814                                                         \
815         VAStatus status = vtable->va##func##GLX args;   \
816         if (status != VA_STATUS_SUCCESS)                \
817             return status;                              \
818     } while (0)
819
820 static VAStatus
821 vaCreateSurfaceGLX_impl_driver(
822     VADriverContextP    ctx,
823     GLenum              target,
824     GLuint              texture,
825     void              **gl_surface
826 )
827 {
828     INVOKE(ctx, CreateSurface, (ctx, target, texture, gl_surface));
829     return VA_STATUS_SUCCESS;
830 }
831
832 static VAStatus
833 vaDestroySurfaceGLX_impl_driver(VADriverContextP ctx, void *gl_surface)
834 {
835     INVOKE(ctx, DestroySurface, (ctx, gl_surface));
836     return VA_STATUS_SUCCESS;
837 }
838
839 static VAStatus
840 vaCopySurfaceGLX_impl_driver(
841     VADriverContextP    ctx,
842     void               *gl_surface,
843     VASurfaceID         surface,
844     unsigned int        flags
845 )
846 {
847     INVOKE(ctx, CopySurface, (ctx, gl_surface, surface, flags));
848     return VA_STATUS_SUCCESS;
849 }
850
851 #undef INVOKE
852
853
854 /* ========================================================================= */
855 /* === VA/GLX implementation from libVA (generic and suboptimal path)    === */
856 /* ========================================================================= */
857
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;     \
862     } while (0)
863
864 // Check VASurfaceGLX is valid
865 static inline int check_surface(VASurfaceGLXP pSurfaceGLX)
866 {
867     return pSurfaceGLX && pSurfaceGLX->magic == VA_SURFACE_GLX_MAGIC;
868 }
869
870 static VAStatus
871 vaCreateSurfaceGLX_impl_libva(
872     VADriverContextP    ctx,
873     GLenum              target,
874     GLuint              texture,
875     void              **gl_surface
876 )
877 {
878     VASurfaceGLXP pSurfaceGLX;
879     struct OpenGLContextState old_cs, *new_cs;
880
881     gl_get_current_context(&old_cs);
882     new_cs = gl_create_context(ctx, &old_cs);
883     if (!new_cs)
884         goto error;
885     if (!gl_set_current_context(new_cs, NULL))
886         goto error;
887
888     pSurfaceGLX = create_surface(ctx, target, texture);
889     if (!pSurfaceGLX)
890         goto error;
891
892     pSurfaceGLX->gl_context = new_cs;
893     *gl_surface = pSurfaceGLX;
894
895     gl_set_current_context(&old_cs, NULL);
896     return VA_STATUS_SUCCESS;
897
898 error:
899     if (new_cs)
900         gl_destroy_context(new_cs);
901
902     return VA_STATUS_ERROR_ALLOCATION_FAILED;    
903 }
904
905 static VAStatus
906 vaDestroySurfaceGLX_impl_libva(VADriverContextP ctx, void *gl_surface)
907 {
908     VASurfaceGLXP pSurfaceGLX;
909     struct OpenGLContextState old_cs, *new_cs;
910
911     INIT_SURFACE(pSurfaceGLX, gl_surface);
912
913     new_cs = pSurfaceGLX->gl_context;
914     if (!gl_set_current_context(new_cs, &old_cs))
915         return VA_STATUS_ERROR_OPERATION_FAILED;
916
917     destroy_surface(ctx, pSurfaceGLX);
918
919     gl_destroy_context(new_cs);
920     gl_set_current_context(&old_cs, NULL);
921     return VA_STATUS_SUCCESS;
922 }
923
924 static inline VAStatus
925 deassociate_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
926 {
927     if (!unbind_pixmap(ctx, pSurfaceGLX))
928         return VA_STATUS_ERROR_OPERATION_FAILED;
929
930     pSurfaceGLX->surface = VA_INVALID_SURFACE;
931     return VA_STATUS_SUCCESS;
932 }
933
934 static VAStatus
935 associate_surface(
936     VADriverContextP    ctx,
937     VASurfaceGLXP       pSurfaceGLX,
938     VASurfaceID         surface,
939     unsigned int        flags
940 )
941 {
942     VAStatus status;
943
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)
948         return status;
949
950     x11_trap_errors();
951     status = ctx->vtable->vaPutSurface(
952         ctx,
953         surface,
954         (void *)pSurfaceGLX->pixmap,
955         0, 0, pSurfaceGLX->width, pSurfaceGLX->height,
956         0, 0, pSurfaceGLX->width, pSurfaceGLX->height,
957         NULL, 0,
958         flags
959     );
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)
964         return status;
965
966     pSurfaceGLX->surface = surface;
967     return VA_STATUS_SUCCESS;
968 }
969
970 static inline VAStatus
971 sync_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
972 {
973     if (pSurfaceGLX->surface == VA_INVALID_SURFACE)
974         return VA_STATUS_ERROR_INVALID_SURFACE;
975
976     return ctx->vtable->vaSyncSurface(ctx, pSurfaceGLX->surface);
977 }
978
979 static inline VAStatus
980 begin_render_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
981 {
982     VAStatus status;
983
984     status = sync_surface(ctx, pSurfaceGLX);
985     if (status != VA_STATUS_SUCCESS)
986         return status;
987
988     if (!bind_pixmap(ctx, pSurfaceGLX))
989         return VA_STATUS_ERROR_OPERATION_FAILED;
990
991     return VA_STATUS_SUCCESS;
992 }
993
994 static inline VAStatus
995 end_render_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
996 {
997     if (!unbind_pixmap(ctx, pSurfaceGLX))
998         return VA_STATUS_ERROR_OPERATION_FAILED;
999
1000     return VA_STATUS_SUCCESS;
1001 }
1002
1003 static VAStatus
1004 copy_surface(
1005     VADriverContextP    ctx,
1006     VASurfaceGLXP       pSurfaceGLX,
1007     VASurfaceID         surface,
1008     unsigned int        flags
1009 )
1010 {
1011     VAStatus status;
1012
1013     /* Associate VA surface */
1014     status = associate_surface(ctx, pSurfaceGLX, surface, flags);
1015     if (status != VA_STATUS_SUCCESS)
1016         return status;
1017
1018     /* Render to FBO */
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);
1024     }
1025     fbo_leave(ctx);
1026     if (status != VA_STATUS_SUCCESS)
1027         return status;
1028
1029     return deassociate_surface(ctx, pSurfaceGLX);
1030 }
1031
1032 static VAStatus
1033 vaCopySurfaceGLX_impl_libva(
1034     VADriverContextP    ctx,
1035     void               *gl_surface,
1036     VASurfaceID         surface,
1037     unsigned int        flags
1038 )
1039 {
1040     VASurfaceGLXP pSurfaceGLX;
1041     VAStatus status;
1042     struct OpenGLContextState old_cs;
1043
1044     INIT_SURFACE(pSurfaceGLX, gl_surface);
1045
1046     if (!gl_set_current_context(pSurfaceGLX->gl_context, &old_cs))
1047         return VA_STATUS_ERROR_OPERATION_FAILED;
1048
1049     status = copy_surface(ctx, pSurfaceGLX, surface, flags);
1050
1051     gl_set_current_context(&old_cs, NULL);
1052     return status;
1053 }
1054
1055 #undef INIT_SURFACE
1056
1057
1058 /* ========================================================================= */
1059 /* === Private VA/GLX vtable initialization                              === */
1060 /* ========================================================================= */
1061
1062 // Initialize GLX driver context
1063 VAStatus va_glx_init_context(VADriverContextP ctx)
1064 {
1065     VADriverContextGLXP glx_ctx = VA_DRIVER_CONTEXT_GLX(ctx);
1066     VADriverVTableGLXP  vtable  = &glx_ctx->vtable;
1067     int glx_major, glx_minor;
1068
1069     if (glx_ctx->is_initialized)
1070         return VA_STATUS_SUCCESS;
1071
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;
1076     }
1077     else {
1078         vtable->vaCreateSurfaceGLX      = vaCreateSurfaceGLX_impl_libva;
1079         vtable->vaDestroySurfaceGLX     = vaDestroySurfaceGLX_impl_libva;
1080         vtable->vaCopySurfaceGLX        = vaCopySurfaceGLX_impl_libva;
1081
1082         if (!glXQueryVersion(ctx->native_dpy, &glx_major, &glx_minor))
1083             return VA_STATUS_ERROR_UNIMPLEMENTED;
1084
1085         if (!check_tfp_extensions(ctx) || !load_tfp_extensions(ctx))
1086             return VA_STATUS_ERROR_UNIMPLEMENTED;
1087
1088         if (!check_fbo_extensions(ctx) || !load_fbo_extensions(ctx))
1089             return VA_STATUS_ERROR_UNIMPLEMENTED;
1090     }
1091
1092     glx_ctx->is_initialized = 1;
1093     return VA_STATUS_SUCCESS;
1094 }