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

8226384: Implement a better logic to switch between OpenGL and Metal pipeline #3851

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -28,14 +28,11 @@
import sun.java2d.NullSurfaceData;
import sun.java2d.SurfaceData;
import sun.lwawt.LWWindowPeer;
import sun.lwawt.macosx.CFRetainedResource;

import sun.lwawt.macosx.CFLayer;
import java.awt.GraphicsConfiguration;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Transparency;

public class MTLLayer extends CFRetainedResource {
public class MTLLayer extends CFLayer {

private native long nativeCreateLayer();
private static native void nativeSetScale(long layerPtr, double scale);
@@ -45,42 +42,15 @@
private static native void validate(long layerPtr, MTLSurfaceData mtlsd);
private static native void blitTexture(long layerPtr);

private LWWindowPeer peer;
private int scale = 1;

private SurfaceData surfaceData; // represents intermediate buffer (texture)

public MTLLayer(LWWindowPeer peer) {
super(0, true);

setPtr(nativeCreateLayer());
this.peer = peer;
}

public long getPointer() {
return ptr;
}

public Rectangle getBounds() {
return peer.getBounds();
}

public GraphicsConfiguration getGraphicsConfiguration() {
return peer.getGraphicsConfiguration();
}

public boolean isOpaque() {
return !peer.isTranslucent();
}

public int getTransparency() {
return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
}

public Object getDestination() {
return peer.getTarget();
}

public SurfaceData replaceSurfaceData() {
if (getBounds().isEmpty()) {
surfaceData = NullSurfaceData.theInstance;
@@ -105,10 +75,6 @@ public SurfaceData replaceSurfaceData() {
return surfaceData;
}

public SurfaceData getSurfaceData() {
return surfaceData;
}

public void validate(final MTLSurfaceData mtlsd) {
MTLRenderQueue rq = MTLRenderQueue.getInstance();
rq.lock();
@@ -26,58 +26,28 @@
package sun.java2d.opengl;

import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.awt.Transparency;

import sun.awt.CGraphicsConfig;
import sun.java2d.NullSurfaceData;
import sun.java2d.SurfaceData;
import sun.lwawt.LWWindowPeer;
import sun.lwawt.macosx.CFRetainedResource;
import sun.java2d.SurfaceData;
import sun.lwawt.macosx.CFLayer;

public class CGLLayer extends CFRetainedResource {
public class CGLLayer extends CFLayer {

private native long nativeCreateLayer();
private static native void nativeSetScale(long layerPtr, double scale);
private static native void validate(long layerPtr, CGLSurfaceData cglsd);
private static native void blitTexture(long layerPtr);

private LWWindowPeer peer;
private int scale = 1;

private SurfaceData surfaceData; // represents intermediate buffer (texture)

public CGLLayer(LWWindowPeer peer) {
super(0, true);

setPtr(nativeCreateLayer());
this.peer = peer;
}

public long getPointer() {
return ptr;
}

This conversation was marked as resolved by jayathirthrao

This comment has been minimized.

@mrserb

mrserb May 5, 2021
Member

dispose and getBounds(probably some others) seems to have the same implementation in both classes?

This comment has been minimized.

@jayathirthrao

jayathirthrao May 5, 2021
Author Member

In dispose() we call validate which internally calls corresponding RenderQueue's of OpenGL/Metal, thats why it is not moved to CFLayer.
For other getters since initialization of peer was happening in individual subclasses(which in turn calls OpenGL/Metal native initialization) i had kept getters also in subclasses, but we can move getters to CFLayer. I have updated the PR.

This comment has been minimized.

@mrserb

mrserb May 7, 2021
Member

You can add a new abstract method to the parent class: getRenderQueue(), and override it in subclasses to call MTLRenderQueue/OGL.getInstance(); In this way, you can push to the parent more methods by calling just this new getRenderQueue().

I think the only methods which cannot be pushe dto the parent is replaceSurfaceData() due to insets usage.

This comment has been minimized.

@jayathirthrao

jayathirthrao May 10, 2021
Author Member

I tried making this change.
Moved all methods except replaceSurfaceData() to CFLayer.java. Moved the jni native declaration also to CFlayer.java.
For this to link i need to change native JNI signatures also to map to CFLayer. But this will result in duplicate JNI symbols. If i change name of these native JNI calls to something like nativeMTL/CGLSetScale, i again have to use if/else check to verify which signature to call. Introducing a common native interface between MTLLayer.m and CGLLayer.m would not help because their implementation is different(Also it is out of scope of this PR).

I am not finding ways to move methods accessing native JNI calls to common CFLayer.

This comment has been minimized.

@jayathirthrao

jayathirthrao May 10, 2021
Author Member

I will push this PR. If we find appropriate way to move more methods to CFLayer.java i will handle it in a separate bug.

public Rectangle getBounds() {
return peer.getBounds();
}

public GraphicsConfiguration getGraphicsConfiguration() {
return peer.getGraphicsConfiguration();
}

public boolean isOpaque() {
return !peer.isTranslucent();
}

public int getTransparency() {
return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
}

public Object getDestination() {
return peer.getTarget();
}

public SurfaceData replaceSurfaceData() {
if (getBounds().isEmpty()) {
surfaceData = NullSurfaceData.theInstance;
@@ -98,10 +68,6 @@ public SurfaceData replaceSurfaceData() {
return surfaceData;
}

public SurfaceData getSurfaceData() {
return surfaceData;
}

public void validate(final CGLSurfaceData cglsd) {
OGLRenderQueue rq = OGLRenderQueue.getInstance();
rq.lock();
@@ -0,0 +1,80 @@
/*
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

package sun.lwawt.macosx;

import sun.java2d.SurfaceData;
import sun.java2d.NullSurfaceData;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.awt.Transparency;
import sun.lwawt.LWWindowPeer;

/**
* Common layer class between OpenGl and Metal.
*/
public abstract class CFLayer extends CFRetainedResource {
protected SurfaceData surfaceData; // represents intermediate buffer (texture)
protected LWWindowPeer peer;

protected CFLayer(long ptr, boolean disposeOnAppKitThread) {
super(ptr, disposeOnAppKitThread);
}

public abstract SurfaceData replaceSurfaceData();

@Override
public void dispose() {
super.dispose();
}

public long getPointer() {
return ptr;
}

public SurfaceData getSurfaceData() {
return surfaceData;
}

public Rectangle getBounds() {
return peer.getBounds();
}

public GraphicsConfiguration getGraphicsConfiguration() {
return peer.getGraphicsConfiguration();
}

public boolean isOpaque() {
return !peer.isTranslucent();
}

public int getTransparency() {
return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
}

public Object getDestination() {
return peer.getTarget();
}
}
@@ -32,9 +32,9 @@
import sun.java2d.SurfaceData;
import sun.java2d.metal.MTLLayer;
import sun.java2d.opengl.CGLLayer;
import sun.lwawt.macosx.CFLayer;
import sun.lwawt.LWWindowPeer;
import sun.lwawt.PlatformWindow;
import sun.lwawt.macosx.CFRetainedResource;
import sun.util.logging.PlatformLogger;


@@ -46,7 +46,7 @@
private static final PlatformLogger focusLogger = PlatformLogger.getLogger(
"sun.lwawt.macosx.focus.CPlatformEmbeddedFrame");

private CFRetainedResource windowLayer;
private CFLayer windowLayer;
private LWWindowPeer peer;
private CEmbeddedFrame target;

@@ -71,20 +71,12 @@ public LWWindowPeer getPeer() {

@Override
public long getLayerPtr() {
if (CGraphicsDevice.usingMetalPipeline()) {
return ((MTLLayer)windowLayer).getPointer();
} else {
return ((CGLLayer)windowLayer).getPointer();
}
return windowLayer.getPointer();
}

@Override
public void dispose() {
if (CGraphicsDevice.usingMetalPipeline()) {
((MTLLayer)windowLayer).dispose();
} else {
((CGLLayer)windowLayer).dispose();
}
windowLayer.dispose();
}

@Override
@@ -115,20 +107,12 @@ public FontMetrics getFontMetrics(Font f) {

@Override
public SurfaceData getScreenSurface() {
if ( CGraphicsDevice.usingMetalPipeline()) {
return ((MTLLayer)windowLayer).getSurfaceData();
} else {
return ((CGLLayer)windowLayer).getSurfaceData();
}
return windowLayer.getSurfaceData();
}

@Override
public SurfaceData replaceSurfaceData() {
if (CGraphicsDevice.usingMetalPipeline()) {
return ((MTLLayer)windowLayer).replaceSurfaceData();
} else {
return ((CGLLayer)windowLayer).replaceSurfaceData();
}
return windowLayer.replaceSurfaceData();
}

@Override
@@ -41,6 +41,7 @@

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);
@@ -51,7 +52,7 @@

private LWWindowPeer peer;
private SurfaceData surfaceData;
private CFRetainedResource windowLayer;
private CFLayer windowLayer;
private CPlatformResponder responder;

public CPlatformView() {
@@ -104,10 +105,7 @@ public void setToolTip(String msg) {
// PAINTING METHODS
// ----------------------------------------------------------------------
public SurfaceData replaceSurfaceData() {
surfaceData = (CGraphicsDevice.usingMetalPipeline()) ?
((MTLLayer)windowLayer).replaceSurfaceData() :
((CGLLayer)windowLayer).replaceSurfaceData()
;
surfaceData = windowLayer.replaceSurfaceData();
return surfaceData;
}

@@ -122,9 +120,7 @@ public void dispose() {
}

public long getWindowLayerPtr() {
return CGraphicsDevice.usingMetalPipeline() ?
((MTLLayer)windowLayer).getPointer() :
((CGLLayer)windowLayer).getPointer();
return windowLayer.getPointer();
}

public void setAutoResizable(boolean toResize) {