blob: ca86c2730ff488d5881738154bd9519dfa4f0fbf [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
Matt Buckleyfff23902024-11-27 21:54:48 +0000155typedef struct ANativeWindow ANativeWindow;
156typedef struct ASurfaceControl ASurfaceControl;
157
Bo Liu406c8ab2021-11-10 19:20:40 -0500158/**
159 * Acquire an instance of the performance hint manager.
160 *
Matt Buckleycc146422023-06-28 19:14:02 +0000161 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500162 */
Xiang Wang352ff402024-01-09 13:27:05 -0800163APerformanceHintManager* _Nullable APerformanceHint_getManager()
164 __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500165
166/**
167 * Creates a session for the given set of threads and sets their initial target work
168 * duration.
Matt Buckleycc146422023-06-28 19:14:02 +0000169 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500170 * @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 Buckleycc146422023-06-28 19:14:02 +0000172 * 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 Yufa8eb012024-10-15 17:00:09 -0700176 * @return APerformanceHintSession pointer on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500177 */
Peiyong Lin81780c42023-10-08 21:11:26 +0000178APerformanceHintSession* _Nullable APerformanceHint_createSession(
179 APerformanceHintManager* _Nonnull manager,
180 const int32_t* _Nonnull threadIds, size_t size,
Bo Liu406c8ab2021-11-10 19:20:40 -0500181 int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
182
183/**
Andy Yufa8eb012024-10-15 17:00:09 -0700184 * 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 */
192APerformanceHintSession* _Nullable APerformanceHint_createSessionUsingConfig(
193 APerformanceHintManager* _Nonnull manager,
194 ASessionCreationConfig* _Nonnull config)
195 __INTRODUCED_IN(36);
196
197/**
Bo Liu406c8ab2021-11-10 19:20:40 -0500198 * 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 */
203int64_t APerformanceHint_getPreferredUpdateRateNanos(
Peiyong Lin81780c42023-10-08 21:11:26 +0000204 APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500205
206/**
Andy Yufa8eb012024-10-15 17:00:09 -0700207 * 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 Liu406c8ab2021-11-10 19:20:40 -0500216 * Updates this session's target duration for each cycle of work.
217 *
218 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000219 * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive.
220 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500221 * EINVAL if targetDurationNanos is not positive.
222 * EPIPE if communication with the system service has failed.
223 */
224int APerformanceHint_updateTargetWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000225 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500226 int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
227
228/**
229 * Reports the actual duration for the last cycle of work.
230 *
Matt Buckleycc146422023-06-28 19:14:02 +0000231 * 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 Liu406c8ab2021-11-10 19:20:40 -0500233 *
234 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000235 * @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 Liu406c8ab2021-11-10 19:20:40 -0500238 * EINVAL if actualDurationNanos is not positive.
239 * EPIPE if communication with the system service has failed.
240 */
241int APerformanceHint_reportActualWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000242 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500243 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 Buckleyfeb30df2024-11-17 01:56:15 +0000249 * This cannot be used to close a Java PerformanceHintManager.Session, as its
250 * lifecycle is tied to the object in the SDK.
251 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500252 * @param session The performance hint session instance to release.
253 */
254void APerformanceHint_closeSession(
Peiyong Lin81780c42023-10-08 21:11:26 +0000255 APerformanceHintSession* _Nonnull session) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500256
Peiyong Linf9c984f2022-11-11 18:28:20 +0000257/**
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 Buckleycc146422023-06-28 19:14:02 +0000261 * @param session The performance hint session instance to update.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000262 * @param threadIds The list of threads to be associated with this session. They must be part of
263 * this app's thread group.
Matt Buckleycc146422023-06-28 19:14:02 +0000264 * @param size The size of the list of threadIds.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000265 * @return 0 on success.
Matt Buckleycc146422023-06-28 19:14:02 +0000266 * 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 Linf9c984f2022-11-11 18:28:20 +0000268 * EPIPE if communication with the system service has failed.
Xiang Wangf6d21b52023-07-25 17:34:01 -0700269 * EPERM if any thread id doesn't belong to the application.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000270 */
271int APerformanceHint_setThreads(
Peiyong Lin81780c42023-10-08 21:11:26 +0000272 APerformanceHintSession* _Nonnull session,
273 const pid_t* _Nonnull threadIds,
Peiyong Linf9c984f2022-11-11 18:28:20 +0000274 size_t size) __INTRODUCED_IN(__ANDROID_API_U__);
275
Matt Buckleycc146422023-06-28 19:14:02 +0000276/**
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 */
285int APerformanceHint_setPreferPowerEfficiency(
Peiyong Lin81780c42023-10-08 21:11:26 +0000286 APerformanceHintSession* _Nonnull session,
Matt Buckleycc146422023-06-28 19:14:02 +0000287 bool enabled) __INTRODUCED_IN(__ANDROID_API_V__);
288
Peiyong Lin81780c42023-10-08 21:11:26 +0000289/**
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 Buckley0bbd1772024-01-31 22:10:42 +0000299 * The work period start timestamp and actual total duration must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000300 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000301 * 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 Lin81780c42023-10-08 21:11:26 +0000304 *
305 * @return 0 on success.
Matt Buckley81cc0932024-01-18 19:56:31 +0000306 * EINVAL if any duration is an invalid number.
Peiyong Lin81780c42023-10-08 21:11:26 +0000307 * EPIPE if communication with the system service has failed.
308 */
309int APerformanceHint_reportActualWorkDuration2(
310 APerformanceHintSession* _Nonnull session,
311 AWorkDuration* _Nonnull workDuration) __INTRODUCED_IN(__ANDROID_API_V__);
312
313/**
Matt Buckley122a1172024-10-21 12:16:36 -0700314 * 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 */
333int 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 */
357int APerformanceHint_notifyWorkloadReset(
358 APerformanceHintSession* _Nonnull session,
359 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
360
361/**
Matt Buckley4b8c0c62024-11-19 14:36:42 -0800362 * 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
389int 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 Lin81780c42023-10-08 21:11:26 +0000395 * 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 Yufa8eb012024-10-15 17:00:09 -0700399 * @return AWorkDuration pointer.
Peiyong Lin81780c42023-10-08 21:11:26 +0000400 */
401AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__);
402
403/**
Andy Yufa8eb012024-10-15 17:00:09 -0700404 * Destroys a {@link AWorkDuration} and frees all resources associated with it.
Peiyong Lin81780c42023-10-08 21:11:26 +0000405 *
406 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
407 */
Xiang Wang352ff402024-01-09 13:27:05 -0800408void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration)
409 __INTRODUCED_IN(__ANDROID_API_V__);
Peiyong Lin81780c42023-10-08 21:11:26 +0000410
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 Buckley81cc0932024-01-18 19:56:31 +0000416 * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000417 */
418void 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 Buckley81cc0932024-01-18 19:56:31 +0000425 * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must
426 * be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000427 */
428void 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 Buckley81cc0932024-01-18 19:56:31 +0000435 * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000436 * greater than or equal to zero. If it is equal to zero, that means the CPU was not
437 * measured.
Peiyong Lin81780c42023-10-08 21:11:26 +0000438 */
439void 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 Buckley0bbd1772024-01-31 22:10:42 +0000447 * 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 +0000448 * measured.
449 */
450void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
451 int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
452
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000453/**
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 */
464APerformanceHintSession* _Nonnull APerformanceHint_borrowSessionFromJava(
465 JNIEnv* _Nonnull env, jobject _Nonnull sessionObj) __INTRODUCED_IN(36);
466
Andy Yufa8eb012024-10-15 17:00:09 -0700467/*
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 */
477ASessionCreationConfig* _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 */
488void 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 */
503int 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 */
520int 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 */
537int 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 */
558int ASessionCreationConfig_setGraphicsPipeline(
Andy Yud54e5892024-11-21 09:46:22 -0800559 ASessionCreationConfig* _Nonnull config, bool enabled) __INTRODUCED_IN(36);
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000560
Matt Buckley4b8c0c62024-11-19 14:36:42 -0800561/**
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 */
579int 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 */
613int ASessionCreationConfig_setUseAutoTiming(
614 ASessionCreationConfig* _Nonnull config,
615 bool cpu, bool gpu)
616 __INTRODUCED_IN(36);
617
Bo Liu406c8ab2021-11-10 19:20:40 -0500618__END_DECLS
619
620#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H
Matt Buckleycc146422023-06-28 19:14:02 +0000621
Peiyong Lin81780c42023-10-08 21:11:26 +0000622/** @} */