Jogamp
Finishing Immutable changes including GLCapabiltiesImmutable.
[jogl.git] / src / newt / classes / com / jogamp / newt / opengl / GLWindow.java
CommitLineData
a959c53b
KR
1/*
2 * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
0feca163 3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
a959c53b
KR
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 */
34
2d57c252 35package com.jogamp.newt.opengl;
a959c53b 36
dac35658 37import com.jogamp.common.GlueGenVersion;
dac35658 38import com.jogamp.nativewindow.NativeWindowVersion;
2d57c252 39import com.jogamp.newt.*;
1ad8c39d 40import com.jogamp.newt.event.*;
0feca163 41import com.jogamp.newt.impl.WindowImpl;
a959c53b 42import javax.media.nativewindow.*;
018c7e86 43import javax.media.nativewindow.util.Point;
a959c53b 44import javax.media.opengl.*;
f3bc93b0 45import com.jogamp.opengl.impl.GLDrawableHelper;
dac35658 46import com.jogamp.opengl.JoglVersion;
018c7e86 47import javax.media.nativewindow.util.Insets;
a959c53b
KR
48
49/**
0feca163
SG
50 * An implementation of {@link javax.media.opengl.GLAutoDrawable} interface,
51 * using an aggregation of a {@link com.jogamp.newt.Window} implementation.
2268a6ce
SG
52 * <P>
53 * This implementation does not make the OpenGL context current<br>
0feca163
SG
54 * before calling the various input EventListener callbacks, ie {@link com.jogamp.newt.event.MouseListener} etc.<br>
55 * This design decision is made in favor of a more performant and simplified
56 * implementation. Also the event dispatcher shall be implemented OpenGL agnostic.<br>
57 * To be able to use OpenGL commands from within such input {@link com.jogamp.newt.event.NEWTEventListener},<br>
58 * you can inject {@link javax.media.opengl.GLRunnable} objects
59 * via {@link #invoke(boolean, javax.media.opengl.GLRunnable)} to the OpenGL command stream.<br>
2268a6ce 60 * <p>
a959c53b 61 */
1b7b5d34 62public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer {
0feca163 63 private WindowImpl window;
a959c53b 64
476d1d75
MB
65 /**
66 * Constructor. Do not call this directly -- use {@link #create()} instead.
67 */
811bd23e 68 protected GLWindow(Window window) {
018c7e86 69 resetCounter();
0feca163
SG
70 this.window = (WindowImpl) window;
71 ((WindowImpl)this.window).setHandleDestroyNotify(false);
1ad8c39d 72 window.addWindowListener(new WindowAdapter() {
969e4276 73 public void windowRepaint(WindowUpdateEvent e) {
0feca163 74 if( !GLWindow.this.window.isSurfaceLockedByOtherThread() && !GLWindow.this.helper.isExternalAnimatorAnimating() ) {
969e4276
SG
75 display();
76 }
77 }
78
a959c53b
KR
79 public void windowResized(WindowEvent e) {
80 sendReshape = true;
0feca163 81 if( !GLWindow.this.window.isSurfaceLockedByOtherThread() && !GLWindow.this.helper.isExternalAnimatorAnimating() ) {
969e4276
SG
82 display();
83 }
a959c53b
KR
84 }
85
a959c53b 86 public void windowDestroyNotify(WindowEvent e) {
0feca163 87 if( !GLWindow.this.window.isSurfaceLockedByOtherThread() && !GLWindow.this.helper.isExternalAnimatorAnimating() ) {
969e4276
SG
88 destroy();
89 } else {
63be4a40
SG
90 sendDestroy = true;
91 }
a959c53b
KR
92 }
93 });
0feca163 94 this.window.setLifecycleHook(new GLLifecycleHook());
a959c53b
KR
95 }
96
0feca163
SG
97 /**
98 * Creates a new GLWindow attaching a new Window referencing a new Screen
99 * with the given GLCapabilities.
100 * <P>
101 * The resulting GLWindow owns the Window, Screen and Device, ie it will be destructed.
102 */
29e3b223 103 public static GLWindow create(GLCapabilitiesImmutable caps) {
0feca163 104 return new GLWindow(NewtFactory.createWindow(caps));
a959c53b
KR
105 }
106
0feca163
SG
107 /**
108 * Creates a new GLWindow attaching a new Window referencing the given Screen
109 * with the given GLCapabilities.
110 * <P>
111 * The resulting GLWindow owns the Window, ie it will be destructed.
112 */
29e3b223 113 public static GLWindow create(Screen screen, GLCapabilitiesImmutable caps) {
0feca163 114 return new GLWindow(NewtFactory.createWindow(screen, caps));
a959c53b
KR
115 }
116
0feca163
SG
117 /**
118 * Creates a new GLWindow attaching the given window.
119 * <P>
120 * The resulting GLWindow does not own the given Window, ie it will not be destructed.
121 */
122 public static GLWindow create(Window window) {
811bd23e 123 return new GLWindow(window);
a959c53b 124 }
6e599a26 125
0feca163
SG
126 /**
127 * Creates a new GLWindow attaching a new child Window
128 * of the given <code>parentNativeWindow</code> with the given GLCapabilities.
129 * <P>
130 * The Display/Screen will be compatible with the <code>parentNativeWindow</code>,
131 * or even identical in case it's a Newt Window.
132 * <P>
133 * The resulting GLWindow owns the Window, ie it will be destructed.
134 */
29e3b223 135 public static GLWindow create(NativeWindow parentNativeWindow, GLCapabilitiesImmutable caps) {
0feca163 136 return new GLWindow(NewtFactory.createWindow(parentNativeWindow, caps));
a959c53b 137 }
a959c53b 138
0feca163
SG
139 //----------------------------------------------------------------------
140 // Window Access
141 //
a959c53b 142
e4913066 143 public final CapabilitiesImmutable getChosenCapabilities() {
0feca163
SG
144 if (drawable == null) {
145 return window.getChosenCapabilities();
811bd23e 146 }
811bd23e 147
0feca163 148 return drawable.getChosenGLCapabilities();
a959c53b
KR
149 }
150
e4913066 151 public final CapabilitiesImmutable getRequestedCapabilities() {
0feca163 152 return window.getRequestedCapabilities();
811bd23e
SG
153 }
154
0feca163
SG
155 public final Window getWindow() {
156 return window;
811bd23e
SG
157 }
158
018c7e86
SG
159 public final NativeWindow getParent() {
160 return window.getParent();
811bd23e
SG
161 }
162
0feca163 163 public final Screen getScreen() {
a959c53b
KR
164 return window.getScreen();
165 }
166
0feca163 167 public final void setTitle(String title) {
a959c53b
KR
168 window.setTitle(title);
169 }
170
0feca163 171 public final String getTitle() {
a959c53b
KR
172 return window.getTitle();
173 }
174
0feca163 175 public final void setUndecorated(boolean value) {
a959c53b
KR
176 window.setUndecorated(value);
177 }
178
0feca163 179 public final boolean isUndecorated() {
a959c53b
KR
180 return window.isUndecorated();
181 }
182
0feca163 183 public final void setFocusAction(FocusRunnable focusAction) {
a4b16ad5
SG
184 window.setFocusAction(focusAction);
185 }
0feca163
SG
186
187 public final void requestFocus() {
188 window.requestFocus();
811bd23e
SG
189 }
190
609e3083
SG
191 public boolean hasFocus() {
192 return window.hasFocus();
193 }
194
0feca163
SG
195 public final Insets getInsets() {
196 return window.getInsets();
811bd23e 197 }
a959c53b 198
0feca163 199 public final void setPosition(int x, int y) {
a959c53b
KR
200 window.setPosition(x, y);
201 }
7bed517f 202
0feca163 203 public final boolean setFullscreen(boolean fullscreen) {
a959c53b
KR
204 return window.setFullscreen(fullscreen);
205 }
0feca163
SG
206
207 public final boolean isFullscreen() {
208 return window.isFullscreen();
811bd23e 209 }
a959c53b 210
0feca163 211 public final boolean isVisible() {
a959c53b
KR
212 return window.isVisible();
213 }
214
0feca163
SG
215 public final String toString() {
216 return "NEWT-GLWindow[ \n\tHelper: " + helper + ", \n\tDrawable: " + drawable +
217 ", \n\tContext: " + context + /** ", \n\tWindow: "+window+", \n\tFactory: "+factory+ */ "]";
a959c53b
KR
218 }
219
0feca163
SG
220 public final int reparentWindow(NativeWindow newParent) {
221 return window.reparentWindow(newParent);
a959c53b
KR
222 }
223
0feca163
SG
224 public final int reparentWindow(NativeWindow newParent, boolean forceDestroyCreate) {
225 return window.reparentWindow(newParent, forceDestroyCreate);
a959c53b
KR
226 }
227
0feca163
SG
228 public final void removeChild(NativeWindow win) {
229 window.removeChild(win);
a959c53b
KR
230 }
231
0feca163
SG
232 public final void addChild(NativeWindow win) {
233 window.addChild(win);
a959c53b
KR
234 }
235
a34d04ad
SG
236 public void screenModeChangeNotify(ScreenMode sm) {
237 window.screenModeChangeNotify(sm);
238 }
239
240 public void screenModeChanged(ScreenMode sm, boolean success) {
241 window.screenModeChanged(sm, success);
242 }
243
0feca163
SG
244 //----------------------------------------------------------------------
245 // Window.LifecycleHook Implementation
246 //
969e4276 247
db4d7d19
SG
248 public final void destroy() {
249 window.destroy();
bf584fba
SG
250 }
251
0feca163
SG
252 public final void setVisible(boolean visible) {
253 window.setVisible(visible);
a959c53b
KR
254 }
255
0feca163
SG
256 public final void setSize(int width, int height) {
257 window.setSize(width, height);
a959c53b
KR
258 }
259
0feca163
SG
260 public final boolean isValid() {
261 return window.isValid();
969e4276 262 }
a959c53b 263
0feca163
SG
264 public final boolean isNativeValid() {
265 return window.isNativeValid();
a959c53b
KR
266 }
267
018c7e86
SG
268 public Point getLocationOnScreen(Point storage) {
269 return window.getLocationOnScreen(storage);
270 }
271
0feca163
SG
272 // Hide methods here ..
273 protected class GLLifecycleHook implements WindowImpl.LifecycleHook {
274
275 class DisposeAction implements Runnable {
276 public void run() {
277 // Lock: Covered by DestroyAction ..
278 helper.dispose(GLWindow.this);
279 }
280 }
281 DisposeAction disposeAction = new DisposeAction();
282
283 /** Window.LifecycleHook */
db4d7d19
SG
284 public synchronized void destroyActionPreLock() {
285 // nop
e6225fce
SG
286 }
287
288 /** Window.LifecycleHook */
db4d7d19 289 public synchronized void destroyActionInLock() {
0feca163 290 if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) {
db4d7d19 291 String msg = new String("GLWindow.destroy() "+Thread.currentThread()+", start");
0feca163
SG
292 System.err.println(msg);
293 //Exception e1 = new Exception(msg);
294 //e1.printStackTrace();
295 }
296
297 if( window.isNativeValid() && null != drawable && drawable.isRealized() ) {
298 if( null != context && context.isCreated() ) {
299 // Catch dispose GLExceptions by GLEventListener, just 'print' them
300 // so we can continue with the destruction.
301 try {
302 helper.invokeGL(drawable, context, disposeAction, null);
303 } catch (GLException gle) {
304 gle.printStackTrace();
305 }
306 context.destroy();
307 }
308 drawable.setRealized(false);
309 }
310 context = null;
311 drawable = null;
312
db4d7d19
SG
313 if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) {
314 System.err.println("GLWindow.destroy() "+Thread.currentThread()+", fin");
0feca163 315 }
db4d7d19 316 }
0feca163 317
db4d7d19
SG
318 /** Window.LifecycleHook */
319 public synchronized void invalidate(boolean unrecoverable) {
0feca163 320 if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) {
db4d7d19
SG
321 String msg = new String("GLWindow.invalidate("+unrecoverable+") "+Thread.currentThread()+", start");
322 System.err.println(msg);
323 //Exception e1 = new Exception(msg);
324 //e1.printStackTrace();
325 }
326 if(unrecoverable) {
327 GLAnimatorControl ctrl = GLWindow.this.getAnimator();
328 if ( null!=ctrl && ctrl.isStarted() ) {
329 ctrl.stop();
330 }
331 helper=null;
0feca163
SG
332 }
333 }
334
335 /** Window.LifecycleHook */
2c1a870f 336 public synchronized void resetCounter() {
0feca163 337 if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) {
2c1a870f
SG
338 System.err.println("GLWindow.resetCounter() "+Thread.currentThread());
339 }
340 GLWindow.this.resetCounter();
341 }
342
343 public synchronized void setVisibleActionPost(boolean visible, boolean nativeWindowCreated) {
344 if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) {
345 String msg = new String("GLWindow.setVisibleActionPost("+visible+", "+nativeWindowCreated+") "+Thread.currentThread()+", start");
0feca163
SG
346 System.err.println(msg);
347 // Exception e1 = new Exception(msg);
348 // e1.printStackTrace();
349 }
350
351 /* if (nativeWindowCreated && null != context) {
352 throw new GLException("InternalError: Native Windows has been just created, but context wasn't destroyed (is not null)");
353 } */
354 if (null == context && visible && 0 != window.getWindowHandle() && 0<getWidth()*getHeight()) {
355 NativeWindow nw;
356 if (window.getWrappedWindow() != null) {
357 nw = NativeWindowFactory.getNativeWindow(window.getWrappedWindow(), window.getGraphicsConfiguration());
358 } else {
359 nw = window;
360 }
29e3b223 361 GLCapabilitiesImmutable glCaps = (GLCapabilitiesImmutable) nw.getGraphicsConfiguration().getNativeGraphicsConfiguration().getChosenCapabilities();
0feca163
SG
362 if(null==factory) {
363 factory = GLDrawableFactory.getFactory(glCaps.getGLProfile());
364 }
365 if(null==drawable) {
366 drawable = factory.createGLDrawable(nw);
367 }
368 drawable.setRealized(true);
369 context = drawable.createContext(null);
0feca163
SG
370 }
371 if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) {
2c1a870f 372 String msg = new String("GLWindow.setVisibleActionPost("+visible+", "+nativeWindowCreated+") "+Thread.currentThread()+", fin");
0feca163
SG
373 System.err.println(msg);
374 //Exception e1 = new Exception(msg);
375 //e1.printStackTrace();
376 }
377 }
378
379 boolean animatorPaused = false;
380
a0e9d6c8 381 public synchronized void pauseRenderingAction() {
0feca163
SG
382 GLAnimatorControl ctrl = GLWindow.this.getAnimator();
383 if ( null!=ctrl && ctrl.isAnimating() && ctrl.getThread() != Thread.currentThread() ) {
384 animatorPaused = true;
385 ctrl.pause();
386 }
387 }
388
a0e9d6c8 389 public synchronized void resumeRenderingAction() {
0feca163
SG
390 GLAnimatorControl ctrl = GLWindow.this.getAnimator();
391 if ( null!=ctrl && animatorPaused ) {
392 animatorPaused = false;
393 ctrl.resume();
394 }
395 }
396 }
a0e9d6c8 397
a959c53b
KR
398 //----------------------------------------------------------------------
399 // OpenGL-related methods and state
400 //
401
a959c53b
KR
402 private GLDrawableFactory factory;
403 private GLDrawable drawable;
404 private GLContext context;
405 private GLDrawableHelper helper = new GLDrawableHelper();
406 // To make reshape events be sent immediately before a display event
407 private boolean sendReshape=false;
408 private boolean sendDestroy=false;
409 private boolean perfLog = false;
0feca163
SG
410 private long startTime, curTime, lastCheck;
411 private int totalFrames, lastFrames;
412
a959c53b
KR
413 public GLDrawableFactory getFactory() {
414 return factory;
415 }
416
417 public void setContext(GLContext newCtx) {
418 context = newCtx;
419 }
420
421 public GLContext getContext() {
422 return context;
423 }
424
425 public GL getGL() {
426 if (context == null) {
427 return null;
428 }
429 return context.getGL();
430 }
431
4e0a5af0 432 public GL setGL(GL gl) {
a959c53b
KR
433 if (context != null) {
434 context.setGL(gl);
4e0a5af0 435 return gl;
a959c53b 436 }
4e0a5af0 437 return null;
a959c53b
KR
438 }
439
440 public void addGLEventListener(GLEventListener listener) {
77413854
SG
441 if(null!=helper) {
442 helper.addGLEventListener(listener);
443 }
a959c53b
KR
444 }
445
302f183c 446 public void addGLEventListener(int index, GLEventListener listener) {
77413854
SG
447 if(null!=helper) {
448 helper.addGLEventListener(index, listener);
449 }
302f183c
SG
450 }
451
a959c53b 452 public void removeGLEventListener(GLEventListener listener) {
77413854
SG
453 if(null!=helper) {
454 helper.removeGLEventListener(listener);
455 }
a959c53b
KR
456 }
457
0feca163 458 public void setAnimator(GLAnimatorControl animatorControl) {
77413854
SG
459 if(null!=helper) {
460 helper.setAnimator(animatorControl);
461 }
969e4276
SG
462 }
463
0feca163 464 public GLAnimatorControl getAnimator() {
77413854
SG
465 if(null!=helper) {
466 return helper.getAnimator();
467 }
468 return null;
969e4276
SG
469 }
470
0feca163
SG
471 public boolean getPerfLogEnabled() { return perfLog; }
472
473 public void enablePerfLog(boolean v) {
474 perfLog = v;
475 }
476
0d24458c 477 public void invoke(boolean wait, GLRunnable glRunnable) {
77413854
SG
478 if(null!=helper) {
479 helper.invoke(this, wait, glRunnable);
480 }
0d24458c
SG
481 }
482
a959c53b 483 public void display() {
cf4c4037
SG
484 display(false);
485 }
486
487 public void display(boolean forceReshape) {
6e599a26
SG
488 if( null == window ) { return; }
489
969e4276 490 if(sendDestroy || ( null!=window && window.hasDeviceChanged() && GLAutoDrawable.SCREEN_CHANGE_ACTION_ENABLED ) ) {
63be4a40
SG
491 sendDestroy=false;
492 destroy();
493 return;
494 }
969e4276 495
5e1a67c9 496 if( null == context && isVisible() && 0<getWidth()*getHeight() ) {
6e599a26
SG
497 // retry native window and drawable/context creation
498 setVisible(true);
499 }
500
0feca163 501 if( isVisible() && isNativeValid() && null != context ) {
969e4276
SG
502 if(forceReshape) {
503 sendReshape = true;
a92906bc 504 }
c8a9c59e 505 if( NativeSurface.LOCK_SURFACE_NOT_READY < lockSurface() ) {
1d265b51 506 try {
c8a9c59e
SG
507 helper.invokeGL(drawable, context, displayAction, initAction);
508 } finally {
509 unlockSurface();
510 }
a959c53b
KR
511 }
512 }
513 }
514
a92906bc 515 /** This implementation uses a static value */
a959c53b 516 public void setAutoSwapBufferMode(boolean onOrOff) {
77413854
SG
517 if(null!=helper) {
518 helper.setAutoSwapBufferMode(onOrOff);
519 }
a959c53b
KR
520 }
521
a92906bc 522 /** This implementation uses a static value */
a959c53b 523 public boolean getAutoSwapBufferMode() {
77413854
SG
524 if(null!=helper) {
525 return helper.getAutoSwapBufferMode();
526 }
527 return false;
a959c53b
KR
528 }
529
530 public void swapBuffers() {
2268a6ce 531 if(drawable!=null && context != null) {
1d333a77 532 // Lock: Locked Surface/Window by MakeCurrent/Release
2268a6ce 533 if (context != GLContext.getCurrent()) {
a959c53b
KR
534 // Assume we should try to make the context current before swapping the buffers
535 helper.invokeGL(drawable, context, swapBuffersAction, initAction);
536 } else {
537 drawable.swapBuffers();
538 }
539 }
540 }
541
542 class InitAction implements Runnable {
543 public void run() {
1d333a77 544 // Lock: Locked Surface/Window by MakeCurrent/Release
a959c53b 545 helper.init(GLWindow.this);
018c7e86 546 resetCounter();
a959c53b
KR
547 }
548 }
549 private InitAction initAction = new InitAction();
550
a959c53b
KR
551 class DisplayAction implements Runnable {
552 public void run() {
1d333a77 553 // Lock: Locked Surface/Window by display _and_ MakeCurrent/Release
a959c53b 554 if (sendReshape) {
969e4276 555 helper.reshape(GLWindow.this, 0, 0, getWidth(), getHeight());
a959c53b
KR
556 sendReshape = false;
557 }
558
559 helper.display(GLWindow.this);
560
561 curTime = System.currentTimeMillis();
562 totalFrames++;
563
564 if(perfLog) {
565 long dt0, dt1;
566 lastFrames++;
567 dt0 = curTime-lastCheck;
568 if ( dt0 > 5000 ) {
569 dt1 = curTime-startTime;
0feca163 570 System.err.println(dt0/1000 +"s: "+ lastFrames + "f, " + (lastFrames*1000)/dt0 + " fps, "+dt0/lastFrames+" ms/f; "+
a959c53b
KR
571 "total: "+ dt1/1000+"s, "+(totalFrames*1000)/dt1 + " fps, "+dt1/totalFrames+" ms/f");
572 lastCheck=curTime;
573 lastFrames=0;
574 }
575 }
576 }
577 }
a92906bc 578 private DisplayAction displayAction = new DisplayAction();
a959c53b 579
0feca163
SG
580 /**
581 * @return Time of the first display call in milliseconds.
582 * This value is reset if becoming visible again or reparenting.
0feca163
SG
583 */
584 public final long getStartTime() {
018c7e86 585 return startTime;
0feca163 586 }
a959c53b 587
0feca163
SG
588 /**
589 * @return Time of the last display call in milliseconds.
590 * This value is reset if becoming visible again or reparenting.
0feca163
SG
591 */
592 public final long getCurrentTime() {
018c7e86 593 return curTime;
0feca163
SG
594 }
595
596 /**
597 * @return Duration <code>getCurrentTime() - getStartTime()</code>.
598 *
599 * @see #getStartTime()
600 * @see #getCurrentTime()
601 */
602 public final long getDuration() {
603 return getCurrentTime()-getStartTime();
604 }
605
606 /**
607 * @return Number of frames displayed since the first display call, ie <code>getStartTime()</code>.
608 * This value is reset if becoming visible again or reparenting.
0feca163 609 */
018c7e86
SG
610 public final int getTotalFrames() {
611 return totalFrames;
612 }
613
614 /** Reset all counter (startTime, currentTime, frame number) */
615 public synchronized void resetCounter() {
616 startTime = System.currentTimeMillis(); // overwrite startTime to real init one
617 curTime = startTime;
618 lastCheck = startTime;
619 totalFrames = 0; lastFrames = 0;
0feca163 620 }
a959c53b 621
a959c53b
KR
622 class SwapBuffersAction implements Runnable {
623 public void run() {
624 drawable.swapBuffers();
625 }
626 }
a959c53b
KR
627 private SwapBuffersAction swapBuffersAction = new SwapBuffersAction();
628
629 //----------------------------------------------------------------------
0feca163 630 // GLDrawable methods
a959c53b
KR
631 //
632
018c7e86
SG
633 public final NativeSurface getNativeSurface() {
634 return null!=drawable ? drawable.getNativeSurface() : null;
a959c53b
KR
635 }
636
0feca163
SG
637 public final long getHandle() {
638 return null!=drawable ? drawable.getHandle() : 0;
8883fa88 639 }
640
0feca163
SG
641 public final int getX() {
642 return window.getX();
6e599a26
SG
643 }
644
0feca163
SG
645 public final int getY() {
646 return window.getY();
6e599a26
SG
647 }
648
0feca163
SG
649 public final int getWidth() {
650 return window.getWidth();
a959c53b
KR
651 }
652
0feca163
SG
653 public final int getHeight() {
654 return window.getHeight();
1d333a77
SG
655 }
656
a959c53b
KR
657 //----------------------------------------------------------------------
658 // GLDrawable methods that are not really needed
659 //
660
0feca163 661 public final GLContext createContext(GLContext shareWith) {
a959c53b
KR
662 return drawable.createContext(shareWith);
663 }
664
0feca163 665 public final void setRealized(boolean realized) {
a959c53b
KR
666 }
667
0feca163 668 public final boolean isRealized() {
811bd23e
SG
669 return ( null != drawable ) ? drawable.isRealized() : false;
670 }
671
29e3b223 672 public final GLCapabilitiesImmutable getChosenGLCapabilities() {
a959c53b
KR
673 if (drawable == null) {
674 throw new GLException("No drawable yet");
675 }
676
677 return drawable.getChosenGLCapabilities();
678 }
679
0feca163 680 public final GLProfile getGLProfile() {
a959c53b
KR
681 if (drawable == null) {
682 throw new GLException("No drawable yet");
683 }
684
685 return drawable.getGLProfile();
686 }
0feca163
SG
687
688 //----------------------------------------------------------------------
1b7b5d34
SG
689 // NEWTEventConsumer
690 //
691 public boolean consumeEvent(NEWTEvent event) {
692 return window.consumeEvent(event);
693 }
694
695 //----------------------------------------------------------------------
0feca163
SG
696 // Window completion
697 //
698 public final void windowRepaint(int x, int y, int width, int height) {
699 window.windowRepaint(x, y, width, height);
700 }
701
702 public final void enqueueEvent(boolean wait, com.jogamp.newt.event.NEWTEvent event) {
703 window.enqueueEvent(wait, event);
704 }
705
706 public final void runOnEDTIfAvail(boolean wait, final Runnable task) {
707 window.runOnEDTIfAvail(wait, task);
708 }
709
710 public final SurfaceUpdatedListener getSurfaceUpdatedListener(int index) {
711 return window.getSurfaceUpdatedListener(index);
712 }
713
714 public final SurfaceUpdatedListener[] getSurfaceUpdatedListeners() {
715 return window.getSurfaceUpdatedListeners();
716 }
717
718 public final void removeAllSurfaceUpdatedListener() {
719 window.removeAllSurfaceUpdatedListener();
720 }
721
722 public final void removeSurfaceUpdatedListener(SurfaceUpdatedListener l) {
723 window.removeSurfaceUpdatedListener(l);
724 }
725
726 public final void addSurfaceUpdatedListener(SurfaceUpdatedListener l) {
727 window.addSurfaceUpdatedListener(l);
728 }
729
730 public final void addSurfaceUpdatedListener(int index, SurfaceUpdatedListener l) throws IndexOutOfBoundsException {
731 window.addSurfaceUpdatedListener(index, l);
732 }
733
734 public void sendWindowEvent(int eventType) {
735 window.sendWindowEvent(eventType);
736 }
737
738 public final WindowListener getWindowListener(int index) {
739 return window.getWindowListener(index);
740 }
741
742 public final WindowListener[] getWindowListeners() {
743 return window.getWindowListeners();
744 }
745
746 public final void removeWindowListener(WindowListener l) {
747 window.removeWindowListener(l);
748 }
749
750 public final void addWindowListener(WindowListener l) {
751 window.addWindowListener(l);
752 }
753
754 public final void addWindowListener(int index, WindowListener l) throws IndexOutOfBoundsException {
755 window.addWindowListener(index, l);
756 }
757
758 public final void addKeyListener(KeyListener l) {
759 window.addKeyListener(l);
760 }
761
762 public final void addKeyListener(int index, KeyListener l) {
763 window.addKeyListener(index, l);
764 }
765
766 public final void removeKeyListener(KeyListener l) {
767 window.removeKeyListener(l);
768 }
769
770 public final KeyListener getKeyListener(int index) {
771 return window.getKeyListener(index);
772 }
773
774 public final KeyListener[] getKeyListeners() {
775 return window.getKeyListeners();
776 }
777
778 public final void addMouseListener(MouseListener l) {
779 window.addMouseListener(l);
780 }
781
782 public final void addMouseListener(int index, MouseListener l) {
783 window.addMouseListener(index, l);
784 }
785
786 public final void removeMouseListener(MouseListener l) {
787 window.removeMouseListener(l);
788 }
789
790 public final MouseListener getMouseListener(int index) {
791 return window.getMouseListener(index);
792 }
793
794 public final MouseListener[] getMouseListeners() {
795 return window.getMouseListeners();
796 }
797
798 //----------------------------------------------------------------------
799 // NativeWindow completion
800 //
801
802 public final int lockSurface() {
803 return window.lockSurface();
804 }
805
806 public final void unlockSurface() throws NativeWindowException {
807 window.unlockSurface();
808 }
809
810 public final boolean isSurfaceLockedByOtherThread() {
811 return window.isSurfaceLockedByOtherThread();
812 }
813
814 public final boolean isSurfaceLocked() {
815 return window.isSurfaceLocked();
816 }
817
818 public final Thread getSurfaceLockOwner() {
819 return window.getSurfaceLockOwner();
820
821 }
822
0feca163
SG
823 public final boolean surfaceSwap() {
824 return window.surfaceSwap();
825 }
826
827 public final void invalidate() {
828 window.invalidate();
829 }
830
831 public final long getWindowHandle() {
832 return window.getWindowHandle();
833
834 }
835
836 public final long getSurfaceHandle() {
837 return window.getSurfaceHandle();
838
839 }
840
841 public final AbstractGraphicsConfiguration getGraphicsConfiguration() {
842 return window.getGraphicsConfiguration();
843 }
844
845 public final long getDisplayHandle() {
846 return window.getDisplayHandle();
847 }
848
849 public final int getScreenIndex() {
850 return window.getScreenIndex();
851 }
852
018c7e86
SG
853 public final void surfaceUpdated(Object updater, NativeSurface ns, long when) {
854 window.surfaceUpdated(updater, ns, when);
0feca163 855 }
597d10f5
SG
856
857 /**
858 * A most simple JOGL AWT test entry
859 */
860 public static void main(String args[]) {
6ea02e25
SG
861 System.err.println(GlueGenVersion.getInstance());
862 System.err.println(NativeWindowVersion.getInstance());
863 System.err.print(JoglVersion.getInstance());
864 System.err.println(NewtVersion.getInstance());
29e3b223 865 GLCapabilitiesImmutable caps = new GLCapabilities( GLProfile.getDefault() );
597d10f5
SG
866
867 GLWindow glWindow = GLWindow.create(caps);
868 glWindow.setSize(128, 128);
869
870 glWindow.addGLEventListener(new GLEventListener() {
871 public void init(GLAutoDrawable drawable) {
872 GL gl = drawable.getGL();
6ea02e25 873 System.err.println(JoglVersion.getInstance().toString(gl));
597d10f5
SG
874 }
875
876 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
877 }
878
879 public void display(GLAutoDrawable drawable) {
880 }
881
882 public void dispose(GLAutoDrawable drawable) {
883 }
884 });
885
886 glWindow.setVisible(true);
db4d7d19 887 glWindow.invalidate();
597d10f5
SG
888 }
889
a959c53b 890}
http://JogAmp.org git info: FAQ, tutorial and man pages.