Files @ r25014:e1f1bf3a062e
Branch filter:

Location: cpp/openttd-patchpack/source/src/video/win32_v.h - annotation

Patric Stout
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.
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r9111:983de9c5a848
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r7170:38b143754b40
r24884:75b20bf9a7fd
r24884:75b20bf9a7fd
r7170:38b143754b40
r24875:dd67bf72984d
r24875:dd67bf72984d
r7170:38b143754b40
r25011:61d28a13bb41
r24878:fb3a0559b42f
r24878:fb3a0559b42f
r24877:41f1a39844df
r23497:a0ab44ebd2fa
r5475:3f5cd13d1b63
r23497:a0ab44ebd2fa
r7170:38b143754b40
r23497:a0ab44ebd2fa
r7170:38b143754b40
r23497:a0ab44ebd2fa
r18160:133e96e28508
r23497:a0ab44ebd2fa
r18395:22ade7a0b9e1
r23497:a0ab44ebd2fa
r20642:9fa74f045d3c
r24665:20a8caea4459
r24884:75b20bf9a7fd
r24884:75b20bf9a7fd
r24885:dcf8db660b78
r24884:75b20bf9a7fd
r24885:dcf8db660b78
r24885:dcf8db660b78
r24885:dcf8db660b78
r24885:dcf8db660b78
r24884:75b20bf9a7fd
r25011:61d28a13bb41
r24877:41f1a39844df
r24665:20a8caea4459
r24797:588223e116f1
r24849:01243a72f255
r24850:df6b081a960b
r24850:df6b081a960b
r24852:00c1bd78d033
r24939:f00f6879a003
r24845:fab6f86c6617
r24878:fb3a0559b42f
r25013:4c02a1340d93
r24888:87ba0f0b3e65
r24879:89d0ee92066a
r24879:89d0ee92066a
r24878:fb3a0559b42f
r24876:e68a991d2383
r24876:e68a991d2383
r24883:b624287d948b
r24883:b624287d948b
r24888:87ba0f0b3e65
r24888:87ba0f0b3e65
r24876:e68a991d2383
r24876:e68a991d2383
r24876:e68a991d2383
r24845:fab6f86c6617
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24883:b624287d948b
r24877:41f1a39844df
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24875:dd67bf72984d
r24876:e68a991d2383
r24876:e68a991d2383
r24877:41f1a39844df
r24877:41f1a39844df
r24883:b624287d948b
r24877:41f1a39844df
r24876:e68a991d2383
r24883:b624287d948b
r24876:e68a991d2383
r24876:e68a991d2383
r24876:e68a991d2383
r24877:41f1a39844df
r24877:41f1a39844df
r24877:41f1a39844df
r24877:41f1a39844df
r24877:41f1a39844df
r24877:41f1a39844df
r24877:41f1a39844df
r7170:38b143754b40
r7170:38b143754b40
r17629:21e9dfd343cd
r24875:dd67bf72984d
r7170:38b143754b40
r24906:0b82953bc689
r24875:dd67bf72984d
r7170:38b143754b40
r5475:3f5cd13d1b63
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24908:4dfe7bc46264
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24895:0ba728d1a5e0
r24895:0ba728d1a5e0
r24905:e535e9d6c85d
r24905:e535e9d6c85d
r25014:e1f1bf3a062e
r25014:e1f1bf3a062e
r24905:e535e9d6c85d
r24905:e535e9d6c85d
r24908:4dfe7bc46264
r24908:4dfe7bc46264
r24908:4dfe7bc46264
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24908:4dfe7bc46264
r24908:4dfe7bc46264
r24908:4dfe7bc46264
r24908:4dfe7bc46264
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24883:b624287d948b
r24888:87ba0f0b3e65
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24906:0b82953bc689
r24879:89d0ee92066a
r25003:fdd8312ceea6
r25003:fdd8312ceea6
r25003:fdd8312ceea6
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r24879:89d0ee92066a
r5988:7f6f0097eeb1
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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 win32_v.h Base of the Windows video driver. */

#ifndef VIDEO_WIN32_H
#define VIDEO_WIN32_H

#include "video_driver.hpp"
#include <mutex>
#include <condition_variable>

/** Base class for Windows video drivers. */
class VideoDriver_Win32Base : public VideoDriver {
public:
	VideoDriver_Win32Base() : main_wnd(nullptr), fullscreen(false) {}

	void Stop() override;

	void MakeDirty(int left, int top, int width, int height) override;

	void MainLoop() override;

	bool ChangeResolution(int w, int h) override;

	bool ToggleFullscreen(bool fullscreen) override;

	bool ClaimMousePointer() override;

	void EditBoxLostFocus() override;

protected:
	HWND main_wnd;          ///< Handle to system window.
	bool fullscreen;        ///< Whether to use (true) fullscreen mode.
	bool has_focus = false; ///< Does our window have system focus?
	Rect dirty_rect;        ///< Region of the screen that needs redrawing.
	int width = 0;          ///< Width in pixels of our display surface.
	int height = 0;         ///< Height in pixels of our display surface.
	int width_org = 0;      ///< Original monitor resolution width, before we changed it.
	int height_org = 0;     ///< Original monitor resolution height, before we changed it.

	bool buffer_locked;     ///< Video buffer was locked by the main thread.

	Dimension GetScreenSize() const override;
	float GetDPIScale() override;
	void InputLoop() override;
	bool LockVideoBuffer() override;
	void UnlockVideoBuffer() override;
	void CheckPaletteAnim() override;
	bool PollEvent() override;

	void Initialize();
	bool MakeWindow(bool full_screen, bool resize = true);
	void ClientSizeChanged(int w, int h, bool force = false);

	/** Get screen depth to use for fullscreen mode. */
	virtual uint8 GetFullscreenBpp();
	/** (Re-)create the backing store. */
	virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
	/** Get a pointer to the video buffer. */
	virtual void *GetVideoPointer() = 0;
	/** Hand video buffer back to the painting backend. */
	virtual void ReleaseVideoPointer() {}
	/** Palette of the window has changed. */
	virtual void PaletteChanged(HWND hWnd) = 0;

private:
	friend LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
};
/** The GDI video driver for windows. */
class VideoDriver_Win32GDI : public VideoDriver_Win32Base {
public:
	VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}

	const char *Start(const StringList &param) override;

	void Stop() override;

	bool AfterBlitterChange() override;

	const char *GetName() const override { return "win32"; }

protected:
	HBITMAP  dib_sect;      ///< System bitmap object referencing our rendering buffer.
	HPALETTE gdi_palette;   ///< Palette object for 8bpp blitter.
	void     *buffer_bits;  ///< Internal rendering buffer.

	void Paint() override;
	void *GetVideoPointer() override { return this->buffer_bits; }

	bool AllocateBackingStore(int w, int h, bool force = false) override;
	void PaletteChanged(HWND hWnd) override;
	void MakePalette();
	void UpdatePalette(HDC dc, uint start, uint count);

#ifdef _DEBUG
public:
	static int RedrawScreenDebug();
#endif
};

/** The factory for Windows' video driver. */
class FVideoDriver_Win32GDI : public DriverFactoryBase {
public:
	FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO, 9, "win32", "Win32 GDI Video Driver") {}
	Driver *CreateInstance() const override { return new VideoDriver_Win32GDI(); }
};

#ifdef WITH_OPENGL

/** The OpenGL video driver for windows. */
class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
public:
	VideoDriver_Win32OpenGL() : dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr) {}

	const char *Start(const StringList &param) override;

	void Stop() override;

	bool ToggleFullscreen(bool fullscreen) override;

	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"; }

protected:
	HDC    dc;          ///< Window device context.
	HGLRC  gl_rc;       ///< OpenGL context.
	bool   vsync;       ///< Enable VSync?
	uint8 *anim_buffer; ///< Animation buffer from OpenGL back-end.

	uint8 GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.

	void Paint() override;

	bool AllocateBackingStore(int w, int h, bool force = false) override;
	void *GetVideoPointer() override;
	void ReleaseVideoPointer() override;
	void PaletteChanged(HWND hWnd) override {}

	const char *AllocateContext();
	void DestroyContext();
};

/** The factory for Windows' OpenGL video driver. */
class FVideoDriver_Win32OpenGL : public DriverFactoryBase {
public:
	FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 10, "win32-opengl", "Win32 OpenGL Video Driver") {}
	/* virtual */ Driver *CreateInstance() const override { return new VideoDriver_Win32OpenGL(); }

protected:
	bool UsesHardwareAcceleration() const override { return true; }
};

#endif /* WITH_OPENGL */

#endif /* VIDEO_WIN32_H */