Browse Source

UI: Disable OSX V-Sync when program is open

OSX has an annoying feature called "BeamSync", which on later versions
of OSX is always on.  For whatever reason, Apple devs decided to force
this feature to always be on, so applications must always render with
v-sync regardless of what they set their swap interval to.

This issue would cause syncing to the vertical refresh for each
additional active display, and wouldn't allow rendering above the
current refresh rate.  This caused major rendering stalls and prevented
video frame timing from being accurate.

This fixes the issue by using an undocumented set of functions to
disable BeamSync.  Note that because this is an undocumented method of
working around the issue, its existence cannot be guaranteed.  If the
functions no longer exist for whatever reason, it will safely do
nothing.
jp9000 10 years ago
parent
commit
832043fc7f

+ 2 - 0
obs/data/locale/en-US.ini

@@ -43,6 +43,8 @@ Untitled="Untitled"
 New="New"
 Duplicate="Duplicate"
 Enable="Enable"
+DisableOSXVSync="Disable OSX V-Sync"
+ResetOSXVSyncOnExit="Reset OSX V-Sync on Exit"
 HighResourceUsage="Encoding overloaded!  Consider turning down video settings or using a faster encoding preset."
 
 # title bar strings

+ 14 - 0
obs/forms/OBSBasicSettings.ui

@@ -2824,6 +2824,20 @@
                      </property>
                     </widget>
                    </item>
+                   <item row="5" column="1">
+                    <widget class="QCheckBox" name="disableOSXVSync">
+                     <property name="text">
+                      <string>DisableOSXVSync</string>
+                     </property>
+                    </widget>
+                   </item>
+                   <item row="6" column="1">
+                    <widget class="QCheckBox" name="resetOSXVSync">
+                     <property name="text">
+                      <string>ResetOSXVSyncOnExit</string>
+                     </property>
+                    </widget>
+                   </item>
                   </layout>
                  </widget>
                 </item>

+ 20 - 0
obs/obs-app.cpp

@@ -333,6 +333,12 @@ bool OBSApp::InitGlobalConfigDefaults()
 
 	config_set_default_bool(globalConfig, "BasicWindow", "PreviewEnabled",
 			true);
+
+#ifdef __APPLE__
+	config_set_default_bool(globalConfig, "Video", "DisableOSXVSync", true);
+	config_set_default_bool(globalConfig, "Video", "ResetOSXVSyncOnExit",
+			true);
+#endif
 	return true;
 }
 
@@ -530,6 +536,15 @@ OBSApp::OBSApp(int &argc, char **argv, profiler_name_store_t *store)
 
 OBSApp::~OBSApp()
 {
+#ifdef __APPLE__
+	bool vsyncDiabled = config_get_bool(globalConfig, "Video",
+			"DisableOSXVSync");
+	bool resetVSync = config_get_bool(globalConfig, "Video",
+			"ResetOSXVSyncOnExit");
+	if (vsyncDiabled && resetVSync)
+		EnableOSXVSync(true);
+#endif
+
 	os_inhibit_sleep_set_active(sleepInhibitor, false);
 	os_inhibit_sleep_destroy(sleepInhibitor);
 }
@@ -639,6 +654,11 @@ void OBSApp::AppInit()
 	config_set_default_string(globalConfig, "Basic", "SceneCollectionFile",
 			Str("Untitled"));
 
+#ifdef __APPLE__
+	if (config_get_bool(globalConfig, "Video", "DisableOSXVSync"))
+		EnableOSXVSync(false);
+#endif
+
 	move_basic_to_profiles();
 	move_basic_to_scene_collections();
 

+ 31 - 0
obs/platform-osx.mm

@@ -16,6 +16,7 @@
 ******************************************************************************/
 
 #include <sstream>
+#include <dlfcn.h>
 #include <util/base.h>
 #include <obs-config.h>
 #include "platform.hpp"
@@ -148,3 +149,33 @@ void SetAlwaysOnTop(QMainWindow *window, bool enable)
 	window->setWindowFlags(flags);
 	window->show();
 }
+
+typedef void (*set_int_t)(int);
+
+void EnableOSXVSync(bool enable)
+{
+	static bool initialized = false;
+	static bool valid = false;
+	static set_int_t set_debug_options = nullptr;
+	static set_int_t deferred_updates = nullptr;
+
+	if (!initialized) {
+		void *quartzCore = dlopen("/System/Library/Frameworks/"
+				"QuartzCore.framework/QuartzCore", RTLD_LAZY);
+		if (quartzCore) {
+			set_debug_options = (set_int_t)dlsym(quartzCore,
+					"CGSSetDebugOptions");
+			deferred_updates = (set_int_t)dlsym(quartzCore,
+					"CGSDeferredUpdates");
+
+			valid = set_debug_options && deferred_updates;
+		}
+
+		initialized = true;
+	}
+
+	if (valid) {
+		set_debug_options(enable ? 0 : 0x08000000);
+		deferred_updates(enable ? 1 : 0);
+	}
+}

+ 4 - 0
obs/platform.hpp

@@ -52,3 +52,7 @@ void SetAlwaysOnTop(QMainWindow *window, bool enable);
 uint32_t GetWindowsVersion();
 void SetAeroEnabled(bool enable);
 #endif
+
+#ifdef __APPLE__
+void EnableOSXVSync(bool enable);
+#endif

+ 43 - 0
obs/window-basic-settings.cpp

@@ -346,6 +346,8 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	HookWidget(ui->colorFormat,          COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->colorSpace,           COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->colorRange,           COMBO_CHANGED,  ADV_CHANGED);
+	HookWidget(ui->disableOSXVSync,      CHECK_CHANGED,  ADV_CHANGED);
+	HookWidget(ui->resetOSXVSync,        CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->streamDelayEnable,    CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->streamDelaySec,       SCROLL_CHANGED, ADV_CHANGED);
 	HookWidget(ui->streamDelayPreserve,  CHECK_CHANGED,  ADV_CHANGED);
@@ -378,6 +380,13 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	ui->adapter = nullptr;
 #endif
 
+#ifndef __APPLE__
+	delete ui->disableOSXVSync;
+	delete ui->resetOSXVSync;
+	ui->disableOSXVSync = nullptr;
+	ui->resetOSXVSync = nullptr;
+#endif
+
 	connect(ui->streamDelaySec, SIGNAL(valueChanged(int)),
 			this, SLOT(UpdateStreamDelayEstimate()));
 	connect(ui->outputMode, SIGNAL(currentIndexChanged(int)),
@@ -1676,6 +1685,16 @@ void OBSBasicSettings::LoadAdvancedSettings()
 		ui->advancedVideoContainer->setEnabled(false);
 	}
 
+#ifdef __APPLE__
+	bool disableOSXVSync = config_get_bool(App()->GlobalConfig(),
+			"Video", "DisableOSXVSync");
+	bool resetOSXVSync = config_get_bool(App()->GlobalConfig(),
+			"Video", "ResetOSXVSyncOnExit");
+	ui->disableOSXVSync->setChecked(disableOSXVSync);
+	ui->resetOSXVSync->setChecked(resetOSXVSync);
+	ui->resetOSXVSync->setEnabled(disableOSXVSync);
+#endif
+
 	loading = false;
 }
 
@@ -2074,6 +2093,20 @@ void OBSBasicSettings::SaveAdvancedSettings()
 		config_set_string(App()->GlobalConfig(), "Video", "Renderer",
 				QT_TO_UTF8(ui->renderer->currentText()));
 #endif
+
+#ifdef __APPLE__
+	if (WidgetChanged(ui->disableOSXVSync)) {
+		bool disable = ui->disableOSXVSync->isChecked();
+		config_set_bool(App()->GlobalConfig(),
+				"Video", "DisableOSXVSync", disable);
+		EnableOSXVSync(!disable);
+	}
+	if (WidgetChanged(ui->resetOSXVSync))
+		config_set_bool(App()->GlobalConfig(),
+				"Video", "ResetOSXVSyncOnExit",
+				ui->resetOSXVSync->isChecked());
+#endif
+
 	SaveSpinBox(ui->audioBufferingTime, "Audio", "BufferingTime");
 	SaveCombo(ui->colorFormat, "Video", "ColorFormat");
 	SaveCombo(ui->colorSpace, "Video", "ColorSpace");
@@ -2992,3 +3025,13 @@ void OBSBasicSettings::SimpleRecordingQualityLosslessWarning(int idx)
 
 	lastSimpleRecQualityIdx = idx;
 }
+
+void OBSBasicSettings::on_disableOSXVSync_clicked()
+{
+#ifdef __APPLE__
+	if (!loading) {
+		bool disable = ui->disableOSXVSync->isChecked();
+		ui->resetOSXVSync->setEnabled(disable);
+	}
+#endif
+}

+ 2 - 0
obs/window-basic-settings.hpp

@@ -256,6 +256,8 @@ private slots:
 	void on_outputResolution_editTextChanged(const QString &text);
 	void on_baseResolution_editTextChanged(const QString &text);
 
+	void on_disableOSXVSync_clicked();
+
 	void GeneralChanged();
 	void AudioChanged();
 	void AudioChangedRestart();