Skip to content

Commit

Permalink
8275843: Random crashes while the UI code is executed
Browse files Browse the repository at this point in the history
Backport-of: a9c1acbb8aec46e4a488b7c77bb6318af87747f6
  • Loading branch information
mrserb committed Jan 3, 2023
1 parent 298fef7 commit 1f4fc33
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@

package sun.java2d.xr;

import java.awt.*;
import java.awt.geom.*;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Paint;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.security.AccessController;
import java.security.PrivilegedAction;

import sun.font.*;
import sun.java2d.*;
import sun.java2d.loops.*;
import sun.awt.image.PixelConverter;
import sun.font.XRTextRenderer;
import sun.java2d.SunGraphics2D;
import sun.java2d.loops.XORComposite;

/**
* Manages per-application resources, e.g. the 1x1 pixmap used for solid color
Expand Down Expand Up @@ -68,6 +71,7 @@ public class XRCompositeManager {
int gradCachePicture;

boolean xorEnabled = false;
int eargb;
int validatedPixel = 0;
Composite validatedComp;
Paint validatedPaint;
Expand Down Expand Up @@ -170,8 +174,10 @@ public void validateCompositeState(Composite comp, AffineTransform xform,
validatedComp = comp;
}

if (sg2d != null && (validatedPixel != sg2d.pixel || updatePaint)) {
validatedPixel = sg2d.pixel;
if (sg2d != null && (eargb != sg2d.eargb || updatePaint)) {
eargb = sg2d.eargb;
validatedPixel = PixelConverter.ArgbPre.instance
.rgbToPixel(eargb, null);
setForeground(validatedPixel);
}

Expand Down
16 changes: 2 additions & 14 deletions src/java.desktop/unix/classes/sun/java2d/xr/XRGraphicsConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
Expand All @@ -25,14 +25,12 @@

package sun.java2d.xr;

import java.awt.Transparency;
import sun.awt.X11GraphicsConfig;
import sun.awt.X11ComponentPeer;
import sun.awt.X11GraphicsConfig;
import sun.awt.X11GraphicsDevice;
import sun.awt.X11GraphicsEnvironment;
import sun.awt.image.SurfaceManager;
import sun.java2d.SurfaceData;
import sun.java2d.loops.SurfaceType;

public class XRGraphicsConfig extends X11GraphicsConfig implements
SurfaceManager.ProxiedGraphicsConfig {
Expand All @@ -58,14 +56,4 @@ public static XRGraphicsConfig getConfig(X11GraphicsDevice device,
public Object getProxyKey() {
return this;
}

public synchronized SurfaceType getSurfaceType() {
if (surfaceType != null) {
return surfaceType;
}

surfaceType = XRSurfaceData.getSurfaceType(this, Transparency.OPAQUE);
return surfaceType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.Raster;

import sun.awt.SunHints;
import sun.awt.SunToolkit;
import sun.awt.X11ComponentPeer;
import sun.awt.image.PixelConverter;
import sun.font.FontManagerNativeLibrary;
import sun.java2d.InvalidPipeException;
import sun.java2d.SunGraphics2D;
import sun.java2d.SurfaceData;
Expand All @@ -55,7 +57,6 @@
import sun.java2d.pipe.TextPipe;
import sun.java2d.pipe.ValidatePipe;
import sun.java2d.x11.XSurfaceData;
import sun.font.FontManagerNativeLibrary;

public abstract class XRSurfaceData extends XSurfaceData {
X11ComponentPeer peer;
Expand Down Expand Up @@ -277,7 +278,7 @@ public static XRPixmapSurfaceData createData(XRGraphicsConfig gc,
}

return new XRPixmapSurfaceData
(gc, width, height, image, getSurfaceType(gc, transparency),
(gc, width, height, image, getPixmapSurfaceType(transparency),
cm, drawable, transparency,
XRUtils.getPictureFormatForTransparency(transparency), depth, isTexture);
}
Expand Down Expand Up @@ -396,8 +397,7 @@ public boolean copyArea(SunGraphics2D sg2d, int x, int y, int w, int h,
* Returns the XRender SurfaceType which is able to fullfill the specified
* transparency requirement.
*/
public static SurfaceType getSurfaceType(XRGraphicsConfig gc,
int transparency) {
public static SurfaceType getPixmapSurfaceType(int transparency) {
SurfaceType sType = null;

switch (transparency) {
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/awt/ColorClass/AlphaColorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* @test
* @key headful
* @bug 8204931 8227392 8224825 8233910
* @bug 8204931 8227392 8224825 8233910 8275843
* @summary test alpha colors are blended with background.
*/

Expand Down
45 changes: 22 additions & 23 deletions test/jdk/java/awt/ColorClass/XRenderTranslucentColorDrawTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 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
Expand All @@ -21,45 +21,44 @@
* questions.
*/


/*
* @test
* @key headful
* @bug 8176795
* @bug 8176795 8275843
* @summary Test verifies that we get proper color when we draw translucent
* color over an opaque color using X Render extension in Linux.
* @requires (os.family == "linux")
* @run main XRenderTranslucentColorDrawTest -Dsun.java2d.xrender=true
* @run main/othervm XRenderTranslucentColorDrawTest
* @run main/othervm -Dsun.java2d.xrender=true XRenderTranslucentColorDrawTest
*/

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;

public class XRenderTranslucentColorDrawTest {

public static void main(String[] args) throws Exception {
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsConfiguration translucentGC = null;
SCREENS: for (GraphicsDevice screen : env.getScreenDevices()) {
public static void main(String[] args) {
var env = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (GraphicsDevice screen : env.getScreenDevices()) {
for (GraphicsConfiguration gc : screen.getConfigurations()) {
if (gc.isTranslucencyCapable()) {
translucentGC = gc;
break SCREENS;
}
test(gc, Transparency.OPAQUE);
test(gc, Transparency.BITMASK);
test(gc, Transparency.TRANSLUCENT);
}
}
if (translucentGC == null) {
throw new RuntimeException("No suitable gc found.");
}
}

private static void test(GraphicsConfiguration gc, int transparency) {
int width = 10;
int height = 10;
VolatileImage image = translucentGC.
createCompatibleVolatileImage(width, height);
VolatileImage image = gc.createCompatibleVolatileImage(width, height,
transparency);
Graphics2D g = image.createGraphics();
// draw opaque black color
g.setColor(new Color(0xff000000, true));
Expand All @@ -72,10 +71,10 @@ public static void main(String[] args) throws Exception {
BufferedImage snapshot = image.getSnapshot();
int argb = snapshot.getRGB(width / 2, height / 2);
// we expect the resultant rgb hex value to be ff808080
if (!(Integer.toHexString(argb).equals("ff808080"))) {
throw new RuntimeException("Using X Render extension for drawing"
+ " translucent color is not giving expected results.");
String actual = Integer.toHexString(argb);
if (!(actual.equals("ff808080"))) {
throw new RuntimeException("Drawing translucent color is not " +
"giving expected results: " + actual);
}
}
}

}
79 changes: 79 additions & 0 deletions test/jdk/java/awt/image/ColorModel/DrawCustomColorModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright Amazon.com Inc. 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.AlphaComposite;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.awt.image.DirectColorModel;
import java.awt.image.WritableRaster;
import java.util.Arrays;

/**
* @test
* @bug 8275843
* @key headful
* @summary No exception or errors should occur.
*/
public final class DrawCustomColorModel {

public static void main(String[] args) {
var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (GraphicsDevice gd : ge.getScreenDevices()) {
GraphicsConfiguration[] gcs = gd.getConfigurations();
for (GraphicsConfiguration gc : gcs) {
test(gc);
}
}
}

private static void test(GraphicsConfiguration gc) {
Frame frame = new Frame(gc);
frame.setUndecorated(true);
frame.pack();
frame.setSize(15, 15);
ColorModel cm = new DirectColorModel(32,
0xff000000, // Red
0x00ff0000, // Green
0x0000ff00, // Blue
0x000000FF // Alpha
);
WritableRaster wr = cm.createCompatibleWritableRaster(16, 16);
DataBufferInt buff = (DataBufferInt) wr.getDataBuffer();
int[] data = buff.getData();
Arrays.fill(data, -1); // more chance to reproduce
Image image = new BufferedImage(cm, wr, false, null);

Graphics2D graphics = (Graphics2D) frame.getGraphics();
graphics.setComposite(AlphaComposite.Src);
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
frame.dispose();
}
}

1 comment on commit 1f4fc33

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.