blob: 6a91d9836e02171a410e637830660978913bb71c [file] [log] [blame]
Michael Wrightf5eee402015-12-07 15:26:38 -05001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @addtogroup Choreographer
Rachel Leee81bf262022-08-23 14:37:59 -070019 *
Dan Albertdc9c4112024-05-02 21:35:02 +000020 * Choreographer coordinates the timing of frame rendering. This is the C
21 * version of the android.view.Choreographer object in Java. If you do not use
22 * Choreographer to pace your render loop, you may render too quickly for the
23 * display, increasing latency between frame submission and presentation.
24 *
25 * Input events are guaranteed to be processed before the frame callback is
26 * called, and will not be run concurrently. Input and sensor events should not
27 * be handled in the Choregrapher callback.
28 *
29 * The frame callback is also the appropriate place to run any per-frame state
30 * update logic. For example, in a game, the frame callback should be
31 * responsible for updating things like physics, AI, game state, and rendering
32 * the frame. Input and sensors should be handled separately via callbacks
33 * registered with AInputQueue and ASensorManager.
Rachel Leee81bf262022-08-23 14:37:59 -070034 *
35 * As of API level 33, apps can follow proper frame pacing and even choose a future frame to render.
36 * The API is used as follows:
37 * 1. The app posts an {@link AChoreographer_vsyncCallback} to Choreographer to run on the next
38 * frame.
39 * 2. The callback is called when it is the time to start the frame with an {@link
40 * AChoreographerFrameCallbackData} payload: information about multiple possible frame
41 * timelines.
42 * 3. Apps can choose a frame timeline from the {@link
43 * AChoreographerFrameCallbackData} payload, depending on the frame deadline they can meet when
44 * rendering the frame and their desired presentation time, and subsequently
45 * {@link ASurfaceTransaction_setFrameTimeline notify SurfaceFlinger}
46 * of the choice. Alternatively, for apps that do not choose a frame timeline, their frame would be
47 * presented at the earliest possible timeline.
48 * - The preferred frame timeline is the default frame
49 * timeline that the platform scheduled for the app, based on device configuration.
50 * 4. SurfaceFlinger attempts to follow the chosen frame timeline, by not applying transactions or
51 * latching buffers before the desired presentation time.
52 *
Dan Albertdc9c4112024-05-02 21:35:02 +000053 * On older devices, AChoreographer_postFrameCallback64 or
54 * AChoreographer_postFrameCallback can be used to lesser effect. They cannot be
55 * used to precisely plan your render timeline, but will rate limit to avoid
56 * overloading the display pipeline and increasing frame latency.
57 *
Michael Wrightf5eee402015-12-07 15:26:38 -050058 * @{
59 */
60
61/**
62 * @file choreographer.h
63 */
64
65#ifndef ANDROID_CHOREOGRAPHER_H
66#define ANDROID_CHOREOGRAPHER_H
67
Santos Cordon908d0082019-02-20 18:08:02 +000068#include <stdint.h>
Michael Wrightfb911b22016-01-26 16:05:54 -080069#include <sys/cdefs.h>
70
Michael Wrightf5eee402015-12-07 15:26:38 -050071__BEGIN_DECLS
72
73struct AChoreographer;
gfan7fed43d2021-04-06 18:50:06 -070074/**
75 * Opaque type that provides access to an AChoreographer object.
76 *
77 * A pointer can be obtained using {@link AChoreographer_getInstance()}.
78 */
Michael Wrightf5eee402015-12-07 15:26:38 -050079typedef struct AChoreographer AChoreographer;
80
Rachel Lee1fb2ddc2022-01-12 14:33:07 -080081
82/**
83 * The identifier of a frame timeline.
84 */
85typedef int64_t AVsyncId;
86
Rachel Lee4879d812021-08-25 11:50:11 -070087struct AChoreographerFrameCallbackData;
88/**
Rachel Leee81bf262022-08-23 14:37:59 -070089 * Opaque type that provides access to an AChoreographerFrameCallbackData object, which contains
90 * various methods to extract frame information.
Rachel Lee4879d812021-08-25 11:50:11 -070091 */
92typedef struct AChoreographerFrameCallbackData AChoreographerFrameCallbackData;
93
Michael Wrightf5eee402015-12-07 15:26:38 -050094/**
95 * Prototype of the function that is called when a new frame is being rendered.
96 * It's passed the time that the frame is being rendered as nanoseconds in the
97 * CLOCK_MONOTONIC time base, as well as the data pointer provided by the
98 * application that registered a callback. All callbacks that run as part of
99 * rendering a frame will observe the same frame time, so it should be used
100 * whenever events need to be synchronized (e.g. animations).
101 */
102typedef void (*AChoreographer_frameCallback)(long frameTimeNanos, void* data);
103
Santos Cordon908d0082019-02-20 18:08:02 +0000104/**
105 * Prototype of the function that is called when a new frame is being rendered.
106 * It's passed the time that the frame is being rendered as nanoseconds in the
107 * CLOCK_MONOTONIC time base, as well as the data pointer provided by the
108 * application that registered a callback. All callbacks that run as part of
109 * rendering a frame will observe the same frame time, so it should be used
110 * whenever events need to be synchronized (e.g. animations).
111 */
112typedef void (*AChoreographer_frameCallback64)(int64_t frameTimeNanos, void* data);
113
Alec Mouri33682e92020-01-10 15:11:15 -0800114/**
Rachel Lee4879d812021-08-25 11:50:11 -0700115 * Prototype of the function that is called when a new frame is being rendered.
Rachel Leee81bf262022-08-23 14:37:59 -0700116 * It is called with \c callbackData describing multiple frame timelines, as well as the \c data
117 * pointer provided by the application that registered a callback. The \c callbackData does not
118 * outlive the callback.
Rachel Lee4879d812021-08-25 11:50:11 -0700119 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800120typedef void (*AChoreographer_vsyncCallback)(
Rachel Lee4879d812021-08-25 11:50:11 -0700121 const AChoreographerFrameCallbackData* callbackData, void* data);
122
123/**
Alec Mouri33682e92020-01-10 15:11:15 -0800124 * Prototype of the function that is called when the display refresh rate
125 * changes. It's passed the new vsync period in nanoseconds, as well as the data
126 * pointer provided by the application that registered a callback.
127 */
128typedef void (*AChoreographer_refreshRateCallback)(int64_t vsyncPeriodNanos, void* data);
129
Michael Wrightf5eee402015-12-07 15:26:38 -0500130/**
131 * Get the AChoreographer instance for the current thread. This must be called
132 * on an ALooper thread.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700133 *
134 * Available since API level 24.
Michael Wrightf5eee402015-12-07 15:26:38 -0500135 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700136AChoreographer* AChoreographer_getInstance() __INTRODUCED_IN(24);
Michael Wrightf5eee402015-12-07 15:26:38 -0500137
138/**
Dan Albertdc9c4112024-05-02 21:35:02 +0000139 * Post a callback to be run when the application should begin rendering the
140 * next frame. The data pointer provided will be passed to the callback function
141 * when it's called.
142 *
143 * The callback will only be run for the next frame, not all subsequent frames,
144 * so to render continuously the callback should itself call
145 * AChoreographer_postFrameCallback.
Dan Albert9ca6b3d2024-05-02 20:36:06 +0000146 *
147 * \bug The callback receives the frame time in nanoseconds as a long. On 32-bit
148 * systems, long is 32-bit, so the frame time will roll over roughly every two
149 * seconds. If your minSdkVersion is 29 or higher, switch to
150 * AChoreographer_postFrameCallback64, which uses a 64-bit frame time for all
151 * platforms. For older OS versions, you must combine the argument with the
152 * upper bits of clock_gettime(CLOCK_MONOTONIC, ...) on 32-bit systems.
153 *
154 * \deprecated Use AChoreographer_postFrameCallback64, which does not have the
155 * bug described above.
Michael Wrightf5eee402015-12-07 15:26:38 -0500156 */
157void AChoreographer_postFrameCallback(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700158 AChoreographer_frameCallback callback, void* data)
Dan Albertc47ac842024-05-02 20:19:54 +0000159 __INTRODUCED_IN(24) __DEPRECATED_IN(29, "Use AChoreographer_postFrameCallback64 instead");
Elliott Hughes9db409b2018-06-18 12:28:46 -0700160
Michael Wrightf5eee402015-12-07 15:26:38 -0500161/**
Dan Albertdc9c4112024-05-02 21:35:02 +0000162 * Post a callback to be run when the application should begin rendering the
163 * next frame following the specified delay. The data pointer provided will be
164 * passed to the callback function when it's called.
165 *
166 * The callback will only be run for the next frame after the delay, not all
167 * subsequent frames, so to render continuously the callback should itself call
168 * AChoreographer_postFrameCallbackDelayed.
Dan Albert9ca6b3d2024-05-02 20:36:06 +0000169 *
170 * \bug The callback receives the frame time in nanoseconds as a long. On 32-bit
171 * systems, long is 32-bit, so the frame time will roll over roughly every two
172 * seconds. If your minSdkVersion is 29 or higher, switch to
173 * AChoreographer_postFrameCallbackDelayed64, which uses a 64-bit frame time for
174 * all platforms. For older OS versions, you must combine the argument with the
175 * upper bits of clock_gettime(CLOCK_MONOTONIC, ...) on 32-bit systems.
176 *
177 * \deprecated Use AChoreographer_postFrameCallbackDelayed64, which does not
178 * have the bug described above.
Michael Wrightf5eee402015-12-07 15:26:38 -0500179 */
180void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700181 AChoreographer_frameCallback callback, void* data,
182 long delayMillis) __INTRODUCED_IN(24)
Dan Albertc47ac842024-05-02 20:19:54 +0000183 __DEPRECATED_IN(29, "Use AChoreographer_postFrameCallbackDelayed64 instead");
Dan Albert494ed552016-09-23 15:57:45 -0700184
Santos Cordon908d0082019-02-20 18:08:02 +0000185/**
Dan Albertdc9c4112024-05-02 21:35:02 +0000186 * Post a callback to be run when the application should begin rendering the
187 * next frame. The data pointer provided will be passed to the callback function
188 * when it's called.
189 *
190 * The callback will only be run on the next frame, not all subsequent frames,
191 * so to render continuously the callback should itself call
192 * AChoreographer_postFrameCallback64.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700193 *
194 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000195 */
Dillon Cower20e67fa2019-07-30 15:39:54 -0700196void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700197 AChoreographer_frameCallback64 callback, void* data)
198 __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000199
200/**
Dan Albertdc9c4112024-05-02 21:35:02 +0000201 * Post a callback to be run when the application should begin rendering the
202 * next frame following the specified delay. The data pointer provided will be
203 * passed to the callback function when it's called.
204 *
205 * The callback will only be run for the next frame after the delay, not all
206 * subsequent frames, so to render continuously the callback should itself call
207 * AChoreographer_postFrameCallbackDelayed64.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700208 *
209 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000210 */
211void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700212 AChoreographer_frameCallback64 callback, void* data,
213 uint32_t delayMillis) __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000214
Alec Mouri33682e92020-01-10 15:11:15 -0800215/**
Dan Albertdc9c4112024-05-02 21:35:02 +0000216 * Posts a callback to be run when the application should begin rendering the
217 * next frame. The data pointer provided will be passed to the callback function
218 * when it's called.
219 *
220 * The callback will only be run for the next frame, not all subsequent frames,
221 * so to render continuously the callback should itself call
222 * AChoreographer_postVsyncCallback.
Rachel Leee81bf262022-08-23 14:37:59 -0700223 *
224 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700225 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800226void AChoreographer_postVsyncCallback(AChoreographer* choreographer,
227 AChoreographer_vsyncCallback callback, void* data)
Rachel Lee4879d812021-08-25 11:50:11 -0700228 __INTRODUCED_IN(33);
229
230/**
Alec Mouri33682e92020-01-10 15:11:15 -0800231 * Registers a callback to be run when the display refresh rate changes. The
232 * data pointer provided will be passed to the callback function when it's
233 * called. The same callback may be registered multiple times, provided that a
234 * different data pointer is provided each time.
235 *
236 * If an application registers a callback for this choreographer instance when
237 * no new callbacks were previously registered, that callback is guaranteed to
238 * be dispatched. However, if the callback and associated data pointer are
239 * unregistered prior to running the callback, then the callback may be silently
240 * dropped.
241 *
242 * This api is thread-safe. Any thread is allowed to register a new refresh
243 * rate callback for the choreographer instance.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700244 *
Alec Mouri4a331652020-07-20 22:05:24 +0000245 * Note that in API level 30, this api is not guaranteed to be atomic with
246 * DisplayManager. That is, calling Display#getRefreshRate very soon after
247 * a refresh rate callback is invoked may return a stale refresh rate. If any
248 * Display properties would be required by this callback, then it is recommended
249 * to listen directly to DisplayManager.DisplayListener#onDisplayChanged events
250 * instead.
251 *
Alec Mouri7015fa92021-02-11 19:31:44 +0000252 * As of API level 31, this api is guaranteed to have a consistent view with DisplayManager;
253 * Display#getRefreshRate is guaranteed to not return a stale refresh rate when invoked from this
254 * callback.
255 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700256 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800257 */
258void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700259 AChoreographer_refreshRateCallback, void* data)
260 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800261
262/**
263 * Unregisters a callback to be run when the display refresh rate changes, along
264 * with the data pointer previously provided when registering the callback. The
265 * callback is only unregistered when the data pointer matches one that was
266 * previously registered.
267 *
268 * This api is thread-safe. Any thread is allowed to unregister an existing
269 * refresh rate callback for the choreographer instance. When a refresh rate
270 * callback and associated data pointer are unregistered, then there is a
271 * guarantee that when the unregistration completes that that callback will not
272 * be run with the data pointer passed.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700273 *
274 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800275 */
276void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700277 AChoreographer_refreshRateCallback, void* data)
278 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800279
Rachel Lee4879d812021-08-25 11:50:11 -0700280/**
Rachel Leee81bf262022-08-23 14:37:59 -0700281 * The time in nanoseconds at which the frame started being rendered.
282 *
283 * Note that this time should \b not be used to advance animation clocks.
284 * Instead, see AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos().
Alec Mouri5a100fa2023-02-24 00:45:29 +0000285 *
286 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700287 */
288int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
289 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
290
291/**
292 * The number of possible frame timelines.
Alec Mouri5a100fa2023-02-24 00:45:29 +0000293 *
294 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700295 */
296size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
297 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
298
299/**
Rachel Leee81bf262022-08-23 14:37:59 -0700300 * Gets the index of the platform-preferred frame timeline.
301 * The preferred frame timeline is the default
302 * by which the platform scheduled the app, based on the device configuration.
Alec Mouri5a100fa2023-02-24 00:45:29 +0000303 *
304 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700305 */
306size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
307 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
308
309/**
Rachel Leee81bf262022-08-23 14:37:59 -0700310 * Gets the token used by the platform to identify the frame timeline at the given \c index.
Alec Mouri5a100fa2023-02-24 00:45:29 +0000311 * q
312 * Available since API level 33.
Rachel Leee81bf262022-08-23 14:37:59 -0700313 *
314 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
315 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Alec Mouri5a100fa2023-02-24 00:45:29 +0000316 *
Rachel Lee4879d812021-08-25 11:50:11 -0700317 */
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800318AVsyncId AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700319 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
320
321/**
Rachel Leee81bf262022-08-23 14:37:59 -0700322 * Gets the time in nanoseconds at which the frame described at the given \c index is expected to
323 * be presented. This time should be used to advance any animation clocks.
324 *
Alec Mouri5a100fa2023-02-24 00:45:29 +0000325 * Available since API level 33.
326 *
Rachel Leee81bf262022-08-23 14:37:59 -0700327 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
328 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Rachel Lee4879d812021-08-25 11:50:11 -0700329 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800330int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700331 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
332
333/**
Rachel Leee81bf262022-08-23 14:37:59 -0700334 * Gets the time in nanoseconds at which the frame described at the given \c index needs to be
335 * ready by in order to be presented on time.
336 *
Alec Mouri5a100fa2023-02-24 00:45:29 +0000337 * Available since API level 33.
338 *
Rachel Leee81bf262022-08-23 14:37:59 -0700339 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
340 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Rachel Lee4879d812021-08-25 11:50:11 -0700341 */
Rachel Lee2825fa22022-01-12 17:35:16 -0800342int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700343 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
344
Michael Wrightf5eee402015-12-07 15:26:38 -0500345__END_DECLS
346
347#endif // ANDROID_CHOREOGRAPHER_H
348
349/** @} */