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 1076:18c164e8aec4
prev1066:ddffe9d2b332
next1134:f502f3d32f90
author nkeynes
date Mon Aug 03 08:41:11 2009 +1000 (12 years ago)
permissions -rw-r--r--
last change Rearrange frame output slightly.
pvr2_display_frame renamed to pvr2_next_frame and changed to update the
frame data without displaying it.
pvr2_redraw_display renamed to pvr2_draw_frame, called internally after
pvr2_next_frame
Add swap_buffers() method to the display driver
Remove explicit glDrawBuffer() calls where they're referencing the window.
pvr2_draw_frame now gets to decide where to draw.
Add force_vsync flag to force double-buffering (not configurable yet)
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; 
    45 static void video_glx_swap_buffers( void );
    47 /* Prototypes for pbuffer support methods */
    48 static void glx_pbuffer_init( display_driver_t driver );
    49 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
    50 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
    51 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
    52 static void glx_pbuffer_finish_render( render_buffer_t buffer );
    53 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer );
    54 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    55 static void glx_pbuffer_display_blank( uint32_t colour );
    56 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    58 /**
    59  * Test if a specific extension is supported. From opengl.org
    60  * @param extension extension name to check for
    61  * @return TRUE if supported, otherwise FALSE.
    62  */
    63 gboolean isServerGLXExtensionSupported( Display *display, int screen, 
    64                                         const char *extension )
    65 {
    66     const char *extensions = NULL;
    67     const char *start;
    68     char *where, *terminator;
    70     /* Extension names should not have spaces. */
    71     where = strchr(extension, ' ');
    72     if (where || *extension == '\0')
    73         return 0;
    74     extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
    75     start = extensions;
    76     for (;;) {
    77         where = strstr((const char *) start, extension);
    78         if (!where)
    79             break;
    80         terminator = where + strlen(extension);
    81         if (where == start || *(where - 1) == ' ')
    82             if (*terminator == ' ' || *terminator == '\0')
    83                 return TRUE;
    84         start = terminator;
    85     }
    86     return FALSE;
    87 }
    89 gboolean video_glx_init( Display *display, int screen )
    90 {
    91     int major, minor;
    92     int glx_major, glx_minor, glx_error;
    94     if( glx_is_initialized ) {
    95         return TRUE;
    96     }
    98     Bool result = XQueryExtension( display, "GLX", &glx_major, &glx_minor, &glx_error ) &&
    99                   glXQueryVersion( display, &major, &minor );
   100     if( result == False ) {
   101         ERROR( "GLX not supported on display" );
   102         return FALSE;
   103     }
   105     glx_version = (major*100) + minor;
   107 #ifdef APPLE_BUILD
   108     /* fbconfig is broken on at least the 10.5 GLX implementation */
   109     glx_fbconfig_supported = FALSE;
   110 #else
   111     glx_fbconfig_supported = (glx_version >= 103 || 
   112             isServerGLXExtensionSupported(display, screen,
   113                     "GLX_SGIX_fbconfig") );
   114 #endif
   115     glx_pbuffer_supported = (glx_version >= 103 ||
   116             isServerGLXExtensionSupported(display, screen,
   117                     "GLX_SGIX_pbuffer") );
   118 //    glx_fbconfig_supported = FALSE;
   119     if( glx_fbconfig_supported ) {
   120         int nelem;
   121         int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
   122                 GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
   123                 GLX_RENDER_TYPE, GLX_RGBA_BIT, 
   124                 GLX_DEPTH_SIZE, 24, 
   125                 GLX_STENCIL_SIZE, 8, 0 };
   126         GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
   127                 fb_attribs, &nelem );
   129         if( configs == NULL || nelem == 0 ) {
   130             /* Try a 16-bit depth buffer and see if it helps */
   131             fb_attribs[5] = 16;
   132             configs = glXChooseFBConfig( display, screen, fb_attribs, &nelem );
   133             if( nelem > 0 ) {
   134                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   135             }
   137         }
   138         if( configs == NULL || nelem == 0 ) {
   139             /* Still didn't work. Fallback to 1.2 methods */
   140             glx_fbconfig_supported = FALSE;
   141             glx_pbuffer_supported = FALSE;
   142         } else {
   143             glx_fbconfig = configs[0];
   144             glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
   145             XFree(configs);
   146         }
   147     }
   149     if( !glx_fbconfig_supported ) {
   150         int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, 0 };
   151         glx_visual = glXChooseVisual( display, screen, attribs );
   152         if( glx_visual == NULL ) {
   153             /* Try the 16-bit fallback here too */
   154             attribs[2] = 16;
   155             glx_visual = glXChooseVisual( display, screen, attribs );
   156             if( glx_visual != NULL ) {
   157                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   158             }
   159         }
   160     }
   162     if( glx_visual == NULL ) {
   163         return FALSE;
   164     }
   166     glx_is_initialized = TRUE;
   167     return TRUE;
   168 }
   170 gboolean video_glx_init_context( Display *display, Window window )
   171 {
   172     if( glx_fbconfig_supported ) {
   173         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   174                 GLX_RGBA_TYPE, NULL, True );
   175         if( glx_context == NULL ) {
   176             ERROR( "Unable to create a GLX Context.");
   177             return FALSE;
   178         }
   180         if( glXMakeCurrent( display, window,
   181                 glx_context ) == False ) {
   182             ERROR( "Unable to prepare GLX context for drawing" );
   183             glXDestroyContext( display, glx_context );
   184             return FALSE;
   185         }
   186     } else {
   187         glx_context = glXCreateContext( display, glx_visual, None, True );
   188         if( glx_context == NULL ) {
   189             ERROR( "Unable to create a GLX Context.");
   190             return FALSE;
   191         }
   193         if( glXMakeCurrent( display, window, glx_context ) == False ) {
   194             ERROR( "Unable to prepare GLX context for drawing" );
   195             glXDestroyContext( display, glx_context );
   196             return FALSE;
   197         }
   198     }
   200     if( !glXIsDirect(display, glx_context) ) {
   201         WARN( "Not using direct rendering - this is likely to be slow" );
   202     }
   204     video_x11_display = display;
   205     video_x11_window = window;
   207     return TRUE;
   208 }
   210 gboolean video_glx_init_driver( display_driver_t driver )
   211 {
   212     driver->swap_buffers = video_glx_swap_buffers;
   213     if( gl_fbo_is_supported() ) { // First preference
   214         gl_fbo_init(driver);
   215     } else if( glx_pbuffer_supported ) {
   216         glx_pbuffer_init(driver);
   217     } else {
   218         ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
   219         video_glx_shutdown();
   220         return FALSE;
   221     }
   222     return TRUE;
   223 }
   226 void video_glx_shutdown()
   227 {
   228     //    texcache_gl_shutdown();
   229     glx_is_initialized = FALSE;
   230     if( glx_context != NULL ) {
   231         glXDestroyContext( video_x11_display, glx_context );
   232         glx_context = NULL;
   233     }
   234     if( glx_visual != NULL ) {
   235         XFree(glx_visual);
   236         glx_visual = NULL;
   237     }
   238 }
   241 XVisualInfo *video_glx_get_visual()
   242 {
   243     return glx_visual;
   244 }
   247 int video_glx_load_font( const gchar *font_name )
   248 {
   249     int lists;
   250     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   251     if (font == NULL)
   252         return -1;
   254     lists = glGenLists(96);
   255     glXUseXFont(font->fid, 32, 96, lists);
   256     XFreeFont(video_x11_display, font);
   257     return lists;
   258 }
   261 static void video_glx_swap_buffers( void )
   262 {
   263     glXSwapBuffers( video_x11_display, video_x11_window );
   264 }
   266 void video_glx_make_window_current( void )
   267 {
   268     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   269 }
   272 // Pbuffer support
   274 /**
   275  * Construct the initial frame buffers and allocate ids for everything.
   276  * The render handling driver methods are set to the fbo versions.
   277  */
   278 static void glx_pbuffer_init( display_driver_t driver ) 
   279 {
   280     GLint stencil_bits = 0;
   282     /* Retrieve the number of stencil bits */
   283     glGetIntegerv( GL_STENCIL_BITS, &stencil_bits );
   284     driver->capabilities.stencil_bits = stencil_bits;
   286     glGenTextures( 1, &glx_pbuffer_texture );
   287     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   288     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   289     driver->set_render_target = glx_pbuffer_set_render_target;
   290     driver->finish_render = glx_pbuffer_finish_render;
   291     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   292     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   293     driver->display_blank = glx_pbuffer_display_blank;
   294     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   295 }
   297 void glx_pbuffer_shutdown()
   298 {
   299     glDeleteTextures( 1, &glx_pbuffer_texture );
   300 }
   302 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   303 {
   304     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   305             GLX_PRESERVED_CONTENTS, True, 0 };
   306     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   307     if( pb == (GLXPbuffer)NULL ) {
   308         ERROR( "Unable to create pbuffer" );
   309         return NULL;
   310     }
   311     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   312     buffer->width = width;
   313     buffer->height = height;
   314     buffer->buf_id = pb;
   315     buffer->tex_id = tex_id;
   316     return buffer;
   317 }
   319 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   320 {
   321     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   322     buffer->buf_id = 0;
   323     free( buffer );
   324 }
   326 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   327 {
   328     glFinish();
   329     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   330         ERROR( "Make context current (pbuffer) failed!" );
   331     }
   332     /* setup the gl context */
   333     glViewport( 0, 0, buffer->width, buffer->height );
   334     glDrawBuffer(GL_FRONT);
   336     return TRUE;
   337 }
   339 static void glx_pbuffer_finish_render( render_buffer_t buffer )
   340 {
   341     glFinish();
   342     if( buffer->tex_id != 0 ) {
   343         // The pbuffer should already be the current context, but just in case...
   344         glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   345         glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->tex_id );
   346         glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   347     }
   348 }
   351 /**
   352  * Render the texture holding the given buffer to the front window
   353  * buffer.
   354  */
   355 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   356 {
   357     glFinish();
   358     glReadBuffer( GL_FRONT );
   359     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   360     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
   361     glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   362     video_glx_make_window_current();
   363     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   364 }
   366 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   367 {
   368     glFinish();
   369     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   370     GLenum type = colour_formats[frame->colour_format].type;
   371     GLenum format = colour_formats[frame->colour_format].format;
   372     int bpp = colour_formats[frame->colour_format].bpp;
   373     int rowstride = (frame->rowstride / bpp) - frame->width;
   375     gl_reset_state();
   376     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   377     glRasterPos2f(0.375, frame->height-0.375);
   378     glPixelZoom( 1.0, 1.0 );
   379     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   380     glFlush();
   381 }
   383 static void glx_pbuffer_display_blank( uint32_t colour )
   384 {
   385     glFinish();
   386     video_glx_make_window_current();
   387     gl_display_blank( colour );
   388 }
   390 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   391                                                 int rowstride, int format )
   392 {
   393     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   394     glReadBuffer( GL_FRONT );
   395     return gl_read_render_buffer( target, buffer, rowstride, format );
   396 }
.