src/video/psp/SDL_pspgl.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Tue, 29 Dec 2015 19:13:56 +0100
changeset 9988 08d023f75d78
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Fixed a crash if creating accelerated renderer after accessing window surface.

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