Skip to content
This repository has been archived by the owner. It is now read-only.

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

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
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
// ----------------------------------------------------------------------

@@ -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;
@@ -334,6 +334,10 @@ public void initialize(Window _target, LWWindowPeer _peer, PlatformWindow _owner

responder = createPlatformResponder();
contentView.initialize(peer, responder);
boolean isTranslucent = peer != null && peer.isTranslucent();
if (!isTranslucent) {
contentView.setWindowLayerOpaque(true);
}

This comment has been minimized.

Loading
@mrserb

mrserb Jul 5, 2021
Member

I think windowLayer should be always in sync with NSWindowOpaque state. And both should be changed together via setOpaque() method.

The change above will call the "setWindowLayerOpaque" twice:

  • LWWindowPeer()->platformWindow.initialize()->contentView.setWindowLayerOpaque(true)
  • LWWindowPeer()->initializeImpl()->setOpaque()->contentView.setWindowLayerOpaque()

Rectangle bounds;
if (!IS(DECORATED, styleBits)) {
@@ -928,6 +932,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) {
@@ -282,3 +282,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:(BOOL)opaque];
}];

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());
}
}
}
}