I'm working on optimizing frame rendering and UI updates in an Android application, and I'm trying to understand the difference between Choreographer.postFrameCallback and Choreographer.postVsyncCallback.
From my understanding:
Choreographer.postFrameCallback(Runnable r): Schedules a callback to be executed on the next frame.
Choreographer.postVsyncCallback(Runnable r): Appears to be related to syncing with vsync, but I'm not sure how it differs in behavior from postFrameCallback.
My questions are:
postFrameCallback and postVsyncCallback?Any clarification on when to use each method would be greatly appreciated!
NOTE:
I am trying to set up postFrameCallback for timing my animation and postVsyncCallback for handling the painting process of my OpenGL surface (SurfaceView). I was expecting the event for postFrameCallback to be called slightly earlier than the callback for postVsyncCallback (doFrame), but that’s not the case. Instead, the postVsyncCallback callback (onVSync) is always triggered before the event of postFrameCallback 😞.
My goal is to ensure that the events for my animations are fired just before the drawing process of my SurfaceView, so the rendering remains accurate. How can I achieve this?
The Choreographer.FrameCallback exists since API level 16 (Android 4.1).
It tells you the time when the frame started being rendered - frameTimeNanos.
The Choreographer.VsyncCallback has been added much later in API level 33 (Android 13). So it won't be available on older devices. Where it is available, it provides more information. In addition to the frame time nanos, Choreographer.FrameData gives you an array of possible frame timelines.
Each timeline object contains a vsync ID, the deadline and the expected presentation time. Based on this information you can decide how much work to do in the current frame. For example, you can simplify the rendering if the deadline is too close.
Besides the possibility of on-demand optimization, both callbacks should provide the same level of performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With