Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8266079: Lanai: AlphaComposite shows differences on Metal compared to OpenGL #4946

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
@@ -41,6 +41,7 @@
private static native void nativeSetInsets(long layerPtr, int top, int left);
private static native void validate(long layerPtr, MTLSurfaceData mtlsd);
private static native void blitTexture(long layerPtr);
private static native void nativeSetOpaque(long layerPtr, boolean opaque);

private int scale = 1;

@@ -104,7 +105,11 @@ private void setScale(final int _scale) {
}
}

// ----------------------------------------------------------------------
@Override
public void setOpaque(boolean opaque) {
execute(ptr -> nativeSetOpaque(ptr, opaque));
}
// ----------------------------------------------------------------------
// NATIVE CALLBACKS
// ----------------------------------------------------------------------

@@ -196,7 +196,7 @@ public LWWindowPeer(Window target, PlatformComponent platformComponent,
}

platformWindow.initialize(target, this, ownerDelegate);

platformWindow.setOpaque(!isTranslucent());
This conversation was marked as resolved by avu

This comment has been minimized.

Loading
@mrserb

mrserb Aug 2, 2021
Member Outdated

!isTranslucent() uses the opaque flag which will be initialised in the code below(see setOpaque(getTarget().isOpaque());)

This comment has been minimized.

Loading
@avu

avu Aug 2, 2021
Author Outdated

Yes, and we need this call here to initialise content view with correct default value
CPlatformWindow.java:931 contentView.setWindowLayerOpaque(isOpaque);

This comment has been minimized.

Loading
@mrserb

mrserb Aug 2, 2021
Member Outdated

I need to look at it closely, I do not understand why we made the native layer opaque=NO by default while the java code uses opaque=true by default. And then we reset the native code from NO to yes by the code above. Why we cannot make both native/java use true/YES and when necessary it will be reset to false/NO by the existing code(via LWWindowPeer.setOpaque() and LWWindowPeer.updateOpaque?)
BTW Probably implementation of LWWindowPeer.setTextured() should call the updateOpaque() as well.

This comment has been minimized.

Loading
@avu

avu Aug 3, 2021
Author Outdated

Yes, sounds reasonable. Moreover, It was my initial attempt but I faced with some regressions in SwingSet2 demo. In some cases background of tree nodes become black and this artifact was reproducible only if some other tabs were clicked before. It was hot time for jdk17 so I decided not to fight with this regression. Now we have more time, so I can spend more time on this.

This comment has been minimized.

Loading
@avu

avu Aug 3, 2021
Author Outdated

Looks like making MTLLayer opaque by default require much more time that I expected. I tried different things to resolve artefacts that I mentioned before but nothing seems to work. So, I suggest to stick with my latest approach. It passes all the tests and don't break SwingSet2.

BTW Probably implementation of LWWindowPeer.setTextured() should call the updateOpaque() as well.

I'll add this change

This comment has been minimized.

Loading
@mrserb

mrserb Aug 3, 2021
Member Outdated

How to reproduce that artifact?

This comment has been minimized.

Loading
@avu

avu Aug 3, 2021
Author Outdated

  1. Run SwingSet2
  2. Click on all tabs starting from the second one
  3. On the tab with swing tree control try to expand some tree nodes -> you'll see black rectangles while performing the clicks

It may not be reproduced on a first attempt. On my machine it's reproducible with probability at least 50%

This comment has been minimized.

Loading
@mrserb

mrserb Aug 4, 2021
Member Outdated

Thank you, looking into it.

This comment has been minimized.

Loading
@mrserb

mrserb Aug 8, 2021
Member Outdated

I check the steps below and was able to reproduce the same artifacts(black rectangles) in the tree tab with the current version of the fix. And I do not see a big difference between setting the layer to YES at the beginning or a little bit later, at the end, it will be opaque and artifacts occur.

  1. Run SwingSet2
  2. Click on all tabs starting from the second one, delay on each tab for the second or so
  3. On the tab with swing tree control try to expand some tree nodes -> you'll see black rectangles while performing the clicks
  4. Repeat from step 2 if no artifacts are visible

This comment has been minimized.

Loading
@avu

avu Aug 11, 2021
Author Outdated

Interesting, now I'm also able to reproduce this problem with the latest version of the fix. Actually, looks like the problem exists even without the fix. We get invalid background with the same scenario. It's not so visible because of default L&F (light). Nevertheless, the color of the background is not exact the same. If we provide apple.awt.application.appearance=NSAppearanceNameDarkAqua VM option, the problem became visible with metal and not reproducible with OGL.

This comment has been minimized.

Loading
@avu

avu Aug 12, 2021
Author Outdated

I've filed a separate CR for this problem JDK-8272392

This comment has been minimized.

Loading
@mrserb

mrserb Aug 12, 2021
Member Outdated

Then you can roll back this change to the default opaque layer?

This comment has been minimized.

Loading
@avu

avu Aug 12, 2021
Author Outdated

Did you mean to make MTLLayer opaque by default? Yes, I did it in the latest version.

This comment has been minimized.

Loading
@mrserb

mrserb Aug 13, 2021
Member Outdated

Then why do you need " platformWindow.setOpaque(!isTranslucent());" above?

This comment has been minimized.

Loading
@avu

avu Aug 13, 2021
Author Outdated

Ah, I see. Updated.

// Init warning window(for applets)
SecurityWarningWindow warn = null;
if (target.getWarningString() != null) {
@@ -520,7 +520,10 @@ public final boolean isTextured() {
}

public final void setTextured(final boolean isTextured) {
textured = isTextured;
if (textured != isTextured) {
textured = isTextured;
updateOpaque();
}
}

@Override
@@ -70,6 +70,10 @@ public boolean isOpaque() {
return !peer.isTranslucent();
}

public void setOpaque(boolean opaque) {
// Default is no op (works well for OGL)
}

public int getTransparency() {
return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
}
@@ -41,7 +41,6 @@

import sun.java2d.SurfaceData;
import sun.java2d.opengl.CGLLayer;
import sun.lwawt.macosx.CFLayer;

public class CPlatformView extends CFRetainedResource {
private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr);
@@ -135,6 +134,10 @@ public boolean isUnderMouse() {
return ref.get();
}

public void setWindowLayerOpaque(boolean opaque) {
windowLayer.setOpaque(opaque);
}

public GraphicsDevice getGraphicsDevice() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
CGraphicsEnvironment cge = (CGraphicsEnvironment)ge;
@@ -928,6 +928,7 @@ public void setOpacity(float opacity) {

@Override
public void setOpaque(boolean isOpaque) {
contentView.setWindowLayerOpaque(isOpaque);
execute(ptr -> CWrapper.NSWindow.setOpaque(ptr, isOpaque));
boolean isTextured = (peer == null) ? false : peer.isTextured();
if (!isTextured) {
@@ -71,7 +71,7 @@ - (id) initWithJavaLayer:(jobject)layer
self.leftInset = 0;
self.framebufferOnly = NO;
self.nextDrawableCount = 0;
self.opaque = FALSE;
self.opaque = YES;
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void*)self);
return self;
@@ -288,3 +288,17 @@ CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* no

[layer blitTexture];
}

JNIEXPORT void JNICALL
Java_sun_java2d_metal_MTLLayer_nativeSetOpaque
(JNIEnv *env, jclass cls, jlong layerPtr, jboolean opaque)
{
JNI_COCOA_ENTER(env);

MTLLayer *mtlLayer = OBJC(layerPtr);
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
[mtlLayer setOpaque:(opaque == JNI_TRUE)];
}];

JNI_COCOA_EXIT(env);
}
@@ -0,0 +1,238 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, JetBrains s.r.o.. 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
* @key headful
* @bug 8266079
* @summary [macosx] window rendering alpha composite test
* @author Alexey Ushakov
* @run main WindowAlphaCompositeTest
* @requires (os.family == "mac")
*/

import java.awt.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;

public class WindowAlphaCompositeTest
{
interface Validate {
boolean validate(int x, int y, Color c);
}
static Color RED128 = new Color(128, 0, 0);
static Color BLUE128 = new Color(0, 0, 128);
static Color PURPLE128 = new Color(128, 0, 128);
static Color RED_BLUE24 = new Color(230, 0, 24);
static Validate redBlackCheck = (int x, int y, Color c) -> {
Color expColor = Color.RED;
if (x > 24 && x < 75) {
expColor = Color.BLACK;
}
return validateColor(c, expColor);
};

static Validate redBlueCheck = (int x, int y, Color c) -> {
Color expColor = Color.RED;
if (x > 24 && x < 75) {
expColor = Color.BLUE;
}
return validateColor(c, expColor);
};

static Validate redCheck = (int x, int y, Color c) -> {
Color expColor = Color.RED;
return validateColor(c, expColor);
};

static Validate redRed128Check = (int x, int y, Color c) -> {
Color expColor = Color.RED;
if (x > 24 && x < 75) {
expColor = RED128;
}
return validateColor(c, expColor);
};

static Validate redBlue128Check = (int x, int y, Color c) -> {
Color expColor = Color.RED;
if (x > 24 && x < 75) {
expColor = BLUE128;
}
return validateColor(c, expColor);
};

static Validate purple128Check = (int x, int y, Color c) -> {
Color expColor = Color.RED;
if (x > 24 && x < 75) {
expColor = PURPLE128;
}
return validateColor(c, expColor);
};
static Validate redBlue24Check = (int x, int y, Color c) -> {
Color expColor = Color.RED;
if (x > 24 && x < 75) {
expColor = RED_BLUE24;
}
return validateColor(c, expColor);
};
static Object[][] alphaComposites = {
{AlphaComposite.Clear, redBlackCheck},
{AlphaComposite.Dst, redCheck},
{AlphaComposite.DstAtop, redCheck},
{AlphaComposite.getInstance(AlphaComposite.DST_IN, 1.0f), redCheck},
{AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.5f), redRed128Check},
{AlphaComposite.getInstance(AlphaComposite.DST_OUT, 1.0f), redBlackCheck},
{AlphaComposite.getInstance(AlphaComposite.DST_OUT, 0.5f), redRed128Check},
{AlphaComposite.DstOver, redCheck},
{AlphaComposite.Src, redBlueCheck},
{AlphaComposite.SrcAtop, redBlueCheck},
{AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f), redBlueCheck},
{AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.5f), redBlue128Check},
{AlphaComposite.SrcOut, redBlackCheck},
{AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f), redBlueCheck},
{AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f), purple128Check},
//Uncomment after resolving issues with Xor mode on Metal
//{AlphaComposite.Xor, redBlackCheck},
{AlphaComposite.getInstance(AlphaComposite.DST_OUT, 0.0f), redCheck},
{AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.0f), redBlackCheck},
{AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.01f), redCheck},
{AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.099f), redBlue24Check}
};

private static final int TD = 10;
static WindowAlphaCompositeTest theTest;
private final Robot robot;
private JFrame frame;

private final static int DELAY = 1000;

public WindowAlphaCompositeTest() {
try {
robot = new Robot();
} catch (AWTException ex) {
throw new RuntimeException(ex);
}
}

public void performTest(final AlphaComposite ac, Validate validate) {

runSwing(() -> {
frame = new JFrame();
frame.setBounds(100, 100, 100, 150);
JComponent contentPane = (JComponent) frame.getContentPane();
JPanel comp = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
renderComposite((Graphics2D) g, ac, 100, 100);
}
};
contentPane.add(comp);
comp.setBackground(Color.BLACK);
frame.setVisible(true);
});

robot.delay(DELAY);

for (int px = 10; px <= 90; px += 20) {
Color c = getTestPixel(px, 90);

if (!validate.validate(px, 90, c)) {
throw new RuntimeException("Test failed. Incorrect color " + c +
" at (" + px + "," + 90 + ") with composite rule=" + ac.getRule() +
" alpha=" + ac.getAlpha());
}
}

runSwing(() -> frame.dispose());

frame = null;
}

public void renderComposite(Graphics2D g, AlphaComposite ac,
int w, int h)
{
// draw stage
// outer rect
g.setComposite(AlphaComposite.SrcOver); // as default
g.setPaint(Color.red);
g.fillRect(0, 0, w, h);

// inner rect
g.setComposite(ac);
g.setPaint(Color.blue);
g.fillRect(w/4, h/4, w/2, h/2);
}

private Color getTestPixel(int x, int y) {
Rectangle bounds = frame.getBounds();
BufferedImage screenImage = robot.createScreenCapture(bounds);
int rgb = screenImage.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
return new Color(red, green, blue);
}

private static boolean validateColor(Color c, Color expected) {
return Math.abs(c.getRed() - expected.getRed()) <= TD &&
Math.abs(c.getGreen() - expected.getGreen()) <= TD &&
Math.abs(c.getBlue() - expected.getBlue()) <= TD;
}

public void dispose() {
if (frame != null) {
frame.dispose();
frame = null;
}
}

private static void runSwing(Runnable r) {
try {
SwingUtilities.invokeAndWait(r);
} catch (InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
if (!System.getProperty("os.name").contains("OS X")) {
System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
return;
}

try {
for (Object[] alphaComposite : alphaComposites) {
if (alphaComposite[1] == null) continue;
runSwing(() -> theTest = new WindowAlphaCompositeTest());
theTest.performTest((AlphaComposite) alphaComposite[0], (Validate) alphaComposite[1]);
}
} finally {
if (theTest != null) {
runSwing(() -> theTest.dispose());
}
}
}
}