From 0120a81c36ffc7455d196960048fe935f82fc9a6 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 10 Nov 2011 17:14:51 +0100 Subject: OS X Layered View: Part6 (native) Using a root CALayer where we attach/detach our GL one - it seems to be more stable, having one root CALayer attached to the JAWT_SurfaceView forever - tackles crach at GL layer destruction - proper release of all GL layer resources - now final [gl-layer dealloc] happens at very destruction of JAWT object, even though it was removed from root-layer earlier (and all other references) - see comment in MacOSXWindowSystemInterface-pbuffer.m :: createNSOpenGLLayer(..) - at least no more crash .. and resource release ASAP, but the GL-layer itself (see above) --- .../nativewindow/jawt/macosx/MacOSXJAWTWindow.java | 128 +++++++++------------ .../jogamp/nativewindow/macosx/OSXUtil.java | 28 +++++ 2 files changed, 83 insertions(+), 73 deletions(-) (limited to 'src/nativewindow/classes') diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java index 71b297791..364b2054d 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java @@ -58,7 +58,6 @@ import jogamp.nativewindow.jawt.JAWTUtil; import jogamp.nativewindow.jawt.JAWTWindow; import jogamp.nativewindow.jawt.JAWT_DrawingSurface; import jogamp.nativewindow.jawt.JAWT_DrawingSurfaceInfo; -import jogamp.nativewindow.jawt.JAWT_Rectangle; import jogamp.nativewindow.macosx.OSXUtil; public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { @@ -78,17 +77,47 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { OSXUtil.DestroyNSWindow(drawable); drawable = 0; } - if(0 != surfaceLayerHandle && null!=dsi) { - OSXUtil.AttachJAWTSurfaceLayer(dsi, 0); - } - surfaceLayerHandle = 0; } } public final boolean isOffscreenLayerSurface() { return isOffscreenLayerSurface; + } + public void attachSurfaceLayer(final long layerHandle) { + if( !isOffscreenLayerSurface() ) { + throw new NativeWindowException("Not using CALAYER"); + } + int lockRes = lockSurface(); + if (NativeSurface.LOCK_SURFACE_NOT_READY >= lockRes) { + throw new NativeWindowException("Could not lock layeredSurfaceHost: "+this); + } + try { + if(DEBUG) { + System.err.println("MacOSXJAWTWindow.attachSurfaceLayer(): 0x"+Long.toHexString(layerHandle)); + } + OSXUtil.AddCASublayer(rootSurfaceLayerHandle, layerHandle); + } finally { + unlockSurface(); + } } - + public void detachSurfaceLayer(final long layerHandle) { + if( !isOffscreenLayerSurface() ) { + throw new NativeWindowException("Not using CALAYER"); + } + int lockRes = lockSurface(); + if (NativeSurface.LOCK_SURFACE_NOT_READY >= lockRes) { + throw new NativeWindowException("Could not lock layeredSurfaceHost: "+this); + } + try { + if(DEBUG) { + System.err.println("MacOSXJAWTWindow.detachSurfaceLayer(): 0x"+Long.toHexString(layerHandle)); + } + OSXUtil.RemoveCASublayer(rootSurfaceLayerHandle, layerHandle); + } finally { + unlockSurface(); + } + } + public long getSurfaceHandle() { return isOffscreenLayerSurface ? surfaceHandle : super.getSurfaceHandle() ; } @@ -117,45 +146,6 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { return sscSet ? sscHeight: super.getHeight(); } - /* - public long getSurfaceLayer() { - if( !isLayeredSurface() ) { - throw new java.lang.UnsupportedOperationException("Not using CALAYER"); - } - if( !dsLocked ) { - throw new NativeWindowException("Not locked"); - } - // return macosxsl.getLayer(); - return getSurfaceLayers().getLayer(); - } */ - - public void attachSurfaceLayer(final long layerHandle) { - if( !isOffscreenLayerSurface() ) { - throw new NativeWindowException("Not using CALAYER"); - } - int lockRes = lockSurface(); - if (NativeSurface.LOCK_SURFACE_NOT_READY >= lockRes) { - throw new NativeWindowException("Could not lock layeredSurfaceHost: "+this); - } - try { - if(DEBUG) { - System.err.println("MacOSXJAWTWindow.attachSurfaceLayer(): 0x"+Long.toHexString(layerHandle)); - } - if(!OSXUtil.AttachJAWTSurfaceLayer(dsi, layerHandle)) { - throw new NativeWindowException("Could not attach JAWT surfaceLayerHandle: "+this); - } - surfaceLayerHandle = layerHandle; - } finally { - unlockSurface(); - } - /* - if( null == macosxsl) { - throw new NativeWindowException("Not locked and/or SurfaceLayers null"); - } - macosxsl.setLayer(layerHandle); */ - // getSurfaceLayers().setLayer(layerHandle); - } - protected int lockSurfaceImpl() throws NativeWindowException { int ret = NativeWindow.LOCK_SURFACE_NOT_READY; if(null == ds) { @@ -234,15 +224,24 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { Capabilities caps = (Capabilities) config.getChosenCapabilities().cloneMutable(); caps.setOnscreen(false); config.setChosenCapabilities(caps); + } + if(0 == rootSurfaceLayerHandle) { + rootSurfaceLayerHandle = OSXUtil.CreateCALayer(); + if(0 == rootSurfaceLayerHandle) { + OSXUtil.DestroyNSWindow(drawable); + drawable = 0; + unlockSurfaceImpl(); + throw new NativeWindowException("Could not create root CALayer: "+this); + } + if(!OSXUtil.AttachJAWTSurfaceLayer(dsi, rootSurfaceLayerHandle)) { + OSXUtil.DestroyCALayer(rootSurfaceLayerHandle); + rootSurfaceLayerHandle = 0; + OSXUtil.DestroyNSWindow(drawable); + drawable = 0; + unlockSurfaceImpl(); + throw new NativeWindowException("Could not attach JAWT surfaceLayerHandle: "+this); + } } - /** - macosxsl = (JAWT_SurfaceLayers) dsi.platformInfo(); - if (null == macosxsl) { - unlockSurfaceImpl(); - return NativeWindow.LOCK_SURFACE_NOT_READY; - } else { - ret = NativeWindow.LOCK_SUCCESS; - } */ ret = NativeWindow.LOCK_SUCCESS; } @@ -261,31 +260,14 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { } ds = null; dsi = null; - // macosxsl = null; } - /** - protected JAWT_SurfaceLayers getSurfaceLayers() { - if( !dsLocked || null == dsi ) { - throw new NativeWindowException("Locked: "+dsLocked+", dsi valid: "+(null!=dsi)); - } - final JAWT_SurfaceLayers macosxsl = (JAWT_SurfaceLayers) dsi.platformInfo(); - if (null == macosxsl) { - throw new NativeWindowException("SurfaceLayer null"); - } - return macosxsl; - } */ - private void dumpInfo() { System.err.println("MaxOSXJAWTWindow: 0x"+Integer.toHexString(this.hashCode())+" - thread: "+Thread.currentThread().getName()); // System.err.println(this); System.err.println("JAWT version: 0x"+Integer.toHexString(JAWT.getJAWT().getVersionCached())+ ", CA_LAYER: "+ (0!=(JAWT.getJAWT().getVersionCached() & JAWT.JAWT_MACOSX_USE_CALAYER))+ - ", isLayeredSurface "+isOffscreenLayerSurface()); - if(null != dsi) { - JAWT_Rectangle r = dsi.getBounds(); - System.err.println("dsi bounds: "+r.getX()+"/"+r.getY()+" "+r.getWidth()+"x"+r.getHeight()); - } + ", isLayeredSurface "+isOffscreenLayerSurface()+", bounds "+bounds); // Thread.dumpStack(); } @@ -307,10 +289,10 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { private JAWT_DrawingSurfaceInfo dsi; private JAWT_MacOSXDrawingSurfaceInfo macosxdsi; - // private JAWT_SurfaceLayers macosxsl; private final boolean isOffscreenLayerSurface; - private long surfaceLayerHandle = 0; + private long rootSurfaceLayerHandle = 0; // is autoreleased, once it is attached to the JAWT_SurfaceLayer + private long surfaceHandle = 0; private int sscWidth, sscHeight; private boolean sscSet = false; diff --git a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java index a07001def..1739288a7 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java @@ -51,7 +51,31 @@ public class OSXUtil { DestroyNSWindow0(nsWindow); } + public static long CreateCALayer() { + return CreateCALayer0(); + } + public static void AddCASublayer(long rootCALayer, long subCALayer) { + if(0==rootCALayer || 0==subCALayer) { + throw new IllegalArgumentException("rootCALayer 0x"+Long.toHexString(rootCALayer)+", subCALayer 0x"+Long.toHexString(subCALayer)); + } + AddCASublayer0(rootCALayer, subCALayer); + } + public static void RemoveCASublayer(long rootCALayer, long subCALayer) { + if(0==rootCALayer || 0==subCALayer) { + throw new IllegalArgumentException("rootCALayer 0x"+Long.toHexString(rootCALayer)+", subCALayer 0x"+Long.toHexString(subCALayer)); + } + RemoveCASublayer0(rootCALayer, subCALayer); + } + public static void DestroyCALayer(long caLayer) { + if(0==caLayer) { + throw new IllegalArgumentException("caLayer 0x"+Long.toHexString(caLayer)); + } + DestroyCALayer0(caLayer); + } public static boolean AttachJAWTSurfaceLayer(JAWT_DrawingSurfaceInfo dsi, long caLayer) { + if(0==caLayer) { + throw new IllegalArgumentException("caLayer 0x"+Long.toHexString(caLayer)); + } return AttachJAWTSurfaceLayer0(dsi.getBuffer(), caLayer); } @@ -73,6 +97,10 @@ public class OSXUtil { private static native void DestroyNSView0(long nsView); private static native long CreateNSWindow0(int x, int y, int width, int height); private static native void DestroyNSWindow0(long nsWindow); + private static native long CreateCALayer0(); + private static native void AddCASublayer0(long rootCALayer, long subCALayer); + private static native void RemoveCASublayer0(long rootCALayer, long subCALayer); + private static native void DestroyCALayer0(long caLayer); private static native boolean AttachJAWTSurfaceLayer0(Buffer jawtDrawingSurfaceInfoBuffer, long caLayer); private static native void RunOnMainThread0(boolean waitUntilDone, Runnable runnable); private static native boolean IsMainThread0(); -- cgit v1.2.3