Skip to content

Commit f3eed05

Browse files
committed
8263928: Add JAWT test files for mac
Reviewed-by: jdv
1 parent 4fbb7c2 commit f3eed05

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.*;
25+
import java.awt.event.*;
26+
import javax.swing.*;
27+
28+
public class MyMacCanvas extends Canvas {
29+
30+
static {
31+
try {
32+
System.loadLibrary("mylib");
33+
} catch (Throwable t) {
34+
System.out.println("Test failed!!");
35+
t.printStackTrace();
36+
System.exit(1);
37+
}
38+
}
39+
40+
public void addNotify() {
41+
super.addNotify();
42+
addNativeCoreAnimationLayer();
43+
}
44+
45+
public native void addNativeCoreAnimationLayer();
46+
47+
static JAWTFrame f;
48+
public static void main(String[] args) {
49+
try {
50+
Robot robot = new Robot();
51+
EventQueue.invokeLater(new Runnable() {
52+
public void run() {
53+
f = new JAWTFrame("JAWTExample");
54+
f.setBackground(Color.white);
55+
f.setLayout(new BorderLayout(10, 20));
56+
f.setLocation(50, 50);
57+
((JComponent) f.getContentPane()).setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.cyan));
58+
f.addNotify();
59+
f.pack();
60+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
61+
f.setVisible(true);
62+
}
63+
});
64+
robot.delay(5000);
65+
Color col1 = new Color(0, 0, 0);
66+
Color col2 = robot.getPixelColor(f.getX()+50, f.getY()+50);
67+
if (col1.equals(col2)) {
68+
System.out.println("Test passed!");
69+
} else {
70+
System.out.println("col1 " + col1 + " col2 " + col2);
71+
throw new RuntimeException("Color of JAWT canvas is wrong or " +
72+
"it was not rendered. " + "Check that other windows " +
73+
"do not block the test frame.");
74+
}
75+
System.exit(0);
76+
} catch (Throwable t) {
77+
System.out.println("Test failed!");
78+
t.printStackTrace();
79+
System.exit(1);
80+
}
81+
}
82+
83+
private static class JAWTFrame extends JFrame {
84+
public JAWTFrame(final String title) {
85+
super(title);
86+
}
87+
88+
public void addNotify() {
89+
super.addNotify(); // ensures native component hierarchy is setup
90+
91+
final Component layerBackedCanvas = new MyMacCanvas();
92+
layerBackedCanvas.setPreferredSize(new Dimension(400, 200));
93+
add(layerBackedCanvas, BorderLayout.CENTER);
94+
95+
invalidate();
96+
}
97+
}
98+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
JAVA_HOME='/export/lanai/build/macosx-x64-debug/jdk'
6+
7+
$JAVA_HOME/bin/javac MyMacCanvas.java
8+
9+
gcc -v myfile.m -I"$JAVA_HOME/include" -c -o myfile.o
10+
11+
gcc -v myfile.o -L$JAVA_HOME/lib -ljawt -framework AppKit -framework OpenGL -framework Metal -framework Quartz -shared -o libmylib.jnilib
12+
13+
$JAVA_HOME/bin/java -Djava.library.path=. -Dsun.java2d.metal=True MyMacCanvas

test/jdk/java/awt/JAWT/myfile.m

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
#import "JAVAVM/jawt_md.h"
25+
#import <Quartz/Quartz.h>
26+
27+
/*
28+
* Pass the block to a selector of a class that extends NSObject
29+
* There is no need to copy the block since this class always waits.
30+
*/
31+
@interface BlockRunner : NSObject { }
32+
33+
+ (void)invokeBlock:(void (^)())block;
34+
@end
35+
36+
@implementation BlockRunner
37+
38+
+ (void)invokeBlock:(void (^)())block{
39+
block();
40+
}
41+
42+
+ (void)performBlock:(void (^)())block {
43+
[self performSelectorOnMainThread:@selector(invokeBlock:) withObject:block waitUntilDone:YES];
44+
}
45+
46+
@end
47+
48+
49+
/*
50+
* Class: MyMacCanvas
51+
* Method: paint
52+
* SIgnature: (Ljava/awt/Graphics;)V
53+
*/
54+
JNIEXPORT void JNICALL Java_MyMacCanvas_addNativeCoreAnimationLayer
55+
(JNIEnv* env, jobject canvas, jobject graphics)
56+
{
57+
printf("paint called\n");
58+
59+
JAWT awt;
60+
awt.version = JAWT_VERSION_1_4 | JAWT_MACOSX_USE_CALAYER;
61+
if (JAWT_GetAWT(env, &awt) == JNI_FALSE) {
62+
printf("AWT Not found\n");
63+
return;
64+
}
65+
66+
printf("JAWT found\n");
67+
68+
/* Get the drawing surface */
69+
JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas);
70+
if (ds == NULL) {
71+
printf("NULL drawing surface\n");
72+
return;
73+
}
74+
75+
/* Lock the drawing surface */
76+
jint lock = ds->Lock(ds);
77+
printf("Lock value %d\n", (int)lock);
78+
if((lock & JAWT_LOCK_ERROR) != 0) {
79+
printf("Error locking surface");
80+
return;
81+
}
82+
83+
/* Get the drawing surface info */
84+
JAWT_DrawingSurfaceInfo *dsi = ds->GetDrawingSurfaceInfo(ds);
85+
if (dsi == NULL) {
86+
printf("Error getting surface info\n");
87+
ds->Unlock(ds);
88+
return;
89+
}
90+
91+
// create and attach the layer on the AppKit thread
92+
void (^block)() = ^(){
93+
// attach the "root layer" to the AWT Canvas surface layers
94+
CALayer *layer = [[CALayer new] autorelease];
95+
id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)dsi->platformInfo;
96+
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB ();
97+
CGFloat rgba[4] = {0.0, 0.0, 0.0, 1.0};
98+
CGColorRef color = CGColorCreate (colorspace, rgba);
99+
layer.backgroundColor = color;
100+
surfaceLayers.layer = layer;
101+
};
102+
103+
if ([NSThread isMainThread]) {
104+
block();
105+
} else {
106+
[BlockRunner performBlock:block];
107+
}
108+
109+
/* Free the drawing surface info */
110+
ds->FreeDrawingSurfaceInfo(dsi);
111+
112+
/* Unlock the drawing surface */
113+
ds->Unlock(ds);
114+
115+
/* Free the drawing surface */
116+
awt.FreeDrawingSurface(ds);
117+
}

0 commit comments

Comments
 (0)