8041982: Use of animated icon in JLayer causes CPU spin
Reviewed-by: alexsch
This commit is contained in:
parent
e9a294c5aa
commit
f7855341e8
@ -158,8 +158,9 @@ public final class JLayer<V extends Component>
|
||||
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,30 +406,57 @@ public final class JLayer<V extends Component>
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
@ -722,4 +722,16 @@ public class LayerUI<V extends Component>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
97
jdk/test/javax/swing/JLayer/8041982/bug8041982.java
Normal file
97
jdk/test/javax/swing/JLayer/8041982/bug8041982.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
jdk/test/javax/swing/JLayer/8041982/cupanim.gif
Normal file
BIN
jdk/test/javax/swing/JLayer/8041982/cupanim.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.7 KiB |
Loading…
Reference in New Issue
Block a user