| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 1 | /* | 
|  | 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 Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 29 | #include <stdint.h> | 
| Michael Wright | fb911b2 | 2016-01-26 16:05:54 -0800 | [diff] [blame] | 30 | #include <sys/cdefs.h> | 
|  | 31 |  | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 32 | __BEGIN_DECLS | 
|  | 33 |  | 
|  | 34 | struct AChoreographer; | 
| gfan | 7fed43d | 2021-04-06 18:50:06 -0700 | [diff] [blame] | 35 | /** | 
|  | 36 | * Opaque type that provides access to an AChoreographer object. | 
|  | 37 | * | 
|  | 38 | * A pointer can be obtained using {@link AChoreographer_getInstance()}. | 
|  | 39 | */ | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 40 | typedef struct AChoreographer AChoreographer; | 
|  | 41 |  | 
| Rachel Lee | 4879d81 | 2021-08-25 11:50:11 -0700 | [diff] [blame] | 42 | struct AChoreographerFrameCallbackData; | 
|  | 43 | /** | 
|  | 44 | * Opaque type that provides access to an AChoreographerFrameCallbackData object. | 
|  | 45 | */ | 
|  | 46 | typedef struct AChoreographerFrameCallbackData AChoreographerFrameCallbackData; | 
|  | 47 |  | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 48 | /** | 
|  | 49 | * Prototype of the function that is called when a new frame is being rendered. | 
|  | 50 | * It's passed the time that the frame is being rendered as nanoseconds in the | 
|  | 51 | * CLOCK_MONOTONIC time base, as well as the data pointer provided by the | 
|  | 52 | * application that registered a callback. All callbacks that run as part of | 
|  | 53 | * rendering a frame will observe the same frame time, so it should be used | 
|  | 54 | * whenever events need to be synchronized (e.g. animations). | 
|  | 55 | */ | 
|  | 56 | typedef void (*AChoreographer_frameCallback)(long frameTimeNanos, void* data); | 
|  | 57 |  | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 58 | /** | 
|  | 59 | * Prototype of the function that is called when a new frame is being rendered. | 
|  | 60 | * It's passed the time that the frame is being rendered as nanoseconds in the | 
|  | 61 | * CLOCK_MONOTONIC time base, as well as the data pointer provided by the | 
|  | 62 | * application that registered a callback. All callbacks that run as part of | 
|  | 63 | * rendering a frame will observe the same frame time, so it should be used | 
|  | 64 | * whenever events need to be synchronized (e.g. animations). | 
|  | 65 | */ | 
|  | 66 | typedef void (*AChoreographer_frameCallback64)(int64_t frameTimeNanos, void* data); | 
|  | 67 |  | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 68 | /** | 
| Rachel Lee | 4879d81 | 2021-08-25 11:50:11 -0700 | [diff] [blame] | 69 | * Prototype of the function that is called when a new frame is being rendered. | 
|  | 70 | * It's passed the frame data that should not outlive the callback, as well as the data pointer | 
|  | 71 | * provided by the application that registered a callback. | 
|  | 72 | */ | 
|  | 73 | typedef void (*AChoreographer_extendedFrameCallback)( | 
|  | 74 | const AChoreographerFrameCallbackData* callbackData, void* data); | 
|  | 75 |  | 
|  | 76 | /** | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 77 | * Prototype of the function that is called when the display refresh rate | 
|  | 78 | * changes. It's passed the new vsync period in nanoseconds, as well as the data | 
|  | 79 | * pointer provided by the application that registered a callback. | 
|  | 80 | */ | 
|  | 81 | typedef void (*AChoreographer_refreshRateCallback)(int64_t vsyncPeriodNanos, void* data); | 
|  | 82 |  | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 83 | /** | 
|  | 84 | * Get the AChoreographer instance for the current thread. This must be called | 
|  | 85 | * on an ALooper thread. | 
| Elliott Hughes | 3d70e53 | 2019-10-29 08:59:39 -0700 | [diff] [blame] | 86 | * | 
|  | 87 | * Available since API level 24. | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 88 | */ | 
| Elliott Hughes | 9db409b | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 89 | AChoreographer* AChoreographer_getInstance() __INTRODUCED_IN(24); | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 90 |  | 
|  | 91 | /** | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 92 | * Deprecated: Use AChoreographer_postFrameCallback64 instead. | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 93 | */ | 
|  | 94 | void AChoreographer_postFrameCallback(AChoreographer* choreographer, | 
| Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 95 | AChoreographer_frameCallback callback, void* data) | 
|  | 96 | __INTRODUCED_IN(24) __DEPRECATED_IN(29); | 
| Elliott Hughes | 9db409b | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 97 |  | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 98 | /** | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 99 | * Deprecated: Use AChoreographer_postFrameCallbackDelayed64 instead. | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 100 | */ | 
|  | 101 | void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer, | 
| Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 102 | AChoreographer_frameCallback callback, void* data, | 
|  | 103 | long delayMillis) __INTRODUCED_IN(24) | 
|  | 104 | __DEPRECATED_IN(29); | 
| Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame] | 105 |  | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 106 | /** | 
|  | 107 | * Power a callback to be run on the next frame.  The data pointer provided will | 
|  | 108 | * be passed to the callback function when it's called. | 
| Elliott Hughes | 3d70e53 | 2019-10-29 08:59:39 -0700 | [diff] [blame] | 109 | * | 
|  | 110 | * Available since API level 29. | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 111 | */ | 
| Dillon Cower | 20e67fa | 2019-07-30 15:39:54 -0700 | [diff] [blame] | 112 | void AChoreographer_postFrameCallback64(AChoreographer* choreographer, | 
| Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 113 | AChoreographer_frameCallback64 callback, void* data) | 
|  | 114 | __INTRODUCED_IN(29); | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 115 |  | 
|  | 116 | /** | 
|  | 117 | * Post a callback to be run on the frame following the specified delay.  The | 
|  | 118 | * data pointer provided will be passed to the callback function when it's | 
|  | 119 | * called. | 
| Elliott Hughes | 3d70e53 | 2019-10-29 08:59:39 -0700 | [diff] [blame] | 120 | * | 
|  | 121 | * Available since API level 29. | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 122 | */ | 
|  | 123 | void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer, | 
| Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 124 | AChoreographer_frameCallback64 callback, void* data, | 
|  | 125 | uint32_t delayMillis) __INTRODUCED_IN(29); | 
| Santos Cordon | 908d008 | 2019-02-20 18:08:02 +0000 | [diff] [blame] | 126 |  | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 127 | /** | 
| Rachel Lee | 4879d81 | 2021-08-25 11:50:11 -0700 | [diff] [blame] | 128 | * Posts a callback to run on the next frame. The data pointer provided will | 
|  | 129 | * be passed to the callback function when it's called. | 
|  | 130 | */ | 
|  | 131 | void AChoreographer_postExtendedFrameCallback(AChoreographer* choreographer, | 
|  | 132 | AChoreographer_extendedFrameCallback callback, void* data) | 
|  | 133 | __INTRODUCED_IN(33); | 
|  | 134 |  | 
|  | 135 | /** | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 136 | * Registers a callback to be run when the display refresh rate changes. The | 
|  | 137 | * data pointer provided will be passed to the callback function when it's | 
|  | 138 | * called. The same callback may be registered multiple times, provided that a | 
|  | 139 | * different data pointer is provided each time. | 
|  | 140 | * | 
|  | 141 | * If an application registers a callback for this choreographer instance when | 
|  | 142 | * no new callbacks were previously registered, that callback is guaranteed to | 
|  | 143 | * be dispatched. However, if the callback and associated data pointer are | 
|  | 144 | * unregistered prior to running the callback, then the callback may be silently | 
|  | 145 | * dropped. | 
|  | 146 | * | 
|  | 147 | * This api is thread-safe. Any thread is allowed to register a new refresh | 
|  | 148 | * rate callback for the choreographer instance. | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 149 | * | 
| Alec Mouri | 4a33165 | 2020-07-20 22:05:24 +0000 | [diff] [blame] | 150 | * Note that in API level 30, this api is not guaranteed to be atomic with | 
|  | 151 | * DisplayManager. That is, calling Display#getRefreshRate very soon after | 
|  | 152 | * a refresh rate callback is invoked may return a stale refresh rate. If any | 
|  | 153 | * Display properties would be required by this callback, then it is recommended | 
|  | 154 | * to listen directly to DisplayManager.DisplayListener#onDisplayChanged events | 
|  | 155 | * instead. | 
|  | 156 | * | 
| Alec Mouri | 7015fa9 | 2021-02-11 19:31:44 +0000 | [diff] [blame] | 157 | * As of API level 31, this api is guaranteed to have a consistent view with DisplayManager; | 
|  | 158 | * Display#getRefreshRate is guaranteed to not return a stale refresh rate when invoked from this | 
|  | 159 | * callback. | 
|  | 160 | * | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 161 | * Available since API level 30. | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 162 | */ | 
|  | 163 | void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer, | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 164 | AChoreographer_refreshRateCallback, void* data) | 
|  | 165 | __INTRODUCED_IN(30); | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 166 |  | 
|  | 167 | /** | 
|  | 168 | * Unregisters a callback to be run when the display refresh rate changes, along | 
|  | 169 | * with the data pointer previously provided when registering the callback. The | 
|  | 170 | * callback is only unregistered when the data pointer matches one that was | 
|  | 171 | * previously registered. | 
|  | 172 | * | 
|  | 173 | * This api is thread-safe. Any thread is allowed to unregister an existing | 
|  | 174 | * refresh rate callback for the choreographer instance. When a refresh rate | 
|  | 175 | * callback and associated data pointer are unregistered, then there is a | 
|  | 176 | * guarantee that when the unregistration completes that that callback will not | 
|  | 177 | * be run with the data pointer passed. | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 178 | * | 
|  | 179 | * Available since API level 30. | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 180 | */ | 
|  | 181 | void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer, | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 182 | AChoreographer_refreshRateCallback, void* data) | 
|  | 183 | __INTRODUCED_IN(30); | 
| Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 184 |  | 
| Rachel Lee | 4879d81 | 2021-08-25 11:50:11 -0700 | [diff] [blame] | 185 | /** | 
|  | 186 | * The time in nanoseconds when the frame started being rendered. | 
|  | 187 | */ | 
|  | 188 | int64_t AChoreographerFrameCallbackData_getFrameTimeNanos( | 
|  | 189 | const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33); | 
|  | 190 |  | 
|  | 191 | /** | 
|  | 192 | * The number of possible frame timelines. | 
|  | 193 | */ | 
|  | 194 | size_t AChoreographerFrameCallbackData_getFrameTimelinesLength( | 
|  | 195 | const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33); | 
|  | 196 |  | 
|  | 197 | /** | 
|  | 198 | * Get index of the platform-preferred FrameTimeline. | 
|  | 199 | */ | 
|  | 200 | size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex( | 
|  | 201 | const AChoreographerFrameCallbackData* data) __INTRODUCED_IN(33); | 
|  | 202 |  | 
|  | 203 | /** | 
|  | 204 | * The vsync ID token used to map Choreographer data. | 
|  | 205 | */ | 
|  | 206 | int64_t AChoreographerFrameCallbackData_getFrameTimelineVsyncId( | 
|  | 207 | const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33); | 
|  | 208 |  | 
|  | 209 | /** | 
|  | 210 | * The time in nanoseconds which the frame at given index is expected to be presented. | 
|  | 211 | */ | 
|  | 212 | int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentTime( | 
|  | 213 | const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33); | 
|  | 214 |  | 
|  | 215 | /** | 
|  | 216 | * The time in nanoseconds which the frame at given index needs to be ready by. | 
|  | 217 | */ | 
|  | 218 | int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadline( | 
|  | 219 | const AChoreographerFrameCallbackData* data, size_t index) __INTRODUCED_IN(33); | 
|  | 220 |  | 
| Michael Wright | f5eee40 | 2015-12-07 15:26:38 -0500 | [diff] [blame] | 221 | __END_DECLS | 
|  | 222 |  | 
|  | 223 | #endif // ANDROID_CHOREOGRAPHER_H | 
|  | 224 |  | 
|  | 225 | /** @} */ |