From d8fa00d35a49f4faf5f04aeb7e2bba4e972965f5 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 9 Nov 2011 01:11:40 +0100 Subject: OS X Layered View: Part3 JAWTWindow/MacOSXJAWTWindow (Java/Native) JAWTWindow: - Add comment about caller of cstr, since it uses reflection - Field 'config' is of type AWTGraphicsConfiguration, validate and type it - Impl. NativeSurfaceHolder/NativeWindowHolder 'hooks' MacOSXJAWTWindow: - Use agnostic JAWTUtil.isJAWTVersionUsingOffscreenLayer() to determine 'isOffscreenLayeredSurface'. We may promote this notion to JAWTWindow, which already holds 'isApplet' - lockSurface() case 'isOffscreenLayeredSurface': - Create dummy NSWindow (OSXUtil) for getWindowHandle() - Fix chosen caps: offscreen --- .../jogamp/nativewindow/jawt/JAWTWindow.java | 25 +++++++- .../nativewindow/jawt/macosx/MacOSXJAWTWindow.java | 69 +++++++++++++++------- .../jogamp/nativewindow/macosx/OSXUtil.java | 9 +++ src/nativewindow/native/macosx/OSXmisc.m | 53 +++++++++++++++++ 4 files changed, 133 insertions(+), 23 deletions(-) (limited to 'src/nativewindow') diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTWindow.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTWindow.java index e168ea757..3f10fa983 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTWindow.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTWindow.java @@ -49,6 +49,7 @@ import javax.media.nativewindow.NativeSurface; import javax.media.nativewindow.NativeWindow; import javax.media.nativewindow.NativeWindowException; import javax.media.nativewindow.SurfaceUpdatedListener; +import javax.media.nativewindow.awt.AWTGraphicsConfiguration; import javax.media.nativewindow.util.Insets; import javax.media.nativewindow.util.InsetsImmutable; import javax.media.nativewindow.util.Point; @@ -63,18 +64,28 @@ public abstract class JAWTWindow implements NativeWindow { // lifetime: forever protected Component component; protected boolean isApplet; - protected AbstractGraphicsConfiguration config; + protected AWTGraphicsConfiguration config; private SurfaceUpdatedHelper surfaceUpdatedHelper = new SurfaceUpdatedHelper(); // lifetime: valid after lock, forever until invalidate protected long drawable; protected Rectangle bounds; - public JAWTWindow(Object comp, AbstractGraphicsConfiguration config) { + /** + * Constructed by {@link jogamp.nativewindow.NativeWindowFactoryImpl#getNativeWindow(Object, AbstractGraphicsConfiguration)} + * via this platform's specialization (X11, OSX, Windows, ..). + * + * @param comp + * @param config + */ + protected JAWTWindow(Object comp, AbstractGraphicsConfiguration config) { if (config == null) { throw new NativeWindowException("Error: AbstractGraphicsConfiguration is null"); } - this.config = config; + if(! ( config instanceof AWTGraphicsConfiguration ) ) { + throw new NativeWindowException("Error: AbstractGraphicsConfiguration is not an AWTGraphicsConfiguration"); + } + this.config = (AWTGraphicsConfiguration) config; init((Component)comp); } @@ -147,6 +158,10 @@ public abstract class JAWTWindow implements NativeWindow { // NativeSurface // + public NativeSurface getNativeSurface() { + return this; + } + private RecursiveLock surfaceLock = LockFactory.createRecursiveLock(); protected abstract int lockSurfaceImpl() throws NativeWindowException; @@ -239,6 +254,10 @@ public abstract class JAWTWindow implements NativeWindow { // NativeWindow // + public NativeWindow getNativeWindow() { + return this; + } + public synchronized void destroy() { invalidate(); if(null!=component) { diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java index 8fbc91176..b75bb3f4d 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/macosx/MacOSXJAWTWindow.java @@ -45,6 +45,8 @@ import java.security.AccessController; import java.security.PrivilegedAction; import javax.media.nativewindow.AbstractGraphicsConfiguration; +import javax.media.nativewindow.Capabilities; +import javax.media.nativewindow.NativeSurface; import javax.media.nativewindow.SurfaceChangeable; import javax.media.nativewindow.NativeWindow; import javax.media.nativewindow.NativeWindowException; @@ -52,6 +54,7 @@ import javax.media.nativewindow.util.Point; import jogamp.nativewindow.jawt.JAWT; import jogamp.nativewindow.jawt.JAWTFactory; +import jogamp.nativewindow.jawt.JAWTUtil; import jogamp.nativewindow.jawt.JAWTWindow; import jogamp.nativewindow.jawt.JAWT_DrawingSurface; import jogamp.nativewindow.jawt.JAWT_DrawingSurfaceInfo; @@ -59,12 +62,9 @@ import jogamp.nativewindow.jawt.JAWT_Rectangle; import jogamp.nativewindow.macosx.OSXUtil; public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { - final boolean isLayeredSurface; - long surfaceHandle = 0; - public MacOSXJAWTWindow(Object comp, AbstractGraphicsConfiguration config) { super(comp, config); - isLayeredSurface = 0 != ( JAWT.getJAWT().getVersionCached() & JAWT.JAWT_MACOSX_USE_CALAYER ); + isOffscreenLayeredSurface = JAWTUtil.isJAWTVersionUsingOffscreenLayer(); dumpInfo(); } @@ -72,14 +72,19 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { } protected void invalidateNative() { + surfaceHandle=0; + if(isOffscreenLayeredSurface && 0 == drawable) { + OSXUtil.DestroyNSWindow(drawable); + drawable = 0; + } } public final boolean isLayeredSurface() { - return isLayeredSurface; + return isOffscreenLayeredSurface; } public long getSurfaceHandle() { - return isLayeredSurface ? surfaceHandle : super.getSurfaceHandle() ; + return isOffscreenLayeredSurface ? surfaceHandle : super.getSurfaceHandle() ; } public void setSurfaceHandle(long surfaceHandle) { @@ -106,15 +111,20 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { public void attachSurfaceLayer(long layerHandle) { if( !isLayeredSurface() ) { - throw new java.lang.UnsupportedOperationException("Not using CALAYER"); + throw new NativeWindowException("Not using CALAYER"); } - if( !dsLocked || null == dsi ) { - throw new NativeWindowException("Locked: "+dsLocked+", dsi valid: "+(null!=dsi)); - } - if(DEBUG) { - System.err.println("MacOSXJAWTWindow.attachSurfaceLayer(): 0x"+Long.toHexString(layerHandle)); + 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.AttachJAWTSurfaceLayer0(dsi, layerHandle); + } finally { + unlockSurface(); } - OSXUtil.AttachJAWTSurfaceLayer0(dsi, layerHandle); /* if( null == macosxsl) { throw new NativeWindowException("Not locked and/or SurfaceLayers null"); @@ -156,9 +166,6 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { return null; } }); - if(DEBUG) { - dumpInfo(); - } } else { dsi = ds.GetDrawingSurfaceInfo(); } @@ -167,8 +174,12 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { return NativeWindow.LOCK_SURFACE_NOT_READY; } } + updateBounds(dsi.getBounds()); + if (DEBUG && firstLock) { + dumpInfo(); + } firstLock = false; - if( !isLayeredSurface ) { + if( !isOffscreenLayeredSurface ) { macosxdsi = (JAWT_MacOSXDrawingSurfaceInfo) dsi.platformInfo(); if (macosxdsi == null) { unlockSurfaceImpl(); @@ -183,6 +194,24 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { ret = NativeWindow.LOCK_SUCCESS; } } else { + /** + * Only create a fake invisible NSWindow for the drawable handle + * to please frameworks requiring such (eg. NEWT). + * + * The actual surface/ca-layer shall be created/attached + * by the upper framework (JOGL) since they require more information. + */ + if(0 == drawable) { + drawable = OSXUtil.CreateNSWindow(0, 0, getBounds().getWidth(), getBounds().getHeight()); + if(0 == drawable) { + unlockSurfaceImpl(); + throw new NativeWindowException("Unable to created dummy NSWindow (layered case)"); + } + // fix caps reflecting offscreen! + Capabilities caps = (Capabilities) config.getChosenCapabilities().cloneMutable(); + caps.setOnscreen(false); + config.setChosenCapabilities(caps); + } /** macosxsl = (JAWT_SurfaceLayers) dsi.platformInfo(); if (null == macosxsl) { @@ -194,9 +223,6 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { ret = NativeWindow.LOCK_SUCCESS; } - if(NativeWindow.LOCK_SURFACE_CHANGED <= ret) { - updateBounds(dsi.getBounds()); - } return ret; } @@ -259,6 +285,9 @@ public class MacOSXJAWTWindow extends JAWTWindow implements SurfaceChangeable { private JAWT_MacOSXDrawingSurfaceInfo macosxdsi; // private JAWT_SurfaceLayers macosxsl; + + final boolean isOffscreenLayeredSurface; + long surfaceHandle = 0; // Workaround for instance of 4796548 private boolean firstLock = true; diff --git a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java index db7764723..79ac24686 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java @@ -44,6 +44,13 @@ public class OSXUtil { DestroyNSView0(nsView); } + public static long CreateNSWindow(int x, int y, int width, int height) { + return CreateNSWindow0(x, y, width, height); + } + public static void DestroyNSWindow(long nsWindow) { + DestroyNSWindow0(nsWindow); + } + public static boolean AttachJAWTSurfaceLayer0(JAWT_DrawingSurfaceInfo dsi, long caLayer) { return AttachJAWTSurfaceLayer0(dsi.getBuffer(), caLayer); } @@ -64,6 +71,8 @@ public class OSXUtil { private static native Object GetLocationOnScreen0(long windowOrView, int src_x, int src_y); private static native long CreateNSView0(int x, int y, int width, int height); 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 boolean AttachJAWTSurfaceLayer0(Buffer jawtDrawingSurfaceInfoBuffer, long caLayer); private static native void RunOnMainThread0(boolean waitUntilDone, Runnable runnable); private static native boolean IsMainThread0(); diff --git a/src/nativewindow/native/macosx/OSXmisc.m b/src/nativewindow/native/macosx/OSXmisc.m index 55ec83279..16a4ed148 100644 --- a/src/nativewindow/native/macosx/OSXmisc.m +++ b/src/nativewindow/native/macosx/OSXmisc.m @@ -169,6 +169,59 @@ JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_DestroyNSView0 [pool release]; } +/* + * Class: Java_jogamp_nativewindow_macosx_OSXUtil + * Method: CreateNSWindow0 + * Signature: (IIIIZ)J + */ +JNIEXPORT jlong JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_CreateNSWindow0 + (JNIEnv *env, jclass unused, jint x, jint y, jint width, jint height) +{ + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + NSRect rect = NSMakeRect(x, y, width, height); + + // Allocate the window + NSWindow* myWindow = [[NSWindow alloc] initWithContentRect: rect + styleMask: NSBorderlessWindowMask + backing: NSBackingStoreBuffered + defer: YES]; + [myWindow setReleasedWhenClosed: YES]; // default + [myWindow setPreservesContentDuringLiveResize: YES]; + + // invisible .. + [myWindow setOpaque: NO]; + [myWindow setBackgroundColor: [NSColor clearColor]]; + + // force surface creation + // [myView lockFocus]; + // [myView unlockFocus]; + + [pool release]; + + return (jlong) ((intptr_t) myWindow); +} + +/* + * Class: Java_jogamp_nativewindow_macosx_OSXUtil + * Method: DestroyNSWindow0 + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_DestroyNSWindow0 + (JNIEnv *env, jclass unused, jlong nsWindow) +{ + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + NSWindow* mWin = (NSWindow*) ((intptr_t) nsWindow); + NSView* mView = [mWin contentView]; + + if(NULL!=mView) { + [mWin setContentView: nil]; + [mView release]; + } + [mWin orderOut: mWin]; + [mWin close]; // performs release! + [pool release]; +} + /* * Class: Java_jogamp_nativewindow_macosx_OSXUtil * Method: attachJAWTSurfaceLayer -- cgit v1.2.3