Android Camera從App層到framework層到HAL層的初始化過程
Android camera 從上到下可以分為四個部分:
Application層、
framework層、
HAL(hardware abstract layer)層、
Kernel層
一般面向開發者的話主要集中在Application層和framework層,但是一些廠商需要定製一些屬於自己特有的東西的時候會去修改HAL層整合自己的一些東西,至於Kernel層一般的開發人員不會涉及到。
下面是一個簡要的camera框架圖:
APP層通過Java呼叫framework層的Java類,framework層的不止包含Java類,還包含jni呼叫和camera client通過IPC Binder繫結獲取下層的服務,然後進入HAL層。
在framework層中,Java框架通過JNI的方式呼叫Native框架,此處的Native作為Client端只給上層應用提供呼叫介面,而具體的業務實現則是由Server端(CameraService)來實現,Client和Server則是通過Binder的方式進行通訊。單獨分析下Camera的C/S架構,其架構圖如下
對於Android camera來說,其主要有:initialize,preview,takepicture三個關鍵點
由於我下載的Android原始碼的版本是7.1.1,版本較新,所以可能分析的內容和以前不大一樣。
下面從Application層開始分析
1、camera的初始化
Camera2的初始化流程與Camera1.0有所區別,本文將就Camera2的內建應用來分析Camera2.0的初始化過程。Camera2.0首先啟動的是CameraActivity,而它繼承自QuickActivity,在程式碼中你會發現沒有重寫OnCreate等生命週期方法,因為此處採用的是模板方法的設計模式,在QuickActivity中的onCreate方法呼叫的是onCreateTasks等方法,所以要看onCreate方法就只須看onCreateTasks方法即可
CameraActivity.java 位於package/apps/Camera2/src/com/camera/ 目錄下
//CameraActivity.java
@Override
public void onCreateTasks(Bundle state) {
Profile profile = mProfiler.create("CameraActivity.onCreateTasks").start();
…
try {
//①初始化mOneCameraOpener物件和mOneCameraManager 物件
mOneCameraOpener = OneCameraModule.provideOneCameraOpener(
mFeatureConfig, mAppContext,mActiveCameraDeviceTracker,
ResolutionUtil.getDisplayMetrics(this));
mOneCameraManager = OneCameraModule.provideOneCameraManager();
} catch (OneCameraException e) {...}
…
//②設定模組資訊
ModulesInfo.setupModules(mAppContext, mModuleManager, mFeatureConfig);
…
//③進行初始化
mCurrentModule.init(this, isSecureCamera(), isCaptureIntent());
…
}
從上面的程式碼中可以看出其主要分為三個步驟:
①初始化mOneCameraOpener物件和mOneCameraManager 物件
②設定模組資訊
③進行初始化
(1)、第一個步驟,初始化OneCameraOpener物件和mOneCameraManager物件,我們初始化相機就必須得開啟相機,並且對開啟的相機進行管理,所以這裡有初始化mOneCameraOpener物件和mOneCameraManager物件,這兩個物件的初始化是在OneCameraModule.java中進行的
OneCameraModule.java 位於package/apps/Camera2/src/com/camera/目錄下
//OneCameraModule.java
public static OneCameraOpener provideOneCameraOpener(OneCameraFeatureConfig featureConfig,Context context,ActiveCameraDeviceTracker activeCameraDeviceTracker,DisplayMetrics displayMetrics) throws OneCameraException {
Optional<OneCameraOpener> manager = Camera2OneCameraOpenerImpl.create(
featureConfig, context, activeCameraDeviceTracker, displayMetrics);
...
return manager.get();
}
public static OneCameraManager provideOneCameraManager() throws OneCameraException {
Optional<Camera2OneCameraManagerImpl> camera2HwManager = Camera2OneCameraManagerImpl.create();
if (camera2HwManager.isPresent()) {
return camera2HwManager.get();
}
...
}
它呼叫Camera2OneCameraOpenerImpl的create方法來獲得一個OneCameraOpener物件和呼叫Camera2OneCameraManagerImpl的create方法來獲得一個OneCameraManager物件。
//Camera2OneCameraOpenerImpl.java
public static Optional<OneCameraOpener> create(OneCameraFeatureConfig featureConfig,Context context,ActiveCameraDeviceTracker activeCameraDeviceTracker,DisplayMetrics displayMetrics) {
...
OneCameraOpener oneCameraOpener = new Camera2OneCameraOpenerImpl(featureConfig,context,cameraManager,activeCameraDeviceTracker,displayMetrics);
return Optional.of(oneCameraOpener);
}
//Camera2OneCameraManagerImpl.java
public static Optional<Camera2OneCameraManagerImpl> create() {
...
Camera2OneCameraManagerImpl hardwareManager =
new Camera2OneCameraManagerImpl(cameraManager);
return Optional.of(hardwareManager);
}
(2)設定模組資訊,即 ModulesInfo.setupModules(mAppContext, mModuleManager, mFeatureConfig);這部分內容比較重要,主要是根據配置資訊,進行一系列模組的註冊:
//ModulesInfo.java
public static void setupModules(Context context, ModuleManager moduleManager,OneCameraFeatureConfig config) {
...
//註冊Photo模組
registerPhotoModule(moduleManager, photoModuleId, SettingsScopeNamespaces.PHOTO,config.isUsingCaptureModule());
//設定為預設的模組
moduleManager.setDefaultModuleIndex(photoModuleId);
//註冊Video模組
registerVideoModule(moduleManager, res.getInteger(R.integer.camera_mode_video),SettingsScopeNamespaces.VIDEO);
if (PhotoSphereHelper.hasLightCycleCapture(context)) {//開啟閃光
//註冊廣角鏡頭
registerWideAngleModule(moduleManager, res.getInteger(
R.integer.camera_mode_panorama),SettingsScopeNamespaces
.PANORAMA);
//註冊光球模組
registerPhotoSphereModule(moduleManager,res.getInteger(
R.integer.camera_mode_photosphere),
SettingsScopeNamespaces.PANORAMA);
}
//若需重新聚焦
if (RefocusHelper.hasRefocusCapture(context)) {
//註冊重聚焦模組
registerRefocusModule(moduleManager, res.getInteger(
R.integer.camera_mode_refocus),
SettingsScopeNamespaces.REFOCUS);
}
//如果有色分離模組
if (GcamHelper.hasGcamAsSeparateModule(config)) {
//註冊色分離模組
registerGcamModule(moduleManager, res.getInteger(
R.integer.camera_mode_gcam),SettingsScopeNamespaces.PHOTO,
config.getHdrPlusSupportLevel(OneCamera.Facing.BACK));
}
int imageCaptureIntentModuleId = res.getInteger(
R.integer.camera_mode_capture_intent);
registerCaptureIntentModule(moduleManager,
imageCaptureIntentModuleId,SettingsScopeNamespaces.PHOTO,
config.isUsingCaptureModule());
}
因為開啟Camera應用,既可以拍照片也可以拍視訊,此處,只分析PhotoModule的註冊
private static void registerPhotoModule(ModuleManager moduleManager, final int moduleId,
final String namespace, final boolean enableCaptureModule) {
moduleManager.registerModule(new ModuleManager.ModuleAgent() {
...
public ModuleController createModule(AppController app, Intent intent) {
Log.v(TAG, "EnableCaptureModule = " + enableCaptureModule);
return enableCaptureModule ? new CaptureModule(app) : new PhotoModule(app);
}
});
}
由上面的程式碼可知,註冊PhotoModule的關鍵還是在moduleManager.registerModule中的ModuleController createModule方法根據enableCaptureModule ? new CaptureModule(app) : new PhotoModule(app);來判斷是進行拍照模組的註冊還是Photo模組的註冊。
(3)進行初始化 mCurrentModule.init();
//CaptureModule.java
public void init(CameraActivity activity, boolean isSecureCamera, boolean
isCaptureIntent) {
...
HandlerThread thread = new HandlerThread("CaptureModule.mCameraHandler");
thread.start();
mCameraHandler = new Handler(thread.getLooper());
//獲取第一步中建立的OneCameraOpener 和OneCameraManager物件
mOneCameraOpener = mAppController.getCameraOpener();
...
//獲取前面建立的OneCameraManager物件
mOneCameraManager = OneCameraModule.provideOneCameraManager();
`...
//新建CaptureModule的UI
mUI = new CaptureModuleUI(activity, mAppController.
getModuleLayoutRoot(), mUIListener);
//設定預覽狀態的監聽
mAppController.setPreviewStatusListener(mPreviewStatusListener);
synchronized (mSurfaceTextureLock) {
//獲取SurfaceTexture
mPreviewSurfaceTexture = mAppController.getCameraAppUI()
.getSurfaceTexture();
}
}
先獲取建立的OneCameraOpener物件以及OneCameraManager物件,然後再設定預覽狀態監聽,這裡主要分析預覽狀態的監聽:
//CaptureModule.java
private final PreviewStatusListener mPreviewStatusListener = new
PreviewStatusListener() {
...
public void onSurfaceTextureAvailable(SurfaceTexture surface,
int width, int height) {
updatePreviewTransform(width, height, true);
synchronized (mSurfaceTextureLock) {
mPreviewSurfaceTexture = surface;
}
//開啟Camera
reopenCamera();
}
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.d(TAG, "onSurfaceTextureDestroyed");
synchronized (mSurfaceTextureLock) {
mPreviewSurfaceTexture = null;
}
//關閉Camera
closeCamera();
return true;
}
public void onSurfaceTextureSizeChanged(SurfaceTexture surface,
int width, int height) {
//更新預覽尺寸
updatePreviewBufferSize();
}
...
};
當SurfaceTexture的狀態變成可用的時候,會呼叫reopenCamera()方法來開啟Camera,分析reopenCamera()方法:
private void reopenCamera() {
if (mPaused) {
return;
}
AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
closeCamera();
if(!mAppController.isPaused()) {
//開啟相機並預覽
openCameraAndStartPreview();
}
}
});
}
繼續分析openCameraAndStartPreview();
//CaptureModule.java
private void openCameraAndStartPreview() {
...
if (mOneCameraOpener == null) {
...
//釋放CameraOpenCloseLock鎖
mCameraOpenCloseLock.release();
mAppController.getFatalErrorHandler().onGenericCameraAccessFailure();
guard.stop("No OneCameraManager");
return;
}
// Derive objects necessary for camera creation.
MainThread mainThread = MainThread.create();
//查詢需要開啟的CameraId
CameraId cameraId = mOneCameraManager.findFirstCameraFacing(mCameraFacing);
...
//開啟Camera
mOneCameraOpener.open(cameraId, captureSetting, mCameraHandler,
mainThread, imageRotationCalculator, mBurstController,
mSoundPlayer,new OpenCallback() {
public void onFailure() {
//進行失敗的處理
...
}
@Override
public void onCameraClosed() {
...
}
@Override
public void onCameraOpened(@Nonnull final OneCamera camera) {
Log.d(TAG, "onCameraOpened: " + camera);
mCamera = camera;
if (mAppController.isPaused()) {
onFailure();
return;
}
...
mMainThread.execute(new Runnable() {
@Override
public void run() {
//通知UI,Camera狀態變化
mAppController.getCameraAppUI().onChangeCamera();
//使能拍照按鈕
mAppController.getButtonManager().enableCameraButton();
}
});
//至此,Camera開啟成功,開始預覽
camera.startPreview(new Surface(getPreviewSurfaceTexture()),
new CaptureReadyCallback() {
@Override
public void onSetupFailed() {
...
}
@Override
public void onReadyForCapture() {
//釋放鎖
mCameraOpenCloseLock.release();
mMainThread.execute(new Runnable() {
@Override
public void run() {
...
onPreviewStarted();
...
onReadyStateChanged(true);
//設定CaptureModule為Capture準備的狀態監聽
mCamera.setReadyStateChangedListener(
CaptureModule.this);
mUI.initializeZoom(mCamera.getMaxZoom());
mCamera.setFocusStateListener(
CaptureModule.this);
}
});
}
});
}
}, mAppController.getFatalErrorHandler());
guard.stop("mOneCameraOpener.open()");
}
}
這裡主要會呼叫Camera2OneCameraOpenerImpl的open方法來開啟Camera,並定義了開啟的回撥函式,對開啟結束後的結果進行處理,如失敗則釋放mCameraOpenCloseLock,並暫停mAppController,如果開啟成功,通知UI成功,並開啟Camera的Preview(在回撥new OpenCallback中開啟預覽函式camera.startPreview()),並且定義了Preview的各種回撥操作(如new CaptureReadyCallback()中的是否設定失敗onSetupFailed,是否準備好拍照onReadyForCapture),針對Open過程,所以繼續分析:
//Camera2OneCameraOpenerImpl.java
public void open(
...
mActiveCameraDeviceTracker.onCameraOpening(cameraKey);
//開啟Camera,此處呼叫framework層的CameraManager類的openCamera,進入frameworks層
mCameraManager.openCamera(cameraKey.getValue(),
new CameraDevice.StateCallback() {
private boolean isFirstCallback = true;
...
public void onOpened(CameraDevice device) {
//第一次呼叫此回撥
if (isFirstCallback) {
isFirstCallback = false;
try {
CameraCharacteristics characteristics = mCameraManager
.getCameraCharacteristics(device.getId());
...
//建立OneCamera物件
OneCamera oneCamera = OneCameraCreator.create(device,
characteristics, mFeatureConfig, captureSetting,
mDisplayMetrics, mContext, mainThread,
imageRotationCalculator, burstController, soundPlayer,
fatalErrorHandler);
if (oneCamera != null) {
//如果oneCamera不為空,則回撥onCameraOpened,後面將做分析
openCallback.onCameraOpened(oneCamera);
} else {
...
openCallback.onFailure();
}
} catch (CameraAccessException e) {
openCallback.onFailure();
} catch (OneCameraAccessException e) {
Log.d(TAG, "Could not create OneCamera", e);
openCallback.onFailure();
}
}
}
}, handler);
...
}
接著呼叫CameraManager的openCamera方法開啟相機,進入到framework層進行初始化
(CameraManager是連線App層和framework層的橋樑)
下面是App層初始化的時序圖:
2、framework層的初始化
通過上面的CameraManager的openCamera進入到framework層,下面我們看openCamera方法:
//CameraManager.java
public void openCamera(@NonNull String cameraId,
@NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler)
throws CameraAccessException {
openCameraForUid(cameraId, callback, handler, USE_CALLING_UID);
}
openCamera方法呼叫了openCameraForUid方法,
//CameraManager.java
public void openCameraForUid(@NonNull String cameraId,
@NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler,
int clientUid)
throws CameraAccessException {
...
openCameraDeviceUserAsync(cameraId, callback, handler, clientUid);
}
接著呼叫了openCameraDeviceUserAsync方法:
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;
android.hardware.camera2.impl.CameraDeviceImpl deviceImpl =
new android.hardware.camera2.impl.CameraDeviceImpl(
cameraId,
callback,
handler,
characteristics);
ICameraDeviceCallbacks callbacks = deviceImpl.getCallbacks();
...
try {
if (supportsCamera2ApiLocked(cameraId)) {
ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
...
cameraUser = cameraService.connectDevice(callbacks, id,
mContext.getOpPackageName(), uid);
} else {
// Use legacy camera implementation for HAL1 devices
Log.i(TAG, "Using legacy camera HAL.");
cameraUser = CameraDeviceUserShim.connectBinderShim(callbacks, id);
}
} catch (ServiceSpecificException e) {
...
} catch (RemoteException e) {
...
}
...
deviceImpl.setRemoteDevice(cameraUser);
device = deviceImpl;
}
return device;
}
該方法最重要的功能就是通過Java的Binder繫結功能獲取CameraService,然後通過CameraService連線到具體的裝置cameraUser = cameraService.connectDevice(callbacks, id, mContext.getOpPackageName(), uid);
//CameraService.cpp
Status CameraService::connectDevice(
const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
int cameraId,
const String16& clientPackageName,
int clientUid,
/*out*/
sp<hardware::camera2::ICameraDeviceUser>* device) {
ATRACE_CALL();
Status ret = Status::ok();
String8 id = String8::format("%d", 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;
}
呼叫connectHelper
//CameraService.h
binder::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;
ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
"Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
(halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
static_cast<int>(effectiveApiLevel));
sp<CLIENT> client = nullptr;
{
// Acquire mServiceLock and prevent other clients from connecting
std::unique_ptr<AutoConditionLock> lock =
AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
if (lock == nullptr) {
ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
, clientPid);
return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
"Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
cameraId.string(), clientName8.string(), clientPid);
}
// 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;
}
}
status_t err;
sp<BasicClient> clientTmp = nullptr;
std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
/*out*/&partial)) != NO_ERROR) {
switch (err) {
case -ENODEV:
return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
"No camera device with ID \"%s\" currently available",
cameraId.string());
case -EBUSY:
return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
"Higher-priority client using camera, ID \"%s\" currently unavailable",
cameraId.string());
default:
return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
"Unexpected error %s (%d) opening camera \"%s\"",
strerror(-err), err, cameraId.string());
}
}
if (clientTmp.get() != nullptr) {
// Handle special case for API1 MediaRecorder where the existing client is returned
device = static_cast<CLIENT*>(clientTmp.get());
return ret;
}
// give flashlight a chance to close devices if necessary.
mFlashlight->prepareDeviceOpen(cameraId);
// TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
int id = cameraIdToInt(cameraId);
if (id == -1) {
ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
cameraId.string());
return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
"Bad camera ID \"%s\" passed to camera open", cameraId.string());
}
int facing = -1;
int deviceVersion = getDeviceVersion(id, /*out*/&facing);
sp<BasicClient> tmp = nullptr;
if(!(ret = makeClient(this, cameraCb, clientPackageName, id, facing, clientPid,
clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
/*out*/&tmp)).isOk()) {
return ret;
}
client = static_cast<CLIENT*>(tmp.get());
LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
__FUNCTION__);
if ((err = client->initialize(mModule)) != OK) {
ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
// Errors could be from the HAL module open call or from AppOpsManager
switch(err) {
case BAD_VALUE:
return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
"Illegal argument to HAL module for camera \"%s\"", cameraId.string());
case -EBUSY:
return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
"Camera \"%s\" is already open", cameraId.string());
case -EUSERS:
return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
"Too many cameras already open, cannot open camera \"%s\"",
cameraId.string());
case PERMISSION_DENIED:
return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
"No permission to open camera \"%s\"", cameraId.string());
case -EACCES:
return STATUS_ERROR_FMT(ERROR_DISABLED,
"Camera \"%s\" disabled by policy", cameraId.string());
case -ENODEV:
default:
return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
"Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
strerror(-err), err);
}
}
// Update shim paremeters for legacy clients
if (effectiveApiLevel == API_1) {
// 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);
} else {
ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
__FUNCTION__, cameraId.string());
}
}
if (shimUpdateOnly) {
// If only updating legacy shim parameters, immediately disconnect client
mServiceLock.unlock();
client->disconnect();
mServiceLock.lock();
} else {
// Otherwise, add client to active clients list
finishConnectLocked(client, partial);
}
} // lock is destroyed, allow further connect calls
// Important: release the mutex here so the client can call back into the service from its
// destructor (can be at the end of the call)
device = client;
return ret;
}
呼叫makeClient
//CameraService.cpp
Status CameraService::makeClient(const sp<CameraService>& cameraService,
const sp<IInterface>& cameraCb, const String16& packageName, int 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) {
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, 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 \"%d\" HAL version %d does not support camera2 API",
cameraId, deviceVersion);
}
break;
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, 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 \"%d\" has unknown HAL version %d",
cameraId, 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, 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 \"%d\" (HAL version %d) cannot be opened as HAL version %d",
cameraId, deviceVersion, halVersion);
}
}
return Status::ok();
}
因為使用的Camera API 2.0,所以建立client=new CameraDeviceClient();
建立之後需要呼叫CameraDeviceClient的Initialize方法進行初始化(下面講)
//CameraDeviceClient.cpp
CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
const String16& clientPackageName,
int cameraId,
int cameraFacing,
int clientPid,
uid_t clientUid,
int servicePid) :
Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
cameraId, cameraFacing, clientPid, clientUid, servicePid),
mInputStream(),
mStreamingRequestId(REQUEST_ID_NONE),
mRequestIdCounter(0) {
ATRACE_CALL();
ALOGI("CameraDeviceClient %d: Opened", cameraId);
}
繼續呼叫Camera2ClientBase;並進行初始化
template <typename TClientBase>
Camera2ClientBase<TClientBase>::Camera2ClientBase(
const sp<CameraService>& cameraService,
const sp<TCamCallbacks>& remoteCallback,
const String16& clientPackageName,
int cameraId,
int cameraFacing,
int clientPid,
uid_t clientUid,
int servicePid):
TClientBase(cameraService, remoteCallback, clientPackageName,
cameraId, cameraFacing, clientPid, clientUid, servicePid),
mSharedCameraCallbacks(remoteCallback),
mDeviceVersion(cameraService->getDeviceVersion(cameraId)),
mDeviceActive(false)
{
ALOGI("Camera %d: Opened. Client: %s (PID %d, UID %d)", cameraId,
String8(clientPackageName).string(), clientPid, clientUid);
mInitialClientPid = clientPid;
mDevice = new Camera3Device(cameraId);
LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
}
建立mDevice=new Camera3Device();
Camera3Device::Camera3Device(int id):
mId(id),
mIsConstrainedHighSpeedConfiguration(false),
mHal3Device(NULL),
mStatus(STATUS_UNINITIALIZED),
mStatusWaiters(0),
mUsePartialResult(false),
mNumPartialResults(1),
mTimestampOffset(0),
mNextResultFrameNumber(0),
mNextReprocessResultFrameNumber(0),
mNextShutterFrameNumber(0),
mNextReprocessShutterFrameNumber(0),
mListener(NULL)
{
ATRACE_CALL();
camera3_callback_ops::notify = &sNotify;
camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
}
CameraDeviceClient建立物件之後需要呼叫initialize進行初始化:
status_t CameraDeviceClient::initialize(CameraModule *module)
{
ATRACE_CALL();
status_t res;
res = Camera2ClientBase::initialize(module);
if (res != OK) {
return res;
}
String8 threadName;
mFrameProcessor = new FrameProcessorBase(mDevice);
threadName = String8::format("CDU-%d-FrameProc", mCameraId);
mFrameProcessor->run(threadName.string());
mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
FRAME_PROCESSOR_LISTENER_MAX_ID,
/*listener*/this,
/*sendPartials*/true);
return OK;
}
這個初始化又呼叫了Camera2ClientBase的initialize(module);
template <typename TClientBase>
status_t Camera2ClientBase<TClientBase>::initialize(CameraModule *module) {
ATRACE_CALL();
ALOGV("%s: Initializing client for camera %d", __FUNCTION__,
TClientBase::mCameraId);
status_t res;
// Verify ops permissions
res = TClientBase::startCameraOps();
if (res != OK) {
return res;
}
if (mDevice == NULL) {
ALOGE("%s: Camera %d: No device connected",
__FUNCTION__, TClientBase::mCameraId);
return NO_INIT;
}
res = mDevice->initialize(module);
if (res != OK) {
ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
__FUNCTION__, TClientBase::mCameraId, strerror(-res), res);
return res;
}
wp<CameraDeviceBase::NotificationListener> weakThis(this);
res = mDevice->setNotifyCallback(weakThis);
return OK;
}
在Camera2ClientBase的初始化過程中又呼叫了Camera3Device的Initialize進行初始化
status_t Camera3Device::initialize(CameraModule *module)
{
ATRACE_CALL();
Mutex::Autolock il(mInterfaceLock);
Mutex::Autolock l(mLock);
ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
if (mStatus != STATUS_UNINITIALIZED) {
CLOGE("Already initialized!");
return INVALID_OPERATION;
}
/** Open HAL device */
status_t res;
String8 deviceName = String8::format("%d", mId);
camera3_device_t *device;
ATRACE_BEGIN("camera3->open");
res = module->open(deviceName.string(),
reinterpret_cast<hw_device_t**>(&device));
ATRACE_END();
if (res != OK) {
SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
return res;
}
/** Cross-check device version */
if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
SET_ERR_L("Could not open camera: "
"Camera device should be at least %x, reports %x instead",
CAMERA_DEVICE_API_VERSION_3_0,
device->common.version);
device->common.close(&device->common);
return BAD_VALUE;
}
camera_info info;
res = module->getCameraInfo(mId, &info);
if (res != OK) return res;
if (info.device_version != device->common.version) {
SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
" and device version (%x).",
info.device_version, device->common.version);
device->common.close(&device->common);
return BAD_VALUE;
}
/** Initialize device with callback functions */
ATRACE_BEGIN("camera3->initialize");
res = device->ops->initialize(device, this);
ATRACE_END();
if (res != OK) {
SET_ERR_L("Unable to initialize HAL device: %s (%d)",
strerror(-res), res);
device->common.close(&device->common);
return BAD_VALUE;
}
/** Start up status tracker thread */
mStatusTracker = new StatusTracker(this);
res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
if (res != OK) {
SET_ERR_L("Unable to start status tracking thread: %s (%d)",
strerror(-res), res);
device->common.close(&device->common);
mStatusTracker.clear();
return res;
}
/** Register in-flight map to the status tracker */
mInFlightStatusId = mStatusTracker->addComponent();
/** Create buffer manager */
mBufferManager = new Camera3BufferManager();
bool aeLockAvailable = false;
camera_metadata_ro_entry aeLockAvailableEntry;
res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
if (res == OK && aeLockAvailableEntry.count > 0) {
aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
}
/** Start up request queue thread */
mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
if (res != OK) {
SET_ERR_L("Unable to start request queue thread: %s (%d)",
strerror(-res), res);
device->common.close(&device->common);
mRequestThread.clear();
return res;
}
mPreparerThread = new PreparerThread();
/** Everything is good to go */
mDeviceVersion = device->common.version;
mDeviceInfo = info.static_camera_characteristics;
mHal3Device = device;
// Determine whether we need to derive sensitivity boost values for older devices.
// If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
// be listed (as the default value 100)
if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
mDerivePostRawSensKey = true;
}
internalUpdateStatusLocked(STATUS_UNCONFIGURED);
mNextStreamId = 0;
mDummyStreamId = NO_STREAM;
mNeedConfig = true;
mPauseStateNotify = false;
// Measure the clock domain offset between camera and video/hw_composer
camera_metadata_entry timestampSource =
mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
mTimestampOffset = getMonoToBoottimeOffset();
}
// Will the HAL be sending in early partial result metadata?
if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
camera_metadata_entry partialResultsCount =
mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
if (partialResultsCount.count > 0) {
mNumPartialResults = partialResultsCount.data.i32[0];
mUsePartialResult = (mNumPartialResults > 1);
}
} else {
camera_metadata_entry partialResultsQuirk =
mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
mUsePartialResult = true;
}
}
camera_metadata_entry configs =
mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
for (uint32_t i = 0; i < configs.count; i += 4) {
if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
configs.data.i32[i + 3] ==
ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
configs.data.i32[i + 2]));
}
}
return OK;
}
在Camera3Device的初始化中又三個重要的步驟:
第一個:module.open(),開啟模組;
第二個:device.ops.initialize()進行HAL層的初始化;
第三個:mRequestThread.run();啟動請求執行緒
至此,framework層的初始化已經完成了,接下來就是HAL層的初始化。
framework層初始化的時序圖:
3、HAL層的初始化
由Camera3Device.cpp中的initialize中的device.ops.initialize呼叫進入到HAL層的初