1 /* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 #define LOG_TAG "QCamera2Factory" 31 32 // System dependencies 33 #include <stdlib.h> 34 #include <utils/Errors.h> 35 36 // Camera dependencies 37 #include "camera.h" 38 #include "camera3.h" 39 #include "HAL/QCamera2HWI.h" 40 #include "HAL3/QCamera3HWI.h" 41 #include "util/QCameraFlash.h" 42 #include "QCamera2Factory.h" 43 #include "QCameraMuxer.h" 44 45 extern "C" { 46 #include "mm_camera_dbg.h" 47 } 48 49 using namespace android; 50 51 namespace qcamera { 52 53 QCamera2Factory *gQCamera2Factory = NULL; 54 QCameraMuxer *gQCameraMuxer = NULL; 55 pthread_mutex_t gCamLock = PTHREAD_MUTEX_INITIALIZER; 56 //Total number of cameras opened simultaneously. 57 //This variable updation is protected by gCamLock. 58 uint8_t gNumCameraSessions = 0; 59 60 volatile uint32_t gKpiDebugLevel = 1; 61 62 /*=========================================================================== 63 * FUNCTION : QCamera2Factory 64 * 65 * DESCRIPTION: default constructor of QCamera2Factory 66 * 67 * PARAMETERS : none 68 * 69 * RETURN : None 70 *==========================================================================*/ 71 QCamera2Factory::QCamera2Factory() 72 { 73 mHalDescriptors = NULL; 74 mCallbacks = NULL; 75 mNumOfCameras = get_num_of_cameras(); 76 int bDualCamera = 0; 77 char propDefault[PROPERTY_VALUE_MAX]; 78 char prop[PROPERTY_VALUE_MAX]; 79 property_get("persist.camera.HAL3.enabled", prop, "1"); 80 int isHAL3Enabled = atoi(prop); 81 82 // Signifies whether system has to enable dual camera mode 83 snprintf(propDefault, PROPERTY_VALUE_MAX, "%d", isDualCamAvailable(isHAL3Enabled)); 84 property_get("persist.camera.dual.camera", prop, propDefault); 85 bDualCamera = atoi(prop); 86 LOGH("dualCamera:%d ", bDualCamera); 87 88 if(bDualCamera) { 89 LOGI("Enabling QCamera Muxer"); 90 if (!gQCameraMuxer) { 91 QCameraMuxer::getCameraMuxer(&gQCameraMuxer, mNumOfCameras); 92 if (!gQCameraMuxer) { 93 LOGE("Error !! Failed to get QCameraMuxer"); 94 } 95 } 96 } 97 if (!gQCameraMuxer && (mNumOfCameras > 0) && 98 (mNumOfCameras <= MM_CAMERA_MAX_NUM_SENSORS)) { 99 mHalDescriptors = new hal_desc[mNumOfCameras]; 100 if ( NULL != mHalDescriptors) { 101 uint32_t cameraId = 0; 102 103 for (int i = 0; i < mNumOfCameras ; i++, cameraId++) { 104 mHalDescriptors[i].cameraId = cameraId; 105 // Set Device version to 3.x when both HAL3 is enabled & its BAYER sensor 106 if (isHAL3Enabled && !(is_yuv_sensor(cameraId))) { 107 mHalDescriptors[i].device_version = 108 CAMERA_DEVICE_API_VERSION_3_0; 109 } else { 110 mHalDescriptors[i].device_version = 111 CAMERA_DEVICE_API_VERSION_1_0; 112 } 113 } 114 } else { 115 LOGE("Not enough resources to allocate HAL descriptor table!"); 116 } 117 } else { 118 LOGI("%d camera devices detected!", mNumOfCameras); 119 } 120 } 121 122 /*=========================================================================== 123 * FUNCTION : ~QCamera2Factory 124 * 125 * DESCRIPTION: deconstructor of QCamera2Factory 126 * 127 * PARAMETERS : none 128 * 129 * RETURN : None 130 *==========================================================================*/ 131 QCamera2Factory::~QCamera2Factory() 132 { 133 if ( NULL != mHalDescriptors ) { 134 delete [] mHalDescriptors; 135 } 136 if (gQCameraMuxer) { 137 delete gQCameraMuxer; 138 gQCameraMuxer = NULL; 139 } 140 } 141 142 /*=========================================================================== 143 * FUNCTION : get_number_of_cameras 144 * 145 * DESCRIPTION: static function to query number of cameras detected 146 * 147 * PARAMETERS : none 148 * 149 * RETURN : number of cameras detected 150 *==========================================================================*/ 151 int QCamera2Factory::get_number_of_cameras() 152 { 153 int numCameras = 0; 154 155 if (!gQCamera2Factory) { 156 gQCamera2Factory = new QCamera2Factory(); 157 if (!gQCamera2Factory) { 158 LOGE("Failed to allocate Camera2Factory object"); 159 return 0; 160 } 161 } 162 163 if(gQCameraMuxer) 164 numCameras = gQCameraMuxer->get_number_of_cameras(); 165 else 166 numCameras = gQCamera2Factory->getNumberOfCameras(); 167 168 LOGH("num of cameras: %d", numCameras); 169 return numCameras; 170 } 171 172 /*=========================================================================== 173 * FUNCTION : get_camera_info 174 * 175 * DESCRIPTION: static function to query camera information with its ID 176 * 177 * PARAMETERS : 178 * @camera_id : camera ID 179 * @info : ptr to camera info struct 180 * 181 * RETURN : int32_t type of status 182 * NO_ERROR -- success 183 * none-zero failure code 184 *==========================================================================*/ 185 int QCamera2Factory::get_camera_info(int camera_id, struct camera_info *info) 186 { 187 int rc = NO_ERROR; 188 189 if(gQCameraMuxer) 190 rc = gQCameraMuxer->get_camera_info(camera_id, info); 191 else 192 rc = gQCamera2Factory->getCameraInfo(camera_id, info); 193 194 return rc; 195 } 196 197 /*=========================================================================== 198 * FUNCTION : set_callbacks 199 * 200 * DESCRIPTION: static function to set callbacks function to camera module 201 * 202 * PARAMETERS : 203 * @callbacks : ptr to callback functions 204 * 205 * RETURN : NO_ERROR -- success 206 * none-zero failure code 207 *==========================================================================*/ 208 int QCamera2Factory::set_callbacks(const camera_module_callbacks_t *callbacks) 209 { 210 int rc = NO_ERROR; 211 if(gQCameraMuxer) 212 rc = gQCameraMuxer->set_callbacks(callbacks); 213 else 214 rc = gQCamera2Factory->setCallbacks(callbacks); 215 216 return rc; 217 } 218 219 /*=========================================================================== 220 * FUNCTION : open_legacy 221 * 222 * DESCRIPTION: Function to open older hal version implementation 223 * 224 * PARAMETERS : 225 * @hw_device : ptr to struct storing camera hardware device info 226 * @camera_id : camera ID 227 * @halVersion: Based on camera_module_t.common.module_api_version 228 * 229 * RETURN : 0 -- success 230 * none-zero failure code 231 *==========================================================================*/ 232 int QCamera2Factory::open_legacy(const struct hw_module_t* module, 233 const char* id, uint32_t halVersion, struct hw_device_t** device) 234 { 235 int rc = NO_ERROR; 236 if (module != &HAL_MODULE_INFO_SYM.common) { 237 LOGE("Invalid module. Trying to open %p, expect %p", 238 module, &HAL_MODULE_INFO_SYM.common); 239 return INVALID_OPERATION; 240 } 241 if (!id) { 242 LOGE("Invalid camera id"); 243 return BAD_VALUE; 244 } 245 if(gQCameraMuxer) 246 rc = gQCameraMuxer->open_legacy(module, id, halVersion, device); 247 else 248 rc = gQCamera2Factory->openLegacy(atoi(id), halVersion, device); 249 250 return rc; 251 } 252 253 /*=========================================================================== 254 * FUNCTION : set_torch_mode 255 * 256 * DESCRIPTION: Attempt to turn on or off the torch mode of the flash unit. 257 * 258 * PARAMETERS : 259 * @camera_id : camera ID 260 * @on : Indicates whether to turn the flash on or off 261 * 262 * RETURN : 0 -- success 263 * none-zero failure code 264 *==========================================================================*/ 265 int QCamera2Factory::set_torch_mode(const char* camera_id, bool on) 266 { 267 return gQCamera2Factory->setTorchMode(camera_id, on); 268 } 269 270 /*=========================================================================== 271 * FUNCTION : getNumberOfCameras 272 * 273 * DESCRIPTION: query number of cameras detected 274 * 275 * PARAMETERS : none 276 * 277 * RETURN : number of cameras detected 278 *==========================================================================*/ 279 int QCamera2Factory::getNumberOfCameras() 280 { 281 return mNumOfCameras; 282 } 283 284 /*=========================================================================== 285 * FUNCTION : getCameraInfo 286 * 287 * DESCRIPTION: query camera information with its ID 288 * 289 * PARAMETERS : 290 * @camera_id : camera ID 291 * @info : ptr to camera info struct 292 * 293 * RETURN : int32_t type of status 294 * NO_ERROR -- success 295 * none-zero failure code 296 *==========================================================================*/ 297 int QCamera2Factory::getCameraInfo(int camera_id, struct camera_info *info) 298 { 299 int rc; 300 301 if (!mNumOfCameras || camera_id >= mNumOfCameras || !info || 302 (camera_id < 0)) { 303 LOGE("Error getting camera info!! mNumOfCameras = %d," 304 "camera_id = %d, info = %p", 305 mNumOfCameras, camera_id, info); 306 return -ENODEV; 307 } 308 309 if ( NULL == mHalDescriptors ) { 310 LOGE("Hal descriptor table is not initialized!"); 311 return NO_INIT; 312 } 313 314 LOGI("Camera id %d API version %d", 315 camera_id, mHalDescriptors[camera_id].device_version); 316 317 // Need ANDROID_FLASH_INFO_AVAILABLE property for flashlight widget to 318 // work and so get the static data regardless of HAL version 319 rc = QCamera3HardwareInterface::getCamInfo( 320 mHalDescriptors[camera_id].cameraId, info); 321 if (mHalDescriptors[camera_id].device_version == 322 CAMERA_DEVICE_API_VERSION_1_0) { 323 info->device_version = CAMERA_DEVICE_API_VERSION_1_0; 324 } 325 326 return rc; 327 } 328 329 /*=========================================================================== 330 * FUNCTION : setCallbacks 331 * 332 * DESCRIPTION: set callback functions to send asynchronous notifications to 333 * frameworks. 334 * 335 * PARAMETERS : 336 * @callbacks : callback function pointer 337 * 338 * RETURN : 339 * NO_ERROR -- success 340 * none-zero failure code 341 *==========================================================================*/ 342 int QCamera2Factory::setCallbacks(const camera_module_callbacks_t *callbacks) 343 { 344 int rc = NO_ERROR; 345 mCallbacks = callbacks; 346 347 rc = QCameraFlash::getInstance().registerCallbacks(callbacks); 348 if (rc != 0) { 349 LOGE("Failed to register callbacks with flash module!"); 350 } 351 352 return rc; 353 } 354 355 /*=========================================================================== 356 * FUNCTION : cameraDeviceOpen 357 * 358 * DESCRIPTION: open a camera device with its ID 359 * 360 * PARAMETERS : 361 * @camera_id : camera ID 362 * @hw_device : ptr to struct storing camera hardware device info 363 * 364 * RETURN : int32_t type of status 365 * NO_ERROR -- success 366 * none-zero failure code 367 *==========================================================================*/ 368 int QCamera2Factory::cameraDeviceOpen(int camera_id, 369 struct hw_device_t **hw_device) 370 { 371 int rc = NO_ERROR; 372 if (camera_id < 0 || camera_id >= mNumOfCameras) 373 return -ENODEV; 374 375 if ( NULL == mHalDescriptors ) { 376 LOGE("Hal descriptor table is not initialized!"); 377 return NO_INIT; 378 } 379 380 LOGI("Open camera id %d API version %d", 381 camera_id, mHalDescriptors[camera_id].device_version); 382 383 if ( mHalDescriptors[camera_id].device_version == CAMERA_DEVICE_API_VERSION_3_0 ) { 384 QCamera3HardwareInterface *hw = new QCamera3HardwareInterface(mHalDescriptors[camera_id].cameraId, 385 mCallbacks); 386 if (!hw) { 387 LOGE("Allocation of hardware interface failed"); 388 return NO_MEMORY; 389 } 390 rc = hw->openCamera(hw_device); 391 if (rc != 0) { 392 delete hw; 393 } 394 } else if (mHalDescriptors[camera_id].device_version == CAMERA_DEVICE_API_VERSION_1_0) { 395 QCamera2HardwareInterface *hw = new QCamera2HardwareInterface((uint32_t)camera_id); 396 if (!hw) { 397 LOGE("Allocation of hardware interface failed"); 398 return NO_MEMORY; 399 } 400 rc = hw->openCamera(hw_device); 401 if (rc != NO_ERROR) { 402 delete hw; 403 } 404 } else { 405 LOGE("Device version for camera id %d invalid %d", 406 camera_id, 407 mHalDescriptors[camera_id].device_version); 408 return BAD_VALUE; 409 } 410 411 return rc; 412 } 413 414 /*=========================================================================== 415 * FUNCTION : camera_device_open 416 * 417 * DESCRIPTION: static function to open a camera device by its ID 418 * 419 * PARAMETERS : 420 * @camera_id : camera ID 421 * @hw_device : ptr to struct storing camera hardware device info 422 * 423 * RETURN : int32_t type of status 424 * NO_ERROR -- success 425 * none-zero failure code 426 *==========================================================================*/ 427 int QCamera2Factory::camera_device_open( 428 const struct hw_module_t *module, const char *id, 429 struct hw_device_t **hw_device) 430 { 431 int rc = NO_ERROR; 432 if (module != &HAL_MODULE_INFO_SYM.common) { 433 LOGE("Invalid module. Trying to open %p, expect %p", 434 module, &HAL_MODULE_INFO_SYM.common); 435 return INVALID_OPERATION; 436 } 437 if (!id) { 438 LOGE("Invalid camera id"); 439 return BAD_VALUE; 440 } 441 442 if(gQCameraMuxer) 443 rc = gQCameraMuxer->camera_device_open(module, id, hw_device); 444 else 445 rc = gQCamera2Factory->cameraDeviceOpen(atoi(id), hw_device); 446 447 return rc; 448 } 449 450 struct hw_module_methods_t QCamera2Factory::mModuleMethods = { 451 .open = QCamera2Factory::camera_device_open, 452 }; 453 454 /*=========================================================================== 455 * FUNCTION : openLegacy 456 * 457 * DESCRIPTION: Function to open older hal version implementation 458 * 459 * PARAMETERS : 460 * @camera_id : camera ID 461 * @halVersion: Based on camera_module_t.common.module_api_version 462 * @hw_device : ptr to struct storing camera hardware device info 463 * 464 * RETURN : 0 -- success 465 * none-zero failure code 466 *==========================================================================*/ 467 int QCamera2Factory::openLegacy( 468 int32_t cameraId, uint32_t halVersion, struct hw_device_t** hw_device) 469 { 470 int rc = NO_ERROR; 471 472 LOGI("openLegacy halVersion: %d", halVersion); 473 //Assumption: all cameras can support legacy API version 474 if (cameraId < 0 || cameraId >= gQCamera2Factory->getNumberOfCameras()) 475 return -ENODEV; 476 477 switch(halVersion) 478 { 479 case CAMERA_DEVICE_API_VERSION_1_0: 480 { 481 QCamera2HardwareInterface *hw = 482 new QCamera2HardwareInterface((uint32_t)cameraId); 483 if (!hw) { 484 LOGE("Allocation of hardware interface failed"); 485 return NO_MEMORY; 486 } 487 rc = hw->openCamera(hw_device); 488 if (rc != NO_ERROR) { 489 delete hw; 490 } 491 break; 492 } 493 default: 494 LOGE("Device API version: %d for camera id %d invalid", 495 halVersion, cameraId); 496 return BAD_VALUE; 497 } 498 499 return rc; 500 } 501 502 /*=========================================================================== 503 * FUNCTION : setTorchMode 504 * 505 * DESCRIPTION: Attempt to turn on or off the torch mode of the flash unit. 506 * 507 * PARAMETERS : 508 * @camera_id : camera ID 509 * @on : Indicates whether to turn the flash on or off 510 * 511 * RETURN : 0 -- success 512 * none-zero failure code 513 *==========================================================================*/ 514 int QCamera2Factory::setTorchMode(const char* camera_id, bool on) 515 { 516 int retVal(0); 517 long cameraIdLong(-1); 518 int cameraIdInt(-1); 519 char* endPointer = NULL; 520 errno = 0; 521 QCameraFlash& flash = QCameraFlash::getInstance(); 522 523 cameraIdLong = strtol(camera_id, &endPointer, 10); 524 525 if ((errno == ERANGE) || 526 (cameraIdLong < 0) || 527 (cameraIdLong >= static_cast<long>(get_number_of_cameras())) || 528 (endPointer == camera_id) || 529 (*endPointer != '\0')) { 530 retVal = -EINVAL; 531 } else if (on) { 532 cameraIdInt = static_cast<int>(cameraIdLong); 533 retVal = flash.initFlash(cameraIdInt); 534 535 if (retVal == 0) { 536 retVal = flash.setFlashMode(cameraIdInt, on); 537 if ((retVal == 0) && (mCallbacks != NULL)) { 538 mCallbacks->torch_mode_status_change(mCallbacks, 539 camera_id, 540 TORCH_MODE_STATUS_AVAILABLE_ON); 541 } else if (retVal == -EALREADY) { 542 // Flash is already on, so treat this as a success. 543 retVal = 0; 544 } 545 } 546 } else { 547 cameraIdInt = static_cast<int>(cameraIdLong); 548 retVal = flash.setFlashMode(cameraIdInt, on); 549 550 if (retVal == 0) { 551 retVal = flash.deinitFlash(cameraIdInt); 552 if ((retVal == 0) && (mCallbacks != NULL)) { 553 mCallbacks->torch_mode_status_change(mCallbacks, 554 camera_id, 555 TORCH_MODE_STATUS_AVAILABLE_OFF); 556 } 557 } else if (retVal == -EALREADY) { 558 // Flash is already off, so treat this as a success. 559 retVal = 0; 560 } 561 } 562 563 return retVal; 564 } 565 566 /*=========================================================================== 567 * FUNCTION : isDualCamAvailable 568 * 569 * DESCRIPTION: Function to check whether we have dual Camera HW available 570 * 571 * PARAMETERS : 572 * @hal3Enabled : HAL3 enable flag 573 * 574 * RETURN : bool - true : have Dual Camera HW available 575 * false : not have Dual Camera HW available 576 *==========================================================================*/ 577 bool QCamera2Factory::isDualCamAvailable(int hal3Enabled) 578 { 579 bool rc = FALSE; 580 int i = 0; 581 camera_info info; 582 cam_sync_type_t cam_type = CAM_TYPE_MAIN; 583 584 for (i = 0; i < mNumOfCameras; i++) { 585 if (!hal3Enabled) { 586 QCamera2HardwareInterface::getCapabilities(i, &info, &cam_type); 587 } 588 589 if(cam_type == CAM_TYPE_AUX) { 590 LOGH("Have Dual Camera HW Avaiable."); 591 rc = TRUE; 592 break; 593 } 594 } 595 596 return rc; 597 } 598 599 }; // namespace qcamera 600 601