8225118: Robot.createScreenCapture() returns black image on HiDPI linux with gtk3
authorant
Thu, 06 Jun 2019 15:46:36 +0300
changeset 55362 37819ad0ac6f
parent 55361 ed5c7d68ed5a
child 55363 f680bedc0dcb
8225118: Robot.createScreenCapture() returns black image on HiDPI linux with gtk3 Reviewed-by: serb
src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.c
src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.h
test/jdk/java/awt/Robot/HiDPIScreenCapture/HiDPIRobotScreenCaptureTest.java
--- a/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.c	Tue Jun 04 14:43:57 2019 +0530
+++ b/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.c	Thu Jun 06 15:46:36 2019 +0300
@@ -324,6 +324,8 @@
         /* GDK */
         fp_gdk_get_default_root_window =
             dl_symbol("gdk_get_default_root_window");
+        fp_gdk_window_get_scale_factor =
+                    dl_symbol("gdk_window_get_scale_factor");
 
         /* Pixbuf */
         fp_gdk_pixbuf_new = dl_symbol("gdk_pixbuf_new");
@@ -2888,7 +2890,10 @@
     jint *ary;
 
     GdkWindow *root = (*fp_gdk_get_default_root_window)();
-    pixbuf = (*fp_gdk_pixbuf_get_from_drawable)(root, x, y, width, height);
+    int win_scale = (*fp_gdk_window_get_scale_factor)(root);
+    pixbuf = (*fp_gdk_pixbuf_get_from_drawable)(
+        root, x, y, (int)(width / (float)win_scale + 0.5), (int)(height / (float)win_scale + 0.5));
+
     if (pixbuf && scale != 1) {
         GdkPixbuf *scaledPixbuf;
         x /= scale;
@@ -2906,8 +2911,8 @@
     if (pixbuf) {
         int nchan = (*fp_gdk_pixbuf_get_n_channels)(pixbuf);
         int stride = (*fp_gdk_pixbuf_get_rowstride)(pixbuf);
-        if ((*fp_gdk_pixbuf_get_width)(pixbuf) == width
-                && (*fp_gdk_pixbuf_get_height)(pixbuf) == height
+        if ((*fp_gdk_pixbuf_get_width)(pixbuf) >= width
+                && (*fp_gdk_pixbuf_get_height)(pixbuf) >= height
                 && (*fp_gdk_pixbuf_get_bits_per_sample)(pixbuf) == 8
                 && (*fp_gdk_pixbuf_get_colorspace)(pixbuf) == GDK_COLORSPACE_RGB
                 && nchan >= 3
--- a/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.h	Tue Jun 04 14:43:57 2019 +0530
+++ b/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.h	Thu Jun 06 15:46:36 2019 +0300
@@ -253,6 +253,7 @@
 static void (*fp_g_free)(gpointer mem);
 static void (*fp_g_object_unref)(gpointer object);
 static GdkWindow *(*fp_gdk_get_default_root_window) (void);
+static int (*fp_gdk_window_get_scale_factor) (GdkWindow *window);
 
 static int (*fp_gdk_pixbuf_get_bits_per_sample)(const GdkPixbuf *pixbuf);
 static guchar *(*fp_gdk_pixbuf_get_pixels)(const GdkPixbuf *pixbuf);
--- a/test/jdk/java/awt/Robot/HiDPIScreenCapture/HiDPIRobotScreenCaptureTest.java	Tue Jun 04 14:43:57 2019 +0530
+++ b/test/jdk/java/awt/Robot/HiDPIScreenCapture/HiDPIRobotScreenCaptureTest.java	Thu Jun 06 15:46:36 2019 +0300
@@ -38,7 +38,7 @@
  * @bug 8073320
  * @summary  Windows HiDPI support
  * @author Alexander Scherbatiy
- * @requires (os.family == "windows")
+ * @requires (os.family == "linux" | os.family == "windows")
  * @run main/othervm -Dsun.java2d.win.uiScaleX=3 -Dsun.java2d.win.uiScaleY=2
  *                    HiDPIRobotScreenCaptureTest
  */
@@ -50,11 +50,13 @@
 
     public static void main(String[] args) throws Exception {
 
-        try {
-            UIManager.setLookAndFeel(
-                    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
-        } catch (Exception e) {
-            return;
+        if (System.getProperty("os.name").toLowerCase().contains("win")) {
+            try {
+                UIManager.setLookAndFeel(
+                        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
+            } catch (Exception e) {
+                return;
+            }
         }
 
         Frame frame = new Frame();