1 /*
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 
22 namespace android {
23 
deriveCameraCharacteristicsKeys(uint32_t deviceVersion,CameraMetadata & chars)24 void 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         const size_t NUM_DERIVED_KEYS_HAL3_3 = 5;
35         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);
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 
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 
81         chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
82 
83         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 
106         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);
113         availableCharsKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
114         availableCharsKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
115         availableCharsKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
116         availableCharsKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
117         chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);
118 
119         // 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         }
134     }
135 
136     // Always add a default for the pre-correction active array if the vendor chooses to omit this
137     camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
138     if (entry.count == 0) {
139         Vector<int32_t> preCorrectionArray;
140         entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
141         preCorrectionArray.appendArray(entry.data.i32, entry.count);
142         chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
143     }
144 
145     return;
146 }
147 
CameraModule(camera_module_t * module)148 CameraModule::CameraModule(camera_module_t *module) {
149     if (module == NULL) {
150         ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
151         assert(0);
152     }
153 
154     mModule = module;
155     mCameraInfoMap.setCapacity(getNumberOfCameras());
156 }
157 
~CameraModule()158 CameraModule::~CameraModule()
159 {
160     while (mCameraInfoMap.size() > 0) {
161         camera_info cameraInfo = mCameraInfoMap.editValueAt(0);
162         if (cameraInfo.static_camera_characteristics != NULL) {
163             free_camera_metadata(
164                     const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics));
165         }
166         mCameraInfoMap.removeItemsAt(0);
167     }
168 }
169 
init()170 int CameraModule::init() {
171     if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
172             mModule->init != NULL) {
173         return mModule->init();
174     }
175     return OK;
176 }
177 
getCameraInfo(int cameraId,struct camera_info * info)178 int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
179     Mutex::Autolock lock(mCameraInfoLock);
180     if (cameraId < 0) {
181         ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
182         return -EINVAL;
183     }
184 
185     // Only override static_camera_characteristics for API2 devices
186     int apiVersion = mModule->common.module_api_version;
187     if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
188         return mModule->get_camera_info(cameraId, info);
189     }
190 
191     ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
192     if (index == NAME_NOT_FOUND) {
193         // Get camera info from raw module and cache it
194         camera_info rawInfo, cameraInfo;
195         int ret = mModule->get_camera_info(cameraId, &rawInfo);
196         if (ret != 0) {
197             return ret;
198         }
199         int deviceVersion = rawInfo.device_version;
200         if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
201             // static_camera_characteristics is invalid
202             *info = rawInfo;
203             return ret;
204         }
205         CameraMetadata m;
206         m = rawInfo.static_camera_characteristics;
207         deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
208         cameraInfo = rawInfo;
209         cameraInfo.static_camera_characteristics = m.release();
210         index = mCameraInfoMap.add(cameraId, cameraInfo);
211     }
212 
213     assert(index != NAME_NOT_FOUND);
214     // return the cached camera info
215     *info = mCameraInfoMap[index];
216     return OK;
217 }
218 
open(const char * id,struct hw_device_t ** device)219 int CameraModule::open(const char* id, struct hw_device_t** device) {
220     return filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
221 }
222 
openLegacy(const char * id,uint32_t halVersion,struct hw_device_t ** device)223 int CameraModule::openLegacy(
224         const char* id, uint32_t halVersion, struct hw_device_t** device) {
225     return mModule->open_legacy(&mModule->common, id, halVersion, device);
226 }
227 
getNumberOfCameras()228 int CameraModule::getNumberOfCameras() {
229     return mModule->get_number_of_cameras();
230 }
231 
setCallbacks(const camera_module_callbacks_t * callbacks)232 int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
233     return mModule->set_callbacks(callbacks);
234 }
235 
isVendorTagDefined()236 bool CameraModule::isVendorTagDefined() {
237     return mModule->get_vendor_tag_ops != NULL;
238 }
239 
getVendorTagOps(vendor_tag_ops_t * ops)240 void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
241     if (mModule->get_vendor_tag_ops) {
242         mModule->get_vendor_tag_ops(ops);
243     }
244 }
245 
setTorchMode(const char * camera_id,bool enable)246 int CameraModule::setTorchMode(const char* camera_id, bool enable) {
247     return mModule->set_torch_mode(camera_id, enable);
248 }
249 
filterOpenErrorCode(status_t err)250 status_t CameraModule::filterOpenErrorCode(status_t err) {
251     switch(err) {
252         case NO_ERROR:
253         case -EBUSY:
254         case -EINVAL:
255         case -EUSERS:
256             return err;
257         default:
258             break;
259     }
260     return -ENODEV;
261 }
262 
getModuleApiVersion()263 uint16_t CameraModule::getModuleApiVersion() {
264     return mModule->common.module_api_version;
265 }
266 
getModuleName()267 const char* CameraModule::getModuleName() {
268     return mModule->common.name;
269 }
270 
getHalApiVersion()271 uint16_t CameraModule::getHalApiVersion() {
272     return mModule->common.hal_api_version;
273 }
274 
getModuleAuthor()275 const char* CameraModule::getModuleAuthor() {
276     return mModule->common.author;
277 }
278 
getDso()279 void* CameraModule::getDso() {
280     return mModule->common.dso;
281 }
282 
283 }; // namespace android
284