blob: 5fa47f64be946d6475fc22ab504e3bcacf94a3c3 [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
17#ifndef ANDROID_NATIVE_PERFORMANCE_HINT_H
18#define ANDROID_NATIVE_PERFORMANCE_HINT_H
19
20#include <sys/cdefs.h>
21
22/******************************************************************
23 *
24 * IMPORTANT NOTICE:
25 *
26 * This file is part of Android's set of stable system headers
27 * exposed by the Android NDK (Native Development Kit).
28 *
29 * Third-party source AND binary code relies on the definitions
30 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
31 *
32 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
33 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
34 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
35 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
36 */
37
38#include <android/api-level.h>
39#include <stdint.h>
40
41__BEGIN_DECLS
42
43struct APerformanceHintManager;
44struct APerformanceHintSession;
45
46/**
47 * An opaque type representing a handle to a performance hint manager.
48 * It must be released after use.
49 *
50 * <p>To use:<ul>
51 * <li>Obtain the performance hint manager instance by calling
52 * {@link APerformanceHint_getManager} function.</li>
53 * <li>Create an {@link APerformanceHintSession} with
54 * {@link APerformanceHint_createSession}.</li>
55 * <li>Get the preferred update rate in nanoseconds with
56 * {@link APerformanceHint_getPreferredUpdateRateNanos}.</li>
57 */
58typedef struct APerformanceHintManager APerformanceHintManager;
59
60/**
61 * An opaque type representing a handle to a performance hint session.
62 * A session can only be acquired from a {@link APerformanceHintManager}
63 * with {@link APerformanceHint_getPreferredUpdateRateNanos}. It must be
64 * freed with {@link APerformanceHint_closeSession} after use.
65 *
66 * A Session represents a group of threads with an inter-related workload such that hints for
67 * their performance should be considered as a unit. The threads in a given session should be
68 * long-life and not created or destroyed dynamically.
69 *
70 * <p>Each session is expected to have a periodic workload with a target duration for each
71 * cycle. The cycle duration is likely greater than the target work duration to allow other
72 * parts of the pipeline to run within the available budget. For example, a renderer thread may
73 * work at 60hz in order to produce frames at the display's frame but have a target work
74 * duration of only 6ms.</p>
75 *
76 * <p>After each cycle of work, the client is expected to use
77 * {@link APerformanceHint_reportActualWorkDuration} to report the actual time taken to
78 * complete.</p>
79 *
80 * <p>To use:<ul>
81 * <li>Update a sessions target duration for each cycle of work
82 * with {@link APerformanceHint_updateTargetWorkDuration}.</li>
83 * <li>Report the actual duration for the last cycle of work with
84 * {@link APerformanceHint_reportActualWorkDuration}.</li>
85 * <li>Release the session instance with
86 * {@link APerformanceHint_closeSession}.</li></ul></p>
87 */
88typedef struct APerformanceHintSession APerformanceHintSession;
89
90/**
91 * Acquire an instance of the performance hint manager.
92 *
93 * @return manager instance on success, nullptr on failure.
94 */
95APerformanceHintManager* APerformanceHint_getManager() __INTRODUCED_IN(__ANDROID_API_T__);
96
97/**
98 * Creates a session for the given set of threads and sets their initial target work
99 * duration.
100 * @param manager The performance hint manager instance.
101 * @param threadIds The list of threads to be associated with this session. They must be part of
102 * this app's thread group.
103 * @param size the size of threadIds.
104 * @param initialTargetWorkDurationNanos The desired duration in nanoseconds for the new session.
105 * This must be positive.
106 * @return manager instance on success, nullptr on failure.
107 */
108APerformanceHintSession* APerformanceHint_createSession(
109 APerformanceHintManager* manager,
110 const int32_t* threadIds, size_t size,
111 int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
112
113/**
114 * Get preferred update rate information for this device.
115 *
116 * @param manager The performance hint manager instance.
117 * @return the preferred update rate supported by device software.
118 */
119int64_t APerformanceHint_getPreferredUpdateRateNanos(
120 APerformanceHintManager* manager) __INTRODUCED_IN(__ANDROID_API_T__);
121
122/**
123 * Updates this session's target duration for each cycle of work.
124 *
125 * @param session The performance hint session instance to update.
126 * @param targetDurationNanos the new desired duration in nanoseconds. This must be positive.
127 * @return 0 on success
128 * EINVAL if targetDurationNanos is not positive.
129 * EPIPE if communication with the system service has failed.
130 */
131int APerformanceHint_updateTargetWorkDuration(
132 APerformanceHintSession* session,
133 int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
134
135/**
136 * Reports the actual duration for the last cycle of work.
137 *
138 * <p>The system will attempt to adjust the core placement of the threads within the thread
139 * group and/or the frequency of the core on which they are run to bring the actual duration
140 * close to the target duration.</p>
141 *
142 * @param session The performance hint session instance to update.
143 * @param actualDurationNanos how long the thread group took to complete its last task in
144 * nanoseconds. This must be positive.
145 * @return 0 on success
146 * EINVAL if actualDurationNanos is not positive.
147 * EPIPE if communication with the system service has failed.
148 */
149int APerformanceHint_reportActualWorkDuration(
150 APerformanceHintSession* session,
151 int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
152
153/**
154 * Release the performance hint manager pointer acquired via
155 * {@link APerformanceHint_createSession}.
156 *
157 * @param session The performance hint session instance to release.
158 */
159void APerformanceHint_closeSession(
160 APerformanceHintSession* session) __INTRODUCED_IN(__ANDROID_API_T__);
161
162__END_DECLS
163
164#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H