8041982: Use of animated icon in JLayer causes CPU spin
authoralexp
Wed, 06 Aug 2014 21:46:17 +0400
changeset 26029 730f9bc4b85e
parent 26028 2ee7c47e175a
child 26030 576ffa819e23
8041982: Use of animated icon in JLayer causes CPU spin Reviewed-by: alexsch
jdk/src/share/classes/javax/swing/JLayer.java
jdk/src/share/classes/javax/swing/plaf/LayerUI.java
jdk/test/javax/swing/JLayer/8041982/bug8041982.java
jdk/test/javax/swing/JLayer/8041982/cupanim.gif
--- a/jdk/src/share/classes/javax/swing/JLayer.java	Wed Aug 06 14:34:16 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/JLayer.java	Wed Aug 06 21:46:17 2014 +0400
@@ -158,8 +158,9 @@
     private LayerUI<? super V> layerUI;
     private JPanel glassPane;
     private long eventMask;
-    private transient boolean isPainting;
-    private transient boolean isPaintingImmediately;
+    private transient boolean isPaintCalling;
+    private transient boolean isPaintImmediatelyCalling;
+    private transient boolean isImageUpdateCalling;
 
     private static final LayerEventController eventController =
             new LayerEventController();
@@ -405,12 +406,12 @@
      * @param h  the height of the region to be painted
      */
     public void paintImmediately(int x, int y, int w, int h) {
-        if (!isPaintingImmediately && getUI() != null) {
-            isPaintingImmediately = true;
+        if (!isPaintImmediatelyCalling && getUI() != null) {
+            isPaintImmediatelyCalling = true;
             try {
                 getUI().paintImmediately(x, y, w, h, this);
             } finally {
-                isPaintingImmediately = false;
+                isPaintImmediatelyCalling = false;
             }
         } else {
             super.paintImmediately(x, y, w, h);
@@ -418,17 +419,44 @@
     }
 
     /**
+     * Delegates its functionality to the
+     * {@link javax.swing.plaf.LayerUI#imageUpdate(java.awt.Image, int, int, int, int, int, JLayer)} method,
+     * if the {@code LayerUI} is set.
+     *
+     * @param     img   the image being observed
+     * @param     infoflags   see {@code imageUpdate} for more information
+     * @param     x   the <i>x</i> coordinate
+     * @param     y   the <i>y</i> coordinate
+     * @param     w   the width
+     * @param     h   the height
+     * @return    {@code false} if the infoflags indicate that the
+     *            image is completely loaded; {@code true} otherwise.
+     */
+    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
+        if (!isImageUpdateCalling && getUI() != null) {
+            isImageUpdateCalling = true;
+            try {
+                return getUI().imageUpdate(img, infoflags, x, y, w, h, this);
+            } finally {
+                isImageUpdateCalling = false;
+            }
+        } else {
+            return super.imageUpdate(img, infoflags, x, y, w, h);
+        }
+    }
+
+    /**
      * Delegates all painting to the {@link javax.swing.plaf.LayerUI} object.
      *
      * @param g the {@code Graphics} to render to
      */
     public void paint(Graphics g) {
-        if (!isPainting) {
-            isPainting = true;
+        if (!isPaintCalling) {
+            isPaintCalling = true;
             try {
                 super.paintComponent(g);
             } finally {
-                isPainting = false;
+                isPaintCalling = false;
             }
         } else {
             super.paint(g);
--- a/jdk/src/share/classes/javax/swing/plaf/LayerUI.java	Wed Aug 06 14:34:16 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/plaf/LayerUI.java	Wed Aug 06 21:46:17 2014 +0400
@@ -722,4 +722,16 @@
     public void paintImmediately(int x, int y, int width, int height, JLayer<? extends V> l) {
         l.paintImmediately(x, y, width, height);
     }
+
+    /**
+     * Delegates its functionality to the default implementation of the {@code JLayer.imageUpdate} method
+     * which is inherited from {@code JLayer}'s base classes.
+     * <p>
+     * This method is to be overridden instead of {@code JLayer.imageUpdate}.
+     * <p>
+     * <b>Note:</b> This method is usually called <b>not</b> on the Event Dispatching Thread.
+     */
+    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h, JLayer<? extends V> l) {
+        return l.imageUpdate(img, infoflags, x, y, w, h);
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JLayer/8041982/bug8041982.java	Wed Aug 06 21:46:17 2014 +0400
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2014, 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.
+ */
+
+/* @test
+ * @bug 8041982
+ * @summary Use of animated icon in JLayer causes CPU spin
+ * @author Alexander Potochkin
+ */
+
+import javax.swing.*;
+import javax.swing.plaf.LayerUI;
+import java.awt.*;
+import java.beans.PropertyChangeEvent;
+
+public class bug8041982 extends JFrame {
+
+    public bug8041982() {
+        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        add(new JLayer<>(new JPanel(), new BusyLayer()));
+        setSize(200, 300);
+        setVisible(true);
+    }
+
+    public static void main(String... args) throws Exception {
+        SwingUtilities.invokeLater(new Runnable() {
+            public void run() {
+                new bug8041982().setVisible(true);
+            }
+        });
+        Thread.sleep(5000);
+    }
+
+    private class BusyLayer extends LayerUI<JComponent> {
+        private volatile boolean animated = true;
+        private Icon icon = new ImageIcon(bug8041982.class.getResource("cupanim.gif"));
+        private int imageUpdateCount;
+
+        @Override
+        public void paint(Graphics g, JComponent c) {
+            super.paint(g, c);
+            if (isAnimated()) {
+                icon.paintIcon(c, g, c.getWidth() / 2 - icon.getIconWidth() /
+                        2,
+                        c.getHeight() / 2 - icon.getIconHeight() / 2);
+            }
+        }
+
+        public boolean isAnimated() {
+            return animated;
+        }
+
+        public void setAnimated(boolean animated) {
+            if (this.animated != animated) {
+                this.animated = animated;
+                firePropertyChange("animated", !animated, animated);
+            }
+        }
+
+        @Override
+        public void applyPropertyChange(PropertyChangeEvent evt, JLayer l) {
+            // this will be called when the busy flag is changed
+            l.repaint();
+        }
+
+        @Override
+        public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h, JLayer<? extends JComponent> l) {
+            System.out.println("imageUpdate " + imageUpdateCount);
+            if (imageUpdateCount++ == 100) {
+                setAnimated(false);
+            } else if (imageUpdateCount > 100) {
+                throw new RuntimeException("Test failed");
+            }
+            return isAnimated() && super.imageUpdate(img, infoflags, x, y, w, h, l);
+        }
+    }
+}
+
Binary file jdk/test/javax/swing/JLayer/8041982/cupanim.gif has changed