Debian Codegraph @

platform/macos/gl_manager_macos_legacy.mm

raw
1 /**************************************************************************/
2 /* gl_manager_macos_legacy.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "gl_manager_macos_legacy.h"
32
33 #if defined(MACOS_ENABLED) && defined(GLES3_ENABLED)
34
35 #include <dlfcn.h>
36 #include <cstdio>
37 #include <cstdlib>
38
39 GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wdeprecated-declarations") // OpenGL is deprecated in macOS 10.14.
40
41 Error GLManagerLegacy_MacOS::create_context(GLWindow &win) {
42 NSOpenGLPixelFormatAttribute attributes[] = {
43 NSOpenGLPFADoubleBuffer,
44 NSOpenGLPFAClosestPolicy,
45 NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
46 NSOpenGLPFAColorSize, 32,
47 NSOpenGLPFADepthSize, 24,
48 NSOpenGLPFAStencilSize, 8,
49 0
50 };
51
52 NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
53 ERR_FAIL_NULL_V(pixel_format, ERR_CANT_CREATE);
54
55 win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context];
56 ERR_FAIL_NULL_V(win.context, ERR_CANT_CREATE);
57 if (shared_context == nullptr) {
58 shared_context = win.context;
59 }
60
61 [win.context setView:win.window_view];
62 [win.context makeCurrentContext];
63
64 return OK;
65 }
66
67 Error GLManagerLegacy_MacOS::window_create(DisplayServer::WindowID p_window_id, id p_view, int p_width, int p_height) {
68 GLWindow win;
69 win.window_view = p_view;
70
71 if (create_context(win) != OK) {
72 return FAILED;
73 }
74
75 windows[p_window_id] = win;
76 window_make_current(p_window_id);
77
78 return OK;
79 }
80
81 void GLManagerLegacy_MacOS::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
82 if (!windows.has(p_window_id)) {
83 return;
84 }
85
86 GLWindow &win = windows[p_window_id];
87
88 GLint dim[2];
89 dim[0] = p_width;
90 dim[1] = p_height;
91 CGLSetParameter((CGLContextObj)[win.context CGLContextObj], kCGLCPSurfaceBackingSize, &dim[0]);
92 CGLEnable((CGLContextObj)[win.context CGLContextObj], kCGLCESurfaceBackingSize);
93 if (OS::get_singleton()->is_hidpi_allowed()) {
94 [win.window_view setWantsBestResolutionOpenGLSurface:YES];
95 } else {
96 [win.window_view setWantsBestResolutionOpenGLSurface:NO];
97 }
98
99 [win.context update];
100 }
101
102 void GLManagerLegacy_MacOS::window_destroy(DisplayServer::WindowID p_window_id) {
103 if (!windows.has(p_window_id)) {
104 return;
105 }
106
107 if (current_window == p_window_id) {
108 current_window = DisplayServer::INVALID_WINDOW_ID;
109 }
110
111 windows.erase(p_window_id);
112 }
113
114 void GLManagerLegacy_MacOS::release_current() {
115 if (current_window == DisplayServer::INVALID_WINDOW_ID) {
116 return;
117 }
118
119 [NSOpenGLContext clearCurrentContext];
120 current_window = DisplayServer::INVALID_WINDOW_ID;
121 }
122
123 void GLManagerLegacy_MacOS::window_make_current(DisplayServer::WindowID p_window_id) {
124 if (current_window == p_window_id) {
125 return;
126 }
127 if (!windows.has(p_window_id)) {
128 return;
129 }
130
131 GLWindow &win = windows[p_window_id];
132 [win.context makeCurrentContext];
133
134 current_window = p_window_id;
135 }
136
137 void GLManagerLegacy_MacOS::swap_buffers() {
138 GLWindow &win = windows[current_window];
139 [win.context flushBuffer];
140 }
141
142 void GLManagerLegacy_MacOS::window_set_per_pixel_transparency_enabled(DisplayServer::WindowID p_window_id, bool p_enabled) {
143 if (!windows.has(p_window_id)) {
144 return;
145 }
146
147 GLWindow &win = windows[p_window_id];
148 if (p_enabled) {
149 GLint opacity = 0;
150 [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
151 } else {
152 GLint opacity = 1;
153 [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
154 }
155 [win.context update];
156 }
157
158 Error GLManagerLegacy_MacOS::initialize() {
159 return framework_loaded ? OK : ERR_CANT_CREATE;
160 }
161
162 void GLManagerLegacy_MacOS::set_use_vsync(bool p_use) {
163 use_vsync = p_use;
164
165 CGLContextObj ctx = CGLGetCurrentContext();
166 if (ctx) {
167 GLint swapInterval = p_use ? 1 : 0;
168 if (CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval) != kCGLNoError) {
169 WARN_PRINT("Could not set V-Sync mode.");
170 }
171 use_vsync = p_use;
172 }
173 }
174
175 bool GLManagerLegacy_MacOS::is_using_vsync() const {
176 return use_vsync;
177 }
178
179 NSOpenGLContext *GLManagerLegacy_MacOS::get_context(DisplayServer::WindowID p_window_id) {
180 if (!windows.has(p_window_id)) {
181 return nullptr;
182 }
183
184 GLWindow &win = windows[p_window_id];
185 return win.context;
186 }
187
188 GLManagerLegacy_MacOS::GLManagerLegacy_MacOS() {
189 NSBundle *framework = [NSBundle bundleWithPath:@"/System/Library/Frameworks/OpenGL.framework"];
190 if (framework) {
191 void *library_handle = dlopen([framework.executablePath UTF8String], RTLD_NOW);
192 if (library_handle) {
193 CGLEnable = (CGLEnablePtr)dlsym(library_handle, "CGLEnable");
194 CGLSetParameter = (CGLSetParameterPtr)dlsym(library_handle, "CGLSetParameter");
195 CGLGetCurrentContext = (CGLGetCurrentContextPtr)dlsym(library_handle, "CGLGetCurrentContext");
196
197 framework_loaded = CGLEnable && CGLSetParameter && CGLGetCurrentContext;
198 }
199 }
200 }
201
202 GLManagerLegacy_MacOS::~GLManagerLegacy_MacOS() {
203 release_current();
204 }
205
206 GODOT_CLANG_WARNING_POP
207
208 #endif // MACOS_ENABLED && GLES3_ENABLED