gl_manager_osx.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*************************************************************************/
  2. /* gl_manager_osx.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_osx.h"
  31. #ifdef OSX_ENABLED
  32. #ifdef GLES3_ENABLED
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. Error GLManager_OSX::_create_context(GLWindow &win) {
  36. NSOpenGLPixelFormatAttribute attributes[] = {
  37. NSOpenGLPFADoubleBuffer,
  38. NSOpenGLPFAClosestPolicy,
  39. NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
  40. NSOpenGLPFAColorSize, 32,
  41. NSOpenGLPFADepthSize, 24,
  42. NSOpenGLPFAStencilSize, 8,
  43. 0
  44. };
  45. NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  46. ERR_FAIL_COND_V(pixel_format == nil, ERR_CANT_CREATE);
  47. win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:_shared_context];
  48. ERR_FAIL_COND_V(win.context == nil, ERR_CANT_CREATE);
  49. if (_shared_context == nullptr) {
  50. _shared_context = win.context;
  51. }
  52. [win.context setView:win.window_view];
  53. [win.context makeCurrentContext];
  54. return OK;
  55. }
  56. Error GLManager_OSX::window_create(DisplayServer::WindowID p_window_id, id p_view, int p_width, int p_height) {
  57. if (p_window_id >= (int)_windows.size()) {
  58. _windows.resize(p_window_id + 1);
  59. }
  60. GLWindow &win = _windows[p_window_id];
  61. win.in_use = true;
  62. win.window_id = p_window_id;
  63. win.width = p_width;
  64. win.height = p_height;
  65. win.window_view = p_view;
  66. if (_create_context(win) != OK) {
  67. _windows.remove(_windows.size() - 1);
  68. return FAILED;
  69. }
  70. window_make_current(_windows.size() - 1);
  71. return OK;
  72. }
  73. void GLManager_OSX::_internal_set_current_window(GLWindow *p_win) {
  74. _current_window = p_win;
  75. }
  76. void GLManager_OSX::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  77. if (p_window_id == -1) {
  78. return;
  79. }
  80. GLWindow &win = _windows[p_window_id];
  81. if (!win.in_use) {
  82. return;
  83. }
  84. win.width = p_width;
  85. win.height = p_height;
  86. GLint dim[2];
  87. dim[0] = p_width;
  88. dim[1] = p_height;
  89. CGLSetParameter((CGLContextObj)[win.context CGLContextObj], kCGLCPSurfaceBackingSize, &dim[0]);
  90. CGLEnable((CGLContextObj)[win.context CGLContextObj], kCGLCESurfaceBackingSize);
  91. if (OS::get_singleton()->is_hidpi_allowed()) {
  92. [win.window_view setWantsBestResolutionOpenGLSurface:YES];
  93. } else {
  94. [win.window_view setWantsBestResolutionOpenGLSurface:NO];
  95. }
  96. [win.context update];
  97. }
  98. int GLManager_OSX::window_get_width(DisplayServer::WindowID p_window_id) {
  99. return get_window(p_window_id).width;
  100. }
  101. int GLManager_OSX::window_get_height(DisplayServer::WindowID p_window_id) {
  102. return get_window(p_window_id).height;
  103. }
  104. void GLManager_OSX::window_destroy(DisplayServer::WindowID p_window_id) {
  105. GLWindow &win = get_window(p_window_id);
  106. win.in_use = false;
  107. if (_current_window == &win) {
  108. _current_window = nullptr;
  109. }
  110. }
  111. void GLManager_OSX::release_current() {
  112. if (!_current_window) {
  113. return;
  114. }
  115. [NSOpenGLContext clearCurrentContext];
  116. }
  117. void GLManager_OSX::window_make_current(DisplayServer::WindowID p_window_id) {
  118. if (p_window_id == -1) {
  119. return;
  120. }
  121. GLWindow &win = _windows[p_window_id];
  122. if (!win.in_use) {
  123. return;
  124. }
  125. if (&win == _current_window) {
  126. return;
  127. }
  128. [win.context makeCurrentContext];
  129. _internal_set_current_window(&win);
  130. }
  131. void GLManager_OSX::make_current() {
  132. if (!_current_window) {
  133. return;
  134. }
  135. if (!_current_window->in_use) {
  136. WARN_PRINT("current window not in use!");
  137. return;
  138. }
  139. [_current_window->context makeCurrentContext];
  140. }
  141. void GLManager_OSX::swap_buffers() {
  142. // NO NEED TO CALL SWAP BUFFERS for each window...
  143. // see https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glXSwapBuffers.xml
  144. if (!_current_window) {
  145. return;
  146. }
  147. if (!_current_window->in_use) {
  148. WARN_PRINT("current window not in use!");
  149. return;
  150. }
  151. [_current_window->context flushBuffer];
  152. }
  153. void GLManager_OSX::window_update(DisplayServer::WindowID p_window_id) {
  154. if (p_window_id == -1) {
  155. return;
  156. }
  157. GLWindow &win = _windows[p_window_id];
  158. if (!win.in_use) {
  159. return;
  160. }
  161. if (&win == _current_window) {
  162. return;
  163. }
  164. [win.context update];
  165. }
  166. Error GLManager_OSX::initialize() {
  167. return OK;
  168. }
  169. void GLManager_OSX::set_use_vsync(bool p_use) {
  170. use_vsync = p_use;
  171. CGLContextObj ctx = CGLGetCurrentContext();
  172. if (ctx) {
  173. GLint swapInterval = p_use ? 1 : 0;
  174. CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval);
  175. use_vsync = p_use;
  176. }
  177. }
  178. bool GLManager_OSX::is_using_vsync() const {
  179. return use_vsync;
  180. }
  181. GLManager_OSX::GLManager_OSX(ContextType p_context_type) {
  182. context_type = p_context_type;
  183. use_vsync = false;
  184. _current_window = nullptr;
  185. }
  186. GLManager_OSX::~GLManager_OSX() {
  187. release_current();
  188. }
  189. #endif // GLES3_ENABLED
  190. #endif // OSX