blob: cd8e63dec2eb1c351cce8efb9393ec534ae163a7 [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
Santos Cordon908d0082019-02-20 18:08:02 +000051#include <stdint.h>
Michael Wrightfb911b22016-01-26 16:05:54 -080052#include <sys/cdefs.h>
53
Michael Wrightf5eee402015-12-07 15:26:38 -050054__BEGIN_DECLS
55
56struct AChoreographer;
gfan7fed43d2021-04-06 18:50:06 -070057/**
58 * Opaque type that provides access to an AChoreographer object.
59 *
60 * A pointer can be obtained using {@link AChoreographer_getInstance()}.
61 */
Michael Wrightf5eee402015-12-07 15:26:38 -050062typedef struct AChoreographer AChoreographer;
63
Rachel Lee1fb2ddc2022-01-12 14:33:07 -080064
65/**
66 * The identifier of a frame timeline.
67 */
68typedef int64_t AVsyncId;
69
Rachel Lee4879d812021-08-25 11:50:11 -070070struct AChoreographerFrameCallbackData;
71/**
Rachel Leee81bf262022-08-23 14:37:59 -070072 * Opaque type that provides access to an AChoreographerFrameCallbackData object, which contains
73 * various methods to extract frame information.
Rachel Lee4879d812021-08-25 11:50:11 -070074 */
75typedef struct AChoreographerFrameCallbackData AChoreographerFrameCallbackData;
76
Michael Wrightf5eee402015-12-07 15:26:38 -050077/**
78 * Prototype of the function that is called when a new frame is being rendered.
79 * It's passed the time that the frame is being rendered as nanoseconds in the
80 * CLOCK_MONOTONIC time base, as well as the data pointer provided by the
81 * application that registered a callback. All callbacks that run as part of
82 * rendering a frame will observe the same frame time, so it should be used
83 * whenever events need to be synchronized (e.g. animations).
84 */
85typedef void (*AChoreographer_frameCallback)(long frameTimeNanos, void* data);
86
Santos Cordon908d0082019-02-20 18:08:02 +000087/**
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_frameCallback64)(int64_t frameTimeNanos, void* data);
96
Alec Mouri33682e92020-01-10 15:11:15 -080097/**
Rachel Lee4879d812021-08-25 11:50:11 -070098 * Prototype of the function that is called when a new frame is being rendered.
Rachel Leee81bf262022-08-23 14:37:59 -070099 * It is called with \c callbackData describing multiple frame timelines, as well as the \c data
100 * pointer provided by the application that registered a callback. The \c callbackData does not
101 * outlive the callback.
Rachel Lee4879d812021-08-25 11:50:11 -0700102 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800103typedef void (*AChoreographer_vsyncCallback)(
Rachel Lee4879d812021-08-25 11:50:11 -0700104 const AChoreographerFrameCallbackData* callbackData, void* data);
105
106/**
Alec Mouri33682e92020-01-10 15:11:15 -0800107 * Prototype of the function that is called when the display refresh rate
108 * changes. It's passed the new vsync period in nanoseconds, as well as the data
109 * pointer provided by the application that registered a callback.
110 */
111typedef void (*AChoreographer_refreshRateCallback)(int64_t vsyncPeriodNanos, void* data);
112
Michael Wrightf5eee402015-12-07 15:26:38 -0500113/**
114 * Get the AChoreographer instance for the current thread. This must be called
115 * on an ALooper thread.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700116 *
117 * Available since API level 24.
Michael Wrightf5eee402015-12-07 15:26:38 -0500118 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700119AChoreographer* AChoreographer_getInstance() __INTRODUCED_IN(24);
Michael Wrightf5eee402015-12-07 15:26:38 -0500120
121/**
Santos Cordon908d0082019-02-20 18:08:02 +0000122 * Deprecated: Use AChoreographer_postFrameCallback64 instead.
Michael Wrightf5eee402015-12-07 15:26:38 -0500123 */
124void AChoreographer_postFrameCallback(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700125 AChoreographer_frameCallback callback, void* data)
126 __INTRODUCED_IN(24) __DEPRECATED_IN(29);
Elliott Hughes9db409b2018-06-18 12:28:46 -0700127
Michael Wrightf5eee402015-12-07 15:26:38 -0500128/**
Santos Cordon908d0082019-02-20 18:08:02 +0000129 * Deprecated: Use AChoreographer_postFrameCallbackDelayed64 instead.
Michael Wrightf5eee402015-12-07 15:26:38 -0500130 */
131void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700132 AChoreographer_frameCallback callback, void* data,
133 long delayMillis) __INTRODUCED_IN(24)
134 __DEPRECATED_IN(29);
Dan Albert494ed552016-09-23 15:57:45 -0700135
Santos Cordon908d0082019-02-20 18:08:02 +0000136/**
Rachel Leee81bf262022-08-23 14:37:59 -0700137 * Post a callback to be run on the next frame. The data pointer provided will
Santos Cordon908d0082019-02-20 18:08:02 +0000138 * be passed to the callback function when it's called.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700139 *
140 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000141 */
Dillon Cower20e67fa2019-07-30 15:39:54 -0700142void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700143 AChoreographer_frameCallback64 callback, void* data)
144 __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000145
146/**
147 * Post a callback to be run on the frame following the specified delay. The
148 * data pointer provided will be passed to the callback function when it's
149 * called.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700150 *
151 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000152 */
153void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700154 AChoreographer_frameCallback64 callback, void* data,
155 uint32_t delayMillis) __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000156
Alec Mouri33682e92020-01-10 15:11:15 -0800157/**
Rachel Leee81bf262022-08-23 14:37:59 -0700158 * Posts a callback to be run on the next frame. The data pointer provided will
Rachel Lee4879d812021-08-25 11:50:11 -0700159 * be passed to the callback function when it's called.
Rachel Leee81bf262022-08-23 14:37:59 -0700160 *
161 * Available since API level 33.
Rachel Lee4879d812021-08-25 11:50:11 -0700162 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800163void AChoreographer_postVsyncCallback(AChoreographer* choreographer,
164 AChoreographer_vsyncCallback callback, void* data)
Rachel Lee4879d812021-08-25 11:50:11 -0700165 __INTRODUCED_IN(33);
166
167/**
Alec Mouri33682e92020-01-10 15:11:15 -0800168 * Registers a callback to be run when the display refresh rate changes. The
169 * data pointer provided will be passed to the callback function when it's
170 * called. The same callback may be registered multiple times, provided that a
171 * different data pointer is provided each time.
172 *
173 * If an application registers a callback for this choreographer instance when
174 * no new callbacks were previously registered, that callback is guaranteed to
175 * be dispatched. However, if the callback and associated data pointer are
176 * unregistered prior to running the callback, then the callback may be silently
177 * dropped.
178 *
179 * This api is thread-safe. Any thread is allowed to register a new refresh
180 * rate callback for the choreographer instance.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700181 *
Alec Mouri4a331652020-07-20 22:05:24 +0000182 * Note that in API level 30, this api is not guaranteed to be atomic with
183 * DisplayManager. That is, calling Display#getRefreshRate very soon after
184 * a refresh rate callback is invoked may return a stale refresh rate. If any
185 * Display properties would be required by this callback, then it is recommended
186 * to listen directly to DisplayManager.DisplayListener#onDisplayChanged events
187 * instead.
188 *
Alec Mouri7015fa92021-02-11 19:31:44 +0000189 * As of API level 31, this api is guaranteed to have a consistent view with DisplayManager;
190 * Display#getRefreshRate is guaranteed to not return a stale refresh rate when invoked from this
191 * callback.
192 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700193 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800194 */
195void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700196 AChoreographer_refreshRateCallback, void* data)
197 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800198
199/**
200 * Unregisters a callback to be run when the display refresh rate changes, along
201 * with the data pointer previously provided when registering the callback. The
202 * callback is only unregistered when the data pointer matches one that was
203 * previously registered.
204 *
205 * This api is thread-safe. Any thread is allowed to unregister an existing
206 * refresh rate callback for the choreographer instance. When a refresh rate
207 * callback and associated data pointer are unregistered, then there is a
208 * guarantee that when the unregistration completes that that callback will not
209 * be run with the data pointer passed.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700210 *
211 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800212 */
213void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700214 AChoreographer_refreshRateCallback, void* data)
215 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800216
Rachel Lee4879d812021-08-25 11:50:11 -0700217/**
Rachel Leee81bf262022-08-23 14:37:59 -0700218 * The time in nanoseconds at which the frame started being rendered.
219 *
220 * Note that this time should \b not be used to advance animation clocks.
221 * Instead, see AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos().
Rachel Lee4879d812021-08-25 11:50:11 -0700222 */
223int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
224 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
225
226/**
227 * The number of possible frame timelines.
228 */
229size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
230 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
231
232/**
Rachel Leee81bf262022-08-23 14:37:59 -0700233 * Gets the index of the platform-preferred frame timeline.
234 * The preferred frame timeline is the default
235 * by which the platform scheduled the app, based on the device configuration.
Rachel Lee4879d812021-08-25 11:50:11 -0700236 */
237size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
238 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
239
240/**
Rachel Leee81bf262022-08-23 14:37:59 -0700241 * Gets the token used by the platform to identify the frame timeline at the given \c index.
242 *
243 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
244 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Rachel Lee4879d812021-08-25 11:50:11 -0700245 */
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800246AVsyncId AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700247 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
248
249/**
Rachel Leee81bf262022-08-23 14:37:59 -0700250 * Gets the time in nanoseconds at which the frame described at the given \c index is expected to
251 * be presented. This time should be used to advance any animation clocks.
252 *
253 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
254 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Rachel Lee4879d812021-08-25 11:50:11 -0700255 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800256int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700257 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
258
259/**
Rachel Leee81bf262022-08-23 14:37:59 -0700260 * Gets the time in nanoseconds at which the frame described at the given \c index needs to be
261 * ready by in order to be presented on time.
262 *
263 * \param index index of a frame timeline, in \f( [0, FrameTimelinesLength) \f). See
264 * AChoreographerFrameCallbackData_getFrameTimelinesLength()
Rachel Lee4879d812021-08-25 11:50:11 -0700265 */
Rachel Lee2825fa22022-01-12 17:35:16 -0800266int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700267 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
268
Michael Wrightf5eee402015-12-07 15:26:38 -0500269__END_DECLS
270
271#endif // ANDROID_CHOREOGRAPHER_H
272
273/** @} */