Changeset - r25014:e1f1bf3a062e
[Not reviewed]
master
0 17 0
Patric Stout - 3 years ago 2021-02-24 14:22:23
truebrain@openttd.org
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.
17 files changed with 206 insertions and 51 deletions:
0 comments (0 inline, 0 general)
src/video/allegro_v.cpp
Show inline comments
 
@@ -409,13 +409,13 @@ bool VideoDriver_Allegro::PollEvent()
 
/**
 
 * There are multiple modules that might be using Allegro and
 
 * Allegro can only be initiated once.
 
 */
 
int _allegro_instance_count = 0;
 

	
 
const char *VideoDriver_Allegro::Start(const StringList &parm)
 
const char *VideoDriver_Allegro::Start(const StringList &param)
 
{
 
	if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
 
		DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
 
		return "Failed to set up Allegro";
 
	}
 
	_allegro_instance_count++;
 
@@ -437,12 +437,14 @@ const char *VideoDriver_Allegro::Start(c
 
	if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
 
		return "Failed to set up Allegro video";
 
	}
 
	MarkWholeScreenDirty();
 
	set_close_button_callback(HandleExitGameRequest);
 

	
 
	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
 

	
 
	return nullptr;
 
}
 

	
 
void VideoDriver_Allegro::Stop()
 
{
 
	if (--_allegro_instance_count == 0) allegro_exit();
 
@@ -472,18 +474,22 @@ void VideoDriver_Allegro::InputLoop()
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
void VideoDriver_Allegro::MainLoop()
 
{
 
	this->StartGameThread();
 

	
 
	for (;;) {
 
		if (_exit_game) return;
 
		if (_exit_game) break;
 

	
 
		this->Tick();
 
		this->SleepTillNextTick();
 
	}
 

	
 
	this->StopGameThread();
 
}
 

	
 
bool VideoDriver_Allegro::ChangeResolution(int w, int h)
 
{
 
	return CreateMainSurface(w, h);
 
}
src/video/cocoa/cocoa_ogl.h
Show inline comments
 
@@ -30,12 +30,14 @@ public:
 
	bool HasEfficient8Bpp() const override { return true; }
 

	
 
	bool UseSystemCursor() override { return true; }
 

	
 
	void ClearSystemSprites() override;
 

	
 
	void PopulateSystemSprites() override;
 

	
 
	bool HasAnimBuffer() override { return true; }
 
	uint8 *GetAnimBuffer() override { return this->anim_buffer; }
 

	
 
	/** Return driver name */
 
	const char *GetName() const override { return "cocoa-opengl"; }
 

	
src/video/cocoa/cocoa_ogl.mm
Show inline comments
 
@@ -211,12 +211,14 @@ const char *VideoDriver_CocoaOpenGL::Sta
 
	if (fullscreen) this->ToggleFullscreen(fullscreen);
 

	
 
	this->GameSizeChanged();
 
	this->UpdateVideoModes();
 
	MarkWholeScreenDirty();
 

	
 
	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
 

	
 
	return nullptr;
 

	
 
}
 

	
 
void VideoDriver_CocoaOpenGL::Stop()
 
{
 
@@ -224,12 +226,17 @@ void VideoDriver_CocoaOpenGL::Stop()
 

	
 
	CGLSetCurrentContext(this->gl_context);
 
	OpenGLBackend::Destroy();
 
	CGLReleaseContext(this->gl_context);
 
}
 

	
 
void VideoDriver_CocoaOpenGL::PopulateSystemSprites()
 
{
 
	OpenGLBackend::Get()->PopulateCursorCache();
 
}
 

	
 
void VideoDriver_CocoaOpenGL::ClearSystemSprites()
 
{
 
	CGLSetCurrentContext(this->gl_context);
 
	OpenGLBackend::Get()->ClearCursorCache();
 
}
 

	
src/video/cocoa/cocoa_v.mm
Show inline comments
 
@@ -432,24 +432,28 @@ void VideoDriver_Cocoa::InputLoop()
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
/** Main game loop. */
 
void VideoDriver_Cocoa::MainLoopReal()
 
{
 
	this->StartGameThread();
 

	
 
	for (;;) {
 
		@autoreleasepool {
 
			if (_exit_game) {
 
				/* Restore saved resolution if in fullscreen mode. */
 
				if (this->IsFullscreen()) _cur_resolution = this->orig_res;
 
				break;
 
			}
 

	
 
			this->Tick();
 
			this->SleepTillNextTick();
 
		}
 
	}
 

	
 
	this->StopGameThread();
 
}
 

	
 

	
 
/* Subclass of OTTD_CocoaView to fix Quartz rendering */
 
@interface OTTD_QuartzView : NSView {
 
	VideoDriver_CocoaQuartz *driver;
 
@@ -555,12 +559,14 @@ const char *VideoDriver_CocoaQuartz::Sta
 

	
 
	if (fullscreen) this->ToggleFullscreen(fullscreen);
 

	
 
	this->GameSizeChanged();
 
	this->UpdateVideoModes();
 

	
 
	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
 

	
 
	return nullptr;
 

	
 
}
 

	
 
void VideoDriver_CocoaQuartz::Stop()
 
{
src/video/dedicated_v.cpp
Show inline comments
 
@@ -264,12 +264,14 @@ void VideoDriver_Dedicated::MainLoop()
 
		} else {
 
			/* We can load this game, so go ahead */
 
			SwitchToMode(SM_LOAD_GAME);
 
		}
 
	}
 

	
 
	this->is_game_threaded = false;
 

	
 
	/* Done loading, start game! */
 

	
 
	if (!_networking) {
 
		DEBUG(net, 0, "Dedicated server could not be started, aborting");
 
		return;
 
	}
src/video/null_v.cpp
Show inline comments
 
@@ -45,15 +45,15 @@ void VideoDriver_Null::MakeDirty(int lef
 

	
 
void VideoDriver_Null::MainLoop()
 
{
 
	uint i;
 

	
 
	for (i = 0; i < this->ticks; i++) {
 
		GameLoop();
 
		InputLoop();
 
		UpdateWindows();
 
		::GameLoop();
 
		::InputLoop();
 
		::UpdateWindows();
 
	}
 
}
 

	
 
bool VideoDriver_Null::ChangeResolution(int w, int h) { return false; }
 

	
 
bool VideoDriver_Null::ToggleFullscreen(bool fs) { return false; }
src/video/opengl.cpp
Show inline comments
 
@@ -1033,27 +1033,38 @@ void OpenGLBackend::Paint()
 
void OpenGLBackend::DrawMouseCursor()
 
{
 
	/* Draw cursor on screen */
 
	_cur_dpi = &_screen;
 
	for (uint i = 0; i < _cursor.sprite_count; ++i) {
 
		SpriteID sprite = _cursor.sprite_seq[i].sprite;
 
		const Sprite *spr = GetSprite(sprite, ST_NORMAL);
 

	
 
		/* Sprites are cached by PopulateCursorCache(). */
 
		if (this->cursor_cache.Contains(sprite)) {
 
			const Sprite *spr = GetSprite(sprite, ST_NORMAL);
 

	
 
			this->RenderOglSprite((OpenGLSprite *)this->cursor_cache.Get(sprite)->data, _cursor.sprite_seq[i].pal,
 
					_cursor.pos.x + _cursor.sprite_pos[i].x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI),
 
					_cursor.pos.y + _cursor.sprite_pos[i].y + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI),
 
					ZOOM_LVL_GUI);
 
		}
 
	}
 
}
 

	
 
void OpenGLBackend::PopulateCursorCache()
 
{
 
	for (uint i = 0; i < _cursor.sprite_count; ++i) {
 
		SpriteID sprite = _cursor.sprite_seq[i].sprite;
 

	
 
		if (!this->cursor_cache.Contains(sprite)) {
 
			Sprite *old = this->cursor_cache.Insert(sprite, (Sprite *)GetRawSprite(sprite, ST_NORMAL, &SimpleSpriteAlloc, this));
 
			if (old != nullptr) {
 
				OpenGLSprite *sprite = (OpenGLSprite *)old->data;
 
				sprite->~OpenGLSprite();
 
				free(old);
 
			}
 
		}
 

	
 
		this->RenderOglSprite((OpenGLSprite *)this->cursor_cache.Get(sprite)->data, _cursor.sprite_seq[i].pal,
 
				_cursor.pos.x + _cursor.sprite_pos[i].x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI),
 
				_cursor.pos.y + _cursor.sprite_pos[i].y + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI),
 
				ZOOM_LVL_GUI);
 
	}
 
}
 

	
 
/**
 
 * Clear all cached cursor sprites.
 
 */
src/video/opengl.h
Show inline comments
 
@@ -85,12 +85,13 @@ public:
 

	
 
	void UpdatePalette(const Colour *pal, uint first, uint length);
 
	bool Resize(int w, int h, bool force = false);
 
	void Paint();
 

	
 
	void DrawMouseCursor();
 
	void PopulateCursorCache();
 
	void ClearCursorCache();
 

	
 
	void *GetVideoBuffer();
 
	uint8 *GetAnimBuffer();
 
	void ReleaseVideoBuffer(const Rect &update_rect);
 
	void ReleaseAnimBuffer(const Rect &update_rect);
src/video/sdl2_default_v.cpp
Show inline comments
 
@@ -104,19 +104,13 @@ void VideoDriver_SDL_Default::Paint()
 
		switch (blitter->UsePaletteAnimation()) {
 
			case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
 
				this->UpdatePalette();
 
				break;
 

	
 
			case Blitter::PALETTE_ANIMATION_BLITTER: {
 
				bool need_buf = _screen.dst_ptr == nullptr;
 
				if (need_buf) _screen.dst_ptr = this->GetVideoPointer();
 
				blitter->PaletteAnimate(this->local_palette);
 
				if (need_buf) {
 
					this->ReleaseVideoPointer();
 
					_screen.dst_ptr = nullptr;
 
				}
 
				break;
 
			}
 

	
 
			case Blitter::PALETTE_ANIMATION_NONE:
 
				break;
 

	
src/video/sdl2_opengl_v.cpp
Show inline comments
 
@@ -107,12 +107,17 @@ const char *VideoDriver_SDL_OpenGL::Allo
 
	this->gl_context = SDL_GL_CreateContext(this->sdl_window);
 
	if (this->gl_context == nullptr) return "SDL2: Can't active GL context";
 

	
 
	return OpenGLBackend::Create(&GetOGLProcAddressCallback);
 
}
 

	
 
void VideoDriver_SDL_OpenGL::PopulateSystemSprites()
 
{
 
	OpenGLBackend::Get()->PopulateCursorCache();
 
}
 

	
 
void VideoDriver_SDL_OpenGL::ClearSystemSprites()
 
{
 
	OpenGLBackend::Get()->ClearCursorCache();
 
}
 

	
 
bool VideoDriver_SDL_OpenGL::AllocateBackingStore(int w, int h, bool force)
src/video/sdl2_opengl_v.h
Show inline comments
 
@@ -21,12 +21,14 @@ public:
 
	bool HasEfficient8Bpp() const override { return true; }
 

	
 
	bool UseSystemCursor() override { return true; }
 

	
 
	void ClearSystemSprites() override;
 

	
 
	void PopulateSystemSprites() override;
 

	
 
	bool HasAnimBuffer() override { return true; }
 
	uint8 *GetAnimBuffer() override { return this->anim_buffer; }
 

	
 
	const char *GetName() const override { return "sdl-opengl"; }
 

	
 
protected:
src/video/sdl2_v.cpp
Show inline comments
 
@@ -538,20 +538,20 @@ const char *VideoDriver_SDL_Base::Initia
 
	FindResolutions();
 
	DEBUG(driver, 2, "Resolution for display: %ux%u", _cur_resolution.width, _cur_resolution.height);
 

	
 
	return nullptr;
 
}
 

	
 
const char *VideoDriver_SDL_Base::Start(const StringList &parm)
 
const char *VideoDriver_SDL_Base::Start(const StringList &param)
 
{
 
	if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
 

	
 
	const char *error = this->Initialize();
 
	if (error != nullptr) return error;
 

	
 
	this->startup_display = FindStartupDisplay(GetDriverParamInt(parm, "display", -1));
 
	this->startup_display = FindStartupDisplay(GetDriverParamInt(param, "display", -1));
 

	
 
	if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
 
		return SDL_GetError();
 
	}
 

	
 
	const char *dname = SDL_GetCurrentVideoDriver();
 
@@ -559,12 +559,18 @@ const char *VideoDriver_SDL_Base::Start(
 

	
 
	MarkWholeScreenDirty();
 

	
 
	SDL_StopTextInput();
 
	this->edit_box_focused = false;
 

	
 
#ifdef __EMSCRIPTEN__
 
	this->is_game_threaded = false;
 
#else
 
	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
 
#endif
 

	
 
	return nullptr;
 
}
 

	
 
void VideoDriver_SDL_Base::Stop()
 
{
 
	SDL_QuitSubSystem(SDL_INIT_VIDEO);
 
@@ -634,15 +640,19 @@ void VideoDriver_SDL_Base::LoopOnce()
 
void VideoDriver_SDL_Base::MainLoop()
 
{
 
#ifdef __EMSCRIPTEN__
 
	/* Run the main loop event-driven, based on RequestAnimationFrame. */
 
	emscripten_set_main_loop_arg(&this->EmscriptenLoop, this, 0, 1);
 
#else
 
	this->StartGameThread();
 

	
 
	while (!_exit_game) {
 
		LoopOnce();
 
	}
 

	
 
	this->StopGameThread();
 
#endif
 
}
 

	
 
bool VideoDriver_SDL_Base::ChangeResolution(int w, int h)
 
{
 
	return CreateMainSurface(w, h, true);
src/video/sdl_v.cpp
Show inline comments
 
@@ -567,16 +567,16 @@ bool VideoDriver_SDL::PollEvent()
 
		}
 
	}
 

	
 
	return true;
 
}
 

	
 
const char *VideoDriver_SDL::Start(const StringList &parm)
 
const char *VideoDriver_SDL::Start(const StringList &param)
 
{
 
	char buf[30];
 
	_use_hwpalette = GetDriverParamInt(parm, "hw_palette", 2);
 
	_use_hwpalette = GetDriverParamInt(param, "hw_palette", 2);
 

	
 
	/* Just on the offchance the audio subsystem started before the video system,
 
	 * check whether any part of SDL has been initialised before getting here.
 
	 * Slightly duplicated with sound/sdl_s.cpp */
 
	int ret_code = 0;
 
	if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
 
@@ -596,12 +596,14 @@ const char *VideoDriver_SDL::Start(const
 
	SDL_VideoDriverName(buf, sizeof buf);
 
	DEBUG(driver, 1, "SDL: using driver '%s'", buf);
 

	
 
	MarkWholeScreenDirty();
 
	SetupKeyboard();
 

	
 
	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
 

	
 
	return nullptr;
 
}
 

	
 
void VideoDriver_SDL::SetupKeyboard()
 
{
 
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 
@@ -644,18 +646,22 @@ void VideoDriver_SDL::InputLoop()
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
void VideoDriver_SDL::MainLoop()
 
{
 
	this->StartGameThread();
 

	
 
	for (;;) {
 
		if (_exit_game) break;
 

	
 
		this->Tick();
 
		this->SleepTillNextTick();
 
	}
 

	
 
	this->StopGameThread();
 
}
 

	
 
bool VideoDriver_SDL::ChangeResolution(int w, int h)
 
{
 
	return CreateMainSurface(w, h);
 
}
src/video/video_driver.cpp
Show inline comments
 
@@ -5,82 +5,144 @@
 
 * 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/>.
 
 */
 

	
 
/** @file video_driver.cpp Common code between video driver implementations. */
 

	
 
#include "../stdafx.h"
 
#include "../debug.h"
 
#include "../core/random_func.hpp"
 
#include "../network/network.h"
 
#include "../debug.h"
 
#include "../gfx_func.h"
 
#include "../progress.h"
 
#include "../thread.h"
 
#include "../window_func.h"
 
#include "video_driver.hpp"
 

	
 
bool _video_hw_accel; ///< Whether to consider hardware accelerated video drivers.
 

	
 
void VideoDriver::GameLoop()
 
{
 
	this->next_game_tick += this->GetGameInterval();
 

	
 
	/* Avoid next_game_tick getting behind more and more if it cannot keep up. */
 
	auto now = std::chrono::steady_clock::now();
 
	if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
 

	
 
	{
 
		std::lock_guard<std::mutex> lock(this->game_state_mutex);
 

	
 
		::GameLoop();
 
	}
 
}
 

	
 
void VideoDriver::GameThread()
 
{
 
	while (!_exit_game) {
 
		this->GameLoop();
 

	
 
		auto now = std::chrono::steady_clock::now();
 
		if (this->next_game_tick > now) {
 
			std::this_thread::sleep_for(this->next_game_tick - now);
 
		} else {
 
			/* Ensure we yield this thread if drawings wants to take a lock on
 
			 * the game state. This is mainly because most OSes have an
 
			 * optimization that if you unlock/lock a mutex in the same thread
 
			 * quickly, it will never context switch even if there is another
 
			 * thread waiting to take the lock on the same mutex. */
 
			std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
 
		}
 
	}
 
}
 

	
 
/* static */ void VideoDriver::GameThreadThunk(VideoDriver *drv)
 
{
 
	drv->GameThread();
 
}
 

	
 
void VideoDriver::StartGameThread()
 
{
 
	if (this->is_game_threaded) {
 
		this->is_game_threaded = StartNewThread(&this->game_thread, "ottd:game", &VideoDriver::GameThreadThunk, this);
 
	}
 

	
 
	DEBUG(driver, 1, "using %sthread for game-loop", this->is_game_threaded ? "" : "no ");
 
}
 

	
 
void VideoDriver::StopGameThread()
 
{
 
	if (!this->is_game_threaded) return;
 

	
 
	this->game_thread.join();
 
}
 

	
 
void VideoDriver::Tick()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 

	
 
	if (cur_ticks >= this->next_game_tick) {
 
		this->next_game_tick += this->GetGameInterval();
 
		/* Avoid next_game_tick getting behind more and more if it cannot keep up. */
 
		if (this->next_game_tick < cur_ticks - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = cur_ticks;
 

	
 
		/* The game loop is the part that can run asynchronously.
 
		 * The rest except sleeping can't. */
 
		this->UnlockVideoBuffer();
 
		::GameLoop();
 
		this->LockVideoBuffer();
 
	if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
 
		this->GameLoop();
 

	
 
		/* For things like dedicated server, don't run a separate draw-tick. */
 
		if (!this->HasGUI()) {
 
			::InputLoop();
 
			UpdateWindows();
 
			::UpdateWindows();
 
			this->next_draw_tick = this->next_game_tick;
 
		}
 
	}
 

	
 
	/* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
 
	if (this->HasGUI() && cur_ticks >= this->next_draw_tick && (_switch_mode == SM_NONE || _game_mode == GM_BOOTSTRAP || HasModalProgress())) {
 
	auto now = std::chrono::steady_clock::now();
 
	if (this->HasGUI() && now >= this->next_draw_tick) {
 
		this->next_draw_tick += this->GetDrawInterval();
 
		/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
 
		if (this->next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = cur_ticks;
 
		if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
 

	
 
		/* Keep the interactive randomizer a bit more random by requesting
 
		 * new values when-ever we can. */
 
		InteractiveRandom();
 

	
 
		while (this->PollEvent()) {}
 
		this->InputLoop();
 

	
 
		/* Check if the fast-forward button is still pressed. */
 
		if (fast_forward_key_pressed && !_networking && _game_mode != GM_MENU) {
 
			ChangeGameSpeed(true);
 
			this->fast_forward_via_key = true;
 
		} else if (this->fast_forward_via_key) {
 
			ChangeGameSpeed(false);
 
			this->fast_forward_via_key = false;
 
		}
 

	
 
		::InputLoop();
 
		UpdateWindows();
 
		{
 
			/* Tell the game-thread to stop so we can have a go. */
 
			std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
 
			std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
 

	
 
			this->LockVideoBuffer();
 

	
 
			while (this->PollEvent()) {}
 
			::InputLoop();
 

	
 
			/* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
 
			if (_game_mode == GM_BOOTSTRAP || _switch_mode == SM_NONE || HasModalProgress()) {
 
				::UpdateWindows();
 
			}
 

	
 
			this->PopulateSystemSprites();
 
		}
 

	
 
		this->CheckPaletteAnim();
 
		this->Paint();
 

	
 
		this->UnlockVideoBuffer();
 
	}
 
}
 

	
 
void VideoDriver::SleepTillNextTick()
 
{
 
	/* See how much time there is till we have to process the next event, and try to hit that as close as possible. */
 
	auto next_tick = std::min(this->next_draw_tick, this->next_game_tick);
 
	auto next_tick = this->next_draw_tick;
 
	auto now = std::chrono::steady_clock::now();
 

	
 
	if (!this->is_game_threaded) {
 
		next_tick = min(next_tick, this->next_game_tick);
 
	}
 

	
 
	if (next_tick > now) {
 
		this->UnlockVideoBuffer();
 
		std::this_thread::sleep_for(next_tick - now);
 
		this->LockVideoBuffer();
 
	}
 
}
src/video/video_driver.hpp
Show inline comments
 
@@ -13,13 +13,17 @@
 
#include "../driver.h"
 
#include "../core/geometry_type.hpp"
 
#include "../core/math_func.hpp"
 
#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;
 
extern std::vector<Dimension> _resolutions;
 
extern Dimension _cur_resolution;
 
extern bool _rightclick_emulate;
 
@@ -28,12 +32,14 @@ extern bool _video_hw_accel;
 
/** The base of all video drivers. */
 
class VideoDriver : public Driver {
 
	const uint DEFAULT_WINDOW_WIDTH = 640u;  ///< Default window width.
 
	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.
 
	 * @param top    The top most line of the dirty area.
 
	 * @param width  The width of the dirty area.
 
	 * @param height The height of the dirty area.
 
@@ -81,12 +87,17 @@ public:
 
	virtual bool UseSystemCursor()
 
	{
 
		return false;
 
	}
 

	
 
	/**
 
	 * Populate all sprites in cache.
 
	 */
 
	virtual void PopulateSystemSprites() {}
 

	
 
	/**
 
	 * Clear all cached sprites.
 
	 */
 
	virtual void ClearSystemSprites() {}
 

	
 
	/**
 
	 * Whether the driver has a graphical user interface with the end user.
 
@@ -237,12 +248,22 @@ protected:
 
	 * Process a single system event.
 
	 * @returns False if there are no more events to process.
 
	 */
 
	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.
 
	 */
 
	void Tick();
 

	
 
@@ -268,9 +289,20 @@ protected:
 

	
 
	std::chrono::steady_clock::time_point next_game_tick;
 
	std::chrono::steady_clock::time_point next_draw_tick;
 

	
 
	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 */
src/video/win32_v.cpp
Show inline comments
 
@@ -861,18 +861,22 @@ bool VideoDriver_Win32Base::PollEvent()
 

	
 
	return true;
 
}
 

	
 
void VideoDriver_Win32Base::MainLoop()
 
{
 
	this->StartGameThread();
 

	
 
	for (;;) {
 
		if (_exit_game) break;
 

	
 
		this->Tick();
 
		this->SleepTillNextTick();
 
	}
 

	
 
	this->StopGameThread();
 
}
 

	
 
void VideoDriver_Win32Base::ClientSizeChanged(int w, int h, bool force)
 
{
 
	/* Allocate backing store of the new size. */
 
	if (this->AllocateBackingStore(w, h, force)) {
 
@@ -992,12 +996,14 @@ const char *VideoDriver_Win32GDI::Start(
 
	this->MakePalette();
 
	this->AllocateBackingStore(_cur_resolution.width, _cur_resolution.height);
 
	this->MakeWindow(_fullscreen);
 

	
 
	MarkWholeScreenDirty();
 

	
 
	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
 

	
 
	return nullptr;
 
}
 

	
 
void VideoDriver_Win32GDI::Stop()
 
{
 
	DeleteObject(this->gdi_palette);
 
@@ -1112,19 +1118,13 @@ void VideoDriver_Win32GDI::Paint()
 
		switch (blitter->UsePaletteAnimation()) {
 
			case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
 
				this->UpdatePalette(dc2, _local_palette.first_dirty, _local_palette.count_dirty);
 
				break;
 

	
 
			case Blitter::PALETTE_ANIMATION_BLITTER: {
 
				bool need_buf = _screen.dst_ptr == nullptr;
 
				if (need_buf) _screen.dst_ptr = this->GetVideoPointer();
 
				blitter->PaletteAnimate(_local_palette);
 
				if (need_buf) {
 
					this->ReleaseVideoPointer();
 
					_screen.dst_ptr = nullptr;
 
				}
 
				break;
 
			}
 

	
 
			case Blitter::PALETTE_ANIMATION_NONE:
 
				break;
 

	
 
@@ -1288,12 +1288,14 @@ const char *VideoDriver_Win32OpenGL::Sta
 
	}
 

	
 
	this->ClientSizeChanged(this->width, this->height, true);
 

	
 
	MarkWholeScreenDirty();
 

	
 
	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
 

	
 
	return nullptr;
 
}
 

	
 
void VideoDriver_Win32OpenGL::Stop()
 
{
 
	this->DestroyContext();
 
@@ -1368,12 +1370,17 @@ bool VideoDriver_Win32OpenGL::AfterBlitt
 
{
 
	assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
 
	this->ClientSizeChanged(this->width, this->height, true);
 
	return true;
 
}
 

	
 
void VideoDriver_Win32OpenGL::PopulateSystemSprites()
 
{
 
	OpenGLBackend::Get()->PopulateCursorCache();
 
}
 

	
 
void VideoDriver_Win32OpenGL::ClearSystemSprites()
 
{
 
	OpenGLBackend::Get()->ClearCursorCache();
 
}
 

	
 
bool VideoDriver_Win32OpenGL::AllocateBackingStore(int w, int h, bool force)
src/video/win32_v.h
Show inline comments
 
@@ -126,12 +126,14 @@ public:
 
	bool AfterBlitterChange() override;
 

	
 
	bool HasEfficient8Bpp() const override { return true; }
 

	
 
	bool UseSystemCursor() override { return true; }
 

	
 
	void PopulateSystemSprites() override;
 

	
 
	void ClearSystemSprites() override;
 

	
 
	bool HasAnimBuffer() override { return true; }
 
	uint8 *GetAnimBuffer() override { return this->anim_buffer; }
 

	
 
	const char *GetName() const override { return "win32-opengl"; }
0 comments (0 inline, 0 general)