8168307: Toolkit.getScreenSize() returns incorrect size on unix in multiscreen systems
authorserb
Thu, 02 Mar 2017 17:18:23 +0300
changeset 44149 92b8963f6498
parent 44148 35def8dd7581
child 44150 022f5ba40098
8168307: Toolkit.getScreenSize() returns incorrect size on unix in multiscreen systems Reviewed-by: azvegint, alexsch
jdk/src/java.desktop/macosx/classes/sun/lwawt/LWToolkit.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java
jdk/src/java.desktop/share/classes/sun/awt/HToolkit.java
jdk/src/java.desktop/share/classes/sun/awt/HeadlessToolkit.java
jdk/src/java.desktop/share/classes/sun/awt/SunToolkit.java
jdk/src/java.desktop/unix/classes/sun/awt/X11/XBaseWindow.java
jdk/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java
jdk/src/java.desktop/unix/classes/sun/awt/X11/XWindow.java
jdk/src/java.desktop/windows/classes/sun/awt/Win32GraphicsEnvironment.java
jdk/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java
jdk/test/java/awt/Toolkit/IsToolkitUseTheMainScreen/IsToolkitUseTheMainScreen.java
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/LWToolkit.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/LWToolkit.java	Thu Mar 02 17:18:23 2017 +0300
@@ -359,13 +359,6 @@
     // ---- NON-COMPONENT PEERS ---- //
 
     @Override
-    public final ColorModel getColorModel() throws HeadlessException {
-        return GraphicsEnvironment.getLocalGraphicsEnvironment()
-                                  .getDefaultScreenDevice()
-                                  .getDefaultConfiguration().getColorModel();
-    }
-
-    @Override
     public final boolean isDesktopSupported() {
         return true;
     }
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Thu Mar 02 17:18:23 2017 +0300
@@ -359,18 +359,6 @@
     }
 
     @Override
-    protected int getScreenHeight() {
-        return GraphicsEnvironment.getLocalGraphicsEnvironment()
-                .getDefaultScreenDevice().getDefaultConfiguration().getBounds().height;
-    }
-
-    @Override
-    protected int getScreenWidth() {
-        return GraphicsEnvironment.getLocalGraphicsEnvironment()
-                .getDefaultScreenDevice().getDefaultConfiguration().getBounds().width;
-    }
-
-    @Override
     protected void initializeDesktopProperties() {
         super.initializeDesktopProperties();
         Map <Object, Object> fontHints = new HashMap<>();
--- a/jdk/src/java.desktop/share/classes/sun/awt/HToolkit.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/awt/HToolkit.java	Thu Mar 02 17:18:23 2017 +0300
@@ -175,18 +175,6 @@
     }
 
     @Override
-    public int getScreenHeight()
-        throws HeadlessException {
-        throw new HeadlessException();
-    }
-
-    @Override
-    public int getScreenWidth()
-        throws HeadlessException {
-        throw new HeadlessException();
-    }
-
-    @Override
     public Dimension getScreenSize()
         throws HeadlessException {
         throw new HeadlessException();
--- a/jdk/src/java.desktop/share/classes/sun/awt/HeadlessToolkit.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/awt/HeadlessToolkit.java	Thu Mar 02 17:18:23 2017 +0300
@@ -172,16 +172,6 @@
         return null;
     }
 
-    public int getScreenHeight()
-        throws HeadlessException {
-        throw new HeadlessException();
-    }
-
-    public int getScreenWidth()
-        throws HeadlessException {
-        throw new HeadlessException();
-    }
-
     @Override
     public Dimension getScreenSize()
         throws HeadlessException {
--- a/jdk/src/java.desktop/share/classes/sun/awt/SunToolkit.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/awt/SunToolkit.java	Thu Mar 02 17:18:23 2017 +0300
@@ -30,6 +30,7 @@
 import java.awt.event.KeyEvent;
 import java.awt.event.WindowEvent;
 import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.DataBufferInt;
 import java.awt.image.ImageObserver;
@@ -560,10 +561,17 @@
 
     @Override
     public Dimension getScreenSize() {
-        return new Dimension(getScreenWidth(), getScreenHeight());
+        return GraphicsEnvironment.getLocalGraphicsEnvironment()
+                .getDefaultScreenDevice().getDefaultConfiguration()
+                .getBounds().getSize();
     }
-    protected abstract int getScreenWidth();
-    protected abstract int getScreenHeight();
+
+    @Override
+    public ColorModel getColorModel() throws HeadlessException {
+        return GraphicsEnvironment.getLocalGraphicsEnvironment()
+                .getDefaultScreenDevice().getDefaultConfiguration()
+                .getColorModel();
+    }
 
     @Override
     @SuppressWarnings("deprecation")
--- a/jdk/src/java.desktop/unix/classes/sun/awt/X11/XBaseWindow.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/unix/classes/sun/awt/X11/XBaseWindow.java	Thu Mar 02 17:18:23 2017 +0300
@@ -528,12 +528,12 @@
                     if (maxBounds.width != Integer.MAX_VALUE) {
                         hints.set_max_width(scaleUp(maxBounds.width));
                     } else {
-                        hints.set_max_width(XToolkit.getDefaultScreenWidth());
+                        hints.set_max_width(XToolkit.getMaxWindowWidthInPixels());
                     }
                     if (maxBounds.height != Integer.MAX_VALUE) {
                         hints.set_max_height(scaleUp(maxBounds.height));
                     } else {
-                        hints.set_max_height(XToolkit.getDefaultScreenHeight());
+                        hints.set_max_height(XToolkit.getMaxWindowHeightInPixels());
                     }
                 } else {
                     hints.set_max_width(scaleUp(width));
@@ -545,12 +545,12 @@
                     if (maxBounds.width != Integer.MAX_VALUE) {
                         hints.set_max_width(scaleUp(maxBounds.width));
                     } else {
-                        hints.set_max_width(scaleUp(XToolkit.getDefaultScreenWidth()));
+                        hints.set_max_width(XToolkit.getMaxWindowWidthInPixels());
                     }
                     if (maxBounds.height != Integer.MAX_VALUE) {
                         hints.set_max_height(scaleUp(maxBounds.height));
                     } else {
-                        hints.set_max_height(scaleUp(XToolkit.getDefaultScreenHeight()));
+                        hints.set_max_height(XToolkit.getMaxWindowHeightInPixels());
                     }
                 } else {
                     // Leave intact
--- a/jdk/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java	Thu Mar 02 17:18:23 2017 +0300
@@ -40,7 +40,6 @@
 import java.awt.font.TextAttribute;
 import java.awt.im.InputMethodHighlight;
 import java.awt.im.spi.InputMethodDescriptor;
-import java.awt.image.ColorModel;
 import java.awt.peer.*;
 import java.beans.PropertyChangeListener;
 import java.security.AccessController;
@@ -108,12 +107,17 @@
     static UIDefaults uidefaults;
     static final X11GraphicsEnvironment localEnv;
     private static final X11GraphicsDevice device;
-    private static final X11GraphicsConfig config;
     private static final long display;
     static int awt_multiclick_time;
     static boolean securityWarningEnabled;
 
-    private static volatile int screenWidth = -1, screenHeight = -1; // Dimensions of default screen
+    /**
+     * Dimensions of default virtual screen in pixels. These values are used to
+     * limit the maximum size of the window.
+     */
+    private static volatile int maxWindowWidthInPixels = -1;
+    private static volatile int maxWindowHeightInPixels = -1;
+
     static long awt_defaultFg; // Pixel
     private static XMouseInfoPeer xPeer;
 
@@ -122,13 +126,11 @@
         if (GraphicsEnvironment.isHeadless()) {
             localEnv = null;
             device = null;
-            config = null;
             display = 0;
         } else {
             localEnv = (X11GraphicsEnvironment) GraphicsEnvironment
                 .getLocalGraphicsEnvironment();
             device = (X11GraphicsDevice) localEnv.getDefaultScreenDevice();
-            config = (X11GraphicsConfig) device.getDefaultConfiguration();
             display = device.getDisplay();
             setupModifierMap();
             initIDs();
@@ -652,8 +654,8 @@
                 @Override
                 public void displayChanged() {
                     // 7045370: Reset the cached values
-                    XToolkit.screenWidth = -1;
-                    XToolkit.screenHeight = -1;
+                    XToolkit.maxWindowWidthInPixels = -1;
+                    XToolkit.maxWindowHeightInPixels = -1;
                 }
 
                 @Override
@@ -670,7 +672,7 @@
     }
 
     private static void initScreenSize() {
-        if (screenWidth == -1 || screenHeight == -1) {
+        if (maxWindowWidthInPixels == -1 || maxWindowHeightInPixels == -1) {
             awtLock();
             try {
                 XWindowAttributes pattr = new XWindowAttributes();
@@ -678,8 +680,8 @@
                     XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),
                                                      XToolkit.getDefaultRootWindow(),
                                                      pattr.pData);
-                    screenWidth  = config.scaleDown(pattr.get_width());
-                    screenHeight = config.scaleDown(pattr.get_height());
+                    maxWindowWidthInPixels = pattr.get_width();
+                    maxWindowHeightInPixels = pattr.get_height();
                 } finally {
                     pattr.dispose();
                 }
@@ -689,24 +691,14 @@
         }
     }
 
-    static int getDefaultScreenWidth() {
+    static int getMaxWindowWidthInPixels() {
         initScreenSize();
-        return screenWidth;
+        return maxWindowWidthInPixels;
     }
 
-    static int getDefaultScreenHeight() {
+    static int getMaxWindowHeightInPixels() {
         initScreenSize();
-        return screenHeight;
-    }
-
-    @Override
-    protected int getScreenWidth() {
-        return getDefaultScreenWidth();
-    }
-
-    @Override
-    protected int getScreenHeight() {
-        return getDefaultScreenHeight();
+        return maxWindowHeightInPixels;
     }
 
     private static Rectangle getWorkArea(long root, int scale)
@@ -1373,20 +1365,6 @@
     static native long getDefaultXColormap();
     static native long getDefaultScreenData();
 
-    static ColorModel screenmodel;
-
-    static ColorModel getStaticColorModel() {
-        if (screenmodel == null) {
-            screenmodel = config.getColorModel ();
-        }
-        return screenmodel;
-    }
-
-    @Override
-    public ColorModel getColorModel() {
-        return getStaticColorModel();
-    }
-
     /**
      * Returns a new input method adapter descriptor for native input methods.
      */
--- a/jdk/src/java.desktop/unix/classes/sun/awt/X11/XWindow.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/unix/classes/sun/awt/X11/XWindow.java	Thu Mar 02 17:18:23 2017 +0300
@@ -339,12 +339,13 @@
         return graphicsConfig.getColorModel (transparency);
     }
 
+    @Override
     public ColorModel getColorModel() {
         if (graphicsConfig != null) {
             return graphicsConfig.getColorModel ();
         }
         else {
-            return XToolkit.getStaticColorModel();
+            return Toolkit.getDefaultToolkit().getColorModel();
         }
     }
 
--- a/jdk/src/java.desktop/windows/classes/sun/awt/Win32GraphicsEnvironment.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/windows/classes/sun/awt/Win32GraphicsEnvironment.java	Thu Mar 02 17:18:23 2017 +0300
@@ -203,9 +203,6 @@
                 }
             }
         }
-        // Reset the static GC for the (possibly new) default screen
-        WToolkit.resetGC();
-
         // notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and
         // CachingSurfaceManagers) about the display change event
         displayChanger.notifyListeners();
--- a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java	Thu Mar 02 16:24:53 2017 +0300
+++ b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java	Thu Mar 02 17:18:23 2017 +0300
@@ -80,8 +80,6 @@
     // Desktop property which specifies whether XP visual styles are in effect
     public static final String XPSTYLE_THEME_ACTIVE = "win.xpstyle.themeActive";
 
-    static GraphicsConfiguration config;
-
     // System clipboard.
     WClipboard clipboard;
 
@@ -145,21 +143,6 @@
     private static native void disableCustomPalette();
 
     /*
-     * Reset the static GraphicsConfiguration to the default.  Called on
-     * startup and when display settings have changed.
-     */
-    public static void resetGC() {
-        if (GraphicsEnvironment.isHeadless()) {
-            config = null;
-        } else {
-          config = (GraphicsEnvironment
-                  .getLocalGraphicsEnvironment()
-          .getDefaultScreenDevice()
-          .getDefaultConfiguration());
-        }
-    }
-
-    /*
      * NOTE: The following embedded*() methods are non-public API intended
      * for internal use only.  The methods are unsupported and could go
      * away in future releases.
@@ -615,21 +598,6 @@
     static native ColorModel makeColorModel();
     static ColorModel screenmodel;
 
-    static ColorModel getStaticColorModel() {
-        if (GraphicsEnvironment.isHeadless()) {
-            throw new IllegalArgumentException();
-        }
-        if (config == null) {
-            resetGC();
-        }
-        return config.getColorModel();
-    }
-
-    @Override
-    public ColorModel getColorModel() {
-        return getStaticColorModel();
-    }
-
     @Override
     public Insets getScreenInsets(GraphicsConfiguration gc)
     {
@@ -643,20 +611,6 @@
         return ge.getXResolution();
     }
 
-    @Override
-    protected int getScreenWidth() {
-        return GraphicsEnvironment.getLocalGraphicsEnvironment()
-                .getDefaultScreenDevice().getDefaultConfiguration()
-                .getBounds().width;
-    }
-
-    @Override
-    protected int getScreenHeight() {
-        return GraphicsEnvironment.getLocalGraphicsEnvironment()
-                .getDefaultScreenDevice().getDefaultConfiguration()
-                .getBounds().height;
-    }
-
     private native Insets getScreenInsets(int screen);
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Toolkit/IsToolkitUseTheMainScreen/IsToolkitUseTheMainScreen.java	Thu Mar 02 17:18:23 2017 +0300
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2016, 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.Dimension;
+import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsEnvironment;
+import java.awt.HeadlessException;
+import java.awt.Toolkit;
+import java.awt.image.ColorModel;
+
+/**
+ * @test
+ * @bug 8168307
+ * @run main/othervm IsToolkitUseTheMainScreen
+ * @run main/othervm -Djava.awt.headless=true IsToolkitUseTheMainScreen
+ */
+public final class IsToolkitUseTheMainScreen {
+
+    public static void main(final String[] args) {
+        if (GraphicsEnvironment.isHeadless()) {
+            testHeadless();
+        } else {
+            testHeadful();
+        }
+    }
+
+    private static void testHeadless() {
+        try {
+            Toolkit.getDefaultToolkit().getScreenSize();
+            throw new RuntimeException("HeadlessException is not thrown");
+        } catch (final HeadlessException ignored) {
+            // expected exception
+        }
+        try {
+            Toolkit.getDefaultToolkit().getColorModel();
+            throw new RuntimeException("HeadlessException is not thrown");
+        } catch (final HeadlessException ignored) {
+            // expected exception
+        }
+    }
+
+    private static void testHeadful() {
+        GraphicsEnvironment ge
+                = GraphicsEnvironment.getLocalGraphicsEnvironment();
+        GraphicsConfiguration gc
+                = ge.getDefaultScreenDevice().getDefaultConfiguration();
+        Dimension gcSize = gc.getBounds().getSize();
+        ColorModel gcCM = gc.getColorModel();
+
+        Dimension toolkitSize = Toolkit.getDefaultToolkit().getScreenSize();
+        ColorModel toolkitCM = Toolkit.getDefaultToolkit().getColorModel();
+
+        if (!gcSize.equals(toolkitSize)) {
+            System.err.println("Toolkit size = " + toolkitSize);
+            System.err.println("GraphicsConfiguration size = " + gcSize);
+            throw new RuntimeException("Incorrect size");
+        }
+        if (!gcCM.equals(toolkitCM)) {
+            System.err.println("Toolkit color model = " + toolkitCM);
+            System.err.println("GraphicsConfiguration color model = " + gcCM);
+            throw new RuntimeException("Incorrect color model");
+        }
+    }
+}