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 573:28ff000e66bb
prev561:533f6b478071
next1065:bc1cc0c54917
author nkeynes
date Fri Jan 11 00:36:07 2008 +0000 (11 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Fix compile error on MacOSX
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;
    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     }
   128     glx_is_initialized = TRUE;
   129     return TRUE;
   130 }
   132 gboolean video_glx_init_context( Display *display, Window window )
   133 {
   134     if( glx_fbconfig_supported ) {
   135         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   136 					   GLX_RGBA_TYPE, NULL, True );
   137 	if( glx_context == NULL ) {
   138 	    ERROR( "Unable to create a GLX Context.");
   139 	    return FALSE;
   140 	}
   142 	if( glXMakeContextCurrent( display, window, window, 
   143 				   glx_context ) == False ) {
   144 	    ERROR( "Unable to prepare GLX context for drawing" );
   145 	    glXDestroyContext( display, glx_context );
   146 	    return FALSE;
   147 	}
   148     } else {
   149         glx_context = glXCreateContext( display, glx_visual, None, True );
   150 	if( glx_context == NULL ) {
   151 	    ERROR( "Unable to create a GLX Context.");
   152 	    return FALSE;
   153 	}
   155 	if( glXMakeCurrent( display, window, glx_context ) == False ) {
   156 	    ERROR( "Unable to prepare GLX context for drawing" );
   157 	    glXDestroyContext( display, glx_context );
   158 	    return FALSE;
   159 	}
   160     }
   162     if( !glXIsDirect(display, glx_context) ) {
   163     	WARN( "Not using direct rendering - this is likely to be slow" );
   164     }
   166     texcache_gl_init();
   167     video_x11_display = display;
   168     video_x11_window = window;
   170     return TRUE;
   171 }
   173 gboolean video_glx_init_driver( display_driver_t driver )
   174 {
   175     if( gl_fbo_is_supported() ) { // First preference
   176 	gl_fbo_init(driver);
   177     } else if( glx_pbuffer_supported ) {
   178 	glx_pbuffer_init(driver);
   179     } else {
   180         ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
   181         video_glx_shutdown();
   182 	return FALSE;
   183     }
   184     return TRUE;
   185 }
   188 void video_glx_shutdown()
   189 {
   190   //    texcache_gl_shutdown();
   191     glx_is_initialized = FALSE;
   192     if( glx_context != NULL ) {
   193         glXDestroyContext( video_x11_display, glx_context );
   194 	glx_context = NULL;
   195     }
   196     if( glx_visual != NULL ) {
   197         XFree(glx_visual);
   198 	glx_visual = NULL;
   199     }
   200 }
   203 XVisualInfo *video_glx_get_visual()
   204 {
   205     return glx_visual;
   206 }
   209 int video_glx_load_font( const gchar *font_name )
   210 {
   211     int lists;
   212     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   213     if (font == NULL)
   214 	return -1;
   216     lists = glGenLists(96);
   217     glXUseXFont(font->fid, 32, 96, lists);
   218     XFreeFont(video_x11_display, font);
   219     return lists;
   220 }
   223 void video_glx_swap_buffers( void )
   224 {
   225     glXSwapBuffers( video_x11_display, video_x11_window );
   226 }
   228 void video_glx_make_window_current( void )
   229 {
   230     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   231 }
   234 // Pbuffer support
   236 /**
   237  * Construct the initial frame buffers and allocate ids for everything.
   238  * The render handling driver methods are set to the fbo versions.
   239  */
   240 static void glx_pbuffer_init( display_driver_t driver ) 
   241 {
   242     glGenTextures( 1, &glx_pbuffer_texture );
   243     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   244     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   245     driver->set_render_target = glx_pbuffer_set_render_target;
   246     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   247     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   248     driver->display_blank = glx_pbuffer_display_blank;
   249     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   250 }
   252 void glx_pbuffer_shutdown()
   253 {
   254     glDeleteTextures( 1, &glx_pbuffer_texture );
   255 }
   257 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height )
   258 {
   259     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   260                       GLX_PRESERVED_CONTENTS, True, 0 };
   261     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   262     if( pb == (GLXPbuffer)NULL ) {
   263 	ERROR( "Unable to create pbuffer" );
   264 	return NULL;
   265     }
   266     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   267     buffer->width = width;
   268     buffer->height = height;
   269     buffer->buf_id = pb;
   270     return buffer;
   271 }
   273 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   274 {
   275     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   276     buffer->buf_id = 0;
   277     free( buffer );
   278 }
   280 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   281 {
   282     glFinish();
   283     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   284 	ERROR( "Make context current (pbuffer) failed!" );
   285     }
   286     /* setup the gl context */
   287     glViewport( 0, 0, buffer->width, buffer->height );
   288     glDrawBuffer(GL_FRONT);
   290     return TRUE;
   291 }
   293 /**
   294  * Render the texture holding the given buffer to the front window
   295  * buffer.
   296  */
   297 static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   298 {
   299     glFinish();
   300     glReadBuffer( GL_FRONT );
   301     glDrawBuffer( GL_FRONT );
   302     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   303     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
   304     glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   305     video_glx_make_window_current();
   306     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   307     return TRUE;
   308 }
   310 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   311 {
   312     glFinish();
   313     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   314     GLenum type = colour_formats[frame->colour_format].type;
   315     GLenum format = colour_formats[frame->colour_format].format;
   316     int bpp = colour_formats[frame->colour_format].bpp;
   317     int rowstride = (frame->rowstride / bpp) - frame->width;
   319     gl_reset_state();
   320     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   321     glRasterPos2f(0.375, frame->height-0.375);
   322     glPixelZoom( 1.0, 1.0 );
   323     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   324     glFlush();
   325 }
   327 static gboolean glx_pbuffer_display_blank( uint32_t colour )
   328 {
   329     glFinish();
   330     video_glx_make_window_current();
   331     return gl_display_blank( colour );
   332 }
   334 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   335 					       int rowstride, int format )
   336 {
   337     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   338     glReadBuffer( GL_FRONT );
   339     return gl_read_render_buffer( target, buffer, rowstride, format );
   340 }
.