Introduction to the Power HAL
The Power Hardware Abstraction Layer (HAL) is the mechanism Android uses to translate high-level system states and application requirements into concrete hardware power configurations. It allows the framework to explicitly hint to the underlying kernel drivers about the performance needs of the current workload.
The IPower HAL Interface
The IPower AIDL interface facilitates communication between PowerManagerService and the vendor's power management firmware.
Core Capabilities
- Interactive State: Notifying the HAL when the device becomes interactive (screen on) or non-interactive (screen off), allowing for base-level CPU governor adjustments.
- Power Hints: Sending specific, context-aware signals (hints) to temporarily boost or limit performance.
- Feature Control: Enabling or disabling specific power-saving features like tap-to-wake.
// hardware/interfaces/power/aidl/android/hardware/power/IPower.aidl
package android.hardware.power;
@VintfStability
interface IPower {
void setMode(in Mode type, in boolean enabled);
boolean isModeSupported(in Mode type);
void setBoost(in Boost type, in int durationMs);
boolean isBoostSupported(in Boost type);
}
Power Hints and Boosts
Instead of the framework trying to manage CPU frequencies directly, it sends semantic hints. The vendor HAL interprets these hints based on the specific SoC architecture.
Key Power Hints
- LAUNCH: Sent when an application is starting. The HAL typically responds by boosting CPU and IO frequencies to maximum for a short duration, ensuring the app loads quickly.
- INTERACTION: Sent when the user touches the screen. The HAL provides a slight, short CPU boost to ensure UI rendering is perfectly smooth and touch latency is minimized.
- VIDEO_ENCODE / VIDEO_DECODE: Indicates heavy multimedia processing. The HAL might shift workloads to specific DSPs or adjust CPU governors for sustained, stable throughput rather than bursty performance.
- LOW_POWER: Sent when Battery Saver mode is enabled. The HAL restricts maximum CPU frequencies and disables non-essential hardware blocks.
// Example: The framework sending an interaction hint internally
// frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
private void userActivityNoUpdateLocked(long eventTime, int event, int flags, int uid) {
// ...
if (event == PowerManager.USER_ACTIVITY_EVENT_TOUCH) {
mPowerHalWrapper.setBoost(Boost.INTERACTION, 0); // 0 means default duration
}
// ...
}
Adaptive Battery Hints
Modern Android versions incorporate machine learning to predict user behavior via the Adaptive Battery feature. The framework can use the Power HAL to inform the hardware about these predictions.
For example, if the system predicts the user won't interact with the device for the next two hours, it might send a specific power mode hint, allowing the HAL to enter deeper sleep states earlier than the standard timeout would dictate.
PowerAdvisor in SurfaceFlinger
The graphics pipeline is highly sensitive to power management. If the CPU or GPU is throttled too aggressively, the device will drop frames.
SurfaceFlinger, the system's display compositor, utilizes a component called PowerAdvisor. PowerAdvisor acts as a smart mediator between the graphics pipeline and the Power HAL.
- Deadline Awareness: PowerAdvisor knows exactly how much time is left before the next vsync deadline.
- Dynamic Adjustments: If rendering a frame is taking too long (approaching the deadline), PowerAdvisor sends an urgent hint to the Power HAL to boost GPU clocks immediately, preventing a dropped frame.
- Efficiency: Conversely, if frames are rendering very quickly, PowerAdvisor hints the HAL to lower clock speeds, saving battery without impacting visible performance.
This tight integration between the display compositor and the Power HAL is crucial for maintaining the smooth, 60fps (or 120fps) UI that modern Android users expect while maximizing battery efficiency.