src/video/psp/SDL_pspgl.c
changeset 7009 161b7b6a5303
child 7037 3fedf1f25b94
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/psp/SDL_pspgl.c	Sun Mar 17 20:07:02 2013 +0800
     1.3 @@ -0,0 +1,207 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#include <stdlib.h>
    1.26 +#include <string.h>
    1.27 +
    1.28 +#include "SDL_error.h"
    1.29 +#include "SDL_pspvideo.h"
    1.30 +#include "SDL_pspgl_c.h"
    1.31 +
    1.32 +/*****************************************************************************/
    1.33 +/* SDL OpenGL/OpenGL ES functions                                            */
    1.34 +/*****************************************************************************/
    1.35 +#define EGLCHK(stmt)							\
    1.36 +	do {										\
    1.37 +		EGLint err;								\
    1.38 +												\
    1.39 +		stmt;									\
    1.40 +		err = eglGetError();					\
    1.41 +		if (err != EGL_SUCCESS) {				\
    1.42 +			SDL_SetError("EGL error %d", err);	\
    1.43 +			return 0;							\
    1.44 +		}										\
    1.45 +	} while (0)
    1.46 +
    1.47 +int
    1.48 +PSP_GL_LoadLibrary(_THIS, const char *path)
    1.49 +{
    1.50 +  if (!_this->gl_config.driver_loaded) {
    1.51 +		_this->gl_config.driver_loaded = 1;
    1.52 +  }
    1.53 +
    1.54 +  return 0;
    1.55 +}
    1.56 +
    1.57 +/* pspgl doesn't provide this call, so stub it out since SDL requires it. 
    1.58 +#define GLSTUB(func,params) void func params {}
    1.59 +  		 
    1.60 +GLSTUB(glOrtho,(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
    1.61 +          			GLdouble zNear, GLdouble zFar))
    1.62 +*/
    1.63 +void *
    1.64 +PSP_GL_GetProcAddress(_THIS, const char *proc)
    1.65 +{
    1.66 +		return eglGetProcAddress(proc);
    1.67 +}
    1.68 +
    1.69 +void
    1.70 +PSP_GL_UnloadLibrary(_THIS)
    1.71 +{
    1.72 +		eglTerminate(_this->gl_data->display);
    1.73 +}
    1.74 +
    1.75 +static EGLint width = 480;
    1.76 +static EGLint height = 272;
    1.77 +
    1.78 +SDL_GLContext
    1.79 +PSP_GL_CreateContext(_THIS, SDL_Window * window)
    1.80 +{
    1.81 +
    1.82 +    SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
    1.83 +
    1.84 +		EGLint attribs[32];
    1.85 +		EGLDisplay display;
    1.86 +		EGLContext context;
    1.87 +		EGLSurface surface;
    1.88 +		EGLConfig config;
    1.89 +		EGLint num_configs;
    1.90 +		int i;
    1.91 +	
    1.92 +
    1.93 +	/* EGL init taken from glutCreateWindow() in PSPGL's glut.c. */
    1.94 +		EGLCHK(display = eglGetDisplay(0));
    1.95 +		EGLCHK(eglInitialize(display, NULL, NULL));
    1.96 +    wdata->uses_gles = SDL_TRUE;   
    1.97 +		window->flags |= SDL_WINDOW_FULLSCREEN;
    1.98 +    	
    1.99 +		/* Setup the config based on SDL's current values. */
   1.100 +		i = 0;
   1.101 +		attribs[i++] = EGL_RED_SIZE;
   1.102 +		attribs[i++] = _this->gl_config.red_size;
   1.103 +		attribs[i++] = EGL_GREEN_SIZE;
   1.104 +		attribs[i++] = _this->gl_config.green_size;
   1.105 +		attribs[i++] = EGL_BLUE_SIZE;
   1.106 +		attribs[i++] = _this->gl_config.blue_size;
   1.107 +		attribs[i++] = EGL_DEPTH_SIZE;
   1.108 +		attribs[i++] = _this->gl_config.depth_size;
   1.109 +	
   1.110 +		if (_this->gl_config.alpha_size)
   1.111 +		{
   1.112 +			attribs[i++] = EGL_ALPHA_SIZE;
   1.113 +			attribs[i++] = _this->gl_config.alpha_size;
   1.114 +		}
   1.115 +		if (_this->gl_config.stencil_size)
   1.116 +		{
   1.117 +			attribs[i++] = EGL_STENCIL_SIZE;
   1.118 +			attribs[i++] = _this->gl_config.stencil_size;
   1.119 +		}
   1.120 +	
   1.121 +		attribs[i++] = EGL_NONE;
   1.122 +		
   1.123 +		EGLCHK(eglChooseConfig(display, attribs, &config, 1, &num_configs));
   1.124 +	
   1.125 +		if (num_configs == 0)
   1.126 +		{
   1.127 +			SDL_SetError("No valid EGL configs for requested mode");
   1.128 +			return 0;
   1.129 +		}
   1.130 +		
   1.131 +		EGLCHK(eglGetConfigAttrib(display, config, EGL_WIDTH, &width));
   1.132 +		EGLCHK(eglGetConfigAttrib(display, config, EGL_HEIGHT, &height));
   1.133 +
   1.134 +		EGLCHK(context = eglCreateContext(display, config, NULL, NULL));
   1.135 +		EGLCHK(surface = eglCreateWindowSurface(display, config, 0, NULL));
   1.136 +		EGLCHK(eglMakeCurrent(display, surface, surface, context));	
   1.137 +		
   1.138 +		_this->gl_data->display = display;
   1.139 +		_this->gl_data->context = context;
   1.140 +		_this->gl_data->surface = surface;
   1.141 +	
   1.142 +
   1.143 +    return context;
   1.144 +}
   1.145 +
   1.146 +int
   1.147 +PSP_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
   1.148 +{
   1.149 +		if (!eglMakeCurrent(_this->gl_data->display, _this->gl_data->surface,
   1.150 +	                      _this->gl_data->surface, _this->gl_data->context))
   1.151 +		{
   1.152 +			SDL_SetError("Unable to make EGL context current");
   1.153 +			return -1;
   1.154 +		}
   1.155 +    return 0;
   1.156 +}
   1.157 +
   1.158 +int
   1.159 +PSP_GL_SetSwapInterval(_THIS, int interval)
   1.160 +{
   1.161 +    EGLBoolean status;    
   1.162 +    status = eglSwapInterval(_this->gl_data->display, interval);
   1.163 +    if (status == EGL_TRUE) {
   1.164 +        /* Return success to upper level */
   1.165 +        _this->gl_data->swapinterval = interval;
   1.166 +        return 0;
   1.167 +    }
   1.168 +    /* Failed to set swap interval */
   1.169 +    SDL_SetError("Unable to set the EGL swap interval");
   1.170 +    return -1;
   1.171 +}
   1.172 +
   1.173 +int
   1.174 +PSP_GL_GetSwapInterval(_THIS)
   1.175 +{
   1.176 +    return _this->gl_data->swapinterval;
   1.177 +}
   1.178 +
   1.179 +void
   1.180 +PSP_GL_SwapWindow(_THIS, SDL_Window * window)
   1.181 +{
   1.182 +    eglSwapBuffers(_this->gl_data->display, _this->gl_data->surface);
   1.183 +}
   1.184 +
   1.185 +void
   1.186 +PSP_GL_DeleteContext(_THIS, SDL_GLContext context)
   1.187 +{
   1.188 +    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
   1.189 +    EGLBoolean status;
   1.190 +
   1.191 +    if (phdata->egl_initialized != SDL_TRUE) {
   1.192 +        SDL_SetError("PSP: GLES initialization failed, no OpenGL ES support");
   1.193 +        return;
   1.194 +    }
   1.195 +
   1.196 +    /* Check if OpenGL ES connection has been initialized */
   1.197 +    if (_this->gl_data->display != EGL_NO_DISPLAY) {
   1.198 +        if (context != EGL_NO_CONTEXT) {
   1.199 +            status = eglDestroyContext(_this->gl_data->display, context);
   1.200 +            if (status != EGL_TRUE) {
   1.201 +                /* Error during OpenGL ES context destroying */
   1.202 +                SDL_SetError("PSP: OpenGL ES context destroy error");
   1.203 +                return;
   1.204 +            }
   1.205 +        }
   1.206 +    }
   1.207 +
   1.208 +    return;
   1.209 +}
   1.210 +