1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
/*
* Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
* ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
* DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
*/
package com.sun.javafx.newt;
import javax.media.opengl.*;
import com.sun.opengl.impl.GLDrawableHelper;
/**
* An implementation of {@link Window} which is customized for OpenGL
* use, and which implements the {@link
* javax.media.opengl.GLAutoDrawable} interface. For convenience, this
* window class guarantees that its OpenGL context is current inside
* the various EventListeners' callbacks (MouseListener, KeyListener,
* etc.).
*/
public class GLWindow extends Window implements GLAutoDrawable {
/**
* Event handling mode: EVENT_HANDLER_GL_NONE.
* No GL context is current, while calling the EventListener.
* This might be inconvenient, but don't impact the performance.
*
* @see com.sun.javafx.newt.GLWindow#setEventHandlerMode(int)
*/
public static final int EVENT_HANDLER_GL_NONE = 0;
/**
* Event handling mode: EVENT_HANDLER_GL_CURRENT.
* The GL context is made current, while calling the EventListener.
* This might be convenient, but impacts the performance
* due to context switches.
*
* This is the default setting!
*
* @see com.sun.javafx.newt.GLWindow#setEventHandlerMode(int)
*/
public static final int EVENT_HANDLER_GL_CURRENT = (1 << 0);
private Window window;
/** Constructor. Do not call this directly -- use {@link
create()} instead. */
protected GLWindow(Window window) {
this.window = window;
window.addWindowListener(new WindowListener() {
public void windowResized(WindowEvent e) {
sendReshape = true;
}
public void windowMoved(WindowEvent e) {
}
});
}
/** Creates a new GLWindow on the local display, screen 0, with a
dummy visual ID, and with the default GLCapabilities. */
public static GLWindow create() {
return create(null, null);
}
public static GLWindow create(boolean undecorated) {
return create(null, null, undecorated);
}
/** Creates a new GLWindow referring to the given window. */
public static GLWindow create(Window window) {
return create(window, null);
}
public static GLWindow create(GLCapabilities caps) {
return create(caps, false);
}
/** Creates a new GLWindow on the local display, screen 0, with a
dummy visual ID, and with the given GLCapabilities. */
public static GLWindow create(GLCapabilities caps, boolean undecorated) {
return create(null, caps, undecorated);
}
/** Creates a new GLWindow referring to the given window, and with the given GLCapabilities. */
public static GLWindow create(Window window, GLCapabilities caps) {
return create(window, caps, false);
}
public static GLWindow create(Window window,
GLCapabilities caps,
boolean undecorated) {
if (caps == null) {
caps = new GLCapabilities();
}
if (window == null) {
Display display = NewtFactory.createDisplay(null); // local display
Screen screen = NewtFactory.createScreen(display, 0); // screen 0
window = NewtFactory.createWindow(screen, caps, undecorated);
}
return new GLWindow(window);
}
protected void createNative(GLCapabilities caps) {
shouldNotCallThis();
}
protected void closeNative() {
shouldNotCallThis();
}
public void close() {
if (context != null) {
if (context == GLContext.getCurrent()) {
context.release();
}
context.destroy();
}
if (drawable != null) {
drawable.setRealized(false);
}
window.close();
}
public boolean getPerfLogEnabled() { return perfLog; }
public void enablePerfLog(boolean v) {
perfLog = v;
}
/**
* Sets the event handling mode.
*
* @see com.sun.javafx.newt.GLWindow#EVENT_HANDLER_GL_NONE
* @see com.sun.javafx.newt.GLWindow#EVENT_HANDLER_GL_CURRENT
*/
public void setEventHandlerMode(int mode) {
eventHandlerMode = mode;
}
public int getEventHandlerMode() {
return eventHandlerMode;
}
public void pumpMessages(int eventMask) {
if( 0 == (eventHandlerMode & EVENT_HANDLER_GL_CURRENT) ) {
window.pumpMessages(eventMask);
} else {
pumpMessagesWithEventMaskAction.eventMask = eventMask;
pumpMessagesImpl(pumpMessagesWithEventMaskAction);
}
}
public void pumpMessages() {
if( 0 == (eventHandlerMode & EVENT_HANDLER_GL_CURRENT) ) {
window.pumpMessages();
} else {
pumpMessagesImpl(pumpMessagesAction);
}
}
class PumpMessagesWithEventMaskAction implements Runnable {
private int eventMask;
public void run() {
window.pumpMessages(eventMask);
}
}
private PumpMessagesWithEventMaskAction pumpMessagesWithEventMaskAction = new PumpMessagesWithEventMaskAction();
class PumpMessagesAction implements Runnable {
public void run() {
window.pumpMessages();
}
}
private PumpMessagesAction pumpMessagesAction = new PumpMessagesAction();
private void pumpMessagesImpl(Runnable pumpMessagesAction) {
// pumpMessagesAction.run();
boolean autoSwapBuffer = helper.getAutoSwapBufferMode();
helper.setAutoSwapBufferMode(false);
try {
helper.invokeGL(drawable, context, pumpMessagesAction, initAction);
} finally {
helper.setAutoSwapBufferMode(autoSwapBuffer);
}
}
protected void dispatchMessages(int eventMask) {
shouldNotCallThis();
}
public void setVisible(boolean visible) {
window.setVisible(visible);
if (visible && context == null) {
factory = GLDrawableFactory.getFactory();
NativeWindow nw = window;
if (window.getWrappedWindow() != null) {
nw = NativeWindowFactory.getNativeWindow(window.getWrappedWindow());
}
drawable = factory.createGLDrawable(nw, window.getChosenCapabilities(), null);
window.setVisible(true);
drawable.setRealized(true);
context = drawable.createContext(null);
}
}
public void setTitle(String title) {
window.setTitle(title);
}
public String getTitle() {
return window.getTitle();
}
public void setUndecorated(boolean value) {
window.setUndecorated(value);
}
public boolean isUndecorated() {
return window.isUndecorated();
}
public void setSize(int width, int height) {
window.setSize(width, height);
}
public void setPosition(int x, int y) {
window.setPosition(x, y);
}
public boolean setFullscreen(boolean fullscreen) {
return window.setFullscreen(fullscreen);
}
public boolean isVisible() {
return window.isVisible();
}
public int getX() {
return window.getX();
}
public int getY() {
return window.getY();
}
public int getWidth() {
return window.getWidth();
}
public int getHeight() {
return window.getHeight();
}
public boolean isFullscreen() {
return window.isFullscreen();
}
public void addMouseListener(MouseListener l) {
window.addMouseListener(l);
}
public void removeMouseListener(MouseListener l) {
window.removeMouseListener(l);
}
public MouseListener[] getMouseListeners() {
return window.getMouseListeners();
}
public void addKeyListener(KeyListener l) {
window.addKeyListener(l);
}
public void removeKeyListener(KeyListener l) {
window.removeKeyListener(l);
}
public KeyListener[] getKeyListeners() {
return window.getKeyListeners();
}
public void addWindowListener(WindowListener l) {
window.addWindowListener(l);
}
public void removeWindowListener(WindowListener l) {
window.removeWindowListener(l);
}
public WindowListener[] getWindowListeners() {
return window.getWindowListeners();
}
//----------------------------------------------------------------------
// OpenGL-related methods and state
//
private int eventHandlerMode = EVENT_HANDLER_GL_CURRENT;
private GLDrawableFactory factory;
private GLDrawable drawable;
private GLContext context;
private GLDrawableHelper helper = new GLDrawableHelper();
// To make reshape events be sent immediately before a display event
private boolean sendReshape;
private boolean perfLog = false;
public GLDrawableFactory getFactory() {
return factory;
}
public GLContext getContext() {
return context;
}
public GL getGL() {
GLContext ctx = getContext();
if (ctx == null) {
return null;
}
return ctx.getGL();
}
public void setGL(GL gl) {
GLContext ctx = getContext();
if (ctx != null) {
ctx.setGL(gl);
}
}
public void addGLEventListener(GLEventListener listener) {
helper.addGLEventListener(listener);
}
public void removeGLEventListener(GLEventListener listener) {
helper.removeGLEventListener(listener);
}
public void display() {
pumpMessages();
helper.invokeGL(drawable, context, displayAction, initAction);
}
public void setAutoSwapBufferMode(boolean onOrOff) {
helper.setAutoSwapBufferMode(onOrOff);
}
public boolean getAutoSwapBufferMode() {
return helper.getAutoSwapBufferMode();
}
public void swapBuffers() {
if (context != null && context != GLContext.getCurrent()) {
// Assume we should try to make the context current before swapping the buffers
helper.invokeGL(drawable, context, swapBuffersAction, initAction);
} else {
drawable.swapBuffers();
}
}
class InitAction implements Runnable {
public void run() {
helper.init(GLWindow.this);
startTime = System.currentTimeMillis();
curTime = startTime;
if(perfLog) {
lastCheck = startTime;
totalFrames = 0; lastFrames = 0;
}
}
}
private InitAction initAction = new InitAction();
class DisplayAction implements Runnable {
public void run() {
if (sendReshape) {
int width = getWidth();
int height = getHeight();
getGL().glViewport(0, 0, width, height);
helper.reshape(GLWindow.this, 0, 0, width, height);
sendReshape = false;
}
helper.display(GLWindow.this);
curTime = System.currentTimeMillis();
totalFrames++;
if(perfLog) {
long dt0, dt1;
lastFrames++;
dt0 = curTime-lastCheck;
if ( dt0 > 5000 ) {
dt1 = curTime-startTime;
System.out.println(dt0/1000 +"s: "+ lastFrames + "f, " + (lastFrames*1000)/dt0 + " fps, "+dt0/lastFrames+" ms/f; "+
"total: "+ dt1/1000+"s, "+(totalFrames*1000)/dt1 + " fps, "+dt1/totalFrames+" ms/f");
lastCheck=curTime;
lastFrames=0;
}
}
}
}
public long getStartTime() { return startTime; }
public long getCurrentTime() { return curTime; }
public long getDuration() { return curTime-startTime; }
public int getTotalFrames() { return totalFrames; }
private long startTime = 0;
private long curTime = 0;
private long lastCheck = 0;
private int totalFrames = 0, lastFrames = 0;
private DisplayAction displayAction = new DisplayAction();
class SwapBuffersAction implements Runnable {
public void run() {
drawable.swapBuffers();
}
}
private SwapBuffersAction swapBuffersAction = new SwapBuffersAction();
//----------------------------------------------------------------------
// GLDrawable methods that are not really needed
//
public GLContext createContext(GLContext shareWith) {
return drawable.createContext(shareWith);
}
public void setRealized(boolean realized) {
}
public GLCapabilities getChosenGLCapabilities() {
if (drawable == null)
return null;
return drawable.getChosenGLCapabilities();
}
public NativeWindow getNativeWindow() {
return drawable.getNativeWindow();
}
//----------------------------------------------------------------------
// Internals only below this point
//
private void shouldNotCallThis() {
throw new RuntimeException("Should not call this");
}
}
|