Android 12(S) 影像顯示系統 - HWC HAL 初始化與呼叫流程

二的次方發表於2022-06-28

必讀:

Android 12(S) 影像顯示系統 - 開篇


 

介面定義

原始碼位置:/hardware/interfaces/graphics/composer/

在原始碼目錄下可以看到4個版本的HIDL Interface定義,最新版本是2.4,也是目前我的測試平臺在用的,不同版本使用帶有版本號的包名來區分,例如 :package android.hardware.graphics.composer@2.4
各版本中均有相同的介面,並且具有繼承關係,新版本繼承舊版介面並擴充套件了新方法。

/frameworks/native/services/surfaceflinger/DisplayHardware/ 中 android::Hwc2::impl::Composer 相當於HIDL的客戶端,framework通過impl::Composer中的方法看跨程式和 hwc hal 互動。

各個版本介面的繼承關係

 

Composer HIDL Service

程式碼位置
/hardware/interfaces/graphics/composer/2.4/default/service.cpp
/hardware/interfaces/graphics/composer/2.4/default/android.hardware.graphics.composer@2.4-service.rc


開機啟動時,init程式解析rc檔案帶起服務程式,在service.cpp的main函式中,註冊服務

int main() {
    ...
    android::sp<IComposer> composer = HwcLoader::load();
    if (composer == nullptr) {
        return 1;
    }
    if (composer->registerAsService() != android::NO_ERROR) {
        ALOGE("failed to register service");
        return 1;
    }
    ...
}

 

可以看到其中呼叫了一個非常重要的方法HwcLoader::load(),看其程式碼:

[/hardware/interfaces/graphics/composer/2.4/utils/passthrough/include/composer-passthrough/2.4/HwcLoader.h]
static IComposer* load() {
    const hw_module_t* module = loadModule();
    if (!module) {
        return nullptr;
    }
    auto hal = createHalWithAdapter(module);
    if (!hal) {
        return nullptr;
    }
    return createComposer(std::move(hal)).release();
}

 

再看loadModule的定義,去找HAL library中定義的HAL_MODULE_INFO_SYM結構體變數

[/hardware/interfaces/graphics/composer/2.1/utils/passthrough/include/composer-passthrough/2.1/HwcLoader.h]
static const hw_module_t* loadModule() {
    const hw_module_t* module;
    int error = hw_get_module(HWC_HARDWARE_MODULE_ID, &module);
    if (error) {
        ALOGI("falling back to gralloc module");
        error = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
    }
    if (error) {
        ALOGE("failed to get hwcomposer or gralloc module");
        return nullptr;
    }
    return module;
}

 

可以看到基本的呼叫流程:
開機啟動 ==> HwcLoader::load() ==> HwcLoader::loadModule() ==> hw_get_module


hw_get_module就是去開啟Vendor廠商實現的HWC HAL模組,這個流程本質上還是舊有的HAL開啟方式,只是外包裝了一層HIDL。
最終得到一個指向HWC module的hw_module_t指標

再回到HwcLoader::load()方法中,接下來就是  createHalWithAdapter ==>  createComposer

 

先看 createHalWithAdapter 做了哪些工作

[/hardware/interfaces/graphics/composer/2.4/utils/passthrough/include/composer-passthrough/2.4/HwcLoader.h]
// create a ComposerHal instance, insert an adapter if necessary
static std::unique_ptr<hal::ComposerHal> createHalWithAdapter(const hw_module_t* module) {
    bool adapted;
    hwc2_device_t* device = openDeviceWithAdapter(module, &adapted);
    if (!device) {
        return nullptr;
    }
    auto hal = std::make_unique<HwcHal>();
    return hal->initWithDevice(std::move(device), !adapted) ? std::move(hal) : nullptr;
}

 

其中呼叫了openDeviceWithAdapter去開啟 hwc2_device_t

[/hardware/interfaces/graphics/composer/2.1/utils/passthrough/include/composer-passthrough/2.1/HwcLoader.h]
// open hwcomposer2 device, install an adapter if necessary
static hwc2_device_t* openDeviceWithAdapter(const hw_module_t* module, bool* outAdapted) {
    if (module->id && std::string(module->id) == GRALLOC_HARDWARE_MODULE_ID) {
        *outAdapted = true;
        return adaptGrallocModule(module);
    }
    hw_device_t* device;
    int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device);
    if (error) {
        ALOGE("failed to open hwcomposer device: %s", strerror(-error));
        return nullptr;
    }
    int major = (device->version >> 24) & 0xf;
    if (major != 2) {
        *outAdapted = true;
        return adaptHwc1Device(std::move(reinterpret_cast<hwc_composer_device_1*>(device)));
    }
    *outAdapted = false;
        return reinterpret_cast<hwc2_device_t*>(device);
}

 

openDeviceWithAdapter中呼叫了open方法

hw_device_t* device;
int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device);

 

這樣就獲取到了 hwc2_device

typedef struct hwc2_device {
     struct hw_device_t common;
     void (*getCapabilities)(struct hwc2_device* device, uint32_t* outCount,
            int32_t* /*hwc2_capability_t*/outCapabilities);
     hwc2_function_pointer_t (*getFunction)(struct hwc2_device* device,
            int32_t /*hwc2_function_descriptor_t*/descriptor);
} hwc2_device_t;

 

回到createHalWithAdapter中再呼叫initWithDevice及儲存到了HwcHal中的mDevice這個變數中

bool initWithDevice(hwc2_device_t* device, bool requireReliablePresentFence) {
    // we own the device from this point on
    mDevice = device;
    ….
}

getFunction函式關聯到了 vendor 廠商實現的hwc hal中具體實現,這個函式通過函式描述符來獲取對應的函式指標。
 

initDispatch的作用介紹

initDispatch()來初始化HWC通用的函式指標

 

2.4 <---2.3<---2.2<---2.1 版本的initDispatch()會被呼叫到來初始化函式指標

在這裡就是把mDispatch函式指標和HWC HAL具體實現的方法一一對應起來

注意一下繼承關係:
V2_1
class HwcHalImpl : public Hal

V2_2 
class HwcHalImpl : public V2_1::passthrough::detail::HwcHalImpl<Hal>

V2_3
class HwcHalImpl : public V2_2::passthrough::detail::HwcHalImpl<Hal>

V2_4
class HwcHalImpl : public V2_3::passthrough::detail::HwcHalImpl<Hal>

 

各個版本的HwcHalImpl類中都有定義一個結構體變數mDispatch,這個結構體的成員變數均為函式指標,是以 HWC2_PFN_* 開頭的函式指標型別。

struct {
    HWC2_PFN_ACCEPT_DISPLAY_CHANGES acceptDisplayChanges;
    HWC2_PFN_CREATE_LAYER createLayer;
    ……
    HWC2_PFN_VALIDATE_DISPLAY validateDisplay;
} mDispatch = {};

 

initDispatch通過呼叫vendor廠商實現的hwc hal中的getFunction方法獲取到descriptor對應的函式指標,並儲存到mDispatch的成員中

 

template <typename T>
bool initDispatch(hwc2_function_descriptor_t desc, T* outPfn) {
   auto pfn = mDevice->getFunction(mDevice, desc);
   if (pfn) {
       *outPfn = reinterpret_cast<T>(pfn);
       return true;
   } else {
       ALOGE("failed to get hwcomposer2 function %d", desc);
       return false;
   }
}

 

簡單總結下上述的分析內容

    1. 開機後啟動了一個名字為android.hardware.graphics.composer@2.4-service的服務程式,用於 framework 和 hwc hal 的通訊;
    2. 這個composer service啟動時,進行了必要的初始化工作,主要就是loadModule和openDevice,在這一過程中獲取到hal層實現的函式的指標,
    並儲存在了mDispatch中
    3. 之後執行時,framework 呼叫功能時,本質就是通過mDispatch持有的函式指標呼叫到 hwc hal 的具體實現

 

Framework 與 hwc hal通訊的流程

直接列印呼叫棧資訊來展示這一流程,以其中的setClientTarget這個方法的呼叫過程為例:


SurfaceFlinger程式的呼叫棧資訊

11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#00 pc 00086f8f  /system/bin/surfaceflinger (android::Hwc2::impl::Composer::setClientTarget(unsigned long long, unsigned int, android::sp<android::GraphicBuffer> const&, int, android::hardware::graphics::common::V1_2::Dataspace, std::__1::vector<android::hardware::graphics::composer::V2_1::IComposerClient::Rect, std::__1::allocator<android::hardware::graphics::composer::V2_1::IComposerClient::Rect> > const&)+182)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#01 pc 00096791  /system/bin/surfaceflinger (android::impl::HWComposer::setClientTarget(android::HalDisplayId, unsigned int, android::sp<android::Fence> const&, android::sp<android::GraphicBuffer> const&, android::hardware::graphics::common::V1_2::Dataspace)+408)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#02 pc 0008e881  /system/bin/surfaceflinger (android::FramebufferSurface::advanceFrame()+332)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#03 pc 00122a55  /system/bin/surfaceflinger (android::compositionengine::impl::RenderSurface::queueBuffer(android::base::unique_fd_impl<android::base::DefaultCloser>)+436)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#04 pc 001069ed  /system/bin/surfaceflinger (android::compositionengine::impl::Output::finishFrame(android::compositionengine::CompositionRefreshArgs const&)+168)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#05 pc 00100c89  /system/bin/surfaceflinger (android::compositionengine::impl::Display::finishFrame(android::compositionengine::CompositionRefreshArgs const&)+76)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#06 pc 001054ef  /system/bin/surfaceflinger (android::compositionengine::impl::Output::present(android::compositionengine::CompositionRefreshArgs const&)+114)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#07 pc 000ff727  /system/bin/surfaceflinger (android::compositionengine::impl::CompositionEngine::present(android::compositionengine::CompositionRefreshArgs&)+146)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#08 pc 000d600b  /system/bin/surfaceflinger (android::SurfaceFlinger::onMessageRefresh()+1530)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#09 pc 000d570d  /system/bin/surfaceflinger (android::SurfaceFlinger::onMessageInvalidate(long long, long long)+8784)
11-13 00:47:20.455   224   224 E HwcComposer: stackdump:#10 pc 000b9907  /system/bin/surfaceflinger (android::impl::MessageQueue::Handler::handleMessage(android::Message const&)+94)
11-13 00:47:20.456   224   224 E HwcComposer: stackdump:#11 pc 00010179  /system/lib/libutils.so (android::Looper::pollInner(int)+288)
11-13 00:47:20.456   224   224 E HwcComposer: stackdump:#12 pc 0000ffff  /system/lib/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+62)
11-13 00:47:20.456   224   224 E HwcComposer: stackdump:#13 pc 000b9dd1  /system/bin/surfaceflinger (android::impl::MessageQueue::waitMessage()+56)
11-13 00:47:20.456   224   224 E HwcComposer: stackdump:#14 pc 000cc50d  /system/bin/surfaceflinger (android::SurfaceFlinger::run()+6)
11-13 00:47:20.456   224   224 E HwcComposer: stackdump:#15 pc 000ff253  /system/bin/surfaceflinger (main+1182)
11-13 00:47:20.456   224   224 E HwcComposer: stackdump:#16 pc 0003253b  /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+54)

 

HWC HAL端的呼叫棧資訊 

11-08 06:04:30.920   339   339 E hwc_display: stackdump:#00 pc 000287cb  /vendor/lib/hw/hwcomposer.stark.so (Display::setClientTarget(native_handle const*, int, int, hwc_region)+90)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#01 pc 00023255  /vendor/lib/hw/hwcomposer.stark.so (hwc_setClientTarget(hwc2_device*, unsigned long long, native_handle const*, int, int, hwc_region)+56)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#02 pc 00007d93  /vendor/bin/hw/android.hardware.graphics.composer@2.4-service (android::hardware::graphics::composer::V2_1::passthrough::detail::HwcHalImpl<android::hardware::graphics::composer::V2_4::hal::ComposerHal>::setClientTarget(unsigned long long, native_handle const*, int, int, std::__1::vector<hwc_rect, std::__1::allocator<hwc_rect> > const&)+40)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#03 pc 00010b11  /vendor/bin/hw/android.hardware.graphics.composer@2.4-service (android::hardware::graphics::composer::V2_1::hal::ComposerCommandEngine::executeSetClientTarget(unsigned short)+244)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#04 pc 0000ed3f  /vendor/bin/hw/android.hardware.graphics.composer@2.4-service (android::hardware::graphics::composer::V2_1::hal::ComposerCommandEngine::execute(unsigned int, android::hardware::hidl_vec<android::hardware::hidl_handle> const&, bool*, unsigned int*, android::hardware::hidl_vec<android::hardware::hidl_handle>*)+86)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#05 pc 0000d0a1  /vendor/bin/hw/android.hardware.graphics.composer@2.4-service (android::hardware::graphics::composer::V2_1::hal::detail::ComposerClientImpl<android::hardware::graphics::composer::V2_3::IComposerClient, android::hardware::graphics::composer::V2_3::hal::ComposerHal>::executeCommands(unsigned int, android::hardware::hidl_vec<android::hardware::hidl_handle> const&, std::__1::function<void (android::hardware::graphics::composer::V2_1::Error, bool, unsigned int, android::hardware::hidl_vec<android::hardware::hidl_handle> const&)>)+84)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#06 pc 0001df97  /vendor/lib/android.hardware.graphics.composer@2.2.so (android::hardware::graphics::composer::V2_2::BnHwComposerClient::_hidl_executeCommands_2_2(android::hidl::base::V1_0::BnHwBase*, android::hardware::Parcel const&, android::hardware::Parcel*, std::__1::function<void (android::hardware::Parcel&)>)+294)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#07 pc 0003182f  /vendor/lib/android.hardware.graphics.composer@2.4.so (android::hardware::graphics::composer::V2_4::BnHwComposerClient::onTransact(unsigned int, android::hardware::Parcel const&, android::hardware::Parcel*, unsigned int, std::__1::function<void (android::hardware::Parcel&)>)+1198)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#08 pc 000546d9  /apex/com.android.vndk.v31/lib/libhidlbase.so (android::hardware::BHwBinder::transact(unsigned int, android::hardware::Parcel const&, android::hardware::Parcel*, unsigned int, std::__1::function<void (android::hardware::Parcel&)>)+96)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#09 pc 000536a7  /apex/com.android.vndk.v31/lib/libhidlbase.so (android::hardware::IPCThreadState::getAndExecuteCommand()+974)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#10 pc 00053239  /apex/com.android.vndk.v31/lib/libhidlbase.so (android::hardware::IPCThreadState::joinThreadPool(bool)+56)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#11 pc 000070fb  /vendor/bin/hw/android.hardware.graphics.composer@2.4-service (main+270)
11-08 06:04:30.920   339   339 E hwc_display: stackdump:#12 pc 0003253b  /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+54)

 

 

發起端:

[/frameworks/native/services/surfaceflinger/DisplayHardware/ComposerHal.cpp]
Error Composer::setClientTarget(Display display, uint32_t slot,
        const sp<GraphicBuffer>& target,
        int acquireFence, Dataspace dataspace,
        const std::vector<IComposerClient::Rect>& damage)
{
    mWriter.selectDisplay(display);
    const native_handle_t* handle = nullptr;
    if (target.get()) {
        handle = target->getNativeBuffer()->handle;
    }
    mWriter.setClientTarget(slot, handle, acquireFence, dataspace, damage);
    return Error::NONE;
}

 

中轉站:

[/hardware/interfaces/graphics/composer/2.1/utils/command-buffer/include/composer-command-buffer/2.1/ComposerCommandBuffer.h]
void setClientTargetInternal(...) {
    beginCommand(IComposerClient::Command::SET_CLIENT_TARGET, length);
    ...
    endCommand();
}
 跨程式了
[/hardware/interfaces/graphics/composer/2.1/utils/hal/include/composer-hal/2.1/ComposerCommandEngine.h]
virtual bool executeCommand(IComposerClient::Command command, uint16_t length) {
    switch (command) {
    ...
        case IComposerClient::Command::SET_CLIENT_TARGET:
            return executeSetClientTarget(length);
    }
}


bool executeSetClientTarget(uint16_t length) {
    ...
    err = mHal->setClientTarget(mCurrentDisplay, clientTarget, fence, dataspace, damage);
    ...
}

 

處理終端:

Vendor廠商實現的HAL,具體實現setClientTarget的功能 

 

 



 

相關文章