blob: d4f30efe3a3c74c19a65b556de92f84cf50ed551 [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 *
20 * Choreographer coordinates the timing of frame rendering. This is the C version of the
21 * android.view.Choreographer object in Java.
22 *
23 * As of API level 33, apps can follow proper frame pacing and even choose a future frame to render.
24 * The API is used as follows:
25 * 1. The app posts an {@link AChoreographer_vsyncCallback} to Choreographer to run on the next
26 * frame.
27 * 2. The callback is called when it is the time to start the frame with an {@link
28 * AChoreographerFrameCallbackData} payload: information about multiple possible frame
29 * timelines.
30 * 3. Apps can choose a frame timeline from the {@link
31 * AChoreographerFrameCallbackData} payload, depending on the frame deadline they can meet when
32 * rendering the frame and their desired presentation time, and subsequently
33 * {@link ASurfaceTransaction_setFrameTimeline notify SurfaceFlinger}
34 * of the choice. Alternatively, for apps that do not choose a frame timeline, their frame would be
35 * presented at the earliest possible timeline.
36 * - The preferred frame timeline is the default frame
37 * timeline that the platform scheduled for the app, based on device configuration.
38 * 4. SurfaceFlinger attempts to follow the chosen frame timeline, by not applying transactions or
39 * latching buffers before the desired presentation time.
40 *
Michael Wrightf5eee402015-12-07 15:26:38 -050041 * @{
42 */
43
44/**
45 * @file choreographer.h
46 */
47
48#ifndef ANDROID_CHOREOGRAPHER_H
49#define ANDROID_CHOREOGRAPHER_H
50
Jerome Gaillard3614f182024-02-13 20:34:58 +000051#include <stddef.h>
Santos Cordon908d0082019-02-20 18:08:02 +000052#include <stdint.h>
Michael Wrightfb911b22016-01-26 16:05:54 -080053#include <sys/cdefs.h>
54
Jerome Gaillard3614f182024-02-13 20:34:58 +000055// This file may also be built on glibc or on Windows/MacOS libc's, so no-op
56// and deprecated definitions are provided.
57#if !defined(__INTRODUCED_IN)
58#define __INTRODUCED_IN(__api_level) /* nothing */
59#endif
60#if !defined(__DEPRECATED_IN)
61#define __DEPRECATED_IN(__api_level) __attribute__((__deprecated__))
62#endif
63
Michael Wrightf5eee402015-12-07 15:26:38 -050064__BEGIN_DECLS
65
66struct AChoreographer;
gfan7fed43d2021-04-06 18:50:06 -070067/**
68 * Opaque type that provides access to an AChoreographer object.
69 *
70 * A pointer can be obtained using {@link AChoreographer_getInstance()}.
71 */
Michael Wrightf5eee402015-12-07 15:26:38 -050072typedef struct AChoreographer AChoreographer;
73
Rachel Lee1fb2ddc2022-01-12 14:33:07 -080074
75/**
76 * The identifier of a frame timeline.
77 */
78typedef int64_t AVsyncId;
79
Rachel Lee4879d812021-08-25 11:50:11 -070080struct AChoreographerFrameCallbackData;
81/**
Rachel Leee81bf262022-08-23 14:37:59 -070082 * Opaque type that provides access to an AChoreographerFrameCallbackData object, which contains
83 * various methods to extract frame information.
Rachel Lee4879d812021-08-25 11:50:11 -070084 */
85typedef struct AChoreographerFrameCallbackData AChoreographerFrameCallbackData;
86
Michael Wrightf5eee402015-12-07 15:26:38 -050087/**
88 * Prototype of the function that is called when a new frame is being rendered.
89 * It's passed the time that the frame is being rendered as nanoseconds in the
90 * CLOCK_MONOTONIC time base, as well as the data pointer provided by the
91 * application that registered a callback. All callbacks that run as part of
92 * rendering a frame will observe the same frame time, so it should be used
93 * whenever events need to be synchronized (e.g. animations).
94 */
95typedef void (*AChoreographer_frameCallback)(long frameTimeNanos, void* data);
96
Santos Cordon908d0082019-02-20 18:08:02 +000097/**
98 * Prototype of the function that is called when a new frame is being rendered.
99 * It's passed the time that the frame is being rendered as nanoseconds in the
100 * CLOCK_MONOTONIC time base, as well as the data pointer provided by the
101 * application that registered a callback. All callbacks that run as part of
102 * rendering a frame will observe the same frame time, so it should be used
103 * whenever events need to be synchronized (e.g. animations).
104 */
105typedef void (*AChoreographer_frameCallback64)(int64_t frameTimeNanos, void* data);
106
Alec Mouri33682e92020-01-10 15:11:15 -0800107/**
Rachel Lee4879d812021-08-25 11:50:11 -0700108 * Prototype of the function that is called when a new frame is being rendered.
Rachel Leee81bf262022-08-23 14:37:59 -0700109 * It is called with \c callbackData describing multiple frame timelines, as well as the \c data
110 * pointer provided by the application that registered a callback. The \c callbackData does not
111 * outlive the callback.
Rachel Lee4879d812021-08-25 11:50:11 -0700112 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800113typedef void (*AChoreographer_vsyncCallback)(
Rachel Lee4879d812021-08-25 11:50:11 -0700114 const AChoreographerFrameCallbackData* callbackData, void* data);
115
116/**
Alec Mouri33682e92020-01-10 15:11:15 -0800117 * Prototype of the function that is called when the display refresh rate
118 * changes. It's passed the new vsync period in nanoseconds, as well as the data
119 * pointer provided by the application that registered a callback.
120 */
121typedef void (*AChoreographer_refreshRateCallback)(int64_t vsyncPeriodNanos, void* data);
122
Michael Wrightf5eee402015-12-07 15:26:38 -0500123/**
124 * Get the AChoreographer instance for the current thread. This must be called
125 * on an ALooper thread.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700126 *
127 * Available since API level 24.
Michael Wrightf5eee402015-12-07 15:26:38 -0500128 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700129AChoreographer* AChoreographer_getInstance() __INTRODUCED_IN(24);
Michael Wrightf5eee402015-12-07 15:26:38 -0500130
131/**
Santos Cordon908d0082019-02-20 18:08:02 +0000132 * Deprecated: Use AChoreographer_postFrameCallback64 instead.
Michael Wrightf5eee402015-12-07 15:26:38 -0500133 */
134void AChoreographer_postFrameCallback(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700135 AChoreographer_frameCallback callback, void* data)
136 __INTRODUCED_IN(24) __DEPRECATED_IN(29);
Elliott Hughes9db409b2018-06-18 12:28:46 -0700137
Michael Wrightf5eee402015-12-07 15:26:38 -0500138/**
Santos Cordon908d0082019-02-20 18:08:02 +0000139 * Deprecated: Use AChoreographer_postFrameCallbackDelayed64 instead.
Michael Wrightf5eee402015-12-07 15:26:38 -0500140 */
141void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700142 AChoreographer_frameCallback callback, void* data,
143 long delayMillis) __INTRODUCED_IN(24)
144 __DEPRECATED_IN(29);
Dan Albert494ed552016-09-23 15:57:45 -0700145
Santos Cordon908d0082019-02-20 18:08:02 +0000146/**
Rachel Leee81bf262022-08-23 14:37:59 -0700147 * Post a callback to be run on the next frame. The data pointer provided will
Santos Cordon908d0082019-02-20 18:08:02 +0000148 * be passed to the callback function when it's called.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700149 *
150 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000151 */
Dillon Cower20e67fa2019-07-30 15:39:54 -0700152void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700153 AChoreographer_frameCallback64 callback, void* data)
154 __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000155
156/**
157 * Post a callback to be run on the frame following the specified delay. The
158 * data pointer provided will be passed to the callback function when it's
159 * called.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700160 *
161 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000162 */
163void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700164 AChoreographer_frameCallback64 callback, void* data,
165 uint32_t delayMillis) __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000166
Alec Mouri33682e92020-01-10 15:11:15 -0800167/**
Rachel Leee81bf262022-08-23 14:37:59 -0700168 * Posts a callback to be run on the next frame. The data pointer provided will
Rachel Lee4879d812021-08-25 11:50:11 -0700169 * be passed to the callback function when it's called.
Rachel Leee81bf262022-08-23 14:37:59 -0700170 *
171 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700172 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800173void AChoreographer_postVsyncCallback(AChoreographer* choreographer,
174 AChoreographer_vsyncCallback callback, void* data)
Rachel Lee4879d812021-08-25 11:50:11 -0700175 __INTRODUCED_IN(33);
176
177/**
Alec Mouri33682e92020-01-10 15:11:15 -0800178 * Registers a callback to be run when the display refresh rate changes. The
179 * data pointer provided will be passed to the callback function when it's
180 * called. The same callback may be registered multiple times, provided that a
181 * different data pointer is provided each time.
182 *
183 * If an application registers a callback for this choreographer instance when
184 * no new callbacks were previously registered, that callback is guaranteed to
185 * be dispatched. However, if the callback and associated data pointer are
186 * unregistered prior to running the callback, then the callback may be silently
187 * dropped.
188 *
189 * This api is thread-safe. Any thread is allowed to register a new refresh
190 * rate callback for the choreographer instance.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700191 *
Alec Mouri4a331652020-07-20 22:05:24 +0000192 * Note that in API level 30, this api is not guaranteed to be atomic with
193 * DisplayManager. That is, calling Display#getRefreshRate very soon after
194 * a refresh rate callback is invoked may return a stale refresh rate. If any
195 * Display properties would be required by this callback, then it is recommended
196 * to listen directly to DisplayManager.DisplayListener#onDisplayChanged events
197 * instead.
198 *
Alec Mouri7015fa92021-02-11 19:31:44 +0000199 * As of API level 31, this api is guaranteed to have a consistent view with DisplayManager;
200 * Display#getRefreshRate is guaranteed to not return a stale refresh rate when invoked from this
201 * callback.
202 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700203 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800204 */
205void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700206 AChoreographer_refreshRateCallback, void* data)
207 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800208
209/**
210 * Unregisters a callback to be run when the display refresh rate changes, along
211 * with the data pointer previously provided when registering the callback. The
212 * callback is only unregistered when the data pointer matches one that was
213 * previously registered.
214 *
215 * This api is thread-safe. Any thread is allowed to unregister an existing
216 * refresh rate callback for the choreographer instance. When a refresh rate
217 * callback and associated data pointer are unregistered, then there is a
218 * guarantee that when the unregistration completes that that callback will not
219 * be run with the data pointer passed.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700220 *
221 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800222 */
223void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700224 AChoreographer_refreshRateCallback, void* data)
225 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800226
Rachel Lee4879d812021-08-25 11:50:11 -0700227/**
Rachel Leee81bf262022-08-23 14:37:59 -0700228 * The time in nanoseconds at which the frame started being rendered.
229 *
230 * Note that this time should \b not be used to advance animation clocks.
231 * Instead, see AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos().
Alec Mouri5a100fa2023-02-24 00:45:29 +0000232 *
233 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700234 */
235int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
236 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
237
238/**
239 * The number of possible frame timelines.
Alec Mouri5a100fa2023-02-24 00:45:29 +0000240 *
241 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700242 */
243size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
244 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
245
246/**
Rachel Leee81bf262022-08-23 14:37:59 -0700247 * Gets the index of the platform-preferred frame timeline.
248 * The preferred frame timeline is the default
249 * by which the platform scheduled the app, based on the device configuration.
Alec Mouri5a100fa2023-02-24 00:45:29 +0000250 *
251 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700252 */
253size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
254 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
255
256/**
Rachel Leee81bf262022-08-23 14:37:59 -0700257 * Gets the token used by the platform to identify the frame timeline at the given \c index.
Alec Mouri5a100fa2023-02-24 00:45:29 +0000258 * q
259 * Available since API level 33.
Rachel Leee81bf262022-08-23 14:37:59 -0700260 *
261 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
262 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Alec Mouri5a100fa2023-02-24 00:45:29 +0000263 *
Rachel Lee4879d812021-08-25 11:50:11 -0700264 */
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800265AVsyncId AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700266 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
267
268/**
Rachel Leee81bf262022-08-23 14:37:59 -0700269 * Gets the time in nanoseconds at which the frame described at the given \c index is expected to
270 * be presented. This time should be used to advance any animation clocks.
271 *
Alec Mouri5a100fa2023-02-24 00:45:29 +0000272 * Available since API level 33.
273 *
Rachel Leee81bf262022-08-23 14:37:59 -0700274 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
275 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Rachel Lee4879d812021-08-25 11:50:11 -0700276 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800277int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700278 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
279
280/**
Rachel Leee81bf262022-08-23 14:37:59 -0700281 * Gets the time in nanoseconds at which the frame described at the given \c index needs to be
282 * ready by in order to be presented on time.
283 *
Alec Mouri5a100fa2023-02-24 00:45:29 +0000284 * Available since API level 33.
285 *
Rachel Leee81bf262022-08-23 14:37:59 -0700286 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
287 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Rachel Lee4879d812021-08-25 11:50:11 -0700288 */
Rachel Lee2825fa22022-01-12 17:35:16 -0800289int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700290 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
291
Michael Wrightf5eee402015-12-07 15:26:38 -0500292__END_DECLS
293
294#endif // ANDROID_CHOREOGRAPHER_H
295
296/** @} */