Changeset - r25003:fdd8312ceea6
[Not reviewed]
master
0 12 0
Patric Stout - 3 years ago 2021-03-08 14:42:39
truebrain@openttd.org
Add: Option to (dis-)allow accelerated video drivers. (#8819)

The video drivers using the OpenGL backend are currently our only
accelerated drivers. The options defaults to off for macOS builds and
to on everywhere else.

Co-authored-by: Michael Lutz <michi@icosahedron.de>
12 files changed with 76 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/driver.cpp
Show inline comments
 
@@ -6,16 +6,18 @@
 
 */
 

	
 
/** @file driver.cpp Base for all driver handling. */
 

	
 
#include "stdafx.h"
 
#include "debug.h"
 
#include "error.h"
 
#include "sound/sound_driver.hpp"
 
#include "music/music_driver.hpp"
 
#include "video/video_driver.hpp"
 
#include "string_func.h"
 
#include "table/strings.h"
 
#include <string>
 
#include <sstream>
 

	
 
#include "safeguards.h"
 

	
 
std::string _ini_videodriver;        ///< The video driver a stored in the configuration file.
 
@@ -108,12 +110,14 @@ bool DriverFactoryBase::SelectDriverImpl
 
				DriverFactoryBase *d = (*it).second;
 

	
 
				/* Check driver type */
 
				if (d->type != type) continue;
 
				if (d->priority != priority) continue;
 

	
 
				if (type == Driver::DT_VIDEO && !_video_hw_accel && d->UsesHardwareAcceleration()) continue;
 

	
 
				Driver *oldd = *GetActiveDriver(type);
 
				Driver *newd = d->CreateInstance();
 
				*GetActiveDriver(type) = newd;
 

	
 
				const char *err = newd->Start({});
 
				if (err == nullptr) {
 
@@ -122,12 +126,18 @@ bool DriverFactoryBase::SelectDriverImpl
 
					return true;
 
				}
 

	
 
				*GetActiveDriver(type) = oldd;
 
				DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err);
 
				delete newd;
 

	
 
				if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
 
					_video_hw_accel = false;
 
					ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION);
 
					ScheduleErrorMessage(msg);
 
				}
 
			}
 
		}
 
		usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
 
	} else {
 
		/* Extract the driver name and put parameter list in parm */
 
		std::istringstream buffer(name);
src/driver.h
Show inline comments
 
@@ -104,12 +104,21 @@ private:
 

	
 
protected:
 
	DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
 

	
 
	virtual ~DriverFactoryBase();
 

	
 
	/**
 
	 * Does the driver use hardware acceleration (video-drivers only).
 
	 * @return True if the driver uses hardware acceleration.
 
	 */
 
	virtual bool UsesHardwareAcceleration() const
 
	{
 
		return false;
 
	}
 

	
 
public:
 
	/**
 
	 * Shuts down all active drivers
 
	 */
 
	static void ShutdownDrivers()
 
	{
src/lang/english.txt
Show inline comments
 
@@ -998,12 +998,16 @@ STR_GAME_OPTIONS_FULLSCREEN             
 
STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP                             :{BLACK}Check this box to play OpenTTD fullscreen mode
 

	
 
STR_GAME_OPTIONS_RESOLUTION                                     :{BLACK}Screen resolution
 
STR_GAME_OPTIONS_RESOLUTION_TOOLTIP                             :{BLACK}Select the screen resolution to use
 
STR_GAME_OPTIONS_RESOLUTION_OTHER                               :other
 

	
 
STR_GAME_OPTIONS_VIDEO_ACCELERATION                             :{BLACK}Hardware acceleration
 
STR_GAME_OPTIONS_VIDEO_ACCELERATION_TOOLTIP                     :{BLACK}Check this box to allow OpenTTD to try to use hardware acceleration. A changed setting will only be applied upon game restart
 
STR_GAME_OPTIONS_VIDEO_ACCELERATION_RESTART                     :{WHITE}The setting will only take effect after a game restart
 

	
 
STR_GAME_OPTIONS_GUI_ZOOM_FRAME                                 :{BLACK}Interface size
 
STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_TOOLTIP                      :{BLACK}Select the interface element size to use
 

	
 
STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_AUTO                         :(auto-detect)
 
STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_NORMAL                       :Normal
 
STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_2X_ZOOM                      :Double size
 
@@ -1784,12 +1788,16 @@ STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRE
 
STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND                :{WHITE}... ignoring Base Graphics set '{RAW_STRING}': not found
 
STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND                  :{WHITE}... ignoring Base Sounds set '{RAW_STRING}': not found
 
STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND                   :{WHITE}... ignoring Base Music set '{RAW_STRING}': not found
 
STR_CONFIG_ERROR_OUT_OF_MEMORY                                  :{WHITE}Out of memory
 
STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG                            :{WHITE}Allocating {BYTES} of spritecache failed. The spritecache was reduced to {BYTES}. This will reduce the performance of OpenTTD. To reduce memory requirements you can try to disable 32bpp graphics and/or zoom-in levels
 

	
 
# Video initalization errors
 
STR_VIDEO_DRIVER_ERROR                                          :{WHITE}Error with video settings...
 
STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION                 :{WHITE}... no compatible GPU found. Hardware acceleration disabled
 

	
 
# Intro window
 
STR_INTRO_CAPTION                                               :{WHITE}OpenTTD {REV}
 

	
 
STR_INTRO_NEW_GAME                                              :{BLACK}New Game
 
STR_INTRO_LOAD_GAME                                             :{BLACK}Load Game
 
STR_INTRO_PLAY_SCENARIO                                         :{BLACK}Play Scenario
src/settings_gui.cpp
Show inline comments
 
@@ -32,12 +32,13 @@
 
#include "language.h"
 
#include "textfile_gui.h"
 
#include "stringfilter_type.h"
 
#include "querystring_gui.h"
 
#include "fontcache.h"
 
#include "zoom_func.h"
 
#include "video/video_driver.hpp"
 

	
 
#include <vector>
 

	
 
#include "safeguards.h"
 

	
 

	
 
@@ -378,12 +379,19 @@ struct GameOptionsWindow : Window {
 
					ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
 
				}
 
				this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
 
				this->SetDirty();
 
				break;
 

	
 
			case WID_GO_VIDEO_ACCEL_BUTTON:
 
				_video_hw_accel = !_video_hw_accel;
 
				ShowErrorMessage(STR_GAME_OPTIONS_VIDEO_ACCELERATION_RESTART, INVALID_STRING_ID, WL_INFO);
 
				this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel);
 
				this->SetDirty();
 
				break;
 

	
 
			default: {
 
				int selected;
 
				DropDownList list = this->BuildDropDownList(widget, &selected);
 
				if (!list.empty()) {
 
					ShowDropDownList(this, std::move(list), selected, widget);
 
				} else {
 
@@ -490,12 +498,13 @@ struct GameOptionsWindow : Window {
 
	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
 
	 */
 
	void OnInvalidateData(int data = 0, bool gui_scope = true) override
 
	{
 
		if (!gui_scope) return;
 
		this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
 
		this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel);
 

	
 
		bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
 
		this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
 

	
 
		for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
 
			this->SetWidgetDisabledState(WID_GO_BASE_GRF_TEXTFILE + tft, BaseGraphics::GetUsedSet() == nullptr || BaseGraphics::GetUsedSet()->GetTextfile(tft) == nullptr);
 
@@ -518,30 +527,34 @@ static const NWidgetPart _nested_game_op
 
			NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_AUTOSAVE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_TOOLTIP), SetFill(1, 0),
 
				EndContainer(),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_RESOLUTION_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_RESOLUTION_TOOLTIP), SetFill(1, 0), SetPadding(0, 0, 3, 0),
 
					NWidget(NWID_HORIZONTAL),
 
					NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0), SetPadding(0, 0, 3, 0),
 
						NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL),
 
						NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
 
					EndContainer(),
 
					NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
 
						NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_VIDEO_ACCELERATION, STR_NULL),
 
						NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_VIDEO_ACCEL_BUTTON),  SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_VIDEO_ACCELERATION_TOOLTIP),
 
					EndContainer(),
 
				EndContainer(),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GUI_ZOOM_FRAME, STR_NULL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_GUI_ZOOM_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_TOOLTIP), SetFill(1, 0),
 
				EndContainer(),
 
				NWidget(NWID_SPACER), SetMinimalSize(0, 0), SetFill(0, 1),
 
			EndContainer(),
 

	
 
			NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_LANG_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_LANGUAGE_TOOLTIP), SetFill(1, 0),
 
				EndContainer(),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_CURRENCY_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_CURRENCY_UNITS_DROPDOWN_TOOLTIP), SetFill(1, 0),
 
				EndContainer(),
 
				NWidget(NWID_SPACER), SetMinimalSize(0, 0), SetFill(0, 1),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GUI_ZOOM_FRAME, STR_NULL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_GUI_ZOOM_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_TOOLTIP), SetFill(1, 0),
 
				EndContainer(),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_FONT_ZOOM, STR_NULL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_FONT_ZOOM_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_TOOLTIP), SetFill(1, 0),
 
				EndContainer(),
 
			EndContainer(),
 
		EndContainer(),
 

	
src/table/misc_settings.ini
Show inline comments
 
@@ -9,12 +9,15 @@ extern std::string _config_language_file
 

	
 
static const char *_support8bppmodes = "no|system|hardware";
 

	
 
#ifdef WITH_COCOA
 
extern bool _allow_hidpi_window;
 
#endif
 
#ifndef WITH_COCOA
 
#define WITHOUT_COCOA
 
#endif
 

	
 
static const SettingDescGlobVarList _misc_settings[] = {
 
[post-amble]
 
};
 
[templates]
 
SDTG_LIST  =  SDTG_LIST($name, $type, $length, $flags, $guiflags, $var, $def,                               $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
 
@@ -57,12 +60,26 @@ full     = ""SHOW_TOWN_NAMES|SHOW_STATIO
 
[SDTG_BOOL]
 
name     = ""fullscreen""
 
var      = _fullscreen
 
def      = false
 
cat      = SC_BASIC
 

	
 
[SDTG_BOOL]
 
ifdef    = WITH_COCOA
 
name     = ""video_hw_accel""
 
var      = _video_hw_accel
 
def      = false
 
cat      = SC_BASIC
 

	
 
[SDTG_BOOL]
 
ifdef    = WITHOUT_COCOA
 
name     = ""video_hw_accel""
 
var      = _video_hw_accel
 
def      = true
 
cat      = SC_BASIC
 

	
 
[SDTG_OMANY]
 
name     = ""support8bpp""
 
type     = SLE_UINT8
 
var      = _support8bpp
 
def      = 0
 
max      = 2
src/video/cocoa/cocoa_ogl.h
Show inline comments
 
@@ -51,9 +51,12 @@ protected:
 
};
 

	
 
class FVideoDriver_CocoaOpenGL : public DriverFactoryBase {
 
public:
 
	FVideoDriver_CocoaOpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 9, "cocoa-opengl", "Cocoa OpenGL Video Driver") {}
 
	Driver *CreateInstance() const override { return new VideoDriver_CocoaOpenGL(); }
 

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

	
 
#endif /* VIDEO_COCOA_OGL_H */
src/video/cocoa/cocoa_v.h
Show inline comments
 
@@ -119,11 +119,11 @@ protected:
 

	
 
	void *GetVideoPointer() override { return this->buffer_depth == 8 ? this->pixel_buffer : this->window_buffer; }
 
};
 

	
 
class FVideoDriver_CocoaQuartz : public DriverFactoryBase {
 
public:
 
	FVideoDriver_CocoaQuartz() : DriverFactoryBase(Driver::DT_VIDEO, 10, "cocoa", "Cocoa Video Driver") {}
 
	FVideoDriver_CocoaQuartz() : DriverFactoryBase(Driver::DT_VIDEO, 8, "cocoa", "Cocoa Video Driver") {}
 
	Driver *CreateInstance() const override { return new VideoDriver_CocoaQuartz(); }
 
};
 

	
 
#endif /* VIDEO_COCOA_H */
src/video/sdl2_opengl_v.h
Show inline comments
 
@@ -48,7 +48,10 @@ private:
 

	
 
/** The factory for SDL' OpenGL video driver. */
 
class FVideoDriver_SDL_OpenGL : public DriverFactoryBase {
 
public:
 
	FVideoDriver_SDL_OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 8, "sdl-opengl", "SDL OpenGL Video Driver") {}
 
	/* virtual */ Driver *CreateInstance() const override { return new VideoDriver_SDL_OpenGL(); }
 

	
 
protected:
 
	bool UsesHardwareAcceleration() const override { return true; }
 
};
src/video/video_driver.cpp
Show inline comments
 
@@ -14,12 +14,14 @@
 
#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.
 

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

	
 
	if (cur_ticks >= this->next_game_tick) {
 
		this->next_game_tick += this->GetGameInterval();
src/video/video_driver.hpp
Show inline comments
 
@@ -20,12 +20,13 @@
 
#include <vector>
 

	
 
extern std::string _ini_videodriver;
 
extern std::vector<Dimension> _resolutions;
 
extern Dimension _cur_resolution;
 
extern bool _rightclick_emulate;
 
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.
 

	
src/video/win32_v.h
Show inline comments
 
@@ -172,11 +172,14 @@ protected:
 

	
 
/** 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 */
src/widgets/settings_widget.h
Show inline comments
 
@@ -29,12 +29,13 @@ enum GameOptionsWidgets {
 
	WID_GO_BASE_SFX_DESCRIPTION = WID_GO_BASE_SFX_TEXTFILE + TFT_END,     ///< Description of selected base SFX.
 
	WID_GO_BASE_MUSIC_DROPDOWN,    ///< Use to select a base music set.
 
	WID_GO_BASE_MUSIC_STATUS,      ///< Info about corrupted files etc.
 
	WID_GO_BASE_MUSIC_TEXTFILE,    ///< Open base music readme, changelog (+1) or license (+2).
 
	WID_GO_BASE_MUSIC_DESCRIPTION = WID_GO_BASE_MUSIC_TEXTFILE + TFT_END, ///< Description of selected base music set.
 
	WID_GO_FONT_ZOOM_DROPDOWN,     ///< Dropdown for the font zoom level.
 
	WID_GO_VIDEO_ACCEL_BUTTON,     ///< Toggle for video acceleration.
 
};
 

	
 
/** Widgets of the #GameSettingsWindow class. */
 
enum GameSettingsWidgets {
 
	WID_GS_FILTER,             ///< Text filter.
 
	WID_GS_OPTIONSPANEL,       ///< Panel widget containing the option lists.
0 comments (0 inline, 0 general)