Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 17 | /** |
| 18 | * @defgroup APerformanceHint Performance Hint Manager |
| 19 | * |
| 20 | * APerformanceHint allows apps to create performance hint sessions for groups |
| 21 | * of threads, and provide hints to the system about the workload of those threads, |
Matt Buckley | 122a117 | 2024-10-21 12:16:36 -0700 | [diff] [blame] | 22 | * to help the system more accurately allocate resources for them. It is the NDK |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 23 | * counterpart to the Java PerformanceHintManager SDK API. |
| 24 | * |
Matt Buckley | 122a117 | 2024-10-21 12:16:36 -0700 | [diff] [blame] | 25 | * This API is intended for periodic workloads, such as frame production. Clients are |
| 26 | * expected to create an instance of APerformanceHintManager, create a session with |
| 27 | * that, and then set a target duration for the session. Then, they can report the actual |
| 28 | * work duration at the end of each cycle to inform the framework about how long those |
| 29 | * workloads are taking. The framework will then compare the actual durations to the target |
| 30 | * duration and attempt to help the client reach a steady state under the target. |
| 31 | * |
| 32 | * Unlike reportActualWorkDuration, the "notify..." hints are intended to be sent in |
| 33 | * advance of large changes in the workload, to prevent them from going over the target |
| 34 | * when there is a sudden, unforseen change. Their effects are intended to last for only |
| 35 | * one cycle, after which reportActualWorkDuration will have a chance to catch up. |
| 36 | * These hints should be used judiciously, only in cases where the workload is changing |
| 37 | * substantially. To enforce that, they are tracked using a per-app rate limiter to avoid |
| 38 | * excessive hinting and encourage clients to be mindful about when to send them. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 39 | * @{ |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * @file performance_hint.h |
| 44 | * @brief API for creating and managing a hint session. |
| 45 | */ |
| 46 | |
| 47 | |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 48 | #ifndef ANDROID_NATIVE_PERFORMANCE_HINT_H |
| 49 | #define ANDROID_NATIVE_PERFORMANCE_HINT_H |
| 50 | |
| 51 | #include <sys/cdefs.h> |
Matt Buckley | feb30df | 2024-11-17 01:56:15 +0000 | [diff] [blame] | 52 | #include <jni.h> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 53 | |
| 54 | /****************************************************************** |
| 55 | * |
| 56 | * IMPORTANT NOTICE: |
| 57 | * |
| 58 | * This file is part of Android's set of stable system headers |
| 59 | * exposed by the Android NDK (Native Development Kit). |
| 60 | * |
| 61 | * Third-party source AND binary code relies on the definitions |
| 62 | * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. |
| 63 | * |
| 64 | * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) |
| 65 | * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS |
| 66 | * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY |
| 67 | * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES |
| 68 | */ |
| 69 | |
Dan Albert | 8d4bea1 | 2024-08-01 22:31:09 +0000 | [diff] [blame] | 70 | #include <stdbool.h> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 71 | #include <stdint.h> |
Peiyong Lin | c1041d4 | 2023-01-26 00:51:33 +0000 | [diff] [blame] | 72 | #include <unistd.h> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 73 | |
| 74 | __BEGIN_DECLS |
| 75 | |
| 76 | struct APerformanceHintManager; |
| 77 | struct APerformanceHintSession; |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 78 | struct AWorkDuration; |
Matt Buckley | 4b8c0c6 | 2024-11-19 14:36:42 -0800 | [diff] [blame] | 79 | struct ANativeWindow; |
| 80 | struct ASurfaceControl; |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * {@link AWorkDuration} is an opaque type that represents the breakdown of the |
| 84 | * actual workload duration in each component internally. |
| 85 | * |
| 86 | * A new {@link AWorkDuration} can be obtained using |
| 87 | * {@link AWorkDuration_create()}, when the client finishes using |
| 88 | * {@link AWorkDuration}, {@link AWorkDuration_release()} must be |
| 89 | * called to destroy and free up the resources associated with |
| 90 | * {@link AWorkDuration}. |
| 91 | * |
| 92 | * This file provides a set of functions to allow clients to set the measured |
| 93 | * work duration of each component on {@link AWorkDuration}. |
| 94 | * |
| 95 | * - AWorkDuration_setWorkPeriodStartTimestampNanos() |
| 96 | * - AWorkDuration_setActualTotalDurationNanos() |
| 97 | * - AWorkDuration_setActualCpuDurationNanos() |
| 98 | * - AWorkDuration_setActualGpuDurationNanos() |
| 99 | */ |
| 100 | typedef struct AWorkDuration AWorkDuration; |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * An opaque type representing a handle to a performance hint manager. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 104 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 105 | * To use:<ul> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 106 | * <li>Obtain the performance hint manager instance by calling |
| 107 | * {@link APerformanceHint_getManager} function.</li> |
| 108 | * <li>Create an {@link APerformanceHintSession} with |
| 109 | * {@link APerformanceHint_createSession}.</li> |
| 110 | * <li>Get the preferred update rate in nanoseconds with |
| 111 | * {@link APerformanceHint_getPreferredUpdateRateNanos}.</li> |
| 112 | */ |
| 113 | typedef struct APerformanceHintManager APerformanceHintManager; |
| 114 | |
| 115 | /** |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 116 | * An opaque type representing a handle to a performance hint session creation configuration. |
| 117 | * It is consumed by {@link APerformanceHint_createSessionUsingConfig}. |
| 118 | * |
| 119 | * A session creation config encapsulates the required information for a session. |
| 120 | * Additionally, the caller can set various settings for the session, |
| 121 | * to be passed during creation, streamlining the session setup process. |
| 122 | * |
| 123 | * The caller may reuse this object and modify the settings in it |
| 124 | * to create additional sessions. |
| 125 | * |
| 126 | */ |
| 127 | typedef struct ASessionCreationConfig ASessionCreationConfig; |
| 128 | |
| 129 | /** |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 130 | * An opaque type representing a handle to a performance hint session. |
| 131 | * A session can only be acquired from a {@link APerformanceHintManager} |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 132 | * with {@link APerformanceHint_createSession} |
| 133 | * or {@link APerformanceHint_createSessionUsingConfig}. It must be |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 134 | * freed with {@link APerformanceHint_closeSession} after use. |
| 135 | * |
| 136 | * A Session represents a group of threads with an inter-related workload such that hints for |
| 137 | * their performance should be considered as a unit. The threads in a given session should be |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 138 | * long-lived and not created or destroyed dynamically. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 139 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 140 | * The work duration API can be used with periodic workloads to dynamically adjust thread |
| 141 | * performance and keep the work on schedule while optimizing the available power budget. |
| 142 | * When using the work duration API, the starting target duration should be specified |
| 143 | * while creating the session, and can later be adjusted with |
| 144 | * {@link APerformanceHint_updateTargetWorkDuration}. While using the work duration |
| 145 | * API, the client is expected to call {@link APerformanceHint_reportActualWorkDuration} each |
| 146 | * cycle to report the actual time taken to complete to the system. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 147 | * |
Xiang Wang | c8681f8 | 2024-04-08 12:45:59 -0700 | [diff] [blame] | 148 | * Note, methods of {@link APerformanceHintSession_*} are not thread safe so callers must |
| 149 | * ensure thread safety. |
| 150 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 151 | * All timings should be from `std::chrono::steady_clock` or `clock_gettime(CLOCK_MONOTONIC, ...)` |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 152 | */ |
| 153 | typedef struct APerformanceHintSession APerformanceHintSession; |
| 154 | |
Matt Buckley | fff2390 | 2024-11-27 21:54:48 +0000 | [diff] [blame] | 155 | typedef struct ANativeWindow ANativeWindow; |
| 156 | typedef struct ASurfaceControl ASurfaceControl; |
| 157 | |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 158 | /** |
| 159 | * Acquire an instance of the performance hint manager. |
| 160 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 161 | * @return APerformanceHintManager instance on success, nullptr on failure. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 162 | */ |
Xiang Wang | 352ff40 | 2024-01-09 13:27:05 -0800 | [diff] [blame] | 163 | APerformanceHintManager* _Nullable APerformanceHint_getManager() |
| 164 | __INTRODUCED_IN(__ANDROID_API_T__); |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Creates a session for the given set of threads and sets their initial target work |
| 168 | * duration. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 169 | * |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 170 | * @param manager The performance hint manager instance. |
| 171 | * @param threadIds The list of threads to be associated with this session. They must be part of |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 172 | * this process' thread group. |
| 173 | * @param size The size of the list of threadIds. |
| 174 | * @param initialTargetWorkDurationNanos The target duration in nanoseconds for the new session. |
| 175 | * This must be positive if using the work duration API, or 0 otherwise. |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 176 | * @return APerformanceHintSession pointer on success, nullptr on failure. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 177 | */ |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 178 | APerformanceHintSession* _Nullable APerformanceHint_createSession( |
| 179 | APerformanceHintManager* _Nonnull manager, |
| 180 | const int32_t* _Nonnull threadIds, size_t size, |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 181 | int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 182 | |
| 183 | /** |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 184 | * Creates a session for the given set of threads that are graphics pipeline threads |
| 185 | * and set their initial target work duration. |
| 186 | * |
| 187 | * @param manager The performance hint manager instance. |
| 188 | * @param config The configuration struct containing required information |
| 189 | * to create a session. |
| 190 | * @return APerformanceHintSession pointer on success, nullptr on failure. |
| 191 | */ |
| 192 | APerformanceHintSession* _Nullable APerformanceHint_createSessionUsingConfig( |
| 193 | APerformanceHintManager* _Nonnull manager, |
| 194 | ASessionCreationConfig* _Nonnull config) |
| 195 | __INTRODUCED_IN(36); |
| 196 | |
| 197 | /** |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 198 | * Get preferred update rate information for this device. |
| 199 | * |
| 200 | * @param manager The performance hint manager instance. |
| 201 | * @return the preferred update rate supported by device software. |
| 202 | */ |
| 203 | int64_t APerformanceHint_getPreferredUpdateRateNanos( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 204 | APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(__ANDROID_API_T__); |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 205 | |
| 206 | /** |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 207 | * Get maximum number of graphics pipieline threads per-app for this device. |
| 208 | * |
| 209 | * @param manager The performance hint manager instance. |
| 210 | * @return the maximum number of graphics pipeline threads supported by device. |
| 211 | */ |
| 212 | int APerformanceHint_getMaxGraphicsPipelineThreadsCount( |
| 213 | APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(36); |
| 214 | |
| 215 | /** |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 216 | * Updates this session's target duration for each cycle of work. |
| 217 | * |
| 218 | * @param session The performance hint session instance to update. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 219 | * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive. |
| 220 | * @return 0 on success. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 221 | * EINVAL if targetDurationNanos is not positive. |
| 222 | * EPIPE if communication with the system service has failed. |
| 223 | */ |
| 224 | int APerformanceHint_updateTargetWorkDuration( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 225 | APerformanceHintSession* _Nonnull session, |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 226 | int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 227 | |
| 228 | /** |
| 229 | * Reports the actual duration for the last cycle of work. |
| 230 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 231 | * The system will attempt to adjust the scheduling and performance of the |
| 232 | * threads within the thread group to bring the actual duration close to the target duration. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 233 | * |
| 234 | * @param session The performance hint session instance to update. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 235 | * @param actualDurationNanos The duration of time the thread group took to complete its last |
| 236 | * task in nanoseconds. This must be positive. |
| 237 | * @return 0 on success. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 238 | * EINVAL if actualDurationNanos is not positive. |
| 239 | * EPIPE if communication with the system service has failed. |
| 240 | */ |
| 241 | int APerformanceHint_reportActualWorkDuration( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 242 | APerformanceHintSession* _Nonnull session, |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 243 | int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 244 | |
| 245 | /** |
| 246 | * Release the performance hint manager pointer acquired via |
| 247 | * {@link APerformanceHint_createSession}. |
| 248 | * |
Matt Buckley | feb30df | 2024-11-17 01:56:15 +0000 | [diff] [blame] | 249 | * This cannot be used to close a Java PerformanceHintManager.Session, as its |
| 250 | * lifecycle is tied to the object in the SDK. |
| 251 | * |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 252 | * @param session The performance hint session instance to release. |
| 253 | */ |
| 254 | void APerformanceHint_closeSession( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 255 | APerformanceHintSession* _Nonnull session) __INTRODUCED_IN(__ANDROID_API_T__); |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 256 | |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 257 | /** |
| 258 | * Set a list of threads to the performance hint session. This operation will replace |
| 259 | * the current list of threads with the given list of threads. |
| 260 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 261 | * @param session The performance hint session instance to update. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 262 | * @param threadIds The list of threads to be associated with this session. They must be part of |
| 263 | * this app's thread group. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 264 | * @param size The size of the list of threadIds. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 265 | * @return 0 on success. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 266 | * EINVAL if the list of thread ids is empty or if any of the thread ids are not part of |
| 267 | the thread group. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 268 | * EPIPE if communication with the system service has failed. |
Xiang Wang | f6d21b5 | 2023-07-25 17:34:01 -0700 | [diff] [blame] | 269 | * EPERM if any thread id doesn't belong to the application. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 270 | */ |
| 271 | int APerformanceHint_setThreads( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 272 | APerformanceHintSession* _Nonnull session, |
| 273 | const pid_t* _Nonnull threadIds, |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 274 | size_t size) __INTRODUCED_IN(__ANDROID_API_U__); |
| 275 | |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 276 | /** |
| 277 | * This tells the session that these threads can be |
| 278 | * safely scheduled to prefer power efficiency over performance. |
| 279 | * |
| 280 | * @param session The performance hint session instance to update. |
| 281 | * @param enabled The flag which sets whether this session will use power-efficient scheduling. |
| 282 | * @return 0 on success. |
| 283 | * EPIPE if communication with the system service has failed. |
| 284 | */ |
| 285 | int APerformanceHint_setPreferPowerEfficiency( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 286 | APerformanceHintSession* _Nonnull session, |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 287 | bool enabled) __INTRODUCED_IN(__ANDROID_API_V__); |
| 288 | |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 289 | /** |
| 290 | * Reports the durations for the last cycle of work. |
| 291 | * |
| 292 | * The system will attempt to adjust the scheduling and performance of the |
| 293 | * threads within the thread group to bring the actual duration close to the target duration. |
| 294 | * |
| 295 | * @param session The {@link APerformanceHintSession} instance to update. |
| 296 | * @param workDuration The {@link AWorkDuration} structure of times the thread group took to |
| 297 | * complete its last task in nanoseconds breaking down into different components. |
| 298 | * |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 299 | * The work period start timestamp and actual total duration must be greater than zero. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 300 | * |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 301 | * The actual CPU and GPU durations must be greater than or equal to zero, and at least one |
| 302 | * of them must be greater than zero. When one of them is equal to zero, it means that type |
| 303 | * of work was not measured for this workload. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 304 | * |
| 305 | * @return 0 on success. |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 306 | * EINVAL if any duration is an invalid number. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 307 | * EPIPE if communication with the system service has failed. |
| 308 | */ |
| 309 | int APerformanceHint_reportActualWorkDuration2( |
| 310 | APerformanceHintSession* _Nonnull session, |
| 311 | AWorkDuration* _Nonnull workDuration) __INTRODUCED_IN(__ANDROID_API_V__); |
| 312 | |
| 313 | /** |
Matt Buckley | 122a117 | 2024-10-21 12:16:36 -0700 | [diff] [blame] | 314 | * Informs the framework of an upcoming increase in the workload of a graphics pipeline |
| 315 | * bound to this session. The user can specify whether the increase is expected to be |
| 316 | * on the CPU, GPU, or both. |
| 317 | * |
| 318 | * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the |
| 319 | * rate limiter. |
| 320 | * |
| 321 | * @param cpu Indicates if the workload increase is expected to affect the CPU. |
| 322 | * @param gpu Indicates if the workload increase is expected to affect the GPU. |
| 323 | * @param debugName A required string used to identify this specific hint during |
| 324 | * tracing. This debug string will only be held for the duration of the |
| 325 | * method, and can be safely discarded after. |
| 326 | * |
| 327 | * @return 0 on success. |
| 328 | * EINVAL if no hints were requested. |
| 329 | * EBUSY if the hint was rate limited. |
| 330 | * EPIPE if communication with the system service has failed. |
| 331 | * ENOTSUP if the hint is not supported. |
| 332 | */ |
| 333 | int APerformanceHint_notifyWorkloadIncrease( |
| 334 | APerformanceHintSession* _Nonnull session, |
| 335 | bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36); |
| 336 | |
| 337 | /** |
| 338 | * Informs the framework of an upcoming reset in the workload of a graphics pipeline |
| 339 | * bound to this session, or the imminent start of a new workload. The user can specify |
| 340 | * whether the reset is expected to affect the CPU, GPU, or both. |
| 341 | * |
| 342 | * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the |
| 343 | * this load tracking. |
| 344 | * |
| 345 | * @param cpu Indicates if the workload reset is expected to affect the CPU. |
| 346 | * @param gpu Indicates if the workload reset is expected to affect the GPU. |
| 347 | * @param debugName A required string used to identify this specific hint during |
| 348 | * tracing. This debug string will only be held for the duration of the |
| 349 | * method, and can be safely discarded after. |
| 350 | * |
| 351 | * @return 0 on success. |
| 352 | * EINVAL if no hints were requested. |
| 353 | * EBUSY if the hint was rate limited. |
| 354 | * EPIPE if communication with the system service has failed. |
| 355 | * ENOTSUP if the hint is not supported. |
| 356 | */ |
| 357 | int APerformanceHint_notifyWorkloadReset( |
| 358 | APerformanceHintSession* _Nonnull session, |
| 359 | bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36); |
| 360 | |
| 361 | /** |
Matt Buckley | 4b8c0c6 | 2024-11-19 14:36:42 -0800 | [diff] [blame] | 362 | * Associates a session with any {@link ASurfaceControl} or {@link ANativeWindow} |
| 363 | * instances managed by this session. |
| 364 | * |
| 365 | * This method is primarily intended for sessions that manage the timing of an entire |
| 366 | * graphics pipeline end-to-end, such as those using the |
| 367 | * {@link ASessionCreationConfig_setGraphicsPipeline} API. However, any session directly |
| 368 | * or indirectly managing a graphics pipeline should still associate themselves with |
| 369 | * directly relevant ASurfaceControl or ANativeWindow instances for better optimization. |
| 370 | * |
| 371 | * To see any benefit from this method, the client must make sure they are updating the framerate |
| 372 | * of attached surfaces using methods such as {@link ANativeWindow_setFrameRate}, or by updating |
| 373 | * any associated ASurfaceControls with transactions that have {ASurfaceTransaction_setFrameRate}. |
| 374 | * |
| 375 | * @param session The {@link APerformanceHintSession} instance to update. |
| 376 | * @param nativeWindows A pointer to a list of ANativeWindows associated with this session. |
| 377 | * nullptr can be passed to indicate there are no associated ANativeWindows. |
| 378 | * @param nativeWindowsSize The number of ANativeWindows in the list. |
| 379 | * @param surfaceControls A pointer to a list of ASurfaceControls associated with this session. |
| 380 | * nullptr can be passed to indicate there are no associated ASurfaceControls. |
| 381 | * @param surfaceControlsSize The number of ASurfaceControls in the list. |
| 382 | * |
| 383 | * @return 0 on success. |
| 384 | * EPIPE if communication has failed. |
| 385 | * ENOTSUP if unsupported. |
| 386 | * EINVAL if invalid or empty arguments passed. |
| 387 | */ |
| 388 | |
| 389 | int APerformanceHint_setNativeSurfaces(APerformanceHintSession* _Nonnull session, |
| 390 | ANativeWindow* _Nonnull* _Nullable nativeWindows, int nativeWindowsSize, |
| 391 | ASurfaceControl* _Nonnull* _Nullable surfaceControls, int surfaceControlsSize) |
| 392 | __INTRODUCED_IN(36); |
| 393 | |
| 394 | /** |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 395 | * Creates a new AWorkDuration. When the client finishes using {@link AWorkDuration}, it should |
| 396 | * call {@link AWorkDuration_release()} to destroy {@link AWorkDuration} and release all resources |
| 397 | * associated with it. |
| 398 | * |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 399 | * @return AWorkDuration pointer. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 400 | */ |
| 401 | AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__); |
| 402 | |
| 403 | /** |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 404 | * Destroys a {@link AWorkDuration} and frees all resources associated with it. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 405 | * |
| 406 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
| 407 | */ |
Xiang Wang | 352ff40 | 2024-01-09 13:27:05 -0800 | [diff] [blame] | 408 | void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration) |
| 409 | __INTRODUCED_IN(__ANDROID_API_V__); |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 410 | |
| 411 | /** |
| 412 | * Sets the work period start timestamp in nanoseconds. |
| 413 | * |
| 414 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
| 415 | * @param workPeriodStartTimestampNanos The work period start timestamp in nanoseconds based on |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 416 | * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 417 | */ |
| 418 | void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 419 | int64_t workPeriodStartTimestampNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 420 | |
| 421 | /** |
| 422 | * Sets the actual total work duration in nanoseconds. |
| 423 | * |
| 424 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 425 | * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must |
| 426 | * be greater than zero. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 427 | */ |
| 428 | void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 429 | int64_t actualTotalDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 430 | |
| 431 | /** |
| 432 | * Sets the actual CPU work duration in nanoseconds. |
| 433 | * |
| 434 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 435 | * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 436 | * greater than or equal to zero. If it is equal to zero, that means the CPU was not |
| 437 | * measured. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 438 | */ |
| 439 | void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 440 | int64_t actualCpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 441 | |
| 442 | /** |
| 443 | * Sets the actual GPU work duration in nanoseconds. |
| 444 | * |
| 445 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}. |
| 446 | * @param actualGpuDurationNanos The actual GPU work duration in nanoseconds, the number must be |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 447 | * greater than or equal to zero. If it is equal to zero, that means the GPU was not |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 448 | * measured. |
| 449 | */ |
| 450 | void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 451 | int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 452 | |
Matt Buckley | feb30df | 2024-11-17 01:56:15 +0000 | [diff] [blame] | 453 | /** |
| 454 | * Return the APerformanceHintSession wrapped by a Java PerformanceHintManager.Session object. |
| 455 | * |
| 456 | * The Java session maintains ownership over the wrapped native session, so it cannot be |
| 457 | * closed using {@link APerformanceHint_closeSession}. |
| 458 | * |
| 459 | * @param env The Java environment where the PerformanceHintManager.Session lives. |
| 460 | * @param sessionObj The Java Session to unwrap. |
| 461 | * |
| 462 | * @return A pointer to the APerformanceHintManager that backs the Java Session. |
| 463 | */ |
| 464 | APerformanceHintSession* _Nonnull APerformanceHint_borrowSessionFromJava( |
| 465 | JNIEnv* _Nonnull env, jobject _Nonnull sessionObj) __INTRODUCED_IN(36); |
| 466 | |
Andy Yu | fa8eb01 | 2024-10-15 17:00:09 -0700 | [diff] [blame] | 467 | /* |
| 468 | * Creates a new ASessionCreationConfig. |
| 469 | * |
| 470 | * When the client finishes using {@link ASessionCreationConfig}, it should |
| 471 | * call {@link ASessionCreationConfig_release()} to destroy |
| 472 | * {@link ASessionCreationConfig} and release all resources |
| 473 | * associated with it. |
| 474 | * |
| 475 | * @return ASessionCreationConfig pointer. |
| 476 | */ |
| 477 | ASessionCreationConfig* _Nonnull ASessionCreationConfig_create() |
| 478 | __INTRODUCED_IN(36); |
| 479 | |
| 480 | |
| 481 | /** |
| 482 | * Destroys a {@link ASessionCreationConfig} and frees all |
| 483 | * resources associated with it. |
| 484 | * |
| 485 | * @param config The {@link ASessionCreationConfig} |
| 486 | * created by calling {@link ASessionCreationConfig_create()}. |
| 487 | */ |
| 488 | void ASessionCreationConfig_release( |
| 489 | ASessionCreationConfig* _Nonnull config) __INTRODUCED_IN(36); |
| 490 | |
| 491 | /** |
| 492 | * Sets the tids to be associated with the session to be created. |
| 493 | * |
| 494 | * @param config The {@link ASessionCreationConfig} |
| 495 | * created by calling {@link ASessionCreationConfig_create()} |
| 496 | * @param tids The list of tids to be associated with this session. They must be part of |
| 497 | * this process' thread group. |
| 498 | * @param size The size of the list of tids. |
| 499 | * |
| 500 | * @return 0 on success. |
| 501 | * EINVAL if invalid array pointer or the value of size |
| 502 | */ |
| 503 | int ASessionCreationConfig_setTids( |
| 504 | ASessionCreationConfig* _Nonnull config, |
| 505 | const pid_t* _Nonnull tids, size_t size) __INTRODUCED_IN(36); |
| 506 | |
| 507 | /** |
| 508 | * Sets the initial target work duration in nanoseconds for the session to be created. |
| 509 | * |
| 510 | * @param config The {@link ASessionCreationConfig} |
| 511 | * created by calling {@link ASessionCreationConfig_create()}. |
| 512 | * @param targetWorkDurationNanos The parameter to specify a target duration |
| 513 | * in nanoseconds for the new session; this value must be positive to use |
| 514 | * the work duration API. |
| 515 | * |
| 516 | * @return 0 on success. |
| 517 | * ENOTSUP if unsupported |
| 518 | * EINVAL if invalid value |
| 519 | */ |
| 520 | int ASessionCreationConfig_setTargetWorkDurationNanos( |
| 521 | ASessionCreationConfig* _Nonnull config, |
| 522 | int64_t targetWorkDurationNanos) __INTRODUCED_IN(36); |
| 523 | |
| 524 | /** |
| 525 | * Sets whether power efficiency mode will be enabled for the session. |
| 526 | * This tells the session that these threads can be |
| 527 | * safely scheduled to prefer power efficiency over performance. |
| 528 | * |
| 529 | * @param config The {@link ASessionCreationConfig} |
| 530 | * created by calling {@link ASessionCreationConfig_create()}. |
| 531 | * @param enabled Whether power efficiency mode will be enabled. |
| 532 | * |
| 533 | * @return 0 on success. |
| 534 | * ENOTSUP if unsupported |
| 535 | * EINVAL if invalid pointer to creation config |
| 536 | */ |
| 537 | int ASessionCreationConfig_setPreferPowerEfficiency( |
| 538 | ASessionCreationConfig* _Nonnull config, bool enabled) __INTRODUCED_IN(36); |
| 539 | |
| 540 | /** |
| 541 | * Sessions setting this hint are expected to time the critical path of |
| 542 | * graphics pipeline from end to end, with the total work duration |
| 543 | * representing the time from the start of frame production until the |
| 544 | * buffer is fully finished drawing. |
| 545 | * |
| 546 | * It should include any threads on the critical path of that pipeline, |
| 547 | * up to a limit accessible from {@link getMaxGraphicsPipelineThreadsCount()}. |
| 548 | * |
| 549 | * @param config The {@link ASessionCreationConfig} |
| 550 | * created by calling {@link ASessionCreationConfig_create()}. |
| 551 | * @param enabled Whether this session manages a graphics pipeline's critical path. |
| 552 | * |
| 553 | * @return 0 on success. |
| 554 | * ENOTSUP if unsupported |
| 555 | * EINVAL if invalid pointer to creation config or maximum threads for graphics |
| 556 | pipeline is reached. |
| 557 | */ |
| 558 | int ASessionCreationConfig_setGraphicsPipeline( |
Andy Yu | d54e589 | 2024-11-21 09:46:22 -0800 | [diff] [blame] | 559 | ASessionCreationConfig* _Nonnull config, bool enabled) __INTRODUCED_IN(36); |
Matt Buckley | feb30df | 2024-11-17 01:56:15 +0000 | [diff] [blame] | 560 | |
Matt Buckley | 4b8c0c6 | 2024-11-19 14:36:42 -0800 | [diff] [blame] | 561 | /** |
| 562 | * Associates a session with any {@link ASurfaceControl} or {@link ANativeWindow} |
| 563 | * instances managed by this session. See {@link APerformanceHint_setNativeSurfaces} |
| 564 | * for more details. |
| 565 | * |
| 566 | * @param config The {@link ASessionCreationConfig} |
| 567 | * created by calling {@link ASessionCreationConfig_create()}. |
| 568 | * @param nativeWindows A pointer to a list of ANativeWindows associated with this session. |
| 569 | * nullptr can be passed to indicate there are no associated ANativeWindows. |
| 570 | * @param nativeWindowsSize The number of ANativeWindows in the list. |
| 571 | * @param surfaceControls A pointer to a list of ASurfaceControls associated with this session. |
| 572 | * nullptr can be passed to indicate there are no associated ASurfaceControls. |
| 573 | * @param surfaceControlsSize The number of ASurfaceControls in the list. |
| 574 | * |
| 575 | * @return 0 on success. |
| 576 | * ENOTSUP if unsupported. |
| 577 | * EINVAL if invalid or empty arguments passed. |
| 578 | */ |
| 579 | int ASessionCreationConfig_setNativeSurfaces( |
| 580 | ASessionCreationConfig* _Nonnull config, |
| 581 | ANativeWindow* _Nonnull* _Nullable nativeWindows, int nativeWindowsSize, |
| 582 | ASurfaceControl* _Nonnull* _Nullable surfaceControls, int surfaceControlsSize) |
| 583 | __INTRODUCED_IN(36); |
| 584 | |
| 585 | /** |
| 586 | * Enable automatic timing mode for sessions using the GRAPHICS_PIPELINE API with an attached |
| 587 | * surface. In this mode, sessions do not need to report actual durations and only need |
| 588 | * to keep their thread list up-to-date, set a native surface, call |
| 589 | * {@link ASessionCreationConfig_setGraphicsPipeline()} to signal that the session is in |
| 590 | * "graphics pipeline" mode, and then set whether automatic timing is desired for the |
| 591 | * CPU, GPU, or both, using this method. |
| 592 | * |
| 593 | * It is still be beneficial to set an accurate target time, as this may help determine |
| 594 | * timing information for some workloads where there is less information available from |
| 595 | * the framework, such as games. Additionally, reported CPU durations will be ignored |
| 596 | * while automatic CPU timing is enabled, and similarly GPU durations will be ignored |
| 597 | * when automatic GPU timing is enabled. When both are enabled, the entire |
| 598 | * reportActualWorkDuration call will be ignored, and the session will be managed |
| 599 | * completely automatically. |
| 600 | * |
| 601 | * This mode will not work unless the client makes sure they are updating the framerate |
| 602 | * of attached surfaces with methods such as {@link ANativeWindow_setFrameRate}, or updating |
| 603 | * any associated ASurfaceControls with transactions that have {ASurfaceTransaction_setFrameRate}. |
| 604 | * |
| 605 | * @param config The {@link ASessionCreationConfig} |
| 606 | * created by calling {@link ASessionCreationConfig_create()}. |
| 607 | * @param cpu Whether to enable automatic timing for the CPU for this session. |
| 608 | * @param gpu Whether to enable automatic timing for the GPU for this session. |
| 609 | * |
| 610 | * @return 0 on success. |
| 611 | * ENOTSUP if unsupported. |
| 612 | */ |
| 613 | int ASessionCreationConfig_setUseAutoTiming( |
| 614 | ASessionCreationConfig* _Nonnull config, |
| 615 | bool cpu, bool gpu) |
| 616 | __INTRODUCED_IN(36); |
| 617 | |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 618 | __END_DECLS |
| 619 | |
| 620 | #endif // ANDROID_NATIVE_PERFORMANCE_HINT_H |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 621 | |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 622 | /** @} */ |