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 1001:cbb01e3114ea
prev982:2d6b3f29f878
next1065:bc1cc0c54917
author nkeynes
date Wed Mar 25 11:57:05 2009 +0000 (10 years ago)
permissions -rw-r--r--
last change Change glXMakeContextCurrent call to glXMakeCurrent - works around crash bug
in mesa: http://www.mail-archive.com/mesa-commit@lists.freedesktop.org/msg04338.html
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 /* Prototypes for pbuffer support methods */
    46 static void glx_pbuffer_init( display_driver_t driver );
    47 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
    48 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
    49 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
    50 static void glx_pbuffer_finish_render( render_buffer_t buffer );
    51 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer );
    52 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    53 static void glx_pbuffer_display_blank( uint32_t colour );
    54 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    56 /**
    57  * Test if a specific extension is supported. From opengl.org
    58  * @param extension extension name to check for
    59  * @return TRUE if supported, otherwise FALSE.
    60  */
    61 gboolean isServerGLXExtensionSupported( Display *display, int screen, 
    62                                         const char *extension )
    63 {
    64     const char *extensions = NULL;
    65     const char *start;
    66     char *where, *terminator;
    68     /* Extension names should not have spaces. */
    69     where = strchr(extension, ' ');
    70     if (where || *extension == '\0')
    71         return 0;
    72     extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
    73     start = extensions;
    74     for (;;) {
    75         where = strstr((const char *) start, extension);
    76         if (!where)
    77             break;
    78         terminator = where + strlen(extension);
    79         if (where == start || *(where - 1) == ' ')
    80             if (*terminator == ' ' || *terminator == '\0')
    81                 return TRUE;
    82         start = terminator;
    83     }
    84     return FALSE;
    85 }
    87 gboolean video_glx_init( Display *display, int screen )
    88 {
    89     int major, minor;
    90     int glx_major, glx_minor, glx_error;
    92     if( glx_is_initialized ) {
    93         return TRUE;
    94     }
    96     Bool result = XQueryExtension( display, "GLX", &glx_major, &glx_minor, &glx_error ) &&
    97                   glXQueryVersion( display, &major, &minor );
    98     if( result == False ) {
    99         ERROR( "GLX not supported on display" );
   100         return FALSE;
   101     }
   103     glx_version = (major*100) + minor;
   105 #ifdef APPLE_BUILD
   106     /* fbconfig is broken on at least the 10.5 GLX implementation */
   107     glx_fbconfig_supported = FALSE;
   108 #else
   109     glx_fbconfig_supported = (glx_version >= 103 || 
   110             isServerGLXExtensionSupported(display, screen,
   111                     "GLX_SGIX_fbconfig") );
   112 #endif
   113     glx_pbuffer_supported = (glx_version >= 103 ||
   114             isServerGLXExtensionSupported(display, screen,
   115                     "GLX_SGIX_pbuffer") );
   116 //    glx_fbconfig_supported = FALSE;
   117     if( glx_fbconfig_supported ) {
   118         int nelem;
   119         int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
   120                 GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
   121                 GLX_RENDER_TYPE, GLX_RGBA_BIT, 
   122                 GLX_DEPTH_SIZE, 24, 
   123                 GLX_STENCIL_SIZE, 8, 0 };
   124         GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
   125                 fb_attribs, &nelem );
   127         if( configs == NULL || nelem == 0 ) {
   128             /* Try a 16-bit depth buffer and see if it helps */
   129             fb_attribs[5] = 16;
   130             configs = glXChooseFBConfig( display, screen, fb_attribs, &nelem );
   131             if( nelem > 0 ) {
   132                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   133             }
   135         }
   136         if( configs == NULL || nelem == 0 ) {
   137             /* Still didn't work. Fallback to 1.2 methods */
   138             glx_fbconfig_supported = FALSE;
   139             glx_pbuffer_supported = FALSE;
   140         } else {
   141             glx_fbconfig = configs[0];
   142             glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
   143             XFree(configs);
   144         }
   145     }
   147     if( !glx_fbconfig_supported ) {
   148         int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, 0 };
   149         glx_visual = glXChooseVisual( display, screen, attribs );
   150         if( glx_visual == NULL ) {
   151             /* Try the 16-bit fallback here too */
   152             attribs[2] = 16;
   153             glx_visual = glXChooseVisual( display, screen, attribs );
   154             if( glx_visual != NULL ) {
   155                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   156             }
   157         }
   158     }
   160     if( glx_visual == NULL ) {
   161         return FALSE;
   162     }
   164     glx_is_initialized = TRUE;
   165     return TRUE;
   166 }
   168 gboolean video_glx_init_context( Display *display, Window window )
   169 {
   170     if( glx_fbconfig_supported ) {
   171         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   172                 GLX_RGBA_TYPE, NULL, True );
   173         if( glx_context == NULL ) {
   174             ERROR( "Unable to create a GLX Context.");
   175             return FALSE;
   176         }
   178         if( glXMakeCurrent( display, window,
   179                 glx_context ) == False ) {
   180             ERROR( "Unable to prepare GLX context for drawing" );
   181             glXDestroyContext( display, glx_context );
   182             return FALSE;
   183         }
   184     } else {
   185         glx_context = glXCreateContext( display, glx_visual, None, True );
   186         if( glx_context == NULL ) {
   187             ERROR( "Unable to create a GLX Context.");
   188             return FALSE;
   189         }
   191         if( glXMakeCurrent( display, window, glx_context ) == False ) {
   192             ERROR( "Unable to prepare GLX context for drawing" );
   193             glXDestroyContext( display, glx_context );
   194             return FALSE;
   195         }
   196     }
   198     if( !glXIsDirect(display, glx_context) ) {
   199         WARN( "Not using direct rendering - this is likely to be slow" );
   200     }
   202     video_x11_display = display;
   203     video_x11_window = window;
   205     return TRUE;
   206 }
   208 gboolean video_glx_init_driver( display_driver_t driver )
   209 {
   210     if( gl_fbo_is_supported() ) { // First preference
   211         gl_fbo_init(driver);
   212     } else if( glx_pbuffer_supported ) {
   213         glx_pbuffer_init(driver);
   214     } else {
   215         ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
   216         video_glx_shutdown();
   217         return FALSE;
   218     }
   219     return TRUE;
   220 }
   223 void video_glx_shutdown()
   224 {
   225     //    texcache_gl_shutdown();
   226     glx_is_initialized = FALSE;
   227     if( glx_context != NULL ) {
   228         glXDestroyContext( video_x11_display, glx_context );
   229         glx_context = NULL;
   230     }
   231     if( glx_visual != NULL ) {
   232         XFree(glx_visual);
   233         glx_visual = NULL;
   234     }
   235 }
   238 XVisualInfo *video_glx_get_visual()
   239 {
   240     return glx_visual;
   241 }
   244 int video_glx_load_font( const gchar *font_name )
   245 {
   246     int lists;
   247     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   248     if (font == NULL)
   249         return -1;
   251     lists = glGenLists(96);
   252     glXUseXFont(font->fid, 32, 96, lists);
   253     XFreeFont(video_x11_display, font);
   254     return lists;
   255 }
   258 void video_glx_swap_buffers( void )
   259 {
   260     glXSwapBuffers( video_x11_display, video_x11_window );
   261 }
   263 void video_glx_make_window_current( void )
   264 {
   265     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   266 }
   269 // Pbuffer support
   271 /**
   272  * Construct the initial frame buffers and allocate ids for everything.
   273  * The render handling driver methods are set to the fbo versions.
   274  */
   275 static void glx_pbuffer_init( display_driver_t driver ) 
   276 {
   277     GLint stencil_bits = 0;
   279     /* Retrieve the number of stencil bits */
   280     glGetIntegerv( GL_STENCIL_BITS, &stencil_bits );
   281     driver->capabilities.stencil_bits = stencil_bits;
   283     glGenTextures( 1, &glx_pbuffer_texture );
   284     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   285     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   286     driver->set_render_target = glx_pbuffer_set_render_target;
   287     driver->finish_render = glx_pbuffer_finish_render;
   288     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   289     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   290     driver->display_blank = glx_pbuffer_display_blank;
   291     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   292 }
   294 void glx_pbuffer_shutdown()
   295 {
   296     glDeleteTextures( 1, &glx_pbuffer_texture );
   297 }
   299 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   300 {
   301     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   302             GLX_PRESERVED_CONTENTS, True, 0 };
   303     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   304     if( pb == (GLXPbuffer)NULL ) {
   305         ERROR( "Unable to create pbuffer" );
   306         return NULL;
   307     }
   308     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   309     buffer->width = width;
   310     buffer->height = height;
   311     buffer->buf_id = pb;
   312     buffer->tex_id = tex_id;
   313     return buffer;
   314 }
   316 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   317 {
   318     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   319     buffer->buf_id = 0;
   320     free( buffer );
   321 }
   323 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   324 {
   325     glFinish();
   326     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   327         ERROR( "Make context current (pbuffer) failed!" );
   328     }
   329     /* setup the gl context */
   330     glViewport( 0, 0, buffer->width, buffer->height );
   331     glDrawBuffer(GL_FRONT);
   333     return TRUE;
   334 }
   336 static void glx_pbuffer_finish_render( render_buffer_t buffer )
   337 {
   338     glFinish();
   339     if( buffer->tex_id != 0 ) {
   340         // The pbuffer should already be the current context, but just in case...
   341         glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   342         glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->tex_id );
   343         glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   344     }
   345 }
   348 /**
   349  * Render the texture holding the given buffer to the front window
   350  * buffer.
   351  */
   352 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   353 {
   354     glFinish();
   355     glReadBuffer( GL_FRONT );
   356     glDrawBuffer( GL_FRONT );
   357     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   358     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
   359     glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   360     video_glx_make_window_current();
   361     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   362 }
   364 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   365 {
   366     glFinish();
   367     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   368     GLenum type = colour_formats[frame->colour_format].type;
   369     GLenum format = colour_formats[frame->colour_format].format;
   370     int bpp = colour_formats[frame->colour_format].bpp;
   371     int rowstride = (frame->rowstride / bpp) - frame->width;
   373     gl_reset_state();
   374     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   375     glRasterPos2f(0.375, frame->height-0.375);
   376     glPixelZoom( 1.0, 1.0 );
   377     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   378     glFlush();
   379 }
   381 static void glx_pbuffer_display_blank( uint32_t colour )
   382 {
   383     glFinish();
   384     video_glx_make_window_current();
   385     gl_display_blank( colour );
   386 }
   388 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   389                                                 int rowstride, int format )
   390 {
   391     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   392     glReadBuffer( GL_FRONT );
   393     return gl_read_render_buffer( target, buffer, rowstride, format );
   394 }
.