blob: eed6b3339fd9632ac435aac00de68c308e95ad7f [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/**
Matt Buckley2e7c8952022-09-21 07:34:10 +000091 * Hints for the session used by {@link APerformanceHint_sendHint} to signal upcoming changes
92 * in the mode or workload.
93 */
94enum SessionHint {
95 /**
96 * This hint indicates a sudden increase in CPU workload intensity. It means
97 * that this hint session needs extra CPU resources immediately to meet the
98 * target duration for the current work cycle.
99 */
100 CPU_LOAD_UP = 0,
101 /**
102 * This hint indicates a decrease in CPU workload intensity. It means that
103 * this hint session can reduce CPU resources and still meet the target duration.
104 */
105 CPU_LOAD_DOWN = 1,
106 /*
107 * This hint indicates an upcoming CPU workload that is completely changed and
108 * unknown. It means that the hint session should reset CPU resources to a known
109 * baseline to prepare for an arbitrary load, and must wake up if inactive.
110 */
111 CPU_LOAD_RESET = 2,
112 /*
113 * This hint indicates that the most recent CPU workload is resuming after a
114 * period of inactivity. It means that the hint session should allocate similar
115 * CPU resources to what was used previously, and must wake up if inactive.
116 */
117 CPU_LOAD_RESUME = 3,
118};
119
120/**
Bo Liu406c8ab2021-11-10 19:20:40 -0500121 * Acquire an instance of the performance hint manager.
122 *
123 * @return manager instance on success, nullptr on failure.
124 */
125APerformanceHintManager* APerformanceHint_getManager() __INTRODUCED_IN(__ANDROID_API_T__);
126
127/**
128 * Creates a session for the given set of threads and sets their initial target work
129 * duration.
130 * @param manager The performance hint manager instance.
131 * @param threadIds The list of threads to be associated with this session. They must be part of
132 * this app's thread group.
133 * @param size the size of threadIds.
134 * @param initialTargetWorkDurationNanos The desired duration in nanoseconds for the new session.
135 * This must be positive.
136 * @return manager instance on success, nullptr on failure.
137 */
138APerformanceHintSession* APerformanceHint_createSession(
139 APerformanceHintManager* manager,
140 const int32_t* threadIds, size_t size,
141 int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
142
143/**
144 * Get preferred update rate information for this device.
145 *
146 * @param manager The performance hint manager instance.
147 * @return the preferred update rate supported by device software.
148 */
149int64_t APerformanceHint_getPreferredUpdateRateNanos(
150 APerformanceHintManager* manager) __INTRODUCED_IN(__ANDROID_API_T__);
151
152/**
153 * Updates this session's target duration for each cycle of work.
154 *
155 * @param session The performance hint session instance to update.
156 * @param targetDurationNanos the new desired duration in nanoseconds. This must be positive.
157 * @return 0 on success
158 * EINVAL if targetDurationNanos is not positive.
159 * EPIPE if communication with the system service has failed.
160 */
161int APerformanceHint_updateTargetWorkDuration(
162 APerformanceHintSession* session,
163 int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
164
165/**
166 * Reports the actual duration for the last cycle of work.
167 *
168 * <p>The system will attempt to adjust the core placement of the threads within the thread
169 * group and/or the frequency of the core on which they are run to bring the actual duration
170 * close to the target duration.</p>
171 *
172 * @param session The performance hint session instance to update.
173 * @param actualDurationNanos how long the thread group took to complete its last task in
174 * nanoseconds. This must be positive.
175 * @return 0 on success
176 * EINVAL if actualDurationNanos is not positive.
177 * EPIPE if communication with the system service has failed.
178 */
179int APerformanceHint_reportActualWorkDuration(
180 APerformanceHintSession* session,
181 int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
182
183/**
184 * Release the performance hint manager pointer acquired via
185 * {@link APerformanceHint_createSession}.
186 *
187 * @param session The performance hint session instance to release.
188 */
189void APerformanceHint_closeSession(
190 APerformanceHintSession* session) __INTRODUCED_IN(__ANDROID_API_T__);
191
Matt Buckley2e7c8952022-09-21 07:34:10 +0000192/**
193 * Sends performance hints to inform the hint session of changes in the workload.
194 *
195 * @param session The performance hint session instance to update.
196 * @param hint The hint to send to the session.
197 * @return 0 on success
198 * EPIPE if communication with the system service has failed.
199 */
200int APerformanceHint_sendHint(
201 APerformanceHintSession* session, int hint) __INTRODUCED_IN(__ANDROID_API_U__);
202
Bo Liu406c8ab2021-11-10 19:20:40 -0500203__END_DECLS
204
205#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H