blob: 5e0ac9f449c8a5ee3a16957c7108300f1394bf39 [file] [log] [blame]
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraModule"
18//#define LOG_NDEBUG 0
19
20#include "CameraModule.h"
21
22namespace android {
23
24void CameraModule::deriveCameraCharacteristicsKeys(
25 uint32_t deviceVersion, CameraMetadata &chars) {
26 // HAL1 devices should not reach here
27 if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
28 ALOGV("%s: Cannot derive keys for HAL version < 2.0");
29 return;
30 }
31
32 // Keys added in HAL3.3
33 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) {
34 Vector<uint8_t> controlModes;
35 uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;
36 chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1);
37 data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
38 chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1);
39 controlModes.push(ANDROID_CONTROL_MODE_OFF);
40 controlModes.push(ANDROID_CONTROL_MODE_AUTO);
41 camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
42 if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) {
43 controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
44 }
45 chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
46 }
47 return;
48}
49
50CameraModule::CameraModule(camera_module_t *module) {
51 if (module == NULL) {
52 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
53 assert(0);
54 }
55
56 mModule = module;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -080057 mCameraInfoMap.setCapacity(getNumberOfCameras());
Yin-Chia Yehe074a932015-01-30 10:29:02 -080058}
59
Eino-Ville Talvala1527f072015-04-07 15:55:31 -070060int CameraModule::init() {
61 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
62 mModule->init != NULL) {
63 return mModule->init();
64 }
65 return OK;
66}
67
Yin-Chia Yehe074a932015-01-30 10:29:02 -080068int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
69 Mutex::Autolock lock(mCameraInfoLock);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -080070 if (cameraId < 0) {
Yin-Chia Yehe074a932015-01-30 10:29:02 -080071 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
72 return -EINVAL;
73 }
74
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -080075 // Only override static_camera_characteristics for API2 devices
76 int apiVersion = mModule->common.module_api_version;
77 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
78 return mModule->get_camera_info(cameraId, info);
79 }
80
Chien-Yu Chen676b21b2015-02-24 10:28:19 -080081 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
82 if (index == NAME_NOT_FOUND) {
83 // Get camera info from raw module and cache it
Yin-Chia Yeh54298b32015-03-24 16:51:41 -070084 camera_info rawInfo, cameraInfo;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080085 int ret = mModule->get_camera_info(cameraId, &rawInfo);
86 if (ret != 0) {
87 return ret;
88 }
Yin-Chia Yeh54298b32015-03-24 16:51:41 -070089 CameraMetadata m;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080090 m = rawInfo.static_camera_characteristics;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -080091 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
Yin-Chia Yeh54298b32015-03-24 16:51:41 -070092 mCameraCharacteristicsMap.add(cameraId, m);
93 cameraInfo = rawInfo;
94 cameraInfo.static_camera_characteristics =
95 mCameraCharacteristicsMap.valueFor(cameraId).getAndLock();
Chien-Yu Chen676b21b2015-02-24 10:28:19 -080096 mCameraInfoMap.add(cameraId, cameraInfo);
97 index = mCameraInfoMap.indexOfKey(cameraId);
Yin-Chia Yehe074a932015-01-30 10:29:02 -080098 }
Chien-Yu Chen676b21b2015-02-24 10:28:19 -080099
100 assert(index != NAME_NOT_FOUND);
101 // return the cached camera info
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700102 *info = mCameraInfoMap[index];
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700103 return OK;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800104}
105
106int CameraModule::open(const char* id, struct hw_device_t** device) {
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800107 return filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800108}
109
110int CameraModule::openLegacy(
111 const char* id, uint32_t halVersion, struct hw_device_t** device) {
112 return mModule->open_legacy(&mModule->common, id, halVersion, device);
113}
114
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800115int CameraModule::getNumberOfCameras() {
116 return mModule->get_number_of_cameras();
117}
118
119int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
120 return mModule->set_callbacks(callbacks);
121}
122
123bool CameraModule::isVendorTagDefined() {
124 return mModule->get_vendor_tag_ops != NULL;
125}
126
127void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
128 if (mModule->get_vendor_tag_ops) {
129 mModule->get_vendor_tag_ops(ops);
130 }
131}
132
133int CameraModule::setTorchMode(const char* camera_id, bool enable) {
134 return mModule->set_torch_mode(camera_id, enable);
135}
136
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800137status_t CameraModule::filterOpenErrorCode(status_t err) {
138 switch(err) {
139 case NO_ERROR:
140 case -EBUSY:
141 case -EINVAL:
142 case -EUSERS:
143 return err;
144 default:
145 break;
146 }
147 return -ENODEV;
148}
149
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800150uint16_t CameraModule::getModuleApiVersion() {
151 return mModule->common.module_api_version;
152}
153
154const char* CameraModule::getModuleName() {
155 return mModule->common.name;
156}
157
158uint16_t CameraModule::getHalApiVersion() {
159 return mModule->common.hal_api_version;
160}
161
162const char* CameraModule::getModuleAuthor() {
163 return mModule->common.author;
164}
165
166void* CameraModule::getDso() {
167 return mModule->common.dso;
168}
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800169
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800170}; // namespace android