Android 11(R) Power HAL AIDL簡析 -- 基本介面

Hi,出發了發表於2021-08-26

Android 11(R) Power HAL AIDL將分三篇文章來介紹:

Android 11(R) Power HAL AIDL簡析 -- 基本介面

Android 11(R) Power HAL AIDL簡析 -- Service功能實現

Android 11(R) Power HAL AIDL簡析 -- 應用例項


 

1 前言

Android 11 引入了在 Android 中使用 AIDL 實現 HAL 的功能。這樣就可以在不使用 HIDL 的情況下實現 Android 的部分程式碼。Power HAL模組即可以採用AIDL方式來實現。在Android 11中已經引入這個Power AIDL Interface,原始碼位於:

/hardware/interfaces/power/aidl/

在學習這部分之前,推薦閱讀AIDL的相關知識,特別是Android 10之後引入的對穩定的 Android 介面定義語言 (AIDL) 的支援(stable AIDL),這裡放上幾篇官網連結:


2 Power HAL AIDL介面介紹


2.1 介面定義原始碼的目錄結構

1. 在Androi原始碼目錄/hardware/interfaces/power/aidl/android/hardware/power/下定義了三個aidl檔案,分別為:

IPower.aidl 定義 power hal介面
Mode.aidl 定義各種 power mode
Boost.aidl 定義Boost type

2. aidl_api目錄:對於Stable AIDL,從 Android 11 開始,versions 凍結在 aidl_api/name 下,和AIDL介面版本相關。

3. default目錄:功能實現的一個示例程式碼,實現了一個android.hardware.power-service.example 這個aidl service

4. vts目錄:vts測試用例


2.2 IPower介面介紹

先看看IPower原始碼是如何定義的,interface IPower的原始碼定義如下:

點選檢視IPower程式碼
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.power;

import android.hardware.power.Boost;
import android.hardware.power.Mode;

@VintfStability
interface IPower {
    /**
     * setMode() is called to enable/disable specific hint mode, which
     * may result in adjustment of power/performance parameters of the
     * cpufreq governor and other controls on device side.
     *
     * A particular platform may choose to ignore any mode hint.
     *
     * @param type Mode which is to be enable/disable.
     * @param enabled true to enable, false to disable the mode.
     */
    oneway void setMode(in Mode type, in boolean enabled);

    /**
     * isModeSupported() is called to query if the given mode hint is
     * supported by vendor.
     *
     * @return true if the hint passed is supported on this platform.
     *         If false, setting the mode will have no effect.
     * @param type Mode to be queried
     */
    boolean isModeSupported(in Mode type);

    /**
     * setBoost() indicates the device may need to boost some resources, as the
     * the load is likely to increase before the kernel governors can react.
     * Depending on the boost, it may be appropriate to raise the frequencies of
     * CPU, GPU, memory subsystem, or stop CPU from going into deep sleep state.
     * A particular platform may choose to ignore this hint.
     *
     * @param type Boost type which is to be set with a timeout.
     * @param durationMs The expected duration of the user's interaction, if
     *        known, or 0 if the expected duration is unknown.
     *        a negative value indicates canceling previous boost.
     *        A given platform can choose to boost some time based on durationMs,
     *        and may also pick an appropriate timeout for 0 case.
     */
    oneway void setBoost(in Boost type, in int durationMs);

    /**
     * isBoostSupported() is called to query if the given boost hint is
     * supported by vendor. When returns false, set the boost will have
     * no effect on the platform.
     *
     * @return true if the hint passed is supported on this platform.
     *         If false, setting the boost will have no effect.
     * @param type Boost to be queried
     */
    boolean isBoostSupported(in Boost type);
}

從原始碼中可以看到,IPower的定義還是比較簡單的,只提供了4個介面,從原始碼註釋也很容易理解其作用,下面我們簡單總結一下:

介面名稱 引數 返回值 作用
setMode

Mode type:開啟或關閉的Power Mode

boolean enabled:true表示開啟,false表示關閉

呼叫這個介面可以啟用/禁用特定Power Mode,這可能導致cpufreq調控器或裝置端其他控制元件的power/performance引數調整。
isModeSupported

Mode type:要查詢的Power Mode

true:支援

fasle:不支援

查詢給定的Power Mode是否支援
setBoost

Boost type:需要設定的type

int durationMs:已知情況下的使用者互動的預期時間。值為0表示未知預期時間,負值表示取消上一次的boost

表示裝置可能需要增加一些資源,因為在核心調控器做出反應之前,負載可能會增加。根據增壓情況,可能適合提高CPU、GPU、記憶體子系統的頻率或停止CPU進入深度睡眠狀態.
isBoostSupported Boost type:要查詢的Boost type

true:支援

fasle:不支援

查詢給定的Boost type是否支援

2.3 Power Mode介紹

先看一下AIDL檔案中的定義

點選檢視PowerMode原始碼
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.power;

@VintfStability
@Backing(type="int")
enum Mode {
    /**
     * This mode indicates that the device is to allow wake up when the
     * screen is tapped twice.
     */
    DOUBLE_TAP_TO_WAKE,

    /**
     * This mode indicates Low power mode is activated or not. Low power
     * mode is intended to save battery at the cost of performance.
     */
    LOW_POWER,

    /**
     * This mode indicates Sustained Performance mode is activated or not.
     * Sustained performance mode is intended to provide a consistent level of
     * performance for a prolonged amount of time.
     */
    SUSTAINED_PERFORMANCE,

    /**
     * Sets the device to a fixed performance level which can be sustained under
     * normal indoor conditions for at least 10 minutes.
     *
     * This is similar to sustained performance mode, except that whereas
     * sustained performance mode puts an upper bound on performance in the
     * interest of long-term stability, fixed performance mode puts both upper
     * and lower bounds on performance such that any workload run while in a
     * fixed performance mode should complete in a repeatable amount of time
     * (except if the device is under thermal throttling).
     *
     * This mode is not intended for general purpose use, but rather to enable
     * games and other performance-sensitive applications to reduce the number
     * of variables during profiling and performance debugging. As such, while
     * it is valid to set the device to minimum clocks for all subsystems in
     * this mode, it is preferable to attempt to make the relative performance
     * of the CPU, GPU, and other subsystems match typical usage, even if the
     * frequencies have to be reduced to provide sustainability.
     *
     * To calibrate this mode, follow these steps:
     *
     * 1) Build and push the HWUI macrobench as described in
     *    //frameworks/base/libs/hwui/tests/macrobench/how_to_run.txt
     * 2) Run the macrobench as follows:
     *    while true; do \
     *      adb shell /data/benchmarktest/hwuimacro/hwuimacro shadowgrid2 -c 200 -r 10; \
     *    done
     * 3) Determine a fixed set of device clocks such that the loop in (2) can
     *    run for at least 10 minutes, starting from an idle device on a desk
     *    at room temperature (roughly 22 Celsius), without hitting thermal
     *    throttling.
     * 4) After setting those clocks, set the system property
     *    ro.power.fixed_performance_scale_factor to a value N, where N is the
     *    number of times the loop from (2) runs during the 10 minute test
     *    cycle. It is expected that in FIXED_PERFORMANCE mode, unless there is
     *    thermal throttling, the loop will run N to N+1 times (inclusive).
     *
     * After calibrating this, while in FIXED_PERFORMANCE mode, the macrobench
     * results obtained while running the loop in (2) should be consistent both
     * within a given run and from the first run in the 10 minute window through
     * the last run in the window.
     */
    FIXED_PERFORMANCE,

    /**
     * This mode indicates VR Mode is activated or not. VR mode is intended
     * to provide minimum guarantee for performance for the amount of time the
     * device can sustain it.
     */
    VR,

    /**
     * This mode indicates that an application has been launched.
     */
    LAUNCH,

    /**
     * This mode indicates that the device is about to enter a period of
     * expensive rendering.
     */
    EXPENSIVE_RENDERING,

    /**
     * This mode indicates that the device is about entering/leaving
     * interactive state. (that is, the system is awake and ready for
     * interaction, often with UI devices such as display and touchscreen
     * enabled) or non-interactive state (the
     * system appears asleep, display usually turned off). The
     * non-interactive state may be entered after a period of
     * inactivity in order to conserve battery power during
     * such inactive periods.
     *
     * Typical actions are to turn on or off devices and adjust
     * cpufreq parameters. This function may also call the
     * appropriate interfaces to allow the kernel to suspend the
     * system to low-power sleep state when entering non-interactive
     * state, and to disallow low-power suspend when the system is in
     * interactive state. When low-power suspend state is allowed, the
     * kernel may suspend the system whenever no wakelocks are held.
     */
    INTERACTIVE,

    /**
     * This mode indicates the device is in device idle, externally known as doze.
     * More details on:
     * https://developer.android.com/training/monitoring-device-state/doze-standby
     */
    DEVICE_IDLE,

    /**
     * This mode indicates that display is either off or still on but is optimized
     * for low-power.
     */
    DISPLAY_INACTIVE,

    /**
     * Below hints are currently not sent in Android framework but OEM might choose to
     * implement for power/perf optimizations.
     */

    /**
     * This mode indicates that low latency audio is active.
     */
    AUDIO_STREAMING_LOW_LATENCY,

    /**
     * This hint indicates that camera secure stream is being started.
     */
    CAMERA_STREAMING_SECURE,

    /**
     * This hint indicates that camera low resolution stream is being started.
     */
    CAMERA_STREAMING_LOW,

    /**
     * This hint indicates that camera mid resolution stream is being started.
     */
    CAMERA_STREAMING_MID,

    /**
     * This hint indicates that camera high resolution stream is being started.
     */
    CAMERA_STREAMING_HIGH,
}

簡單介紹各個Mode:

  • DOUBLE_TAP_TO_WAKE : 此模式表示裝置允許在螢幕被點選兩次時被喚醒
  • LOW_POWER:此模式表示低電量模式是否被啟用。這種模式以犧牲效能為代價來節省電量消耗
  • SUSTAINED_PERFORMANCE:持續效能模式旨在在較長時間內提供一致的效能水平。
  • FIXED_PERFORMANCE:將裝置設定為一個固定的效能水平,該水平可在正常室內條件下持續至少10分鐘。此模式於SUSTAINED_PERFORMANCE模式,不同之處在於:SUSTAINED_PERFORMANCE為了長期穩定性對效能設定了上限;FIXED_PERFORMANCE模式對效能同時設定了上限和下限,以便在FIXED_PERFORMANCE模式下執行的任何工作負載都應在可重複的時間內完成。
  • VR:VR 模式旨在在裝置可以維持的時間內為效能提供最低限度的保證。
  • LAUNCH此模式表示已啟動應用程式
  • EXPENSIVE_RENDERING:此模式表示裝置即將進入昂貴的渲染週期。 
  • INTERACTIVE此模式表示裝置即將進入/離開 互動狀態或非互動狀態。 非互動狀態可以在不活動時間段之後進入,以便在這樣的不活動時間段期間節省電池電量。典型的操作是開啟或關閉裝置並調整 cpufreq 引數。該函式還可以呼叫適當的介面,允許核心在進入非互動狀態時將系統掛起到低功耗休眠狀態,並在系統處於互動狀態時禁止低功耗掛起。 當允許低功耗掛起狀態時,核心可以在沒有喚醒鎖保持時掛起系統。
  • DEVICE_IDLE此模式表示裝置處於裝置空閒狀態,詳細資訊可參考: 針對低電耗模式和應用待機模式進行優化
  • DISPLAY_INACTIVE此模式表示顯示器關閉或仍然開啟,但已針對低功耗進行了優化。 

以下提示選專案前未在加入 Android 框架,但 OEM 可能會選擇實施電源/效能優化。 

  • AUDIO_STREAMING_LOW_LATENCY:此模式表示低延遲音訊處於啟用狀態
  • CAMERA_STREAMING_SECURE:此模式表示正在啟動相機 安全流
  • CAMERA_STREAMING_LOW:此模式表示正在啟動相機 低 解析度流。 
  • CAMERA_STREAMING_MID:此模式表示正在啟動相機 中 解析度流。 
  • CAMERA_STREAMING_HIGH:此模式表示正在啟動相機 高 解析度流

2.4 Boost type介紹

首先看一下AIDL的原始碼:

點選檢視Boost程式碼
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.power;

@VintfStability
@Backing(type="int")
enum Boost {
    /**
     * This boost is set when user interacting with the device, for example,
     * touchscreen events are incoming. CPU and GPU load may be expected soon,
     * and it may be appropriate to raise speeds of CPU, memory bus etc.
     * Note that this is different from INTERACTIVE mode, which only indicates
     * that such interaction *may* occur, not that it is actively occurring.
     */
    INTERACTION,

    /**
     * This boost indicates that the framework is likely to provide a new
     * display frame soon. This implies that the device should ensure that the
     * display processing path is powered up and ready to receive that update.
     */
    DISPLAY_UPDATE_IMMINENT,

    /**
     * Below hints are currently not sent in Android framework but OEM might choose to
     * implement for power/perf optimizations.
     */

    /**
     * This boost indicates that the device is interacting with ML accelerator.
     */
    ML_ACC,

    /**
     * This boost indicates that the device is setting up audio stream.
     */
    AUDIO_LAUNCH,

    /**
     * This boost indicates that camera is being launched.
     */
    CAMERA_LAUNCH,

    /**
     * This boost indicates that camera shot is being taken.
     */
    CAMERA_SHOT,
}

幾個列舉常量的大概解釋如下:

  • INTERACTION:這種boost是在使用者與裝置互動時設定的,例如,觸控式螢幕事件傳入。 CPU 和 GPU 負載可能很快就會出現,可能適當提高 CPU、記憶體匯流排等的速度。 注意,這與 INTERACTIVE 模式不同,互動模式僅表示這種互動*可能*發生,而不是主動發生 . 
  • DISPLAY_UPDATE_IMMINENT:這種boost表明框架可能很快會提供一個新的顯示幀。這意味著裝置應確保顯示處理路徑已通電並準備好接收該更新。 
  • ML_ACC:此boost表明裝置正在與 ML 加速器互動
  • AUDIO_LAUNCH:此boost表示裝置正在設定音訊流。
  • CAMERA_LAUNCH:此boost表示Camera正在被啟動
  • CAMERA_SHOT:此boost表示Camera正在拍攝

3 結語

通過上面的介紹,應該對power hal aidl有了一個直觀的認識,清楚了有哪些介面/哪些模式,下一篇中我們會在此基礎上通過一個示例講解如何實現這個aidl serivice功能。

 

相關文章