8069361: SunGraphics2D.getDefaultTransform() does not include scale factor
authoralexsch
Wed, 22 Apr 2015 13:46:03 +0400
changeset 30475 d55fb9992239
parent 30474 afc246c255e1
child 30476 dd563be4f10f
8069361: SunGraphics2D.getDefaultTransform() does not include scale factor Reviewed-by: flar, prr, serb
jdk/src/java.desktop/macosx/classes/sun/awt/CGraphicsConfig.java
jdk/src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java
jdk/test/java/awt/Graphics2D/ScaledTransform/ScaledTransform.java
--- a/jdk/src/java.desktop/macosx/classes/sun/awt/CGraphicsConfig.java	Tue Apr 21 20:11:59 2015 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/awt/CGraphicsConfig.java	Wed Apr 22 13:46:03 2015 +0400
@@ -72,7 +72,8 @@
 
     @Override
     public AffineTransform getDefaultTransform() {
-        return new AffineTransform();
+        double scaleFactor = device.getScaleFactor();
+        return AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
     }
 
     @Override
--- a/jdk/src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java	Tue Apr 21 20:11:59 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java	Wed Apr 22 13:46:03 2015 +0400
@@ -244,7 +244,6 @@
     public Shape usrClip;
     protected Region devClip;           // Actual physical drawable in pixels
 
-    private final int devScale;         // Actual physical scale factor
     private int resolutionVariantHint;
 
     // cached state for text rendering
@@ -268,8 +267,6 @@
         surfaceData = sd;
         foregroundColor = fg;
         backgroundColor = bg;
-
-        transform = new AffineTransform();
         stroke = defaultStroke;
         composite = defaultComposite;
         paint = foregroundColor;
@@ -287,14 +284,13 @@
 
         interpolationType = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
 
-        validateColor();
-
-        devScale = sd.getDefaultScale();
-        if (devScale != 1) {
-            transform.setToScale(devScale, devScale);
+        transform = getDefaultTransform();
+        if (!transform.isIdentity()) {
             invalidateTransform();
         }
 
+        validateColor();
+
         font = f;
         if (font == null) {
             font = defaultFont;
@@ -304,6 +300,11 @@
         invalidatePipe();
     }
 
+    private AffineTransform getDefaultTransform() {
+        GraphicsConfiguration gc = getDeviceConfiguration();
+        return (gc == null) ? new AffineTransform() : gc.getDefaultTransform();
+    }
+
     protected Object clone() {
         try {
             SunGraphics2D g = (SunGraphics2D) super.clone();
@@ -1610,11 +1611,10 @@
      */
     @Override
     public void setTransform(AffineTransform Tx) {
-        if ((constrainX | constrainY) == 0 && devScale == 1) {
+        if ((constrainX | constrainY) == 0) {
             transform.setTransform(Tx);
         } else {
-            transform.setTransform(devScale, 0, 0, devScale, constrainX,
-                                   constrainY);
+            transform.setToTranslation(constrainX, constrainY);
             transform.concatenate(Tx);
         }
         invalidateTransform();
@@ -1674,13 +1674,11 @@
      */
     @Override
     public AffineTransform getTransform() {
-        if ((constrainX | constrainY) == 0 && devScale == 1) {
+        if ((constrainX | constrainY) == 0) {
             return new AffineTransform(transform);
         }
-        final double invScale = 1.0 / devScale;
-        AffineTransform tx = new AffineTransform(invScale, 0, 0, invScale,
-                                                 -constrainX * invScale,
-                                                 -constrainY * invScale);
+        AffineTransform tx
+                = AffineTransform.getTranslateInstance(-constrainX, -constrainY);
         tx.concatenate(transform);
         return tx;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Graphics2D/ScaledTransform/ScaledTransform.java	Wed Apr 22 13:46:03 2015 +0400
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2015, 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.Dialog;
+import java.awt.Frame;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
+import java.awt.Panel;
+import java.awt.geom.AffineTransform;
+
+/*
+ * @test
+ * @bug 8069361
+ * @summary SunGraphics2D.getDefaultTransform() does not include scale factor
+ * @author Alexander Scherbatiy
+ * @run main ScaledTransform
+ */
+public class ScaledTransform {
+
+    private static volatile boolean passed = false;
+
+    public static void main(String[] args) {
+        GraphicsEnvironment ge = GraphicsEnvironment.
+                getLocalGraphicsEnvironment();
+
+        if (ge.isHeadlessInstance()) {
+            return;
+        }
+
+        for (GraphicsDevice gd : ge.getScreenDevices()) {
+            for (GraphicsConfiguration gc : gd.getConfigurations()) {
+                testScaleFactor(gc);
+            }
+        }
+    }
+
+    private static void testScaleFactor(final GraphicsConfiguration gc) {
+        final Dialog dialog = new Dialog((Frame) null, "Test", true, gc);
+
+        try {
+            dialog.setSize(100, 100);
+            Panel panel = new Panel() {
+
+                @Override
+                public void paint(Graphics g) {
+                    if (g instanceof Graphics2D) {
+                        AffineTransform gcTx = gc.getDefaultTransform();
+                        AffineTransform gTx
+                                = ((Graphics2D) g).getTransform();
+                        passed = gcTx.getScaleX() == gTx.getScaleX()
+                                && gcTx.getScaleY() == gTx.getScaleY();
+                    } else {
+                        passed = true;
+                    }
+                    dialog.setVisible(false);
+                }
+            };
+            dialog.add(panel);
+            dialog.setVisible(true);
+
+            if (!passed) {
+                throw new RuntimeException("Transform is not scaled!");
+            }
+        } finally {
+            dialog.dispose();
+        }
+    }
+}