Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit f7aa8b5

Browse files
author
Yuri Nesterenko
committed
8243925: Toolkit#getScreenInsets() returns wrong value on HiDPI screens (Windows)
Reviewed-by: bae Backport-of: 9c415c4
1 parent b6077e7 commit f7aa8b5

File tree

3 files changed

+134
-13
lines changed

3 files changed

+134
-13
lines changed

src/java.desktop/windows/native/libawt/windows/awt_Toolkit.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,13 +2693,14 @@ Java_sun_awt_windows_WToolkit_getScreenInsets(JNIEnv *env,
26932693
jclass insetsClass = env->FindClass("java/awt/Insets");
26942694
DASSERT(insetsClass != NULL);
26952695
CHECK_NULL_RETURN(insetsClass, NULL);
2696-
2696+
Devices::InstanceAccess devices;
2697+
AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
26972698
insets = env->NewObject(insetsClass,
26982699
AwtToolkit::insetsMID,
2699-
rect.top,
2700-
rect.left,
2701-
rect.bottom,
2702-
rect.right);
2700+
device == NULL ? rect.top : device->ScaleDownY(rect.top),
2701+
device == NULL ? rect.left : device->ScaleDownX(rect.left),
2702+
device == NULL ? rect.bottom : device->ScaleDownY(rect.bottom),
2703+
device == NULL ? rect.right : device->ScaleDownX(rect.right));
27032704
}
27042705

27052706
if (safe_ExceptionOccurred(env)) {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.GraphicsDevice;
25+
import java.awt.GraphicsEnvironment;
26+
import java.awt.Insets;
27+
import java.awt.Toolkit;
28+
import java.util.List;
29+
import java.util.concurrent.TimeUnit;
30+
31+
import jdk.test.lib.process.OutputAnalyzer;
32+
import jdk.test.lib.process.ProcessTools;
33+
34+
/**
35+
* @test
36+
* @bug 8243925
37+
* @key headful
38+
* @requires (os.family == "windows")
39+
* @summary Verifies Toolkit.getScreenInsets using different DPI
40+
* @library /test/lib
41+
* @run main/othervm -Dsun.java2d.uiScale=1 ScreenInsetsDPIVariation
42+
*/
43+
public final class ScreenInsetsDPIVariation {
44+
45+
public static void main(String[] args) throws Exception {
46+
var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
47+
GraphicsDevice[] screenDevices = ge.getScreenDevices();
48+
if (args.length == 0) {
49+
for (int i = 0; i < screenDevices.length; i++) {
50+
var gd = screenDevices[i];
51+
var gc = gd.getDefaultConfiguration();
52+
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
53+
checkAllDPI(i, insets);
54+
}
55+
} else {
56+
int screen = Integer.parseInt(args[0]);
57+
int left = Integer.parseInt(args[1]);
58+
int right = Integer.parseInt(args[2]);
59+
int top = Integer.parseInt(args[3]);
60+
int bottom = Integer.parseInt(args[4]);
61+
double scale = Double.parseDouble(args[5]);
62+
63+
System.err.println("screen = " + screen);
64+
System.err.println("scale = " + scale);
65+
if (screen >= screenDevices.length) {
66+
return; // devices were changed, skip
67+
}
68+
var gc = screenDevices[screen].getDefaultConfiguration();
69+
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
70+
check(insets.left, left / scale);
71+
check(insets.right, right / scale);
72+
check(insets.top, top / scale);
73+
check(insets.bottom, bottom / scale);
74+
}
75+
}
76+
77+
private static void check(int actual, double expected) {
78+
if (actual != clipRound(expected)) {
79+
System.err.println("Expected: " + expected);
80+
System.err.println("Actual: " + actual);
81+
throw new RuntimeException("Wrong size");
82+
}
83+
}
84+
85+
private static void checkAllDPI(int screen, Insets insets)
86+
throws Exception {
87+
for (String dpi : List.of("1", "1.5", "1.75", "2", "2.5", "3", "3.1")) {
88+
runProcess(dpi, screen, insets);
89+
}
90+
}
91+
92+
public static int clipRound(double coordinate) {
93+
double newv = coordinate - 0.5;
94+
if (newv < Integer.MIN_VALUE) {
95+
return Integer.MIN_VALUE;
96+
}
97+
if (newv > Integer.MAX_VALUE) {
98+
return Integer.MAX_VALUE;
99+
}
100+
return (int) Math.ceil(newv);
101+
}
102+
103+
private static void runProcess(String dpi, int screen, Insets insets)
104+
throws Exception {
105+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
106+
"-Dsun.java2d.uiScale=" + dpi,
107+
ScreenInsetsDPIVariation.class.getSimpleName(),
108+
String.valueOf(screen), String.valueOf(insets.left),
109+
String.valueOf(insets.right), String.valueOf(insets.top),
110+
String.valueOf(insets.bottom), dpi);
111+
Process worker = ProcessTools.startProcess("Worker", pb, null, 20,
112+
TimeUnit.SECONDS);
113+
new OutputAnalyzer(worker).shouldHaveExitValue(0);
114+
}
115+
}

test/jdk/java/awt/Window/MinimumSizeDPIVariation/MinimumSizeDPIVariation.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ public static void main(String[] args) throws Exception {
5959
System.err.println("comp = " + comp);
6060
System.err.println("scale = " + scale);
6161

62-
Dimension minimumSize = switch (comp) {
63-
case "frame" -> test(new Frame());
64-
case "window" -> test(new Window(null));
65-
case "dialog" -> test(new Dialog((Frame) null));
66-
default -> throw new java.lang.IllegalStateException(
62+
Dimension minimumSize = null;
63+
if (comp.equals("frame")) {
64+
minimumSize = test(new Frame());
65+
} else if (comp.equals("window")) {
66+
minimumSize = test(new Window(null));
67+
} else if (comp.equals("dialog")) {
68+
minimumSize = test(new Dialog((Frame)null));
69+
} else {
70+
throw new java.lang.IllegalStateException(
6771
"Unexpected value: " + comp);
68-
};
72+
}
73+
6974
check(minimumSize.width, Math.max(w / scale, 1));
7075
check(minimumSize.height, Math.max(h / scale, 1));
7176
}
@@ -103,12 +108,12 @@ private static void checkAllDPI(String comp, int w, int h)
103108
throws Exception {
104109
if (!Platform.isOSX()) {
105110
for (String dpi : List.of("1.5", "1.75", "2", "2.5")) {
106-
runPocess(dpi, comp, w, h);
111+
runProcess(dpi, comp, w, h);
107112
}
108113
}
109114
}
110115

111-
private static void runPocess(String dpi, String comp, int w, int h)
116+
private static void runProcess(String dpi, String comp, int w, int h)
112117
throws Exception {
113118
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
114119
"-Dsun.java2d.uiScale=" + dpi,

0 commit comments

Comments
 (0)