diff options
15 files changed, 358 insertions, 249 deletions
diff --git a/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java b/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java index 278e2dc37..2fdf18404 100644 --- a/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java +++ b/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java @@ -946,6 +946,7 @@ public class GLCanvas extends Canvas implements AWTGLAutoDrawable, WindowClosing animatorPaused = false; } + // OLS will be detached by disposeGL's context destruction below if( null != context ) { if( context.isCreated() ) { // Catch dispose GLExceptions by GLEventListener, just 'print' them diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java index 9e6085030..f01e03a2f 100644 --- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java +++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java @@ -462,9 +462,9 @@ public abstract class MacOSXCGLContext extends GLContextImpl // NSOpenGLContext-based implementation class NSOpenGLImpl implements GLBackendImpl { private long pixelFormat = 0; // lifecycle: [create - destroy] - private long nsOpenGLLayer = 0; // lifecycle: [associateDrawable_true - associateDrawable_false] - private float screenVSyncTimeout; // microSec - private int vsyncTimeout; // microSec - for nsOpenGLLayer mode + private volatile long nsOpenGLLayer = 0; // lifecycle: [associateDrawable_true - associateDrawable_false] + private float screenVSyncTimeout = 16666; // microSec - defaults to 1/60s + private volatile int vsyncTimeout = 16666 + 1000; // microSec - for nsOpenGLLayer mode - defaults to 1/60s + 1ms private int lastWidth=0, lastHeight=0; // allowing to detect size change private boolean needsSetContextPBuffer = false; private ShaderProgram gl3ShaderProgram = null; @@ -472,6 +472,8 @@ public abstract class MacOSXCGLContext extends GLContextImpl @Override public boolean isNSContext() { return true; } + + /** Only returns a valid NSView. If !NSView, return null and mark either pbuffer and FBO. */ private long getNSViewHandle(boolean[] isPBuffer, boolean[] isFBO) { final long nsViewHandle; if(drawable instanceof GLFBODrawableImpl) { @@ -479,27 +481,15 @@ public abstract class MacOSXCGLContext extends GLContextImpl isPBuffer[0] = false; isFBO[0] = true; if(DEBUG) { - System.err.println("NS create GLFBODrawableImpl drawable: isFBO "+isFBO+", isPBuffer "+isPBuffer+", "+drawable.getClass().getName()+",\n\t"+drawable); - } - } else if(drawable instanceof MacOSXCGLDrawable) { - // we allow null here! (special pbuffer case) - nsViewHandle = ((MacOSXCGLDrawable)MacOSXCGLContext.this.drawable).getNSViewHandle(); - isPBuffer[0] = CGL.isNSOpenGLPixelBuffer(drawable.getHandle()); - isFBO[0] = false; - if(DEBUG) { - System.err.println("NS create MacOSXCGLDrawable drawable handle isFBO "+isFBO+", isPBuffer "+isPBuffer+", "+drawable.getClass().getName()+",\n\t"+drawable); + System.err.println("NS viewHandle.1: GLFBODrawableImpl drawable: isFBO "+isFBO+", isPBuffer "+isPBuffer+", "+drawable.getClass().getName()+",\n\t"+drawable); } } else { - // we only allow a valid NSView here final long drawableHandle = drawable.getHandle(); final boolean isNSView = OSXUtil.isNSView(drawableHandle); final boolean isNSWindow = OSXUtil.isNSWindow(drawableHandle); isPBuffer[0] = CGL.isNSOpenGLPixelBuffer(drawableHandle); isFBO[0] = false; - if(DEBUG) { - System.err.println("NS create Anonymous drawable handle "+toHexString(drawableHandle)+": isNSView "+isNSView+", isNSWindow "+isNSWindow+", isFBO "+isFBO+", isPBuffer "+isPBuffer+", "+drawable.getClass().getName()+",\n\t"+drawable); - } if( isNSView ) { nsViewHandle = drawableHandle; } else if( isNSWindow ) { @@ -507,7 +497,10 @@ public abstract class MacOSXCGLContext extends GLContextImpl } else if( isPBuffer[0] ) { nsViewHandle = 0; } else { - throw new RuntimeException("Anonymous drawable instance's handle neither NSView, NSWindow nor PBuffer: "+toHexString(drawableHandle)+", "+drawable.getClass().getName()+",\n\t"+drawable); + throw new RuntimeException("Drawable's handle neither NSView, NSWindow nor PBuffer: drawableHandle "+toHexString(drawableHandle)+", isNSView "+isNSView+", isNSWindow "+isNSWindow+", isFBO "+isFBO[0]+", isPBuffer "+isPBuffer[0]+", "+drawable.getClass().getName()+",\n\t"+drawable); + } + if(DEBUG) { + System.err.println("NS viewHandle.2: drawableHandle "+toHexString(drawableHandle)+" -> nsViewHandle "+toHexString(nsViewHandle)+": isNSView "+isNSView+", isNSWindow "+isNSWindow+", isFBO "+isFBO[0]+", isPBuffer "+isPBuffer[0]+", "+drawable.getClass().getName()+",\n\t"+drawable); } } needsSetContextPBuffer = isPBuffer[0]; @@ -567,8 +560,10 @@ public abstract class MacOSXCGLContext extends GLContextImpl _fixedCaps.setOnscreen( !isFBO && !isPBuffer ); fixedCaps = GLGraphicsConfigurationUtil.fixOpaqueGLCapabilities(_fixedCaps, chosenCaps.isBackgroundOpaque()); } - int sRefreshRate = OSXUtil.GetScreenRefreshRate(drawable.getNativeSurface().getGraphicsConfiguration().getScreen().getIndex()); - screenVSyncTimeout = 1000000f / sRefreshRate; + final int sRefreshRate = OSXUtil.GetScreenRefreshRate(drawable.getNativeSurface().getGraphicsConfiguration().getScreen().getIndex()); + if( 0 < sRefreshRate ) { + screenVSyncTimeout = 1000000f / sRefreshRate; + } if(DEBUG) { System.err.println("NS create OSX>=lion "+isLionOrLater); System.err.println("NS create incompleteView: "+incompleteView); @@ -611,7 +606,6 @@ public abstract class MacOSXCGLContext extends GLContextImpl pixelFormat = 0; } return CGL.deleteContext(ctx, true); - } @Override @@ -622,10 +616,8 @@ public abstract class MacOSXCGLContext extends GLContextImpl System.err.println("MaxOSXCGLContext.NSOpenGLImpl.associateDrawable: "+bound+", ctx "+toHexString(contextHandle)+", hasBackingLayerHost "+(null!=backingLayerHost)); } - if( bound ) { - - if( null != backingLayerHost ) { - + if( bound ) { + if( null != backingLayerHost ) { if( 0 != nsOpenGLLayer ) { // FIXME: redundant throw new InternalError("Lifecycle: bound=true, hasBackingLayerHost=true, but 'nsOpenGLLayer' is already/still set local: "+nsOpenGLLayer+", "+this); } @@ -676,32 +668,59 @@ public abstract class MacOSXCGLContext extends GLContextImpl } /** - * NSOpenGLLayer creation is performed on the current thread, - * which immediately creates it's own GL ctx sharing this ctx - * not causing any locking issues. + * NSOpenGLLayer creation and it's attachment is performed on the main w/o blocking, + * due to OSX main-thread requirements. + * Note: It somewhat works from another thread, however, + * GC-dealloc of the 'released' resources would happen very late! * - * Subsequent attaching is performed on main-thread w/o blocking. + * NSOpenGLLayer initialization creates it's own GL ctx sharing + * this ctx, hence we have to lock this ctx in the main-thread. * - * This is a lock free operation. - */ - final long cglCtx = CGL.getCGLContext(ctx); - if(0 == cglCtx) { - throw new InternalError("Null CGLContext for: "+this); - } - nsOpenGLLayer = CGL.createNSOpenGLLayer(ctx, gl3ShaderProgramName, pixelFormat, pbufferHandle, texID, chosenCaps.isBackgroundOpaque(), lastWidth, lastHeight); - if (DEBUG) { - System.err.println("NS create nsOpenGLLayer "+toHexString(nsOpenGLLayer)+" w/ pbuffer "+toHexString(pbufferHandle)+", texID "+texID+", texSize "+lastWidth+"x"+lastHeight+", "+drawable); - } - backingLayerHost.attachSurfaceLayer(nsOpenGLLayer); - setSwapInterval(1); // enabled per default in layered surface - } else { + * Locking of this ctx while creation and attachment + * also gives us good means of synchronization, i.e. it will be + * performed after this thread ends it's associateDrawable() [makeCurrent(), setDrawable(..)] + * and before the next display cycle involving makeCurrent(). + */ + OSXUtil.RunOnMainThread(false, new Runnable() { + public void run() { + if (DEBUG) { + System.err.println("NS create nsOpenGLLayer.0 "+Thread.currentThread().getName()); + } + final long cglCtx = CGL.getCGLContext(ctx); + if(0 == cglCtx) { + throw new GLException("Null CGLContext for: "+MacOSXCGLContext.this); + } + if( CGL.kCGLNoError != CGL.CGLLockContext(cglCtx) ) { + throw new GLException("Could not lock CGLContext for: "+MacOSXCGLContext.this); + } + try { + nsOpenGLLayer = CGL.createNSOpenGLLayer(ctx, gl3ShaderProgramName, pixelFormat, pbufferHandle, texID, chosenCaps.isBackgroundOpaque(), lastWidth, lastHeight); + if (DEBUG) { + System.err.println("NS create nsOpenGLLayer.2 "+Thread.currentThread().getName()+": "+toHexString(nsOpenGLLayer)+" w/ pbuffer "+toHexString(pbufferHandle)+", texID "+texID+", texSize "+lastWidth+"x"+lastHeight+", "+drawable); + } + backingLayerHost.attachSurfaceLayer(nsOpenGLLayer); + setSwapInterval(1); // enabled per default in layered surface + } catch (Throwable t) { + throw new GLException("createNSOpenGLLayer failed for: "+MacOSXCGLContext.this, t); + } finally { + if(CGL.kCGLNoError != CGL.CGLUnlockContext(cglCtx)) { + throw new GLException("Could not unlock CGLContext for: "+MacOSXCGLContext.this); + } + } + if (DEBUG) { + System.err.println("NS create nsOpenGLLayer.X "+Thread.currentThread().getName()); + } + } + }); + CGL.setContextView(contextHandle, 0); // [ctx clearDrawable] + } else { // -> null == backingLayerHost lastWidth = drawable.getWidth(); lastHeight = drawable.getHeight(); boolean[] isPBuffer = { false }; boolean[] isFBO = { false }; - CGL.setContextView(contextHandle, getNSViewHandle(isPBuffer, isFBO)); + CGL.setContextView(contextHandle, getNSViewHandle(isPBuffer, isFBO)); // will call [ctx clearDrawable] if view == 0, otherwise [ctx setView: view] if valid } - } else { + } else { // -> !bound if( 0 != nsOpenGLLayer ) { if( null == backingLayerHost ) { // FIXME: redundant throw new InternalError("Lifecycle: bound=false, hasNSOpneGLLayer=true, but 'backingLayerHost' is null local: "+nsOpenGLLayer+", "+this); @@ -714,13 +733,20 @@ public abstract class MacOSXCGLContext extends GLContextImpl // still having a valid OLS attached to surface (parent OLS could have been removed) backingLayerHost.detachSurfaceLayer(); } - CGL.releaseNSOpenGLLayer(nsOpenGLLayer); + // All CALayer lifecycle calls are deferred on main-thread, so is this. + final long _nsOpenGLLayer = nsOpenGLLayer; + nsOpenGLLayer = 0; + OSXUtil.RunOnMainThread(false, new Runnable() { + public void run() { + CGL.releaseNSOpenGLLayer(_nsOpenGLLayer); + } + }); if( null != gl3ShaderProgram ) { gl3ShaderProgram.destroy(MacOSXCGLContext.this.gl.getGL3()); gl3ShaderProgram = null; } - nsOpenGLLayer = 0; } + CGL.setContextView(contextHandle, 0); // [ctx clearDrawable] } } @@ -808,8 +834,10 @@ public abstract class MacOSXCGLContext extends GLContextImpl public boolean setSwapInterval(int interval) { if(0 != nsOpenGLLayer) { CGL.setNSOpenGLLayerSwapInterval(nsOpenGLLayer, interval); - vsyncTimeout = interval * (int)screenVSyncTimeout + 1000; // +1ms - if(DEBUG) { System.err.println("NS setSwapInterval: "+vsyncTimeout+" micros"); } + if( 0 < interval ) { + vsyncTimeout = interval * (int)screenVSyncTimeout + 1000; // +1ms + } + if(DEBUG) { System.err.println("NS setSwapInterval: "+interval+" -> "+vsyncTimeout+" micros"); } } CGL.setSwapInterval(contextHandle, interval); return true; @@ -839,8 +867,9 @@ public abstract class MacOSXCGLContext extends GLContextImpl } if(valid) { if(0 == skipSync) { - // If v-sync is disabled, frames will be drawn as quickly as possible - // w/o delay but in sync w/ CALayer. Otherwise wait until next swap interval (v-sync). + // If v-sync is disabled, frames will be drawn as quickly as possible w/o delay, + // while still synchronizing w/ CALayer. + // If v-sync is enabled wait until next swap interval (v-sync). CGL.waitUntilNSOpenGLLayerIsReady(nsOpenGLLayer, vsyncTimeout); } else { skipSync--; diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java index 0f282d33f..1daa892ba 100644 --- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java +++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java @@ -106,10 +106,6 @@ public abstract class MacOSXCGLDrawable extends GLDrawableImpl { protected void setRealizedImpl() { } - protected long getNSViewHandle() { - return GLBackendType.NSOPENGL == openGLMode ? getHandle() : 0; - } - @Override protected void associateContext(GLContext ctx, boolean bound) { // NOTE: we need to keep track of the created contexts in order to diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXPbufferCGLDrawable.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXPbufferCGLDrawable.java index ddff43031..1e845d179 100644 --- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXPbufferCGLDrawable.java +++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXPbufferCGLDrawable.java @@ -92,12 +92,6 @@ public class MacOSXPbufferCGLDrawable extends MacOSXCGLDrawable { return new MacOSXPbufferCGLContext(this, shareWith); } - @Override - protected long getNSViewHandle() { - // pbuffer handle is NSOpenGLPixelBuffer - return 0; - } - protected int getTextureTarget() { return pBufferTexTarget; } protected int getTextureWidth() { return pBufferTexWidth; } protected int getTextureHeight() { return pBufferTexHeight; } diff --git a/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m b/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m index 125ca8af8..96783c75d 100644 --- a/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m +++ b/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m @@ -117,6 +117,7 @@ extern GLboolean glIsVertexArray (GLuint array); CGLContextObj cglCtx = [self CGLContextObj]; DBG_PRINT("MyNSOpenGLContext::dealloc.0 %p (refcnt %d) - CGL-Ctx %p\n", self, (int)[self retainCount], cglCtx); + // NSLog(@"MyNSOpenGLContext::dealloc: %@",[NSThread callStackSymbols]); [self clearDrawable]; if( NULL != cglCtx ) { CGLDestroyContext( cglCtx ); @@ -143,8 +144,8 @@ extern GLboolean glIsVertexArray (GLuint array); NSOpenGLPixelFormat* parentPixelFmt; int texWidth; int texHeight; - int dedicatedWidth; - int dedicatedHeight; + volatile int dedicatedWidth; + volatile int dedicatedHeight; volatile NSOpenGLPixelBuffer* pbuffer; volatile GLuint textureID; volatile NSOpenGLPixelBuffer* newPBuffer; @@ -184,7 +185,6 @@ extern GLboolean glIsVertexArray (GLuint array); - (void) setGLEnabled: (Bool) enable; - (Bool) validateTexSizeWithDedicatedSize; -- (Bool) validateTexSize: (int) _texWidth texHeight: (int) _texHeight; - (void) setTextureID: (int) _texID; - (Bool) isSamePBuffer: (NSOpenGLPixelBuffer*) p; @@ -316,6 +316,14 @@ static const GLfloat gl_verts[] = { } } if(NULL != displayLink) { + CVReturn cvres; + DBG_PRINT("MyNSOpenGLLayer::openGLContextForPixelFormat.1: setup DisplayLink %p\n", displayLink); + cvres = CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, [glContext CGLContextObj], [parentPixelFmt CGLPixelFormatObj]); + if(kCVReturnSuccess != cvres) { + DBG_PRINT("MyNSOpenGLLayer::init %p, CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext failed: %d\n", self, cvres); + } + } + if(NULL != displayLink) { cvres = CVDisplayLinkSetOutputCallback(displayLink, renderMyNSOpenGLLayer, self); if(kCVReturnSuccess != cvres) { DBG_PRINT("MyNSOpenGLLayer::init %p, CVDisplayLinkSetOutputCallback failed: %d\n", self, cvres); @@ -354,14 +362,9 @@ static const GLfloat gl_verts[] = { - (Bool) validateTexSizeWithDedicatedSize { - return [self validateTexSize: dedicatedWidth texHeight: dedicatedHeight]; -} - -- (Bool) validateTexSize: (int) _texWidth texHeight: (int) _texHeight -{ - if(_texHeight != texHeight || _texWidth != texWidth) { - texWidth = _texWidth; - texHeight = _texHeight; + if( dedicatedHeight != texHeight || dedicatedWidth != texWidth ) { + texWidth = dedicatedWidth; + texHeight = dedicatedHeight; GLfloat texCoordWidth, texCoordHeight; if(NULL != pbuffer) { @@ -477,10 +480,10 @@ static const GLfloat gl_verts[] = { - (void)releaseLayer { DBG_PRINT("MyNSOpenGLLayer::releaseLayer.0: %p (refcnt %d)\n", self, (int)[self retainCount]); + [self setGLEnabled: NO]; [self disableAnimation]; pthread_mutex_lock(&renderLock); [self deallocPBuffer]; - // [[self openGLContext] release]; if( NULL != glContext ) { [glContext release]; glContext = NULL; @@ -570,16 +573,6 @@ static const GLfloat gl_verts[] = { { DBG_PRINT("MyNSOpenGLLayer::openGLContextForPixelFormat.0: %p (refcnt %d) - pfmt %p, ctx %p, DisplayLink %p\n", self, (int)[self retainCount], pixelFormat, glContext, displayLink); -#ifndef HAS_CADisplayLink - if(NULL != displayLink) { - CVReturn cvres; - DBG_PRINT("MyNSOpenGLLayer::openGLContextForPixelFormat.1: setup DisplayLink %p\n", displayLink); - cvres = CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, [glContext CGLContextObj], [pixelFormat CGLPixelFormatObj]); - if(kCVReturnSuccess != cvres) { - DBG_PRINT("MyNSOpenGLLayer::init %p, CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext failed: %d\n", self, cvres); - } - } -#endif return glContext; } @@ -607,6 +600,9 @@ static const GLfloat gl_verts[] = { GLenum textureTarget; Bool texSizeChanged = [self validateTexSizeWithDedicatedSize]; + if( texSizeChanged ) { + [context update]; + } if( NULL != pbuffer ) { if( texSizeChanged && 0 != textureID ) { @@ -812,29 +808,23 @@ static const GLfloat gl_verts[] = { struct timespec t0, t1, td, td2; timespec_now(&t0); #endif - if(0 < to_micros) { - struct timespec to_abs = lastWaitTime; - timespec_addmicros(&to_abs, to_micros); - #ifdef DBG_SYNC - timespec_subtract(&td, &to_abs, &t0); - fprintf(stderr, ", (%ld) / ", timespec_milliseconds(&td)); - #endif - wr = pthread_cond_timedwait(&renderSignal, &renderLock, &to_abs); - #ifdef DBG_SYNC - timespec_now(&t1); - timespec_subtract(&td, &t1, &t0); - timespec_subtract(&td2, &t1, &lastWaitTime); - fprintf(stderr, "(%ld) / (%ld) ms", timespec_milliseconds(&td), timespec_milliseconds(&td2)); - #endif - } else { - pthread_cond_wait (&renderSignal, &renderLock); - #ifdef DBG_SYNC - timespec_now(&t1); - timespec_subtract(&td, &t1, &t0); - timespec_subtract(&td2, &t1, &lastWaitTime); - fprintf(stderr, "(%ld) / (%ld) ms", timespec_milliseconds(&td), timespec_milliseconds(&td2)); - #endif + if( 0 >= to_micros ) { + to_micros = 16666 + 1000; // defaults to 1/60s + 1ms + NSLog(@"MyNSOpenGLContext::waitUntilRenderSignal: to_micros was zero, using defaults"); } + struct timespec to_abs = lastWaitTime; + timespec_addmicros(&to_abs, to_micros); + #ifdef DBG_SYNC + timespec_subtract(&td, &to_abs, &t0); + fprintf(stderr, ", (%ld) / ", timespec_milliseconds(&td)); + #endif + wr = pthread_cond_timedwait(&renderSignal, &renderLock, &to_abs); + #ifdef DBG_SYNC + timespec_now(&t1); + timespec_subtract(&td, &t1, &t0); + timespec_subtract(&td2, &t1, &lastWaitTime); + fprintf(stderr, "(%ld) / (%ld) ms", timespec_milliseconds(&td), timespec_milliseconds(&td2)); + #endif ready = YES; } } while (NO == ready && 0 == wr) ; @@ -923,9 +913,16 @@ void setNSOpenGLLayerNeedsDisplayPBuffer(NSOpenGLLayer* layer, NSOpenGLPixelBuff void releaseNSOpenGLLayer(NSOpenGLLayer* layer) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; MyNSOpenGLLayer* l = (MyNSOpenGLLayer*) layer; + + [CATransaction begin]; + [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; + DBG_PRINT("MyNSOpenGLLayer::releaseNSOpenGLLayer.0: %p\n", l); [l releaseLayer]; DBG_PRINT("MyNSOpenGLLayer::releaseNSOpenGLLayer.X: %p\n", l); + + [CATransaction commit]; + [pool release]; } diff --git a/src/jogl/native/macosx/MacOSXWindowSystemInterface.m b/src/jogl/native/macosx/MacOSXWindowSystemInterface.m index f8faeb8d0..38f1789e3 100644 --- a/src/jogl/native/macosx/MacOSXWindowSystemInterface.m +++ b/src/jogl/native/macosx/MacOSXWindowSystemInterface.m @@ -501,6 +501,28 @@ NSView* getNSView(NSOpenGLContext* ctx) { return view; } +static Bool lockViewIfReady(NSView *view) { + Bool viewReady = false; + + if (view != nil) { + if ([view lockFocusIfCanDraw] == NO) { + DBG_PRINT("lockViewIfReady.1 [view lockFocusIfCanDraw] failed\n"); + } else { + NSRect frame = [view frame]; + if ((frame.size.width == 0) || (frame.size.height == 0)) { + [view unlockFocus]; + DBG_PRINT("lockViewIfReady.2 view.frame size %dx%d\n", (int)frame.size.width, (int)frame.size.height); + } else { + DBG_PRINT("lockViewIfReady.X ready and locked\n"); + viewReady = true; + } + } + } else { + DBG_PRINT("lockViewIfReady.3 nil view\n"); + } + return viewReady; +} + NSOpenGLContext* createContext(NSOpenGLContext* share, NSView* view, Bool incompleteView, @@ -515,40 +537,18 @@ NSOpenGLContext* createContext(NSOpenGLContext* share, DBG_PRINT("createContext.0: share %p, view %p, incompleteView %d, pixfmt %p, opaque %d\n", share, view, (int)incompleteView, fmt, opaque); - if (view != nil) { - Bool viewReady = true; - - if(!incompleteView) { - if ([view lockFocusIfCanDraw] == NO) { - DBG_PRINT("createContext.1 [view lockFocusIfCanDraw] failed\n"); - viewReady = false; - } - } - if(viewReady) { - NSRect frame = [view frame]; - if ((frame.size.width == 0) || (frame.size.height == 0)) { - if(!incompleteView) { - [view unlockFocus]; - } - DBG_PRINT("createContext.2 view.frame size %dx%d\n", (int)frame.size.width, (int)frame.size.height); - viewReady = false; - } - } + Bool viewReadyAndLocked = incompleteView ? false : lockViewIfReady(view); - if (!viewReady) - { - if (viewNotReady != NULL) - { - *viewNotReady = 1; - } + if (nil != viewNotReady) { + *viewNotReady = 1; + } - // the view is not ready yet - DBG_PRINT("createContext.X: view not ready yet\n"); - [pool release]; - return NULL; - } + if (nil != view && !incompleteView && !viewReadyAndLocked) { + DBG_PRINT("createContext.X: Assumed complete view not ready yet\n"); + [pool release]; + return NULL; } - + NSOpenGLContext* ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:share]; if ( nil != ctx && nil != view ) { @@ -556,10 +556,8 @@ NSOpenGLContext* createContext(NSOpenGLContext* share, GLint zeroOpacity = 0; [ctx setValues:&zeroOpacity forParameter:NSOpenGLCPSurfaceOpacity]; } - if(!incompleteView) { - DBG_PRINT("createContext.3.0: setView\n"); + if( viewReadyAndLocked ) { [ctx setView:view]; - DBG_PRINT("createContext.3.X: setView\n"); [view unlockFocus]; } } @@ -571,8 +569,19 @@ NSOpenGLContext* createContext(NSOpenGLContext* share, void setContextView(NSOpenGLContext* ctx, NSView* view) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; - if ( nil != ctx && nil != view ) { - [ctx setView:view]; + if ( nil != ctx ) { + if ( nil != view ) { + Bool viewReadyAndLocked = lockViewIfReady(view); + DBG_PRINT("setContextView.0: ctx %p, view %p: setView: %d\n", ctx, view, viewReadyAndLocked); + if( viewReadyAndLocked ) { + [ctx setView:view]; + [view unlockFocus]; + } + } else { + DBG_PRINT("setContextView.1: ctx %p, view nil: clearDrawable\n", ctx); + [ctx clearDrawable]; + } + DBG_PRINT("setContextView.X\n"); } [pool release]; } @@ -725,7 +734,7 @@ void setContextTextureImageToPBuffer(NSOpenGLContext* ctx, NSOpenGLPixelBuffer* Bool isNSOpenGLPixelBuffer(uint64_t object) { NSObject *nsObj = (NSObject*) (intptr_t) object; DBG_PRINT("isNSOpenGLPixelBuffer.0: obj %p\n", object); - Bool res = [nsObj isMemberOfClass:[NSOpenGLPixelBuffer class]]; + Bool res = [nsObj isKindOfClass:[NSOpenGLPixelBuffer class]]; DBG_PRINT("isNSOpenGLPixelBuffer.X: res %d\n", (int)res); return res; } diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java b/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java index 3e5e629b6..15a0f550f 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java @@ -134,6 +134,13 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, } protected synchronized void invalidate() { + if(DEBUG) { + System.err.println("JAWTWindow.invalidate()"); + // Thread.dumpStack(); + } + if( isSurfaceLayerAttached() ) { + detachSurfaceLayer(); + } invalidateNative(); jawt = null; isOffscreenLayerSurface = false; diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java index 758105713..d11d24664 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java @@ -74,13 +74,16 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { System.err.println("MacOSXJAWTWindow.invalidateNative(): osh-enabled "+isOffscreenLayerSurfaceEnabled()+ ", osh-set "+offscreenSurfaceHandleSet+ ", osh "+toHexString(offscreenSurfaceHandle)+ - ", rsh "+toHexString(rootSurfaceLayerHandle)+ - ", wh "+toHexString(windowHandle)); + ", rsh "+toHexString(rootSurfaceLayer)+ + ", wh "+toHexString(windowHandle)+" - "+Thread.currentThread().getName()); } offscreenSurfaceHandle=0; offscreenSurfaceHandleSet=false; if( isOffscreenLayerSurfaceEnabled() ) { - if(0 != rootSurfaceLayerHandle) { + if(0 != windowHandle) { + OSXUtil.DestroyNSWindow(windowHandle); + } + if(0 != rootSurfaceLayer) { final JAWT jawt = getJAWT(); if( null != jawt ) { final JAWT_DrawingSurface ds = jawt.GetDrawingSurface(component); @@ -90,9 +93,9 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { try { dsi = ds.GetDrawingSurfaceInfo(); try { - UnsetJAWTRootSurfaceLayer(dsi.getBuffer(), rootSurfaceLayerHandle); + UnsetJAWTRootSurfaceLayer0(dsi.getBuffer(), rootSurfaceLayer); } catch (Exception e) { - System.err.println("Error clearing JAWT rootSurfaceLayerHandle "+toHexString(rootSurfaceLayerHandle)); + System.err.println("Error clearing JAWT rootSurfaceLayerHandle "+toHexString(rootSurfaceLayer)); e.printStackTrace(); } } finally { @@ -105,19 +108,15 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { jawt.FreeDrawingSurface(ds); } } - OSXUtil.DestroyCALayer(rootSurfaceLayerHandle); - - rootSurfaceLayerHandle = 0; - } - if(0 != windowHandle) { - OSXUtil.DestroyNSWindow(windowHandle); + OSXUtil.DestroyCALayer(rootSurfaceLayer); + rootSurfaceLayer = 0; } } windowHandle=0; } - + protected void attachSurfaceLayerImpl(final long layerHandle) { - OSXUtil.AddCASublayer(rootSurfaceLayerHandle, layerHandle, getWidth(), getHeight()); + OSXUtil.AddCASublayer(rootSurfaceLayer, layerHandle, getWidth(), getHeight()); } protected void layoutSurfaceLayerImpl() { @@ -127,11 +126,11 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { if(DEBUG) { System.err.println("JAWTWindow.fixSurfaceLayerLayout: "+toHexString(osl) + ", bounds "+bounds+", "+w+"x"+h); } - OSXUtil.FixCALayerLayout(rootSurfaceLayerHandle, osl, w, h); + OSXUtil.FixCALayerLayout(rootSurfaceLayer, osl, w, h); } protected void detachSurfaceLayerImpl(final long layerHandle) { - OSXUtil.RemoveCASublayer(rootSurfaceLayerHandle, layerHandle); + OSXUtil.RemoveCASublayer(rootSurfaceLayer, layerHandle); } @Override @@ -243,23 +242,23 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { } } if(null == errMsg) { - if(0 == rootSurfaceLayerHandle) { - rootSurfaceLayerHandle = OSXUtil.CreateCALayer(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight()); - if(0 == rootSurfaceLayerHandle) { + if(0 == rootSurfaceLayer) { + rootSurfaceLayer = OSXUtil.CreateCALayer(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight()); + if(0 == rootSurfaceLayer) { errMsg = "Could not create root CALayer"; } else { try { - SetJAWTRootSurfaceLayer(dsi.getBuffer(), rootSurfaceLayerHandle); + SetJAWTRootSurfaceLayer0(dsi.getBuffer(), rootSurfaceLayer); } catch(Exception e) { - errMsg = "Could not set JAWT rootSurfaceLayerHandle "+toHexString(rootSurfaceLayerHandle)+", cause: "+e.getMessage(); + errMsg = "Could not set JAWT rootSurfaceLayerHandle "+toHexString(rootSurfaceLayer)+", cause: "+e.getMessage(); } } } } if(null != errMsg) { - if(0 != rootSurfaceLayerHandle) { - OSXUtil.DestroyCALayer(rootSurfaceLayerHandle); - rootSurfaceLayerHandle = 0; + if(0 != rootSurfaceLayer) { + OSXUtil.DestroyCALayer(rootSurfaceLayer); + rootSurfaceLayer = 0; } if(0 != windowHandle) { OSXUtil.DestroyNSWindow(windowHandle); @@ -316,18 +315,11 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { /** * Set the given root CALayer in the JAWT surface */ - private static void SetJAWTRootSurfaceLayer(final Buffer jawtDrawingSurfaceInfoBuffer, final long caLayer) { - SetJAWTRootSurfaceLayer0(jawtDrawingSurfaceInfoBuffer, caLayer); - } - + private static native void SetJAWTRootSurfaceLayer0(Buffer jawtDrawingSurfaceInfoBuffer, long caLayer); + /** - * Unset the given root CALayer in the JAWT surface + * Unset the given root CALayer in the JAWT surface, passing the NIO DrawingSurfaceInfo buffer */ - private static void UnsetJAWTRootSurfaceLayer(final Buffer jawtDrawingSurfaceInfoBuffer, final long caLayer) { - UnsetJAWTRootSurfaceLayer0(jawtDrawingSurfaceInfoBuffer, caLayer); - } - - private static native void SetJAWTRootSurfaceLayer0(Buffer jawtDrawingSurfaceInfoBuffer, long caLayer); private static native void UnsetJAWTRootSurfaceLayer0(Buffer jawtDrawingSurfaceInfoBuffer, long caLayer); // Variables for lockSurface/unlockSurface @@ -337,7 +329,7 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { private JAWT_MacOSXDrawingSurfaceInfo macosxdsi; - private long rootSurfaceLayerHandle = 0; // attached to the JAWT_SurfaceLayer + private long rootSurfaceLayer = 0; // attached to the JAWT_SurfaceLayer private long windowHandle = 0; private long offscreenSurfaceHandle = 0; diff --git a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java index 1563a5a8b..703c3d972 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java @@ -83,11 +83,11 @@ public class OSXUtil implements ToolkitProperties { public static final boolean hasThreadingIssues() { return false; } public static boolean isNSView(long object) { - return isNSView0(object); + return 0 != object ? isNSView0(object) : false; } public static boolean isNSWindow(long object) { - return isNSWindow0(object); + return 0 != object ? isNSWindow0(object) : false; } /** @@ -164,8 +164,7 @@ public class OSXUtil implements ToolkitProperties { } RunOnMainThread(false, new Runnable() { public void run() { - AddCASublayer0(rootCALayer, subCALayer); - FixCALayerLayout0(rootCALayer, subCALayer, width, height); + AddCASublayer0(rootCALayer, subCALayer, width, height); } }); } @@ -194,24 +193,32 @@ public class OSXUtil implements ToolkitProperties { } /** - * Detach a sub CALayer from the root CALayer + * Detach a sub CALayer from the root CALayer on the main-thread w/o blocking. */ public static void RemoveCASublayer(final long rootCALayer, final long subCALayer) { if(0==rootCALayer || 0==subCALayer) { throw new IllegalArgumentException("rootCALayer 0x"+Long.toHexString(rootCALayer)+", subCALayer 0x"+Long.toHexString(subCALayer)); } - RemoveCASublayer0(rootCALayer, subCALayer); + RunOnMainThread(false, new Runnable() { + public void run() { + RemoveCASublayer0(rootCALayer, subCALayer); + } + }); } /** - * Destroy a CALayer + * Destroy a CALayer on main-thread w/o blocking. * @see #CreateCALayer(int, int, int, int) */ public static void DestroyCALayer(final long caLayer) { if(0==caLayer) { throw new IllegalArgumentException("caLayer 0x"+Long.toHexString(caLayer)); } - DestroyCALayer0(caLayer); + RunOnMainThread(false, new Runnable() { + public void run() { + DestroyCALayer0(caLayer); + } + }); } /** @@ -337,7 +344,7 @@ public class OSXUtil implements ToolkitProperties { private static native long GetNSView0(long nsWindow); private static native long GetNSWindow0(long nsView); private static native long CreateCALayer0(int x, int y, int width, int height); - private static native void AddCASublayer0(long rootCALayer, long subCALayer); + private static native void AddCASublayer0(long rootCALayer, long subCALayer, int width, int height); private static native void FixCALayerLayout0(long rootCALayer, long subCALayer, int width, int height); private static native void RemoveCASublayer0(long rootCALayer, long subCALayer); private static native void DestroyCALayer0(long caLayer); diff --git a/src/nativewindow/native/macosx/OSXmisc.m b/src/nativewindow/native/macosx/OSXmisc.m index 14a9781f7..81dcfa959 100644 --- a/src/nativewindow/native/macosx/OSXmisc.m +++ b/src/nativewindow/native/macosx/OSXmisc.m @@ -127,13 +127,17 @@ Java_jogamp_nativewindow_macosx_OSXUtil_initIDs0(JNIEnv *env, jclass _unused) { JNIEXPORT jboolean JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_isNSView0(JNIEnv *env, jclass _unused, jlong object) { NSObject *nsObj = (NSObject*) (intptr_t) object; - return [nsObj isMemberOfClass:[NSView class]]; + jboolean u = [nsObj isKindOfClass:[NSView class]]; + DBG_PRINT( "isNSView(obj: %p): %s -> %d\n", nsObj, [[nsObj description] UTF8String], u); + return u; } JNIEXPORT jboolean JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_isNSWindow0(JNIEnv *env, jclass _unused, jlong object) { NSObject *nsObj = (NSObject*) (intptr_t) object; - return [nsObj isMemberOfClass:[NSWindow class]]; + jboolean u = [nsObj isKindOfClass:[NSWindow class]]; + DBG_PRINT( "isNSWindow(obj: %p): %s -> %d\n", nsObj, [[nsObj description] UTF8String], u); + return u; } /* @@ -425,23 +429,56 @@ JNIEXPORT jlong JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_CreateCALayer0 return (jlong) ((intptr_t) layer); } +static void FixCALayerLayout0(MyCALayer* rootLayer, CALayer* subLayer, jint width, jint height) { + if( NULL != rootLayer ) { + CGRect lRect = [rootLayer frame]; + if(lRect.origin.x!=0 || lRect.origin.y!=0 || lRect.size.width!=width || lRect.size.height!=height) { + DBG_PRINT("CALayer::FixCALayerLayout0.0: Root %p exp 0/0 %dx%d -> frame0: %lf/%lf %lfx%lf\n", + rootLayer, (int)width, (int)height, lRect.origin.x, lRect.origin.y, lRect.size.width, lRect.size.height); + lRect.origin.x = 0; + lRect.origin.y = 0; + lRect.size.width = width; + lRect.size.height = height; + [rootLayer setFrame: lRect]; + } + } + if( NULL != subLayer ) { + CGRect lRect = [subLayer frame]; + if(lRect.origin.x!=0 || lRect.origin.y!=0 || lRect.size.width!=width || lRect.size.height!=height) { + DBG_PRINT("CALayer::FixCALayerLayout0.0: SubL %p exp 0/0 %dx%d -> frame0: %lf/%lf %lfx%lf\n", + subLayer, (int)width, (int)height, lRect.origin.x, lRect.origin.y, lRect.size.width, lRect.size.height); + lRect.origin.x = 0; + lRect.origin.y = 0; + lRect.size.width = width; + lRect.size.height = height; + if( [subLayer conformsToProtocol:@protocol(NWDedicatedSize)] ) { + CALayer <NWDedicatedSize> * subLayerDS = (CALayer <NWDedicatedSize> *) subLayer; + [subLayerDS setDedicatedSize: lRect.size]; + } else { + [subLayer setFrame: lRect]; + } + } + } +} + /* * Class: Java_jogamp_nativewindow_macosx_OSXUtil * Method: AddCASublayer0 - * Signature: (JJ)V + * Signature: (JJII)V */ JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_AddCASublayer0 - (JNIEnv *env, jclass unused, jlong rootCALayer, jlong subCALayer) + (JNIEnv *env, jclass unused, jlong rootCALayer, jlong subCALayer, jint width, jint height) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; MyCALayer* rootLayer = (MyCALayer*) ((intptr_t) rootCALayer); CALayer* subLayer = (CALayer*) ((intptr_t) subCALayer); - [subLayer retain]; // Pairs w/ RemoveCASublayer - [CATransaction begin]; [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; + [rootLayer retain]; // Pairs w/ RemoveCASublayer + [subLayer retain]; // Pairs w/ RemoveCASublayer + CGRect lRectRoot = [rootLayer frame]; DBG_PRINT("CALayer::AddCASublayer0.0: Origin %p frame0: %lf/%lf %lfx%lf\n", rootLayer, lRectRoot.origin.x, lRectRoot.origin.y, lRectRoot.size.width, lRectRoot.size.height); @@ -471,11 +508,7 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_AddCASublayer0 [subLayer setAutoresizingMask: (kCALayerWidthSizable|kCALayerHeightSizable)]; [subLayer setNeedsDisplayOnBoundsChange: YES]; - // Trigger display and hence ctx creation. - // The latter is essential since since the parent-context lock is cleared - // only for this window of time (method call). - [rootLayer setNeedsDisplay]; - [rootLayer displayIfNeeded]; + FixCALayerLayout0(rootLayer, subLayer, width, height); [CATransaction commit]; @@ -499,35 +532,7 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_FixCALayerLayout0 [CATransaction begin]; [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; - if( NULL != rootLayer ) { - CGRect lRect = [rootLayer frame]; - if(lRect.origin.x!=0 || lRect.origin.y!=0 || lRect.size.width!=width || lRect.size.height!=height) { - DBG_PRINT("CALayer::FixCALayerLayout0.0: Root %p exp 0/0 %dx%d -> frame0: %lf/%lf %lfx%lf\n", - rootLayer, (int)width, (int)height, lRect.origin.x, lRect.origin.y, lRect.size.width, lRect.size.height); - lRect.origin.x = 0; - lRect.origin.y = 0; - lRect.size.width = width; - lRect.size.height = height; - [rootLayer setFrame: lRect]; - } - } - if( NULL != subLayer ) { - CGRect lRect = [subLayer frame]; - if(lRect.origin.x!=0 || lRect.origin.y!=0 || lRect.size.width!=width || lRect.size.height!=height) { - DBG_PRINT("CALayer::FixCALayerLayout0.0: SubL %p exp 0/0 %dx%d -> frame0: %lf/%lf %lfx%lf\n", - subLayer, (int)width, (int)height, lRect.origin.x, lRect.origin.y, lRect.size.width, lRect.size.height); - lRect.origin.x = 0; - lRect.origin.y = 0; - lRect.size.width = width; - lRect.size.height = height; - if( [subLayer conformsToProtocol:@protocol(NWDedicatedSize)] ) { - CALayer <NWDedicatedSize> * subLayerDS = (CALayer <NWDedicatedSize> *) subLayer; - [subLayerDS setDedicatedSize: lRect.size]; - } else { - [subLayer setFrame: lRect]; - } - } - } + FixCALayerLayout0(rootLayer, subLayer, width, height); [CATransaction commit]; @@ -556,6 +561,7 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_RemoveCASublayer0 [subLayer removeFromSuperlayer]; [subLayer release]; // Pairs w/ AddCASublayer + [rootLayer release]; // Pairs w/ AddCASublayer [CATransaction commit]; @@ -575,8 +581,14 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_DestroyCALayer0 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; MyCALayer* layer = (MyCALayer*) ((intptr_t) caLayer); + [CATransaction begin]; + [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; + DBG_PRINT("CALayer::DestroyCALayer0.0: root %p (refcnt %d)\n", layer, (int)[layer retainCount]); - [layer release]; // Trigger release of root CALayer + [layer release]; // Trigger release and dealloc of root CALayer, it's child etc .. + + [CATransaction commit]; + [pool release]; DBG_PRINT("CALayer::DestroyCALayer0.X: root %p\n", layer); } @@ -590,6 +602,10 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_jawt_macosx_MacOSXJAWTWindow_Set (JNIEnv *env, jclass unused, jobject jawtDrawingSurfaceInfoBuffer, jlong caLayer) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + [CATransaction begin]; + [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; + JAWT_DrawingSurfaceInfo* dsi = (JAWT_DrawingSurfaceInfo*) (*env)->GetDirectBufferAddress(env, jawtDrawingSurfaceInfoBuffer); if (NULL == dsi) { NativewindowCommon_throwNewRuntimeException(env, "Argument \"jawtDrawingSurfaceInfoBuffer\" was not a direct buffer"); @@ -598,7 +614,10 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_jawt_macosx_MacOSXJAWTWindow_Set MyCALayer* layer = (MyCALayer*) (intptr_t) caLayer; id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)dsi->platformInfo; DBG_PRINT("CALayer::SetJAWTRootSurfaceLayer.0: pre %p -> root %p (refcnt %d)\n", surfaceLayers.layer, layer, (int)[layer retainCount]); - surfaceLayers.layer = [layer retain]; // Pairs w/ Unset + [surfaceLayers setLayer: [layer retain]]; // Pairs w/ Unset + + [CATransaction commit]; + [pool release]; DBG_PRINT("CALayer::SetJAWTRootSurfaceLayer.X: root %p (refcnt %d)\n", layer, (int)[layer retainCount]); } @@ -612,6 +631,10 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_jawt_macosx_MacOSXJAWTWindow_Uns (JNIEnv *env, jclass unused, jobject jawtDrawingSurfaceInfoBuffer, jlong caLayer) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + [CATransaction begin]; + [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; + JAWT_DrawingSurfaceInfo* dsi = (JAWT_DrawingSurfaceInfo*) (*env)->GetDirectBufferAddress(env, jawtDrawingSurfaceInfoBuffer); if (NULL == dsi) { NativewindowCommon_throwNewRuntimeException(env, "Argument \"jawtDrawingSurfaceInfoBuffer\" was not a direct buffer"); @@ -625,7 +648,10 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_jawt_macosx_MacOSXJAWTWindow_Uns } DBG_PRINT("CALayer::UnsetJAWTRootSurfaceLayer.0: root %p (refcnt %d) -> nil\n", layer, (int)[layer retainCount]); [layer release]; // Pairs w/ Set - surfaceLayers.layer = NULL; + [surfaceLayers setLayer: NULL]; + + [CATransaction commit]; + [pool release]; DBG_PRINT("CALayer::UnsetJAWTRootSurfaceLayer.X: root %p (refcnt %d) -> nil\n", layer, (int)[layer retainCount]); } @@ -735,8 +761,10 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_RunOnMainThread0 DBG_PRINT2( "RunOnMainThread0.1.0\n"); MainRunnable * mr = [[MainRunnable alloc] initWithRunnable: runnableObj jvmHandle: jvmHandle jvmVersion: jvmVersion]; + [mr performSelectorOnMainThread:@selector(jRun) withObject:nil waitUntilDone:NO]; DBG_PRINT2( "RunOnMainThread0.1.1\n"); + [mr release]; DBG_PRINT2( "RunOnMainThread0.1.2\n"); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove01GLCanvasSwingAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove01GLCanvasSwingAWT.java index ce8f9adc8..48c3c89b3 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove01GLCanvasSwingAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove01GLCanvasSwingAWT.java @@ -51,17 +51,21 @@ import org.junit.BeforeClass; import org.junit.Test; import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; +import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.UITestCase; public class TestAddRemove01GLCanvasSwingAWT extends UITestCase { static long durationPerTest = 50; static int addRemoveCount = 15; + static int pauseEach = 0; + static int pauseDuration = 500; static boolean noOnscreenTest = false; static boolean noOffscreenTest = false; static boolean shallUseOffscreenPBufferLayer = false; static GLProfile glp; static int width, height; static boolean waitForKey = false; + static boolean waitForKeyPost = false; @BeforeClass public static void initClass() { @@ -167,9 +171,19 @@ public class TestAddRemove01GLCanvasSwingAWT extends UITestCase { } while ( ( System.currentTimeMillis() - t0 ) < durationPerTest ) ; System.err.println("GLCanvas isOffscreenLayerSurfaceEnabled: "+glc.isOffscreenLayerSurfaceEnabled()+": "+glc.getChosenGLCapabilities()); + + dispose(top[0]); - dispose(top[0]); + if( 0 < pauseEach && 0 == i % pauseEach ) { + System.err.println("******* P A U S E - Start ********"); + // OSXUtil.WaitUntilFinish(); + Thread.sleep(pauseDuration); + System.err.println("******* P A U S E - End ********"); + } } + if(waitForKeyPost) { + UITestCase.waitForKey("End"); + } } @Test @@ -213,9 +227,13 @@ public class TestAddRemove01GLCanvasSwingAWT extends UITestCase { } catch (Exception ex) { ex.printStackTrace(); } } else if(args[i].equals("-loops")) { i++; - try { - addRemoveCount = Integer.parseInt(args[i]); - } catch (Exception ex) { ex.printStackTrace(); } + addRemoveCount = MiscUtils.atoi(args[i], addRemoveCount); + } else if(args[i].equals("-pauseEach")) { + i++; + pauseEach = MiscUtils.atoi(args[i], pauseEach); + } else if(args[i].equals("-pauseDuration")) { + i++; + pauseDuration = MiscUtils.atoi(args[i], pauseDuration); } else if(args[i].equals("-noOnscreen")) { noOnscreenTest = true; } else if(args[i].equals("-noOffscreen")) { @@ -224,11 +242,15 @@ public class TestAddRemove01GLCanvasSwingAWT extends UITestCase { shallUseOffscreenPBufferLayer = true; } else if(args[i].equals("-wait")) { waitForKey = true; - } + } else if(args[i].equals("-waitPost")) { + waitForKeyPost = true; + } } System.err.println("waitForKey "+waitForKey); System.err.println("addRemoveCount "+addRemoveCount); + System.err.println("pauseEach "+pauseEach); + System.err.println("pauseDuration "+pauseDuration); System.err.println("noOnscreenTest "+noOnscreenTest); System.err.println("noOffscreenTest "+noOffscreenTest); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove02GLWindowNewtCanvasAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove02GLWindowNewtCanvasAWT.java index 3d78943f9..ce88abfba 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove02GLWindowNewtCanvasAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove02GLWindowNewtCanvasAWT.java @@ -53,11 +53,14 @@ import org.junit.Test; import com.jogamp.newt.awt.NewtCanvasAWT; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; +import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.UITestCase; public class TestAddRemove02GLWindowNewtCanvasAWT extends UITestCase { static long durationPerTest = 50; static int addRemoveCount = 15; + static int pauseEach = 0; + static int pauseDuration = 500; static boolean noOnscreenTest = false; static boolean noOffscreenTest = false; static boolean shallUseOffscreenPBufferLayer = false; @@ -174,6 +177,11 @@ public class TestAddRemove02GLWindowNewtCanvasAWT extends UITestCase { dispose(top[0]); glw.destroy(); + + if( 0 < pauseEach && 0 == i % pauseEach ) { + System.err.println("******* P A U S E ********"); + Thread.sleep(pauseDuration); + } } } @@ -218,9 +226,13 @@ public class TestAddRemove02GLWindowNewtCanvasAWT extends UITestCase { } catch (Exception ex) { ex.printStackTrace(); } } else if(args[i].equals("-loops")) { i++; - try { - addRemoveCount = Integer.parseInt(args[i]); - } catch (Exception ex) { ex.printStackTrace(); } + addRemoveCount = MiscUtils.atoi(args[i], addRemoveCount); + } else if(args[i].equals("-pauseEach")) { + i++; + pauseEach = MiscUtils.atoi(args[i], pauseEach); + } else if(args[i].equals("-pauseDuration")) { + i++; + pauseDuration = MiscUtils.atoi(args[i], pauseDuration); } else if(args[i].equals("-noOnscreen")) { noOnscreenTest = true; } else if(args[i].equals("-noOffscreen")) { @@ -234,6 +246,8 @@ public class TestAddRemove02GLWindowNewtCanvasAWT extends UITestCase { System.err.println("waitForKey "+waitForKey); System.err.println("addRemoveCount "+addRemoveCount); + System.err.println("pauseEach "+pauseEach); + System.err.println("pauseDuration "+pauseDuration); System.err.println("noOnscreenTest "+noOnscreenTest); System.err.println("noOffscreenTest "+noOffscreenTest); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove03GLWindowNEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove03GLWindowNEWT.java index 9d7a4026b..981b14af3 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove03GLWindowNEWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestAddRemove03GLWindowNEWT.java @@ -41,11 +41,14 @@ import org.junit.Test; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; +import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.UITestCase; public class TestAddRemove03GLWindowNEWT extends UITestCase { static long durationPerTest = 50; static int addRemoveCount = 15; + static int pauseEach = 0; + static int pauseDuration = 500; static GLProfile glp; static int width, height; static boolean waitForKey = false; @@ -91,6 +94,11 @@ public class TestAddRemove03GLWindowNEWT extends UITestCase { System.err.println("GLWindow: "+glw.getChosenGLCapabilities()); glw.destroy(); + + if( 0 < pauseEach && 0 == i % pauseEach ) { + System.err.println("******* P A U S E ********"); + Thread.sleep(pauseDuration); + } } } @@ -111,9 +119,13 @@ public class TestAddRemove03GLWindowNEWT extends UITestCase { } catch (Exception ex) { ex.printStackTrace(); } } else if(args[i].equals("-loops")) { i++; - try { - addRemoveCount = Integer.parseInt(args[i]); - } catch (Exception ex) { ex.printStackTrace(); } + addRemoveCount = MiscUtils.atoi(args[i], addRemoveCount); + } else if(args[i].equals("-pauseEach")) { + i++; + pauseEach = MiscUtils.atoi(args[i], pauseEach); + } else if(args[i].equals("-pauseDuration")) { + i++; + pauseDuration = MiscUtils.atoi(args[i], pauseDuration); } else if(args[i].equals("-wait")) { waitForKey = true; } @@ -121,6 +133,8 @@ public class TestAddRemove03GLWindowNEWT extends UITestCase { System.err.println("waitForKey "+waitForKey); System.err.println("addRemoveCount "+addRemoveCount); + System.err.println("pauseEach "+pauseEach); + System.err.println("pauseDuration "+pauseDuration); if(waitForKey) { UITestCase.waitForKey("Start"); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT.java index 0673e2f45..0b70cf151 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT.java @@ -64,6 +64,7 @@ import com.jogamp.opengl.test.junit.util.UITestCase; public class TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT extends UITestCase { static final int widthStep = 800/4; static final int heightStep = 600/4; + static boolean waitForKey = false; volatile int szStep = 2; static GLCapabilities getCaps(String profile) { @@ -331,6 +332,14 @@ public class TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT extends UITestCase { } public static void main(String args[]) throws IOException { + for(int i=0; i<args.length; i++) { + if(args[i].equals("-wait")) { + waitForKey = true; + } + } + if(waitForKey) { + UITestCase.waitForKey("Start"); + } org.junit.runner.JUnitCore.main(TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT.class.getName()); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java index 47891a8df..14b1f04b0 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java @@ -28,9 +28,7 @@ package com.jogamp.opengl.test.junit.jogl.demos.es2.newt; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import com.jogamp.newt.Display; import com.jogamp.newt.NewtFactory; @@ -130,11 +128,7 @@ public class TestGearsES2NEWT extends UITestCase { public void display(GLAutoDrawable drawable) { GLAnimatorControl actrl = drawable.getAnimator(); if(waitForKey && actrl.getTotalFPSFrames() == 60*3) { - BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); - System.err.println("Press enter to continue"); - try { - System.err.println(stdin.readLine()); - } catch (IOException e) { } + UITestCase.waitForKey("3s mark"); actrl.resetFPSCounter(); waitForKey = false; } @@ -418,11 +412,7 @@ public class TestGearsES2NEWT extends UITestCase { System.err.println("exclusiveContext "+exclusiveContext); if(waitForKey) { - BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); - System.err.println("Press enter to continue"); - try { - System.err.println(stdin.readLine()); - } catch (IOException e) { } + UITestCase.waitForKey("Start"); } org.junit.runner.JUnitCore.main(TestGearsES2NEWT.class.getName()); } |