We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5465

Summary: [KMSDRM] Avoid new windows to be created
Product: SDL Reporter: Manuel Alfayate Corchete <redwindwanderer>
Component: videoAssignee: Manuel Alfayate Corchete <redwindwanderer>
Status: ASSIGNED --- QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: 2.0.15   
Hardware: x86_64   
OS: Linux   
Attachments: Patch to prevent programs from creating more than 1 window on KMSDRM
V2 of the patch. Cursor code changes moved to a different issue.

Description Manuel Alfayate Corchete 2021-01-15 10:55:58 UTC
Hi there,

Since the KMSDRM backend window creation, swapping, etc.., works DIRECTLY with the graphics card file descriptor, and we don't have a graphics server, only a working SDL_window can be created.

For example, two windows could request a buffer swap, but since waiting on a pageflip uses file descriptor events, it wouldn't work well for two windows requiring pageflips.

So, before the next release, I would like to avoid the creation of more than one window, as a prevention. Is that OK?

It would just be a matter of checking the number of windows in the windows list in KMSDRM_CreateWindow() and return if there's a window already.
Comment 1 Sam Lantinga 2021-01-15 17:11:33 UTC
Yes, that's fine, please attach a patch for review. In general, feel free to attach patches when you open issues on Bugzilla so they can be reviewed and approved in one step.
Comment 2 Manuel Alfayate Corchete 2021-01-15 21:11:56 UTC
Created attachment 4667 [details]
Patch to prevent programs from creating more than 1 window on KMSDRM

Here's a patch.

It also makes a minor adjustment at the end of KMSDRM_DestroyWindow() and a small refactor on the cursor buffer creation.
Comment 3 Manuel Alfayate Corchete 2021-01-15 21:53:34 UTC
Created attachment 4670 [details]
V2 of the patch. Cursor code changes moved to a different issue.

I have done a second version of the patch because some more changes have been made to the cursor code, to fix another different issue, so changes related to that fix are in that bug report.
Comment 4 Sam Lantinga 2021-01-19 16:49:47 UTC
Looks good, go ahead and commit.
Comment 5 Manuel Alfayate Corchete 2021-01-19 20:36:49 UTC
@Sam Before commiting, I would like to ask you a question.
I am thinking about adding support for multiple displays (I could create a different SDL_Display for each video file descriptor: In Rpi4, which has two HDMI outputs, there's /dev/dri/card0 and /dev/dri/card1, so for each file descriptor I can create a different GBM DEV, and I can flip them independently too).

Thing is, what's the relationship between SDL_Displays and SDL_Windows? I mean: I know each SDL_Window has an SDL_Display member (ie, it's displayed on a display), but does SDL2 expect multiple windows on the same display, for example?
If that's the case, I may still be limiting the window number to 1, but I can add multiple displays anyway so a single window could occupy 2 displays, for example.
I believe SDL2 expects to be able to do that: create a window that's part in a display and part in another.
Comment 6 Sam Lantinga 2021-01-19 22:48:49 UTC
Yes, SDL expects to be able to create windows on any display and potentially span displays.
Comment 7 Manuel Alfayate Corchete 2021-01-20 09:51:18 UTC
@Sam

Ok, as things work on non-atomic KMSDRM, I can have as many windows as displays, and as many displays as video card FDs on the machine.

So, for example, on a Pi4 I can have two FDs, hence two displays, hence two windows.

I am currently initializing the SDL_displays in SDL_VideoInit().
Let's say I have two of them.
Does SDL assign each window to a display internally if I don't do it in KMSDRM_CreateWindow? 

In other words, in KMSDRM_CreateWindow(), I get the window display here:
https://hg.libsdl.org/SDL/file/d4c15ecfcf62/src/video/kmsdrm/SDL_kmsdrmvideo.c#l1074

...But I have never assigned that. How does SDL manage the SDL_window->SDL_display relationhip internally? First window->first display? Then second window -> first display too?

Reading the first answer here:

https://stackoverflow.com/questions/41745492/sdl2-how-to-position-a-window-on-a-second-monitor

...I understand that SDL2 uses a global coord system, so it assigns window->display internally depending on the window position on that coord system. Is that right?

If that's the case, that doesn't look like it will work with my current trick of programming a different video mode for smaller-than-native window sizes...
Comment 8 Sam Lantinga 2021-01-20 18:32:21 UTC
Yes, that's correct. The coordinate space is the display's normal resolution, and it's expected that the monitor will change resolution to match fullscreen windows, so I think you're okay there.
Comment 9 Manuel Alfayate Corchete 2021-01-20 21:48:45 UTC
(In reply to Sam Lantinga from comment #8)
> Yes, that's correct. The coordinate space is the display's normal
> resolution, and it's expected that the monitor will change resolution to
> match fullscreen windows, so I think you're okay there.

But I am also changing resolution to display NON-FULLSCREEN windows (because having the image on a tiny area only looks ugly without a windowing system: it's mostly a cosmetic hack, so to say).
Maybe I should NOT change resolution for NON-FULLSCREEN windows so I don't "break the API"?
Comment 10 Sam Lantinga 2021-01-20 22:45:06 UTC
Yes, non-fullscreen windows should be just that - non-fullscreen. :)
Comment 11 Manuel Alfayate Corchete 2021-01-21 02:33:58 UTC
@Sam For the non-fullscreen windows being non-fullscreen, I have opened a new issue with a patch that does precissely that:

https://bugzilla.libsdl.org/show_bug.cgi?id=5489