Browse Source

Merge pull request #95235 from bruvzg/macos_opengl_load

[macOS] Load OpenGL.framework by path to avoid issues with non-Latin executable names.
Rémi Verschelde 1 year ago
parent
commit
7cf8e5ef2b

+ 1 - 0
platform/macos/gl_manager_macos_legacy.h

@@ -62,6 +62,7 @@ class GLManagerLegacy_MacOS {
 
 	Error create_context(GLWindow &win);
 
+	bool framework_loaded = false;
 	bool use_vsync = false;
 	CGLEnablePtr CGLEnable = nullptr;
 	CGLSetParameterPtr CGLSetParameter = nullptr;

+ 13 - 7
platform/macos/gl_manager_macos_legacy.mm

@@ -32,6 +32,7 @@
 
 #if defined(MACOS_ENABLED) && defined(GLES3_ENABLED)
 
+#include <dlfcn.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -156,7 +157,7 @@ void GLManagerLegacy_MacOS::window_set_per_pixel_transparency_enabled(DisplaySer
 }
 
 Error GLManagerLegacy_MacOS::initialize() {
-	return OK;
+	return framework_loaded ? OK : ERR_CANT_CREATE;
 }
 
 void GLManagerLegacy_MacOS::set_use_vsync(bool p_use) {
@@ -186,12 +187,17 @@ NSOpenGLContext *GLManagerLegacy_MacOS::get_context(DisplayServer::WindowID p_wi
 }
 
 GLManagerLegacy_MacOS::GLManagerLegacy_MacOS() {
-	CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
-	CFBundleLoadExecutable(framework);
-
-	CGLEnable = (CGLEnablePtr)CFBundleGetFunctionPointerForName(framework, CFSTR("CGLEnable"));
-	CGLSetParameter = (CGLSetParameterPtr)CFBundleGetFunctionPointerForName(framework, CFSTR("CGLSetParameter"));
-	CGLGetCurrentContext = (CGLGetCurrentContextPtr)CFBundleGetFunctionPointerForName(framework, CFSTR("CGLGetCurrentContext"));
+	NSBundle *framework = [NSBundle bundleWithPath:@"/System/Library/Frameworks/OpenGL.framework"];
+	if (framework) {
+		void *library_handle = dlopen([framework.executablePath UTF8String], RTLD_NOW);
+		if (library_handle) {
+			CGLEnable = (CGLEnablePtr)dlsym(library_handle, "CGLEnable");
+			CGLSetParameter = (CGLSetParameterPtr)dlsym(library_handle, "CGLSetParameter");
+			CGLGetCurrentContext = (CGLGetCurrentContextPtr)dlsym(library_handle, "CGLGetCurrentContext");
+
+			framework_loaded = CGLEnable && CGLSetParameter && CGLGetCurrentContext;
+		}
+	}
 }
 
 GLManagerLegacy_MacOS::~GLManagerLegacy_MacOS() {