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 669:ab344e42bca9
prev665:99ae9dc4cab7
next676:705a5707da13
author nkeynes
date Mon May 12 10:00:13 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Cleanup most of the -Wall warnings (getting a bit sloppy...)
Convert FP code to use fixed banks rather than indirect pointer
(3-4% faster this way now)
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 <string.h>
    20 #include "display.h"
    21 #include <X11/Xlib.h>
    22 #include <GL/glx.h>
    23 #include "pvr2/pvr2.h"
    24 #include "pvr2/glutil.h"
    25 #include "drivers/video_glx.h"
    26 #include "drivers/video_gl.h"
    28 /**
    29  * General X11 parameters. The front-end driver is expected to set this up
    30  * by calling video_glx_init after initializing itself.
    31  */
    32 Display *video_x11_display = NULL;
    33 Window video_x11_window = 0;
    35 static int glx_version = 100;
    36 static XVisualInfo *glx_visual = NULL;
    37 static GLXFBConfig glx_fbconfig;
    38 static GLXContext glx_context = NULL;
    39 static gboolean glx_is_initialized = FALSE;
    40 static gboolean glx_fbconfig_supported = FALSE;
    41 static gboolean glx_pbuffer_supported = FALSE;
    42 static GLuint glx_pbuffer_texture = 0; 
    44 /* Prototypes for pbuffer support methods */
    45 static void glx_pbuffer_init( display_driver_t driver );
    46 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height );
    47 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
    48 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
    49 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer );
    50 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    51 static void glx_pbuffer_display_blank( uint32_t colour );
    52 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    54 /**
    55  * Test if a specific extension is supported. From opengl.org
    56  * @param extension extension name to check for
    57  * @return TRUE if supported, otherwise FALSE.
    58  */
    59 gboolean isServerGLXExtensionSupported( Display *display, int screen, 
    60 					const char *extension )
    61 {
    62     const char *extensions = NULL;
    63     const char *start;
    64     char *where, *terminator;
    66     /* Extension names should not have spaces. */
    67     where = strchr(extension, ' ');
    68     if (where || *extension == '\0')
    69 	return 0;
    70     extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
    71     start = extensions;
    72     for (;;) {
    73 	where = strstr((const char *) start, extension);
    74 	if (!where)
    75 	    break;
    76 	terminator = where + strlen(extension);
    77 	if (where == start || *(where - 1) == ' ')
    78 	    if (*terminator == ' ' || *terminator == '\0')
    79 		return TRUE;
    80 	start = terminator;
    81     }
    82     return FALSE;
    83 }
    85 gboolean video_glx_init( Display *display, int screen )
    86 {
    87     int major, minor;
    89     if( glx_is_initialized ) {
    90         return TRUE;
    91     }
    93     Bool result = glXQueryVersion( display, &major, &minor );
    94     if( result != False ) {
    95         glx_version = (major*100) + minor;
    96     }
    98     glx_fbconfig_supported = (glx_version >= 103 || 
    99 			      isServerGLXExtensionSupported(display, screen,
   100 						      "GLX_SGIX_fbconfig") );
   101     glx_pbuffer_supported = (glx_version >= 103 ||
   102 			     isServerGLXExtensionSupported(display, screen,
   103 						     "GLX_SGIX_pbuffer") );
   105     if( glx_fbconfig_supported ) {
   106 	int nelem;
   107         int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
   108 			     GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
   109 			     GLX_RENDER_TYPE, GLX_RGBA_BIT, 
   110 			     GLX_DEPTH_SIZE, 24, 0 };
   111 	GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
   112 						  fb_attribs, &nelem );
   114 	if( configs == NULL || nelem == 0 ) {
   115 	    /* Didn't work. Fallback to 1.2 methods */
   116 	    glx_fbconfig_supported = FALSE;
   117 	    glx_pbuffer_supported = FALSE;
   118 	} else {
   119 	    glx_fbconfig = configs[0];
   120 	    glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
   121 	    XFree(configs);
   122 	}
   123     }
   125     if( !glx_fbconfig_supported ) {
   126         int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, 0 };
   127 	glx_visual = glXChooseVisual( display, screen, attribs );
   128     }
   130     if( glx_visual == NULL ) {
   131         return FALSE;
   132     }
   134     glx_is_initialized = TRUE;
   135     return TRUE;
   136 }
   138 gboolean video_glx_init_context( Display *display, Window window )
   139 {
   140     if( glx_fbconfig_supported ) {
   141         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   142 					   GLX_RGBA_TYPE, NULL, True );
   143 	if( glx_context == NULL ) {
   144 	    ERROR( "Unable to create a GLX Context.");
   145 	    return FALSE;
   146 	}
   148 	if( glXMakeContextCurrent( display, window, window, 
   149 				   glx_context ) == False ) {
   150 	    ERROR( "Unable to prepare GLX context for drawing" );
   151 	    glXDestroyContext( display, glx_context );
   152 	    return FALSE;
   153 	}
   154     } else {
   155         glx_context = glXCreateContext( display, glx_visual, None, True );
   156 	if( glx_context == NULL ) {
   157 	    ERROR( "Unable to create a GLX Context.");
   158 	    return FALSE;
   159 	}
   161 	if( glXMakeCurrent( display, window, glx_context ) == False ) {
   162 	    ERROR( "Unable to prepare GLX context for drawing" );
   163 	    glXDestroyContext( display, glx_context );
   164 	    return FALSE;
   165 	}
   166     }
   168     if( !glXIsDirect(display, glx_context) ) {
   169     	WARN( "Not using direct rendering - this is likely to be slow" );
   170     }
   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 void 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 }
   314 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   315 {
   316     glFinish();
   317     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   318     GLenum type = colour_formats[frame->colour_format].type;
   319     GLenum format = colour_formats[frame->colour_format].format;
   320     int bpp = colour_formats[frame->colour_format].bpp;
   321     int rowstride = (frame->rowstride / bpp) - frame->width;
   323     gl_reset_state();
   324     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   325     glRasterPos2f(0.375, frame->height-0.375);
   326     glPixelZoom( 1.0, 1.0 );
   327     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   328     glFlush();
   329 }
   331 static void glx_pbuffer_display_blank( uint32_t colour )
   332 {
   333     glFinish();
   334     video_glx_make_window_current();
   335     gl_display_blank( colour );
   336 }
   338 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   339 					       int rowstride, int format )
   340 {
   341     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   342     glReadBuffer( GL_FRONT );
   343     return gl_read_render_buffer( target, buffer, rowstride, format );
   344 }
.