*/
+#include "glproc.hpp"
+
#include <stdlib.h>
#include <iostream>
+#include <dlfcn.h>
+
#include <Cocoa/Cocoa.h>
#include "glws.hpp"
namespace glws {
-NSAutoreleasePool *autoreleasePool = nil;
+static __thread NSAutoreleasePool *
+autoreleasePool = nil;
class CocoaVisual : public Visual
NSWindow *window;
NSOpenGLContext *currentContext;
- CocoaDrawable(const Visual *vis, int w, int h) :
- Drawable(vis, w, h),
+ CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
+ Drawable(vis, w, h, pbuffer),
currentContext(nil)
{
NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
};
+static inline void
+initThread(void) {
+ if (autoreleasePool == nil) {
+ autoreleasePool = [[NSAutoreleasePool alloc] init];
+ }
+}
+
void
init(void) {
- [NSApplication sharedApplication];
+ // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
+ _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
- autoreleasePool = [[NSAutoreleasePool alloc] init];
+ initThread();
+
+ [NSApplication sharedApplication];
[NSApp finishLaunching];
}
Visual *
createVisual(bool doubleBuffer, Profile profile) {
+
+ initThread();
+
if (profile != PROFILE_COMPAT &&
profile != PROFILE_CORE) {
return nil;
return NULL;
#endif
}
+
+ // Use Apple software rendering for debugging purposes.
+ if (0) {
+ attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
+ }
+
attribs.end();
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
}
Drawable *
-createDrawable(const Visual *visual, int width, int height)
+createDrawable(const Visual *visual, int width, int height, bool pbuffer)
{
- return new CocoaDrawable(visual, width, height);
+ initThread();
+
+ return new CocoaDrawable(visual, width, height, pbuffer);
}
Context *
createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
{
+ initThread();
+
NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
NSOpenGLContext *share_context = nil;
NSOpenGLContext *context;
bool
makeCurrent(Drawable *drawable, Context *context)
{
+ initThread();
+
if (!drawable || !context) {
[NSOpenGLContext clearCurrentContext];
} else {
bool
processEvents(void) {
- NSEvent* event;
+ initThread();
+ NSEvent* event;
do {
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]