28package com.jogamp.opengl.test.junit.jogl.awt;
30import java.applet.Applet;
31import java.awt.BorderLayout;
33import java.awt.DisplayMode;
34import java.awt.EventQueue;
36import java.awt.GraphicsDevice;
37import java.awt.GraphicsEnvironment;
38import java.awt.Insets;
39import java.awt.Rectangle;
40import java.awt.event.KeyEvent;
41import java.awt.event.KeyListener;
42import java.awt.event.MouseEvent;
43import java.awt.event.MouseMotionListener;
44import java.awt.event.WindowAdapter;
45import java.awt.event.WindowEvent;
46import java.lang.reflect.InvocationTargetException;
47import java.util.Timer;
48import java.util.TimerTask;
50import com.jogamp.opengl.*;
51import com.jogamp.opengl.awt.GLCanvas;
53import org.junit.Assume;
55import org.junit.FixMethodOrder;
56import org.junit.runners.MethodSorters;
58import com.jogamp.common.os.Clock;
59import com.jogamp.common.os.Platform;
60import com.jogamp.common.util.VersionNumber;
61import com.jogamp.common.util.awt.AWTEDTExecutor;
62import com.jogamp.opengl.util.AnimatorBase;
63import com.jogamp.opengl.test.junit.util.MiscUtils;
64import com.jogamp.opengl.test.junit.util.UITestCase;
88@FixMethodOrder(MethodSorters.NAME_ASCENDING)
90 static int framesPerTest = 240;
92 static class MiniPApplet
extends Applet implements MouseMotionListener, KeyListener {
93 private static final long serialVersionUID = 1L;
99 public int frameRate = 120;
100 public int numSamples = 4;
102 public boolean fullScreen =
false;
103 public boolean useAnimator =
true;
104 public boolean resizeableFrame =
true;
106 public boolean restartCanvas =
true;
107 public int restartTimeout = 100;
109 public boolean printThreadInfo =
false;
110 public boolean printEventInfo =
false;
119 String OPENGL_VENDOR;
120 String OPENGL_RENDERER;
121 String OPENGL_VERSION;
122 String OPENGL_EXTENSIONS;
124 int currentSamples = -1;
131 private SimpleListener listener;
132 private CustomAnimator animator;
134 private long beforeTime;
135 private long overSleepTime;
136 private final long frameRatePeriod = 1000000000L / frameRate;
138 private boolean initialized =
false;
139 private boolean osxCALayerAWTModBug =
false;
140 boolean justInitialized =
true;
142 private double theta = 0;
143 private double s = 0;
144 private double c = 0;
146 private long millisOffset;
147 private int fcount, lastm;
149 private final int fint = 3;
151 private boolean setFramerate =
false;
152 private boolean restarted =
false;
154 private int frameCount = 0;
156 void run()
throws InterruptedException, InvocationTargetException {
160 while ( frameCount < framesPerTest ) {
165 if (restartCanvas && restartTimeout == frameCount) {
170 animator.requestRender();
178 if(
null == frame ) {
188 void setup()
throws InterruptedException, InvocationTargetException {
189 if (printThreadInfo) System.out.println(
"Current thread at setup(): " + Thread.currentThread());
191 millisOffset = System.currentTimeMillis();
193 final VersionNumber version170 =
new VersionNumber(1, 7, 0);
194 osxCALayerAWTModBug = Platform.OSType.MACOS == Platform.getOSType() &&
195 0 > Platform.getJavaVersionNumber().compareTo(version170);
196 System.err.println(
"OSX CALayer AWT-Mod Bug "+osxCALayerAWTModBug);
197 System.err.println(
"OSType "+Platform.getOSType());
198 System.err.println(
"Java Version "+Platform.getJavaVersionNumber());
204 final MiniPApplet applet =
this;
206 final GraphicsEnvironment environment =
207 GraphicsEnvironment.getLocalGraphicsEnvironment();
208 final GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
209 frame =
new Frame(displayDevice.getDefaultConfiguration());
211 final Rectangle fullScreenRect;
213 final DisplayMode mode = displayDevice.getDisplayMode();
214 fullScreenRect =
new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
216 fullScreenRect =
null;
219 AWTEDTExecutor.singleton.invoke(
true,
new Runnable() {
222 frame.setTitle(
"MiniPApplet");
226 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
229 frame.setUndecorated(
true);
230 frame.setBackground(Color.GRAY);
231 frame.setBounds(fullScreenRect);
232 frame.setVisible(
true);
234 }
catch (
final Throwable t) {
236 Assume.assumeNoException(t);
240 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
243 frame.setLayout(
null);
250 frame.setResizable(resizeableFrame);
253 frame.setBounds(fullScreenRect);
254 applet.setBounds((fullScreenRect.width - applet.width) / 2,
255 (fullScreenRect.height - applet.height) / 2,
256 applet.width, applet.height);
258 final Insets insets = frame.getInsets();
260 final int windowW = applet.width + insets.left + insets.right;
261 final int windowH = applet.height + insets.top + insets.bottom;
262 final int locationX = 100;
263 final int locationY = 100;
265 frame.setSize(windowW, windowH);
266 frame.setLocation(locationX, locationY);
268 final int usableWindowH = windowH - insets.top - insets.bottom;
269 applet.setBounds((windowW - width)/2, insets.top + (usableWindowH - height)/2, width, height);
272 }
catch (
final Throwable t) {
274 Assume.assumeNoException(t);
279 frame.addWindowListener(
new WindowAdapter() {
281 public void windowClosing(
final WindowEvent e) {
284 }
catch (
final Exception ex) {
285 Assume.assumeNoException(ex);
290 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
293 frame.setVisible(
true);
306 canvas =
new GLCanvas(capabilities);
307 canvas.setBounds(0, 0, width, height);
309 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
312 MiniPApplet.this.setLayout(
new BorderLayout());
313 MiniPApplet.this.add(canvas, BorderLayout.CENTER);
314 MiniPApplet.this.validate();
320 listener =
new SimpleListener();
323 animator =
new CustomAnimator(canvas);
329 void restart()
throws InterruptedException, InvocationTargetException {
330 System.out.println(
"Restarting surface...");
335 animator.remove(canvas);
344 canvas =
new GLCanvas(capabilities);
345 canvas.setBounds(0, 0, width, height);
348 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
351 MiniPApplet.this.setLayout(
new BorderLayout());
352 MiniPApplet.this.add(canvas, BorderLayout.CENTER);
353 MiniPApplet.this.validate();
360 animator.add(canvas);
364 setFramerate =
false;
367 System.out.println(
"Done");
370 void dispose()
throws InterruptedException, InvocationTargetException {
371 if(
null == frame ) {
378 animator.remove(canvas);
381 if( EventQueue.isDispatchThread() ) {
382 MiniPApplet.this.remove(canvas);
383 frame.remove(MiniPApplet.this);
388 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
391 MiniPApplet.this.remove(canvas);
392 frame.remove(MiniPApplet.this);
400 void draw(
final GL2 gl) {
401 if( !osxCALayerAWTModBug || !justInitialized ) {
402 AWTEDTExecutor.singleton.invoke(
true,
new Runnable() {
405 frame.setTitle(
"frame " + frameCount);
409 if (printThreadInfo) System.out.println(
"Current thread at draw(): " + Thread.currentThread());
411 if (OPENGL_VENDOR ==
null) {
416 System.out.println(OPENGL_VENDOR);
417 System.out.println(OPENGL_RENDERER);
418 System.out.println(OPENGL_VERSION);
419 System.out.println(OPENGL_EXTENSIONS);
421 final int[] temp = { 0 };
423 System.out.println(
"Maximum number of samples supported by the hardware: " + temp[0]);
424 System.out.println(
"Frame: "+frame);
425 System.out.println(
"Applet: "+MiniPApplet.this);
426 System.out.println(
"GLCanvas: "+canvas);
430 if (currentSamples == -1) {
431 final int[] temp = { 0 };
433 currentSamples = temp[0];
434 if (numSamples != currentSamples) {
435 System.err.println(
"Requested sampling level " + numSamples +
" not supported. Using " + currentSamples +
" samples instead.");
440 if (60 < frameRate) {
443 }
else if (30 < frameRate) {
452 final int[] temp = { 0 };
454 if (numSamples != temp[0]) {
455 System.err.println(
"Multisampling level requested " + numSamples +
" not supported. Using " + temp[0] +
"samples instead.");
478 final int m = (int) (System.currentTimeMillis() - millisOffset);
479 if (m - lastm > 1000 * fint) {
480 frate = (float)(fcount) / fint;
483 System.err.println(
"fps: " + frate);
488 final long afterTime = Clock.currentNanos();
489 final long timeDiff = afterTime - beforeTime;
490 final long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
494 Thread.sleep(sleepTime / 1000000L, (
int) (sleepTime % 1000000L));
495 }
catch (
final InterruptedException ex) { }
497 overSleepTime = (Clock.currentNanos() - afterTime) - sleepTime;
503 beforeTime = Clock.currentNanos();
510 justInitialized =
false;
518 justInitialized =
true;
522 public void reshape(
final GLAutoDrawable drawable,
final int x,
final int y,
final int w,
final int h) { }
526 public void mouseDragged(
final MouseEvent ev) {
527 if (printEventInfo) {
528 System.err.println(
"Mouse dragged event: " + ev);
533 public void mouseMoved(
final MouseEvent ev) {
534 if (printEventInfo) {
535 System.err.println(
"Mouse moved event: " + ev);
540 public void keyPressed(
final KeyEvent ev) {
541 if (printEventInfo) {
542 System.err.println(
"Key pressed event: " + ev);
547 public void keyReleased(
final KeyEvent ev) {
548 if (printEventInfo) {
549 System.err.println(
"Key released event: " + ev);
554 public void keyTyped(
final KeyEvent ev) {
555 if (printEventInfo) {
556 System.err.println(
"Key typed event: " + ev);
564 private Timer timer =
null;
565 private TimerTask task =
null;
566 private volatile boolean shouldRun;
570 return "Custom" + prefix +
"Animator" ;
576 if (drawable !=
null) {
587 return (timer !=
null);
592 return (timer !=
null) && (task !=
null);
595 private void startTask() {
600 task =
new TimerTask() {
601 private boolean firstRun =
true;
605 Thread.currentThread().setName(
"OPENGL");
608 if(CustomAnimator.this.shouldRun) {
609 CustomAnimator.this.animThread = Thread.currentThread();
612 synchronized (
this) {
620 fpsCounter.resetFPSCounter();
623 timer.schedule(task, 0, 1);
627 public synchronized boolean start() {
638 public synchronized boolean stop() {
654 }
catch (
final InterruptedException e) { }
659 public final synchronized boolean isPaused() {
return false; }
661 public synchronized boolean resume() {
return false; }
663 public synchronized boolean pause() {
return false; }
669 TestGLCanvasAWTActionDeadlock02AWT.MiniPApplet mini;
673 }
catch (
final Exception e) {
674 throw new RuntimeException(e);
679 }
catch (
final Exception ex) {
680 Assume.assumeNoException(ex);
685 public static void main(
final String args[]) {
686 for(
int i=0; i<args.length; i++) {
687 if(args[i].equals(
"-frames")) {
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
Specifies a set of OpenGL capabilities.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
void setSampleBuffers(final boolean enable)
Defaults to false.
void setDepthBits(final int depthBits)
Sets the number of bits requested for the depth buffer.
Specifies the the OpenGL profile.
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
A heavyweight AWT component which provides OpenGL rendering support.
GLEventListener disposeGLEventListener(final GLEventListener listener, final boolean remove)
Disposes the given listener via dispose(..) if it has been initialized and added to this queue.
final GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
GLEventListener removeGLEventListener(final GLEventListener listener)
Removes the given listener from this drawable queue.
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Sample program that relies on JOGL's mechanism to handle the OpenGL context and rendering loop when u...
static void main(final String args[])
An Animator subclass which renders one frame at the time upon calls to the requestRender() method.
final synchronized boolean isStarted()
Indicates whether this animator has been started.
synchronized boolean stop()
Stops this CustomAnimator.
final synchronized boolean isPaused()
Indicates whether this animator is started and either manually paused or paused automatically due to ...
synchronized void requestRender()
synchronized boolean start()
Starts this animator, if not running.
String getBaseName(final String prefix)
final synchronized boolean isAnimating()
Indicates whether this animator is started and is not paused.
synchronized boolean resume()
Resumes animation if paused.
synchronized boolean pause()
Pauses this animator.
CustomAnimator(final GLAutoDrawable drawable)
Creates an CustomAnimator with an initial drawable to animate.
static int atoi(final String str, final int def)
Base implementation of GLAnimatorControl
void glVertex2d(double x, double y)
Entry point to C language function: void {@native glVertex2d}(GLdouble x, GLdouble y) Part of GL_V...
void glBegin(int mode)
Entry point to C language function: void {@native glBegin}(GLenum mode) Part of GL_VERSION_1_0
void glEnd()
Entry point to C language function: void {@native glEnd}() Part of GL_VERSION_1_0
void glColor3f(float red, float green, float blue)
Entry point to C language function: void {@native glColor3f}(GLfloat red, GLfloat green,...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
GL2 getGL2()
Casts this object to the GL2 interface.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void dispose(GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void glGetIntegerv(int pname, IntBuffer data)
Entry point to C language function: void {@native glGetIntegerv}(GLenum pname, GLint * data) Part ...
static final int GL_EXTENSIONS
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_EXTENSIONS" with expr...
static final int GL_TRIANGLES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLES" with expre...
static final int GL_VERSION
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VERSION" with express...
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
static final int GL_SAMPLES
GL_ES_VERSION_2_0, GL_VERSION_1_3, GL_VERSION_ES_1_0, GL_3DFX_multisample, GL_ARB_multisample,...
String glGetString(int name)
Entry point to C language function: const GLubyte * {@native glGetString}(GLenum name) Part of GL_...
static final int GL_RENDERER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RENDERER" with expres...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_VENDOR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VENDOR" with expressi...
static final int GL_MAX_SAMPLES
GL_ES_VERSION_3_0, GL_ARB_framebuffer_object, GL_VERSION_3_0, GL_NV_framebuffer_multisample,...
void glFlush()
Entry point to C language function: void {@native glFlush}() Part of GL_ES_VERSION_2_0,...
void addKeyListener(KeyListener l)
void addMouseMotionListener(MouseMotionListener l)