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 561:533f6b478071
prev545:fdcdcd8b9fd1
next573:28ff000e66bb
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
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 <X11/Xlib.h>
    20 #include <GL/glx.h>
    21 #include <GL/gl.h>
    22 #include "dream.h"
    23 #include "pvr2/pvr2.h"
    24 #include "drivers/video_glx.h"
    25 #include "drivers/gl_common.h"
    27 /**
    28  * General X11 parameters. The front-end driver is expected to set this up
    29  * by calling video_glx_init after initializing itself.
    30  */
    31 Display *video_x11_display = NULL;
    32 Window video_x11_window = 0;
    33 static gboolean glsl_loaded = FALSE;
    35 static int glx_version = 100;
    36 static XVisualInfo *glx_visual;
    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 int 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 gboolean 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 gboolean 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     }
   129     glx_is_initialized = TRUE;
   130     return TRUE;
   131 }
   133 gboolean video_glx_init_context( Display *display, Window window )
   134 {
   135     if( glx_fbconfig_supported ) {
   136         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   137 					   GLX_RGBA_TYPE, NULL, True );
   138 	if( glx_context == NULL ) {
   139 	    ERROR( "Unable to create a GLX Context.");
   140 	    return FALSE;
   141 	}
   143 	if( glXMakeContextCurrent( display, window, window, 
   144 				   glx_context ) == False ) {
   145 	    ERROR( "Unable to prepare GLX context for drawing" );
   146 	    glXDestroyContext( display, glx_context );
   147 	    return FALSE;
   148 	}
   149     } else {
   150         glx_context = glXCreateContext( display, glx_visual, None, True );
   151 	if( glx_context == NULL ) {
   152 	    ERROR( "Unable to create a GLX Context.");
   153 	    return FALSE;
   154 	}
   156 	if( glXMakeCurrent( display, window, glx_context ) == False ) {
   157 	    ERROR( "Unable to prepare GLX context for drawing" );
   158 	    glXDestroyContext( display, glx_context );
   159 	    return FALSE;
   160 	}
   161     }
   163     if( !glXIsDirect(display, glx_context) ) {
   164     	WARN( "Not using direct rendering - this is likely to be slow" );
   165     }
   167     texcache_gl_init();
   168     video_x11_display = display;
   169     video_x11_window = window;
   171     return TRUE;
   172 }
   174 gboolean video_glx_init_driver( display_driver_t driver )
   175 {
   176     if( gl_fbo_is_supported() ) { // First preference
   177 	gl_fbo_init(driver);
   178     } else if( glx_pbuffer_supported ) {
   179 	glx_pbuffer_init(driver);
   180     } else {
   181         ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
   182         video_glx_shutdown();
   183 	return FALSE;
   184     }
   185     return TRUE;
   186 }
   189 void video_glx_shutdown()
   190 {
   191   //    texcache_gl_shutdown();
   192     glx_is_initialized = FALSE;
   193     if( glx_context != NULL ) {
   194         glXDestroyContext( video_x11_display, glx_context );
   195 	glx_context = NULL;
   196     }
   197     if( glx_visual != NULL ) {
   198         XFree(glx_visual);
   199 	glx_visual = NULL;
   200     }
   201 }
   204 XVisualInfo *video_glx_get_visual()
   205 {
   206     return glx_visual;
   207 }
   210 int video_glx_load_font( const gchar *font_name )
   211 {
   212     int lists;
   213     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   214     if (font == NULL)
   215 	return -1;
   217     lists = glGenLists(96);
   218     glXUseXFont(font->fid, 32, 96, lists);
   219     XFreeFont(video_x11_display, font);
   220     return lists;
   221 }
   224 void video_glx_swap_buffers( void )
   225 {
   226     glXSwapBuffers( video_x11_display, video_x11_window );
   227 }
   229 void video_glx_make_window_current( void )
   230 {
   231     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   232 }
   235 // Pbuffer support
   237 /**
   238  * Construct the initial frame buffers and allocate ids for everything.
   239  * The render handling driver methods are set to the fbo versions.
   240  */
   241 static void glx_pbuffer_init( display_driver_t driver ) 
   242 {
   243     glGenTextures( 1, &glx_pbuffer_texture );
   244     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   245     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   246     driver->set_render_target = glx_pbuffer_set_render_target;
   247     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   248     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   249     driver->display_blank = glx_pbuffer_display_blank;
   250     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   251 }
   253 void glx_pbuffer_shutdown()
   254 {
   255     glDeleteTextures( 1, &glx_pbuffer_texture );
   256 }
   258 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height )
   259 {
   260     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   261                       GLX_PRESERVED_CONTENTS, True, 0 };
   262     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   263     if( pb == (GLXPbuffer)NULL ) {
   264 	ERROR( "Unable to create pbuffer" );
   265 	return NULL;
   266     }
   267     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   268     buffer->width = width;
   269     buffer->height = height;
   270     buffer->buf_id = pb;
   271     return buffer;
   272 }
   274 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   275 {
   276     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   277     buffer->buf_id = 0;
   278     free( buffer );
   279 }
   281 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   282 {
   283     glFinish();
   284     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   285 	ERROR( "Make context current (pbuffer) failed!" );
   286     }
   287     /* setup the gl context */
   288     glViewport( 0, 0, buffer->width, buffer->height );
   289     glDrawBuffer(GL_FRONT);
   291     return TRUE;
   292 }
   294 /**
   295  * Render the texture holding the given buffer to the front window
   296  * buffer.
   297  */
   298 static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   299 {
   300     glFinish();
   301     glReadBuffer( GL_FRONT );
   302     glDrawBuffer( GL_FRONT );
   303     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   304     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
   305     glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   306     video_glx_make_window_current();
   307     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   308     return TRUE;
   309 }
   311 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   312 {
   313     glFinish();
   314     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   315     GLenum type = colour_formats[frame->colour_format].type;
   316     GLenum format = colour_formats[frame->colour_format].format;
   317     int bpp = colour_formats[frame->colour_format].bpp;
   318     int rowstride = (frame->rowstride / bpp) - frame->width;
   320     gl_reset_state();
   321     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   322     glRasterPos2f(0.375, frame->height-0.375);
   323     glPixelZoom( 1.0, 1.0 );
   324     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   325     glFlush();
   326 }
   328 static gboolean glx_pbuffer_display_blank( uint32_t colour )
   329 {
   330     glFinish();
   331     video_glx_make_window_current();
   332     return gl_display_blank( colour );
   333 }
   335 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   336 					       int rowstride, int format )
   337 {
   338     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   339     glReadBuffer( GL_FRONT );
   340     return gl_read_render_buffer( target, buffer, rowstride, format );
   341 }
.