blob: 75a8bd49fc7c3cc9f01714861b725029bb83f2f7 [file] [log] [blame]
Bo Liu406c8ab2021-11-10 19:20:40 -05001/*
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 Buckleycc146422023-06-28 19:14:02 +000017 /**
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 Buckley122a1172024-10-21 12:16:36 -070022 * to help the system more accurately allocate resources for them. It is the NDK
Matt Buckleycc146422023-06-28 19:14:02 +000023 * counterpart to the Java PerformanceHintManager SDK API.
24 *
Matt Buckley122a1172024-10-21 12:16:36 -070025 * 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 Buckleycc146422023-06-28 19:14:02 +000039 * @{
40 */
41
42/**
43 * @file performance_hint.h
44 * @brief API for creating and managing a hint session.
45 */
46
47
Bo Liu406c8ab2021-11-10 19:20:40 -050048#ifndef ANDROID_NATIVE_PERFORMANCE_HINT_H
49#define ANDROID_NATIVE_PERFORMANCE_HINT_H
50
51#include <sys/cdefs.h>
Matt Buckleyfeb30df2024-11-17 01:56:15 +000052#include <jni.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050053
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 Albert8d4bea12024-08-01 22:31:09 +000070#include <stdbool.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050071#include <stdint.h>
Peiyong Linc1041d42023-01-26 00:51:33 +000072#include <unistd.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050073
74__BEGIN_DECLS
75
76struct APerformanceHintManager;
77struct APerformanceHintSession;
Peiyong Lin81780c42023-10-08 21:11:26 +000078struct AWorkDuration;
Matt Buckley4b8c0c62024-11-19 14:36:42 -080079struct ANativeWindow;
80struct ASurfaceControl;
Peiyong Lin81780c42023-10-08 21:11:26 +000081
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 */
100typedef struct AWorkDuration AWorkDuration;
Bo Liu406c8ab2021-11-10 19:20:40 -0500101
102/**
103 * An opaque type representing a handle to a performance hint manager.
Bo Liu406c8ab2021-11-10 19:20:40 -0500104 *
Matt Buckleycc146422023-06-28 19:14:02 +0000105 * To use:<ul>
Bo Liu406c8ab2021-11-10 19:20:40 -0500106 * <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 */
113typedef struct APerformanceHintManager APerformanceHintManager;
114
115/**
Andy Yufa8eb012024-10-15 17:00:09 -0700116 * 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 */
127typedef struct ASessionCreationConfig ASessionCreationConfig;
128
129/**
Bo Liu406c8ab2021-11-10 19:20:40 -0500130 * An opaque type representing a handle to a performance hint session.
131 * A session can only be acquired from a {@link APerformanceHintManager}
Andy Yufa8eb012024-10-15 17:00:09 -0700132 * with {@link APerformanceHint_createSession}
133 * or {@link APerformanceHint_createSessionUsingConfig}. It must be
Bo Liu406c8ab2021-11-10 19:20:40 -0500134 * 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 Buckleycc146422023-06-28 19:14:02 +0000138 * long-lived and not created or destroyed dynamically.
Bo Liu406c8ab2021-11-10 19:20:40 -0500139 *
Matt Buckleycc146422023-06-28 19:14:02 +0000140 * 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 Liu406c8ab2021-11-10 19:20:40 -0500147 *
Xiang Wangc8681f82024-04-08 12:45:59 -0700148 * Note, methods of {@link APerformanceHintSession_*} are not thread safe so callers must
149 * ensure thread safety.
150 *
Matt Buckleycc146422023-06-28 19:14:02 +0000151 * All timings should be from `std::chrono::steady_clock` or `clock_gettime(CLOCK_MONOTONIC, ...)`
Bo Liu406c8ab2021-11-10 19:20:40 -0500152 */
153typedef struct APerformanceHintSession APerformanceHintSession;
154
155/**
156 * Acquire an instance of the performance hint manager.
157 *
Matt Buckleycc146422023-06-28 19:14:02 +0000158 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500159 */
Xiang Wang352ff402024-01-09 13:27:05 -0800160APerformanceHintManager* _Nullable APerformanceHint_getManager()
161 __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500162
163/**
164 * Creates a session for the given set of threads and sets their initial target work
165 * duration.
Matt Buckleycc146422023-06-28 19:14:02 +0000166 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500167 * @param manager The performance hint manager instance.
168 * @param threadIds The list of threads to be associated with this session. They must be part of
Matt Buckleycc146422023-06-28 19:14:02 +0000169 * this process' thread group.
170 * @param size The size of the list of threadIds.
171 * @param initialTargetWorkDurationNanos The target duration in nanoseconds for the new session.
172 * This must be positive if using the work duration API, or 0 otherwise.
Andy Yufa8eb012024-10-15 17:00:09 -0700173 * @return APerformanceHintSession pointer on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500174 */
Peiyong Lin81780c42023-10-08 21:11:26 +0000175APerformanceHintSession* _Nullable APerformanceHint_createSession(
176 APerformanceHintManager* _Nonnull manager,
177 const int32_t* _Nonnull threadIds, size_t size,
Bo Liu406c8ab2021-11-10 19:20:40 -0500178 int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
179
180/**
Andy Yufa8eb012024-10-15 17:00:09 -0700181 * Creates a session for the given set of threads that are graphics pipeline threads
182 * and set their initial target work duration.
183 *
184 * @param manager The performance hint manager instance.
185 * @param config The configuration struct containing required information
186 * to create a session.
187 * @return APerformanceHintSession pointer on success, nullptr on failure.
188 */
189APerformanceHintSession* _Nullable APerformanceHint_createSessionUsingConfig(
190 APerformanceHintManager* _Nonnull manager,
191 ASessionCreationConfig* _Nonnull config)
192 __INTRODUCED_IN(36);
193
194/**
Bo Liu406c8ab2021-11-10 19:20:40 -0500195 * Get preferred update rate information for this device.
196 *
197 * @param manager The performance hint manager instance.
198 * @return the preferred update rate supported by device software.
199 */
200int64_t APerformanceHint_getPreferredUpdateRateNanos(
Peiyong Lin81780c42023-10-08 21:11:26 +0000201 APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500202
203/**
Andy Yufa8eb012024-10-15 17:00:09 -0700204 * Get maximum number of graphics pipieline threads per-app for this device.
205 *
206 * @param manager The performance hint manager instance.
207 * @return the maximum number of graphics pipeline threads supported by device.
208 */
209 int APerformanceHint_getMaxGraphicsPipelineThreadsCount(
210 APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(36);
211
212/**
Bo Liu406c8ab2021-11-10 19:20:40 -0500213 * Updates this session's target duration for each cycle of work.
214 *
215 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000216 * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive.
217 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500218 * EINVAL if targetDurationNanos is not positive.
219 * EPIPE if communication with the system service has failed.
220 */
221int APerformanceHint_updateTargetWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000222 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500223 int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
224
225/**
226 * Reports the actual duration for the last cycle of work.
227 *
Matt Buckleycc146422023-06-28 19:14:02 +0000228 * The system will attempt to adjust the scheduling and performance of the
229 * threads within the thread group to bring the actual duration close to the target duration.
Bo Liu406c8ab2021-11-10 19:20:40 -0500230 *
231 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000232 * @param actualDurationNanos The duration of time the thread group took to complete its last
233 * task in nanoseconds. This must be positive.
234 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500235 * EINVAL if actualDurationNanos is not positive.
236 * EPIPE if communication with the system service has failed.
237 */
238int APerformanceHint_reportActualWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000239 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500240 int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
241
242/**
243 * Release the performance hint manager pointer acquired via
244 * {@link APerformanceHint_createSession}.
245 *
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000246 * This cannot be used to close a Java PerformanceHintManager.Session, as its
247 * lifecycle is tied to the object in the SDK.
248 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500249 * @param session The performance hint session instance to release.
250 */
251void APerformanceHint_closeSession(
Peiyong Lin81780c42023-10-08 21:11:26 +0000252 APerformanceHintSession* _Nonnull session) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500253
Peiyong Linf9c984f2022-11-11 18:28:20 +0000254/**
255 * Set a list of threads to the performance hint session. This operation will replace
256 * the current list of threads with the given list of threads.
257 *
Matt Buckleycc146422023-06-28 19:14:02 +0000258 * @param session The performance hint session instance to update.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000259 * @param threadIds The list of threads to be associated with this session. They must be part of
260 * this app's thread group.
Matt Buckleycc146422023-06-28 19:14:02 +0000261 * @param size The size of the list of threadIds.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000262 * @return 0 on success.
Matt Buckleycc146422023-06-28 19:14:02 +0000263 * EINVAL if the list of thread ids is empty or if any of the thread ids are not part of
264 the thread group.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000265 * EPIPE if communication with the system service has failed.
Xiang Wangf6d21b52023-07-25 17:34:01 -0700266 * EPERM if any thread id doesn't belong to the application.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000267 */
268int APerformanceHint_setThreads(
Peiyong Lin81780c42023-10-08 21:11:26 +0000269 APerformanceHintSession* _Nonnull session,
270 const pid_t* _Nonnull threadIds,
Peiyong Linf9c984f2022-11-11 18:28:20 +0000271 size_t size) __INTRODUCED_IN(__ANDROID_API_U__);
272
Matt Buckleycc146422023-06-28 19:14:02 +0000273/**
274 * This tells the session that these threads can be
275 * safely scheduled to prefer power efficiency over performance.
276 *
277 * @param session The performance hint session instance to update.
278 * @param enabled The flag which sets whether this session will use power-efficient scheduling.
279 * @return 0 on success.
280 * EPIPE if communication with the system service has failed.
281 */
282int APerformanceHint_setPreferPowerEfficiency(
Peiyong Lin81780c42023-10-08 21:11:26 +0000283 APerformanceHintSession* _Nonnull session,
Matt Buckleycc146422023-06-28 19:14:02 +0000284 bool enabled) __INTRODUCED_IN(__ANDROID_API_V__);
285
Peiyong Lin81780c42023-10-08 21:11:26 +0000286/**
287 * Reports the durations for the last cycle of work.
288 *
289 * The system will attempt to adjust the scheduling and performance of the
290 * threads within the thread group to bring the actual duration close to the target duration.
291 *
292 * @param session The {@link APerformanceHintSession} instance to update.
293 * @param workDuration The {@link AWorkDuration} structure of times the thread group took to
294 * complete its last task in nanoseconds breaking down into different components.
295 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000296 * The work period start timestamp and actual total duration must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000297 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000298 * The actual CPU and GPU durations must be greater than or equal to zero, and at least one
299 * of them must be greater than zero. When one of them is equal to zero, it means that type
300 * of work was not measured for this workload.
Peiyong Lin81780c42023-10-08 21:11:26 +0000301 *
302 * @return 0 on success.
Matt Buckley81cc0932024-01-18 19:56:31 +0000303 * EINVAL if any duration is an invalid number.
Peiyong Lin81780c42023-10-08 21:11:26 +0000304 * EPIPE if communication with the system service has failed.
305 */
306int APerformanceHint_reportActualWorkDuration2(
307 APerformanceHintSession* _Nonnull session,
308 AWorkDuration* _Nonnull workDuration) __INTRODUCED_IN(__ANDROID_API_V__);
309
310/**
Matt Buckley122a1172024-10-21 12:16:36 -0700311 * Informs the framework of an upcoming increase in the workload of a graphics pipeline
312 * bound to this session. The user can specify whether the increase is expected to be
313 * on the CPU, GPU, or both.
314 *
315 * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the
316 * rate limiter.
317 *
318 * @param cpu Indicates if the workload increase is expected to affect the CPU.
319 * @param gpu Indicates if the workload increase is expected to affect the GPU.
320 * @param debugName A required string used to identify this specific hint during
321 * tracing. This debug string will only be held for the duration of the
322 * method, and can be safely discarded after.
323 *
324 * @return 0 on success.
325 * EINVAL if no hints were requested.
326 * EBUSY if the hint was rate limited.
327 * EPIPE if communication with the system service has failed.
328 * ENOTSUP if the hint is not supported.
329 */
330int APerformanceHint_notifyWorkloadIncrease(
331 APerformanceHintSession* _Nonnull session,
332 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
333
334/**
335 * Informs the framework of an upcoming reset in the workload of a graphics pipeline
336 * bound to this session, or the imminent start of a new workload. The user can specify
337 * whether the reset is expected to affect the CPU, GPU, or both.
338 *
339 * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the
340 * this load tracking.
341 *
342 * @param cpu Indicates if the workload reset is expected to affect the CPU.
343 * @param gpu Indicates if the workload reset is expected to affect the GPU.
344 * @param debugName A required string used to identify this specific hint during
345 * tracing. This debug string will only be held for the duration of the
346 * method, and can be safely discarded after.
347 *
348 * @return 0 on success.
349 * EINVAL if no hints were requested.
350 * EBUSY if the hint was rate limited.
351 * EPIPE if communication with the system service has failed.
352 * ENOTSUP if the hint is not supported.
353 */
354int APerformanceHint_notifyWorkloadReset(
355 APerformanceHintSession* _Nonnull session,
356 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
357
358/**
Matt Buckley4b8c0c62024-11-19 14:36:42 -0800359 * Associates a session with any {@link ASurfaceControl} or {@link ANativeWindow}
360 * instances managed by this session.
361 *
362 * This method is primarily intended for sessions that manage the timing of an entire
363 * graphics pipeline end-to-end, such as those using the
364 * {@link ASessionCreationConfig_setGraphicsPipeline} API. However, any session directly
365 * or indirectly managing a graphics pipeline should still associate themselves with
366 * directly relevant ASurfaceControl or ANativeWindow instances for better optimization.
367 *
368 * To see any benefit from this method, the client must make sure they are updating the framerate
369 * of attached surfaces using methods such as {@link ANativeWindow_setFrameRate}, or by updating
370 * any associated ASurfaceControls with transactions that have {ASurfaceTransaction_setFrameRate}.
371 *
372 * @param session The {@link APerformanceHintSession} instance to update.
373 * @param nativeWindows A pointer to a list of ANativeWindows associated with this session.
374 * nullptr can be passed to indicate there are no associated ANativeWindows.
375 * @param nativeWindowsSize The number of ANativeWindows in the list.
376 * @param surfaceControls A pointer to a list of ASurfaceControls associated with this session.
377 * nullptr can be passed to indicate there are no associated ASurfaceControls.
378 * @param surfaceControlsSize The number of ASurfaceControls in the list.
379 *
380 * @return 0 on success.
381 * EPIPE if communication has failed.
382 * ENOTSUP if unsupported.
383 * EINVAL if invalid or empty arguments passed.
384 */
385
386int APerformanceHint_setNativeSurfaces(APerformanceHintSession* _Nonnull session,
387 ANativeWindow* _Nonnull* _Nullable nativeWindows, int nativeWindowsSize,
388 ASurfaceControl* _Nonnull* _Nullable surfaceControls, int surfaceControlsSize)
389 __INTRODUCED_IN(36);
390
391/**
Peiyong Lin81780c42023-10-08 21:11:26 +0000392 * Creates a new AWorkDuration. When the client finishes using {@link AWorkDuration}, it should
393 * call {@link AWorkDuration_release()} to destroy {@link AWorkDuration} and release all resources
394 * associated with it.
395 *
Andy Yufa8eb012024-10-15 17:00:09 -0700396 * @return AWorkDuration pointer.
Peiyong Lin81780c42023-10-08 21:11:26 +0000397 */
398AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__);
399
400/**
Andy Yufa8eb012024-10-15 17:00:09 -0700401 * Destroys a {@link AWorkDuration} and frees all resources associated with it.
Peiyong Lin81780c42023-10-08 21:11:26 +0000402 *
403 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
404 */
Xiang Wang352ff402024-01-09 13:27:05 -0800405void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration)
406 __INTRODUCED_IN(__ANDROID_API_V__);
Peiyong Lin81780c42023-10-08 21:11:26 +0000407
408/**
409 * Sets the work period start timestamp in nanoseconds.
410 *
411 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
412 * @param workPeriodStartTimestampNanos The work period start timestamp in nanoseconds based on
Matt Buckley81cc0932024-01-18 19:56:31 +0000413 * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000414 */
415void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* _Nonnull aWorkDuration,
416 int64_t workPeriodStartTimestampNanos) __INTRODUCED_IN(__ANDROID_API_V__);
417
418/**
419 * Sets the actual total work duration in nanoseconds.
420 *
421 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000422 * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must
423 * be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000424 */
425void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
426 int64_t actualTotalDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
427
428/**
429 * Sets the actual CPU work duration in nanoseconds.
430 *
431 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000432 * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000433 * greater than or equal to zero. If it is equal to zero, that means the CPU was not
434 * measured.
Peiyong Lin81780c42023-10-08 21:11:26 +0000435 */
436void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
437 int64_t actualCpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
438
439/**
440 * Sets the actual GPU work duration in nanoseconds.
441 *
442 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}.
443 * @param actualGpuDurationNanos The actual GPU work duration in nanoseconds, the number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000444 * greater than or equal to zero. If it is equal to zero, that means the GPU was not
Peiyong Lin81780c42023-10-08 21:11:26 +0000445 * measured.
446 */
447void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
448 int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
449
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000450/**
451 * Return the APerformanceHintSession wrapped by a Java PerformanceHintManager.Session object.
452 *
453 * The Java session maintains ownership over the wrapped native session, so it cannot be
454 * closed using {@link APerformanceHint_closeSession}.
455 *
456 * @param env The Java environment where the PerformanceHintManager.Session lives.
457 * @param sessionObj The Java Session to unwrap.
458 *
459 * @return A pointer to the APerformanceHintManager that backs the Java Session.
460 */
461APerformanceHintSession* _Nonnull APerformanceHint_borrowSessionFromJava(
462 JNIEnv* _Nonnull env, jobject _Nonnull sessionObj) __INTRODUCED_IN(36);
463
Andy Yufa8eb012024-10-15 17:00:09 -0700464/*
465 * Creates a new ASessionCreationConfig.
466 *
467 * When the client finishes using {@link ASessionCreationConfig}, it should
468 * call {@link ASessionCreationConfig_release()} to destroy
469 * {@link ASessionCreationConfig} and release all resources
470 * associated with it.
471 *
472 * @return ASessionCreationConfig pointer.
473 */
474ASessionCreationConfig* _Nonnull ASessionCreationConfig_create()
475 __INTRODUCED_IN(36);
476
477
478/**
479 * Destroys a {@link ASessionCreationConfig} and frees all
480 * resources associated with it.
481 *
482 * @param config The {@link ASessionCreationConfig}
483 * created by calling {@link ASessionCreationConfig_create()}.
484 */
485void ASessionCreationConfig_release(
486 ASessionCreationConfig* _Nonnull config) __INTRODUCED_IN(36);
487
488/**
489 * Sets the tids to be associated with the session to be created.
490 *
491 * @param config The {@link ASessionCreationConfig}
492 * created by calling {@link ASessionCreationConfig_create()}
493 * @param tids The list of tids to be associated with this session. They must be part of
494 * this process' thread group.
495 * @param size The size of the list of tids.
496 *
497 * @return 0 on success.
498 * EINVAL if invalid array pointer or the value of size
499 */
500int ASessionCreationConfig_setTids(
501 ASessionCreationConfig* _Nonnull config,
502 const pid_t* _Nonnull tids, size_t size) __INTRODUCED_IN(36);
503
504/**
505 * Sets the initial target work duration in nanoseconds for the session to be created.
506 *
507 * @param config The {@link ASessionCreationConfig}
508 * created by calling {@link ASessionCreationConfig_create()}.
509 * @param targetWorkDurationNanos The parameter to specify a target duration
510 * in nanoseconds for the new session; this value must be positive to use
511 * the work duration API.
512 *
513 * @return 0 on success.
514 * ENOTSUP if unsupported
515 * EINVAL if invalid value
516 */
517int ASessionCreationConfig_setTargetWorkDurationNanos(
518 ASessionCreationConfig* _Nonnull config,
519 int64_t targetWorkDurationNanos) __INTRODUCED_IN(36);
520
521/**
522 * Sets whether power efficiency mode will be enabled for the session.
523 * This tells the session that these threads can be
524 * safely scheduled to prefer power efficiency over performance.
525 *
526 * @param config The {@link ASessionCreationConfig}
527 * created by calling {@link ASessionCreationConfig_create()}.
528 * @param enabled Whether power efficiency mode will be enabled.
529 *
530 * @return 0 on success.
531 * ENOTSUP if unsupported
532 * EINVAL if invalid pointer to creation config
533 */
534int ASessionCreationConfig_setPreferPowerEfficiency(
535 ASessionCreationConfig* _Nonnull config, bool enabled) __INTRODUCED_IN(36);
536
537/**
538 * Sessions setting this hint are expected to time the critical path of
539 * graphics pipeline from end to end, with the total work duration
540 * representing the time from the start of frame production until the
541 * buffer is fully finished drawing.
542 *
543 * It should include any threads on the critical path of that pipeline,
544 * up to a limit accessible from {@link getMaxGraphicsPipelineThreadsCount()}.
545 *
546 * @param config The {@link ASessionCreationConfig}
547 * created by calling {@link ASessionCreationConfig_create()}.
548 * @param enabled Whether this session manages a graphics pipeline's critical path.
549 *
550 * @return 0 on success.
551 * ENOTSUP if unsupported
552 * EINVAL if invalid pointer to creation config or maximum threads for graphics
553 pipeline is reached.
554 */
555int ASessionCreationConfig_setGraphicsPipeline(
Andy Yud54e5892024-11-21 09:46:22 -0800556 ASessionCreationConfig* _Nonnull config, bool enabled) __INTRODUCED_IN(36);
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000557
Matt Buckley4b8c0c62024-11-19 14:36:42 -0800558/**
559 * Associates a session with any {@link ASurfaceControl} or {@link ANativeWindow}
560 * instances managed by this session. See {@link APerformanceHint_setNativeSurfaces}
561 * for more details.
562 *
563 * @param config The {@link ASessionCreationConfig}
564 * created by calling {@link ASessionCreationConfig_create()}.
565 * @param nativeWindows A pointer to a list of ANativeWindows associated with this session.
566 * nullptr can be passed to indicate there are no associated ANativeWindows.
567 * @param nativeWindowsSize The number of ANativeWindows in the list.
568 * @param surfaceControls A pointer to a list of ASurfaceControls associated with this session.
569 * nullptr can be passed to indicate there are no associated ASurfaceControls.
570 * @param surfaceControlsSize The number of ASurfaceControls in the list.
571 *
572 * @return 0 on success.
573 * ENOTSUP if unsupported.
574 * EINVAL if invalid or empty arguments passed.
575 */
576int ASessionCreationConfig_setNativeSurfaces(
577 ASessionCreationConfig* _Nonnull config,
578 ANativeWindow* _Nonnull* _Nullable nativeWindows, int nativeWindowsSize,
579 ASurfaceControl* _Nonnull* _Nullable surfaceControls, int surfaceControlsSize)
580 __INTRODUCED_IN(36);
581
582/**
583 * Enable automatic timing mode for sessions using the GRAPHICS_PIPELINE API with an attached
584 * surface. In this mode, sessions do not need to report actual durations and only need
585 * to keep their thread list up-to-date, set a native surface, call
586 * {@link ASessionCreationConfig_setGraphicsPipeline()} to signal that the session is in
587 * "graphics pipeline" mode, and then set whether automatic timing is desired for the
588 * CPU, GPU, or both, using this method.
589 *
590 * It is still be beneficial to set an accurate target time, as this may help determine
591 * timing information for some workloads where there is less information available from
592 * the framework, such as games. Additionally, reported CPU durations will be ignored
593 * while automatic CPU timing is enabled, and similarly GPU durations will be ignored
594 * when automatic GPU timing is enabled. When both are enabled, the entire
595 * reportActualWorkDuration call will be ignored, and the session will be managed
596 * completely automatically.
597 *
598 * This mode will not work unless the client makes sure they are updating the framerate
599 * of attached surfaces with methods such as {@link ANativeWindow_setFrameRate}, or updating
600 * any associated ASurfaceControls with transactions that have {ASurfaceTransaction_setFrameRate}.
601 *
602 * @param config The {@link ASessionCreationConfig}
603 * created by calling {@link ASessionCreationConfig_create()}.
604 * @param cpu Whether to enable automatic timing for the CPU for this session.
605 * @param gpu Whether to enable automatic timing for the GPU for this session.
606 *
607 * @return 0 on success.
608 * ENOTSUP if unsupported.
609 */
610int ASessionCreationConfig_setUseAutoTiming(
611 ASessionCreationConfig* _Nonnull config,
612 bool cpu, bool gpu)
613 __INTRODUCED_IN(36);
614
Bo Liu406c8ab2021-11-10 19:20:40 -0500615__END_DECLS
616
617#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H
Matt Buckleycc146422023-06-28 19:14:02 +0000618
Peiyong Lin81780c42023-10-08 21:11:26 +0000619/** @} */