| 1 |
/**************************************************************************/ |
| 2 |
/* embedded_gl_manager.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 "embedded_gl_manager.h" |
| 32 |
|
| 33 |
#if defined(MACOS_ENABLED) && defined(GLES3_ENABLED) |
| 34 |
|
| 35 |
#import "drivers/gles3/storage/texture_storage.h" |
| 36 |
#import "platform_gl.h" |
| 37 |
|
| 38 |
#import <QuartzCore/QuartzCore.h> |
| 39 |
#include <dlfcn.h> |
| 40 |
|
| 41 |
GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wdeprecated-declarations") // OpenGL is deprecated in macOS 10.14. |
| 42 |
|
| 43 |
Error GLManagerEmbedded::create_context(GLWindow &p_win) { |
| 44 |
NSOpenGLPixelFormatAttribute attributes[] = { |
| 45 |
NSOpenGLPFADoubleBuffer, |
| 46 |
NSOpenGLPFAClosestPolicy, |
| 47 |
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core, |
| 48 |
NSOpenGLPFAColorSize, 32, |
| 49 |
NSOpenGLPFADepthSize, 24, |
| 50 |
NSOpenGLPFAStencilSize, 8, |
| 51 |
0 |
| 52 |
}; |
| 53 |
|
| 54 |
NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; |
| 55 |
ERR_FAIL_NULL_V(pixel_format, ERR_CANT_CREATE); |
| 56 |
|
| 57 |
p_win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context]; |
| 58 |
ERR_FAIL_NULL_V(p_win.context, ERR_CANT_CREATE); |
| 59 |
if (shared_context == nullptr) { |
| 60 |
shared_context = p_win.context; |
| 61 |
} |
| 62 |
|
| 63 |
[p_win.context makeCurrentContext]; |
| 64 |
|
| 65 |
return OK; |
| 66 |
} |
| 67 |
|
| 68 |
Error GLManagerEmbedded::window_create(DisplayServer::WindowID p_window_id, CALayer *p_layer, int p_width, int p_height) { |
| 69 |
GLWindow win; |
| 70 |
win.layer = p_layer; |
| 71 |
win.width = 0; |
| 72 |
win.height = 0; |
| 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 |
|
| 84 |
void GLManagerEmbedded::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) { |
| 85 |
GLWindowElement *el = windows.find(p_window_id); |
| 86 |
ERR_FAIL_NULL_MSG(el, "Window resize failed: window does not exist."); |
| 87 |
|
| 88 |
GLWindow &win = el->get(); |
| 89 |
|
| 90 |
if (win.width == (uint32_t)p_width && win.height == (uint32_t)p_height) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
win.width = (uint32_t)p_width; |
| 95 |
win.height = (uint32_t)p_height; |
| 96 |
|
| 97 |
win.destroy_framebuffers(); |
| 98 |
|
| 99 |
for (FrameBuffer &fb : win.framebuffers) { |
| 100 |
NSDictionary *surfaceProps = @{ |
| 101 |
(NSString *)kIOSurfaceWidth : @(p_width), |
| 102 |
(NSString *)kIOSurfaceHeight : @(p_height), |
| 103 |
(NSString *)kIOSurfaceBytesPerElement : @(4), |
| 104 |
(NSString *)kIOSurfacePixelFormat : @(kCVPixelFormatType_32BGRA), |
| 105 |
}; |
| 106 |
fb.surface = IOSurfaceCreate((__bridge CFDictionaryRef)surfaceProps); |
| 107 |
if (fb.surface == nullptr) { |
| 108 |
ERR_PRINT(vformat("Failed to create IOSurface: width=%d, height=%d", p_width, p_height)); |
| 109 |
win.destroy_framebuffers(); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
glGenTextures(1, &fb.tex); |
| 114 |
glBindTexture(GL_TEXTURE_RECTANGLE, fb.tex); |
| 115 |
|
| 116 |
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 117 |
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 118 |
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 119 |
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 120 |
|
| 121 |
CGLError err = CGLTexImageIOSurface2D(CGLGetCurrentContext(), |
| 122 |
GL_TEXTURE_RECTANGLE, |
| 123 |
GL_RGBA, |
| 124 |
p_width, |
| 125 |
p_height, |
| 126 |
GL_BGRA, |
| 127 |
GL_UNSIGNED_INT_8_8_8_8_REV, |
| 128 |
fb.surface, |
| 129 |
0); |
| 130 |
if (err != kCGLNoError) { |
| 131 |
String err_string = String(CGLErrorString(err)); |
| 132 |
ERR_PRINT(vformat("CGLTexImageIOSurface2D failed (%d): %s", err, err_string)); |
| 133 |
win.destroy_framebuffers(); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
glGenFramebuffers(1, &fb.fbo); |
| 138 |
glBindFramebuffer(GL_FRAMEBUFFER, fb.fbo); |
| 139 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, fb.tex, 0); |
| 140 |
|
| 141 |
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
| 142 |
ERR_PRINT("Unable to create framebuffer from IOSurface texture."); |
| 143 |
win.destroy_framebuffers(); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 148 |
glBindTexture(GL_TEXTURE_RECTANGLE, 0); |
| 149 |
} |
| 150 |
win.current_fb = 0; |
| 151 |
GLES3::TextureStorage::system_fbo = win.framebuffers[win.current_fb].fbo; |
| 152 |
win.is_valid = true; |
| 153 |
} |
| 154 |
|
| 155 |
void GLManagerEmbedded::GLWindow::destroy_framebuffers() { |
| 156 |
is_valid = false; |
| 157 |
GLES3::TextureStorage::system_fbo = 0; |
| 158 |
|
| 159 |
for (FrameBuffer &fb : framebuffers) { |
| 160 |
if (fb.fbo) { |
| 161 |
glDeleteFramebuffers(1, &fb.fbo); |
| 162 |
fb.fbo = 0; |
| 163 |
} |
| 164 |
|
| 165 |
if (fb.tex) { |
| 166 |
glDeleteTextures(1, &fb.tex); |
| 167 |
fb.tex = 0; |
| 168 |
} |
| 169 |
|
| 170 |
if (fb.surface) { |
| 171 |
IOSurfaceRef old_surface = fb.surface; |
| 172 |
fb.surface = nullptr; |
| 173 |
CFRelease(old_surface); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
Size2i GLManagerEmbedded::window_get_size(DisplayServer::WindowID p_window_id) const { |
| 179 |
const GLWindowElement *el = windows.find(p_window_id); |
| 180 |
if (el == nullptr) { |
| 181 |
return Size2i(); |
| 182 |
} |
| 183 |
|
| 184 |
const GLWindow &win = el->value(); |
| 185 |
return Size2i(win.width, win.height); |
| 186 |
} |
| 187 |
|
| 188 |
void GLManagerEmbedded::window_destroy(DisplayServer::WindowID p_window_id) { |
| 189 |
GLWindowElement *el = windows.find(p_window_id); |
| 190 |
if (el == nullptr) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
if (current_window == p_window_id) { |
| 195 |
current_window = DisplayServer::INVALID_WINDOW_ID; |
| 196 |
} |
| 197 |
|
| 198 |
windows.erase(el); |
| 199 |
} |
| 200 |
|
| 201 |
void GLManagerEmbedded::release_current() { |
| 202 |
if (current_window == DisplayServer::INVALID_WINDOW_ID) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
[NSOpenGLContext clearCurrentContext]; |
| 207 |
current_window = DisplayServer::INVALID_WINDOW_ID; |
| 208 |
} |
| 209 |
|
| 210 |
void GLManagerEmbedded::window_make_current(DisplayServer::WindowID p_window_id) { |
| 211 |
if (current_window == p_window_id) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
const GLWindowElement *el = windows.find(p_window_id); |
| 216 |
if (el == nullptr) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
const GLWindow &win = el->value(); |
| 221 |
[win.context makeCurrentContext]; |
| 222 |
|
| 223 |
current_window = p_window_id; |
| 224 |
} |
| 225 |
|
| 226 |
void GLManagerEmbedded::swap_buffers() { |
| 227 |
GLWindow &win = windows[current_window]; |
| 228 |
[win.context flushBuffer]; |
| 229 |
|
| 230 |
static bool last_valid = false; |
| 231 |
if (!win.is_valid) { |
| 232 |
if (last_valid) { |
| 233 |
ERR_PRINT("GLWindow framebuffers are invalid."); |
| 234 |
last_valid = false; |
| 235 |
} |
| 236 |
return; |
| 237 |
} |
| 238 |
last_valid = true; |
| 239 |
|
| 240 |
if (display_link_running) { |
| 241 |
dispatch_semaphore_wait(display_semaphore, DISPATCH_TIME_FOREVER); |
| 242 |
} |
| 243 |
|
| 244 |
[CATransaction begin]; |
| 245 |
[CATransaction setDisableActions:YES]; |
| 246 |
win.layer.contents = (__bridge id)win.framebuffers[win.current_fb].surface; |
| 247 |
[CATransaction commit]; |
| 248 |
win.current_fb = (win.current_fb + 1) % BUFFER_COUNT; |
| 249 |
GLES3::TextureStorage::system_fbo = win.framebuffers[win.current_fb].fbo; |
| 250 |
} |
| 251 |
|
| 252 |
Error GLManagerEmbedded::initialize() { |
| 253 |
return framework_loaded ? OK : ERR_CANT_CREATE; |
| 254 |
} |
| 255 |
|
| 256 |
void GLManagerEmbedded::create_display_link() { |
| 257 |
DEV_ASSERT(display_link == nullptr); |
| 258 |
|
| 259 |
CVReturn err = CVDisplayLinkCreateWithCGDisplay(CGMainDisplayID(), &display_link); |
| 260 |
ERR_FAIL_COND_MSG(err != kCVReturnSuccess, "Failed to create display link."); |
| 261 |
|
| 262 |
__block dispatch_semaphore_t local_semaphore = display_semaphore; |
| 263 |
|
| 264 |
CVDisplayLinkSetOutputHandler(display_link, ^CVReturn(CVDisplayLinkRef p_display_link, const CVTimeStamp *p_now, const CVTimeStamp *p_output_time, CVOptionFlags p_flags, CVOptionFlags *p_flags_out) { |
| 265 |
dispatch_semaphore_signal(local_semaphore); |
| 266 |
return kCVReturnSuccess; |
| 267 |
}); |
| 268 |
} |
| 269 |
|
| 270 |
void GLManagerEmbedded::release_display_link() { |
| 271 |
DEV_ASSERT(display_link != nullptr); |
| 272 |
if (CVDisplayLinkIsRunning(display_link)) { |
| 273 |
CVDisplayLinkStop(display_link); |
| 274 |
} |
| 275 |
CVDisplayLinkRelease(display_link); |
| 276 |
display_link = nullptr; |
| 277 |
} |
| 278 |
|
| 279 |
void GLManagerEmbedded::set_display_id(uint32_t p_display_id) { |
| 280 |
if (display_id == p_display_id) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
CVReturn err = CVDisplayLinkSetCurrentCGDisplay(display_link, static_cast<CGDirectDisplayID>(p_display_id)); |
| 285 |
ERR_FAIL_COND_MSG(err != kCVReturnSuccess, "Failed to set display ID for display link."); |
| 286 |
} |
| 287 |
|
| 288 |
void GLManagerEmbedded::set_vsync_enabled(bool p_enabled) { |
| 289 |
if (p_enabled == vsync_enabled) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
vsync_enabled = p_enabled; |
| 294 |
|
| 295 |
if (vsync_enabled) { |
| 296 |
if (!CVDisplayLinkIsRunning(display_link)) { |
| 297 |
CVReturn err = CVDisplayLinkStart(display_link); |
| 298 |
ERR_FAIL_COND_MSG(err != kCVReturnSuccess, "Failed to start display link."); |
| 299 |
display_link_running = true; |
| 300 |
} |
| 301 |
} else { |
| 302 |
if (CVDisplayLinkIsRunning(display_link)) { |
| 303 |
CVReturn err = CVDisplayLinkStop(display_link); |
| 304 |
ERR_FAIL_COND_MSG(err != kCVReturnSuccess, "Failed to stop display link."); |
| 305 |
display_link_running = false; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
GLManagerEmbedded::GLManagerEmbedded() { |
| 311 |
display_semaphore = dispatch_semaphore_create(BUFFER_COUNT); |
| 312 |
|
| 313 |
create_display_link(); |
| 314 |
|
| 315 |
NSBundle *framework = [NSBundle bundleWithIdentifier:@"com.apple.opengl"]; |
| 316 |
if ([framework load]) { |
| 317 |
void *library_handle = dlopen([framework.executablePath UTF8String], RTLD_NOW); |
| 318 |
if (library_handle) { |
| 319 |
CGLGetCurrentContext = (CGLGetCurrentContextPtr)dlsym(library_handle, "CGLGetCurrentContext"); |
| 320 |
CGLTexImageIOSurface2D = (CGLTexImageIOSurface2DPtr)dlsym(library_handle, "CGLTexImageIOSurface2D"); |
| 321 |
CGLErrorString = (CGLErrorStringPtr)dlsym(library_handle, "CGLErrorString"); |
| 322 |
framework_loaded = CGLGetCurrentContext && CGLTexImageIOSurface2D && CGLErrorString; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
GLManagerEmbedded::~GLManagerEmbedded() { |
| 328 |
release_display_link(); |
| 329 |
release_current(); |
| 330 |
} |
| 331 |
|
| 332 |
GODOT_CLANG_WARNING_POP |
| 333 |
|
| 334 |
#endif // MACOS_ENABLED && GLES3_ENABLED |