Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use Choreographer.postFrameCallback vs. Choreographer.postVsyncCallback?

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:

  1. What are the key differences between postFrameCallback and postVsyncCallback?
  2. Are there specific use cases where one is preferable over the other?
  3. If I'm performing animations or drawing operations, which method should I use for optimal performance?

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?

like image 822
zeus Avatar asked Dec 02 '25 18:12

zeus


1 Answers

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.

like image 195
dev.bmax Avatar answered Dec 04 '25 09:12

dev.bmax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!