Skip to content
0.2.0-dev Download ZIP
211 lines ·6.9 KB 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 "core/os/os.h"
36
37#include <dlfcn.h>
38
39#include <cstdio>
40#include <cstdlib>
41
42GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wdeprecated-declarations") // OpenGL is deprecated in macOS 10.14.
43
44Error GLManagerLegacy_MacOS::create_context(GLWindow &win) {
45 NSOpenGLPixelFormatAttribute attributes[] = {
46 NSOpenGLPFADoubleBuffer,
47 NSOpenGLPFAClosestPolicy,
48 NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
49 NSOpenGLPFAColorSize, 32,
50 NSOpenGLPFADepthSize, 24,
51 NSOpenGLPFAStencilSize, 8,
52 0
53 };
54
55 NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
56 ERR_FAIL_NULL_V(pixel_format, ERR_CANT_CREATE);
57
58 win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context];
59 ERR_FAIL_NULL_V(win.context, ERR_CANT_CREATE);
60 if (shared_context == nullptr) {
61 shared_context = win.context;
62 }
63
64 [win.context setView:win.window_view];
65 [win.context makeCurrentContext];
66
67 return OK;
68}
69
70Error GLManagerLegacy_MacOS::window_create(DisplayServerEnums::WindowID p_window_id, id p_view, int p_width, int p_height) {
71 GLWindow win;
72 win.window_view = p_view;
73
74 if (create_context(win) != OK) {
75 return FAILED;
76 }
77
78 windows[p_window_id] = win;
79 window_make_current(p_window_id);
80
81 return OK;
82}
83
84void GLManagerLegacy_MacOS::window_resize(DisplayServerEnums::WindowID p_window_id, int p_width, int p_height) {
85 if (!windows.has(p_window_id)) {
86 return;
87 }
88
89 GLWindow &win = windows[p_window_id];
90
91 GLint dim[2];
92 dim[0] = p_width;
93 dim[1] = p_height;
94 CGLSetParameter((CGLContextObj)[win.context CGLContextObj], kCGLCPSurfaceBackingSize, &dim[0]);
95 CGLEnable((CGLContextObj)[win.context CGLContextObj], kCGLCESurfaceBackingSize);
96 if (OS::get_singleton()->is_hidpi_allowed()) {
97 [win.window_view setWantsBestResolutionOpenGLSurface:YES];
98 } else {
99 [win.window_view setWantsBestResolutionOpenGLSurface:NO];
100 }
101
102 [win.context update];
103}
104
105void GLManagerLegacy_MacOS::window_destroy(DisplayServerEnums::WindowID p_window_id) {
106 if (!windows.has(p_window_id)) {
107 return;
108 }
109
110 if (current_window == p_window_id) {
111 current_window = DisplayServerEnums::INVALID_WINDOW_ID;
112 }
113
114 windows.erase(p_window_id);
115}
116
117void GLManagerLegacy_MacOS::release_current() {
118 if (current_window == DisplayServerEnums::INVALID_WINDOW_ID) {
119 return;
120 }
121
122 [NSOpenGLContext clearCurrentContext];
123 current_window = DisplayServerEnums::INVALID_WINDOW_ID;
124}
125
126void GLManagerLegacy_MacOS::window_make_current(DisplayServerEnums::WindowID p_window_id) {
127 if (current_window == p_window_id) {
128 return;
129 }
130 if (!windows.has(p_window_id)) {
131 return;
132 }
133
134 GLWindow &win = windows[p_window_id];
135 [win.context makeCurrentContext];
136
137 current_window = p_window_id;
138}
139
140void GLManagerLegacy_MacOS::swap_buffers() {
141 GLWindow &win = windows[current_window];
142 [win.context flushBuffer];
143}
144
145void GLManagerLegacy_MacOS::window_set_per_pixel_transparency_enabled(DisplayServerEnums::WindowID p_window_id, bool p_enabled) {
146 if (!windows.has(p_window_id)) {
147 return;
148 }
149
150 GLWindow &win = windows[p_window_id];
151 if (p_enabled) {
152 GLint opacity = 0;
153 [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
154 } else {
155 GLint opacity = 1;
156 [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
157 }
158 [win.context update];
159}
160
161Error GLManagerLegacy_MacOS::initialize() {
162 return framework_loaded ? OK : ERR_CANT_CREATE;
163}
164
165void GLManagerLegacy_MacOS::set_use_vsync(bool p_use) {
166 use_vsync = p_use;
167
168 CGLContextObj ctx = CGLGetCurrentContext();
169 if (ctx) {
170 GLint swapInterval = p_use ? 1 : 0;
171 if (CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval) != kCGLNoError) {
172 WARN_PRINT("Could not set V-Sync mode.");
173 }
174 use_vsync = p_use;
175 }
176}
177
178bool GLManagerLegacy_MacOS::is_using_vsync() const {
179 return use_vsync;
180}
181
182NSOpenGLContext *GLManagerLegacy_MacOS::get_context(DisplayServerEnums::WindowID p_window_id) {
183 if (!windows.has(p_window_id)) {
184 return nullptr;
185 }
186
187 GLWindow &win = windows[p_window_id];
188 return win.context;
189}
190
191GLManagerLegacy_MacOS::GLManagerLegacy_MacOS() {
192 NSBundle *framework = [NSBundle bundleWithPath:@"/System/Library/Frameworks/OpenGL.framework"];
193 if (framework) {
194 void *library_handle = dlopen([framework.executablePath UTF8String], RTLD_NOW);
195 if (library_handle) {
196 CGLEnable = (CGLEnablePtr)dlsym(library_handle, "CGLEnable");
197 CGLSetParameter = (CGLSetParameterPtr)dlsym(library_handle, "CGLSetParameter");
198 CGLGetCurrentContext = (CGLGetCurrentContextPtr)dlsym(library_handle, "CGLGetCurrentContext");
199
200 framework_loaded = CGLEnable && CGLSetParameter && CGLGetCurrentContext;
201 }
202 }
203}
204
205GLManagerLegacy_MacOS::~GLManagerLegacy_MacOS() {
206 release_current();
207}
208
209GODOT_CLANG_WARNING_POP
210
211#endif // MACOS_ENABLED && GLES3_ENABLED