gl_manager_macos_legacy.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_macos_legacy.h"
  31. #ifdef MACOS_ENABLED
  32. #ifdef GLES3_ENABLED
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. Error GLManager_MacOS::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_MacOS::window_create(DisplayServer::WindowID p_window_id, id p_view, int p_width, int p_height) {
  57. GLWindow win;
  58. win.width = p_width;
  59. win.height = p_height;
  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 GLManager_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. win.width = p_width;
  74. win.height = p_height;
  75. GLint dim[2];
  76. dim[0] = p_width;
  77. dim[1] = p_height;
  78. CGLSetParameter((CGLContextObj)[win.context CGLContextObj], kCGLCPSurfaceBackingSize, &dim[0]);
  79. CGLEnable((CGLContextObj)[win.context CGLContextObj], kCGLCESurfaceBackingSize);
  80. if (OS::get_singleton()->is_hidpi_allowed()) {
  81. [win.window_view setWantsBestResolutionOpenGLSurface:YES];
  82. } else {
  83. [win.window_view setWantsBestResolutionOpenGLSurface:NO];
  84. }
  85. [win.context update];
  86. }
  87. int GLManager_MacOS::window_get_width(DisplayServer::WindowID p_window_id) {
  88. if (!windows.has(p_window_id)) {
  89. return 0;
  90. }
  91. GLWindow &win = windows[p_window_id];
  92. return win.width;
  93. }
  94. int GLManager_MacOS::window_get_height(DisplayServer::WindowID p_window_id) {
  95. if (!windows.has(p_window_id)) {
  96. return 0;
  97. }
  98. GLWindow &win = windows[p_window_id];
  99. return win.height;
  100. }
  101. void GLManager_MacOS::window_destroy(DisplayServer::WindowID p_window_id) {
  102. if (!windows.has(p_window_id)) {
  103. return;
  104. }
  105. if (current_window == p_window_id) {
  106. current_window = DisplayServer::INVALID_WINDOW_ID;
  107. }
  108. windows.erase(p_window_id);
  109. }
  110. void GLManager_MacOS::release_current() {
  111. if (current_window == DisplayServer::INVALID_WINDOW_ID) {
  112. return;
  113. }
  114. [NSOpenGLContext clearCurrentContext];
  115. }
  116. void GLManager_MacOS::window_make_current(DisplayServer::WindowID p_window_id) {
  117. if (current_window == p_window_id) {
  118. return;
  119. }
  120. if (!windows.has(p_window_id)) {
  121. return;
  122. }
  123. GLWindow &win = windows[p_window_id];
  124. [win.context makeCurrentContext];
  125. current_window = p_window_id;
  126. }
  127. void GLManager_MacOS::make_current() {
  128. if (current_window == DisplayServer::INVALID_WINDOW_ID) {
  129. return;
  130. }
  131. if (!windows.has(current_window)) {
  132. return;
  133. }
  134. GLWindow &win = windows[current_window];
  135. [win.context makeCurrentContext];
  136. }
  137. void GLManager_MacOS::swap_buffers() {
  138. GLWindow &win = windows[current_window];
  139. [win.context flushBuffer];
  140. }
  141. void GLManager_MacOS::window_update(DisplayServer::WindowID p_window_id) {
  142. if (!windows.has(p_window_id)) {
  143. return;
  144. }
  145. GLWindow &win = windows[p_window_id];
  146. [win.context update];
  147. }
  148. void GLManager_MacOS::window_set_per_pixel_transparency_enabled(DisplayServer::WindowID p_window_id, bool p_enabled) {
  149. if (!windows.has(p_window_id)) {
  150. return;
  151. }
  152. GLWindow &win = windows[p_window_id];
  153. if (p_enabled) {
  154. GLint opacity = 0;
  155. [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
  156. } else {
  157. GLint opacity = 1;
  158. [win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
  159. }
  160. [win.context update];
  161. }
  162. Error GLManager_MacOS::initialize() {
  163. return OK;
  164. }
  165. void GLManager_MacOS::set_use_vsync(bool p_use) {
  166. use_vsync = p_use;
  167. CGLContextObj ctx = CGLGetCurrentContext();
  168. if (ctx) {
  169. GLint swapInterval = p_use ? 1 : 0;
  170. CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval);
  171. use_vsync = p_use;
  172. }
  173. }
  174. bool GLManager_MacOS::is_using_vsync() const {
  175. return use_vsync;
  176. }
  177. GLManager_MacOS::GLManager_MacOS(ContextType p_context_type) {
  178. context_type = p_context_type;
  179. }
  180. GLManager_MacOS::~GLManager_MacOS() {
  181. release_current();
  182. }
  183. #endif // GLES3_ENABLED
  184. #endif // MACOS_ENABLED