Android 12(S) 图像显示系统 - HWC HAL 初始化与调用流程
必读:
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的功能
Android 12(S) 图像显示系统 - HWC HAL 初始化与调用流程的更多相关文章
- Android 12(S) 图像显示系统 - SurfaceFlinger之VSync-上篇(十六)
必读: Android 12(S) 图像显示系统 - 开篇 一.前言 为了提高Android系统的UI交互速度和操作的流畅度,在Android 4.1中,引入了Project Butter,即&quo ...
- Android 12(S) 图像显示系统 - SurfaceFlinger GPU合成/CLIENT合成方式 - 随笔1
必读: Android 12(S) 图像显示系统 - 开篇 一.前言 SurfaceFlinger中的图层选择GPU合成(CLIENT合成方式)时,会把待合成的图层Layers通过renderengi ...
- Android 12(S) 图像显示系统 - drm_hwcomposer 简析(上)
必读: Android 12(S) 图像显示系统 - 开篇 前言 Android源码中有包含drm_hwcomposer:/external/drm_hwcomposer/ drm_hwcompose ...
- Android 12(S) 图像显示系统 - GraphicBuffer同步机制 - Fence
必读: Android 12(S) 图像显示系统 - 开篇 一.前言 前面的文章中讲解Android BufferQueue的机制时,有遇到过Fence,但没有具体讲解.这篇文章,就针对Fence这种 ...
- Android 12(S) 图像显示系统 - drm_hwcomposer 简析(下)
必读: Android 12(S) 图像显示系统 - 开篇 合成方式 合成类型的定义:/hardware/interfaces/graphics/composer/2.1/IComposerClien ...
- Android 12(S) 图像显示系统 - 基础知识之 BitTube
必读: Android 12(S) 图像显示系统 - 开篇 一.基本概念 在Android显示子系统中,我们会看到有使用BitTube来进行跨进程数据传递.BitTube的实现很简洁,就是一对&quo ...
- Android 12(S) 图像显示系统 - SurfaceFlinger 之 VSync - 中篇(十七)
必读: Android 12(S) 图像显示系统 - 开篇 1 前言 这一篇文章,将继续讲解有关VSync的知识,前一篇文章 Android 12(S) 图像显示系统 - SurfaceFlinger ...
- Android 12(S) 图形显示系统 - 解读Gralloc架构及GraphicBuffer创建/传递/释放(十四)
必读: Android 12(S) 图形显示系统 - 开篇 一.前言 在前面的文章中,已经出现过 GraphicBuffer 的身影,GraphicBuffer 是Android图形显示系统中的一个重 ...
- Android 12(S) 图形显示系统 - 示例应用(二)
1 前言 为了更深刻的理解Android图形系统抽象的概念和BufferQueue的工作机制,这篇文章我们将从Native Level入手,基于Android图形系统API写作一个简单的图形处理小程序 ...
随机推荐
- Myeclipse+svn相关文章
Myeclipse安装svn插件https://www.cnblogs.com/liuyk-code/p/7519886.html 使用svn https://jingyan.baidu.com/ar ...
- RecyclerView + SQLite 简易备忘录-----上
先看效果 图一只是做了简单的页面,没有连接数据库,刚写完页面才想起备忘录好像不需要登录------但用SharedPreferences写了个记住密码. 图二是主页面,实现了搜索,添加,删除,修改几个 ...
- 终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享
某个周一上午,小涛像往常一样泡上一杯热咖啡 ️,准备打开项目协同开始新一天的工作,突然隔壁的小文喊道:"快看,用户支持群里炸锅了 -" 用户 A:"Git 服务有点问题, ...
- vue下一代状态管理Pinia.js 保证你看的明明白白!
1.pinia的简单介绍 Pinia最初是在2019年11月左右重新设计使用Composition API的 Vue 商店外观的实验. 从那时起,最初的原则相同,但 Pinia 适用于 Vue 2 和 ...
- ucore lab2 物理内存管理 学习笔记
总的来讲把的LAB1代码逻辑理顺后再往后学就轻松了一大截.LAB2过遍课程视频,再多翻翻实验指导书基本上就没遇到啥大坎儿.对这节学得东西做个总结就是一张图: 练习0:填写已有实验 本实验依赖实验1.请 ...
- 将Excel数据转换为Java可识别时间(Date、Timestamp)
引言 Excel的时间,POI读取到的是double,这个值不是timestamp.需要进行一些转换,将它转换为Date或者Timestamp. Excel中的日期数据: 程序中读取到的数据: 如何转 ...
- 关于数据拓展及面试题讲解 Java
强类型语言 要求变量的使用严格符合规定,所有变量都必须先定义后才能使用 弱类型语言 Java 的数控类型分为两大类 基本类型(primitive type) 引用类型(reference type) ...
- Hadoop介绍篇
Hadoop详解 1.前言 对于初次接触Hadoop的小伙伴来说,Hadoop是一个很陌生的东西,尤其是Hadoop与大数据之间的关联,写这篇文章之前,我也有许多关于Hadoop与大数据的疑惑,接下来 ...
- zabbix的web界面访问失败问题排查
现象:用curl访问显示拒绝链接,查看zabbix-server日志也无异常 1.检查防火墙,SElinux是否关闭 2.检查zabbix-server服务是否启动 3.检查80端口是否被占用,比方是 ...
- 【mq】从零开始实现 mq-12-消息的批量发送与回执
前景回顾 [mq]从零开始实现 mq-01-生产者.消费者启动 [mq]从零开始实现 mq-02-如何实现生产者调用消费者? [mq]从零开始实现 mq-03-引入 broker 中间人 [mq]从零 ...