Permalink
Browse files

i379, CGLCreateContext/CGLDestroyContext/CGLLockContext/CGLUnlockCont…

…ext/CGLGetCurrentContext/CGLSetCurrentContext implemented for Windows, NSOpenGLContext currentContext is now per-thread

git-svn-id: http://cocotron.googlecode.com/svn/trunk@625 43251c19-cd1f-0410-9ec0-95c558c233a8
  • Loading branch information...
1 parent 026ad4a commit 509a8227a1a21b2aaafba2a4b64c4a2e2d7f1dd6 cocotron committed Sep 2, 2009
@@ -2862,6 +2862,7 @@
FEC0C7820F146AF800903F6D /* KGImageSource_GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = FEC0C7800F146AF800903F6D /* KGImageSource_GIF.m */; };
FEC0C7830F146AF800903F6D /* KGImageSource_GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = FEC0C77F0F146AF800903F6D /* KGImageSource_GIF.h */; };
FEC0C7840F146AF800903F6D /* KGImageSource_GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = FEC0C7800F146AF800903F6D /* KGImageSource_GIF.m */; };
+ FED35B6F104DF8C10038ED7C /* CGLContext.m in Sources */ = {isa = PBXBuildFile; fileRef = FED35B6E104DF8C10038ED7C /* CGLContext.m */; };
FEDF951F0CAF43E7009DC96F /* KGPDFContext.h in Headers */ = {isa = PBXBuildFile; fileRef = FEDF951B0CAF43E7009DC96F /* KGPDFContext.h */; settings = {ATTRIBUTES = (Private, ); }; };
FEDF95200CAF43E7009DC96F /* KGPDFContext.m in Sources */ = {isa = PBXBuildFile; fileRef = FEDF951C0CAF43E7009DC96F /* KGPDFContext.m */; };
FEE36B800F4B20C6005AEBBF /* NSToolbarOverflowArrow.pdf in Resources */ = {isa = PBXBuildFile; fileRef = FEE36B7F0F4B20C5005AEBBF /* NSToolbarOverflowArrow.pdf */; };
@@ -3837,6 +3838,7 @@
FED310DE0BCC027200552615 /* NSAnimation.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = NSAnimation.m; sourceTree = "<group>"; };
FED310DF0BCC027200552615 /* NSViewAnimation.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = NSViewAnimation.h; sourceTree = "<group>"; };
FED310E00BCC027200552615 /* NSViewAnimation.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = NSViewAnimation.m; sourceTree = "<group>"; };
+ FED35B6E104DF8C10038ED7C /* CGLContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CGLContext.m; sourceTree = "<group>"; };
FEDF951B0CAF43E7009DC96F /* KGPDFContext.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = KGPDFContext.h; sourceTree = "<group>"; };
FEDF951C0CAF43E7009DC96F /* KGPDFContext.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = KGPDFContext.m; sourceTree = "<group>"; };
FEE36B7F0F4B20C5005AEBBF /* NSToolbarOverflowArrow.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = NSToolbarOverflowArrow.pdf; sourceTree = "<group>"; };
@@ -4591,6 +4593,7 @@
6E2B56060976075300DA0954 /* Win32 */ = {
isa = PBXGroup;
children = (
+ FED35B6E104DF8C10038ED7C /* CGLContext.m */,
FEA661210FF5C6F200E2C03B /* KGFont_gdi.h */,
FEA661220FF5C6F200E2C03B /* KGFont_gdi.m */,
FEA25F960EECCE4D008750D0 /* NSSound_win32.h */,
@@ -8009,6 +8012,7 @@
FE463EE3102D0F9D00B265D6 /* NSViewController.m in Sources */,
FE49B9A81030C49300ADCC6C /* NSCollectionViewItem.m in Sources */,
FE953DDB103B0EA0008A71F0 /* NSEvent_other.m in Sources */,
+ FED35B6F104DF8C10038ED7C /* CGLContext.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -8359,6 +8363,7 @@
"-DAPPKIT_INSIDE_BUILD",
"-DCORETEXT_INSIDE_BUILD",
"-DCOREGRAPHICS_INSIDE_BUILD",
+ "-DCGL_INSIDE_BUILD",
"-D__LITTLE_ENDIAN__",
"-Wreturn-type",
);
@@ -8423,6 +8428,7 @@
"-DAPPKIT_INSIDE_BUILD",
"-DCORETEXT_INSIDE_BUILD",
"-DCOREGRAPHICS_INSIDE_BUILD",
+ "-DCGL_INSIDE_BUILD",
"-D__LITTLE_ENDIAN__",
"-DLINUX",
"-Wreturn-type",
@@ -8475,6 +8481,7 @@
"-DAPPKIT_INSIDE_BUILD",
"-DCORETEXT_INSIDE_BUILD",
"-DCOREGRAPHICS_INSIDE_BUILD",
+ "-DCGL_INSIDE_BUILD",
"-D__LITTLE_ENDIAN__",
"-DLINUX",
"-Wreturn-type",
@@ -8525,6 +8532,7 @@
"-DAPPKIT_INSIDE_BUILD",
"-DCORETEXT_INSIDE_BUILD",
"-DCOREGRAPHICS_INSIDE_BUILD",
+ "-DCGL_INSIDE_BUILD",
"-D__LITTLE_ENDIAN__",
"-DDARWIN",
"-Wreturn-type",
@@ -8581,6 +8589,7 @@
"-DAPPKIT_INSIDE_BUILD",
"-DCORETEXT_INSIDE_BUILD",
"-DCOREGRAPHICS_INSIDE_BUILD",
+ "-DCGL_INSIDE_BUILD",
"-D__LITTLE_ENDIAN__",
"-DDARWIN",
"-Wreturn-type",
@@ -8629,6 +8638,7 @@
"-DAPPKIT_INSIDE_BUILD",
"-DCORETEXT_INSIDE_BUILD",
"-DCOREGRAPHICS_INSIDE_BUILD",
+ "-DCGL_INSIDE_BUILD",
"-D__LITTLE_ENDIAN__",
"-Wreturn-type",
);
@@ -9,27 +9,34 @@
#import <AppKit/NSOpenGLPixelFormat.h>
#import <AppKit/NSOpenGLDrawable.h>
#import <AppKit/NSRaise.h>
+#import <OpenGL/OpenGL.h>
+#import <Foundation/NSThread-Private.h>
@interface NSOpenGLContext(private)
-(void)_clearCurrentContext;
@end
-static NSOpenGLContext *currentContext=nil;
+@implementation NSOpenGLContext
-// Platform must define this function
-void CGLContextDelete(void *glContext);
+static inline NSOpenGLContext *_currentContext(){
+ return (NSOpenGLContext *)NSThreadSharedInstanceDoNotCreate(@"NSOpenGLContext");
+}
-@implementation NSOpenGLContext
+static void _setCurrentContext(NSOpenGLContext *context){
+ [NSCurrentThread() setSharedObject:context forClassName:@"NSOpenGLContext"];
+}
+
+static inline void _clearCurrentContext(){
+ [_currentContext() _clearCurrentContext];
+ _setCurrentContext(nil);
+}
+(NSOpenGLContext *)currentContext {
- return currentContext;
+ return _currentContext();
}
+(void)clearCurrentContext {
- if(currentContext!=nil) {
- [currentContext _clearCurrentContext];
- }
- currentContext=nil;
+ _clearCurrentContext();
}
-initWithFormat:(NSOpenGLPixelFormat *)pixelFormat shareContext:(NSOpenGLContext *)shareContext {
@@ -38,13 +45,14 @@ +(void)clearCurrentContext {
}
-(void)dealloc {
- if(currentContext==self)
- currentContext=nil;
+// FIXME: this doesn't actually work because if we are the current context we're retained by the thread shared object dict
+ if(_currentContext()==self)
+ _clearCurrentContext();
[_pixelFormat release];
_view=nil;
[_drawable invalidate];
[_drawable release];
- CGLContextDelete(_glContext);
+ CGLDestroyContext(_glContext);
[super dealloc];
}
@@ -93,7 +101,7 @@ -(void)makeCurrentContext {
}
}
[_drawable makeCurrentWithGLContext:_glContext];
- currentContext=self;
+ _setCurrentContext(self);
}
-(void)_clearCurrentContext {
@@ -0,0 +1,72 @@
+#import <OpenGL/OpenGL.h>
+#import <Foundation/NSString.h>
+
+#import "opengl_dll.h"
+
+struct CGLContextObj {
+ CRITICAL_SECTION lock; // FIXME: this should be converted to the OS*Lock* functions when they appear
+ HDC dc;
+ HGLRC glrc;
+};
+
+// FIXME: there should be a lock around initialization of this
+static DWORD cglThreadStorageIndex(){
+ static DWORD tlsIndex=TLS_OUT_OF_INDEXES;
+
+ if(tlsIndex==TLS_OUT_OF_INDEXES)
+ tlsIndex=TlsAlloc();
+
+ if(tlsIndex==TLS_OUT_OF_INDEXES)
+ NSLog(@"TlsAlloc failed in CGLContext");
+
+ return tlsIndex;
+}
+
+CGL_EXPORT CGLContextObj CGLGetCurrentContext(void) {
+ CGLContextObj result=TlsGetValue(cglThreadStorageIndex());
+
+ return result;
+}
+
+CGL_EXPORT CGLError CGLSetCurrentContext(CGLContextObj context) {
+ TlsSetValue(cglThreadStorageIndex(),context);
+ opengl_wglMakeCurrent(context->dc,context->glrc);
+ return kCGLNoError;
+}
+
+CGL_EXPORT CGLError CGLCreateContext(CGLPixelFormatObj pixelFormat,CGLContextObj other,CGLContextObj *resultp) {
+// FIXME: yes, this is bogus
+ HDC dc=(HDC)other;
+ CGLContextObj result=NSZoneMalloc(NULL,sizeof(struct CGLContextObj));
+
+ InitializeCriticalSection(&(result->lock));
+ result->dc=dc;
+ result->glrc=opengl_wglCreateContext(dc);
+ *resultp=result;
+
+ return kCGLNoError;
+}
+
+CGL_EXPORT CGLError CGLDestroyContext(CGLContextObj context) {
+ if(context!=NULL){
+ if(CGLGetCurrentContext()==context)
+ CGLSetCurrentContext(NULL);
+
+ DeleteCriticalSection(&(context->lock));
+ opengl_wglDeleteContext(context->glrc);
+ NSZoneFree(NULL,context);
+ }
+
+ return kCGLNoError;
+}
+
+CGL_EXPORT CGLError CGLLockContext(CGLContextObj context) {
+ EnterCriticalSection(&(context->lock));
+ return kCGLNoError;
+}
+
+CGL_EXPORT CGLError CGLUnlockContext(CGLContextObj context) {
+ LeaveCriticalSection(&(context->lock));
+ return kCGLNoError;
+}
+
@@ -2,15 +2,7 @@
#import <AppKit/NSOpenGLPixelFormat.h>
#import <AppKit/NSWindow-Private.h>
#import <AppKit/Win32Window.h>
-#import "opengl_dll.h"
-
-void CGLContextDelete(void *glContext) {
- if(glContext!=NULL){
- if(opengl_wglGetCurrentContext()==glContext)
- opengl_wglMakeCurrent(NULL,NULL);
- opengl_wglDeleteContext(glContext);
- }
-}
+#import <OpenGL/OpenGL.h>
@interface NSOpenGLDrawable(GDI)
@@ -196,7 +188,11 @@ -(HDC)dc {
}
-(void *)createGLContext {
- return opengl_wglCreateContext([self dc]);
+ CGLContextObj result=NULL;
+
+ CGLCreateContext(NULL,(void *)[self dc],&result);
+
+ return result;
}
-(void)invalidate {
@@ -221,13 +217,13 @@ -(void)updateWithView:(NSView *)view {
MoveWindow(_windowHandle, frame.origin.x, parentHeight-(frame.origin.y+frame.size.height),frame.size.width, frame.size.height,YES);
}
--(void)makeCurrentWithGLContext:(HGLRC)glContext {
- if(!opengl_wglMakeCurrent(_dc,glContext))
+-(void)makeCurrentWithGLContext:(CGLContextObj)glContext {
+ if(!CGLSetCurrentContext(glContext))
NSLog(@"wglMakeCurrent failed");
}
--(void)clearCurrentWithGLContext:(HGLRC)glContext {
- opengl_wglMakeCurrent(NULL,NULL);
+-(void)clearCurrentWithGLContext:(CGLContextObj)glContext {
+ CGLSetCurrentContext(NULL);
}
-(void)swapBuffers {
@@ -17,7 +17,7 @@
#include <GL/glx.h>
#include <GL/glu.h>
-void CGLContextDelete(void *glContext)
+void CGLDestroyContext(void *glContext)
{
glXDestroyContext([(X11Display*)[NSDisplay currentDisplay] display], glContext);
}
@@ -52,12 +52,12 @@ NSUInteger NSRealMemoryAvailable(void) {
}
static DWORD Win32ThreadStorageIndex() {
- static DWORD tlsIndex=0xFFFFFFFF;
+ static DWORD tlsIndex=TLS_OUT_OF_INDEXES;
- if(tlsIndex==0xFFFFFFFF)
+ if(tlsIndex==TLS_OUT_OF_INDEXES)
tlsIndex=TlsAlloc();
- if(tlsIndex==0xFFFFFFFF)
+ if(tlsIndex==TLS_OUT_OF_INDEXES)
Win32Assert("TlsAlloc");
return tlsIndex;
View
@@ -0,0 +1,4 @@
+#import <OpenGL/CGLTypes.h>
+
+CGL_EXPORT CGLContextObj CGLGetCurrentContext(void);
+CGL_EXPORT CGLError CGLSetCurrentContext(CGLContextObj context);
View
@@ -0,0 +1,53 @@
+
+#ifdef __cplusplus
+
+#if defined(__WIN32__)
+#if defined(CGL_INSIDE_BUILD)
+#define CGL_EXPORT extern "C" __declspec(dllexport)
+#else
+#define CGL_EXPORT extern "C" __declspec(dllimport)
+#endif
+#else
+#define CGL_EXPORT extern "C"
+#endif
+
+#else
+
+#if defined(__WIN32__)
+#if defined(CGL_INSIDE_BUILD)
+#define CGL_EXPORT __declspec(dllexport) extern
+#else
+#define CGL_EXPORT __declspec(dllimport) extern
+#endif
+#else
+#define CGL_EXPORT extern
+#endif
+
+
+#endif // __cplusplus
+
+typedef enum {
+ kCGLNoError=0,
+ kCGLBadAttribute=10000,
+ kCGLBadProperty=10001,
+ kCGLBadPixelFormat=10002,
+ kCGLBadRendererInfo=10003,
+ kCGLBadContext=10004,
+ kCGLBadDrawable=10005,
+ kCGLBadDisplay=10006,
+ kCGLBadState=10007,
+ kCGLBadValue=10008,
+ kCGLBadMatch=10009,
+ kCGLBadEnumeration=10010,
+ kCGLBadOffScreen=10011,
+ kCGLBadFullScreen=10012,
+ kCGLBadWindow=10013,
+ kCGLBadAddress=10014,
+ kCGLBadCodeModule=10015,
+ kCGLBadAlloc=10016,
+ kCGLBadConnection=10017,
+} CGLError;
+
+typedef struct CGLContextObj *CGLContextObj;
+typedef struct CGLPixelFormatObj *CGLPixelFormatObj;
+typedef struct CGLPBufferObj *CGLPBufferObj;
View
@@ -1,2 +1,11 @@
#import <OpenGL/gl.h>
#import <OpenGL/glext.h>
+
+#import <OpenGL/CGLTypes.h>
+#import <OpenGL/CGLCurrent.h>
+
+CGL_EXPORT CGLError CGLCreateContext(CGLPixelFormatObj pixelFormat,CGLContextObj other,CGLContextObj *result);
+CGL_EXPORT CGLError CGLDestroyContext(CGLContextObj context);
+
+CGL_EXPORT CGLError CGLLockContext(CGLContextObj context);
+CGL_EXPORT CGLError CGLUnlockContext(CGLContextObj context);
Oops, something went wrong.

0 comments on commit 509a822

Please sign in to comment.