2010-09-07 15:34:51 +00:00
|
|
|
//========================================================================
|
2011-03-06 00:46:39 +00:00
|
|
|
// GLFW - An OpenGL library
|
2010-09-07 15:34:51 +00:00
|
|
|
// Platform: Cocoa/NSOpenGL
|
2010-09-07 15:41:26 +00:00
|
|
|
// API Version: 3.0
|
2010-09-07 15:34:51 +00:00
|
|
|
// WWW: http://www.glfw.org/
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
|
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would
|
|
|
|
// be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not
|
|
|
|
// be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
2012-08-13 14:03:44 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2010-09-16 01:25:36 +00:00
|
|
|
|
2012-08-12 12:13:18 +00:00
|
|
|
// The per-thread current context/window pointer
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-08-13 14:03:44 +00:00
|
|
|
static pthread_key_t _glfwCurrentTLS;
|
2012-08-12 12:13:18 +00:00
|
|
|
|
|
|
|
|
2010-09-16 01:25:36 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2012-12-29 23:45:22 +00:00
|
|
|
////// GLFW internal API //////
|
2010-09-16 01:25:36 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-13 14:03:44 +00:00
|
|
|
// Initialize OpenGL support
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2013-01-15 19:49:29 +00:00
|
|
|
int _glfwInitContextAPI(void)
|
2012-08-13 14:03:44 +00:00
|
|
|
{
|
|
|
|
if (pthread_key_create(&_glfwCurrentTLS, NULL) != 0)
|
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"NSOpenGL: Failed to create context TLS");
|
2012-08-13 14:03:44 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate OpenGL support
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2013-01-15 19:49:29 +00:00
|
|
|
void _glfwTerminateContextAPI(void)
|
2012-08-13 14:03:44 +00:00
|
|
|
{
|
|
|
|
pthread_key_delete(_glfwCurrentTLS);
|
|
|
|
}
|
|
|
|
|
2012-12-29 23:58:18 +00:00
|
|
|
// Create the OpenGL context
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-12-29 23:58:18 +00:00
|
|
|
int _glfwCreateContext(_GLFWwindow* window,
|
|
|
|
const _GLFWwndconfig* wndconfig,
|
|
|
|
const _GLFWfbconfig* fbconfig)
|
|
|
|
{
|
|
|
|
unsigned int attributeCount = 0;
|
|
|
|
|
|
|
|
// Mac OS X needs non-zero color size, so set resonable values
|
|
|
|
int colorBits = fbconfig->redBits + fbconfig->greenBits + fbconfig->blueBits;
|
|
|
|
if (colorBits == 0)
|
|
|
|
colorBits = 24;
|
|
|
|
else if (colorBits < 15)
|
|
|
|
colorBits = 15;
|
|
|
|
|
|
|
|
if (wndconfig->clientAPI == GLFW_OPENGL_ES_API)
|
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
|
|
|
"NSOpenGL: This API does not support OpenGL ES");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
|
|
|
// Fail if any OpenGL version above 2.1 other than 3.2 was requested
|
|
|
|
if (wndconfig->glMajor > 3 ||
|
|
|
|
(wndconfig->glMajor == 3 && wndconfig->glMinor != 2))
|
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
|
|
|
"NSOpenGL: The targeted version of Mac OS X does not "
|
|
|
|
"support any OpenGL version above 2.1 except 3.2");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wndconfig->glMajor > 2)
|
|
|
|
{
|
|
|
|
if (!wndconfig->glForward)
|
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
|
|
|
"NSOpenGL: The targeted version of Mac OS X only "
|
|
|
|
"supports OpenGL 3.2 contexts if they are "
|
|
|
|
"forward-compatible");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wndconfig->glProfile != GLFW_OPENGL_CORE_PROFILE)
|
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
|
|
|
"NSOpenGL: The targeted version of Mac OS X only "
|
|
|
|
"supports OpenGL 3.2 contexts if they use the "
|
|
|
|
"core profile");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Fail if OpenGL 3.0 or above was requested
|
|
|
|
if (wndconfig->glMajor > 2)
|
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
|
|
|
"NSOpenGL: The targeted version of Mac OS X does not "
|
|
|
|
"support OpenGL version 3.0 or above");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
|
|
|
|
|
|
|
|
// Fail if a robustness strategy was requested
|
|
|
|
if (wndconfig->glRobustness)
|
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
|
|
|
"NSOpenGL: Mac OS X does not support OpenGL robustness "
|
|
|
|
"strategies");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ADD_ATTR(x) { attributes[attributeCount++] = x; }
|
|
|
|
#define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
|
|
|
|
|
|
|
|
// Arbitrary array size here
|
|
|
|
NSOpenGLPixelFormatAttribute attributes[40];
|
|
|
|
|
|
|
|
ADD_ATTR(NSOpenGLPFADoubleBuffer);
|
|
|
|
|
2013-01-03 19:01:56 +00:00
|
|
|
if (wndconfig->monitor)
|
2012-12-29 23:58:18 +00:00
|
|
|
{
|
|
|
|
ADD_ATTR(NSOpenGLPFANoRecovery);
|
|
|
|
ADD_ATTR2(NSOpenGLPFAScreenMask,
|
|
|
|
CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
|
|
|
if (wndconfig->glMajor > 2)
|
|
|
|
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
|
|
|
|
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
|
|
|
|
|
|
|
|
ADD_ATTR2(NSOpenGLPFAColorSize, colorBits);
|
|
|
|
|
|
|
|
if (fbconfig->alphaBits > 0)
|
|
|
|
ADD_ATTR2(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
|
|
|
|
|
|
|
|
if (fbconfig->depthBits > 0)
|
|
|
|
ADD_ATTR2(NSOpenGLPFADepthSize, fbconfig->depthBits);
|
|
|
|
|
|
|
|
if (fbconfig->stencilBits > 0)
|
|
|
|
ADD_ATTR2(NSOpenGLPFAStencilSize, fbconfig->stencilBits);
|
|
|
|
|
|
|
|
int accumBits = fbconfig->accumRedBits + fbconfig->accumGreenBits +
|
|
|
|
fbconfig->accumBlueBits + fbconfig->accumAlphaBits;
|
|
|
|
|
|
|
|
if (accumBits > 0)
|
|
|
|
ADD_ATTR2(NSOpenGLPFAAccumSize, accumBits);
|
|
|
|
|
|
|
|
if (fbconfig->auxBuffers > 0)
|
|
|
|
ADD_ATTR2(NSOpenGLPFAAuxBuffers, fbconfig->auxBuffers);
|
|
|
|
|
|
|
|
if (fbconfig->stereo)
|
|
|
|
ADD_ATTR(NSOpenGLPFAStereo);
|
|
|
|
|
|
|
|
if (fbconfig->samples > 0)
|
|
|
|
{
|
|
|
|
ADD_ATTR2(NSOpenGLPFASampleBuffers, 1);
|
|
|
|
ADD_ATTR2(NSOpenGLPFASamples, fbconfig->samples);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
|
|
|
|
// frambuffer, so there's no need (and no way) to request it
|
|
|
|
|
|
|
|
ADD_ATTR(0);
|
|
|
|
|
|
|
|
#undef ADD_ATTR
|
|
|
|
#undef ADD_ATTR2
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
window->nsgl.pixelFormat =
|
2012-12-29 23:58:18 +00:00
|
|
|
[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
2013-01-02 00:40:42 +00:00
|
|
|
if (window->nsgl.pixelFormat == nil)
|
2012-12-29 23:58:18 +00:00
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"NSOpenGL: Failed to create OpenGL pixel format");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSOpenGLContext* share = NULL;
|
|
|
|
|
|
|
|
if (wndconfig->share)
|
2013-01-02 00:40:42 +00:00
|
|
|
share = wndconfig->share->nsgl.context;
|
2012-12-29 23:58:18 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
window->nsgl.context =
|
|
|
|
[[NSOpenGLContext alloc] initWithFormat:window->nsgl.pixelFormat
|
2012-12-29 23:58:18 +00:00
|
|
|
shareContext:share];
|
2013-01-02 00:40:42 +00:00
|
|
|
if (window->nsgl.context == nil)
|
2012-12-29 23:58:18 +00:00
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"NSOpenGL: Failed to create OpenGL context");
|
2012-12-29 23:58:18 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the OpenGL context
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-12-29 23:58:18 +00:00
|
|
|
void _glfwDestroyContext(_GLFWwindow* window)
|
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->nsgl.pixelFormat release];
|
|
|
|
window->nsgl.pixelFormat = nil;
|
2012-12-29 23:58:18 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->nsgl.context release];
|
|
|
|
window->nsgl.context = nil;
|
2012-12-29 23:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 23:45:22 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW platform API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-07-27 14:01:27 +00:00
|
|
|
void _glfwPlatformMakeContextCurrent(_GLFWwindow* window)
|
|
|
|
{
|
|
|
|
if (window)
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->nsgl.context makeCurrentContext];
|
2011-07-27 14:01:27 +00:00
|
|
|
else
|
|
|
|
[NSOpenGLContext clearCurrentContext];
|
2012-08-12 12:13:18 +00:00
|
|
|
|
2012-08-13 14:03:44 +00:00
|
|
|
pthread_setspecific(_glfwCurrentTLS, window);
|
2012-08-12 12:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_GLFWwindow* _glfwPlatformGetCurrentContext(void)
|
|
|
|
{
|
2012-08-13 14:03:44 +00:00
|
|
|
return (_GLFWwindow*) pthread_getspecific(_glfwCurrentTLS);
|
2011-07-27 14:01:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-06 16:13:37 +00:00
|
|
|
void _glfwPlatformSwapBuffers(_GLFWwindow* window)
|
2011-03-04 13:25:12 +00:00
|
|
|
{
|
|
|
|
// ARP appears to be unnecessary, but this is future-proof
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->nsgl.context flushBuffer];
|
2011-03-04 13:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _glfwPlatformSwapInterval(int interval)
|
|
|
|
{
|
2012-08-13 14:03:44 +00:00
|
|
|
_GLFWwindow* window = _glfwPlatformGetCurrentContext();
|
2011-03-04 13:25:12 +00:00
|
|
|
|
|
|
|
GLint sync = interval;
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->nsgl.context setValues:&sync forParameter:NSOpenGLCPSwapInterval];
|
2011-03-04 13:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
int _glfwPlatformExtensionSupported(const char* extension)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2011-03-04 13:29:05 +00:00
|
|
|
// There are no NSGL extensions
|
2010-09-07 15:34:51 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:16:40 +00:00
|
|
|
GLFWglproc _glfwPlatformGetProcAddress(const char* procname)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
CFStringRef symbolName = CFStringCreateWithCString(kCFAllocatorDefault,
|
|
|
|
procname,
|
|
|
|
kCFStringEncodingASCII);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
GLFWglproc symbol = CFBundleGetFunctionPointerForName(_glfw.nsgl.framework,
|
2012-06-04 22:16:40 +00:00
|
|
|
symbolName);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
CFRelease(symbolName);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:38:14 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW native API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GLFWAPI id glfwGetNSGLContext(GLFWwindow* handle)
|
|
|
|
{
|
|
|
|
_GLFWwindow* window = (_GLFWwindow*) handle;
|
|
|
|
|
|
|
|
if (!_glfwInitialized)
|
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_NOT_INITIALIZED, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window->nsgl.context;
|
|
|
|
}
|
|
|
|
|