src/video/SDL_egl.c
changeset 10740 bb53965b659d
parent 10737 3406a0f8b041
child 10741 a32fd6b7412b
     1.1 --- a/src/video/SDL_egl.c	Sun Jan 01 18:47:29 2017 -0800
     1.2 +++ b/src/video/SDL_egl.c	Thu Dec 29 11:49:18 2016 -0500
     1.3 @@ -74,7 +74,42 @@
     1.4  { \
     1.5      return SDL_SetError("Could not retrieve EGL function " #NAME); \
     1.6  }
     1.7 -    
     1.8 +
     1.9 +static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
    1.10 +{
    1.11 +#define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
    1.12 +    switch (eglErrorCode) {
    1.13 +        SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
    1.14 +        SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
    1.15 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
    1.16 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
    1.17 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
    1.18 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
    1.19 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
    1.20 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
    1.21 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
    1.22 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
    1.23 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
    1.24 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
    1.25 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
    1.26 +        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
    1.27 +        SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
    1.28 +    }
    1.29 +    return "";
    1.30 +}
    1.31 +
    1.32 +int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
    1.33 +{
    1.34 +    const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
    1.35 +    char altErrorText[32];
    1.36 +    if (errorText[0] == '\0') {
    1.37 +        /* An unknown-to-SDL error code was reported.  Report its hexadecimal value, instead of its name. */
    1.38 +        SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
    1.39 +        errorText = altErrorText;
    1.40 +    }
    1.41 +    return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
    1.42 +}
    1.43 +
    1.44  /* EGL implementation of SDL OpenGL ES support */
    1.45  #ifdef EGL_KHR_create_context        
    1.46  static int SDL_EGL_HasExtension(_THIS, const char *ext)
    1.47 @@ -265,7 +300,8 @@
    1.48      LOAD_FUNC(eglWaitGL);
    1.49      LOAD_FUNC(eglBindAPI);
    1.50      LOAD_FUNC(eglQueryString);
    1.51 -    
    1.52 +    LOAD_FUNC(eglGetError);
    1.53 +
    1.54  #if !defined(__WINRT__)
    1.55      _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
    1.56      if (!_this->egl_data->egl_display) {
    1.57 @@ -378,7 +414,7 @@
    1.58          configs, SDL_arraysize(configs),
    1.59          &found_configs) == EGL_FALSE ||
    1.60          found_configs == 0) {
    1.61 -        return SDL_SetError("Couldn't find matching EGL config");
    1.62 +        return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
    1.63      }
    1.64  
    1.65      /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
    1.66 @@ -497,15 +533,23 @@
    1.67                                        share_context, attribs);
    1.68  
    1.69      if (egl_context == EGL_NO_CONTEXT) {
    1.70 -        SDL_SetError("Could not create EGL context");
    1.71 +        SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
    1.72          return NULL;
    1.73      }
    1.74  
    1.75      _this->egl_data->egl_swapinterval = 0;
    1.76  
    1.77      if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
    1.78 +        /* Save the SDL error set by SDL_EGL_MakeCurrent */
    1.79 +        char errorText[1024];
    1.80 +        SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
    1.81 +
    1.82 +        /* Delete the context, which may alter the value returned by SDL_GetError() */
    1.83          SDL_EGL_DeleteContext(_this, egl_context);
    1.84 -        SDL_SetError("Could not make EGL context current");
    1.85 +
    1.86 +        /* Restore the SDL error */
    1.87 +        SDL_SetError("%s", errorText);
    1.88 +
    1.89          return NULL;
    1.90      }
    1.91  
    1.92 @@ -529,7 +573,7 @@
    1.93      } else {
    1.94          if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
    1.95              egl_surface, egl_surface, egl_context)) {
    1.96 -            return SDL_SetError("Unable to make EGL context current");
    1.97 +            return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
    1.98          }
    1.99      }
   1.100        
   1.101 @@ -551,7 +595,7 @@
   1.102          return 0;
   1.103      }
   1.104      
   1.105 -    return SDL_SetError("Unable to set the EGL swap interval");
   1.106 +    return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
   1.107  }
   1.108  
   1.109  int
   1.110 @@ -569,7 +613,7 @@
   1.111  SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
   1.112  {
   1.113      if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
   1.114 -        return SDL_SetError("eglSwapBuffers() failed");
   1.115 +        return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
   1.116      }
   1.117      return 0;
   1.118  }
   1.119 @@ -594,6 +638,8 @@
   1.120  EGLSurface *
   1.121  SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
   1.122  {
   1.123 +    EGLSurface * surface;
   1.124 +
   1.125      if (SDL_EGL_ChooseConfig(_this) != 0) {
   1.126          return EGL_NO_SURFACE;
   1.127      }
   1.128 @@ -612,10 +658,14 @@
   1.129      }
   1.130  #endif    
   1.131      
   1.132 -    return _this->egl_data->eglCreateWindowSurface(
   1.133 +    surface = _this->egl_data->eglCreateWindowSurface(
   1.134              _this->egl_data->egl_display,
   1.135              _this->egl_data->egl_config,
   1.136              nw, NULL);
   1.137 +    if (surface == EGL_NO_SURFACE) {
   1.138 +        SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
   1.139 +    }
   1.140 +    return surface;
   1.141  }
   1.142  
   1.143  void