gl_manager_macos_legacy.mm 7.0 KB

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