Added support for simulated vsync in the renderer

This kicks in if the platform doesn't support vsync directly, or if the present fails for some reason (e.g. minimized on some platforms)

Fixes https://github.com/libsdl-org/SDL/issues/5134
This commit is contained in:
Sam Lantinga 2022-09-15 01:00:12 -07:00
parent 339f7a2f6b
commit d744aafb05
15 changed files with 115 additions and 37 deletions

View File

@ -24,6 +24,7 @@
#include "SDL_hints.h" #include "SDL_hints.h"
#include "SDL_render.h" #include "SDL_render.h"
#include "SDL_timer.h"
#include "SDL_sysrender.h" #include "SDL_sysrender.h"
#include "software/SDL_render_sw_c.h" #include "software/SDL_render_sw_c.h"
#include "../video/SDL_pixels_c.h" #include "../video/SDL_pixels_c.h"
@ -1013,6 +1014,14 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
} }
} }
if ((flags & SDL_RENDERER_PRESENTVSYNC) != 0) {
renderer->wanted_vsync = SDL_TRUE;
if ((renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) == 0) {
renderer->simulate_vsync = SDL_TRUE;
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
}
}
VerifyDrawQueueFunctions(renderer); VerifyDrawQueueFunctions(renderer);
@ -4260,9 +4269,38 @@ SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
format, pixels, pitch); format, pixels, pitch);
} }
static void
SDL_RenderSimulateVSync(SDL_Renderer * renderer)
{
Uint32 now, elapsed;
const Uint32 interval = (1000 / 60); /* FIXME: What FPS? */
if (!interval) {
/* We can't do sub-ms delay, so just return here */
return;
}
now = SDL_GetTicks();
elapsed = (now - renderer->last_present);
if (elapsed < interval) {
Uint32 duration = (interval - elapsed);
SDL_Delay(duration);
now = SDL_GetTicks();
}
if (renderer->last_present) {
elapsed = (now - renderer->last_present);
renderer->last_present += (elapsed / interval) * interval;
} else {
renderer->last_present = now;
}
}
void void
SDL_RenderPresent(SDL_Renderer * renderer) SDL_RenderPresent(SDL_Renderer * renderer)
{ {
SDL_bool presented = SDL_TRUE;
CHECK_RENDERER_MAGIC(renderer, ); CHECK_RENDERER_MAGIC(renderer, );
FlushRenderCommands(renderer); /* time to send everything to the GPU! */ FlushRenderCommands(renderer); /* time to send everything to the GPU! */
@ -4270,11 +4308,17 @@ SDL_RenderPresent(SDL_Renderer * renderer)
#if DONT_DRAW_WHILE_HIDDEN #if DONT_DRAW_WHILE_HIDDEN
/* Don't present while we're hidden */ /* Don't present while we're hidden */
if (renderer->hidden) { if (renderer->hidden) {
return; presented = SDL_FALSE;
} } else
#endif #endif
if (renderer->RenderPresent(renderer) < 0) {
presented = SDL_FALSE;
}
renderer->RenderPresent(renderer); if (renderer->simulate_vsync ||
(!presented && renderer->wanted_vsync)) {
SDL_RenderSimulateVSync(renderer);
}
} }
void void
@ -4530,10 +4574,15 @@ SDL_RenderSetVSync(SDL_Renderer * renderer, int vsync)
return SDL_Unsupported(); return SDL_Unsupported();
} }
if (renderer->SetVSync) { renderer->wanted_vsync = vsync ? SDL_TRUE : SDL_FALSE;
return renderer->SetVSync(renderer, vsync);
if (!renderer->SetVSync ||
renderer->SetVSync(renderer, vsync) < 0) {
renderer->simulate_vsync = vsync ? SDL_TRUE : SDL_FALSE;
} else {
renderer->simulate_vsync = SDL_FALSE;
} }
return SDL_Unsupported(); return 0;
} }
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -188,7 +188,7 @@ struct SDL_Renderer
int (*SetRenderTarget) (SDL_Renderer * renderer, SDL_Texture * texture); int (*SetRenderTarget) (SDL_Renderer * renderer, SDL_Texture * texture);
int (*RenderReadPixels) (SDL_Renderer * renderer, const SDL_Rect * rect, int (*RenderReadPixels) (SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch); Uint32 format, void * pixels, int pitch);
void (*RenderPresent) (SDL_Renderer * renderer); int (*RenderPresent) (SDL_Renderer * renderer);
void (*DestroyTexture) (SDL_Renderer * renderer, SDL_Texture * texture); void (*DestroyTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
void (*DestroyRenderer) (SDL_Renderer * renderer); void (*DestroyRenderer) (SDL_Renderer * renderer);
@ -208,6 +208,11 @@ struct SDL_Renderer
SDL_Window *window; SDL_Window *window;
SDL_bool hidden; SDL_bool hidden;
/* Whether we should simulate vsync */
SDL_bool wanted_vsync;
SDL_bool simulate_vsync;
Uint32 last_present;
/* The logical resolution for rendering */ /* The logical resolution for rendering */
int logical_w; int logical_w;
int logical_h; int logical_h;

View File

@ -1384,7 +1384,7 @@ D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status; return status;
} }
static void static int
D3D_RenderPresent(SDL_Renderer * renderer) D3D_RenderPresent(SDL_Renderer * renderer)
{ {
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata; D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
@ -1398,15 +1398,16 @@ D3D_RenderPresent(SDL_Renderer * renderer)
result = IDirect3DDevice9_TestCooperativeLevel(data->device); result = IDirect3DDevice9_TestCooperativeLevel(data->device);
if (result == D3DERR_DEVICELOST) { if (result == D3DERR_DEVICELOST) {
/* We'll reset later */ /* We'll reset later */
return; return -1;
} }
if (result == D3DERR_DEVICENOTRESET) { if (result == D3DERR_DEVICENOTRESET) {
D3D_Reset(renderer); D3D_Reset(renderer);
} }
result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL); result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
if (FAILED(result)) { if (FAILED(result)) {
D3D_SetError("Present()", result); return D3D_SetError("Present()", result);
} }
return 0;
} }
static void static void

View File

@ -2288,7 +2288,7 @@ done:
return status; return status;
} }
static void static int
D3D11_RenderPresent(SDL_Renderer * renderer) D3D11_RenderPresent(SDL_Renderer * renderer)
{ {
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata; D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
@ -2334,7 +2334,7 @@ D3D11_RenderPresent(SDL_Renderer * renderer)
* *
* TODO, WinRT: consider throwing an exception if D3D11_RenderPresent fails, especially if there is a way to salvage debug info from users' machines * TODO, WinRT: consider throwing an exception if D3D11_RenderPresent fails, especially if there is a way to salvage debug info from users' machines
*/ */
if ( result == DXGI_ERROR_DEVICE_REMOVED ) { if (result == DXGI_ERROR_DEVICE_REMOVED) {
D3D11_HandleDeviceLost(renderer); D3D11_HandleDeviceLost(renderer);
} else if (result == DXGI_ERROR_INVALID_CALL) { } else if (result == DXGI_ERROR_INVALID_CALL) {
/* We probably went through a fullscreen <-> windowed transition */ /* We probably went through a fullscreen <-> windowed transition */
@ -2342,7 +2342,9 @@ D3D11_RenderPresent(SDL_Renderer * renderer)
} else { } else {
WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result); WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result);
} }
return -1;
} }
return 0;
} }
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP

View File

@ -2932,7 +2932,7 @@ done:
return status; return status;
} }
static void static int
D3D12_RenderPresent(SDL_Renderer * renderer) D3D12_RenderPresent(SDL_Renderer * renderer)
{ {
D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata; D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
@ -2982,6 +2982,7 @@ D3D12_RenderPresent(SDL_Renderer * renderer)
} else { } else {
WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result); WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result);
} }
return -1;
} else { } else {
/* Wait for the GPU and move to the next frame */ /* Wait for the GPU and move to the next frame */
result = D3D_CALL(data->commandQueue, Signal, data->fence, data->fenceValue); result = D3D_CALL(data->commandQueue, Signal, data->fence, data->fenceValue);
@ -3013,6 +3014,7 @@ D3D12_RenderPresent(SDL_Renderer * renderer)
#if defined(__XBOXONE__) || defined(__XBOXSERIES__) #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
D3D12_XBOX_StartFrame(data->d3dDevice, &data->frameToken); D3D12_XBOX_StartFrame(data->d3dDevice, &data->frameToken);
#endif #endif
return 0;
} }
} }

View File

@ -1497,7 +1497,7 @@ METAL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status; return status;
}} }}
static void static int
METAL_RenderPresent(SDL_Renderer * renderer) METAL_RenderPresent(SDL_Renderer * renderer)
{ @autoreleasepool { { @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata; METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
@ -1528,6 +1528,11 @@ METAL_RenderPresent(SDL_Renderer * renderer)
data.mtlcmdencoder = nil; data.mtlcmdencoder = nil;
data.mtlcmdbuffer = nil; data.mtlcmdbuffer = nil;
data.mtlbackbuffer = nil; data.mtlbackbuffer = nil;
if (!ready) {
return -1;
}
return 0;
}} }}
static void static void

View File

@ -22,6 +22,7 @@
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED #if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
#include "SDL_hints.h" #include "SDL_hints.h"
#include "../../video/SDL_sysvideo.h" /* For SDL_GL_SwapWindowWithResult */
#include "SDL_opengl.h" #include "SDL_opengl.h"
#include "../SDL_sysrender.h" #include "../SDL_sysrender.h"
#include "SDL_shaders_gl.h" #include "SDL_shaders_gl.h"
@ -1502,12 +1503,12 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status; return status;
} }
static void static int
GL_RenderPresent(SDL_Renderer * renderer) GL_RenderPresent(SDL_Renderer * renderer)
{ {
GL_ActivateRenderer(renderer); GL_ActivateRenderer(renderer);
SDL_GL_SwapWindow(renderer->window); return SDL_GL_SwapWindowWithResult(renderer->window);
} }
static void static void

View File

@ -23,6 +23,7 @@
#if SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED #if SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED
#include "SDL_hints.h" #include "SDL_hints.h"
#include "../../video/SDL_sysvideo.h" /* For SDL_GL_SwapWindowWithResult */
#include "SDL_opengles.h" #include "SDL_opengles.h"
#include "../SDL_sysrender.h" #include "../SDL_sysrender.h"
#include "../../SDL_utils_c.h" #include "../../SDL_utils_c.h"
@ -948,12 +949,12 @@ GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status; return status;
} }
static void static int
GLES_RenderPresent(SDL_Renderer * renderer) GLES_RenderPresent(SDL_Renderer * renderer)
{ {
GLES_ActivateRenderer(renderer); GLES_ActivateRenderer(renderer);
SDL_GL_SwapWindow(renderer->window); return SDL_GL_SwapWindowWithResult(renderer->window);
} }
static void static void

View File

@ -23,6 +23,7 @@
#if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED #if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
#include "SDL_hints.h" #include "SDL_hints.h"
#include "../../video/SDL_sysvideo.h" /* For SDL_GL_SwapWindowWithResult */
#include "SDL_opengles2.h" #include "SDL_opengles2.h"
#include "../SDL_sysrender.h" #include "../SDL_sysrender.h"
#include "../../video/SDL_blit.h" #include "../../video/SDL_blit.h"
@ -1983,11 +1984,11 @@ GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status; return status;
} }
static void static int
GLES2_RenderPresent(SDL_Renderer *renderer) GLES2_RenderPresent(SDL_Renderer *renderer)
{ {
/* Tell the video driver to swap buffers */ /* Tell the video driver to swap buffers */
SDL_GL_SwapWindow(renderer->window); return SDL_GL_SwapWindowWithResult(renderer->window);
} }
static int static int

View File

@ -654,7 +654,7 @@ PS2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return SDL_Unsupported(); return SDL_Unsupported();
} }
static void static int
PS2_RenderPresent(SDL_Renderer * renderer) PS2_RenderPresent(SDL_Renderer * renderer)
{ {
PS2_RenderData *data = (PS2_RenderData *) renderer->driverdata; PS2_RenderData *data = (PS2_RenderData *) renderer->driverdata;
@ -676,6 +676,7 @@ PS2_RenderPresent(SDL_Renderer * renderer)
} }
gsKit_TexManager_nextFrame(data->gsGlobal); gsKit_TexManager_nextFrame(data->gsGlobal);
gsKit_clear(data->gsGlobal, GS_BLACK); gsKit_clear(data->gsGlobal, GS_BLACK);
return 0;
} }
static void static void

View File

@ -1250,12 +1250,13 @@ PSP_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return SDL_Unsupported(); return SDL_Unsupported();
} }
static void static int
PSP_RenderPresent(SDL_Renderer * renderer) PSP_RenderPresent(SDL_Renderer * renderer)
{ {
PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata; PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
if(!data->displayListAvail) if (!data->displayListAvail) {
return; return -1;
}
data->displayListAvail = SDL_FALSE; data->displayListAvail = SDL_FALSE;
sceGuFinish(); sceGuFinish();
@ -1268,6 +1269,7 @@ PSP_RenderPresent(SDL_Renderer * renderer)
data->backbuffer = data->frontbuffer; data->backbuffer = data->frontbuffer;
data->frontbuffer = vabsptr(sceGuSwapBuffers()); data->frontbuffer = vabsptr(sceGuSwapBuffers());
return 0;
} }
static void static void

View File

@ -983,14 +983,15 @@ SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
format, pixels, pitch); format, pixels, pitch);
} }
static void static int
SW_RenderPresent(SDL_Renderer * renderer) SW_RenderPresent(SDL_Renderer * renderer)
{ {
SDL_Window *window = renderer->window; SDL_Window *window = renderer->window;
if (window) { if (!window) {
SDL_UpdateWindowSurface(window); return -1;
} }
return SDL_UpdateWindowSurface(window);
} }
static void static void

View File

@ -100,7 +100,7 @@ static int VITA_GXM_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rec
Uint32 pixel_format, void *pixels, int pitch); Uint32 pixel_format, void *pixels, int pitch);
static void VITA_GXM_RenderPresent(SDL_Renderer *renderer); static int VITA_GXM_RenderPresent(SDL_Renderer *renderer);
static void VITA_GXM_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture); static void VITA_GXM_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture);
static void VITA_GXM_DestroyRenderer(SDL_Renderer *renderer); static void VITA_GXM_DestroyRenderer(SDL_Renderer *renderer);
@ -1185,7 +1185,7 @@ VITA_GXM_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
} }
static void static int
VITA_GXM_RenderPresent(SDL_Renderer *renderer) VITA_GXM_RenderPresent(SDL_Renderer *renderer)
{ {
VITA_GXM_RenderData *data = (VITA_GXM_RenderData *) renderer->driverdata; VITA_GXM_RenderData *data = (VITA_GXM_RenderData *) renderer->driverdata;
@ -1227,6 +1227,7 @@ VITA_GXM_RenderPresent(SDL_Renderer *renderer)
data->pool_index = 0; data->pool_index = 0;
data->current_pool = (data->current_pool + 1) % 2; data->current_pool = (data->current_pool + 1) % 2;
return 0;
} }
static void static void

View File

@ -519,6 +519,8 @@ extern void SDL_ToggleDragAndDropSupport(void);
extern int SDL_GetPointDisplayIndex(const SDL_Point * point); extern int SDL_GetPointDisplayIndex(const SDL_Point * point);
extern int SDL_GL_SwapWindowWithResult(SDL_Window * window);
#endif /* SDL_sysvideo_h_ */ #endif /* SDL_sysvideo_h_ */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -4152,22 +4152,26 @@ SDL_GL_GetSwapInterval(void)
} }
} }
void int
SDL_GL_SwapWindow(SDL_Window * window) SDL_GL_SwapWindowWithResult(SDL_Window * window)
{ {
CHECK_WINDOW_MAGIC(window,); CHECK_WINDOW_MAGIC(window, -1);
if (!(window->flags & SDL_WINDOW_OPENGL)) { if (!(window->flags & SDL_WINDOW_OPENGL)) {
SDL_SetError("The specified window isn't an OpenGL window"); return SDL_SetError("The specified window isn't an OpenGL window");
return;
} }
if (SDL_GL_GetCurrentWindow() != window) { if (SDL_GL_GetCurrentWindow() != window) {
SDL_SetError("The specified window has not been made current"); return SDL_SetError("The specified window has not been made current");
return;
} }
_this->GL_SwapWindow(_this, window); return _this->GL_SwapWindow(_this, window);
}
void
SDL_GL_SwapWindow(SDL_Window * window)
{
SDL_GL_SwapWindowWithResult(window);
} }
void void