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 606:23029426ab8f
prev586:2a3ba82cf243
next635:76c63aac3590
next653:3202ff01d48e
author nkeynes
date Fri Jan 25 05:52:51 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Detect if we failed to obtain a GLX visual and abort rather than crashing
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 "display.h"
    20 #include "pvr2/pvr2.h"
    21 #include "drivers/gl_common.h"
    22 #include <X11/Xlib.h>
    23 #include <GL/glx.h>
    24 #include "drivers/video_glx.h"
    26 /**
    27  * General X11 parameters. The front-end driver is expected to set this up
    28  * by calling video_glx_init after initializing itself.
    29  */
    30 Display *video_x11_display = NULL;
    31 Window video_x11_window = 0;
    32 static gboolean glsl_loaded = FALSE;
    34 static int glx_version = 100;
    35 static XVisualInfo *glx_visual = NULL;
    36 static GLXFBConfig glx_fbconfig;
    37 static GLXContext glx_context = NULL;
    38 static gboolean glx_is_initialized = FALSE;
    39 static gboolean glx_fbconfig_supported = FALSE;
    40 static gboolean glx_pbuffer_supported = FALSE;
    41 static int glx_pbuffer_texture = 0; 
    43 /* Prototypes for pbuffer support methods */
    44 static void glx_pbuffer_init( display_driver_t driver );
    45 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height );
    46 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
    47 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
    48 static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer );
    49 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    50 static gboolean glx_pbuffer_display_blank( uint32_t colour );
    51 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    53 /**
    54  * Test if a specific extension is supported. From opengl.org
    55  * @param extension extension name to check for
    56  * @return TRUE if supported, otherwise FALSE.
    57  */
    58 gboolean isServerGLXExtensionSupported( Display *display, int screen, 
    59 					const char *extension )
    60 {
    61     const char *extensions = NULL;
    62     const char *start;
    63     char *where, *terminator;
    65     /* Extension names should not have spaces. */
    66     where = strchr(extension, ' ');
    67     if (where || *extension == '\0')
    68 	return 0;
    69     extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
    70     start = extensions;
    71     for (;;) {
    72 	where = strstr((const char *) start, extension);
    73 	if (!where)
    74 	    break;
    75 	terminator = where + strlen(extension);
    76 	if (where == start || *(where - 1) == ' ')
    77 	    if (*terminator == ' ' || *terminator == '\0')
    78 		return TRUE;
    79 	start = terminator;
    80     }
    81     return FALSE;
    82 }
    84 gboolean video_glx_init( Display *display, int screen )
    85 {
    86     int major, minor;
    88     if( glx_is_initialized ) {
    89         return TRUE;
    90     }
    92     Bool result = glXQueryVersion( display, &major, &minor );
    93     if( result != False ) {
    94         glx_version = (major*100) + minor;
    95     }
    97     glx_fbconfig_supported = (glx_version >= 103 || 
    98 			      isServerGLXExtensionSupported(display, screen,
    99 						      "GLX_SGIX_fbconfig") );
   100     glx_pbuffer_supported = (glx_version >= 103 ||
   101 			     isServerGLXExtensionSupported(display, screen,
   102 						     "GLX_SGIX_pbuffer") );
   104     if( glx_fbconfig_supported ) {
   105 	int nelem;
   106         int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
   107 			     GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
   108 			     GLX_RENDER_TYPE, GLX_RGBA_BIT, 
   109 			     GLX_DEPTH_SIZE, 24, 0 };
   110 	GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
   111 						  fb_attribs, &nelem );
   113 	if( configs == NULL || nelem == 0 ) {
   114 	    /* Didn't work. Fallback to 1.2 methods */
   115 	    glx_fbconfig_supported = FALSE;
   116 	    glx_pbuffer_supported = FALSE;
   117 	} else {
   118 	    glx_fbconfig = configs[0];
   119 	    glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
   120 	    XFree(configs);
   121 	}
   122     }
   124     if( !glx_fbconfig_supported ) {
   125         int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, 0 };
   126 	glx_visual = glXChooseVisual( display, screen, attribs );
   127     }
   129     if( glx_visual == NULL ) {
   130         return FALSE;
   131     }
   133     glx_is_initialized = TRUE;
   134     return TRUE;
   135 }
   137 gboolean video_glx_init_context( Display *display, Window window )
   138 {
   139     if( glx_fbconfig_supported ) {
   140         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   141 					   GLX_RGBA_TYPE, NULL, True );
   142 	if( glx_context == NULL ) {
   143 	    ERROR( "Unable to create a GLX Context.");
   144 	    return FALSE;
   145 	}
   147 	if( glXMakeContextCurrent( display, window, window, 
   148 				   glx_context ) == False ) {
   149 	    ERROR( "Unable to prepare GLX context for drawing" );
   150 	    glXDestroyContext( display, glx_context );
   151 	    return FALSE;
   152 	}
   153     } else {
   154         glx_context = glXCreateContext( display, glx_visual, None, True );
   155 	if( glx_context == NULL ) {
   156 	    ERROR( "Unable to create a GLX Context.");
   157 	    return FALSE;
   158 	}
   160 	if( glXMakeCurrent( display, window, glx_context ) == False ) {
   161 	    ERROR( "Unable to prepare GLX context for drawing" );
   162 	    glXDestroyContext( display, glx_context );
   163 	    return FALSE;
   164 	}
   165     }
   167     if( !glXIsDirect(display, glx_context) ) {
   168     	WARN( "Not using direct rendering - this is likely to be slow" );
   169     }
   171     texcache_gl_init();
   172     video_x11_display = display;
   173     video_x11_window = window;
   175     return TRUE;
   176 }
   178 gboolean video_glx_init_driver( display_driver_t driver )
   179 {
   180     if( gl_fbo_is_supported() ) { // First preference
   181 	gl_fbo_init(driver);
   182     } else if( glx_pbuffer_supported ) {
   183 	glx_pbuffer_init(driver);
   184     } else {
   185         ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
   186         video_glx_shutdown();
   187 	return FALSE;
   188     }
   189     return TRUE;
   190 }
   193 void video_glx_shutdown()
   194 {
   195   //    texcache_gl_shutdown();
   196     glx_is_initialized = FALSE;
   197     if( glx_context != NULL ) {
   198         glXDestroyContext( video_x11_display, glx_context );
   199 	glx_context = NULL;
   200     }
   201     if( glx_visual != NULL ) {
   202         XFree(glx_visual);
   203 	glx_visual = NULL;
   204     }
   205 }
   208 XVisualInfo *video_glx_get_visual()
   209 {
   210     return glx_visual;
   211 }
   214 int video_glx_load_font( const gchar *font_name )
   215 {
   216     int lists;
   217     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   218     if (font == NULL)
   219 	return -1;
   221     lists = glGenLists(96);
   222     glXUseXFont(font->fid, 32, 96, lists);
   223     XFreeFont(video_x11_display, font);
   224     return lists;
   225 }
   228 void video_glx_swap_buffers( void )
   229 {
   230     glXSwapBuffers( video_x11_display, video_x11_window );
   231 }
   233 void video_glx_make_window_current( void )
   234 {
   235     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   236 }
   239 // Pbuffer support
   241 /**
   242  * Construct the initial frame buffers and allocate ids for everything.
   243  * The render handling driver methods are set to the fbo versions.
   244  */
   245 static void glx_pbuffer_init( display_driver_t driver ) 
   246 {
   247     glGenTextures( 1, &glx_pbuffer_texture );
   248     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   249     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   250     driver->set_render_target = glx_pbuffer_set_render_target;
   251     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   252     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   253     driver->display_blank = glx_pbuffer_display_blank;
   254     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   255 }
   257 void glx_pbuffer_shutdown()
   258 {
   259     glDeleteTextures( 1, &glx_pbuffer_texture );
   260 }
   262 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height )
   263 {
   264     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   265                       GLX_PRESERVED_CONTENTS, True, 0 };
   266     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   267     if( pb == (GLXPbuffer)NULL ) {
   268 	ERROR( "Unable to create pbuffer" );
   269 	return NULL;
   270     }
   271     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   272     buffer->width = width;
   273     buffer->height = height;
   274     buffer->buf_id = pb;
   275     return buffer;
   276 }
   278 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   279 {
   280     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   281     buffer->buf_id = 0;
   282     free( buffer );
   283 }
   285 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   286 {
   287     glFinish();
   288     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   289 	ERROR( "Make context current (pbuffer) failed!" );
   290     }
   291     /* setup the gl context */
   292     glViewport( 0, 0, buffer->width, buffer->height );
   293     glDrawBuffer(GL_FRONT);
   295     return TRUE;
   296 }
   298 /**
   299  * Render the texture holding the given buffer to the front window
   300  * buffer.
   301  */
   302 static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   303 {
   304     glFinish();
   305     glReadBuffer( GL_FRONT );
   306     glDrawBuffer( GL_FRONT );
   307     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   308     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
   309     glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   310     video_glx_make_window_current();
   311     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   312     return TRUE;
   313 }
   315 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   316 {
   317     glFinish();
   318     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   319     GLenum type = colour_formats[frame->colour_format].type;
   320     GLenum format = colour_formats[frame->colour_format].format;
   321     int bpp = colour_formats[frame->colour_format].bpp;
   322     int rowstride = (frame->rowstride / bpp) - frame->width;
   324     gl_reset_state();
   325     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   326     glRasterPos2f(0.375, frame->height-0.375);
   327     glPixelZoom( 1.0, 1.0 );
   328     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   329     glFlush();
   330 }
   332 static gboolean glx_pbuffer_display_blank( uint32_t colour )
   333 {
   334     glFinish();
   335     video_glx_make_window_current();
   336     return gl_display_blank( colour );
   337 }
   339 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   340 					       int rowstride, int format )
   341 {
   342     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   343     glReadBuffer( GL_FRONT );
   344     return gl_read_render_buffer( target, buffer, rowstride, format );
   345 }
.