Keep current OpenGL context when possible
authorPatrice Mandin <patmandin@gmail.com>
Fri, 26 Nov 2004 22:11:30 +0000
changeset 9920324ce32b2d9
parent 991 12b13601a544
child 993 2662da16d668
Keep current OpenGL context when possible
src/video/ataricommon/SDL_atarigl.c
src/video/ataricommon/SDL_atarigl_c.h
src/video/gem/SDL_gemvideo.c
src/video/xbios/SDL_xbios.c
     1.1 --- a/src/video/ataricommon/SDL_atarigl.c	Fri Nov 26 16:16:50 2004 +0000
     1.2 +++ b/src/video/ataricommon/SDL_atarigl.c	Fri Nov 26 22:11:30 2004 +0000
     1.3 @@ -80,13 +80,9 @@
     1.4  	return (gl_active);
     1.5  }
     1.6  
     1.7 -void SDL_AtariGL_Quit(_THIS)
     1.8 +void SDL_AtariGL_Quit(_THIS, SDL_bool unload)
     1.9  {
    1.10  #ifdef HAVE_OPENGL
    1.11 -	if (!gl_active) {
    1.12 -		return;
    1.13 -	}
    1.14 -
    1.15  	if (gl_oldmesa) {
    1.16  		/* Old mesa implementations */
    1.17  		if (this->gl_data->OSMesaDestroyLDG) {
    1.18 @@ -106,7 +102,9 @@
    1.19  		}
    1.20  	}
    1.21  
    1.22 -	SDL_AtariGL_UnloadLibrary(this);
    1.23 +	if (unload) {
    1.24 +		SDL_AtariGL_UnloadLibrary(this);
    1.25 +	}
    1.26  
    1.27  #endif /* HAVE_OPENGL */
    1.28  	gl_active = 0;
    1.29 @@ -378,6 +376,8 @@
    1.30  	GLenum osmesa_format;
    1.31  	SDL_PixelFormat *pixel_format;
    1.32  	Uint32	redmask;
    1.33 +	int recreatecontext;
    1.34 +	GLint newaccumsize;
    1.35  
    1.36  	if (this->gl_config.dll_handle) {
    1.37  		if (this->gl_data->OSMesaCreateContextExt == NULL) {
    1.38 @@ -440,11 +440,39 @@
    1.39  			break;
    1.40  	}
    1.41  
    1.42 -	gl_ctx = this->gl_data->OSMesaCreateContextExt(
    1.43 -		osmesa_format, this->gl_config.depth_size,
    1.44 -		this->gl_config.stencil_size, this->gl_config.accum_red_size +
    1.45 -		this->gl_config.accum_green_size + this->gl_config.accum_blue_size +
    1.46 -		this->gl_config.accum_alpha_size, NULL );
    1.47 +	/* Try to keep current context if possible */
    1.48 +	newaccumsize =
    1.49 +		this->gl_config.accum_red_size +
    1.50 +		this->gl_config.accum_green_size +
    1.51 +		this->gl_config.accum_blue_size +
    1.52 +		this->gl_config.accum_alpha_size;
    1.53 +	recreatecontext=1;
    1.54 +	if (gl_ctx &&
    1.55 +		(gl_curformat == osmesa_format) &&
    1.56 +		(gl_curdepth == this->gl_config.depth_size) &&
    1.57 +		(gl_curstencil == this->gl_config.stencil_size) &&
    1.58 +		(gl_curaccum == newaccumsize)) {
    1.59 +		recreatecontext = 0;
    1.60 +	}
    1.61 +	if (recreatecontext) {
    1.62 +		SDL_AtariGL_Quit(this, SDL_FALSE);
    1.63 +
    1.64 +		gl_ctx = this->gl_data->OSMesaCreateContextExt(
    1.65 +			osmesa_format, this->gl_config.depth_size,
    1.66 +			this->gl_config.stencil_size, newaccumsize, NULL );
    1.67 +
    1.68 +		if (gl_ctx) {
    1.69 +			gl_curformat = osmesa_format;
    1.70 +			gl_curdepth = this->gl_config.depth_size;
    1.71 +			gl_curstencil = this->gl_config.stencil_size;
    1.72 +			gl_curaccum = newaccumsize;
    1.73 +		} else {
    1.74 +			gl_curformat = 0;
    1.75 +			gl_curdepth = 0;
    1.76 +			gl_curstencil = 0;
    1.77 +			gl_curaccum = 0;
    1.78 +		}
    1.79 +	}
    1.80  
    1.81  	return (gl_ctx != NULL);
    1.82  }
    1.83 @@ -454,6 +482,7 @@
    1.84  	GLenum osmesa_format;
    1.85  	SDL_PixelFormat *pixel_format;
    1.86  	Uint32	redmask;
    1.87 +	int recreatecontext;
    1.88  
    1.89  	if (this->gl_config.dll_handle) {
    1.90  		if (this->gl_data->OSMesaCreateLDG == NULL) {
    1.91 @@ -520,9 +549,31 @@
    1.92  			break;
    1.93  	}
    1.94  
    1.95 -	gl_shadow = this->gl_data->OSMesaCreateLDG(
    1.96 -		osmesa_format, GL_UNSIGNED_BYTE, current->w, current->h
    1.97 -	);
    1.98 +	/* Try to keep current context if possible */
    1.99 +	recreatecontext=1;
   1.100 +	if (gl_shadow &&
   1.101 +		(gl_curformat == osmesa_format) &&
   1.102 +		(gl_curwidth == current->w) &&
   1.103 +		(gl_curheight == current->h)) {
   1.104 +		recreatecontext = 0;
   1.105 +	}
   1.106 +	if (recreatecontext) {
   1.107 +		SDL_AtariGL_Quit(this, SDL_FALSE);
   1.108 +
   1.109 +		gl_shadow = this->gl_data->OSMesaCreateLDG(
   1.110 +			osmesa_format, GL_UNSIGNED_BYTE, current->w, current->h
   1.111 +		);
   1.112 +
   1.113 +		if (gl_shadow) {
   1.114 +			gl_curformat = osmesa_format;
   1.115 +			gl_curwidth = current->w;
   1.116 +			gl_curheight = current->h;
   1.117 +		} else {
   1.118 +			gl_curformat = 0;
   1.119 +			gl_curwidth = 0;
   1.120 +			gl_curheight = 0;
   1.121 +		}
   1.122 +	}
   1.123  
   1.124  	return (gl_shadow != NULL);
   1.125  }
     2.1 --- a/src/video/ataricommon/SDL_atarigl_c.h	Fri Nov 26 16:16:50 2004 +0000
     2.2 +++ b/src/video/ataricommon/SDL_atarigl_c.h	Fri Nov 26 22:11:30 2004 +0000
     2.3 @@ -61,6 +61,11 @@
     2.4  	/* mesa_gl.ldg, tiny_gl.ldg */
     2.5  	void *(*OSMesaCreateLDG)( long format, long type, long width, long height );
     2.6  	void (*OSMesaDestroyLDG)(void);
     2.7 +
     2.8 +	/* Info needed to compare existing context with new asked one */
     2.9 +	int width, height;
    2.10 +	GLenum format;
    2.11 +	GLint depth,stencil,accum;
    2.12  };
    2.13  
    2.14  /* Variable names */
    2.15 @@ -71,10 +76,16 @@
    2.16  #define gl_shadow	(this->gl_data->gl_shadow)
    2.17  #define gl_convert	(this->gl_data->ConvertSurface)
    2.18  #define gl_copyshadow	(this->gl_data->CopyShadow)
    2.19 +#define gl_curformat	(this->gl_data->format)
    2.20 +#define gl_curdepth		(this->gl_data->depth)
    2.21 +#define gl_curstencil	(this->gl_data->stencil)
    2.22 +#define gl_curaccum		(this->gl_data->accum)
    2.23 +#define gl_curwidth	(this->gl_data->width)
    2.24 +#define gl_curheight		(this->gl_data->height)
    2.25  
    2.26  /* OpenGL functions */
    2.27  extern int SDL_AtariGL_Init(_THIS, SDL_Surface *current);
    2.28 -extern void SDL_AtariGL_Quit(_THIS);
    2.29 +extern void SDL_AtariGL_Quit(_THIS, SDL_bool unload);
    2.30  extern void SDL_AtariGL_InitPointers(_THIS);
    2.31  
    2.32  extern int SDL_AtariGL_LoadLibrary(_THIS, const char *path);
     3.1 --- a/src/video/gem/SDL_gemvideo.c	Fri Nov 26 16:16:50 2004 +0000
     3.2 +++ b/src/video/gem/SDL_gemvideo.c	Fri Nov 26 22:11:30 2004 +0000
     3.3 @@ -475,12 +475,6 @@
     3.4  
     3.5  static void GEM_FreeBuffers(_THIS)
     3.6  {
     3.7 -#ifdef HAVE_OPENGL
     3.8 -	if (gl_active) {
     3.9 -		SDL_AtariGL_Quit(this);
    3.10 -	}
    3.11 -#endif
    3.12 -
    3.13  	/* Release buffer */
    3.14  	if ( GEM_buffer2 ) {
    3.15  		free( GEM_buffer2 );
    3.16 @@ -1047,6 +1041,12 @@
    3.17  
    3.18  	GEM_FreeBuffers(this);
    3.19  
    3.20 +#ifdef HAVE_OPENGL
    3.21 +	if (gl_active) {
    3.22 +		SDL_AtariGL_Quit(this, SDL_TRUE);
    3.23 +	}
    3.24 +#endif
    3.25 +
    3.26  	/* Destroy window */
    3.27  	if (GEM_handle>=0) {
    3.28  		wind_close(GEM_handle);
     4.1 --- a/src/video/xbios/SDL_xbios.c	Fri Nov 26 16:16:50 2004 +0000
     4.2 +++ b/src/video/xbios/SDL_xbios.c	Fri Nov 26 22:11:30 2004 +0000
     4.3 @@ -434,12 +434,6 @@
     4.4  {
     4.5  	int i;
     4.6  
     4.7 -#ifdef HAVE_OPENGL
     4.8 -	if (gl_active) {
     4.9 -		SDL_AtariGL_Quit(this);
    4.10 -	}
    4.11 -#endif
    4.12 -
    4.13  	for (i=0;i<2;i++) {
    4.14  		if (XBIOS_screensmem[i]!=NULL) {
    4.15  			Mfree(XBIOS_screensmem[i]);
    4.16 @@ -859,6 +853,13 @@
    4.17  	Vsync();
    4.18  #endif
    4.19  
    4.20 +
    4.21 +#ifdef HAVE_OPENGL
    4.22 +	if (gl_active) {
    4.23 +		SDL_AtariGL_Quit(this, SDL_TRUE);
    4.24 +	}
    4.25 +#endif
    4.26 +
    4.27  	if (XBIOS_oldpalette) {
    4.28  		free(XBIOS_oldpalette);
    4.29  		XBIOS_oldpalette=NULL;