1 /* 2 * Copyright (C) 2013 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 "Camera3-Device" 18 #define ATRACE_TAG ATRACE_TAG_CAMERA 19 //#define LOG_NDEBUG 0 20 //#define LOG_NNDEBUG 0 // Per-frame verbose logging 21 22 #ifdef LOG_NNDEBUG 23 #define ALOGVV(...) ALOGV(__VA_ARGS__) 24 #else 25 #define ALOGVV(...) ((void)0) 26 #endif 27 28 // Convenience macro for transient errors 29 #define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \ 30 ##__VA_ARGS__) 31 32 // Convenience macros for transitioning to the error state 33 #define SET_ERR(fmt, ...) setErrorState( \ 34 "%s: " fmt, __FUNCTION__, \ 35 ##__VA_ARGS__) 36 #define SET_ERR_L(fmt, ...) setErrorStateLocked( \ 37 "%s: " fmt, __FUNCTION__, \ 38 ##__VA_ARGS__) 39 40 #include <inttypes.h> 41 42 #include <utils/Log.h> 43 #include <utils/Trace.h> 44 #include <utils/Timers.h> 45 #include <cutils/properties.h> 46 47 #include <android/hardware/camera2/ICameraDeviceUser.h> 48 49 #include "utils/CameraTraces.h" 50 #include "mediautils/SchedulingPolicyService.h" 51 #include "device3/Camera3Device.h" 52 #include "device3/Camera3OutputStream.h" 53 #include "device3/Camera3InputStream.h" 54 #include "device3/Camera3ZslStream.h" 55 #include "device3/Camera3DummyStream.h" 56 #include "CameraService.h" 57 58 using namespace android::camera3; 59 60 namespace android { 61 62 Camera3Device::Camera3Device(int id): 63 mId(id), 64 mIsConstrainedHighSpeedConfiguration(false), 65 mHal3Device(NULL), 66 mStatus(STATUS_UNINITIALIZED), 67 mStatusWaiters(0), 68 mUsePartialResult(false), 69 mNumPartialResults(1), 70 mTimestampOffset(0), 71 mNextResultFrameNumber(0), 72 mNextReprocessResultFrameNumber(0), 73 mNextShutterFrameNumber(0), 74 mNextReprocessShutterFrameNumber(0), 75 mListener(NULL) 76 { 77 ATRACE_CALL(); 78 camera3_callback_ops::notify = &sNotify; 79 camera3_callback_ops::process_capture_result = &sProcessCaptureResult; 80 ALOGV("%s: Created device for camera %d", __FUNCTION__, id); 81 } 82 83 Camera3Device::~Camera3Device() 84 { 85 ATRACE_CALL(); 86 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId); 87 disconnect(); 88 } 89 90 int Camera3Device::getId() const { 91 return mId; 92 } 93 94 /** 95 * CameraDeviceBase interface 96 */ 97 98 status_t Camera3Device::initialize(CameraModule *module) 99 { 100 ATRACE_CALL(); 101 Mutex::Autolock il(mInterfaceLock); 102 Mutex::Autolock l(mLock); 103 104 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId); 105 if (mStatus != STATUS_UNINITIALIZED) { 106 CLOGE("Already initialized!"); 107 return INVALID_OPERATION; 108 } 109 110 /** Open HAL device */ 111 112 status_t res; 113 String8 deviceName = String8::format("%d", mId); 114 115 camera3_device_t *device; 116 117 ATRACE_BEGIN("camera3->open"); 118 res = module->open(deviceName.string(), 119 reinterpret_cast<hw_device_t**>(&device)); 120 ATRACE_END(); 121 122 if (res != OK) { 123 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res); 124 return res; 125 } 126 127 /** Cross-check device version */ 128 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) { 129 SET_ERR_L("Could not open camera: " 130 "Camera device should be at least %x, reports %x instead", 131 CAMERA_DEVICE_API_VERSION_3_0, 132 device->common.version); 133 device->common.close(&device->common); 134 return BAD_VALUE; 135 } 136 137 camera_info info; 138 res = module->getCameraInfo(mId, &info); 139 if (res != OK) return res; 140 141 if (info.device_version != device->common.version) { 142 SET_ERR_L("HAL reporting mismatched camera_info version (%x)" 143 " and device version (%x).", 144 info.device_version, device->common.version); 145 device->common.close(&device->common); 146 return BAD_VALUE; 147 } 148 149 /** Initialize device with callback functions */ 150 151 ATRACE_BEGIN("camera3->initialize"); 152 res = device->ops->initialize(device, this); 153 ATRACE_END(); 154 155 if (res != OK) { 156 SET_ERR_L("Unable to initialize HAL device: %s (%d)", 157 strerror(-res), res); 158 device->common.close(&device->common); 159 return BAD_VALUE; 160 } 161 162 /** Start up status tracker thread */ 163 mStatusTracker = new StatusTracker(this); 164 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string()); 165 if (res != OK) { 166 SET_ERR_L("Unable to start status tracking thread: %s (%d)", 167 strerror(-res), res); 168 device->common.close(&device->common); 169 mStatusTracker.clear(); 170 return res; 171 } 172 173 /** Create buffer manager */ 174 mBufferManager = new Camera3BufferManager(); 175 176 bool aeLockAvailable = false; 177 camera_metadata_ro_entry aeLockAvailableEntry; 178 res = find_camera_metadata_ro_entry(info.static_camera_characteristics, 179 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry); 180 if (res == OK && aeLockAvailableEntry.count > 0) { 181 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] == 182 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE); 183 } 184 185 /** Start up request queue thread */ 186 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable); 187 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string()); 188 if (res != OK) { 189 SET_ERR_L("Unable to start request queue thread: %s (%d)", 190 strerror(-res), res); 191 device->common.close(&device->common); 192 mRequestThread.clear(); 193 return res; 194 } 195 196 mPreparerThread = new PreparerThread(); 197 198 /** Everything is good to go */ 199 200 mDeviceVersion = device->common.version; 201 mDeviceInfo = info.static_camera_characteristics; 202 mHal3Device = device; 203 204 // Determine whether we need to derive sensitivity boost values for older devices. 205 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control 206 // be listed (as the default value 100) 207 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 && 208 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) { 209 mDerivePostRawSensKey = true; 210 } 211 212 internalUpdateStatusLocked(STATUS_UNCONFIGURED); 213 mNextStreamId = 0; 214 mDummyStreamId = NO_STREAM; 215 mNeedConfig = true; 216 mPauseStateNotify = false; 217 218 // Measure the clock domain offset between camera and video/hw_composer 219 camera_metadata_entry timestampSource = 220 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE); 221 if (timestampSource.count > 0 && timestampSource.data.u8[0] == 222 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) { 223 mTimestampOffset = getMonoToBoottimeOffset(); 224 } 225 226 // Will the HAL be sending in early partial result metadata? 227 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { 228 camera_metadata_entry partialResultsCount = 229 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT); 230 if (partialResultsCount.count > 0) { 231 mNumPartialResults = partialResultsCount.data.i32[0]; 232 mUsePartialResult = (mNumPartialResults > 1); 233 } 234 } else { 235 camera_metadata_entry partialResultsQuirk = 236 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT); 237 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) { 238 mUsePartialResult = true; 239 } 240 } 241 242 camera_metadata_entry configs = 243 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); 244 for (uint32_t i = 0; i < configs.count; i += 4) { 245 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && 246 configs.data.i32[i + 3] == 247 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) { 248 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1], 249 configs.data.i32[i + 2])); 250 } 251 } 252 253 return OK; 254 } 255 256 status_t Camera3Device::disconnect() { 257 ATRACE_CALL(); 258 Mutex::Autolock il(mInterfaceLock); 259 260 ALOGI("%s: E", __FUNCTION__); 261 262 status_t res = OK; 263 264 { 265 Mutex::Autolock l(mLock); 266 if (mStatus == STATUS_UNINITIALIZED) return res; 267 268 if (mStatus == STATUS_ACTIVE || 269 (mStatus == STATUS_ERROR && mRequestThread != NULL)) { 270 res = mRequestThread->clearRepeatingRequests(); 271 if (res != OK) { 272 SET_ERR_L("Can't stop streaming"); 273 // Continue to close device even in case of error 274 } else { 275 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); 276 if (res != OK) { 277 SET_ERR_L("Timeout waiting for HAL to drain"); 278 // Continue to close device even in case of error 279 } 280 } 281 } 282 283 if (mStatus == STATUS_ERROR) { 284 CLOGE("Shutting down in an error state"); 285 } 286 287 if (mStatusTracker != NULL) { 288 mStatusTracker->requestExit(); 289 } 290 291 if (mRequestThread != NULL) { 292 mRequestThread->requestExit(); 293 } 294 295 mOutputStreams.clear(); 296 mInputStream.clear(); 297 } 298 299 // Joining done without holding mLock, otherwise deadlocks may ensue 300 // as the threads try to access parent state 301 if (mRequestThread != NULL && mStatus != STATUS_ERROR) { 302 // HAL may be in a bad state, so waiting for request thread 303 // (which may be stuck in the HAL processCaptureRequest call) 304 // could be dangerous. 305 mRequestThread->join(); 306 } 307 308 if (mStatusTracker != NULL) { 309 mStatusTracker->join(); 310 } 311 312 camera3_device_t *hal3Device; 313 { 314 Mutex::Autolock l(mLock); 315 316 mRequestThread.clear(); 317 mStatusTracker.clear(); 318 mBufferManager.clear(); 319 320 hal3Device = mHal3Device; 321 } 322 323 // Call close without internal mutex held, as the HAL close may need to 324 // wait on assorted callbacks,etc, to complete before it can return. 325 if (hal3Device != NULL) { 326 ATRACE_BEGIN("camera3->close"); 327 hal3Device->common.close(&hal3Device->common); 328 ATRACE_END(); 329 } 330 331 { 332 Mutex::Autolock l(mLock); 333 mHal3Device = NULL; 334 internalUpdateStatusLocked(STATUS_UNINITIALIZED); 335 } 336 337 ALOGI("%s: X", __FUNCTION__); 338 return res; 339 } 340 341 // For dumping/debugging only - 342 // try to acquire a lock a few times, eventually give up to proceed with 343 // debug/dump operations 344 bool Camera3Device::tryLockSpinRightRound(Mutex& lock) { 345 bool gotLock = false; 346 for (size_t i = 0; i < kDumpLockAttempts; ++i) { 347 if (lock.tryLock() == NO_ERROR) { 348 gotLock = true; 349 break; 350 } else { 351 usleep(kDumpSleepDuration); 352 } 353 } 354 return gotLock; 355 } 356 357 Camera3Device::Size Camera3Device::getMaxJpegResolution() const { 358 int32_t maxJpegWidth = 0, maxJpegHeight = 0; 359 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { 360 const int STREAM_CONFIGURATION_SIZE = 4; 361 const int STREAM_FORMAT_OFFSET = 0; 362 const int STREAM_WIDTH_OFFSET = 1; 363 const int STREAM_HEIGHT_OFFSET = 2; 364 const int STREAM_IS_INPUT_OFFSET = 3; 365 camera_metadata_ro_entry_t availableStreamConfigs = 366 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); 367 if (availableStreamConfigs.count == 0 || 368 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) { 369 return Size(0, 0); 370 } 371 372 // Get max jpeg size (area-wise). 373 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) { 374 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET]; 375 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET]; 376 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET]; 377 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET]; 378 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT 379 && format == HAL_PIXEL_FORMAT_BLOB && 380 (width * height > maxJpegWidth * maxJpegHeight)) { 381 maxJpegWidth = width; 382 maxJpegHeight = height; 383 } 384 } 385 } else { 386 camera_metadata_ro_entry availableJpegSizes = 387 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES); 388 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) { 389 return Size(0, 0); 390 } 391 392 // Get max jpeg size (area-wise). 393 for (size_t i = 0; i < availableJpegSizes.count; i += 2) { 394 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1]) 395 > (maxJpegWidth * maxJpegHeight)) { 396 maxJpegWidth = availableJpegSizes.data.i32[i]; 397 maxJpegHeight = availableJpegSizes.data.i32[i + 1]; 398 } 399 } 400 } 401 return Size(maxJpegWidth, maxJpegHeight); 402 } 403 404 nsecs_t Camera3Device::getMonoToBoottimeOffset() { 405 // try three times to get the clock offset, choose the one 406 // with the minimum gap in measurements. 407 const int tries = 3; 408 nsecs_t bestGap, measured; 409 for (int i = 0; i < tries; ++i) { 410 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); 411 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME); 412 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); 413 const nsecs_t gap = tmono2 - tmono; 414 if (i == 0 || gap < bestGap) { 415 bestGap = gap; 416 measured = tbase - ((tmono + tmono2) >> 1); 417 } 418 } 419 return measured; 420 } 421 422 /** 423 * Map Android N dataspace definitions back to Android M definitions, for 424 * use with HALv3.3 or older. 425 * 426 * Only map where correspondences exist, and otherwise preserve the value. 427 */ 428 android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) { 429 switch (dataSpace) { 430 case HAL_DATASPACE_V0_SRGB_LINEAR: 431 return HAL_DATASPACE_SRGB_LINEAR; 432 case HAL_DATASPACE_V0_SRGB: 433 return HAL_DATASPACE_SRGB; 434 case HAL_DATASPACE_V0_JFIF: 435 return HAL_DATASPACE_JFIF; 436 case HAL_DATASPACE_V0_BT601_625: 437 return HAL_DATASPACE_BT601_625; 438 case HAL_DATASPACE_V0_BT601_525: 439 return HAL_DATASPACE_BT601_525; 440 case HAL_DATASPACE_V0_BT709: 441 return HAL_DATASPACE_BT709; 442 default: 443 return dataSpace; 444 } 445 } 446 447 ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const { 448 // Get max jpeg size (area-wise). 449 Size maxJpegResolution = getMaxJpegResolution(); 450 if (maxJpegResolution.width == 0) { 451 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!", 452 __FUNCTION__, mId); 453 return BAD_VALUE; 454 } 455 456 // Get max jpeg buffer size 457 ssize_t maxJpegBufferSize = 0; 458 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE); 459 if (jpegBufMaxSize.count == 0) { 460 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId); 461 return BAD_VALUE; 462 } 463 maxJpegBufferSize = jpegBufMaxSize.data.i32[0]; 464 assert(kMinJpegBufferSize < maxJpegBufferSize); 465 466 // Calculate final jpeg buffer size for the given resolution. 467 float scaleFactor = ((float) (width * height)) / 468 (maxJpegResolution.width * maxJpegResolution.height); 469 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) + 470 kMinJpegBufferSize; 471 if (jpegBufferSize > maxJpegBufferSize) { 472 jpegBufferSize = maxJpegBufferSize; 473 } 474 475 return jpegBufferSize; 476 } 477 478 ssize_t Camera3Device::getPointCloudBufferSize() const { 479 const int FLOATS_PER_POINT=4; 480 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES); 481 if (maxPointCount.count == 0) { 482 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!", 483 __FUNCTION__, mId); 484 return BAD_VALUE; 485 } 486 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) + 487 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT; 488 return maxBytesForPointCloud; 489 } 490 491 ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const { 492 const int PER_CONFIGURATION_SIZE = 3; 493 const int WIDTH_OFFSET = 0; 494 const int HEIGHT_OFFSET = 1; 495 const int SIZE_OFFSET = 2; 496 camera_metadata_ro_entry rawOpaqueSizes = 497 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE); 498 size_t count = rawOpaqueSizes.count; 499 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) { 500 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!", 501 __FUNCTION__, mId, count); 502 return BAD_VALUE; 503 } 504 505 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) { 506 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] && 507 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) { 508 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET]; 509 } 510 } 511 512 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!", 513 __FUNCTION__, mId, width, height); 514 return BAD_VALUE; 515 } 516 517 status_t Camera3Device::dump(int fd, const Vector<String16> &args) { 518 ATRACE_CALL(); 519 (void)args; 520 521 // Try to lock, but continue in case of failure (to avoid blocking in 522 // deadlocks) 523 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock); 524 bool gotLock = tryLockSpinRightRound(mLock); 525 526 ALOGW_IF(!gotInterfaceLock, 527 "Camera %d: %s: Unable to lock interface lock, proceeding anyway", 528 mId, __FUNCTION__); 529 ALOGW_IF(!gotLock, 530 "Camera %d: %s: Unable to lock main lock, proceeding anyway", 531 mId, __FUNCTION__); 532 533 bool dumpTemplates = false; 534 535 String16 templatesOption("-t"); 536 String16 monitorOption("-m"); 537 int n = args.size(); 538 for (int i = 0; i < n; i++) { 539 if (args[i] == templatesOption) { 540 dumpTemplates = true; 541 } 542 if (args[i] == monitorOption) { 543 if (i + 1 < n) { 544 String8 monitorTags = String8(args[i + 1]); 545 if (monitorTags == "off") { 546 mTagMonitor.disableMonitoring(); 547 } else { 548 mTagMonitor.parseTagsToMonitor(monitorTags); 549 } 550 } else { 551 mTagMonitor.disableMonitoring(); 552 } 553 } 554 } 555 556 String8 lines; 557 558 const char *status = 559 mStatus == STATUS_ERROR ? "ERROR" : 560 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : 561 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" : 562 mStatus == STATUS_CONFIGURED ? "CONFIGURED" : 563 mStatus == STATUS_ACTIVE ? "ACTIVE" : 564 "Unknown"; 565 566 lines.appendFormat(" Device status: %s\n", status); 567 if (mStatus == STATUS_ERROR) { 568 lines.appendFormat(" Error cause: %s\n", mErrorCause.string()); 569 } 570 lines.appendFormat(" Stream configuration:\n"); 571 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ? 572 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL"); 573 574 if (mInputStream != NULL) { 575 write(fd, lines.string(), lines.size()); 576 mInputStream->dump(fd, args); 577 } else { 578 lines.appendFormat(" No input stream.\n"); 579 write(fd, lines.string(), lines.size()); 580 } 581 for (size_t i = 0; i < mOutputStreams.size(); i++) { 582 mOutputStreams[i]->dump(fd,args); 583 } 584 585 if (mBufferManager != NULL) { 586 lines = String8(" Camera3 Buffer Manager:\n"); 587 write(fd, lines.string(), lines.size()); 588 mBufferManager->dump(fd, args); 589 } 590 591 lines = String8(" In-flight requests:\n"); 592 if (mInFlightMap.size() == 0) { 593 lines.append(" None\n"); 594 } else { 595 for (size_t i = 0; i < mInFlightMap.size(); i++) { 596 InFlightRequest r = mInFlightMap.valueAt(i); 597 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata" 598 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), 599 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false", 600 r.numBuffersLeft); 601 } 602 } 603 write(fd, lines.string(), lines.size()); 604 605 { 606 lines = String8(" Last request sent:\n"); 607 write(fd, lines.string(), lines.size()); 608 609 CameraMetadata lastRequest = getLatestRequestLocked(); 610 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6); 611 } 612 613 if (dumpTemplates) { 614 const char *templateNames[] = { 615 "TEMPLATE_PREVIEW", 616 "TEMPLATE_STILL_CAPTURE", 617 "TEMPLATE_VIDEO_RECORD", 618 "TEMPLATE_VIDEO_SNAPSHOT", 619 "TEMPLATE_ZERO_SHUTTER_LAG", 620 "TEMPLATE_MANUAL" 621 }; 622 623 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) { 624 const camera_metadata_t *templateRequest; 625 templateRequest = 626 mHal3Device->ops->construct_default_request_settings( 627 mHal3Device, i); 628 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]); 629 if (templateRequest == NULL) { 630 lines.append(" Not supported\n"); 631 write(fd, lines.string(), lines.size()); 632 } else { 633 write(fd, lines.string(), lines.size()); 634 dump_indented_camera_metadata(templateRequest, 635 fd, /*verbosity*/2, /*indentation*/8); 636 } 637 } 638 } 639 640 mTagMonitor.dumpMonitoredMetadata(fd); 641 642 if (mHal3Device != NULL) { 643 lines = String8(" HAL device dump:\n"); 644 write(fd, lines.string(), lines.size()); 645 mHal3Device->ops->dump(mHal3Device, fd); 646 } 647 648 if (gotLock) mLock.unlock(); 649 if (gotInterfaceLock) mInterfaceLock.unlock(); 650 651 return OK; 652 } 653 654 const CameraMetadata& Camera3Device::info() const { 655 ALOGVV("%s: E", __FUNCTION__); 656 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || 657 mStatus == STATUS_ERROR)) { 658 ALOGW("%s: Access to static info %s!", __FUNCTION__, 659 mStatus == STATUS_ERROR ? 660 "when in error state" : "before init"); 661 } 662 return mDeviceInfo; 663 } 664 665 status_t Camera3Device::checkStatusOkToCaptureLocked() { 666 switch (mStatus) { 667 case STATUS_ERROR: 668 CLOGE("Device has encountered a serious error"); 669 return INVALID_OPERATION; 670 case STATUS_UNINITIALIZED: 671 CLOGE("Device not initialized"); 672 return INVALID_OPERATION; 673 case STATUS_UNCONFIGURED: 674 case STATUS_CONFIGURED: 675 case STATUS_ACTIVE: 676 // OK 677 break; 678 default: 679 SET_ERR_L("Unexpected status: %d", mStatus); 680 return INVALID_OPERATION; 681 } 682 return OK; 683 } 684 685 status_t Camera3Device::convertMetadataListToRequestListLocked( 686 const List<const CameraMetadata> &metadataList, RequestList *requestList) { 687 if (requestList == NULL) { 688 CLOGE("requestList cannot be NULL."); 689 return BAD_VALUE; 690 } 691 692 int32_t burstId = 0; 693 for (List<const CameraMetadata>::const_iterator it = metadataList.begin(); 694 it != metadataList.end(); ++it) { 695 sp<CaptureRequest> newRequest = setUpRequestLocked(*it); 696 if (newRequest == 0) { 697 CLOGE("Can't create capture request"); 698 return BAD_VALUE; 699 } 700 701 // Setup burst Id and request Id 702 newRequest->mResultExtras.burstId = burstId++; 703 if (it->exists(ANDROID_REQUEST_ID)) { 704 if (it->find(ANDROID_REQUEST_ID).count == 0) { 705 CLOGE("RequestID entry exists; but must not be empty in metadata"); 706 return BAD_VALUE; 707 } 708 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0]; 709 } else { 710 CLOGE("RequestID does not exist in metadata"); 711 return BAD_VALUE; 712 } 713 714 requestList->push_back(newRequest); 715 716 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId); 717 } 718 719 // Setup batch size if this is a high speed video recording request. 720 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) { 721 auto firstRequest = requestList->begin(); 722 for (auto& outputStream : (*firstRequest)->mOutputStreams) { 723 if (outputStream->isVideoStream()) { 724 (*firstRequest)->mBatchSize = requestList->size(); 725 break; 726 } 727 } 728 } 729 730 return OK; 731 } 732 733 status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) { 734 ATRACE_CALL(); 735 736 List<const CameraMetadata> requests; 737 requests.push_back(request); 738 return captureList(requests, /*lastFrameNumber*/NULL); 739 } 740 741 status_t Camera3Device::submitRequestsHelper( 742 const List<const CameraMetadata> &requests, bool repeating, 743 /*out*/ 744 int64_t *lastFrameNumber) { 745 ATRACE_CALL(); 746 Mutex::Autolock il(mInterfaceLock); 747 Mutex::Autolock l(mLock); 748 749 status_t res = checkStatusOkToCaptureLocked(); 750 if (res != OK) { 751 // error logged by previous call 752 return res; 753 } 754 755 RequestList requestList; 756 757 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList); 758 if (res != OK) { 759 // error logged by previous call 760 return res; 761 } 762 763 if (repeating) { 764 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber); 765 } else { 766 res = mRequestThread->queueRequestList(requestList, lastFrameNumber); 767 } 768 769 if (res == OK) { 770 waitUntilStateThenRelock(/*active*/true, kActiveTimeout); 771 if (res != OK) { 772 SET_ERR_L("Can't transition to active in %f seconds!", 773 kActiveTimeout/1e9); 774 } 775 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId, 776 (*(requestList.begin()))->mResultExtras.requestId); 777 } else { 778 CLOGE("Cannot queue request. Impossible."); 779 return BAD_VALUE; 780 } 781 782 return res; 783 } 784 785 status_t Camera3Device::captureList(const List<const CameraMetadata> &requests, 786 int64_t *lastFrameNumber) { 787 ATRACE_CALL(); 788 789 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber); 790 } 791 792 status_t Camera3Device::setStreamingRequest(const CameraMetadata &request, 793 int64_t* /*lastFrameNumber*/) { 794 ATRACE_CALL(); 795 796 List<const CameraMetadata> requests; 797 requests.push_back(request); 798 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL); 799 } 800 801 status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests, 802 int64_t *lastFrameNumber) { 803 ATRACE_CALL(); 804 805 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber); 806 } 807 808 sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( 809 const CameraMetadata &request) { 810 status_t res; 811 812 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) { 813 res = configureStreamsLocked(); 814 // Stream configuration failed. Client might try other configuraitons. 815 if (res != OK) { 816 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res); 817 return NULL; 818 } else if (mStatus == STATUS_UNCONFIGURED) { 819 // Stream configuration successfully configure to empty stream configuration. 820 CLOGE("No streams configured"); 821 return NULL; 822 } 823 } 824 825 sp<CaptureRequest> newRequest = createCaptureRequest(request); 826 return newRequest; 827 } 828 829 status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) { 830 ATRACE_CALL(); 831 Mutex::Autolock il(mInterfaceLock); 832 Mutex::Autolock l(mLock); 833 834 switch (mStatus) { 835 case STATUS_ERROR: 836 CLOGE("Device has encountered a serious error"); 837 return INVALID_OPERATION; 838 case STATUS_UNINITIALIZED: 839 CLOGE("Device not initialized"); 840 return INVALID_OPERATION; 841 case STATUS_UNCONFIGURED: 842 case STATUS_CONFIGURED: 843 case STATUS_ACTIVE: 844 // OK 845 break; 846 default: 847 SET_ERR_L("Unexpected status: %d", mStatus); 848 return INVALID_OPERATION; 849 } 850 ALOGV("Camera %d: Clearing repeating request", mId); 851 852 return mRequestThread->clearRepeatingRequests(lastFrameNumber); 853 } 854 855 status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { 856 ATRACE_CALL(); 857 Mutex::Autolock il(mInterfaceLock); 858 859 return mRequestThread->waitUntilRequestProcessed(requestId, timeout); 860 } 861 862 status_t Camera3Device::createInputStream( 863 uint32_t width, uint32_t height, int format, int *id) { 864 ATRACE_CALL(); 865 Mutex::Autolock il(mInterfaceLock); 866 Mutex::Autolock l(mLock); 867 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d", 868 mId, mNextStreamId, width, height, format); 869 870 status_t res; 871 bool wasActive = false; 872 873 switch (mStatus) { 874 case STATUS_ERROR: 875 ALOGE("%s: Device has encountered a serious error", __FUNCTION__); 876 return INVALID_OPERATION; 877 case STATUS_UNINITIALIZED: 878 ALOGE("%s: Device not initialized", __FUNCTION__); 879 return INVALID_OPERATION; 880 case STATUS_UNCONFIGURED: 881 case STATUS_CONFIGURED: 882 // OK 883 break; 884 case STATUS_ACTIVE: 885 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 886 res = internalPauseAndWaitLocked(); 887 if (res != OK) { 888 SET_ERR_L("Can't pause captures to reconfigure streams!"); 889 return res; 890 } 891 wasActive = true; 892 break; 893 default: 894 SET_ERR_L("%s: Unexpected status: %d", mStatus); 895 return INVALID_OPERATION; 896 } 897 assert(mStatus != STATUS_ACTIVE); 898 899 if (mInputStream != 0) { 900 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); 901 return INVALID_OPERATION; 902 } 903 904 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, 905 width, height, format); 906 newStream->setStatusTracker(mStatusTracker); 907 908 mInputStream = newStream; 909 910 *id = mNextStreamId++; 911 912 // Continue captures if active at start 913 if (wasActive) { 914 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 915 res = configureStreamsLocked(); 916 if (res != OK) { 917 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", 918 __FUNCTION__, mNextStreamId, strerror(-res), res); 919 return res; 920 } 921 internalResumeLocked(); 922 } 923 924 ALOGV("Camera %d: Created input stream", mId); 925 return OK; 926 } 927 928 929 status_t Camera3Device::createZslStream( 930 uint32_t width, uint32_t height, 931 int depth, 932 /*out*/ 933 int *id, 934 sp<Camera3ZslStream>* zslStream) { 935 ATRACE_CALL(); 936 Mutex::Autolock il(mInterfaceLock); 937 Mutex::Autolock l(mLock); 938 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d", 939 mId, mNextStreamId, width, height, depth); 940 941 status_t res; 942 bool wasActive = false; 943 944 switch (mStatus) { 945 case STATUS_ERROR: 946 ALOGE("%s: Device has encountered a serious error", __FUNCTION__); 947 return INVALID_OPERATION; 948 case STATUS_UNINITIALIZED: 949 ALOGE("%s: Device not initialized", __FUNCTION__); 950 return INVALID_OPERATION; 951 case STATUS_UNCONFIGURED: 952 case STATUS_CONFIGURED: 953 // OK 954 break; 955 case STATUS_ACTIVE: 956 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 957 res = internalPauseAndWaitLocked(); 958 if (res != OK) { 959 SET_ERR_L("Can't pause captures to reconfigure streams!"); 960 return res; 961 } 962 wasActive = true; 963 break; 964 default: 965 SET_ERR_L("Unexpected status: %d", mStatus); 966 return INVALID_OPERATION; 967 } 968 assert(mStatus != STATUS_ACTIVE); 969 970 if (mInputStream != 0) { 971 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); 972 return INVALID_OPERATION; 973 } 974 975 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId, 976 width, height, depth); 977 newStream->setStatusTracker(mStatusTracker); 978 979 res = mOutputStreams.add(mNextStreamId, newStream); 980 if (res < 0) { 981 ALOGE("%s: Can't add new stream to set: %s (%d)", 982 __FUNCTION__, strerror(-res), res); 983 return res; 984 } 985 mInputStream = newStream; 986 987 mNeedConfig = true; 988 989 *id = mNextStreamId++; 990 *zslStream = newStream; 991 992 // Continue captures if active at start 993 if (wasActive) { 994 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 995 res = configureStreamsLocked(); 996 if (res != OK) { 997 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", 998 __FUNCTION__, mNextStreamId, strerror(-res), res); 999 return res; 1000 } 1001 internalResumeLocked(); 1002 } 1003 1004 ALOGV("Camera %d: Created ZSL stream", mId); 1005 return OK; 1006 } 1007 1008 status_t Camera3Device::createStream(sp<Surface> consumer, 1009 uint32_t width, uint32_t height, int format, android_dataspace dataSpace, 1010 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) { 1011 ATRACE_CALL(); 1012 Mutex::Autolock il(mInterfaceLock); 1013 Mutex::Autolock l(mLock); 1014 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d" 1015 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation, 1016 consumerUsage); 1017 1018 status_t res; 1019 bool wasActive = false; 1020 1021 switch (mStatus) { 1022 case STATUS_ERROR: 1023 CLOGE("Device has encountered a serious error"); 1024 return INVALID_OPERATION; 1025 case STATUS_UNINITIALIZED: 1026 CLOGE("Device not initialized"); 1027 return INVALID_OPERATION; 1028 case STATUS_UNCONFIGURED: 1029 case STATUS_CONFIGURED: 1030 // OK 1031 break; 1032 case STATUS_ACTIVE: 1033 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 1034 res = internalPauseAndWaitLocked(); 1035 if (res != OK) { 1036 SET_ERR_L("Can't pause captures to reconfigure streams!"); 1037 return res; 1038 } 1039 wasActive = true; 1040 break; 1041 default: 1042 SET_ERR_L("Unexpected status: %d", mStatus); 1043 return INVALID_OPERATION; 1044 } 1045 assert(mStatus != STATUS_ACTIVE); 1046 1047 sp<Camera3OutputStream> newStream; 1048 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support 1049 // such devices. 1050 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) { 1051 streamSetId = CAMERA3_STREAM_SET_ID_INVALID; 1052 } 1053 1054 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration 1055 // which requires a consumer surface to be available. 1056 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) { 1057 ALOGE("HAL3.1 doesn't support deferred consumer stream creation"); 1058 return BAD_VALUE; 1059 } 1060 1061 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { 1062 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format"); 1063 return BAD_VALUE; 1064 } 1065 1066 // Use legacy dataspace values for older HALs 1067 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) { 1068 dataSpace = mapToLegacyDataspace(dataSpace); 1069 } 1070 if (format == HAL_PIXEL_FORMAT_BLOB) { 1071 ssize_t blobBufferSize; 1072 if (dataSpace != HAL_DATASPACE_DEPTH) { 1073 blobBufferSize = getJpegBufferSize(width, height); 1074 if (blobBufferSize <= 0) { 1075 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize); 1076 return BAD_VALUE; 1077 } 1078 } else { 1079 blobBufferSize = getPointCloudBufferSize(); 1080 if (blobBufferSize <= 0) { 1081 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize); 1082 return BAD_VALUE; 1083 } 1084 } 1085 newStream = new Camera3OutputStream(mNextStreamId, consumer, 1086 width, height, blobBufferSize, format, dataSpace, rotation, 1087 mTimestampOffset, streamSetId); 1088 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) { 1089 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height); 1090 if (rawOpaqueBufferSize <= 0) { 1091 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize); 1092 return BAD_VALUE; 1093 } 1094 newStream = new Camera3OutputStream(mNextStreamId, consumer, 1095 width, height, rawOpaqueBufferSize, format, dataSpace, rotation, 1096 mTimestampOffset, streamSetId); 1097 } else if (consumer == nullptr) { 1098 newStream = new Camera3OutputStream(mNextStreamId, 1099 width, height, format, consumerUsage, dataSpace, rotation, 1100 mTimestampOffset, streamSetId); 1101 } else { 1102 newStream = new Camera3OutputStream(mNextStreamId, consumer, 1103 width, height, format, dataSpace, rotation, 1104 mTimestampOffset, streamSetId); 1105 } 1106 newStream->setStatusTracker(mStatusTracker); 1107 1108 /** 1109 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2) 1110 * requires buffers to be statically allocated for internal static buffer registration, while 1111 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because 1112 * not all HAL implementation supports dynamic buffer registeration, exlude it as well. 1113 */ 1114 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) { 1115 newStream->setBufferManager(mBufferManager); 1116 } 1117 1118 res = mOutputStreams.add(mNextStreamId, newStream); 1119 if (res < 0) { 1120 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res); 1121 return res; 1122 } 1123 1124 *id = mNextStreamId++; 1125 mNeedConfig = true; 1126 1127 // Continue captures if active at start 1128 if (wasActive) { 1129 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 1130 res = configureStreamsLocked(); 1131 if (res != OK) { 1132 CLOGE("Can't reconfigure device for new stream %d: %s (%d)", 1133 mNextStreamId, strerror(-res), res); 1134 return res; 1135 } 1136 internalResumeLocked(); 1137 } 1138 ALOGV("Camera %d: Created new stream", mId); 1139 return OK; 1140 } 1141 1142 status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) { 1143 ATRACE_CALL(); 1144 (void)outputId; (void)id; 1145 1146 CLOGE("Unimplemented"); 1147 return INVALID_OPERATION; 1148 } 1149 1150 1151 status_t Camera3Device::getStreamInfo(int id, 1152 uint32_t *width, uint32_t *height, 1153 uint32_t *format, android_dataspace *dataSpace) { 1154 ATRACE_CALL(); 1155 Mutex::Autolock il(mInterfaceLock); 1156 Mutex::Autolock l(mLock); 1157 1158 switch (mStatus) { 1159 case STATUS_ERROR: 1160 CLOGE("Device has encountered a serious error"); 1161 return INVALID_OPERATION; 1162 case STATUS_UNINITIALIZED: 1163 CLOGE("Device not initialized!"); 1164 return INVALID_OPERATION; 1165 case STATUS_UNCONFIGURED: 1166 case STATUS_CONFIGURED: 1167 case STATUS_ACTIVE: 1168 // OK 1169 break; 1170 default: 1171 SET_ERR_L("Unexpected status: %d", mStatus); 1172 return INVALID_OPERATION; 1173 } 1174 1175 ssize_t idx = mOutputStreams.indexOfKey(id); 1176 if (idx == NAME_NOT_FOUND) { 1177 CLOGE("Stream %d is unknown", id); 1178 return idx; 1179 } 1180 1181 if (width) *width = mOutputStreams[idx]->getWidth(); 1182 if (height) *height = mOutputStreams[idx]->getHeight(); 1183 if (format) *format = mOutputStreams[idx]->getFormat(); 1184 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace(); 1185 return OK; 1186 } 1187 1188 status_t Camera3Device::setStreamTransform(int id, 1189 int transform) { 1190 ATRACE_CALL(); 1191 Mutex::Autolock il(mInterfaceLock); 1192 Mutex::Autolock l(mLock); 1193 1194 switch (mStatus) { 1195 case STATUS_ERROR: 1196 CLOGE("Device has encountered a serious error"); 1197 return INVALID_OPERATION; 1198 case STATUS_UNINITIALIZED: 1199 CLOGE("Device not initialized"); 1200 return INVALID_OPERATION; 1201 case STATUS_UNCONFIGURED: 1202 case STATUS_CONFIGURED: 1203 case STATUS_ACTIVE: 1204 // OK 1205 break; 1206 default: 1207 SET_ERR_L("Unexpected status: %d", mStatus); 1208 return INVALID_OPERATION; 1209 } 1210 1211 ssize_t idx = mOutputStreams.indexOfKey(id); 1212 if (idx == NAME_NOT_FOUND) { 1213 CLOGE("Stream %d does not exist", 1214 id); 1215 return BAD_VALUE; 1216 } 1217 1218 return mOutputStreams.editValueAt(idx)->setTransform(transform); 1219 } 1220 1221 status_t Camera3Device::deleteStream(int id) { 1222 ATRACE_CALL(); 1223 Mutex::Autolock il(mInterfaceLock); 1224 Mutex::Autolock l(mLock); 1225 status_t res; 1226 1227 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id); 1228 1229 // CameraDevice semantics require device to already be idle before 1230 // deleteStream is called, unlike for createStream. 1231 if (mStatus == STATUS_ACTIVE) { 1232 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId); 1233 return -EBUSY; 1234 } 1235 1236 sp<Camera3StreamInterface> deletedStream; 1237 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id); 1238 if (mInputStream != NULL && id == mInputStream->getId()) { 1239 deletedStream = mInputStream; 1240 mInputStream.clear(); 1241 } else { 1242 if (outputStreamIdx == NAME_NOT_FOUND) { 1243 CLOGE("Stream %d does not exist", id); 1244 return BAD_VALUE; 1245 } 1246 } 1247 1248 // Delete output stream or the output part of a bi-directional stream. 1249 if (outputStreamIdx != NAME_NOT_FOUND) { 1250 deletedStream = mOutputStreams.editValueAt(outputStreamIdx); 1251 mOutputStreams.removeItem(id); 1252 } 1253 1254 // Free up the stream endpoint so that it can be used by some other stream 1255 res = deletedStream->disconnect(); 1256 if (res != OK) { 1257 SET_ERR_L("Can't disconnect deleted stream %d", id); 1258 // fall through since we want to still list the stream as deleted. 1259 } 1260 mDeletedStreams.add(deletedStream); 1261 mNeedConfig = true; 1262 1263 return res; 1264 } 1265 1266 status_t Camera3Device::deleteReprocessStream(int id) { 1267 ATRACE_CALL(); 1268 (void)id; 1269 1270 CLOGE("Unimplemented"); 1271 return INVALID_OPERATION; 1272 } 1273 1274 status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) { 1275 ATRACE_CALL(); 1276 ALOGV("%s: E", __FUNCTION__); 1277 1278 Mutex::Autolock il(mInterfaceLock); 1279 Mutex::Autolock l(mLock); 1280 1281 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) { 1282 mNeedConfig = true; 1283 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed; 1284 } 1285 1286 return configureStreamsLocked(); 1287 } 1288 1289 status_t Camera3Device::getInputBufferProducer( 1290 sp<IGraphicBufferProducer> *producer) { 1291 Mutex::Autolock il(mInterfaceLock); 1292 Mutex::Autolock l(mLock); 1293 1294 if (producer == NULL) { 1295 return BAD_VALUE; 1296 } else if (mInputStream == NULL) { 1297 return INVALID_OPERATION; 1298 } 1299 1300 return mInputStream->getInputBufferProducer(producer); 1301 } 1302 1303 status_t Camera3Device::createDefaultRequest(int templateId, 1304 CameraMetadata *request) { 1305 ATRACE_CALL(); 1306 ALOGV("%s: for template %d", __FUNCTION__, templateId); 1307 1308 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) { 1309 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110", 1310 IPCThreadState::self()->getCallingUid(), nullptr, 0); 1311 return BAD_VALUE; 1312 } 1313 1314 Mutex::Autolock il(mInterfaceLock); 1315 Mutex::Autolock l(mLock); 1316 1317 switch (mStatus) { 1318 case STATUS_ERROR: 1319 CLOGE("Device has encountered a serious error"); 1320 return INVALID_OPERATION; 1321 case STATUS_UNINITIALIZED: 1322 CLOGE("Device is not initialized!"); 1323 return INVALID_OPERATION; 1324 case STATUS_UNCONFIGURED: 1325 case STATUS_CONFIGURED: 1326 case STATUS_ACTIVE: 1327 // OK 1328 break; 1329 default: 1330 SET_ERR_L("Unexpected status: %d", mStatus); 1331 return INVALID_OPERATION; 1332 } 1333 1334 if (!mRequestTemplateCache[templateId].isEmpty()) { 1335 *request = mRequestTemplateCache[templateId]; 1336 return OK; 1337 } 1338 1339 const camera_metadata_t *rawRequest; 1340 ATRACE_BEGIN("camera3->construct_default_request_settings"); 1341 rawRequest = mHal3Device->ops->construct_default_request_settings( 1342 mHal3Device, templateId); 1343 ATRACE_END(); 1344 if (rawRequest == NULL) { 1345 ALOGI("%s: template %d is not supported on this camera device", 1346 __FUNCTION__, templateId); 1347 return BAD_VALUE; 1348 } 1349 1350 mRequestTemplateCache[templateId] = rawRequest; 1351 1352 // Derive some new keys for backward compatibility 1353 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists( 1354 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) { 1355 int32_t defaultBoost[1] = {100}; 1356 mRequestTemplateCache[templateId].update( 1357 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST, 1358 defaultBoost, 1); 1359 } 1360 1361 *request = mRequestTemplateCache[templateId]; 1362 return OK; 1363 } 1364 1365 status_t Camera3Device::waitUntilDrained() { 1366 ATRACE_CALL(); 1367 Mutex::Autolock il(mInterfaceLock); 1368 Mutex::Autolock l(mLock); 1369 1370 return waitUntilDrainedLocked(); 1371 } 1372 1373 status_t Camera3Device::waitUntilDrainedLocked() { 1374 switch (mStatus) { 1375 case STATUS_UNINITIALIZED: 1376 case STATUS_UNCONFIGURED: 1377 ALOGV("%s: Already idle", __FUNCTION__); 1378 return OK; 1379 case STATUS_CONFIGURED: 1380 // To avoid race conditions, check with tracker to be sure 1381 case STATUS_ERROR: 1382 case STATUS_ACTIVE: 1383 // Need to verify shut down 1384 break; 1385 default: 1386 SET_ERR_L("Unexpected status: %d",mStatus); 1387 return INVALID_OPERATION; 1388 } 1389 1390 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId); 1391 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); 1392 if (res != OK) { 1393 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res), 1394 res); 1395 } 1396 return res; 1397 } 1398 1399 1400 void Camera3Device::internalUpdateStatusLocked(Status status) { 1401 mStatus = status; 1402 mRecentStatusUpdates.add(mStatus); 1403 mStatusChanged.broadcast(); 1404 } 1405 1406 // Pause to reconfigure 1407 status_t Camera3Device::internalPauseAndWaitLocked() { 1408 mRequestThread->setPaused(true); 1409 mPauseStateNotify = true; 1410 1411 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId); 1412 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); 1413 if (res != OK) { 1414 SET_ERR_L("Can't idle device in %f seconds!", 1415 kShutdownTimeout/1e9); 1416 } 1417 1418 return res; 1419 } 1420 1421 // Resume after internalPauseAndWaitLocked 1422 status_t Camera3Device::internalResumeLocked() { 1423 status_t res; 1424 1425 mRequestThread->setPaused(false); 1426 1427 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); 1428 if (res != OK) { 1429 SET_ERR_L("Can't transition to active in %f seconds!", 1430 kActiveTimeout/1e9); 1431 } 1432 mPauseStateNotify = false; 1433 return OK; 1434 } 1435 1436 status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) { 1437 status_t res = OK; 1438 1439 size_t startIndex = 0; 1440 if (mStatusWaiters == 0) { 1441 // Clear the list of recent statuses if there are no existing threads waiting on updates to 1442 // this status list 1443 mRecentStatusUpdates.clear(); 1444 } else { 1445 // If other threads are waiting on updates to this status list, set the position of the 1446 // first element that this list will check rather than clearing the list. 1447 startIndex = mRecentStatusUpdates.size(); 1448 } 1449 1450 mStatusWaiters++; 1451 1452 bool stateSeen = false; 1453 do { 1454 if (active == (mStatus == STATUS_ACTIVE)) { 1455 // Desired state is current 1456 break; 1457 } 1458 1459 res = mStatusChanged.waitRelative(mLock, timeout); 1460 if (res != OK) break; 1461 1462 // This is impossible, but if not, could result in subtle deadlocks and invalid state 1463 // transitions. 1464 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(), 1465 "%s: Skipping status updates in Camera3Device, may result in deadlock.", 1466 __FUNCTION__); 1467 1468 // Encountered desired state since we began waiting 1469 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) { 1470 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) { 1471 stateSeen = true; 1472 break; 1473 } 1474 } 1475 } while (!stateSeen); 1476 1477 mStatusWaiters--; 1478 1479 return res; 1480 } 1481 1482 1483 status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) { 1484 ATRACE_CALL(); 1485 Mutex::Autolock l(mOutputLock); 1486 1487 if (listener != NULL && mListener != NULL) { 1488 ALOGW("%s: Replacing old callback listener", __FUNCTION__); 1489 } 1490 mListener = listener; 1491 mRequestThread->setNotificationListener(listener); 1492 mPreparerThread->setNotificationListener(listener); 1493 1494 return OK; 1495 } 1496 1497 bool Camera3Device::willNotify3A() { 1498 return false; 1499 } 1500 1501 status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { 1502 status_t res; 1503 Mutex::Autolock l(mOutputLock); 1504 1505 while (mResultQueue.empty()) { 1506 res = mResultSignal.waitRelative(mOutputLock, timeout); 1507 if (res == TIMED_OUT) { 1508 return res; 1509 } else if (res != OK) { 1510 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)", 1511 __FUNCTION__, mId, timeout, strerror(-res), res); 1512 return res; 1513 } 1514 } 1515 return OK; 1516 } 1517 1518 status_t Camera3Device::getNextResult(CaptureResult *frame) { 1519 ATRACE_CALL(); 1520 Mutex::Autolock l(mOutputLock); 1521 1522 if (mResultQueue.empty()) { 1523 return NOT_ENOUGH_DATA; 1524 } 1525 1526 if (frame == NULL) { 1527 ALOGE("%s: argument cannot be NULL", __FUNCTION__); 1528 return BAD_VALUE; 1529 } 1530 1531 CaptureResult &result = *(mResultQueue.begin()); 1532 frame->mResultExtras = result.mResultExtras; 1533 frame->mMetadata.acquire(result.mMetadata); 1534 mResultQueue.erase(mResultQueue.begin()); 1535 1536 return OK; 1537 } 1538 1539 status_t Camera3Device::triggerAutofocus(uint32_t id) { 1540 ATRACE_CALL(); 1541 Mutex::Autolock il(mInterfaceLock); 1542 1543 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); 1544 // Mix-in this trigger into the next request and only the next request. 1545 RequestTrigger trigger[] = { 1546 { 1547 ANDROID_CONTROL_AF_TRIGGER, 1548 ANDROID_CONTROL_AF_TRIGGER_START 1549 }, 1550 { 1551 ANDROID_CONTROL_AF_TRIGGER_ID, 1552 static_cast<int32_t>(id) 1553 } 1554 }; 1555 1556 return mRequestThread->queueTrigger(trigger, 1557 sizeof(trigger)/sizeof(trigger[0])); 1558 } 1559 1560 status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { 1561 ATRACE_CALL(); 1562 Mutex::Autolock il(mInterfaceLock); 1563 1564 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); 1565 // Mix-in this trigger into the next request and only the next request. 1566 RequestTrigger trigger[] = { 1567 { 1568 ANDROID_CONTROL_AF_TRIGGER, 1569 ANDROID_CONTROL_AF_TRIGGER_CANCEL 1570 }, 1571 { 1572 ANDROID_CONTROL_AF_TRIGGER_ID, 1573 static_cast<int32_t>(id) 1574 } 1575 }; 1576 1577 return mRequestThread->queueTrigger(trigger, 1578 sizeof(trigger)/sizeof(trigger[0])); 1579 } 1580 1581 status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { 1582 ATRACE_CALL(); 1583 Mutex::Autolock il(mInterfaceLock); 1584 1585 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); 1586 // Mix-in this trigger into the next request and only the next request. 1587 RequestTrigger trigger[] = { 1588 { 1589 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, 1590 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START 1591 }, 1592 { 1593 ANDROID_CONTROL_AE_PRECAPTURE_ID, 1594 static_cast<int32_t>(id) 1595 } 1596 }; 1597 1598 return mRequestThread->queueTrigger(trigger, 1599 sizeof(trigger)/sizeof(trigger[0])); 1600 } 1601 1602 status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId, 1603 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) { 1604 ATRACE_CALL(); 1605 (void)reprocessStreamId; (void)buffer; (void)listener; 1606 1607 CLOGE("Unimplemented"); 1608 return INVALID_OPERATION; 1609 } 1610 1611 status_t Camera3Device::flush(int64_t *frameNumber) { 1612 ATRACE_CALL(); 1613 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId); 1614 Mutex::Autolock il(mInterfaceLock); 1615 1616 { 1617 Mutex::Autolock l(mLock); 1618 mRequestThread->clear(/*out*/frameNumber); 1619 } 1620 1621 status_t res; 1622 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) { 1623 res = mRequestThread->flush(); 1624 } else { 1625 Mutex::Autolock l(mLock); 1626 res = waitUntilDrainedLocked(); 1627 } 1628 1629 return res; 1630 } 1631 1632 status_t Camera3Device::prepare(int streamId) { 1633 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId); 1634 } 1635 1636 status_t Camera3Device::prepare(int maxCount, int streamId) { 1637 ATRACE_CALL(); 1638 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId); 1639 Mutex::Autolock il(mInterfaceLock); 1640 Mutex::Autolock l(mLock); 1641 1642 sp<Camera3StreamInterface> stream; 1643 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); 1644 if (outputStreamIdx == NAME_NOT_FOUND) { 1645 CLOGE("Stream %d does not exist", streamId); 1646 return BAD_VALUE; 1647 } 1648 1649 stream = mOutputStreams.editValueAt(outputStreamIdx); 1650 1651 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) { 1652 CLOGE("Stream %d has already been a request target", streamId); 1653 return BAD_VALUE; 1654 } 1655 1656 if (mRequestThread->isStreamPending(stream)) { 1657 CLOGE("Stream %d is already a target in a pending request", streamId); 1658 return BAD_VALUE; 1659 } 1660 1661 return mPreparerThread->prepare(maxCount, stream); 1662 } 1663 1664 status_t Camera3Device::tearDown(int streamId) { 1665 ATRACE_CALL(); 1666 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId); 1667 Mutex::Autolock il(mInterfaceLock); 1668 Mutex::Autolock l(mLock); 1669 1670 // Teardown can only be accomplished on devices that don't require register_stream_buffers, 1671 // since we cannot call register_stream_buffers except right after configure_streams. 1672 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) { 1673 ALOGE("%s: Unable to tear down streams on device HAL v%x", 1674 __FUNCTION__, mHal3Device->common.version); 1675 return NO_INIT; 1676 } 1677 1678 sp<Camera3StreamInterface> stream; 1679 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); 1680 if (outputStreamIdx == NAME_NOT_FOUND) { 1681 CLOGE("Stream %d does not exist", streamId); 1682 return BAD_VALUE; 1683 } 1684 1685 stream = mOutputStreams.editValueAt(outputStreamIdx); 1686 1687 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) { 1688 CLOGE("Stream %d is a target of a in-progress request", streamId); 1689 return BAD_VALUE; 1690 } 1691 1692 return stream->tearDown(); 1693 } 1694 1695 status_t Camera3Device::addBufferListenerForStream(int streamId, 1696 wp<Camera3StreamBufferListener> listener) { 1697 ATRACE_CALL(); 1698 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId); 1699 Mutex::Autolock il(mInterfaceLock); 1700 Mutex::Autolock l(mLock); 1701 1702 sp<Camera3StreamInterface> stream; 1703 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); 1704 if (outputStreamIdx == NAME_NOT_FOUND) { 1705 CLOGE("Stream %d does not exist", streamId); 1706 return BAD_VALUE; 1707 } 1708 1709 stream = mOutputStreams.editValueAt(outputStreamIdx); 1710 stream->addBufferListener(listener); 1711 1712 return OK; 1713 } 1714 1715 uint32_t Camera3Device::getDeviceVersion() { 1716 ATRACE_CALL(); 1717 Mutex::Autolock il(mInterfaceLock); 1718 return mDeviceVersion; 1719 } 1720 1721 /** 1722 * Methods called by subclasses 1723 */ 1724 1725 void Camera3Device::notifyStatus(bool idle) { 1726 { 1727 // Need mLock to safely update state and synchronize to current 1728 // state of methods in flight. 1729 Mutex::Autolock l(mLock); 1730 // We can get various system-idle notices from the status tracker 1731 // while starting up. Only care about them if we've actually sent 1732 // in some requests recently. 1733 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) { 1734 return; 1735 } 1736 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId, 1737 idle ? "idle" : "active"); 1738 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE); 1739 1740 // Skip notifying listener if we're doing some user-transparent 1741 // state changes 1742 if (mPauseStateNotify) return; 1743 } 1744 1745 sp<NotificationListener> listener; 1746 { 1747 Mutex::Autolock l(mOutputLock); 1748 listener = mListener.promote(); 1749 } 1750 if (idle && listener != NULL) { 1751 listener->notifyIdle(); 1752 } 1753 } 1754 1755 status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) { 1756 ATRACE_CALL(); 1757 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId); 1758 Mutex::Autolock il(mInterfaceLock); 1759 Mutex::Autolock l(mLock); 1760 1761 if (consumer == nullptr) { 1762 CLOGE("Null consumer is passed!"); 1763 return BAD_VALUE; 1764 } 1765 1766 ssize_t idx = mOutputStreams.indexOfKey(streamId); 1767 if (idx == NAME_NOT_FOUND) { 1768 CLOGE("Stream %d is unknown", streamId); 1769 return idx; 1770 } 1771 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx]; 1772 status_t res = stream->setConsumer(consumer); 1773 if (res != OK) { 1774 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res)); 1775 return res; 1776 } 1777 1778 if (!stream->isConfiguring()) { 1779 CLOGE("Stream %d was already fully configured.", streamId); 1780 return INVALID_OPERATION; 1781 } 1782 1783 res = stream->finishConfiguration(mHal3Device); 1784 if (res != OK) { 1785 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", 1786 stream->getId(), strerror(-res), res); 1787 return res; 1788 } 1789 1790 return OK; 1791 } 1792 1793 /** 1794 * Camera3Device private methods 1795 */ 1796 1797 sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( 1798 const CameraMetadata &request) { 1799 ATRACE_CALL(); 1800 status_t res; 1801 1802 sp<CaptureRequest> newRequest = new CaptureRequest; 1803 newRequest->mSettings = request; 1804 1805 camera_metadata_entry_t inputStreams = 1806 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); 1807 if (inputStreams.count > 0) { 1808 if (mInputStream == NULL || 1809 mInputStream->getId() != inputStreams.data.i32[0]) { 1810 CLOGE("Request references unknown input stream %d", 1811 inputStreams.data.u8[0]); 1812 return NULL; 1813 } 1814 // Lazy completion of stream configuration (allocation/registration) 1815 // on first use 1816 if (mInputStream->isConfiguring()) { 1817 res = mInputStream->finishConfiguration(mHal3Device); 1818 if (res != OK) { 1819 SET_ERR_L("Unable to finish configuring input stream %d:" 1820 " %s (%d)", 1821 mInputStream->getId(), strerror(-res), res); 1822 return NULL; 1823 } 1824 } 1825 // Check if stream is being prepared 1826 if (mInputStream->isPreparing()) { 1827 CLOGE("Request references an input stream that's being prepared!"); 1828 return NULL; 1829 } 1830 1831 newRequest->mInputStream = mInputStream; 1832 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); 1833 } 1834 1835 camera_metadata_entry_t streams = 1836 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); 1837 if (streams.count == 0) { 1838 CLOGE("Zero output streams specified!"); 1839 return NULL; 1840 } 1841 1842 for (size_t i = 0; i < streams.count; i++) { 1843 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]); 1844 if (idx == NAME_NOT_FOUND) { 1845 CLOGE("Request references unknown stream %d", 1846 streams.data.u8[i]); 1847 return NULL; 1848 } 1849 sp<Camera3OutputStreamInterface> stream = 1850 mOutputStreams.editValueAt(idx); 1851 1852 // It is illegal to include a deferred consumer output stream into a request 1853 if (stream->isConsumerConfigurationDeferred()) { 1854 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer", 1855 stream->getId()); 1856 return NULL; 1857 } 1858 1859 // Lazy completion of stream configuration (allocation/registration) 1860 // on first use 1861 if (stream->isConfiguring()) { 1862 res = stream->finishConfiguration(mHal3Device); 1863 if (res != OK) { 1864 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", 1865 stream->getId(), strerror(-res), res); 1866 return NULL; 1867 } 1868 } 1869 // Check if stream is being prepared 1870 if (stream->isPreparing()) { 1871 CLOGE("Request references an output stream that's being prepared!"); 1872 return NULL; 1873 } 1874 1875 newRequest->mOutputStreams.push(stream); 1876 } 1877 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); 1878 newRequest->mBatchSize = 1; 1879 1880 return newRequest; 1881 } 1882 1883 bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) { 1884 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) { 1885 Size size = mSupportedOpaqueInputSizes[i]; 1886 if (size.width == width && size.height == height) { 1887 return true; 1888 } 1889 } 1890 1891 return false; 1892 } 1893 1894 void Camera3Device::cancelStreamsConfigurationLocked() { 1895 int res = OK; 1896 if (mInputStream != NULL && mInputStream->isConfiguring()) { 1897 res = mInputStream->cancelConfiguration(); 1898 if (res != OK) { 1899 CLOGE("Can't cancel configuring input stream %d: %s (%d)", 1900 mInputStream->getId(), strerror(-res), res); 1901 } 1902 } 1903 1904 for (size_t i = 0; i < mOutputStreams.size(); i++) { 1905 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i); 1906 if (outputStream->isConfiguring()) { 1907 res = outputStream->cancelConfiguration(); 1908 if (res != OK) { 1909 CLOGE("Can't cancel configuring output stream %d: %s (%d)", 1910 outputStream->getId(), strerror(-res), res); 1911 } 1912 } 1913 } 1914 1915 // Return state to that at start of call, so that future configures 1916 // properly clean things up 1917 internalUpdateStatusLocked(STATUS_UNCONFIGURED); 1918 mNeedConfig = true; 1919 } 1920 1921 status_t Camera3Device::configureStreamsLocked() { 1922 ATRACE_CALL(); 1923 status_t res; 1924 1925 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) { 1926 CLOGE("Not idle"); 1927 return INVALID_OPERATION; 1928 } 1929 1930 if (!mNeedConfig) { 1931 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); 1932 return OK; 1933 } 1934 1935 // Workaround for device HALv3.2 or older spec bug - zero streams requires 1936 // adding a dummy stream instead. 1937 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround. 1938 if (mOutputStreams.size() == 0) { 1939 addDummyStreamLocked(); 1940 } else { 1941 tryRemoveDummyStreamLocked(); 1942 } 1943 1944 // Start configuring the streams 1945 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId); 1946 1947 camera3_stream_configuration config; 1948 config.operation_mode = mIsConstrainedHighSpeedConfiguration ? 1949 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE : 1950 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE; 1951 config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); 1952 1953 Vector<camera3_stream_t*> streams; 1954 streams.setCapacity(config.num_streams); 1955 1956 if (mInputStream != NULL) { 1957 camera3_stream_t *inputStream; 1958 inputStream = mInputStream->startConfiguration(); 1959 if (inputStream == NULL) { 1960 CLOGE("Can't start input stream configuration"); 1961 cancelStreamsConfigurationLocked(); 1962 return INVALID_OPERATION; 1963 } 1964 streams.add(inputStream); 1965 } 1966 1967 for (size_t i = 0; i < mOutputStreams.size(); i++) { 1968 1969 // Don't configure bidi streams twice, nor add them twice to the list 1970 if (mOutputStreams[i].get() == 1971 static_cast<Camera3StreamInterface*>(mInputStream.get())) { 1972 1973 config.num_streams--; 1974 continue; 1975 } 1976 1977 camera3_stream_t *outputStream; 1978 outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); 1979 if (outputStream == NULL) { 1980 CLOGE("Can't start output stream configuration"); 1981 cancelStreamsConfigurationLocked(); 1982 return INVALID_OPERATION; 1983 } 1984 streams.add(outputStream); 1985 } 1986 1987 config.streams = streams.editArray(); 1988 1989 // Do the HAL configuration; will potentially touch stream 1990 // max_buffers, usage, priv fields. 1991 ATRACE_BEGIN("camera3->configure_streams"); 1992 res = mHal3Device->ops->configure_streams(mHal3Device, &config); 1993 ATRACE_END(); 1994 1995 if (res == BAD_VALUE) { 1996 // HAL rejected this set of streams as unsupported, clean up config 1997 // attempt and return to unconfigured state 1998 CLOGE("Set of requested inputs/outputs not supported by HAL"); 1999 cancelStreamsConfigurationLocked(); 2000 return BAD_VALUE; 2001 } else if (res != OK) { 2002 // Some other kind of error from configure_streams - this is not 2003 // expected 2004 SET_ERR_L("Unable to configure streams with HAL: %s (%d)", 2005 strerror(-res), res); 2006 return res; 2007 } 2008 2009 // Finish all stream configuration immediately. 2010 // TODO: Try to relax this later back to lazy completion, which should be 2011 // faster 2012 2013 if (mInputStream != NULL && mInputStream->isConfiguring()) { 2014 res = mInputStream->finishConfiguration(mHal3Device); 2015 if (res != OK) { 2016 CLOGE("Can't finish configuring input stream %d: %s (%d)", 2017 mInputStream->getId(), strerror(-res), res); 2018 cancelStreamsConfigurationLocked(); 2019 return BAD_VALUE; 2020 } 2021 } 2022 2023 for (size_t i = 0; i < mOutputStreams.size(); i++) { 2024 sp<Camera3OutputStreamInterface> outputStream = 2025 mOutputStreams.editValueAt(i); 2026 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) { 2027 res = outputStream->finishConfiguration(mHal3Device); 2028 if (res != OK) { 2029 CLOGE("Can't finish configuring output stream %d: %s (%d)", 2030 outputStream->getId(), strerror(-res), res); 2031 cancelStreamsConfigurationLocked(); 2032 return BAD_VALUE; 2033 } 2034 } 2035 } 2036 2037 // Request thread needs to know to avoid using repeat-last-settings protocol 2038 // across configure_streams() calls 2039 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration); 2040 2041 char value[PROPERTY_VALUE_MAX]; 2042 property_get("camera.fifo.disable", value, "0"); 2043 int32_t disableFifo = atoi(value); 2044 if (disableFifo != 1) { 2045 // Boost priority of request thread to SCHED_FIFO. 2046 pid_t requestThreadTid = mRequestThread->getTid(); 2047 res = requestPriority(getpid(), requestThreadTid, 2048 kRequestThreadPriority, /*asynchronous*/ false); 2049 if (res != OK) { 2050 ALOGW("Can't set realtime priority for request processing thread: %s (%d)", 2051 strerror(-res), res); 2052 } else { 2053 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid); 2054 } 2055 } 2056 2057 // Update device state 2058 2059 mNeedConfig = false; 2060 2061 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ? 2062 STATUS_CONFIGURED : STATUS_UNCONFIGURED); 2063 2064 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId); 2065 2066 // tear down the deleted streams after configure streams. 2067 mDeletedStreams.clear(); 2068 2069 return OK; 2070 } 2071 2072 status_t Camera3Device::addDummyStreamLocked() { 2073 ATRACE_CALL(); 2074 status_t res; 2075 2076 if (mDummyStreamId != NO_STREAM) { 2077 // Should never be adding a second dummy stream when one is already 2078 // active 2079 SET_ERR_L("%s: Camera %d: A dummy stream already exists!", 2080 __FUNCTION__, mId); 2081 return INVALID_OPERATION; 2082 } 2083 2084 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId); 2085 2086 sp<Camera3OutputStreamInterface> dummyStream = 2087 new Camera3DummyStream(mNextStreamId); 2088 2089 res = mOutputStreams.add(mNextStreamId, dummyStream); 2090 if (res < 0) { 2091 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res); 2092 return res; 2093 } 2094 2095 mDummyStreamId = mNextStreamId; 2096 mNextStreamId++; 2097 2098 return OK; 2099 } 2100 2101 status_t Camera3Device::tryRemoveDummyStreamLocked() { 2102 ATRACE_CALL(); 2103 status_t res; 2104 2105 if (mDummyStreamId == NO_STREAM) return OK; 2106 if (mOutputStreams.size() == 1) return OK; 2107 2108 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId); 2109 2110 // Ok, have a dummy stream and there's at least one other output stream, 2111 // so remove the dummy 2112 2113 sp<Camera3StreamInterface> deletedStream; 2114 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId); 2115 if (outputStreamIdx == NAME_NOT_FOUND) { 2116 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId); 2117 return INVALID_OPERATION; 2118 } 2119 2120 deletedStream = mOutputStreams.editValueAt(outputStreamIdx); 2121 mOutputStreams.removeItemsAt(outputStreamIdx); 2122 2123 // Free up the stream endpoint so that it can be used by some other stream 2124 res = deletedStream->disconnect(); 2125 if (res != OK) { 2126 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId); 2127 // fall through since we want to still list the stream as deleted. 2128 } 2129 mDeletedStreams.add(deletedStream); 2130 mDummyStreamId = NO_STREAM; 2131 2132 return res; 2133 } 2134 2135 void Camera3Device::setErrorState(const char *fmt, ...) { 2136 Mutex::Autolock l(mLock); 2137 va_list args; 2138 va_start(args, fmt); 2139 2140 setErrorStateLockedV(fmt, args); 2141 2142 va_end(args); 2143 } 2144 2145 void Camera3Device::setErrorStateV(const char *fmt, va_list args) { 2146 Mutex::Autolock l(mLock); 2147 setErrorStateLockedV(fmt, args); 2148 } 2149 2150 void Camera3Device::setErrorStateLocked(const char *fmt, ...) { 2151 va_list args; 2152 va_start(args, fmt); 2153 2154 setErrorStateLockedV(fmt, args); 2155 2156 va_end(args); 2157 } 2158 2159 void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { 2160 // Print out all error messages to log 2161 String8 errorCause = String8::formatV(fmt, args); 2162 ALOGE("Camera %d: %s", mId, errorCause.string()); 2163 2164 // But only do error state transition steps for the first error 2165 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; 2166 2167 mErrorCause = errorCause; 2168 2169 mRequestThread->setPaused(true); 2170 internalUpdateStatusLocked(STATUS_ERROR); 2171 2172 // Notify upstream about a device error 2173 sp<NotificationListener> listener = mListener.promote(); 2174 if (listener != NULL) { 2175 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, 2176 CaptureResultExtras()); 2177 } 2178 2179 // Save stack trace. View by dumping it later. 2180 CameraTraces::saveTrace(); 2181 // TODO: consider adding errorCause and client pid/procname 2182 } 2183 2184 /** 2185 * In-flight request management 2186 */ 2187 2188 status_t Camera3Device::registerInFlight(uint32_t frameNumber, 2189 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput, 2190 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { 2191 ATRACE_CALL(); 2192 Mutex::Autolock l(mInFlightLock); 2193 2194 ssize_t res; 2195 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput, 2196 aeTriggerCancelOverride)); 2197 if (res < 0) return res; 2198 2199 return OK; 2200 } 2201 2202 void Camera3Device::returnOutputBuffers( 2203 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers, 2204 nsecs_t timestamp) { 2205 for (size_t i = 0; i < numBuffers; i++) 2206 { 2207 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream); 2208 status_t res = stream->returnBuffer(outputBuffers[i], timestamp); 2209 // Note: stream may be deallocated at this point, if this buffer was 2210 // the last reference to it. 2211 if (res != OK) { 2212 ALOGE("Can't return buffer to its stream: %s (%d)", 2213 strerror(-res), res); 2214 } 2215 } 2216 } 2217 2218 2219 void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) { 2220 2221 const InFlightRequest &request = mInFlightMap.valueAt(idx); 2222 const uint32_t frameNumber = mInFlightMap.keyAt(idx); 2223 2224 nsecs_t sensorTimestamp = request.sensorTimestamp; 2225 nsecs_t shutterTimestamp = request.shutterTimestamp; 2226 2227 // Check if it's okay to remove the request from InFlightMap: 2228 // In the case of a successful request: 2229 // all input and output buffers, all result metadata, shutter callback 2230 // arrived. 2231 // In the case of a unsuccessful request: 2232 // all input and output buffers arrived. 2233 if (request.numBuffersLeft == 0 && 2234 (request.requestStatus != OK || 2235 (request.haveResultMetadata && shutterTimestamp != 0))) { 2236 ATRACE_ASYNC_END("frame capture", frameNumber); 2237 2238 // Sanity check - if sensor timestamp matches shutter timestamp 2239 if (request.requestStatus == OK && 2240 sensorTimestamp != shutterTimestamp) { 2241 SET_ERR("sensor timestamp (%" PRId64 2242 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", 2243 sensorTimestamp, frameNumber, shutterTimestamp); 2244 } 2245 2246 // for an unsuccessful request, it may have pending output buffers to 2247 // return. 2248 assert(request.requestStatus != OK || 2249 request.pendingOutputBuffers.size() == 0); 2250 returnOutputBuffers(request.pendingOutputBuffers.array(), 2251 request.pendingOutputBuffers.size(), 0); 2252 2253 mInFlightMap.removeItemsAt(idx, 1); 2254 2255 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); 2256 } 2257 2258 // Sanity check - if we have too many in-flight frames, something has 2259 // likely gone wrong 2260 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) { 2261 CLOGE("In-flight list too large: %zu", mInFlightMap.size()); 2262 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > 2263 kInFlightWarnLimitHighSpeed) { 2264 CLOGE("In-flight list too large for high speed configuration: %zu", 2265 mInFlightMap.size()); 2266 } 2267 } 2268 2269 void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber, 2270 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { 2271 if (result == nullptr) return; 2272 2273 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, 2274 (int32_t*)&frameNumber, 1) != OK) { 2275 SET_ERR("Failed to set frame number %d in metadata", frameNumber); 2276 return; 2277 } 2278 2279 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) { 2280 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber); 2281 return; 2282 } 2283 2284 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride); 2285 2286 // Valid result, insert into queue 2287 List<CaptureResult>::iterator queuedResult = 2288 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result)); 2289 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 2290 ", burstId = %" PRId32, __FUNCTION__, 2291 queuedResult->mResultExtras.requestId, 2292 queuedResult->mResultExtras.frameNumber, 2293 queuedResult->mResultExtras.burstId); 2294 2295 mResultSignal.signal(); 2296 } 2297 2298 2299 void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult, 2300 const CaptureResultExtras &resultExtras, uint32_t frameNumber, 2301 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { 2302 Mutex::Autolock l(mOutputLock); 2303 2304 CaptureResult captureResult; 2305 captureResult.mResultExtras = resultExtras; 2306 captureResult.mMetadata = partialResult; 2307 2308 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride); 2309 } 2310 2311 2312 void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata, 2313 CaptureResultExtras &resultExtras, 2314 CameraMetadata &collectedPartialResult, 2315 uint32_t frameNumber, 2316 bool reprocess, 2317 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { 2318 if (pendingMetadata.isEmpty()) 2319 return; 2320 2321 Mutex::Autolock l(mOutputLock); 2322 2323 // TODO: need to track errors for tighter bounds on expected frame number 2324 if (reprocess) { 2325 if (frameNumber < mNextReprocessResultFrameNumber) { 2326 SET_ERR("Out-of-order reprocess capture result metadata submitted! " 2327 "(got frame number %d, expecting %d)", 2328 frameNumber, mNextReprocessResultFrameNumber); 2329 return; 2330 } 2331 mNextReprocessResultFrameNumber = frameNumber + 1; 2332 } else { 2333 if (frameNumber < mNextResultFrameNumber) { 2334 SET_ERR("Out-of-order capture result metadata submitted! " 2335 "(got frame number %d, expecting %d)", 2336 frameNumber, mNextResultFrameNumber); 2337 return; 2338 } 2339 mNextResultFrameNumber = frameNumber + 1; 2340 } 2341 2342 CaptureResult captureResult; 2343 captureResult.mResultExtras = resultExtras; 2344 captureResult.mMetadata = pendingMetadata; 2345 2346 // Append any previous partials to form a complete result 2347 if (mUsePartialResult && !collectedPartialResult.isEmpty()) { 2348 captureResult.mMetadata.append(collectedPartialResult); 2349 } 2350 2351 // Derive some new keys for backward compaibility 2352 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists( 2353 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) { 2354 int32_t defaultBoost[1] = {100}; 2355 captureResult.mMetadata.update( 2356 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST, 2357 defaultBoost, 1); 2358 } 2359 2360 captureResult.mMetadata.sort(); 2361 2362 // Check that there's a timestamp in the result metadata 2363 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); 2364 if (timestamp.count == 0) { 2365 SET_ERR("No timestamp provided by HAL for frame %d!", 2366 frameNumber); 2367 return; 2368 } 2369 2370 mTagMonitor.monitorMetadata(TagMonitor::RESULT, 2371 frameNumber, timestamp.data.i64[0], captureResult.mMetadata); 2372 2373 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride); 2374 } 2375 2376 /** 2377 * Camera HAL device callback methods 2378 */ 2379 2380 void Camera3Device::processCaptureResult(const camera3_capture_result *result) { 2381 ATRACE_CALL(); 2382 2383 status_t res; 2384 2385 uint32_t frameNumber = result->frame_number; 2386 if (result->result == NULL && result->num_output_buffers == 0 && 2387 result->input_buffer == NULL) { 2388 SET_ERR("No result data provided by HAL for frame %d", 2389 frameNumber); 2390 return; 2391 } 2392 2393 // For HAL3.2 or above, If HAL doesn't support partial, it must always set 2394 // partial_result to 1 when metadata is included in this result. 2395 if (!mUsePartialResult && 2396 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 && 2397 result->result != NULL && 2398 result->partial_result != 1) { 2399 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" 2400 " if partial result is not supported", 2401 frameNumber, result->partial_result); 2402 return; 2403 } 2404 2405 bool isPartialResult = false; 2406 CameraMetadata collectedPartialResult; 2407 CaptureResultExtras resultExtras; 2408 bool hasInputBufferInRequest = false; 2409 2410 // Get shutter timestamp and resultExtras from list of in-flight requests, 2411 // where it was added by the shutter notification for this frame. If the 2412 // shutter timestamp isn't received yet, append the output buffers to the 2413 // in-flight request and they will be returned when the shutter timestamp 2414 // arrives. Update the in-flight status and remove the in-flight entry if 2415 // all result data and shutter timestamp have been received. 2416 nsecs_t shutterTimestamp = 0; 2417 2418 { 2419 Mutex::Autolock l(mInFlightLock); 2420 ssize_t idx = mInFlightMap.indexOfKey(frameNumber); 2421 if (idx == NAME_NOT_FOUND) { 2422 SET_ERR("Unknown frame number for capture result: %d", 2423 frameNumber); 2424 return; 2425 } 2426 InFlightRequest &request = mInFlightMap.editValueAt(idx); 2427 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 2428 ", frameNumber = %" PRId64 ", burstId = %" PRId32 2429 ", partialResultCount = %d", 2430 __FUNCTION__, request.resultExtras.requestId, 2431 request.resultExtras.frameNumber, request.resultExtras.burstId, 2432 result->partial_result); 2433 // Always update the partial count to the latest one if it's not 0 2434 // (buffers only). When framework aggregates adjacent partial results 2435 // into one, the latest partial count will be used. 2436 if (result->partial_result != 0) 2437 request.resultExtras.partialResultCount = result->partial_result; 2438 2439 // Check if this result carries only partial metadata 2440 if (mUsePartialResult && result->result != NULL) { 2441 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { 2442 if (result->partial_result > mNumPartialResults || result->partial_result < 1) { 2443 SET_ERR("Result is malformed for frame %d: partial_result %u must be in" 2444 " the range of [1, %d] when metadata is included in the result", 2445 frameNumber, result->partial_result, mNumPartialResults); 2446 return; 2447 } 2448 isPartialResult = (result->partial_result < mNumPartialResults); 2449 if (isPartialResult) { 2450 request.collectedPartialResult.append(result->result); 2451 } 2452 } else { 2453 camera_metadata_ro_entry_t partialResultEntry; 2454 res = find_camera_metadata_ro_entry(result->result, 2455 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry); 2456 if (res != NAME_NOT_FOUND && 2457 partialResultEntry.count > 0 && 2458 partialResultEntry.data.u8[0] == 2459 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) { 2460 // A partial result. Flag this as such, and collect this 2461 // set of metadata into the in-flight entry. 2462 isPartialResult = true; 2463 request.collectedPartialResult.append( 2464 result->result); 2465 request.collectedPartialResult.erase( 2466 ANDROID_QUIRKS_PARTIAL_RESULT); 2467 } 2468 } 2469 2470 if (isPartialResult) { 2471 // Send partial capture result 2472 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber, 2473 request.aeTriggerCancelOverride); 2474 } 2475 } 2476 2477 shutterTimestamp = request.shutterTimestamp; 2478 hasInputBufferInRequest = request.hasInputBuffer; 2479 2480 // Did we get the (final) result metadata for this capture? 2481 if (result->result != NULL && !isPartialResult) { 2482 if (request.haveResultMetadata) { 2483 SET_ERR("Called multiple times with metadata for frame %d", 2484 frameNumber); 2485 return; 2486 } 2487 if (mUsePartialResult && 2488 !request.collectedPartialResult.isEmpty()) { 2489 collectedPartialResult.acquire( 2490 request.collectedPartialResult); 2491 } 2492 request.haveResultMetadata = true; 2493 } 2494 2495 uint32_t numBuffersReturned = result->num_output_buffers; 2496 if (result->input_buffer != NULL) { 2497 if (hasInputBufferInRequest) { 2498 numBuffersReturned += 1; 2499 } else { 2500 ALOGW("%s: Input buffer should be NULL if there is no input" 2501 " buffer sent in the request", 2502 __FUNCTION__); 2503 } 2504 } 2505 request.numBuffersLeft -= numBuffersReturned; 2506 if (request.numBuffersLeft < 0) { 2507 SET_ERR("Too many buffers returned for frame %d", 2508 frameNumber); 2509 return; 2510 } 2511 2512 camera_metadata_ro_entry_t entry; 2513 res = find_camera_metadata_ro_entry(result->result, 2514 ANDROID_SENSOR_TIMESTAMP, &entry); 2515 if (res == OK && entry.count == 1) { 2516 request.sensorTimestamp = entry.data.i64[0]; 2517 } 2518 2519 // If shutter event isn't received yet, append the output buffers to 2520 // the in-flight request. Otherwise, return the output buffers to 2521 // streams. 2522 if (shutterTimestamp == 0) { 2523 request.pendingOutputBuffers.appendArray(result->output_buffers, 2524 result->num_output_buffers); 2525 } else { 2526 returnOutputBuffers(result->output_buffers, 2527 result->num_output_buffers, shutterTimestamp); 2528 } 2529 2530 if (result->result != NULL && !isPartialResult) { 2531 if (shutterTimestamp == 0) { 2532 request.pendingMetadata = result->result; 2533 request.collectedPartialResult = collectedPartialResult; 2534 } else { 2535 CameraMetadata metadata; 2536 metadata = result->result; 2537 sendCaptureResult(metadata, request.resultExtras, 2538 collectedPartialResult, frameNumber, hasInputBufferInRequest, 2539 request.aeTriggerCancelOverride); 2540 } 2541 } 2542 2543 removeInFlightRequestIfReadyLocked(idx); 2544 } // scope for mInFlightLock 2545 2546 if (result->input_buffer != NULL) { 2547 if (hasInputBufferInRequest) { 2548 Camera3Stream *stream = 2549 Camera3Stream::cast(result->input_buffer->stream); 2550 res = stream->returnInputBuffer(*(result->input_buffer)); 2551 // Note: stream may be deallocated at this point, if this buffer was the 2552 // last reference to it. 2553 if (res != OK) { 2554 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" 2555 " its stream:%s (%d)", __FUNCTION__, 2556 frameNumber, strerror(-res), res); 2557 } 2558 } else { 2559 ALOGW("%s: Input buffer should be NULL if there is no input" 2560 " buffer sent in the request, skipping input buffer return.", 2561 __FUNCTION__); 2562 } 2563 } 2564 } 2565 2566 void Camera3Device::notify(const camera3_notify_msg *msg) { 2567 ATRACE_CALL(); 2568 sp<NotificationListener> listener; 2569 { 2570 Mutex::Autolock l(mOutputLock); 2571 listener = mListener.promote(); 2572 } 2573 2574 if (msg == NULL) { 2575 SET_ERR("HAL sent NULL notify message!"); 2576 return; 2577 } 2578 2579 switch (msg->type) { 2580 case CAMERA3_MSG_ERROR: { 2581 notifyError(msg->message.error, listener); 2582 break; 2583 } 2584 case CAMERA3_MSG_SHUTTER: { 2585 notifyShutter(msg->message.shutter, listener); 2586 break; 2587 } 2588 default: 2589 SET_ERR("Unknown notify message from HAL: %d", 2590 msg->type); 2591 } 2592 } 2593 2594 void Camera3Device::notifyError(const camera3_error_msg_t &msg, 2595 sp<NotificationListener> listener) { 2596 2597 // Map camera HAL error codes to ICameraDeviceCallback error codes 2598 // Index into this with the HAL error code 2599 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = { 2600 // 0 = Unused error code 2601 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, 2602 // 1 = CAMERA3_MSG_ERROR_DEVICE 2603 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, 2604 // 2 = CAMERA3_MSG_ERROR_REQUEST 2605 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 2606 // 3 = CAMERA3_MSG_ERROR_RESULT 2607 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, 2608 // 4 = CAMERA3_MSG_ERROR_BUFFER 2609 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER 2610 }; 2611 2612 int32_t errorCode = 2613 ((msg.error_code >= 0) && 2614 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ? 2615 halErrorMap[msg.error_code] : 2616 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; 2617 2618 int streamId = 0; 2619 if (msg.error_stream != NULL) { 2620 Camera3Stream *stream = 2621 Camera3Stream::cast(msg.error_stream); 2622 streamId = stream->getId(); 2623 } 2624 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d", 2625 mId, __FUNCTION__, msg.frame_number, 2626 streamId, msg.error_code); 2627 2628 CaptureResultExtras resultExtras; 2629 switch (errorCode) { 2630 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: 2631 // SET_ERR calls notifyError 2632 SET_ERR("Camera HAL reported serious device error"); 2633 break; 2634 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: 2635 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: 2636 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: 2637 { 2638 Mutex::Autolock l(mInFlightLock); 2639 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number); 2640 if (idx >= 0) { 2641 InFlightRequest &r = mInFlightMap.editValueAt(idx); 2642 r.requestStatus = msg.error_code; 2643 resultExtras = r.resultExtras; 2644 } else { 2645 resultExtras.frameNumber = msg.frame_number; 2646 ALOGE("Camera %d: %s: cannot find in-flight request on " 2647 "frame %" PRId64 " error", mId, __FUNCTION__, 2648 resultExtras.frameNumber); 2649 } 2650 } 2651 resultExtras.errorStreamId = streamId; 2652 if (listener != NULL) { 2653 listener->notifyError(errorCode, resultExtras); 2654 } else { 2655 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__); 2656 } 2657 break; 2658 default: 2659 // SET_ERR calls notifyError 2660 SET_ERR("Unknown error message from HAL: %d", msg.error_code); 2661 break; 2662 } 2663 } 2664 2665 void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg, 2666 sp<NotificationListener> listener) { 2667 ssize_t idx; 2668 2669 // Set timestamp for the request in the in-flight tracking 2670 // and get the request ID to send upstream 2671 { 2672 Mutex::Autolock l(mInFlightLock); 2673 idx = mInFlightMap.indexOfKey(msg.frame_number); 2674 if (idx >= 0) { 2675 InFlightRequest &r = mInFlightMap.editValueAt(idx); 2676 2677 // Verify ordering of shutter notifications 2678 { 2679 Mutex::Autolock l(mOutputLock); 2680 // TODO: need to track errors for tighter bounds on expected frame number. 2681 if (r.hasInputBuffer) { 2682 if (msg.frame_number < mNextReprocessShutterFrameNumber) { 2683 SET_ERR("Shutter notification out-of-order. Expected " 2684 "notification for frame %d, got frame %d", 2685 mNextReprocessShutterFrameNumber, msg.frame_number); 2686 return; 2687 } 2688 mNextReprocessShutterFrameNumber = msg.frame_number + 1; 2689 } else { 2690 if (msg.frame_number < mNextShutterFrameNumber) { 2691 SET_ERR("Shutter notification out-of-order. Expected " 2692 "notification for frame %d, got frame %d", 2693 mNextShutterFrameNumber, msg.frame_number); 2694 return; 2695 } 2696 mNextShutterFrameNumber = msg.frame_number + 1; 2697 } 2698 } 2699 2700 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64, 2701 mId, __FUNCTION__, 2702 msg.frame_number, r.resultExtras.requestId, msg.timestamp); 2703 // Call listener, if any 2704 if (listener != NULL) { 2705 listener->notifyShutter(r.resultExtras, msg.timestamp); 2706 } 2707 2708 r.shutterTimestamp = msg.timestamp; 2709 2710 // send pending result and buffers 2711 sendCaptureResult(r.pendingMetadata, r.resultExtras, 2712 r.collectedPartialResult, msg.frame_number, 2713 r.hasInputBuffer, r.aeTriggerCancelOverride); 2714 returnOutputBuffers(r.pendingOutputBuffers.array(), 2715 r.pendingOutputBuffers.size(), r.shutterTimestamp); 2716 r.pendingOutputBuffers.clear(); 2717 2718 removeInFlightRequestIfReadyLocked(idx); 2719 } 2720 } 2721 if (idx < 0) { 2722 SET_ERR("Shutter notification for non-existent frame number %d", 2723 msg.frame_number); 2724 } 2725 } 2726 2727 2728 CameraMetadata Camera3Device::getLatestRequestLocked() { 2729 ALOGV("%s", __FUNCTION__); 2730 2731 CameraMetadata retVal; 2732 2733 if (mRequestThread != NULL) { 2734 retVal = mRequestThread->getLatestRequest(); 2735 } 2736 2737 return retVal; 2738 } 2739 2740 2741 void Camera3Device::monitorMetadata(TagMonitor::eventSource source, 2742 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) { 2743 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata); 2744 } 2745 2746 /** 2747 * RequestThread inner class methods 2748 */ 2749 2750 Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, 2751 sp<StatusTracker> statusTracker, 2752 camera3_device_t *hal3Device, 2753 bool aeLockAvailable) : 2754 Thread(/*canCallJava*/false), 2755 mParent(parent), 2756 mStatusTracker(statusTracker), 2757 mHal3Device(hal3Device), 2758 mListener(nullptr), 2759 mId(getId(parent)), 2760 mReconfigured(false), 2761 mDoPause(false), 2762 mPaused(true), 2763 mFrameNumber(0), 2764 mLatestRequestId(NAME_NOT_FOUND), 2765 mCurrentAfTriggerId(0), 2766 mCurrentPreCaptureTriggerId(0), 2767 mRepeatingLastFrameNumber( 2768 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES), 2769 mAeLockAvailable(aeLockAvailable), 2770 mPrepareVideoStream(false) { 2771 mStatusId = statusTracker->addComponent(); 2772 } 2773 2774 void Camera3Device::RequestThread::setNotificationListener( 2775 wp<NotificationListener> listener) { 2776 Mutex::Autolock l(mRequestLock); 2777 mListener = listener; 2778 } 2779 2780 void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) { 2781 Mutex::Autolock l(mRequestLock); 2782 mReconfigured = true; 2783 // Prepare video stream for high speed recording. 2784 mPrepareVideoStream = isConstrainedHighSpeed; 2785 } 2786 2787 status_t Camera3Device::RequestThread::queueRequestList( 2788 List<sp<CaptureRequest> > &requests, 2789 /*out*/ 2790 int64_t *lastFrameNumber) { 2791 Mutex::Autolock l(mRequestLock); 2792 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); 2793 ++it) { 2794 mRequestQueue.push_back(*it); 2795 } 2796 2797 if (lastFrameNumber != NULL) { 2798 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1; 2799 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".", 2800 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber, 2801 *lastFrameNumber); 2802 } 2803 2804 unpauseForNewRequests(); 2805 2806 return OK; 2807 } 2808 2809 2810 status_t Camera3Device::RequestThread::queueTrigger( 2811 RequestTrigger trigger[], 2812 size_t count) { 2813 2814 Mutex::Autolock l(mTriggerMutex); 2815 status_t ret; 2816 2817 for (size_t i = 0; i < count; ++i) { 2818 ret = queueTriggerLocked(trigger[i]); 2819 2820 if (ret != OK) { 2821 return ret; 2822 } 2823 } 2824 2825 return OK; 2826 } 2827 2828 int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { 2829 sp<Camera3Device> d = device.promote(); 2830 if (d != NULL) return d->mId; 2831 return 0; 2832 } 2833 2834 status_t Camera3Device::RequestThread::queueTriggerLocked( 2835 RequestTrigger trigger) { 2836 2837 uint32_t tag = trigger.metadataTag; 2838 ssize_t index = mTriggerMap.indexOfKey(tag); 2839 2840 switch (trigger.getTagType()) { 2841 case TYPE_BYTE: 2842 // fall-through 2843 case TYPE_INT32: 2844 break; 2845 default: 2846 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, 2847 trigger.getTagType()); 2848 return INVALID_OPERATION; 2849 } 2850 2851 /** 2852 * Collect only the latest trigger, since we only have 1 field 2853 * in the request settings per trigger tag, and can't send more than 1 2854 * trigger per request. 2855 */ 2856 if (index != NAME_NOT_FOUND) { 2857 mTriggerMap.editValueAt(index) = trigger; 2858 } else { 2859 mTriggerMap.add(tag, trigger); 2860 } 2861 2862 return OK; 2863 } 2864 2865 status_t Camera3Device::RequestThread::setRepeatingRequests( 2866 const RequestList &requests, 2867 /*out*/ 2868 int64_t *lastFrameNumber) { 2869 Mutex::Autolock l(mRequestLock); 2870 if (lastFrameNumber != NULL) { 2871 *lastFrameNumber = mRepeatingLastFrameNumber; 2872 } 2873 mRepeatingRequests.clear(); 2874 mRepeatingRequests.insert(mRepeatingRequests.begin(), 2875 requests.begin(), requests.end()); 2876 2877 unpauseForNewRequests(); 2878 2879 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; 2880 return OK; 2881 } 2882 2883 bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) { 2884 if (mRepeatingRequests.empty()) { 2885 return false; 2886 } 2887 int32_t requestId = requestIn->mResultExtras.requestId; 2888 const RequestList &repeatRequests = mRepeatingRequests; 2889 // All repeating requests are guaranteed to have same id so only check first quest 2890 const sp<CaptureRequest> firstRequest = *repeatRequests.begin(); 2891 return (firstRequest->mResultExtras.requestId == requestId); 2892 } 2893 2894 status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) { 2895 Mutex::Autolock l(mRequestLock); 2896 return clearRepeatingRequestsLocked(lastFrameNumber); 2897 2898 } 2899 2900 status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) { 2901 mRepeatingRequests.clear(); 2902 if (lastFrameNumber != NULL) { 2903 *lastFrameNumber = mRepeatingLastFrameNumber; 2904 } 2905 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; 2906 return OK; 2907 } 2908 2909 status_t Camera3Device::RequestThread::clear( 2910 /*out*/int64_t *lastFrameNumber) { 2911 Mutex::Autolock l(mRequestLock); 2912 ALOGV("RequestThread::%s:", __FUNCTION__); 2913 2914 mRepeatingRequests.clear(); 2915 2916 // Send errors for all requests pending in the request queue, including 2917 // pending repeating requests 2918 sp<NotificationListener> listener = mListener.promote(); 2919 if (listener != NULL) { 2920 for (RequestList::iterator it = mRequestQueue.begin(); 2921 it != mRequestQueue.end(); ++it) { 2922 // Abort the input buffers for reprocess requests. 2923 if ((*it)->mInputStream != NULL) { 2924 camera3_stream_buffer_t inputBuffer; 2925 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer); 2926 if (res != OK) { 2927 ALOGW("%s: %d: couldn't get input buffer while clearing the request " 2928 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); 2929 } else { 2930 res = (*it)->mInputStream->returnInputBuffer(inputBuffer); 2931 if (res != OK) { 2932 ALOGE("%s: %d: couldn't return input buffer while clearing the request " 2933 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); 2934 } 2935 } 2936 } 2937 // Set the frame number this request would have had, if it 2938 // had been submitted; this frame number will not be reused. 2939 // The requestId and burstId fields were set when the request was 2940 // submitted originally (in convertMetadataListToRequestListLocked) 2941 (*it)->mResultExtras.frameNumber = mFrameNumber++; 2942 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 2943 (*it)->mResultExtras); 2944 } 2945 } 2946 mRequestQueue.clear(); 2947 mTriggerMap.clear(); 2948 if (lastFrameNumber != NULL) { 2949 *lastFrameNumber = mRepeatingLastFrameNumber; 2950 } 2951 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; 2952 return OK; 2953 } 2954 2955 status_t Camera3Device::RequestThread::flush() { 2956 ATRACE_CALL(); 2957 Mutex::Autolock l(mFlushLock); 2958 2959 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) { 2960 return mHal3Device->ops->flush(mHal3Device); 2961 } 2962 2963 return -ENOTSUP; 2964 } 2965 2966 void Camera3Device::RequestThread::setPaused(bool paused) { 2967 Mutex::Autolock l(mPauseLock); 2968 mDoPause = paused; 2969 mDoPauseSignal.signal(); 2970 } 2971 2972 status_t Camera3Device::RequestThread::waitUntilRequestProcessed( 2973 int32_t requestId, nsecs_t timeout) { 2974 Mutex::Autolock l(mLatestRequestMutex); 2975 status_t res; 2976 while (mLatestRequestId != requestId) { 2977 nsecs_t startTime = systemTime(); 2978 2979 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); 2980 if (res != OK) return res; 2981 2982 timeout -= (systemTime() - startTime); 2983 } 2984 2985 return OK; 2986 } 2987 2988 void Camera3Device::RequestThread::requestExit() { 2989 // Call parent to set up shutdown 2990 Thread::requestExit(); 2991 // The exit from any possible waits 2992 mDoPauseSignal.signal(); 2993 mRequestSignal.signal(); 2994 } 2995 2996 2997 /** 2998 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so 2999 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF 3000 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides 3001 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the 3002 * request. 3003 */ 3004 void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) { 3005 request->mAeTriggerCancelOverride.applyAeLock = false; 3006 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false; 3007 3008 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) { 3009 return; 3010 } 3011 3012 camera_metadata_entry_t aePrecaptureTrigger = 3013 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); 3014 if (aePrecaptureTrigger.count > 0 && 3015 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) { 3016 // Always override CANCEL to IDLE 3017 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; 3018 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1); 3019 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true; 3020 request->mAeTriggerCancelOverride.aePrecaptureTrigger = 3021 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL; 3022 3023 if (mAeLockAvailable == true) { 3024 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK); 3025 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) { 3026 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON; 3027 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1); 3028 request->mAeTriggerCancelOverride.applyAeLock = true; 3029 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF; 3030 } 3031 } 3032 } 3033 } 3034 3035 /** 3036 * Override result metadata for cancelling AE precapture trigger applied in 3037 * handleAePrecaptureCancelRequest(). 3038 */ 3039 void Camera3Device::overrideResultForPrecaptureCancel( 3040 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { 3041 if (aeTriggerCancelOverride.applyAeLock) { 3042 // Only devices <= v3.2 should have this override 3043 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2); 3044 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1); 3045 } 3046 3047 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) { 3048 // Only devices <= v3.2 should have this override 3049 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2); 3050 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, 3051 &aeTriggerCancelOverride.aePrecaptureTrigger, 1); 3052 } 3053 } 3054 3055 void Camera3Device::RequestThread::checkAndStopRepeatingRequest() { 3056 bool surfaceAbandoned = false; 3057 int64_t lastFrameNumber = 0; 3058 sp<NotificationListener> listener; 3059 { 3060 Mutex::Autolock l(mRequestLock); 3061 // Check all streams needed by repeating requests are still valid. Otherwise, stop 3062 // repeating requests. 3063 for (const auto& request : mRepeatingRequests) { 3064 for (const auto& s : request->mOutputStreams) { 3065 if (s->isAbandoned()) { 3066 surfaceAbandoned = true; 3067 clearRepeatingRequestsLocked(&lastFrameNumber); 3068 break; 3069 } 3070 } 3071 if (surfaceAbandoned) { 3072 break; 3073 } 3074 } 3075 listener = mListener.promote(); 3076 } 3077 3078 if (listener != NULL && surfaceAbandoned) { 3079 listener->notifyRepeatingRequestError(lastFrameNumber); 3080 } 3081 } 3082 3083 bool Camera3Device::RequestThread::threadLoop() { 3084 ATRACE_CALL(); 3085 status_t res; 3086 3087 // Handle paused state. 3088 if (waitIfPaused()) { 3089 return true; 3090 } 3091 3092 // Wait for the next batch of requests. 3093 waitForNextRequestBatch(); 3094 if (mNextRequests.size() == 0) { 3095 return true; 3096 } 3097 3098 // Get the latest request ID, if any 3099 int latestRequestId; 3100 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1]. 3101 captureRequest->mSettings.find(ANDROID_REQUEST_ID); 3102 if (requestIdEntry.count > 0) { 3103 latestRequestId = requestIdEntry.data.i32[0]; 3104 } else { 3105 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__); 3106 latestRequestId = NAME_NOT_FOUND; 3107 } 3108 3109 // Prepare a batch of HAL requests and output buffers. 3110 res = prepareHalRequests(); 3111 if (res == TIMED_OUT) { 3112 // Not a fatal error if getting output buffers time out. 3113 cleanUpFailedRequests(/*sendRequestError*/ true); 3114 // Check if any stream is abandoned. 3115 checkAndStopRepeatingRequest(); 3116 return true; 3117 } else if (res != OK) { 3118 cleanUpFailedRequests(/*sendRequestError*/ false); 3119 return false; 3120 } 3121 3122 // Inform waitUntilRequestProcessed thread of a new request ID 3123 { 3124 Mutex::Autolock al(mLatestRequestMutex); 3125 3126 mLatestRequestId = latestRequestId; 3127 mLatestRequestSignal.signal(); 3128 } 3129 3130 // Submit a batch of requests to HAL. 3131 // Use flush lock only when submitting multilple requests in a batch. 3132 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request() 3133 // which may take a long time to finish so synchronizing flush() and 3134 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush(). 3135 // For now, only synchronize for high speed recording and we should figure something out for 3136 // removing the synchronization. 3137 bool useFlushLock = mNextRequests.size() > 1; 3138 3139 if (useFlushLock) { 3140 mFlushLock.lock(); 3141 } 3142 3143 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__, 3144 mNextRequests.size()); 3145 for (auto& nextRequest : mNextRequests) { 3146 // Submit request and block until ready for next one 3147 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number); 3148 ATRACE_BEGIN("camera3->process_capture_request"); 3149 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest); 3150 ATRACE_END(); 3151 3152 if (res != OK) { 3153 // Should only get a failure here for malformed requests or device-level 3154 // errors, so consider all errors fatal. Bad metadata failures should 3155 // come through notify. 3156 SET_ERR("RequestThread: Unable to submit capture request %d to HAL" 3157 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res), 3158 res); 3159 cleanUpFailedRequests(/*sendRequestError*/ false); 3160 if (useFlushLock) { 3161 mFlushLock.unlock(); 3162 } 3163 return false; 3164 } 3165 3166 // Mark that the request has be submitted successfully. 3167 nextRequest.submitted = true; 3168 3169 // Update the latest request sent to HAL 3170 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged 3171 Mutex::Autolock al(mLatestRequestMutex); 3172 3173 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings); 3174 mLatestRequest.acquire(cloned); 3175 3176 sp<Camera3Device> parent = mParent.promote(); 3177 if (parent != NULL) { 3178 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number, 3179 0, mLatestRequest); 3180 } 3181 } 3182 3183 if (nextRequest.halRequest.settings != NULL) { 3184 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings); 3185 } 3186 3187 // Remove any previously queued triggers (after unlock) 3188 res = removeTriggers(mPrevRequest); 3189 if (res != OK) { 3190 SET_ERR("RequestThread: Unable to remove triggers " 3191 "(capture request %d, HAL device: %s (%d)", 3192 nextRequest.halRequest.frame_number, strerror(-res), res); 3193 cleanUpFailedRequests(/*sendRequestError*/ false); 3194 if (useFlushLock) { 3195 mFlushLock.unlock(); 3196 } 3197 return false; 3198 } 3199 } 3200 3201 if (useFlushLock) { 3202 mFlushLock.unlock(); 3203 } 3204 3205 // Unset as current request 3206 { 3207 Mutex::Autolock l(mRequestLock); 3208 mNextRequests.clear(); 3209 } 3210 3211 return true; 3212 } 3213 3214 status_t Camera3Device::RequestThread::prepareHalRequests() { 3215 ATRACE_CALL(); 3216 3217 for (auto& nextRequest : mNextRequests) { 3218 sp<CaptureRequest> captureRequest = nextRequest.captureRequest; 3219 camera3_capture_request_t* halRequest = &nextRequest.halRequest; 3220 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers; 3221 3222 // Prepare a request to HAL 3223 halRequest->frame_number = captureRequest->mResultExtras.frameNumber; 3224 3225 // Insert any queued triggers (before metadata is locked) 3226 status_t res = insertTriggers(captureRequest); 3227 3228 if (res < 0) { 3229 SET_ERR("RequestThread: Unable to insert triggers " 3230 "(capture request %d, HAL device: %s (%d)", 3231 halRequest->frame_number, strerror(-res), res); 3232 return INVALID_OPERATION; 3233 } 3234 int triggerCount = res; 3235 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); 3236 mPrevTriggers = triggerCount; 3237 3238 // If the request is the same as last, or we had triggers last time 3239 if (mPrevRequest != captureRequest || triggersMixedIn) { 3240 /** 3241 * HAL workaround: 3242 * Insert a dummy trigger ID if a trigger is set but no trigger ID is 3243 */ 3244 res = addDummyTriggerIds(captureRequest); 3245 if (res != OK) { 3246 SET_ERR("RequestThread: Unable to insert dummy trigger IDs " 3247 "(capture request %d, HAL device: %s (%d)", 3248 halRequest->frame_number, strerror(-res), res); 3249 return INVALID_OPERATION; 3250 } 3251 3252 /** 3253 * The request should be presorted so accesses in HAL 3254 * are O(logn). Sidenote, sorting a sorted metadata is nop. 3255 */ 3256 captureRequest->mSettings.sort(); 3257 halRequest->settings = captureRequest->mSettings.getAndLock(); 3258 mPrevRequest = captureRequest; 3259 ALOGVV("%s: Request settings are NEW", __FUNCTION__); 3260 3261 IF_ALOGV() { 3262 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); 3263 find_camera_metadata_ro_entry( 3264 halRequest->settings, 3265 ANDROID_CONTROL_AF_TRIGGER, 3266 &e 3267 ); 3268 if (e.count > 0) { 3269 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", 3270 __FUNCTION__, 3271 halRequest->frame_number, 3272 e.data.u8[0]); 3273 } 3274 } 3275 } else { 3276 // leave request.settings NULL to indicate 'reuse latest given' 3277 ALOGVV("%s: Request settings are REUSED", 3278 __FUNCTION__); 3279 } 3280 3281 uint32_t totalNumBuffers = 0; 3282 3283 // Fill in buffers 3284 if (captureRequest->mInputStream != NULL) { 3285 halRequest->input_buffer = &captureRequest->mInputBuffer; 3286 totalNumBuffers += 1; 3287 } else { 3288 halRequest->input_buffer = NULL; 3289 } 3290 3291 outputBuffers->insertAt(camera3_stream_buffer_t(), 0, 3292 captureRequest->mOutputStreams.size()); 3293 halRequest->output_buffers = outputBuffers->array(); 3294 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) { 3295 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i); 3296 3297 // Prepare video buffers for high speed recording on the first video request. 3298 if (mPrepareVideoStream && outputStream->isVideoStream()) { 3299 // Only try to prepare video stream on the first video request. 3300 mPrepareVideoStream = false; 3301 3302 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX); 3303 while (res == NOT_ENOUGH_DATA) { 3304 res = outputStream->prepareNextBuffer(); 3305 } 3306 if (res != OK) { 3307 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)", 3308 __FUNCTION__, strerror(-res), res); 3309 outputStream->cancelPrepare(); 3310 } 3311 } 3312 3313 res = outputStream->getBuffer(&outputBuffers->editItemAt(i)); 3314 if (res != OK) { 3315 // Can't get output buffer from gralloc queue - this could be due to 3316 // abandoned queue or other consumer misbehavior, so not a fatal 3317 // error 3318 ALOGE("RequestThread: Can't get output buffer, skipping request:" 3319 " %s (%d)", strerror(-res), res); 3320 3321 return TIMED_OUT; 3322 } 3323 halRequest->num_output_buffers++; 3324 } 3325 totalNumBuffers += halRequest->num_output_buffers; 3326 3327 // Log request in the in-flight queue 3328 sp<Camera3Device> parent = mParent.promote(); 3329 if (parent == NULL) { 3330 // Should not happen, and nowhere to send errors to, so just log it 3331 CLOGE("RequestThread: Parent is gone"); 3332 return INVALID_OPERATION; 3333 } 3334 res = parent->registerInFlight(halRequest->frame_number, 3335 totalNumBuffers, captureRequest->mResultExtras, 3336 /*hasInput*/halRequest->input_buffer != NULL, 3337 captureRequest->mAeTriggerCancelOverride); 3338 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64 3339 ", burstId = %" PRId32 ".", 3340 __FUNCTION__, 3341 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber, 3342 captureRequest->mResultExtras.burstId); 3343 if (res != OK) { 3344 SET_ERR("RequestThread: Unable to register new in-flight request:" 3345 " %s (%d)", strerror(-res), res); 3346 return INVALID_OPERATION; 3347 } 3348 } 3349 3350 return OK; 3351 } 3352 3353 CameraMetadata Camera3Device::RequestThread::getLatestRequest() const { 3354 Mutex::Autolock al(mLatestRequestMutex); 3355 3356 ALOGV("RequestThread::%s", __FUNCTION__); 3357 3358 return mLatestRequest; 3359 } 3360 3361 bool Camera3Device::RequestThread::isStreamPending( 3362 sp<Camera3StreamInterface>& stream) { 3363 Mutex::Autolock l(mRequestLock); 3364 3365 for (const auto& nextRequest : mNextRequests) { 3366 if (!nextRequest.submitted) { 3367 for (const auto& s : nextRequest.captureRequest->mOutputStreams) { 3368 if (stream == s) return true; 3369 } 3370 if (stream == nextRequest.captureRequest->mInputStream) return true; 3371 } 3372 } 3373 3374 for (const auto& request : mRequestQueue) { 3375 for (const auto& s : request->mOutputStreams) { 3376 if (stream == s) return true; 3377 } 3378 if (stream == request->mInputStream) return true; 3379 } 3380 3381 for (const auto& request : mRepeatingRequests) { 3382 for (const auto& s : request->mOutputStreams) { 3383 if (stream == s) return true; 3384 } 3385 if (stream == request->mInputStream) return true; 3386 } 3387 3388 return false; 3389 } 3390 3391 void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) { 3392 if (mNextRequests.empty()) { 3393 return; 3394 } 3395 3396 for (auto& nextRequest : mNextRequests) { 3397 // Skip the ones that have been submitted successfully. 3398 if (nextRequest.submitted) { 3399 continue; 3400 } 3401 3402 sp<CaptureRequest> captureRequest = nextRequest.captureRequest; 3403 camera3_capture_request_t* halRequest = &nextRequest.halRequest; 3404 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers; 3405 3406 if (halRequest->settings != NULL) { 3407 captureRequest->mSettings.unlock(halRequest->settings); 3408 } 3409 3410 if (captureRequest->mInputStream != NULL) { 3411 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; 3412 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer); 3413 } 3414 3415 for (size_t i = 0; i < halRequest->num_output_buffers; i++) { 3416 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; 3417 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0); 3418 } 3419 3420 if (sendRequestError) { 3421 Mutex::Autolock l(mRequestLock); 3422 sp<NotificationListener> listener = mListener.promote(); 3423 if (listener != NULL) { 3424 listener->notifyError( 3425 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 3426 captureRequest->mResultExtras); 3427 } 3428 } 3429 } 3430 3431 Mutex::Autolock l(mRequestLock); 3432 mNextRequests.clear(); 3433 } 3434 3435 void Camera3Device::RequestThread::waitForNextRequestBatch() { 3436 // Optimized a bit for the simple steady-state case (single repeating 3437 // request), to avoid putting that request in the queue temporarily. 3438 Mutex::Autolock l(mRequestLock); 3439 3440 assert(mNextRequests.empty()); 3441 3442 NextRequest nextRequest; 3443 nextRequest.captureRequest = waitForNextRequestLocked(); 3444 if (nextRequest.captureRequest == nullptr) { 3445 return; 3446 } 3447 3448 nextRequest.halRequest = camera3_capture_request_t(); 3449 nextRequest.submitted = false; 3450 mNextRequests.add(nextRequest); 3451 3452 // Wait for additional requests 3453 const size_t batchSize = nextRequest.captureRequest->mBatchSize; 3454 3455 for (size_t i = 1; i < batchSize; i++) { 3456 NextRequest additionalRequest; 3457 additionalRequest.captureRequest = waitForNextRequestLocked(); 3458 if (additionalRequest.captureRequest == nullptr) { 3459 break; 3460 } 3461 3462 additionalRequest.halRequest = camera3_capture_request_t(); 3463 additionalRequest.submitted = false; 3464 mNextRequests.add(additionalRequest); 3465 } 3466 3467 if (mNextRequests.size() < batchSize) { 3468 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.", 3469 mNextRequests.size(), batchSize); 3470 cleanUpFailedRequests(/*sendRequestError*/true); 3471 } 3472 3473 return; 3474 } 3475 3476 sp<Camera3Device::CaptureRequest> 3477 Camera3Device::RequestThread::waitForNextRequestLocked() { 3478 status_t res; 3479 sp<CaptureRequest> nextRequest; 3480 3481 while (mRequestQueue.empty()) { 3482 if (!mRepeatingRequests.empty()) { 3483 // Always atomically enqueue all requests in a repeating request 3484 // list. Guarantees a complete in-sequence set of captures to 3485 // application. 3486 const RequestList &requests = mRepeatingRequests; 3487 RequestList::const_iterator firstRequest = 3488 requests.begin(); 3489 nextRequest = *firstRequest; 3490 mRequestQueue.insert(mRequestQueue.end(), 3491 ++firstRequest, 3492 requests.end()); 3493 // No need to wait any longer 3494 3495 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1; 3496 3497 break; 3498 } 3499 3500 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); 3501 3502 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) || 3503 exitPending()) { 3504 Mutex::Autolock pl(mPauseLock); 3505 if (mPaused == false) { 3506 ALOGV("%s: RequestThread: Going idle", __FUNCTION__); 3507 mPaused = true; 3508 // Let the tracker know 3509 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 3510 if (statusTracker != 0) { 3511 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); 3512 } 3513 } 3514 // Stop waiting for now and let thread management happen 3515 return NULL; 3516 } 3517 } 3518 3519 if (nextRequest == NULL) { 3520 // Don't have a repeating request already in hand, so queue 3521 // must have an entry now. 3522 RequestList::iterator firstRequest = 3523 mRequestQueue.begin(); 3524 nextRequest = *firstRequest; 3525 mRequestQueue.erase(firstRequest); 3526 } 3527 3528 // In case we've been unpaused by setPaused clearing mDoPause, need to 3529 // update internal pause state (capture/setRepeatingRequest unpause 3530 // directly). 3531 Mutex::Autolock pl(mPauseLock); 3532 if (mPaused) { 3533 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__); 3534 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 3535 if (statusTracker != 0) { 3536 statusTracker->markComponentActive(mStatusId); 3537 } 3538 } 3539 mPaused = false; 3540 3541 // Check if we've reconfigured since last time, and reset the preview 3542 // request if so. Can't use 'NULL request == repeat' across configure calls. 3543 if (mReconfigured) { 3544 mPrevRequest.clear(); 3545 mReconfigured = false; 3546 } 3547 3548 if (nextRequest != NULL) { 3549 nextRequest->mResultExtras.frameNumber = mFrameNumber++; 3550 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId; 3551 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId; 3552 3553 // Since RequestThread::clear() removes buffers from the input stream, 3554 // get the right buffer here before unlocking mRequestLock 3555 if (nextRequest->mInputStream != NULL) { 3556 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer); 3557 if (res != OK) { 3558 // Can't get input buffer from gralloc queue - this could be due to 3559 // disconnected queue or other producer misbehavior, so not a fatal 3560 // error 3561 ALOGE("%s: Can't get input buffer, skipping request:" 3562 " %s (%d)", __FUNCTION__, strerror(-res), res); 3563 3564 sp<NotificationListener> listener = mListener.promote(); 3565 if (listener != NULL) { 3566 listener->notifyError( 3567 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 3568 nextRequest->mResultExtras); 3569 } 3570 return NULL; 3571 } 3572 } 3573 } 3574 3575 handleAePrecaptureCancelRequest(nextRequest); 3576 3577 return nextRequest; 3578 } 3579 3580 bool Camera3Device::RequestThread::waitIfPaused() { 3581 status_t res; 3582 Mutex::Autolock l(mPauseLock); 3583 while (mDoPause) { 3584 if (mPaused == false) { 3585 mPaused = true; 3586 ALOGV("%s: RequestThread: Paused", __FUNCTION__); 3587 // Let the tracker know 3588 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 3589 if (statusTracker != 0) { 3590 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); 3591 } 3592 } 3593 3594 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); 3595 if (res == TIMED_OUT || exitPending()) { 3596 return true; 3597 } 3598 } 3599 // We don't set mPaused to false here, because waitForNextRequest needs 3600 // to further manage the paused state in case of starvation. 3601 return false; 3602 } 3603 3604 void Camera3Device::RequestThread::unpauseForNewRequests() { 3605 // With work to do, mark thread as unpaused. 3606 // If paused by request (setPaused), don't resume, to avoid 3607 // extra signaling/waiting overhead to waitUntilPaused 3608 mRequestSignal.signal(); 3609 Mutex::Autolock p(mPauseLock); 3610 if (!mDoPause) { 3611 ALOGV("%s: RequestThread: Going active", __FUNCTION__); 3612 if (mPaused) { 3613 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 3614 if (statusTracker != 0) { 3615 statusTracker->markComponentActive(mStatusId); 3616 } 3617 } 3618 mPaused = false; 3619 } 3620 } 3621 3622 void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { 3623 sp<Camera3Device> parent = mParent.promote(); 3624 if (parent != NULL) { 3625 va_list args; 3626 va_start(args, fmt); 3627 3628 parent->setErrorStateV(fmt, args); 3629 3630 va_end(args); 3631 } 3632 } 3633 3634 status_t Camera3Device::RequestThread::insertTriggers( 3635 const sp<CaptureRequest> &request) { 3636 3637 Mutex::Autolock al(mTriggerMutex); 3638 3639 sp<Camera3Device> parent = mParent.promote(); 3640 if (parent == NULL) { 3641 CLOGE("RequestThread: Parent is gone"); 3642 return DEAD_OBJECT; 3643 } 3644 3645 CameraMetadata &metadata = request->mSettings; 3646 size_t count = mTriggerMap.size(); 3647 3648 for (size_t i = 0; i < count; ++i) { 3649 RequestTrigger trigger = mTriggerMap.valueAt(i); 3650 uint32_t tag = trigger.metadataTag; 3651 3652 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) { 3653 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID); 3654 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue); 3655 if (isAeTrigger) { 3656 request->mResultExtras.precaptureTriggerId = triggerId; 3657 mCurrentPreCaptureTriggerId = triggerId; 3658 } else { 3659 request->mResultExtras.afTriggerId = triggerId; 3660 mCurrentAfTriggerId = triggerId; 3661 } 3662 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { 3663 continue; // Trigger ID tag is deprecated since device HAL 3.2 3664 } 3665 } 3666 3667 camera_metadata_entry entry = metadata.find(tag); 3668 3669 if (entry.count > 0) { 3670 /** 3671 * Already has an entry for this trigger in the request. 3672 * Rewrite it with our requested trigger value. 3673 */ 3674 RequestTrigger oldTrigger = trigger; 3675 3676 oldTrigger.entryValue = entry.data.u8[0]; 3677 3678 mTriggerReplacedMap.add(tag, oldTrigger); 3679 } else { 3680 /** 3681 * More typical, no trigger entry, so we just add it 3682 */ 3683 mTriggerRemovedMap.add(tag, trigger); 3684 } 3685 3686 status_t res; 3687 3688 switch (trigger.getTagType()) { 3689 case TYPE_BYTE: { 3690 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 3691 res = metadata.update(tag, 3692 &entryValue, 3693 /*count*/1); 3694 break; 3695 } 3696 case TYPE_INT32: 3697 res = metadata.update(tag, 3698 &trigger.entryValue, 3699 /*count*/1); 3700 break; 3701 default: 3702 ALOGE("%s: Type not supported: 0x%x", 3703 __FUNCTION__, 3704 trigger.getTagType()); 3705 return INVALID_OPERATION; 3706 } 3707 3708 if (res != OK) { 3709 ALOGE("%s: Failed to update request metadata with trigger tag %s" 3710 ", value %d", __FUNCTION__, trigger.getTagName(), 3711 trigger.entryValue); 3712 return res; 3713 } 3714 3715 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, 3716 trigger.getTagName(), 3717 trigger.entryValue); 3718 } 3719 3720 mTriggerMap.clear(); 3721 3722 return count; 3723 } 3724 3725 status_t Camera3Device::RequestThread::removeTriggers( 3726 const sp<CaptureRequest> &request) { 3727 Mutex::Autolock al(mTriggerMutex); 3728 3729 CameraMetadata &metadata = request->mSettings; 3730 3731 /** 3732 * Replace all old entries with their old values. 3733 */ 3734 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { 3735 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); 3736 3737 status_t res; 3738 3739 uint32_t tag = trigger.metadataTag; 3740 switch (trigger.getTagType()) { 3741 case TYPE_BYTE: { 3742 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 3743 res = metadata.update(tag, 3744 &entryValue, 3745 /*count*/1); 3746 break; 3747 } 3748 case TYPE_INT32: 3749 res = metadata.update(tag, 3750 &trigger.entryValue, 3751 /*count*/1); 3752 break; 3753 default: 3754 ALOGE("%s: Type not supported: 0x%x", 3755 __FUNCTION__, 3756 trigger.getTagType()); 3757 return INVALID_OPERATION; 3758 } 3759 3760 if (res != OK) { 3761 ALOGE("%s: Failed to restore request metadata with trigger tag %s" 3762 ", trigger value %d", __FUNCTION__, 3763 trigger.getTagName(), trigger.entryValue); 3764 return res; 3765 } 3766 } 3767 mTriggerReplacedMap.clear(); 3768 3769 /** 3770 * Remove all new entries. 3771 */ 3772 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { 3773 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); 3774 status_t res = metadata.erase(trigger.metadataTag); 3775 3776 if (res != OK) { 3777 ALOGE("%s: Failed to erase metadata with trigger tag %s" 3778 ", trigger value %d", __FUNCTION__, 3779 trigger.getTagName(), trigger.entryValue); 3780 return res; 3781 } 3782 } 3783 mTriggerRemovedMap.clear(); 3784 3785 return OK; 3786 } 3787 3788 status_t Camera3Device::RequestThread::addDummyTriggerIds( 3789 const sp<CaptureRequest> &request) { 3790 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here 3791 static const int32_t dummyTriggerId = 1; 3792 status_t res; 3793 3794 CameraMetadata &metadata = request->mSettings; 3795 3796 // If AF trigger is active, insert a dummy AF trigger ID if none already 3797 // exists 3798 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER); 3799 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID); 3800 if (afTrigger.count > 0 && 3801 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE && 3802 afId.count == 0) { 3803 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1); 3804 if (res != OK) return res; 3805 } 3806 3807 // If AE precapture trigger is active, insert a dummy precapture trigger ID 3808 // if none already exists 3809 camera_metadata_entry pcTrigger = 3810 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); 3811 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); 3812 if (pcTrigger.count > 0 && 3813 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE && 3814 pcId.count == 0) { 3815 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID, 3816 &dummyTriggerId, 1); 3817 if (res != OK) return res; 3818 } 3819 3820 return OK; 3821 } 3822 3823 /** 3824 * PreparerThread inner class methods 3825 */ 3826 3827 Camera3Device::PreparerThread::PreparerThread() : 3828 Thread(/*canCallJava*/false), mListener(nullptr), 3829 mActive(false), mCancelNow(false) { 3830 } 3831 3832 Camera3Device::PreparerThread::~PreparerThread() { 3833 Thread::requestExitAndWait(); 3834 if (mCurrentStream != nullptr) { 3835 mCurrentStream->cancelPrepare(); 3836 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); 3837 mCurrentStream.clear(); 3838 } 3839 clear(); 3840 } 3841 3842 status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) { 3843 status_t res; 3844 3845 Mutex::Autolock l(mLock); 3846 sp<NotificationListener> listener = mListener.promote(); 3847 3848 res = stream->startPrepare(maxCount); 3849 if (res == OK) { 3850 // No preparation needed, fire listener right off 3851 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId()); 3852 if (listener != NULL) { 3853 listener->notifyPrepared(stream->getId()); 3854 } 3855 return OK; 3856 } else if (res != NOT_ENOUGH_DATA) { 3857 return res; 3858 } 3859 3860 // Need to prepare, start up thread if necessary 3861 if (!mActive) { 3862 // mRunning will change to false before the thread fully shuts down, so wait to be sure it 3863 // isn't running 3864 Thread::requestExitAndWait(); 3865 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND); 3866 if (res != OK) { 3867 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res)); 3868 if (listener != NULL) { 3869 listener->notifyPrepared(stream->getId()); 3870 } 3871 return res; 3872 } 3873 mCancelNow = false; 3874 mActive = true; 3875 ALOGV("%s: Preparer stream started", __FUNCTION__); 3876 } 3877 3878 // queue up the work 3879 mPendingStreams.push_back(stream); 3880 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId()); 3881 3882 return OK; 3883 } 3884 3885 status_t Camera3Device::PreparerThread::clear() { 3886 Mutex::Autolock l(mLock); 3887 3888 for (const auto& stream : mPendingStreams) { 3889 stream->cancelPrepare(); 3890 } 3891 mPendingStreams.clear(); 3892 mCancelNow = true; 3893 3894 return OK; 3895 } 3896 3897 void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) { 3898 Mutex::Autolock l(mLock); 3899 mListener = listener; 3900 } 3901 3902 bool Camera3Device::PreparerThread::threadLoop() { 3903 status_t res; 3904 { 3905 Mutex::Autolock l(mLock); 3906 if (mCurrentStream == nullptr) { 3907 // End thread if done with work 3908 if (mPendingStreams.empty()) { 3909 ALOGV("%s: Preparer stream out of work", __FUNCTION__); 3910 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would 3911 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive. 3912 mActive = false; 3913 return false; 3914 } 3915 3916 // Get next stream to prepare 3917 auto it = mPendingStreams.begin(); 3918 mCurrentStream = *it; 3919 mPendingStreams.erase(it); 3920 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId()); 3921 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId()); 3922 } else if (mCancelNow) { 3923 mCurrentStream->cancelPrepare(); 3924 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); 3925 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId()); 3926 mCurrentStream.clear(); 3927 mCancelNow = false; 3928 return true; 3929 } 3930 } 3931 3932 res = mCurrentStream->prepareNextBuffer(); 3933 if (res == NOT_ENOUGH_DATA) return true; 3934 if (res != OK) { 3935 // Something bad happened; try to recover by cancelling prepare and 3936 // signalling listener anyway 3937 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__, 3938 mCurrentStream->getId(), res, strerror(-res)); 3939 mCurrentStream->cancelPrepare(); 3940 } 3941 3942 // This stream has finished, notify listener 3943 Mutex::Autolock l(mLock); 3944 sp<NotificationListener> listener = mListener.promote(); 3945 if (listener != NULL) { 3946 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__, 3947 mCurrentStream->getId()); 3948 listener->notifyPrepared(mCurrentStream->getId()); 3949 } 3950 3951 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); 3952 mCurrentStream.clear(); 3953 3954 return true; 3955 } 3956 3957 /** 3958 * Static callback forwarding methods from HAL to instance 3959 */ 3960 3961 void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, 3962 const camera3_capture_result *result) { 3963 Camera3Device *d = 3964 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 3965 3966 d->processCaptureResult(result); 3967 } 3968 3969 void Camera3Device::sNotify(const camera3_callback_ops *cb, 3970 const camera3_notify_msg *msg) { 3971 Camera3Device *d = 3972 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 3973 d->notify(msg); 3974 } 3975 3976 }; // namespace android 3977