Android8.0 HwComposer 硬件抽象层

图形引擎SurfaceFlinger作为图形系统的心脏,自然需要心跳动力,主要来自图形Composer。Composer有两种一种来自硬件驱动,一种来自软件模拟。今天我们来看Hardware部分。主要分析vsynct同步信号是如何产生传递的,为此我们将略过Fence机制(跨硬件同步),层级合成(硬件合成)相关的内容。

Android8.0 HwComposer 硬件抽象层_第1张图片

1. DisplayHardware层

frameworks\native\services\surfaceflinger\SurfaceFlinger.h
我们从引擎的头文件看出,SurfaceFlinger继承自HWComposer::EventHandler

class SurfaceFlinger : public BnSurfaceComposer,
                       private IBinder::DeathRecipient,
                       private HWComposer::EventHandler {......}

frameworks\native\services\surfaceflinger\DisplayHardware\HWComposer.h

在HWComposer的头文件中定义EventHandler类,主要有同步,热拔插,刷新三个方法,可以大胆推测是在SurfaceFlinger中实现了这三个方法,由硬件Composer驱动来调用。

    class EventHandler {
        friend class HWComposer;
        virtual void onVSyncReceived(
            HWComposer* composer, int32_t disp, nsecs_t timestamp) = 0;
        virtual void onHotplugReceived(HWComposer* composer, int32_t disp, bool connected) = 0;
        virtual void onInvalidateReceived(HWComposer* composer) = 0;
    protected:
        virtual ~EventHandler() {}
    };

frameworks\native\services\surfaceflinger\SurfaceFlinger.cpp
在SurfaceFlinger的init()函数中初始化了HWComposer,并设置了事件回调句柄

void SurfaceFlinger::init() {
  ......
  mRealHwc = new HWComposer(false);
    mHwc = mRealHwc;
    mHwc->setEventHandler(static_cast<HWComposer::EventHandler*>(this));
  ......
}

frameworks\native\services\surfaceflinger\DisplayHardware\HWComposer.cpp
初始化HWComposer,加载Device

HWComposer::HWComposer(bool useVrComposer)
    : mHwcDevice(),
      mDisplayData(2),
      mFreeDisplaySlots(),
      mHwcDisplaySlots(),
      mCBContext(),
      mEventHandler(nullptr),
      mVSyncCounts(),
      mRemainingHwcVirtualDisplays(0)
{
    for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
        mLastHwVSync[i] = 0;
        mVSyncCounts[i] = 0;
    }

    loadHwcModule(useVrComposer); //加载Device
}

典型的回调写法,为HWComposer设置事件回调句柄,并绑定回调

void HWComposer::setEventHandler(EventHandler* handler)
{
    ......
    bool wasNull = (mEventHandler == nullptr);
    mEventHandler = handler; //持有引用

    if (wasNull) {
        auto hotplugHook = std::bind(&HWComposer::hotplug, this, //绑定hotplug
                std::placeholders::_1, std::placeholders::_2); //两个参数
        mHwcDevice->registerHotplugCallback(hotplugHook); //注册hotplugHook到mHwcDevice
        auto invalidateHook = std::bind(&HWComposer::invalidate, this,
                std::placeholders::_1);
        mHwcDevice->registerRefreshCallback(invalidateHook);
        auto vsyncHook = std::bind(&HWComposer::vsync, this,
                std::placeholders::_1, std::placeholders::_2);
        mHwcDevice->registerVsyncCallback(vsyncHook);
    }
}

注册到mHwcDevice的hotplugHook方法会调用hotplug函数

void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display,
        HWC2::Connection connected) {
    ......
    int32_t disp = 0;
    if (!mDisplayData[0].hwcDisplay) {
        mDisplayData[0].hwcDisplay = display;
        mHwcDisplaySlots[display->getId()] = 0;
        disp = DisplayDevice::DISPLAY_PRIMARY;
    } else {
        // Disconnect is handled through HWComposer::disconnectDisplay via
        // SurfaceFlinger's onHotplugReceived callback handling
        if (connected == HWC2::Connection::Connected) {
            mDisplayData[1].hwcDisplay = display;
            mHwcDisplaySlots[display->getId()] = 1;
        }
        disp = DisplayDevice::DISPLAY_EXTERNAL;
    }
    //不得了,调用SurfaceFlinger实现的onHotplugReceived函数
    mEventHandler->onHotplugReceived(this, disp,
            connected == HWC2::Connection::Connected);
}

frameworks\native\services\surfaceflinger\DisplayHardware\HWComposer.h
这里申明了mHwcDevice

 std::unique_ptr<HWC2::Device>   mHwcDevice;

frameworks\native\services\surfaceflinger\DisplayHardware\HWC2.cpp
注册回调

void Device::registerHotplugCallback(HotplugCallback hotplug)
{
    ALOGV("registerHotplugCallback");
    mHotplug = hotplug; //引用
    for (auto& pending : mPendingHotplugs) {
        auto& display = pending.first;
        auto connected = pending.second;
        ALOGV("Sending pending hotplug(%" PRIu64 ", %s)", display->getId(),
                to_string(connected).c_str());
        mHotplug(std::move(display), connected);
    }
}

frameworks\native\services\surfaceflinger\DisplayHardware\HWC2.cpp
在HWC2文件的头部

extern "C" {
    static void hotplug_hook(hwc2_callback_data_t callbackData,
            hwc2_display_t displayId, int32_t intConnected) {
        auto device = static_cast<HWC2::Device*>(callbackData);
        auto display = device->getDisplayById(displayId);
        if (display) {
            auto connected = static_cast<HWC2::Connection>(intConnected);
            device->callHotplug(std::move(display), connected); //由具体设备回调
        } else {
            ALOGE("Hotplug callback called with unknown display %" PRIu64,
                    displayId);
        }
    }
    ......
}

执行绑定的hotplugHook函数

void Device::callHotplug(std::shared_ptr<Display> display, Connection connected)
{
    if (connected == Connection::Connected) {
        if (!display->isConnected()) {
            mComposer->setClientTargetSlotCount(display->getId());
            display->loadConfigs();
            display->setConnected(true);
        }
    } else {
        display->setConnected(false);
        mDisplays.erase(display->getId());
    }

    if (mHotplug) {
        mHotplug(std::move(display), connected);//执行
    } else {
        ALOGV("callHotplug called, but no valid callback registered, storing");
        mPendingHotplugs.emplace_back(std::move(display), connected);
    }
}

那么hotplug_hook由谁调用呢,我们在此找到ComposerCallback

class ComposerCallback : public Hwc2::IComposerCallback {
public:
    ComposerCallback(Device* device) : mDevice(device) {}

    Return<void> onHotplug(Hwc2::Display display,
            Connection connected) override
    {
        hotplug_hook(mDevice, display, static_cast<int32_t>(connected)); //执行
        return Void();
    }
    .......

private:
    Device* mDevice;
};
} // namespace anonymous

这个registerCallbacks函数在Device初始化时被调用

void Device::registerCallbacks()
{
    sp<ComposerCallback> callback = new ComposerCallback(this);
    mComposer->registerCallback(callback);
}

frameworks\native\services\surfaceflinger\DisplayHardware\HWC2.h
在HWC2.h文件中找到了申明

 std::unique_ptr<android::Hwc2::Composer> mComposer;

2. Treble层

frameworks\native\services\surfaceflinger\DisplayHardware\ComposerHal.h
ComposerHal对接了Treble架构的Hal

Composer::Composer(bool useVrComposer)
    : mWriter(kWriterInitialSize),
      mIsUsingVrComposer(useVrComposer)
{
    if (mIsUsingVrComposer) {
        mComposer = IComposer::getService("vr");
    } else {
        mComposer = IComposer::getService(); //默认名
    }

    if (mComposer == nullptr) {
        LOG_ALWAYS_FATAL("failed to get hwcomposer service");
    }

    mComposer->createClient(
            [&](const auto& tmpError, const auto& tmpClient)
            {
                if (tmpError == Error::NONE) {
                    mClient = tmpClient;
                }
            });
    if (mClient == nullptr) {
        LOG_ALWAYS_FATAL("failed to create composer client");
    }
}

hardware\interfaces\graphics\composer\2.1\default\Hwc.h

class HwcHal : public IComposer, public ComposerBase {......}

hardware\interfaces\graphics\composer\2.1\default\Hwc.cpp
载入Composer硬件抽象层模块

IComposer* HIDL_FETCH_IComposer(const char*)
{
    const hw_module_t* module = nullptr;
    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &module);
    if (err) {
        ALOGE("failed to get hwcomposer module");
        return nullptr;
    }

    return new HwcHal(module);
}

就此创建一个ComposerClient

Return<void> HwcHal::createClient(createClient_cb hidl_cb)
{
    Error err = Error::NONE;
    sp<ComposerClient> client;

    {
        std::lock_guard<std::mutex> lock(mClientMutex);

        // only one client is allowed
        if (mClient == nullptr) {
            client = new ComposerClient(*this); //新建ComposerClient
            client->initialize();
            mClient = client;
        } else {
            err = Error::NO_RESOURCES;
        }
    }

    hidl_cb(err, client);

    return Void();
}

frameworks\native\services\surfaceflinger\DisplayHardware\ComposerHal.cpp
继续Device : registerCallback

void Composer::registerCallback(const sp<IComposerCallback>& callback)
{
    auto ret = mClient->registerCallback(callback);
    if (!ret.isOk()) {
        ALOGE("failed to register IComposerCallback");
    }
}

frameworks\native\services\surfaceflinger\DisplayHardware\ComposerHal.h

 sp<IComposer> mComposer;
 sp<IComposerClient> mClient;

hardware\interfaces\graphics\composer\2.1\default\ComposerClient.cpp

Return<void> ComposerClient::registerCallback(
        const sp<IComposerCallback>& callback)
{
    // no locking as we require this function to be called only once
    mCallback = callback; //持有引用
    mHal.enableCallback(callback != nullptr);
    return Void();
}

hardware\interfaces\graphics\composer\2.1\default\Hwc.cpp

void HwcHal::enableCallback(bool enable)
{
    if (enable) {
        mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this,
                reinterpret_cast<hwc2_function_pointer_t>(hotplugHook));
    } else {
        mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this,
                nullptr);
    }
}

hardware\interfaces\graphics\composer\2.1\default\Hwc.cpp
调用了ComposerClient的onHotplug函数

void HwcHal::hotplugHook(hwc2_callback_data_t callbackData,
        hwc2_display_t display, int32_t connected)
{
    auto hal = reinterpret_cast<HwcHal*>(callbackData);
    auto client = hal->getClient();  //ComposerClient
    if (client != nullptr) {
        client->onHotplug(display,
                static_cast<IComposerCallback::Connection>(connected));
    }
}

那么问题来了mDispatch是啥
mDispatch是一个结构体,它持有交互函数

void HwcHal::initDispatch()
{
    initDispatch(HWC2_FUNCTION_ACCEPT_DISPLAY_CHANGES,
            &mDispatch.acceptDisplayChanges);
    initDispatch(HWC2_FUNCTION_CREATE_LAYER, &mDispatch.createLayer);
    initDispatch(HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY,
            &mDispatch.createVirtualDisplay);
    initDispatch(HWC2_FUNCTION_DESTROY_LAYER, &mDispatch.destroyLayer);
    initDispatch(HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY,
            &mDispatch.destroyVirtualDisplay);
    ......

模板函数,与硬件抽象层函数绑定

template<typename T>
void HwcHal::initDispatch(hwc2_function_descriptor_t desc, T* outPfn)
{
    auto pfn = mDevice->getFunction(mDevice, desc); //调用具体函数
    if (!pfn) {
        LOG_ALWAYS_FATAL("failed to get hwcomposer2 function %d", desc);
    }

    *outPfn = reinterpret_cast<T>(pfn);
}

hardware\libhardware\include\hardware\hwcomposer2.h
交由具体的厂商实现

struct{ 
    ......
    /* getFunction(..., descriptor)
     *
     * Returns a function pointer which implements the requested description.
     *
     * Parameters:
     *   descriptor - the function to return
     *
     * Returns either a function pointer implementing the requested descriptor
     *   or NULL if the described function is not supported by this device.
     */
    hwc2_function_pointer_t (*getFunction)(struct hwc2_device* device,
            int32_t /*hwc2_function_descriptor_t*/ descriptor);
} hwc2_device_t;

至此,HardwareComposer的vsync,hotplug的信号流程清楚了。关于HwComposer的层级合成我们以后再看。

你可能感兴趣的:(Android)