View | Details | Raw Unified | Return to bug 1398
Collapse All | Expand All

(-)a/src/test-native/Bug1398macOSContextOpsOnMainThread.c (+241 lines)
Line 0 Link Here
1
#include <Cocoa/Cocoa.h>
2
#include <JavaVM/jni.h>
3
#include <dlfcn.h>
4
#include <pthread.h>
5
6
#define DEBUG_TRACE	1
7
#define TRACE(fmt, ...) \
8
	do { if (DEBUG_TRACE) fprintf(err, "%s:%d:%s(): " fmt "\n", __FILE__, __LINE__, __func__, __VA_ARGS__); fflush(err); } while (0)
9
10
// JNI_CreateJavaVM
11
typedef jint (JNICALL CREATEVM)(JavaVM **pvm, void **env, void *args);
12
13
void die(JNIEnv *env);
14
15
@interface AppDelegate : NSObject <NSApplicationDelegate>
16
17
@end
18
19
FILE *err = NULL;
20
JavaVM *jvm = NULL;
21
22
static const char *JNI_CREATEJAVAVM = "JNI_CreateJavaVM";
23
void *jvm_lib = NULL;
24
25
void *create_vm(const char *jvm_lib_path)
26
{
27
	void *sym = NULL;
28
	jvm_lib = dlopen(jvm_lib_path, RTLD_LAZY | RTLD_GLOBAL);
29
	if (jvm_lib) {
30
		TRACE("Found libjli.dylib%s", "");
31
		sym = dlsym(jvm_lib, JNI_CREATEJAVAVM);
32
	} else {
33
		TRACE("Unable to find libjli.dylib%s", "");
34
	}
35
	return sym;
36
}
37
38
static void *launchJava(void *ptr)
39
{
40
	int k = 0;
41
42
	JNIEnv		*env = NULL;
43
	JNINativeMethod	nm_activity[20];
44
	jint		res;
45
	jclass		cls;
46
	jmethodID	mid;
47
	jobject		gui;
48
	jthrowable	ex;
49
	// JDK > 1.5
50
	JavaVMInitArgs	vm_args;
51
52
	vm_args.nOptions = 3;
53
	JavaVMOption options[vm_args.nOptions];
54
	options[0].optionString	   = "-Djava.class.path=.:../../../gluegen/build/gluegen-rt.jar:../../build/jar/jogl-all.jar";
55
//	options[1].optionString	   = "-Djava.library.path=lib";
56
	options[1].optionString	   = "-Dnativewindow.debug=all";
57
	options[2].optionString	   = "-Djogl.debug=all";
58
59
	vm_args.version		   = JNI_VERSION_1_4;
60
	vm_args.options		   = options;
61
	vm_args.ignoreUnrecognized = JNI_TRUE;
62
63
	/* Create the Java VM */
64
	CREATEVM *CreateVM = create_vm((char *)ptr);
65
	TRACE("CreateVM:%lx env:%lx vm_args:%lx", (long unsigned int)CreateVM, (long unsigned int)&env, (long unsigned int)&vm_args);
66
	res = CreateVM(&jvm, (void**)&env, &vm_args);
67
	if (res < 0) {
68
		TRACE("Can't create Java VM%s", "");
69
		exit(1);
70
	} else {
71
		TRACE("VM Created%s", "");
72
	}
73
74
	cls = (*env)->FindClass(env, "Bug1398macOSContextOpsOnMainThread");
75
	ex = (*env)->ExceptionOccurred(env);
76
	if (ex) {
77
		die(env);
78
	}
79
80
	mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
81
	if (mid == NULL)
82
		goto destroy;
83
84
	gui = (*env)->NewObject(env, cls, mid);
85
	TRACE("Just passed NewObject()...%s", "");
86
87
destroy:
88
	if ((*env)->ExceptionOccurred(env)) {
89
		// handle exception
90
	}
91
92
	if (err)
93
		fclose(err);
94
95
	if (jvm_lib) {
96
		dlclose(jvm_lib);
97
		jvm_lib = NULL;
98
	}
99
100
	die(env);
101
102
	return 0;
103
}
104
105
void show_error_dialog(JNIEnv *env, jthrowable ex)
106
{
107
	jclass dialogClass = (*env)->FindClass(env, "javax/swing/JOptionPane");
108
	jmethodID showMsg = (*env)->GetStaticMethodID(env, dialogClass, "showMessageDialog", "(Ljava/awt/Component;Ljava/lang/Object;Ljava/lang/String;I)V");
109
110
	jstring msg = (*env)->NewStringUTF(env, "\nWe be dead...\n\n");
111
112
	// extract message from exception
113
	jclass stringClass = (*env)->FindClass(env, "java/lang/String");
114
	jclass exClass = (*env)->GetObjectClass(env, ex);
115
	jmethodID exGetMessage = (*env)->GetMethodID(env, exClass, "getMessage", "()Ljava/lang/String;");
116
	jmethodID concat = (*env)->GetMethodID(env, stringClass, "concat", "(Ljava/lang/String;)Ljava/lang/String;");
117
	jstring exMsg = (*env)->CallObjectMethod(env, ex, exGetMessage);
118
	msg = (*env)->CallObjectMethod(env, msg, concat, exMsg); // append exception message to msg
119
120
	jstring title = (*env)->NewStringUTF(env, "Error");
121
	(*env)->CallStaticVoidMethod(env, dialogClass, showMsg, NULL, msg, title, (jint)0);
122
}
123
124
void die(JNIEnv *env)
125
{
126
	TRACE("\n*\n*\n*\ndieing...\n%s", "");
127
128
	jthrowable ex = (*env)->ExceptionOccurred(env);
129
	if (ex) {
130
		(*env)->ExceptionDescribe(env);
131
		show_error_dialog(env, ex);
132
	}
133
134
	// DestroyJavaVM hangs on Windows so just exit for now
135
#ifndef WIN32
136
	(*jvm)->DestroyJavaVM(jvm);
137
#else
138
	if (jvm_lib)
139
		FreeLibrary(jvm_lib);
140
	if (c_lib)
141
		FreeLibrary(c_lib);
142
#endif
143
	TRACE("VM = DEAD!%s\n", "");
144
	exit(0);
145
}
146
147
void create_jvm_thread(const char *jvm_lib_path)
148
{
149
	pthread_t vmthread;
150
151
	struct rlimit limit;
152
	size_t stack_size = 0;
153
	int rc = getrlimit(RLIMIT_STACK, &limit);
154
155
	if (rc == 0) {
156
		if (limit.rlim_cur != 0LL) {
157
			stack_size = (size_t)limit.rlim_cur;
158
		}
159
	}
160
161
	pthread_attr_t thread_attr;
162
	pthread_attr_init(&thread_attr);
163
	pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
164
	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
165
	if (stack_size > 0) {
166
		pthread_attr_setstacksize(&thread_attr, stack_size);
167
	}
168
169
	pthread_create(&vmthread, &thread_attr, launchJava, (void *)jvm_lib_path);
170
	pthread_attr_destroy(&thread_attr);
171
}
172
173
static AppDelegate* _appDelegate;
174
175
int main(int argc, const char *argv[])
176
{
177
	err = stderr;
178
179
	for (int k = 1; k < argc; k++) {
180
		TRACE("argv[%d]:%s", k, argv[k]);
181
	}
182
	if (argc < 2) {
183
		TRACE("Usage: Bug1398macOSContextOpsOnMainThread %s", "[libjli.dylib path]");
184
		exit(1);
185
	}
186
	@autoreleasepool
187
	{
188
		_appDelegate = [AppDelegate new];
189
		[NSApplication sharedApplication];
190
		[NSApp activateIgnoringOtherApps:YES];
191
		[NSApp setDelegate:_appDelegate];
192
193
		create_jvm_thread(argv[1]);
194
195
		return NSApplicationMain(argc, (const char **)argv);
196
	}
197
}
198
199
200
@interface AppDelegate ()
201
202
@property (strong) IBOutlet NSWindow *window;
203
204
@end
205
206
@implementation AppDelegate
207
208
-(id) init
209
{
210
	self = [super init];
211
	NSLog(@"init");
212
	return self;
213
}
214
215
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
216
217
	NSLog(@"App starting...");
218
219
	//Create a new Block Operation and add it to the Operation Queue
220
	NSOperationQueue *operationQueue = [NSOperationQueue new];
221
222
	NSBlockOperation *startUpCompletionOperation = [NSBlockOperation blockOperationWithBlock:^{
223
		//The startup Object has been loaded now close the splash screen
224
		//This the completion block operation
225
		[[NSOperationQueue mainQueue] addOperationWithBlock:^{
226
			NSLog(@"startUpCompletionOperation main thread? ANS - %@",[NSThread isMainThread]? @"YES":@"NO");
227
//			launchJava((void *)"jre/lib/jli/libjli.dylib");
228
		}];
229
	}];
230
231
	NSBlockOperation *startUpOperation = [NSBlockOperation blockOperationWithBlock:^{
232
		// wait for everything to load and JVM to power up
233
		sleep(3); // wait for a bit for NewObject to complete
234
	}];
235
236
	[startUpCompletionOperation addDependency:startUpOperation];
237
	[operationQueue addOperation:startUpCompletionOperation];
238
	[operationQueue addOperation:startUpOperation];
239
}
240
241
@end
(-)a/src/test-native/Bug1398macOSContextOpsOnMainThread.java (+126 lines)
Line 0 Link Here
1
import java.awt.BorderLayout;
2
import java.awt.Color;
3
import java.awt.Container;
4
import java.awt.Dimension;
5
import java.awt.DisplayMode;
6
import java.awt.GraphicsDevice;
7
import java.awt.GraphicsConfiguration;
8
import java.awt.Rectangle;
9
import java.awt.Toolkit;
10
import java.awt.event.WindowAdapter;
11
import java.awt.event.WindowEvent;
12
import javax.swing.JFrame;
13
import javax.swing.JPanel;
14
import com.jogamp.opengl.GL2;
15
import com.jogamp.opengl.GLAutoDrawable;
16
import com.jogamp.opengl.GLCapabilities;
17
import com.jogamp.opengl.GLEventListener;
18
import com.jogamp.opengl.GLProfile;
19
import com.jogamp.opengl.awt.GLCanvas;
20
import com.jogamp.opengl.util.FPSAnimator;
21
22
public class Bug1398macOSContextOpsOnMainThread extends JFrame implements GLEventListener {
23
24
	protected GLCanvas canvas;
25
26
	static {
27
		GLProfile.initSingleton();
28
	}
29
30
	public Bug1398macOSContextOpsOnMainThread() throws Exception {
31
		System.out.println("Java version: " + Runtime.class.getPackage().getSpecificationVersion() + " (" + Runtime.class.getPackage().getImplementationVersion() + ")");
32
		System.out.println("classloader:" + Thread.currentThread().getContextClassLoader());
33
		System.out.println("OS: " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch"));
34
35
		setTitle("Bug1398macOSContextOpsOnMainThread");
36
		//setUndecorated(true);
37
		//setResizable(false);
38
		setDefaultCloseOperation(EXIT_ON_CLOSE);
39
		setBackground(Color.WHITE);
40
		Dimension dim = new Dimension(800, 600);
41
		GraphicsDevice device = getGraphicsConfiguration().getDevice();
42
		DisplayMode dm = device.getDisplayMode();
43
		System.out.println("w:" + dm.getWidth() + " h:" + dm.getHeight() + " rr:" + dm.getRefreshRate() + " bits:" + dm.getBitDepth() + " dim.w:" + dim.width + " dim.h:" + dim.height);
44
		GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2));
45
		canvas = new GLCanvas(caps);
46
		canvas.setBounds(0, 0, 1, 1);
47
48
		JPanel panel = new JPanel();
49
		panel.setLayout(null);
50
		panel.setPreferredSize(dim);
51
		panel.add(canvas);
52
53
		Container c = getContentPane();
54
		c.setLayout(new BorderLayout());
55
		c.add(panel, BorderLayout.CENTER);
56
57
		pack();
58
59
		// center window
60
		GraphicsConfiguration gc = getGraphicsConfiguration();
61
		Rectangle bounds = gc.getBounds();
62
		System.out.println("gc.bounds: " + bounds);
63
		dim = Toolkit.getDefaultToolkit().getScreenSize();
64
		System.out.println("dim: " + dim);
65
		int w = getSize().width;
66
		int h = getSize().height;
67
		int x = (dim.width  - w) / 2;
68
		int y = (dim.height - h) / 2;
69
		setLocation(x, y);
70
		setVisible(true);
71
72
		final FPSAnimator animator = new FPSAnimator(canvas, 5, true);
73
		addWindowListener(new WindowAdapter() {
74
			public void windowClosing(WindowEvent e) {
75
				// Run this on another thread than the AWT event queue to
76
				// make sure the call to Animator.stop() completes before
77
				// exiting
78
				new Thread(new Runnable() {
79
					public void run() {
80
						animator.stop();
81
					}
82
				}).start();
83
			}
84
		});
85
		animator.start();
86
	}
87
88
	/**
89
	 * OpenGL funcs
90
	 */
91
	private void initExtension(GL2 gl, String glExtensionName) {
92
		if (!gl.isExtensionAvailable(glExtensionName)) {
93
			final String message = "OpenGL extension \"" + glExtensionName + "\" not available.\n\nPlease update your display driver to the latest version.";
94
			throw new RuntimeException(message);
95
		}
96
	}
97
98
	@Override
99
	public void init(GLAutoDrawable drawable) {
100
		GL2 gl = drawable.getGL().getGL2();
101
102
		int[] arg = new int[1];
103
		gl.glGetIntegerv(GL2.GL_MAX_TEXTURE_SIZE, arg, 0);
104
		System.out.println("GL_MAX_TEXTURE_SIZE:" + arg[0]);
105
106
		System.out.println("Available GL Extensions: " + gl.glGetString(GL2.GL_EXTENSIONS));
107
108
		initExtension(gl, "GL_ARB_texture_non_power_of_two");
109
	}
110
111
	@Override
112
	public void dispose(GLAutoDrawable drawable) {
113
		GL2 gl = drawable.getGL().getGL2();
114
	}
115
116
	@Override
117
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
118
		GL2 gl = drawable.getGL().getGL2();
119
	}
120
121
	@Override
122
	public void display(GLAutoDrawable drawable) {
123
		GL2 gl = drawable.getGL().getGL2();
124
	}
125
}
126
(-)a/src/test-native/make.sh (+4 lines)
Lines 8-10 gcc -o glExtensionsListGL2 glExtensionsListGL2.c -lX11 -lGL Link Here
8
gcc -o glExtensionsListGL3 glExtensionsListGL3.c -lX11 -lGL
8
gcc -o glExtensionsListGL3 glExtensionsListGL3.c -lX11 -lGL
9
gcc -o contextRetargetDrawable01 contextRetargetDrawable01.c -lX11 -lGL
9
gcc -o contextRetargetDrawable01 contextRetargetDrawable01.c -lX11 -lGL
10
gcc -o contextRetargetDrawable02 contextRetargetDrawable02.c -lX11 -lGL
10
gcc -o contextRetargetDrawable02 contextRetargetDrawable02.c -lX11 -lGL
11
12
gcc -x objective-c -framework Cocoa -o Bug1398macOSContextOpsOnMainThread Bug1398macOSContextOpsOnMainThread.c \
13
	&& javac -source 1.8 -target 1.8 -classpath ../../../gluegen/build/gluegen-rt.jar:../../build/jar/jogl-all.jar Bug1398macOSContextOpsOnMainThread.java
14

Return to bug 1398