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 %s: %s: " fmt, mId.string(), __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/Camera3DummyStream.h" 55#include "device3/Camera3SharedOutputStream.h" 56#include "CameraService.h" 57 58using namespace android::camera3; 59using namespace android::hardware::camera; 60using namespace android::hardware::camera::device::V3_2; 61 62namespace android { 63 64Camera3Device::Camera3Device(const String8 &id): 65 mId(id), 66 mOperatingMode(NO_MODE), 67 mIsConstrainedHighSpeedConfiguration(false), 68 mStatus(STATUS_UNINITIALIZED), 69 mStatusWaiters(0), 70 mUsePartialResult(false), 71 mNumPartialResults(1), 72 mTimestampOffset(0), 73 mNextResultFrameNumber(0), 74 mNextReprocessResultFrameNumber(0), 75 mNextShutterFrameNumber(0), 76 mNextReprocessShutterFrameNumber(0), 77 mListener(NULL), 78 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID) 79{ 80 ATRACE_CALL(); 81 camera3_callback_ops::notify = &sNotify; 82 camera3_callback_ops::process_capture_result = &sProcessCaptureResult; 83 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string()); 84} 85 86Camera3Device::~Camera3Device() 87{ 88 ATRACE_CALL(); 89 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string()); 90 disconnect(); 91} 92 93const String8& Camera3Device::getId() const { 94 return mId; 95} 96 97status_t Camera3Device::initialize(sp<CameraProviderManager> manager) { 98 ATRACE_CALL(); 99 Mutex::Autolock il(mInterfaceLock); 100 Mutex::Autolock l(mLock); 101 102 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string()); 103 if (mStatus != STATUS_UNINITIALIZED) { 104 CLOGE("Already initialized!"); 105 return INVALID_OPERATION; 106 } 107 if (manager == nullptr) return INVALID_OPERATION; 108 109 sp<ICameraDeviceSession> session; 110 ATRACE_BEGIN("CameraHal::openSession"); 111 status_t res = manager->openSession(mId.string(), this, 112 /*out*/ &session); 113 ATRACE_END(); 114 if (res != OK) { 115 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res); 116 return res; 117 } 118 119 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo); 120 if (res != OK) { 121 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res); 122 session->close(); 123 return res; 124 } 125 126 std::shared_ptr<RequestMetadataQueue> queue; 127 auto requestQueueRet = session->getCaptureRequestMetadataQueue( 128 [&queue](const auto& descriptor) { 129 queue = std::make_shared<RequestMetadataQueue>(descriptor); 130 if (!queue->isValid() || queue->availableToWrite() <= 0) { 131 ALOGE("HAL returns empty request metadata fmq, not use it"); 132 queue = nullptr; 133 // don't use the queue onwards. 134 } 135 }); 136 if (!requestQueueRet.isOk()) { 137 ALOGE("Transaction error when getting request metadata fmq: %s, not use it", 138 requestQueueRet.description().c_str()); 139 return DEAD_OBJECT; 140 } 141 142 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue; 143 auto resultQueueRet = session->getCaptureResultMetadataQueue( 144 [&resQueue](const auto& descriptor) { 145 resQueue = std::make_unique<ResultMetadataQueue>(descriptor); 146 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) { 147 ALOGE("HAL returns empty result metadata fmq, not use it"); 148 resQueue = nullptr; 149 // Don't use the resQueue onwards. 150 } 151 }); 152 if (!resultQueueRet.isOk()) { 153 ALOGE("Transaction error when getting result metadata queue from camera session: %s", 154 resultQueueRet.description().c_str()); 155 return DEAD_OBJECT; 156 } 157 IF_ALOGV() { 158 session->interfaceChain([]( 159 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) { 160 ALOGV("Session interface chain:"); 161 for (auto iface : interfaceChain) { 162 ALOGV(" %s", iface.c_str()); 163 } 164 }); 165 } 166 167 mInterface = new HalInterface(session, queue); 168 std::string providerType; 169 mVendorTagId = manager->getProviderTagIdLocked(mId.string()); 170 171 return initializeCommonLocked(); 172} 173 174status_t Camera3Device::initializeCommonLocked() { 175 176 /** Start up status tracker thread */ 177 mStatusTracker = new StatusTracker(this); 178 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string()); 179 if (res != OK) { 180 SET_ERR_L("Unable to start status tracking thread: %s (%d)", 181 strerror(-res), res); 182 mInterface->close(); 183 mStatusTracker.clear(); 184 return res; 185 } 186 187 /** Register in-flight map to the status tracker */ 188 mInFlightStatusId = mStatusTracker->addComponent(); 189 190 /** Create buffer manager */ 191 mBufferManager = new Camera3BufferManager(); 192 193 mTagMonitor.initialize(mVendorTagId); 194 195 /** Start up request queue thread */ 196 mRequestThread = new RequestThread(this, mStatusTracker, mInterface); 197 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string()); 198 if (res != OK) { 199 SET_ERR_L("Unable to start request queue thread: %s (%d)", 200 strerror(-res), res); 201 mInterface->close(); 202 mRequestThread.clear(); 203 return res; 204 } 205 206 mPreparerThread = new PreparerThread(); 207 208 internalUpdateStatusLocked(STATUS_UNCONFIGURED); 209 mNextStreamId = 0; 210 mDummyStreamId = NO_STREAM; 211 mNeedConfig = true; 212 mPauseStateNotify = false; 213 214 // Measure the clock domain offset between camera and video/hw_composer 215 camera_metadata_entry timestampSource = 216 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE); 217 if (timestampSource.count > 0 && timestampSource.data.u8[0] == 218 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) { 219 mTimestampOffset = getMonoToBoottimeOffset(); 220 } 221 222 // Will the HAL be sending in early partial result metadata? 223 camera_metadata_entry partialResultsCount = 224 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT); 225 if (partialResultsCount.count > 0) { 226 mNumPartialResults = partialResultsCount.data.i32[0]; 227 mUsePartialResult = (mNumPartialResults > 1); 228 } 229 230 camera_metadata_entry configs = 231 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); 232 for (uint32_t i = 0; i < configs.count; i += 4) { 233 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && 234 configs.data.i32[i + 3] == 235 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) { 236 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1], 237 configs.data.i32[i + 2])); 238 } 239 } 240 241 return OK; 242} 243 244status_t Camera3Device::disconnect() { 245 ATRACE_CALL(); 246 Mutex::Autolock il(mInterfaceLock); 247 248 ALOGI("%s: E", __FUNCTION__); 249 250 status_t res = OK; 251 std::vector<wp<Camera3StreamInterface>> streams; 252 nsecs_t maxExpectedDuration = getExpectedInFlightDuration(); 253 { 254 Mutex::Autolock l(mLock); 255 if (mStatus == STATUS_UNINITIALIZED) return res; 256 257 if (mStatus == STATUS_ACTIVE || 258 (mStatus == STATUS_ERROR && mRequestThread != NULL)) { 259 res = mRequestThread->clearRepeatingRequests(); 260 if (res != OK) { 261 SET_ERR_L("Can't stop streaming"); 262 // Continue to close device even in case of error 263 } else { 264 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration); 265 if (res != OK) { 266 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)", 267 maxExpectedDuration); 268 // Continue to close device even in case of error 269 } 270 } 271 } 272 273 if (mStatus == STATUS_ERROR) { 274 CLOGE("Shutting down in an error state"); 275 } 276 277 if (mStatusTracker != NULL) { 278 mStatusTracker->requestExit(); 279 } 280 281 if (mRequestThread != NULL) { 282 mRequestThread->requestExit(); 283 } 284 285 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0)); 286 for (size_t i = 0; i < mOutputStreams.size(); i++) { 287 streams.push_back(mOutputStreams[i]); 288 } 289 if (mInputStream != nullptr) { 290 streams.push_back(mInputStream); 291 } 292 } 293 294 // Joining done without holding mLock, otherwise deadlocks may ensue 295 // as the threads try to access parent state 296 if (mRequestThread != NULL && mStatus != STATUS_ERROR) { 297 // HAL may be in a bad state, so waiting for request thread 298 // (which may be stuck in the HAL processCaptureRequest call) 299 // could be dangerous. 300 mRequestThread->join(); 301 } 302 303 if (mStatusTracker != NULL) { 304 mStatusTracker->join(); 305 } 306 307 HalInterface* interface; 308 { 309 Mutex::Autolock l(mLock); 310 mRequestThread.clear(); 311 mStatusTracker.clear(); 312 interface = mInterface.get(); 313 } 314 315 // Call close without internal mutex held, as the HAL close may need to 316 // wait on assorted callbacks,etc, to complete before it can return. 317 interface->close(); 318 319 flushInflightRequests(); 320 321 { 322 Mutex::Autolock l(mLock); 323 mInterface->clear(); 324 mOutputStreams.clear(); 325 mInputStream.clear(); 326 mDeletedStreams.clear(); 327 mBufferManager.clear(); 328 internalUpdateStatusLocked(STATUS_UNINITIALIZED); 329 } 330 331 for (auto& weakStream : streams) { 332 sp<Camera3StreamInterface> stream = weakStream.promote(); 333 if (stream != nullptr) { 334 ALOGE("%s: Stream %d leaked! strong reference (%d)!", 335 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1); 336 } 337 } 338 339 ALOGI("%s: X", __FUNCTION__); 340 return res; 341} 342 343// For dumping/debugging only - 344// try to acquire a lock a few times, eventually give up to proceed with 345// debug/dump operations 346bool Camera3Device::tryLockSpinRightRound(Mutex& lock) { 347 bool gotLock = false; 348 for (size_t i = 0; i < kDumpLockAttempts; ++i) { 349 if (lock.tryLock() == NO_ERROR) { 350 gotLock = true; 351 break; 352 } else { 353 usleep(kDumpSleepDuration); 354 } 355 } 356 return gotLock; 357} 358 359Camera3Device::Size Camera3Device::getMaxJpegResolution() const { 360 int32_t maxJpegWidth = 0, maxJpegHeight = 0; 361 const int STREAM_CONFIGURATION_SIZE = 4; 362 const int STREAM_FORMAT_OFFSET = 0; 363 const int STREAM_WIDTH_OFFSET = 1; 364 const int STREAM_HEIGHT_OFFSET = 2; 365 const int STREAM_IS_INPUT_OFFSET = 3; 366 camera_metadata_ro_entry_t availableStreamConfigs = 367 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); 368 if (availableStreamConfigs.count == 0 || 369 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) { 370 return Size(0, 0); 371 } 372 373 // Get max jpeg size (area-wise). 374 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) { 375 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET]; 376 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET]; 377 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET]; 378 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET]; 379 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT 380 && format == HAL_PIXEL_FORMAT_BLOB && 381 (width * height > maxJpegWidth * maxJpegHeight)) { 382 maxJpegWidth = width; 383 maxJpegHeight = height; 384 } 385 } 386 387 return Size(maxJpegWidth, maxJpegHeight); 388} 389 390nsecs_t Camera3Device::getMonoToBoottimeOffset() { 391 // try three times to get the clock offset, choose the one 392 // with the minimum gap in measurements. 393 const int tries = 3; 394 nsecs_t bestGap, measured; 395 for (int i = 0; i < tries; ++i) { 396 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); 397 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME); 398 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); 399 const nsecs_t gap = tmono2 - tmono; 400 if (i == 0 || gap < bestGap) { 401 bestGap = gap; 402 measured = tbase - ((tmono + tmono2) >> 1); 403 } 404 } 405 return measured; 406} 407 408hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat( 409 int frameworkFormat) { 410 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat; 411} 412 413DataspaceFlags Camera3Device::mapToHidlDataspace( 414 android_dataspace dataSpace) { 415 return dataSpace; 416} 417 418BufferUsageFlags Camera3Device::mapToConsumerUsage( 419 uint64_t usage) { 420 return usage; 421} 422 423StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) { 424 switch (rotation) { 425 case CAMERA3_STREAM_ROTATION_0: 426 return StreamRotation::ROTATION_0; 427 case CAMERA3_STREAM_ROTATION_90: 428 return StreamRotation::ROTATION_90; 429 case CAMERA3_STREAM_ROTATION_180: 430 return StreamRotation::ROTATION_180; 431 case CAMERA3_STREAM_ROTATION_270: 432 return StreamRotation::ROTATION_270; 433 } 434 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation); 435 return StreamRotation::ROTATION_0; 436} 437 438status_t Camera3Device::mapToStreamConfigurationMode( 439 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) { 440 if (mode == nullptr) return BAD_VALUE; 441 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) { 442 switch(operationMode) { 443 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE: 444 *mode = StreamConfigurationMode::NORMAL_MODE; 445 break; 446 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE: 447 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE; 448 break; 449 default: 450 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode); 451 return BAD_VALUE; 452 } 453 } else { 454 *mode = static_cast<StreamConfigurationMode>(operationMode); 455 } 456 return OK; 457} 458 459camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) { 460 switch (status) { 461 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK; 462 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR; 463 } 464 return CAMERA3_BUFFER_STATUS_ERROR; 465} 466 467int Camera3Device::mapToFrameworkFormat( 468 hardware::graphics::common::V1_0::PixelFormat pixelFormat) { 469 return static_cast<uint32_t>(pixelFormat); 470} 471 472android_dataspace Camera3Device::mapToFrameworkDataspace( 473 DataspaceFlags dataSpace) { 474 return static_cast<android_dataspace>(dataSpace); 475} 476 477uint64_t Camera3Device::mapConsumerToFrameworkUsage( 478 BufferUsageFlags usage) { 479 return usage; 480} 481 482uint64_t Camera3Device::mapProducerToFrameworkUsage( 483 BufferUsageFlags usage) { 484 return usage; 485} 486 487ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const { 488 // Get max jpeg size (area-wise). 489 Size maxJpegResolution = getMaxJpegResolution(); 490 if (maxJpegResolution.width == 0) { 491 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!", 492 __FUNCTION__, mId.string()); 493 return BAD_VALUE; 494 } 495 496 // Get max jpeg buffer size 497 ssize_t maxJpegBufferSize = 0; 498 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE); 499 if (jpegBufMaxSize.count == 0) { 500 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__, 501 mId.string()); 502 return BAD_VALUE; 503 } 504 maxJpegBufferSize = jpegBufMaxSize.data.i32[0]; 505 assert(kMinJpegBufferSize < maxJpegBufferSize); 506 507 // Calculate final jpeg buffer size for the given resolution. 508 float scaleFactor = ((float) (width * height)) / 509 (maxJpegResolution.width * maxJpegResolution.height); 510 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) + 511 kMinJpegBufferSize; 512 if (jpegBufferSize > maxJpegBufferSize) { 513 jpegBufferSize = maxJpegBufferSize; 514 } 515 516 return jpegBufferSize; 517} 518 519ssize_t Camera3Device::getPointCloudBufferSize() const { 520 const int FLOATS_PER_POINT=4; 521 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES); 522 if (maxPointCount.count == 0) { 523 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!", 524 __FUNCTION__, mId.string()); 525 return BAD_VALUE; 526 } 527 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) + 528 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT; 529 return maxBytesForPointCloud; 530} 531 532ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const { 533 const int PER_CONFIGURATION_SIZE = 3; 534 const int WIDTH_OFFSET = 0; 535 const int HEIGHT_OFFSET = 1; 536 const int SIZE_OFFSET = 2; 537 camera_metadata_ro_entry rawOpaqueSizes = 538 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE); 539 size_t count = rawOpaqueSizes.count; 540 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) { 541 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!", 542 __FUNCTION__, mId.string(), count); 543 return BAD_VALUE; 544 } 545 546 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) { 547 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] && 548 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) { 549 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET]; 550 } 551 } 552 553 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!", 554 __FUNCTION__, mId.string(), width, height); 555 return BAD_VALUE; 556} 557 558status_t Camera3Device::dump(int fd, const Vector<String16> &args) { 559 ATRACE_CALL(); 560 (void)args; 561 562 // Try to lock, but continue in case of failure (to avoid blocking in 563 // deadlocks) 564 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock); 565 bool gotLock = tryLockSpinRightRound(mLock); 566 567 ALOGW_IF(!gotInterfaceLock, 568 "Camera %s: %s: Unable to lock interface lock, proceeding anyway", 569 mId.string(), __FUNCTION__); 570 ALOGW_IF(!gotLock, 571 "Camera %s: %s: Unable to lock main lock, proceeding anyway", 572 mId.string(), __FUNCTION__); 573 574 bool dumpTemplates = false; 575 576 String16 templatesOption("-t"); 577 String16 monitorOption("-m"); 578 int n = args.size(); 579 for (int i = 0; i < n; i++) { 580 if (args[i] == templatesOption) { 581 dumpTemplates = true; 582 } 583 if (args[i] == monitorOption) { 584 if (i + 1 < n) { 585 String8 monitorTags = String8(args[i + 1]); 586 if (monitorTags == "off") { 587 mTagMonitor.disableMonitoring(); 588 } else { 589 mTagMonitor.parseTagsToMonitor(monitorTags); 590 } 591 } else { 592 mTagMonitor.disableMonitoring(); 593 } 594 } 595 } 596 597 String8 lines; 598 599 const char *status = 600 mStatus == STATUS_ERROR ? "ERROR" : 601 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : 602 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" : 603 mStatus == STATUS_CONFIGURED ? "CONFIGURED" : 604 mStatus == STATUS_ACTIVE ? "ACTIVE" : 605 "Unknown"; 606 607 lines.appendFormat(" Device status: %s\n", status); 608 if (mStatus == STATUS_ERROR) { 609 lines.appendFormat(" Error cause: %s\n", mErrorCause.string()); 610 } 611 lines.appendFormat(" Stream configuration:\n"); 612 const char *mode = 613 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" : 614 mOperatingMode == static_cast<int>( 615 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" : 616 "CUSTOM"; 617 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode); 618 619 if (mInputStream != NULL) { 620 write(fd, lines.string(), lines.size()); 621 mInputStream->dump(fd, args); 622 } else { 623 lines.appendFormat(" No input stream.\n"); 624 write(fd, lines.string(), lines.size()); 625 } 626 for (size_t i = 0; i < mOutputStreams.size(); i++) { 627 mOutputStreams[i]->dump(fd,args); 628 } 629 630 if (mBufferManager != NULL) { 631 lines = String8(" Camera3 Buffer Manager:\n"); 632 write(fd, lines.string(), lines.size()); 633 mBufferManager->dump(fd, args); 634 } 635 636 lines = String8(" In-flight requests:\n"); 637 if (mInFlightMap.size() == 0) { 638 lines.append(" None\n"); 639 } else { 640 for (size_t i = 0; i < mInFlightMap.size(); i++) { 641 InFlightRequest r = mInFlightMap.valueAt(i); 642 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata" 643 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), 644 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false", 645 r.numBuffersLeft); 646 } 647 } 648 write(fd, lines.string(), lines.size()); 649 650 if (mRequestThread != NULL) { 651 mRequestThread->dumpCaptureRequestLatency(fd, 652 " ProcessCaptureRequest latency histogram:"); 653 } 654 655 { 656 lines = String8(" Last request sent:\n"); 657 write(fd, lines.string(), lines.size()); 658 659 CameraMetadata lastRequest = getLatestRequestLocked(); 660 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6); 661 } 662 663 if (dumpTemplates) { 664 const char *templateNames[] = { 665 "TEMPLATE_PREVIEW", 666 "TEMPLATE_STILL_CAPTURE", 667 "TEMPLATE_VIDEO_RECORD", 668 "TEMPLATE_VIDEO_SNAPSHOT", 669 "TEMPLATE_ZERO_SHUTTER_LAG", 670 "TEMPLATE_MANUAL" 671 }; 672 673 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) { 674 camera_metadata_t *templateRequest = nullptr; 675 mInterface->constructDefaultRequestSettings( 676 (camera3_request_template_t) i, &templateRequest); 677 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]); 678 if (templateRequest == nullptr) { 679 lines.append(" Not supported\n"); 680 write(fd, lines.string(), lines.size()); 681 } else { 682 write(fd, lines.string(), lines.size()); 683 dump_indented_camera_metadata(templateRequest, 684 fd, /*verbosity*/2, /*indentation*/8); 685 } 686 free_camera_metadata(templateRequest); 687 } 688 } 689 690 mTagMonitor.dumpMonitoredMetadata(fd); 691 692 if (mInterface->valid()) { 693 lines = String8(" HAL device dump:\n"); 694 write(fd, lines.string(), lines.size()); 695 mInterface->dump(fd); 696 } 697 698 if (gotLock) mLock.unlock(); 699 if (gotInterfaceLock) mInterfaceLock.unlock(); 700 701 return OK; 702} 703 704const CameraMetadata& Camera3Device::info() const { 705 ALOGVV("%s: E", __FUNCTION__); 706 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || 707 mStatus == STATUS_ERROR)) { 708 ALOGW("%s: Access to static info %s!", __FUNCTION__, 709 mStatus == STATUS_ERROR ? 710 "when in error state" : "before init"); 711 } 712 return mDeviceInfo; 713} 714 715status_t Camera3Device::checkStatusOkToCaptureLocked() { 716 switch (mStatus) { 717 case STATUS_ERROR: 718 CLOGE("Device has encountered a serious error"); 719 return INVALID_OPERATION; 720 case STATUS_UNINITIALIZED: 721 CLOGE("Device not initialized"); 722 return INVALID_OPERATION; 723 case STATUS_UNCONFIGURED: 724 case STATUS_CONFIGURED: 725 case STATUS_ACTIVE: 726 // OK 727 break; 728 default: 729 SET_ERR_L("Unexpected status: %d", mStatus); 730 return INVALID_OPERATION; 731 } 732 return OK; 733} 734 735status_t Camera3Device::convertMetadataListToRequestListLocked( 736 const List<const CameraMetadata> &metadataList, 737 const std::list<const SurfaceMap> &surfaceMaps, 738 bool repeating, 739 RequestList *requestList) { 740 if (requestList == NULL) { 741 CLOGE("requestList cannot be NULL."); 742 return BAD_VALUE; 743 } 744 745 int32_t burstId = 0; 746 List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin(); 747 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin(); 748 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end(); 749 ++metadataIt, ++surfaceMapIt) { 750 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt); 751 if (newRequest == 0) { 752 CLOGE("Can't create capture request"); 753 return BAD_VALUE; 754 } 755 756 newRequest->mRepeating = repeating; 757 758 // Setup burst Id and request Id 759 newRequest->mResultExtras.burstId = burstId++; 760 if (metadataIt->exists(ANDROID_REQUEST_ID)) { 761 if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) { 762 CLOGE("RequestID entry exists; but must not be empty in metadata"); 763 return BAD_VALUE; 764 } 765 newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0]; 766 } else { 767 CLOGE("RequestID does not exist in metadata"); 768 return BAD_VALUE; 769 } 770 771 requestList->push_back(newRequest); 772 773 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId); 774 } 775 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) { 776 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__); 777 return BAD_VALUE; 778 } 779 780 // Setup batch size if this is a high speed video recording request. 781 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) { 782 auto firstRequest = requestList->begin(); 783 for (auto& outputStream : (*firstRequest)->mOutputStreams) { 784 if (outputStream->isVideoStream()) { 785 (*firstRequest)->mBatchSize = requestList->size(); 786 break; 787 } 788 } 789 } 790 791 return OK; 792} 793 794status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) { 795 ATRACE_CALL(); 796 797 List<const CameraMetadata> requests; 798 std::list<const SurfaceMap> surfaceMaps; 799 convertToRequestList(requests, surfaceMaps, request); 800 801 return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL); 802} 803 804void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests, 805 std::list<const SurfaceMap>& surfaceMaps, 806 const CameraMetadata& request) { 807 requests.push_back(request); 808 809 SurfaceMap surfaceMap; 810 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS); 811 // With no surface list passed in, stream and surface will have 1-to-1 812 // mapping. So the surface index is 0 for each stream in the surfaceMap. 813 for (size_t i = 0; i < streams.count; i++) { 814 surfaceMap[streams.data.i32[i]].push_back(0); 815 } 816 surfaceMaps.push_back(surfaceMap); 817} 818 819status_t Camera3Device::submitRequestsHelper( 820 const List<const CameraMetadata> &requests, 821 const std::list<const SurfaceMap> &surfaceMaps, 822 bool repeating, 823 /*out*/ 824 int64_t *lastFrameNumber) { 825 ATRACE_CALL(); 826 Mutex::Autolock il(mInterfaceLock); 827 Mutex::Autolock l(mLock); 828 829 status_t res = checkStatusOkToCaptureLocked(); 830 if (res != OK) { 831 // error logged by previous call 832 return res; 833 } 834 835 RequestList requestList; 836 837 res = convertMetadataListToRequestListLocked(requests, surfaceMaps, 838 repeating, /*out*/&requestList); 839 if (res != OK) { 840 // error logged by previous call 841 return res; 842 } 843 844 if (repeating) { 845 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber); 846 } else { 847 res = mRequestThread->queueRequestList(requestList, lastFrameNumber); 848 } 849 850 if (res == OK) { 851 waitUntilStateThenRelock(/*active*/true, kActiveTimeout); 852 if (res != OK) { 853 SET_ERR_L("Can't transition to active in %f seconds!", 854 kActiveTimeout/1e9); 855 } 856 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(), 857 (*(requestList.begin()))->mResultExtras.requestId); 858 } else { 859 CLOGE("Cannot queue request. Impossible."); 860 return BAD_VALUE; 861 } 862 863 return res; 864} 865 866// Only one processCaptureResult should be called at a time, so 867// the locks won't block. The locks are present here simply to enforce this. 868hardware::Return<void> Camera3Device::processCaptureResult( 869 const hardware::hidl_vec< 870 hardware::camera::device::V3_2::CaptureResult>& results) { 871 // Ideally we should grab mLock, but that can lead to deadlock, and 872 // it's not super important to get up to date value of mStatus for this 873 // warning print, hence skipping the lock here 874 if (mStatus == STATUS_ERROR) { 875 // Per API contract, HAL should act as closed after device error 876 // But mStatus can be set to error by framework as well, so just log 877 // a warning here. 878 ALOGW("%s: received capture result in error state.", __FUNCTION__); 879 } 880 881 if (mProcessCaptureResultLock.tryLock() != OK) { 882 // This should never happen; it indicates a wrong client implementation 883 // that doesn't follow the contract. But, we can be tolerant here. 884 ALOGE("%s: callback overlapped! waiting 1s...", 885 __FUNCTION__); 886 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) { 887 ALOGE("%s: cannot acquire lock in 1s, dropping results", 888 __FUNCTION__); 889 // really don't know what to do, so bail out. 890 return hardware::Void(); 891 } 892 } 893 for (const auto& result : results) { 894 processOneCaptureResultLocked(result); 895 } 896 mProcessCaptureResultLock.unlock(); 897 return hardware::Void(); 898} 899 900void Camera3Device::processOneCaptureResultLocked( 901 const hardware::camera::device::V3_2::CaptureResult& result) { 902 camera3_capture_result r; 903 status_t res; 904 r.frame_number = result.frameNumber; 905 906 hardware::camera::device::V3_2::CameraMetadata resultMetadata; 907 if (result.fmqResultSize > 0) { 908 resultMetadata.resize(result.fmqResultSize); 909 if (mResultMetadataQueue == nullptr) { 910 return; // logged in initialize() 911 } 912 if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) { 913 ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64, 914 __FUNCTION__, result.frameNumber, result.fmqResultSize); 915 return; 916 } 917 } else { 918 resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()), 919 result.result.size()); 920 } 921 922 if (resultMetadata.size() != 0) { 923 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data()); 924 size_t expected_metadata_size = resultMetadata.size(); 925 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) { 926 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)", 927 __FUNCTION__, result.frameNumber, strerror(-res), res); 928 return; 929 } 930 } else { 931 r.result = nullptr; 932 } 933 934 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size()); 935 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size()); 936 for (size_t i = 0; i < result.outputBuffers.size(); i++) { 937 auto& bDst = outputBuffers[i]; 938 const StreamBuffer &bSrc = result.outputBuffers[i]; 939 940 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId); 941 if (idx == NAME_NOT_FOUND) { 942 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d", 943 __FUNCTION__, result.frameNumber, i, bSrc.streamId); 944 return; 945 } 946 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream(); 947 948 buffer_handle_t *buffer; 949 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer); 950 if (res != OK) { 951 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d", 952 __FUNCTION__, result.frameNumber, i, bSrc.streamId); 953 return; 954 } 955 bDst.buffer = buffer; 956 bDst.status = mapHidlBufferStatus(bSrc.status); 957 bDst.acquire_fence = -1; 958 if (bSrc.releaseFence == nullptr) { 959 bDst.release_fence = -1; 960 } else if (bSrc.releaseFence->numFds == 1) { 961 bDst.release_fence = dup(bSrc.releaseFence->data[0]); 962 } else { 963 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1", 964 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds); 965 return; 966 } 967 } 968 r.num_output_buffers = outputBuffers.size(); 969 r.output_buffers = outputBuffers.data(); 970 971 camera3_stream_buffer_t inputBuffer; 972 if (result.inputBuffer.streamId == -1) { 973 r.input_buffer = nullptr; 974 } else { 975 if (mInputStream->getId() != result.inputBuffer.streamId) { 976 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__, 977 result.frameNumber, result.inputBuffer.streamId); 978 return; 979 } 980 inputBuffer.stream = mInputStream->asHalStream(); 981 buffer_handle_t *buffer; 982 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId, 983 &buffer); 984 if (res != OK) { 985 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d", 986 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId); 987 return; 988 } 989 inputBuffer.buffer = buffer; 990 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status); 991 inputBuffer.acquire_fence = -1; 992 if (result.inputBuffer.releaseFence == nullptr) { 993 inputBuffer.release_fence = -1; 994 } else if (result.inputBuffer.releaseFence->numFds == 1) { 995 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]); 996 } else { 997 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1", 998 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds); 999 return; 1000 } 1001 r.input_buffer = &inputBuffer; 1002 } 1003 1004 r.partial_result = result.partialResult; 1005 1006 processCaptureResult(&r); 1007} 1008 1009hardware::Return<void> Camera3Device::notify( 1010 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) { 1011 // Ideally we should grab mLock, but that can lead to deadlock, and 1012 // it's not super important to get up to date value of mStatus for this 1013 // warning print, hence skipping the lock here 1014 if (mStatus == STATUS_ERROR) { 1015 // Per API contract, HAL should act as closed after device error 1016 // But mStatus can be set to error by framework as well, so just log 1017 // a warning here. 1018 ALOGW("%s: received notify message in error state.", __FUNCTION__); 1019 } 1020 1021 for (const auto& msg : msgs) { 1022 notify(msg); 1023 } 1024 return hardware::Void(); 1025} 1026 1027void Camera3Device::notify( 1028 const hardware::camera::device::V3_2::NotifyMsg& msg) { 1029 1030 camera3_notify_msg m; 1031 switch (msg.type) { 1032 case MsgType::ERROR: 1033 m.type = CAMERA3_MSG_ERROR; 1034 m.message.error.frame_number = msg.msg.error.frameNumber; 1035 if (msg.msg.error.errorStreamId >= 0) { 1036 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId); 1037 if (idx == NAME_NOT_FOUND) { 1038 ALOGE("%s: Frame %d: Invalid error stream id %d", 1039 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId); 1040 return; 1041 } 1042 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream(); 1043 } else { 1044 m.message.error.error_stream = nullptr; 1045 } 1046 switch (msg.msg.error.errorCode) { 1047 case ErrorCode::ERROR_DEVICE: 1048 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE; 1049 break; 1050 case ErrorCode::ERROR_REQUEST: 1051 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; 1052 break; 1053 case ErrorCode::ERROR_RESULT: 1054 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT; 1055 break; 1056 case ErrorCode::ERROR_BUFFER: 1057 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER; 1058 break; 1059 } 1060 break; 1061 case MsgType::SHUTTER: 1062 m.type = CAMERA3_MSG_SHUTTER; 1063 m.message.shutter.frame_number = msg.msg.shutter.frameNumber; 1064 m.message.shutter.timestamp = msg.msg.shutter.timestamp; 1065 break; 1066 } 1067 notify(&m); 1068} 1069 1070status_t Camera3Device::captureList(const List<const CameraMetadata> &requests, 1071 const std::list<const SurfaceMap> &surfaceMaps, 1072 int64_t *lastFrameNumber) { 1073 ATRACE_CALL(); 1074 1075 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber); 1076} 1077 1078status_t Camera3Device::setStreamingRequest(const CameraMetadata &request, 1079 int64_t* /*lastFrameNumber*/) { 1080 ATRACE_CALL(); 1081 1082 List<const CameraMetadata> requests; 1083 std::list<const SurfaceMap> surfaceMaps; 1084 convertToRequestList(requests, surfaceMaps, request); 1085 1086 return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps, 1087 /*lastFrameNumber*/NULL); 1088} 1089 1090status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests, 1091 const std::list<const SurfaceMap> &surfaceMaps, 1092 int64_t *lastFrameNumber) { 1093 ATRACE_CALL(); 1094 1095 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber); 1096} 1097 1098sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( 1099 const CameraMetadata &request, const SurfaceMap &surfaceMap) { 1100 status_t res; 1101 1102 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) { 1103 // This point should only be reached via API1 (API2 must explicitly call configureStreams) 1104 // so unilaterally select normal operating mode. 1105 res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE); 1106 // Stream configuration failed. Client might try other configuraitons. 1107 if (res != OK) { 1108 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res); 1109 return NULL; 1110 } else if (mStatus == STATUS_UNCONFIGURED) { 1111 // Stream configuration successfully configure to empty stream configuration. 1112 CLOGE("No streams configured"); 1113 return NULL; 1114 } 1115 } 1116 1117 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap); 1118 return newRequest; 1119} 1120 1121status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) { 1122 ATRACE_CALL(); 1123 Mutex::Autolock il(mInterfaceLock); 1124 Mutex::Autolock l(mLock); 1125 1126 switch (mStatus) { 1127 case STATUS_ERROR: 1128 CLOGE("Device has encountered a serious error"); 1129 return INVALID_OPERATION; 1130 case STATUS_UNINITIALIZED: 1131 CLOGE("Device not initialized"); 1132 return INVALID_OPERATION; 1133 case STATUS_UNCONFIGURED: 1134 case STATUS_CONFIGURED: 1135 case STATUS_ACTIVE: 1136 // OK 1137 break; 1138 default: 1139 SET_ERR_L("Unexpected status: %d", mStatus); 1140 return INVALID_OPERATION; 1141 } 1142 ALOGV("Camera %s: Clearing repeating request", mId.string()); 1143 1144 return mRequestThread->clearRepeatingRequests(lastFrameNumber); 1145} 1146 1147status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { 1148 ATRACE_CALL(); 1149 Mutex::Autolock il(mInterfaceLock); 1150 1151 return mRequestThread->waitUntilRequestProcessed(requestId, timeout); 1152} 1153 1154status_t Camera3Device::createInputStream( 1155 uint32_t width, uint32_t height, int format, int *id) { 1156 ATRACE_CALL(); 1157 Mutex::Autolock il(mInterfaceLock); 1158 nsecs_t maxExpectedDuration = getExpectedInFlightDuration(); 1159 Mutex::Autolock l(mLock); 1160 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d", 1161 mId.string(), mNextStreamId, width, height, format); 1162 1163 status_t res; 1164 bool wasActive = false; 1165 1166 switch (mStatus) { 1167 case STATUS_ERROR: 1168 ALOGE("%s: Device has encountered a serious error", __FUNCTION__); 1169 return INVALID_OPERATION; 1170 case STATUS_UNINITIALIZED: 1171 ALOGE("%s: Device not initialized", __FUNCTION__); 1172 return INVALID_OPERATION; 1173 case STATUS_UNCONFIGURED: 1174 case STATUS_CONFIGURED: 1175 // OK 1176 break; 1177 case STATUS_ACTIVE: 1178 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 1179 res = internalPauseAndWaitLocked(maxExpectedDuration); 1180 if (res != OK) { 1181 SET_ERR_L("Can't pause captures to reconfigure streams!"); 1182 return res; 1183 } 1184 wasActive = true; 1185 break; 1186 default: 1187 SET_ERR_L("%s: Unexpected status: %d", mStatus); 1188 return INVALID_OPERATION; 1189 } 1190 assert(mStatus != STATUS_ACTIVE); 1191 1192 if (mInputStream != 0) { 1193 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); 1194 return INVALID_OPERATION; 1195 } 1196 1197 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, 1198 width, height, format); 1199 newStream->setStatusTracker(mStatusTracker); 1200 1201 mInputStream = newStream; 1202 1203 *id = mNextStreamId++; 1204 1205 // Continue captures if active at start 1206 if (wasActive) { 1207 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 1208 // Reuse current operating mode for new stream config 1209 res = configureStreamsLocked(mOperatingMode); 1210 if (res != OK) { 1211 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", 1212 __FUNCTION__, mNextStreamId, strerror(-res), res); 1213 return res; 1214 } 1215 internalResumeLocked(); 1216 } 1217 1218 ALOGV("Camera %s: Created input stream", mId.string()); 1219 return OK; 1220} 1221 1222status_t Camera3Device::createStream(sp<Surface> consumer, 1223 uint32_t width, uint32_t height, int format, 1224 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id, 1225 int streamSetId, bool isShared, uint64_t consumerUsage) { 1226 ATRACE_CALL(); 1227 1228 if (consumer == nullptr) { 1229 ALOGE("%s: consumer must not be null", __FUNCTION__); 1230 return BAD_VALUE; 1231 } 1232 1233 std::vector<sp<Surface>> consumers; 1234 consumers.push_back(consumer); 1235 1236 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height, 1237 format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage); 1238} 1239 1240status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers, 1241 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format, 1242 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id, 1243 int streamSetId, bool isShared, uint64_t consumerUsage) { 1244 ATRACE_CALL(); 1245 Mutex::Autolock il(mInterfaceLock); 1246 nsecs_t maxExpectedDuration = getExpectedInFlightDuration(); 1247 Mutex::Autolock l(mLock); 1248 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d" 1249 " consumer usage %" PRIu64 ", isShared %d", mId.string(), mNextStreamId, width, height, format, 1250 dataSpace, rotation, consumerUsage, isShared); 1251 1252 status_t res; 1253 bool wasActive = false; 1254 1255 switch (mStatus) { 1256 case STATUS_ERROR: 1257 CLOGE("Device has encountered a serious error"); 1258 return INVALID_OPERATION; 1259 case STATUS_UNINITIALIZED: 1260 CLOGE("Device not initialized"); 1261 return INVALID_OPERATION; 1262 case STATUS_UNCONFIGURED: 1263 case STATUS_CONFIGURED: 1264 // OK 1265 break; 1266 case STATUS_ACTIVE: 1267 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 1268 res = internalPauseAndWaitLocked(maxExpectedDuration); 1269 if (res != OK) { 1270 SET_ERR_L("Can't pause captures to reconfigure streams!"); 1271 return res; 1272 } 1273 wasActive = true; 1274 break; 1275 default: 1276 SET_ERR_L("Unexpected status: %d", mStatus); 1277 return INVALID_OPERATION; 1278 } 1279 assert(mStatus != STATUS_ACTIVE); 1280 1281 sp<Camera3OutputStream> newStream; 1282 1283 if (consumers.size() == 0 && !hasDeferredConsumer) { 1284 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__); 1285 return BAD_VALUE; 1286 } 1287 1288 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { 1289 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format"); 1290 return BAD_VALUE; 1291 } 1292 1293 if (format == HAL_PIXEL_FORMAT_BLOB) { 1294 ssize_t blobBufferSize; 1295 if (dataSpace != HAL_DATASPACE_DEPTH) { 1296 blobBufferSize = getJpegBufferSize(width, height); 1297 if (blobBufferSize <= 0) { 1298 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize); 1299 return BAD_VALUE; 1300 } 1301 } else { 1302 blobBufferSize = getPointCloudBufferSize(); 1303 if (blobBufferSize <= 0) { 1304 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize); 1305 return BAD_VALUE; 1306 } 1307 } 1308 newStream = new Camera3OutputStream(mNextStreamId, consumers[0], 1309 width, height, blobBufferSize, format, dataSpace, rotation, 1310 mTimestampOffset, streamSetId); 1311 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) { 1312 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height); 1313 if (rawOpaqueBufferSize <= 0) { 1314 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize); 1315 return BAD_VALUE; 1316 } 1317 newStream = new Camera3OutputStream(mNextStreamId, consumers[0], 1318 width, height, rawOpaqueBufferSize, format, dataSpace, rotation, 1319 mTimestampOffset, streamSetId); 1320 } else if (isShared) { 1321 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers, 1322 width, height, format, consumerUsage, dataSpace, rotation, 1323 mTimestampOffset, streamSetId); 1324 } else if (consumers.size() == 0 && hasDeferredConsumer) { 1325 newStream = new Camera3OutputStream(mNextStreamId, 1326 width, height, format, consumerUsage, dataSpace, rotation, 1327 mTimestampOffset, streamSetId); 1328 } else { 1329 newStream = new Camera3OutputStream(mNextStreamId, consumers[0], 1330 width, height, format, dataSpace, rotation, 1331 mTimestampOffset, streamSetId); 1332 } 1333 newStream->setStatusTracker(mStatusTracker); 1334 1335 newStream->setBufferManager(mBufferManager); 1336 1337 res = mOutputStreams.add(mNextStreamId, newStream); 1338 if (res < 0) { 1339 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res); 1340 return res; 1341 } 1342 1343 *id = mNextStreamId++; 1344 mNeedConfig = true; 1345 1346 // Continue captures if active at start 1347 if (wasActive) { 1348 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 1349 // Reuse current operating mode for new stream config 1350 res = configureStreamsLocked(mOperatingMode); 1351 if (res != OK) { 1352 CLOGE("Can't reconfigure device for new stream %d: %s (%d)", 1353 mNextStreamId, strerror(-res), res); 1354 return res; 1355 } 1356 internalResumeLocked(); 1357 } 1358 ALOGV("Camera %s: Created new stream", mId.string()); 1359 return OK; 1360} 1361 1362status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) { 1363 ATRACE_CALL(); 1364 if (nullptr == streamInfo) { 1365 return BAD_VALUE; 1366 } 1367 Mutex::Autolock il(mInterfaceLock); 1368 Mutex::Autolock l(mLock); 1369 1370 switch (mStatus) { 1371 case STATUS_ERROR: 1372 CLOGE("Device has encountered a serious error"); 1373 return INVALID_OPERATION; 1374 case STATUS_UNINITIALIZED: 1375 CLOGE("Device not initialized!"); 1376 return INVALID_OPERATION; 1377 case STATUS_UNCONFIGURED: 1378 case STATUS_CONFIGURED: 1379 case STATUS_ACTIVE: 1380 // OK 1381 break; 1382 default: 1383 SET_ERR_L("Unexpected status: %d", mStatus); 1384 return INVALID_OPERATION; 1385 } 1386 1387 ssize_t idx = mOutputStreams.indexOfKey(id); 1388 if (idx == NAME_NOT_FOUND) { 1389 CLOGE("Stream %d is unknown", id); 1390 return idx; 1391 } 1392 1393 streamInfo->width = mOutputStreams[idx]->getWidth(); 1394 streamInfo->height = mOutputStreams[idx]->getHeight(); 1395 streamInfo->format = mOutputStreams[idx]->getFormat(); 1396 streamInfo->dataSpace = mOutputStreams[idx]->getDataSpace(); 1397 streamInfo->formatOverridden = mOutputStreams[idx]->isFormatOverridden(); 1398 streamInfo->originalFormat = mOutputStreams[idx]->getOriginalFormat(); 1399 streamInfo->dataSpaceOverridden = mOutputStreams[idx]->isDataSpaceOverridden(); 1400 streamInfo->originalDataSpace = mOutputStreams[idx]->getOriginalDataSpace(); 1401 return OK; 1402} 1403 1404status_t Camera3Device::setStreamTransform(int id, 1405 int transform) { 1406 ATRACE_CALL(); 1407 Mutex::Autolock il(mInterfaceLock); 1408 Mutex::Autolock l(mLock); 1409 1410 switch (mStatus) { 1411 case STATUS_ERROR: 1412 CLOGE("Device has encountered a serious error"); 1413 return INVALID_OPERATION; 1414 case STATUS_UNINITIALIZED: 1415 CLOGE("Device not initialized"); 1416 return INVALID_OPERATION; 1417 case STATUS_UNCONFIGURED: 1418 case STATUS_CONFIGURED: 1419 case STATUS_ACTIVE: 1420 // OK 1421 break; 1422 default: 1423 SET_ERR_L("Unexpected status: %d", mStatus); 1424 return INVALID_OPERATION; 1425 } 1426 1427 ssize_t idx = mOutputStreams.indexOfKey(id); 1428 if (idx == NAME_NOT_FOUND) { 1429 CLOGE("Stream %d does not exist", 1430 id); 1431 return BAD_VALUE; 1432 } 1433 1434 return mOutputStreams.editValueAt(idx)->setTransform(transform); 1435} 1436 1437status_t Camera3Device::deleteStream(int id) { 1438 ATRACE_CALL(); 1439 Mutex::Autolock il(mInterfaceLock); 1440 Mutex::Autolock l(mLock); 1441 status_t res; 1442 1443 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id); 1444 1445 // CameraDevice semantics require device to already be idle before 1446 // deleteStream is called, unlike for createStream. 1447 if (mStatus == STATUS_ACTIVE) { 1448 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string()); 1449 return -EBUSY; 1450 } 1451 1452 if (mStatus == STATUS_ERROR) { 1453 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state", 1454 __FUNCTION__, mId.string()); 1455 return -EBUSY; 1456 } 1457 1458 sp<Camera3StreamInterface> deletedStream; 1459 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id); 1460 if (mInputStream != NULL && id == mInputStream->getId()) { 1461 deletedStream = mInputStream; 1462 mInputStream.clear(); 1463 } else { 1464 if (outputStreamIdx == NAME_NOT_FOUND) { 1465 CLOGE("Stream %d does not exist", id); 1466 return BAD_VALUE; 1467 } 1468 } 1469 1470 // Delete output stream or the output part of a bi-directional stream. 1471 if (outputStreamIdx != NAME_NOT_FOUND) { 1472 deletedStream = mOutputStreams.editValueAt(outputStreamIdx); 1473 mOutputStreams.removeItem(id); 1474 } 1475 1476 // Free up the stream endpoint so that it can be used by some other stream 1477 res = deletedStream->disconnect(); 1478 if (res != OK) { 1479 SET_ERR_L("Can't disconnect deleted stream %d", id); 1480 // fall through since we want to still list the stream as deleted. 1481 } 1482 mDeletedStreams.add(deletedStream); 1483 mNeedConfig = true; 1484 1485 return res; 1486} 1487 1488status_t Camera3Device::configureStreams(int operatingMode) { 1489 ATRACE_CALL(); 1490 ALOGV("%s: E", __FUNCTION__); 1491 1492 Mutex::Autolock il(mInterfaceLock); 1493 Mutex::Autolock l(mLock); 1494 1495 return configureStreamsLocked(operatingMode); 1496} 1497 1498status_t Camera3Device::getInputBufferProducer( 1499 sp<IGraphicBufferProducer> *producer) { 1500 ATRACE_CALL(); 1501 Mutex::Autolock il(mInterfaceLock); 1502 Mutex::Autolock l(mLock); 1503 1504 if (producer == NULL) { 1505 return BAD_VALUE; 1506 } else if (mInputStream == NULL) { 1507 return INVALID_OPERATION; 1508 } 1509 1510 return mInputStream->getInputBufferProducer(producer); 1511} 1512 1513status_t Camera3Device::createDefaultRequest(int templateId, 1514 CameraMetadata *request) { 1515 ATRACE_CALL(); 1516 ALOGV("%s: for template %d", __FUNCTION__, templateId); 1517 1518 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) { 1519 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110", 1520 IPCThreadState::self()->getCallingUid(), nullptr, 0); 1521 return BAD_VALUE; 1522 } 1523 1524 Mutex::Autolock il(mInterfaceLock); 1525 1526 { 1527 Mutex::Autolock l(mLock); 1528 switch (mStatus) { 1529 case STATUS_ERROR: 1530 CLOGE("Device has encountered a serious error"); 1531 return INVALID_OPERATION; 1532 case STATUS_UNINITIALIZED: 1533 CLOGE("Device is not initialized!"); 1534 return INVALID_OPERATION; 1535 case STATUS_UNCONFIGURED: 1536 case STATUS_CONFIGURED: 1537 case STATUS_ACTIVE: 1538 // OK 1539 break; 1540 default: 1541 SET_ERR_L("Unexpected status: %d", mStatus); 1542 return INVALID_OPERATION; 1543 } 1544 1545 if (!mRequestTemplateCache[templateId].isEmpty()) { 1546 *request = mRequestTemplateCache[templateId]; 1547 return OK; 1548 } 1549 } 1550 1551 camera_metadata_t *rawRequest; 1552 status_t res = mInterface->constructDefaultRequestSettings( 1553 (camera3_request_template_t) templateId, &rawRequest); 1554 1555 { 1556 Mutex::Autolock l(mLock); 1557 if (res == BAD_VALUE) { 1558 ALOGI("%s: template %d is not supported on this camera device", 1559 __FUNCTION__, templateId); 1560 return res; 1561 } else if (res != OK) { 1562 CLOGE("Unable to construct request template %d: %s (%d)", 1563 templateId, strerror(-res), res); 1564 return res; 1565 } 1566 1567 set_camera_metadata_vendor_id(rawRequest, mVendorTagId); 1568 mRequestTemplateCache[templateId].acquire(rawRequest); 1569 1570 *request = mRequestTemplateCache[templateId]; 1571 } 1572 return OK; 1573} 1574 1575status_t Camera3Device::waitUntilDrained() { 1576 ATRACE_CALL(); 1577 Mutex::Autolock il(mInterfaceLock); 1578 nsecs_t maxExpectedDuration = getExpectedInFlightDuration(); 1579 Mutex::Autolock l(mLock); 1580 1581 return waitUntilDrainedLocked(maxExpectedDuration); 1582} 1583 1584status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) { 1585 switch (mStatus) { 1586 case STATUS_UNINITIALIZED: 1587 case STATUS_UNCONFIGURED: 1588 ALOGV("%s: Already idle", __FUNCTION__); 1589 return OK; 1590 case STATUS_CONFIGURED: 1591 // To avoid race conditions, check with tracker to be sure 1592 case STATUS_ERROR: 1593 case STATUS_ACTIVE: 1594 // Need to verify shut down 1595 break; 1596 default: 1597 SET_ERR_L("Unexpected status: %d",mStatus); 1598 return INVALID_OPERATION; 1599 } 1600 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(), 1601 maxExpectedDuration); 1602 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration); 1603 if (res != OK) { 1604 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res), 1605 res); 1606 } 1607 return res; 1608} 1609 1610 1611void Camera3Device::internalUpdateStatusLocked(Status status) { 1612 mStatus = status; 1613 mRecentStatusUpdates.add(mStatus); 1614 mStatusChanged.broadcast(); 1615} 1616 1617// Pause to reconfigure 1618status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) { 1619 mRequestThread->setPaused(true); 1620 mPauseStateNotify = true; 1621 1622 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(), 1623 maxExpectedDuration); 1624 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration); 1625 if (res != OK) { 1626 SET_ERR_L("Can't idle device in %f seconds!", 1627 maxExpectedDuration/1e9); 1628 } 1629 1630 return res; 1631} 1632 1633// Resume after internalPauseAndWaitLocked 1634status_t Camera3Device::internalResumeLocked() { 1635 status_t res; 1636 1637 mRequestThread->setPaused(false); 1638 1639 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); 1640 if (res != OK) { 1641 SET_ERR_L("Can't transition to active in %f seconds!", 1642 kActiveTimeout/1e9); 1643 } 1644 mPauseStateNotify = false; 1645 return OK; 1646} 1647 1648status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) { 1649 status_t res = OK; 1650 1651 size_t startIndex = 0; 1652 if (mStatusWaiters == 0) { 1653 // Clear the list of recent statuses if there are no existing threads waiting on updates to 1654 // this status list 1655 mRecentStatusUpdates.clear(); 1656 } else { 1657 // If other threads are waiting on updates to this status list, set the position of the 1658 // first element that this list will check rather than clearing the list. 1659 startIndex = mRecentStatusUpdates.size(); 1660 } 1661 1662 mStatusWaiters++; 1663 1664 bool stateSeen = false; 1665 do { 1666 if (active == (mStatus == STATUS_ACTIVE)) { 1667 // Desired state is current 1668 break; 1669 } 1670 1671 res = mStatusChanged.waitRelative(mLock, timeout); 1672 if (res != OK) break; 1673 1674 // This is impossible, but if not, could result in subtle deadlocks and invalid state 1675 // transitions. 1676 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(), 1677 "%s: Skipping status updates in Camera3Device, may result in deadlock.", 1678 __FUNCTION__); 1679 1680 // Encountered desired state since we began waiting 1681 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) { 1682 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) { 1683 stateSeen = true; 1684 break; 1685 } 1686 } 1687 } while (!stateSeen); 1688 1689 mStatusWaiters--; 1690 1691 return res; 1692} 1693 1694 1695status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) { 1696 ATRACE_CALL(); 1697 Mutex::Autolock l(mOutputLock); 1698 1699 if (listener != NULL && mListener != NULL) { 1700 ALOGW("%s: Replacing old callback listener", __FUNCTION__); 1701 } 1702 mListener = listener; 1703 mRequestThread->setNotificationListener(listener); 1704 mPreparerThread->setNotificationListener(listener); 1705 1706 return OK; 1707} 1708 1709bool Camera3Device::willNotify3A() { 1710 return false; 1711} 1712 1713status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { 1714 ATRACE_CALL(); 1715 status_t res; 1716 Mutex::Autolock l(mOutputLock); 1717 1718 while (mResultQueue.empty()) { 1719 res = mResultSignal.waitRelative(mOutputLock, timeout); 1720 if (res == TIMED_OUT) { 1721 return res; 1722 } else if (res != OK) { 1723 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)", 1724 __FUNCTION__, mId.string(), timeout, strerror(-res), res); 1725 return res; 1726 } 1727 } 1728 return OK; 1729} 1730 1731status_t Camera3Device::getNextResult(CaptureResult *frame) { 1732 ATRACE_CALL(); 1733 Mutex::Autolock l(mOutputLock); 1734 1735 if (mResultQueue.empty()) { 1736 return NOT_ENOUGH_DATA; 1737 } 1738 1739 if (frame == NULL) { 1740 ALOGE("%s: argument cannot be NULL", __FUNCTION__); 1741 return BAD_VALUE; 1742 } 1743 1744 CaptureResult &result = *(mResultQueue.begin()); 1745 frame->mResultExtras = result.mResultExtras; 1746 frame->mMetadata.acquire(result.mMetadata); 1747 mResultQueue.erase(mResultQueue.begin()); 1748 1749 return OK; 1750} 1751 1752status_t Camera3Device::triggerAutofocus(uint32_t id) { 1753 ATRACE_CALL(); 1754 Mutex::Autolock il(mInterfaceLock); 1755 1756 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); 1757 // Mix-in this trigger into the next request and only the next request. 1758 RequestTrigger trigger[] = { 1759 { 1760 ANDROID_CONTROL_AF_TRIGGER, 1761 ANDROID_CONTROL_AF_TRIGGER_START 1762 }, 1763 { 1764 ANDROID_CONTROL_AF_TRIGGER_ID, 1765 static_cast<int32_t>(id) 1766 } 1767 }; 1768 1769 return mRequestThread->queueTrigger(trigger, 1770 sizeof(trigger)/sizeof(trigger[0])); 1771} 1772 1773status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { 1774 ATRACE_CALL(); 1775 Mutex::Autolock il(mInterfaceLock); 1776 1777 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); 1778 // Mix-in this trigger into the next request and only the next request. 1779 RequestTrigger trigger[] = { 1780 { 1781 ANDROID_CONTROL_AF_TRIGGER, 1782 ANDROID_CONTROL_AF_TRIGGER_CANCEL 1783 }, 1784 { 1785 ANDROID_CONTROL_AF_TRIGGER_ID, 1786 static_cast<int32_t>(id) 1787 } 1788 }; 1789 1790 return mRequestThread->queueTrigger(trigger, 1791 sizeof(trigger)/sizeof(trigger[0])); 1792} 1793 1794status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { 1795 ATRACE_CALL(); 1796 Mutex::Autolock il(mInterfaceLock); 1797 1798 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); 1799 // Mix-in this trigger into the next request and only the next request. 1800 RequestTrigger trigger[] = { 1801 { 1802 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, 1803 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START 1804 }, 1805 { 1806 ANDROID_CONTROL_AE_PRECAPTURE_ID, 1807 static_cast<int32_t>(id) 1808 } 1809 }; 1810 1811 return mRequestThread->queueTrigger(trigger, 1812 sizeof(trigger)/sizeof(trigger[0])); 1813} 1814 1815status_t Camera3Device::flush(int64_t *frameNumber) { 1816 ATRACE_CALL(); 1817 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string()); 1818 Mutex::Autolock il(mInterfaceLock); 1819 1820 { 1821 Mutex::Autolock l(mLock); 1822 mRequestThread->clear(/*out*/frameNumber); 1823 } 1824 1825 return mRequestThread->flush(); 1826} 1827 1828status_t Camera3Device::prepare(int streamId) { 1829 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId); 1830} 1831 1832status_t Camera3Device::prepare(int maxCount, int streamId) { 1833 ATRACE_CALL(); 1834 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId); 1835 Mutex::Autolock il(mInterfaceLock); 1836 Mutex::Autolock l(mLock); 1837 1838 sp<Camera3StreamInterface> stream; 1839 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); 1840 if (outputStreamIdx == NAME_NOT_FOUND) { 1841 CLOGE("Stream %d does not exist", streamId); 1842 return BAD_VALUE; 1843 } 1844 1845 stream = mOutputStreams.editValueAt(outputStreamIdx); 1846 1847 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) { 1848 CLOGE("Stream %d has already been a request target", streamId); 1849 return BAD_VALUE; 1850 } 1851 1852 if (mRequestThread->isStreamPending(stream)) { 1853 CLOGE("Stream %d is already a target in a pending request", streamId); 1854 return BAD_VALUE; 1855 } 1856 1857 return mPreparerThread->prepare(maxCount, stream); 1858} 1859 1860status_t Camera3Device::tearDown(int streamId) { 1861 ATRACE_CALL(); 1862 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId); 1863 Mutex::Autolock il(mInterfaceLock); 1864 Mutex::Autolock l(mLock); 1865 1866 sp<Camera3StreamInterface> stream; 1867 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); 1868 if (outputStreamIdx == NAME_NOT_FOUND) { 1869 CLOGE("Stream %d does not exist", streamId); 1870 return BAD_VALUE; 1871 } 1872 1873 stream = mOutputStreams.editValueAt(outputStreamIdx); 1874 1875 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) { 1876 CLOGE("Stream %d is a target of a in-progress request", streamId); 1877 return BAD_VALUE; 1878 } 1879 1880 return stream->tearDown(); 1881} 1882 1883status_t Camera3Device::addBufferListenerForStream(int streamId, 1884 wp<Camera3StreamBufferListener> listener) { 1885 ATRACE_CALL(); 1886 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId); 1887 Mutex::Autolock il(mInterfaceLock); 1888 Mutex::Autolock l(mLock); 1889 1890 sp<Camera3StreamInterface> stream; 1891 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); 1892 if (outputStreamIdx == NAME_NOT_FOUND) { 1893 CLOGE("Stream %d does not exist", streamId); 1894 return BAD_VALUE; 1895 } 1896 1897 stream = mOutputStreams.editValueAt(outputStreamIdx); 1898 stream->addBufferListener(listener); 1899 1900 return OK; 1901} 1902 1903/** 1904 * Methods called by subclasses 1905 */ 1906 1907void Camera3Device::notifyStatus(bool idle) { 1908 ATRACE_CALL(); 1909 { 1910 // Need mLock to safely update state and synchronize to current 1911 // state of methods in flight. 1912 Mutex::Autolock l(mLock); 1913 // We can get various system-idle notices from the status tracker 1914 // while starting up. Only care about them if we've actually sent 1915 // in some requests recently. 1916 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) { 1917 return; 1918 } 1919 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(), 1920 idle ? "idle" : "active"); 1921 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE); 1922 1923 // Skip notifying listener if we're doing some user-transparent 1924 // state changes 1925 if (mPauseStateNotify) return; 1926 } 1927 1928 sp<NotificationListener> listener; 1929 { 1930 Mutex::Autolock l(mOutputLock); 1931 listener = mListener.promote(); 1932 } 1933 if (idle && listener != NULL) { 1934 listener->notifyIdle(); 1935 } 1936} 1937 1938status_t Camera3Device::setConsumerSurfaces(int streamId, 1939 const std::vector<sp<Surface>>& consumers) { 1940 ATRACE_CALL(); 1941 ALOGV("%s: Camera %s: set consumer surface for stream %d", 1942 __FUNCTION__, mId.string(), streamId); 1943 Mutex::Autolock il(mInterfaceLock); 1944 Mutex::Autolock l(mLock); 1945 1946 if (consumers.size() == 0) { 1947 CLOGE("No consumer is passed!"); 1948 return BAD_VALUE; 1949 } 1950 1951 ssize_t idx = mOutputStreams.indexOfKey(streamId); 1952 if (idx == NAME_NOT_FOUND) { 1953 CLOGE("Stream %d is unknown", streamId); 1954 return idx; 1955 } 1956 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx]; 1957 status_t res = stream->setConsumers(consumers); 1958 if (res != OK) { 1959 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res)); 1960 return res; 1961 } 1962 1963 if (stream->isConsumerConfigurationDeferred()) { 1964 if (!stream->isConfiguring()) { 1965 CLOGE("Stream %d was already fully configured.", streamId); 1966 return INVALID_OPERATION; 1967 } 1968 1969 res = stream->finishConfiguration(); 1970 if (res != OK) { 1971 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", 1972 stream->getId(), strerror(-res), res); 1973 return res; 1974 } 1975 } 1976 1977 return OK; 1978} 1979 1980status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) { 1981 Mutex::Autolock il(mInterfaceLock); 1982 Mutex::Autolock l(mLock); 1983 1984 int idx = mOutputStreams.indexOfKey(streamId); 1985 if (idx == NAME_NOT_FOUND) { 1986 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId); 1987 return BAD_VALUE; 1988 } 1989 1990 sp<Camera3OutputStreamInterface> stream = mOutputStreams.editValueAt(idx); 1991 return stream->dropBuffers(dropping); 1992} 1993 1994/** 1995 * Camera3Device private methods 1996 */ 1997 1998sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( 1999 const CameraMetadata &request, const SurfaceMap &surfaceMap) { 2000 ATRACE_CALL(); 2001 status_t res; 2002 2003 sp<CaptureRequest> newRequest = new CaptureRequest; 2004 newRequest->mSettings = request; 2005 2006 camera_metadata_entry_t inputStreams = 2007 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); 2008 if (inputStreams.count > 0) { 2009 if (mInputStream == NULL || 2010 mInputStream->getId() != inputStreams.data.i32[0]) { 2011 CLOGE("Request references unknown input stream %d", 2012 inputStreams.data.u8[0]); 2013 return NULL; 2014 } 2015 // Lazy completion of stream configuration (allocation/registration) 2016 // on first use 2017 if (mInputStream->isConfiguring()) { 2018 res = mInputStream->finishConfiguration(); 2019 if (res != OK) { 2020 SET_ERR_L("Unable to finish configuring input stream %d:" 2021 " %s (%d)", 2022 mInputStream->getId(), strerror(-res), res); 2023 return NULL; 2024 } 2025 } 2026 // Check if stream is being prepared 2027 if (mInputStream->isPreparing()) { 2028 CLOGE("Request references an input stream that's being prepared!"); 2029 return NULL; 2030 } 2031 2032 newRequest->mInputStream = mInputStream; 2033 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); 2034 } 2035 2036 camera_metadata_entry_t streams = 2037 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); 2038 if (streams.count == 0) { 2039 CLOGE("Zero output streams specified!"); 2040 return NULL; 2041 } 2042 2043 for (size_t i = 0; i < streams.count; i++) { 2044 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]); 2045 if (idx == NAME_NOT_FOUND) { 2046 CLOGE("Request references unknown stream %d", 2047 streams.data.u8[i]); 2048 return NULL; 2049 } 2050 sp<Camera3OutputStreamInterface> stream = 2051 mOutputStreams.editValueAt(idx); 2052 2053 // It is illegal to include a deferred consumer output stream into a request 2054 auto iter = surfaceMap.find(streams.data.i32[i]); 2055 if (iter != surfaceMap.end()) { 2056 const std::vector<size_t>& surfaces = iter->second; 2057 for (const auto& surface : surfaces) { 2058 if (stream->isConsumerConfigurationDeferred(surface)) { 2059 CLOGE("Stream %d surface %zu hasn't finished configuration yet " 2060 "due to deferred consumer", stream->getId(), surface); 2061 return NULL; 2062 } 2063 } 2064 newRequest->mOutputSurfaces[i] = surfaces; 2065 } 2066 2067 // Lazy completion of stream configuration (allocation/registration) 2068 // on first use 2069 if (stream->isConfiguring()) { 2070 res = stream->finishConfiguration(); 2071 if (res != OK) { 2072 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", 2073 stream->getId(), strerror(-res), res); 2074 return NULL; 2075 } 2076 } 2077 // Check if stream is being prepared 2078 if (stream->isPreparing()) { 2079 CLOGE("Request references an output stream that's being prepared!"); 2080 return NULL; 2081 } 2082 2083 newRequest->mOutputStreams.push(stream); 2084 } 2085 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); 2086 newRequest->mBatchSize = 1; 2087 2088 return newRequest; 2089} 2090 2091bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) { 2092 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) { 2093 Size size = mSupportedOpaqueInputSizes[i]; 2094 if (size.width == width && size.height == height) { 2095 return true; 2096 } 2097 } 2098 2099 return false; 2100} 2101 2102void Camera3Device::cancelStreamsConfigurationLocked() { 2103 int res = OK; 2104 if (mInputStream != NULL && mInputStream->isConfiguring()) { 2105 res = mInputStream->cancelConfiguration(); 2106 if (res != OK) { 2107 CLOGE("Can't cancel configuring input stream %d: %s (%d)", 2108 mInputStream->getId(), strerror(-res), res); 2109 } 2110 } 2111 2112 for (size_t i = 0; i < mOutputStreams.size(); i++) { 2113 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i); 2114 if (outputStream->isConfiguring()) { 2115 res = outputStream->cancelConfiguration(); 2116 if (res != OK) { 2117 CLOGE("Can't cancel configuring output stream %d: %s (%d)", 2118 outputStream->getId(), strerror(-res), res); 2119 } 2120 } 2121 } 2122 2123 // Return state to that at start of call, so that future configures 2124 // properly clean things up 2125 internalUpdateStatusLocked(STATUS_UNCONFIGURED); 2126 mNeedConfig = true; 2127} 2128 2129status_t Camera3Device::configureStreamsLocked(int operatingMode) { 2130 ATRACE_CALL(); 2131 status_t res; 2132 2133 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) { 2134 CLOGE("Not idle"); 2135 return INVALID_OPERATION; 2136 } 2137 2138 if (operatingMode < 0) { 2139 CLOGE("Invalid operating mode: %d", operatingMode); 2140 return BAD_VALUE; 2141 } 2142 2143 bool isConstrainedHighSpeed = 2144 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) == 2145 operatingMode; 2146 2147 if (mOperatingMode != operatingMode) { 2148 mNeedConfig = true; 2149 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed; 2150 mOperatingMode = operatingMode; 2151 } 2152 2153 if (!mNeedConfig) { 2154 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); 2155 return OK; 2156 } 2157 2158 // Workaround for device HALv3.2 or older spec bug - zero streams requires 2159 // adding a dummy stream instead. 2160 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround. 2161 if (mOutputStreams.size() == 0) { 2162 addDummyStreamLocked(); 2163 } else { 2164 tryRemoveDummyStreamLocked(); 2165 } 2166 2167 // Start configuring the streams 2168 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string()); 2169 2170 camera3_stream_configuration config; 2171 config.operation_mode = mOperatingMode; 2172 config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); 2173 2174 Vector<camera3_stream_t*> streams; 2175 streams.setCapacity(config.num_streams); 2176 2177 if (mInputStream != NULL) { 2178 camera3_stream_t *inputStream; 2179 inputStream = mInputStream->startConfiguration(); 2180 if (inputStream == NULL) { 2181 CLOGE("Can't start input stream configuration"); 2182 cancelStreamsConfigurationLocked(); 2183 return INVALID_OPERATION; 2184 } 2185 streams.add(inputStream); 2186 } 2187 2188 for (size_t i = 0; i < mOutputStreams.size(); i++) { 2189 2190 // Don't configure bidi streams twice, nor add them twice to the list 2191 if (mOutputStreams[i].get() == 2192 static_cast<Camera3StreamInterface*>(mInputStream.get())) { 2193 2194 config.num_streams--; 2195 continue; 2196 } 2197 2198 camera3_stream_t *outputStream; 2199 outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); 2200 if (outputStream == NULL) { 2201 CLOGE("Can't start output stream configuration"); 2202 cancelStreamsConfigurationLocked(); 2203 return INVALID_OPERATION; 2204 } 2205 streams.add(outputStream); 2206 } 2207 2208 config.streams = streams.editArray(); 2209 2210 // Do the HAL configuration; will potentially touch stream 2211 // max_buffers, usage, priv fields. 2212 2213 res = mInterface->configureStreams(&config); 2214 2215 if (res == BAD_VALUE) { 2216 // HAL rejected this set of streams as unsupported, clean up config 2217 // attempt and return to unconfigured state 2218 CLOGE("Set of requested inputs/outputs not supported by HAL"); 2219 cancelStreamsConfigurationLocked(); 2220 return BAD_VALUE; 2221 } else if (res != OK) { 2222 // Some other kind of error from configure_streams - this is not 2223 // expected 2224 SET_ERR_L("Unable to configure streams with HAL: %s (%d)", 2225 strerror(-res), res); 2226 return res; 2227 } 2228 2229 // Finish all stream configuration immediately. 2230 // TODO: Try to relax this later back to lazy completion, which should be 2231 // faster 2232 2233 if (mInputStream != NULL && mInputStream->isConfiguring()) { 2234 res = mInputStream->finishConfiguration(); 2235 if (res != OK) { 2236 CLOGE("Can't finish configuring input stream %d: %s (%d)", 2237 mInputStream->getId(), strerror(-res), res); 2238 cancelStreamsConfigurationLocked(); 2239 return BAD_VALUE; 2240 } 2241 } 2242 2243 for (size_t i = 0; i < mOutputStreams.size(); i++) { 2244 sp<Camera3OutputStreamInterface> outputStream = 2245 mOutputStreams.editValueAt(i); 2246 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) { 2247 res = outputStream->finishConfiguration(); 2248 if (res != OK) { 2249 CLOGE("Can't finish configuring output stream %d: %s (%d)", 2250 outputStream->getId(), strerror(-res), res); 2251 cancelStreamsConfigurationLocked(); 2252 return BAD_VALUE; 2253 } 2254 } 2255 } 2256 2257 // Request thread needs to know to avoid using repeat-last-settings protocol 2258 // across configure_streams() calls 2259 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration); 2260 2261 char value[PROPERTY_VALUE_MAX]; 2262 property_get("camera.fifo.disable", value, "0"); 2263 int32_t disableFifo = atoi(value); 2264 if (disableFifo != 1) { 2265 // Boost priority of request thread to SCHED_FIFO. 2266 pid_t requestThreadTid = mRequestThread->getTid(); 2267 res = requestPriority(getpid(), requestThreadTid, 2268 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false); 2269 if (res != OK) { 2270 ALOGW("Can't set realtime priority for request processing thread: %s (%d)", 2271 strerror(-res), res); 2272 } else { 2273 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid); 2274 } 2275 } 2276 2277 // Update device state 2278 2279 mNeedConfig = false; 2280 2281 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ? 2282 STATUS_CONFIGURED : STATUS_UNCONFIGURED); 2283 2284 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string()); 2285 2286 // tear down the deleted streams after configure streams. 2287 mDeletedStreams.clear(); 2288 2289 return OK; 2290} 2291 2292status_t Camera3Device::addDummyStreamLocked() { 2293 ATRACE_CALL(); 2294 status_t res; 2295 2296 if (mDummyStreamId != NO_STREAM) { 2297 // Should never be adding a second dummy stream when one is already 2298 // active 2299 SET_ERR_L("%s: Camera %s: A dummy stream already exists!", 2300 __FUNCTION__, mId.string()); 2301 return INVALID_OPERATION; 2302 } 2303 2304 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string()); 2305 2306 sp<Camera3OutputStreamInterface> dummyStream = 2307 new Camera3DummyStream(mNextStreamId); 2308 2309 res = mOutputStreams.add(mNextStreamId, dummyStream); 2310 if (res < 0) { 2311 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res); 2312 return res; 2313 } 2314 2315 mDummyStreamId = mNextStreamId; 2316 mNextStreamId++; 2317 2318 return OK; 2319} 2320 2321status_t Camera3Device::tryRemoveDummyStreamLocked() { 2322 ATRACE_CALL(); 2323 status_t res; 2324 2325 if (mDummyStreamId == NO_STREAM) return OK; 2326 if (mOutputStreams.size() == 1) return OK; 2327 2328 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string()); 2329 2330 // Ok, have a dummy stream and there's at least one other output stream, 2331 // so remove the dummy 2332 2333 sp<Camera3StreamInterface> deletedStream; 2334 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId); 2335 if (outputStreamIdx == NAME_NOT_FOUND) { 2336 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId); 2337 return INVALID_OPERATION; 2338 } 2339 2340 deletedStream = mOutputStreams.editValueAt(outputStreamIdx); 2341 mOutputStreams.removeItemsAt(outputStreamIdx); 2342 2343 // Free up the stream endpoint so that it can be used by some other stream 2344 res = deletedStream->disconnect(); 2345 if (res != OK) { 2346 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId); 2347 // fall through since we want to still list the stream as deleted. 2348 } 2349 mDeletedStreams.add(deletedStream); 2350 mDummyStreamId = NO_STREAM; 2351 2352 return res; 2353} 2354 2355void Camera3Device::setErrorState(const char *fmt, ...) { 2356 ATRACE_CALL(); 2357 Mutex::Autolock l(mLock); 2358 va_list args; 2359 va_start(args, fmt); 2360 2361 setErrorStateLockedV(fmt, args); 2362 2363 va_end(args); 2364} 2365 2366void Camera3Device::setErrorStateV(const char *fmt, va_list args) { 2367 ATRACE_CALL(); 2368 Mutex::Autolock l(mLock); 2369 setErrorStateLockedV(fmt, args); 2370} 2371 2372void Camera3Device::setErrorStateLocked(const char *fmt, ...) { 2373 va_list args; 2374 va_start(args, fmt); 2375 2376 setErrorStateLockedV(fmt, args); 2377 2378 va_end(args); 2379} 2380 2381void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { 2382 // Print out all error messages to log 2383 String8 errorCause = String8::formatV(fmt, args); 2384 ALOGE("Camera %s: %s", mId.string(), errorCause.string()); 2385 2386 // But only do error state transition steps for the first error 2387 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; 2388 2389 mErrorCause = errorCause; 2390 2391 if (mRequestThread != nullptr) { 2392 mRequestThread->setPaused(true); 2393 } 2394 internalUpdateStatusLocked(STATUS_ERROR); 2395 2396 // Notify upstream about a device error 2397 sp<NotificationListener> listener = mListener.promote(); 2398 if (listener != NULL) { 2399 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, 2400 CaptureResultExtras()); 2401 } 2402 2403 // Save stack trace. View by dumping it later. 2404 CameraTraces::saveTrace(); 2405 // TODO: consider adding errorCause and client pid/procname 2406} 2407 2408/** 2409 * In-flight request management 2410 */ 2411 2412status_t Camera3Device::registerInFlight(uint32_t frameNumber, 2413 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput, 2414 bool hasAppCallback, nsecs_t maxExpectedDuration) { 2415 ATRACE_CALL(); 2416 Mutex::Autolock l(mInFlightLock); 2417 2418 ssize_t res; 2419 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput, 2420 hasAppCallback, maxExpectedDuration)); 2421 if (res < 0) return res; 2422 2423 if (mInFlightMap.size() == 1) { 2424 // hold mLock to prevent race with disconnect 2425 Mutex::Autolock l(mLock); 2426 if (mStatusTracker != nullptr) { 2427 mStatusTracker->markComponentActive(mInFlightStatusId); 2428 } 2429 } 2430 2431 mExpectedInflightDuration += maxExpectedDuration; 2432 return OK; 2433} 2434 2435void Camera3Device::returnOutputBuffers( 2436 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers, 2437 nsecs_t timestamp) { 2438 for (size_t i = 0; i < numBuffers; i++) 2439 { 2440 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream); 2441 status_t res = stream->returnBuffer(outputBuffers[i], timestamp); 2442 // Note: stream may be deallocated at this point, if this buffer was 2443 // the last reference to it. 2444 if (res != OK) { 2445 ALOGE("Can't return buffer to its stream: %s (%d)", 2446 strerror(-res), res); 2447 } 2448 } 2449} 2450 2451void Camera3Device::removeInFlightMapEntryLocked(int idx) { 2452 ATRACE_CALL(); 2453 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration; 2454 mInFlightMap.removeItemsAt(idx, 1); 2455 2456 // Indicate idle inFlightMap to the status tracker 2457 if (mInFlightMap.size() == 0) { 2458 // hold mLock to prevent race with disconnect 2459 Mutex::Autolock l(mLock); 2460 if (mStatusTracker != nullptr) { 2461 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE); 2462 } 2463 } 2464 mExpectedInflightDuration -= duration; 2465} 2466 2467void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) { 2468 2469 const InFlightRequest &request = mInFlightMap.valueAt(idx); 2470 const uint32_t frameNumber = mInFlightMap.keyAt(idx); 2471 2472 nsecs_t sensorTimestamp = request.sensorTimestamp; 2473 nsecs_t shutterTimestamp = request.shutterTimestamp; 2474 2475 // Check if it's okay to remove the request from InFlightMap: 2476 // In the case of a successful request: 2477 // all input and output buffers, all result metadata, shutter callback 2478 // arrived. 2479 // In the case of a unsuccessful request: 2480 // all input and output buffers arrived. 2481 if (request.numBuffersLeft == 0 && 2482 (request.skipResultMetadata || 2483 (request.haveResultMetadata && shutterTimestamp != 0))) { 2484 ATRACE_ASYNC_END("frame capture", frameNumber); 2485 2486 // Sanity check - if sensor timestamp matches shutter timestamp in the 2487 // case of request having callback. 2488 if (request.hasCallback && request.requestStatus == OK && 2489 sensorTimestamp != shutterTimestamp) { 2490 SET_ERR("sensor timestamp (%" PRId64 2491 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", 2492 sensorTimestamp, frameNumber, shutterTimestamp); 2493 } 2494 2495 // for an unsuccessful request, it may have pending output buffers to 2496 // return. 2497 assert(request.requestStatus != OK || 2498 request.pendingOutputBuffers.size() == 0); 2499 returnOutputBuffers(request.pendingOutputBuffers.array(), 2500 request.pendingOutputBuffers.size(), 0); 2501 2502 removeInFlightMapEntryLocked(idx); 2503 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); 2504 } 2505 2506 // Sanity check - if we have too many in-flight frames, something has 2507 // likely gone wrong 2508 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) { 2509 CLOGE("In-flight list too large: %zu", mInFlightMap.size()); 2510 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > 2511 kInFlightWarnLimitHighSpeed) { 2512 CLOGE("In-flight list too large for high speed configuration: %zu", 2513 mInFlightMap.size()); 2514 } 2515} 2516 2517void Camera3Device::flushInflightRequests() { 2518 ATRACE_CALL(); 2519 { // First return buffers cached in mInFlightMap 2520 Mutex::Autolock l(mInFlightLock); 2521 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) { 2522 const InFlightRequest &request = mInFlightMap.valueAt(idx); 2523 returnOutputBuffers(request.pendingOutputBuffers.array(), 2524 request.pendingOutputBuffers.size(), 0); 2525 } 2526 mInFlightMap.clear(); 2527 mExpectedInflightDuration = 0; 2528 } 2529 2530 // Then return all inflight buffers not returned by HAL 2531 std::vector<std::pair<int32_t, int32_t>> inflightKeys; 2532 mInterface->getInflightBufferKeys(&inflightKeys); 2533 2534 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1; 2535 for (auto& pair : inflightKeys) { 2536 int32_t frameNumber = pair.first; 2537 int32_t streamId = pair.second; 2538 buffer_handle_t* buffer; 2539 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer); 2540 if (res != OK) { 2541 ALOGE("%s: Frame %d: No in-flight buffer for stream %d", 2542 __FUNCTION__, frameNumber, streamId); 2543 continue; 2544 } 2545 2546 camera3_stream_buffer_t streamBuffer; 2547 streamBuffer.buffer = buffer; 2548 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; 2549 streamBuffer.acquire_fence = -1; 2550 streamBuffer.release_fence = -1; 2551 2552 // First check if the buffer belongs to deleted stream 2553 bool streamDeleted = false; 2554 for (auto& stream : mDeletedStreams) { 2555 if (streamId == stream->getId()) { 2556 streamDeleted = true; 2557 // Return buffer to deleted stream 2558 camera3_stream* halStream = stream->asHalStream(); 2559 streamBuffer.stream = halStream; 2560 switch (halStream->stream_type) { 2561 case CAMERA3_STREAM_OUTPUT: 2562 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0); 2563 if (res != OK) { 2564 ALOGE("%s: Can't return output buffer for frame %d to" 2565 " stream %d: %s (%d)", __FUNCTION__, 2566 frameNumber, streamId, strerror(-res), res); 2567 } 2568 break; 2569 case CAMERA3_STREAM_INPUT: 2570 res = stream->returnInputBuffer(streamBuffer); 2571 if (res != OK) { 2572 ALOGE("%s: Can't return input buffer for frame %d to" 2573 " stream %d: %s (%d)", __FUNCTION__, 2574 frameNumber, streamId, strerror(-res), res); 2575 } 2576 break; 2577 default: // Bi-direcitonal stream is deprecated 2578 ALOGE("%s: stream %d has unknown stream type %d", 2579 __FUNCTION__, streamId, halStream->stream_type); 2580 break; 2581 } 2582 break; 2583 } 2584 } 2585 if (streamDeleted) { 2586 continue; 2587 } 2588 2589 // Then check against configured streams 2590 if (streamId == inputStreamId) { 2591 streamBuffer.stream = mInputStream->asHalStream(); 2592 res = mInputStream->returnInputBuffer(streamBuffer); 2593 if (res != OK) { 2594 ALOGE("%s: Can't return input buffer for frame %d to" 2595 " stream %d: %s (%d)", __FUNCTION__, 2596 frameNumber, streamId, strerror(-res), res); 2597 } 2598 } else { 2599 ssize_t idx = mOutputStreams.indexOfKey(streamId); 2600 if (idx == NAME_NOT_FOUND) { 2601 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId); 2602 continue; 2603 } 2604 streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream(); 2605 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0); 2606 } 2607 } 2608} 2609 2610void Camera3Device::insertResultLocked(CaptureResult *result, 2611 uint32_t frameNumber) { 2612 if (result == nullptr) return; 2613 2614 camera_metadata_t *meta = const_cast<camera_metadata_t *>( 2615 result->mMetadata.getAndLock()); 2616 set_camera_metadata_vendor_id(meta, mVendorTagId); 2617 result->mMetadata.unlock(meta); 2618 2619 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, 2620 (int32_t*)&frameNumber, 1) != OK) { 2621 SET_ERR("Failed to set frame number %d in metadata", frameNumber); 2622 return; 2623 } 2624 2625 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) { 2626 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber); 2627 return; 2628 } 2629 2630 // Valid result, insert into queue 2631 List<CaptureResult>::iterator queuedResult = 2632 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result)); 2633 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 2634 ", burstId = %" PRId32, __FUNCTION__, 2635 queuedResult->mResultExtras.requestId, 2636 queuedResult->mResultExtras.frameNumber, 2637 queuedResult->mResultExtras.burstId); 2638 2639 mResultSignal.signal(); 2640} 2641 2642 2643void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult, 2644 const CaptureResultExtras &resultExtras, uint32_t frameNumber) { 2645 ATRACE_CALL(); 2646 Mutex::Autolock l(mOutputLock); 2647 2648 CaptureResult captureResult; 2649 captureResult.mResultExtras = resultExtras; 2650 captureResult.mMetadata = partialResult; 2651 2652 insertResultLocked(&captureResult, frameNumber); 2653} 2654 2655 2656void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata, 2657 CaptureResultExtras &resultExtras, 2658 CameraMetadata &collectedPartialResult, 2659 uint32_t frameNumber, 2660 bool reprocess) { 2661 ATRACE_CALL(); 2662 if (pendingMetadata.isEmpty()) 2663 return; 2664 2665 Mutex::Autolock l(mOutputLock); 2666 2667 // TODO: need to track errors for tighter bounds on expected frame number 2668 if (reprocess) { 2669 if (frameNumber < mNextReprocessResultFrameNumber) { 2670 SET_ERR("Out-of-order reprocess capture result metadata submitted! " 2671 "(got frame number %d, expecting %d)", 2672 frameNumber, mNextReprocessResultFrameNumber); 2673 return; 2674 } 2675 mNextReprocessResultFrameNumber = frameNumber + 1; 2676 } else { 2677 if (frameNumber < mNextResultFrameNumber) { 2678 SET_ERR("Out-of-order capture result metadata submitted! " 2679 "(got frame number %d, expecting %d)", 2680 frameNumber, mNextResultFrameNumber); 2681 return; 2682 } 2683 mNextResultFrameNumber = frameNumber + 1; 2684 } 2685 2686 CaptureResult captureResult; 2687 captureResult.mResultExtras = resultExtras; 2688 captureResult.mMetadata = pendingMetadata; 2689 2690 // Append any previous partials to form a complete result 2691 if (mUsePartialResult && !collectedPartialResult.isEmpty()) { 2692 captureResult.mMetadata.append(collectedPartialResult); 2693 } 2694 2695 captureResult.mMetadata.sort(); 2696 2697 // Check that there's a timestamp in the result metadata 2698 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); 2699 if (timestamp.count == 0) { 2700 SET_ERR("No timestamp provided by HAL for frame %d!", 2701 frameNumber); 2702 return; 2703 } 2704 2705 mTagMonitor.monitorMetadata(TagMonitor::RESULT, 2706 frameNumber, timestamp.data.i64[0], captureResult.mMetadata); 2707 2708 insertResultLocked(&captureResult, frameNumber); 2709} 2710 2711/** 2712 * Camera HAL device callback methods 2713 */ 2714 2715void Camera3Device::processCaptureResult(const camera3_capture_result *result) { 2716 ATRACE_CALL(); 2717 2718 status_t res; 2719 2720 uint32_t frameNumber = result->frame_number; 2721 if (result->result == NULL && result->num_output_buffers == 0 && 2722 result->input_buffer == NULL) { 2723 SET_ERR("No result data provided by HAL for frame %d", 2724 frameNumber); 2725 return; 2726 } 2727 2728 if (!mUsePartialResult && 2729 result->result != NULL && 2730 result->partial_result != 1) { 2731 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" 2732 " if partial result is not supported", 2733 frameNumber, result->partial_result); 2734 return; 2735 } 2736 2737 bool isPartialResult = false; 2738 CameraMetadata collectedPartialResult; 2739 CaptureResultExtras resultExtras; 2740 bool hasInputBufferInRequest = false; 2741 2742 // Get shutter timestamp and resultExtras from list of in-flight requests, 2743 // where it was added by the shutter notification for this frame. If the 2744 // shutter timestamp isn't received yet, append the output buffers to the 2745 // in-flight request and they will be returned when the shutter timestamp 2746 // arrives. Update the in-flight status and remove the in-flight entry if 2747 // all result data and shutter timestamp have been received. 2748 nsecs_t shutterTimestamp = 0; 2749 2750 { 2751 Mutex::Autolock l(mInFlightLock); 2752 ssize_t idx = mInFlightMap.indexOfKey(frameNumber); 2753 if (idx == NAME_NOT_FOUND) { 2754 SET_ERR("Unknown frame number for capture result: %d", 2755 frameNumber); 2756 return; 2757 } 2758 InFlightRequest &request = mInFlightMap.editValueAt(idx); 2759 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 2760 ", frameNumber = %" PRId64 ", burstId = %" PRId32 2761 ", partialResultCount = %d, hasCallback = %d", 2762 __FUNCTION__, request.resultExtras.requestId, 2763 request.resultExtras.frameNumber, request.resultExtras.burstId, 2764 result->partial_result, request.hasCallback); 2765 // Always update the partial count to the latest one if it's not 0 2766 // (buffers only). When framework aggregates adjacent partial results 2767 // into one, the latest partial count will be used. 2768 if (result->partial_result != 0) 2769 request.resultExtras.partialResultCount = result->partial_result; 2770 2771 // Check if this result carries only partial metadata 2772 if (mUsePartialResult && result->result != NULL) { 2773 if (result->partial_result > mNumPartialResults || result->partial_result < 1) { 2774 SET_ERR("Result is malformed for frame %d: partial_result %u must be in" 2775 " the range of [1, %d] when metadata is included in the result", 2776 frameNumber, result->partial_result, mNumPartialResults); 2777 return; 2778 } 2779 isPartialResult = (result->partial_result < mNumPartialResults); 2780 if (isPartialResult) { 2781 request.collectedPartialResult.append(result->result); 2782 } 2783 2784 if (isPartialResult && request.hasCallback) { 2785 // Send partial capture result 2786 sendPartialCaptureResult(result->result, request.resultExtras, 2787 frameNumber); 2788 } 2789 } 2790 2791 shutterTimestamp = request.shutterTimestamp; 2792 hasInputBufferInRequest = request.hasInputBuffer; 2793 2794 // Did we get the (final) result metadata for this capture? 2795 if (result->result != NULL && !isPartialResult) { 2796 if (request.haveResultMetadata) { 2797 SET_ERR("Called multiple times with metadata for frame %d", 2798 frameNumber); 2799 return; 2800 } 2801 if (mUsePartialResult && 2802 !request.collectedPartialResult.isEmpty()) { 2803 collectedPartialResult.acquire( 2804 request.collectedPartialResult); 2805 } 2806 request.haveResultMetadata = true; 2807 } 2808 2809 uint32_t numBuffersReturned = result->num_output_buffers; 2810 if (result->input_buffer != NULL) { 2811 if (hasInputBufferInRequest) { 2812 numBuffersReturned += 1; 2813 } else { 2814 ALOGW("%s: Input buffer should be NULL if there is no input" 2815 " buffer sent in the request", 2816 __FUNCTION__); 2817 } 2818 } 2819 request.numBuffersLeft -= numBuffersReturned; 2820 if (request.numBuffersLeft < 0) { 2821 SET_ERR("Too many buffers returned for frame %d", 2822 frameNumber); 2823 return; 2824 } 2825 2826 camera_metadata_ro_entry_t entry; 2827 res = find_camera_metadata_ro_entry(result->result, 2828 ANDROID_SENSOR_TIMESTAMP, &entry); 2829 if (res == OK && entry.count == 1) { 2830 request.sensorTimestamp = entry.data.i64[0]; 2831 } 2832 2833 // If shutter event isn't received yet, append the output buffers to 2834 // the in-flight request. Otherwise, return the output buffers to 2835 // streams. 2836 if (shutterTimestamp == 0) { 2837 request.pendingOutputBuffers.appendArray(result->output_buffers, 2838 result->num_output_buffers); 2839 } else { 2840 returnOutputBuffers(result->output_buffers, 2841 result->num_output_buffers, shutterTimestamp); 2842 } 2843 2844 if (result->result != NULL && !isPartialResult) { 2845 if (shutterTimestamp == 0) { 2846 request.pendingMetadata = result->result; 2847 request.collectedPartialResult = collectedPartialResult; 2848 } else if (request.hasCallback) { 2849 CameraMetadata metadata; 2850 metadata = result->result; 2851 sendCaptureResult(metadata, request.resultExtras, 2852 collectedPartialResult, frameNumber, 2853 hasInputBufferInRequest); 2854 } 2855 } 2856 2857 removeInFlightRequestIfReadyLocked(idx); 2858 } // scope for mInFlightLock 2859 2860 if (result->input_buffer != NULL) { 2861 if (hasInputBufferInRequest) { 2862 Camera3Stream *stream = 2863 Camera3Stream::cast(result->input_buffer->stream); 2864 res = stream->returnInputBuffer(*(result->input_buffer)); 2865 // Note: stream may be deallocated at this point, if this buffer was the 2866 // last reference to it. 2867 if (res != OK) { 2868 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" 2869 " its stream:%s (%d)", __FUNCTION__, 2870 frameNumber, strerror(-res), res); 2871 } 2872 } else { 2873 ALOGW("%s: Input buffer should be NULL if there is no input" 2874 " buffer sent in the request, skipping input buffer return.", 2875 __FUNCTION__); 2876 } 2877 } 2878} 2879 2880void Camera3Device::notify(const camera3_notify_msg *msg) { 2881 ATRACE_CALL(); 2882 sp<NotificationListener> listener; 2883 { 2884 Mutex::Autolock l(mOutputLock); 2885 listener = mListener.promote(); 2886 } 2887 2888 if (msg == NULL) { 2889 SET_ERR("HAL sent NULL notify message!"); 2890 return; 2891 } 2892 2893 switch (msg->type) { 2894 case CAMERA3_MSG_ERROR: { 2895 notifyError(msg->message.error, listener); 2896 break; 2897 } 2898 case CAMERA3_MSG_SHUTTER: { 2899 notifyShutter(msg->message.shutter, listener); 2900 break; 2901 } 2902 default: 2903 SET_ERR("Unknown notify message from HAL: %d", 2904 msg->type); 2905 } 2906} 2907 2908void Camera3Device::notifyError(const camera3_error_msg_t &msg, 2909 sp<NotificationListener> listener) { 2910 ATRACE_CALL(); 2911 // Map camera HAL error codes to ICameraDeviceCallback error codes 2912 // Index into this with the HAL error code 2913 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = { 2914 // 0 = Unused error code 2915 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, 2916 // 1 = CAMERA3_MSG_ERROR_DEVICE 2917 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, 2918 // 2 = CAMERA3_MSG_ERROR_REQUEST 2919 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 2920 // 3 = CAMERA3_MSG_ERROR_RESULT 2921 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, 2922 // 4 = CAMERA3_MSG_ERROR_BUFFER 2923 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER 2924 }; 2925 2926 int32_t errorCode = 2927 ((msg.error_code >= 0) && 2928 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ? 2929 halErrorMap[msg.error_code] : 2930 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; 2931 2932 int streamId = 0; 2933 if (msg.error_stream != NULL) { 2934 Camera3Stream *stream = 2935 Camera3Stream::cast(msg.error_stream); 2936 streamId = stream->getId(); 2937 } 2938 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d", 2939 mId.string(), __FUNCTION__, msg.frame_number, 2940 streamId, msg.error_code); 2941 2942 CaptureResultExtras resultExtras; 2943 switch (errorCode) { 2944 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: 2945 // SET_ERR calls notifyError 2946 SET_ERR("Camera HAL reported serious device error"); 2947 break; 2948 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: 2949 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: 2950 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: 2951 { 2952 Mutex::Autolock l(mInFlightLock); 2953 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number); 2954 if (idx >= 0) { 2955 InFlightRequest &r = mInFlightMap.editValueAt(idx); 2956 r.requestStatus = msg.error_code; 2957 resultExtras = r.resultExtras; 2958 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode 2959 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST == 2960 errorCode) { 2961 r.skipResultMetadata = true; 2962 } 2963 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == 2964 errorCode) { 2965 // In case of missing result check whether the buffers 2966 // returned. If they returned, then remove inflight 2967 // request. 2968 removeInFlightRequestIfReadyLocked(idx); 2969 } 2970 } else { 2971 resultExtras.frameNumber = msg.frame_number; 2972 ALOGE("Camera %s: %s: cannot find in-flight request on " 2973 "frame %" PRId64 " error", mId.string(), __FUNCTION__, 2974 resultExtras.frameNumber); 2975 } 2976 } 2977 resultExtras.errorStreamId = streamId; 2978 if (listener != NULL) { 2979 listener->notifyError(errorCode, resultExtras); 2980 } else { 2981 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__); 2982 } 2983 break; 2984 default: 2985 // SET_ERR calls notifyError 2986 SET_ERR("Unknown error message from HAL: %d", msg.error_code); 2987 break; 2988 } 2989} 2990 2991void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg, 2992 sp<NotificationListener> listener) { 2993 ATRACE_CALL(); 2994 ssize_t idx; 2995 2996 // Set timestamp for the request in the in-flight tracking 2997 // and get the request ID to send upstream 2998 { 2999 Mutex::Autolock l(mInFlightLock); 3000 idx = mInFlightMap.indexOfKey(msg.frame_number); 3001 if (idx >= 0) { 3002 InFlightRequest &r = mInFlightMap.editValueAt(idx); 3003 3004 // Verify ordering of shutter notifications 3005 { 3006 Mutex::Autolock l(mOutputLock); 3007 // TODO: need to track errors for tighter bounds on expected frame number. 3008 if (r.hasInputBuffer) { 3009 if (msg.frame_number < mNextReprocessShutterFrameNumber) { 3010 SET_ERR("Shutter notification out-of-order. Expected " 3011 "notification for frame %d, got frame %d", 3012 mNextReprocessShutterFrameNumber, msg.frame_number); 3013 return; 3014 } 3015 mNextReprocessShutterFrameNumber = msg.frame_number + 1; 3016 } else { 3017 if (msg.frame_number < mNextShutterFrameNumber) { 3018 SET_ERR("Shutter notification out-of-order. Expected " 3019 "notification for frame %d, got frame %d", 3020 mNextShutterFrameNumber, msg.frame_number); 3021 return; 3022 } 3023 mNextShutterFrameNumber = msg.frame_number + 1; 3024 } 3025 } 3026 3027 r.shutterTimestamp = msg.timestamp; 3028 if (r.hasCallback) { 3029 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64, 3030 mId.string(), __FUNCTION__, 3031 msg.frame_number, r.resultExtras.requestId, msg.timestamp); 3032 // Call listener, if any 3033 if (listener != NULL) { 3034 listener->notifyShutter(r.resultExtras, msg.timestamp); 3035 } 3036 // send pending result and buffers 3037 sendCaptureResult(r.pendingMetadata, r.resultExtras, 3038 r.collectedPartialResult, msg.frame_number, 3039 r.hasInputBuffer); 3040 } 3041 returnOutputBuffers(r.pendingOutputBuffers.array(), 3042 r.pendingOutputBuffers.size(), r.shutterTimestamp); 3043 r.pendingOutputBuffers.clear(); 3044 3045 removeInFlightRequestIfReadyLocked(idx); 3046 } 3047 } 3048 if (idx < 0) { 3049 SET_ERR("Shutter notification for non-existent frame number %d", 3050 msg.frame_number); 3051 } 3052} 3053 3054 3055CameraMetadata Camera3Device::getLatestRequestLocked() { 3056 ALOGV("%s", __FUNCTION__); 3057 3058 CameraMetadata retVal; 3059 3060 if (mRequestThread != NULL) { 3061 retVal = mRequestThread->getLatestRequest(); 3062 } 3063 3064 return retVal; 3065} 3066 3067 3068void Camera3Device::monitorMetadata(TagMonitor::eventSource source, 3069 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) { 3070 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata); 3071} 3072 3073/** 3074 * HalInterface inner class methods 3075 */ 3076 3077Camera3Device::HalInterface::HalInterface( 3078 sp<ICameraDeviceSession> &session, 3079 std::shared_ptr<RequestMetadataQueue> queue) : 3080 mHidlSession(session), 3081 mRequestMetadataQueue(queue) {} 3082 3083Camera3Device::HalInterface::HalInterface() {} 3084 3085Camera3Device::HalInterface::HalInterface(const HalInterface& other) : 3086 mHidlSession(other.mHidlSession), 3087 mRequestMetadataQueue(other.mRequestMetadataQueue) {} 3088 3089bool Camera3Device::HalInterface::valid() { 3090 return (mHidlSession != nullptr); 3091} 3092 3093void Camera3Device::HalInterface::clear() { 3094 mHidlSession.clear(); 3095} 3096 3097bool Camera3Device::HalInterface::supportBatchRequest() { 3098 return mHidlSession != nullptr; 3099} 3100 3101status_t Camera3Device::HalInterface::constructDefaultRequestSettings( 3102 camera3_request_template_t templateId, 3103 /*out*/ camera_metadata_t **requestTemplate) { 3104 ATRACE_NAME("CameraHal::constructDefaultRequestSettings"); 3105 if (!valid()) return INVALID_OPERATION; 3106 status_t res = OK; 3107 3108 common::V1_0::Status status; 3109 RequestTemplate id; 3110 switch (templateId) { 3111 case CAMERA3_TEMPLATE_PREVIEW: 3112 id = RequestTemplate::PREVIEW; 3113 break; 3114 case CAMERA3_TEMPLATE_STILL_CAPTURE: 3115 id = RequestTemplate::STILL_CAPTURE; 3116 break; 3117 case CAMERA3_TEMPLATE_VIDEO_RECORD: 3118 id = RequestTemplate::VIDEO_RECORD; 3119 break; 3120 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: 3121 id = RequestTemplate::VIDEO_SNAPSHOT; 3122 break; 3123 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: 3124 id = RequestTemplate::ZERO_SHUTTER_LAG; 3125 break; 3126 case CAMERA3_TEMPLATE_MANUAL: 3127 id = RequestTemplate::MANUAL; 3128 break; 3129 default: 3130 // Unknown template ID 3131 return BAD_VALUE; 3132 } 3133 auto err = mHidlSession->constructDefaultRequestSettings(id, 3134 [&status, &requestTemplate] 3135 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) { 3136 status = s; 3137 if (status == common::V1_0::Status::OK) { 3138 const camera_metadata *r = 3139 reinterpret_cast<const camera_metadata_t*>(request.data()); 3140 size_t expectedSize = request.size(); 3141 int ret = validate_camera_metadata_structure(r, &expectedSize); 3142 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) { 3143 *requestTemplate = clone_camera_metadata(r); 3144 if (*requestTemplate == nullptr) { 3145 ALOGE("%s: Unable to clone camera metadata received from HAL", 3146 __FUNCTION__); 3147 status = common::V1_0::Status::INTERNAL_ERROR; 3148 } 3149 } else { 3150 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__); 3151 status = common::V1_0::Status::INTERNAL_ERROR; 3152 } 3153 } 3154 }); 3155 if (!err.isOk()) { 3156 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); 3157 res = DEAD_OBJECT; 3158 } else { 3159 res = CameraProviderManager::mapToStatusT(status); 3160 } 3161 3162 return res; 3163} 3164 3165status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) { 3166 ATRACE_NAME("CameraHal::configureStreams"); 3167 if (!valid()) return INVALID_OPERATION; 3168 status_t res = OK; 3169 3170 // Convert stream config to HIDL 3171 std::set<int> activeStreams; 3172 StreamConfiguration requestedConfiguration; 3173 requestedConfiguration.streams.resize(config->num_streams); 3174 for (size_t i = 0; i < config->num_streams; i++) { 3175 Stream &dst = requestedConfiguration.streams[i]; 3176 camera3_stream_t *src = config->streams[i]; 3177 3178 Camera3Stream* cam3stream = Camera3Stream::cast(src); 3179 cam3stream->setBufferFreedListener(this); 3180 int streamId = cam3stream->getId(); 3181 StreamType streamType; 3182 switch (src->stream_type) { 3183 case CAMERA3_STREAM_OUTPUT: 3184 streamType = StreamType::OUTPUT; 3185 break; 3186 case CAMERA3_STREAM_INPUT: 3187 streamType = StreamType::INPUT; 3188 break; 3189 default: 3190 ALOGE("%s: Stream %d: Unsupported stream type %d", 3191 __FUNCTION__, streamId, config->streams[i]->stream_type); 3192 return BAD_VALUE; 3193 } 3194 dst.id = streamId; 3195 dst.streamType = streamType; 3196 dst.width = src->width; 3197 dst.height = src->height; 3198 dst.format = mapToPixelFormat(src->format); 3199 dst.usage = mapToConsumerUsage(cam3stream->getUsage()); 3200 dst.dataSpace = mapToHidlDataspace(src->data_space); 3201 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation); 3202 3203 activeStreams.insert(streamId); 3204 // Create Buffer ID map if necessary 3205 if (mBufferIdMaps.count(streamId) == 0) { 3206 mBufferIdMaps.emplace(streamId, BufferIdMap{}); 3207 } 3208 } 3209 // remove BufferIdMap for deleted streams 3210 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) { 3211 int streamId = it->first; 3212 bool active = activeStreams.count(streamId) > 0; 3213 if (!active) { 3214 it = mBufferIdMaps.erase(it); 3215 } else { 3216 ++it; 3217 } 3218 } 3219 3220 res = mapToStreamConfigurationMode( 3221 (camera3_stream_configuration_mode_t) config->operation_mode, 3222 /*out*/ &requestedConfiguration.operationMode); 3223 if (res != OK) { 3224 return res; 3225 } 3226 3227 // Invoke configureStreams 3228 3229 device::V3_3::HalStreamConfiguration finalConfiguration; 3230 common::V1_0::Status status; 3231 3232 // See if we have v3.3 HAL 3233 sp<device::V3_3::ICameraDeviceSession> hidlSession_3_3; 3234 auto castResult = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession); 3235 if (castResult.isOk()) { 3236 hidlSession_3_3 = castResult; 3237 } else { 3238 ALOGE("%s: Transaction error when casting ICameraDeviceSession: %s", __FUNCTION__, 3239 castResult.description().c_str()); 3240 } 3241 if (hidlSession_3_3 != nullptr) { 3242 // We do; use v3.3 for the call 3243 ALOGV("%s: v3.3 device found", __FUNCTION__); 3244 auto err = hidlSession_3_3->configureStreams_3_3(requestedConfiguration, 3245 [&status, &finalConfiguration] 3246 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) { 3247 finalConfiguration = halConfiguration; 3248 status = s; 3249 }); 3250 if (!err.isOk()) { 3251 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); 3252 return DEAD_OBJECT; 3253 } 3254 } else { 3255 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration 3256 ALOGV("%s: v3.2 device found", __FUNCTION__); 3257 HalStreamConfiguration finalConfiguration_3_2; 3258 auto err = mHidlSession->configureStreams(requestedConfiguration, 3259 [&status, &finalConfiguration_3_2] 3260 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) { 3261 finalConfiguration_3_2 = halConfiguration; 3262 status = s; 3263 }); 3264 if (!err.isOk()) { 3265 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); 3266 return DEAD_OBJECT; 3267 } 3268 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size()); 3269 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) { 3270 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i]; 3271 finalConfiguration.streams[i].overrideDataSpace = 3272 requestedConfiguration.streams[i].dataSpace; 3273 } 3274 } 3275 3276 if (status != common::V1_0::Status::OK ) { 3277 return CameraProviderManager::mapToStatusT(status); 3278 } 3279 3280 // And convert output stream configuration from HIDL 3281 3282 for (size_t i = 0; i < config->num_streams; i++) { 3283 camera3_stream_t *dst = config->streams[i]; 3284 int streamId = Camera3Stream::cast(dst)->getId(); 3285 3286 // Start scan at i, with the assumption that the stream order matches 3287 size_t realIdx = i; 3288 bool found = false; 3289 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) { 3290 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) { 3291 found = true; 3292 break; 3293 } 3294 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1; 3295 } 3296 if (!found) { 3297 ALOGE("%s: Stream %d not found in stream configuration response from HAL", 3298 __FUNCTION__, streamId); 3299 return INVALID_OPERATION; 3300 } 3301 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx]; 3302 3303 Camera3Stream* dstStream = Camera3Stream::cast(dst); 3304 dstStream->setFormatOverride(false); 3305 dstStream->setDataSpaceOverride(false); 3306 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat); 3307 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace); 3308 3309 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { 3310 if (dst->format != overrideFormat) { 3311 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__, 3312 streamId, dst->format); 3313 } 3314 if (dst->data_space != overrideDataSpace) { 3315 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__, 3316 streamId, dst->format); 3317 } 3318 } else { 3319 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false); 3320 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false); 3321 3322 // Override allowed with IMPLEMENTATION_DEFINED 3323 dst->format = overrideFormat; 3324 dst->data_space = overrideDataSpace; 3325 } 3326 3327 if (dst->stream_type == CAMERA3_STREAM_INPUT) { 3328 if (src.v3_2.producerUsage != 0) { 3329 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage", 3330 __FUNCTION__, streamId); 3331 return INVALID_OPERATION; 3332 } 3333 dstStream->setUsage( 3334 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage)); 3335 } else { 3336 // OUTPUT 3337 if (src.v3_2.consumerUsage != 0) { 3338 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage", 3339 __FUNCTION__, streamId); 3340 return INVALID_OPERATION; 3341 } 3342 dstStream->setUsage( 3343 mapProducerToFrameworkUsage(src.v3_2.producerUsage)); 3344 } 3345 dst->max_buffers = src.v3_2.maxBuffers; 3346 } 3347 3348 return res; 3349} 3350 3351void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request, 3352 /*out*/device::V3_2::CaptureRequest* captureRequest, 3353 /*out*/std::vector<native_handle_t*>* handlesCreated) { 3354 ATRACE_CALL(); 3355 if (captureRequest == nullptr || handlesCreated == nullptr) { 3356 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null", 3357 __FUNCTION__, captureRequest, handlesCreated); 3358 return; 3359 } 3360 3361 captureRequest->frameNumber = request->frame_number; 3362 3363 captureRequest->fmqSettingsSize = 0; 3364 3365 { 3366 std::lock_guard<std::mutex> lock(mInflightLock); 3367 if (request->input_buffer != nullptr) { 3368 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId(); 3369 buffer_handle_t buf = *(request->input_buffer->buffer); 3370 auto pair = getBufferId(buf, streamId); 3371 bool isNewBuffer = pair.first; 3372 uint64_t bufferId = pair.second; 3373 captureRequest->inputBuffer.streamId = streamId; 3374 captureRequest->inputBuffer.bufferId = bufferId; 3375 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr; 3376 captureRequest->inputBuffer.status = BufferStatus::OK; 3377 native_handle_t *acquireFence = nullptr; 3378 if (request->input_buffer->acquire_fence != -1) { 3379 acquireFence = native_handle_create(1,0); 3380 acquireFence->data[0] = request->input_buffer->acquire_fence; 3381 handlesCreated->push_back(acquireFence); 3382 } 3383 captureRequest->inputBuffer.acquireFence = acquireFence; 3384 captureRequest->inputBuffer.releaseFence = nullptr; 3385 3386 pushInflightBufferLocked(captureRequest->frameNumber, streamId, 3387 request->input_buffer->buffer, 3388 request->input_buffer->acquire_fence); 3389 } else { 3390 captureRequest->inputBuffer.streamId = -1; 3391 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER; 3392 } 3393 3394 captureRequest->outputBuffers.resize(request->num_output_buffers); 3395 for (size_t i = 0; i < request->num_output_buffers; i++) { 3396 const camera3_stream_buffer_t *src = request->output_buffers + i; 3397 StreamBuffer &dst = captureRequest->outputBuffers[i]; 3398 int32_t streamId = Camera3Stream::cast(src->stream)->getId(); 3399 buffer_handle_t buf = *(src->buffer); 3400 auto pair = getBufferId(buf, streamId); 3401 bool isNewBuffer = pair.first; 3402 dst.streamId = streamId; 3403 dst.bufferId = pair.second; 3404 dst.buffer = isNewBuffer ? buf : nullptr; 3405 dst.status = BufferStatus::OK; 3406 native_handle_t *acquireFence = nullptr; 3407 if (src->acquire_fence != -1) { 3408 acquireFence = native_handle_create(1,0); 3409 acquireFence->data[0] = src->acquire_fence; 3410 handlesCreated->push_back(acquireFence); 3411 } 3412 dst.acquireFence = acquireFence; 3413 dst.releaseFence = nullptr; 3414 3415 pushInflightBufferLocked(captureRequest->frameNumber, streamId, 3416 src->buffer, src->acquire_fence); 3417 } 3418 } 3419} 3420 3421status_t Camera3Device::HalInterface::processBatchCaptureRequests( 3422 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) { 3423 ATRACE_NAME("CameraHal::processBatchCaptureRequests"); 3424 if (!valid()) return INVALID_OPERATION; 3425 3426 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests; 3427 size_t batchSize = requests.size(); 3428 captureRequests.resize(batchSize); 3429 std::vector<native_handle_t*> handlesCreated; 3430 3431 for (size_t i = 0; i < batchSize; i++) { 3432 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated); 3433 } 3434 3435 std::vector<device::V3_2::BufferCache> cachesToRemove; 3436 { 3437 std::lock_guard<std::mutex> lock(mBufferIdMapLock); 3438 for (auto& pair : mFreedBuffers) { 3439 // The stream might have been removed since onBufferFreed 3440 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) { 3441 cachesToRemove.push_back({pair.first, pair.second}); 3442 } 3443 } 3444 mFreedBuffers.clear(); 3445 } 3446 3447 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR; 3448 *numRequestProcessed = 0; 3449 3450 // Write metadata to FMQ. 3451 for (size_t i = 0; i < batchSize; i++) { 3452 camera3_capture_request_t* request = requests[i]; 3453 device::V3_2::CaptureRequest* captureRequest = &captureRequests[i]; 3454 3455 if (request->settings != nullptr) { 3456 size_t settingsSize = get_camera_metadata_size(request->settings); 3457 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write( 3458 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) { 3459 captureRequest->settings.resize(0); 3460 captureRequest->fmqSettingsSize = settingsSize; 3461 } else { 3462 if (mRequestMetadataQueue != nullptr) { 3463 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__); 3464 } 3465 captureRequest->settings.setToExternal( 3466 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)), 3467 get_camera_metadata_size(request->settings)); 3468 captureRequest->fmqSettingsSize = 0u; 3469 } 3470 } else { 3471 // A null request settings maps to a size-0 CameraMetadata 3472 captureRequest->settings.resize(0); 3473 captureRequest->fmqSettingsSize = 0u; 3474 } 3475 } 3476 auto err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove, 3477 [&status, &numRequestProcessed] (auto s, uint32_t n) { 3478 status = s; 3479 *numRequestProcessed = n; 3480 }); 3481 if (!err.isOk()) { 3482 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); 3483 return DEAD_OBJECT; 3484 } 3485 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) { 3486 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests", 3487 __FUNCTION__, *numRequestProcessed, batchSize); 3488 status = common::V1_0::Status::INTERNAL_ERROR; 3489 } 3490 3491 for (auto& handle : handlesCreated) { 3492 native_handle_delete(handle); 3493 } 3494 return CameraProviderManager::mapToStatusT(status); 3495} 3496 3497status_t Camera3Device::HalInterface::processCaptureRequest( 3498 camera3_capture_request_t *request) { 3499 ATRACE_NAME("CameraHal::processCaptureRequest"); 3500 if (!valid()) return INVALID_OPERATION; 3501 status_t res = OK; 3502 3503 uint32_t numRequestProcessed = 0; 3504 std::vector<camera3_capture_request_t*> requests(1); 3505 requests[0] = request; 3506 res = processBatchCaptureRequests(requests, &numRequestProcessed); 3507 3508 return res; 3509} 3510 3511status_t Camera3Device::HalInterface::flush() { 3512 ATRACE_NAME("CameraHal::flush"); 3513 if (!valid()) return INVALID_OPERATION; 3514 status_t res = OK; 3515 3516 auto err = mHidlSession->flush(); 3517 if (!err.isOk()) { 3518 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); 3519 res = DEAD_OBJECT; 3520 } else { 3521 res = CameraProviderManager::mapToStatusT(err); 3522 } 3523 3524 return res; 3525} 3526 3527status_t Camera3Device::HalInterface::dump(int /*fd*/) { 3528 ATRACE_NAME("CameraHal::dump"); 3529 if (!valid()) return INVALID_OPERATION; 3530 3531 // Handled by CameraProviderManager::dump 3532 3533 return OK; 3534} 3535 3536status_t Camera3Device::HalInterface::close() { 3537 ATRACE_NAME("CameraHal::close()"); 3538 if (!valid()) return INVALID_OPERATION; 3539 status_t res = OK; 3540 3541 auto err = mHidlSession->close(); 3542 // Interface will be dead shortly anyway, so don't log errors 3543 if (!err.isOk()) { 3544 res = DEAD_OBJECT; 3545 } 3546 3547 return res; 3548} 3549 3550void Camera3Device::HalInterface::getInflightBufferKeys( 3551 std::vector<std::pair<int32_t, int32_t>>* out) { 3552 std::lock_guard<std::mutex> lock(mInflightLock); 3553 out->clear(); 3554 out->reserve(mInflightBufferMap.size()); 3555 for (auto& pair : mInflightBufferMap) { 3556 uint64_t key = pair.first; 3557 int32_t streamId = key & 0xFFFFFFFF; 3558 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF; 3559 out->push_back(std::make_pair(frameNumber, streamId)); 3560 } 3561 return; 3562} 3563 3564status_t Camera3Device::HalInterface::pushInflightBufferLocked( 3565 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) { 3566 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId); 3567 auto pair = std::make_pair(buffer, acquireFence); 3568 mInflightBufferMap[key] = pair; 3569 return OK; 3570} 3571 3572status_t Camera3Device::HalInterface::popInflightBuffer( 3573 int32_t frameNumber, int32_t streamId, 3574 /*out*/ buffer_handle_t **buffer) { 3575 std::lock_guard<std::mutex> lock(mInflightLock); 3576 3577 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId); 3578 auto it = mInflightBufferMap.find(key); 3579 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND; 3580 auto pair = it->second; 3581 *buffer = pair.first; 3582 int acquireFence = pair.second; 3583 if (acquireFence > 0) { 3584 ::close(acquireFence); 3585 } 3586 mInflightBufferMap.erase(it); 3587 return OK; 3588} 3589 3590std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId( 3591 const buffer_handle_t& buf, int streamId) { 3592 std::lock_guard<std::mutex> lock(mBufferIdMapLock); 3593 3594 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId); 3595 auto it = bIdMap.find(buf); 3596 if (it == bIdMap.end()) { 3597 bIdMap[buf] = mNextBufferId++; 3598 ALOGV("stream %d now have %zu buffer caches, buf %p", 3599 streamId, bIdMap.size(), buf); 3600 return std::make_pair(true, mNextBufferId - 1); 3601 } else { 3602 return std::make_pair(false, it->second); 3603 } 3604} 3605 3606void Camera3Device::HalInterface::onBufferFreed( 3607 int streamId, const native_handle_t* handle) { 3608 std::lock_guard<std::mutex> lock(mBufferIdMapLock); 3609 uint64_t bufferId = BUFFER_ID_NO_BUFFER; 3610 auto mapIt = mBufferIdMaps.find(streamId); 3611 if (mapIt == mBufferIdMaps.end()) { 3612 // streamId might be from a deleted stream here 3613 ALOGI("%s: stream %d has been removed", 3614 __FUNCTION__, streamId); 3615 return; 3616 } 3617 BufferIdMap& bIdMap = mapIt->second; 3618 auto it = bIdMap.find(handle); 3619 if (it == bIdMap.end()) { 3620 ALOGW("%s: cannot find buffer %p in stream %d", 3621 __FUNCTION__, handle, streamId); 3622 return; 3623 } else { 3624 bufferId = it->second; 3625 bIdMap.erase(it); 3626 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p", 3627 __FUNCTION__, streamId, bIdMap.size(), handle); 3628 } 3629 mFreedBuffers.push_back(std::make_pair(streamId, bufferId)); 3630} 3631 3632/** 3633 * RequestThread inner class methods 3634 */ 3635 3636Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, 3637 sp<StatusTracker> statusTracker, 3638 sp<HalInterface> interface) : 3639 Thread(/*canCallJava*/false), 3640 mParent(parent), 3641 mStatusTracker(statusTracker), 3642 mInterface(interface), 3643 mListener(nullptr), 3644 mId(getId(parent)), 3645 mReconfigured(false), 3646 mDoPause(false), 3647 mPaused(true), 3648 mFrameNumber(0), 3649 mLatestRequestId(NAME_NOT_FOUND), 3650 mCurrentAfTriggerId(0), 3651 mCurrentPreCaptureTriggerId(0), 3652 mRepeatingLastFrameNumber( 3653 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES), 3654 mPrepareVideoStream(false), 3655 mRequestLatency(kRequestLatencyBinSize) { 3656 mStatusId = statusTracker->addComponent(); 3657} 3658 3659Camera3Device::RequestThread::~RequestThread() {} 3660 3661void Camera3Device::RequestThread::setNotificationListener( 3662 wp<NotificationListener> listener) { 3663 ATRACE_CALL(); 3664 Mutex::Autolock l(mRequestLock); 3665 mListener = listener; 3666} 3667 3668void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) { 3669 ATRACE_CALL(); 3670 Mutex::Autolock l(mRequestLock); 3671 mReconfigured = true; 3672 // Prepare video stream for high speed recording. 3673 mPrepareVideoStream = isConstrainedHighSpeed; 3674} 3675 3676status_t Camera3Device::RequestThread::queueRequestList( 3677 List<sp<CaptureRequest> > &requests, 3678 /*out*/ 3679 int64_t *lastFrameNumber) { 3680 ATRACE_CALL(); 3681 Mutex::Autolock l(mRequestLock); 3682 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); 3683 ++it) { 3684 mRequestQueue.push_back(*it); 3685 } 3686 3687 if (lastFrameNumber != NULL) { 3688 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1; 3689 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".", 3690 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber, 3691 *lastFrameNumber); 3692 } 3693 3694 unpauseForNewRequests(); 3695 3696 return OK; 3697} 3698 3699 3700status_t Camera3Device::RequestThread::queueTrigger( 3701 RequestTrigger trigger[], 3702 size_t count) { 3703 ATRACE_CALL(); 3704 Mutex::Autolock l(mTriggerMutex); 3705 status_t ret; 3706 3707 for (size_t i = 0; i < count; ++i) { 3708 ret = queueTriggerLocked(trigger[i]); 3709 3710 if (ret != OK) { 3711 return ret; 3712 } 3713 } 3714 3715 return OK; 3716} 3717 3718const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { 3719 static String8 deadId("<DeadDevice>"); 3720 sp<Camera3Device> d = device.promote(); 3721 if (d != nullptr) return d->mId; 3722 return deadId; 3723} 3724 3725status_t Camera3Device::RequestThread::queueTriggerLocked( 3726 RequestTrigger trigger) { 3727 3728 uint32_t tag = trigger.metadataTag; 3729 ssize_t index = mTriggerMap.indexOfKey(tag); 3730 3731 switch (trigger.getTagType()) { 3732 case TYPE_BYTE: 3733 // fall-through 3734 case TYPE_INT32: 3735 break; 3736 default: 3737 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, 3738 trigger.getTagType()); 3739 return INVALID_OPERATION; 3740 } 3741 3742 /** 3743 * Collect only the latest trigger, since we only have 1 field 3744 * in the request settings per trigger tag, and can't send more than 1 3745 * trigger per request. 3746 */ 3747 if (index != NAME_NOT_FOUND) { 3748 mTriggerMap.editValueAt(index) = trigger; 3749 } else { 3750 mTriggerMap.add(tag, trigger); 3751 } 3752 3753 return OK; 3754} 3755 3756status_t Camera3Device::RequestThread::setRepeatingRequests( 3757 const RequestList &requests, 3758 /*out*/ 3759 int64_t *lastFrameNumber) { 3760 ATRACE_CALL(); 3761 Mutex::Autolock l(mRequestLock); 3762 if (lastFrameNumber != NULL) { 3763 *lastFrameNumber = mRepeatingLastFrameNumber; 3764 } 3765 mRepeatingRequests.clear(); 3766 mRepeatingRequests.insert(mRepeatingRequests.begin(), 3767 requests.begin(), requests.end()); 3768 3769 unpauseForNewRequests(); 3770 3771 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; 3772 return OK; 3773} 3774 3775bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) { 3776 if (mRepeatingRequests.empty()) { 3777 return false; 3778 } 3779 int32_t requestId = requestIn->mResultExtras.requestId; 3780 const RequestList &repeatRequests = mRepeatingRequests; 3781 // All repeating requests are guaranteed to have same id so only check first quest 3782 const sp<CaptureRequest> firstRequest = *repeatRequests.begin(); 3783 return (firstRequest->mResultExtras.requestId == requestId); 3784} 3785 3786status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) { 3787 ATRACE_CALL(); 3788 Mutex::Autolock l(mRequestLock); 3789 return clearRepeatingRequestsLocked(lastFrameNumber); 3790 3791} 3792 3793status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) { 3794 mRepeatingRequests.clear(); 3795 if (lastFrameNumber != NULL) { 3796 *lastFrameNumber = mRepeatingLastFrameNumber; 3797 } 3798 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; 3799 return OK; 3800} 3801 3802status_t Camera3Device::RequestThread::clear( 3803 /*out*/int64_t *lastFrameNumber) { 3804 ATRACE_CALL(); 3805 Mutex::Autolock l(mRequestLock); 3806 ALOGV("RequestThread::%s:", __FUNCTION__); 3807 3808 mRepeatingRequests.clear(); 3809 3810 // Send errors for all requests pending in the request queue, including 3811 // pending repeating requests 3812 sp<NotificationListener> listener = mListener.promote(); 3813 if (listener != NULL) { 3814 for (RequestList::iterator it = mRequestQueue.begin(); 3815 it != mRequestQueue.end(); ++it) { 3816 // Abort the input buffers for reprocess requests. 3817 if ((*it)->mInputStream != NULL) { 3818 camera3_stream_buffer_t inputBuffer; 3819 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer, 3820 /*respectHalLimit*/ false); 3821 if (res != OK) { 3822 ALOGW("%s: %d: couldn't get input buffer while clearing the request " 3823 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); 3824 } else { 3825 res = (*it)->mInputStream->returnInputBuffer(inputBuffer); 3826 if (res != OK) { 3827 ALOGE("%s: %d: couldn't return input buffer while clearing the request " 3828 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); 3829 } 3830 } 3831 } 3832 // Set the frame number this request would have had, if it 3833 // had been submitted; this frame number will not be reused. 3834 // The requestId and burstId fields were set when the request was 3835 // submitted originally (in convertMetadataListToRequestListLocked) 3836 (*it)->mResultExtras.frameNumber = mFrameNumber++; 3837 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 3838 (*it)->mResultExtras); 3839 } 3840 } 3841 mRequestQueue.clear(); 3842 3843 Mutex::Autolock al(mTriggerMutex); 3844 mTriggerMap.clear(); 3845 if (lastFrameNumber != NULL) { 3846 *lastFrameNumber = mRepeatingLastFrameNumber; 3847 } 3848 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; 3849 return OK; 3850} 3851 3852status_t Camera3Device::RequestThread::flush() { 3853 ATRACE_CALL(); 3854 Mutex::Autolock l(mFlushLock); 3855 3856 return mInterface->flush(); 3857} 3858 3859void Camera3Device::RequestThread::setPaused(bool paused) { 3860 ATRACE_CALL(); 3861 Mutex::Autolock l(mPauseLock); 3862 mDoPause = paused; 3863 mDoPauseSignal.signal(); 3864} 3865 3866status_t Camera3Device::RequestThread::waitUntilRequestProcessed( 3867 int32_t requestId, nsecs_t timeout) { 3868 ATRACE_CALL(); 3869 Mutex::Autolock l(mLatestRequestMutex); 3870 status_t res; 3871 while (mLatestRequestId != requestId) { 3872 nsecs_t startTime = systemTime(); 3873 3874 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); 3875 if (res != OK) return res; 3876 3877 timeout -= (systemTime() - startTime); 3878 } 3879 3880 return OK; 3881} 3882 3883void Camera3Device::RequestThread::requestExit() { 3884 // Call parent to set up shutdown 3885 Thread::requestExit(); 3886 // The exit from any possible waits 3887 mDoPauseSignal.signal(); 3888 mRequestSignal.signal(); 3889 3890 mRequestLatency.log("ProcessCaptureRequest latency histogram"); 3891 mRequestLatency.reset(); 3892} 3893 3894void Camera3Device::RequestThread::checkAndStopRepeatingRequest() { 3895 ATRACE_CALL(); 3896 bool surfaceAbandoned = false; 3897 int64_t lastFrameNumber = 0; 3898 sp<NotificationListener> listener; 3899 { 3900 Mutex::Autolock l(mRequestLock); 3901 // Check all streams needed by repeating requests are still valid. Otherwise, stop 3902 // repeating requests. 3903 for (const auto& request : mRepeatingRequests) { 3904 for (const auto& s : request->mOutputStreams) { 3905 if (s->isAbandoned()) { 3906 surfaceAbandoned = true; 3907 clearRepeatingRequestsLocked(&lastFrameNumber); 3908 break; 3909 } 3910 } 3911 if (surfaceAbandoned) { 3912 break; 3913 } 3914 } 3915 listener = mListener.promote(); 3916 } 3917 3918 if (listener != NULL && surfaceAbandoned) { 3919 listener->notifyRepeatingRequestError(lastFrameNumber); 3920 } 3921} 3922 3923bool Camera3Device::RequestThread::sendRequestsBatch() { 3924 ATRACE_CALL(); 3925 status_t res; 3926 size_t batchSize = mNextRequests.size(); 3927 std::vector<camera3_capture_request_t*> requests(batchSize); 3928 uint32_t numRequestProcessed = 0; 3929 for (size_t i = 0; i < batchSize; i++) { 3930 requests[i] = &mNextRequests.editItemAt(i).halRequest; 3931 } 3932 3933 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number); 3934 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed); 3935 3936 bool triggerRemoveFailed = false; 3937 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0); 3938 for (size_t i = 0; i < numRequestProcessed; i++) { 3939 NextRequest& nextRequest = mNextRequests.editItemAt(i); 3940 nextRequest.submitted = true; 3941 3942 3943 // Update the latest request sent to HAL 3944 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged 3945 Mutex::Autolock al(mLatestRequestMutex); 3946 3947 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings); 3948 mLatestRequest.acquire(cloned); 3949 3950 sp<Camera3Device> parent = mParent.promote(); 3951 if (parent != NULL) { 3952 parent->monitorMetadata(TagMonitor::REQUEST, 3953 nextRequest.halRequest.frame_number, 3954 0, mLatestRequest); 3955 } 3956 } 3957 3958 if (nextRequest.halRequest.settings != NULL) { 3959 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings); 3960 } 3961 3962 if (!triggerRemoveFailed) { 3963 // Remove any previously queued triggers (after unlock) 3964 status_t removeTriggerRes = removeTriggers(mPrevRequest); 3965 if (removeTriggerRes != OK) { 3966 triggerRemoveFailed = true; 3967 triggerFailedRequest = nextRequest; 3968 } 3969 } 3970 } 3971 3972 if (triggerRemoveFailed) { 3973 SET_ERR("RequestThread: Unable to remove triggers " 3974 "(capture request %d, HAL device: %s (%d)", 3975 triggerFailedRequest.halRequest.frame_number, strerror(-res), res); 3976 cleanUpFailedRequests(/*sendRequestError*/ false); 3977 return false; 3978 } 3979 3980 if (res != OK) { 3981 // Should only get a failure here for malformed requests or device-level 3982 // errors, so consider all errors fatal. Bad metadata failures should 3983 // come through notify. 3984 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)", 3985 mNextRequests[numRequestProcessed].halRequest.frame_number, 3986 strerror(-res), res); 3987 cleanUpFailedRequests(/*sendRequestError*/ false); 3988 return false; 3989 } 3990 return true; 3991} 3992 3993bool Camera3Device::RequestThread::sendRequestsOneByOne() { 3994 status_t res; 3995 3996 for (auto& nextRequest : mNextRequests) { 3997 // Submit request and block until ready for next one 3998 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number); 3999 res = mInterface->processCaptureRequest(&nextRequest.halRequest); 4000 4001 if (res != OK) { 4002 // Should only get a failure here for malformed requests or device-level 4003 // errors, so consider all errors fatal. Bad metadata failures should 4004 // come through notify. 4005 SET_ERR("RequestThread: Unable to submit capture request %d to HAL" 4006 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res), 4007 res); 4008 cleanUpFailedRequests(/*sendRequestError*/ false); 4009 return false; 4010 } 4011 4012 // Mark that the request has be submitted successfully. 4013 nextRequest.submitted = true; 4014 4015 // Update the latest request sent to HAL 4016 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged 4017 Mutex::Autolock al(mLatestRequestMutex); 4018 4019 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings); 4020 mLatestRequest.acquire(cloned); 4021 4022 sp<Camera3Device> parent = mParent.promote(); 4023 if (parent != NULL) { 4024 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number, 4025 0, mLatestRequest); 4026 } 4027 } 4028 4029 if (nextRequest.halRequest.settings != NULL) { 4030 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings); 4031 } 4032 4033 // Remove any previously queued triggers (after unlock) 4034 res = removeTriggers(mPrevRequest); 4035 if (res != OK) { 4036 SET_ERR("RequestThread: Unable to remove triggers " 4037 "(capture request %d, HAL device: %s (%d)", 4038 nextRequest.halRequest.frame_number, strerror(-res), res); 4039 cleanUpFailedRequests(/*sendRequestError*/ false); 4040 return false; 4041 } 4042 } 4043 return true; 4044} 4045 4046nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) { 4047 nsecs_t maxExpectedDuration = kDefaultExpectedDuration; 4048 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); 4049 find_camera_metadata_ro_entry(request, 4050 ANDROID_CONTROL_AE_MODE, 4051 &e); 4052 if (e.count == 0) return maxExpectedDuration; 4053 4054 switch (e.data.u8[0]) { 4055 case ANDROID_CONTROL_AE_MODE_OFF: 4056 find_camera_metadata_ro_entry(request, 4057 ANDROID_SENSOR_EXPOSURE_TIME, 4058 &e); 4059 if (e.count > 0) { 4060 maxExpectedDuration = e.data.i64[0]; 4061 } 4062 find_camera_metadata_ro_entry(request, 4063 ANDROID_SENSOR_FRAME_DURATION, 4064 &e); 4065 if (e.count > 0) { 4066 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration); 4067 } 4068 break; 4069 default: 4070 find_camera_metadata_ro_entry(request, 4071 ANDROID_CONTROL_AE_TARGET_FPS_RANGE, 4072 &e); 4073 if (e.count > 1) { 4074 maxExpectedDuration = 1e9 / e.data.u8[0]; 4075 } 4076 break; 4077 } 4078 4079 return maxExpectedDuration; 4080} 4081 4082bool Camera3Device::RequestThread::threadLoop() { 4083 ATRACE_CALL(); 4084 status_t res; 4085 4086 // Handle paused state. 4087 if (waitIfPaused()) { 4088 return true; 4089 } 4090 4091 // Wait for the next batch of requests. 4092 waitForNextRequestBatch(); 4093 if (mNextRequests.size() == 0) { 4094 return true; 4095 } 4096 4097 // Get the latest request ID, if any 4098 int latestRequestId; 4099 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1]. 4100 captureRequest->mSettings.find(ANDROID_REQUEST_ID); 4101 if (requestIdEntry.count > 0) { 4102 latestRequestId = requestIdEntry.data.i32[0]; 4103 } else { 4104 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__); 4105 latestRequestId = NAME_NOT_FOUND; 4106 } 4107 4108 // Prepare a batch of HAL requests and output buffers. 4109 res = prepareHalRequests(); 4110 if (res == TIMED_OUT) { 4111 // Not a fatal error if getting output buffers time out. 4112 cleanUpFailedRequests(/*sendRequestError*/ true); 4113 // Check if any stream is abandoned. 4114 checkAndStopRepeatingRequest(); 4115 return true; 4116 } else if (res != OK) { 4117 cleanUpFailedRequests(/*sendRequestError*/ false); 4118 return false; 4119 } 4120 4121 // Inform waitUntilRequestProcessed thread of a new request ID 4122 { 4123 Mutex::Autolock al(mLatestRequestMutex); 4124 4125 mLatestRequestId = latestRequestId; 4126 mLatestRequestSignal.signal(); 4127 } 4128 4129 // Submit a batch of requests to HAL. 4130 // Use flush lock only when submitting multilple requests in a batch. 4131 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request() 4132 // which may take a long time to finish so synchronizing flush() and 4133 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush(). 4134 // For now, only synchronize for high speed recording and we should figure something out for 4135 // removing the synchronization. 4136 bool useFlushLock = mNextRequests.size() > 1; 4137 4138 if (useFlushLock) { 4139 mFlushLock.lock(); 4140 } 4141 4142 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__, 4143 mNextRequests.size()); 4144 4145 bool submitRequestSuccess = false; 4146 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC); 4147 if (mInterface->supportBatchRequest()) { 4148 submitRequestSuccess = sendRequestsBatch(); 4149 } else { 4150 submitRequestSuccess = sendRequestsOneByOne(); 4151 } 4152 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC); 4153 mRequestLatency.add(tRequestStart, tRequestEnd); 4154 4155 if (useFlushLock) { 4156 mFlushLock.unlock(); 4157 } 4158 4159 // Unset as current request 4160 { 4161 Mutex::Autolock l(mRequestLock); 4162 mNextRequests.clear(); 4163 } 4164 4165 return submitRequestSuccess; 4166} 4167 4168status_t Camera3Device::RequestThread::prepareHalRequests() { 4169 ATRACE_CALL(); 4170 4171 for (size_t i = 0; i < mNextRequests.size(); i++) { 4172 auto& nextRequest = mNextRequests.editItemAt(i); 4173 sp<CaptureRequest> captureRequest = nextRequest.captureRequest; 4174 camera3_capture_request_t* halRequest = &nextRequest.halRequest; 4175 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers; 4176 4177 // Prepare a request to HAL 4178 halRequest->frame_number = captureRequest->mResultExtras.frameNumber; 4179 4180 // Insert any queued triggers (before metadata is locked) 4181 status_t res = insertTriggers(captureRequest); 4182 4183 if (res < 0) { 4184 SET_ERR("RequestThread: Unable to insert triggers " 4185 "(capture request %d, HAL device: %s (%d)", 4186 halRequest->frame_number, strerror(-res), res); 4187 return INVALID_OPERATION; 4188 } 4189 int triggerCount = res; 4190 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); 4191 mPrevTriggers = triggerCount; 4192 4193 // If the request is the same as last, or we had triggers last time 4194 if (mPrevRequest != captureRequest || triggersMixedIn) { 4195 /** 4196 * HAL workaround: 4197 * Insert a dummy trigger ID if a trigger is set but no trigger ID is 4198 */ 4199 res = addDummyTriggerIds(captureRequest); 4200 if (res != OK) { 4201 SET_ERR("RequestThread: Unable to insert dummy trigger IDs " 4202 "(capture request %d, HAL device: %s (%d)", 4203 halRequest->frame_number, strerror(-res), res); 4204 return INVALID_OPERATION; 4205 } 4206 4207 /** 4208 * The request should be presorted so accesses in HAL 4209 * are O(logn). Sidenote, sorting a sorted metadata is nop. 4210 */ 4211 captureRequest->mSettings.sort(); 4212 halRequest->settings = captureRequest->mSettings.getAndLock(); 4213 mPrevRequest = captureRequest; 4214 ALOGVV("%s: Request settings are NEW", __FUNCTION__); 4215 4216 IF_ALOGV() { 4217 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); 4218 find_camera_metadata_ro_entry( 4219 halRequest->settings, 4220 ANDROID_CONTROL_AF_TRIGGER, 4221 &e 4222 ); 4223 if (e.count > 0) { 4224 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", 4225 __FUNCTION__, 4226 halRequest->frame_number, 4227 e.data.u8[0]); 4228 } 4229 } 4230 } else { 4231 // leave request.settings NULL to indicate 'reuse latest given' 4232 ALOGVV("%s: Request settings are REUSED", 4233 __FUNCTION__); 4234 } 4235 4236 uint32_t totalNumBuffers = 0; 4237 4238 // Fill in buffers 4239 if (captureRequest->mInputStream != NULL) { 4240 halRequest->input_buffer = &captureRequest->mInputBuffer; 4241 totalNumBuffers += 1; 4242 } else { 4243 halRequest->input_buffer = NULL; 4244 } 4245 4246 outputBuffers->insertAt(camera3_stream_buffer_t(), 0, 4247 captureRequest->mOutputStreams.size()); 4248 halRequest->output_buffers = outputBuffers->array(); 4249 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) { 4250 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j); 4251 4252 // Prepare video buffers for high speed recording on the first video request. 4253 if (mPrepareVideoStream && outputStream->isVideoStream()) { 4254 // Only try to prepare video stream on the first video request. 4255 mPrepareVideoStream = false; 4256 4257 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX); 4258 while (res == NOT_ENOUGH_DATA) { 4259 res = outputStream->prepareNextBuffer(); 4260 } 4261 if (res != OK) { 4262 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)", 4263 __FUNCTION__, strerror(-res), res); 4264 outputStream->cancelPrepare(); 4265 } 4266 } 4267 4268 res = outputStream->getBuffer(&outputBuffers->editItemAt(j), 4269 captureRequest->mOutputSurfaces[j]); 4270 if (res != OK) { 4271 // Can't get output buffer from gralloc queue - this could be due to 4272 // abandoned queue or other consumer misbehavior, so not a fatal 4273 // error 4274 ALOGE("RequestThread: Can't get output buffer, skipping request:" 4275 " %s (%d)", strerror(-res), res); 4276 4277 return TIMED_OUT; 4278 } 4279 halRequest->num_output_buffers++; 4280 4281 } 4282 totalNumBuffers += halRequest->num_output_buffers; 4283 4284 // Log request in the in-flight queue 4285 sp<Camera3Device> parent = mParent.promote(); 4286 if (parent == NULL) { 4287 // Should not happen, and nowhere to send errors to, so just log it 4288 CLOGE("RequestThread: Parent is gone"); 4289 return INVALID_OPERATION; 4290 } 4291 4292 // If this request list is for constrained high speed recording (not 4293 // preview), and the current request is not the last one in the batch, 4294 // do not send callback to the app. 4295 bool hasCallback = true; 4296 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) { 4297 hasCallback = false; 4298 } 4299 res = parent->registerInFlight(halRequest->frame_number, 4300 totalNumBuffers, captureRequest->mResultExtras, 4301 /*hasInput*/halRequest->input_buffer != NULL, 4302 hasCallback, 4303 calculateMaxExpectedDuration(halRequest->settings)); 4304 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64 4305 ", burstId = %" PRId32 ".", 4306 __FUNCTION__, 4307 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber, 4308 captureRequest->mResultExtras.burstId); 4309 if (res != OK) { 4310 SET_ERR("RequestThread: Unable to register new in-flight request:" 4311 " %s (%d)", strerror(-res), res); 4312 return INVALID_OPERATION; 4313 } 4314 } 4315 4316 return OK; 4317} 4318 4319CameraMetadata Camera3Device::RequestThread::getLatestRequest() const { 4320 ATRACE_CALL(); 4321 Mutex::Autolock al(mLatestRequestMutex); 4322 4323 ALOGV("RequestThread::%s", __FUNCTION__); 4324 4325 return mLatestRequest; 4326} 4327 4328bool Camera3Device::RequestThread::isStreamPending( 4329 sp<Camera3StreamInterface>& stream) { 4330 ATRACE_CALL(); 4331 Mutex::Autolock l(mRequestLock); 4332 4333 for (const auto& nextRequest : mNextRequests) { 4334 if (!nextRequest.submitted) { 4335 for (const auto& s : nextRequest.captureRequest->mOutputStreams) { 4336 if (stream == s) return true; 4337 } 4338 if (stream == nextRequest.captureRequest->mInputStream) return true; 4339 } 4340 } 4341 4342 for (const auto& request : mRequestQueue) { 4343 for (const auto& s : request->mOutputStreams) { 4344 if (stream == s) return true; 4345 } 4346 if (stream == request->mInputStream) return true; 4347 } 4348 4349 for (const auto& request : mRepeatingRequests) { 4350 for (const auto& s : request->mOutputStreams) { 4351 if (stream == s) return true; 4352 } 4353 if (stream == request->mInputStream) return true; 4354 } 4355 4356 return false; 4357} 4358 4359nsecs_t Camera3Device::getExpectedInFlightDuration() { 4360 ATRACE_CALL(); 4361 Mutex::Autolock al(mInFlightLock); 4362 return mExpectedInflightDuration > kMinInflightDuration ? 4363 mExpectedInflightDuration : kMinInflightDuration; 4364} 4365 4366void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) { 4367 if (mNextRequests.empty()) { 4368 return; 4369 } 4370 4371 for (auto& nextRequest : mNextRequests) { 4372 // Skip the ones that have been submitted successfully. 4373 if (nextRequest.submitted) { 4374 continue; 4375 } 4376 4377 sp<CaptureRequest> captureRequest = nextRequest.captureRequest; 4378 camera3_capture_request_t* halRequest = &nextRequest.halRequest; 4379 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers; 4380 4381 if (halRequest->settings != NULL) { 4382 captureRequest->mSettings.unlock(halRequest->settings); 4383 } 4384 4385 if (captureRequest->mInputStream != NULL) { 4386 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; 4387 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer); 4388 } 4389 4390 for (size_t i = 0; i < halRequest->num_output_buffers; i++) { 4391 //Buffers that failed processing could still have 4392 //valid acquire fence. 4393 int acquireFence = (*outputBuffers)[i].acquire_fence; 4394 if (0 <= acquireFence) { 4395 close(acquireFence); 4396 outputBuffers->editItemAt(i).acquire_fence = -1; 4397 } 4398 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; 4399 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0); 4400 } 4401 4402 if (sendRequestError) { 4403 Mutex::Autolock l(mRequestLock); 4404 sp<NotificationListener> listener = mListener.promote(); 4405 if (listener != NULL) { 4406 listener->notifyError( 4407 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 4408 captureRequest->mResultExtras); 4409 } 4410 } 4411 4412 // Remove yet-to-be submitted inflight request from inflightMap 4413 { 4414 sp<Camera3Device> parent = mParent.promote(); 4415 if (parent != NULL) { 4416 Mutex::Autolock l(parent->mInFlightLock); 4417 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber); 4418 if (idx >= 0) { 4419 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64, 4420 __FUNCTION__, captureRequest->mResultExtras.frameNumber); 4421 parent->removeInFlightMapEntryLocked(idx); 4422 } 4423 } 4424 } 4425 } 4426 4427 Mutex::Autolock l(mRequestLock); 4428 mNextRequests.clear(); 4429} 4430 4431void Camera3Device::RequestThread::waitForNextRequestBatch() { 4432 ATRACE_CALL(); 4433 // Optimized a bit for the simple steady-state case (single repeating 4434 // request), to avoid putting that request in the queue temporarily. 4435 Mutex::Autolock l(mRequestLock); 4436 4437 assert(mNextRequests.empty()); 4438 4439 NextRequest nextRequest; 4440 nextRequest.captureRequest = waitForNextRequestLocked(); 4441 if (nextRequest.captureRequest == nullptr) { 4442 return; 4443 } 4444 4445 nextRequest.halRequest = camera3_capture_request_t(); 4446 nextRequest.submitted = false; 4447 mNextRequests.add(nextRequest); 4448 4449 // Wait for additional requests 4450 const size_t batchSize = nextRequest.captureRequest->mBatchSize; 4451 4452 for (size_t i = 1; i < batchSize; i++) { 4453 NextRequest additionalRequest; 4454 additionalRequest.captureRequest = waitForNextRequestLocked(); 4455 if (additionalRequest.captureRequest == nullptr) { 4456 break; 4457 } 4458 4459 additionalRequest.halRequest = camera3_capture_request_t(); 4460 additionalRequest.submitted = false; 4461 mNextRequests.add(additionalRequest); 4462 } 4463 4464 if (mNextRequests.size() < batchSize) { 4465 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.", 4466 mNextRequests.size(), batchSize); 4467 cleanUpFailedRequests(/*sendRequestError*/true); 4468 } 4469 4470 return; 4471} 4472 4473sp<Camera3Device::CaptureRequest> 4474 Camera3Device::RequestThread::waitForNextRequestLocked() { 4475 status_t res; 4476 sp<CaptureRequest> nextRequest; 4477 4478 while (mRequestQueue.empty()) { 4479 if (!mRepeatingRequests.empty()) { 4480 // Always atomically enqueue all requests in a repeating request 4481 // list. Guarantees a complete in-sequence set of captures to 4482 // application. 4483 const RequestList &requests = mRepeatingRequests; 4484 RequestList::const_iterator firstRequest = 4485 requests.begin(); 4486 nextRequest = *firstRequest; 4487 mRequestQueue.insert(mRequestQueue.end(), 4488 ++firstRequest, 4489 requests.end()); 4490 // No need to wait any longer 4491 4492 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1; 4493 4494 break; 4495 } 4496 4497 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); 4498 4499 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) || 4500 exitPending()) { 4501 Mutex::Autolock pl(mPauseLock); 4502 if (mPaused == false) { 4503 ALOGV("%s: RequestThread: Going idle", __FUNCTION__); 4504 mPaused = true; 4505 // Let the tracker know 4506 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 4507 if (statusTracker != 0) { 4508 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); 4509 } 4510 } 4511 // Stop waiting for now and let thread management happen 4512 return NULL; 4513 } 4514 } 4515 4516 if (nextRequest == NULL) { 4517 // Don't have a repeating request already in hand, so queue 4518 // must have an entry now. 4519 RequestList::iterator firstRequest = 4520 mRequestQueue.begin(); 4521 nextRequest = *firstRequest; 4522 mRequestQueue.erase(firstRequest); 4523 if (mRequestQueue.empty() && !nextRequest->mRepeating) { 4524 sp<NotificationListener> listener = mListener.promote(); 4525 if (listener != NULL) { 4526 listener->notifyRequestQueueEmpty(); 4527 } 4528 } 4529 } 4530 4531 // In case we've been unpaused by setPaused clearing mDoPause, need to 4532 // update internal pause state (capture/setRepeatingRequest unpause 4533 // directly). 4534 Mutex::Autolock pl(mPauseLock); 4535 if (mPaused) { 4536 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__); 4537 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 4538 if (statusTracker != 0) { 4539 statusTracker->markComponentActive(mStatusId); 4540 } 4541 } 4542 mPaused = false; 4543 4544 // Check if we've reconfigured since last time, and reset the preview 4545 // request if so. Can't use 'NULL request == repeat' across configure calls. 4546 if (mReconfigured) { 4547 mPrevRequest.clear(); 4548 mReconfigured = false; 4549 } 4550 4551 if (nextRequest != NULL) { 4552 nextRequest->mResultExtras.frameNumber = mFrameNumber++; 4553 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId; 4554 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId; 4555 4556 // Since RequestThread::clear() removes buffers from the input stream, 4557 // get the right buffer here before unlocking mRequestLock 4558 if (nextRequest->mInputStream != NULL) { 4559 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer); 4560 if (res != OK) { 4561 // Can't get input buffer from gralloc queue - this could be due to 4562 // disconnected queue or other producer misbehavior, so not a fatal 4563 // error 4564 ALOGE("%s: Can't get input buffer, skipping request:" 4565 " %s (%d)", __FUNCTION__, strerror(-res), res); 4566 4567 sp<NotificationListener> listener = mListener.promote(); 4568 if (listener != NULL) { 4569 listener->notifyError( 4570 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, 4571 nextRequest->mResultExtras); 4572 } 4573 return NULL; 4574 } 4575 } 4576 } 4577 4578 return nextRequest; 4579} 4580 4581bool Camera3Device::RequestThread::waitIfPaused() { 4582 ATRACE_CALL(); 4583 status_t res; 4584 Mutex::Autolock l(mPauseLock); 4585 while (mDoPause) { 4586 if (mPaused == false) { 4587 mPaused = true; 4588 ALOGV("%s: RequestThread: Paused", __FUNCTION__); 4589 // Let the tracker know 4590 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 4591 if (statusTracker != 0) { 4592 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); 4593 } 4594 } 4595 4596 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); 4597 if (res == TIMED_OUT || exitPending()) { 4598 return true; 4599 } 4600 } 4601 // We don't set mPaused to false here, because waitForNextRequest needs 4602 // to further manage the paused state in case of starvation. 4603 return false; 4604} 4605 4606void Camera3Device::RequestThread::unpauseForNewRequests() { 4607 ATRACE_CALL(); 4608 // With work to do, mark thread as unpaused. 4609 // If paused by request (setPaused), don't resume, to avoid 4610 // extra signaling/waiting overhead to waitUntilPaused 4611 mRequestSignal.signal(); 4612 Mutex::Autolock p(mPauseLock); 4613 if (!mDoPause) { 4614 ALOGV("%s: RequestThread: Going active", __FUNCTION__); 4615 if (mPaused) { 4616 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 4617 if (statusTracker != 0) { 4618 statusTracker->markComponentActive(mStatusId); 4619 } 4620 } 4621 mPaused = false; 4622 } 4623} 4624 4625void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { 4626 sp<Camera3Device> parent = mParent.promote(); 4627 if (parent != NULL) { 4628 va_list args; 4629 va_start(args, fmt); 4630 4631 parent->setErrorStateV(fmt, args); 4632 4633 va_end(args); 4634 } 4635} 4636 4637status_t Camera3Device::RequestThread::insertTriggers( 4638 const sp<CaptureRequest> &request) { 4639 ATRACE_CALL(); 4640 Mutex::Autolock al(mTriggerMutex); 4641 4642 sp<Camera3Device> parent = mParent.promote(); 4643 if (parent == NULL) { 4644 CLOGE("RequestThread: Parent is gone"); 4645 return DEAD_OBJECT; 4646 } 4647 4648 CameraMetadata &metadata = request->mSettings; 4649 size_t count = mTriggerMap.size(); 4650 4651 for (size_t i = 0; i < count; ++i) { 4652 RequestTrigger trigger = mTriggerMap.valueAt(i); 4653 uint32_t tag = trigger.metadataTag; 4654 4655 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) { 4656 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID); 4657 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue); 4658 if (isAeTrigger) { 4659 request->mResultExtras.precaptureTriggerId = triggerId; 4660 mCurrentPreCaptureTriggerId = triggerId; 4661 } else { 4662 request->mResultExtras.afTriggerId = triggerId; 4663 mCurrentAfTriggerId = triggerId; 4664 } 4665 continue; 4666 } 4667 4668 camera_metadata_entry entry = metadata.find(tag); 4669 4670 if (entry.count > 0) { 4671 /** 4672 * Already has an entry for this trigger in the request. 4673 * Rewrite it with our requested trigger value. 4674 */ 4675 RequestTrigger oldTrigger = trigger; 4676 4677 oldTrigger.entryValue = entry.data.u8[0]; 4678 4679 mTriggerReplacedMap.add(tag, oldTrigger); 4680 } else { 4681 /** 4682 * More typical, no trigger entry, so we just add it 4683 */ 4684 mTriggerRemovedMap.add(tag, trigger); 4685 } 4686 4687 status_t res; 4688 4689 switch (trigger.getTagType()) { 4690 case TYPE_BYTE: { 4691 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 4692 res = metadata.update(tag, 4693 &entryValue, 4694 /*count*/1); 4695 break; 4696 } 4697 case TYPE_INT32: 4698 res = metadata.update(tag, 4699 &trigger.entryValue, 4700 /*count*/1); 4701 break; 4702 default: 4703 ALOGE("%s: Type not supported: 0x%x", 4704 __FUNCTION__, 4705 trigger.getTagType()); 4706 return INVALID_OPERATION; 4707 } 4708 4709 if (res != OK) { 4710 ALOGE("%s: Failed to update request metadata with trigger tag %s" 4711 ", value %d", __FUNCTION__, trigger.getTagName(), 4712 trigger.entryValue); 4713 return res; 4714 } 4715 4716 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, 4717 trigger.getTagName(), 4718 trigger.entryValue); 4719 } 4720 4721 mTriggerMap.clear(); 4722 4723 return count; 4724} 4725 4726status_t Camera3Device::RequestThread::removeTriggers( 4727 const sp<CaptureRequest> &request) { 4728 ATRACE_CALL(); 4729 Mutex::Autolock al(mTriggerMutex); 4730 4731 CameraMetadata &metadata = request->mSettings; 4732 4733 /** 4734 * Replace all old entries with their old values. 4735 */ 4736 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { 4737 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); 4738 4739 status_t res; 4740 4741 uint32_t tag = trigger.metadataTag; 4742 switch (trigger.getTagType()) { 4743 case TYPE_BYTE: { 4744 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 4745 res = metadata.update(tag, 4746 &entryValue, 4747 /*count*/1); 4748 break; 4749 } 4750 case TYPE_INT32: 4751 res = metadata.update(tag, 4752 &trigger.entryValue, 4753 /*count*/1); 4754 break; 4755 default: 4756 ALOGE("%s: Type not supported: 0x%x", 4757 __FUNCTION__, 4758 trigger.getTagType()); 4759 return INVALID_OPERATION; 4760 } 4761 4762 if (res != OK) { 4763 ALOGE("%s: Failed to restore request metadata with trigger tag %s" 4764 ", trigger value %d", __FUNCTION__, 4765 trigger.getTagName(), trigger.entryValue); 4766 return res; 4767 } 4768 } 4769 mTriggerReplacedMap.clear(); 4770 4771 /** 4772 * Remove all new entries. 4773 */ 4774 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { 4775 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); 4776 status_t res = metadata.erase(trigger.metadataTag); 4777 4778 if (res != OK) { 4779 ALOGE("%s: Failed to erase metadata with trigger tag %s" 4780 ", trigger value %d", __FUNCTION__, 4781 trigger.getTagName(), trigger.entryValue); 4782 return res; 4783 } 4784 } 4785 mTriggerRemovedMap.clear(); 4786 4787 return OK; 4788} 4789 4790status_t Camera3Device::RequestThread::addDummyTriggerIds( 4791 const sp<CaptureRequest> &request) { 4792 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here 4793 static const int32_t dummyTriggerId = 1; 4794 status_t res; 4795 4796 CameraMetadata &metadata = request->mSettings; 4797 4798 // If AF trigger is active, insert a dummy AF trigger ID if none already 4799 // exists 4800 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER); 4801 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID); 4802 if (afTrigger.count > 0 && 4803 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE && 4804 afId.count == 0) { 4805 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1); 4806 if (res != OK) return res; 4807 } 4808 4809 // If AE precapture trigger is active, insert a dummy precapture trigger ID 4810 // if none already exists 4811 camera_metadata_entry pcTrigger = 4812 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); 4813 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); 4814 if (pcTrigger.count > 0 && 4815 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE && 4816 pcId.count == 0) { 4817 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID, 4818 &dummyTriggerId, 1); 4819 if (res != OK) return res; 4820 } 4821 4822 return OK; 4823} 4824 4825/** 4826 * PreparerThread inner class methods 4827 */ 4828 4829Camera3Device::PreparerThread::PreparerThread() : 4830 Thread(/*canCallJava*/false), mListener(nullptr), 4831 mActive(false), mCancelNow(false) { 4832} 4833 4834Camera3Device::PreparerThread::~PreparerThread() { 4835 Thread::requestExitAndWait(); 4836 if (mCurrentStream != nullptr) { 4837 mCurrentStream->cancelPrepare(); 4838 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); 4839 mCurrentStream.clear(); 4840 } 4841 clear(); 4842} 4843 4844status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) { 4845 ATRACE_CALL(); 4846 status_t res; 4847 4848 Mutex::Autolock l(mLock); 4849 sp<NotificationListener> listener = mListener.promote(); 4850 4851 res = stream->startPrepare(maxCount); 4852 if (res == OK) { 4853 // No preparation needed, fire listener right off 4854 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId()); 4855 if (listener != NULL) { 4856 listener->notifyPrepared(stream->getId()); 4857 } 4858 return OK; 4859 } else if (res != NOT_ENOUGH_DATA) { 4860 return res; 4861 } 4862 4863 // Need to prepare, start up thread if necessary 4864 if (!mActive) { 4865 // mRunning will change to false before the thread fully shuts down, so wait to be sure it 4866 // isn't running 4867 Thread::requestExitAndWait(); 4868 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND); 4869 if (res != OK) { 4870 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res)); 4871 if (listener != NULL) { 4872 listener->notifyPrepared(stream->getId()); 4873 } 4874 return res; 4875 } 4876 mCancelNow = false; 4877 mActive = true; 4878 ALOGV("%s: Preparer stream started", __FUNCTION__); 4879 } 4880 4881 // queue up the work 4882 mPendingStreams.push_back(stream); 4883 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId()); 4884 4885 return OK; 4886} 4887 4888status_t Camera3Device::PreparerThread::clear() { 4889 ATRACE_CALL(); 4890 Mutex::Autolock l(mLock); 4891 4892 for (const auto& stream : mPendingStreams) { 4893 stream->cancelPrepare(); 4894 } 4895 mPendingStreams.clear(); 4896 mCancelNow = true; 4897 4898 return OK; 4899} 4900 4901void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) { 4902 ATRACE_CALL(); 4903 Mutex::Autolock l(mLock); 4904 mListener = listener; 4905} 4906 4907bool Camera3Device::PreparerThread::threadLoop() { 4908 status_t res; 4909 { 4910 Mutex::Autolock l(mLock); 4911 if (mCurrentStream == nullptr) { 4912 // End thread if done with work 4913 if (mPendingStreams.empty()) { 4914 ALOGV("%s: Preparer stream out of work", __FUNCTION__); 4915 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would 4916 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive. 4917 mActive = false; 4918 return false; 4919 } 4920 4921 // Get next stream to prepare 4922 auto it = mPendingStreams.begin(); 4923 mCurrentStream = *it; 4924 mPendingStreams.erase(it); 4925 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId()); 4926 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId()); 4927 } else if (mCancelNow) { 4928 mCurrentStream->cancelPrepare(); 4929 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); 4930 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId()); 4931 mCurrentStream.clear(); 4932 mCancelNow = false; 4933 return true; 4934 } 4935 } 4936 4937 res = mCurrentStream->prepareNextBuffer(); 4938 if (res == NOT_ENOUGH_DATA) return true; 4939 if (res != OK) { 4940 // Something bad happened; try to recover by cancelling prepare and 4941 // signalling listener anyway 4942 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__, 4943 mCurrentStream->getId(), res, strerror(-res)); 4944 mCurrentStream->cancelPrepare(); 4945 } 4946 4947 // This stream has finished, notify listener 4948 Mutex::Autolock l(mLock); 4949 sp<NotificationListener> listener = mListener.promote(); 4950 if (listener != NULL) { 4951 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__, 4952 mCurrentStream->getId()); 4953 listener->notifyPrepared(mCurrentStream->getId()); 4954 } 4955 4956 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); 4957 mCurrentStream.clear(); 4958 4959 return true; 4960} 4961 4962/** 4963 * Static callback forwarding methods from HAL to instance 4964 */ 4965 4966void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, 4967 const camera3_capture_result *result) { 4968 Camera3Device *d = 4969 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 4970 4971 d->processCaptureResult(result); 4972} 4973 4974void Camera3Device::sNotify(const camera3_callback_ops *cb, 4975 const camera3_notify_msg *msg) { 4976 Camera3Device *d = 4977 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 4978 d->notify(msg); 4979} 4980 4981}; // namespace android 4982