blob: fcbf9587a9753a9b1b80476e465b9565a4c21526 [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) {
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -070034 const size_t NUM_DERIVED_KEYS_HAL3_3 = 5;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080035 Vector<uint8_t> controlModes;
36 uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;
37 chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1);
38 data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
39 chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1);
Yin-Chia Yehe074a932015-01-30 10:29:02 -080040 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 }
Zhijun He9c5af612015-05-04 16:34:37 -070045
46 // Only advertise CONTROL_OFF mode if 3A manual controls are supported.
47 bool isManualAeSupported = false;
48 bool isManualAfSupported = false;
49 bool isManualAwbSupported = false;
50 entry = chars.find(ANDROID_CONTROL_AE_AVAILABLE_MODES);
51 if (entry.count > 0) {
52 for (size_t i = 0; i < entry.count; i++) {
53 if (entry.data.u8[i] == ANDROID_CONTROL_AE_MODE_OFF) {
54 isManualAeSupported = true;
55 break;
56 }
57 }
58 }
59 entry = chars.find(ANDROID_CONTROL_AF_AVAILABLE_MODES);
60 if (entry.count > 0) {
61 for (size_t i = 0; i < entry.count; i++) {
62 if (entry.data.u8[i] == ANDROID_CONTROL_AF_MODE_OFF) {
63 isManualAfSupported = true;
64 break;
65 }
66 }
67 }
68 entry = chars.find(ANDROID_CONTROL_AWB_AVAILABLE_MODES);
69 if (entry.count > 0) {
70 for (size_t i = 0; i < entry.count; i++) {
71 if (entry.data.u8[i] == ANDROID_CONTROL_AWB_MODE_OFF) {
72 isManualAwbSupported = true;
73 break;
74 }
75 }
76 }
77 if (isManualAeSupported && isManualAfSupported && isManualAwbSupported) {
78 controlModes.push(ANDROID_CONTROL_MODE_OFF);
79 }
80
Yin-Chia Yehe074a932015-01-30 10:29:02 -080081 chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -070082
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -070083 entry = chars.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
84 // HAL3.2 devices passing existing CTS test should all support all LSC modes and LSC map
85 bool lensShadingModeSupported = false;
86 if (entry.count > 0) {
87 for (size_t i = 0; i < entry.count; i++) {
88 if (entry.data.i32[i] == ANDROID_SHADING_MODE) {
89 lensShadingModeSupported = true;
90 break;
91 }
92 }
93 }
94 Vector<uint8_t> lscModes;
95 Vector<uint8_t> lscMapModes;
96 lscModes.push(ANDROID_SHADING_MODE_FAST);
97 lscModes.push(ANDROID_SHADING_MODE_HIGH_QUALITY);
98 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF);
99 if (lensShadingModeSupported) {
100 lscModes.push(ANDROID_SHADING_MODE_OFF);
101 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON);
102 }
103 chars.update(ANDROID_SHADING_AVAILABLE_MODES, lscModes);
104 chars.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, lscMapModes);
105
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700106 entry = chars.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
107 Vector<int32_t> availableCharsKeys;
108 availableCharsKeys.setCapacity(entry.count + NUM_DERIVED_KEYS_HAL3_3);
109 for (size_t i = 0; i < entry.count; i++) {
110 availableCharsKeys.push(entry.data.i32[i]);
111 }
112 availableCharsKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700113 availableCharsKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700114 availableCharsKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700115 availableCharsKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
116 availableCharsKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700117 chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700118
Zhijun He1fa89992015-06-01 15:44:31 -0700119 // Need update android.control.availableHighSpeedVideoConfigurations since HAL3.3
120 // adds batch size to this array.
121 entry = chars.find(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS);
122 if (entry.count > 0) {
123 Vector<int32_t> highSpeedConfig;
124 for (size_t i = 0; i < entry.count; i += 4) {
125 highSpeedConfig.add(entry.data.i32[i]); // width
126 highSpeedConfig.add(entry.data.i32[i + 1]); // height
127 highSpeedConfig.add(entry.data.i32[i + 2]); // fps_min
128 highSpeedConfig.add(entry.data.i32[i + 3]); // fps_max
129 highSpeedConfig.add(1); // batchSize_max. default to 1 for HAL3.2
130 }
131 chars.update(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS,
132 highSpeedConfig);
133 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800134 }
135 return;
136}
137
138CameraModule::CameraModule(camera_module_t *module) {
139 if (module == NULL) {
140 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
141 assert(0);
142 }
143
144 mModule = module;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800145 mCameraInfoMap.setCapacity(getNumberOfCameras());
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800146}
147
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700148int CameraModule::init() {
149 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
150 mModule->init != NULL) {
151 return mModule->init();
152 }
153 return OK;
154}
155
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800156int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
157 Mutex::Autolock lock(mCameraInfoLock);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800158 if (cameraId < 0) {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800159 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
160 return -EINVAL;
161 }
162
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800163 // Only override static_camera_characteristics for API2 devices
164 int apiVersion = mModule->common.module_api_version;
165 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
166 return mModule->get_camera_info(cameraId, info);
167 }
168
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800169 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
170 if (index == NAME_NOT_FOUND) {
171 // Get camera info from raw module and cache it
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700172 camera_info rawInfo, cameraInfo;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800173 int ret = mModule->get_camera_info(cameraId, &rawInfo);
174 if (ret != 0) {
175 return ret;
176 }
Zhijun He9c5af612015-05-04 16:34:37 -0700177 int deviceVersion = rawInfo.device_version;
Yin-Chia Yeh7768ded2015-04-15 12:16:02 -0700178 if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
179 // static_camera_characteristics is invalid
180 *info = rawInfo;
181 return ret;
182 }
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700183 CameraMetadata m;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800184 m = rawInfo.static_camera_characteristics;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800185 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700186 mCameraCharacteristicsMap.add(cameraId, m);
187 cameraInfo = rawInfo;
188 cameraInfo.static_camera_characteristics =
189 mCameraCharacteristicsMap.valueFor(cameraId).getAndLock();
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800190 mCameraInfoMap.add(cameraId, cameraInfo);
191 index = mCameraInfoMap.indexOfKey(cameraId);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800192 }
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800193
194 assert(index != NAME_NOT_FOUND);
195 // return the cached camera info
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700196 *info = mCameraInfoMap[index];
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700197 return OK;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800198}
199
200int CameraModule::open(const char* id, struct hw_device_t** device) {
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800201 return filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800202}
203
204int CameraModule::openLegacy(
205 const char* id, uint32_t halVersion, struct hw_device_t** device) {
206 return mModule->open_legacy(&mModule->common, id, halVersion, device);
207}
208
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800209int CameraModule::getNumberOfCameras() {
210 return mModule->get_number_of_cameras();
211}
212
213int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
214 return mModule->set_callbacks(callbacks);
215}
216
217bool CameraModule::isVendorTagDefined() {
218 return mModule->get_vendor_tag_ops != NULL;
219}
220
221void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
222 if (mModule->get_vendor_tag_ops) {
223 mModule->get_vendor_tag_ops(ops);
224 }
225}
226
227int CameraModule::setTorchMode(const char* camera_id, bool enable) {
228 return mModule->set_torch_mode(camera_id, enable);
229}
230
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800231status_t CameraModule::filterOpenErrorCode(status_t err) {
232 switch(err) {
233 case NO_ERROR:
234 case -EBUSY:
235 case -EINVAL:
236 case -EUSERS:
237 return err;
238 default:
239 break;
240 }
241 return -ENODEV;
242}
243
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800244uint16_t CameraModule::getModuleApiVersion() {
245 return mModule->common.module_api_version;
246}
247
248const char* CameraModule::getModuleName() {
249 return mModule->common.name;
250}
251
252uint16_t CameraModule::getHalApiVersion() {
253 return mModule->common.hal_api_version;
254}
255
256const char* CameraModule::getModuleAuthor() {
257 return mModule->common.author;
258}
259
260void* CameraModule::getDso() {
261 return mModule->common.dso;
262}
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800263
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800264}; // namespace android