Search
lxdream.org :: lxdream/src/drivers/video_glx.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_glx.c
changeset 1258:f8a9c0fd2abb
prev1256:a9d29fe74bf3
author nkeynes
date Mon Mar 05 11:41:03 2012 +1000 (9 years ago)
permissions -rw-r--r--
last change Small cleanups:
Refactor the post-windowing setup into gl_init_driver() in video_gl.c
Move gl_sl.c into src/drivers and tidy up a bit.
Fix OS X compiling plugins with -mdynamic-no-pic
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Shared functions for all X11-based display drivers.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <stdlib.h>
    20 #include <string.h>
    21 #include "display.h"
    22 #include <X11/Xlib.h>
    23 #include <GL/glx.h>
    24 #include "pvr2/pvr2.h"
    25 #include "pvr2/glutil.h"
    26 #include "drivers/video_glx.h"
    27 #include "drivers/video_gl.h"
    29 /**
    30  * General X11 parameters. The front-end driver is expected to set this up
    31  * by calling video_glx_init after initializing itself.
    32  */
    33 Display *video_x11_display = NULL;
    34 Window video_x11_window = 0;
    36 static int glx_version = 100;
    37 static XVisualInfo *glx_visual = NULL;
    38 static GLXFBConfig glx_fbconfig;
    39 static GLXContext glx_context = NULL;
    40 static gboolean glx_is_initialized = FALSE;
    41 static gboolean glx_fbconfig_supported = FALSE;
    42 static gboolean glx_pbuffer_supported = FALSE;
    43 static GLuint glx_pbuffer_texture = 0; 
    44 static int glx_depth_bits = 0;
    46 static void video_glx_swap_buffers( void );
    47 static void video_glx_print_info( FILE *out );
    49 /* Prototypes for pbuffer support methods */
    50 static void glx_pbuffer_init( display_driver_t driver );
    51 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
    52 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
    53 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
    54 static void glx_pbuffer_finish_render( render_buffer_t buffer );
    55 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer );
    56 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    57 static void glx_pbuffer_display_blank( uint32_t colour );
    58 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    60 /**
    61  * Test if a specific extension is supported. From opengl.org
    62  * @param extension extension name to check for
    63  * @return TRUE if supported, otherwise FALSE.
    64  */
    65 gboolean isServerGLXExtensionSupported( Display *display, int screen, 
    66                                         const char *extension )
    67 {
    68     const char *extensions = NULL;
    69     const char *start;
    70     char *where, *terminator;
    72     /* Extension names should not have spaces. */
    73     where = strchr(extension, ' ');
    74     if (where || *extension == '\0')
    75         return 0;
    76     extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
    77     start = extensions;
    78     for (;;) {
    79         where = strstr((const char *) start, extension);
    80         if (!where)
    81             break;
    82         terminator = where + strlen(extension);
    83         if (where == start || *(where - 1) == ' ')
    84             if (*terminator == ' ' || *terminator == '\0')
    85                 return TRUE;
    86         start = terminator;
    87     }
    88     return FALSE;
    89 }
    91 gboolean video_glx_init( Display *display, int screen )
    92 {
    93     int major, minor;
    94     int glx_major, glx_minor, glx_error;
    96     if( glx_is_initialized ) {
    97         return TRUE;
    98     }
   100     Bool result = XQueryExtension( display, "GLX", &glx_major, &glx_minor, &glx_error ) &&
   101                   glXQueryVersion( display, &major, &minor );
   102     if( result == False ) {
   103         ERROR( "GLX not supported on display" );
   104         return FALSE;
   105     }
   107     glx_version = (major*100) + minor;
   109 #ifdef APPLE_BUILD
   110     /* fbconfig is broken on at least the 10.5 GLX implementation */
   111     glx_fbconfig_supported = FALSE;
   112 #else
   113     glx_fbconfig_supported = (glx_version >= 103 || 
   114             isServerGLXExtensionSupported(display, screen,
   115                     "GLX_SGIX_fbconfig") );
   116 #endif
   117     glx_pbuffer_supported = (glx_version >= 103 ||
   118             isServerGLXExtensionSupported(display, screen,
   119                     "GLX_SGIX_pbuffer") );
   120 //    glx_fbconfig_supported = FALSE;
   121     if( glx_fbconfig_supported ) {
   122         int nelem;
   123         glx_depth_bits = 24;
   124         int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
   125                 GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
   126                 GLX_RENDER_TYPE, GLX_RGBA_BIT, 
   127                 GLX_DEPTH_SIZE, 24, 
   128                 GLX_STENCIL_SIZE, 8, 0 };
   129         GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
   130                 fb_attribs, &nelem );
   132         if( configs == NULL || nelem == 0 ) {
   133             /* Try a 16-bit depth buffer and see if it helps */
   134             fb_attribs[5] = 16;
   135             glx_depth_bits = 16;
   136             configs = glXChooseFBConfig( display, screen, fb_attribs, &nelem );
   137             if( nelem > 0 ) {
   138                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   139             }
   141         }
   142         if( configs == NULL || nelem == 0 ) {
   143             /* Still didn't work. Fallback to 1.2 methods */
   144             glx_fbconfig_supported = FALSE;
   145             glx_pbuffer_supported = FALSE;
   146         } else {
   147             glx_fbconfig = configs[0];
   148             glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
   149             XFree(configs);
   150         }
   151     }
   153     if( !glx_fbconfig_supported ) {
   154         glx_depth_bits = 24;
   155         int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, 0 };
   156         glx_visual = glXChooseVisual( display, screen, attribs );
   157         if( glx_visual == NULL ) {
   158             /* Try the 16-bit fallback here too */
   159             glx_depth_bits = 16;
   160             attribs[2] = 16;
   161             glx_visual = glXChooseVisual( display, screen, attribs );
   162             if( glx_visual != NULL ) {
   163                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   164             }
   165         }
   166     }
   168     if( glx_visual == NULL ) {
   169         return FALSE;
   170     }
   172     glx_is_initialized = TRUE;
   173     return TRUE;
   174 }
   176 static void video_glx_print_info( FILE *out )
   177 {
   178     XWindowAttributes attr;
   180     if( !XGetWindowAttributes(video_x11_display, video_x11_window, &attr) )
   181         return; /* Failed */
   182     int screen = XScreenNumberOfScreen(attr.screen);
   184     fprintf( out, "GLX Server: %s %s\n", glXQueryServerString(video_x11_display, screen, GLX_VENDOR),
   185             glXQueryServerString(video_x11_display, screen, GLX_VERSION) );
   186     fprintf( out, "GLX Client: %s %s\n", glXGetClientString(video_x11_display, GLX_VENDOR),
   187             glXGetClientString(video_x11_display, GLX_VERSION) );
   188     fprintf( out, "GLX Server Extensions:\n" );
   189     fprint_extensions( out, glXQueryServerString(video_x11_display, screen, GLX_EXTENSIONS) );
   190     fprintf( out, "GLX Client Extensions:\n" );
   191     fprint_extensions( out, glXGetClientString(video_x11_display, GLX_EXTENSIONS) );
   192     fprintf( out, "GLX Extensions:\n" );
   193     fprint_extensions( out, glXQueryExtensionsString(video_x11_display, screen) );
   195 }
   197 gboolean video_glx_init_context( Display *display, Window window )
   198 {
   199     if( glx_fbconfig_supported ) {
   200         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   201                 GLX_RGBA_TYPE, NULL, True );
   202         if( glx_context == NULL ) {
   203             ERROR( "Unable to create a GLX Context.");
   204             return FALSE;
   205         }
   207         if( glXMakeCurrent( display, window,
   208                 glx_context ) == False ) {
   209             ERROR( "Unable to prepare GLX context for drawing" );
   210             glXDestroyContext( display, glx_context );
   211             return FALSE;
   212         }
   213     } else {
   214         glx_context = glXCreateContext( display, glx_visual, None, True );
   215         if( glx_context == NULL ) {
   216             ERROR( "Unable to create a GLX Context.");
   217             return FALSE;
   218         }
   220         if( glXMakeCurrent( display, window, glx_context ) == False ) {
   221             ERROR( "Unable to prepare GLX context for drawing" );
   222             glXDestroyContext( display, glx_context );
   223             return FALSE;
   224         }
   225     }
   227     if( !glXIsDirect(display, glx_context) ) {
   228         WARN( "Not using direct rendering - this is likely to be slow" );
   229     }
   231     video_x11_display = display;
   232     video_x11_window = window;
   234     return TRUE;
   235 }
   237 gboolean video_glx_init_driver( display_driver_t driver )
   238 {
   239     driver->swap_buffers = video_glx_swap_buffers;
   240     driver->print_info = video_glx_print_info;
   241     driver->capabilities.has_gl = TRUE;
   242     driver->capabilities.depth_bits = glx_depth_bits;
   243     if( !gl_init_driver(driver, !glx_pbuffer_supported) ) {
   244         video_glx_shutdown();
   245         return FALSE;
   246     }
   247     if( driver->create_render_buffer == NULL ) {
   248         /* If we get here, pbuffers are supported and FBO didn't work */
   249         glx_pbuffer_init(driver);
   250     }
   251     return TRUE;
   252 }
   255 void video_glx_shutdown()
   256 {
   257     //    texcache_gl_shutdown();
   258     glx_is_initialized = FALSE;
   259     if( glx_context != NULL ) {
   260         glXDestroyContext( video_x11_display, glx_context );
   261         glx_context = NULL;
   262     }
   263     if( glx_visual != NULL ) {
   264         XFree(glx_visual);
   265         glx_visual = NULL;
   266     }
   267 }
   270 XVisualInfo *video_glx_get_visual()
   271 {
   272     return glx_visual;
   273 }
   276 int video_glx_load_font( const gchar *font_name )
   277 {
   278     int lists;
   279     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   280     if (font == NULL)
   281         return -1;
   283     lists = glGenLists(96);
   284     glXUseXFont(font->fid, 32, 96, lists);
   285     XFreeFont(video_x11_display, font);
   286     return lists;
   287 }
   290 static void video_glx_swap_buffers( void )
   291 {
   292     glXSwapBuffers( video_x11_display, video_x11_window );
   293 }
   295 void video_glx_make_window_current( void )
   296 {
   297     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   298 }
   301 // Pbuffer support
   303 /**
   304  * Construct the initial frame buffers and allocate ids for everything.
   305  * The render handling driver methods are set to the fbo versions.
   306  */
   307 static void glx_pbuffer_init( display_driver_t driver ) 
   308 {
   309     GLint stencil_bits = 0;
   311     /* Retrieve the number of stencil bits */
   312     glGetIntegerv( GL_STENCIL_BITS, &stencil_bits );
   313     driver->capabilities.stencil_bits = stencil_bits;
   315     glGenTextures( 1, &glx_pbuffer_texture );
   316     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   317     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   318     driver->set_render_target = glx_pbuffer_set_render_target;
   319     driver->finish_render = glx_pbuffer_finish_render;
   320     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   321     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   322     driver->display_blank = glx_pbuffer_display_blank;
   323     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   324 }
   326 void glx_pbuffer_shutdown()
   327 {
   328     glDeleteTextures( 1, &glx_pbuffer_texture );
   329 }
   331 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   332 {
   333     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   334             GLX_PRESERVED_CONTENTS, True, 0 };
   335     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   336     if( pb == (GLXPbuffer)NULL ) {
   337         ERROR( "Unable to create pbuffer" );
   338         return NULL;
   339     }
   340     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   341     buffer->width = width;
   342     buffer->height = height;
   343     buffer->buf_id = pb;
   344     buffer->tex_id = tex_id;
   345     return buffer;
   346 }
   348 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   349 {
   350     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   351     buffer->buf_id = 0;
   352     free( buffer );
   353 }
   355 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   356 {
   357     glFinish();
   358     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   359         ERROR( "Make context current (pbuffer) failed!" );
   360     }
   361     /* setup the gl context */
   362     glViewport( 0, 0, buffer->width, buffer->height );
   363     glDrawBuffer(GL_FRONT);
   365     return TRUE;
   366 }
   368 static void glx_pbuffer_finish_render( render_buffer_t buffer )
   369 {
   370     glFinish();
   371     if( buffer->tex_id != 0 ) {
   372         // The pbuffer should already be the current context, but just in case...
   373         glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   374         glBindTexture( GL_TEXTURE_2D, buffer->tex_id );
   375         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   376     }
   377 }
   380 /**
   381  * Render the texture holding the given buffer to the front window
   382  * buffer.
   383  */
   384 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   385 {
   386     glFinish();
   387     glReadBuffer( GL_FRONT );
   388     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   389     glBindTexture( GL_TEXTURE_2D, glx_pbuffer_texture );
   390     glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   391     video_glx_make_window_current();
   392     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   393 }
   395 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   396 {
   397     glFinish();
   398     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   399     GLenum type = colour_formats[frame->colour_format].type;
   400     GLenum format = colour_formats[frame->colour_format].format;
   401     int bpp = colour_formats[frame->colour_format].bpp;
   402     int rowstride = (frame->rowstride / bpp) - frame->width;
   404     gl_framebuffer_setup();
   405     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   406     glRasterPos2f(0.375, frame->height-0.375);
   407     glPixelZoom( 1.0, 1.0 );
   408     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   409     glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
   410     glFlush();
   411     gl_framebuffer_cleanup();
   412 }
   414 static void glx_pbuffer_display_blank( uint32_t colour )
   415 {
   416     glFinish();
   417     video_glx_make_window_current();
   418     gl_display_blank( colour );
   419 }
   421 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   422                                                 int rowstride, int format )
   423 {
   424     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   425     glReadBuffer( GL_FRONT );
   426     return gl_read_render_buffer( target, buffer, rowstride, format );
   427 }
.