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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 42cafee..0930d80 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -24,6 +24,7 @@
#include "SDL_hints.h"
#include "SDL_render.h"
+#include "SDL_timer.h"
#include "SDL_sysrender.h"
#include "software/SDL_render_sw_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);
@@ -4260,9 +4269,38 @@ SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
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
SDL_RenderPresent(SDL_Renderer * renderer)
{
+ SDL_bool presented = SDL_TRUE;
+
CHECK_RENDERER_MAGIC(renderer, );
FlushRenderCommands(renderer); /* time to send everything to the GPU! */
@@ -4270,11 +4308,17 @@ SDL_RenderPresent(SDL_Renderer * renderer)
#if DONT_DRAW_WHILE_HIDDEN
/* Don't present while we're hidden */
if (renderer->hidden) {
- return;
- }
+ presented = SDL_FALSE;
+ } else
#endif
+ if (renderer->RenderPresent(renderer) < 0) {
+ presented = SDL_FALSE;
+ }
- renderer->RenderPresent(renderer);
+ if (renderer->simulate_vsync ||
+ (!presented && renderer->wanted_vsync)) {
+ SDL_RenderSimulateVSync(renderer);
+ }
}
void
@@ -4530,10 +4574,15 @@ SDL_RenderSetVSync(SDL_Renderer * renderer, int vsync)
return SDL_Unsupported();
}
- if (renderer->SetVSync) {
- return renderer->SetVSync(renderer, vsync);
+ renderer->wanted_vsync = vsync ? SDL_TRUE : SDL_FALSE;
+
+ 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: */
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 4dcb98a..6deb5b2 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -188,7 +188,7 @@ struct SDL_Renderer
int (*SetRenderTarget) (SDL_Renderer * renderer, SDL_Texture * texture);
int (*RenderReadPixels) (SDL_Renderer * renderer, const SDL_Rect * rect,
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 (*DestroyRenderer) (SDL_Renderer * renderer);
@@ -208,6 +208,11 @@ struct SDL_Renderer
SDL_Window *window;
SDL_bool hidden;
+ /* Whether we should simulate vsync */
+ SDL_bool wanted_vsync;
+ SDL_bool simulate_vsync;
+ Uint32 last_present;
+
/* The logical resolution for rendering */
int logical_w;
int logical_h;
diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index ae83009..cad5536 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -1384,7 +1384,7 @@ D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status;
}
-static void
+static int
D3D_RenderPresent(SDL_Renderer * renderer)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
@@ -1398,15 +1398,16 @@ D3D_RenderPresent(SDL_Renderer * renderer)
result = IDirect3DDevice9_TestCooperativeLevel(data->device);
if (result == D3DERR_DEVICELOST) {
/* We'll reset later */
- return;
+ return -1;
}
if (result == D3DERR_DEVICENOTRESET) {
D3D_Reset(renderer);
}
result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
if (FAILED(result)) {
- D3D_SetError("Present()", result);
+ return D3D_SetError("Present()", result);
}
+ return 0;
}
static void
diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index f7dfefe..8369e54 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -2288,7 +2288,7 @@ done:
return status;
}
-static void
+static int
D3D11_RenderPresent(SDL_Renderer * renderer)
{
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
*/
- if ( result == DXGI_ERROR_DEVICE_REMOVED ) {
+ if (result == DXGI_ERROR_DEVICE_REMOVED) {
D3D11_HandleDeviceLost(renderer);
} else if (result == DXGI_ERROR_INVALID_CALL) {
/* We probably went through a fullscreen <-> windowed transition */
@@ -2342,7 +2342,9 @@ D3D11_RenderPresent(SDL_Renderer * renderer)
} else {
WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result);
}
+ return -1;
}
+ return 0;
}
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
index 3fde465..e5cbd4c 100644
--- a/src/render/direct3d12/SDL_render_d3d12.c
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -2932,7 +2932,7 @@ done:
return status;
}
-static void
+static int
D3D12_RenderPresent(SDL_Renderer * renderer)
{
D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
@@ -2982,6 +2982,7 @@ D3D12_RenderPresent(SDL_Renderer * renderer)
} else {
WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result);
}
+ return -1;
} else {
/* Wait for the GPU and move to the next frame */
result = D3D_CALL(data->commandQueue, Signal, data->fence, data->fenceValue);
@@ -3013,6 +3014,7 @@ D3D12_RenderPresent(SDL_Renderer * renderer)
#if defined(__XBOXONE__) || defined(__XBOXSERIES__)
D3D12_XBOX_StartFrame(data->d3dDevice, &data->frameToken);
#endif
+ return 0;
}
}
diff --git a/src/render/metal/SDL_render_metal.m b/src/render/metal/SDL_render_metal.m
index fdd0438..0532890 100644
--- a/src/render/metal/SDL_render_metal.m
+++ b/src/render/metal/SDL_render_metal.m
@@ -1497,7 +1497,7 @@ METAL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status;
}}
-static void
+static int
METAL_RenderPresent(SDL_Renderer * renderer)
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
@@ -1528,6 +1528,11 @@ METAL_RenderPresent(SDL_Renderer * renderer)
data.mtlcmdencoder = nil;
data.mtlcmdbuffer = nil;
data.mtlbackbuffer = nil;
+
+ if (!ready) {
+ return -1;
+ }
+ return 0;
}}
static void
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index a3f5767..c55c387 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -22,6 +22,7 @@
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
+#include "../../video/SDL_sysvideo.h" /* For SDL_GL_SwapWindowWithResult */
#include "SDL_opengl.h"
#include "../SDL_sysrender.h"
#include "SDL_shaders_gl.h"
@@ -1502,12 +1503,12 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status;
}
-static void
+static int
GL_RenderPresent(SDL_Renderer * renderer)
{
GL_ActivateRenderer(renderer);
- SDL_GL_SwapWindow(renderer->window);
+ return SDL_GL_SwapWindowWithResult(renderer->window);
}
static void
diff --git a/src/render/opengles/SDL_render_gles.c b/src/render/opengles/SDL_render_gles.c
index 5886449..a5fbab3 100644
--- a/src/render/opengles/SDL_render_gles.c
+++ b/src/render/opengles/SDL_render_gles.c
@@ -23,6 +23,7 @@
#if SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
+#include "../../video/SDL_sysvideo.h" /* For SDL_GL_SwapWindowWithResult */
#include "SDL_opengles.h"
#include "../SDL_sysrender.h"
#include "../../SDL_utils_c.h"
@@ -948,12 +949,12 @@ GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status;
}
-static void
+static int
GLES_RenderPresent(SDL_Renderer * renderer)
{
GLES_ActivateRenderer(renderer);
- SDL_GL_SwapWindow(renderer->window);
+ return SDL_GL_SwapWindowWithResult(renderer->window);
}
static void
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index 7606262..8718f79 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -23,6 +23,7 @@
#if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
+#include "../../video/SDL_sysvideo.h" /* For SDL_GL_SwapWindowWithResult */
#include "SDL_opengles2.h"
#include "../SDL_sysrender.h"
#include "../../video/SDL_blit.h"
@@ -1983,11 +1984,11 @@ GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return status;
}
-static void
+static int
GLES2_RenderPresent(SDL_Renderer *renderer)
{
/* Tell the video driver to swap buffers */
- SDL_GL_SwapWindow(renderer->window);
+ return SDL_GL_SwapWindowWithResult(renderer->window);
}
static int
diff --git a/src/render/ps2/SDL_render_ps2.c b/src/render/ps2/SDL_render_ps2.c
index ec89663..ad25306 100644
--- a/src/render/ps2/SDL_render_ps2.c
+++ b/src/render/ps2/SDL_render_ps2.c
@@ -654,7 +654,7 @@ PS2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return SDL_Unsupported();
}
-static void
+static int
PS2_RenderPresent(SDL_Renderer * renderer)
{
PS2_RenderData *data = (PS2_RenderData *) renderer->driverdata;
@@ -676,6 +676,7 @@ PS2_RenderPresent(SDL_Renderer * renderer)
}
gsKit_TexManager_nextFrame(data->gsGlobal);
gsKit_clear(data->gsGlobal, GS_BLACK);
+ return 0;
}
static void
diff --git a/src/render/psp/SDL_render_psp.c b/src/render/psp/SDL_render_psp.c
index cdc0b91..6f08440 100644
--- a/src/render/psp/SDL_render_psp.c
+++ b/src/render/psp/SDL_render_psp.c
@@ -1250,12 +1250,13 @@ PSP_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return SDL_Unsupported();
}
-static void
+static int
PSP_RenderPresent(SDL_Renderer * renderer)
{
PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
- if(!data->displayListAvail)
- return;
+ if (!data->displayListAvail) {
+ return -1;
+ }
data->displayListAvail = SDL_FALSE;
sceGuFinish();
@@ -1268,6 +1269,7 @@ PSP_RenderPresent(SDL_Renderer * renderer)
data->backbuffer = data->frontbuffer;
data->frontbuffer = vabsptr(sceGuSwapBuffers());
+ return 0;
}
static void
diff --git a/src/render/software/SDL_render_sw.c b/src/render/software/SDL_render_sw.c
index 34092b5..874f92b 100644
--- a/src/render/software/SDL_render_sw.c
+++ b/src/render/software/SDL_render_sw.c
@@ -983,14 +983,15 @@ SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
format, pixels, pitch);
}
-static void
+static int
SW_RenderPresent(SDL_Renderer * renderer)
{
SDL_Window *window = renderer->window;
- if (window) {
- SDL_UpdateWindowSurface(window);
+ if (!window) {
+ return -1;
}
+ return SDL_UpdateWindowSurface(window);
}
static void
diff --git a/src/render/vitagxm/SDL_render_vita_gxm.c b/src/render/vitagxm/SDL_render_vita_gxm.c
index a4b2cee..b76a91b 100644
--- a/src/render/vitagxm/SDL_render_vita_gxm.c
+++ b/src/render/vitagxm/SDL_render_vita_gxm.c
@@ -100,7 +100,7 @@ static int VITA_GXM_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rec
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_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_RenderData *data = (VITA_GXM_RenderData *) renderer->driverdata;
@@ -1227,6 +1227,7 @@ VITA_GXM_RenderPresent(SDL_Renderer *renderer)
data->pool_index = 0;
data->current_pool = (data->current_pool + 1) % 2;
+ return 0;
}
static void
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 4f602d3..7202f1d 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -519,6 +519,8 @@ extern void SDL_ToggleDragAndDropSupport(void);
extern int SDL_GetPointDisplayIndex(const SDL_Point * point);
+extern int SDL_GL_SwapWindowWithResult(SDL_Window * window);
+
#endif /* SDL_sysvideo_h_ */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 3f4cfd7..992a76d 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -4152,22 +4152,26 @@ SDL_GL_GetSwapInterval(void)
}
}
-void
-SDL_GL_SwapWindow(SDL_Window * window)
+int
+SDL_GL_SwapWindowWithResult(SDL_Window * window)
{
- CHECK_WINDOW_MAGIC(window,);
+ CHECK_WINDOW_MAGIC(window, -1);
if (!(window->flags & SDL_WINDOW_OPENGL)) {
- SDL_SetError("The specified window isn't an OpenGL window");
- return;
+ return SDL_SetError("The specified window isn't an OpenGL window");
}
if (SDL_GL_GetCurrentWindow() != window) {
- SDL_SetError("The specified window has not been made current");
- return;
+ return SDL_SetError("The specified window has not been made current");
}
- _this->GL_SwapWindow(_this, window);
+ return _this->GL_SwapWindow(_this, window);
+}
+
+void
+SDL_GL_SwapWindow(SDL_Window * window)
+{
+ SDL_GL_SwapWindowWithResult(window);
}
void