Cocos2dxRenderer FPS controlling

Hi,
I’ve been testing my game on several Android devices, and found that it work smooth for most of them,
except one device, that seems to be having jittery FPS. Although the FPS is about ~58, my scrolling objects seems to jitter a lot.
The things is, this device is by no means a low end device, I’m using the Nexus 7 that has quad core cpu, and strong GPU.
On the Galaxy Nexus my game runs smoothly.

I’ve found the following code:

 public void onDrawFrame(GL10 gl) {

        long now = System.nanoTime();
        long interval = now - last;

        // should render a frame when onDrawFrame() is called
        // or there is a "ghost"
        nativeRender();     

        // fps controlling
        if (interval < animationInterval){ 
            try {
                // because we render it before, so we should sleep twice time interval
                Thread.sleep((animationInterval - interval) * 2 / NANOSECONDSPERMINISECOND);
            } catch (Exception e){}
        }   

        last = now;
    }

And it seems there is a conditional sleep between rendering frames.
The first thing I did was commenting out the Sleep, and it worked! The game felt very smooth on that device.
The second thing I’ve done, was to add the sleep, but now, without the x2 on the interval, and again, the game felt smooth.
Adding the original line, made it jitter again.

So, my question is, why to multiply it by 2?

And, why am I seeing different behavior on this high end device?

Any thoughts?

Removing the * 2 worked for me as well. I was seeing a weird jitter on my Galaxy S3 which is now gone.

Also added “APP_ABI := armeabi armeabi-v7a” to my Application.mk, not sure if that affects performance.

1 Like

armeabi is for old devices CPUs so I think the performance change was due to the sleep :slight_smile:

Thanks for posting this! It definitely improved the frame-rate stuttering on my Galaxy SII T989.

Hi,

Thanks for sharing! Saved about 1000 hours of man work from us :slight_smile:

Had exactly the same problems, worked with Galaxy Nexus, but had problems with Nexus 7. We are using box2d and the update function was called irregularly and the fps was altering a lot => finally causing problems because box2d update loop. The Nexus 7 has tegra gpu and chipset, which makes some difference in the HW level at least.

I had the same issue on my HTC One X (again a high end device).

I suspect that the condition check on interval < animationInterval happens a lot less on low end devices so the sleep is not called.

I changed it to the below and have less issues (I left the * 1 in so I remember where to change it):-
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) * 1 / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);

I still can see the frame rate drop from 59 to ~30 when nothing is happening.

What should this line be and do we even need it?

This is an answer I got from Romain Guy (Android framework engineer):

Window composition is vsynced and drawing is therefore capped by the display's refresh rate. On some devices it's 60 Hz, on others it can be slightly lower (Nexus S is 57 if I remember correctly.)

Anyway, if you use OpenGL no need to sleep, your eglSwapBuffers will block. You are automatically synced to the display's refresh rate.

So if we are targeting 60FPS, we don’t need any sleep in that code.

This function is used for controlling fps. But i forgot why x2. May be we should remove the codes.

I am having a similar issue running on blackberry z10 I am using momentics ide but I made the changes and still there is no difference. I am a blackberry noob so would this work at all on it?

@zhangxm I still have this issue. I use Samsung Galaxy S4 with Android 4.4.2 and draw nothing but display stats using cocos2d-x 3.2. Why we have this much strong jitter on Android?

I am still having this issue with 3.6. I could not find the line in the source above so I’m guessing it has been removed since then. Is there a way to fix this with the latest source. I have noticeable drops when nothing is on the screen and its really annoying. I am also using a Galaxy S3.

I am using cocos2d-x 3.7.1, had jitter (game is slow) on devices (Moto x play and Mototrola Nexus 6). I don’t have any multiplier by 2 in my cocos2dxrenderer.java file, Any solution?