gl_manager_macos_legacy.mm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 GLManager_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_COND_V(pixel_format == nil, ERR_CANT_CREATE);
  48. win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context];
  49. ERR_FAIL_COND_V(win.context == nil, 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 GLManager_MacOS::window_create(DisplayServer::WindowID p_window_id, id p_view, int p_width, int p_height) {
  58. GLWindow win;
  59. win.width = p_width;
  60. win.height = p_height;
  61. win.window_view = p_view;
  62. if (create_context(win) != OK) {
  63. return FAILED;
  64. }
  65. windows[p_window_id] = win;
  66. window_make_current(p_window_id);
  67. return OK;
  68. }
  69. void GLManager_MacOS::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  70. if (!windows.has(p_window_id)) {
  71. return;
  72. }
  73. GLWindow &win = windows[p_window_id];
  74. win.width = p_width;
  75. win.height = p_height;
  76. GLint dim[2];
  77. dim[0] = p_width;
  78. dim[1] = p_height;
  79. CGLSetParameter((CGLContextObj)[win.context CGLContextObj], kCGLCPSurfaceBackingSize, &dim[0]);
  80. CGLEnable((CGLContextObj)[win.context CGLContextObj], kCGLCESurfaceBackingSize);
  81. if (OS::get_singleton()->is_hidpi_allowed()) {
  82. [win.window_view setWantsBestResolutionOpenGLSurface:YES];
  83. } else {
  84. [win.window_view setWantsBestResolutionOpenGLSurface:NO];
  85. }
  86. [win.context update];
  87. }
  88. int GLManager_MacOS::window_get_width(DisplayServer::WindowID p_window_id) {
  89. if (!windows.has(p_window_id)) {
  90. return 0;
  91. }
  92. GLWindow &win = windows[p_window_id];
  93. return win.width;
  94. }
  95. int GLManager_MacOS::window_get_height(DisplayServer::WindowID p_window_id) {
  96. if (!windows.has(p_window_id)) {
  97. return 0;
  98. }
  99. GLWindow &win = windows[p_window_id];
  100. return win.height;
  101. }
  102. void GLManager_MacOS::window_destroy(DisplayServer::WindowID p_window_id) {
  103. if (!windows.has(p_window_id)) {
  104. return;
  105. }
  106. if (current_window == p_window_id) {
  107. current_window = DisplayServer::INVALID_WINDOW_ID;
  108. }
  109. windows.erase(p_window_id);
  110. }
  111. void GLManager_MacOS::release_current() {
  112. if (current_window == DisplayServer::INVALID_WINDOW_ID) {
  113. return;
  114. }
  115. [NSOpenGLContext clearCurrentContext];
  116. }
  117. void GLManager_MacOS::window_make_current(DisplayServer::WindowID p_window_id) {
  118. if (current_window == p_window_id) {
  119. return;
  120. }
  121. if (!windows.has(p_window_id)) {
  122. return;
  123. }
  124. GLWindow &win = windows[p_window_id];
  125. [win.context makeCurrentContext];
  126. current_window = p_window_id;
  127. }
  128. void GLManager_MacOS::make_current() {
  129. if (current_window == DisplayServer::INVALID_WINDOW_ID) {
  130. return;
  131. }
  132. if (!windows.has(current_window)) {
  133. return;
  134. }
  135. GLWindow &win = windows[current_window];
  136. [win.context makeCurrentContext];
  137. }
  138. void GLManager_MacOS::swap_buffers() {
  139. GLWindow &win = windows[current_window];
  140. [win.context flushBuffer];
  141. }
  142. void GLManager_MacOS::window_update(DisplayServer::WindowID p_window_id) {
  143. if (!windows.has(p_window_id)) {
  144. return;
  145. }
  146. GLWindow &win = windows[p_window_id];
  147. [win.context update];
  148. }
  149. void GLManager_MacOS::window_set_per_pixel_transparency_enabled(DisplayServer::WindowID p_window_id, bool p_enabled) {
  150. if (!windows.has(p_window_id)) {
  151. return;
  152. }
  153. GLWindow &win = windows[p_window_id];
  154. if (p_enabled) {
  155. GLint opacity = 0;
  156. [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
  157. } else {
  158. GLint opacity = 1;
  159. [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
  160. }
  161. [win.context update];
  162. }
  163. Error GLManager_MacOS::initialize() {
  164. return OK;
  165. }
  166. void GLManager_MacOS::set_use_vsync(bool p_use) {
  167. use_vsync = p_use;
  168. CGLContextObj ctx = CGLGetCurrentContext();
  169. if (ctx) {
  170. GLint swapInterval = p_use ? 1 : 0;
  171. CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval);
  172. use_vsync = p_use;
  173. }
  174. }
  175. bool GLManager_MacOS::is_using_vsync() const {
  176. return use_vsync;
  177. }
  178. NSOpenGLContext *GLManager_MacOS::get_context(DisplayServer::WindowID p_window_id) {
  179. if (!windows.has(p_window_id)) {
  180. return nullptr;
  181. }
  182. GLWindow &win = windows[p_window_id];
  183. return win.context;
  184. }
  185. GLManager_MacOS::GLManager_MacOS(ContextType p_context_type) {
  186. context_type = p_context_type;
  187. }
  188. GLManager_MacOS::~GLManager_MacOS() {
  189. release_current();
  190. }
  191. #pragma clang diagnostic pop
  192. #endif // MACOS_ENABLED && GLES3_ENABLED