目录
一、前言
二、frameworks openCamera流程
2.1 openCamera详细流程讲解
2.1.1 CameraManager流程
2.1.2 CameraService流程
2.1.3 CameraDeviceClient
2.1.4 Camera2ClientBase
2.1.5 Camera3Device
2.1.6 CameraProviderManager
2.2 厂商CameraDevice实现
本文主要介绍基于Android Camera API2的frameworks openCamera流程。
frameworks openCamera流程图如图2.1。图2.1包含了openCamera的主要流程,通过Binder通讯,实现了Camera API2的openCamera接口到libacameraservice的跨进程调用。
图2.1 openCamera流程图图2.1 是frameworks openCamera流程图,主要流程是Camera API2应用层接口到libcameraservice的流程。Camera API2应用层部分是CameraManager,libcameraservice包含了CameraService、CameraDeviceClient、Camera2ClientBase、Camera3Device、CameraProviderManager。详细流程如下。
path:frameworks/base/core/java/android/hardware/camera2/CameraManager.java
相关流程:opencamera -> openCameraForUid -> openCameraDeviceUserAsync
openCameraDeviceUserAsync关键方法:
671 private CameraDevice openCameraDeviceUserAsync(String cameraId,
672 CameraDevice.StateCallback callback, Executor executor, final int uid,
673 final int oomScoreOffset) throws CameraAccessException {
......
693 try {
694 ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
695 if (cameraService == null) {
696 throw new ServiceSpecificException(
697 ICameraService.ERROR_DISCONNECTED,
698 "Camera service is currently unavailable");
699 }
//cameraUser就是mRemoteDevice
700 cameraUser = cameraService.connectDevice(callbacks, cameraId,
701 mContext.getOpPackageName(), mContext.getAttributionTag(), uid,
702 oomScoreOffset, mContext.getApplicationInfo().targetSdkVersion);
mRemoteDevice是CameraDeviceClient,获取mRemoteDevice用于后续CameraDeviceClient的配流(createStream),起预览(submitRequestList)。
path:xref: /frameworks/av/services/camera/libcameraservice/CameraService.cpp
由于openCamera需要获取mCameraProviderManager对象用于通知Camera hal打开camera,因此CameraService首先需要初始化mCameraProviderManager。
mCameraProviderManager初始化流程:CameraService的超级类(所有类的父类)是RefBase,实例化的时候会调用onFirstRef()函数,在onFirstRef()函数中会调用enumerateProviders()实例化mCameraProviderManager ,并执行mCameraProviderManager初始化。
相关流程:onFirstRef ->enumerateProviders -> new CameraProviderManager
202 status_t CameraService::enumerateProviders() {
203 status_t res;
204
205 std::vector deviceIds;
206 {
207 Mutex::Autolock l(mServiceLock);
208
209 if (nullptr == mCameraProviderManager.get()) {
210 mCameraProviderManager = new CameraProviderManager();//构造mCameraProviderManager
211 res = mCameraProviderManager->initialize(this);//mCameraProviderManager初始化
212 if (res != OK) {
由于已经提前获取CameraProviderManager,openCamera在CameraService流程如下。
相关流程:connectDevice -> connectHelper -> makeClient
->initialize
path:xref: /frameworks/av/services/camera/libcameraservice/CameraService.cpp
1750 Status CameraService::connectHelper(const sp& cameraCb, const String8& cameraId,
1751 int api1CameraId, const String16& clientPackageName, bool systemNativeClient,
1752 const std::optional& clientFeatureId, int clientUid, int clientPid,
1753 apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion,
1754 /*out*/sp& device) {
......
1844 bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera(
1845 mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion);
//makeClient
1846 if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient,
1847 clientFeatureId, cameraId, api1CameraId, facing, orientation,
1848 clientPid, clientUid, getpid(),
1849 deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass,
1850 /*out*/&tmp)).isOk()) {
1851 return ret;
1852 }
1853 client = static_cast(tmp.get());
1854
1855 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1856 __FUNCTION__);
1857
1858 String8 monitorTags = isClientWatched(client.get()) ? mMonitorTags : String8("");
//client initialize
1859 err = client->initialize(mCameraProviderManager, monitorTags);
1860 if (err != OK) {
1861 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
makeClient
940 Status CameraService::makeClient(const sp& cameraService,
941 const sp& cameraCb, const String16& packageName, bool systemNativeClient,
942 const std::optional& featureId, const String8& cameraId,
943 int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid,
944 int servicePid, std::pair deviceVersionAndTransport,
945 apiLevel effectiveApiLevel, bool overrideForPerfClass, /*out*/sp* client) {
......
979 } else { // Camera2 API route
980 sp tmp =
981 static_cast(cameraCb.get());
982 *client = new CameraDeviceClient(cameraService, tmp, packageName,
983 systemNativeClient, featureId, cameraId, facing, sensorOrientation,
984 clientPid, clientUid, servicePid, overrideForPerfClass);
985 }
986 return Status::ok();
987 }
path:xref: /frameworks/av/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
CameraDeviceClient构造时会创建Camera2ClientBase,用于openCamera的initialize流程。
CameraDeviceClient initialize流程
相关流程:initialize -> initializeImpl -> Camera2ClientBase::initialize
mFrameProcessor线程用于processCaptureResult参数回调,用于不停回调每一帧数据的处理result。
112 status_t CameraDeviceClient::initialize(sp manager,
113 const String8& monitorTags) {
114 return initializeImpl(manager, monitorTags);
115 }
116
117 template
118 status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
119 ATRACE_CALL();
120 status_t res;
121
122 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
123 if (res != OK) {
124 return res;
125 }
126
127 String8 threadName;
//数据回调处理的流程
128 mFrameProcessor = new FrameProcessorBase(mDevice);
129 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
130 mFrameProcessor->run(threadName.string());
131
132 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
133 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
134 /*listener*/this,
135 /*sendPartials*/true);
path:xref: /frameworks/av/services/camera/libcameraservice/common/Camera2ClientBase.cpp
Camera2ClientBase initialize流程
相关流程:initialize -> initializeImpl -> mDevice->initialize
A13通过Binder通讯,使用了AIDL完成mDevice初始化。
89 template
90 status_t Camera2ClientBase::initialize(sp manager,
91 const String8& monitorTags) {
92 return initializeImpl(manager, monitorTags);
93 }
94
95 template
96 template
97 status_t Camera2ClientBase::initializeImpl(TProviderPtr providerPtr,
98 const String8& monitorTags) {
99 ATRACE_CALL();
100 ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
101 TClientBase::mCameraIdStr.string());
102 status_t res;
103
......
115 switch (providerTransport) {
......;
121 case IPCTransport::AIDL:
122 mDevice =
123 new AidlCamera3Device(TClientBase::mCameraIdStr, mOverrideForPerfClass,
124 mLegacyClient); //mDevice初始化
125 break;
126 default:
136
137 res = mDevice->initialize(providerPtr, monitorTags);
138 if (res != OK) {
139 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
140 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
141 return res;
142 }
......
147 return OK;
148 }
path:xref: /frameworks/av/services/camera/libcameraservice/device3/Camera3Device.cpp(A12)
由于A13获取的Camera3Device对象mDevice是aidl实例化的,因此A13的Camera3Device实现是AidlCamera3Device.cpp,在AidlCamera3Device.cpp中完成manager->openAidlSession后,会再回调到Camera3Device执行initializeCommonLocked,initializeCommonLocked的主要作用是initializeCommonLocked,启动下发request的RequestThread线程。
相关流程:initialize -> manager->openSession
path:xref: /frameworks/av/services/camera/libcameraservice/device3/aidl/AidlCamera3Device.cpp(A13)
169 status_t AidlCamera3Device::initialize(sp manager,
170 const String8& monitorTags) {
171 ATRACE_CALL();
.......
182 std::shared_ptr session;
183 ATRACE_BEGIN("CameraHal::openSession");
//openAidlSession
184 status_t res = manager->openAidlSession(mId.string(), mCallbacks,
185 /*out*/ &session);
186 ATRACE_END();
187 if (res != OK) {
188 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
189 return res;
190 }
.......
291 mInterface = new AidlHalInterface(session, queue, mUseHalBufManager, mSupportOfflineProcessing);
.......
306
307 return initializeCommonLocked();//启动RequestThread线程
308 }
openCamera走到 Camera3Device会创建下发request的线程mRequestThread,并执行mRequestThread->run。
path:xref: /frameworks/av/services/camera/libcameraservice/device3/Camera3Device.cpp
114 status_t Camera3Device::initializeCommonLocked() {
115
116 /** Start watchdog thread */
117 mCameraServiceWatchdog = new CameraServiceWatchdog();
......
//Camera3BufferManager
145 /** Create buffer manager */
146 mBufferManager = new Camera3BufferManager();
147
......
170 /** Start up request queue thread */
171 mRequestThread = createNewRequestThread(
172 this, mStatusTracker, mInterface, sessionParamKeys,
173 mUseHalBufManager, mSupportCameraMute);
//RequestThread
174 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
175 if (res != OK) {
176 SET_ERR_L("Unable to start request queue thread: %s (%d)",
......
//PreparerThread
183 mPreparerThread = new PreparerThread();
184
185 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
path:xref: /frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp
相关流程:openAidlSession -> interface->open -> Camera hal openCamera
CameraProviderManager openAidlSession之前,CameraProviderManager初始化会调用initialize完成cameraservice和provider进程的连接,provider进程也就是camera hal进程。
153 status_t CameraProviderManager::initialize(wp listener,
154 HidlServiceInteractionProxy* hidlProxy) {
155 std::lock_guard lock(mInterfaceMutex);
156 if (hidlProxy == nullptr) {
157 ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__);
158 return BAD_VALUE;
159 }
160 mListener = listener;
161 mDeviceState = 0;
162 auto res = tryToInitAndAddHidlProvidersLocked(hidlProxy); //初始化和添加hidl
163 if (res != OK) {
164 // Logging done in called function;
165 return res;
166 }
167 res = tryToAddAidlProvidersLocked(); //实现cameraservice和provider进程(Camera hal进程)连接。
168
169 IPCThreadState::self()->flushCommands();
170
171 return res;
172 }
openAidlSession
604 status_t CameraProviderManager::openAidlSession(const std::string &id,
605 const std::shared_ptr<
606 aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback,
607 /*out*/
608 std::shared_ptr *session) {
609
610 std::lock_guard lock(mInterfaceMutex);
611
612 auto deviceInfo = findDeviceInfoLocked(id);
613 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
614
615 auto *aidlDeviceInfo3 = static_cast(deviceInfo);
616 sp parentProvider = deviceInfo->mParentProvider.promote();
617 if (parentProvider == nullptr) {
618 return DEAD_OBJECT;
619 }
620 auto provider =
621 static_cast(parentProvider.get())->startProviderInterface();
622 if (provider == nullptr) {
623 return DEAD_OBJECT;
624 }
625 std::shared_ptr halCameraProvider =
626 std::make_shared(provider, provider->descriptor);
627 saveRef(DeviceMode::CAMERA, id, halCameraProvider);
628 //获取对端的CameraDevice的interface
629 auto interface = aidlDeviceInfo3->startDeviceInterface();
630 if (interface == nullptr) {
631 removeRef(DeviceMode::CAMERA, id);
632 return DEAD_OBJECT;
633 }
634 //interface是获取的CameraDevice对象
635 auto ret = interface->open(callback, session);
636 if (!ret.isOk()) {
637 removeRef(DeviceMode::CAMERA, id);
638 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
639 __FUNCTION__, id.c_str(), ret.getMessage());
640 return AidlProviderInfo::mapToStatusT(ret);
641 }
642 return OK;
643 }
自此openCamera走到CameraProviderManager就完成了framework层openCamera的动作。往下是走到vendor目录(厂商)CameraDevice实现->厂商 hardware openCamera->Camera hal openCamera()。
path:xref: /hardware/interfaces/camera/device/3.3/default/CameraDevice.cpp(谷歌默认接口)
path:vendor/xxx/interfaces/camera/device/3.xx/default/CameraDevice.cpp(厂商定义的实现)