Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / video / video_driver.hpp
blobb87dc705290d62db004f5604b6478ed5f572bd50
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 */
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 <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <mutex>
23 #include <thread>
24 #include <vector>
25 #include <functional>
27 extern std::string _ini_videodriver;
28 extern std::vector<Dimension> _resolutions;
29 extern Dimension _cur_resolution;
30 extern bool _rightclick_emulate;
31 extern bool _video_hw_accel;
32 extern bool _video_vsync;
34 /** The base of all video drivers. */
35 class VideoDriver : public Driver {
36 const uint DEFAULT_WINDOW_WIDTH = 640u; ///< Default window width.
37 const uint DEFAULT_WINDOW_HEIGHT = 480u; ///< Default window height.
39 public:
40 VideoDriver() : fast_forward_key_pressed(false), fast_forward_via_key(false), is_game_threaded(true) {}
42 /**
43 * Mark a particular area dirty.
44 * @param left The left most line of the dirty area.
45 * @param top The top most line of the dirty area.
46 * @param width The width of the dirty area.
47 * @param height The height of the dirty area.
49 virtual void MakeDirty(int left, int top, int width, int height) = 0;
51 /**
52 * Perform the actual drawing.
54 virtual void MainLoop() = 0;
56 /**
57 * Change the resolution of the window.
58 * @param w The new width.
59 * @param h The new height.
60 * @return True if the change succeeded.
62 virtual bool ChangeResolution(int w, int h) = 0;
64 /**
65 * Change the full screen setting.
66 * @param fullscreen The new setting.
67 * @return True if the change succeeded.
69 virtual bool ToggleFullscreen(bool fullscreen) = 0;
71 /**
72 * Change the vsync setting.
73 * @param vsync The new setting.
75 virtual void ToggleVsync(bool vsync) {}
77 /**
78 * Callback invoked after the blitter was changed.
79 * @return True if no error.
81 virtual bool AfterBlitterChange()
83 return true;
86 virtual bool ClaimMousePointer()
88 return true;
91 /**
92 * Get whether the mouse cursor is drawn by the video driver.
93 * @return True if cursor drawing is done by the video driver.
95 virtual bool UseSystemCursor()
97 return false;
101 * Populate all sprites in cache.
103 virtual void PopulateSystemSprites() {}
106 * Clear all cached sprites.
108 virtual void ClearSystemSprites() {}
111 * Whether the driver has a graphical user interface with the end user.
112 * Or in other words, whether we should spawn a thread for world generation
113 * and NewGRF scanning so the graphical updates can keep coming. Otherwise
114 * progress has to be shown on the console, which uses by definition another
115 * thread/process for display purposes.
116 * @return True for all drivers except null and dedicated.
118 virtual bool HasGUI() const
120 return true;
124 * Has this video driver an efficient code path for palette animated 8-bpp sprites?
125 * @return True if the driver has an efficient code path for 8-bpp.
127 virtual bool HasEfficient8Bpp() const
129 return false;
133 * Does this video driver support a separate animation buffer in addition to the colour buffer?
134 * @return True if a separate animation buffer is supported.
136 virtual bool HasAnimBuffer()
138 return false;
142 * Get a pointer to the animation buffer of the video back-end.
143 * @return Pointer to the buffer or nullptr if no animation buffer is supported.
145 virtual uint8 *GetAnimBuffer()
147 return nullptr;
151 * An edit box lost the input focus. Abort character compositing if necessary.
153 virtual void EditBoxLostFocus() {}
156 * An edit box gained the input focus
158 virtual void EditBoxGainedFocus() {}
161 * Get a list of refresh rates of each available monitor.
162 * @return A vector of the refresh rates of all available monitors.
164 virtual std::vector<int> GetListOfMonitorRefreshRates()
166 return {};
170 * Get a suggested default GUI zoom taking screen DPI into account.
172 virtual ZoomLevel GetSuggestedUIZoom()
174 float dpi_scale = this->GetDPIScale();
176 if (dpi_scale >= 3.0f) return ZOOM_LVL_NORMAL;
177 if (dpi_scale >= 1.5f) return ZOOM_LVL_OUT_2X;
178 return ZOOM_LVL_OUT_4X;
182 * Queue a function to be called on the main thread with game state
183 * lock held and video buffer locked. Queued functions will be
184 * executed on the next draw tick.
185 * @param func Function to call.
187 void QueueOnMainThread(std::function<void()> &&func)
189 std::lock_guard<std::mutex> lock(this->cmd_queue_mutex);
191 this->cmd_queue.emplace_back(std::forward<std::function<void()>>(func));
194 void GameLoopPause();
197 * Get the currently active instance of the video driver.
199 static VideoDriver *GetInstance() {
200 return static_cast<VideoDriver*>(*DriverFactoryBase::GetActiveDriver(Driver::DT_VIDEO));
204 * Helper struct to ensure the video buffer is locked and ready for drawing. The destructor
205 * will make sure the buffer is unlocked no matter how the scope is exited.
207 struct VideoBufferLocker {
208 VideoBufferLocker()
210 this->unlock = VideoDriver::GetInstance()->LockVideoBuffer();
213 ~VideoBufferLocker()
215 if (this->unlock) VideoDriver::GetInstance()->UnlockVideoBuffer();
218 private:
219 bool unlock; ///< Stores if the lock did anything that has to be undone.
222 protected:
223 const uint ALLOWED_DRIFT = 5; ///< How many times videodriver can miss deadlines without it being overly compensated.
226 * Get the resolution of the main screen.
228 virtual Dimension GetScreenSize() const { return { DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT }; }
231 * Get DPI scaling factor of the screen OTTD is displayed on.
232 * @return 1.0 for default platform DPI, > 1.0 for higher DPI values, and < 1.0 for smaller DPI values.
234 virtual float GetDPIScale() { return 1.0f; }
237 * Apply resolution auto-detection and clamp to sensible defaults.
239 void UpdateAutoResolution()
241 if (_cur_resolution.width == 0 || _cur_resolution.height == 0) {
242 /* Auto-detect a good resolution. We aim for 75% of the screen size.
243 * Limit width times height times bytes per pixel to fit a 32 bit
244 * integer, This way all internal drawing routines work correctly. */
245 Dimension res = this->GetScreenSize();
246 _cur_resolution.width = ClampU(res.width * 3 / 4, DEFAULT_WINDOW_WIDTH, UINT16_MAX / 2);
247 _cur_resolution.height = ClampU(res.height * 3 / 4, DEFAULT_WINDOW_HEIGHT, UINT16_MAX / 2);
252 * Handle input logic, is CTRL pressed, should we fast-forward, etc.
254 virtual void InputLoop() {}
257 * Make sure the video buffer is ready for drawing.
258 * @returns True if the video buffer has to be unlocked.
260 virtual bool LockVideoBuffer() {
261 return false;
265 * Unlock a previously locked video buffer.
267 virtual void UnlockVideoBuffer() {}
270 * Paint the window.
272 virtual void Paint() {}
275 * Process any pending palette animation.
277 virtual void CheckPaletteAnim() {}
280 * Process a single system event.
281 * @returns False if there are no more events to process.
283 virtual bool PollEvent() { return false; };
286 * Start the loop for game-tick.
288 void StartGameThread();
291 * Stop the loop for the game-tick. This can still tick at most one time before truly shutting down.
293 void StopGameThread();
296 * Give the video-driver a tick.
297 * It will process any potential game-tick and/or draw-tick, and/or any
298 * other video-driver related event.
300 void Tick();
303 * Sleep till the next tick is about to happen.
305 void SleepTillNextTick();
307 std::chrono::steady_clock::duration GetGameInterval()
309 /* If we are paused, run on normal speed. */
310 if (_pause_mode) return std::chrono::milliseconds(MILLISECONDS_PER_TICK);
311 /* Infinite speed, as quickly as you can. */
312 if (_game_speed == 0) return std::chrono::microseconds(0);
314 return std::chrono::microseconds(MILLISECONDS_PER_TICK * 1000 * 100 / _game_speed);
317 std::chrono::steady_clock::duration GetDrawInterval()
319 return std::chrono::microseconds(1000000 / _settings_client.gui.refresh_rate);
322 /** Execute all queued commands. */
323 void DrainCommandQueue()
325 std::vector<std::function<void()>> cmds{};
328 /* Exchange queue with an empty one to limit the time we
329 * hold the mutex. This also ensures that queued functions can
330 * add new functions to the queue without everything blocking. */
331 std::lock_guard<std::mutex> lock(this->cmd_queue_mutex);
332 cmds.swap(this->cmd_queue);
335 for (auto &f : cmds) {
336 f();
340 std::chrono::steady_clock::time_point next_game_tick;
341 std::chrono::steady_clock::time_point next_draw_tick;
343 bool fast_forward_key_pressed; ///< The fast-forward key is being pressed.
344 bool fast_forward_via_key; ///< The fast-forward was enabled by key press.
346 bool is_game_threaded;
347 std::thread game_thread;
348 std::mutex game_state_mutex;
349 std::mutex game_thread_wait_mutex;
351 static void GameThreadThunk(VideoDriver *drv);
353 private:
354 std::mutex cmd_queue_mutex;
355 std::vector<std::function<void()>> cmd_queue;
357 void GameLoop();
358 void GameThread();
361 #endif /* VIDEO_VIDEO_DRIVER_HPP */