src/video/psp/SDL_pspgl.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 09 Dec 2016 01:47:43 -0800
changeset 10690 23a825f341e6
parent 9998 f67cf37e9cd4
child 10695 e54fce44f677
permissions -rw-r--r--
Fixed bug 3513 - SDL_GL_SwapWindow does not return error status

Return an error code from SDL_GL_SwapWindow(), like the other SDL APIs.
kimonline@7009
     1
/*
kimonline@7009
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
philipp@9328
    21
#include "../../SDL_internal.h"
kimonline@7009
    22
philipp@9329
    23
#if SDL_VIDEO_DRIVER_PSP
philipp@9329
    24
kimonline@7009
    25
#include <stdlib.h>
kimonline@7009
    26
#include <string.h>
kimonline@7009
    27
kimonline@7009
    28
#include "SDL_error.h"
kimonline@7009
    29
#include "SDL_pspvideo.h"
kimonline@7009
    30
#include "SDL_pspgl_c.h"
kimonline@7009
    31
kimonline@7009
    32
/*****************************************************************************/
kimonline@7009
    33
/* SDL OpenGL/OpenGL ES functions                                            */
kimonline@7009
    34
/*****************************************************************************/
slouken@7191
    35
#define EGLCHK(stmt)                            \
slouken@7191
    36
    do {                                        \
slouken@7191
    37
        EGLint err;                             \
slouken@7191
    38
                                                \
slouken@7191
    39
        stmt;                                   \
slouken@7191
    40
        err = eglGetError();                    \
slouken@7191
    41
        if (err != EGL_SUCCESS) {               \
slouken@7191
    42
            SDL_SetError("EGL error %d", err);  \
slouken@7191
    43
            return 0;                           \
slouken@7191
    44
        }                                       \
slouken@7191
    45
    } while (0)
kimonline@7009
    46
kimonline@7009
    47
int
kimonline@7009
    48
PSP_GL_LoadLibrary(_THIS, const char *path)
kimonline@7009
    49
{
kimonline@7009
    50
  if (!_this->gl_config.driver_loaded) {
slouken@7191
    51
        _this->gl_config.driver_loaded = 1;
kimonline@7009
    52
  }
kimonline@7009
    53
kimonline@7009
    54
  return 0;
kimonline@7009
    55
}
kimonline@7009
    56
slouken@7191
    57
/* pspgl doesn't provide this call, so stub it out since SDL requires it.
kimonline@7009
    58
#define GLSTUB(func,params) void func params {}
slouken@7191
    59
kimonline@7009
    60
GLSTUB(glOrtho,(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
slouken@7191
    61
                    GLdouble zNear, GLdouble zFar))
kimonline@7009
    62
*/
kimonline@7009
    63
void *
kimonline@7009
    64
PSP_GL_GetProcAddress(_THIS, const char *proc)
kimonline@7009
    65
{
slouken@7191
    66
        return eglGetProcAddress(proc);
kimonline@7009
    67
}
kimonline@7009
    68
kimonline@7009
    69
void
kimonline@7009
    70
PSP_GL_UnloadLibrary(_THIS)
kimonline@7009
    71
{
slouken@7191
    72
        eglTerminate(_this->gl_data->display);
kimonline@7009
    73
}
kimonline@7009
    74
kimonline@7009
    75
static EGLint width = 480;
kimonline@7009
    76
static EGLint height = 272;
kimonline@7009
    77
kimonline@7009
    78
SDL_GLContext
kimonline@7009
    79
PSP_GL_CreateContext(_THIS, SDL_Window * window)
kimonline@7009
    80
{
kimonline@7009
    81
kimonline@7009
    82
    SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
kimonline@7009
    83
slouken@7191
    84
        EGLint attribs[32];
slouken@7191
    85
        EGLDisplay display;
slouken@7191
    86
        EGLContext context;
slouken@7191
    87
        EGLSurface surface;
slouken@7191
    88
        EGLConfig config;
slouken@7191
    89
        EGLint num_configs;
slouken@7191
    90
        int i;
kimonline@7009
    91
kimonline@7009
    92
slouken@7191
    93
    /* EGL init taken from glutCreateWindow() in PSPGL's glut.c. */
slouken@7191
    94
        EGLCHK(display = eglGetDisplay(0));
slouken@7191
    95
        EGLCHK(eglInitialize(display, NULL, NULL));
slouken@7191
    96
    wdata->uses_gles = SDL_TRUE;
slouken@7191
    97
        window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@7191
    98
slouken@7191
    99
        /* Setup the config based on SDL's current values. */
slouken@7191
   100
        i = 0;
slouken@7191
   101
        attribs[i++] = EGL_RED_SIZE;
slouken@7191
   102
        attribs[i++] = _this->gl_config.red_size;
slouken@7191
   103
        attribs[i++] = EGL_GREEN_SIZE;
slouken@7191
   104
        attribs[i++] = _this->gl_config.green_size;
slouken@7191
   105
        attribs[i++] = EGL_BLUE_SIZE;
slouken@7191
   106
        attribs[i++] = _this->gl_config.blue_size;
slouken@7191
   107
        attribs[i++] = EGL_DEPTH_SIZE;
slouken@7191
   108
        attribs[i++] = _this->gl_config.depth_size;
slouken@7191
   109
slouken@7191
   110
        if (_this->gl_config.alpha_size)
slouken@7191
   111
        {
slouken@7191
   112
            attribs[i++] = EGL_ALPHA_SIZE;
slouken@7191
   113
            attribs[i++] = _this->gl_config.alpha_size;
slouken@7191
   114
        }
slouken@7191
   115
        if (_this->gl_config.stencil_size)
slouken@7191
   116
        {
slouken@7191
   117
            attribs[i++] = EGL_STENCIL_SIZE;
slouken@7191
   118
            attribs[i++] = _this->gl_config.stencil_size;
slouken@7191
   119
        }
slouken@7191
   120
slouken@7191
   121
        attribs[i++] = EGL_NONE;
slouken@7191
   122
slouken@7191
   123
        EGLCHK(eglChooseConfig(display, attribs, &config, 1, &num_configs));
slouken@7191
   124
slouken@7191
   125
        if (num_configs == 0)
slouken@7191
   126
        {
slouken@7191
   127
            SDL_SetError("No valid EGL configs for requested mode");
slouken@7191
   128
            return 0;
slouken@7191
   129
        }
slouken@7191
   130
slouken@7191
   131
        EGLCHK(eglGetConfigAttrib(display, config, EGL_WIDTH, &width));
slouken@7191
   132
        EGLCHK(eglGetConfigAttrib(display, config, EGL_HEIGHT, &height));
slouken@7191
   133
slouken@7191
   134
        EGLCHK(context = eglCreateContext(display, config, NULL, NULL));
slouken@7191
   135
        EGLCHK(surface = eglCreateWindowSurface(display, config, 0, NULL));
slouken@7191
   136
        EGLCHK(eglMakeCurrent(display, surface, surface, context));
slouken@7191
   137
slouken@7191
   138
        _this->gl_data->display = display;
slouken@7191
   139
        _this->gl_data->context = context;
slouken@7191
   140
        _this->gl_data->surface = surface;
slouken@7191
   141
kimonline@7009
   142
kimonline@7009
   143
    return context;
kimonline@7009
   144
}
kimonline@7009
   145
kimonline@7009
   146
int
kimonline@7009
   147
PSP_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
kimonline@7009
   148
{
slouken@7191
   149
        if (!eglMakeCurrent(_this->gl_data->display, _this->gl_data->surface,
slouken@7191
   150
                          _this->gl_data->surface, _this->gl_data->context))
slouken@7191
   151
        {
slouken@7191
   152
            return SDL_SetError("Unable to make EGL context current");
slouken@7191
   153
        }
kimonline@7009
   154
    return 0;
kimonline@7009
   155
}
kimonline@7009
   156
kimonline@7009
   157
int
kimonline@7009
   158
PSP_GL_SetSwapInterval(_THIS, int interval)
kimonline@7009
   159
{
slouken@7191
   160
    EGLBoolean status;
kimonline@7009
   161
    status = eglSwapInterval(_this->gl_data->display, interval);
kimonline@7009
   162
    if (status == EGL_TRUE) {
kimonline@7009
   163
        /* Return success to upper level */
kimonline@7009
   164
        _this->gl_data->swapinterval = interval;
kimonline@7009
   165
        return 0;
kimonline@7009
   166
    }
kimonline@7009
   167
    /* Failed to set swap interval */
icculus@7037
   168
    return SDL_SetError("Unable to set the EGL swap interval");
kimonline@7009
   169
}
kimonline@7009
   170
kimonline@7009
   171
int
kimonline@7009
   172
PSP_GL_GetSwapInterval(_THIS)
kimonline@7009
   173
{
kimonline@7009
   174
    return _this->gl_data->swapinterval;
kimonline@7009
   175
}
kimonline@7009
   176
slouken@10690
   177
int
kimonline@7009
   178
PSP_GL_SwapWindow(_THIS, SDL_Window * window)
kimonline@7009
   179
{
kimonline@7009
   180
    eglSwapBuffers(_this->gl_data->display, _this->gl_data->surface);
slouken@10690
   181
    return 0;
kimonline@7009
   182
}
kimonline@7009
   183
kimonline@7009
   184
void
kimonline@7009
   185
PSP_GL_DeleteContext(_THIS, SDL_GLContext context)
kimonline@7009
   186
{
kimonline@7009
   187
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
kimonline@7009
   188
    EGLBoolean status;
kimonline@7009
   189
kimonline@7009
   190
    if (phdata->egl_initialized != SDL_TRUE) {
kimonline@7009
   191
        SDL_SetError("PSP: GLES initialization failed, no OpenGL ES support");
kimonline@7009
   192
        return;
kimonline@7009
   193
    }
kimonline@7009
   194
kimonline@7009
   195
    /* Check if OpenGL ES connection has been initialized */
kimonline@7009
   196
    if (_this->gl_data->display != EGL_NO_DISPLAY) {
kimonline@7009
   197
        if (context != EGL_NO_CONTEXT) {
kimonline@7009
   198
            status = eglDestroyContext(_this->gl_data->display, context);
kimonline@7009
   199
            if (status != EGL_TRUE) {
kimonline@7009
   200
                /* Error during OpenGL ES context destroying */
kimonline@7009
   201
                SDL_SetError("PSP: OpenGL ES context destroy error");
kimonline@7009
   202
                return;
kimonline@7009
   203
            }
kimonline@7009
   204
        }
kimonline@7009
   205
    }
kimonline@7009
   206
kimonline@7009
   207
    return;
kimonline@7009
   208
}
kimonline@7009
   209
philipp@9329
   210
#endif /* SDL_VIDEO_DRIVER_PSP */
philipp@9329
   211
philipp@9329
   212
/* vi: set ts=4 sw=4 expandtab: */