提交 69526bc6 编写于 作者: A alexsch

8040291: [macosx] Http-Images are not fully loaded when using ImageIcon

Reviewed-by: serb, pchelko
上级 fc6f83bc
...@@ -226,7 +226,9 @@ public class MediaTracker implements java.io.Serializable { ...@@ -226,7 +226,9 @@ public class MediaTracker implements java.io.Serializable {
addImageImpl(image, id, w, h); addImageImpl(image, id, w, h);
Image rvImage = getResolutionVariant(image); Image rvImage = getResolutionVariant(image);
if (rvImage != null) { if (rvImage != null) {
addImageImpl(rvImage, id, 2 * w, 2 * h); addImageImpl(rvImage, id,
w == -1 ? -1 : 2 * w,
h == -1 ? -1 : 2 * h);
} }
} }
...@@ -810,8 +812,9 @@ public class MediaTracker implements java.io.Serializable { ...@@ -810,8 +812,9 @@ public class MediaTracker implements java.io.Serializable {
removeImageImpl(image, id, width, height); removeImageImpl(image, id, width, height);
Image rvImage = getResolutionVariant(image); Image rvImage = getResolutionVariant(image);
if (rvImage != null) { if (rvImage != null) {
removeImageImpl(rvImage, id, 2 * width, 2 * height); removeImageImpl(rvImage, id,
width == -1 ? -1 : 2 * width,
height == -1 ? -1 : 2 * height);
} }
notifyAll(); // Notify in case remaining images are "done". notifyAll(); // Notify in case remaining images are "done".
} }
......
...@@ -851,22 +851,30 @@ public abstract class SunToolkit extends Toolkit ...@@ -851,22 +851,30 @@ public abstract class SunToolkit extends Toolkit
private int checkResolutionVariant(Image img, int w, int h, ImageObserver o) { private int checkResolutionVariant(Image img, int w, int h, ImageObserver o) {
ToolkitImage rvImage = getResolutionVariant(img); ToolkitImage rvImage = getResolutionVariant(img);
int rvw = getRVSize(w);
int rvh = getRVSize(h);
// Ignore the resolution variant in case of error // Ignore the resolution variant in case of error
return (rvImage == null || rvImage.hasError()) ? 0xFFFF : return (rvImage == null || rvImage.hasError()) ? 0xFFFF :
checkImage(rvImage, 2 * w, 2 * h, MultiResolutionToolkitImage. checkImage(rvImage, rvw, rvh, MultiResolutionToolkitImage.
getResolutionVariantObserver( getResolutionVariantObserver(
img, o, w, h, 2 * w, 2 * h)); img, o, w, h, rvw, rvh, true));
} }
private boolean prepareResolutionVariant(Image img, int w, int h, private boolean prepareResolutionVariant(Image img, int w, int h,
ImageObserver o) { ImageObserver o) {
ToolkitImage rvImage = getResolutionVariant(img); ToolkitImage rvImage = getResolutionVariant(img);
int rvw = getRVSize(w);
int rvh = getRVSize(h);
// Ignore the resolution variant in case of error // Ignore the resolution variant in case of error
return rvImage == null || rvImage.hasError() || prepareImage( return rvImage == null || rvImage.hasError() || prepareImage(
rvImage, 2 * w, 2 * h, rvImage, rvw, rvh,
MultiResolutionToolkitImage.getResolutionVariantObserver( MultiResolutionToolkitImage.getResolutionVariantObserver(
img, o, w, h, 2 * w, 2 * h)); img, o, w, h, rvw, rvh, true));
}
private static int getRVSize(int size){
return size == -1 ? -1 : 2 * size;
} }
private static ToolkitImage getResolutionVariant(Image image) { private static ToolkitImage getResolutionVariant(Image image) {
......
...@@ -66,6 +66,14 @@ public class MultiResolutionToolkitImage extends ToolkitImage implements MultiRe ...@@ -66,6 +66,14 @@ public class MultiResolutionToolkitImage extends ToolkitImage implements MultiRe
final Image image, final ImageObserver observer, final Image image, final ImageObserver observer,
final int imgWidth, final int imgHeight, final int imgWidth, final int imgHeight,
final int rvWidth, final int rvHeight) { final int rvWidth, final int rvHeight) {
return getResolutionVariantObserver(image, observer,
imgWidth, imgHeight, rvWidth, rvHeight, false);
}
public static ImageObserver getResolutionVariantObserver(
final Image image, final ImageObserver observer,
final int imgWidth, final int imgHeight,
final int rvWidth, final int rvHeight, boolean concatenateInfo) {
if (observer == null) { if (observer == null) {
return null; return null;
...@@ -92,6 +100,11 @@ public class MultiResolutionToolkitImage extends ToolkitImage implements MultiRe ...@@ -92,6 +100,11 @@ public class MultiResolutionToolkitImage extends ToolkitImage implements MultiRe
y /= 2; y /= 2;
} }
if(concatenateInfo){
flags &= ((ToolkitImage) image).
getImageRep().check(null);
}
return observer.imageUpdate( return observer.imageUpdate(
image, flags, x, y, width, height); image, flags, x, y, width, height);
}; };
......
/*
* 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.
*/
import java.awt.Color;
import java.awt.Graphics;
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;
import sun.awt.SunToolkit;
import sun.awt.image.MultiResolutionToolkitImage;
/**
* @test
* @bug 8040291
* @author Alexander Scherbatiy
* @summary [macosx] Http-Images are not fully loaded when using ImageIcon
* @run main MultiResolutionToolkitImageTest
*/
public class MultiResolutionToolkitImageTest {
private static final int IMAGE_WIDTH = 300;
private static final int IMAGE_HEIGHT = 200;
private static final Color COLOR_1X = Color.GREEN;
private static final Color COLOR_2X = Color.BLUE;
private static final String IMAGE_NAME_1X = "image.png";
private static final String IMAGE_NAME_2X = "image@2x.png";
private static final int WAIT_TIME = 400;
private static volatile boolean isImageLoaded = false;
private static volatile boolean isRVObserverCalled = false;
public static void main(String[] args) throws Exception {
if (!checkOS()) {
return;
}
generateImages();
testToolkitMultiResolutionImageLoad();
}
static void testToolkitMultiResolutionImageLoad() throws Exception {
File imageFile = new File(IMAGE_NAME_1X);
String fileName = imageFile.getAbsolutePath();
Image image = Toolkit.getDefaultToolkit().getImage(fileName);
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
toolkit.prepareImage(image, -1, -1, new LoadImageObserver());
final long time = WAIT_TIME + System.currentTimeMillis();
while ((!isImageLoaded || !isRVObserverCalled)
&& System.currentTimeMillis() < time) {
Thread.sleep(50);
}
if(!isImageLoaded){
throw new RuntimeException("Image is not loaded!");
}
if(!isRVObserverCalled){
throw new RuntimeException("Resolution Variant observer is not called!");
}
}
static void generateImages() throws Exception {
if (!new File(IMAGE_NAME_1X).exists()) {
generateImage(1);
}
if (!new File(IMAGE_NAME_2X).exists()) {
generateImage(2);
}
}
static void generateImage(int scale) throws Exception {
BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(scale == 1 ? COLOR_1X : COLOR_2X);
g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);
File file = new File(scale == 1 ? IMAGE_NAME_1X : IMAGE_NAME_2X);
ImageIO.write(image, "png", file);
}
static boolean checkOS() {
return OSInfo.getOSType() == OSInfo.OSType.MACOSX;
}
static class LoadImageObserver implements ImageObserver {
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
if (isRVObserver()) {
isRVObserverCalled = true;
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Image resolutionVariant = getResolutionVariant(img);
int rvFlags = toolkit.checkImage(resolutionVariant, width, height,
new IdleImageObserver());
if (rvFlags < infoflags) {
throw new RuntimeException("Info flags are greater than"
+ " resolution varint info flags");
}
} else if ((infoflags & ALLBITS) != 0) {
isImageLoaded = true;
}
return (infoflags & ALLBITS) == 0;
}
}
static boolean isRVObserver() {
Exception e = new Exception();
for (StackTraceElement elem : e.getStackTrace()) {
if (elem.getClassName().endsWith("MultiResolutionToolkitImage")) {
return true;
}
}
return false;
}
static class IdleImageObserver implements ImageObserver {
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
return false;
}
}
static Image getResolutionVariant(Image image) {
return ((MultiResolutionToolkitImage) image).getResolutionVariant();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册