From 83be0fa0ffe96c1acbc232965c2c03f40768184d Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 20 May 2014 20:58:40 +0200 Subject: Bug 742 HiDPI: Add access to private HiDPI in AWT pixelScale value in JAWTUtil and JAWTWindow --- .../com/jogamp/nativewindow/awt/JAWTWindow.java | 24 +++++++- .../classes/jogamp/nativewindow/jawt/JAWTUtil.java | 66 +++++++++++++++++++++- 2 files changed, 87 insertions(+), 3 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 e2a4ad4bd..8d7c382ee 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java @@ -46,6 +46,7 @@ import com.jogamp.nativewindow.MutableGraphicsConfiguration; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; +import java.awt.GraphicsDevice; import java.awt.Window; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; @@ -750,6 +751,25 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, return component.hasFocus(); } + /** + * Returns the pixel scale factor of this {@link Component}'s {@link GraphicsDevice}, if supported. + *

+ * If the component is not yet {@link Component#isDisplayable() displayable}, + * zero is returned. + *

+ *

+ * If the component does not support pixel scaling the default + * one is returned. + *

+ *

+ * Note: Currently only supported on OSX since 1.7.0_40 for HiDPI retina displays + *

+ * @return the pixel scale factor + */ + protected final int getPixelScale() { + return JAWTUtil.getPixelScale(component); + } + protected StringBuilder jawt2String(StringBuilder sb) { if( null == sb ) { sb = new StringBuilder(); @@ -760,7 +780,9 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, if(null != jawt) { sb.append("JAWT version: ").append(toHexString(jawt.getCachedVersion())). append(", CA_LAYER: ").append(JAWTUtil.isJAWTUsingOffscreenLayer(jawt)). - append(", isLayeredSurface ").append(isOffscreenLayerSurfaceEnabled()).append(", bounds ").append(bounds).append(", insets ").append(insets); + append(", isLayeredSurface ").append(isOffscreenLayerSurfaceEnabled()). + append(", bounds ").append(bounds).append(", insets ").append(insets). + append(", pixelScale ").append(getPixelScale()); } else { sb.append("JAWT n/a, bounds ").append(bounds).append(", insets ").append(insets); } diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java index d4edf2ba9..fb979d440 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java @@ -37,7 +37,10 @@ package jogamp.nativewindow.jawt; +import java.awt.Component; import java.awt.EventQueue; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Toolkit; import java.lang.reflect.InvocationTargetException; @@ -85,12 +88,15 @@ public class JAWTUtil { private static final RecursiveLock jawtLock; private static final ToolkitLock jawtToolkitLock; + private static final Method getScaleFactorMethod; + private static class PrivilegedDataBlob1 { PrivilegedDataBlob1() { ok = false; } - Method sunToolkitAWTLockMethod; - Method sunToolkitAWTUnlockMethod; + Method sunToolkitAWTLockMethod; + Method sunToolkitAWTUnlockMethod; + Method getScaleFactorMethod; boolean ok; } @@ -308,6 +314,7 @@ public class JAWTUtil { sunToolkitAWTUnlockMethod = null; hasSunToolkitAWTLock = false; // hasSunToolkitAWTLock = false; + getScaleFactorMethod = null; } else { // Non-headless case JAWTJNILibLoader.initSingleton(); // load libjawt.so @@ -342,11 +349,17 @@ public class JAWTUtil { } catch (Exception e) { // Either not a Sun JDK or the interfaces have changed since 1.4.2 / 1.5 } + try { + GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + d.getScaleFactorMethod = gd.getClass().getDeclaredMethod("getScaleFactor"); + d.getScaleFactorMethod.setAccessible(true); + } catch (Throwable t) {} return d; } }); sunToolkitAWTLockMethod = pdb1.sunToolkitAWTLockMethod; sunToolkitAWTUnlockMethod = pdb1.sunToolkitAWTUnlockMethod; + getScaleFactorMethod = pdb1.getScaleFactorMethod; boolean _hasSunToolkitAWTLock = false; if ( pdb1.ok ) { @@ -516,5 +529,54 @@ public class JAWTUtil { return jawtToolkitLock; } + /** + * Returns the pixel scale factor of the given {@link GraphicsDevice}, if supported. + *

+ * If the component does not support pixel scaling the default + * one is returned. + *

+ *

+ * Note: Currently only supported on OSX since 1.7.0_40 for HiDPI retina displays + *

+ * @param device the {@link GraphicsDevice} instance used to query the pixel scale + * @return the pixel scale factor + */ + public static final int getPixelScale(final GraphicsDevice device) { + if( null != getScaleFactorMethod ) { + try { + final Object res = getScaleFactorMethod.invoke(device); + if (res instanceof Integer) { + return ((Integer)res).intValue(); + } + } catch (Throwable t) {} + } + return 1; + } + + /** + * Returns the pixel scale factor of the given {@link Component}'s {@link GraphicsDevice}, if supported. + *

+ * If the component is not yet {@link Component#isDisplayable() displayable}, + * zero is returned. + *

+ *

+ * If the component does not support pixel scaling the default + * one is returned. + *

+ *

+ * Note: Currently only supported on OSX since 1.7.0_40 for HiDPI retina displays + *

+ * @param component the {@link Component} instance used to query the pixel scale + * @return the pixel scale factor + */ + public static final int getPixelScale(final Component component) { + final GraphicsConfiguration gc = component.getGraphicsConfiguration(); + final GraphicsDevice device = null != gc ? gc.getDevice() : null; + if( null == device ) { + return 0; + } else { + return JAWTUtil.getPixelScale(device); + } + } } -- cgit v1.2.3