gl_manager_macos_legacy.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "gl_manager_macos_legacy.h"
  31. #if defined(MACOS_ENABLED) && defined(GLES3_ENABLED)
  32. #include <dlfcn.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #pragma clang diagnostic push
  36. #pragma clang diagnostic ignored "-Wdeprecated-declarations" // OpenGL is deprecated in macOS 10.14
  37. Error GLManagerLegacy_MacOS::create_context(GLWindow &win) {
  38. NSOpenGLPixelFormatAttribute attributes[] = {
  39. NSOpenGLPFADoubleBuffer,
  40. NSOpenGLPFAClosestPolicy,
  41. NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
  42. NSOpenGLPFAColorSize, 32,
  43. NSOpenGLPFADepthSize, 24,
  44. NSOpenGLPFAStencilSize, 8,
  45. 0
  46. };
  47. NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  48. ERR_FAIL_NULL_V(pixel_format, ERR_CANT_CREATE);
  49. win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context];
  50. ERR_FAIL_NULL_V(win.context, ERR_CANT_CREATE);
  51. if (shared_context == nullptr) {
  52. shared_context = win.context;
  53. }
  54. [win.context setView:win.window_view];
  55. [win.context makeCurrentContext];
  56. return OK;
  57. }
  58. Error GLManagerLegacy_MacOS::window_create(DisplayServer::WindowID p_window_id, id p_view, int p_width, int p_height) {
  59. GLWindow win;
  60. win.window_view = p_view;
  61. if (create_context(win) != OK) {
  62. return FAILED;
  63. }
  64. windows[p_window_id] = win;
  65. window_make_current(p_window_id);
  66. return OK;
  67. }
  68. void GLManagerLegacy_MacOS::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  69. if (!windows.has(p_window_id)) {
  70. return;
  71. }
  72. GLWindow &win = windows[p_window_id];
  73. GLint dim[2];
  74. dim[0] = p_width;
  75. dim[1] = p_height;
  76. CGLSetParameter((CGLContextObj)[win.context CGLContextObj], kCGLCPSurfaceBackingSize, &dim[0]);
  77. CGLEnable((CGLContextObj)[win.context CGLContextObj], kCGLCESurfaceBackingSize);
  78. if (OS::get_singleton()->is_hidpi_allowed()) {
  79. [win.window_view setWantsBestResolutionOpenGLSurface:YES];
  80. } else {
  81. [win.window_view setWantsBestResolutionOpenGLSurface:NO];
  82. }
  83. [win.context update];
  84. }
  85. void GLManagerLegacy_MacOS::window_destroy(DisplayServer::WindowID p_window_id) {
  86. if (!windows.has(p_window_id)) {
  87. return;
  88. }
  89. if (current_window == p_window_id) {
  90. current_window = DisplayServer::INVALID_WINDOW_ID;
  91. }
  92. windows.erase(p_window_id);
  93. }
  94. void GLManagerLegacy_MacOS::release_current() {
  95. if (current_window == DisplayServer::INVALID_WINDOW_ID) {
  96. return;
  97. }
  98. [NSOpenGLContext clearCurrentContext];
  99. current_window = DisplayServer::INVALID_WINDOW_ID;
  100. }
  101. void GLManagerLegacy_MacOS::window_make_current(DisplayServer::WindowID p_window_id) {
  102. if (current_window == p_window_id) {
  103. return;
  104. }
  105. if (!windows.has(p_window_id)) {
  106. return;
  107. }
  108. GLWindow &win = windows[p_window_id];
  109. [win.context makeCurrentContext];
  110. current_window = p_window_id;
  111. }
  112. void GLManagerLegacy_MacOS::swap_buffers() {
  113. GLWindow &win = windows[current_window];
  114. [win.context flushBuffer];
  115. }
  116. void GLManagerLegacy_MacOS::window_set_per_pixel_transparency_enabled(DisplayServer::WindowID p_window_id, bool p_enabled) {
  117. if (!windows.has(p_window_id)) {
  118. return;
  119. }
  120. GLWindow &win = windows[p_window_id];
  121. if (p_enabled) {
  122. GLint opacity = 0;
  123. [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
  124. } else {
  125. GLint opacity = 1;
  126. [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
  127. }
  128. [win.context update];
  129. }
  130. Error GLManagerLegacy_MacOS::initialize() {
  131. return framework_loaded ? OK : ERR_CANT_CREATE;
  132. }
  133. void GLManagerLegacy_MacOS::set_use_vsync(bool p_use) {
  134. use_vsync = p_use;
  135. CGLContextObj ctx = CGLGetCurrentContext();
  136. if (ctx) {
  137. GLint swapInterval = p_use ? 1 : 0;
  138. if (CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval) != kCGLNoError) {
  139. WARN_PRINT("Could not set V-Sync mode.");
  140. }
  141. use_vsync = p_use;
  142. }
  143. }
  144. bool GLManagerLegacy_MacOS::is_using_vsync() const {
  145. return use_vsync;
  146. }
  147. NSOpenGLContext *GLManagerLegacy_MacOS::get_context(DisplayServer::WindowID p_window_id) {
  148. if (!windows.has(p_window_id)) {
  149. return nullptr;
  150. }
  151. GLWindow &win = windows[p_window_id];
  152. return win.context;
  153. }
  154. GLManagerLegacy_MacOS::GLManagerLegacy_MacOS() {
  155. NSBundle *framework = [NSBundle bundleWithPath:@"/System/Library/Frameworks/OpenGL.framework"];
  156. if (framework) {
  157. void *library_handle = dlopen([framework.executablePath UTF8String], RTLD_NOW);
  158. if (library_handle) {
  159. CGLEnable = (CGLEnablePtr)dlsym(library_handle, "CGLEnable");
  160. CGLSetParameter = (CGLSetParameterPtr)dlsym(library_handle, "CGLSetParameter");
  161. CGLGetCurrentContext = (CGLGetCurrentContextPtr)dlsym(library_handle, "CGLGetCurrentContext");
  162. framework_loaded = CGLEnable && CGLSetParameter && CGLGetCurrentContext;
  163. }
  164. }
  165. }
  166. GLManagerLegacy_MacOS::~GLManagerLegacy_MacOS() {
  167. release_current();
  168. }
  169. #pragma clang diagnostic pop
  170. #endif // MACOS_ENABLED && GLES3_ENABLED