From fb03e913c0c55a9a96e93a00ef6590b9bea9a62a Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 7 Mar 2023 01:33:22 +0100 Subject: Clock: Use Clock.currentNanos() instead of System.nanoTime(); Enhancing FPSCounterImpl accuracy by maintaining timestamps in [ns] Idea: Perhaps we want to use [ns] for FPSCounter's method types by now? --- src/jogl/classes/jogamp/opengl/FPSCounterImpl.java | 51 +++++++++++++--------- src/jogl/classes/jogamp/opengl/GLContextImpl.java | 5 ++- 2 files changed, 33 insertions(+), 23 deletions(-) (limited to 'src/jogl/classes/jogamp') diff --git a/src/jogl/classes/jogamp/opengl/FPSCounterImpl.java b/src/jogl/classes/jogamp/opengl/FPSCounterImpl.java index 7f8f262e9..61d6d965b 100644 --- a/src/jogl/classes/jogamp/opengl/FPSCounterImpl.java +++ b/src/jogl/classes/jogamp/opengl/FPSCounterImpl.java @@ -30,16 +30,26 @@ package jogamp.opengl; import java.io.PrintStream; import java.util.concurrent.TimeUnit; +import com.jogamp.common.os.Clock; import com.jogamp.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; + + // counter in [ns] + private long fpsStartTimeNS, fpsLastUpdateTimeNS; + + // counter in [ms] + private long fpsLastPeriodMS, fpsTotalDurationMS; + + // counter in events + private int fpsUpdateFramesInterval; private int fpsTotalFrames; + + // counter in fps private float fpsLast, fpsTotal; /** Creates a disabled instance */ @@ -52,25 +62,24 @@ public class FPSCounterImpl implements FPSCounter { * 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 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); - fpsLastPeriod = now - fpsLastUpdateTime; - fpsLastPeriod = Math.max(fpsLastPeriod, 1); // div 0 - fpsLast = ( fpsUpdateFramesInterval * 1000f ) / ( fpsLastPeriod ) ; + final long now = Clock.currentNanos(); + fpsLastPeriodMS = TimeUnit.NANOSECONDS.toMillis(now - fpsLastUpdateTimeNS); + fpsLastPeriodMS = Math.max(fpsLastPeriodMS, 1); // div 0 + fpsLast = ( fpsUpdateFramesInterval * 1000f ) / ( fpsLastPeriodMS ) ; - fpsTotalDuration = now - fpsStartTime; - fpsTotalDuration = Math.max(fpsTotalDuration, 1); // div 0 - fpsTotal= ( fpsTotalFrames * 1000f ) / ( fpsTotalDuration ) ; + fpsTotalDurationMS = TimeUnit.NANOSECONDS.toMillis(now - fpsStartTimeNS); + fpsTotalDurationMS = Math.max(fpsTotalDurationMS, 1); // div 0 + fpsTotal= ( fpsTotalFrames * 1000f ) / ( fpsTotalDurationMS ) ; if(null != fpsOutputStream) { fpsOutputStream.println(toString()); } - fpsLastUpdateTime = now; + fpsLastUpdateTimeNS = now; } } @@ -82,8 +91,8 @@ public class FPSCounterImpl implements FPSCounter { 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"); + sb.append(fpsTotalDurationMS/1000 +" s: "+ fpsUpdateFramesInterval+" f / "+ fpsLastPeriodMS+" ms, " + fpsLastS+" fps, "+ fpsLastPeriodMS/fpsUpdateFramesInterval+" ms/f; "+ + "total: "+ fpsTotalFrames+" f, "+ fpsTotalS+ " fps, "+ fpsTotalDurationMS/fpsTotalFrames+" ms/f"); return sb; } @@ -101,12 +110,12 @@ public class FPSCounterImpl implements FPSCounter { @Override public final synchronized void resetFPSCounter() { - fpsStartTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); // overwrite startTime to real init one - fpsLastUpdateTime = fpsStartTime; - fpsLastPeriod = 0; + fpsStartTimeNS = Clock.currentNanos(); + fpsLastUpdateTimeNS = fpsStartTimeNS; + fpsLastPeriodMS = 0; fpsTotalFrames = 0; fpsLast = 0f; fpsTotal = 0f; - fpsLastPeriod = 0; fpsTotalDuration=0; + fpsLastPeriodMS = 0; fpsTotalDurationMS=0; } @Override @@ -116,17 +125,17 @@ public class FPSCounterImpl implements FPSCounter { @Override public final synchronized long getFPSStartTime() { - return fpsStartTime; + return TimeUnit.NANOSECONDS.toMillis(fpsStartTimeNS); } @Override public final synchronized long getLastFPSUpdateTime() { - return fpsLastUpdateTime; + return TimeUnit.NANOSECONDS.toMillis(fpsLastUpdateTimeNS); } @Override public final synchronized long getLastFPSPeriod() { - return fpsLastPeriod; + return fpsLastPeriodMS; } @Override @@ -141,7 +150,7 @@ public class FPSCounterImpl implements FPSCounter { @Override public final synchronized long getTotalFPSDuration() { - return fpsTotalDuration; + return fpsTotalDurationMS; } @Override diff --git a/src/jogl/classes/jogamp/opengl/GLContextImpl.java b/src/jogl/classes/jogamp/opengl/GLContextImpl.java index d97e0e201..7964416fe 100644 --- a/src/jogl/classes/jogamp/opengl/GLContextImpl.java +++ b/src/jogl/classes/jogamp/opengl/GLContextImpl.java @@ -50,6 +50,7 @@ import java.util.Map; import java.util.Set; import com.jogamp.common.ExceptionUtils; +import com.jogamp.common.os.Clock; import com.jogamp.common.os.DynamicLookupHelper; import com.jogamp.common.os.Platform; import com.jogamp.common.util.Bitfield; @@ -1200,7 +1201,7 @@ public abstract class GLContextImpl extends GLContext { if (DEBUG) { System.err.println(getThreadName() + ": createContextARB-MapGLVersions START (GLDesktop "+hasOpenGLDesktopSupport+", GLES "+hasOpenGLESSupport+", minorVersion "+hasMinorVersionSupport+") on "+device); } - final long t0 = ( DEBUG ) ? System.nanoTime() : 0; + final long t0 = ( DEBUG ) ? Clock.currentNanos() : 0; boolean success = false; // Following GLProfile.GL_PROFILE_LIST_ALL order of profile detection { GL4bc, GL3bc, GL2, GL4, GL3, GL2GL3, GLES2, GL2ES2, GLES1, GL2ES1 } boolean hasGL4bc = false; @@ -1350,7 +1351,7 @@ public abstract class GLContextImpl extends GLContext { GLContext.setAvailableGLVersionsSet(device, true); } if(DEBUG) { - final long t1 = System.nanoTime(); + final long t1 = Clock.currentNanos(); System.err.println(getThreadName() + ": createContextARB-MapGLVersions END (success "+success+") on "+device+", profileAliasing: "+PROFILE_ALIASING+", total "+(t1-t0)/1e6 +"ms"); if( success ) { System.err.println(GLContext.dumpAvailableGLVersions(null).toString()); -- cgit v1.2.3