[KMSDRM] Better error handling: no more segfaults on window creation failure.

This commit is contained in:
vanfanel 2021-03-19 04:25:40 +01:00 committed by Sam Lantinga
parent 4acd1dcad4
commit cf7eef37b0
2 changed files with 38 additions and 41 deletions

View File

@ -477,18 +477,9 @@ KMSDRM_InitMouse(_THIS, SDL_VideoDisplay *display)
mouse->WarpMouse = KMSDRM_WarpMouse; mouse->WarpMouse = KMSDRM_WarpMouse;
mouse->WarpMouseGlobal = KMSDRM_WarpMouseGlobal; mouse->WarpMouseGlobal = KMSDRM_WarpMouseGlobal;
/* SDL expects to set the default cursor of the display when we init the mouse,
but since we have moved the KMSDRM_InitMouse() call to KMSDRM_CreateWindow(),
we end up calling KMSDRM_InitMouse() every time we create a window, so we
have to prevent this from being done every time a new window is created.
If we don't, new default cursors would stack up on mouse->cursors and SDL
would have to hide and delete them at quit, not to mention the memory leak... */
if(dispdata->set_default_cursor_pending) {
SDL_SetDefaultCursor(KMSDRM_CreateDefaultCursor()); SDL_SetDefaultCursor(KMSDRM_CreateDefaultCursor());
dispdata->set_default_cursor_pending = SDL_FALSE; dispdata->set_default_cursor_pending = SDL_FALSE;
} }
}
void void
KMSDRM_QuitMouse(_THIS) KMSDRM_QuitMouse(_THIS)

View File

@ -535,9 +535,9 @@ void KMSDRM_AddDisplay (_THIS, drmModeConnector *connector, drmModeRes *resource
dispdata->modeset_pending = SDL_FALSE; dispdata->modeset_pending = SDL_FALSE;
dispdata->cursor_bo = NULL; dispdata->cursor_bo = NULL;
/* Since we create and show the default cursor on KMSDRM_InitMouse() and /* Since we create and show the default cursor on KMSDRM_InitMouse(),
we call KMSDRM_InitMouse() everytime we create a new window, we have and we call KMSDRM_InitMouse() when we create a window, we have to know
to be sure to create and show the default cursor only the first time. if the display used by the window already has a default cursor or not.
If we don't, new default cursors would stack up on mouse->cursors and SDL If we don't, new default cursors would stack up on mouse->cursors and SDL
would have to hide and delete them at quit, not to mention the memory leak... */ would have to hide and delete them at quit, not to mention the memory leak... */
dispdata->set_default_cursor_pending = SDL_TRUE; dispdata->set_default_cursor_pending = SDL_TRUE;
@ -1087,8 +1087,12 @@ KMSDRM_DestroyWindow(_THIS, SDL_Window *window)
/* Destroy GBM surface and buffers. */ /* Destroy GBM surface and buffers. */
KMSDRM_DestroySurfaces(_this, window); KMSDRM_DestroySurfaces(_this, window);
/* Unload library and deinit GBM, but only if this is the last remaining window.*/ /* Unload library and deinit GBM, but only if this is the last window.
if ((viddata->num_windows - 1) == 0) { Note that this is the right comparision because num_windows could be 1
if there is a complete window, or 0 if we got here from SDL_CreateWindow()
because KMSDRM_CreateWindow() returned an error so the window wasn't
added to the windows list. */
if (viddata->num_windows <= 1) {
/* Unload EGL/GL library and free egl_data. */ /* Unload EGL/GL library and free egl_data. */
if (_this->egl_data) { if (_this->egl_data) {
@ -1152,8 +1156,7 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
/* Allocate window internal data */ /* Allocate window internal data */
windata = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData)); windata = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData));
if (!windata) { if (!windata) {
ret = SDL_OutOfMemory(); return(SDL_OutOfMemory());
goto cleanup;
} }
/* Setup driver data for this window */ /* Setup driver data for this window */
@ -1185,7 +1188,8 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
but only when we come here for the first time, but only when we come here for the first time,
and only if it's not a VK window. */ and only if it's not a VK window. */
if ((ret = KMSDRM_GBMInit(_this, dispdata))) { if ((ret = KMSDRM_GBMInit(_this, dispdata))) {
goto cleanup; return (SDL_SetError("Can't init GBM on window creation."));
}
} }
/* Manually load the GL library. KMSDRM_EGL_LoadLibrary() has already /* Manually load the GL library. KMSDRM_EGL_LoadLibrary() has already
@ -1196,13 +1200,17 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
if (!_this->egl_data) { if (!_this->egl_data) {
egl_display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev; egl_display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev;
if (SDL_EGL_LoadLibrary(_this, NULL, egl_display, EGL_PLATFORM_GBM_MESA)) { if (SDL_EGL_LoadLibrary(_this, NULL, egl_display, EGL_PLATFORM_GBM_MESA)) {
goto cleanup; return (SDL_SetError("Can't load EGL/GL library on window creation."));
} }
_this->gl_config.driver_loaded = 1; _this->gl_config.driver_loaded = 1;
} }
/* Init the cursor stuff for the window display, but ONLY if we haven't done so
on this display before. */
if (!dispdata->set_default_cursor_pending) {
/* Create the cursor BO for the display of this window, /* Create the cursor BO for the display of this window,
now that we know this is not a VK window. */ now that we know this is not a VK window. */
KMSDRM_CreateCursorBO(display); KMSDRM_CreateCursorBO(display);
@ -1217,6 +1225,8 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
destruction/creation cycles. So we must manually re-show the destruction/creation cycles. So we must manually re-show the
visible cursors, if necessary, when we create a window. */ visible cursors, if necessary, when we create a window. */
KMSDRM_InitCursor(); KMSDRM_InitCursor();
dispdata->set_default_cursor_pending = SDL_TRUE;
} }
/* The FULLSCREEN flags are cut out from window->flags at this point, /* The FULLSCREEN flags are cut out from window->flags at this point,
@ -1244,7 +1254,7 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
/* Create the window surfaces with the size we have just chosen. /* Create the window surfaces with the size we have just chosen.
Needs the window diverdata in place. */ Needs the window diverdata in place. */
if ((ret = KMSDRM_CreateSurfaces(_this, window))) { if ((ret = KMSDRM_CreateSurfaces(_this, window))) {
goto cleanup; return (SDL_SetError("Can't window GBM/EGL surfaces on window creation."));
} }
/* Tell app about the size we have determined for the window, /* Tell app about the size we have determined for the window,
@ -1264,8 +1274,7 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
viddata->max_windows = new_max_windows; viddata->max_windows = new_max_windows;
if (!viddata->windows) { if (!viddata->windows) {
ret = SDL_OutOfMemory(); return (SDL_OutOfMemory());
goto cleanup;
} }
} }
@ -1278,12 +1287,9 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
SDL_SetMouseFocus(window); SDL_SetMouseFocus(window);
SDL_SetKeyboardFocus(window); SDL_SetKeyboardFocus(window);
cleanup: /* Allocated windata will be freed in KMSDRM_DestroyWindow,
and KMSDRM_DestroyWindow() will be called by SDL_CreateWindow()
if (ret) { if we return error on any of the previous returns of the function. */
/* Allocated windata will be freed in KMSDRM_DestroyWindow */
KMSDRM_DestroyWindow(_this, window);
}
return ret; return ret;
} }