mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-11 05:59:52 +00:00
8257500: Drawing MultiResolutionImage with ImageObserver "leaks" memory
Reviewed-by: azvegint, aivanov
This commit is contained in:
parent
65a245e262
commit
6800ba465f
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 2021, 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
|
||||
@ -22,15 +22,18 @@
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.awt.image;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.image.ImageObserver;
|
||||
import java.awt.image.MultiResolutionImage;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.function.Function;
|
||||
import sun.awt.SoftCache;
|
||||
|
||||
public class MultiResolutionToolkitImage extends ToolkitImage implements MultiResolutionImage {
|
||||
|
||||
@ -79,12 +82,6 @@ public class MultiResolutionToolkitImage extends ToolkitImage implements MultiRe
|
||||
private static final int BITS_INFO = ImageObserver.SOMEBITS
|
||||
| ImageObserver.FRAMEBITS | ImageObserver.ALLBITS;
|
||||
|
||||
private static class ObserverCache {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
static final SoftCache INSTANCE = new SoftCache();
|
||||
}
|
||||
|
||||
public static ImageObserver getResolutionVariantObserver(
|
||||
final Image image, final ImageObserver observer,
|
||||
final int imgWidth, final int imgHeight,
|
||||
@ -103,38 +100,53 @@ public class MultiResolutionToolkitImage extends ToolkitImage implements MultiRe
|
||||
}
|
||||
|
||||
synchronized (ObserverCache.INSTANCE) {
|
||||
ImageObserver o = (ImageObserver) ObserverCache.INSTANCE.get(observer);
|
||||
return ObserverCache.INSTANCE.computeIfAbsent(observer,
|
||||
key -> new ObserverCache(key, concatenateInfo, image));
|
||||
}
|
||||
}
|
||||
|
||||
if (o == null) {
|
||||
private static final class ObserverCache implements ImageObserver {
|
||||
|
||||
o = (Image resolutionVariant, int flags,
|
||||
int x, int y, int width, int height) -> {
|
||||
private static final Map<ImageObserver, ImageObserver> INSTANCE =
|
||||
new WeakHashMap<>();
|
||||
|
||||
if ((flags & (ImageObserver.WIDTH | BITS_INFO)) != 0) {
|
||||
width = (width + 1) / 2;
|
||||
}
|
||||
private final boolean concat;
|
||||
private final WeakReference<Image> imageRef;
|
||||
private final WeakReference<ImageObserver> observerRef;
|
||||
|
||||
if ((flags & (ImageObserver.HEIGHT | BITS_INFO)) != 0) {
|
||||
height = (height + 1) / 2;
|
||||
}
|
||||
private ObserverCache(ImageObserver obs, boolean concat, Image img) {
|
||||
this.concat = concat;
|
||||
imageRef = new WeakReference<>(img);
|
||||
observerRef = new WeakReference<>(obs);
|
||||
}
|
||||
|
||||
if ((flags & BITS_INFO) != 0) {
|
||||
x /= 2;
|
||||
y /= 2;
|
||||
}
|
||||
@Override
|
||||
public boolean imageUpdate(Image img, int infoflags,
|
||||
int x, int y, int width, int height) {
|
||||
ImageObserver observer = observerRef.get();
|
||||
Image image = imageRef.get();
|
||||
|
||||
if(concatenateInfo){
|
||||
flags &= ((ToolkitImage) image).
|
||||
getImageRep().check(null);
|
||||
}
|
||||
|
||||
return observer.imageUpdate(
|
||||
image, flags, x, y, width, height);
|
||||
};
|
||||
|
||||
ObserverCache.INSTANCE.put(observer, o);
|
||||
if (observer == null || image == null) {
|
||||
return false;
|
||||
}
|
||||
return o;
|
||||
|
||||
if ((infoflags & (ImageObserver.WIDTH | BITS_INFO)) != 0) {
|
||||
width = (width + 1) / 2;
|
||||
}
|
||||
|
||||
if ((infoflags & (ImageObserver.HEIGHT | BITS_INFO)) != 0) {
|
||||
height = (height + 1) / 2;
|
||||
}
|
||||
|
||||
if ((infoflags & BITS_INFO) != 0) {
|
||||
x /= 2;
|
||||
y /= 2;
|
||||
}
|
||||
|
||||
if (concat) {
|
||||
infoflags &= ((ToolkitImage) image).getImageRep().check(null);
|
||||
}
|
||||
return observer.imageUpdate(image, infoflags, x, y, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2021, 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.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BaseMultiResolutionImage;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.ImageObserver;
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import static java.awt.image.BufferedImage.TYPE_INT_RGB;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8257500
|
||||
* @summary Drawing MultiResolutionImage with ImageObserver may "leaks" memory
|
||||
*/
|
||||
public final class ImageObserverLeak {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Reference<ImageObserver> ref = test();
|
||||
|
||||
while (!ref.refersTo(null)) {
|
||||
Thread.sleep(500);
|
||||
// Cannot generate OOM here, it will clear the SoftRefs as well
|
||||
System.gc();
|
||||
}
|
||||
}
|
||||
|
||||
private static Reference<ImageObserver> test() throws Exception {
|
||||
BufferedImage src = new BufferedImage(200, 200, TYPE_INT_RGB);
|
||||
Image mri = new BaseMultiResolutionImage(src);
|
||||
ImageObserver observer = new ImageObserver() {
|
||||
@Override
|
||||
public boolean imageUpdate(Image img, int infoflags, int x, int y,
|
||||
int width, int height) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Reference<ImageObserver> ref = new WeakReference<>(observer);
|
||||
|
||||
BufferedImage dst = new BufferedImage(200, 300, TYPE_INT_RGB);
|
||||
Graphics2D g2d = dst.createGraphics();
|
||||
g2d.drawImage(mri, 0, 0, observer);
|
||||
g2d.dispose();
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2014, 2021, 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
|
||||
@ -27,7 +27,6 @@ import java.awt.Image;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.ImageObserver;
|
||||
import static java.awt.image.ImageObserver.ALLBITS;
|
||||
import java.io.File;
|
||||
import javax.imageio.ImageIO;
|
||||
import sun.awt.OSInfo;
|
||||
@ -37,14 +36,13 @@ import sun.awt.image.MultiResolutionToolkitImage;
|
||||
/**
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8040291
|
||||
* @author Alexander Scherbatiy
|
||||
* @bug 8040291 8257500
|
||||
* @requires os.family == "mac"
|
||||
* @summary [macosx] Http-Images are not fully loaded when using ImageIcon
|
||||
* @modules java.desktop/sun.awt
|
||||
* java.desktop/sun.awt.image
|
||||
* @run main MultiResolutionToolkitImageTest
|
||||
*/
|
||||
|
||||
public class MultiResolutionToolkitImageTest {
|
||||
|
||||
private static final int IMAGE_WIDTH = 300;
|
||||
@ -140,7 +138,7 @@ public class MultiResolutionToolkitImageTest {
|
||||
Exception e = new Exception();
|
||||
|
||||
for (StackTraceElement elem : e.getStackTrace()) {
|
||||
if (elem.getClassName().endsWith("MultiResolutionToolkitImage")) {
|
||||
if (elem.getClassName().endsWith("ObserverCache")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user