Codechange: Generalise the delayed blitter change to a generic video driver command queue.
(cherry picked from commit 91b8ce073f)
This commit is contained in:
committed by
Jonathan G Rennison
parent
e856d3f8fe
commit
1b46ef756e
@@ -22,6 +22,7 @@
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#if defined(__MINGW32__)
|
||||
#include "../3rdparty/mingw-std-threads/mingw.mutex.h"
|
||||
#include "../3rdparty/mingw-std-threads/mingw.condition_variable.h"
|
||||
@@ -41,7 +42,7 @@ class VideoDriver : public Driver {
|
||||
const uint DEFAULT_WINDOW_HEIGHT = 480u; ///< Default window height.
|
||||
|
||||
public:
|
||||
VideoDriver() : fast_forward_key_pressed(false), fast_forward_via_key(false), is_game_threaded(true), change_blitter(nullptr) {}
|
||||
VideoDriver() : fast_forward_key_pressed(false), fast_forward_via_key(false), is_game_threaded(true) {}
|
||||
|
||||
/**
|
||||
* Mark a particular area dirty.
|
||||
@@ -183,12 +184,16 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue a request to change the blitter. This is not executed immediately,
|
||||
* but instead on the next draw-tick.
|
||||
* Queue a function to be called on the main thread with game state
|
||||
* lock held and video buffer locked. Queued functions will be
|
||||
* executed on the next draw tick.
|
||||
* @param func Function to call.
|
||||
*/
|
||||
void ChangeBlitter(const char *new_blitter)
|
||||
void QueueOnMainThread(std::function<void()> &&func)
|
||||
{
|
||||
this->change_blitter = new_blitter;
|
||||
std::lock_guard<std::mutex> lock(this->cmd_queue_mutex);
|
||||
|
||||
this->cmd_queue.emplace_back(std::forward<std::function<void()>>(func));
|
||||
}
|
||||
|
||||
void GameLoopPause();
|
||||
@@ -337,11 +342,29 @@ protected:
|
||||
static void GameThreadThunk(VideoDriver *drv);
|
||||
|
||||
private:
|
||||
std::mutex cmd_queue_mutex;
|
||||
std::vector<std::function<void()>> cmd_queue;
|
||||
|
||||
/** Execute all queued commands. */
|
||||
void DrainCommandQueue()
|
||||
{
|
||||
std::vector<std::function<void()>> cmds{};
|
||||
|
||||
{
|
||||
/* Exchange queue with an empty one to limit the time we
|
||||
* hold the mutex. This also ensures that queued functions can
|
||||
* add new functions to the queue without everything blocking. */
|
||||
std::lock_guard<std::mutex> lock(this->cmd_queue_mutex);
|
||||
cmds.swap(this->cmd_queue);
|
||||
}
|
||||
|
||||
for (auto &f : cmds) {
|
||||
f();
|
||||
}
|
||||
}
|
||||
|
||||
void GameLoop();
|
||||
void GameThread();
|
||||
void RealChangeBlitter(const char *repl_blitter);
|
||||
|
||||
const char *change_blitter; ///< Request to change the blitter. nullptr if no pending request.
|
||||
};
|
||||
|
||||
#endif /* VIDEO_VIDEO_DRIVER_HPP */
|
||||
|
||||
Reference in New Issue
Block a user