Skip to content

Commit 18ceff0

Browse files
author
Android Build Coastguard Worker
committed
Snap for 12742097 from 8a19587 to 25Q1-release
Change-Id: I5508363131dd665e9f7c1d21a5dd87c2cab1c73b
2 parents e3918bd + 8a19587 commit 18ceff0

25 files changed

Lines changed: 302 additions & 169 deletions

include/android/thermal.h

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,45 +140,47 @@ void AThermal_releaseManager(AThermalManager* _Nonnull manager) __INTRODUCED_IN(
140140
* Available since API level 30.
141141
*
142142
* @param manager The manager instance to use to query the thermal status.
143-
* Acquired via {@link AThermal_acquireManager}.
143+
* Acquired via {@link AThermal_acquireManager}.
144144
*
145145
* @return current thermal status, ATHERMAL_STATUS_ERROR on failure.
146146
*/
147147
AThermalStatus
148-
AThermal_getCurrentThermalStatus(AThermalManager* _Nonnull manager) __INTRODUCED_IN(30);
148+
AThermal_getCurrentThermalStatus(AThermalManager *_Nonnull manager) __INTRODUCED_IN(30);
149149

150150
/**
151-
* Register the thermal status listener for thermal status change.
151+
* Register a thermal status listener for thermal status change.
152152
*
153153
* Available since API level 30.
154154
*
155155
* @param manager The manager instance to use to register.
156-
* Acquired via {@link AThermal_acquireManager}.
157-
* @param callback The callback function to be called when thermal status updated.
156+
* Acquired via {@link AThermal_acquireManager}.
157+
* @param callback The callback function to be called on system binder thread pool when thermal
158+
* status updated.
158159
* @param data The data pointer to be passed when callback is called.
159160
*
160161
* @return 0 on success
161162
* EINVAL if the listener and data pointer were previously added and not removed.
162-
* EPERM if the required permission is not held.
163-
* EPIPE if communication with the system service has failed.
163+
* EPIPE if communication with the system service has failed, the listener will not get
164+
* removed and this call should be retried
164165
*/
165-
int AThermal_registerThermalStatusListener(AThermalManager* _Nonnull manager,
166+
int AThermal_registerThermalStatusListener(AThermalManager *_Nonnull manager,
166167
AThermal_StatusCallback _Nullable callback,
167168
void* _Nullable data) __INTRODUCED_IN(30);
168169

169170
/**
170-
* Unregister the thermal status listener previously resgistered.
171+
* Unregister a thermal status listener previously registered.
172+
*
173+
* No subsequent invocations of the callback will occur after this function returns successfully.
171174
*
172175
* Available since API level 30.
173176
*
174177
* @param manager The manager instance to use to unregister.
175-
* Acquired via {@link AThermal_acquireManager}.
176-
* @param callback The callback function to be called when thermal status updated.
178+
* Acquired via {@link AThermal_acquireManager}.
179+
* @param callback The callback function that was previously registered.
177180
* @param data The data pointer to be passed when callback is called.
178181
*
179182
* @return 0 on success
180183
* EINVAL if the listener and data pointer were not previously added.
181-
* EPERM if the required permission is not held.
182184
* EPIPE if communication with the system service has failed.
183185
*/
184186
int AThermal_unregisterThermalStatusListener(AThermalManager* _Nonnull manager,
@@ -254,32 +256,38 @@ typedef struct AThermalHeadroomThreshold AThermalHeadroomThreshold;
254256
* The headroom threshold is used to interpret the possible thermal throttling status based on
255257
* the headroom prediction. For example, if the headroom threshold for
256258
* {@link ATHERMAL_STATUS_LIGHT} is 0.7, and a headroom prediction in 10s returns 0.75
257-
* (or {@code AThermal_getThermalHeadroom(10)=0.75}), one can expect that in 10 seconds the system
259+
* (or `AThermal_getThermalHeadroom(10)=0.75}`, one can expect that in 10 seconds the system
258260
* could be in lightly throttled state if the workload remains the same. The app can consider
259261
* taking actions according to the nearest throttling status the difference between the headroom and
260262
* the threshold.
261263
* <p>
262264
* For new devices it's guaranteed to have a single sensor, but for older devices with multiple
263265
* sensors reporting different threshold values, the minimum threshold is taken to be conservative
264266
* on predictions. Thus, when reading real-time headroom, it's not guaranteed that a real-time value
265-
* of 0.75 (or {@code AThermal_getThermalHeadroom(0)}=0.75) exceeding the threshold of 0.7 above
267+
* of 0.75 (or `AThermal_getThermalHeadroom(0)=0.75`) exceeding the threshold of 0.7 above
266268
* will always come with lightly throttled state
267-
* (or {@code AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_LIGHT}) but it can be lower
268-
* (or {@code AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_NONE}).
269+
* (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_LIGHT`) but it can be lower
270+
* (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_NONE`).
269271
* While it's always guaranteed that the device won't be throttled heavier than the unmet
270272
* threshold's state, so a real-time headroom of 0.75 will never come with
271273
* {@link #ATHERMAL_STATUS_MODERATE} but always lower, and 0.65 will never come with
272274
* {@link ATHERMAL_STATUS_LIGHT} but {@link #ATHERMAL_STATUS_NONE}.
273275
* <p>
274-
* The returned list of thresholds is cached on first successful query and owned by the thermal
275-
* manager, which will not change between calls to this function. The caller should only need to
276-
* free the manager with {@link AThermal_releaseManager}.
276+
* Starting in Android 16, this polling API may return different results when called depending on
277+
* the device. The new headroom listener API {@link #AThermal_HeadroomCallback} can be used to
278+
* detect headroom thresholds changes.
279+
* <p>
280+
* Before API level 36 the returned list of thresholds is cached on first successful query and owned
281+
* by the thermal manager, which will not change between calls to this function. The caller should
282+
* only need to free the manager with {@link AThermal_releaseManager}.
283+
* <p>
277284
*
278285
* @param manager The manager instance to use.
279286
* Acquired via {@link AThermal_acquireManager}.
280287
* @param outThresholds non-null output pointer to null AThermalHeadroomThreshold pointer, which
281-
* will be set to the cached array of thresholds if thermal thresholds are supported
282-
* by the system or device, otherwise nullptr or unmodified.
288+
* will be set to a new array of thresholds if thermal thresholds are supported
289+
* by the system or device, otherwise nullptr or unmodified. The client should
290+
* clean up the thresholds by array-deleting the threshold pointer.
283291
* @param size non-null output pointer whose value will be set to the size of the threshold array
284292
* or 0 if it's not supported.
285293
* @return 0 on success
@@ -292,6 +300,71 @@ int AThermal_getThermalHeadroomThresholds(AThermalManager* _Nonnull manager,
292300
* _Nullable outThresholds,
293301
size_t* _Nonnull size) __INTRODUCED_IN(35);
294302

303+
/**
304+
* Prototype of the function that is called when thermal headroom or thresholds changes.
305+
* It's passed the updated thermal headroom and thresholds as parameters, as well as the
306+
* pointer provided by the client that registered a callback.
307+
*
308+
* @param data The data pointer to be passed when callback is called.
309+
* @param headroom The current non-negative normalized headroom value, also see
310+
* {@link AThermal_getThermalHeadroom}.
311+
* @param forecastHeadroom The forecasted non-negative normalized headroom value, also see
312+
* {@link AThermal_getThermalHeadroom}.
313+
* @param forecastSeconds The seconds used for the forecast by the system.
314+
* @param thresholds The current headroom thresholds. The thresholds pointer will be a constant
315+
* shared across all callbacks registered from the same process, and it will be
316+
* destroyed after all the callbacks are finished. If the client intents to
317+
* persist the values, it should make a copy of it during the callback.
318+
* @param thresholdsCount The count of thresholds.
319+
*/
320+
typedef void (*AThermal_HeadroomCallback)(void *_Nullable data,
321+
float headroom,
322+
float forecastHeadroom,
323+
int forecastSeconds,
324+
const AThermalHeadroomThreshold* _Nullable thresholds,
325+
size_t thresholdsCount);
326+
327+
/**
328+
* Register a thermal headroom listener for thermal headroom or thresholds change.
329+
*
330+
* Available since API level 36.
331+
*
332+
* @param manager The manager instance to use to register.
333+
* Acquired via {@link AThermal_acquireManager}.
334+
* @param callback The callback function to be called on system binder thread pool when thermal
335+
* headroom or thresholds update.
336+
* @param data The data pointer to be passed when callback is called.
337+
*
338+
* @return 0 on success
339+
* EINVAL if the listener and data pointer were previously added and not removed.
340+
* EPIPE if communication with the system service has failed.
341+
*/
342+
int AThermal_registerThermalHeadroomListener(AThermalManager* _Nonnull manager,
343+
AThermal_HeadroomCallback _Nullable callback,
344+
void* _Nullable data) __INTRODUCED_IN(36);
345+
346+
/**
347+
* Unregister a thermal headroom listener previously registered.
348+
*
349+
* No subsequent invocations of the callback will occur after this function returns successfully.
350+
*
351+
* Available since API level 36.
352+
*
353+
* @param manager The manager instance to use to unregister.
354+
* Acquired via {@link AThermal_acquireManager}.
355+
* @param callback The callback function that was previously registered.
356+
* @param data The data pointer that was previously registered.
357+
*
358+
* @return 0 on success
359+
* EINVAL if the listener and data pointer were not previously added.
360+
* EPIPE if communication with the system service has failed, the listener will not get
361+
* removed and this call should be retried
362+
*/
363+
364+
int AThermal_unregisterThermalHeadroomListener(AThermalManager* _Nonnull manager,
365+
AThermal_HeadroomCallback _Nullable callback,
366+
void* _Nullable data) __INTRODUCED_IN(36);
367+
295368
#ifdef __cplusplus
296369
}
297370
#endif

libs/binder/TEST_MAPPING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
{
7070
"name": "CtsOsTestCases_ParcelAndBinderTests"
7171
},
72+
{
73+
"name": "FrameworksCoreTests_all_binder"
74+
},
7275
{
7376
"name": "libbinder_rs-internal_test"
7477
},

libs/gui/FrameRateUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool ValidateFrameRate(float frameRate, int8_t compatibility, int8_t changeFrame
4242

4343
if (compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT &&
4444
compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE &&
45-
compatibility != ANATIVEWINDOW_FRAME_RATE_GTE &&
45+
compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE &&
4646
(!privileged ||
4747
(compatibility != ANATIVEWINDOW_FRAME_RATE_EXACT &&
4848
compatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE))) {

libs/gui/tests/FrameRateUtilsTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ TEST(FrameRateUtilsTest, ValidateFrameRate) {
3434
ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS, ""));
3535
EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
3636
ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
37-
EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_GTE,
37+
EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE,
3838
ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
3939

4040
// Privileged APIs.

libs/nativewindow/include/android/native_window.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ enum ANativeWindow_FrameRateCompatibility {
243243
* There are no inherent restrictions on the frame rate of this window. When
244244
* the system selects a frame rate other than what the app requested, the
245245
* app will be able to run at the system frame rate without requiring pull
246-
* down. This value should be used when displaying game content, UIs, and
247-
* anything that isn't video.
246+
* down. This value should be used when displaying game content.
248247
*/
249248
ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT = 0,
250249
/**
@@ -256,7 +255,14 @@ enum ANativeWindow_FrameRateCompatibility {
256255
* stuttering) than it would be if the system had chosen the app's requested
257256
* frame rate. This value should be used for video content.
258257
*/
259-
ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE = 1
258+
ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE = 1,
259+
260+
/**
261+
* The window requests a frame rate that is greater than or equal to the specified frame rate.
262+
* This value should be used for UIs, animations, scrolling, and anything that is not a game
263+
* or video.
264+
*/
265+
ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE = 2
260266
};
261267

262268
/**

libs/nativewindow/include/system/window.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,7 @@ enum {
10601060
/**
10611061
* This surface will vote for the minimum refresh rate.
10621062
*/
1063-
ANATIVEWINDOW_FRAME_RATE_MIN,
1064-
1065-
/**
1066-
* The surface requests a frame rate that is greater than or equal to `frameRate`.
1067-
*/
1068-
ANATIVEWINDOW_FRAME_RATE_GTE
1063+
ANATIVEWINDOW_FRAME_RATE_MIN
10691064
};
10701065

10711066
/*

libs/nativewindow/rust/src/lib.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use binder::{
3030
StatusCode,
3131
};
3232
use ffi::{
33-
AHardwareBuffer, AHardwareBuffer_Desc, AHardwareBuffer_readFromParcel,
34-
AHardwareBuffer_writeToParcel, ARect,
33+
AHardwareBuffer, AHardwareBuffer_Desc, AHardwareBuffer_Plane, AHardwareBuffer_Planes,
34+
AHardwareBuffer_readFromParcel, AHardwareBuffer_writeToParcel, ARect,
3535
};
3636
use std::ffi::c_void;
3737
use std::fmt::{self, Debug, Formatter};
@@ -313,6 +313,57 @@ impl HardwareBuffer {
313313
})
314314
}
315315

316+
/// Lock a potentially multi-planar hardware buffer for direct CPU access.
317+
///
318+
/// # Safety
319+
///
320+
/// - If `fence` is `None`, the caller must ensure that all writes to the buffer have completed
321+
/// before calling this function.
322+
/// - If the buffer has `AHARDWAREBUFFER_FORMAT_BLOB`, multiple threads or process may lock the
323+
/// buffer simultaneously, but the caller must ensure that they don't access it simultaneously
324+
/// and break Rust's aliasing rules, like any other shared memory.
325+
/// - Otherwise if `usage` includes `AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY` or
326+
/// `AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN`, the caller must ensure that no other threads or
327+
/// processes lock the buffer simultaneously for any usage.
328+
/// - Otherwise, the caller must ensure that no other threads lock the buffer for writing
329+
/// simultaneously.
330+
/// - If `rect` is not `None`, the caller must not modify the buffer outside of that rectangle.
331+
pub unsafe fn lock_planes<'a>(
332+
&'a self,
333+
usage: AHardwareBuffer_UsageFlags,
334+
fence: Option<BorrowedFd>,
335+
rect: Option<&ARect>,
336+
) -> Result<Vec<PlaneGuard<'a>>, StatusCode> {
337+
let fence = if let Some(fence) = fence { fence.as_raw_fd() } else { -1 };
338+
let rect = rect.map(ptr::from_ref).unwrap_or(null());
339+
let mut planes = AHardwareBuffer_Planes {
340+
planeCount: 0,
341+
planes: [const { AHardwareBuffer_Plane { data: null_mut(), pixelStride: 0, rowStride: 0 } };
342+
4],
343+
};
344+
345+
// SAFETY: The `AHardwareBuffer` pointer we wrap is always valid, and the various out
346+
// pointers are valid because they come from references. Our caller promises that writes have
347+
// completed and there will be no simultaneous read/write locks.
348+
let status = unsafe {
349+
ffi::AHardwareBuffer_lockPlanes(self.0.as_ptr(), usage.0, fence, rect, &mut planes)
350+
};
351+
status_result(status)?;
352+
let plane_count = planes.planeCount.try_into().unwrap();
353+
Ok(planes.planes[..plane_count]
354+
.iter()
355+
.map(|plane| PlaneGuard {
356+
guard: HardwareBufferGuard {
357+
buffer: self,
358+
address: NonNull::new(plane.data)
359+
.expect("AHardwareBuffer_lockAndGetInfo set a null outVirtualAddress"),
360+
},
361+
pixel_stride: plane.pixelStride,
362+
row_stride: plane.rowStride,
363+
})
364+
.collect())
365+
}
366+
316367
/// Locks the hardware buffer for direct CPU access, returning information about the bytes per
317368
/// pixel and stride as well.
318369
///
@@ -510,6 +561,18 @@ pub struct LockedBufferInfo<'a> {
510561
pub stride: u32,
511562
}
512563

564+
/// A guard for a single plane of a locked `HardwareBuffer`, with additional information about the
565+
/// stride.
566+
#[derive(Debug)]
567+
pub struct PlaneGuard<'a> {
568+
/// The locked buffer guard.
569+
pub guard: HardwareBufferGuard<'a>,
570+
/// The stride in bytes between the color channel for one pixel to the next pixel.
571+
pub pixel_stride: u32,
572+
/// The stride in bytes between rows in the buffer.
573+
pub row_stride: u32,
574+
}
575+
513576
#[cfg(test)]
514577
mod test {
515578
use super::*;

0 commit comments

Comments
 (0)