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

(cherry picked from commit a522bfe3f1)
This commit is contained in:
Sam Lantinga
2024-06-29 02:34:17 -07:00
parent a0ec7c0906
commit a3f0c373d3
2 changed files with 143 additions and 64 deletions

View File

@@ -2181,6 +2181,76 @@ int video_setWindowCenteredOnDisplay(void *arg)
return TEST_COMPLETED;
}
/**
* Tests window surface functionality
*/
static int video_getWindowSurface(void *arg)
{
const char *title = "video_getWindowSurface Test Window";
SDL_Window *window;
SDL_Surface *surface;
SDL_Renderer *renderer;
Uint32 renderer_flags = SDL_RENDERER_ACCELERATED;
int result;
if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "dummy") == 0) {
renderer_flags = SDL_RENDERER_SOFTWARE;
}
/* Make sure we're testing interaction with an accelerated renderer */
SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "1");
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 320, 0);
SDLTest_AssertPass("Call to SDL_CreateWindow('%s', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 320, 0)", title);
SDLTest_AssertCheck(window != NULL, "Validate that returned window is not NULL");
surface = SDL_GetWindowSurface(window);
SDLTest_AssertPass("Call to SDL_GetWindowSurface(window)");
SDLTest_AssertCheck(surface != NULL, "Validate that returned surface is not NULL");
SDLTest_AssertCheck(SDL_HasWindowSurface(window), "Validate that window has a surface");
result = SDL_UpdateWindowSurface(window);
SDLTest_AssertPass("Call to SDL_UpdateWindowSurface(window)");
SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0, got: %d", result);
/* We shouldn't be able to create a renderer on a window with a surface */
renderer = SDL_CreateRenderer(window, -1, renderer_flags);
SDLTest_AssertPass("Call to SDL_CreateRenderer(window, -1, 0x%x)", renderer_flags);
SDLTest_AssertCheck(renderer == NULL, "Validate that returned renderer is NULL");
result = SDL_DestroyWindowSurface(window);
SDLTest_AssertPass("Call to SDL_DestroyWindowSurface(window)");
SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0, got: %d", result);
SDLTest_AssertCheck(!SDL_HasWindowSurface(window), "Validate that window does not have a surface");
/* We should be able to create a renderer on the window now */
renderer = SDL_CreateRenderer(window, -1, renderer_flags);
SDLTest_AssertPass("Call to SDL_CreateRenderer(window, -1, 0x%x)", renderer_flags);
SDLTest_AssertCheck(renderer != NULL, "Validate that returned renderer is not NULL");
/* We should not be able to create a window surface now, unless it was created by the renderer */
if (!SDL_HasWindowSurface(window)) {
surface = SDL_GetWindowSurface(window);
SDLTest_AssertPass("Call to SDL_GetWindowSurface(window)");
SDLTest_AssertCheck(surface == NULL, "Validate that returned surface is NULL");
}
SDL_DestroyRenderer(renderer);
SDLTest_AssertPass("Call to SDL_DestroyRenderer(renderer)");
SDLTest_AssertCheck(!SDL_HasWindowSurface(window), "Validate that window does not have a surface");
/* We should be able to create a window surface again */
surface = SDL_GetWindowSurface(window);
SDLTest_AssertPass("Call to SDL_GetWindowSurface(window)");
SDLTest_AssertCheck(surface != NULL, "Validate that returned surface is not NULL");
SDLTest_AssertCheck(SDL_HasWindowSurface(window), "Validate that window has a surface");
/* Clean up */
SDL_DestroyWindow(window);
return TEST_COMPLETED;
}
/* ================= Test References ================== */
/* Video test cases */
@@ -2265,13 +2335,17 @@ static const SDLTest_TestCaseReference videoTest23 =
static const SDLTest_TestCaseReference videoTest24 =
{ (SDLTest_TestCaseFp) video_setWindowCenteredOnDisplay, "video_setWindowCenteredOnDisplay", "Checks using SDL_WINDOWPOS_CENTERED_DISPLAY centers the window on a display", TEST_ENABLED };
static const SDLTest_TestCaseReference videoTest25 = {
(SDLTest_TestCaseFp)video_getWindowSurface, "video_getWindowSurface", "Checks window surface functionality", TEST_ENABLED
};
/* Sequence of Video test cases */
static const SDLTest_TestCaseReference *videoTests[] = {
&videoTest1, &videoTest2, &videoTest3, &videoTest4, &videoTest5, &videoTest6,
&videoTest7, &videoTest8, &videoTest9, &videoTest10, &videoTest11, &videoTest12,
&videoTest13, &videoTest14, &videoTest15, &videoTest16, &videoTest17,
&videoTest18, &videoTest19, &videoTest20, &videoTest21, &videoTest22,
&videoTest23, &videoTest24, NULL
&videoTest23, &videoTest24, &videoTest25, NULL
};
/* Video test suite (global) */