? ? ?
目錄
一、APP層源碼分析
??????????????????二,framework層代碼分析
???????????????2.1 binder溯源
????????這幾天擼了android11 aosp閃光燈源碼,本著前人栽樹后人乘涼的原則,有志于android系統開發的新同學們提供一盞明燈,照亮你們前行。
? ? ? ? 本人擼代碼風格,喜歡從app擼到kernel,啟航出發。
一、APP層源碼分析
? ? ? ? 下拉狀態欄,可見“手電筒”快捷開關,點擊后開啟手電筒,UI見下圖,
這部分代碼,在目錄:frameworks/base/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java,點擊按鈕回調,
protected void handleClick() {if (ActivityManager.isUserAMonkey()) {return;}boolean newState = !mState.value;refreshState(newState);//點擊“手電筒”核心核數mFlashlightController.setFlashlight(newState);}
通過?mFlashlightController.setFlashlight(newState)走到文件
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightControllerImpl.java,
public void setFlashlight(boolean enabled) {boolean pendingError = false;synchronized (this) {if (mCameraId == null) return;if (mFlashlightEnabled != enabled) {mFlashlightEnabled = enabled;try {//核心調用mCameraManager.setTorchMode(mCameraId, enabled);} catch (CameraAccessException e) {Log.e(TAG, "Couldn't set torch mode", e);mFlashlightEnabled = false;pendingError = true;}}}dispatchModeChanged(mFlashlightEnabled);if (pendingError) {dispatchError();}}
通過mCameraManager.setTorchMode(mCameraId, enabled)走到frameworks/base/core/java/android/hardware/camera2/CameraManager.java,
public void setTorchMode(@NonNull String cameraId, boolean enabled)throws CameraAccessException {if (CameraManagerGlobal.sCameraServiceDisabled) {throw new IllegalArgumentException("No cameras available on device");}CameraManagerGlobal.get().setTorchMode(cameraId, enabled);}
CameraManagerGlobal.get().setTorchMode(cameraId, enabled)走到,
public void setTorchMode(String cameraId, boolean enabled) throws CameraAccessException {synchronized(mLock) {if (cameraId == null) {throw new IllegalArgumentException("cameraId was null");}ICameraService cameraService = getCameraService();if (cameraService == null) {throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,"Camera service is currently unavailable");}try {cameraService.setTorchMode(cameraId, enabled, mTorchClientBinder);} catch(ServiceSpecificException e) {throwAsPublicException(e);} catch (RemoteException e) {throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,"Camera service is currently unavailable");}}}
????????ICameraService cameraService = getCameraService();看到這句是不是很熟悉,android獲取系統服務,跨進程調用。只是和我們app開發常用的 不同,這里直接拿到camera服務的proxy。對下面這行代碼不理解的,可以學習下android binder機制,本質就是拿到camera服務的proxy
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
? ? ? ? 原來操作“手電筒”的不是單獨模塊,而是通過camera模塊來控制的,666。
二,framework層代碼分析
2.1 binder溯源
? ? ? ? 由第一部分分析可知,“手電筒”通過cameraservice來操作,結合binder知識,可以在ICameraService.aidl接口定義中找到,
void setTorchMode(String cameraId, boolean enabled, IBinder clientBinder);
aidl文件會自動生成java文件,再通過jni調用到cpp文件,最終在ICameraService.cpp中實現了client的調用,
::android::binder::Status BpCameraService::setTorchMode(const ::android::String16& cameraId, bool enabled, const ::android::sp<::android::IBinder>& clientBinder) {::android::Parcel _aidl_data;::android::Parcel _aidl_reply;::android::status_t _aidl_ret_status = ::android::OK;::android::binder::Status _aidl_status;_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_data.writeString16(cameraId);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_data.writeBool(enabled);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_data.writeStrongBinder(clientBinder);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = remote()->transact(::android::IBinder::FIRST_CALL_TRANSACTION + 15 /* setTorchMode */, _aidl_data, &_aidl_reply);if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && ICameraService::getDefaultImpl())) {return ICameraService::getDefaultImpl()->setTorchMode(cameraId, enabled, clientBinder);}if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}if (!_aidl_status.isOk()) {return _aidl_status;}_aidl_error:_aidl_status.setFromStatusT(_aidl_ret_status);return _aidl_status;
}
通過remote()->transact()
發起IPC請求,事務碼FIRST_CALL_TRANSACTION + 15
對應setTorchMode方法,數據通過_aidl_data
Parcel傳遞,執行到CameraService.cpp文件,
Status CameraService::setTorchMode(const String16& cameraId, bool enabled,const sp<IBinder>& clientBinder) {Mutex::Autolock lock(mServiceLock);ATRACE_CALL();if (enabled && clientBinder == nullptr) {ALOGE("%s: torch client binder is NULL", __FUNCTION__);return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,"Torch client Binder is null");}String8 id = String8(cameraId.string());int uid = CameraThreadState::getCallingUid();if (shouldRejectSystemCameraConnection(id)) {return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode"" for system only device %s: ", id.string());}// verify id is valid.auto state = getCameraState(id);if (state == nullptr) {ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,"Camera ID \"%s\" is a not valid camera ID", id.string());}StatusInternal cameraStatus = state->getStatus();if (cameraStatus != StatusInternal::PRESENT &&cameraStatus != StatusInternal::NOT_AVAILABLE) {ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,"Camera ID \"%s\" is a not valid camera ID", id.string());}{Mutex::Autolock al(mTorchStatusMutex);TorchModeStatus status;status_t err = getTorchStatusLocked(id, &status);if (err != OK) {if (err == NAME_NOT_FOUND) {return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,"Camera \"%s\" does not have a flash unit", id.string());}ALOGE("%s: getting current torch status failed for camera %s",__FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,"Error updating torch status for camera \"%s\": %s (%d)", id.string(),strerror(-err), err);}if (status == TorchModeStatus::NOT_AVAILABLE) {if (cameraStatus == StatusInternal::NOT_AVAILABLE) {ALOGE("%s: torch mode of camera %s is not available because ""camera is in use", __FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,"Torch for camera \"%s\" is not available due to an existing camera user",id.string());} else {ALOGE("%s: torch mode of camera %s is not available due to ""insufficient resources", __FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,"Torch for camera \"%s\" is not available due to insufficient resources",id.string());}}}{// Update UID map - this is used in the torch status changed callbacks, so must be done// before setTorchModeMutex::Autolock al(mTorchUidMapMutex);if (mTorchUidMap.find(id) == mTorchUidMap.end()) {mTorchUidMap[id].first = uid;mTorchUidMap[id].second = uid;} else {// Set the pending UIDmTorchUidMap[id].first = uid;}}status_t err = mFlashlight->setTorchMode(id, enabled);if (err != OK) {int32_t errorCode;String8 msg;switch (err) {case -ENOSYS:msg = String8::format("Camera \"%s\" has no flashlight",id.string());errorCode = ERROR_ILLEGAL_ARGUMENT;break;default:msg = String8::format("Setting torch mode of camera \"%s\" to %d failed: %s (%d)",id.string(), enabled, strerror(-err), err);errorCode = ERROR_INVALID_OPERATION;}ALOGE("%s: %s", __FUNCTION__, msg.string());return STATUS_ERROR(errorCode, msg.string());}{// update the link to client's deathMutex::Autolock al(mTorchClientMapMutex);ssize_t index = mTorchClientMap.indexOfKey(id);if (enabled) {if (index == NAME_NOT_FOUND) {mTorchClientMap.add(id, clientBinder);} else {mTorchClientMap.valueAt(index)->unlinkToDeath(this);mTorchClientMap.replaceValueAt(index, clientBinder);}clientBinder->linkToDeath(this);} else if (index != NAME_NOT_FOUND) {mTorchClientMap.valueAt(index)->unlinkToDeath(this);}}int clientPid = CameraThreadState::getCallingPid();const char *id_cstr = id.c_str();const char *torchState = enabled ? "on" : "off";ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid);logTorchEvent(id_cstr, torchState , clientPid);return Status::ok();
}
其中,關鍵句status_t err = mFlashlight->setTorchMode(id, enabled);再由hal層交互可知,?
最終通過CameraProviderManager
查詢HAL實現的setTorchMode()接口來控制device。
status_t CameraProviderManager::setTorchMode(const std::string &id, bool enabled) {std::lock_guard<std::mutex> lock(mInterfaceMutex);auto deviceInfo = findDeviceInfoLocked(id);if (deviceInfo == nullptr) return NAME_NOT_FOUND;// Pass the camera ID to start interface so that it will save it to the map of ICameraProviders// that are currently in use.sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();if (parentProvider == nullptr) {return DEAD_OBJECT;}const sp<provider::V2_4::ICameraProvider> interface = parentProvider->startProviderInterface();if (interface == nullptr) {return DEAD_OBJECT;}saveRef(DeviceMode::TORCH, deviceInfo->mId, interface);return deviceInfo->setTorchMode(enabled);
}
最終,通過deviceInfo->setTorchMode(enabled)來實現對硬件的操作。