src/video/ataricommon/SDL_atarigl.c
changeset 991 12b13601a544
parent 989 475166d13b44
child 992 0324ce32b2d9
equal deleted inserted replaced
990:8e20c48a9c13 991:12b13601a544
    22 
    22 
    23 /* Atari OSMesa.ldg implementation of SDL OpenGL support */
    23 /* Atari OSMesa.ldg implementation of SDL OpenGL support */
    24 
    24 
    25 /*--- Includes ---*/
    25 /*--- Includes ---*/
    26 
    26 
       
    27 #include <stdio.h>
       
    28 #include <stdlib.h>
       
    29 #include <string.h>
    27 #ifdef HAVE_OPENGL
    30 #ifdef HAVE_OPENGL
    28 #include <GL/osmesa.h>
    31 #include <GL/osmesa.h>
    29 #endif
    32 #endif
       
    33 
       
    34 #include <mint/osbind.h>
    30 
    35 
    31 #include "SDL_video.h"
    36 #include "SDL_video.h"
    32 #include "SDL_error.h"
    37 #include "SDL_error.h"
    33 #include "SDL_endian.h"
    38 #include "SDL_endian.h"
    34 #include "SDL_atarigl_c.h"
    39 #include "SDL_atarigl_c.h"
    35 
    40 #ifdef ENABLE_OSMESA_SHARED
    36 /*--- Variables ---*/
    41 #include "SDL_loadso.h"
       
    42 #endif
       
    43 
       
    44 /*--- Defines ---*/
       
    45 
       
    46 #define PATH_OSMESA_LDG	"osmesa.ldg"
       
    47 #define PATH_MESAGL_LDG	"mesa_gl.ldg"
       
    48 #define PATH_TINYGL_LDG	"tiny_gl.ldg"
    37 
    49 
    38 /*--- Functions prototypes ---*/
    50 /*--- Functions prototypes ---*/
    39 
    51 
    40 static void ConvertNull(SDL_Surface *surface);
    52 static void SDL_AtariGL_UnloadLibrary(_THIS);
    41 static void Convert565To555be(SDL_Surface *surface);
    53 
    42 static void Convert565To555le(SDL_Surface *surface);
    54 static void CopyShadowNull(_THIS, SDL_Surface *surface);
    43 static void Convert565le(SDL_Surface *surface);
    55 static void CopyShadowDirect(_THIS, SDL_Surface *surface);
    44 static void ConvertBGRAToABGR(SDL_Surface *surface);
    56 static void CopyShadow8888To555(_THIS, SDL_Surface *surface);
       
    57 static void CopyShadow8888To565(_THIS, SDL_Surface *surface);
       
    58 
       
    59 static void ConvertNull(_THIS, SDL_Surface *surface);
       
    60 static void Convert565To555be(_THIS, SDL_Surface *surface);
       
    61 static void Convert565To555le(_THIS, SDL_Surface *surface);
       
    62 static void Convert565le(_THIS, SDL_Surface *surface);
       
    63 static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface);
       
    64 
       
    65 static int InitNew(_THIS, SDL_Surface *current);
       
    66 static int InitOld(_THIS, SDL_Surface *current);
    45 
    67 
    46 /*--- Public functions ---*/
    68 /*--- Public functions ---*/
    47 
    69 
    48 int SDL_AtariGL_Init(_THIS, SDL_Surface *current)
    70 int SDL_AtariGL_Init(_THIS, SDL_Surface *current)
    49 {
    71 {
    50 #ifdef HAVE_OPENGL
    72 #ifdef HAVE_OPENGL
       
    73 	if (gl_oldmesa) {
       
    74 		gl_active = InitOld(this, current);		
       
    75 	} else {
       
    76 		gl_active = InitNew(this, current);		
       
    77 	}
       
    78 #endif
       
    79 
       
    80 	return (gl_active);
       
    81 }
       
    82 
       
    83 void SDL_AtariGL_Quit(_THIS)
       
    84 {
       
    85 #ifdef HAVE_OPENGL
       
    86 	if (!gl_active) {
       
    87 		return;
       
    88 	}
       
    89 
       
    90 	if (gl_oldmesa) {
       
    91 		/* Old mesa implementations */
       
    92 		if (this->gl_data->OSMesaDestroyLDG) {
       
    93 			this->gl_data->OSMesaDestroyLDG();
       
    94 		}
       
    95 		if (gl_shadow) {
       
    96 			Mfree(gl_shadow);
       
    97 			gl_shadow = NULL;
       
    98 		}
       
    99 	} else {
       
   100 		/* New mesa implementation */
       
   101 		if (gl_ctx) {
       
   102 			if (this->gl_data->OSMesaDestroyContext) {
       
   103 				this->gl_data->OSMesaDestroyContext(gl_ctx);
       
   104 			}
       
   105 			gl_ctx = NULL;
       
   106 		}
       
   107 	}
       
   108 
       
   109 	SDL_AtariGL_UnloadLibrary(this);
       
   110 
       
   111 #endif /* HAVE_OPENGL */
       
   112 	gl_active = 0;
       
   113 }
       
   114 
       
   115 int SDL_AtariGL_LoadLibrary(_THIS, const char *path)
       
   116 {
       
   117 #ifdef HAVE_OPENGL
       
   118 
       
   119 #ifdef ENABLE_OSMESA_SHARED
       
   120 	void *handle;
       
   121 
       
   122 	if (gl_active) {
       
   123 		SDL_SetError("OpenGL context already created");
       
   124 		return -1;
       
   125 	}
       
   126 
       
   127 	/* Unload previous driver */
       
   128 	SDL_AtariGL_UnloadLibrary(this);
       
   129 
       
   130 	/* Load library given by path */
       
   131 	handle = SDL_LoadObject(path);
       
   132 	if (handle == NULL) {
       
   133 		/* Try to load another one */
       
   134 		path = getenv("SDL_VIDEO_GL_DRIVER");
       
   135 		if ( path != NULL ) {
       
   136 			handle = SDL_LoadObject(path);
       
   137 		}
       
   138 
       
   139 		/* If it does not work, try some other */
       
   140 		if (handle == NULL) {
       
   141 			path = PATH_OSMESA_LDG;
       
   142 			handle = SDL_LoadObject(path);
       
   143 		}
       
   144 
       
   145 		if (handle == NULL) {
       
   146 			path = PATH_MESAGL_LDG;
       
   147 			handle = SDL_LoadObject(path);
       
   148 		}
       
   149 
       
   150 		if (handle == NULL) {
       
   151 			path = PATH_TINYGL_LDG;
       
   152 			handle = SDL_LoadObject(path);
       
   153 		}
       
   154 	}
       
   155 
       
   156 	if (handle == NULL) {
       
   157 		SDL_SetError("Could not load OpenGL library");
       
   158 		return -1;
       
   159 	}
       
   160 
       
   161 	/* Load functions pointers (osmesa.ldg) */
       
   162 	this->gl_data->OSMesaCreateContextExt = SDL_LoadFunction(handle, "OSMesaCreateContextExt");
       
   163 	this->gl_data->OSMesaDestroyContext = SDL_LoadFunction(handle, "OSMesaDestroyContext");
       
   164 	this->gl_data->OSMesaMakeCurrent = SDL_LoadFunction(handle, "OSMesaMakeCurrent");
       
   165 	this->gl_data->OSMesaPixelStore = SDL_LoadFunction(handle, "OSMesaPixelStore");
       
   166 	this->gl_data->OSMesaGetProcAddress = SDL_LoadFunction(handle, "OSMesaGetProcAddress");
       
   167 	this->gl_data->glGetIntegerv = SDL_LoadFunction(handle, "glGetIntegerv");
       
   168 
       
   169 	/* Load old functions pointers (mesa_gl.ldg, tiny_gl.ldg) */
       
   170 	this->gl_data->OSMesaCreateLDG = SDL_LoadFunction(handle, "OSMesaCreateLDG");
       
   171 	this->gl_data->OSMesaDestroyLDG = SDL_LoadFunction(handle, "OSMesaDestroyLDG");
       
   172 
       
   173 	gl_oldmesa = 0;
       
   174 
       
   175 	if ( (this->gl_data->OSMesaCreateContextExt == NULL) || 
       
   176 	     (this->gl_data->OSMesaDestroyContext == NULL) ||
       
   177 	     (this->gl_data->OSMesaMakeCurrent == NULL) ||
       
   178 	     (this->gl_data->OSMesaPixelStore == NULL) ||
       
   179 	     (this->gl_data->glGetIntegerv == NULL) ||
       
   180 	     (this->gl_data->OSMesaGetProcAddress == NULL)) {
       
   181 		/* Hum, maybe old library ? */
       
   182 		if ( (this->gl_data->OSMesaCreateLDG == NULL) || 
       
   183 		     (this->gl_data->OSMesaDestroyLDG == NULL)) {
       
   184 			SDL_SetError("Could not retrieve OpenGL functions");
       
   185 			return -1;
       
   186 		} else {
       
   187 			gl_oldmesa = 1;
       
   188 		}
       
   189 	}
       
   190 
       
   191 	this->gl_config.dll_handle = handle;
       
   192 	if ( path ) {
       
   193 		strncpy(this->gl_config.driver_path, path,
       
   194 			sizeof(this->gl_config.driver_path)-1);
       
   195 	} else {
       
   196 		strcpy(this->gl_config.driver_path, "");
       
   197 	}
       
   198 
       
   199 #endif
       
   200 	this->gl_config.driver_loaded = 1;
       
   201 
       
   202 	return 0;
       
   203 #else
       
   204 	return -1;
       
   205 #endif
       
   206 }
       
   207 
       
   208 void *SDL_AtariGL_GetProcAddress(_THIS, const char *proc)
       
   209 {
       
   210 	void *func = NULL;
       
   211 #ifdef HAVE_OPENGL
       
   212 
       
   213 	if (this->gl_config.dll_handle) {
       
   214 		func = SDL_LoadFunction(this->gl_config.dll_handle, (void *)proc);
       
   215 	} else if (this->gl_data->OSMesaGetProcAddress) {
       
   216 		func = this->gl_data->OSMesaGetProcAddress(proc);
       
   217 	}
       
   218 
       
   219 #endif
       
   220 	return func;
       
   221 }
       
   222 
       
   223 int SDL_AtariGL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
       
   224 {
       
   225 #ifdef HAVE_OPENGL
       
   226 	GLenum mesa_attrib;
       
   227 	SDL_Surface *surface;
       
   228 
       
   229 	if (this->gl_config.dll_handle) {
       
   230 		if (this->gl_data->glGetIntegerv == NULL) {
       
   231 			return -1;
       
   232 		}
       
   233 	}
       
   234 
       
   235 	if (!gl_active) {
       
   236 		return -1;
       
   237 	}
       
   238 
       
   239 	switch(attrib) {
       
   240 		case SDL_GL_RED_SIZE:
       
   241 			mesa_attrib = GL_RED_BITS;
       
   242 			break;
       
   243 		case SDL_GL_GREEN_SIZE:
       
   244 			mesa_attrib = GL_GREEN_BITS;
       
   245 			break;
       
   246 		case SDL_GL_BLUE_SIZE:
       
   247 			mesa_attrib = GL_BLUE_BITS;
       
   248 			break;
       
   249 		case SDL_GL_ALPHA_SIZE:
       
   250 			mesa_attrib = GL_ALPHA_BITS;
       
   251 			break;
       
   252 		case SDL_GL_DOUBLEBUFFER:
       
   253 			surface = this->screen;
       
   254 			*value = ((surface->flags & SDL_DOUBLEBUF)==SDL_DOUBLEBUF);
       
   255 			return 0;
       
   256 		case SDL_GL_DEPTH_SIZE:
       
   257 			mesa_attrib = GL_DEPTH_BITS;
       
   258 			break;
       
   259 		case SDL_GL_STENCIL_SIZE:
       
   260 			mesa_attrib = GL_STENCIL_BITS;
       
   261 			break;
       
   262 		case SDL_GL_ACCUM_RED_SIZE:
       
   263 			mesa_attrib = GL_ACCUM_RED_BITS;
       
   264 			break;
       
   265 		case SDL_GL_ACCUM_GREEN_SIZE:
       
   266 			mesa_attrib = GL_ACCUM_GREEN_BITS;
       
   267 			break;
       
   268 		case SDL_GL_ACCUM_BLUE_SIZE:
       
   269 			mesa_attrib = GL_ACCUM_BLUE_BITS;
       
   270 			break;
       
   271 		case SDL_GL_ACCUM_ALPHA_SIZE:
       
   272 			mesa_attrib = GL_ACCUM_ALPHA_BITS;
       
   273 			break;
       
   274 		default :
       
   275 			return -1;
       
   276 	}
       
   277 
       
   278 	this->gl_data->glGetIntegerv(mesa_attrib, value);
       
   279 	return 0;
       
   280 #else
       
   281 	return -1;
       
   282 #endif
       
   283 }
       
   284 
       
   285 int SDL_AtariGL_MakeCurrent(_THIS)
       
   286 {
       
   287 #ifdef HAVE_OPENGL
       
   288 	SDL_Surface *surface;
       
   289 	GLenum type;
       
   290 
       
   291 	if (gl_oldmesa && gl_active) {
       
   292 		return 0;
       
   293 	}
       
   294 
       
   295 	if (this->gl_config.dll_handle) {
       
   296 		if ((this->gl_data->OSMesaMakeCurrent == NULL) ||
       
   297 			(this->gl_data->OSMesaPixelStore == NULL)) {
       
   298 			return -1;
       
   299 		}
       
   300 	}
       
   301 
       
   302 	if (!gl_active) {
       
   303 		SDL_SetError("Invalid OpenGL context");
       
   304 		return -1;
       
   305 	}
       
   306 
       
   307 	surface = this->screen;
       
   308 	
       
   309 	if ((surface->format->BitsPerPixel == 15) || (surface->format->BitsPerPixel == 16)) {
       
   310 		type = GL_UNSIGNED_SHORT_5_6_5;
       
   311 	} else {
       
   312 		type = GL_UNSIGNED_BYTE;
       
   313 	}
       
   314 
       
   315 	if (!(this->gl_data->OSMesaMakeCurrent(gl_ctx, surface->pixels, type, surface->w, surface->h))) {
       
   316 		SDL_SetError("Can not make OpenGL context current");
       
   317 		return -1;
       
   318 	}
       
   319 
       
   320 	/* OSMesa draws upside down */
       
   321 	this->gl_data->OSMesaPixelStore(OSMESA_Y_UP, 0);
       
   322 
       
   323 	return 0;
       
   324 #else
       
   325 	return -1;
       
   326 #endif
       
   327 }
       
   328 
       
   329 void SDL_AtariGL_SwapBuffers(_THIS)
       
   330 {
       
   331 #ifdef HAVE_OPENGL
       
   332 	if (gl_active) {
       
   333 		gl_copyshadow(this, this->screen);
       
   334 		gl_convert(this, this->screen);
       
   335 	}
       
   336 #endif
       
   337 }
       
   338 
       
   339 void SDL_AtariGL_InitPointers(_THIS)
       
   340 {
       
   341 #if defined(HAVE_OPENGL)
       
   342 	this->gl_data->OSMesaCreateContextExt = OSMesaCreateContextExt;
       
   343 	this->gl_data->OSMesaDestroyContext = OSMesaDestroyContext;
       
   344 	this->gl_data->OSMesaMakeCurrent = OSMesaMakeCurrent;
       
   345 	this->gl_data->OSMesaPixelStore = OSMesaPixelStore;
       
   346 	this->gl_data->OSMesaGetProcAddress = OSMesaGetProcAddress;
       
   347 	this->gl_data->glGetIntegerv = glGetIntegerv;
       
   348 #endif
       
   349 }
       
   350 
       
   351 /*--- Private functions ---*/
       
   352 
       
   353 static void SDL_AtariGL_UnloadLibrary(_THIS)
       
   354 {
       
   355 #if defined(HAVE_OPENGL)
       
   356 	if (this->gl_config.dll_handle) {
       
   357 		SDL_UnloadObject(this->gl_config.dll_handle);
       
   358 		this->gl_config.dll_handle = NULL;
       
   359 
       
   360 		/* Restore pointers to static library */
       
   361 		this->gl_data->OSMesaCreateContextExt = OSMesaCreateContextExt;
       
   362 		this->gl_data->OSMesaDestroyContext = OSMesaDestroyContext;
       
   363 		this->gl_data->OSMesaMakeCurrent = OSMesaMakeCurrent;
       
   364 		this->gl_data->OSMesaPixelStore = OSMesaPixelStore;
       
   365 		this->gl_data->OSMesaGetProcAddress = OSMesaGetProcAddress;
       
   366 		this->gl_data->glGetIntegerv = glGetIntegerv;
       
   367 
       
   368 		this->gl_data->OSMesaCreateLDG = NULL;
       
   369 		this->gl_data->OSMesaDestroyLDG = NULL;
       
   370 	}
       
   371 #endif
       
   372 }
       
   373 
       
   374 /*--- Creation of an OpenGL context using new/old functions ---*/
       
   375 
       
   376 static int InitNew(_THIS, SDL_Surface *current)
       
   377 {
    51 	GLenum osmesa_format;
   378 	GLenum osmesa_format;
    52 	SDL_PixelFormat *pixel_format;
   379 	SDL_PixelFormat *pixel_format;
    53 	Uint32	redmask;
   380 	Uint32	redmask;
    54 
   381 
    55 	SDL_AtariGL_Quit(this);	/* Destroy previous context if exist */
   382 	if (this->gl_config.dll_handle) {
       
   383 		if (this->gl_data->OSMesaCreateContextExt == NULL) {
       
   384 			return 0;
       
   385 		}
       
   386 	}
    56 
   387 
    57 	/* Init OpenGL context using OSMesa */
   388 	/* Init OpenGL context using OSMesa */
    58 	gl_convert = ConvertNull;
   389 	gl_convert = ConvertNull;
       
   390 	gl_copyshadow = CopyShadowNull;
    59 
   391 
    60 	pixel_format = current->format;
   392 	pixel_format = current->format;
    61 	redmask = pixel_format->Rmask;
   393 	redmask = pixel_format->Rmask;
    62 	switch (pixel_format->BitsPerPixel) {
   394 	switch (pixel_format->BitsPerPixel) {
    63 		case 15:
   395 		case 15:
    64 			/* 1555, big and little endian, unsupported */
   396 			/* 1555, big and little endian, unsupported */
       
   397 			gl_pixelsize = 2;
    65 			osmesa_format = OSMESA_RGB_565;
   398 			osmesa_format = OSMESA_RGB_565;
    66 			if (redmask == 31<<10) {
   399 			if (redmask == 31<<10) {
    67 				gl_convert = Convert565To555be;
   400 				gl_convert = Convert565To555be;
    68 			} else {
   401 			} else {
    69 				gl_convert = Convert565To555le;
   402 				gl_convert = Convert565To555le;
    70 			}
   403 			}
    71 			break;
   404 			break;
    72 		case 16:
   405 		case 16:
       
   406 			gl_pixelsize = 2;
    73 			if (redmask == 31<<11) {
   407 			if (redmask == 31<<11) {
    74 				osmesa_format = OSMESA_RGB_565;
   408 				osmesa_format = OSMESA_RGB_565;
    75 			} else {
   409 			} else {
    76 				/* 565, little endian, unsupported */
   410 				/* 565, little endian, unsupported */
    77 				osmesa_format = OSMESA_RGB_565;
   411 				osmesa_format = OSMESA_RGB_565;
    78 				gl_convert = Convert565le;
   412 				gl_convert = Convert565le;
    79 			}
   413 			}
    80 			break;
   414 			break;
    81 		case 24:
   415 		case 24:
       
   416 			gl_pixelsize = 3;
    82 			if (redmask == 255<<16) {
   417 			if (redmask == 255<<16) {
    83 				osmesa_format = OSMESA_RGB;
   418 				osmesa_format = OSMESA_RGB;
    84 			} else {
   419 			} else {
    85 				osmesa_format = OSMESA_BGR;
   420 				osmesa_format = OSMESA_BGR;
    86 			}
   421 			}
    87 			break;
   422 			break;
    88 		case 32:
   423 		case 32:
       
   424 			gl_pixelsize = 4;
    89 			if (redmask == 255<<16) {
   425 			if (redmask == 255<<16) {
    90 				osmesa_format = OSMESA_ARGB;
   426 				osmesa_format = OSMESA_ARGB;
    91 			} else if (redmask == 255<<8) {
   427 			} else if (redmask == 255<<8) {
    92 				osmesa_format = OSMESA_BGRA;
   428 				osmesa_format = OSMESA_BGRA;
    93 			} else if (redmask == 255<<24) {
   429 			} else if (redmask == 255<<24) {
    97 				osmesa_format = OSMESA_BGRA;
   433 				osmesa_format = OSMESA_BGRA;
    98 				gl_convert = ConvertBGRAToABGR;
   434 				gl_convert = ConvertBGRAToABGR;
    99 			}
   435 			}
   100 			break;
   436 			break;
   101 		default:
   437 		default:
       
   438 			gl_pixelsize = 1;
   102 			osmesa_format = OSMESA_COLOR_INDEX;
   439 			osmesa_format = OSMESA_COLOR_INDEX;
   103 			break;
   440 			break;
   104 	}
   441 	}
   105 
   442 
   106 	gl_ctx = OSMesaCreateContextExt( osmesa_format, this->gl_config.depth_size,
   443 	gl_ctx = this->gl_data->OSMesaCreateContextExt(
       
   444 		osmesa_format, this->gl_config.depth_size,
   107 		this->gl_config.stencil_size, this->gl_config.accum_red_size +
   445 		this->gl_config.stencil_size, this->gl_config.accum_red_size +
   108 		this->gl_config.accum_green_size + this->gl_config.accum_blue_size +
   446 		this->gl_config.accum_green_size + this->gl_config.accum_blue_size +
   109 		this->gl_config.accum_alpha_size, NULL );
   447 		this->gl_config.accum_alpha_size, NULL );
   110 
   448 
   111 	gl_active = (gl_ctx != NULL);
   449 	return (gl_ctx != NULL);
   112 	return (gl_active);
   450 }
   113 #else
   451 
   114 	return 0;
   452 static int InitOld(_THIS, SDL_Surface *current)
   115 #endif
   453 {
   116 }
   454 	GLenum osmesa_format;
   117 
   455 	SDL_PixelFormat *pixel_format;
   118 void SDL_AtariGL_Quit(_THIS)
   456 	Uint32	redmask;
   119 {
   457 
   120 #ifdef HAVE_OPENGL
   458 	if (this->gl_config.dll_handle) {
   121 	/* Shutdown OpenGL context */
   459 		if (this->gl_data->OSMesaCreateLDG == NULL) {
   122 	if (gl_ctx) {
       
   123 		OSMesaDestroyContext(gl_ctx);
       
   124 		gl_ctx = NULL;
       
   125 	}
       
   126 #endif
       
   127 	gl_active = 0;
       
   128 }
       
   129 
       
   130 int SDL_AtariGL_LoadLibrary(_THIS, const char *path)
       
   131 {
       
   132 #ifdef HAVE_OPENGL
       
   133 	/* Library is always opened */
       
   134 	this->gl_config.driver_loaded = 1;
       
   135 #endif
       
   136 	return 0;
       
   137 }
       
   138 
       
   139 void *SDL_AtariGL_GetProcAddress(_THIS, const char *proc)
       
   140 {
       
   141 	void *func = NULL;
       
   142 #ifdef HAVE_OPENGL
       
   143 	if (gl_ctx != NULL) {
       
   144 		func = OSMesaGetProcAddress(proc);
       
   145 	}
       
   146 #endif
       
   147 	return func;
       
   148 }
       
   149 
       
   150 int SDL_AtariGL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
       
   151 {
       
   152 #ifdef HAVE_OPENGL
       
   153 	GLenum mesa_attrib;
       
   154 	SDL_Surface *surface;
       
   155 
       
   156 	if (gl_ctx == NULL) {
       
   157 		return -1;
       
   158 	}
       
   159 
       
   160 	switch(attrib) {
       
   161 		case SDL_GL_RED_SIZE:
       
   162 			mesa_attrib = GL_RED_BITS;
       
   163 			break;
       
   164 		case SDL_GL_GREEN_SIZE:
       
   165 			mesa_attrib = GL_GREEN_BITS;
       
   166 			break;
       
   167 		case SDL_GL_BLUE_SIZE:
       
   168 			mesa_attrib = GL_BLUE_BITS;
       
   169 			break;
       
   170 		case SDL_GL_ALPHA_SIZE:
       
   171 			mesa_attrib = GL_ALPHA_BITS;
       
   172 			break;
       
   173 		case SDL_GL_DOUBLEBUFFER:
       
   174 			surface = this->screen;
       
   175 			*value = ((surface->flags & SDL_DOUBLEBUF)==SDL_DOUBLEBUF);
       
   176 			return 0;
   460 			return 0;
   177 		case SDL_GL_DEPTH_SIZE:
   461 		}
   178 			mesa_attrib = GL_DEPTH_BITS;
   462 	}
   179 			break;
   463 
   180 		case SDL_GL_STENCIL_SIZE:
   464 	/* Init OpenGL context using OSMesa */
   181 			mesa_attrib = GL_STENCIL_BITS;
   465 	gl_convert = ConvertNull;
   182 			break;
   466 	gl_copyshadow = CopyShadowNull;
   183 		case SDL_GL_ACCUM_RED_SIZE:
   467 
   184 			mesa_attrib = GL_ACCUM_RED_BITS;
   468 	pixel_format = current->format;
   185 			break;
   469 	redmask = pixel_format->Rmask;
   186 		case SDL_GL_ACCUM_GREEN_SIZE:
   470 	switch (pixel_format->BitsPerPixel) {
   187 			mesa_attrib = GL_ACCUM_GREEN_BITS;
   471 		case 15:
   188 			break;
   472 			/* 15 bits unsupported */
   189 		case SDL_GL_ACCUM_BLUE_SIZE:
   473 			gl_pixelsize = 2;
   190 			mesa_attrib = GL_ACCUM_BLUE_BITS;
   474 			osmesa_format = OSMESA_ARGB;
   191 			break;
   475 			if (redmask == 31<<10) {
   192 		case SDL_GL_ACCUM_ALPHA_SIZE:
   476 				gl_copyshadow = CopyShadow8888To555;
   193 			mesa_attrib = GL_ACCUM_ALPHA_BITS;
   477 			} else {
   194 			break;
   478 				gl_copyshadow = CopyShadow8888To565;
   195 		default :
   479 				gl_convert = Convert565To555le;
   196 			return -1;
   480 			}
   197 	}
   481 			break;
   198 
   482 		case 16:
   199 	glGetIntegerv(mesa_attrib, value);
   483 			/* 16 bits unsupported */
   200 	return 0;
   484 			gl_pixelsize = 2;
   201 #else
   485 			osmesa_format = OSMESA_ARGB;
   202 	return -1;
   486 			gl_copyshadow = CopyShadow8888To565;
   203 #endif
   487 			if (redmask != 31<<11) {
   204 }
   488 				/* 565, little endian, unsupported */
   205 
   489 				gl_convert = Convert565le;
   206 int SDL_AtariGL_MakeCurrent(_THIS)
   490 			}
   207 {
   491 			break;
   208 #ifdef HAVE_OPENGL
   492 		case 24:
   209 	SDL_Surface *surface;
   493 			gl_pixelsize = 3;
   210 	GLenum type;
   494 			gl_copyshadow = CopyShadowDirect;
   211 
   495 			if (redmask == 255<<16) {
   212 	if (gl_ctx == NULL) {
   496 				osmesa_format = OSMESA_RGB;
   213 		SDL_SetError("Invalid OpenGL context");
   497 			} else {
   214 		return -1;
   498 				osmesa_format = OSMESA_BGR;
   215 	}
   499 			}
   216 
   500 			break;
   217 	surface = this->screen;
   501 		case 32:
   218 	
   502 			gl_pixelsize = 4;
   219 	if ((surface->format->BitsPerPixel == 15) || (surface->format->BitsPerPixel == 16)) {
   503 			gl_copyshadow = CopyShadowDirect;
   220 		type = GL_UNSIGNED_SHORT_5_6_5;
   504 			if (redmask == 255<<16) {
   221 	} else {
   505 				osmesa_format = OSMESA_ARGB;
   222 		type = GL_UNSIGNED_BYTE;
   506 			} else if (redmask == 255<<8) {
   223 	}
   507 				osmesa_format = OSMESA_BGRA;
   224 
   508 			} else if (redmask == 255<<24) {
   225 	if (!OSMesaMakeCurrent(gl_ctx, surface->pixels, type, surface->w, surface->h)) {
   509 				osmesa_format = OSMESA_RGBA;
   226 		SDL_SetError("Can not make OpenGL context current");
   510 			} else {
   227 		return -1;
   511 				/* ABGR format unsupported */
   228 	}
   512 				osmesa_format = OSMESA_BGRA;
   229 
   513 				gl_convert = ConvertBGRAToABGR;
   230 	/* OSMesa draws upside down */
   514 			}
   231 	OSMesaPixelStore(OSMESA_Y_UP, 0);
   515 			break;
   232 
   516 		default:
   233 	return 0;
   517 			gl_pixelsize = 1;
   234 #else
   518 			gl_copyshadow = CopyShadowDirect;
   235 	return -1;
   519 			osmesa_format = OSMESA_COLOR_INDEX;
   236 #endif
   520 			break;
   237 }
   521 	}
   238 
   522 
   239 void SDL_AtariGL_SwapBuffers(_THIS)
   523 	gl_shadow = this->gl_data->OSMesaCreateLDG(
   240 {
   524 		osmesa_format, GL_UNSIGNED_BYTE, current->w, current->h
   241 #ifdef HAVE_OPENGL
   525 	);
   242 	if (gl_ctx == NULL) {
   526 
   243 		return;
   527 	return (gl_shadow != NULL);
   244 	}
   528 }
   245 
   529 
   246 	gl_convert(this->screen);
   530 /*--- Conversions routines from shadow buffer to the screen ---*/
   247 #endif
   531 
   248 }
   532 static void CopyShadowNull(_THIS, SDL_Surface *surface)
   249 
   533 {
   250 /*--- Private functions ---*/
   534 }
   251 
   535 
   252 static void ConvertNull(SDL_Surface *surface)
   536 static void CopyShadowDirect(_THIS, SDL_Surface *surface)
   253 {
   537 {
   254 }
   538 	int y, srcpitch, dstpitch;
   255 
   539 	Uint8 *srcline, *dstline;
   256 static void Convert565To555be(SDL_Surface *surface)
   540 
       
   541 	srcline = gl_shadow;
       
   542 	srcpitch = surface->w * gl_pixelsize;
       
   543 	dstline = surface->pixels;
       
   544 	dstpitch = surface->pitch;
       
   545 
       
   546 	for (y=0; y<surface->h; y++) {
       
   547 		memcpy(dstline, srcline, srcpitch);
       
   548 
       
   549 		srcline += srcpitch;
       
   550 		dstline += dstpitch;
       
   551 	}
       
   552 }
       
   553 
       
   554 static void CopyShadow8888To555(_THIS, SDL_Surface *surface)
       
   555 {
       
   556 	int x,y, srcpitch, dstpitch;
       
   557 	Uint16 *dstline, *dstcol;
       
   558 	Uint32 *srcline, *srccol;
       
   559 
       
   560 	srcline = (Uint32 *)gl_shadow;
       
   561 	srcpitch = surface->w;
       
   562 	dstline = surface->pixels;
       
   563 	dstpitch = surface->pitch >>1;
       
   564 
       
   565 	for (y=0; y<surface->h; y++) {
       
   566 		srccol = srcline;
       
   567 		dstcol = dstline;
       
   568 		for (x=0; x<surface->w; x++) {
       
   569 			Uint32 srccolor;
       
   570 			Uint16 dstcolor;
       
   571 			
       
   572 			srccolor = *srccol++;
       
   573 			dstcolor = (srccolor>>9) & (31<<10);
       
   574 			dstcolor |= (srccolor>>6) & (31<<5);
       
   575 			dstcolor |= (srccolor>>3) & 31;
       
   576 			*dstcol++ = dstcolor;
       
   577 		}
       
   578 
       
   579 		srcline += srcpitch;
       
   580 		dstline += dstpitch;
       
   581 	}
       
   582 }
       
   583 
       
   584 static void CopyShadow8888To565(_THIS, SDL_Surface *surface)
       
   585 {
       
   586 	int x,y, srcpitch, dstpitch;
       
   587 	Uint16 *dstline, *dstcol;
       
   588 	Uint32 *srcline, *srccol;
       
   589 
       
   590 	srcline = (Uint32 *)gl_shadow;
       
   591 	srcpitch = surface->w;
       
   592 	dstline = surface->pixels;
       
   593 	dstpitch = surface->pitch >>1;
       
   594 
       
   595 	for (y=0; y<surface->h; y++) {
       
   596 		srccol = srcline;
       
   597 		dstcol = dstline;
       
   598 
       
   599 		for (x=0; x<surface->w; x++) {
       
   600 			Uint32 srccolor;
       
   601 			Uint16 dstcolor;
       
   602 			
       
   603 			srccolor = *srccol++;
       
   604 			dstcolor = (srccolor>>8) & (31<<11);
       
   605 			dstcolor |= (srccolor>>5) & (63<<5);
       
   606 			dstcolor |= (srccolor>>3) & 31;
       
   607 			*dstcol++ = dstcolor;
       
   608 		}
       
   609 
       
   610 		srcline += srcpitch;
       
   611 		dstline += dstpitch;
       
   612 	}
       
   613 }
       
   614 
       
   615 /*--- Conversions routines in the screen ---*/
       
   616 
       
   617 static void ConvertNull(_THIS, SDL_Surface *surface)
       
   618 {
       
   619 }
       
   620 
       
   621 static void Convert565To555be(_THIS, SDL_Surface *surface)
   257 {
   622 {
   258 	int x,y, pitch;
   623 	int x,y, pitch;
   259 	unsigned short *line, *pixel;
   624 	unsigned short *line, *pixel;
   260 
   625 
   261 	line = surface->pixels;
   626 	line = surface->pixels;
   270 
   635 
   271 		line += pitch;
   636 		line += pitch;
   272 	}
   637 	}
   273 }
   638 }
   274 
   639 
   275 static void Convert565To555le(SDL_Surface *surface)
   640 static void Convert565To555le(_THIS, SDL_Surface *surface)
   276 {
   641 {
   277 	int x,y, pitch;
   642 	int x,y, pitch;
   278 	unsigned short *line, *pixel;
   643 	unsigned short *line, *pixel;
   279 
   644 
   280 	line = surface->pixels;
   645 	line = surface->pixels;
   290 
   655 
   291 		line += pitch;
   656 		line += pitch;
   292 	}
   657 	}
   293 }
   658 }
   294 
   659 
   295 static void Convert565le(SDL_Surface *surface)
   660 static void Convert565le(_THIS, SDL_Surface *surface)
   296 {
   661 {
   297 	int x,y, pitch;
   662 	int x,y, pitch;
   298 	unsigned short *line, *pixel;
   663 	unsigned short *line, *pixel;
   299 
   664 
   300 	line = surface->pixels;
   665 	line = surface->pixels;
   309 
   674 
   310 		line += pitch;
   675 		line += pitch;
   311 	}
   676 	}
   312 }
   677 }
   313 
   678 
   314 static void ConvertBGRAToABGR(SDL_Surface *surface)
   679 static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface)
   315 {
   680 {
   316 	int x,y, pitch;
   681 	int x,y, pitch;
   317 	unsigned long *line, *pixel;
   682 	unsigned long *line, *pixel;
   318 
   683 
   319 	line = surface->pixels;
   684 	line = surface->pixels;