blob: 63aa7ff6c04a9062bc52c53ca833ac77b70ca06c [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
19 * @{
20 */
21
22/**
23 * @file choreographer.h
24 */
25
26#ifndef ANDROID_CHOREOGRAPHER_H
27#define ANDROID_CHOREOGRAPHER_H
28
Santos Cordon908d0082019-02-20 18:08:02 +000029#include <stdint.h>
Michael Wrightfb911b22016-01-26 16:05:54 -080030#include <sys/cdefs.h>
31
Michael Wrightf5eee402015-12-07 15:26:38 -050032__BEGIN_DECLS
33
34struct AChoreographer;
gfan7fed43d2021-04-06 18:50:06 -070035/**
36 * Opaque type that provides access to an AChoreographer object.
37 *
38 * A pointer can be obtained using {@link AChoreographer_getInstance()}.
39 */
Michael Wrightf5eee402015-12-07 15:26:38 -050040typedef struct AChoreographer AChoreographer;
41
Rachel Lee1fb2ddc2022-01-12 14:33:07 -080042
43/**
44 * The identifier of a frame timeline.
45 */
46typedef int64_t AVsyncId;
47
Rachel Lee4879d812021-08-25 11:50:11 -070048struct AChoreographerFrameCallbackData;
49/**
50 * Opaque type that provides access to an AChoreographerFrameCallbackData object.
51 */
52typedef struct AChoreographerFrameCallbackData AChoreographerFrameCallbackData;
53
Michael Wrightf5eee402015-12-07 15:26:38 -050054/**
55 * Prototype of the function that is called when a new frame is being rendered.
56 * It's passed the time that the frame is being rendered as nanoseconds in the
57 * CLOCK_MONOTONIC time base, as well as the data pointer provided by the
58 * application that registered a callback. All callbacks that run as part of
59 * rendering a frame will observe the same frame time, so it should be used
60 * whenever events need to be synchronized (e.g. animations).
61 */
62typedef void (*AChoreographer_frameCallback)(long frameTimeNanos, void* data);
63
Santos Cordon908d0082019-02-20 18:08:02 +000064/**
65 * Prototype of the function that is called when a new frame is being rendered.
66 * It's passed the time that the frame is being rendered as nanoseconds in the
67 * CLOCK_MONOTONIC time base, as well as the data pointer provided by the
68 * application that registered a callback. All callbacks that run as part of
69 * rendering a frame will observe the same frame time, so it should be used
70 * whenever events need to be synchronized (e.g. animations).
71 */
72typedef void (*AChoreographer_frameCallback64)(int64_t frameTimeNanos, void* data);
73
Alec Mouri33682e92020-01-10 15:11:15 -080074/**
Rachel Lee4879d812021-08-25 11:50:11 -070075 * Prototype of the function that is called when a new frame is being rendered.
76 * It's passed the frame data that should not outlive the callback, as well as the data pointer
77 * provided by the application that registered a callback.
78 */
Rachel Leef4dc39f2022-02-15 18:30:59 -080079typedef void (*AChoreographer_vsyncCallback)(
Rachel Lee4879d812021-08-25 11:50:11 -070080 const AChoreographerFrameCallbackData* callbackData, void* data);
81
82/**
Alec Mouri33682e92020-01-10 15:11:15 -080083 * Prototype of the function that is called when the display refresh rate
84 * changes. It's passed the new vsync period in nanoseconds, as well as the data
85 * pointer provided by the application that registered a callback.
86 */
87typedef void (*AChoreographer_refreshRateCallback)(int64_t vsyncPeriodNanos, void* data);
88
Michael Wrightf5eee402015-12-07 15:26:38 -050089/**
90 * Get the AChoreographer instance for the current thread. This must be called
91 * on an ALooper thread.
Elliott Hughes3d70e532019-10-29 08:59:39 -070092 *
93 * Available since API level 24.
Michael Wrightf5eee402015-12-07 15:26:38 -050094 */
Elliott Hughes9db409b2018-06-18 12:28:46 -070095AChoreographer* AChoreographer_getInstance() __INTRODUCED_IN(24);
Michael Wrightf5eee402015-12-07 15:26:38 -050096
97/**
Santos Cordon908d0082019-02-20 18:08:02 +000098 * Deprecated: Use AChoreographer_postFrameCallback64 instead.
Michael Wrightf5eee402015-12-07 15:26:38 -050099 */
100void AChoreographer_postFrameCallback(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700101 AChoreographer_frameCallback callback, void* data)
102 __INTRODUCED_IN(24) __DEPRECATED_IN(29);
Elliott Hughes9db409b2018-06-18 12:28:46 -0700103
Michael Wrightf5eee402015-12-07 15:26:38 -0500104/**
Santos Cordon908d0082019-02-20 18:08:02 +0000105 * Deprecated: Use AChoreographer_postFrameCallbackDelayed64 instead.
Michael Wrightf5eee402015-12-07 15:26:38 -0500106 */
107void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700108 AChoreographer_frameCallback callback, void* data,
109 long delayMillis) __INTRODUCED_IN(24)
110 __DEPRECATED_IN(29);
Dan Albert494ed552016-09-23 15:57:45 -0700111
Santos Cordon908d0082019-02-20 18:08:02 +0000112/**
113 * Power a callback to be run on the next frame. The data pointer provided will
114 * be passed to the callback function when it's called.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700115 *
116 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000117 */
Dillon Cower20e67fa2019-07-30 15:39:54 -0700118void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700119 AChoreographer_frameCallback64 callback, void* data)
120 __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000121
122/**
123 * Post a callback to be run on the frame following the specified delay. The
124 * data pointer provided will be passed to the callback function when it's
125 * called.
Elliott Hughes3d70e532019-10-29 08:59:39 -0700126 *
127 * Available since API level 29.
Santos Cordon908d0082019-02-20 18:08:02 +0000128 */
129void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
Alec Mouri271de042020-04-27 22:38:19 -0700130 AChoreographer_frameCallback64 callback, void* data,
131 uint32_t delayMillis) __INTRODUCED_IN(29);
Santos Cordon908d0082019-02-20 18:08:02 +0000132
Alec Mouri33682e92020-01-10 15:11:15 -0800133/**
Rachel Lee4879d812021-08-25 11:50:11 -0700134 * Posts a callback to run on the next frame. The data pointer provided will
135 * be passed to the callback function when it's called.
136 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800137void AChoreographer_postVsyncCallback(AChoreographer* choreographer,
138 AChoreographer_vsyncCallback callback, void* data)
Rachel Lee4879d812021-08-25 11:50:11 -0700139 __INTRODUCED_IN(33);
140
141/**
Alec Mouri33682e92020-01-10 15:11:15 -0800142 * Registers a callback to be run when the display refresh rate changes. The
143 * data pointer provided will be passed to the callback function when it's
144 * called. The same callback may be registered multiple times, provided that a
145 * different data pointer is provided each time.
146 *
147 * If an application registers a callback for this choreographer instance when
148 * no new callbacks were previously registered, that callback is guaranteed to
149 * be dispatched. However, if the callback and associated data pointer are
150 * unregistered prior to running the callback, then the callback may be silently
151 * dropped.
152 *
153 * This api is thread-safe. Any thread is allowed to register a new refresh
154 * rate callback for the choreographer instance.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700155 *
Alec Mouri4a331652020-07-20 22:05:24 +0000156 * Note that in API level 30, this api is not guaranteed to be atomic with
157 * DisplayManager. That is, calling Display#getRefreshRate very soon after
158 * a refresh rate callback is invoked may return a stale refresh rate. If any
159 * Display properties would be required by this callback, then it is recommended
160 * to listen directly to DisplayManager.DisplayListener#onDisplayChanged events
161 * instead.
162 *
Alec Mouri7015fa92021-02-11 19:31:44 +0000163 * As of API level 31, this api is guaranteed to have a consistent view with DisplayManager;
164 * Display#getRefreshRate is guaranteed to not return a stale refresh rate when invoked from this
165 * callback.
166 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700167 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800168 */
169void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700170 AChoreographer_refreshRateCallback, void* data)
171 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800172
173/**
174 * Unregisters a callback to be run when the display refresh rate changes, along
175 * with the data pointer previously provided when registering the callback. The
176 * callback is only unregistered when the data pointer matches one that was
177 * previously registered.
178 *
179 * This api is thread-safe. Any thread is allowed to unregister an existing
180 * refresh rate callback for the choreographer instance. When a refresh rate
181 * callback and associated data pointer are unregistered, then there is a
182 * guarantee that when the unregistration completes that that callback will not
183 * be run with the data pointer passed.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700184 *
185 * Available since API level 30.
Alec Mouri33682e92020-01-10 15:11:15 -0800186 */
187void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700188 AChoreographer_refreshRateCallback, void* data)
189 __INTRODUCED_IN(30);
Alec Mouri33682e92020-01-10 15:11:15 -0800190
Rachel Lee4879d812021-08-25 11:50:11 -0700191/**
192 * The time in nanoseconds when the frame started being rendered.
193 */
194int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
195 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
196
197/**
198 * The number of possible frame timelines.
199 */
200size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
201 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
202
203/**
204 * Get index of the platform-preferred FrameTimeline.
205 */
206size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
207 const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33);
208
209/**
210 * The vsync ID token used to map Choreographer data.
211 */
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800212AVsyncId AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700213 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
214
215/**
216 * The time in nanoseconds which the frame at given index is expected to be presented.
217 */
Rachel Leef4dc39f2022-02-15 18:30:59 -0800218int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700219 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
220
221/**
222 * The time in nanoseconds which the frame at given index needs to be ready by.
223 */
Rachel Lee2825fa22022-01-12 17:35:16 -0800224int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700225 const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33);
226
Michael Wrightf5eee402015-12-07 15:26:38 -0500227__END_DECLS
228
229#endif // ANDROID_CHOREOGRAPHER_H
230
231/** @} */