Explorar o código

properly adhere to fps limits

vaxerski %!s(int64=3) %!d(string=hai) anos
pai
achega
87b765e618

+ 14 - 14
src/WallpaperEngine/Application/CWallpaperApplication.cpp

@@ -334,7 +334,16 @@ namespace WallpaperEngine::Application
         } else {
 #endif
             while (!videoDriver->closeRequested () && this->m_context.state.general.keepRunning) {
+                static float startTime, endTime, minimumTime = 1.0f / this->m_context.settings.render.maximumFPS;
+                // get the start time of the frame
+                startTime = videoDriver->getRenderTime ();
                 renderFrame();
+                // get the end time of the frame
+                endTime = videoDriver->getRenderTime ();
+
+                // ensure the frame time is correct to not overrun FPS
+                if ((endTime - startTime) < minimumTime)
+                    usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
             }
 #ifdef ENABLE_WAYLAND
         }
@@ -349,8 +358,6 @@ namespace WallpaperEngine::Application
     }
 
     void CWallpaperApplication::renderFrame() {
-
-        static float startTime, endTime, minimumTime = 1.0f / this->m_context.settings.render.maximumFPS;
         static time_t seconds;
         static struct tm* timeinfo;
 
@@ -359,24 +366,17 @@ namespace WallpaperEngine::Application
         timeinfo = localtime(&seconds);
         g_Daytime = ((timeinfo->tm_hour * 60) + timeinfo->tm_min) / (24.0 * 60.0);
 
-        // update audio recorder
-        audioDriver->update ();
-        // update input information
-        inputContext->update ();
         // keep track of the previous frame's time
         g_TimeLast = g_Time;
         // calculate the current time value
         g_Time = videoDriver->getRenderTime ();
-        // get the start time of the frame
-        startTime = g_Time;
+
+        // update audio recorder
+        audioDriver->update ();
+        // update input information
+        inputContext->update ();
         // render the scene
         context->render ();
-        // get the end time of the frame
-        endTime = videoDriver->getRenderTime ();
-
-         // ensure the frame time is correct to not overrun FPS
-        if ((endTime - startTime) < minimumTime)
-            usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
 
         if (!this->m_context.settings.screenshot.take || videoDriver->getFrameCounter () != 5)
             return;

+ 9 - 0
src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.cpp

@@ -269,6 +269,13 @@ static void surfaceFrameCallback(void *data, struct wl_callback *cb, uint32_t ti
     PLS->output->rendering = true;
     PLS->output->driver->wallpaperApplication->renderFrame();
     PLS->output->rendering = false;
+
+    float renderTime = PLS->output->driver->getRenderTime();
+
+    if ((renderTime - PLS->lastTime) < PLS->minimumTime)
+        usleep ((PLS->minimumTime - (renderTime - PLS->lastTime)) * CLOCKS_PER_SEC);
+
+    PLS->lastTime = renderTime;
 }
 
 const struct wl_callback_listener frameListener = {
@@ -316,6 +323,8 @@ CLayerSurface::CLayerSurface(CWaylandOpenGLDriver* pDriver, SWaylandOutput* pOut
 
     if (eglMakeCurrent(pDriver->eglContext.display, eglSurface, eglSurface, pDriver->eglContext.context) == EGL_FALSE)
         sLog.exception("Failed to make egl current");
+
+    minimumTime = 1.0f / pDriver->wallpaperApplication->getContext().settings.render.maximumFPS;
 }
 
 CLayerSurface::~CLayerSurface() {

+ 1 - 0
src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.h

@@ -56,6 +56,7 @@ namespace WallpaperEngine::Render::Drivers
         wl_cursor* pointer = nullptr;
         wl_surface* cursorSurface = nullptr;
         bool callbackInitialized = false;
+        float lastTime, minimumTime;
     };
 
     class CWaylandOpenGLDriver : public CVideoDriver