src/video/psp/SDL_pspgl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 May 2013 14:17:52 -0700
changeset 7191 75360622e65f
parent 7037 3fedf1f25b94
child 8149 681eb46b8ac4
permissions -rw-r--r--
File style cleanup for the SDL 2.0 release
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
/*****************************************************************************/
slouken@7191
    32
#define EGLCHK(stmt)                            \
slouken@7191
    33
    do {                                        \
slouken@7191
    34
        EGLint err;                             \
slouken@7191
    35
                                                \
slouken@7191
    36
        stmt;                                   \
slouken@7191
    37
        err = eglGetError();                    \
slouken@7191
    38
        if (err != EGL_SUCCESS) {               \
slouken@7191
    39
            SDL_SetError("EGL error %d", err);  \
slouken@7191
    40
            return 0;                           \
slouken@7191
    41
        }                                       \
slouken@7191
    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) {
slouken@7191
    48
        _this->gl_config.driver_loaded = 1;
kimonline@7009
    49
  }
kimonline@7009
    50
kimonline@7009
    51
  return 0;
kimonline@7009
    52
}
kimonline@7009
    53
slouken@7191
    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 {}
slouken@7191
    56
kimonline@7009
    57
GLSTUB(glOrtho,(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
slouken@7191
    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
{
slouken@7191
    63
        return eglGetProcAddress(proc);
kimonline@7009
    64
}
kimonline@7009
    65
kimonline@7009
    66
void
kimonline@7009
    67
PSP_GL_UnloadLibrary(_THIS)
kimonline@7009
    68
{
slouken@7191
    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
slouken@7191
    81
        EGLint attribs[32];
slouken@7191
    82
        EGLDisplay display;
slouken@7191
    83
        EGLContext context;
slouken@7191
    84
        EGLSurface surface;
slouken@7191
    85
        EGLConfig config;
slouken@7191
    86
        EGLint num_configs;
slouken@7191
    87
        int i;
kimonline@7009
    88
kimonline@7009
    89
slouken@7191
    90
    /* EGL init taken from glutCreateWindow() in PSPGL's glut.c. */
slouken@7191
    91
        EGLCHK(display = eglGetDisplay(0));
slouken@7191
    92
        EGLCHK(eglInitialize(display, NULL, NULL));
slouken@7191
    93
    wdata->uses_gles = SDL_TRUE;
slouken@7191
    94
        window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@7191
    95
slouken@7191
    96
        /* Setup the config based on SDL's current values. */
slouken@7191
    97
        i = 0;
slouken@7191
    98
        attribs[i++] = EGL_RED_SIZE;
slouken@7191
    99
        attribs[i++] = _this->gl_config.red_size;
slouken@7191
   100
        attribs[i++] = EGL_GREEN_SIZE;
slouken@7191
   101
        attribs[i++] = _this->gl_config.green_size;
slouken@7191
   102
        attribs[i++] = EGL_BLUE_SIZE;
slouken@7191
   103
        attribs[i++] = _this->gl_config.blue_size;
slouken@7191
   104
        attribs[i++] = EGL_DEPTH_SIZE;
slouken@7191
   105
        attribs[i++] = _this->gl_config.depth_size;
slouken@7191
   106
slouken@7191
   107
        if (_this->gl_config.alpha_size)
slouken@7191
   108
        {
slouken@7191
   109
            attribs[i++] = EGL_ALPHA_SIZE;
slouken@7191
   110
            attribs[i++] = _this->gl_config.alpha_size;
slouken@7191
   111
        }
slouken@7191
   112
        if (_this->gl_config.stencil_size)
slouken@7191
   113
        {
slouken@7191
   114
            attribs[i++] = EGL_STENCIL_SIZE;
slouken@7191
   115
            attribs[i++] = _this->gl_config.stencil_size;
slouken@7191
   116
        }
slouken@7191
   117
slouken@7191
   118
        attribs[i++] = EGL_NONE;
slouken@7191
   119
slouken@7191
   120
        EGLCHK(eglChooseConfig(display, attribs, &config, 1, &num_configs));
slouken@7191
   121
slouken@7191
   122
        if (num_configs == 0)
slouken@7191
   123
        {
slouken@7191
   124
            SDL_SetError("No valid EGL configs for requested mode");
slouken@7191
   125
            return 0;
slouken@7191
   126
        }
slouken@7191
   127
slouken@7191
   128
        EGLCHK(eglGetConfigAttrib(display, config, EGL_WIDTH, &width));
slouken@7191
   129
        EGLCHK(eglGetConfigAttrib(display, config, EGL_HEIGHT, &height));
slouken@7191
   130
slouken@7191
   131
        EGLCHK(context = eglCreateContext(display, config, NULL, NULL));
slouken@7191
   132
        EGLCHK(surface = eglCreateWindowSurface(display, config, 0, NULL));
slouken@7191
   133
        EGLCHK(eglMakeCurrent(display, surface, surface, context));
slouken@7191
   134
slouken@7191
   135
        _this->gl_data->display = display;
slouken@7191
   136
        _this->gl_data->context = context;
slouken@7191
   137
        _this->gl_data->surface = surface;
slouken@7191
   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
{
slouken@7191
   146
        if (!eglMakeCurrent(_this->gl_data->display, _this->gl_data->surface,
slouken@7191
   147
                          _this->gl_data->surface, _this->gl_data->context))
slouken@7191
   148
        {
slouken@7191
   149
            return SDL_SetError("Unable to make EGL context current");
slouken@7191
   150
        }
kimonline@7009
   151
    return 0;
kimonline@7009
   152
}
kimonline@7009
   153
kimonline@7009
   154
int
kimonline@7009
   155
PSP_GL_SetSwapInterval(_THIS, int interval)
kimonline@7009
   156
{
slouken@7191
   157
    EGLBoolean status;
kimonline@7009
   158
    status = eglSwapInterval(_this->gl_data->display, interval);
kimonline@7009
   159
    if (status == EGL_TRUE) {
kimonline@7009
   160
        /* Return success to upper level */
kimonline@7009
   161
        _this->gl_data->swapinterval = interval;
kimonline@7009
   162
        return 0;
kimonline@7009
   163
    }
kimonline@7009
   164
    /* Failed to set swap interval */
icculus@7037
   165
    return SDL_SetError("Unable to set the EGL swap interval");
kimonline@7009
   166
}
kimonline@7009
   167
kimonline@7009
   168
int
kimonline@7009
   169
PSP_GL_GetSwapInterval(_THIS)
kimonline@7009
   170
{
kimonline@7009
   171
    return _this->gl_data->swapinterval;
kimonline@7009
   172
}
kimonline@7009
   173
kimonline@7009
   174
void
kimonline@7009
   175
PSP_GL_SwapWindow(_THIS, SDL_Window * window)
kimonline@7009
   176
{
kimonline@7009
   177
    eglSwapBuffers(_this->gl_data->display, _this->gl_data->surface);
kimonline@7009
   178
}
kimonline@7009
   179
kimonline@7009
   180
void
kimonline@7009
   181
PSP_GL_DeleteContext(_THIS, SDL_GLContext context)
kimonline@7009
   182
{
kimonline@7009
   183
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
kimonline@7009
   184
    EGLBoolean status;
kimonline@7009
   185
kimonline@7009
   186
    if (phdata->egl_initialized != SDL_TRUE) {
kimonline@7009
   187
        SDL_SetError("PSP: GLES initialization failed, no OpenGL ES support");
kimonline@7009
   188
        return;
kimonline@7009
   189
    }
kimonline@7009
   190
kimonline@7009
   191
    /* Check if OpenGL ES connection has been initialized */
kimonline@7009
   192
    if (_this->gl_data->display != EGL_NO_DISPLAY) {
kimonline@7009
   193
        if (context != EGL_NO_CONTEXT) {
kimonline@7009
   194
            status = eglDestroyContext(_this->gl_data->display, context);
kimonline@7009
   195
            if (status != EGL_TRUE) {
kimonline@7009
   196
                /* Error during OpenGL ES context destroying */
kimonline@7009
   197
                SDL_SetError("PSP: OpenGL ES context destroy error");
kimonline@7009
   198
                return;
kimonline@7009
   199
            }
kimonline@7009
   200
        }
kimonline@7009
   201
    }
kimonline@7009
   202
kimonline@7009
   203
    return;
kimonline@7009
   204
}
kimonline@7009
   205