Fixed bug 4349 - SDL_CreateWindow fails with KMS/DRM after upgrading Mesa to 18.2.3
authorSam Lantinga <slouken@libsdl.org>
Wed, 31 Oct 2018 15:16:51 -0700
changeset 12376cfc65d4d49ae
parent 12375 d9a16c76d8d1
child 12377 84618d571795
Fixed bug 4349 - SDL_CreateWindow fails with KMS/DRM after upgrading Mesa to 18.2.3

Rainer Sabelka

After I did an upgrade of my arch Linux installation (resulting in an update of Mesa to version 18.2.3), all my SDL2 applications which use the KMS/DRM driver stopped working.
Reason: Creating a Window with SDL_CreateWindow failed because the call to EGL
eglCreateWindowSurface() returns an error "EGL_BAD_MATCH".
After investigating with the debugger I figured, that the configuration, which has been selected from the output of eglChooseConfig(), has an "EGL_NATIVE_VISUAL_ID" which does not match the "format" of the underlying gbm surface.

The attached patch fixes the problem. It does so, by mimicking Weston's behavior.
All configurations returned from eglChooseConfig() which have an visual_id different from the gbm format are discarded, and only from the remaining ones the "best" match is selected.
src/video/SDL_egl.c
src/video/SDL_egl_c.h
src/video/kmsdrm/SDL_kmsdrmvideo.c
     1.1 --- a/src/video/SDL_egl.c	Wed Oct 31 15:01:20 2018 -0700
     1.2 +++ b/src/video/SDL_egl.c	Wed Oct 31 15:16:51 2018 -0700
     1.3 @@ -449,6 +449,12 @@
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +void
     1.8 +SDL_EGL_SetRequiredVisualId(_THIS, int visual_id) 
     1.9 +{
    1.10 +    _this->egl_data->egl_required_visual_id=visual_id;
    1.11 +}
    1.12 +
    1.13  #ifdef DUMP_EGL_CONFIG
    1.14  
    1.15  #define ATTRIBUTE(_attr) { _attr, #_attr }
    1.16 @@ -513,14 +519,8 @@
    1.17  /* 64 seems nice. */
    1.18      EGLint attribs[64];
    1.19      EGLint found_configs = 0, value;
    1.20 -#ifdef SDL_VIDEO_DRIVER_KMSDRM
    1.21 -    /* Intel EGL on KMS/DRM (al least) returns invalid configs that confuse the bitdiff search used */
    1.22 -    /* later in this function, so we simply use the first one when using the KMSDRM driver for now. */
    1.23 -    EGLConfig configs[1];
    1.24 -#else
    1.25      /* 128 seems even nicer here */
    1.26      EGLConfig configs[128];
    1.27 -#endif
    1.28      int i, j, best_bitdiff = -1, bitdiff;
    1.29     
    1.30      if (!_this->egl_data) {
    1.31 @@ -603,6 +603,16 @@
    1.32      /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
    1.33  
    1.34      for (i = 0; i < found_configs; i++ ) {
    1.35 +        if (_this->egl_data->egl_required_visual_id)
    1.36 +        {
    1.37 +            EGLint format;
    1.38 +            _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
    1.39 +                                            configs[i], 
    1.40 +                                            EGL_NATIVE_VISUAL_ID, &format);
    1.41 +            if (_this->egl_data->egl_required_visual_id != format)
    1.42 +                continue;
    1.43 +        }
    1.44 +
    1.45          bitdiff = 0;
    1.46          for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
    1.47              if (attribs[j] == EGL_NONE) {
     2.1 --- a/src/video/SDL_egl_c.h	Wed Oct 31 15:01:20 2018 -0700
     2.2 +++ b/src/video/SDL_egl_c.h	Wed Oct 31 15:16:51 2018 -0700
     2.3 @@ -37,6 +37,7 @@
     2.4      int egl_swapinterval;
     2.5      int egl_surfacetype;
     2.6      int egl_version_major, egl_version_minor;
     2.7 +    EGLint egl_required_visual_id;
     2.8      
     2.9      EGLDisplay(EGLAPIENTRY *eglGetDisplay) (NativeDisplayType display);
    2.10      EGLDisplay(EGLAPIENTRY *eglGetPlatformDisplay) (EGLenum platform,
    2.11 @@ -102,6 +103,7 @@
    2.12  extern int SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display, EGLenum platform);
    2.13  extern void *SDL_EGL_GetProcAddress(_THIS, const char *proc);
    2.14  extern void SDL_EGL_UnloadLibrary(_THIS);
    2.15 +extern void SDL_EGL_SetRequiredVisualId(_THIS, int visual_id);
    2.16  extern int SDL_EGL_ChooseConfig(_THIS);
    2.17  extern int SDL_EGL_SetSwapInterval(_THIS, int interval);
    2.18  extern int SDL_EGL_GetSwapInterval(_THIS);
     3.1 --- a/src/video/kmsdrm/SDL_kmsdrmvideo.c	Wed Oct 31 15:01:20 2018 -0700
     3.2 +++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c	Wed Oct 31 15:16:51 2018 -0700
     3.3 @@ -587,6 +587,7 @@
     3.4              goto error;
     3.5          }
     3.6      }
     3.7 +    SDL_EGL_SetRequiredVisualId(_this, surface_fmt);
     3.8      wdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) wdata->gs);
     3.9  
    3.10      if (wdata->egl_surface == EGL_NO_SURFACE) {