src/video/psp/SDL_pspgl.c
author Captain Lex <kimonline@126.com>
Sun, 17 Mar 2013 20:07:02 +0800
changeset 7009 161b7b6a5303
child 7037 3fedf1f25b94
permissions -rw-r--r--
Add PSP support
kimonline@7009
     1
/*
kimonline@7009
     2
  Simple DirectMedia Layer
kimonline@7009
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
kimonline@7009
     4
kimonline@7009
     5
  This software is provided 'as-is', without any express or implied
kimonline@7009
     6
  warranty.  In no event will the authors be held liable for any damages
kimonline@7009
     7
  arising from the use of this software.
kimonline@7009
     8
kimonline@7009
     9
  Permission is granted to anyone to use this software for any purpose,
kimonline@7009
    10
  including commercial applications, and to alter it and redistribute it
kimonline@7009
    11
  freely, subject to the following restrictions:
kimonline@7009
    12
kimonline@7009
    13
  1. The origin of this software must not be misrepresented; you must not
kimonline@7009
    14
     claim that you wrote the original software. If you use this software
kimonline@7009
    15
     in a product, an acknowledgment in the product documentation would be
kimonline@7009
    16
     appreciated but is not required.
kimonline@7009
    17
  2. Altered source versions must be plainly marked as such, and must not be
kimonline@7009
    18
     misrepresented as being the original software.
kimonline@7009
    19
  3. This notice may not be removed or altered from any source distribution.
kimonline@7009
    20
*/
kimonline@7009
    21
kimonline@7009
    22
#include <stdlib.h>
kimonline@7009
    23
#include <string.h>
kimonline@7009
    24
kimonline@7009
    25
#include "SDL_error.h"
kimonline@7009
    26
#include "SDL_pspvideo.h"
kimonline@7009
    27
#include "SDL_pspgl_c.h"
kimonline@7009
    28
kimonline@7009
    29
/*****************************************************************************/
kimonline@7009
    30
/* SDL OpenGL/OpenGL ES functions                                            */
kimonline@7009
    31
/*****************************************************************************/
kimonline@7009
    32
#define EGLCHK(stmt)							\
kimonline@7009
    33
	do {										\
kimonline@7009
    34
		EGLint err;								\
kimonline@7009
    35
												\
kimonline@7009
    36
		stmt;									\
kimonline@7009
    37
		err = eglGetError();					\
kimonline@7009
    38
		if (err != EGL_SUCCESS) {				\
kimonline@7009
    39
			SDL_SetError("EGL error %d", err);	\
kimonline@7009
    40
			return 0;							\
kimonline@7009
    41
		}										\
kimonline@7009
    42
	} while (0)
kimonline@7009
    43
kimonline@7009
    44
int
kimonline@7009
    45
PSP_GL_LoadLibrary(_THIS, const char *path)
kimonline@7009
    46
{
kimonline@7009
    47
  if (!_this->gl_config.driver_loaded) {
kimonline@7009
    48
		_this->gl_config.driver_loaded = 1;
kimonline@7009
    49
  }
kimonline@7009
    50
kimonline@7009
    51
  return 0;
kimonline@7009
    52
}
kimonline@7009
    53
kimonline@7009
    54
/* pspgl doesn't provide this call, so stub it out since SDL requires it. 
kimonline@7009
    55
#define GLSTUB(func,params) void func params {}
kimonline@7009
    56
  		 
kimonline@7009
    57
GLSTUB(glOrtho,(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
kimonline@7009
    58
          			GLdouble zNear, GLdouble zFar))
kimonline@7009
    59
*/
kimonline@7009
    60
void *
kimonline@7009
    61
PSP_GL_GetProcAddress(_THIS, const char *proc)
kimonline@7009
    62
{
kimonline@7009
    63
		return eglGetProcAddress(proc);
kimonline@7009
    64
}
kimonline@7009
    65
kimonline@7009
    66
void
kimonline@7009
    67
PSP_GL_UnloadLibrary(_THIS)
kimonline@7009
    68
{
kimonline@7009
    69
		eglTerminate(_this->gl_data->display);
kimonline@7009
    70
}
kimonline@7009
    71
kimonline@7009
    72
static EGLint width = 480;
kimonline@7009
    73
static EGLint height = 272;
kimonline@7009
    74
kimonline@7009
    75
SDL_GLContext
kimonline@7009
    76
PSP_GL_CreateContext(_THIS, SDL_Window * window)
kimonline@7009
    77
{
kimonline@7009
    78
kimonline@7009
    79
    SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
kimonline@7009
    80
kimonline@7009
    81
		EGLint attribs[32];
kimonline@7009
    82
		EGLDisplay display;
kimonline@7009
    83
		EGLContext context;
kimonline@7009
    84
		EGLSurface surface;
kimonline@7009
    85
		EGLConfig config;
kimonline@7009
    86
		EGLint num_configs;
kimonline@7009
    87
		int i;
kimonline@7009
    88
	
kimonline@7009
    89
kimonline@7009
    90
	/* EGL init taken from glutCreateWindow() in PSPGL's glut.c. */
kimonline@7009
    91
		EGLCHK(display = eglGetDisplay(0));
kimonline@7009
    92
		EGLCHK(eglInitialize(display, NULL, NULL));
kimonline@7009
    93
    wdata->uses_gles = SDL_TRUE;   
kimonline@7009
    94
		window->flags |= SDL_WINDOW_FULLSCREEN;
kimonline@7009
    95
    	
kimonline@7009
    96
		/* Setup the config based on SDL's current values. */
kimonline@7009
    97
		i = 0;
kimonline@7009
    98
		attribs[i++] = EGL_RED_SIZE;
kimonline@7009
    99
		attribs[i++] = _this->gl_config.red_size;
kimonline@7009
   100
		attribs[i++] = EGL_GREEN_SIZE;
kimonline@7009
   101
		attribs[i++] = _this->gl_config.green_size;
kimonline@7009
   102
		attribs[i++] = EGL_BLUE_SIZE;
kimonline@7009
   103
		attribs[i++] = _this->gl_config.blue_size;
kimonline@7009
   104
		attribs[i++] = EGL_DEPTH_SIZE;
kimonline@7009
   105
		attribs[i++] = _this->gl_config.depth_size;
kimonline@7009
   106
	
kimonline@7009
   107
		if (_this->gl_config.alpha_size)
kimonline@7009
   108
		{
kimonline@7009
   109
			attribs[i++] = EGL_ALPHA_SIZE;
kimonline@7009
   110
			attribs[i++] = _this->gl_config.alpha_size;
kimonline@7009
   111
		}
kimonline@7009
   112
		if (_this->gl_config.stencil_size)
kimonline@7009
   113
		{
kimonline@7009
   114
			attribs[i++] = EGL_STENCIL_SIZE;
kimonline@7009
   115
			attribs[i++] = _this->gl_config.stencil_size;
kimonline@7009
   116
		}
kimonline@7009
   117
	
kimonline@7009
   118
		attribs[i++] = EGL_NONE;
kimonline@7009
   119
		
kimonline@7009
   120
		EGLCHK(eglChooseConfig(display, attribs, &config, 1, &num_configs));
kimonline@7009
   121
	
kimonline@7009
   122
		if (num_configs == 0)
kimonline@7009
   123
		{
kimonline@7009
   124
			SDL_SetError("No valid EGL configs for requested mode");
kimonline@7009
   125
			return 0;
kimonline@7009
   126
		}
kimonline@7009
   127
		
kimonline@7009
   128
		EGLCHK(eglGetConfigAttrib(display, config, EGL_WIDTH, &width));
kimonline@7009
   129
		EGLCHK(eglGetConfigAttrib(display, config, EGL_HEIGHT, &height));
kimonline@7009
   130
kimonline@7009
   131
		EGLCHK(context = eglCreateContext(display, config, NULL, NULL));
kimonline@7009
   132
		EGLCHK(surface = eglCreateWindowSurface(display, config, 0, NULL));
kimonline@7009
   133
		EGLCHK(eglMakeCurrent(display, surface, surface, context));	
kimonline@7009
   134
		
kimonline@7009
   135
		_this->gl_data->display = display;
kimonline@7009
   136
		_this->gl_data->context = context;
kimonline@7009
   137
		_this->gl_data->surface = surface;
kimonline@7009
   138
	
kimonline@7009
   139
kimonline@7009
   140
    return context;
kimonline@7009
   141
}
kimonline@7009
   142
kimonline@7009
   143
int
kimonline@7009
   144
PSP_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
kimonline@7009
   145
{
kimonline@7009
   146
		if (!eglMakeCurrent(_this->gl_data->display, _this->gl_data->surface,
kimonline@7009
   147
	                      _this->gl_data->surface, _this->gl_data->context))
kimonline@7009
   148
		{
kimonline@7009
   149
			SDL_SetError("Unable to make EGL context current");
kimonline@7009
   150
			return -1;
kimonline@7009
   151
		}
kimonline@7009
   152
    return 0;
kimonline@7009
   153
}
kimonline@7009
   154
kimonline@7009
   155
int
kimonline@7009
   156
PSP_GL_SetSwapInterval(_THIS, int interval)
kimonline@7009
   157
{
kimonline@7009
   158
    EGLBoolean status;    
kimonline@7009
   159
    status = eglSwapInterval(_this->gl_data->display, interval);
kimonline@7009
   160
    if (status == EGL_TRUE) {
kimonline@7009
   161
        /* Return success to upper level */
kimonline@7009
   162
        _this->gl_data->swapinterval = interval;
kimonline@7009
   163
        return 0;
kimonline@7009
   164
    }
kimonline@7009
   165
    /* Failed to set swap interval */
kimonline@7009
   166
    SDL_SetError("Unable to set the EGL swap interval");
kimonline@7009
   167
    return -1;
kimonline@7009
   168
}
kimonline@7009
   169
kimonline@7009
   170
int
kimonline@7009
   171
PSP_GL_GetSwapInterval(_THIS)
kimonline@7009
   172
{
kimonline@7009
   173
    return _this->gl_data->swapinterval;
kimonline@7009
   174
}
kimonline@7009
   175
kimonline@7009
   176
void
kimonline@7009
   177
PSP_GL_SwapWindow(_THIS, SDL_Window * window)
kimonline@7009
   178
{
kimonline@7009
   179
    eglSwapBuffers(_this->gl_data->display, _this->gl_data->surface);
kimonline@7009
   180
}
kimonline@7009
   181
kimonline@7009
   182
void
kimonline@7009
   183
PSP_GL_DeleteContext(_THIS, SDL_GLContext context)
kimonline@7009
   184
{
kimonline@7009
   185
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
kimonline@7009
   186
    EGLBoolean status;
kimonline@7009
   187
kimonline@7009
   188
    if (phdata->egl_initialized != SDL_TRUE) {
kimonline@7009
   189
        SDL_SetError("PSP: GLES initialization failed, no OpenGL ES support");
kimonline@7009
   190
        return;
kimonline@7009
   191
    }
kimonline@7009
   192
kimonline@7009
   193
    /* Check if OpenGL ES connection has been initialized */
kimonline@7009
   194
    if (_this->gl_data->display != EGL_NO_DISPLAY) {
kimonline@7009
   195
        if (context != EGL_NO_CONTEXT) {
kimonline@7009
   196
            status = eglDestroyContext(_this->gl_data->display, context);
kimonline@7009
   197
            if (status != EGL_TRUE) {
kimonline@7009
   198
                /* Error during OpenGL ES context destroying */
kimonline@7009
   199
                SDL_SetError("PSP: OpenGL ES context destroy error");
kimonline@7009
   200
                return;
kimonline@7009
   201
            }
kimonline@7009
   202
        }
kimonline@7009
   203
    }
kimonline@7009
   204
kimonline@7009
   205
    return;
kimonline@7009
   206
}
kimonline@7009
   207