diff --git a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java index a428115f..da1f6d56 100644 --- a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java +++ b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java @@ -82,8 +82,8 @@ final class AWTSurfaceLock { // It is only needed on first call, so we avoid it on all subsequent calls // due to performance.. - // Allow the use of a Core Animation Layer only when using non fullscreen Display.setParent() or AWTGLCanvas - final boolean allowCALayer = ((Display.getParent() != null && !Display.isFullscreen()) || component instanceof AWTGLCanvas) && isApplet(component) && LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 6); + // Allow the use of a Core Animation Layer only when using non fullscreen Display.setParent() or AWTGLCanvas and OS X 10.6+ + final boolean allowCALayer = ((Display.getParent() != null && !Display.isFullscreen()) || component instanceof AWTGLCanvas) && LWJGLUtil.isMacOSXEqualsOrBetterThan(10, 6); if (firstLockSucceeded) return lockAndInitHandle(lock_buffer, component, allowCALayer); @@ -107,22 +107,4 @@ final class AWTSurfaceLock { } private static native void nUnlock(ByteBuffer lock_buffer) throws LWJGLException; - - /** - * This method will return true if the component is running in an applet - */ - public boolean isApplet(Canvas component) { - - Component parent = component.getParent(); - - while (parent != null) { - if (parent instanceof Applet) { - return true; - } - parent = parent.getParent(); - } - - // not an applet - return false; - } } diff --git a/src/native/macosx/org_lwjgl_opengl_Display.m b/src/native/macosx/org_lwjgl_opengl_Display.m index a97e3594..fffe738b 100644 --- a/src/native/macosx/org_lwjgl_opengl_Display.m +++ b/src/native/macosx/org_lwjgl_opengl_Display.m @@ -446,8 +446,13 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIE } if (parented) { - window_info->window = [peer_info->parent window]; - + if (peer_info->parent != nil) { + window_info->window = [peer_info->parent window]; + } + else { + window_info->window = nil; + } + if (window_info->window != nil) { [peer_info->parent addSubview:window_info->view]; [window_info->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m index 4b2483f7..ef7dff2a 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXCanvasPeerInfo.m @@ -39,8 +39,6 @@ */ #import -//#import - #include #include #include "awt_tools.h" @@ -48,17 +46,91 @@ #include "context.h" #include "common_tools.h" + +@interface GLLayer : NSOpenGLLayer {} +- (void) attachLayer; +- (void) removeLayer; +@end + JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle (JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle); - AWTSurfaceLock *surface = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi = (JAWT_MacOSXDrawingSurfaceInfo *)surface->dsi->platformInfo; - peer_info->parent = macosx_dsi->cocoaViewRef; + // check for CALayer support + if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) { + + if (macosx_dsi != NULL) { + // get the root layer of the AWT Canvas + id surfaceLayers = (id )macosx_dsi; + GLLayer *glLayer = [[GLLayer new] autorelease]; + [glLayer performSelectorOnMainThread:@selector(attachLayer:) withObject:surfaceLayers waitUntilDone:NO]; + } + + peer_info->isWindowed = true; + peer_info->parent = nil; + + [pool release]; + return; + } + + // no CALayer support, fallback to using legacy method of getting the NSView of an AWT Canvas + peer_info->parent = macosx_dsi->cocoaViewRef; peer_info->isWindowed = true; [pool release]; } + +@implementation GLLayer + +- (void) attachLayer:(id)surfaceLayers { + self.asynchronous = YES; + self.needsDisplayOnBoundsChange = YES; + self.opaque = NO; + self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; + surfaceLayers.layer = self; +} + +- (void) removeLayer:(id)surfaceLayers { + surfaceLayers.layer = nil; +} + +-(void)drawInCGLContext:(CGLContextObj)glContext + pixelFormat:(CGLPixelFormatObj)pixelFormat + forLayerTime:(CFTimeInterval)timeInterval + displayTime:(const CVTimeStamp *)timeStamp { + + // set the current context + CGLSetCurrentContext(glContext); + + // draw a single red quad spinning around based on the current time + GLfloat rotate = timeInterval * 60.0; // 60 degrees per second + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glRotatef(rotate, 0.0, 0.0, 1.0); + glBegin(GL_QUADS); + glColor3f(1.0, 1.0, 0.0); + glVertex2f(-0.5, -0.5); + glVertex2f(-0.5, 0.5); + glVertex2f( 0.5, 0.5); + glVertex2f( 0.5, -0.5); + glEnd(); + glPopMatrix(); + + // call super to finalize the drawing - by default all it does is call glFlush() + [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; +} + +-(BOOL)canDrawInCGLContext:(CGLContextObj)glContext + pixelFormat:(CGLPixelFormatObj)pixelFormat + forLayerTime:(CFTimeInterval)timeInterval + displayTime:(const CVTimeStamp *)timeStamp { + return YES; +} + +@end diff --git a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m index 4abf8ba4..830fd882 100644 --- a/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m +++ b/src/native/macosx/org_lwjgl_opengl_MacOSXContextImplementation.m @@ -82,7 +82,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre NSSize displaySize = peer_info->window_info->display_rect.size; GLint dim[2] = {displaySize.width, displaySize.height}; CGLSetParameter(cgcontext, kCGLCPSurfaceBackingSize, dim); - CGLEnable(cgcontext, kCGLCESurfaceBackingSize); + CGLEnable(cgcontext, kCGLCESurfaceBackingSize); } else { // disable any fixed backbuffer size to allow resizing