1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file video_driver.cpp Common code between video driver implementations. */
9 
10 #include "../stdafx.h"
11 #include "../core/random_func.hpp"
12 #include "../network/network.h"
13 #include "../blitter/factory.hpp"
14 #include "../debug.h"
15 #include "../fontcache.h"
16 #include "../gfx_func.h"
17 #include "../gfxinit.h"
18 #include "../progress.h"
19 #include "../thread.h"
20 #include "../window_func.h"
21 #include "video_driver.hpp"
22 
23 bool _video_hw_accel; ///< Whether to consider hardware accelerated video drivers.
24 bool _video_vsync; ///< Whether we should use vsync (only if _video_hw_accel is enabled).
25 
GameLoop()26 void VideoDriver::GameLoop()
27 {
28 	this->next_game_tick += this->GetGameInterval();
29 
30 	/* Avoid next_game_tick getting behind more and more if it cannot keep up. */
31 	auto now = std::chrono::steady_clock::now();
32 	if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
33 
34 	{
35 		std::lock_guard<std::mutex> lock(this->game_state_mutex);
36 
37 		::GameLoop();
38 	}
39 }
40 
GameThread()41 void VideoDriver::GameThread()
42 {
43 	while (!_exit_game) {
44 		this->GameLoop();
45 
46 		auto now = std::chrono::steady_clock::now();
47 		if (this->next_game_tick > now) {
48 			std::this_thread::sleep_for(this->next_game_tick - now);
49 		} else {
50 			/* Ensure we yield this thread if drawings wants to take a lock on
51 			 * the game state. This is mainly because most OSes have an
52 			 * optimization that if you unlock/lock a mutex in the same thread
53 			 * quickly, it will never context switch even if there is another
54 			 * thread waiting to take the lock on the same mutex. */
55 			std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
56 		}
57 	}
58 }
59 
60 /**
61  * Pause the game-loop for a bit, releasing the game-state lock. This allows,
62  * if the draw-tick requested this, the drawing to happen.
63  */
GameLoopPause()64 void VideoDriver::GameLoopPause()
65 {
66 	/* If we are not called from the game-thread, ignore this request. */
67 	if (std::this_thread::get_id() != this->game_thread.get_id()) return;
68 
69 	this->game_state_mutex.unlock();
70 
71 	{
72 		/* See GameThread() for more details on this lock. */
73 		std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
74 	}
75 
76 	this->game_state_mutex.lock();
77 }
78 
GameThreadThunk(VideoDriver * drv)79 /* static */ void VideoDriver::GameThreadThunk(VideoDriver *drv)
80 {
81 	drv->GameThread();
82 }
83 
StartGameThread()84 void VideoDriver::StartGameThread()
85 {
86 	if (this->is_game_threaded) {
87 		this->is_game_threaded = StartNewThread(&this->game_thread, "ottd:game", &VideoDriver::GameThreadThunk, this);
88 	}
89 
90 	Debug(driver, 1, "using {}thread for game-loop", this->is_game_threaded ? "" : "no ");
91 }
92 
StopGameThread()93 void VideoDriver::StopGameThread()
94 {
95 	if (!this->is_game_threaded) return;
96 
97 	this->game_thread.join();
98 }
99 
Tick()100 void VideoDriver::Tick()
101 {
102 	if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
103 		this->GameLoop();
104 
105 		/* For things like dedicated server, don't run a separate draw-tick. */
106 		if (!this->HasGUI()) {
107 			::InputLoop();
108 			::UpdateWindows();
109 			this->next_draw_tick = this->next_game_tick;
110 		}
111 	}
112 
113 	auto now = std::chrono::steady_clock::now();
114 	if (this->HasGUI() && now >= this->next_draw_tick) {
115 		this->next_draw_tick += this->GetDrawInterval();
116 		/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
117 		if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
118 
119 		/* Locking video buffer can block (especially with vsync enabled), do it before taking game state lock. */
120 		this->LockVideoBuffer();
121 
122 		{
123 			/* Tell the game-thread to stop so we can have a go. */
124 			std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
125 			std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
126 
127 			/* Keep the interactive randomizer a bit more random by requesting
128 			 * new values when-ever we can. */
129 			InteractiveRandom();
130 
131 			this->DrainCommandQueue();
132 
133 			while (this->PollEvent()) {}
134 			this->InputLoop();
135 
136 			/* Check if the fast-forward button is still pressed. */
137 			if (fast_forward_key_pressed && !_networking && _game_mode != GM_MENU) {
138 				ChangeGameSpeed(true);
139 				this->fast_forward_via_key = true;
140 			} else if (this->fast_forward_via_key) {
141 				ChangeGameSpeed(false);
142 				this->fast_forward_via_key = false;
143 			}
144 
145 			::InputLoop();
146 
147 			/* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
148 			if (_game_mode == GM_BOOTSTRAP || _switch_mode == SM_NONE || HasModalProgress()) {
149 				::UpdateWindows();
150 			}
151 
152 			this->PopulateSystemSprites();
153 		}
154 
155 		this->CheckPaletteAnim();
156 		this->Paint();
157 
158 		this->UnlockVideoBuffer();
159 	}
160 }
161 
SleepTillNextTick()162 void VideoDriver::SleepTillNextTick()
163 {
164 	auto next_tick = this->next_draw_tick;
165 	auto now = std::chrono::steady_clock::now();
166 
167 	if (!this->is_game_threaded) {
168 		next_tick = min(next_tick, this->next_game_tick);
169 	}
170 
171 	if (next_tick > now) {
172 		std::this_thread::sleep_for(next_tick - now);
173 	}
174 }
175