nsgl_context.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //========================================================================
  2. // GLFW 3.4 macOS - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2009-2019 Camilla Löwy <[email protected]>
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would
  17. // be appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not
  20. // be misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. //
  25. //========================================================================
  26. // It is fine to use C99 in this file because it will not be built with VS
  27. //========================================================================
  28. #include "internal.h"
  29. #if defined(_GLFW_COCOA)
  30. #include <unistd.h>
  31. #include <math.h>
  32. static void makeContextCurrentNSGL(_GLFWwindow* window)
  33. {
  34. @autoreleasepool {
  35. if (window)
  36. [window->context.nsgl.object makeCurrentContext];
  37. else
  38. [NSOpenGLContext clearCurrentContext];
  39. _glfwPlatformSetTls(&_glfw.contextSlot, window);
  40. } // autoreleasepool
  41. }
  42. static void swapBuffersNSGL(_GLFWwindow* window)
  43. {
  44. @autoreleasepool {
  45. // HACK: Simulate vsync with usleep as NSGL swap interval does not apply to
  46. // windows with a non-visible occlusion state
  47. if (window->ns.occluded)
  48. {
  49. int interval = 0;
  50. [window->context.nsgl.object getValues:&interval
  51. forParameter:NSOpenGLContextParameterSwapInterval];
  52. if (interval > 0)
  53. {
  54. const double framerate = 60.0;
  55. const uint64_t frequency = _glfwPlatformGetTimerFrequency();
  56. const uint64_t value = _glfwPlatformGetTimerValue();
  57. const double elapsed = value / (double) frequency;
  58. const double period = 1.0 / framerate;
  59. const double delay = period - fmod(elapsed, period);
  60. usleep(floorl(delay * 1e6));
  61. }
  62. }
  63. [window->context.nsgl.object flushBuffer];
  64. } // autoreleasepool
  65. }
  66. static void swapIntervalNSGL(int interval)
  67. {
  68. @autoreleasepool {
  69. _GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot);
  70. assert(window != NULL);
  71. [window->context.nsgl.object setValues:&interval
  72. forParameter:NSOpenGLContextParameterSwapInterval];
  73. } // autoreleasepool
  74. }
  75. static int extensionSupportedNSGL(const char* extension)
  76. {
  77. // There are no NSGL extensions
  78. return GLFW_FALSE;
  79. }
  80. static GLFWglproc getProcAddressNSGL(const char* procname)
  81. {
  82. CFStringRef symbolName = CFStringCreateWithCString(kCFAllocatorDefault,
  83. procname,
  84. kCFStringEncodingASCII);
  85. GLFWglproc symbol = CFBundleGetFunctionPointerForName(_glfw.nsgl.framework,
  86. symbolName);
  87. CFRelease(symbolName);
  88. return symbol;
  89. }
  90. static void destroyContextNSGL(_GLFWwindow* window)
  91. {
  92. @autoreleasepool {
  93. [window->context.nsgl.pixelFormat release];
  94. window->context.nsgl.pixelFormat = nil;
  95. [window->context.nsgl.object release];
  96. window->context.nsgl.object = nil;
  97. } // autoreleasepool
  98. }
  99. //////////////////////////////////////////////////////////////////////////
  100. ////// GLFW internal API //////
  101. //////////////////////////////////////////////////////////////////////////
  102. // Initialize OpenGL support
  103. //
  104. GLFWbool _glfwInitNSGL(void)
  105. {
  106. if (_glfw.nsgl.framework)
  107. return GLFW_TRUE;
  108. _glfw.nsgl.framework =
  109. CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
  110. if (_glfw.nsgl.framework == NULL)
  111. {
  112. _glfwInputError(GLFW_API_UNAVAILABLE,
  113. "NSGL: Failed to locate OpenGL framework");
  114. return GLFW_FALSE;
  115. }
  116. return GLFW_TRUE;
  117. }
  118. // Terminate OpenGL support
  119. //
  120. void _glfwTerminateNSGL(void)
  121. {
  122. }
  123. // Create the OpenGL context
  124. //
  125. GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window,
  126. const _GLFWctxconfig* ctxconfig,
  127. const _GLFWfbconfig* fbconfig)
  128. {
  129. if (ctxconfig->client == GLFW_OPENGL_ES_API)
  130. {
  131. _glfwInputError(GLFW_API_UNAVAILABLE,
  132. "NSGL: OpenGL ES is not available on macOS");
  133. return GLFW_FALSE;
  134. }
  135. if (ctxconfig->major > 2)
  136. {
  137. if (ctxconfig->major == 3 && ctxconfig->minor < 2)
  138. {
  139. _glfwInputError(GLFW_VERSION_UNAVAILABLE,
  140. "NSGL: The targeted version of macOS does not support OpenGL 3.0 or 3.1 but may support 3.2 and above");
  141. return GLFW_FALSE;
  142. }
  143. }
  144. // Context robustness modes (GL_KHR_robustness) are not yet supported by
  145. // macOS but are not a hard constraint, so ignore and continue
  146. // Context release behaviors (GL_KHR_context_flush_control) are not yet
  147. // supported by macOS but are not a hard constraint, so ignore and continue
  148. // Debug contexts (GL_KHR_debug) are not yet supported by macOS but are not
  149. // a hard constraint, so ignore and continue
  150. // No-error contexts (GL_KHR_no_error) are not yet supported by macOS but
  151. // are not a hard constraint, so ignore and continue
  152. #define ADD_ATTRIB(a) \
  153. { \
  154. assert((size_t) index < sizeof(attribs) / sizeof(attribs[0])); \
  155. attribs[index++] = a; \
  156. }
  157. #define SET_ATTRIB(a, v) { ADD_ATTRIB(a); ADD_ATTRIB(v); }
  158. NSOpenGLPixelFormatAttribute attribs[40];
  159. int index = 0;
  160. ADD_ATTRIB(NSOpenGLPFAAccelerated);
  161. ADD_ATTRIB(NSOpenGLPFAClosestPolicy);
  162. if (ctxconfig->nsgl.offline)
  163. {
  164. ADD_ATTRIB(NSOpenGLPFAAllowOfflineRenderers);
  165. // NOTE: This replaces the NSSupportsAutomaticGraphicsSwitching key in
  166. // Info.plist for unbundled applications
  167. // HACK: This assumes that NSOpenGLPixelFormat will remain
  168. // a straightforward wrapper of its CGL counterpart
  169. ADD_ATTRIB(kCGLPFASupportsAutomaticGraphicsSwitching);
  170. }
  171. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
  172. if (ctxconfig->major >= 4)
  173. {
  174. SET_ATTRIB(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core);
  175. }
  176. else
  177. #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
  178. if (ctxconfig->major >= 3)
  179. {
  180. SET_ATTRIB(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  181. }
  182. if (ctxconfig->major <= 2)
  183. {
  184. if (fbconfig->auxBuffers != GLFW_DONT_CARE)
  185. SET_ATTRIB(NSOpenGLPFAAuxBuffers, fbconfig->auxBuffers);
  186. if (fbconfig->accumRedBits != GLFW_DONT_CARE &&
  187. fbconfig->accumGreenBits != GLFW_DONT_CARE &&
  188. fbconfig->accumBlueBits != GLFW_DONT_CARE &&
  189. fbconfig->accumAlphaBits != GLFW_DONT_CARE)
  190. {
  191. const int accumBits = fbconfig->accumRedBits +
  192. fbconfig->accumGreenBits +
  193. fbconfig->accumBlueBits +
  194. fbconfig->accumAlphaBits;
  195. SET_ATTRIB(NSOpenGLPFAAccumSize, accumBits);
  196. }
  197. }
  198. if (fbconfig->redBits != GLFW_DONT_CARE &&
  199. fbconfig->greenBits != GLFW_DONT_CARE &&
  200. fbconfig->blueBits != GLFW_DONT_CARE)
  201. {
  202. int colorBits = fbconfig->redBits +
  203. fbconfig->greenBits +
  204. fbconfig->blueBits;
  205. // macOS needs non-zero color size, so set reasonable values
  206. if (colorBits == 0)
  207. colorBits = 24;
  208. else if (colorBits < 15)
  209. colorBits = 15;
  210. SET_ATTRIB(NSOpenGLPFAColorSize, colorBits);
  211. }
  212. if (fbconfig->alphaBits != GLFW_DONT_CARE)
  213. SET_ATTRIB(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
  214. if (fbconfig->depthBits != GLFW_DONT_CARE)
  215. SET_ATTRIB(NSOpenGLPFADepthSize, fbconfig->depthBits);
  216. if (fbconfig->stencilBits != GLFW_DONT_CARE)
  217. SET_ATTRIB(NSOpenGLPFAStencilSize, fbconfig->stencilBits);
  218. if (fbconfig->stereo)
  219. {
  220. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
  221. _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
  222. "NSGL: Stereo rendering is deprecated");
  223. return GLFW_FALSE;
  224. #else
  225. ADD_ATTRIB(NSOpenGLPFAStereo);
  226. #endif
  227. }
  228. if (fbconfig->doublebuffer)
  229. ADD_ATTRIB(NSOpenGLPFADoubleBuffer);
  230. if (fbconfig->samples != GLFW_DONT_CARE)
  231. {
  232. if (fbconfig->samples == 0)
  233. {
  234. SET_ATTRIB(NSOpenGLPFASampleBuffers, 0);
  235. }
  236. else
  237. {
  238. SET_ATTRIB(NSOpenGLPFASampleBuffers, 1);
  239. SET_ATTRIB(NSOpenGLPFASamples, fbconfig->samples);
  240. }
  241. }
  242. // NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
  243. // framebuffer, so there's no need (and no way) to request it
  244. ADD_ATTRIB(0);
  245. #undef ADD_ATTRIB
  246. #undef SET_ATTRIB
  247. window->context.nsgl.pixelFormat =
  248. [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
  249. if (window->context.nsgl.pixelFormat == nil)
  250. {
  251. _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
  252. "NSGL: Failed to find a suitable pixel format");
  253. return GLFW_FALSE;
  254. }
  255. NSOpenGLContext* share = nil;
  256. if (ctxconfig->share)
  257. share = ctxconfig->share->context.nsgl.object;
  258. window->context.nsgl.object =
  259. [[NSOpenGLContext alloc] initWithFormat:window->context.nsgl.pixelFormat
  260. shareContext:share];
  261. if (window->context.nsgl.object == nil)
  262. {
  263. _glfwInputError(GLFW_VERSION_UNAVAILABLE,
  264. "NSGL: Failed to create OpenGL context");
  265. return GLFW_FALSE;
  266. }
  267. if (fbconfig->transparent)
  268. {
  269. GLint opaque = 0;
  270. [window->context.nsgl.object setValues:&opaque
  271. forParameter:NSOpenGLContextParameterSurfaceOpacity];
  272. }
  273. [window->ns.view setWantsBestResolutionOpenGLSurface:window->ns.retina];
  274. [window->context.nsgl.object setView:window->ns.view];
  275. window->context.makeCurrent = makeContextCurrentNSGL;
  276. window->context.swapBuffers = swapBuffersNSGL;
  277. window->context.swapInterval = swapIntervalNSGL;
  278. window->context.extensionSupported = extensionSupportedNSGL;
  279. window->context.getProcAddress = getProcAddressNSGL;
  280. window->context.destroy = destroyContextNSGL;
  281. return GLFW_TRUE;
  282. }
  283. //////////////////////////////////////////////////////////////////////////
  284. ////// GLFW native API //////
  285. //////////////////////////////////////////////////////////////////////////
  286. GLFWAPI id glfwGetNSGLContext(GLFWwindow* handle)
  287. {
  288. _GLFWwindow* window = (_GLFWwindow*) handle;
  289. _GLFW_REQUIRE_INIT_OR_RETURN(nil);
  290. if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA)
  291. {
  292. _glfwInputError(GLFW_PLATFORM_UNAVAILABLE,
  293. "NSGL: Platform not initialized");
  294. return nil;
  295. }
  296. if (window->context.source != GLFW_NATIVE_CONTEXT_API)
  297. {
  298. _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
  299. return nil;
  300. }
  301. return window->context.nsgl.object;
  302. }
  303. #endif // _GLFW_COCOA