Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
/ SDL Public archive

Commit

Permalink
video: Don't check if we can use a "texture framebuffer" until needed.
Browse files Browse the repository at this point in the history
This prevents SDL from making an OpenGL context and maybe throwing it away
immediately by default. It will now only do it when trying to request a
window framebuffer directly, or creating an SDL_Renderer with the "software"
backend, which makes that request itself.

The way SDL decides if it should use a "texture framebuffer" needs dramatic
updating, but this solves the immediate problem.

Reference Issue #4624.
  • Loading branch information
icculus committed Jan 26, 2022
1 parent 71e06a5 commit f37e4a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/video/SDL_sysvideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ struct SDL_VideoDevice

/* * * */
/* Data common to all drivers */
SDL_bool checked_texture_framebuffer;
SDL_bool is_dummy;
SDL_bool suspend_screensaver;
SDL_Window *wakeup_window;
Expand Down
46 changes: 35 additions & 11 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,27 @@ SDL_DestroyWindowTexture(SDL_VideoDevice *unused, SDL_Window * window)
}


/* This will switch the video backend from using a software surface to
using a GPU texture through the 2D render API, if we think this would
be more efficient. This only checks once, on demand. */
static void
PrepareWindowFramebuffer()
{
/* Add the renderer framebuffer emulation if desired */
if (_this->checked_texture_framebuffer) {
return;
}

_this->checked_texture_framebuffer = SDL_TRUE;

if (ShouldUseTextureFramebuffer()) {
_this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
_this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
_this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
}
}


static int
cmpmodes(const void *A, const void *B)
{
Expand Down Expand Up @@ -564,13 +585,6 @@ SDL_VideoInit(const char *driver_name)
return SDL_SetError("The video driver did not add any displays");
}

/* Add the renderer framebuffer emulation if desired */
if (ShouldUseTextureFramebuffer()) {
_this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
_this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
_this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
}

/* Disable the screen saver by default. This is a change from <= 2.0.1,
but most things using SDL are games or media players; you wouldn't
want a screensaver to trigger if you're playing exclusively with a
Expand Down Expand Up @@ -1827,9 +1841,13 @@ SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
window->surface = NULL;
window->surface_valid = SDL_FALSE;
}
if (_this->DestroyWindowFramebuffer) {
_this->DestroyWindowFramebuffer(_this, 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 (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
_this->DestroyWindow(_this, window);
}
Expand Down Expand Up @@ -2547,6 +2565,8 @@ SDL_CreateWindowFramebuffer(SDL_Window * window)
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;

PrepareWindowFramebuffer();

if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
return NULL;
}
Expand Down Expand Up @@ -2610,6 +2630,8 @@ SDL_UpdateWindowSurfaceRects(SDL_Window * window, const SDL_Rect * rects,
return SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
}

SDL_assert(_this->checked_texture_framebuffer); /* we should have done this before we had a valid surface. */

return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
}

Expand Down Expand Up @@ -3138,8 +3160,10 @@ SDL_DestroyWindow(SDL_Window * window)
window->surface = NULL;
window->surface_valid = SDL_FALSE;
}
if (_this->DestroyWindowFramebuffer) {
_this->DestroyWindowFramebuffer(_this, 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 (_this->DestroyWindow) {
_this->DestroyWindow(_this, window);
Expand Down

0 comments on commit f37e4a9

Please sign in to comment.