From ea819ff768d507c37a981c1ab0bdc0cad32c6a87 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 23 Apr 2011 06:20:45 +0200 Subject: New FPSCounter, impl. by GLWindow and GLAnimatorControl (fps perf related API change) - Don't fetch System.currentTimeMillis() by default and for every frame (performance) - Default behavior is FPSCounter switched off - Enable by frame interval, ie measure each 60 frames. - FPSCounterImpl is default impl. used by impl. FPSCounter class (reduce code/redundancy) - Might be promoted to GLAutoDrawable ?! --- .../classes/com/jogamp/opengl/util/Animator.java | 14 +-- .../com/jogamp/opengl/util/AnimatorBase.java | 61 +++++---- .../com/jogamp/opengl/util/FPSAnimator.java | 2 +- .../classes/javax/media/opengl/FPSCounter.java | 117 ++++++++++++++++++ .../javax/media/opengl/GLAnimatorControl.java | 42 +------ src/jogl/classes/jogamp/opengl/FPSCounterImpl.java | 137 +++++++++++++++++++++ .../classes/com/jogamp/newt/opengl/GLWindow.java | 98 ++++++--------- .../junit/graph/demos/GPURegionNewtDemo01.java | 4 +- .../junit/graph/demos/GPURegionNewtDemo02.java | 4 +- .../graph/demos/GPURendererListenerBase01.java | 6 +- .../test/junit/graph/demos/GPUTextNewtDemo01.java | 6 +- .../test/junit/graph/demos/GPUTextNewtDemo02.java | 8 +- .../graph/demos/GPUTextRendererListenerBase01.java | 4 +- .../junit/graph/demos/ui/UIListenerBase01.java | 2 +- .../test/junit/graph/demos/ui/UINewtDemo01.java | 4 +- .../junit/jogl/acore/TestSharedContextListAWT.java | 4 +- .../jogl/acore/TestSharedContextListNEWT.java | 5 +- .../junit/jogl/demos/gl2/gears/TestGearsAWT.java | 4 +- .../jogl/demos/gl2/gears/TestGearsGLJPanelAWT.java | 4 +- .../gl2/gears/TestGearsGLJPanelAWTBug450.java | 3 +- .../jogl/demos/gl2/gears/newt/TestGearsNEWT.java | 5 +- .../gl2/gears/newt/TestGearsNewtAWTWrapper.java | 4 +- .../test/junit/jogl/glsl/GLSLMiscHelper.java | 22 +++- .../junit/jogl/glsl/TestGLSLShaderState01NEWT.java | 15 +-- .../junit/jogl/glsl/TestGLSLShaderState02NEWT.java | 21 ++-- .../test/junit/jogl/glsl/TestGLSLSimple01NEWT.java | 4 +- .../jogl/glsl/TestShaderCompilationBug459AWT.java | 4 +- .../offscreen/TestOffscreen01GLPBufferNEWT.java | 6 +- .../jogl/offscreen/TestOffscreen02BitmapNEWT.java | 2 +- .../test/junit/jogl/offscreen/WindowUtilNEWT.java | 2 +- .../junit/newt/TestDisplayLifecycle01NEWT.java | 36 +++--- .../junit/newt/TestDisplayLifecycle02NEWT.java | 52 ++++---- .../test/junit/newt/TestFocus01SwingAWTRobot.java | 6 +- .../test/junit/newt/TestFocus02SwingAWTRobot.java | 6 +- .../test/junit/newt/TestGLWindows01NEWT.java | 20 +-- .../junit/newt/TestGLWindows02NEWTAnimated.java | 15 ++- .../test/junit/newt/TestListenerCom01AWT.java | 3 +- .../test/junit/newt/TestRemoteGLWindows01NEWT.java | 4 +- .../junit/newt/parenting/TestParenting01NEWT.java | 127 +++++++++---------- .../junit/newt/parenting/TestParenting01aAWT.java | 18 ++- .../newt/parenting/TestParenting01cSwingAWT.java | 19 +-- .../junit/newt/parenting/TestParenting03AWT.java | 18 +-- .../junit/newt/parenting/TestParenting03bAWT.java | 20 +-- 43 files changed, 584 insertions(+), 374 deletions(-) create mode 100644 src/jogl/classes/javax/media/opengl/FPSCounter.java create mode 100644 src/jogl/classes/jogamp/opengl/FPSCounterImpl.java (limited to 'src') diff --git a/src/jogl/classes/com/jogamp/opengl/util/Animator.java b/src/jogl/classes/com/jogamp/opengl/util/Animator.java index 4fbd0e478..e7fbc4d58 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/Animator.java +++ b/src/jogl/classes/com/jogamp/opengl/util/Animator.java @@ -125,7 +125,7 @@ public class Animator extends AnimatorBase { class MainLoop implements Runnable { public String toString() { - return "[started "+isStartedImpl()+", animating "+isAnimatingImpl()+", paused "+isPausedImpl()+", frames "+getTotalFrames()+", drawable "+drawables.size()+"]"; + return "[started "+isStartedImpl()+", animating "+isAnimatingImpl()+", paused "+isPausedImpl()+", drawable "+drawables.size()+"]"; } public void run() { @@ -134,11 +134,7 @@ public class Animator extends AnimatorBase { if(DEBUG) { System.err.println("Animator start:" + Thread.currentThread() + ": " + toString()); } - - startTime = System.currentTimeMillis(); - curTime = startTime; - totalFrames = 0; - + fpsCounter.resetFPSCounter(); animThread = Thread.currentThread(); setIsAnimatingSynced(false); // barrier Animator.this.notifyAll(); @@ -161,9 +157,7 @@ public class Animator extends AnimatorBase { if (wasPaused) { // resume from pause -> reset counter - startTime = System.currentTimeMillis(); - curTime = startTime; - totalFrames = 0; + fpsCounter.resetFPSCounter(); if (DEBUG) { System.err.println("Animator resume:" + Thread.currentThread() + ": " + toString()); } @@ -269,7 +263,7 @@ public class Animator extends AnimatorBase { if (runnable == null) { runnable = new MainLoop(); } - resetCounter(); + fpsCounter.resetFPSCounter(); String threadName = Thread.currentThread().getName()+"-"+baseName; Thread thread; if(null==threadGroup) { diff --git a/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java b/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java index 01c2ea664..a6ba74665 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java +++ b/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java @@ -30,7 +30,12 @@ package com.jogamp.opengl.util; import com.jogamp.common.util.locks.RecursiveLock; import jogamp.opengl.Debug; +import jogamp.opengl.FPSCounterImpl; + +import java.io.PrintStream; import java.util.ArrayList; + +import javax.media.opengl.FPSCounter; import javax.media.opengl.GLAnimatorControl; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLProfile; @@ -61,9 +66,7 @@ public abstract class AnimatorBase implements GLAnimatorControl { protected Thread animThread; protected boolean ignoreExceptions; protected boolean printExceptions; - protected long startTime; - protected long curTime; - protected int totalFrames; + protected FPSCounterImpl fpsCounter = new FPSCounterImpl(); protected RecursiveLock stateSync = new RecursiveLock(); /** Creates a new, empty Animator. */ @@ -83,7 +86,6 @@ public abstract class AnimatorBase implements GLAnimatorControl { baseName = baseName.concat("-"+animatorCount); drawablesEmpty = true; } - resetCounter(); } protected abstract String getBaseName(String prefix); @@ -138,25 +140,48 @@ public abstract class AnimatorBase implements GLAnimatorControl { lightweight widgets are continually being redrawn. */ protected void display() { impl.display(drawables, ignoreExceptions, printExceptions); - curTime = System.currentTimeMillis(); - totalFrames++; + fpsCounter.tickFPS(); + } + + public final void setUpdateFPSFrames(int frames, PrintStream out) { + fpsCounter.setUpdateFPSFrames(frames, out); + } + + public final void resetFPSCounter() { + fpsCounter.resetFPSCounter(); } - public long getCurrentTime() { - return curTime; + public final int getUpdateFPSFrames() { + return fpsCounter.getUpdateFPSFrames(); + } + + public final long getFPSStartTime() { + return fpsCounter.getFPSStartTime(); } - public long getDuration() { - return curTime - startTime; + public final long getLastFPSUpdateTime() { + return fpsCounter.getLastFPSUpdateTime(); } - public long getStartTime() { - return startTime; + public final long getLastFPSPeriod() { + return fpsCounter.getLastFPSPeriod(); + } + + public final float getLastFPS() { + return fpsCounter.getLastFPS(); + } + + public final int getTotalFPSFrames() { + return fpsCounter.getTotalFPSFrames(); } - public int getTotalFrames() { - return totalFrames; + public final long getTotalFPSDuration() { + return fpsCounter.getTotalFPSDuration(); } + + public final float getTotalFPS() { + return fpsCounter.getTotalFPS(); + } public final Thread getThread() { stateSync.lock(); @@ -167,12 +192,6 @@ public abstract class AnimatorBase implements GLAnimatorControl { } } - public synchronized void resetCounter() { - startTime = System.currentTimeMillis(); // overwrite startTime to real init one - curTime = startTime; - totalFrames = 0; - } - /** Sets a flag causing this Animator to ignore exceptions produced while redrawing the drawables. By default this flag is set to false, causing any exception thrown to halt the Animator. */ @@ -189,6 +208,6 @@ public abstract class AnimatorBase implements GLAnimatorControl { } public String toString() { - return getClass().getName()+"[started "+isStarted()+", animating "+isAnimating()+", paused "+isPaused()+", frames "+getTotalFrames()+", drawable "+drawables.size()+"]"; + return getClass().getName()+"[started "+isStarted()+", animating "+isAnimating()+", paused "+isPaused()+", drawable "+drawables.size()+"]"; } } diff --git a/src/jogl/classes/com/jogamp/opengl/util/FPSAnimator.java b/src/jogl/classes/com/jogamp/opengl/util/FPSAnimator.java index f59351ad8..f7fc58160 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/FPSAnimator.java +++ b/src/jogl/classes/com/jogamp/opengl/util/FPSAnimator.java @@ -130,7 +130,7 @@ public class FPSAnimator extends AnimatorBase { } }; - resetCounter(); + fpsCounter.resetFPSCounter(); shouldRun = true; if (scheduleAtFixedRate) { diff --git a/src/jogl/classes/javax/media/opengl/FPSCounter.java b/src/jogl/classes/javax/media/opengl/FPSCounter.java new file mode 100644 index 000000000..aa42ac9e0 --- /dev/null +++ b/src/jogl/classes/javax/media/opengl/FPSCounter.java @@ -0,0 +1,117 @@ +/** + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package javax.media.opengl; + +import java.io.PrintStream; + +/** + * FPSCounter feature.
+ * An implementation initially has the FPSCounter feature disabled.
+ * Use {@link #setUpdateFPSFrames(int, PrintStream)} to enable and disable the FPSCounter feature. + */ +public interface FPSCounter { + public static final int DEFAULT_FRAMES_PER_INTERVAL = 60; + + /** + * @param frames Update interval in frames.
At every rendered frames interval the currentTime and fps values are updated. + * If the frames interval is <= 0, no update will be issued, ie the FPSCounter feature is turned off. You may choose {@link #DEFAULT_FRAMES_PER_INTERVAL}. + * @param out optional print stream where the fps values gets printed if not null at every frames interval + */ + void setUpdateFPSFrames(int frames, PrintStream out); + + /** + * Reset all performance counter (startTime, currentTime, frame number) + */ + void resetFPSCounter(); + + /** + * @return update interval in frames + * + * @see #setUpdateFPSFrames(int, PrintStream) + */ + int getUpdateFPSFrames(); + + /** + * Returns the time of the first display call in milliseconds after enabling this feature via {@link #setUpdateFPSFrames(int, PrintStream)}.
+ * This value is reset via {@link #resetFPSCounter()}. + * + * @see #setUpdateFPSFrames(int, PrintStream) + * @see #resetFPSCounter() + */ + long getFPSStartTime(); + + /** + * Returns the time of the last update interval in milliseconds, if this feature is enabled via {@link #setUpdateFPSFrames(int, PrintStream)}.
+ * This value is reset via {@link #resetFPSCounter()}. + * + * @see #setUpdateFPSFrames(int, PrintStream) + * @see #resetFPSCounter() + */ + long getLastFPSUpdateTime(); + + /** + * @return Duration of the last update interval in milliseconds. + * + * @see #setUpdateFPSFrames(int, PrintStream) + * @see #resetFPSCounter() + */ + long getLastFPSPeriod(); + + /** + * @return Last update interval's frames per seconds, {@link #getUpdateFPSFrames()} / {@link #getLastFPSPeriod()} + * + * @see #setUpdateFPSFrames(int, PrintStream) + * @see #resetFPSCounter() + */ + float getLastFPS(); + + /** + * @return Number of frame rendered since {@link #getFPSStartTime()} up to {@link #getLastFPSUpdateTime()} + * + * @see #setUpdateFPSFrames(int, PrintStream) + * @see #resetFPSCounter() + */ + int getTotalFPSFrames(); + + /** + * @return Total duration in milliseconds, {@link #getLastFPSUpdateTime()} - {@link #getFPSStartTime()} + * + * @see #setUpdateFPSFrames(int, PrintStream) + * @see #resetFPSCounter() + */ + long getTotalFPSDuration(); + + + /** + * @return Total frames per seconds, {@link #getTotalFPSFrames()} / {@link #getTotalFPSDuration()} + * + * @see #setUpdateFPSFrames(int, PrintStream) + * @see #resetFPSCounter() + */ + float getTotalFPS(); +} diff --git a/src/jogl/classes/javax/media/opengl/GLAnimatorControl.java b/src/jogl/classes/javax/media/opengl/GLAnimatorControl.java index 2c8c7cca3..83e9e22c4 100644 --- a/src/jogl/classes/javax/media/opengl/GLAnimatorControl.java +++ b/src/jogl/classes/javax/media/opengl/GLAnimatorControl.java @@ -32,47 +32,7 @@ package javax.media.opengl; * An animator control interface, * which implementation may drive a {@link javax.media.opengl.GLAutoDrawable} animation. */ -public interface GLAnimatorControl { - - /** - * @return Time of the first display call in milliseconds. - * This value is reset if started or resumed. - * - * @see #start() - * @see #resume() - */ - long getStartTime(); - - /** - * @return Time of the last display call in milliseconds. - * This value is reset if started or resumed. - * - * @see #start() - * @see #resume() - */ - long getCurrentTime(); - - /** - * @return Duration getCurrentTime() - getStartTime(). - * - * @see #getStartTime() - * @see #getCurrentTime() - */ - long getDuration(); - - - /** - * @return Number of frame cycles displayed - * since the first display call, ie getStartTime(). - * This value is reset if started or resumed. - * - * @see #start() - * @see #resume() - */ - int getTotalFrames(); - - /** Reset all performance counter (startTime, currentTime, frame number) */ - public void resetCounter(); +public interface GLAnimatorControl extends FPSCounter { /** * Indicates whether this animator is running, ie. has been started and not stopped. diff --git a/src/jogl/classes/jogamp/opengl/FPSCounterImpl.java b/src/jogl/classes/jogamp/opengl/FPSCounterImpl.java new file mode 100644 index 000000000..96d62fbb3 --- /dev/null +++ b/src/jogl/classes/jogamp/opengl/FPSCounterImpl.java @@ -0,0 +1,137 @@ +/** + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package jogamp.opengl; + +import java.io.PrintStream; +import javax.media.opengl.FPSCounter; + +/** + * Default implementation of FPSCounter to be used for FPSCounter implementing renderer. + */ +public class FPSCounterImpl implements FPSCounter { + private int fpsUpdateFramesInterval; + private PrintStream fpsOutputStream ; + private long fpsStartTime, fpsLastUpdateTime, fpsLastPeriod, fpsTotalDuration; + private int fpsTotalFrames; + private float fpsLast, fpsTotal; + + /** Creates a disabled instance */ + public FPSCounterImpl() { + setUpdateFPSFrames(0, null); + } + + /** + * Increases total frame count and updates values if feature is enabled and + * update interval is reached.
+ * + * Shall be called by actual FPSCounter implementing renderer, after display a new frame. + * + */ + public final synchronized void tickFPS() { + fpsTotalFrames++; + if(fpsUpdateFramesInterval>0 && fpsTotalFrames%fpsUpdateFramesInterval == 0) { + final long now = System.currentTimeMillis(); + fpsLastPeriod = now - fpsLastUpdateTime; + fpsLastPeriod = Math.max(fpsLastPeriod, 1); // div 0 + fpsLast = ( (float)fpsUpdateFramesInterval * 1000f ) / ( (float) fpsLastPeriod ) ; + + fpsTotalDuration = now - fpsStartTime; + fpsTotalDuration = Math.max(fpsTotalDuration, 1); // div 0 + fpsTotal= ( (float)fpsTotalFrames * 1000f ) / ( (float) fpsTotalDuration ) ; + + if(null != fpsOutputStream) { + fpsOutputStream.println(toString()); + } + + fpsLastUpdateTime = now; + } + } + + public StringBuilder toString(StringBuilder sb) { + if(null==sb) { + sb = new StringBuilder(); + } + String fpsLastS = String.valueOf(fpsLast); + fpsLastS = fpsLastS.substring(0, fpsLastS.indexOf('.') + 2); + String fpsTotalS = String.valueOf(fpsTotal); + fpsTotalS = fpsTotalS.substring(0, fpsTotalS.indexOf('.') + 2); + sb.append(fpsTotalDuration/1000 +" s: "+ fpsUpdateFramesInterval+" f / "+ fpsLastPeriod+" ms, " + fpsLastS+" fps, "+ fpsLastPeriod/fpsUpdateFramesInterval+" ms/f; "+ + "total: "+ fpsTotalFrames+" f, "+ fpsTotalS+ " fps, "+ fpsTotalDuration/fpsTotalFrames+" ms/f"); + return sb; + } + + public String toString() { + return toString(null).toString(); + } + + public final synchronized void setUpdateFPSFrames(int frames, PrintStream out) { + fpsUpdateFramesInterval = frames; + fpsOutputStream = out; + resetFPSCounter(); + } + + public final synchronized void resetFPSCounter() { + fpsStartTime = System.currentTimeMillis(); // overwrite startTime to real init one + fpsLastUpdateTime = fpsStartTime; + fpsLastPeriod = 0; + fpsTotalFrames = 0; + fpsLast = 0f; fpsTotal = 0f; + } + + public final synchronized int getUpdateFPSFrames() { + return fpsUpdateFramesInterval; + } + + public final synchronized long getFPSStartTime() { + return fpsStartTime; + } + + public final synchronized long getLastFPSUpdateTime() { + return fpsLastUpdateTime; + } + + public final synchronized long getLastFPSPeriod() { + return fpsLastPeriod; + } + + public final synchronized float getLastFPS() { + return fpsLast; + } + + public final synchronized int getTotalFPSFrames() { + return fpsTotalFrames; + } + + public final synchronized long getTotalFPSDuration() { + return fpsTotalDuration; + } + + public final synchronized float getTotalFPS() { + return fpsTotal; + } +} diff --git a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java index 48531fcf8..2dcb6345e 100644 --- a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java +++ b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java @@ -34,6 +34,7 @@ package com.jogamp.newt.opengl; +import java.io.PrintStream; import java.util.List; import com.jogamp.common.GlueGenVersion; @@ -48,6 +49,7 @@ import javax.media.nativewindow.util.Point; import javax.media.nativewindow.util.Insets; import javax.media.opengl.*; +import jogamp.opengl.FPSCounterImpl; import jogamp.opengl.GLDrawableHelper; import com.jogamp.opengl.JoglVersion; @@ -64,14 +66,14 @@ import com.jogamp.opengl.JoglVersion; * via {@link #invoke(boolean, javax.media.opengl.GLRunnable)} to the OpenGL command stream.
*

*/ -public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer { +public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer, FPSCounter { private WindowImpl window; /** * Constructor. Do not call this directly -- use {@link #create()} instead. */ protected GLWindow(Window window) { - resetCounter(); + resetFPSCounter(); this.window = (WindowImpl) window; ((WindowImpl)this.window).setHandleDestroyNotify(false); window.addWindowListener(new WindowAdapter() { @@ -365,7 +367,7 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer { if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) { System.err.println("GLWindow.resetCounter() "+Thread.currentThread()); } - GLWindow.this.resetCounter(); + GLWindow.this.resetFPSCounter(); } public synchronized void setVisibleActionPost(boolean visible, boolean nativeWindowCreated) { @@ -433,9 +435,7 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer { // To make reshape events be sent immediately before a display event private boolean sendReshape=false; private boolean sendDestroy=false; - private boolean perfLog = false; - private long startTime, curTime, lastCheck; - private int totalFrames, lastFrames; + private FPSCounterImpl fpsCounter = new FPSCounterImpl(); public GLDrawableFactory getFactory() { return factory; @@ -507,12 +507,6 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer { return null; } - public boolean getPerfLogEnabled() { return perfLog; } - - public void enablePerfLog(boolean v) { - perfLog = v; - } - public void invoke(boolean wait, GLRunnable glRunnable) { if(null!=helper) { helper.invoke(this, wait, glRunnable); @@ -577,7 +571,7 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer { public final void run() { // Lock: Locked Surface/Window by MakeCurrent/Release helper.init(GLWindow.this); - resetCounter(); + resetFPSCounter(); } } private InitAction initAction = new InitAction(); @@ -592,66 +586,50 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer { helper.display(GLWindow.this); - curTime = System.currentTimeMillis(); - totalFrames++; - - if(perfLog) { - long dt0, dt1; - lastFrames++; - dt0 = curTime-lastCheck; - if ( dt0 > 5000 ) { - dt1 = curTime-startTime; - System.err.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; - } - } + fpsCounter.tickFPS(); } } private DisplayAction displayAction = new DisplayAction(); - /** - * @return Time of the first display call in milliseconds. - * This value is reset if becoming visible again or reparenting. - */ - public final long getStartTime() { - return startTime; + public final void setUpdateFPSFrames(int frames, PrintStream out) { + fpsCounter.setUpdateFPSFrames(frames, out); + } + + public final void resetFPSCounter() { + fpsCounter.resetFPSCounter(); } - /** - * @return Time of the last display call in milliseconds. - * This value is reset if becoming visible again or reparenting. - */ - public final long getCurrentTime() { - return curTime; + public final int getUpdateFPSFrames() { + return fpsCounter.getUpdateFPSFrames(); + } + + public final long getFPSStartTime() { + return fpsCounter.getFPSStartTime(); } - /** - * @return Duration getCurrentTime() - getStartTime(). - * - * @see #getStartTime() - * @see #getCurrentTime() - */ - public final long getDuration() { - return getCurrentTime()-getStartTime(); + public final long getLastFPSUpdateTime() { + return fpsCounter.getLastFPSUpdateTime(); } - /** - * @return Number of frames displayed since the first display call, ie getStartTime(). - * This value is reset if becoming visible again or reparenting. - */ - public final int getTotalFrames() { - return totalFrames; + public final long getLastFPSPeriod() { + return fpsCounter.getLastFPSPeriod(); + } + + public final float getLastFPS() { + return fpsCounter.getLastFPS(); + } + + public final int getTotalFPSFrames() { + return fpsCounter.getTotalFPSFrames(); } - /** Reset all counter (startTime, currentTime, frame number) */ - public final synchronized void resetCounter() { - startTime = System.currentTimeMillis(); // overwrite startTime to real init one - curTime = startTime; - lastCheck = startTime; - totalFrames = 0; lastFrames = 0; + public final long getTotalFPSDuration() { + return fpsCounter.getTotalFPSDuration(); } + + public final float getTotalFPS() { + return fpsCounter.getTotalFPS(); + } private class SwapBuffersAction implements Runnable { public final void run() { diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo01.java index e2ed5f0d0..a7534dc01 100755 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo01.java @@ -28,6 +28,7 @@ package com.jogamp.opengl.test.junit.graph.demos; +import javax.media.opengl.FPSCounter; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLProfile; @@ -69,11 +70,12 @@ public class GPURegionNewtDemo01 { regionGLListener.attachInputListenerTo(window); window.addGLEventListener(regionGLListener); - window.enablePerfLog(true); + window.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); window.setVisible(true); //FPSAnimator animator = new FPSAnimator(60); Animator animator = new Animator(); + animator.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator.add(window); animator.start(); } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo02.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo02.java index 678a40c29..9353e0c06 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo02.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURegionNewtDemo02.java @@ -28,6 +28,7 @@ package com.jogamp.opengl.test.junit.graph.demos; +import javax.media.opengl.FPSCounter; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLProfile; @@ -69,11 +70,12 @@ public class GPURegionNewtDemo02 { regionGLListener.attachInputListenerTo(window); window.addGLEventListener(regionGLListener); - window.enablePerfLog(true); + window.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); window.setVisible(true); //FPSAnimator animator = new FPSAnimator(60); Animator animator = new Animator(); + animator.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator.add(window); animator.start(); } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java index 17e0a06d2..b173c1f06 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import javax.media.opengl.FPSCounter; import javax.media.opengl.GL; import javax.media.opengl.GL2ES2; import javax.media.opengl.GLAnimatorControl; @@ -228,7 +229,10 @@ public abstract class GPURendererListenerBase01 implements GLEventListener { gl.setSwapInterval(i); final GLAnimatorControl a = drawable.getAnimator(); if( null != a ) { - a.resetCounter(); + a.resetFPSCounter(); + } + if(drawable instanceof FPSCounter) { + ((FPSCounter)drawable).resetFPSCounter(); } System.err.println("Swap Interval: "+i); } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo01.java index ff22a920e..219438146 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo01.java @@ -28,6 +28,7 @@ package com.jogamp.opengl.test.junit.graph.demos; +import javax.media.opengl.FPSCounter; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLProfile; @@ -61,11 +62,12 @@ public class GPUTextNewtDemo01 { GPUTextGLListener0A textGLListener = new GPUTextGLListener0A(rs, Region.SINGLE_PASS, 0, DEBUG, TRACE); textGLListener.attachInputListenerTo(window); window.addGLEventListener(textGLListener); - - window.enablePerfLog(true); + + window.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); window.setVisible(true); // FPSAnimator animator = new FPSAnimator(10); Animator animator = new Animator(); + animator.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator.add(window); animator.start(); } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo02.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo02.java index 067d45b73..cbe597508 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo02.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextNewtDemo02.java @@ -27,6 +27,7 @@ */ package com.jogamp.opengl.test.junit.graph.demos; +import javax.media.opengl.FPSCounter; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLProfile; @@ -72,11 +73,12 @@ public class GPUTextNewtDemo02 { // ((TextRenderer)textGLListener.getRenderer()).setCacheLimit(32); textGLListener.attachInputListenerTo(window); window.addGLEventListener(textGLListener); - - window.enablePerfLog(true); + + window.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); window.setVisible(true); // FPSAnimator animator = new FPSAnimator(60); - Animator animator = new Animator(); + Animator animator = new Animator(); + animator.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator.add(window); animator.start(); } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextRendererListenerBase01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextRendererListenerBase01.java index 7199226f9..119b38584 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextRendererListenerBase01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPUTextRendererListenerBase01.java @@ -137,11 +137,11 @@ public abstract class GPUTextRendererListenerBase01 extends GPURendererListenerB final int width = drawable.getWidth(); final int height = drawable.getHeight(); final GLAnimatorControl animator = drawable.getAnimator(); - final boolean _drawFPS = drawFPS && null != animator && animator.getTotalFrames()>10; + final boolean _drawFPS = drawFPS && null != animator && animator.getTotalFPSFrames()>10; textRenderer.reshapeOrtho(null, width, height, 0.1f, 7000.0f); if(_drawFPS) { - final float fps = ( animator.getTotalFrames() * 1000.0f ) / (float) animator.getDuration() ; + final float fps = ( animator.getTotalFPSFrames() * 1000.0f ) / (float) animator.getTotalFPSDuration() ; final String fpsS = String.valueOf(fps); final int fpsSp = fpsS.indexOf('.'); textRenderer.resetModelview(null); diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java index f854d8195..c74d11f0d 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java @@ -294,7 +294,7 @@ public abstract class UIListenerBase01 implements GLEventListener { gl.setSwapInterval(i); final GLAnimatorControl a = drawable.getAnimator(); if( null != a ) { - a.resetCounter(); + a.resetFPSCounter(); } System.err.println("Swap Interval: "+i); } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UINewtDemo01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UINewtDemo01.java index e2aff5793..7d0ad83fa 100755 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UINewtDemo01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UINewtDemo01.java @@ -28,6 +28,7 @@ package com.jogamp.opengl.test.junit.graph.demos.ui; +import javax.media.opengl.FPSCounter; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLProfile; @@ -67,10 +68,11 @@ public class UINewtDemo01 { uiGLListener.attachInputListenerTo(window); window.addGLEventListener(uiGLListener); - window.enablePerfLog(true); + window.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); window.setVisible(true); Animator animator = new Animator(); + animator.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator.add(window); animator.start(); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestSharedContextListAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestSharedContextListAWT.java index bf4c493bc..10c4bce42 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestSharedContextListAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestSharedContextListAWT.java @@ -28,6 +28,7 @@ package com.jogamp.opengl.test.junit.jogl.acore; +import javax.media.opengl.FPSCounter; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLDrawableFactory; import javax.media.opengl.GLPbuffer; @@ -119,8 +120,9 @@ public class TestSharedContextListAWT extends UITestCase { GLCanvas glc2 = runTestGL(f2, animator, width, 0, true); GLCanvas glc3 = runTestGL(f3, animator, 0, height, false); + animator.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator.start(); - while(animator.isAnimating() && animator.getDuration() X11 Display). diff --git a/src/test/com/jogamp/opengl/test/junit/newt/TestGLWindows02NEWTAnimated.java b/src/test/com/jogamp/opengl/test/junit/newt/TestGLWindows02NEWTAnimated.java index 6582a96c0..f7aab9efa 100644 --- a/src/test/com/jogamp/opengl/test/junit/newt/TestGLWindows02NEWTAnimated.java +++ b/src/test/com/jogamp/opengl/test/junit/newt/TestGLWindows02NEWTAnimated.java @@ -118,8 +118,9 @@ public class TestGLWindows02NEWTAnimated extends UITestCase { Assert.assertNotNull(caps); GLWindow window = createWindow(null, caps, width, height, true /* onscreen */, false /* undecorated */); Animator animator = new Animator(window); + animator.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); Assert.assertTrue(animator.start()); - while(animator.isAnimating() && animator.getDuration() glWindow1: compatible Assert.assertEquals(true, glWindow2.isVisible()); - System.err.println("Frames(1) "+glWindow2.getTotalFrames()); + System.err.println("Frames(1) "+glWindow2.getTotalFPSFrames()); reparentAction = glWindow2.reparentWindow(glWindow1, reparentRecreate); - System.err.println("Frames(2) "+glWindow2.getTotalFrames()); + System.err.println("Frames(2) "+glWindow2.getTotalFPSFrames()); Assert.assertTrue(Window.ReparentAction.ACTION_INVALID < reparentAction); Assert.assertEquals(true, glWindow2.isVisible()); Assert.assertEquals(true, glWindow2.isNativeValid()); Assert.assertSame(glWindow1,glWindow2.getParent()); - System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B2: "+glWindow2.getTotalFrames()); - Assert.assertTrue(0 < glWindow2.getTotalFrames()); + System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B2: "+glWindow2.getTotalFPSFrames()); + Assert.assertTrue(0 < glWindow2.getTotalFPSFrames()); Assert.assertEquals(1,display1.getReferenceCount()); Assert.assertEquals(true,display1.isNativeValid()); @@ -425,8 +419,8 @@ public class TestParenting01NEWT extends UITestCase { Assert.assertEquals(true, glWindow2.isVisible()); Assert.assertEquals(true, glWindow2.isNativeValid()); Assert.assertNull(glWindow2.getParent()); - System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B3: "+glWindow2.getTotalFrames()); - Assert.assertTrue(0 < glWindow2.getTotalFrames()); + System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B3: "+glWindow2.getTotalFPSFrames()); + Assert.assertTrue(0 < glWindow2.getTotalFPSFrames()); Assert.assertEquals(1,display1.getReferenceCount()); Assert.assertEquals(true,display1.isNativeValid()); @@ -519,11 +513,6 @@ public class TestParenting01NEWT extends UITestCase { } protected void testWindowParenting03ReparentWin2TopImpl(boolean reparentRecreate) throws InterruptedException { - int x = 0; - int y = 0; - - NEWTEventFiFo eventFifo = new NEWTEventFiFo(); - Assert.assertEquals(0,Display.getActiveDisplayNumber()); Display display1 = null; Screen screen1 = null; @@ -566,12 +555,12 @@ public class TestParenting01NEWT extends UITestCase { Assert.assertSame(screen1,glWindow2.getScreen()); Assert.assertSame(display1,glWindow2.getScreen().getDisplay()); - Assert.assertEquals(0, glWindow1.getTotalFrames()); - Assert.assertEquals(0, glWindow2.getTotalFrames()); + Assert.assertEquals(0, glWindow1.getTotalFPSFrames()); + Assert.assertEquals(0, glWindow2.getTotalFPSFrames()); glWindow1.setVisible(true); - System.err.println("Frames for setVisible(): A1: "+glWindow1.getTotalFrames()+", B1: "+glWindow2.getTotalFrames()); - Assert.assertTrue(0 < glWindow2.getTotalFrames()); - Assert.assertTrue(0 < glWindow1.getTotalFrames()); + System.err.println("Frames for setVisible(): A1: "+glWindow1.getTotalFPSFrames()+", B1: "+glWindow2.getTotalFPSFrames()); + Assert.assertTrue(0 < glWindow2.getTotalFPSFrames()); + Assert.assertTrue(0 < glWindow1.getTotalFPSFrames()); Assert.assertEquals(1,display1.getReferenceCount()); Assert.assertEquals(true,display1.isNativeValid()); @@ -584,13 +573,15 @@ public class TestParenting01NEWT extends UITestCase { Assert.assertEquals(1,Display.getActiveDisplayNumber()); Animator animator1 = new Animator(glWindow1); + animator1.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator1.start(); Animator animator2 = new Animator(glWindow2); + animator2.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator2.start(); int state = 0; int reparentAction; - while(animator1.isAnimating() && animator1.getDuration()<3*durationPerTest) { + while(animator1.isAnimating() && animator1.getTotalFPSDuration()<3*durationPerTest) { Thread.sleep(durationPerTest); switch(state) { case 0: @@ -599,8 +590,8 @@ public class TestParenting01NEWT extends UITestCase { Assert.assertTrue(Window.ReparentAction.ACTION_INVALID < reparentAction); Assert.assertEquals(true, glWindow2.isVisible()); Assert.assertEquals(true, glWindow2.isNativeValid()); - System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B2: "+glWindow2.getTotalFrames()); - Assert.assertTrue(0 < glWindow2.getTotalFrames()); + System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B2: "+glWindow2.getTotalFPSFrames()); + Assert.assertTrue(0 < glWindow2.getTotalFPSFrames()); Assert.assertNull(glWindow2.getParent()); Assert.assertSame(screen1,glWindow2.getScreen()); Assert.assertSame(display1,glWindow2.getScreen().getDisplay()); @@ -612,8 +603,8 @@ public class TestParenting01NEWT extends UITestCase { Assert.assertTrue(Window.ReparentAction.ACTION_INVALID < reparentAction); Assert.assertEquals(true, glWindow2.isVisible()); Assert.assertEquals(true, glWindow2.isNativeValid()); - System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B3 "+glWindow2.getTotalFrames()); - Assert.assertTrue(0 < glWindow2.getTotalFrames()); + System.err.println("Frames for reparentWindow(parent, "+reparentRecreate+"): "+reparentAction+", B3 "+glWindow2.getTotalFPSFrames()); + Assert.assertTrue(0 < glWindow2.getTotalFPSFrames()); Assert.assertSame(glWindow1,glWindow2.getParent()); Assert.assertSame(screen1,glWindow2.getScreen()); Assert.assertSame(display1,glWindow2.getScreen().getDisplay()); diff --git a/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aAWT.java b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aAWT.java index 424fff0e2..3046eb061 100644 --- a/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aAWT.java @@ -123,8 +123,9 @@ public class TestParenting01aAWT extends UITestCase { Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent()); Animator animator1 = new Animator(glWindow1); + animator1.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err); animator1.start(); - while(animator1.isAnimating() && animator1.getDuration()