summaryrefslogtreecommitdiff
path: root/src/video/video_driver.hpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-02-24 15:22:23 +0100
committerPatric Stout <github@truebrain.nl>2021-03-08 19:18:55 +0100
commite56d2c63c306dd087de26088729d09233b1122c2 (patch)
treedfebf7ee7f39a113b22d696a01bb5b1eefd72d42 /src/video/video_driver.hpp
parent3a4a15cc93015ce5bed4f7720d4f0f05178c09e9 (diff)
downloadopenttd-e56d2c63c306dd087de26088729d09233b1122c2.tar.xz
Add: [Video] move GameLoop into its own thread
This allows drawing to happen while the GameLoop is doing an iteration too. Sadly, not much drawing currently can be done while the GameLoop is running, as for example PollEvent() or UpdateWindows() can influence the game-state. As such, they first need to acquire a lock on the game-state before they can be called. Currently, the main advantage is the time spend in Paint(), which for non-OpenGL drivers can be a few milliseconds. For OpenGL this is more like 0.05 milliseconds; in these instances this change doesn't add any benefits for now. This is an alternative to the former "draw-thread", which moved the drawing in a thread for some OSes. It has similar performance gain as this does, although this implementation allows for more finer control over what suffers when the GameLoop takes too long: drawing or the next GameLoop. For now they both suffer equally.
Diffstat (limited to 'src/video/video_driver.hpp')
-rw-r--r--src/video/video_driver.hpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp
index cfb853dba..423e46436 100644
--- a/src/video/video_driver.hpp
+++ b/src/video/video_driver.hpp
@@ -16,7 +16,11 @@
#include "../gfx_func.h"
#include "../settings_type.h"
#include "../zoom_type.h"
+#include <atomic>
#include <chrono>
+#include <condition_variable>
+#include <mutex>
+#include <thread>
#include <vector>
extern std::string _ini_videodriver;
@@ -31,6 +35,8 @@ class VideoDriver : public Driver {
const uint DEFAULT_WINDOW_HEIGHT = 480u; ///< Default window height.
public:
+ VideoDriver() : is_game_threaded(true) {}
+
/**
* Mark a particular area dirty.
* @param left The left most line of the dirty area.
@@ -84,6 +90,11 @@ public:
}
/**
+ * Populate all sprites in cache.
+ */
+ virtual void PopulateSystemSprites() {}
+
+ /**
* Clear all cached sprites.
*/
virtual void ClearSystemSprites() {}
@@ -240,6 +251,16 @@ protected:
virtual bool PollEvent() { return false; };
/**
+ * Start the loop for game-tick.
+ */
+ void StartGameThread();
+
+ /**
+ * Stop the loop for the game-tick. This can still tick at most one time before truly shutting down.
+ */
+ void StopGameThread();
+
+ /**
* Give the video-driver a tick.
* It will process any potential game-tick and/or draw-tick, and/or any
* other video-driver related event.
@@ -271,6 +292,17 @@ protected:
bool fast_forward_key_pressed; ///< The fast-forward key is being pressed.
bool fast_forward_via_key; ///< The fast-forward was enabled by key press.
+
+ bool is_game_threaded;
+ std::thread game_thread;
+ std::mutex game_state_mutex;
+ std::mutex game_thread_wait_mutex;
+
+ static void GameThreadThunk(VideoDriver *drv);
+
+private:
+ void GameLoop();
+ void GameThread();
};
#endif /* VIDEO_VIDEO_DRIVER_HPP */