Clean up any renderer in SDL_DestroyWindowSurface()

Also added an automated test to verify window surface functionality.

Fixes https://github.com/libsdl-org/SDL/issues/10133
This commit is contained in:
Sam Lantinga
2024-06-29 02:34:17 -07:00
parent 6f199eabb8
commit a522bfe3f1
2 changed files with 148 additions and 71 deletions

View File

@@ -2442,12 +2442,6 @@ int SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
/* Tear down the old native window */
SDL_DestroyWindowSurface(window);
if (_this->checked_texture_framebuffer) { /* never checked? No framebuffer to destroy. Don't risk calling the wrong implementation. */
if (_this->DestroyWindowFramebuffer) {
_this->DestroyWindowFramebuffer(_this, window);
}
}
if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
if (flags & SDL_WINDOW_OPENGL) {
need_gl_load = SDL_TRUE;
@@ -3251,57 +3245,59 @@ static SDL_Surface *SDL_CreateWindowFramebuffer(SDL_Window *window)
using a GPU texture through the 2D render API, if we think this would
be more efficient. This only checks once, on demand. */
if (!_this->checked_texture_framebuffer) {
SDL_bool attempt_texture_framebuffer = SDL_TRUE;
/* See if the user or application wants to specifically disable the framebuffer */
const char *hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
if (hint) {
if ((*hint == '0') || (SDL_strcasecmp(hint, "false") == 0) || (SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) == 0)) {
attempt_texture_framebuffer = SDL_FALSE;
}
}
SDL_bool attempt_texture_framebuffer;
if (_this->is_dummy) { /* dummy driver never has GPU support, of course. */
attempt_texture_framebuffer = SDL_FALSE;
}
} else {
/* See if the user or application wants to specifically disable the framebuffer */
const char *hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
if (hint && *hint) {
if ((*hint == '0') || (SDL_strcasecmp(hint, "false") == 0) || (SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) == 0)) {
attempt_texture_framebuffer = SDL_FALSE;
} else {
attempt_texture_framebuffer = SDL_TRUE;
}
} else {
attempt_texture_framebuffer = SDL_TRUE;
#ifdef SDL_PLATFORM_LINUX
/* On WSL, direct X11 is faster than using OpenGL for window framebuffers, so try to detect WSL and avoid texture framebuffer. */
else if ((_this->CreateWindowFramebuffer) && (SDL_strcmp(_this->name, "x11") == 0)) {
struct stat sb;
if ((stat("/proc/sys/fs/binfmt_misc/WSLInterop", &sb) == 0) || (stat("/run/WSL", &sb) == 0)) { /* if either of these exist, we're on WSL. */
attempt_texture_framebuffer = SDL_FALSE;
}
}
/* On WSL, direct X11 is faster than using OpenGL for window framebuffers, so try to detect WSL and avoid texture framebuffer. */
if ((_this->CreateWindowFramebuffer) && (SDL_strcmp(_this->name, "x11") == 0)) {
struct stat sb;
if ((stat("/proc/sys/fs/binfmt_misc/WSLInterop", &sb) == 0) || (stat("/run/WSL", &sb) == 0)) { /* if either of these exist, we're on WSL. */
attempt_texture_framebuffer = SDL_FALSE;
}
}
#endif
#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) /* GDI BitBlt() is way faster than Direct3D dynamic textures right now. (!!! FIXME: is this still true?) */
else if ((_this->CreateWindowFramebuffer) && (SDL_strcmp(_this->name, "windows") == 0)) {
attempt_texture_framebuffer = SDL_FALSE;
}
if (_this->CreateWindowFramebuffer && (SDL_strcmp(_this->name, "windows") == 0)) {
attempt_texture_framebuffer = SDL_FALSE;
}
#endif
#ifdef SDL_PLATFORM_EMSCRIPTEN
else {
attempt_texture_framebuffer = SDL_FALSE;
}
attempt_texture_framebuffer = SDL_FALSE;
#endif
}
if (attempt_texture_framebuffer) {
if (SDL_CreateWindowTexture(_this, window, &format, &pixels, &pitch) < 0) {
/* !!! FIXME: if this failed halfway (made renderer, failed to make texture, etc),
!!! FIXME: we probably need to clean this up so it doesn't interfere with
!!! FIXME: a software fallback at the system level (can we blit to an
!!! FIXME: OpenGL window? etc). */
} else {
/* future attempts will just try to use a texture framebuffer. */
/* !!! FIXME: maybe we shouldn't override these but check if we used a texture
!!! FIXME: framebuffer at the right places; is it feasible we could have an
!!! FIXME: accelerated OpenGL window and a second ends up in software? */
_this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
_this->SetWindowFramebufferVSync = SDL_SetWindowTextureVSync;
_this->GetWindowFramebufferVSync = SDL_GetWindowTextureVSync;
_this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
_this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
created_framebuffer = SDL_TRUE;
if (attempt_texture_framebuffer) {
if (SDL_CreateWindowTexture(_this, window, &format, &pixels, &pitch) < 0) {
/* !!! FIXME: if this failed halfway (made renderer, failed to make texture, etc),
!!! FIXME: we probably need to clean this up so it doesn't interfere with
!!! FIXME: a software fallback at the system level (can we blit to an
!!! FIXME: OpenGL window? etc). */
} else {
/* future attempts will just try to use a texture framebuffer. */
/* !!! FIXME: maybe we shouldn't override these but check if we used a texture
!!! FIXME: framebuffer at the right places; is it feasible we could have an
!!! FIXME: accelerated OpenGL window and a second ends up in software? */
_this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
_this->SetWindowFramebufferVSync = SDL_SetWindowTextureVSync;
_this->GetWindowFramebufferVSync = SDL_GetWindowTextureVSync;
_this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
_this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
created_framebuffer = SDL_TRUE;
}
}
}
@@ -3310,6 +3306,7 @@ static SDL_Surface *SDL_CreateWindowFramebuffer(SDL_Window *window)
if (!created_framebuffer) {
if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
SDL_SetError("Window framebuffer support not available");
return NULL;
}
@@ -3319,6 +3316,7 @@ static SDL_Surface *SDL_CreateWindowFramebuffer(SDL_Window *window)
}
if (window->surface) {
/* We may have gone recursive and already created the surface */
return window->surface;
}
@@ -3337,7 +3335,12 @@ SDL_Surface *SDL_GetWindowSurface(SDL_Window *window)
CHECK_WINDOW_MAGIC(window, NULL);
if (!window->surface_valid) {
SDL_DestroyWindowSurface(window);
if (window->surface) {
window->surface->flags &= ~SDL_DONTFREE;
SDL_DestroySurface(window->surface);
window->surface = NULL;
}
window->surface = SDL_CreateWindowFramebuffer(window);
if (window->surface) {
window->surface_valid = SDL_TRUE;
@@ -3394,6 +3397,25 @@ int SDL_UpdateWindowSurfaceRects(SDL_Window *window, const SDL_Rect *rects,
return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
}
int SDL_DestroyWindowSurface(SDL_Window *window)
{
CHECK_WINDOW_MAGIC(window, -1);
if (window->surface) {
window->surface->flags &= ~SDL_DONTFREE;
SDL_DestroySurface(window->surface);
window->surface = NULL;
window->surface_valid = SDL_FALSE;
}
if (_this->checked_texture_framebuffer) { /* never checked? No framebuffer to destroy. Don't risk calling the wrong implementation. */
if (_this->DestroyWindowFramebuffer) {
_this->DestroyWindowFramebuffer(_this, window);
}
}
return 0;
}
int SDL_SetWindowOpacity(SDL_Window *window, float opacity)
{
int retval;
@@ -3417,19 +3439,6 @@ int SDL_SetWindowOpacity(SDL_Window *window, float opacity)
return retval;
}
int SDL_DestroyWindowSurface(SDL_Window *window)
{
CHECK_WINDOW_MAGIC(window, -1);
if (window->surface) {
window->surface->flags &= ~SDL_DONTFREE;
SDL_DestroySurface(window->surface);
window->surface = NULL;
window->surface_valid = SDL_FALSE;
}
return 0;
}
int SDL_GetWindowOpacity(SDL_Window *window, float *out_opacity)
{
CHECK_WINDOW_MAGIC(window, -1);
@@ -3922,12 +3931,6 @@ void SDL_DestroyWindow(SDL_Window *window)
SDL_DestroyWindowSurface(window);
if (_this->checked_texture_framebuffer) { /* never checked? No framebuffer to destroy. Don't risk calling the wrong implementation. */
if (_this->DestroyWindowFramebuffer) {
_this->DestroyWindowFramebuffer(_this, window);
}
}
/* Make no context current if this is the current context window */
if (window->flags & SDL_WINDOW_OPENGL) {
if (_this->current_glwin == window) {