SDL_GetWindowSurface works with renderer on X11

I couldn’t find anything related to this and the closest post was this:

From the wiki: SDL_GetWindowSurface - SDL Wiki

You may not combine this with 3D or the rendering API on this window.

But it seems to work with X11 and also with a software renderer on wayland,
kmsdrm and X11.

Tested on the git version. Using sway for wayland and X11 (XWayland), dwm for
X11 and linux 5.18.10 for kmsdrm. And if it matters I’m using intel integrated
graphics.

$ sdl2-config --version
2.23.1

Here’s a simple example that causes this problem.

#include <assert.h>
#include "SDL.h"

int
main(void)
{
	int err = SDL_Init(SDL_INIT_VIDEO);
	assert(err >= 0);

	SDL_Window *w = SDL_CreateWindow("bug?", SDL_WINDOWPOS_UNDEFINED,
			SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
	assert(w != NULL);

	SDL_Renderer *r = SDL_CreateRenderer(w, -1, 0
			/* | SDL_RENDERER_SOFTWARE */
			);
	assert(r != NULL);

	SDL_Surface *s = SDL_GetWindowSurface(w);
	if (s == NULL) {
		SDL_Log("%s\n", SDL_GetError());
	}

	SDL_DestroyRenderer(r);
	SDL_DestroyWindow(w);
	SDL_Quit();
	return 0;
}

On wayland and kmsdrm it outputs: INFO: No hardware accelerated renderers available
and on X11 it works without errors.

When SDL_RENDERER_SOFTWARE is uncommented it seems to work fine.

I’m not sure what the “problem” is. The documentation states that you may not use both SDL_CreateRenderer() and SDL_GetWindowSurface() on the same window: you have to choose whether to use the rendering API or the surface API.

It doesn’t say that it definitely won’t work, just that it may not work (for example on some platforms and/or some backends) so you mustn’t rely on it.

So it’s like undefined behaviour and I can’t expect it to be consistent between different backends.

I guess I just misinterpreted the documentation. Thanks for the clarification.

This is almost inevitably the case for any abstraction layer supporting multiple platforms and backends. It can’t make them all behave identically, but it can define a documented subset of features that they will all support.