Android8.0 Camera系統架構(一)
隨著Android系統的不斷升級,相機子系統框架也在不斷進化,由最初的API1和HAL1到現在的API2和HAL3,由最初簡單的拍照,錄製到現在的連拍,AI人像;可以說是架構上變動最大最頻繁的子系統。很多裝置仍然依賴相機 HAL1,因此 Android 7.0 繼續支援該模組。此外,Android 相機服務還支援同時實現兩種 HAL(1 和 3),如果您希望通過相機 HAL1 支援效能略低的前置攝像頭,並通過相機 HAL3 支援更為高階的後置攝像頭。Android 的相機硬體抽象層 (HAL) 可將 Camera 2 中較高級別的相機框架 API 連線到底層的相機驅動程式和硬體。相機子系統包括相機管道元件的實現,而相機 HAL 則可提供用於實現您的這些元件版本的介面。從 Android 8.0 開始,相機 HAL 介面是 Project Treble 的一部分,相應的 HIDL 介面在硬體/介面/相機中定義。該實現會封裝仍在使用舊版 API 的舊 HAL。從 Android 8.0 開始,相機 HAL 實現必須使用 HIDL API;不支援使用舊版介面。Android8.0下最新的相機架構具有更高的靈活性。架構如下:
重新設計 Android Camera API 的目的在於大幅提高應用對於 Android 裝置上的相機子系統的控制能力,同時重新組織 API,提高其效率和可維護性。藉助額外的控制能力,您可以更輕鬆地在 Android 裝置上構建高品質的相機應用,這些應用可在多種產品上穩定執行,同時仍會盡可能使用裝置專用演算法來最大限度地提升質量和效能。版本 3 相機子系統將多個執行模式整合為一個統一的檢視,您可以使用這種檢視實現之前的任何模式以及一些其他模式,例如連拍模式。這樣一來,便可以提高使用者對聚焦、曝光以及更多後期處理(例如降噪、對比度和銳化)效果的控制能力。此外,這種簡化的檢視還能夠使應用開發者更輕鬆地使用相機的各種功能。架構圖已經很清晰的描述了各層架構之間的關係,我們按圖索驥從最新的架構開始,再看完整的架構,最後我們回到應用層來看Camera子系統的設計。
1. CameraService
CameraManager與CameraService通過Binder機制,形成推拉回調
frameworks\base\core\java\android\hardware\camera2\CameraManager.java
frameworks\av\camera\aidl\android\hardware\ICameraService.aidl
frameworks\av\camera\aidl\android\hardware\ICameraServiceListener.aidl
private static final class CameraManagerGlobal extends ICameraServiceListener.Stub {
......
public ICameraService getCameraService() {
synchronized(mLock) {
connectCameraServiceLocked(); //連線服務
if (mCameraService == null) {
Log.e(TAG, "Camera service is unavailable");
}
return mCameraService;
}
}
......
}
CameraManager通過CameraManagerGlobal訪問CameraService服務,並註冊監聽,CamreaService持有CameraServiceListener列表,並回調結果給CameraManager
private void connectCameraServiceLocked() {
//查詢服務引用
IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
//轉換服務介面
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
try {
CameraStatus[] cameraStatuses = cameraService.addListener(this); //註冊回撥監聽
//存副本
mCameraService = cameraService;
}
}
frameworks\av\services\camera\libcameraservice\CameraService.h
class CameraService :
public BinderService<CameraService>,
public virtual ::android::hardware::BnCameraService, //Bn端(服務端)
public virtual IBinder::DeathRecipient {.....}
註冊CameraService回撥監聽
Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
std::vector<hardware::CameraStatus> *cameraStatuses) {
{
Mutex::Autolock lock(mStatusListenerLock);
for (auto& it : mListenerList) {
if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
}
}
mListenerList.push_back(listener); //註冊
}
return Status::ok();
}
CameraService初始化
void CameraService::onFirstRef()
{
BnCameraService::onFirstRef();
res = enumerateProviders(); //列舉Provider
CameraService::pingCameraServiceProxy();
}
呼叫CameraProviderManager列舉裝置
status_t CameraService::enumerateProviders() {
if (nullptr == mCameraProviderManager.get()) {
mCameraProviderManager = new CameraProviderManager();
res = mCameraProviderManager->initialize(this); //初始化
}
mNumberOfCameras = mCameraProviderManager->getCameraCount(); //相機數目
mNumberOfNormalCameras =
mCameraProviderManager->getAPI1CompatibleCameraCount(); //可用API
mCameraProviderManager->setUpVendorTags(); //第三方廠商Tag
if (nullptr == mFlashlight.get()) {
mFlashlight = new CameraFlashlight(mCameraProviderManager, this); //閃光燈
}
res = mFlashlight->findFlashUnits();
return OK;
}
2. CameraProviderManager
frameworks\av\services\camera\libcameraservice\common\CameraProviderManager.cpp
status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
ServiceInteractionProxy* proxy) {
std::lock_guard<std::mutex> lock(mInterfaceMutex);
mListener = listener;
mServiceProxy = proxy;
// Registering will trigger notifications for all already-known providers
bool success = mServiceProxy->registerForNotifications( //註冊代理通知
/* instance name, empty means no filter */ "",
this);
return INVALID_OPERATION;
}
//新增提供者
addProviderLocked(kLegacyProviderName, /*expected*/ false);
return OK;
}
查詢初始化並儲存Provider; mServiceProxy是ServiceInteractionProxy*;
status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) {
sp<provider::V2_4::ICameraProvider> interface;
interface = mServiceProxy->getService(newProvider); //獲取服務
sp<ProviderInfo> providerInfo =
new ProviderInfo(newProvider, interface, this);
status_t res = providerInfo->initialize(); //執行初始化
mProviders.push_back(providerInfo); //備份
return OK;
}
frameworks\av\services\camera\libcameraservice\common\CameraProviderManager.h
struct ServiceInteractionProxy {
virtual bool registerForNotifications(
const std::string &serviceName,
const sp<hidl::manager::V1_0::IServiceNotification>
¬ification) = 0;
virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
const std::string &serviceName) = 0;
virtual ~ServiceInteractionProxy() {}
};
// Standard use case - call into the normal generated static methods which invoke
// the real hardware service manager
struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
virtual bool registerForNotifications(
const std::string &serviceName,
const sp<hidl::manager::V1_0::IServiceNotification>
¬ification) override {
return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
serviceName, notification);
}
virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
const std::string &serviceName) override { //呼叫HAL
return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
}
};
3. Camera硬體抽象層
hardware\interfaces\camera\provider\2.4\default\CameraProvider.h
struct CameraProvider : public ICameraProvider, public camera_module_callbacks_t {......}
執行初始化
bool CameraProvider::initialize() {
camera_module_t *rawModule;
int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID, //熟悉的配方,熟悉的操作
(const hw_module_t **)&rawModule);
mModule = new CameraModule(rawModule); //封裝了一層
err = mModule->init();
// Setup callback now because we are going to try openLegacy next
err = mModule->setCallbacks(this);
mNumberOfLegacyCameras = mModule->getNumberOfCameras();
for (int i = 0; i < mNumberOfLegacyCameras; i++) {
struct camera_info info;
auto rc = mModule->getCameraInfo(i, &info); //查相機資訊
char cameraId[kMaxCameraIdLen];
snprintf(cameraId, sizeof(cameraId), "%d", i);
std::string cameraIdStr(cameraId);
mCameraStatusMap[cameraIdStr] = CAMERA_DEVICE_STATUS_PRESENT;
mCameraIds.add(cameraIdStr);
......
}
return false; // mInitFailed
}
hardware\interfaces\camera\common\1.0\default\CameraModule.cpp
CameraModule::CameraModule(camera_module_t *module) {
mModule = module; //save this ref
}
做了一些版本相關處理
int CameraModule::init() {
ATRACE_CALL();
int res = OK;
if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
mModule->init != NULL) {
ATRACE_BEGIN("camera_module->init");
res = mModule->init(); //初始化
ATRACE_END();
}
mCameraInfoMap.setCapacity(getNumberOfCameras());
return res;
}
hardware\libhardware\include\hardware\camera_common.h
最終通過HAL與相機裝置驅動互動
{
......
*/
int (*init)();
/* reserved for future use */
void* reserved[5];
} camera_module_t;
讓我們往回倒一下車,providerInfo->initialize();
status_t CameraProviderManager::ProviderInfo::initialize() {
status_t res = parseProviderName(mProviderName, &mType, &mId);
hardware::Return<Status> status = mInterface->setCallback(this);
hardware::Return<bool> linked = mInterface->linkToDeath(this, /*cookie*/ mId);
//初始化相機裝置
// Get initial list of camera devices, if any
std::vector<std::string> devices;
hardware::Return<void> ret = mInterface->getCameraIdList([&status, &devices]( //獲取device
Status idStatus,
const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) {
status = idStatus;
sp<StatusListener> listener = mManager->getStatusListener();
for (auto& device : devices) {
std::string id;
status_t res = addDevice(device, //天加device
hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id);
}
for (auto& device : mDevices) {
mUniqueCameraIds.insert(device->mId);
if (device->isAPI1Compatible()) {
mUniqueAPI1CompatibleCameraIds.insert(device->mId);
}
}
mUniqueDeviceCount = mUniqueCameraIds.size();
return OK;
}
status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name,
CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) {
uint16_t major, minor;
std::string type, id;
status_t res = parseDeviceName(name, &major, &minor, &type, &id); //解析裝置名
if (mManager->isValidDeviceLocked(id, major)) { //驗證
return BAD_VALUE;
}
std::unique_ptr<DeviceInfo> deviceInfo;
switch (major) {
case 1:
deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, mProviderTagid, //Device1
id, minor);
break;
case 3:
deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid, //Device3
id, minor);
break;
default:
return BAD_VALUE;
}
if (deviceInfo == nullptr) return BAD_VALUE;
deviceInfo->mStatus = initialStatus; //回撥設定
mDevices.push_back(std::move(deviceInfo)); //儲存引用
if (parsedId != nullptr) {
*parsedId = id;
}
return OK;
}
初始化裝置資訊
template<class DeviceInfoT>
std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo>
CameraProviderManager::ProviderInfo::initializeDeviceInfo(
const std::string &name, const metadata_vendor_id_t tagId,
const std::string &id, uint16_t minorVersion) const {
Status status;
auto cameraInterface =
getDeviceInterface<typename DeviceInfoT::InterfaceT>(name); //獲取HAL裝置遠端介面
if (cameraInterface == nullptr) return nullptr;
return std::unique_ptr<DeviceInfo>( //返回裝置資訊
new DeviceInfoT(name, tagId, id, minorVersion, resourceCost,
cameraInterface));
}
通過ICameraDevice關聯硬體抽象層
template<>
sp<device::V1_0::ICameraDevice>
CameraProviderManager::ProviderInfo::getDeviceInterface
<device::V1_0::ICameraDevice>(const std::string &name) const {
Status status;
sp<device::V1_0::ICameraDevice> cameraInterface;
hardware::Return<void> ret;
ret = mInterface->getCameraDeviceInterface_V1_x(name, [&status, &cameraInterface](
Status s, sp<device::V1_0::ICameraDevice> interface) {
status = s;
cameraInterface = interface;
});
return cameraInterface;
}
硬體抽象層呼叫介面(Treble架構)
struct CameraDevice : public ICameraDevice {......}
4. CameraDeviceClient與CameraDevice
新的架構圖中右邊的分支已經跟蹤完畢,我們回過頭來看左邊的分支
frameworks/base/core/java/android/hardware/camera2/CameraManager.java
private CameraDevice openCameraDeviceUserAsync(String cameraId,
CameraDevice.StateCallback callback, Handler handler, final int uid)
throws CameraAccessException {
CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
CameraDevice device = null;
synchronized (mLock) {
ICameraDeviceUser cameraUser = null;
//CameraDeviceImpl 為 CameraDevice抽象類的自類
android.hardware.camera2.impl.CameraDeviceImpl deviceImpl =
new android.hardware.camera2.impl.CameraDeviceImpl(
cameraId,
callback,
handler,
characteristics,
mContext.getApplicationInfo().targetSdkVersion);
ICameraDeviceCallbacks callbacks = deviceImpl.getCallbacks();
try {
if (supportsCamera2ApiLocked(cameraId)) {
// Use cameraservice's cameradeviceclient implementation for HAL3.2+ devices
ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
//連線相機Device
cameraUser = cameraService.connectDevice(callbacks, cameraId,
mContext.getOpPackageName(), uid);
} else {
// Use legacy camera implementation for HAL1 devices
//使用舊版HAL1
cameraUser = CameraDeviceUserShim.connectBinderShim(callbacks, id);
}
} catch (ServiceSpecificException e) {
......
}
// TODO: factor out callback to be non-nested, then move setter to constructor
// For now, calling setRemoteDevice will fire initial
// onOpened/onUnconfigured callbacks.
// This function call may post onDisconnected and throw CAMERA_DISCONNECTED if
// cameraUser dies during setup.
deviceImpl.setRemoteDevice(cameraUser); //設定
device = deviceImpl;
}
return device;
}
frameworks/av/services/camera/libcameraservice/CameraService.cpp
Status CameraService::connectDevice(
const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
const String16& cameraId,
const String16& clientPackageName,
int clientUid,
/*out*/
sp<hardware::camera2::ICameraDeviceUser>* device) {
ATRACE_CALL();
Status ret = Status::ok();
String8 id = String8(cameraId);
sp<CameraDeviceClient> client = nullptr;
//呼叫模板函式
ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
clientUid, USE_CALLING_PID, API_2,
/*legacyMode*/ false, /*shimUpdateOnly*/ false,
/*out*/client);
if(!ret.isOk()) {
logRejected(id, getCallingPid(), String8(clientPackageName),
ret.toString8());
return ret;
}
*device = client; //返回裝置
return ret;
}
模板函式
template<class CALLBACK, class CLIENT>
Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
/*out*/sp<CLIENT>& device) {
binder::Status ret = binder::Status::ok();
String8 clientName8(clientPackageName);
int originalClientPid = 0;
sp<CLIENT> client = nullptr;
{
......
// Enforce client permissions and do basic sanity checks
if(!(ret = validateConnectLocked(cameraId, clientName8, //連線驗證
/*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
return ret;
}
// Check the shim parameters after acquiring lock, if they have already been updated and
// we were doing a shim update, return immediately
if (shimUpdateOnly) {
auto cameraState = getCameraState(cameraId);
if (cameraState != nullptr) {
if (!cameraState->getShimParams().isEmpty()) return ret;
}
}
......
sp<BasicClient> tmp = nullptr;
if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
/*out*/&tmp)).isOk()) {
return ret;
}
client = static_cast<CLIENT*>(tmp.get());
//初始化
err = client->initialize(mCameraProviderManager);
// Update shim paremeters for legacy clients
if (effectiveApiLevel == API_1) { //系統舊版本API1
// Assume we have always received a Client subclass for API1
sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
String8 rawParams = shimClient->getParameters();
CameraParameters params(rawParams);
auto cameraState = getCameraState(cameraId);
if (cameraState != nullptr) {
cameraState->setShimParams(params);
}
}
} // lock is destroyed, allow further connect calls
device = client;
return ret;
}
建立不同HAL版本對應的相機Client
Status CameraService::makeClient(const sp<CameraService>& cameraService,
const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
/*out*/sp<BasicClient>* client) {
if (halVersion < 0 || halVersion == deviceVersion) {
// Default path: HAL version is unspecified by caller, create CameraClient
// based on device version reported by the HAL.
switch(deviceVersion) {
//HAL1的架構
case CAMERA_DEVICE_API_VERSION_1_0:
if (effectiveApiLevel == API_1) { // Camera1 API route
sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
*client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
facing, clientPid, clientUid, getpid(), legacyMode);
} else { // Camera2 API route
ALOGW("Camera using old HAL version: %d", deviceVersion);
return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
"Camera device \"%s\" HAL version %d does not support camera2 API",
cameraId.string(), deviceVersion);
}
break;
//HAL3的架構
case CAMERA_DEVICE_API_VERSION_3_0:
case CAMERA_DEVICE_API_VERSION_3_1:
case CAMERA_DEVICE_API_VERSION_3_2:
case CAMERA_DEVICE_API_VERSION_3_3:
case CAMERA_DEVICE_API_VERSION_3_4:
if (effectiveApiLevel == API_1) { // Camera1 API route
sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
*client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
facing, clientPid, clientUid, servicePid, legacyMode);
} else { // Camera2 API route
sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
*client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
facing, clientPid, clientUid, servicePid);
}
break;
default:
// Should not be reachable
ALOGE("Unknown camera device HAL version: %d", deviceVersion);
return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
"Camera device \"%s\" has unknown HAL version %d",
cameraId.string(), deviceVersion);
}
} else {
// A particular HAL version is requested by caller. Create CameraClient
// based on the requested HAL version.
if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
// Only support higher HAL version device opened as HAL1.0 device.
sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
*client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
facing, clientPid, clientUid, servicePid, legacyMode);
} else {
// Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
" opened as HAL %x device", halVersion, deviceVersion,
CAMERA_DEVICE_API_VERSION_1_0);
return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
"Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
cameraId.string(), deviceVersion, halVersion);
}
}
return Status::ok();
}
frameworks/av/services/camera/libcameraservice/api2/CameraDeviceClient.h
struct CameraDeviceClientBase :
public CameraService::BasicClient,
public hardware::camera2::BnCameraDeviceUser //CameraDeviceUser的服務端
{
typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks; //該回調在CameraDeviceImpl
const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
return mRemoteCallback;
}
......
};
HAL3對應的CameraDeviceClient
class CameraDeviceClient :
public Camera2ClientBase<CameraDeviceClientBase>,
public camera2::FrameProcessorBase::FilteredListener
{......}
如此以來CameraDeviceClient就繼承了CameraDeviceClientBase,間接繼承了BnCameraDeviceUser
template <typename TClientBase>
class Camera2ClientBase :
public TClientBase,
public CameraDeviceBase::NotificationListener
{
持有遠端mRemoteDevice的Binder物件
public void setRemoteDevice(ICameraDeviceUser remoteDevice) throws CameraAccessException {
synchronized(mInterfaceLock) {
mRemoteDevice = new ICameraDeviceUserWrapper(remoteDevice); //包裝
IBinder remoteDeviceBinder = remoteDevice.asBinder();//遠端Binder服務,HAL3架構下是CameraDeviceClient
......
mDeviceHandler.post(mCallOnOpened);
mDeviceHandler.post(mCallOnUnconfigured);
}
}
通過此遠端回撥將CamreaDevice與CameraDeviceClient聯絡起來,處理來自CameraDeviceClient的訊息
public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub {......}
5. CameraDeviceClient與Camera3Device
回到剛才的err = client->initialize(mCameraProviderManager);
status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager) {
return initializeImpl(manager);
}
template<typename TProviderPtr>
status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr) {
ATRACE_CALL();
status_t res;
res = Camera2ClientBase::initialize(providerPtr); //呼叫初始化
if (res != OK) {
return res;
}
......
return OK;
}
frameworks/av/services/camera/libcameraservice/common/Camera2ClientBase.cpp
template <typename TClientBase>
status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager) {
return initializeImpl(manager);
}
template <typename TClientBase>
template <typename TProviderPtr>
status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr) {
ATRACE_CALL();
ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
TClientBase::mCameraIdStr.string());
status_t res;
// Verify ops permissions
res = TClientBase::startCameraOps();
if (res != OK) {
return res;
}
if (mDevice == NULL) {
ALOGE("%s: Camera %s: No device connected",
__FUNCTION__, TClientBase::mCameraIdStr.string());
return NO_INIT;
}
res = mDevice->initialize(providerPtr); //呼叫具體的裝置初始化
if (res != OK) {
ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
__FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
return res;
}
wp<CameraDeviceBase::NotificationListener> weakThis(this);
res = mDevice->setNotifyCallback(weakThis);
return OK;
}
frameworks/av/services/camera/libcameraservice/common/Camera2ClientBase.h
sp<CameraDeviceBase> mDevice;
rameworks/av/services/camera/libcameraservice/device3/Camera3Device.h
class Camera3Device :
public CameraDeviceBase, //繼承了CameraDeviceBase
virtual public hardware::camera::device::V3_2::ICameraDeviceCallback,
private camera3_callback_ops {
frameworks/av/services/camera/libcameraservice/device3/Camera3Device.cpp
status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
......
sp<ICameraDeviceSession> session;
ATRACE_BEGIN("CameraHal::openSession");
status_t res = manager->openSession(mId.string(), this, //通過session獲取Device
/*out*/ &session);
res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo); //通過相機引數獲取Device
std::shared_ptr<RequestMetadataQueue> queue;
auto requestQueueRet = session->getCaptureRequestMetadataQueue(//元資料捕獲請求佇列
[&queue](const auto& descriptor) {
queue = std::make_shared<RequestMetadataQueue>(descriptor);
if (!queue->isValid() || queue->availableToWrite() <= 0) {
ALOGE("HAL returns empty request metadata fmq, not use it");
queue = nullptr;
// don't use the queue onwards.
}
});
std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
auto resultQueueRet = session->getCaptureResultMetadataQueue(//元資料捕獲結果佇列
[&resQueue](const auto& descriptor) {
resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
ALOGE("HAL returns empty result metadata fmq, not use it");
resQueue = nullptr;
// Don't use the resQueue onwards.
}
});
IF_ALOGV() {
session->interfaceChain([](
::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
ALOGV("Session interface chain:");
for (auto iface : interfaceChain) {
ALOGV(" %s", iface.c_str());
}
});
}
mInterface = new HalInterface(session, queue); //新建硬體抽象層介面例項
std::string providerType;
mVendorTagId = manager->getProviderTagIdLocked(mId.string());
return initializeCommonLocked();
}
至此新Camera架構整個藍圖構建完成,我們只是按圖索驥從Java層一直到HAL層,事實上Camera大部分重要的東西都在其驅動演算法層,我們暫且不去深究。下一篇我們將從整個新舊架構的角度去分析Camera系統。