Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Fix fps issue when certain animation intervals are set #2162

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class AxmolRenderer implements GLSurfaceView.Renderer {

// The final animation interval which is used in 'onDrawFrame'
private static long sAnimationInterval = (long) (1.0f / 60f * AxmolRenderer.NANOSECONDSPERSECOND);
private static float FPS_CONTROL_THRESHOLD = 1.0f / 1200.0f * AxmolRenderer.NANOSECONDSPERSECOND;

// ===========================================================
// Fields
Expand Down Expand Up @@ -91,26 +92,24 @@ public void onSurfaceChanged(final GL10 GL10, final int width, final int height)
@Override
public void onDrawFrame(final GL10 gl) {
/*
* Fix 60fps limiting doesn't work when high-end device is working in 120fps mode.
* Render time MUST be counted in, or the FPS will slower than appointed.
*/
if (AxmolRenderer.sAnimationInterval <= 1.0f / 1200.0f * AxmolRenderer.NANOSECONDSPERSECOND) {
AxmolRenderer.nativeRender();
} else {
final long now = System.nanoTime();
final long interval = now - this.mLastTickInNanoSeconds;

/*
* Render time MUST be counted in, or the FPS will slower than appointed.
*/
this.mLastTickInNanoSeconds = now;
AxmolRenderer.nativeRender();
AxmolRenderer.nativeRender();
/*
* No need to use algorithm in default(60,90,120... FPS) situation,
* since onDrawFrame() was called by system 60 times per second by default.
*/
if (AxmolRenderer.sAnimationInterval > AxmolRenderer.FPS_CONTROL_THRESHOLD) {
final long interval = System.nanoTime() - this.mLastTickInNanoSeconds;

if (interval < AxmolRenderer.sAnimationInterval) {
try {
Thread.sleep((AxmolRenderer.sAnimationInterval - interval) / AxmolRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}

this.mLastTickInNanoSeconds = System.nanoTime();
}
}

Expand Down