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/>.
8 /** @file video_driver.hpp Base of all video drivers. */
10 #ifndef VIDEO_VIDEO_DRIVER_HPP
11 #define VIDEO_VIDEO_DRIVER_HPP
13 #include "../driver.h"
14 #include "../core/geometry_type.hpp"
15 #include "../core/math_func.hpp"
16 #include "../gfx_func.h"
17 #include "../settings_type.h"
18 #include "../zoom_type.h"
19 #include "../network/network_func.h"
22 #include <condition_variable>
26 extern std::string _ini_videodriver
;
27 extern std::vector
<Dimension
> _resolutions
;
28 extern Dimension _cur_resolution
;
29 extern bool _rightclick_emulate
;
30 extern bool _video_hw_accel
;
31 extern bool _video_vsync
;
33 /** The base of all video drivers. */
34 class VideoDriver
: public Driver
{
35 const uint DEFAULT_WINDOW_WIDTH
= 640u; ///< Default window width.
36 const uint DEFAULT_WINDOW_HEIGHT
= 480u; ///< Default window height.
39 VideoDriver(bool uses_hardware_acceleration
= false) : fast_forward_key_pressed(false), fast_forward_via_key(false), is_game_threaded(true), uses_hardware_acceleration(uses_hardware_acceleration
) {}
42 * Mark a particular area dirty.
43 * @param left The left most line of the dirty area.
44 * @param top The top most line of the dirty area.
45 * @param width The width of the dirty area.
46 * @param height The height of the dirty area.
48 virtual void MakeDirty(int left
, int top
, int width
, int height
) = 0;
51 * Perform the actual drawing.
53 virtual void MainLoop() = 0;
56 * Change the resolution of the window.
57 * @param w The new width.
58 * @param h The new height.
59 * @return True if the change succeeded.
61 virtual bool ChangeResolution(int w
, int h
) = 0;
64 * Change the full screen setting.
65 * @param fullscreen The new setting.
66 * @return True if the change succeeded.
68 virtual bool ToggleFullscreen(bool fullscreen
) = 0;
71 * Change the vsync setting.
72 * @param vsync The new setting.
74 virtual void ToggleVsync([[maybe_unused
]] bool vsync
) {}
77 * Callback invoked after the blitter was changed.
78 * @return True if no error.
80 virtual bool AfterBlitterChange()
85 virtual bool ClaimMousePointer()
91 * Get whether the mouse cursor is drawn by the video driver.
92 * @return True if cursor drawing is done by the video driver.
94 virtual bool UseSystemCursor()
100 * Populate all sprites in cache.
102 virtual void PopulateSystemSprites() {}
105 * Clear all cached sprites.
107 virtual void ClearSystemSprites() {}
110 * Whether the driver has a graphical user interface with the end user.
111 * Or in other words, whether we should spawn a thread for world generation
112 * and NewGRF scanning so the graphical updates can keep coming. Otherwise
113 * progress has to be shown on the console, which uses by definition another
114 * thread/process for display purposes.
115 * @return True for all drivers except null and dedicated.
117 virtual bool HasGUI() const
123 * Has this video driver an efficient code path for palette animated 8-bpp sprites?
124 * @return True if the driver has an efficient code path for 8-bpp.
126 virtual bool HasEfficient8Bpp() const
132 * Does this video driver support a separate animation buffer in addition to the colour buffer?
133 * @return True if a separate animation buffer is supported.
135 virtual bool HasAnimBuffer()
141 * Get a pointer to the animation buffer of the video back-end.
142 * @return Pointer to the buffer or nullptr if no animation buffer is supported.
144 virtual uint8_t *GetAnimBuffer()
150 * An edit box lost the input focus. Abort character compositing if necessary.
152 virtual void EditBoxLostFocus() {}
155 * An edit box gained the input focus
157 virtual void EditBoxGainedFocus() {}
160 * Get a list of refresh rates of each available monitor.
161 * @return A vector of the refresh rates of all available monitors.
163 virtual std::vector
<int> GetListOfMonitorRefreshRates()
169 * Get a suggested default GUI scale taking screen DPI into account.
171 virtual int GetSuggestedUIScale()
173 float dpi_scale
= this->GetDPIScale();
175 return Clamp(dpi_scale
* 100, MIN_INTERFACE_SCALE
, MAX_INTERFACE_SCALE
);
178 virtual std::string_view
GetInfoString() const
180 return this->GetName();
184 * Queue a function to be called on the main thread with game state
185 * lock held and video buffer locked. Queued functions will be
186 * executed on the next draw tick.
187 * @param func Function to call.
189 void QueueOnMainThread(std::function
<void()> &&func
)
191 std::lock_guard
<std::mutex
> lock(this->cmd_queue_mutex
);
193 this->cmd_queue
.emplace_back(std::forward
<std::function
<void()>>(func
));
196 void GameLoopPause();
199 * Get the currently active instance of the video driver.
201 static VideoDriver
*GetInstance()
203 return static_cast<VideoDriver
*>(*DriverFactoryBase::GetActiveDriver(Driver::DT_VIDEO
));
206 static std::string
GetCaption();
209 * Helper struct to ensure the video buffer is locked and ready for drawing. The destructor
210 * will make sure the buffer is unlocked no matter how the scope is exited.
212 struct VideoBufferLocker
{
215 this->unlock
= VideoDriver::GetInstance()->LockVideoBuffer();
220 if (this->unlock
) VideoDriver::GetInstance()->UnlockVideoBuffer();
224 bool unlock
; ///< Stores if the lock did anything that has to be undone.
228 const uint ALLOWED_DRIFT
= 5; ///< How many times videodriver can miss deadlines without it being overly compensated.
231 * Get the resolution of the main screen.
233 virtual Dimension
GetScreenSize() const { return { DEFAULT_WINDOW_WIDTH
, DEFAULT_WINDOW_HEIGHT
}; }
236 * Get DPI scaling factor of the screen OTTD is displayed on.
237 * @return 1.0 for default platform DPI, > 1.0 for higher DPI values, and < 1.0 for smaller DPI values.
239 virtual float GetDPIScale() { return 1.0f
; }
242 * Apply resolution auto-detection and clamp to sensible defaults.
244 void UpdateAutoResolution()
246 if (_cur_resolution
.width
== 0 || _cur_resolution
.height
== 0) {
247 /* Auto-detect a good resolution. We aim for 75% of the screen size.
248 * Limit width times height times bytes per pixel to fit a 32 bit
249 * integer, This way all internal drawing routines work correctly. */
250 Dimension res
= this->GetScreenSize();
251 _cur_resolution
.width
= ClampU(res
.width
* 3 / 4, DEFAULT_WINDOW_WIDTH
, UINT16_MAX
/ 2);
252 _cur_resolution
.height
= ClampU(res
.height
* 3 / 4, DEFAULT_WINDOW_HEIGHT
, UINT16_MAX
/ 2);
257 * Handle input logic, is CTRL pressed, should we fast-forward, etc.
259 virtual void InputLoop() {}
262 * Make sure the video buffer is ready for drawing.
263 * @returns True if the video buffer has to be unlocked.
265 virtual bool LockVideoBuffer()
271 * Unlock a previously locked video buffer.
273 virtual void UnlockVideoBuffer() {}
278 virtual void Paint() {}
281 * Process any pending palette animation.
283 virtual void CheckPaletteAnim() {}
286 * Process a single system event.
287 * @returns False if there are no more events to process.
289 virtual bool PollEvent() { return false; };
292 * Start the loop for game-tick.
294 void StartGameThread();
297 * Stop the loop for the game-tick. This can still tick at most one time before truly shutting down.
299 void StopGameThread();
302 * Give the video-driver a tick.
303 * It will process any potential game-tick and/or draw-tick, and/or any
304 * other video-driver related event.
309 * Sleep till the next tick is about to happen.
311 void SleepTillNextTick();
313 std::chrono::steady_clock::duration
GetGameInterval()
315 #ifdef DEBUG_DUMP_COMMANDS
316 /* When replaying, run as fast as we can. */
317 extern bool _ddc_fastforward
;
318 if (_ddc_fastforward
) return std::chrono::microseconds(0);
319 #endif /* DEBUG_DUMP_COMMANDS */
321 /* If we are paused, run on normal speed. */
322 if (_pause_mode
) return std::chrono::milliseconds(MILLISECONDS_PER_TICK
);
323 /* Infinite speed, as quickly as you can. */
324 if (_game_speed
== 0) return std::chrono::microseconds(0);
326 return std::chrono::microseconds(MILLISECONDS_PER_TICK
* 1000 * 100 / _game_speed
);
329 std::chrono::steady_clock::duration
GetDrawInterval()
331 /* If vsync, draw interval is decided by the display driver */
332 if (_video_vsync
&& this->uses_hardware_acceleration
) return std::chrono::microseconds(0);
333 return std::chrono::microseconds(1000000 / _settings_client
.gui
.refresh_rate
);
336 /** Execute all queued commands. */
337 void DrainCommandQueue()
339 std::vector
<std::function
<void()>> cmds
{};
342 /* Exchange queue with an empty one to limit the time we
343 * hold the mutex. This also ensures that queued functions can
344 * add new functions to the queue without everything blocking. */
345 std::lock_guard
<std::mutex
> lock(this->cmd_queue_mutex
);
346 cmds
.swap(this->cmd_queue
);
349 for (auto &f
: cmds
) {
354 std::chrono::steady_clock::time_point next_game_tick
;
355 std::chrono::steady_clock::time_point next_draw_tick
;
357 bool fast_forward_key_pressed
; ///< The fast-forward key is being pressed.
358 bool fast_forward_via_key
; ///< The fast-forward was enabled by key press.
360 bool is_game_threaded
;
361 std::thread game_thread
;
362 std::mutex game_state_mutex
;
363 std::mutex game_thread_wait_mutex
;
365 bool uses_hardware_acceleration
;
367 static void GameThreadThunk(VideoDriver
*drv
);
370 std::mutex cmd_queue_mutex
;
371 std::vector
<std::function
<void()>> cmd_queue
;
377 #endif /* VIDEO_VIDEO_DRIVER_HPP */