From f354fb204d8973453c538dda78a2c82c87be61dc Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 19 Mar 2013 00:22:28 +0100 Subject: OSX/CALayer: OSX/CALayer Threading Part3 - Run CALayer ops in a streaming design on main-thread w/o [infinitive] blocking History: Part1 commit 896e8b021b39e9415040a57a1d540d7d24b02db1 (Run CALayer Ops on current thread to avoid blocking) Part2 commit 28c6472335b924080d638b33a28f8f4eedb459b1 (Run CALayer Ops on main-thread w/o blocking) Dependency: GlueGen commit 4becdfa125b07ff969d6540e1112735b53cd15eb (Fix RecursiveLockImpl* Timeout corner case) Part2 misses essential locking of the OpenGL context (and it's surface upfront) while creating the NSOpenGLLayer instance. The latter instantiates a OpenGL context shared w/ JOGL's, hence it cannot be locked. Encapsulating NSOpenGLLayer creation/attachment and it's detachment/release in sub-classes AttachNSOpenGLLayer and DetachNSOpenGLLayer, where instances will be streamed on main-thread. Both tasks are triggered at associateDrawable(boolean bound). The mentioned GL context locking requires disturbs the 'streaming' design considerably in AttachNSOpenGLLayer. It is solved by attempt to acquire the recursive lock of the surface and the context via 'tryLock(maxwait)' w/ screen-vSync-period/2. If the locks could not be acquired completly, the AttachNSOpenGLLayer instance will be re-queued to the main-thread for later execution. Before DetachNSOpenGLLayer is being streamed, it is validated whether AttachNSOpenGLLayer did run. A recursive situation does happen w/ resizing an offscreen pbuffer drawable! Hence extra care is being taken. --- .../com/jogamp/nativewindow/awt/JAWTWindow.java | 73 +++++++++++----------- .../media/nativewindow/OffscreenLayerSurface.java | 5 ++ .../nativewindow/jawt/macosx/MacOSXJAWTWindow.java | 58 ++++++++++------- .../jawt/windows/WindowsJAWTWindow.java | 7 --- .../nativewindow/jawt/x11/X11JAWTWindow.java | 7 --- .../jogamp/nativewindow/macosx/OSXUtil.java | 64 ++++++++++++++----- 6 files changed, 127 insertions(+), 87 deletions(-) (limited to 'src/nativewindow/classes') diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java b/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java index 15a0f550f..16d4a07ef 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java @@ -85,7 +85,7 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, protected long drawable; protected Rectangle bounds; protected Insets insets; - private long offscreenSurfaceLayer; + private volatile long offscreenSurfaceLayer; private long drawable_old; @@ -135,12 +135,12 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, protected synchronized void invalidate() { if(DEBUG) { - System.err.println("JAWTWindow.invalidate()"); + System.err.println("JAWTWindow.invalidate() - "+Thread.currentThread().getName()); + if( isSurfaceLayerAttached() ) { + System.err.println("OffscreenSurfaceLayer still attached: 0x"+Long.toHexString(offscreenSurfaceLayer)); + } // Thread.dumpStack(); } - if( isSurfaceLayerAttached() ) { - detachSurfaceLayer(); - } invalidateNative(); jawt = null; isOffscreenLayerSurface = false; @@ -227,21 +227,16 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, if( !isOffscreenLayerSurfaceEnabled() ) { throw new NativeWindowException("Not an offscreen layer surface"); } - int lockRes = lockSurface(); - if (NativeSurface.LOCK_SURFACE_NOT_READY >= lockRes) { - throw new NativeWindowException("Could not lock (offscreen layer): "+this); - } - try { - if(DEBUG) { - System.err.println("JAWTWindow.attachSurfaceHandle: "+toHexString(layerHandle) + ", bounds "+bounds); - } - attachSurfaceLayerImpl(layerHandle); - offscreenSurfaceLayer = layerHandle; - } finally { - unlockSurface(); + if(DEBUG) { + System.err.println("JAWTWindow.attachSurfaceHandle: "+toHexString(layerHandle) + ", bounds "+bounds); } + attachSurfaceLayerImpl(layerHandle); + offscreenSurfaceLayer = layerHandle; + layoutSurfaceLayerImpl(layerHandle, getWidth(), getHeight()); + } + protected void attachSurfaceLayerImpl(final long layerHandle) { + throw new UnsupportedOperationException("offscreen layer not supported"); } - protected abstract void attachSurfaceLayerImpl(final long layerHandle); /** * Layout the offscreen layer according to the implementing class's constraints. @@ -256,38 +251,39 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, * @see #isOffscreenLayerSurfaceEnabled() * @throws NativeWindowException if {@link #isOffscreenLayerSurfaceEnabled()} == false */ - protected void layoutSurfaceLayerImpl() {} + protected void layoutSurfaceLayerImpl(long layerHandle, int width, int height) {} private final void layoutSurfaceLayerIfEnabled() throws NativeWindowException { if( isOffscreenLayerSurfaceEnabled() && 0 != offscreenSurfaceLayer ) { - layoutSurfaceLayerImpl(); + layoutSurfaceLayerImpl(offscreenSurfaceLayer, getWidth(), getHeight()); } } @Override public final void detachSurfaceLayer() throws NativeWindowException { - if( !isOffscreenLayerSurfaceEnabled() ) { - throw new java.lang.UnsupportedOperationException("Not an offscreen layer surface"); - } if( 0 == offscreenSurfaceLayer) { throw new NativeWindowException("No offscreen layer attached: "+this); } - int lockRes = lockSurface(); - if (NativeSurface.LOCK_SURFACE_NOT_READY >= lockRes) { - throw new NativeWindowException("Could not lock (offscreen layer): "+this); - } - try { - if(DEBUG) { - System.err.println("JAWTWindow.detachSurfaceHandle(): osh "+toHexString(offscreenSurfaceLayer)); - } - detachSurfaceLayerImpl(offscreenSurfaceLayer); - offscreenSurfaceLayer = 0; - } finally { - unlockSurface(); + if(DEBUG) { + System.err.println("JAWTWindow.detachSurfaceHandle(): osh "+toHexString(offscreenSurfaceLayer)); } + detachSurfaceLayerImpl(offscreenSurfaceLayer, detachSurfaceLayerNotify); } - protected abstract void detachSurfaceLayerImpl(final long layerHandle); + private final Runnable detachSurfaceLayerNotify = new Runnable() { + @Override + public void run() { + offscreenSurfaceLayer = 0; + } + }; + + /** + * @param detachNotify Runnable to be called before native detachment + */ + protected void detachSurfaceLayerImpl(final long layerHandle, final Runnable detachNotify) { + throw new UnsupportedOperationException("offscreen layer not supported"); + } + @Override public final long getAttachedSurfaceLayer() { @@ -305,6 +301,11 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, getPrivateGraphicsConfiguration().setChosenCapabilities(caps); } + @Override + public final RecursiveLock getLock() { + return surfaceLock; + } + // // SurfaceUpdateListener // diff --git a/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java b/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java index 8c02a68bb..1826008ad 100644 --- a/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java +++ b/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java @@ -27,6 +27,8 @@ */ package javax.media.nativewindow; +import com.jogamp.common.util.locks.RecursiveLock; + /** * Interface specifying the offscreen layer surface protocol. */ @@ -60,4 +62,7 @@ public interface OffscreenLayerSurface { /** Sets the capabilities of this instance, allowing upstream API's to refine it, i.e. OpenGL related settings. */ public void setChosenCapabilities(CapabilitiesImmutable caps); + /** Returns the recursive lock object of this surface, which synchronizes multithreaded access. */ + public RecursiveLock getLock(); + } diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java index d11d24664..9f76392a9 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java @@ -69,16 +69,18 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { } } + @Override protected void invalidateNative() { if(DEBUG) { System.err.println("MacOSXJAWTWindow.invalidateNative(): osh-enabled "+isOffscreenLayerSurfaceEnabled()+ - ", osh-set "+offscreenSurfaceHandleSet+ - ", osh "+toHexString(offscreenSurfaceHandle)+ - ", rsh "+toHexString(rootSurfaceLayer)+ + ", osd-set "+offscreenSurfaceDrawableSet+ + ", osd "+toHexString(offscreenSurfaceDrawable)+ + ", osl "+toHexString(getAttachedSurfaceLayer())+ + ", rsl "+toHexString(rootSurfaceLayer)+ ", wh "+toHexString(windowHandle)+" - "+Thread.currentThread().getName()); } - offscreenSurfaceHandle=0; - offscreenSurfaceHandleSet=false; + offscreenSurfaceDrawable=0; + offscreenSurfaceDrawableSet=false; if( isOffscreenLayerSurfaceEnabled() ) { if(0 != windowHandle) { OSXUtil.DestroyNSWindow(windowHandle); @@ -108,29 +110,41 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { jawt.FreeDrawingSurface(ds); } } - OSXUtil.DestroyCALayer(rootSurfaceLayer); - rootSurfaceLayer = 0; + + OSXUtil.RunOnMainThread(false, new Runnable() { + public void run() { + OSXUtil.DestroyCALayer(rootSurfaceLayer, false); + rootSurfaceLayer = 0; + } + }); } } windowHandle=0; } + @Override protected void attachSurfaceLayerImpl(final long layerHandle) { OSXUtil.AddCASublayer(rootSurfaceLayer, layerHandle, getWidth(), getHeight()); } - protected void layoutSurfaceLayerImpl() { - final long osl = getAttachedSurfaceLayer(); - final int w = getWidth(); - final int h = getHeight(); + @Override + protected void layoutSurfaceLayerImpl(long layerHandle, int width, int height) { if(DEBUG) { - System.err.println("JAWTWindow.fixSurfaceLayerLayout: "+toHexString(osl) + ", bounds "+bounds+", "+w+"x"+h); + System.err.println("JAWTWindow.layoutSurfaceLayerImpl: "+toHexString(layerHandle) + ", "+width+"x"+height+"; "+this); } - OSXUtil.FixCALayerLayout(rootSurfaceLayer, osl, w, h); + OSXUtil.FixCALayerLayout(rootSurfaceLayer, layerHandle, width, height); } - protected void detachSurfaceLayerImpl(final long layerHandle) { - OSXUtil.RemoveCASublayer(rootSurfaceLayer, layerHandle); + @Override + protected void detachSurfaceLayerImpl(final long layerHandle, final Runnable detachNotify) { + OSXUtil.RunOnMainThread(false, new Runnable() { + public void run() { + final long l = MacOSXJAWTWindow.this.getAttachedSurfaceLayer(); + if( 0 != l ) { + detachNotify.run(); + OSXUtil.RemoveCASublayer(rootSurfaceLayer, layerHandle, false); + } + } } ); } @Override @@ -140,7 +154,7 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { @Override public final long getSurfaceHandle() { - return offscreenSurfaceHandleSet ? offscreenSurfaceHandle : drawable /* super.getSurfaceHandle() */ ; + return offscreenSurfaceDrawableSet ? offscreenSurfaceDrawable : drawable /* super.getSurfaceHandle() */ ; } public void setSurfaceHandle(long surfaceHandle) { @@ -150,8 +164,8 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { if(DEBUG) { System.err.println("MacOSXJAWTWindow.setSurfaceHandle(): "+toHexString(surfaceHandle)); } - this.offscreenSurfaceHandle = surfaceHandle; - this.offscreenSurfaceHandleSet = true; + this.offscreenSurfaceDrawable = surfaceHandle; + this.offscreenSurfaceDrawableSet = true; } protected JAWT fetchJAWTImpl() throws NativeWindowException { @@ -257,7 +271,7 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { } if(null != errMsg) { if(0 != rootSurfaceLayer) { - OSXUtil.DestroyCALayer(rootSurfaceLayer); + OSXUtil.DestroyCALayer(rootSurfaceLayer, true); rootSurfaceLayer = 0; } if(0 != windowHandle) { @@ -329,11 +343,11 @@ public class MacOSXJAWTWindow extends JAWTWindow implements MutableSurface { private JAWT_MacOSXDrawingSurfaceInfo macosxdsi; - private long rootSurfaceLayer = 0; // attached to the JAWT_SurfaceLayer + private volatile long rootSurfaceLayer = 0; // attached to the JAWT_SurfaceLayer private long windowHandle = 0; - private long offscreenSurfaceHandle = 0; - private boolean offscreenSurfaceHandleSet = false; + private long offscreenSurfaceDrawable = 0; + private boolean offscreenSurfaceDrawableSet = false; // Workaround for instance of 4796548 private boolean firstLock = true; diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/windows/WindowsJAWTWindow.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/windows/WindowsJAWTWindow.java index 5d1d43792..905a313d5 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/windows/WindowsJAWTWindow.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/windows/WindowsJAWTWindow.java @@ -64,13 +64,6 @@ public class WindowsJAWTWindow extends JAWTWindow { windowHandle = 0; } - protected void attachSurfaceLayerImpl(final long layerHandle) { - throw new UnsupportedOperationException("offscreen layer not supported"); - } - protected void detachSurfaceLayerImpl(final long layerHandle) { - throw new UnsupportedOperationException("offscreen layer not supported"); - } - protected JAWT fetchJAWTImpl() throws NativeWindowException { return JAWTUtil.getJAWT(false); // no offscreen } diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/x11/X11JAWTWindow.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/x11/X11JAWTWindow.java index 467809284..2e5dc7fb5 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/x11/X11JAWTWindow.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/x11/X11JAWTWindow.java @@ -59,13 +59,6 @@ public class X11JAWTWindow extends JAWTWindow { protected void invalidateNative() { } - protected void attachSurfaceLayerImpl(final long layerHandle) { - throw new UnsupportedOperationException("offscreen layer not supported"); - } - protected void detachSurfaceLayerImpl(final long layerHandle) { - throw new UnsupportedOperationException("offscreen layer not supported"); - } - protected JAWT fetchJAWTImpl() throws NativeWindowException { return JAWTUtil.getJAWT(false); // no offscreen } diff --git a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java index 703c3d972..4b102be00 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java @@ -138,11 +138,15 @@ public class OSXUtil implements ToolkitProperties { /** * Create a CALayer suitable to act as a root CALayer. - * @see #DestroyCALayer(long) + * @see #DestroyCALayer(long, boolean) * @see #AddCASublayer(long, long) */ public static long CreateCALayer(final int x, final int y, final int width, final int height) { - return CreateCALayer0(x, y, width, height); + final long l = CreateCALayer0(x, y, width, height); + if(DEBUG) { + System.err.println("OSXUtil.CreateCALayer: 0x"+Long.toHexString(l)+" - "+Thread.currentThread().getName()); + } + return l; } /** @@ -156,7 +160,7 @@ public class OSXUtil implements ToolkitProperties { * they will be used for creation. *

* @see #CreateCALayer(int, int, int, int) - * @see #RemoveCASublayer(long, long) + * @see #RemoveCASublayer(long, long, boolean) */ public static void AddCASublayer(final long rootCALayer, final long subCALayer, final int width, final int height) { if(0==rootCALayer || 0==subCALayer) { @@ -164,6 +168,9 @@ public class OSXUtil implements ToolkitProperties { } RunOnMainThread(false, new Runnable() { public void run() { + if(DEBUG) { + System.err.println("OSXUtil.AttachCALayer: 0x"+Long.toHexString(subCALayer)+" - "+Thread.currentThread().getName()); + } AddCASublayer0(rootCALayer, subCALayer, width, height); } }); @@ -193,32 +200,48 @@ public class OSXUtil implements ToolkitProperties { } /** - * Detach a sub CALayer from the root CALayer on the main-thread w/o blocking. + * Detach a sub CALayer from the root CALayer. + * @param onMainThread if true method will be performed on the main-thread w/o blocking. */ - public static void RemoveCASublayer(final long rootCALayer, final long subCALayer) { + public static void RemoveCASublayer(final long rootCALayer, final long subCALayer, boolean onMainThread) { if(0==rootCALayer || 0==subCALayer) { throw new IllegalArgumentException("rootCALayer 0x"+Long.toHexString(rootCALayer)+", subCALayer 0x"+Long.toHexString(subCALayer)); } - RunOnMainThread(false, new Runnable() { + final Runnable action = new Runnable() { public void run() { + if(DEBUG) { + System.err.println("OSXUtil.DetachCALayer: 0x"+Long.toHexString(subCALayer)+" - "+Thread.currentThread().getName()); + } RemoveCASublayer0(rootCALayer, subCALayer); - } - }); + } }; + if( onMainThread ) { + RunOnMainThread(false, action); + } else { + action.run(); + } } /** - * Destroy a CALayer on main-thread w/o blocking. + * Destroy a CALayer. + * @param onMainThread if true method will be performed on the main-thread w/o blocking. * @see #CreateCALayer(int, int, int, int) */ - public static void DestroyCALayer(final long caLayer) { + public static void DestroyCALayer(final long caLayer, boolean onMainThread) { if(0==caLayer) { throw new IllegalArgumentException("caLayer 0x"+Long.toHexString(caLayer)); } - RunOnMainThread(false, new Runnable() { + final Runnable action = new Runnable() { public void run() { + if(DEBUG) { + System.err.println("OSXUtil.DestroyCALayer: 0x"+Long.toHexString(caLayer)+" - "+Thread.currentThread().getName()); + } DestroyCALayer0(caLayer); - } - }); + } }; + if( onMainThread ) { + RunOnMainThread(false, action); + } else { + action.run(); + } } /** @@ -238,7 +261,7 @@ public class OSXUtil implements ToolkitProperties { // otherwise we may freeze the OSX main thread. Throwable throwable = null; final Object sync = new Object(); - final RunnableTask rt = new RunnableTask( runnable, waitUntilDone ? sync : null, true ); + final RunnableTask rt = new RunnableTask( runnable, waitUntilDone ? sync : null, true, waitUntilDone ? null : System.err ); synchronized(sync) { RunOnMainThread0(rt); if( waitUntilDone ) { @@ -258,6 +281,16 @@ public class OSXUtil implements ToolkitProperties { } } + /** + * Run later on current OSX thread. + * + * @param runnable + * @param delay delay to run the runnable in milliseconds + */ + public static void RunLater(Runnable runnable, int delay) { + RunLater0(new RunnableTask( runnable, null, true, System.err ), delay); + } + private static Runnable _nop = new Runnable() { public void run() {}; }; /** Issues a {@link #RunOnMainThread(boolean, Runnable)} w/ an NOP runnable, while waiting until done. */ @@ -282,7 +315,7 @@ public class OSXUtil implements ToolkitProperties { // otherwise we may freeze the OSX main thread. Throwable throwable = null; final Object sync = new Object(); - final FunctionTask rt = new FunctionTask( func, waitUntilDone ? sync : null, true ); + final FunctionTask rt = new FunctionTask( func, waitUntilDone ? sync : null, true, waitUntilDone ? null : System.err ); synchronized(sync) { rt.setArgs(args); RunOnMainThread0(rt); @@ -349,6 +382,7 @@ public class OSXUtil implements ToolkitProperties { private static native void RemoveCASublayer0(long rootCALayer, long subCALayer); private static native void DestroyCALayer0(long caLayer); private static native void RunOnMainThread0(Runnable runnable); + private static native void RunLater0(Runnable runnable, int delay); private static native boolean IsMainThread0(); private static native int GetScreenRefreshRate0(int scrn_idx); } -- cgit v1.2.3