I have a game using GLSurfaceView.Renderer
in the usual manner to render 3D graphics with OpenGL ES 2.0.
All the rendering code is within the onDrawFrame()
method, and when timed takes around 24ms to complete; this would easily give a 30 FPS result.
However I am finding the the onDrawFrame()
call itself is often being called only every 50 to 100ms, so that the total time of onDrawFrame()
is actually 70 to 120 ms, giving at best a 14 FPS result.
Why would android be taking so long to invoke onDrawFrame()
? Reading up on this on stackoverflow and other places, I gather onDrawFrame()
"locks" until the GPU has rendered the frame, and so I wonder if it could it be that my GPU rendering is taking a very long time, rather than something specific in Android causing the delay?
Yes, based on your description, it's very likely that the rendering on the GPU is limiting your frame rate.
Picture your OpenGL calls submitting work to the GPU. After all the code in your onDrawFrame()
call has completed, this means that all your rendering has been submitted to the GPU. But it does not mean that the GPU has completed the work. That can take an arbitrarily long time, depending on the amount and complexity of the work you submitted.
In Android, the system will attempt to redraw at the display refresh rate, which is typically 60 Hz. But if the GPU can't produce 60 frames per second because your rendering is too complex, it does not make sense to still invoke your onDrawFrame()
method 60 times per second. This would result in more and more GPU work being queued up, resulting in an increasing amount of system resources being used for pending GPU commands, and in increased latency between your code making the rendering calls and the result showing up on the display.
To avoid this undesirable behavior, the system will throttle the calls to onDrawFrame()
to a point where the GPU can keep up with rendering the frames you produce. It might let you get 1 or 2 frames ahead of what the GPU already completed, but will then block until the GPU has completed enough work before asking you to draw another frame.