gl_manager_macos_legacy.mm 6.7 KB

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