From e6d53e29f05a6928192f6c4a988b2aa558be8d65 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 4 Jan 2020 16:58:49 +0100 Subject: Bug 1358: Add OSXUtil.GetWindowPixelScale(..) It is now possible to retrieve the pixel scale [NSWindow backingScaleFactor] after native creation by NEWT - as we have changed the lifecycly (all-in-one native method on AppKit) --- .../jogamp/nativewindow/macosx/OSXUtil.java | 8 ++++ src/nativewindow/native/macosx/OSXmisc.m | 45 +++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java index 1b7ba0d94..e847279e1 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/macosx/OSXUtil.java @@ -143,6 +143,13 @@ public class OSXUtil implements ToolkitProperties { return 1.0f; // default } } + public static float GetWindowPixelScale(final long windowOrView) { + if( 0 != windowOrView ) { + return GetWindowPixelScale1(windowOrView); + } else { + return 1.0f; // default + } + } public static long CreateNSWindow(final int x, final int y, final int width, final int height) { final long res[] = { 0 }; @@ -428,6 +435,7 @@ public class OSXUtil implements ToolkitProperties { private static native Object GetInsets0(long windowOrView); private static native float GetScreenPixelScale1(int displayID); private static native float GetScreenPixelScale2(long windowOrView); + private static native float GetWindowPixelScale1(long windowOrView); private static native long CreateNSWindow0(int x, int y, int width, int height); private static native void DestroyNSWindow0(long nsWindow); private static native long GetNSView0(long nsWindow); diff --git a/src/nativewindow/native/macosx/OSXmisc.m b/src/nativewindow/native/macosx/OSXmisc.m index 51bbdcba3..8cbfde8ed 100644 --- a/src/nativewindow/native/macosx/OSXmisc.m +++ b/src/nativewindow/native/macosx/OSXmisc.m @@ -308,7 +308,7 @@ NS_ENDHANDLER /* * Class: Java_jogamp_nativewindow_macosx_OSXUtil - * Method: GetScreenPixelScale1 + * Method: GetScreenPixelScale2 * Signature: (J)F */ JNIEXPORT jfloat JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_GetScreenPixelScale2 @@ -345,6 +345,49 @@ NS_ENDHANDLER return (jfloat)pixelScale; } +/* + * Class: Java_jogamp_nativewindow_macosx_OSXUtil + * Method: GetWindowPixelScale1 + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_jogamp_nativewindow_macosx_OSXUtil_GetWindowPixelScale1 + (JNIEnv *env, jclass unused, jlong winOrView) +{ + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + NSObject *nsObj = (NSObject*) (intptr_t) winOrView; + NSWindow* win = NULL; + NSView* view = NULL; + NSScreen *screen = NULL; + + if( [nsObj isKindOfClass:[NSWindow class]] ) { + win = (NSWindow*) nsObj; + view = [win contentView]; + screen = [win screen]; + } else if( nsObj != NULL && [nsObj isKindOfClass:[NSView class]] ) { + view = (NSView*) nsObj; + win = [view window]; + screen = [win screen]; + } else { + NativewindowCommon_throwNewRuntimeException(env, "neither win nor view %p\n", nsObj); + } + + BOOL useHiDPI = false; + CGFloat pixelScale = 1.0; // default +NS_DURING + // Available >= 10.7 + useHiDPI = [view wantsBestResolutionOpenGLSurface]; + if( useHiDPI ) { + pixelScale = [win backingScaleFactor]; // HiDPI scaling + } +NS_HANDLER +NS_ENDHANDLER + + [pool release]; + + return (jfloat)pixelScale; +} + /* * Class: Java_jogamp_nativewindow_macosx_OSXUtil * Method: CreateNSWindow0 -- cgit v1.2.3