8175293: Window size is not updated after setting location to display with different DPI
Reviewed-by: serb
--- a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java Sun Feb 26 23:34:34 2017 +0300
+++ b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java Mon Feb 27 09:26:41 2017 +0300
@@ -554,9 +554,8 @@
float newScaleY = newDev.getDefaultScaleY();
if (scaleX != newScaleX || scaleY != newScaleY) {
- if (oldDev.getScreen() == newDev.getScreen()) {
- windowDPIChange(scaleX, scaleY, newScaleX, newScaleY);
- }
+ windowDPIChange(oldDev.getScreen(), scaleX, scaleY,
+ newDev.getScreen(), newScaleX, newScaleY);
scaleX = newScaleX;
scaleY = newScaleY;
}
@@ -802,8 +801,8 @@
}
}
- native void windowDPIChange(float prevScaleX, float prevScaleY,
- float newScaleX, float newScaleY);
+ native void windowDPIChange(int prevScreen, float prevScaleX, float prevScaleY,
+ int newScreen, float newScaleX, float newScaleY);
/*
* The method maps the list of the active windows to the window's AppContext,
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Component.cpp Sun Feb 26 23:34:34 2017 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Component.cpp Mon Feb 27 09:26:41 2017 +0300
@@ -1505,6 +1505,9 @@
mr = WmSysCommand(static_cast<UINT>(wParam & 0xFFF0),
GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
break;
+ case WM_ENTERSIZEMOVE:
+ mr = WmEnterSizeMove();
+ break;
case WM_EXITSIZEMOVE:
mr = WmExitSizeMove();
break;
@@ -2051,6 +2054,11 @@
return mrDoDefault;
}
+MsgRouting AwtComponent::WmEnterSizeMove()
+{
+ return mrDoDefault;
+}
+
MsgRouting AwtComponent::WmExitSizeMove()
{
return mrDoDefault;
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Component.h Sun Feb 26 23:34:34 2017 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Component.h Mon Feb 27 09:26:41 2017 +0300
@@ -583,6 +583,7 @@
virtual MsgRouting WmNcPaint(HRGN hrgn);
virtual MsgRouting WmNcHitTest(UINT x, UINT y, LRESULT &retVal);
virtual MsgRouting WmSysCommand(UINT uCmdType, int xPos, int yPos);
+ virtual MsgRouting WmEnterSizeMove();
virtual MsgRouting WmExitSizeMove();
virtual MsgRouting WmEnterMenuLoop(BOOL isTrackPopupMenu);
virtual MsgRouting WmExitMenuLoop(BOOL isTrackPopupMenu);
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Window.cpp Sun Feb 26 23:34:34 2017 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Window.cpp Mon Feb 27 09:26:41 2017 +0300
@@ -156,8 +156,10 @@
// struct for _WindowDPIChange() method
struct ScaleStruct {
jobject window;
+ jint prevScreen;
jfloat prevScaleX;
jfloat prevScaleY;
+ jint screen;
jfloat scaleX;
jfloat scaleY;
};
@@ -236,6 +238,8 @@
m_alwaysOnTop = false;
fullScreenExclusiveModeState = FALSE;
+ m_winSizeMove = FALSE;
+ prevScaleRec = { -1, -1, -1 };
}
AwtWindow::~AwtWindow()
@@ -1761,9 +1765,6 @@
(env)->SetIntField(peer, AwtWindow::sysYID, ScaleDownY(rect.top));
SendComponentEvent(java_awt_event_ComponentEvent_COMPONENT_MOVED);
- prevX = rect.left;
- prevY = rect.top;
-
env->DeleteLocalRef(target);
return AwtComponent::WmMove(x, y);
}
@@ -1803,6 +1804,19 @@
return mrDoDefault;
}
+MsgRouting AwtWindow::WmEnterSizeMove()
+{
+ m_winSizeMove = TRUE;
+ return mrDoDefault;
+}
+
+MsgRouting AwtWindow::WmExitSizeMove()
+{
+ m_winSizeMove = FALSE;
+ CheckWindowDPIChange();
+ return mrDoDefault;
+}
+
/*
* Override AwtComponent's size handling to first update the
* java AWT target's dimension fields directly, since Windows
@@ -2064,8 +2078,6 @@
int curScrn = GetScreenImOn();
if (curScrn != m_screenNum) { // we've been moved
- int prevScrn = m_screenNum;
-
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
jclass peerCls = env->GetObjectClass(m_peerObject);
@@ -2081,79 +2093,71 @@
}
env->CallVoidMethod(m_peerObject, draggedID);
-
m_screenNum = curScrn;
- WindowDPIChange(prevScrn, curScrn);
env->DeleteLocalRef(peerCls);
}
}
-int Disposition(int x1, int x2, int x) {
- return x < x1 ? -1 : (x > x2 ? 1 : 0);
-}
-
-void AwtWindow::WindowDPIChange(int prevScreen, int screen) {
- Devices::InstanceAccess devices;
- AwtWin32GraphicsDevice* prevDevice = devices->GetDevice(prevScreen);
- AwtWin32GraphicsDevice* device = devices->GetDevice(screen);
-
- if (prevDevice && device) {
- RECT prevBounds;
- RECT bounds;
-
- if (MonitorBounds(prevDevice->GetMonitor(), &prevBounds)
- && MonitorBounds(device->GetMonitor(), &bounds)) {
- int x;
- int y;
- int dx;
- int dy;
- RECT rect;
-
- ::GetWindowRect(GetHWnd(), &rect);
- x = rect.left;
- y = rect.top;
- dx = x - prevX;
- dy = y - prevY;
-
- if (dx != 0 || dy != 0) {
- int w = rect.right - rect.left;
- int h = rect.bottom - rect.top;
- int dispX = Disposition(prevBounds.left, prevBounds.right,
- (bounds.left + bounds.right) / 2);
- int dispY = Disposition(prevBounds.top, prevBounds.bottom,
- (bounds.top + bounds.bottom) / 2);
-
- w = w * device->GetScaleX() / prevDevice->GetScaleX();
- h = h * device->GetScaleY() / prevDevice->GetScaleY();
-
- prevX = x;
- prevY = y;
-
- if (dx != 0 && dispX != 0) {
- x = dispX > 0 ? bounds.left : bounds.right - w;
- y = min(y, bounds.top);
- ReshapeNoScale(x, y, w, h);
- } else if (dy != 0 && dispY != 0) {
- x = max(x, bounds.left);
- y = dispY > 0 ? bounds.top : bounds.bottom - h;
- ReshapeNoScale(x, y, w, h);
+void AwtWindow::CheckWindowDPIChange() {
+
+ if (prevScaleRec.screen != -1 ) {
+ float prevScaleX = prevScaleRec.scaleX;
+ float prevScaleY = prevScaleRec.scaleY;
+
+ if (prevScaleX >= 1 && prevScaleY >= 1) {
+ Devices::InstanceAccess devices;
+ AwtWin32GraphicsDevice* device = devices->GetDevice(m_screenNum);
+ if (device) {
+ float scaleX = device->GetScaleX();
+ float scaleY = device->GetScaleY();
+ if (prevScaleX != scaleX || prevScaleY != scaleY) {
+ WindowDPIChange(prevScaleRec.screen, prevScaleX, prevScaleY,
+ m_screenNum, scaleX, scaleY);
}
}
}
+ prevScaleRec.screen = -1;
}
}
-void AwtWindow::WindowDPIChange(float prevScaleX, float prevScaleY, float scaleX, float scaleY) {
+void AwtWindow::WindowDPIChange(int prevScreen,
+ float prevScaleX, float prevScaleY,
+ int screen, float scaleX,
+ float scaleY)
+{
+ int x;
+ int y;
int w;
int h;
RECT rect;
+ if (prevScaleX == scaleX && prevScaleY == scaleY) {
+ return;
+ }
+
::GetWindowRect(GetHWnd(), &rect);
-
+ x = rect.left;
+ y = rect.top;
w = (rect.right - rect.left) * scaleX / prevScaleX;
h = (rect.bottom - rect.top) * scaleY / prevScaleY;
- ReshapeNoScale(rect.left, rect.top, w, h);
+
+ if (prevScreen != screen) {
+ Devices::InstanceAccess devices;
+ AwtWin32GraphicsDevice* device = devices->GetDevice(screen);
+ if (device) {
+ RECT bounds;
+ if (MonitorBounds(device->GetMonitor(), &bounds)) {
+ x = x < bounds.left ? bounds.left : x;
+ y = y < bounds.top ? bounds.top : y;
+
+ x = (x + w > bounds.right) ? bounds.right - w : x;
+ y = (y + h > bounds.bottom) ? bounds.bottom - h : y;
+ }
+ }
+ }
+
+ ReshapeNoScale(x, y, w, h);
}
BOOL AwtWindow::IsFocusableWindow() {
@@ -3191,8 +3195,10 @@
ScaleStruct *ss = (ScaleStruct *)param;
jobject self = ss->window;
+ jint prevScreen = ss->prevScreen;
jfloat prevScaleX = ss->prevScaleX;
jfloat prevScaleY = ss->prevScaleY;
+ jint screen = ss->screen;
jfloat scaleX = ss->scaleX;
jfloat scaleY = ss->scaleY;
@@ -3200,7 +3206,17 @@
JNI_CHECK_PEER_GOTO(self, ret);
AwtWindow *window = (AwtWindow *)pData;
- window->WindowDPIChange(prevScaleX, prevScaleY, scaleX, scaleY);
+ if (window->m_winSizeMove) {
+ if (window->prevScaleRec.screen == -1) {
+ window->prevScaleRec.screen = prevScreen;
+ window->prevScaleRec.scaleX = prevScaleX;
+ window->prevScaleRec.scaleY = prevScaleY;
+ }
+ }
+ else {
+ window->WindowDPIChange(prevScreen, prevScaleX, prevScaleY,
+ screen, scaleX, scaleY);
+ }
ret:
env->DeleteGlobalRef(self);
@@ -3908,18 +3924,21 @@
/*
* Class: sun_awt_windows_WWindowPeer
* Method: windowDPIChange
-* Signature: (FFFF)V
+* Signature: (IFFIFF)V
*/
JNIEXPORT void JNICALL
Java_sun_awt_windows_WWindowPeer_windowDPIChange(JNIEnv *env, jobject self,
- jfloat prevScaleX, jfloat prevScaleY, jfloat scaleX, jfloat scaleY)
+ jint prevScreen, jfloat prevScaleX, jfloat prevScaleY,
+ jint screen, jfloat scaleX, jfloat scaleY)
{
TRY;
ScaleStruct *ss = new ScaleStruct;
ss->window = env->NewGlobalRef(self);
+ ss->prevScreen = prevScreen;
ss->prevScaleX = prevScaleX;
ss->prevScaleY = prevScaleY;
+ ss->screen = screen;
ss->scaleX = scaleX;
ss->scaleY = scaleY;
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Window.h Sun Feb 26 23:34:34 2017 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Window.h Mon Feb 27 09:26:41 2017 +0300
@@ -173,6 +173,8 @@
virtual MsgRouting WmMove(int x, int y);
virtual MsgRouting WmSize(UINT type, int w, int h);
virtual MsgRouting WmSizing();
+ virtual MsgRouting WmEnterSizeMove();
+ virtual MsgRouting WmExitSizeMove();
virtual MsgRouting WmPaint(HDC hDC);
virtual MsgRouting WmSettingChange(UINT wFlag, LPCTSTR pszSection);
virtual MsgRouting WmNcCalcSize(BOOL fCalcValidRects,
@@ -384,12 +386,20 @@
private:
int m_screenNum;
- int prevX;
- int prevY;
+
+ typedef struct {
+ jint screen;
+ jfloat scaleX;
+ jfloat scaleY;
+ } ScaleRec;
+
+ BOOL m_winSizeMove;
+ ScaleRec prevScaleRec;
void InitOwner(AwtWindow *owner);
- void WindowDPIChange(int prevScreen, int newScreen);
- void WindowDPIChange(float prevScaleX, float prevScaleY, float scaleX, float scaleY);
+ void CheckWindowDPIChange();
+ void WindowDPIChange(int prevScreen, float prevScaleX, float prevScaleY,
+ int newScreen, float scaleX, float scaleY);
Type m_windowType;
void InitType(JNIEnv *env, jobject peer);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Window/WindowResizingOnDPIChanging/WindowResizingOnSetLocationTest.java Mon Feb 27 09:26:41 2017 +0300
@@ -0,0 +1,307 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Color;
+import java.awt.FlowLayout;
+import java.awt.Font;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.HeadlessException;
+import java.awt.Image;
+import java.awt.Insets;
+import java.awt.Rectangle;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.geom.AffineTransform;
+import java.awt.image.AbstractMultiResolutionImage;
+import java.awt.image.BufferedImage;
+import java.awt.image.ImageObserver;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
+
+/* @test
+ * @bug 8175293
+ * @summary HiDPI (Windows): Swing components have incorrect sizes after
+ * changing display resolution
+ * @run main/manual/othervm WindowResizingOnSetLocationTest
+ */
+public class WindowResizingOnSetLocationTest {
+
+ private static volatile boolean testResult = false;
+ private static volatile CountDownLatch countDownLatch;
+ private static TestFrame frame;
+ private static JFrame mainFrame;
+
+ private static final String INSTRUCTIONS = "INSTRUCTIONS:\n"
+ + "Verify that a window is properly resized after setting the location"
+ + " to a display with different DPI.\n"
+ + "\n"
+ + "The test is applicable for a multi-monitor system where displays"
+ + " are configured to have different DPI\n"
+ + "\n"
+ + "1. Press Show Frame button\n"
+ + "The frame appear.\n"
+ + "2. Check that the string \"scales [ScaleX, ScaleY]\" is painted on the window"
+ + " where ScaleX and ScaleY are the scales for current display.\n"
+ + "The scales are calculated as DPI / 96 and are 1 for the DPI value 96"
+ + " and 2 for the DPI value 192.\n"
+ + "3. Press 'Move to another display' button.\n"
+ + "4. Check that the frame appears on the another display.\n"
+ + "5. Check that the string \"scales [ScaleX, ScaleY]\" is updated"
+ + " to show the right display scales.\n"
+ + "6. Check that the window is properly resized.\n"
+ + "7. Check that the window is properly repainted and does not contain drawing artifacts\n"
+ + "Try different display positions (left, right, top, bottom).\n"
+ + "If all tests are passed, press PASS, else press FAIL.\n";
+
+ public static void main(String args[]) throws Exception {
+
+ countDownLatch = new CountDownLatch(1);
+ SwingUtilities.invokeLater(WindowResizingOnSetLocationTest::createUI);
+ countDownLatch.await(15, TimeUnit.MINUTES);
+ if (!testResult) {
+ throw new RuntimeException("Test fails!");
+ }
+ }
+
+ private static void createUI() {
+
+ mainFrame = new JFrame("DPI change test");
+ GridBagLayout layout = new GridBagLayout();
+ JPanel mainControlPanel = new JPanel(layout);
+ JPanel resultButtonPanel = new JPanel(layout);
+
+ GridBagConstraints gbc = new GridBagConstraints();
+
+ JPanel testPanel = new JPanel(new FlowLayout());
+ JButton frameButton = new JButton("Show Frame");
+ frameButton.addActionListener((e) -> {
+ int x = 20;
+ int y = 10;
+ int w = 400;
+ int h = 300;
+
+ frame = new TestFrame(w, h);
+ frame.setLocation(x, y);
+ frame.setVisible(true);
+
+ });
+ testPanel.add(frameButton);
+
+ gbc.gridx = 0;
+ gbc.gridy = 0;
+ gbc.fill = GridBagConstraints.HORIZONTAL;
+ mainControlPanel.add(testPanel, gbc);
+
+ JTextArea instructionTextArea = new JTextArea();
+ instructionTextArea.setText(INSTRUCTIONS);
+ instructionTextArea.setEditable(false);
+ instructionTextArea.setBackground(Color.white);
+
+ gbc.gridx = 0;
+ gbc.gridy = 1;
+ gbc.fill = GridBagConstraints.HORIZONTAL;
+ mainControlPanel.add(instructionTextArea, gbc);
+
+ JButton passButton = new JButton("Pass");
+ passButton.setActionCommand("Pass");
+ passButton.addActionListener((ActionEvent e) -> {
+ testResult = true;
+ disposeFrames();
+ countDownLatch.countDown();
+
+ });
+
+ JButton failButton = new JButton("Fail");
+ failButton.setActionCommand("Fail");
+ failButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ disposeFrames();
+ countDownLatch.countDown();
+ }
+ });
+
+ gbc.gridx = 0;
+ gbc.gridy = 0;
+ resultButtonPanel.add(passButton, gbc);
+
+ gbc.gridx = 1;
+ gbc.gridy = 0;
+ resultButtonPanel.add(failButton, gbc);
+
+ gbc.gridx = 0;
+ gbc.gridy = 2;
+ mainControlPanel.add(resultButtonPanel, gbc);
+
+ mainFrame.add(mainControlPanel);
+ mainFrame.pack();
+
+ mainFrame.addWindowListener(new WindowAdapter() {
+
+ @Override
+ public void windowClosing(WindowEvent e) {
+ disposeFrames();
+ countDownLatch.countDown();
+ }
+ });
+ mainFrame.setVisible(true);
+ }
+
+ private static void disposeFrames() {
+ if (frame != null && frame.isVisible()) {
+ frame.dispose();
+ }
+
+ if (mainFrame != null && mainFrame.isVisible()) {
+ mainFrame.dispose();
+ }
+ }
+
+ static class TestFrame extends JFrame {
+
+ private final TestMultiResolutionImage mrImage;
+
+ public TestFrame(int width, int height) throws HeadlessException {
+ super("Test Frame");
+ setSize(width, height);
+ mrImage = new TestMultiResolutionImage(width, height);
+
+ JPanel panel = new JPanel(new FlowLayout()) {
+ @Override
+ public void paint(Graphics g) {
+ super.paint(g);
+ AffineTransform tx = ((Graphics2D) g).getTransform();
+ mrImage.scaleX = tx.getScaleX();
+ mrImage.scaleY = tx.getScaleY();
+ Insets insets = getInsets();
+ g.drawImage(mrImage, insets.left, insets.bottom, null);
+ }
+ };
+
+ JButton button = new JButton("Move to another display");
+ button.addActionListener((e) -> {
+ GraphicsConfiguration config = getGraphicsConfiguration();
+ GraphicsDevice device = config.getDevice();
+
+ GraphicsDevice[] devices = GraphicsEnvironment
+ .getLocalGraphicsEnvironment()
+ .getScreenDevices();
+
+ boolean found = false;
+ for (GraphicsDevice dev : devices) {
+ if (!dev.equals(device)) {
+ found = true;
+ Rectangle bounds = dev.getDefaultConfiguration().getBounds();
+
+ AffineTransform tx = config.getDefaultTransform();
+ int x = (int) Math.round(bounds.x / tx.getScaleX()) + 15;
+ int y = (int) Math.round(bounds.y / tx.getScaleY()) + 15;
+ frame.setLocation(x, y);
+ break;
+ }
+ }
+
+ if (!found) {
+ System.out.println("Another display not found!");
+ }
+ });
+
+ panel.add(button);
+ add(panel);
+ }
+ }
+
+ static class TestMultiResolutionImage extends AbstractMultiResolutionImage {
+
+ final int width;
+ final int height;
+ double scaleX;
+ double scaleY;
+
+ public TestMultiResolutionImage(int width, int height) {
+ this.width = width;
+ this.height = height;
+ }
+
+ @Override
+ public int getWidth(ImageObserver observer) {
+ return width;
+ }
+
+ @Override
+ public int getHeight(ImageObserver observer) {
+ return height;
+ }
+
+ @Override
+ protected Image getBaseImage() {
+ return getResolutionVariant(width, height);
+ }
+
+ @Override
+ public Image getResolutionVariant(double destImageWidth, double destImageHeight) {
+
+ int w = (int) destImageWidth;
+ int h = (int) destImageHeight;
+
+ BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
+ Graphics2D g = img.createGraphics();
+ g.scale(scaleX, scaleY);
+ int red = (int) (255 / scaleX);
+ int green = (int) (250 / scaleX);
+ int blue = (int) (20 / scaleX);
+ g.setColor(new Color(red, green, blue));
+ g.fillRect(0, 0, width, height);
+
+ g.setColor(Color.decode("#87CEFA"));
+ Font f = g.getFont();
+ g.setFont(new Font(f.getName(), Font.BOLD, 24));
+ g.drawString(String.format("scales: [%1.2fx, %1.2fx]", scaleX, scaleY),
+ width / 6, height / 2);
+
+ g.dispose();
+ return img;
+ }
+
+ @Override
+ public List<Image> getResolutionVariants() {
+ return Collections.unmodifiableList(Arrays.asList(getBaseImage()));
+ }
+ }
+}