nsgl_context.m 11 KB

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