1 层级架构概览
2 总体逻辑顺序计
(1) CameraProvider进程启动、注册
(2) CameraServer进程启动、注册、初始化
(3) CameraServer初始化过程中通过HIDL通信获取CameraProvider,并对 CameraProvider进行初始化
CmeraProvider进程注册、启动
代码位置:
/hardware/interfaces/camera/provider/2.4/default
入口在service.cpp中的main函数
1. int main()
2. {
3. ALOGI("Camera provider Service is starting.");
4. // The camera HAL may communicate to other vendor components via
5. // /dev/vndbinder
6. android::ProcessState::initWithDriver("/dev/vndbinder");
7. return defaultPassthroughServiceImplementation<ICameraProvider>("legacy/0", /*maxThreads*/ 6);
8. }
第6行:初始化binder通信驱动节点,打开binder设备
第7行:创建默认为直通模式(Passthrough)的CameraProvider服务实现
defaultPassthroughServiceImplementation的实现在:
./system/libhidl/transport/include/hidl/LegacySupport.h
1. /**
2. * Creates default passthrough service implementation. This method never returns.
3. *
4. * Return value is exit status.
5. */
6. template
7. __attribute__((warn_unused_result))
8. status_t defaultPassthroughServiceImplementation(std::string name,
9. size_t maxThreads = 1) {
10. configureRpcThreadpool(maxThreads, true);
11. status_t result = registerPassthroughServiceImplementation(name);
12.
13. if (result != OK) {
14. return result;
15. }
16.
17. joinRpcThreadpool();
18. return 0;
19. }
第11行:CameraProvider进程以名称“legacy/0”注册到系统中。
CameraServer进程启动并初始化CameraProvider
1. int main(int argc __unused, char** argv __unused)
2. {
3. signal(SIGPIPE, SIG_IGN);
4.
5. // Set 3 threads for HIDL calls
6. hardware::configureRpcThreadpool(3, /*willjoin*/ false);
7.
8. sp proc(ProcessState::self());
9. sp sm = defaultServiceManager();
10. ALOGI("ServiceManager: %p", sm.get());
11. CameraService::instantiate();
12. ProcessState::self()->startThreadPool();
13. IPCThreadState::self()->joinThreadPool();
14. }
重点在第11行:CameraService::instantiate(),其他都是与Binder通信机制相关的
Instantiate()方法并不在CameraService中实现,而是在它的父类 BinderService中:
frameworks/native/libs/binder/include/binder/BinderService.h
1. template
2. class BinderService
3. {
4. public:
5. static status_t publish(bool allowIsolated = false) {
6. sp sm(defaultServiceManager());
7. return sm->addService(
8. String16(SERVICE::getServiceName()),
9. new SERVICE(), allowIsolated);
10. }
11.
12. static void publishAndJoinThreadPool(bool allowIsolated = false) {
13. publish(allowIsolated);
14. joinThreadPool();
15. }
16.
17. static void instantiate() { publish(); }
18.
19. static status_t shutdown() { return NO_ERROR; }
20. …
21. …
22. …
23.
24. }
可以看到,instantiate()方法通过调用publish()将CameraService添加到ServiceManager中,这样其他进程就可以通过Binder调用CameraService了。
要注意的是,在addService函数中,CameraService被强指针引用了,在Android中,对象首次被强指针引用时,会调用自身的onFirstRef()函数进行初始化
frameworks/av/services/camera/libcameraservice/CameraService.cpp
26. void CameraService::onFirstRef()
27. {
28. ALOGI("CameraService process starting");
29.
30. BnCameraService::onFirstRef();
31.
32. // Update battery life tracking if service is restarting
33. BatteryNotifier& notifier(BatteryNotifier::getInstance());
34. notifier.noteResetCamera();
35. notifier.noteResetFlashlight();
36.
37. status_t res = INVALID_OPERATION;
38.
39. res = enumerateProviders();
40. if (res == OK) {
41. mInitialized = true;
42. }
43.
44. CameraService::pingCameraServiceProxy();
45. }
第14行是重点:
res = enumerateProviders()
这个函数内容较多,看下关键的部分:
47. if (nullptr == mCameraProviderManager.get()) {
48. mCameraProviderManager = new CameraProviderManager();
49. res = mCameraProviderManager->initialize(this);
50. if (res != OK) {
51. ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
52. __FUNCTION__, strerror(-res), res);
53. return res;
54. }
55. }
这里的CameraProviderManager是CameraService和CameraProvider沟通的桥梁。
看下CameraProviderManager的initialize方法:
57. status_t CameraProviderManager::initialize(wp listener,
58. ServiceInteractionProxy* proxy) {
59. std::lock_guard lock(mInterfaceMutex);
60. if (proxy == nullptr) {
61. ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__);
62. return BAD_VALUE;
63. }
64. mListener = listener;
65. mServiceProxy = proxy;
66.
67. // Registering will trigger notifications for all already-known providers
68. bool success = mServiceProxy->registerForNotifications(
69. /* instance name, empty means no filter */ "",
70. this);
71. if (!success) {
72. ALOGE("%s: Unable to register with hardware service manager for notifications "
73. "about camera providers", __FUNCTION__);
74. return INVALID_OPERATION;
75. }
76.
77. // See if there's a passthrough HAL, but let's not complain if there's not
78. addProviderLocked(kLegacyProviderName, /*expected*/ false);
79.
80. return OK;
81. }
重点在第22行,addProviderLocked, 这个函数主要作用是将找到的这个 Provider 通过 ProviderInfo 记录下来并初始化,Provider名称为legacy/0
83. status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) {
84. for (const auto& providerInfo : mProviders) {
85. if (providerInfo->mProviderName == newProvider) {
86. ALOGW("%s: Camera provider HAL with name '%s' already registered", __FUNCTION__,
87. newProvider.c_str());
88. return ALREADY_EXISTS;
89. }
90. }
91.
92. sp interface;
93. interface = mServiceProxy->getService(newProvider);
94.
95. if (interface == nullptr) {
96. if (expected) {
97. ALOGE("%s: Camera provider HAL '%s' is not actually available", __FUNCTION__,
98. newProvider.c_str());
99. return BAD_VALUE;
100. } else {
101. return OK;
102. }
103. }
104.
105. sp providerInfo =
106. new ProviderInfo(newProvider, interface, this);
107. status_t res = providerInfo->initialize();
108. if (res != OK) {
109. return res;
110. }
111.
112. mProviders.push_back(providerInfo);
113.
114. return OK;
115. }
这里用CameraProvider通过Binder通信获取到Hal 层CameraProvider的信息并进行初始化。
getService函数最终会调用到:
frameworks/av/services/camera/libcameraservice/common/ CameraProviderManager.h
117. virtual sp getService(
118. const std::string &serviceName) override {
119. return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
}
```
IcameraProvider::getService函数最终会通过HIDL通信调用到CameraProvider.cpp中的HIDL_FETCH_IcameraProvider函数中:
hardware/interfaces/camera/provider/2.4/default/CameraProvider.cpp
122. ICameraProvider* HIDL_FETCH_ICameraProvider(const char* name) {
123. if (strcmp(name, kLegacyProviderName) != 0) {
124. return nullptr;
125. }
126. CameraProvider* provider = new CameraProvider();
127. if (provider == nullptr) {
128. ALOGE("%s: cannot allocate camera provider!", __FUNCTION__);
129. return nullptr;
130. }
131. if (provider->isInitFailed()) {
132. ALOGE("%s: camera provider init failed!", __FUNCTION__);
133. delete provider;
134. return nullptr;
135. }
136. return provider;
137. }
CameraProvider的构造方法
139. CameraProvider::CameraProvider() :
140. camera_module_callbacks_t({sCameraDeviceStatusChange,
141. sTorchModeStatusChange}) {
142. mInitFailed = initialize();
143. }
bool CameraProvider::initialize()实现,这个方法我们截取重点部分:
145. camera_module_t *rawModule;
146. int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
147. (const hw_module_t **)&rawModule);
148. if (err < 0) {
149. ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
150. return true;
151. }
152.
153. mModule = new CameraModule(rawModule);
154. err = mModule->init();
155. if (err != OK) {
156. ALOGE("Could not initialize camera HAL module: %d (%s)", err, strerror(-err));
157. mModule.clear();
158. return true;
159. }
160. ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
这里通过hw_get_module调用动态链接库获取硬件的信息,并包装到CameraModule中。至此,CameraProvider初始化完成。