Skip to content

Commit 53db2a0

Browse files
committed
8226384: Implement a better logic to switch between OpenGL and Metal pipeline
Reviewed-by: prr
1 parent 1603ca2 commit 53db2a0

File tree

5 files changed

+95
-103
lines changed

5 files changed

+95
-103
lines changed

src/java.desktop/macosx/classes/sun/java2d/metal/MTLLayer.java

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@
2828
import sun.java2d.NullSurfaceData;
2929
import sun.java2d.SurfaceData;
3030
import sun.lwawt.LWWindowPeer;
31-
import sun.lwawt.macosx.CFRetainedResource;
32-
31+
import sun.lwawt.macosx.CFLayer;
3332
import java.awt.GraphicsConfiguration;
3433
import java.awt.Insets;
35-
import java.awt.Rectangle;
36-
import java.awt.Transparency;
3734

38-
public class MTLLayer extends CFRetainedResource {
35+
public class MTLLayer extends CFLayer {
3936

4037
private native long nativeCreateLayer();
4138
private static native void nativeSetScale(long layerPtr, double scale);
@@ -45,42 +42,15 @@ public class MTLLayer extends CFRetainedResource {
4542
private static native void validate(long layerPtr, MTLSurfaceData mtlsd);
4643
private static native void blitTexture(long layerPtr);
4744

48-
private LWWindowPeer peer;
4945
private int scale = 1;
5046

51-
private SurfaceData surfaceData; // represents intermediate buffer (texture)
52-
5347
public MTLLayer(LWWindowPeer peer) {
5448
super(0, true);
5549

5650
setPtr(nativeCreateLayer());
5751
this.peer = peer;
5852
}
5953

60-
public long getPointer() {
61-
return ptr;
62-
}
63-
64-
public Rectangle getBounds() {
65-
return peer.getBounds();
66-
}
67-
68-
public GraphicsConfiguration getGraphicsConfiguration() {
69-
return peer.getGraphicsConfiguration();
70-
}
71-
72-
public boolean isOpaque() {
73-
return !peer.isTranslucent();
74-
}
75-
76-
public int getTransparency() {
77-
return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
78-
}
79-
80-
public Object getDestination() {
81-
return peer.getTarget();
82-
}
83-
8454
public SurfaceData replaceSurfaceData() {
8555
if (getBounds().isEmpty()) {
8656
surfaceData = NullSurfaceData.theInstance;
@@ -105,10 +75,6 @@ public SurfaceData replaceSurfaceData() {
10575
return surfaceData;
10676
}
10777

108-
public SurfaceData getSurfaceData() {
109-
return surfaceData;
110-
}
111-
11278
public void validate(final MTLSurfaceData mtlsd) {
11379
MTLRenderQueue rq = MTLRenderQueue.getInstance();
11480
rq.lock();

src/java.desktop/macosx/classes/sun/java2d/opengl/CGLLayer.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,58 +26,28 @@
2626
package sun.java2d.opengl;
2727

2828
import java.awt.GraphicsConfiguration;
29-
import java.awt.Rectangle;
30-
import java.awt.Transparency;
31-
3229
import sun.awt.CGraphicsConfig;
3330
import sun.java2d.NullSurfaceData;
34-
import sun.java2d.SurfaceData;
3531
import sun.lwawt.LWWindowPeer;
36-
import sun.lwawt.macosx.CFRetainedResource;
32+
import sun.java2d.SurfaceData;
33+
import sun.lwawt.macosx.CFLayer;
3734

38-
public class CGLLayer extends CFRetainedResource {
35+
public class CGLLayer extends CFLayer {
3936

4037
private native long nativeCreateLayer();
4138
private static native void nativeSetScale(long layerPtr, double scale);
4239
private static native void validate(long layerPtr, CGLSurfaceData cglsd);
4340
private static native void blitTexture(long layerPtr);
4441

45-
private LWWindowPeer peer;
4642
private int scale = 1;
4743

48-
private SurfaceData surfaceData; // represents intermediate buffer (texture)
49-
5044
public CGLLayer(LWWindowPeer peer) {
5145
super(0, true);
5246

5347
setPtr(nativeCreateLayer());
5448
this.peer = peer;
5549
}
5650

57-
public long getPointer() {
58-
return ptr;
59-
}
60-
61-
public Rectangle getBounds() {
62-
return peer.getBounds();
63-
}
64-
65-
public GraphicsConfiguration getGraphicsConfiguration() {
66-
return peer.getGraphicsConfiguration();
67-
}
68-
69-
public boolean isOpaque() {
70-
return !peer.isTranslucent();
71-
}
72-
73-
public int getTransparency() {
74-
return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
75-
}
76-
77-
public Object getDestination() {
78-
return peer.getTarget();
79-
}
80-
8151
public SurfaceData replaceSurfaceData() {
8252
if (getBounds().isEmpty()) {
8353
surfaceData = NullSurfaceData.theInstance;
@@ -98,10 +68,6 @@ public SurfaceData replaceSurfaceData() {
9868
return surfaceData;
9969
}
10070

101-
public SurfaceData getSurfaceData() {
102-
return surfaceData;
103-
}
104-
10571
public void validate(final CGLSurfaceData cglsd) {
10672
OGLRenderQueue rq = OGLRenderQueue.getInstance();
10773
rq.lock();
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package sun.lwawt.macosx;
27+
28+
import sun.java2d.SurfaceData;
29+
import sun.java2d.NullSurfaceData;
30+
import java.awt.GraphicsConfiguration;
31+
import java.awt.Rectangle;
32+
import java.awt.Transparency;
33+
import sun.lwawt.LWWindowPeer;
34+
35+
/**
36+
* Common layer class between OpenGl and Metal.
37+
*/
38+
public abstract class CFLayer extends CFRetainedResource {
39+
protected SurfaceData surfaceData; // represents intermediate buffer (texture)
40+
protected LWWindowPeer peer;
41+
42+
protected CFLayer(long ptr, boolean disposeOnAppKitThread) {
43+
super(ptr, disposeOnAppKitThread);
44+
}
45+
46+
public abstract SurfaceData replaceSurfaceData();
47+
48+
@Override
49+
public void dispose() {
50+
super.dispose();
51+
}
52+
53+
public long getPointer() {
54+
return ptr;
55+
}
56+
57+
public SurfaceData getSurfaceData() {
58+
return surfaceData;
59+
}
60+
61+
public Rectangle getBounds() {
62+
return peer.getBounds();
63+
}
64+
65+
public GraphicsConfiguration getGraphicsConfiguration() {
66+
return peer.getGraphicsConfiguration();
67+
}
68+
69+
public boolean isOpaque() {
70+
return !peer.isTranslucent();
71+
}
72+
73+
public int getTransparency() {
74+
return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
75+
}
76+
77+
public Object getDestination() {
78+
return peer.getTarget();
79+
}
80+
}

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformEmbeddedFrame.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import sun.java2d.SurfaceData;
3333
import sun.java2d.metal.MTLLayer;
3434
import sun.java2d.opengl.CGLLayer;
35+
import sun.lwawt.macosx.CFLayer;
3536
import sun.lwawt.LWWindowPeer;
3637
import sun.lwawt.PlatformWindow;
37-
import sun.lwawt.macosx.CFRetainedResource;
3838
import sun.util.logging.PlatformLogger;
3939

4040

@@ -46,7 +46,7 @@ public class CPlatformEmbeddedFrame implements PlatformWindow {
4646
private static final PlatformLogger focusLogger = PlatformLogger.getLogger(
4747
"sun.lwawt.macosx.focus.CPlatformEmbeddedFrame");
4848

49-
private CFRetainedResource windowLayer;
49+
private CFLayer windowLayer;
5050
private LWWindowPeer peer;
5151
private CEmbeddedFrame target;
5252

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

7272
@Override
7373
public long getLayerPtr() {
74-
if (CGraphicsDevice.usingMetalPipeline()) {
75-
return ((MTLLayer)windowLayer).getPointer();
76-
} else {
77-
return ((CGLLayer)windowLayer).getPointer();
78-
}
74+
return windowLayer.getPointer();
7975
}
8076

8177
@Override
8278
public void dispose() {
83-
if (CGraphicsDevice.usingMetalPipeline()) {
84-
((MTLLayer)windowLayer).dispose();
85-
} else {
86-
((CGLLayer)windowLayer).dispose();
87-
}
79+
windowLayer.dispose();
8880
}
8981

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

116108
@Override
117109
public SurfaceData getScreenSurface() {
118-
if ( CGraphicsDevice.usingMetalPipeline()) {
119-
return ((MTLLayer)windowLayer).getSurfaceData();
120-
} else {
121-
return ((CGLLayer)windowLayer).getSurfaceData();
122-
}
110+
return windowLayer.getSurfaceData();
123111
}
124112

125113
@Override
126114
public SurfaceData replaceSurfaceData() {
127-
if (CGraphicsDevice.usingMetalPipeline()) {
128-
return ((MTLLayer)windowLayer).replaceSurfaceData();
129-
} else {
130-
return ((CGLLayer)windowLayer).replaceSurfaceData();
131-
}
115+
return windowLayer.replaceSurfaceData();
132116
}
133117

134118
@Override

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformView.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import sun.java2d.SurfaceData;
4343
import sun.java2d.opengl.CGLLayer;
44+
import sun.lwawt.macosx.CFLayer;
4445

4546
public class CPlatformView extends CFRetainedResource {
4647
private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr);
@@ -51,7 +52,7 @@ public class CPlatformView extends CFRetainedResource {
5152

5253
private LWWindowPeer peer;
5354
private SurfaceData surfaceData;
54-
private CFRetainedResource windowLayer;
55+
private CFLayer windowLayer;
5556
private CPlatformResponder responder;
5657

5758
public CPlatformView() {
@@ -104,10 +105,7 @@ public void setToolTip(String msg) {
104105
// PAINTING METHODS
105106
// ----------------------------------------------------------------------
106107
public SurfaceData replaceSurfaceData() {
107-
surfaceData = (CGraphicsDevice.usingMetalPipeline()) ?
108-
((MTLLayer)windowLayer).replaceSurfaceData() :
109-
((CGLLayer)windowLayer).replaceSurfaceData()
110-
;
108+
surfaceData = windowLayer.replaceSurfaceData();
111109
return surfaceData;
112110
}
113111

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

124122
public long getWindowLayerPtr() {
125-
return CGraphicsDevice.usingMetalPipeline() ?
126-
((MTLLayer)windowLayer).getPointer() :
127-
((CGLLayer)windowLayer).getPointer();
123+
return windowLayer.getPointer();
128124
}
129125

130126
public void setAutoResizable(boolean toResize) {

0 commit comments

Comments
 (0)