blob: 2b4a5f5f53c9c3de3c371d819d2fda77f77def91 [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 Buckleyfd2b7862024-12-03 17:18:33 -0800362 * Informs the framework of an upcoming one-off expensive frame for a graphics pipeline
363 * bound to this session. This frame will be treated as not representative of the workload as a
364 * whole, and it will be discarded the purposes of load tracking. The user can specify
365 * whether the workload spike is expected to be on the CPU, GPU, or both.
366 *
367 * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the
368 * rate limiter.
369 *
370 * @param cpu Indicates if the workload spike is expected to affect the CPU.
371 * @param gpu Indicates if the workload spike is expected to affect the GPU.
372 * @param debugName A required string used to identify this specific hint during
373 * tracing. This debug string will only be held for the duration of the
374 * method, and can be safely discarded after.
375 *
376 * @return 0 on success.
377 * EINVAL if no hints were requested.
378 * EBUSY if the hint was rate limited.
379 * EPIPE if communication with the system service has failed.
380 * ENOTSUP if the hint is not supported.
381 */
382int APerformanceHint_notifyWorkloadSpike(
383 APerformanceHintSession* _Nonnull session,
384 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
385
386/**
Matt Buckley4b8c0c62024-11-19 14:36:42 -0800387 * Associates a session with any {@link ASurfaceControl} or {@link ANativeWindow}
388 * instances managed by this session.
389 *
390 * This method is primarily intended for sessions that manage the timing of an entire
391 * graphics pipeline end-to-end, such as those using the
392 * {@link ASessionCreationConfig_setGraphicsPipeline} API. However, any session directly
393 * or indirectly managing a graphics pipeline should still associate themselves with
394 * directly relevant ASurfaceControl or ANativeWindow instances for better optimization.
395 *
396 * To see any benefit from this method, the client must make sure they are updating the framerate
397 * of attached surfaces using methods such as {@link ANativeWindow_setFrameRate}, or by updating
398 * any associated ASurfaceControls with transactions that have {ASurfaceTransaction_setFrameRate}.
399 *
400 * @param session The {@link APerformanceHintSession} instance to update.
401 * @param nativeWindows A pointer to a list of ANativeWindows associated with this session.
402 * nullptr can be passed to indicate there are no associated ANativeWindows.
403 * @param nativeWindowsSize The number of ANativeWindows in the list.
404 * @param surfaceControls A pointer to a list of ASurfaceControls associated with this session.
405 * nullptr can be passed to indicate there are no associated ASurfaceControls.
406 * @param surfaceControlsSize The number of ASurfaceControls in the list.
407 *
408 * @return 0 on success.
409 * EPIPE if communication has failed.
410 * ENOTSUP if unsupported.
411 * EINVAL if invalid or empty arguments passed.
412 */
413
414int APerformanceHint_setNativeSurfaces(APerformanceHintSession* _Nonnull session,
415 ANativeWindow* _Nonnull* _Nullable nativeWindows, int nativeWindowsSize,
416 ASurfaceControl* _Nonnull* _Nullable surfaceControls, int surfaceControlsSize)
417 __INTRODUCED_IN(36);
418
419/**
Peiyong Lin81780c42023-10-08 21:11:26 +0000420 * Creates a new AWorkDuration. When the client finishes using {@link AWorkDuration}, it should
421 * call {@link AWorkDuration_release()} to destroy {@link AWorkDuration} and release all resources
422 * associated with it.
423 *
Andy Yufa8eb012024-10-15 17:00:09 -0700424 * @return AWorkDuration pointer.
Peiyong Lin81780c42023-10-08 21:11:26 +0000425 */
426AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__);
427
428/**
Andy Yufa8eb012024-10-15 17:00:09 -0700429 * Destroys a {@link AWorkDuration} and frees all resources associated with it.
Peiyong Lin81780c42023-10-08 21:11:26 +0000430 *
431 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
432 */
Xiang Wang352ff402024-01-09 13:27:05 -0800433void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration)
434 __INTRODUCED_IN(__ANDROID_API_V__);
Peiyong Lin81780c42023-10-08 21:11:26 +0000435
436/**
437 * Sets the work period start timestamp in nanoseconds.
438 *
439 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
440 * @param workPeriodStartTimestampNanos The work period start timestamp in nanoseconds based on
Matt Buckley81cc0932024-01-18 19:56:31 +0000441 * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000442 */
443void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* _Nonnull aWorkDuration,
444 int64_t workPeriodStartTimestampNanos) __INTRODUCED_IN(__ANDROID_API_V__);
445
446/**
447 * Sets the actual total work duration in nanoseconds.
448 *
449 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000450 * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must
451 * be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000452 */
453void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
454 int64_t actualTotalDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
455
456/**
457 * Sets the actual CPU work duration in nanoseconds.
458 *
459 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000460 * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000461 * greater than or equal to zero. If it is equal to zero, that means the CPU was not
462 * measured.
Peiyong Lin81780c42023-10-08 21:11:26 +0000463 */
464void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
465 int64_t actualCpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
466
467/**
468 * Sets the actual GPU work duration in nanoseconds.
469 *
470 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}.
471 * @param actualGpuDurationNanos The actual GPU work duration in nanoseconds, the number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000472 * 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 +0000473 * measured.
474 */
475void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
476 int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
477
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000478/**
479 * Return the APerformanceHintSession wrapped by a Java PerformanceHintManager.Session object.
480 *
481 * The Java session maintains ownership over the wrapped native session, so it cannot be
482 * closed using {@link APerformanceHint_closeSession}.
483 *
484 * @param env The Java environment where the PerformanceHintManager.Session lives.
485 * @param sessionObj The Java Session to unwrap.
486 *
487 * @return A pointer to the APerformanceHintManager that backs the Java Session.
488 */
489APerformanceHintSession* _Nonnull APerformanceHint_borrowSessionFromJava(
490 JNIEnv* _Nonnull env, jobject _Nonnull sessionObj) __INTRODUCED_IN(36);
491
Andy Yufa8eb012024-10-15 17:00:09 -0700492/*
493 * Creates a new ASessionCreationConfig.
494 *
495 * When the client finishes using {@link ASessionCreationConfig}, it should
496 * call {@link ASessionCreationConfig_release()} to destroy
497 * {@link ASessionCreationConfig} and release all resources
498 * associated with it.
499 *
500 * @return ASessionCreationConfig pointer.
501 */
502ASessionCreationConfig* _Nonnull ASessionCreationConfig_create()
503 __INTRODUCED_IN(36);
504
505
506/**
507 * Destroys a {@link ASessionCreationConfig} and frees all
508 * resources associated with it.
509 *
510 * @param config The {@link ASessionCreationConfig}
511 * created by calling {@link ASessionCreationConfig_create()}.
512 */
513void ASessionCreationConfig_release(
514 ASessionCreationConfig* _Nonnull config) __INTRODUCED_IN(36);
515
516/**
517 * Sets the tids to be associated with the session to be created.
518 *
519 * @param config The {@link ASessionCreationConfig}
520 * created by calling {@link ASessionCreationConfig_create()}
521 * @param tids The list of tids to be associated with this session. They must be part of
522 * this process' thread group.
523 * @param size The size of the list of tids.
524 *
525 * @return 0 on success.
526 * EINVAL if invalid array pointer or the value of size
527 */
528int ASessionCreationConfig_setTids(
529 ASessionCreationConfig* _Nonnull config,
530 const pid_t* _Nonnull tids, size_t size) __INTRODUCED_IN(36);
531
532/**
533 * Sets the initial target work duration in nanoseconds for the session to be created.
534 *
535 * @param config The {@link ASessionCreationConfig}
536 * created by calling {@link ASessionCreationConfig_create()}.
537 * @param targetWorkDurationNanos The parameter to specify a target duration
538 * in nanoseconds for the new session; this value must be positive to use
539 * the work duration API.
540 *
541 * @return 0 on success.
542 * ENOTSUP if unsupported
543 * EINVAL if invalid value
544 */
545int ASessionCreationConfig_setTargetWorkDurationNanos(
546 ASessionCreationConfig* _Nonnull config,
547 int64_t targetWorkDurationNanos) __INTRODUCED_IN(36);
548
549/**
550 * Sets whether power efficiency mode will be enabled for the session.
551 * This tells the session that these threads can be
552 * safely scheduled to prefer power efficiency over performance.
553 *
554 * @param config The {@link ASessionCreationConfig}
555 * created by calling {@link ASessionCreationConfig_create()}.
556 * @param enabled Whether power efficiency mode will be enabled.
557 *
558 * @return 0 on success.
559 * ENOTSUP if unsupported
560 * EINVAL if invalid pointer to creation config
561 */
562int ASessionCreationConfig_setPreferPowerEfficiency(
563 ASessionCreationConfig* _Nonnull config, bool enabled) __INTRODUCED_IN(36);
564
565/**
566 * Sessions setting this hint are expected to time the critical path of
567 * graphics pipeline from end to end, with the total work duration
568 * representing the time from the start of frame production until the
569 * buffer is fully finished drawing.
570 *
571 * It should include any threads on the critical path of that pipeline,
572 * up to a limit accessible from {@link getMaxGraphicsPipelineThreadsCount()}.
573 *
574 * @param config The {@link ASessionCreationConfig}
575 * created by calling {@link ASessionCreationConfig_create()}.
576 * @param enabled Whether this session manages a graphics pipeline's critical path.
577 *
578 * @return 0 on success.
579 * ENOTSUP if unsupported
580 * EINVAL if invalid pointer to creation config or maximum threads for graphics
581 pipeline is reached.
582 */
583int ASessionCreationConfig_setGraphicsPipeline(
Andy Yud54e5892024-11-21 09:46:22 -0800584 ASessionCreationConfig* _Nonnull config, bool enabled) __INTRODUCED_IN(36);
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000585
Matt Buckley4b8c0c62024-11-19 14:36:42 -0800586/**
587 * Associates a session with any {@link ASurfaceControl} or {@link ANativeWindow}
588 * instances managed by this session. See {@link APerformanceHint_setNativeSurfaces}
589 * for more details.
590 *
591 * @param config The {@link ASessionCreationConfig}
592 * created by calling {@link ASessionCreationConfig_create()}.
593 * @param nativeWindows A pointer to a list of ANativeWindows associated with this session.
594 * nullptr can be passed to indicate there are no associated ANativeWindows.
595 * @param nativeWindowsSize The number of ANativeWindows in the list.
596 * @param surfaceControls A pointer to a list of ASurfaceControls associated with this session.
597 * nullptr can be passed to indicate there are no associated ASurfaceControls.
598 * @param surfaceControlsSize The number of ASurfaceControls in the list.
599 *
600 * @return 0 on success.
601 * ENOTSUP if unsupported.
602 * EINVAL if invalid or empty arguments passed.
603 */
604int ASessionCreationConfig_setNativeSurfaces(
605 ASessionCreationConfig* _Nonnull config,
606 ANativeWindow* _Nonnull* _Nullable nativeWindows, int nativeWindowsSize,
607 ASurfaceControl* _Nonnull* _Nullable surfaceControls, int surfaceControlsSize)
608 __INTRODUCED_IN(36);
609
610/**
611 * Enable automatic timing mode for sessions using the GRAPHICS_PIPELINE API with an attached
612 * surface. In this mode, sessions do not need to report actual durations and only need
613 * to keep their thread list up-to-date, set a native surface, call
614 * {@link ASessionCreationConfig_setGraphicsPipeline()} to signal that the session is in
615 * "graphics pipeline" mode, and then set whether automatic timing is desired for the
616 * CPU, GPU, or both, using this method.
617 *
618 * It is still be beneficial to set an accurate target time, as this may help determine
619 * timing information for some workloads where there is less information available from
620 * the framework, such as games. Additionally, reported CPU durations will be ignored
621 * while automatic CPU timing is enabled, and similarly GPU durations will be ignored
622 * when automatic GPU timing is enabled. When both are enabled, the entire
623 * reportActualWorkDuration call will be ignored, and the session will be managed
624 * completely automatically.
625 *
626 * This mode will not work unless the client makes sure they are updating the framerate
627 * of attached surfaces with methods such as {@link ANativeWindow_setFrameRate}, or updating
628 * any associated ASurfaceControls with transactions that have {ASurfaceTransaction_setFrameRate}.
629 *
630 * @param config The {@link ASessionCreationConfig}
631 * created by calling {@link ASessionCreationConfig_create()}.
632 * @param cpu Whether to enable automatic timing for the CPU for this session.
633 * @param gpu Whether to enable automatic timing for the GPU for this session.
634 *
635 * @return 0 on success.
636 * ENOTSUP if unsupported.
637 */
638int ASessionCreationConfig_setUseAutoTiming(
639 ASessionCreationConfig* _Nonnull config,
640 bool cpu, bool gpu)
641 __INTRODUCED_IN(36);
642
Bo Liu406c8ab2021-11-10 19:20:40 -0500643__END_DECLS
644
645#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H
Matt Buckleycc146422023-06-28 19:14:02 +0000646
Peiyong Lin81780c42023-10-08 21:11:26 +0000647/** @} */