Camera3Device.cpp revision 95dd5ba5bf83716f2eed5fe72366c4212464d710
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_TAG "Camera3-Device" 18#define ATRACE_TAG ATRACE_TAG_CAMERA 19//#define LOG_NDEBUG 0 20//#define LOG_NNDEBUG 0 // Per-frame verbose logging 21 22#ifdef LOG_NNDEBUG 23#define ALOGVV(...) ALOGV(__VA_ARGS__) 24#else 25#define ALOGVV(...) ((void)0) 26#endif 27 28// Convenience macro for transient errors 29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \ 30 ##__VA_ARGS__) 31 32// Convenience macros for transitioning to the error state 33#define SET_ERR(fmt, ...) setErrorState( \ 34 "%s: " fmt, __FUNCTION__, \ 35 ##__VA_ARGS__) 36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \ 37 "%s: " fmt, __FUNCTION__, \ 38 ##__VA_ARGS__) 39 40#include <inttypes.h> 41 42#include <utils/Log.h> 43#include <utils/Trace.h> 44#include <utils/Timers.h> 45 46#include "utils/CameraTraces.h" 47#include "device3/Camera3Device.h" 48#include "device3/Camera3OutputStream.h" 49#include "device3/Camera3InputStream.h" 50#include "device3/Camera3ZslStream.h" 51 52using namespace android::camera3; 53 54namespace android { 55 56Camera3Device::Camera3Device(int id): 57 mId(id), 58 mHal3Device(NULL), 59 mStatus(STATUS_UNINITIALIZED), 60 mUsePartialResultQuirk(false), 61 mNextResultFrameNumber(0), 62 mNextShutterFrameNumber(0), 63 mListener(NULL) 64{ 65 ATRACE_CALL(); 66 camera3_callback_ops::notify = &sNotify; 67 camera3_callback_ops::process_capture_result = &sProcessCaptureResult; 68 ALOGV("%s: Created device for camera %d", __FUNCTION__, id); 69} 70 71Camera3Device::~Camera3Device() 72{ 73 ATRACE_CALL(); 74 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId); 75 disconnect(); 76} 77 78int Camera3Device::getId() const { 79 return mId; 80} 81 82/** 83 * CameraDeviceBase interface 84 */ 85 86status_t Camera3Device::initialize(camera_module_t *module) 87{ 88 ATRACE_CALL(); 89 Mutex::Autolock il(mInterfaceLock); 90 Mutex::Autolock l(mLock); 91 92 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId); 93 if (mStatus != STATUS_UNINITIALIZED) { 94 CLOGE("Already initialized!"); 95 return INVALID_OPERATION; 96 } 97 98 /** Open HAL device */ 99 100 status_t res; 101 String8 deviceName = String8::format("%d", mId); 102 103 camera3_device_t *device; 104 105 ATRACE_BEGIN("camera3->open"); 106 res = module->common.methods->open(&module->common, deviceName.string(), 107 reinterpret_cast<hw_device_t**>(&device)); 108 ATRACE_END(); 109 110 if (res != OK) { 111 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res); 112 return res; 113 } 114 115 /** Cross-check device version */ 116 117 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) { 118 SET_ERR_L("Could not open camera: " 119 "Camera device should be at least %x, reports %x instead", 120 CAMERA_DEVICE_API_VERSION_3_0, 121 device->common.version); 122 device->common.close(&device->common); 123 return BAD_VALUE; 124 } 125 126 camera_info info; 127 res = module->get_camera_info(mId, &info); 128 if (res != OK) return res; 129 130 if (info.device_version != device->common.version) { 131 SET_ERR_L("HAL reporting mismatched camera_info version (%x)" 132 " and device version (%x).", 133 info.device_version, device->common.version); 134 device->common.close(&device->common); 135 return BAD_VALUE; 136 } 137 138 /** Initialize device with callback functions */ 139 140 ATRACE_BEGIN("camera3->initialize"); 141 res = device->ops->initialize(device, this); 142 ATRACE_END(); 143 144 if (res != OK) { 145 SET_ERR_L("Unable to initialize HAL device: %s (%d)", 146 strerror(-res), res); 147 device->common.close(&device->common); 148 return BAD_VALUE; 149 } 150 151 /** Start up status tracker thread */ 152 mStatusTracker = new StatusTracker(this); 153 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string()); 154 if (res != OK) { 155 SET_ERR_L("Unable to start status tracking thread: %s (%d)", 156 strerror(-res), res); 157 device->common.close(&device->common); 158 mStatusTracker.clear(); 159 return res; 160 } 161 162 /** Start up request queue thread */ 163 164 mRequestThread = new RequestThread(this, mStatusTracker, device); 165 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string()); 166 if (res != OK) { 167 SET_ERR_L("Unable to start request queue thread: %s (%d)", 168 strerror(-res), res); 169 device->common.close(&device->common); 170 mRequestThread.clear(); 171 return res; 172 } 173 174 /** Everything is good to go */ 175 176 mDeviceInfo = info.static_camera_characteristics; 177 mHal3Device = device; 178 mStatus = STATUS_UNCONFIGURED; 179 mNextStreamId = 0; 180 mNeedConfig = true; 181 mPauseStateNotify = false; 182 183 /** Check for quirks */ 184 185 // Will the HAL be sending in early partial result metadata? 186 camera_metadata_entry partialResultsQuirk = 187 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT); 188 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) { 189 mUsePartialResultQuirk = true; 190 } 191 192 return OK; 193} 194 195status_t Camera3Device::disconnect() { 196 ATRACE_CALL(); 197 Mutex::Autolock il(mInterfaceLock); 198 199 ALOGV("%s: E", __FUNCTION__); 200 201 status_t res = OK; 202 203 { 204 Mutex::Autolock l(mLock); 205 if (mStatus == STATUS_UNINITIALIZED) return res; 206 207 if (mStatus == STATUS_ACTIVE || 208 (mStatus == STATUS_ERROR && mRequestThread != NULL)) { 209 res = mRequestThread->clearRepeatingRequests(); 210 if (res != OK) { 211 SET_ERR_L("Can't stop streaming"); 212 // Continue to close device even in case of error 213 } else { 214 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); 215 if (res != OK) { 216 SET_ERR_L("Timeout waiting for HAL to drain"); 217 // Continue to close device even in case of error 218 } 219 } 220 } 221 222 if (mStatus == STATUS_ERROR) { 223 CLOGE("Shutting down in an error state"); 224 } 225 226 if (mStatusTracker != NULL) { 227 mStatusTracker->requestExit(); 228 } 229 230 if (mRequestThread != NULL) { 231 mRequestThread->requestExit(); 232 } 233 234 mOutputStreams.clear(); 235 mInputStream.clear(); 236 } 237 238 // Joining done without holding mLock, otherwise deadlocks may ensue 239 // as the threads try to access parent state 240 if (mRequestThread != NULL && mStatus != STATUS_ERROR) { 241 // HAL may be in a bad state, so waiting for request thread 242 // (which may be stuck in the HAL processCaptureRequest call) 243 // could be dangerous. 244 mRequestThread->join(); 245 } 246 247 if (mStatusTracker != NULL) { 248 mStatusTracker->join(); 249 } 250 251 { 252 Mutex::Autolock l(mLock); 253 254 mRequestThread.clear(); 255 mStatusTracker.clear(); 256 257 if (mHal3Device != NULL) { 258 ATRACE_BEGIN("camera3->close"); 259 mHal3Device->common.close(&mHal3Device->common); 260 ATRACE_END(); 261 mHal3Device = NULL; 262 } 263 264 mStatus = STATUS_UNINITIALIZED; 265 } 266 267 ALOGV("%s: X", __FUNCTION__); 268 return res; 269} 270 271// For dumping/debugging only - 272// try to acquire a lock a few times, eventually give up to proceed with 273// debug/dump operations 274bool Camera3Device::tryLockSpinRightRound(Mutex& lock) { 275 bool gotLock = false; 276 for (size_t i = 0; i < kDumpLockAttempts; ++i) { 277 if (lock.tryLock() == NO_ERROR) { 278 gotLock = true; 279 break; 280 } else { 281 usleep(kDumpSleepDuration); 282 } 283 } 284 return gotLock; 285} 286 287status_t Camera3Device::dump(int fd, const Vector<String16> &args) { 288 ATRACE_CALL(); 289 (void)args; 290 291 // Try to lock, but continue in case of failure (to avoid blocking in 292 // deadlocks) 293 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock); 294 bool gotLock = tryLockSpinRightRound(mLock); 295 296 ALOGW_IF(!gotInterfaceLock, 297 "Camera %d: %s: Unable to lock interface lock, proceeding anyway", 298 mId, __FUNCTION__); 299 ALOGW_IF(!gotLock, 300 "Camera %d: %s: Unable to lock main lock, proceeding anyway", 301 mId, __FUNCTION__); 302 303 String8 lines; 304 305 const char *status = 306 mStatus == STATUS_ERROR ? "ERROR" : 307 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : 308 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" : 309 mStatus == STATUS_CONFIGURED ? "CONFIGURED" : 310 mStatus == STATUS_ACTIVE ? "ACTIVE" : 311 "Unknown"; 312 313 lines.appendFormat(" Device status: %s\n", status); 314 if (mStatus == STATUS_ERROR) { 315 lines.appendFormat(" Error cause: %s\n", mErrorCause.string()); 316 } 317 lines.appendFormat(" Stream configuration:\n"); 318 319 if (mInputStream != NULL) { 320 write(fd, lines.string(), lines.size()); 321 mInputStream->dump(fd, args); 322 } else { 323 lines.appendFormat(" No input stream.\n"); 324 write(fd, lines.string(), lines.size()); 325 } 326 for (size_t i = 0; i < mOutputStreams.size(); i++) { 327 mOutputStreams[i]->dump(fd,args); 328 } 329 330 lines = String8(" In-flight requests:\n"); 331 if (mInFlightMap.size() == 0) { 332 lines.append(" None\n"); 333 } else { 334 for (size_t i = 0; i < mInFlightMap.size(); i++) { 335 InFlightRequest r = mInFlightMap.valueAt(i); 336 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata" 337 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), 338 r.captureTimestamp, r.haveResultMetadata ? "true" : "false", 339 r.numBuffersLeft); 340 } 341 } 342 write(fd, lines.string(), lines.size()); 343 344 { 345 lines = String8(" Last request sent:\n"); 346 write(fd, lines.string(), lines.size()); 347 348 CameraMetadata lastRequest = getLatestRequestLocked(); 349 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6); 350 } 351 352 if (mHal3Device != NULL) { 353 lines = String8(" HAL device dump:\n"); 354 write(fd, lines.string(), lines.size()); 355 mHal3Device->ops->dump(mHal3Device, fd); 356 } 357 358 if (gotLock) mLock.unlock(); 359 if (gotInterfaceLock) mInterfaceLock.unlock(); 360 361 return OK; 362} 363 364const CameraMetadata& Camera3Device::info() const { 365 ALOGVV("%s: E", __FUNCTION__); 366 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || 367 mStatus == STATUS_ERROR)) { 368 ALOGW("%s: Access to static info %s!", __FUNCTION__, 369 mStatus == STATUS_ERROR ? 370 "when in error state" : "before init"); 371 } 372 return mDeviceInfo; 373} 374 375status_t Camera3Device::checkStatusOkToCaptureLocked() { 376 switch (mStatus) { 377 case STATUS_ERROR: 378 CLOGE("Device has encountered a serious error"); 379 return INVALID_OPERATION; 380 case STATUS_UNINITIALIZED: 381 CLOGE("Device not initialized"); 382 return INVALID_OPERATION; 383 case STATUS_UNCONFIGURED: 384 case STATUS_CONFIGURED: 385 case STATUS_ACTIVE: 386 // OK 387 break; 388 default: 389 SET_ERR_L("Unexpected status: %d", mStatus); 390 return INVALID_OPERATION; 391 } 392 return OK; 393} 394 395status_t Camera3Device::convertMetadataListToRequestListLocked( 396 const List<const CameraMetadata> &metadataList, RequestList *requestList) { 397 if (requestList == NULL) { 398 CLOGE("requestList cannot be NULL."); 399 return BAD_VALUE; 400 } 401 402 for (List<const CameraMetadata>::const_iterator it = metadataList.begin(); 403 it != metadataList.end(); ++it) { 404 sp<CaptureRequest> newRequest = setUpRequestLocked(*it); 405 if (newRequest == 0) { 406 CLOGE("Can't create capture request"); 407 return BAD_VALUE; 408 } 409 requestList->push_back(newRequest); 410 } 411 return OK; 412} 413 414status_t Camera3Device::capture(CameraMetadata &request) { 415 ATRACE_CALL(); 416 status_t res; 417 Mutex::Autolock il(mInterfaceLock); 418 Mutex::Autolock l(mLock); 419 420 // TODO: take ownership of the request 421 422 switch (mStatus) { 423 case STATUS_ERROR: 424 CLOGE("Device has encountered a serious error"); 425 return INVALID_OPERATION; 426 case STATUS_UNINITIALIZED: 427 CLOGE("Device not initialized"); 428 return INVALID_OPERATION; 429 case STATUS_UNCONFIGURED: 430 // May be lazily configuring streams, will check during setup 431 case STATUS_CONFIGURED: 432 case STATUS_ACTIVE: 433 // OK 434 break; 435 default: 436 SET_ERR_L("Unexpected status: %d", mStatus); 437 return INVALID_OPERATION; 438 } 439 440 sp<CaptureRequest> newRequest = setUpRequestLocked(request); 441 if (newRequest == NULL) { 442 CLOGE("Can't create capture request"); 443 return BAD_VALUE; 444 } 445 446 res = mRequestThread->queueRequest(newRequest); 447 if (res == OK) { 448 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); 449 if (res != OK) { 450 SET_ERR_L("Can't transition to active in %f seconds!", 451 kActiveTimeout/1e9); 452 } 453 ALOGV("Camera %d: Capture request enqueued", mId); 454 } else { 455 CLOGE("Cannot queue request. Impossible."); // queueRequest always returns OK. 456 return BAD_VALUE; 457 } 458 return res; 459} 460 461status_t Camera3Device::submitRequestsHelper( 462 const List<const CameraMetadata> &requests, bool repeating) { 463 ATRACE_CALL(); 464 Mutex::Autolock il(mInterfaceLock); 465 Mutex::Autolock l(mLock); 466 467 status_t res = checkStatusOkToCaptureLocked(); 468 if (res != OK) { 469 // error logged by previous call 470 return res; 471 } 472 473 RequestList requestList; 474 475 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList); 476 if (res != OK) { 477 // error logged by previous call 478 return res; 479 } 480 481 if (repeating) { 482 res = mRequestThread->setRepeatingRequests(requestList); 483 } else { 484 res = mRequestThread->queueRequestList(requestList); 485 } 486 487 if (res == OK) { 488 waitUntilStateThenRelock(/*active*/true, kActiveTimeout); 489 if (res != OK) { 490 SET_ERR_L("Can't transition to active in %f seconds!", 491 kActiveTimeout/1e9); 492 } 493 ALOGV("Camera %d: Capture request enqueued", mId); 494 } else { 495 CLOGE("Cannot queue request. Impossible."); 496 return BAD_VALUE; 497 } 498 499 return res; 500} 501 502status_t Camera3Device::captureList(const List<const CameraMetadata> &requests) { 503 ATRACE_CALL(); 504 505 return submitRequestsHelper(requests, /*repeating*/false); 506} 507 508status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) { 509 ATRACE_CALL(); 510 status_t res; 511 Mutex::Autolock il(mInterfaceLock); 512 Mutex::Autolock l(mLock); 513 514 switch (mStatus) { 515 case STATUS_ERROR: 516 CLOGE("Device has encountered a serious error"); 517 return INVALID_OPERATION; 518 case STATUS_UNINITIALIZED: 519 CLOGE("Device not initialized"); 520 return INVALID_OPERATION; 521 case STATUS_UNCONFIGURED: 522 // May be lazily configuring streams, will check during setup 523 case STATUS_CONFIGURED: 524 case STATUS_ACTIVE: 525 // OK 526 break; 527 default: 528 SET_ERR_L("Unexpected status: %d", mStatus); 529 return INVALID_OPERATION; 530 } 531 532 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request); 533 if (newRepeatingRequest == NULL) { 534 CLOGE("Can't create repeating request"); 535 return BAD_VALUE; 536 } 537 538 RequestList newRepeatingRequests; 539 newRepeatingRequests.push_back(newRepeatingRequest); 540 541 res = mRequestThread->setRepeatingRequests(newRepeatingRequests); 542 if (res == OK) { 543 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); 544 if (res != OK) { 545 SET_ERR_L("Can't transition to active in %f seconds!", 546 kActiveTimeout/1e9); 547 } 548 ALOGV("Camera %d: Repeating request set", mId); 549 } 550 return res; 551} 552 553status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests) { 554 ATRACE_CALL(); 555 556 return submitRequestsHelper(requests, /*repeating*/true); 557} 558 559sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( 560 const CameraMetadata &request) { 561 status_t res; 562 563 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) { 564 res = configureStreamsLocked(); 565 if (res != OK) { 566 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res); 567 return NULL; 568 } 569 if (mStatus == STATUS_UNCONFIGURED) { 570 CLOGE("No streams configured"); 571 return NULL; 572 } 573 } 574 575 sp<CaptureRequest> newRequest = createCaptureRequest(request); 576 return newRequest; 577} 578 579status_t Camera3Device::clearStreamingRequest() { 580 ATRACE_CALL(); 581 Mutex::Autolock il(mInterfaceLock); 582 Mutex::Autolock l(mLock); 583 584 switch (mStatus) { 585 case STATUS_ERROR: 586 CLOGE("Device has encountered a serious error"); 587 return INVALID_OPERATION; 588 case STATUS_UNINITIALIZED: 589 CLOGE("Device not initialized"); 590 return INVALID_OPERATION; 591 case STATUS_UNCONFIGURED: 592 case STATUS_CONFIGURED: 593 case STATUS_ACTIVE: 594 // OK 595 break; 596 default: 597 SET_ERR_L("Unexpected status: %d", mStatus); 598 return INVALID_OPERATION; 599 } 600 ALOGV("Camera %d: Clearing repeating request", mId); 601 return mRequestThread->clearRepeatingRequests(); 602} 603 604status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { 605 ATRACE_CALL(); 606 Mutex::Autolock il(mInterfaceLock); 607 608 return mRequestThread->waitUntilRequestProcessed(requestId, timeout); 609} 610 611status_t Camera3Device::createInputStream( 612 uint32_t width, uint32_t height, int format, int *id) { 613 ATRACE_CALL(); 614 Mutex::Autolock il(mInterfaceLock); 615 Mutex::Autolock l(mLock); 616 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d", 617 mId, mNextStreamId, width, height, format); 618 619 status_t res; 620 bool wasActive = false; 621 622 switch (mStatus) { 623 case STATUS_ERROR: 624 ALOGE("%s: Device has encountered a serious error", __FUNCTION__); 625 return INVALID_OPERATION; 626 case STATUS_UNINITIALIZED: 627 ALOGE("%s: Device not initialized", __FUNCTION__); 628 return INVALID_OPERATION; 629 case STATUS_UNCONFIGURED: 630 case STATUS_CONFIGURED: 631 // OK 632 break; 633 case STATUS_ACTIVE: 634 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 635 res = internalPauseAndWaitLocked(); 636 if (res != OK) { 637 SET_ERR_L("Can't pause captures to reconfigure streams!"); 638 return res; 639 } 640 wasActive = true; 641 break; 642 default: 643 SET_ERR_L("%s: Unexpected status: %d", mStatus); 644 return INVALID_OPERATION; 645 } 646 assert(mStatus != STATUS_ACTIVE); 647 648 if (mInputStream != 0) { 649 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); 650 return INVALID_OPERATION; 651 } 652 653 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, 654 width, height, format); 655 newStream->setStatusTracker(mStatusTracker); 656 657 mInputStream = newStream; 658 659 *id = mNextStreamId++; 660 661 // Continue captures if active at start 662 if (wasActive) { 663 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 664 res = configureStreamsLocked(); 665 if (res != OK) { 666 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", 667 __FUNCTION__, mNextStreamId, strerror(-res), res); 668 return res; 669 } 670 internalResumeLocked(); 671 } 672 673 ALOGV("Camera %d: Created input stream", mId); 674 return OK; 675} 676 677 678status_t Camera3Device::createZslStream( 679 uint32_t width, uint32_t height, 680 int depth, 681 /*out*/ 682 int *id, 683 sp<Camera3ZslStream>* zslStream) { 684 ATRACE_CALL(); 685 Mutex::Autolock il(mInterfaceLock); 686 Mutex::Autolock l(mLock); 687 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d", 688 mId, mNextStreamId, width, height, depth); 689 690 status_t res; 691 bool wasActive = false; 692 693 switch (mStatus) { 694 case STATUS_ERROR: 695 ALOGE("%s: Device has encountered a serious error", __FUNCTION__); 696 return INVALID_OPERATION; 697 case STATUS_UNINITIALIZED: 698 ALOGE("%s: Device not initialized", __FUNCTION__); 699 return INVALID_OPERATION; 700 case STATUS_UNCONFIGURED: 701 case STATUS_CONFIGURED: 702 // OK 703 break; 704 case STATUS_ACTIVE: 705 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 706 res = internalPauseAndWaitLocked(); 707 if (res != OK) { 708 SET_ERR_L("Can't pause captures to reconfigure streams!"); 709 return res; 710 } 711 wasActive = true; 712 break; 713 default: 714 SET_ERR_L("Unexpected status: %d", mStatus); 715 return INVALID_OPERATION; 716 } 717 assert(mStatus != STATUS_ACTIVE); 718 719 if (mInputStream != 0) { 720 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); 721 return INVALID_OPERATION; 722 } 723 724 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId, 725 width, height, depth); 726 newStream->setStatusTracker(mStatusTracker); 727 728 res = mOutputStreams.add(mNextStreamId, newStream); 729 if (res < 0) { 730 ALOGE("%s: Can't add new stream to set: %s (%d)", 731 __FUNCTION__, strerror(-res), res); 732 return res; 733 } 734 mInputStream = newStream; 735 736 *id = mNextStreamId++; 737 *zslStream = newStream; 738 739 // Continue captures if active at start 740 if (wasActive) { 741 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 742 res = configureStreamsLocked(); 743 if (res != OK) { 744 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", 745 __FUNCTION__, mNextStreamId, strerror(-res), res); 746 return res; 747 } 748 internalResumeLocked(); 749 } 750 751 ALOGV("Camera %d: Created ZSL stream", mId); 752 return OK; 753} 754 755status_t Camera3Device::createStream(sp<ANativeWindow> consumer, 756 uint32_t width, uint32_t height, int format, size_t size, int *id) { 757 ATRACE_CALL(); 758 Mutex::Autolock il(mInterfaceLock); 759 Mutex::Autolock l(mLock); 760 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %zu", 761 mId, mNextStreamId, width, height, format, size); 762 763 status_t res; 764 bool wasActive = false; 765 766 switch (mStatus) { 767 case STATUS_ERROR: 768 CLOGE("Device has encountered a serious error"); 769 return INVALID_OPERATION; 770 case STATUS_UNINITIALIZED: 771 CLOGE("Device not initialized"); 772 return INVALID_OPERATION; 773 case STATUS_UNCONFIGURED: 774 case STATUS_CONFIGURED: 775 // OK 776 break; 777 case STATUS_ACTIVE: 778 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 779 res = internalPauseAndWaitLocked(); 780 if (res != OK) { 781 SET_ERR_L("Can't pause captures to reconfigure streams!"); 782 return res; 783 } 784 wasActive = true; 785 break; 786 default: 787 SET_ERR_L("Unexpected status: %d", mStatus); 788 return INVALID_OPERATION; 789 } 790 assert(mStatus != STATUS_ACTIVE); 791 792 sp<Camera3OutputStream> newStream; 793 if (format == HAL_PIXEL_FORMAT_BLOB) { 794 newStream = new Camera3OutputStream(mNextStreamId, consumer, 795 width, height, size, format); 796 } else { 797 newStream = new Camera3OutputStream(mNextStreamId, consumer, 798 width, height, format); 799 } 800 newStream->setStatusTracker(mStatusTracker); 801 802 res = mOutputStreams.add(mNextStreamId, newStream); 803 if (res < 0) { 804 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res); 805 return res; 806 } 807 808 *id = mNextStreamId++; 809 mNeedConfig = true; 810 811 // Continue captures if active at start 812 if (wasActive) { 813 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 814 res = configureStreamsLocked(); 815 if (res != OK) { 816 CLOGE("Can't reconfigure device for new stream %d: %s (%d)", 817 mNextStreamId, strerror(-res), res); 818 return res; 819 } 820 internalResumeLocked(); 821 } 822 ALOGV("Camera %d: Created new stream", mId); 823 return OK; 824} 825 826status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) { 827 ATRACE_CALL(); 828 (void)outputId; (void)id; 829 830 CLOGE("Unimplemented"); 831 return INVALID_OPERATION; 832} 833 834 835status_t Camera3Device::getStreamInfo(int id, 836 uint32_t *width, uint32_t *height, uint32_t *format) { 837 ATRACE_CALL(); 838 Mutex::Autolock il(mInterfaceLock); 839 Mutex::Autolock l(mLock); 840 841 switch (mStatus) { 842 case STATUS_ERROR: 843 CLOGE("Device has encountered a serious error"); 844 return INVALID_OPERATION; 845 case STATUS_UNINITIALIZED: 846 CLOGE("Device not initialized!"); 847 return INVALID_OPERATION; 848 case STATUS_UNCONFIGURED: 849 case STATUS_CONFIGURED: 850 case STATUS_ACTIVE: 851 // OK 852 break; 853 default: 854 SET_ERR_L("Unexpected status: %d", mStatus); 855 return INVALID_OPERATION; 856 } 857 858 ssize_t idx = mOutputStreams.indexOfKey(id); 859 if (idx == NAME_NOT_FOUND) { 860 CLOGE("Stream %d is unknown", id); 861 return idx; 862 } 863 864 if (width) *width = mOutputStreams[idx]->getWidth(); 865 if (height) *height = mOutputStreams[idx]->getHeight(); 866 if (format) *format = mOutputStreams[idx]->getFormat(); 867 868 return OK; 869} 870 871status_t Camera3Device::setStreamTransform(int id, 872 int transform) { 873 ATRACE_CALL(); 874 Mutex::Autolock il(mInterfaceLock); 875 Mutex::Autolock l(mLock); 876 877 switch (mStatus) { 878 case STATUS_ERROR: 879 CLOGE("Device has encountered a serious error"); 880 return INVALID_OPERATION; 881 case STATUS_UNINITIALIZED: 882 CLOGE("Device not initialized"); 883 return INVALID_OPERATION; 884 case STATUS_UNCONFIGURED: 885 case STATUS_CONFIGURED: 886 case STATUS_ACTIVE: 887 // OK 888 break; 889 default: 890 SET_ERR_L("Unexpected status: %d", mStatus); 891 return INVALID_OPERATION; 892 } 893 894 ssize_t idx = mOutputStreams.indexOfKey(id); 895 if (idx == NAME_NOT_FOUND) { 896 CLOGE("Stream %d does not exist", 897 id); 898 return BAD_VALUE; 899 } 900 901 return mOutputStreams.editValueAt(idx)->setTransform(transform); 902} 903 904status_t Camera3Device::deleteStream(int id) { 905 ATRACE_CALL(); 906 Mutex::Autolock il(mInterfaceLock); 907 Mutex::Autolock l(mLock); 908 status_t res; 909 910 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id); 911 912 // CameraDevice semantics require device to already be idle before 913 // deleteStream is called, unlike for createStream. 914 if (mStatus == STATUS_ACTIVE) { 915 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId); 916 return -EBUSY; 917 } 918 919 sp<Camera3StreamInterface> deletedStream; 920 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id); 921 if (mInputStream != NULL && id == mInputStream->getId()) { 922 deletedStream = mInputStream; 923 mInputStream.clear(); 924 } else { 925 if (outputStreamIdx == NAME_NOT_FOUND) { 926 CLOGE("Stream %d does not exist", id); 927 return BAD_VALUE; 928 } 929 } 930 931 // Delete output stream or the output part of a bi-directional stream. 932 if (outputStreamIdx != NAME_NOT_FOUND) { 933 deletedStream = mOutputStreams.editValueAt(outputStreamIdx); 934 mOutputStreams.removeItem(id); 935 } 936 937 // Free up the stream endpoint so that it can be used by some other stream 938 res = deletedStream->disconnect(); 939 if (res != OK) { 940 SET_ERR_L("Can't disconnect deleted stream %d", id); 941 // fall through since we want to still list the stream as deleted. 942 } 943 mDeletedStreams.add(deletedStream); 944 mNeedConfig = true; 945 946 return res; 947} 948 949status_t Camera3Device::deleteReprocessStream(int id) { 950 ATRACE_CALL(); 951 (void)id; 952 953 CLOGE("Unimplemented"); 954 return INVALID_OPERATION; 955} 956 957 958status_t Camera3Device::createDefaultRequest(int templateId, 959 CameraMetadata *request) { 960 ATRACE_CALL(); 961 ALOGV("%s: for template %d", __FUNCTION__, templateId); 962 Mutex::Autolock il(mInterfaceLock); 963 Mutex::Autolock l(mLock); 964 965 switch (mStatus) { 966 case STATUS_ERROR: 967 CLOGE("Device has encountered a serious error"); 968 return INVALID_OPERATION; 969 case STATUS_UNINITIALIZED: 970 CLOGE("Device is not initialized!"); 971 return INVALID_OPERATION; 972 case STATUS_UNCONFIGURED: 973 case STATUS_CONFIGURED: 974 case STATUS_ACTIVE: 975 // OK 976 break; 977 default: 978 SET_ERR_L("Unexpected status: %d", mStatus); 979 return INVALID_OPERATION; 980 } 981 982 const camera_metadata_t *rawRequest; 983 ATRACE_BEGIN("camera3->construct_default_request_settings"); 984 rawRequest = mHal3Device->ops->construct_default_request_settings( 985 mHal3Device, templateId); 986 ATRACE_END(); 987 if (rawRequest == NULL) { 988 SET_ERR_L("HAL is unable to construct default settings for template %d", 989 templateId); 990 return DEAD_OBJECT; 991 } 992 *request = rawRequest; 993 994 return OK; 995} 996 997status_t Camera3Device::waitUntilDrained() { 998 ATRACE_CALL(); 999 Mutex::Autolock il(mInterfaceLock); 1000 Mutex::Autolock l(mLock); 1001 1002 return waitUntilDrainedLocked(); 1003} 1004 1005status_t Camera3Device::waitUntilDrainedLocked() { 1006 switch (mStatus) { 1007 case STATUS_UNINITIALIZED: 1008 case STATUS_UNCONFIGURED: 1009 ALOGV("%s: Already idle", __FUNCTION__); 1010 return OK; 1011 case STATUS_CONFIGURED: 1012 // To avoid race conditions, check with tracker to be sure 1013 case STATUS_ERROR: 1014 case STATUS_ACTIVE: 1015 // Need to verify shut down 1016 break; 1017 default: 1018 SET_ERR_L("Unexpected status: %d",mStatus); 1019 return INVALID_OPERATION; 1020 } 1021 1022 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId); 1023 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); 1024 return res; 1025} 1026 1027// Pause to reconfigure 1028status_t Camera3Device::internalPauseAndWaitLocked() { 1029 mRequestThread->setPaused(true); 1030 mPauseStateNotify = true; 1031 1032 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId); 1033 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); 1034 if (res != OK) { 1035 SET_ERR_L("Can't idle device in %f seconds!", 1036 kShutdownTimeout/1e9); 1037 } 1038 1039 return res; 1040} 1041 1042// Resume after internalPauseAndWaitLocked 1043status_t Camera3Device::internalResumeLocked() { 1044 status_t res; 1045 1046 mRequestThread->setPaused(false); 1047 1048 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); 1049 if (res != OK) { 1050 SET_ERR_L("Can't transition to active in %f seconds!", 1051 kActiveTimeout/1e9); 1052 } 1053 mPauseStateNotify = false; 1054 return OK; 1055} 1056 1057status_t Camera3Device::waitUntilStateThenRelock(bool active, 1058 nsecs_t timeout) { 1059 status_t res = OK; 1060 if (active == (mStatus == STATUS_ACTIVE)) { 1061 // Desired state already reached 1062 return res; 1063 } 1064 1065 bool stateSeen = false; 1066 do { 1067 mRecentStatusUpdates.clear(); 1068 1069 res = mStatusChanged.waitRelative(mLock, timeout); 1070 if (res != OK) break; 1071 1072 // Check state change history during wait 1073 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) { 1074 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) { 1075 stateSeen = true; 1076 break; 1077 } 1078 } 1079 } while (!stateSeen); 1080 1081 return res; 1082} 1083 1084 1085status_t Camera3Device::setNotifyCallback(NotificationListener *listener) { 1086 ATRACE_CALL(); 1087 Mutex::Autolock l(mOutputLock); 1088 1089 if (listener != NULL && mListener != NULL) { 1090 ALOGW("%s: Replacing old callback listener", __FUNCTION__); 1091 } 1092 mListener = listener; 1093 1094 return OK; 1095} 1096 1097bool Camera3Device::willNotify3A() { 1098 return false; 1099} 1100 1101status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { 1102 status_t res; 1103 Mutex::Autolock l(mOutputLock); 1104 1105 while (mResultQueue.empty()) { 1106 res = mResultSignal.waitRelative(mOutputLock, timeout); 1107 if (res == TIMED_OUT) { 1108 return res; 1109 } else if (res != OK) { 1110 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)", 1111 __FUNCTION__, mId, timeout, strerror(-res), res); 1112 return res; 1113 } 1114 } 1115 return OK; 1116} 1117 1118status_t Camera3Device::getNextFrame(CameraMetadata *frame) { 1119 ATRACE_CALL(); 1120 Mutex::Autolock l(mOutputLock); 1121 1122 if (mResultQueue.empty()) { 1123 return NOT_ENOUGH_DATA; 1124 } 1125 1126 CameraMetadata &result = *(mResultQueue.begin()); 1127 frame->acquire(result); 1128 mResultQueue.erase(mResultQueue.begin()); 1129 1130 return OK; 1131} 1132 1133status_t Camera3Device::triggerAutofocus(uint32_t id) { 1134 ATRACE_CALL(); 1135 Mutex::Autolock il(mInterfaceLock); 1136 1137 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); 1138 // Mix-in this trigger into the next request and only the next request. 1139 RequestTrigger trigger[] = { 1140 { 1141 ANDROID_CONTROL_AF_TRIGGER, 1142 ANDROID_CONTROL_AF_TRIGGER_START 1143 }, 1144 { 1145 ANDROID_CONTROL_AF_TRIGGER_ID, 1146 static_cast<int32_t>(id) 1147 }, 1148 }; 1149 1150 return mRequestThread->queueTrigger(trigger, 1151 sizeof(trigger)/sizeof(trigger[0])); 1152} 1153 1154status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { 1155 ATRACE_CALL(); 1156 Mutex::Autolock il(mInterfaceLock); 1157 1158 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); 1159 // Mix-in this trigger into the next request and only the next request. 1160 RequestTrigger trigger[] = { 1161 { 1162 ANDROID_CONTROL_AF_TRIGGER, 1163 ANDROID_CONTROL_AF_TRIGGER_CANCEL 1164 }, 1165 { 1166 ANDROID_CONTROL_AF_TRIGGER_ID, 1167 static_cast<int32_t>(id) 1168 }, 1169 }; 1170 1171 return mRequestThread->queueTrigger(trigger, 1172 sizeof(trigger)/sizeof(trigger[0])); 1173} 1174 1175status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { 1176 ATRACE_CALL(); 1177 Mutex::Autolock il(mInterfaceLock); 1178 1179 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); 1180 // Mix-in this trigger into the next request and only the next request. 1181 RequestTrigger trigger[] = { 1182 { 1183 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, 1184 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START 1185 }, 1186 { 1187 ANDROID_CONTROL_AE_PRECAPTURE_ID, 1188 static_cast<int32_t>(id) 1189 }, 1190 }; 1191 1192 return mRequestThread->queueTrigger(trigger, 1193 sizeof(trigger)/sizeof(trigger[0])); 1194} 1195 1196status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId, 1197 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) { 1198 ATRACE_CALL(); 1199 (void)reprocessStreamId; (void)buffer; (void)listener; 1200 1201 CLOGE("Unimplemented"); 1202 return INVALID_OPERATION; 1203} 1204 1205status_t Camera3Device::flush() { 1206 ATRACE_CALL(); 1207 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId); 1208 Mutex::Autolock il(mInterfaceLock); 1209 Mutex::Autolock l(mLock); 1210 1211 mRequestThread->clear(); 1212 status_t res; 1213 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) { 1214 res = mHal3Device->ops->flush(mHal3Device); 1215 } else { 1216 res = waitUntilDrainedLocked(); 1217 } 1218 1219 return res; 1220} 1221 1222/** 1223 * Methods called by subclasses 1224 */ 1225 1226void Camera3Device::notifyStatus(bool idle) { 1227 { 1228 // Need mLock to safely update state and synchronize to current 1229 // state of methods in flight. 1230 Mutex::Autolock l(mLock); 1231 // We can get various system-idle notices from the status tracker 1232 // while starting up. Only care about them if we've actually sent 1233 // in some requests recently. 1234 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) { 1235 return; 1236 } 1237 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId, 1238 idle ? "idle" : "active"); 1239 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE; 1240 mRecentStatusUpdates.add(mStatus); 1241 mStatusChanged.signal(); 1242 1243 // Skip notifying listener if we're doing some user-transparent 1244 // state changes 1245 if (mPauseStateNotify) return; 1246 } 1247 NotificationListener *listener; 1248 { 1249 Mutex::Autolock l(mOutputLock); 1250 listener = mListener; 1251 } 1252 if (idle && listener != NULL) { 1253 listener->notifyIdle(); 1254 } 1255} 1256 1257/** 1258 * Camera3Device private methods 1259 */ 1260 1261sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( 1262 const CameraMetadata &request) { 1263 ATRACE_CALL(); 1264 status_t res; 1265 1266 sp<CaptureRequest> newRequest = new CaptureRequest; 1267 newRequest->mSettings = request; 1268 1269 camera_metadata_entry_t inputStreams = 1270 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); 1271 if (inputStreams.count > 0) { 1272 if (mInputStream == NULL || 1273 mInputStream->getId() != inputStreams.data.i32[0]) { 1274 CLOGE("Request references unknown input stream %d", 1275 inputStreams.data.u8[0]); 1276 return NULL; 1277 } 1278 // Lazy completion of stream configuration (allocation/registration) 1279 // on first use 1280 if (mInputStream->isConfiguring()) { 1281 res = mInputStream->finishConfiguration(mHal3Device); 1282 if (res != OK) { 1283 SET_ERR_L("Unable to finish configuring input stream %d:" 1284 " %s (%d)", 1285 mInputStream->getId(), strerror(-res), res); 1286 return NULL; 1287 } 1288 } 1289 1290 newRequest->mInputStream = mInputStream; 1291 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); 1292 } 1293 1294 camera_metadata_entry_t streams = 1295 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); 1296 if (streams.count == 0) { 1297 CLOGE("Zero output streams specified!"); 1298 return NULL; 1299 } 1300 1301 for (size_t i = 0; i < streams.count; i++) { 1302 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]); 1303 if (idx == NAME_NOT_FOUND) { 1304 CLOGE("Request references unknown stream %d", 1305 streams.data.u8[i]); 1306 return NULL; 1307 } 1308 sp<Camera3OutputStreamInterface> stream = 1309 mOutputStreams.editValueAt(idx); 1310 1311 // Lazy completion of stream configuration (allocation/registration) 1312 // on first use 1313 if (stream->isConfiguring()) { 1314 res = stream->finishConfiguration(mHal3Device); 1315 if (res != OK) { 1316 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", 1317 stream->getId(), strerror(-res), res); 1318 return NULL; 1319 } 1320 } 1321 1322 newRequest->mOutputStreams.push(stream); 1323 } 1324 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); 1325 1326 return newRequest; 1327} 1328 1329status_t Camera3Device::configureStreamsLocked() { 1330 ATRACE_CALL(); 1331 status_t res; 1332 1333 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) { 1334 CLOGE("Not idle"); 1335 return INVALID_OPERATION; 1336 } 1337 1338 if (!mNeedConfig) { 1339 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); 1340 return OK; 1341 } 1342 1343 // Start configuring the streams 1344 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId); 1345 1346 camera3_stream_configuration config; 1347 1348 config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); 1349 1350 Vector<camera3_stream_t*> streams; 1351 streams.setCapacity(config.num_streams); 1352 1353 if (mInputStream != NULL) { 1354 camera3_stream_t *inputStream; 1355 inputStream = mInputStream->startConfiguration(); 1356 if (inputStream == NULL) { 1357 SET_ERR_L("Can't start input stream configuration"); 1358 return INVALID_OPERATION; 1359 } 1360 streams.add(inputStream); 1361 } 1362 1363 for (size_t i = 0; i < mOutputStreams.size(); i++) { 1364 1365 // Don't configure bidi streams twice, nor add them twice to the list 1366 if (mOutputStreams[i].get() == 1367 static_cast<Camera3StreamInterface*>(mInputStream.get())) { 1368 1369 config.num_streams--; 1370 continue; 1371 } 1372 1373 camera3_stream_t *outputStream; 1374 outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); 1375 if (outputStream == NULL) { 1376 SET_ERR_L("Can't start output stream configuration"); 1377 return INVALID_OPERATION; 1378 } 1379 streams.add(outputStream); 1380 } 1381 1382 config.streams = streams.editArray(); 1383 1384 // Do the HAL configuration; will potentially touch stream 1385 // max_buffers, usage, priv fields. 1386 ATRACE_BEGIN("camera3->configure_streams"); 1387 res = mHal3Device->ops->configure_streams(mHal3Device, &config); 1388 ATRACE_END(); 1389 1390 if (res != OK) { 1391 SET_ERR_L("Unable to configure streams with HAL: %s (%d)", 1392 strerror(-res), res); 1393 return res; 1394 } 1395 1396 // Finish all stream configuration immediately. 1397 // TODO: Try to relax this later back to lazy completion, which should be 1398 // faster 1399 1400 if (mInputStream != NULL && mInputStream->isConfiguring()) { 1401 res = mInputStream->finishConfiguration(mHal3Device); 1402 if (res != OK) { 1403 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)", 1404 mInputStream->getId(), strerror(-res), res); 1405 return res; 1406 } 1407 } 1408 1409 for (size_t i = 0; i < mOutputStreams.size(); i++) { 1410 sp<Camera3OutputStreamInterface> outputStream = 1411 mOutputStreams.editValueAt(i); 1412 if (outputStream->isConfiguring()) { 1413 res = outputStream->finishConfiguration(mHal3Device); 1414 if (res != OK) { 1415 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", 1416 outputStream->getId(), strerror(-res), res); 1417 return res; 1418 } 1419 } 1420 } 1421 1422 // Request thread needs to know to avoid using repeat-last-settings protocol 1423 // across configure_streams() calls 1424 mRequestThread->configurationComplete(); 1425 1426 // Update device state 1427 1428 mNeedConfig = false; 1429 1430 if (config.num_streams > 0) { 1431 mStatus = STATUS_CONFIGURED; 1432 } else { 1433 mStatus = STATUS_UNCONFIGURED; 1434 } 1435 1436 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId); 1437 1438 return OK; 1439} 1440 1441void Camera3Device::setErrorState(const char *fmt, ...) { 1442 Mutex::Autolock l(mLock); 1443 va_list args; 1444 va_start(args, fmt); 1445 1446 setErrorStateLockedV(fmt, args); 1447 1448 va_end(args); 1449} 1450 1451void Camera3Device::setErrorStateV(const char *fmt, va_list args) { 1452 Mutex::Autolock l(mLock); 1453 setErrorStateLockedV(fmt, args); 1454} 1455 1456void Camera3Device::setErrorStateLocked(const char *fmt, ...) { 1457 va_list args; 1458 va_start(args, fmt); 1459 1460 setErrorStateLockedV(fmt, args); 1461 1462 va_end(args); 1463} 1464 1465void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { 1466 // Print out all error messages to log 1467 String8 errorCause = String8::formatV(fmt, args); 1468 ALOGE("Camera %d: %s", mId, errorCause.string()); 1469 1470 // But only do error state transition steps for the first error 1471 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; 1472 1473 // Save stack trace. View by dumping it later. 1474 CameraTraces::saveTrace(); 1475 // TODO: consider adding errorCause and client pid/procname 1476 1477 mErrorCause = errorCause; 1478 1479 mRequestThread->setPaused(true); 1480 mStatus = STATUS_ERROR; 1481} 1482 1483/** 1484 * In-flight request management 1485 */ 1486 1487status_t Camera3Device::registerInFlight(int32_t frameNumber, 1488 int32_t requestId, int32_t numBuffers) { 1489 ATRACE_CALL(); 1490 Mutex::Autolock l(mInFlightLock); 1491 1492 ssize_t res; 1493 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers)); 1494 if (res < 0) return res; 1495 1496 return OK; 1497} 1498 1499/** 1500 * QUIRK(partial results) 1501 * Check if all 3A fields are ready, and send off a partial 3A-only result 1502 * to the output frame queue 1503 */ 1504bool Camera3Device::processPartial3AQuirk( 1505 int32_t frameNumber, int32_t requestId, 1506 const CameraMetadata& partial) { 1507 1508 // Check if all 3A states are present 1509 // The full list of fields is 1510 // android.control.afMode 1511 // android.control.awbMode 1512 // android.control.aeState 1513 // android.control.awbState 1514 // android.control.afState 1515 // android.control.afTriggerID 1516 // android.control.aePrecaptureID 1517 // TODO: Add android.control.aeMode 1518 1519 bool gotAllStates = true; 1520 1521 uint8_t afMode; 1522 uint8_t awbMode; 1523 uint8_t aeState; 1524 uint8_t afState; 1525 uint8_t awbState; 1526 int32_t afTriggerId; 1527 int32_t aeTriggerId; 1528 1529 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE, 1530 &afMode, frameNumber); 1531 1532 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE, 1533 &awbMode, frameNumber); 1534 1535 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE, 1536 &aeState, frameNumber); 1537 1538 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE, 1539 &afState, frameNumber); 1540 1541 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE, 1542 &awbState, frameNumber); 1543 1544 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID, 1545 &afTriggerId, frameNumber); 1546 1547 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID, 1548 &aeTriggerId, frameNumber); 1549 1550 if (!gotAllStates) return false; 1551 1552 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, " 1553 "AF state %d, AE state %d, AWB state %d, " 1554 "AF trigger %d, AE precapture trigger %d", 1555 __FUNCTION__, mId, frameNumber, requestId, 1556 afMode, awbMode, 1557 afState, aeState, awbState, 1558 afTriggerId, aeTriggerId); 1559 1560 // Got all states, so construct a minimal result to send 1561 // In addition to the above fields, this means adding in 1562 // android.request.frameCount 1563 // android.request.requestId 1564 // android.quirks.partialResult 1565 1566 const size_t kMinimal3AResultEntries = 10; 1567 1568 Mutex::Autolock l(mOutputLock); 1569 1570 CameraMetadata& min3AResult = 1571 *mResultQueue.insert( 1572 mResultQueue.end(), 1573 CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0)); 1574 1575 if (!insert3AResult(min3AResult, ANDROID_REQUEST_FRAME_COUNT, 1576 &frameNumber, frameNumber)) { 1577 return false; 1578 } 1579 1580 if (!insert3AResult(min3AResult, ANDROID_REQUEST_ID, 1581 &requestId, frameNumber)) { 1582 return false; 1583 } 1584 1585 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL; 1586 if (!insert3AResult(min3AResult, ANDROID_QUIRKS_PARTIAL_RESULT, 1587 &partialResult, frameNumber)) { 1588 return false; 1589 } 1590 1591 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_MODE, 1592 &afMode, frameNumber)) { 1593 return false; 1594 } 1595 1596 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_MODE, 1597 &awbMode, frameNumber)) { 1598 return false; 1599 } 1600 1601 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_STATE, 1602 &aeState, frameNumber)) { 1603 return false; 1604 } 1605 1606 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_STATE, 1607 &afState, frameNumber)) { 1608 return false; 1609 } 1610 1611 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_STATE, 1612 &awbState, frameNumber)) { 1613 return false; 1614 } 1615 1616 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_TRIGGER_ID, 1617 &afTriggerId, frameNumber)) { 1618 return false; 1619 } 1620 1621 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_PRECAPTURE_ID, 1622 &aeTriggerId, frameNumber)) { 1623 return false; 1624 } 1625 1626 mResultSignal.signal(); 1627 1628 return true; 1629} 1630 1631template<typename T> 1632bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag, 1633 T* value, int32_t frameNumber) { 1634 (void) frameNumber; 1635 1636 camera_metadata_ro_entry_t entry; 1637 1638 entry = result.find(tag); 1639 if (entry.count == 0) { 1640 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__, 1641 mId, frameNumber, get_camera_metadata_tag_name(tag)); 1642 return false; 1643 } 1644 1645 if (sizeof(T) == sizeof(uint8_t)) { 1646 *value = entry.data.u8[0]; 1647 } else if (sizeof(T) == sizeof(int32_t)) { 1648 *value = entry.data.i32[0]; 1649 } else { 1650 ALOGE("%s: Unexpected type", __FUNCTION__); 1651 return false; 1652 } 1653 return true; 1654} 1655 1656template<typename T> 1657bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag, 1658 const T* value, int32_t frameNumber) { 1659 if (result.update(tag, value, 1) != NO_ERROR) { 1660 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end()); 1661 SET_ERR("Frame %d: Failed to set %s in partial metadata", 1662 frameNumber, get_camera_metadata_tag_name(tag)); 1663 return false; 1664 } 1665 return true; 1666} 1667 1668/** 1669 * Camera HAL device callback methods 1670 */ 1671 1672void Camera3Device::processCaptureResult(const camera3_capture_result *result) { 1673 ATRACE_CALL(); 1674 1675 status_t res; 1676 1677 uint32_t frameNumber = result->frame_number; 1678 if (result->result == NULL && result->num_output_buffers == 0) { 1679 SET_ERR("No result data provided by HAL for frame %d", 1680 frameNumber); 1681 return; 1682 } 1683 bool partialResultQuirk = false; 1684 CameraMetadata collectedQuirkResult; 1685 1686 // Get capture timestamp from list of in-flight requests, where it was added 1687 // by the shutter notification for this frame. Then update the in-flight 1688 // status and remove the in-flight entry if all result data has been 1689 // received. 1690 nsecs_t timestamp = 0; 1691 { 1692 Mutex::Autolock l(mInFlightLock); 1693 ssize_t idx = mInFlightMap.indexOfKey(frameNumber); 1694 if (idx == NAME_NOT_FOUND) { 1695 SET_ERR("Unknown frame number for capture result: %d", 1696 frameNumber); 1697 return; 1698 } 1699 InFlightRequest &request = mInFlightMap.editValueAt(idx); 1700 1701 // Check if this result carries only partial metadata 1702 if (mUsePartialResultQuirk && result->result != NULL) { 1703 camera_metadata_ro_entry_t partialResultEntry; 1704 res = find_camera_metadata_ro_entry(result->result, 1705 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry); 1706 if (res != NAME_NOT_FOUND && 1707 partialResultEntry.count > 0 && 1708 partialResultEntry.data.u8[0] == 1709 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) { 1710 // A partial result. Flag this as such, and collect this 1711 // set of metadata into the in-flight entry. 1712 partialResultQuirk = true; 1713 request.partialResultQuirk.collectedResult.append( 1714 result->result); 1715 request.partialResultQuirk.collectedResult.erase( 1716 ANDROID_QUIRKS_PARTIAL_RESULT); 1717 // Fire off a 3A-only result if possible 1718 if (!request.partialResultQuirk.haveSent3A) { 1719 request.partialResultQuirk.haveSent3A = 1720 processPartial3AQuirk(frameNumber, 1721 request.requestId, 1722 request.partialResultQuirk.collectedResult); 1723 } 1724 } 1725 } 1726 1727 timestamp = request.captureTimestamp; 1728 /** 1729 * One of the following must happen before it's legal to call process_capture_result, 1730 * unless partial metadata is being provided: 1731 * - CAMERA3_MSG_SHUTTER (expected during normal operation) 1732 * - CAMERA3_MSG_ERROR (expected during flush) 1733 */ 1734 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) { 1735 SET_ERR("Called before shutter notify for frame %d", 1736 frameNumber); 1737 return; 1738 } 1739 1740 // Did we get the (final) result metadata for this capture? 1741 if (result->result != NULL && !partialResultQuirk) { 1742 if (request.haveResultMetadata) { 1743 SET_ERR("Called multiple times with metadata for frame %d", 1744 frameNumber); 1745 return; 1746 } 1747 if (mUsePartialResultQuirk && 1748 !request.partialResultQuirk.collectedResult.isEmpty()) { 1749 collectedQuirkResult.acquire( 1750 request.partialResultQuirk.collectedResult); 1751 } 1752 request.haveResultMetadata = true; 1753 } 1754 1755 request.numBuffersLeft -= result->num_output_buffers; 1756 1757 if (request.numBuffersLeft < 0) { 1758 SET_ERR("Too many buffers returned for frame %d", 1759 frameNumber); 1760 return; 1761 } 1762 1763 // Check if everything has arrived for this result (buffers and metadata), remove it from 1764 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush). 1765 if ((request.requestStatus != OK) || 1766 (request.haveResultMetadata && request.numBuffersLeft == 0)) { 1767 ATRACE_ASYNC_END("frame capture", frameNumber); 1768 mInFlightMap.removeItemsAt(idx, 1); 1769 } 1770 1771 // Sanity check - if we have too many in-flight frames, something has 1772 // likely gone wrong 1773 if (mInFlightMap.size() > kInFlightWarnLimit) { 1774 CLOGE("In-flight list too large: %zu", mInFlightMap.size()); 1775 } 1776 1777 } 1778 1779 // Process the result metadata, if provided 1780 bool gotResult = false; 1781 if (result->result != NULL && !partialResultQuirk) { 1782 Mutex::Autolock l(mOutputLock); 1783 1784 gotResult = true; 1785 1786 if (frameNumber != mNextResultFrameNumber) { 1787 SET_ERR("Out-of-order capture result metadata submitted! " 1788 "(got frame number %d, expecting %d)", 1789 frameNumber, mNextResultFrameNumber); 1790 return; 1791 } 1792 mNextResultFrameNumber++; 1793 1794 CameraMetadata captureResult; 1795 captureResult = result->result; 1796 1797 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT, 1798 (int32_t*)&frameNumber, 1) != OK) { 1799 SET_ERR("Failed to set frame# in metadata (%d)", 1800 frameNumber); 1801 gotResult = false; 1802 } else { 1803 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)", 1804 __FUNCTION__, mId, frameNumber); 1805 } 1806 1807 // Append any previous partials to form a complete result 1808 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) { 1809 captureResult.append(collectedQuirkResult); 1810 } 1811 1812 captureResult.sort(); 1813 1814 // Check that there's a timestamp in the result metadata 1815 1816 camera_metadata_entry entry = 1817 captureResult.find(ANDROID_SENSOR_TIMESTAMP); 1818 if (entry.count == 0) { 1819 SET_ERR("No timestamp provided by HAL for frame %d!", 1820 frameNumber); 1821 gotResult = false; 1822 } else if (timestamp != entry.data.i64[0]) { 1823 SET_ERR("Timestamp mismatch between shutter notify and result" 1824 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)", 1825 frameNumber, timestamp, entry.data.i64[0]); 1826 gotResult = false; 1827 } 1828 1829 if (gotResult) { 1830 // Valid result, insert into queue 1831 CameraMetadata& queuedResult = 1832 *mResultQueue.insert(mResultQueue.end(), CameraMetadata()); 1833 queuedResult.swap(captureResult); 1834 } 1835 } // scope for mOutputLock 1836 1837 // Return completed buffers to their streams with the timestamp 1838 1839 for (size_t i = 0; i < result->num_output_buffers; i++) { 1840 Camera3Stream *stream = 1841 Camera3Stream::cast(result->output_buffers[i].stream); 1842 res = stream->returnBuffer(result->output_buffers[i], timestamp); 1843 // Note: stream may be deallocated at this point, if this buffer was the 1844 // last reference to it. 1845 if (res != OK) { 1846 ALOGE("Can't return buffer %zu for frame %d to its stream: " 1847 " %s (%d)", i, frameNumber, strerror(-res), res); 1848 } 1849 } 1850 1851 // Finally, signal any waiters for new frames 1852 1853 if (gotResult) { 1854 mResultSignal.signal(); 1855 } 1856 1857} 1858 1859 1860 1861void Camera3Device::notify(const camera3_notify_msg *msg) { 1862 ATRACE_CALL(); 1863 NotificationListener *listener; 1864 { 1865 Mutex::Autolock l(mOutputLock); 1866 listener = mListener; 1867 } 1868 1869 if (msg == NULL) { 1870 SET_ERR("HAL sent NULL notify message!"); 1871 return; 1872 } 1873 1874 switch (msg->type) { 1875 case CAMERA3_MSG_ERROR: { 1876 int streamId = 0; 1877 if (msg->message.error.error_stream != NULL) { 1878 Camera3Stream *stream = 1879 Camera3Stream::cast( 1880 msg->message.error.error_stream); 1881 streamId = stream->getId(); 1882 } 1883 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d", 1884 mId, __FUNCTION__, msg->message.error.frame_number, 1885 streamId, msg->message.error.error_code); 1886 1887 // Set request error status for the request in the in-flight tracking 1888 { 1889 Mutex::Autolock l(mInFlightLock); 1890 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number); 1891 if (idx >= 0) { 1892 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code; 1893 } 1894 } 1895 1896 if (listener != NULL) { 1897 listener->notifyError(msg->message.error.error_code, 1898 msg->message.error.frame_number, streamId); 1899 } 1900 break; 1901 } 1902 case CAMERA3_MSG_SHUTTER: { 1903 ssize_t idx; 1904 uint32_t frameNumber = msg->message.shutter.frame_number; 1905 nsecs_t timestamp = msg->message.shutter.timestamp; 1906 // Verify ordering of shutter notifications 1907 { 1908 Mutex::Autolock l(mOutputLock); 1909 if (frameNumber != mNextShutterFrameNumber) { 1910 SET_ERR("Shutter notification out-of-order. Expected " 1911 "notification for frame %d, got frame %d", 1912 mNextShutterFrameNumber, frameNumber); 1913 break; 1914 } 1915 mNextShutterFrameNumber++; 1916 } 1917 1918 int32_t requestId = -1; 1919 1920 // Set timestamp for the request in the in-flight tracking 1921 // and get the request ID to send upstream 1922 { 1923 Mutex::Autolock l(mInFlightLock); 1924 idx = mInFlightMap.indexOfKey(frameNumber); 1925 if (idx >= 0) { 1926 InFlightRequest &r = mInFlightMap.editValueAt(idx); 1927 r.captureTimestamp = timestamp; 1928 requestId = r.requestId; 1929 } 1930 } 1931 if (idx < 0) { 1932 SET_ERR("Shutter notification for non-existent frame number %d", 1933 frameNumber); 1934 break; 1935 } 1936 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64, 1937 mId, __FUNCTION__, frameNumber, requestId, timestamp); 1938 // Call listener, if any 1939 if (listener != NULL) { 1940 listener->notifyShutter(requestId, timestamp); 1941 } 1942 break; 1943 } 1944 default: 1945 SET_ERR("Unknown notify message from HAL: %d", 1946 msg->type); 1947 } 1948} 1949 1950CameraMetadata Camera3Device::getLatestRequestLocked() { 1951 ALOGV("%s", __FUNCTION__); 1952 1953 CameraMetadata retVal; 1954 1955 if (mRequestThread != NULL) { 1956 retVal = mRequestThread->getLatestRequest(); 1957 } 1958 1959 return retVal; 1960} 1961 1962/** 1963 * RequestThread inner class methods 1964 */ 1965 1966Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, 1967 sp<StatusTracker> statusTracker, 1968 camera3_device_t *hal3Device) : 1969 Thread(false), 1970 mParent(parent), 1971 mStatusTracker(statusTracker), 1972 mHal3Device(hal3Device), 1973 mId(getId(parent)), 1974 mReconfigured(false), 1975 mDoPause(false), 1976 mPaused(true), 1977 mFrameNumber(0), 1978 mLatestRequestId(NAME_NOT_FOUND) { 1979 mStatusId = statusTracker->addComponent(); 1980} 1981 1982void Camera3Device::RequestThread::configurationComplete() { 1983 Mutex::Autolock l(mRequestLock); 1984 mReconfigured = true; 1985} 1986 1987status_t Camera3Device::RequestThread::queueRequest( 1988 sp<CaptureRequest> request) { 1989 Mutex::Autolock l(mRequestLock); 1990 mRequestQueue.push_back(request); 1991 1992 unpauseForNewRequests(); 1993 1994 return OK; 1995} 1996 1997status_t Camera3Device::RequestThread::queueRequestList( 1998 List<sp<CaptureRequest> > &requests) { 1999 Mutex::Autolock l(mRequestLock); 2000 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); 2001 ++it) { 2002 mRequestQueue.push_back(*it); 2003 } 2004 2005 unpauseForNewRequests(); 2006 2007 return OK; 2008} 2009 2010 2011status_t Camera3Device::RequestThread::queueTrigger( 2012 RequestTrigger trigger[], 2013 size_t count) { 2014 2015 Mutex::Autolock l(mTriggerMutex); 2016 status_t ret; 2017 2018 for (size_t i = 0; i < count; ++i) { 2019 ret = queueTriggerLocked(trigger[i]); 2020 2021 if (ret != OK) { 2022 return ret; 2023 } 2024 } 2025 2026 return OK; 2027} 2028 2029int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { 2030 sp<Camera3Device> d = device.promote(); 2031 if (d != NULL) return d->mId; 2032 return 0; 2033} 2034 2035status_t Camera3Device::RequestThread::queueTriggerLocked( 2036 RequestTrigger trigger) { 2037 2038 uint32_t tag = trigger.metadataTag; 2039 ssize_t index = mTriggerMap.indexOfKey(tag); 2040 2041 switch (trigger.getTagType()) { 2042 case TYPE_BYTE: 2043 // fall-through 2044 case TYPE_INT32: 2045 break; 2046 default: 2047 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, 2048 trigger.getTagType()); 2049 return INVALID_OPERATION; 2050 } 2051 2052 /** 2053 * Collect only the latest trigger, since we only have 1 field 2054 * in the request settings per trigger tag, and can't send more than 1 2055 * trigger per request. 2056 */ 2057 if (index != NAME_NOT_FOUND) { 2058 mTriggerMap.editValueAt(index) = trigger; 2059 } else { 2060 mTriggerMap.add(tag, trigger); 2061 } 2062 2063 return OK; 2064} 2065 2066status_t Camera3Device::RequestThread::setRepeatingRequests( 2067 const RequestList &requests) { 2068 Mutex::Autolock l(mRequestLock); 2069 mRepeatingRequests.clear(); 2070 mRepeatingRequests.insert(mRepeatingRequests.begin(), 2071 requests.begin(), requests.end()); 2072 2073 unpauseForNewRequests(); 2074 2075 return OK; 2076} 2077 2078status_t Camera3Device::RequestThread::clearRepeatingRequests() { 2079 Mutex::Autolock l(mRequestLock); 2080 mRepeatingRequests.clear(); 2081 return OK; 2082} 2083 2084status_t Camera3Device::RequestThread::clear() { 2085 Mutex::Autolock l(mRequestLock); 2086 mRepeatingRequests.clear(); 2087 mRequestQueue.clear(); 2088 mTriggerMap.clear(); 2089 return OK; 2090} 2091 2092void Camera3Device::RequestThread::setPaused(bool paused) { 2093 Mutex::Autolock l(mPauseLock); 2094 mDoPause = paused; 2095 mDoPauseSignal.signal(); 2096} 2097 2098status_t Camera3Device::RequestThread::waitUntilRequestProcessed( 2099 int32_t requestId, nsecs_t timeout) { 2100 Mutex::Autolock l(mLatestRequestMutex); 2101 status_t res; 2102 while (mLatestRequestId != requestId) { 2103 nsecs_t startTime = systemTime(); 2104 2105 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); 2106 if (res != OK) return res; 2107 2108 timeout -= (systemTime() - startTime); 2109 } 2110 2111 return OK; 2112} 2113 2114void Camera3Device::RequestThread::requestExit() { 2115 // Call parent to set up shutdown 2116 Thread::requestExit(); 2117 // The exit from any possible waits 2118 mDoPauseSignal.signal(); 2119 mRequestSignal.signal(); 2120} 2121 2122bool Camera3Device::RequestThread::threadLoop() { 2123 2124 status_t res; 2125 2126 // Handle paused state. 2127 if (waitIfPaused()) { 2128 return true; 2129 } 2130 2131 // Get work to do 2132 2133 sp<CaptureRequest> nextRequest = waitForNextRequest(); 2134 if (nextRequest == NULL) { 2135 return true; 2136 } 2137 2138 // Create request to HAL 2139 camera3_capture_request_t request = camera3_capture_request_t(); 2140 Vector<camera3_stream_buffer_t> outputBuffers; 2141 2142 // Get the request ID, if any 2143 int requestId; 2144 camera_metadata_entry_t requestIdEntry = 2145 nextRequest->mSettings.find(ANDROID_REQUEST_ID); 2146 if (requestIdEntry.count > 0) { 2147 requestId = requestIdEntry.data.i32[0]; 2148 } else { 2149 ALOGW("%s: Did not have android.request.id set in the request", 2150 __FUNCTION__); 2151 requestId = NAME_NOT_FOUND; 2152 } 2153 2154 // Insert any queued triggers (before metadata is locked) 2155 int32_t triggerCount; 2156 res = insertTriggers(nextRequest); 2157 if (res < 0) { 2158 SET_ERR("RequestThread: Unable to insert triggers " 2159 "(capture request %d, HAL device: %s (%d)", 2160 (mFrameNumber+1), strerror(-res), res); 2161 cleanUpFailedRequest(request, nextRequest, outputBuffers); 2162 return false; 2163 } 2164 triggerCount = res; 2165 2166 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); 2167 2168 // If the request is the same as last, or we had triggers last time 2169 if (mPrevRequest != nextRequest || triggersMixedIn) { 2170 /** 2171 * HAL workaround: 2172 * Insert a dummy trigger ID if a trigger is set but no trigger ID is 2173 */ 2174 res = addDummyTriggerIds(nextRequest); 2175 if (res != OK) { 2176 SET_ERR("RequestThread: Unable to insert dummy trigger IDs " 2177 "(capture request %d, HAL device: %s (%d)", 2178 (mFrameNumber+1), strerror(-res), res); 2179 cleanUpFailedRequest(request, nextRequest, outputBuffers); 2180 return false; 2181 } 2182 2183 /** 2184 * The request should be presorted so accesses in HAL 2185 * are O(logn). Sidenote, sorting a sorted metadata is nop. 2186 */ 2187 nextRequest->mSettings.sort(); 2188 request.settings = nextRequest->mSettings.getAndLock(); 2189 mPrevRequest = nextRequest; 2190 ALOGVV("%s: Request settings are NEW", __FUNCTION__); 2191 2192 IF_ALOGV() { 2193 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); 2194 find_camera_metadata_ro_entry( 2195 request.settings, 2196 ANDROID_CONTROL_AF_TRIGGER, 2197 &e 2198 ); 2199 if (e.count > 0) { 2200 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", 2201 __FUNCTION__, 2202 mFrameNumber+1, 2203 e.data.u8[0]); 2204 } 2205 } 2206 } else { 2207 // leave request.settings NULL to indicate 'reuse latest given' 2208 ALOGVV("%s: Request settings are REUSED", 2209 __FUNCTION__); 2210 } 2211 2212 camera3_stream_buffer_t inputBuffer; 2213 2214 // Fill in buffers 2215 2216 if (nextRequest->mInputStream != NULL) { 2217 request.input_buffer = &inputBuffer; 2218 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer); 2219 if (res != OK) { 2220 ALOGE("RequestThread: Can't get input buffer, skipping request:" 2221 " %s (%d)", strerror(-res), res); 2222 cleanUpFailedRequest(request, nextRequest, outputBuffers); 2223 return true; 2224 } 2225 } else { 2226 request.input_buffer = NULL; 2227 } 2228 2229 outputBuffers.insertAt(camera3_stream_buffer_t(), 0, 2230 nextRequest->mOutputStreams.size()); 2231 request.output_buffers = outputBuffers.array(); 2232 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) { 2233 res = nextRequest->mOutputStreams.editItemAt(i)-> 2234 getBuffer(&outputBuffers.editItemAt(i)); 2235 if (res != OK) { 2236 ALOGE("RequestThread: Can't get output buffer, skipping request:" 2237 " %s (%d)", strerror(-res), res); 2238 cleanUpFailedRequest(request, nextRequest, outputBuffers); 2239 return true; 2240 } 2241 request.num_output_buffers++; 2242 } 2243 2244 request.frame_number = mFrameNumber++; 2245 2246 // Log request in the in-flight queue 2247 sp<Camera3Device> parent = mParent.promote(); 2248 if (parent == NULL) { 2249 CLOGE("RequestThread: Parent is gone"); 2250 cleanUpFailedRequest(request, nextRequest, outputBuffers); 2251 return false; 2252 } 2253 2254 res = parent->registerInFlight(request.frame_number, requestId, 2255 request.num_output_buffers); 2256 if (res != OK) { 2257 SET_ERR("RequestThread: Unable to register new in-flight request:" 2258 " %s (%d)", strerror(-res), res); 2259 cleanUpFailedRequest(request, nextRequest, outputBuffers); 2260 return false; 2261 } 2262 2263 // Inform waitUntilRequestProcessed thread of a new request ID 2264 { 2265 Mutex::Autolock al(mLatestRequestMutex); 2266 2267 mLatestRequestId = requestId; 2268 mLatestRequestSignal.signal(); 2269 } 2270 2271 // Submit request and block until ready for next one 2272 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number); 2273 ATRACE_BEGIN("camera3->process_capture_request"); 2274 res = mHal3Device->ops->process_capture_request(mHal3Device, &request); 2275 ATRACE_END(); 2276 2277 if (res != OK) { 2278 SET_ERR("RequestThread: Unable to submit capture request %d to HAL" 2279 " device: %s (%d)", request.frame_number, strerror(-res), res); 2280 cleanUpFailedRequest(request, nextRequest, outputBuffers); 2281 return false; 2282 } 2283 2284 // Update the latest request sent to HAL 2285 if (request.settings != NULL) { // Don't update them if they were unchanged 2286 Mutex::Autolock al(mLatestRequestMutex); 2287 2288 camera_metadata_t* cloned = clone_camera_metadata(request.settings); 2289 mLatestRequest.acquire(cloned); 2290 } 2291 2292 if (request.settings != NULL) { 2293 nextRequest->mSettings.unlock(request.settings); 2294 } 2295 2296 // Remove any previously queued triggers (after unlock) 2297 res = removeTriggers(mPrevRequest); 2298 if (res != OK) { 2299 SET_ERR("RequestThread: Unable to remove triggers " 2300 "(capture request %d, HAL device: %s (%d)", 2301 request.frame_number, strerror(-res), res); 2302 return false; 2303 } 2304 mPrevTriggers = triggerCount; 2305 2306 // Return input buffer back to framework 2307 if (request.input_buffer != NULL) { 2308 Camera3Stream *stream = 2309 Camera3Stream::cast(request.input_buffer->stream); 2310 res = stream->returnInputBuffer(*(request.input_buffer)); 2311 // Note: stream may be deallocated at this point, if this buffer was the 2312 // last reference to it. 2313 if (res != OK) { 2314 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" 2315 " its stream:%s (%d)", __FUNCTION__, 2316 request.frame_number, strerror(-res), res); 2317 // TODO: Report error upstream 2318 } 2319 } 2320 2321 return true; 2322} 2323 2324CameraMetadata Camera3Device::RequestThread::getLatestRequest() const { 2325 Mutex::Autolock al(mLatestRequestMutex); 2326 2327 ALOGV("RequestThread::%s", __FUNCTION__); 2328 2329 return mLatestRequest; 2330} 2331 2332void Camera3Device::RequestThread::cleanUpFailedRequest( 2333 camera3_capture_request_t &request, 2334 sp<CaptureRequest> &nextRequest, 2335 Vector<camera3_stream_buffer_t> &outputBuffers) { 2336 2337 if (request.settings != NULL) { 2338 nextRequest->mSettings.unlock(request.settings); 2339 } 2340 if (request.input_buffer != NULL) { 2341 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR; 2342 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer)); 2343 } 2344 for (size_t i = 0; i < request.num_output_buffers; i++) { 2345 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; 2346 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer( 2347 outputBuffers[i], 0); 2348 } 2349} 2350 2351sp<Camera3Device::CaptureRequest> 2352 Camera3Device::RequestThread::waitForNextRequest() { 2353 status_t res; 2354 sp<CaptureRequest> nextRequest; 2355 2356 // Optimized a bit for the simple steady-state case (single repeating 2357 // request), to avoid putting that request in the queue temporarily. 2358 Mutex::Autolock l(mRequestLock); 2359 2360 while (mRequestQueue.empty()) { 2361 if (!mRepeatingRequests.empty()) { 2362 // Always atomically enqueue all requests in a repeating request 2363 // list. Guarantees a complete in-sequence set of captures to 2364 // application. 2365 const RequestList &requests = mRepeatingRequests; 2366 RequestList::const_iterator firstRequest = 2367 requests.begin(); 2368 nextRequest = *firstRequest; 2369 mRequestQueue.insert(mRequestQueue.end(), 2370 ++firstRequest, 2371 requests.end()); 2372 // No need to wait any longer 2373 break; 2374 } 2375 2376 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); 2377 2378 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) || 2379 exitPending()) { 2380 Mutex::Autolock pl(mPauseLock); 2381 if (mPaused == false) { 2382 ALOGV("%s: RequestThread: Going idle", __FUNCTION__); 2383 mPaused = true; 2384 // Let the tracker know 2385 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 2386 if (statusTracker != 0) { 2387 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); 2388 } 2389 } 2390 // Stop waiting for now and let thread management happen 2391 return NULL; 2392 } 2393 } 2394 2395 if (nextRequest == NULL) { 2396 // Don't have a repeating request already in hand, so queue 2397 // must have an entry now. 2398 RequestList::iterator firstRequest = 2399 mRequestQueue.begin(); 2400 nextRequest = *firstRequest; 2401 mRequestQueue.erase(firstRequest); 2402 } 2403 2404 // In case we've been unpaused by setPaused clearing mDoPause, need to 2405 // update internal pause state (capture/setRepeatingRequest unpause 2406 // directly). 2407 Mutex::Autolock pl(mPauseLock); 2408 if (mPaused) { 2409 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__); 2410 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 2411 if (statusTracker != 0) { 2412 statusTracker->markComponentActive(mStatusId); 2413 } 2414 } 2415 mPaused = false; 2416 2417 // Check if we've reconfigured since last time, and reset the preview 2418 // request if so. Can't use 'NULL request == repeat' across configure calls. 2419 if (mReconfigured) { 2420 mPrevRequest.clear(); 2421 mReconfigured = false; 2422 } 2423 2424 return nextRequest; 2425} 2426 2427bool Camera3Device::RequestThread::waitIfPaused() { 2428 status_t res; 2429 Mutex::Autolock l(mPauseLock); 2430 while (mDoPause) { 2431 if (mPaused == false) { 2432 mPaused = true; 2433 ALOGV("%s: RequestThread: Paused", __FUNCTION__); 2434 // Let the tracker know 2435 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 2436 if (statusTracker != 0) { 2437 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); 2438 } 2439 } 2440 2441 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); 2442 if (res == TIMED_OUT || exitPending()) { 2443 return true; 2444 } 2445 } 2446 // We don't set mPaused to false here, because waitForNextRequest needs 2447 // to further manage the paused state in case of starvation. 2448 return false; 2449} 2450 2451void Camera3Device::RequestThread::unpauseForNewRequests() { 2452 // With work to do, mark thread as unpaused. 2453 // If paused by request (setPaused), don't resume, to avoid 2454 // extra signaling/waiting overhead to waitUntilPaused 2455 mRequestSignal.signal(); 2456 Mutex::Autolock p(mPauseLock); 2457 if (!mDoPause) { 2458 ALOGV("%s: RequestThread: Going active", __FUNCTION__); 2459 if (mPaused) { 2460 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 2461 if (statusTracker != 0) { 2462 statusTracker->markComponentActive(mStatusId); 2463 } 2464 } 2465 mPaused = false; 2466 } 2467} 2468 2469void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { 2470 sp<Camera3Device> parent = mParent.promote(); 2471 if (parent != NULL) { 2472 va_list args; 2473 va_start(args, fmt); 2474 2475 parent->setErrorStateV(fmt, args); 2476 2477 va_end(args); 2478 } 2479} 2480 2481status_t Camera3Device::RequestThread::insertTriggers( 2482 const sp<CaptureRequest> &request) { 2483 2484 Mutex::Autolock al(mTriggerMutex); 2485 2486 CameraMetadata &metadata = request->mSettings; 2487 size_t count = mTriggerMap.size(); 2488 2489 for (size_t i = 0; i < count; ++i) { 2490 RequestTrigger trigger = mTriggerMap.valueAt(i); 2491 2492 uint32_t tag = trigger.metadataTag; 2493 camera_metadata_entry entry = metadata.find(tag); 2494 2495 if (entry.count > 0) { 2496 /** 2497 * Already has an entry for this trigger in the request. 2498 * Rewrite it with our requested trigger value. 2499 */ 2500 RequestTrigger oldTrigger = trigger; 2501 2502 oldTrigger.entryValue = entry.data.u8[0]; 2503 2504 mTriggerReplacedMap.add(tag, oldTrigger); 2505 } else { 2506 /** 2507 * More typical, no trigger entry, so we just add it 2508 */ 2509 mTriggerRemovedMap.add(tag, trigger); 2510 } 2511 2512 status_t res; 2513 2514 switch (trigger.getTagType()) { 2515 case TYPE_BYTE: { 2516 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 2517 res = metadata.update(tag, 2518 &entryValue, 2519 /*count*/1); 2520 break; 2521 } 2522 case TYPE_INT32: 2523 res = metadata.update(tag, 2524 &trigger.entryValue, 2525 /*count*/1); 2526 break; 2527 default: 2528 ALOGE("%s: Type not supported: 0x%x", 2529 __FUNCTION__, 2530 trigger.getTagType()); 2531 return INVALID_OPERATION; 2532 } 2533 2534 if (res != OK) { 2535 ALOGE("%s: Failed to update request metadata with trigger tag %s" 2536 ", value %d", __FUNCTION__, trigger.getTagName(), 2537 trigger.entryValue); 2538 return res; 2539 } 2540 2541 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, 2542 trigger.getTagName(), 2543 trigger.entryValue); 2544 } 2545 2546 mTriggerMap.clear(); 2547 2548 return count; 2549} 2550 2551status_t Camera3Device::RequestThread::removeTriggers( 2552 const sp<CaptureRequest> &request) { 2553 Mutex::Autolock al(mTriggerMutex); 2554 2555 CameraMetadata &metadata = request->mSettings; 2556 2557 /** 2558 * Replace all old entries with their old values. 2559 */ 2560 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { 2561 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); 2562 2563 status_t res; 2564 2565 uint32_t tag = trigger.metadataTag; 2566 switch (trigger.getTagType()) { 2567 case TYPE_BYTE: { 2568 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 2569 res = metadata.update(tag, 2570 &entryValue, 2571 /*count*/1); 2572 break; 2573 } 2574 case TYPE_INT32: 2575 res = metadata.update(tag, 2576 &trigger.entryValue, 2577 /*count*/1); 2578 break; 2579 default: 2580 ALOGE("%s: Type not supported: 0x%x", 2581 __FUNCTION__, 2582 trigger.getTagType()); 2583 return INVALID_OPERATION; 2584 } 2585 2586 if (res != OK) { 2587 ALOGE("%s: Failed to restore request metadata with trigger tag %s" 2588 ", trigger value %d", __FUNCTION__, 2589 trigger.getTagName(), trigger.entryValue); 2590 return res; 2591 } 2592 } 2593 mTriggerReplacedMap.clear(); 2594 2595 /** 2596 * Remove all new entries. 2597 */ 2598 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { 2599 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); 2600 status_t res = metadata.erase(trigger.metadataTag); 2601 2602 if (res != OK) { 2603 ALOGE("%s: Failed to erase metadata with trigger tag %s" 2604 ", trigger value %d", __FUNCTION__, 2605 trigger.getTagName(), trigger.entryValue); 2606 return res; 2607 } 2608 } 2609 mTriggerRemovedMap.clear(); 2610 2611 return OK; 2612} 2613 2614status_t Camera3Device::RequestThread::addDummyTriggerIds( 2615 const sp<CaptureRequest> &request) { 2616 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here 2617 static const int32_t dummyTriggerId = 1; 2618 status_t res; 2619 2620 CameraMetadata &metadata = request->mSettings; 2621 2622 // If AF trigger is active, insert a dummy AF trigger ID if none already 2623 // exists 2624 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER); 2625 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID); 2626 if (afTrigger.count > 0 && 2627 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE && 2628 afId.count == 0) { 2629 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1); 2630 if (res != OK) return res; 2631 } 2632 2633 // If AE precapture trigger is active, insert a dummy precapture trigger ID 2634 // if none already exists 2635 camera_metadata_entry pcTrigger = 2636 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); 2637 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); 2638 if (pcTrigger.count > 0 && 2639 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE && 2640 pcId.count == 0) { 2641 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID, 2642 &dummyTriggerId, 1); 2643 if (res != OK) return res; 2644 } 2645 2646 return OK; 2647} 2648 2649 2650/** 2651 * Static callback forwarding methods from HAL to instance 2652 */ 2653 2654void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, 2655 const camera3_capture_result *result) { 2656 Camera3Device *d = 2657 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 2658 d->processCaptureResult(result); 2659} 2660 2661void Camera3Device::sNotify(const camera3_callback_ops *cb, 2662 const camera3_notify_msg *msg) { 2663 Camera3Device *d = 2664 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 2665 d->notify(msg); 2666} 2667 2668}; // namespace android 2669