8175301: Java GUI hangs on Windows when Display set to 125%
authoralexsch
Mon, 13 Mar 2017 22:55:10 +0300
changeset 44342 e8285d724740
parent 44157 8296ab3368eb
child 44343 f0c8e3d65331
8175301: Java GUI hangs on Windows when Display set to 125% Reviewed-by: serb, azvegint
jdk/src/java.desktop/windows/classes/sun/java2d/d3d/D3DGraphicsConfig.java
jdk/src/java.desktop/windows/classes/sun/java2d/d3d/D3DSurfaceData.java
jdk/test/javax/swing/JFrame/8175301/ScaledFrameBackgroundTest.java
--- a/jdk/src/java.desktop/windows/classes/sun/java2d/d3d/D3DGraphicsConfig.java	Fri Mar 10 09:20:55 2017 -0800
+++ b/jdk/src/java.desktop/windows/classes/sun/java2d/d3d/D3DGraphicsConfig.java	Mon Mar 13 22:55:10 2017 +0300
@@ -186,7 +186,17 @@
         SurfaceData sd = d3dvsm.getPrimarySurfaceData();
         if (sd instanceof D3DSurfaceData) {
             D3DSurfaceData d3dsd = (D3DSurfaceData)sd;
-            D3DSurfaceData.swapBuffers(d3dsd, x1, y1, x2, y2);
+            double scaleX = sd.getDefaultScaleX();
+            double scaleY = sd.getDefaultScaleY();
+            if (scaleX > 1 || scaleY > 1) {
+                int sx1 = (int) Math.floor(x1 * scaleX);
+                int sy1 = (int) Math.floor(y1 * scaleY);
+                int sx2 = (int) Math.ceil(x2 * scaleX);
+                int sy2 = (int) Math.ceil(y2 * scaleY);
+                D3DSurfaceData.swapBuffers(d3dsd, sx1, sy1, sx2, sy2);
+            } else {
+                D3DSurfaceData.swapBuffers(d3dsd, x1, y1, x2, y2);
+            }
         } else {
             // the surface was likely lost could not have been restored
             Graphics g = peer.getGraphics();
--- a/jdk/src/java.desktop/windows/classes/sun/java2d/d3d/D3DSurfaceData.java	Fri Mar 10 09:20:55 2017 -0800
+++ b/jdk/src/java.desktop/windows/classes/sun/java2d/d3d/D3DSurfaceData.java	Mon Mar 13 22:55:10 2017 +0300
@@ -766,7 +766,17 @@
                 final Component target = (Component)sd.getPeer().getTarget();
                 SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
                     public void run() {
-                        target.repaint(x1, y1, x2, y2);
+                        double scaleX = sd.getDefaultScaleX();
+                        double scaleY = sd.getDefaultScaleY();
+                        if (scaleX > 1 || scaleY > 1) {
+                            int sx1 = (int) Math.floor(x1 / scaleX);
+                            int sy1 = (int) Math.floor(y1 / scaleY);
+                            int sx2 = (int) Math.ceil(x2 / scaleX);
+                            int sy2 = (int) Math.ceil(y2 / scaleY);
+                            target.repaint(sx1, sy1, sx2 - sx1, sy2 - sy1);
+                        } else {
+                            target.repaint(x1, y1, x2 - x1, y2 - y1);
+                        }
                     }
                 });
                 return;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JFrame/8175301/ScaledFrameBackgroundTest.java	Mon Mar 13 22:55:10 2017 +0300
@@ -0,0 +1,87 @@
+/*
+ * 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.Rectangle;
+import java.awt.Robot;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+
+/**
+ * @test
+ * @bug 8175301
+ * @summary Java GUI hangs on Windows when Display set to 125%
+ * @run main/othervm -Dsun.java2d.uiScale=2 ScaledFrameBackgroundTest
+ * @run main/othervm -Dsun.java2d.uiScale=2 -Dsun.java2d.d3d=true ScaledFrameBackgroundTest
+ * @run main/othervm -Dsun.java2d.uiScale=2 -Dsun.java2d.opengl=true ScaledFrameBackgroundTest
+ */
+public class ScaledFrameBackgroundTest {
+
+    private static final Color BACKGROUND = Color.RED;
+    private static JFrame frame;
+
+    public static void main(String[] args) throws Exception {
+
+        Robot robot = new Robot();
+        robot.setAutoDelay(50);
+
+        SwingUtilities.invokeAndWait(() -> {
+            frame = new JFrame();
+            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+            frame.setSize(400, 300);
+            JPanel panel = new JPanel();
+            panel.setBackground(BACKGROUND);
+            frame.getContentPane().add(panel);
+            frame.setVisible(true);
+        });
+
+        robot.waitForIdle();
+        Thread.sleep(200);
+
+        Rectangle[] rects = new Rectangle[1];
+        SwingUtilities.invokeAndWait(() -> {
+            rects[0] = frame.getBounds();
+        });
+
+        Rectangle bounds = rects[0];
+
+        int x = bounds.x + bounds.width / 4;
+        int y = bounds.y + bounds.height / 4;
+
+        Color color = robot.getPixelColor(x, y);
+
+        if (!BACKGROUND.equals(color)) {
+            throw new RuntimeException("Wrong backgound color!");
+        }
+
+        x = bounds.x + 3 * bounds.width / 4;
+        y = bounds.y + 3 * bounds.height / 4;
+
+        color = robot.getPixelColor(x, y);
+
+        if (!BACKGROUND.equals(color)) {
+            throw new RuntimeException("Wrong backgound color!");
+        }
+    }
+}