Home | History | Annotate | Download | only in default
      1 /*
      2  * Copyright (C) 2016 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 "CamComm1.0-CamModule"
     18 #define ATRACE_TAG ATRACE_TAG_CAMERA
     19 //#define LOG_NDEBUG 0
     20 
     21 #include <utils/Trace.h>
     22 
     23 #include "CameraModule.h"
     24 
     25 namespace android {
     26 namespace hardware {
     27 namespace camera {
     28 namespace common {
     29 namespace V1_0 {
     30 namespace helper {
     31 
     32 void CameraModule::deriveCameraCharacteristicsKeys(
     33         uint32_t deviceVersion, CameraMetadata &chars) {
     34     ATRACE_CALL();
     35 
     36     Vector<int32_t> derivedCharKeys;
     37     Vector<int32_t> derivedRequestKeys;
     38     Vector<int32_t> derivedResultKeys;
     39     // Keys added in HAL3.3
     40     if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) {
     41         Vector<uint8_t> controlModes;
     42         uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;
     43         chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1);
     44         data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
     45         chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1);
     46         controlModes.push(ANDROID_CONTROL_MODE_AUTO);
     47         camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
     48         if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) {
     49             controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
     50         }
     51 
     52         // Only advertise CONTROL_OFF mode if 3A manual controls are supported.
     53         bool isManualAeSupported = false;
     54         bool isManualAfSupported = false;
     55         bool isManualAwbSupported = false;
     56         entry = chars.find(ANDROID_CONTROL_AE_AVAILABLE_MODES);
     57         if (entry.count > 0) {
     58             for (size_t i = 0; i < entry.count; i++) {
     59                 if (entry.data.u8[i] == ANDROID_CONTROL_AE_MODE_OFF) {
     60                     isManualAeSupported = true;
     61                     break;
     62                 }
     63             }
     64         }
     65         entry = chars.find(ANDROID_CONTROL_AF_AVAILABLE_MODES);
     66         if (entry.count > 0) {
     67             for (size_t i = 0; i < entry.count; i++) {
     68                 if (entry.data.u8[i] == ANDROID_CONTROL_AF_MODE_OFF) {
     69                     isManualAfSupported = true;
     70                     break;
     71                 }
     72             }
     73         }
     74         entry = chars.find(ANDROID_CONTROL_AWB_AVAILABLE_MODES);
     75         if (entry.count > 0) {
     76             for (size_t i = 0; i < entry.count; i++) {
     77                 if (entry.data.u8[i] == ANDROID_CONTROL_AWB_MODE_OFF) {
     78                     isManualAwbSupported = true;
     79                     break;
     80                 }
     81             }
     82         }
     83         if (isManualAeSupported && isManualAfSupported && isManualAwbSupported) {
     84             controlModes.push(ANDROID_CONTROL_MODE_OFF);
     85         }
     86 
     87         chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
     88 
     89         entry = chars.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
     90         // HAL3.2 devices passing existing CTS test should all support all LSC modes and LSC map
     91         bool lensShadingModeSupported = false;
     92         if (entry.count > 0) {
     93             for (size_t i = 0; i < entry.count; i++) {
     94                 if (entry.data.i32[i] == ANDROID_SHADING_MODE) {
     95                     lensShadingModeSupported = true;
     96                     break;
     97                 }
     98             }
     99         }
    100         Vector<uint8_t> lscModes;
    101         Vector<uint8_t> lscMapModes;
    102         lscModes.push(ANDROID_SHADING_MODE_FAST);
    103         lscModes.push(ANDROID_SHADING_MODE_HIGH_QUALITY);
    104         lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF);
    105         if (lensShadingModeSupported) {
    106             lscModes.push(ANDROID_SHADING_MODE_OFF);
    107             lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON);
    108         }
    109         chars.update(ANDROID_SHADING_AVAILABLE_MODES, lscModes);
    110         chars.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, lscMapModes);
    111 
    112         derivedCharKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE);
    113         derivedCharKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
    114         derivedCharKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
    115         derivedCharKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
    116         derivedCharKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
    117 
    118         // Need update android.control.availableHighSpeedVideoConfigurations since HAL3.3
    119         // adds batch size to this array.
    120         entry = chars.find(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS);
    121         if (entry.count > 0) {
    122             Vector<int32_t> highSpeedConfig;
    123             for (size_t i = 0; i < entry.count; i += 4) {
    124                 highSpeedConfig.add(entry.data.i32[i]); // width
    125                 highSpeedConfig.add(entry.data.i32[i + 1]); // height
    126                 highSpeedConfig.add(entry.data.i32[i + 2]); // fps_min
    127                 highSpeedConfig.add(entry.data.i32[i + 3]); // fps_max
    128                 highSpeedConfig.add(1); // batchSize_max. default to 1 for HAL3.2
    129             }
    130             chars.update(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS,
    131                     highSpeedConfig);
    132         }
    133     }
    134 
    135     // Keys added in HAL3.4
    136     if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_4) {
    137         // Check if HAL supports RAW_OPAQUE output
    138         camera_metadata_entry entry = chars.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
    139         bool supportRawOpaque = false;
    140         bool supportAnyRaw = false;
    141         const int STREAM_CONFIGURATION_SIZE = 4;
    142         const int STREAM_FORMAT_OFFSET = 0;
    143         const int STREAM_WIDTH_OFFSET = 1;
    144         const int STREAM_HEIGHT_OFFSET = 2;
    145         const int STREAM_IS_INPUT_OFFSET = 3;
    146         Vector<int32_t> rawOpaqueSizes;
    147 
    148         for (size_t i=0; i < entry.count; i += STREAM_CONFIGURATION_SIZE) {
    149             int32_t format = entry.data.i32[i + STREAM_FORMAT_OFFSET];
    150             int32_t width = entry.data.i32[i + STREAM_WIDTH_OFFSET];
    151             int32_t height = entry.data.i32[i + STREAM_HEIGHT_OFFSET];
    152             int32_t isInput = entry.data.i32[i + STREAM_IS_INPUT_OFFSET];
    153             if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
    154                     format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
    155                 supportRawOpaque = true;
    156                 rawOpaqueSizes.push(width);
    157                 rawOpaqueSizes.push(height);
    158                 // 2 bytes per pixel. This rough estimation is only used when
    159                 // HAL does not fill in the opaque raw size
    160                 rawOpaqueSizes.push(width * height *2);
    161             }
    162             if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
    163                     (format == HAL_PIXEL_FORMAT_RAW16 ||
    164                      format == HAL_PIXEL_FORMAT_RAW10 ||
    165                      format == HAL_PIXEL_FORMAT_RAW12 ||
    166                      format == HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
    167                 supportAnyRaw = true;
    168             }
    169         }
    170 
    171         if (supportRawOpaque) {
    172             entry = chars.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
    173             if (entry.count == 0) {
    174                 // Fill in estimated value if HAL does not list it
    175                 chars.update(ANDROID_SENSOR_OPAQUE_RAW_SIZE, rawOpaqueSizes);
    176                 derivedCharKeys.push(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
    177             }
    178         }
    179 
    180         // Check if HAL supports any RAW output, if so, fill in postRawSensitivityBoost range
    181         if (supportAnyRaw) {
    182             int32_t defaultRange[2] = {100, 100};
    183             entry = chars.find(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE);
    184             if (entry.count == 0) {
    185                 // Fill in default value (100, 100)
    186                 chars.update(
    187                         ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE,
    188                         defaultRange, 2);
    189                 derivedCharKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE);
    190                 // Actual request/results will be derived by camera device.
    191                 derivedRequestKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST);
    192                 derivedResultKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST);
    193             }
    194         }
    195     }
    196 
    197     // Always add a default for the pre-correction active array if the vendor chooses to omit this
    198     camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
    199     if (entry.count == 0) {
    200         Vector<int32_t> preCorrectionArray;
    201         entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
    202         preCorrectionArray.appendArray(entry.data.i32, entry.count);
    203         chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
    204         derivedCharKeys.push(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
    205     }
    206 
    207     // Add those newly added keys to AVAILABLE_CHARACTERISTICS_KEYS
    208     // This has to be done at this end of this function.
    209     if (derivedCharKeys.size() > 0) {
    210         appendAvailableKeys(
    211                 chars, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, derivedCharKeys);
    212     }
    213     if (derivedRequestKeys.size() > 0) {
    214         appendAvailableKeys(
    215                 chars, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, derivedRequestKeys);
    216     }
    217     if (derivedResultKeys.size() > 0) {
    218         appendAvailableKeys(
    219                 chars, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, derivedResultKeys);
    220     }
    221     return;
    222 }
    223 
    224 void CameraModule::appendAvailableKeys(CameraMetadata &chars,
    225         int32_t keyTag, const Vector<int32_t>& appendKeys) {
    226     camera_metadata_entry entry = chars.find(keyTag);
    227     Vector<int32_t> availableKeys;
    228     availableKeys.setCapacity(entry.count + appendKeys.size());
    229     for (size_t i = 0; i < entry.count; i++) {
    230         availableKeys.push(entry.data.i32[i]);
    231     }
    232     for (size_t i = 0; i < appendKeys.size(); i++) {
    233         availableKeys.push(appendKeys[i]);
    234     }
    235     chars.update(keyTag, availableKeys);
    236 }
    237 
    238 CameraModule::CameraModule(camera_module_t *module) {
    239     if (module == NULL) {
    240         ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
    241         assert(0);
    242     }
    243     mModule = module;
    244 }
    245 
    246 CameraModule::~CameraModule()
    247 {
    248     while (mCameraInfoMap.size() > 0) {
    249         camera_info cameraInfo = mCameraInfoMap.editValueAt(0);
    250         if (cameraInfo.static_camera_characteristics != NULL) {
    251             free_camera_metadata(
    252                     const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics));
    253         }
    254         mCameraInfoMap.removeItemsAt(0);
    255     }
    256 }
    257 
    258 int CameraModule::init() {
    259     ATRACE_CALL();
    260     int res = OK;
    261     if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
    262             mModule->init != NULL) {
    263         ATRACE_BEGIN("camera_module->init");
    264         res = mModule->init();
    265         ATRACE_END();
    266     }
    267     mCameraInfoMap.setCapacity(getNumberOfCameras());
    268     return res;
    269 }
    270 
    271 int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
    272     ATRACE_CALL();
    273     Mutex::Autolock lock(mCameraInfoLock);
    274     if (cameraId < 0) {
    275         ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
    276         return -EINVAL;
    277     }
    278 
    279     // Only override static_camera_characteristics for API2 devices
    280     int apiVersion = mModule->common.module_api_version;
    281     if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
    282         int ret;
    283         ATRACE_BEGIN("camera_module->get_camera_info");
    284         ret = mModule->get_camera_info(cameraId, info);
    285         // Fill in this so CameraService won't be confused by
    286         // possibly 0 device_version
    287         info->device_version = CAMERA_DEVICE_API_VERSION_1_0;
    288         ATRACE_END();
    289         return ret;
    290     }
    291 
    292     ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
    293     if (index == NAME_NOT_FOUND) {
    294         // Get camera info from raw module and cache it
    295         camera_info rawInfo, cameraInfo;
    296         ATRACE_BEGIN("camera_module->get_camera_info");
    297         int ret = mModule->get_camera_info(cameraId, &rawInfo);
    298         ATRACE_END();
    299         if (ret != 0) {
    300             return ret;
    301         }
    302         int deviceVersion = rawInfo.device_version;
    303         if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_0) {
    304             // static_camera_characteristics is invalid
    305             *info = rawInfo;
    306             return ret;
    307         }
    308         CameraMetadata m;
    309         m.append(rawInfo.static_camera_characteristics);
    310         deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
    311         cameraInfo = rawInfo;
    312         cameraInfo.static_camera_characteristics = m.release();
    313         index = mCameraInfoMap.add(cameraId, cameraInfo);
    314     }
    315 
    316     assert(index != NAME_NOT_FOUND);
    317     // return the cached camera info
    318     *info = mCameraInfoMap[index];
    319     return OK;
    320 }
    321 
    322 int CameraModule::getDeviceVersion(int cameraId) {
    323     ssize_t index = mDeviceVersionMap.indexOfKey(cameraId);
    324     if (index == NAME_NOT_FOUND) {
    325         int deviceVersion;
    326         if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
    327             struct camera_info info;
    328             getCameraInfo(cameraId, &info);
    329             deviceVersion = info.device_version;
    330         } else {
    331             deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
    332         }
    333         index = mDeviceVersionMap.add(cameraId, deviceVersion);
    334     }
    335     assert(index != NAME_NOT_FOUND);
    336     return mDeviceVersionMap[index];
    337 }
    338 
    339 int CameraModule::open(const char* id, struct hw_device_t** device) {
    340     int res;
    341     ATRACE_BEGIN("camera_module->open");
    342     res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
    343     ATRACE_END();
    344     return res;
    345 }
    346 
    347 bool CameraModule::isOpenLegacyDefined() const {
    348     if (getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_3) {
    349         return false;
    350     }
    351     return mModule->open_legacy != NULL;
    352 }
    353 
    354 int CameraModule::openLegacy(
    355         const char* id, uint32_t halVersion, struct hw_device_t** device) {
    356     int res;
    357     ATRACE_BEGIN("camera_module->open_legacy");
    358     res = mModule->open_legacy(&mModule->common, id, halVersion, device);
    359     ATRACE_END();
    360     return res;
    361 }
    362 
    363 int CameraModule::getNumberOfCameras() {
    364     int numCameras;
    365     ATRACE_BEGIN("camera_module->get_number_of_cameras");
    366     numCameras = mModule->get_number_of_cameras();
    367     ATRACE_END();
    368     return numCameras;
    369 }
    370 
    371 int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
    372     int res = OK;
    373     ATRACE_BEGIN("camera_module->set_callbacks");
    374     if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
    375         res = mModule->set_callbacks(callbacks);
    376     }
    377     ATRACE_END();
    378     return res;
    379 }
    380 
    381 bool CameraModule::isVendorTagDefined() const {
    382     return mModule->get_vendor_tag_ops != NULL;
    383 }
    384 
    385 void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
    386     if (mModule->get_vendor_tag_ops) {
    387         ATRACE_BEGIN("camera_module->get_vendor_tag_ops");
    388         mModule->get_vendor_tag_ops(ops);
    389         ATRACE_END();
    390     }
    391 }
    392 
    393 bool CameraModule::isSetTorchModeSupported() const {
    394     if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
    395         if (mModule->set_torch_mode == NULL) {
    396             ALOGE("%s: Module 2.4 device must support set torch API!",
    397                     __FUNCTION__);
    398             return false;
    399         }
    400         return true;
    401     }
    402     return false;
    403 }
    404 
    405 int CameraModule::setTorchMode(const char* camera_id, bool enable) {
    406     int res = INVALID_OPERATION;
    407     if (mModule->set_torch_mode != NULL) {
    408         ATRACE_BEGIN("camera_module->set_torch_mode");
    409         res = mModule->set_torch_mode(camera_id, enable);
    410         ATRACE_END();
    411     }
    412     return res;
    413 }
    414 
    415 status_t CameraModule::filterOpenErrorCode(status_t err) {
    416     switch(err) {
    417         case NO_ERROR:
    418         case -EBUSY:
    419         case -EINVAL:
    420         case -EUSERS:
    421             return err;
    422         default:
    423             break;
    424     }
    425     return -ENODEV;
    426 }
    427 
    428 void CameraModule::removeCamera(int cameraId) {
    429     free_camera_metadata(
    430         const_cast<camera_metadata_t*>(mCameraInfoMap[cameraId].static_camera_characteristics));
    431     mCameraInfoMap.removeItem(cameraId);
    432     mDeviceVersionMap.removeItem(cameraId);
    433 }
    434 
    435 uint16_t CameraModule::getModuleApiVersion() const {
    436     return mModule->common.module_api_version;
    437 }
    438 
    439 const char* CameraModule::getModuleName() const {
    440     return mModule->common.name;
    441 }
    442 
    443 uint16_t CameraModule::getHalApiVersion() const {
    444     return mModule->common.hal_api_version;
    445 }
    446 
    447 const char* CameraModule::getModuleAuthor() const {
    448     return mModule->common.author;
    449 }
    450 
    451 void* CameraModule::getDso() {
    452     return mModule->common.dso;
    453 }
    454 
    455 } // namespace helper
    456 } // namespace V1_0
    457 } // namespace common
    458 } // namespace camera
    459 } // namespace hardware
    460 } // namespace android
    461