libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.71k stars 1.8k forks source link

SDL3, macOS: Race condition when resizing window #11048

Open andreasgrabher opened 1 week ago

andreasgrabher commented 1 week ago

There seems to be a race condition with resize events and the window size reported by SDL_GetWindowSize().

If an event of type SDL_EVENT_WINDOW_RESIZED occurs my application executes this code to maintain aspect ratio:

void Screen_SizeChanged(void) {
    float scale;
    int h;

    SDL_GetWindowSize(sdlWindow, NULL, &h);
    scale = (float)h / height;
    SDL_SetWindowSize(sdlWindow, width*scale, h);
    SDL_SetRenderScale(sdlRenderer, scale*dpiFactor, scale*dpiFactor);
}

This works for most part but fails sometimes (see appended video).

Btw. I am not using the fixed aspect ratio feature because of this bug: https://github.com/libsdl-org/SDL/issues/9698

https://github.com/user-attachments/assets/b3972d8d-4871-4753-8114-bf00feef7d32

Kontrabant commented 1 week ago

Out of curiosity, if you add SDL_SyncWindow(sdlWindow); right after the SDL_SetWindowSize() call, does the bug still occur?

andreasgrabher commented 1 week ago

Still the same with SDL_SyncWindow(). I may have to mention it only happens if rendering is done on a secondary thread. The function mentioned in the opening post is called from the main thread as is the creation of the window and renderer.

slime73 commented 1 week ago

I may have to mention it only happens if rendering is done on a secondary thread.

While the bug might not be related to SDL_Render directly, it's probably worth mentioning SDL_Render is only supported in the main thread: https://wiki.libsdl.org/SDL3/CategoryRender

andreasgrabher commented 1 week ago

I know, but I have used threaded rendering on macOS ever since with SDL2 without any issues. Using SDL2 it also works on Windows. This bug makes rendering from the main thread a pain on macOS: https://github.com/libsdl-org/SDL/issues/10475

Energy impact of my application is more than doubled and performance is poor because of that issue.

Btw.: It seems that the crash described in https://github.com/libsdl-org/SDL/issues/9698 is gone. But using fixed aspect ratio does not work around the problem described in the initial post.

andreasgrabher commented 1 week ago

Update: This issue is only present when using gpu renderer. Using metal renderer all seems to be fine.