From 994312c2120c1e4a7a363d9cb7e941d8c892f7ef Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 16 May 2020 16:25:13 -0700 Subject: [PATCH] Force GL to block after the current frame instead during the next frame --- .../video/ffmpeg-renderers/eglvid.cpp | 18 +++++++++++++++++- app/streaming/video/ffmpeg-renderers/eglvid.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/streaming/video/ffmpeg-renderers/eglvid.cpp b/app/streaming/video/ffmpeg-renderers/eglvid.cpp index a02ab582..58bb62af 100644 --- a/app/streaming/video/ffmpeg-renderers/eglvid.cpp +++ b/app/streaming/video/ffmpeg-renderers/eglvid.cpp @@ -70,6 +70,7 @@ EGLRenderer::EGLRenderer(IFFmpegRenderer *backendRenderer) m_VAO(0), m_ColorSpace(AVCOL_SPC_NB), m_ColorFull(false), + m_BlockingSwapBuffers(false), m_glEGLImageTargetTexture2DOES(nullptr), m_glGenVertexArraysOES(nullptr), m_glBindVertexArrayOES(nullptr), @@ -404,8 +405,10 @@ bool EGLRenderer::initialize(PDECODER_PARAMETERS params) // Try to use adaptive VSYNC unless we're using frame pacing. // Frame pacing relies on us blocking in renderFrame() to // match the display refresh rate. - if (params->enableFramePacing || SDL_GL_SetSwapInterval(-1)) + if (params->enableFramePacing || SDL_GL_SetSwapInterval(-1)) { SDL_GL_SetSwapInterval(1); + m_BlockingSwapBuffers = true; + } } else { SDL_GL_SetSwapInterval(0); } @@ -606,6 +609,19 @@ void EGLRenderer::renderFrame(AVFrame* frame) glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); SDL_GL_SwapWindow(m_Window); + + if (m_BlockingSwapBuffers) { + // This glClear() forces us to block until the buffer swap is + // complete to continue rendering. Mesa won't actually wait + // for the swap with just glFinish() alone. Waiting here keeps us + // in lock step with the display refresh rate. If we don't wait + // here, we'll stall on the first GL call next frame. Doing the + // wait here instead allows more time for a newer frame to arrive + // for next renderFrame() call. + glClear(GL_COLOR_BUFFER_BIT); + glFinish(); + } + if (frame->hw_frames_ctx != nullptr) m_Backend->freeEGLImages(m_EGLDisplay, imgs); } diff --git a/app/streaming/video/ffmpeg-renderers/eglvid.h b/app/streaming/video/ffmpeg-renderers/eglvid.h index d64b82e7..13fc2d13 100644 --- a/app/streaming/video/ffmpeg-renderers/eglvid.h +++ b/app/streaming/video/ffmpeg-renderers/eglvid.h @@ -33,6 +33,7 @@ private: unsigned int m_VAO; int m_ColorSpace; bool m_ColorFull; + bool m_BlockingSwapBuffers; PFNGLEGLIMAGETARGETTEXTURE2DOESPROC m_glEGLImageTargetTexture2DOES; PFNGLGENVERTEXARRAYSOESPROC m_glGenVertexArraysOES; PFNGLBINDVERTEXARRAYOESPROC m_glBindVertexArrayOES;