diff options
author | Sven Gothel <[email protected]> | 2015-02-17 07:30:34 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2015-02-17 07:30:34 +0100 |
commit | 37ec129f36097f41ed0d45cbccd7a93e493e2bb9 (patch) | |
tree | cb3f418d8b405c4ecb5825386cc04cf7687b5a98 /src | |
parent | a28e1610e1c29279847bce80e1aa80a947ff799e (diff) |
Bug 1130 - Add Mapping from AWT Component -> NEWT [Screen, MonitorDevice]
The mapping AWT Component -> NEWT [Screen, MonitorDevice]
shall allow generic AWT applications to utilize NEWT's MonitorDevice
information like physical monitor-size and DPI.
- AWT-Component -> NEWT-Display:
- NewtFactoryAWT.createDisplay
- AWT-Component -> NEWT-Screen:
- NewtFactoryAWT.createScreen
- AWT-Component -> NEWT-MonitorMode:
- NewtFactoryAWT.getMonitorDevice
- NewtFactoryAWT.getMonitorDevice
- If OSX, utilizing OSX's AWT Component -> MonitorDevice-Index mapping
- Otherwise using the coverage to identify MonitorDevice
See TestGearsES2GLJPanelAWT 'GetPixelScale',
demonstrating the mapping while pressing 'p' (cached MonitorMode)
and pressing SHIFT-'p' (non-cached MonitorMode).
Diffstat (limited to 'src')
6 files changed, 271 insertions, 33 deletions
diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java index e733b7233..93c3dbaf7 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java @@ -49,14 +49,19 @@ import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Map; +import com.jogamp.nativewindow.AbstractGraphicsDevice; +import com.jogamp.nativewindow.AbstractGraphicsScreen; import com.jogamp.nativewindow.NativeWindowException; import com.jogamp.nativewindow.NativeWindowFactory; import com.jogamp.nativewindow.ToolkitLock; +import com.jogamp.nativewindow.awt.AWTGraphicsScreen; import jogamp.common.os.PlatformPropsImpl; import jogamp.nativewindow.Debug; import jogamp.nativewindow.NWJNILibLoader; +import jogamp.nativewindow.jawt.x11.X11SunJDKReflection; import jogamp.nativewindow.macosx.OSXUtil; +import jogamp.nativewindow.x11.X11Lib; import com.jogamp.common.os.Platform; import com.jogamp.common.util.PropertyAccess; @@ -545,6 +550,21 @@ public class JAWTUtil { return jawtToolkitLock; } + public static final int getMonitorIndex(final GraphicsDevice device) { + int idx = -1; + if( null != getCGDisplayIDMethodOnOSX ) { + // OSX specific + try { + final Object res = getCGDisplayIDMethodOnOSX.invoke(device); + if (res instanceof Integer) { + final int displayID = ((Integer)res).intValue(); + idx = OSXUtil.GetNSScreenIdx(displayID); + } + } catch (final Throwable t) {} + } + return idx; + } + /** * Returns the pixel scale factor of the given {@link GraphicsDevice}, if supported. * <p> @@ -628,5 +648,73 @@ public class JAWTUtil { } return changed; } + + private static String getThreadName() { + return Thread.currentThread().getName(); + } + private static String toHexString(final long val) { + return "0x" + Long.toHexString(val); + } + + /** + * @param awtComp must be {@link java.awt.Component#isDisplayable() displayable} + * and must have a {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration} + * @return AbstractGraphicsDevice instance reflecting the {@code awtComp} + * @throws IllegalArgumentException if {@code awtComp} is not {@link java.awt.Component#isDisplayable() displayable} + * or has {@code null} {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration}. + * @see #getAbstractGraphicsScreen(java.awt.Component) + */ + public static AbstractGraphicsDevice createDevice(final java.awt.Component awtComp) throws IllegalArgumentException { + if( !awtComp.isDisplayable() ) { + throw new IllegalArgumentException("Given AWT-Component is not displayable: "+awtComp); + } + final GraphicsDevice device; + final GraphicsConfiguration gc = awtComp.getGraphicsConfiguration(); + if(null!=gc) { + device = gc.getDevice(); + } else { + throw new IllegalArgumentException("Given AWT-Component has no GraphicsConfiguration set: "+awtComp); + } + + final String displayConnection; + final String nwt = NativeWindowFactory.getNativeWindowType(true); + if( NativeWindowFactory.TYPE_X11 == nwt ) { + final long displayHandleAWT = X11SunJDKReflection.graphicsDeviceGetDisplay(device); + if( 0 == displayHandleAWT ) { + displayConnection = null; // default + if(DEBUG) { + System.err.println(getThreadName()+" - JAWTUtil.createDevice: Null AWT dpy, default X11 display"); + } + } else { + /** + * Using the AWT display handle works fine with NVidia. + * However we experienced different results w/ AMD drivers, + * some work, but some behave erratic. + * I.e. hangs in XQueryExtension(..) via X11GraphicsScreen. + */ + displayConnection = X11Lib.XDisplayString(displayHandleAWT); + if(DEBUG) { + System.err.println(getThreadName()+" - JAWTUtil.createDevice: AWT dpy "+displayConnection+" / "+toHexString(displayHandleAWT)); + } + } + } else { + displayConnection = null; // default + } + return NativeWindowFactory.createDevice(displayConnection, true /* own */); + } + + /** + * @param awtComp must be {@link java.awt.Component#isDisplayable() displayable} + * and must have a {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration} + * @return AbstractGraphicsScreen instance reflecting the {@code awtComp} + * @throws IllegalArgumentException if {@code awtComp} is not {@link java.awt.Component#isDisplayable() displayable} + * or has {@code null} {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration}. + * @see #createDevice(java.awt.Component) + */ + public static AbstractGraphicsScreen getAbstractGraphicsScreen(final java.awt.Component awtComp) throws IllegalArgumentException { + final AbstractGraphicsDevice adevice = createDevice(awtComp); + return NativeWindowFactory.createScreen(adevice, AWTGraphicsScreen.findScreenIndex(awtComp.getGraphicsConfiguration().getDevice())); + } + } diff --git a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java index 8ec7b7e95..a5970b87c 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java @@ -107,6 +107,9 @@ public class OSXUtil implements ToolkitProperties { return (Insets) GetInsets0(windowOrView); } + public static int GetNSScreenIdx(final int displayID) { + return GetNSScreenIdx0(displayID); + } public static double GetPixelScaleByScreenIdx(final int screenIndex) { return GetPixelScale0(screenIndex); } @@ -395,6 +398,7 @@ public class OSXUtil implements ToolkitProperties { private static native boolean isNSWindow0(long object); private static native Object GetLocationOnScreen0(long windowOrView, int src_x, int src_y); private static native Object GetInsets0(long windowOrView); + private static native int GetNSScreenIdx0(int displayID); private static native double GetPixelScale0(int screenIndex); private static native double GetPixelScale1(int displayID); private static native double GetPixelScale2(long windowOrView); diff --git a/src/nativewindow/native/macosx/OSXmisc.m b/src/nativewindow/native/macosx/OSXmisc.m index 2cc272a41..87494e946 100644 --- a/src/nativewindow/native/macosx/OSXmisc.m +++ b/src/nativewindow/native/macosx/OSXmisc.m @@ -156,6 +156,18 @@ static NSScreen * OSXUtil_getNSScreenByCGDirectDisplayID(CGDirectDisplayID displ } return (NSScreen *) [screens objectAtIndex: 0]; } +static int OSXUtil_getNSScreenIdxByCGDirectDisplayID(CGDirectDisplayID displayID) { + NSArray *screens = [NSScreen screens]; + int i; + for(i=[screens count]-1; i>=0; i--) { + NSScreen * screen = (NSScreen *) [screens objectAtIndex: i]; + CGDirectDisplayID dID = OSXUtil_getCGDirectDisplayIDByNSScreen(screen); + if( dID == displayID ) { + return i; + } + } + return -1; +} /* * Class: Java_jogamp_nativewindow_macosx_OSXUtil @@ -263,6 +275,22 @@ JNIEXPORT jobject JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_GetInsets0 /* * Class: Java_jogamp_nativewindow_macosx_OSXUtil + * Method: GetNSScreenIdx0 + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_GetNSScreenIdx0 + (JNIEnv *env, jclass unused, jint displayID) +{ + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + int idx = OSXUtil_getNSScreenIdxByCGDirectDisplayID((CGDirectDisplayID)displayID); + [pool release]; + + return idx; +} + +/* + * Class: Java_jogamp_nativewindow_macosx_OSXUtil * Method: GetPixelScale0 * Signature: (I)D */ diff --git a/src/newt/classes/com/jogamp/newt/Screen.java b/src/newt/classes/com/jogamp/newt/Screen.java index 0e6895d4b..b8f096fc3 100644 --- a/src/newt/classes/com/jogamp/newt/Screen.java +++ b/src/newt/classes/com/jogamp/newt/Screen.java @@ -238,6 +238,18 @@ public abstract class Screen { return monitors.get(0); } + public final MonitorDevice getMonitor(final int monitorId) { + final List<MonitorDevice> monitors = getMonitorDevices(); + final int monitorCount = monitors.size(); + for(int i=0; i<monitorCount; i++) { + final MonitorDevice monitor = monitors.get(i); + if( monitor.getId() == monitorId ) { + return monitor; + } + } + return null; + } + /** * Calculates the union of all monitor's {@link MonitorDevice#getViewport() viewport} in pixel- and window units. * <p> diff --git a/src/newt/classes/jogamp/newt/awt/NewtFactoryAWT.java b/src/newt/classes/jogamp/newt/awt/NewtFactoryAWT.java index 23e9c4370..4faa5de9c 100644 --- a/src/newt/classes/jogamp/newt/awt/NewtFactoryAWT.java +++ b/src/newt/classes/jogamp/newt/awt/NewtFactoryAWT.java @@ -28,17 +28,30 @@ package jogamp.newt.awt; +import java.awt.GraphicsDevice; + import com.jogamp.nativewindow.AbstractGraphicsConfiguration; import com.jogamp.nativewindow.CapabilitiesImmutable; import com.jogamp.nativewindow.NativeWindow; import com.jogamp.nativewindow.NativeWindowException; import com.jogamp.nativewindow.NativeWindowFactory; +import jogamp.nativewindow.awt.AWTMisc; +import jogamp.nativewindow.jawt.JAWTUtil; +import jogamp.nativewindow.jawt.x11.X11SunJDKReflection; +import jogamp.nativewindow.x11.X11Lib; import jogamp.newt.Debug; import com.jogamp.nativewindow.awt.AWTGraphicsConfiguration; +import com.jogamp.nativewindow.awt.AWTGraphicsScreen; import com.jogamp.nativewindow.awt.JAWTWindow; +import com.jogamp.nativewindow.util.Point; +import com.jogamp.nativewindow.util.Rectangle; +import com.jogamp.nativewindow.util.RectangleImmutable; +import com.jogamp.newt.Display; +import com.jogamp.newt.MonitorDevice; import com.jogamp.newt.NewtFactory; +import com.jogamp.newt.Screen; public class NewtFactoryAWT extends NewtFactory { public static final boolean DEBUG_IMPLEMENTATION = Debug.debug("Window"); @@ -83,5 +96,102 @@ public class NewtFactoryAWT extends NewtFactory { config.getScreen().getDevice().close(); } + /** + * @param awtComp + * @throws IllegalArgumentException if {@code awtComp} is not {@link java.awt.Component#isDisplayable() displayable} + * or has {@code null} {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration}. + */ + private static void checkComponentValid(final java.awt.Component awtComp) throws IllegalArgumentException { + if( !awtComp.isDisplayable() ) { + throw new IllegalArgumentException("Given AWT-Component is not displayable: "+awtComp); + } + if( null == awtComp.getGraphicsConfiguration() ) { + throw new IllegalArgumentException("Given AWT-Component has no GraphicsConfiguration set: "+awtComp); + } + } + + /** + * @param awtComp must be {@link java.awt.Component#isDisplayable() displayable} + * and must have a {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration} + * @param reuse attempt to reuse an existing {@link Display} with same <code>name</code> if set true, otherwise create a new instance. + * @return {@link Display} instance reflecting the {@code awtComp} + * @throws IllegalArgumentException if {@code awtComp} is not {@link java.awt.Component#isDisplayable() displayable} + * or has {@code null} {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration}. + * @see #getAbstractGraphicsScreen(java.awt.Component) + */ + public static Display createDisplay(final java.awt.Component awtComp, final boolean reuse) throws IllegalArgumentException { + checkComponentValid(awtComp); + final GraphicsDevice device = awtComp.getGraphicsConfiguration().getDevice(); + + final String displayConnection; + final String nwt = NativeWindowFactory.getNativeWindowType(true); + if( NativeWindowFactory.TYPE_X11 == nwt ) { + final long displayHandleAWT = X11SunJDKReflection.graphicsDeviceGetDisplay(device); + if( 0 == displayHandleAWT ) { + displayConnection = null; // default + } else { + /** + * Using the AWT display handle works fine with NVidia. + * However we experienced different results w/ AMD drivers, + * some work, but some behave erratic. + * I.e. hangs in XQueryExtension(..) via X11GraphicsScreen. + */ + displayConnection = X11Lib.XDisplayString(displayHandleAWT); + } + } else { + displayConnection = null; // default + } + return NewtFactory.createDisplay(displayConnection, reuse); + } + + /** + * @param awtComp must be {@link java.awt.Component#isDisplayable() displayable} + * and must have a {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration} + * @param reuse attempt to reuse an existing {@link Display} with same <code>name</code> if set true, otherwise create a new instance. + * @return {@link Screen} instance reflecting the {@code awtComp} + * @throws IllegalArgumentException if {@code awtComp} is not {@link java.awt.Component#isDisplayable() displayable} + * or has {@code null} {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration}. + * @see #createDevice(java.awt.Component) + */ + public static Screen createScreen(final java.awt.Component awtComp, final boolean reuse) throws IllegalArgumentException { + final Display display = createDisplay(awtComp, reuse); + return NewtFactory.createScreen(display, AWTGraphicsScreen.findScreenIndex(awtComp.getGraphicsConfiguration().getDevice())); + } + + /** + * Retrieves the {@link MonitorDevice} for the given displayable {@code awtComp}. + * <p> + * In case this method shall be called multiple times, it is advised to keep the given {@link Screen} instance + * natively created during operation. This should be done via the initial {@link Screen#addReference()}. + * After operation, user shall destroy the instance accordingly via the final {@link Screen#removeReference()}. + * </p> + * @param screen the {@link Screen} instance matching {@code awtComp}, i.e. via {@link #createScreen(java.awt.Component, boolean)}. + * @param awtComp must be {@link java.awt.Component#isDisplayable() displayable} + * and must have a {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration} + * @return {@link MonitorDevice} instance reflecting the {@code awtComp} + * @throws IllegalArgumentException if {@code awtComp} is not {@link java.awt.Component#isDisplayable() displayable} + * or has {@code null} {@link java.awt.Component#getGraphicsConfiguration() GraphicsConfiguration}. + * @see #createScreen(java.awt.Component, boolean) + */ + public static MonitorDevice getMonitorDevice(final Screen screen, final java.awt.Component awtComp) throws IllegalArgumentException { + checkComponentValid(awtComp); + final String nwt = NativeWindowFactory.getNativeWindowType(true); + MonitorDevice res = null; + screen.addReference(); + try { + if( NativeWindowFactory.TYPE_MACOSX == nwt ) { + res = screen.getMonitor( JAWTUtil.getMonitorIndex( awtComp.getGraphicsConfiguration().getDevice() ) ); + } + if( null == res ) { + // Fallback, use AWT component coverage + final Point los = AWTMisc.getLocationOnScreenSafe(null, awtComp, false); + final RectangleImmutable r = new Rectangle(los.getX(), los.getY(), awtComp.getWidth(), awtComp.getHeight()); + res = screen.getMainMonitor(r); + } + } finally { + screen.removeReference(); + } + return res; + } } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/awt/TestGearsES2GLJPanelAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/awt/TestGearsES2GLJPanelAWT.java index 4b17f3011..dfc2d1165 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/awt/TestGearsES2GLJPanelAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/awt/TestGearsES2GLJPanelAWT.java @@ -31,8 +31,6 @@ package com.jogamp.opengl.test.junit.jogl.demos.es2.awt; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.Dimension; -import java.awt.DisplayMode; -import java.awt.GraphicsDevice; import java.lang.reflect.InvocationTargetException; import com.jogamp.nativewindow.ScalableSurface; @@ -44,9 +42,12 @@ import com.jogamp.opengl.GLCapabilitiesImmutable; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.awt.GLJPanel; + import javax.swing.JFrame; import javax.swing.SwingUtilities; +import jogamp.newt.awt.NewtFactoryAWT; + import org.junit.AfterClass; import org.junit.Assert; import org.junit.Assume; @@ -55,13 +56,9 @@ import org.junit.Test; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; -import com.jogamp.newt.Display; import com.jogamp.newt.MonitorDevice; -import com.jogamp.newt.NewtFactory; import com.jogamp.newt.Screen; import com.jogamp.newt.event.KeyEvent; -import com.jogamp.newt.event.MonitorEvent; -import com.jogamp.newt.event.MonitorModeListener; import com.jogamp.newt.event.TraceKeyAdapter; import com.jogamp.newt.event.TraceWindowAdapter; import com.jogamp.newt.event.awt.AWTKeyAdapter; @@ -178,6 +175,10 @@ public class TestGearsES2GLJPanelAWT extends UITestCase { Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true)); Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glJPanel, true)); + final Screen screen = NewtFactoryAWT.createScreen(glJPanel, true); + screen.addReference(); // initial native creation - keep alive! + System.err.println("GetPixelScale: AWT -> Screen: "+screen); + final float[] hasSurfacePixelScale1 = glJPanel.getCurrentSurfaceScale(new float[2]); System.err.println("HiDPI PixelScale: "+reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]+" (req) -> "+ valReqSurfacePixelScale[0]+"x"+valReqSurfacePixelScale[1]+" (val) -> "+ @@ -200,36 +201,30 @@ public class TestGearsES2GLJPanelAWT extends UITestCase { if( e.isAutoRepeat() ) { return; } - if(e.getKeyChar()=='p') { + if( e.getKeySymbol() == KeyEvent.VK_P ) { System.err.println(); - final java.awt.Point los = glJPanel.getLocationOnScreen(); - final RectangleImmutable r = new Rectangle(los.x, los.y, glJPanel.getWidth(), glJPanel.getHeight()); - final GraphicsDevice gd = glJPanel.getGraphicsConfiguration().getDevice(); - final DisplayMode dm = gd.getDisplayMode(); - System.err.printf("GetPixelScale: AWT DisplayMode %d x %d pixel-units%n", dm.getWidth(), dm.getHeight()); - System.err.printf("GetPixelScale: NW Screen: %s%n", glJPanel.getNativeSurface().getGraphicsConfiguration().getScreen()); - System.err.printf("GetPixelScale: Panel Bounds: %s window-units%n", r.toString()); - System.err.printf("GetPixelScale: Panel Resolution: %d x %d pixel-units%n", glJPanel.getSurfaceWidth(), glJPanel.getSurfaceHeight()); { - final Display dpy = NewtFactory.createDisplay(null); - final Screen screen = NewtFactory.createScreen(dpy, 0); - screen.addReference(); - final MonitorModeListener sml = new MonitorModeListener() { - @Override - public void monitorModeChangeNotify(final MonitorEvent me) { - } - @Override - public void monitorModeChanged(final MonitorEvent me, final boolean success) { - } - }; - screen.addMonitorModeListener(sml); - try { - final MonitorDevice md = screen.getMainMonitor(r); - System.err.printf("GetPixelScale: %s%n", md.toString()); - } finally { - screen.removeReference(); - } + // Just for manual validation! + final java.awt.Point los = glJPanel.getLocationOnScreen(); + final RectangleImmutable r = new Rectangle(los.x, los.y, glJPanel.getWidth(), glJPanel.getHeight()); + System.err.printf("GetPixelScale: Panel Bounds: %s window-units%n", r.toString()); + System.err.printf("GetPixelScale: Panel Resolution: %d x %d pixel-units%n", glJPanel.getSurfaceWidth(), glJPanel.getSurfaceHeight()); + } + final MonitorDevice monitor = NewtFactoryAWT.getMonitorDevice(screen, glJPanel); + System.err.printf("GetPixelScale: %s%n", monitor.toString()); + final float[] pixelPerMM; + final boolean cached; + if( e.isShiftDown() ) { + // SHIFT: query current mode! + pixelPerMM = monitor.getPixelsPerMM(monitor.queryCurrentMode(), new float[2]); + cached = false; + } else { + // Default: Use cached mode! + pixelPerMM = monitor.getPixelsPerMM(new float[2]); + cached = true; } + System.err.println(" pixel/mm ["+pixelPerMM[0]+", "+pixelPerMM[1]+"], cached-mode "+cached); + System.err.println(" pixel/in ["+pixelPerMM[0]*25.4f+", "+pixelPerMM[1]*25.4f+"], cached-mode "+cached); System.err.println(); } else if(e.getKeyChar()=='x') { final float[] hadSurfacePixelScale = glJPanel.getCurrentSurfaceScale(new float[2]); @@ -313,6 +308,7 @@ public class TestGearsES2GLJPanelAWT extends UITestCase { glJPanel.destroy(); frame.dispose(); } } ); + screen.removeReference(); // final native destroy } @Test |