Camera3Device.cpp revision 7b82efe7a376c882f8f938e1c41b8311a8cdda4a
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 <utils/Log.h> 41#include <utils/Trace.h> 42#include <utils/Timers.h> 43 44#include "device3/Camera3Device.h" 45#include "device3/Camera3OutputStream.h" 46#include "device3/Camera3InputStream.h" 47#include "device3/Camera3ZslStream.h" 48 49using namespace android::camera3; 50 51namespace android { 52 53Camera3Device::Camera3Device(int id): 54 mId(id), 55 mHal3Device(NULL), 56 mStatus(STATUS_UNINITIALIZED), 57 mNextResultFrameNumber(0), 58 mNextShutterFrameNumber(0), 59 mListener(NULL) 60{ 61 ATRACE_CALL(); 62 camera3_callback_ops::notify = &sNotify; 63 camera3_callback_ops::process_capture_result = &sProcessCaptureResult; 64 ALOGV("%s: Created device for camera %d", __FUNCTION__, id); 65} 66 67Camera3Device::~Camera3Device() 68{ 69 ATRACE_CALL(); 70 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId); 71 disconnect(); 72} 73 74int Camera3Device::getId() const { 75 return mId; 76} 77 78/** 79 * CameraDeviceBase interface 80 */ 81 82status_t Camera3Device::initialize(camera_module_t *module) 83{ 84 ATRACE_CALL(); 85 Mutex::Autolock l(mLock); 86 87 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId); 88 if (mStatus != STATUS_UNINITIALIZED) { 89 CLOGE("Already initialized!"); 90 return INVALID_OPERATION; 91 } 92 93 /** Open HAL device */ 94 95 status_t res; 96 String8 deviceName = String8::format("%d", mId); 97 98 camera3_device_t *device; 99 100 res = module->common.methods->open(&module->common, deviceName.string(), 101 reinterpret_cast<hw_device_t**>(&device)); 102 103 if (res != OK) { 104 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res); 105 return res; 106 } 107 108 /** Cross-check device version */ 109 110 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) { 111 SET_ERR_L("Could not open camera: " 112 "Camera device is not version %x, reports %x instead", 113 CAMERA_DEVICE_API_VERSION_3_0, 114 device->common.version); 115 device->common.close(&device->common); 116 return BAD_VALUE; 117 } 118 119 camera_info info; 120 res = module->get_camera_info(mId, &info); 121 if (res != OK) return res; 122 123 if (info.device_version != device->common.version) { 124 SET_ERR_L("HAL reporting mismatched camera_info version (%x)" 125 " and device version (%x).", 126 device->common.version, info.device_version); 127 device->common.close(&device->common); 128 return BAD_VALUE; 129 } 130 131 /** Initialize device with callback functions */ 132 133 ATRACE_BEGIN("camera3->initialize"); 134 res = device->ops->initialize(device, this); 135 ATRACE_END(); 136 137 if (res != OK) { 138 SET_ERR_L("Unable to initialize HAL device: %s (%d)", 139 strerror(-res), res); 140 device->common.close(&device->common); 141 return BAD_VALUE; 142 } 143 144 /** Get vendor metadata tags */ 145 146 mVendorTagOps.get_camera_vendor_section_name = NULL; 147 148 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops"); 149 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps); 150 ATRACE_END(); 151 152 if (mVendorTagOps.get_camera_vendor_section_name != NULL) { 153 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps); 154 if (res != OK) { 155 SET_ERR_L("Unable to set tag ops: %s (%d)", 156 strerror(-res), res); 157 device->common.close(&device->common); 158 return res; 159 } 160 } 161 162 /** Start up request queue thread */ 163 164 mRequestThread = new RequestThread(this, 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_IDLE; 179 mNextStreamId = 0; 180 mNeedConfig = true; 181 182 return OK; 183} 184 185status_t Camera3Device::disconnect() { 186 ATRACE_CALL(); 187 Mutex::Autolock l(mLock); 188 189 ALOGV("%s: E", __FUNCTION__); 190 191 status_t res = OK; 192 if (mStatus == STATUS_UNINITIALIZED) return res; 193 194 if (mStatus == STATUS_ACTIVE || 195 (mStatus == STATUS_ERROR && mRequestThread != NULL)) { 196 res = mRequestThread->clearRepeatingRequests(); 197 if (res != OK) { 198 SET_ERR_L("Can't stop streaming"); 199 // Continue to close device even in case of error 200 } else { 201 res = waitUntilDrainedLocked(); 202 if (res != OK) { 203 SET_ERR_L("Timeout waiting for HAL to drain"); 204 // Continue to close device even in case of error 205 } 206 } 207 } 208 assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR); 209 210 if (mStatus == STATUS_ERROR) { 211 CLOGE("Shutting down in an error state"); 212 } 213 214 if (mRequestThread != NULL) { 215 mRequestThread->requestExit(); 216 } 217 218 mOutputStreams.clear(); 219 mInputStream.clear(); 220 221 if (mRequestThread != NULL) { 222 if (mStatus != STATUS_ERROR) { 223 // HAL may be in a bad state, so waiting for request thread 224 // (which may be stuck in the HAL processCaptureRequest call) 225 // could be dangerous. 226 mRequestThread->join(); 227 } 228 mRequestThread.clear(); 229 } 230 231 if (mHal3Device != NULL) { 232 mHal3Device->common.close(&mHal3Device->common); 233 mHal3Device = NULL; 234 } 235 236 mStatus = STATUS_UNINITIALIZED; 237 238 ALOGV("%s: X", __FUNCTION__); 239 return res; 240} 241 242status_t Camera3Device::dump(int fd, const Vector<String16> &args) { 243 ATRACE_CALL(); 244 (void)args; 245 String8 lines; 246 247 const char *status = 248 mStatus == STATUS_ERROR ? "ERROR" : 249 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : 250 mStatus == STATUS_IDLE ? "IDLE" : 251 mStatus == STATUS_ACTIVE ? "ACTIVE" : 252 "Unknown"; 253 lines.appendFormat(" Device status: %s\n", status); 254 if (mStatus == STATUS_ERROR) { 255 lines.appendFormat(" Error cause: %s\n", mErrorCause.string()); 256 } 257 lines.appendFormat(" Stream configuration:\n"); 258 259 if (mInputStream != NULL) { 260 write(fd, lines.string(), lines.size()); 261 mInputStream->dump(fd, args); 262 } else { 263 lines.appendFormat(" No input stream.\n"); 264 write(fd, lines.string(), lines.size()); 265 } 266 for (size_t i = 0; i < mOutputStreams.size(); i++) { 267 mOutputStreams[i]->dump(fd,args); 268 } 269 270 lines = String8(" In-flight requests:\n"); 271 if (mInFlightMap.size() == 0) { 272 lines.append(" None\n"); 273 } else { 274 for (size_t i = 0; i < mInFlightMap.size(); i++) { 275 InFlightRequest r = mInFlightMap.valueAt(i); 276 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata" 277 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), 278 r.captureTimestamp, r.haveResultMetadata ? "true" : "false", 279 r.numBuffersLeft); 280 } 281 } 282 write(fd, lines.string(), lines.size()); 283 284 if (mHal3Device != NULL) { 285 lines = String8(" HAL device dump:\n"); 286 write(fd, lines.string(), lines.size()); 287 mHal3Device->ops->dump(mHal3Device, fd); 288 } 289 290 return OK; 291} 292 293const CameraMetadata& Camera3Device::info() const { 294 ALOGVV("%s: E", __FUNCTION__); 295 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || 296 mStatus == STATUS_ERROR)) { 297 ALOGW("%s: Access to static info %s!", __FUNCTION__, 298 mStatus == STATUS_ERROR ? 299 "when in error state" : "before init"); 300 } 301 return mDeviceInfo; 302} 303 304status_t Camera3Device::capture(CameraMetadata &request) { 305 ATRACE_CALL(); 306 Mutex::Autolock l(mLock); 307 308 // TODO: take ownership of the request 309 310 switch (mStatus) { 311 case STATUS_ERROR: 312 CLOGE("Device has encountered a serious error"); 313 return INVALID_OPERATION; 314 case STATUS_UNINITIALIZED: 315 CLOGE("Device not initialized"); 316 return INVALID_OPERATION; 317 case STATUS_IDLE: 318 case STATUS_ACTIVE: 319 // OK 320 break; 321 default: 322 SET_ERR_L("Unexpected status: %d", mStatus); 323 return INVALID_OPERATION; 324 } 325 326 sp<CaptureRequest> newRequest = setUpRequestLocked(request); 327 if (newRequest == NULL) { 328 CLOGE("Can't create capture request"); 329 return BAD_VALUE; 330 } 331 332 return mRequestThread->queueRequest(newRequest); 333} 334 335 336status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) { 337 ATRACE_CALL(); 338 Mutex::Autolock l(mLock); 339 340 switch (mStatus) { 341 case STATUS_ERROR: 342 CLOGE("Device has encountered a serious error"); 343 return INVALID_OPERATION; 344 case STATUS_UNINITIALIZED: 345 CLOGE("Device not initialized"); 346 return INVALID_OPERATION; 347 case STATUS_IDLE: 348 case STATUS_ACTIVE: 349 // OK 350 break; 351 default: 352 SET_ERR_L("Unexpected status: %d", mStatus); 353 return INVALID_OPERATION; 354 } 355 356 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request); 357 if (newRepeatingRequest == NULL) { 358 CLOGE("Can't create repeating request"); 359 return BAD_VALUE; 360 } 361 362 RequestList newRepeatingRequests; 363 newRepeatingRequests.push_back(newRepeatingRequest); 364 365 return mRequestThread->setRepeatingRequests(newRepeatingRequests); 366} 367 368 369sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( 370 const CameraMetadata &request) { 371 status_t res; 372 373 if (mStatus == STATUS_IDLE) { 374 res = configureStreamsLocked(); 375 if (res != OK) { 376 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res); 377 return NULL; 378 } 379 } 380 381 sp<CaptureRequest> newRequest = createCaptureRequest(request); 382 return newRequest; 383} 384 385status_t Camera3Device::clearStreamingRequest() { 386 ATRACE_CALL(); 387 Mutex::Autolock l(mLock); 388 389 switch (mStatus) { 390 case STATUS_ERROR: 391 CLOGE("Device has encountered a serious error"); 392 return INVALID_OPERATION; 393 case STATUS_UNINITIALIZED: 394 CLOGE("Device not initialized"); 395 return INVALID_OPERATION; 396 case STATUS_IDLE: 397 case STATUS_ACTIVE: 398 // OK 399 break; 400 default: 401 SET_ERR_L("Unexpected status: %d", mStatus); 402 return INVALID_OPERATION; 403 } 404 405 return mRequestThread->clearRepeatingRequests(); 406} 407 408status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { 409 ATRACE_CALL(); 410 411 return mRequestThread->waitUntilRequestProcessed(requestId, timeout); 412} 413 414status_t Camera3Device::createInputStream( 415 uint32_t width, uint32_t height, int format, int *id) { 416 ATRACE_CALL(); 417 Mutex::Autolock l(mLock); 418 419 status_t res; 420 bool wasActive = false; 421 422 switch (mStatus) { 423 case STATUS_ERROR: 424 ALOGE("%s: Device has encountered a serious error", __FUNCTION__); 425 return INVALID_OPERATION; 426 case STATUS_UNINITIALIZED: 427 ALOGE("%s: Device not initialized", __FUNCTION__); 428 return INVALID_OPERATION; 429 case STATUS_IDLE: 430 // OK 431 break; 432 case STATUS_ACTIVE: 433 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 434 mRequestThread->setPaused(true); 435 res = waitUntilDrainedLocked(); 436 if (res != OK) { 437 ALOGE("%s: Can't pause captures to reconfigure streams!", 438 __FUNCTION__); 439 mStatus = STATUS_ERROR; 440 return res; 441 } 442 wasActive = true; 443 break; 444 default: 445 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus); 446 return INVALID_OPERATION; 447 } 448 assert(mStatus == STATUS_IDLE); 449 450 if (mInputStream != 0) { 451 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); 452 return INVALID_OPERATION; 453 } 454 455 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, 456 width, height, format); 457 458 mInputStream = newStream; 459 460 *id = mNextStreamId++; 461 462 // Continue captures if active at start 463 if (wasActive) { 464 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 465 res = configureStreamsLocked(); 466 if (res != OK) { 467 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", 468 __FUNCTION__, mNextStreamId, strerror(-res), res); 469 return res; 470 } 471 mRequestThread->setPaused(false); 472 } 473 474 return OK; 475} 476 477 478status_t Camera3Device::createZslStream( 479 uint32_t width, uint32_t height, 480 int depth, 481 /*out*/ 482 int *id, 483 sp<Camera3ZslStream>* zslStream) { 484 ATRACE_CALL(); 485 Mutex::Autolock l(mLock); 486 487 status_t res; 488 bool wasActive = false; 489 490 switch (mStatus) { 491 case STATUS_ERROR: 492 ALOGE("%s: Device has encountered a serious error", __FUNCTION__); 493 return INVALID_OPERATION; 494 case STATUS_UNINITIALIZED: 495 ALOGE("%s: Device not initialized", __FUNCTION__); 496 return INVALID_OPERATION; 497 case STATUS_IDLE: 498 // OK 499 break; 500 case STATUS_ACTIVE: 501 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 502 mRequestThread->setPaused(true); 503 res = waitUntilDrainedLocked(); 504 if (res != OK) { 505 ALOGE("%s: Can't pause captures to reconfigure streams!", 506 __FUNCTION__); 507 mStatus = STATUS_ERROR; 508 return res; 509 } 510 wasActive = true; 511 break; 512 default: 513 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus); 514 return INVALID_OPERATION; 515 } 516 assert(mStatus == STATUS_IDLE); 517 518 if (mInputStream != 0) { 519 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); 520 return INVALID_OPERATION; 521 } 522 523 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId, 524 width, height, depth); 525 526 res = mOutputStreams.add(mNextStreamId, newStream); 527 if (res < 0) { 528 ALOGE("%s: Can't add new stream to set: %s (%d)", 529 __FUNCTION__, strerror(-res), res); 530 return res; 531 } 532 mInputStream = newStream; 533 534 *id = mNextStreamId++; 535 *zslStream = newStream; 536 537 // Continue captures if active at start 538 if (wasActive) { 539 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 540 res = configureStreamsLocked(); 541 if (res != OK) { 542 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", 543 __FUNCTION__, mNextStreamId, strerror(-res), res); 544 return res; 545 } 546 mRequestThread->setPaused(false); 547 } 548 549 return OK; 550} 551 552status_t Camera3Device::createStream(sp<ANativeWindow> consumer, 553 uint32_t width, uint32_t height, int format, size_t size, int *id) { 554 ATRACE_CALL(); 555 Mutex::Autolock l(mLock); 556 557 status_t res; 558 bool wasActive = false; 559 560 switch (mStatus) { 561 case STATUS_ERROR: 562 CLOGE("Device has encountered a serious error"); 563 return INVALID_OPERATION; 564 case STATUS_UNINITIALIZED: 565 CLOGE("Device not initialized"); 566 return INVALID_OPERATION; 567 case STATUS_IDLE: 568 // OK 569 break; 570 case STATUS_ACTIVE: 571 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); 572 mRequestThread->setPaused(true); 573 res = waitUntilDrainedLocked(); 574 if (res != OK) { 575 ALOGE("%s: Can't pause captures to reconfigure streams!", 576 __FUNCTION__); 577 return res; 578 } 579 wasActive = true; 580 break; 581 default: 582 SET_ERR_L("Unexpected status: %d", mStatus); 583 return INVALID_OPERATION; 584 } 585 assert(mStatus == STATUS_IDLE); 586 587 sp<Camera3OutputStream> newStream; 588 if (format == HAL_PIXEL_FORMAT_BLOB) { 589 newStream = new Camera3OutputStream(mNextStreamId, consumer, 590 width, height, size, format); 591 } else { 592 newStream = new Camera3OutputStream(mNextStreamId, consumer, 593 width, height, format); 594 } 595 596 res = mOutputStreams.add(mNextStreamId, newStream); 597 if (res < 0) { 598 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res); 599 return res; 600 } 601 602 *id = mNextStreamId++; 603 mNeedConfig = true; 604 605 // Continue captures if active at start 606 if (wasActive) { 607 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); 608 res = configureStreamsLocked(); 609 if (res != OK) { 610 CLOGE("Can't reconfigure device for new stream %d: %s (%d)", 611 mNextStreamId, strerror(-res), res); 612 return res; 613 } 614 mRequestThread->setPaused(false); 615 } 616 617 return OK; 618} 619 620status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) { 621 ATRACE_CALL(); 622 (void)outputId; (void)id; 623 624 CLOGE("Unimplemented"); 625 return INVALID_OPERATION; 626} 627 628 629status_t Camera3Device::getStreamInfo(int id, 630 uint32_t *width, uint32_t *height, uint32_t *format) { 631 ATRACE_CALL(); 632 Mutex::Autolock l(mLock); 633 634 switch (mStatus) { 635 case STATUS_ERROR: 636 CLOGE("Device has encountered a serious error"); 637 return INVALID_OPERATION; 638 case STATUS_UNINITIALIZED: 639 CLOGE("Device not initialized!"); 640 return INVALID_OPERATION; 641 case STATUS_IDLE: 642 case STATUS_ACTIVE: 643 // OK 644 break; 645 default: 646 SET_ERR_L("Unexpected status: %d", mStatus); 647 return INVALID_OPERATION; 648 } 649 650 ssize_t idx = mOutputStreams.indexOfKey(id); 651 if (idx == NAME_NOT_FOUND) { 652 CLOGE("Stream %d is unknown", id); 653 return idx; 654 } 655 656 if (width) *width = mOutputStreams[idx]->getWidth(); 657 if (height) *height = mOutputStreams[idx]->getHeight(); 658 if (format) *format = mOutputStreams[idx]->getFormat(); 659 660 return OK; 661} 662 663status_t Camera3Device::setStreamTransform(int id, 664 int transform) { 665 ATRACE_CALL(); 666 Mutex::Autolock l(mLock); 667 668 switch (mStatus) { 669 case STATUS_ERROR: 670 CLOGE("Device has encountered a serious error"); 671 return INVALID_OPERATION; 672 case STATUS_UNINITIALIZED: 673 CLOGE("Device not initialized"); 674 return INVALID_OPERATION; 675 case STATUS_IDLE: 676 case STATUS_ACTIVE: 677 // OK 678 break; 679 default: 680 SET_ERR_L("Unexpected status: %d", mStatus); 681 return INVALID_OPERATION; 682 } 683 684 ssize_t idx = mOutputStreams.indexOfKey(id); 685 if (idx == NAME_NOT_FOUND) { 686 CLOGE("Stream %d does not exist", 687 id); 688 return BAD_VALUE; 689 } 690 691 return mOutputStreams.editValueAt(idx)->setTransform(transform); 692} 693 694status_t Camera3Device::deleteStream(int id) { 695 ATRACE_CALL(); 696 Mutex::Autolock l(mLock); 697 status_t res; 698 699 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id); 700 701 // CameraDevice semantics require device to already be idle before 702 // deleteStream is called, unlike for createStream. 703 if (mStatus != STATUS_IDLE) { 704 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId); 705 return -EBUSY; 706 } 707 708 sp<Camera3StreamInterface> deletedStream; 709 if (mInputStream != NULL && id == mInputStream->getId()) { 710 deletedStream = mInputStream; 711 mInputStream.clear(); 712 } else { 713 ssize_t idx = mOutputStreams.indexOfKey(id); 714 if (idx == NAME_NOT_FOUND) { 715 CLOGE("Stream %d does not exist", id); 716 return BAD_VALUE; 717 } 718 deletedStream = mOutputStreams.editValueAt(idx); 719 mOutputStreams.removeItem(id); 720 } 721 722 // Free up the stream endpoint so that it can be used by some other stream 723 res = deletedStream->disconnect(); 724 if (res != OK) { 725 SET_ERR_L("Can't disconnect deleted stream %d", id); 726 // fall through since we want to still list the stream as deleted. 727 } 728 mDeletedStreams.add(deletedStream); 729 mNeedConfig = true; 730 731 return res; 732} 733 734status_t Camera3Device::deleteReprocessStream(int id) { 735 ATRACE_CALL(); 736 (void)id; 737 738 CLOGE("Unimplemented"); 739 return INVALID_OPERATION; 740} 741 742 743status_t Camera3Device::createDefaultRequest(int templateId, 744 CameraMetadata *request) { 745 ATRACE_CALL(); 746 ALOGV("%s: for template %d", __FUNCTION__, templateId); 747 Mutex::Autolock l(mLock); 748 749 switch (mStatus) { 750 case STATUS_ERROR: 751 CLOGE("Device has encountered a serious error"); 752 return INVALID_OPERATION; 753 case STATUS_UNINITIALIZED: 754 CLOGE("Device is not initialized!"); 755 return INVALID_OPERATION; 756 case STATUS_IDLE: 757 case STATUS_ACTIVE: 758 // OK 759 break; 760 default: 761 SET_ERR_L("Unexpected status: %d", mStatus); 762 return INVALID_OPERATION; 763 } 764 765 const camera_metadata_t *rawRequest; 766 ATRACE_BEGIN("camera3->construct_default_request_settings"); 767 rawRequest = mHal3Device->ops->construct_default_request_settings( 768 mHal3Device, templateId); 769 ATRACE_END(); 770 if (rawRequest == NULL) { 771 SET_ERR_L("HAL is unable to construct default settings for template %d", 772 templateId); 773 return DEAD_OBJECT; 774 } 775 *request = rawRequest; 776 777 return OK; 778} 779 780status_t Camera3Device::waitUntilDrained() { 781 ATRACE_CALL(); 782 Mutex::Autolock l(mLock); 783 784 return waitUntilDrainedLocked(); 785} 786 787status_t Camera3Device::waitUntilDrainedLocked() { 788 ATRACE_CALL(); 789 status_t res; 790 791 switch (mStatus) { 792 case STATUS_UNINITIALIZED: 793 case STATUS_IDLE: 794 ALOGV("%s: Already idle", __FUNCTION__); 795 return OK; 796 case STATUS_ERROR: 797 case STATUS_ACTIVE: 798 // Need to shut down 799 break; 800 default: 801 SET_ERR_L("Unexpected status: %d",mStatus); 802 return INVALID_OPERATION; 803 } 804 805 if (mRequestThread != NULL) { 806 res = mRequestThread->waitUntilPaused(kShutdownTimeout); 807 if (res != OK) { 808 SET_ERR_L("Can't stop request thread in %f seconds!", 809 kShutdownTimeout/1e9); 810 return res; 811 } 812 } 813 if (mInputStream != NULL) { 814 res = mInputStream->waitUntilIdle(kShutdownTimeout); 815 if (res != OK) { 816 SET_ERR_L("Can't idle input stream %d in %f seconds!", 817 mInputStream->getId(), kShutdownTimeout/1e9); 818 return res; 819 } 820 } 821 for (size_t i = 0; i < mOutputStreams.size(); i++) { 822 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout); 823 if (res != OK) { 824 SET_ERR_L("Can't idle output stream %d in %f seconds!", 825 mOutputStreams.keyAt(i), kShutdownTimeout/1e9); 826 return res; 827 } 828 } 829 830 if (mStatus != STATUS_ERROR) { 831 mStatus = STATUS_IDLE; 832 } 833 834 return OK; 835} 836 837status_t Camera3Device::setNotifyCallback(NotificationListener *listener) { 838 ATRACE_CALL(); 839 Mutex::Autolock l(mOutputLock); 840 841 if (listener != NULL && mListener != NULL) { 842 ALOGW("%s: Replacing old callback listener", __FUNCTION__); 843 } 844 mListener = listener; 845 846 return OK; 847} 848 849bool Camera3Device::willNotify3A() { 850 return false; 851} 852 853status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { 854 ATRACE_CALL(); 855 status_t res; 856 Mutex::Autolock l(mOutputLock); 857 858 while (mResultQueue.empty()) { 859 res = mResultSignal.waitRelative(mOutputLock, timeout); 860 if (res == TIMED_OUT) { 861 return res; 862 } else if (res != OK) { 863 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)", 864 __FUNCTION__, mId, timeout, strerror(-res), res); 865 return res; 866 } 867 } 868 return OK; 869} 870 871status_t Camera3Device::getNextFrame(CameraMetadata *frame) { 872 ATRACE_CALL(); 873 Mutex::Autolock l(mOutputLock); 874 875 if (mResultQueue.empty()) { 876 return NOT_ENOUGH_DATA; 877 } 878 879 CameraMetadata &result = *(mResultQueue.begin()); 880 frame->acquire(result); 881 mResultQueue.erase(mResultQueue.begin()); 882 883 return OK; 884} 885 886status_t Camera3Device::triggerAutofocus(uint32_t id) { 887 ATRACE_CALL(); 888 889 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); 890 // Mix-in this trigger into the next request and only the next request. 891 RequestTrigger trigger[] = { 892 { 893 ANDROID_CONTROL_AF_TRIGGER, 894 ANDROID_CONTROL_AF_TRIGGER_START 895 }, 896 { 897 ANDROID_CONTROL_AF_TRIGGER_ID, 898 static_cast<int32_t>(id) 899 }, 900 }; 901 902 return mRequestThread->queueTrigger(trigger, 903 sizeof(trigger)/sizeof(trigger[0])); 904} 905 906status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { 907 ATRACE_CALL(); 908 909 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); 910 // Mix-in this trigger into the next request and only the next request. 911 RequestTrigger trigger[] = { 912 { 913 ANDROID_CONTROL_AF_TRIGGER, 914 ANDROID_CONTROL_AF_TRIGGER_CANCEL 915 }, 916 { 917 ANDROID_CONTROL_AF_TRIGGER_ID, 918 static_cast<int32_t>(id) 919 }, 920 }; 921 922 return mRequestThread->queueTrigger(trigger, 923 sizeof(trigger)/sizeof(trigger[0])); 924} 925 926status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { 927 ATRACE_CALL(); 928 929 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); 930 // Mix-in this trigger into the next request and only the next request. 931 RequestTrigger trigger[] = { 932 { 933 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, 934 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START 935 }, 936 { 937 ANDROID_CONTROL_AE_PRECAPTURE_ID, 938 static_cast<int32_t>(id) 939 }, 940 }; 941 942 return mRequestThread->queueTrigger(trigger, 943 sizeof(trigger)/sizeof(trigger[0])); 944} 945 946status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId, 947 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) { 948 ATRACE_CALL(); 949 (void)reprocessStreamId; (void)buffer; (void)listener; 950 951 CLOGE("Unimplemented"); 952 return INVALID_OPERATION; 953} 954 955/** 956 * Camera3Device private methods 957 */ 958 959sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( 960 const CameraMetadata &request) { 961 ATRACE_CALL(); 962 status_t res; 963 964 sp<CaptureRequest> newRequest = new CaptureRequest; 965 newRequest->mSettings = request; 966 967 camera_metadata_entry_t inputStreams = 968 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); 969 if (inputStreams.count > 0) { 970 if (mInputStream == NULL || 971 mInputStream->getId() != inputStreams.data.u8[0]) { 972 CLOGE("Request references unknown input stream %d", 973 inputStreams.data.u8[0]); 974 return NULL; 975 } 976 // Lazy completion of stream configuration (allocation/registration) 977 // on first use 978 if (mInputStream->isConfiguring()) { 979 res = mInputStream->finishConfiguration(mHal3Device); 980 if (res != OK) { 981 SET_ERR_L("Unable to finish configuring input stream %d:" 982 " %s (%d)", 983 mInputStream->getId(), strerror(-res), res); 984 return NULL; 985 } 986 } 987 988 newRequest->mInputStream = mInputStream; 989 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); 990 } 991 992 camera_metadata_entry_t streams = 993 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); 994 if (streams.count == 0) { 995 CLOGE("Zero output streams specified!"); 996 return NULL; 997 } 998 999 for (size_t i = 0; i < streams.count; i++) { 1000 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]); 1001 if (idx == NAME_NOT_FOUND) { 1002 CLOGE("Request references unknown stream %d", 1003 streams.data.u8[i]); 1004 return NULL; 1005 } 1006 sp<Camera3OutputStreamInterface> stream = 1007 mOutputStreams.editValueAt(idx); 1008 1009 // Lazy completion of stream configuration (allocation/registration) 1010 // on first use 1011 if (stream->isConfiguring()) { 1012 res = stream->finishConfiguration(mHal3Device); 1013 if (res != OK) { 1014 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", 1015 stream->getId(), strerror(-res), res); 1016 return NULL; 1017 } 1018 } 1019 1020 newRequest->mOutputStreams.push(stream); 1021 } 1022 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); 1023 1024 return newRequest; 1025} 1026 1027status_t Camera3Device::configureStreamsLocked() { 1028 ATRACE_CALL(); 1029 status_t res; 1030 1031 if (mStatus != STATUS_IDLE) { 1032 CLOGE("Not idle"); 1033 return INVALID_OPERATION; 1034 } 1035 1036 if (!mNeedConfig) { 1037 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); 1038 mStatus = STATUS_ACTIVE; 1039 return OK; 1040 } 1041 1042 // Start configuring the streams 1043 1044 camera3_stream_configuration config; 1045 1046 config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); 1047 1048 Vector<camera3_stream_t*> streams; 1049 streams.setCapacity(config.num_streams); 1050 1051 if (mInputStream != NULL) { 1052 camera3_stream_t *inputStream; 1053 inputStream = mInputStream->startConfiguration(); 1054 if (inputStream == NULL) { 1055 SET_ERR_L("Can't start input stream configuration"); 1056 return INVALID_OPERATION; 1057 } 1058 streams.add(inputStream); 1059 } 1060 1061 for (size_t i = 0; i < mOutputStreams.size(); i++) { 1062 1063 // Don't configure bidi streams twice, nor add them twice to the list 1064 if (mOutputStreams[i].get() == 1065 static_cast<Camera3StreamInterface*>(mInputStream.get())) { 1066 1067 config.num_streams--; 1068 continue; 1069 } 1070 1071 camera3_stream_t *outputStream; 1072 outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); 1073 if (outputStream == NULL) { 1074 SET_ERR_L("Can't start output stream configuration"); 1075 return INVALID_OPERATION; 1076 } 1077 streams.add(outputStream); 1078 } 1079 1080 config.streams = streams.editArray(); 1081 1082 // Do the HAL configuration; will potentially touch stream 1083 // max_buffers, usage, priv fields. 1084 ATRACE_BEGIN("camera3->configure_streams"); 1085 res = mHal3Device->ops->configure_streams(mHal3Device, &config); 1086 ATRACE_END(); 1087 1088 if (res != OK) { 1089 SET_ERR_L("Unable to configure streams with HAL: %s (%d)", 1090 strerror(-res), res); 1091 return res; 1092 } 1093 1094 // Finish all stream configuration immediately. 1095 // TODO: Try to relax this later back to lazy completion, which should be 1096 // faster 1097 1098 if (mInputStream != NULL && mInputStream->isConfiguring()) { 1099 res = mInputStream->finishConfiguration(mHal3Device); 1100 if (res != OK) { 1101 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)", 1102 mInputStream->getId(), strerror(-res), res); 1103 return res; 1104 } 1105 } 1106 1107 for (size_t i = 0; i < mOutputStreams.size(); i++) { 1108 sp<Camera3OutputStreamInterface> outputStream = 1109 mOutputStreams.editValueAt(i); 1110 if (outputStream->isConfiguring()) { 1111 res = outputStream->finishConfiguration(mHal3Device); 1112 if (res != OK) { 1113 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", 1114 outputStream->getId(), strerror(-res), res); 1115 return res; 1116 } 1117 } 1118 } 1119 1120 // Request thread needs to know to avoid using repeat-last-settings protocol 1121 // across configure_streams() calls 1122 mRequestThread->configurationComplete(); 1123 1124 // Finish configuring the streams lazily on first reference 1125 1126 mStatus = STATUS_ACTIVE; 1127 mNeedConfig = false; 1128 1129 return OK; 1130} 1131 1132void Camera3Device::setErrorState(const char *fmt, ...) { 1133 Mutex::Autolock l(mLock); 1134 va_list args; 1135 va_start(args, fmt); 1136 1137 setErrorStateLockedV(fmt, args); 1138 1139 va_end(args); 1140} 1141 1142void Camera3Device::setErrorStateV(const char *fmt, va_list args) { 1143 Mutex::Autolock l(mLock); 1144 setErrorStateLockedV(fmt, args); 1145} 1146 1147void Camera3Device::setErrorStateLocked(const char *fmt, ...) { 1148 va_list args; 1149 va_start(args, fmt); 1150 1151 setErrorStateLockedV(fmt, args); 1152 1153 va_end(args); 1154} 1155 1156void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { 1157 // Print out all error messages to log 1158 String8 errorCause = String8::formatV(fmt, args); 1159 ALOGE("Camera %d: %s", mId, errorCause.string()); 1160 1161 // But only do error state transition steps for the first error 1162 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; 1163 1164 mErrorCause = errorCause; 1165 1166 mRequestThread->setPaused(true); 1167 mStatus = STATUS_ERROR; 1168} 1169 1170/** 1171 * In-flight request management 1172 */ 1173 1174status_t Camera3Device::registerInFlight(int32_t frameNumber, 1175 int32_t numBuffers) { 1176 ATRACE_CALL(); 1177 Mutex::Autolock l(mInFlightLock); 1178 1179 ssize_t res; 1180 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers)); 1181 if (res < 0) return res; 1182 1183 return OK; 1184} 1185 1186/** 1187 * Camera HAL device callback methods 1188 */ 1189 1190void Camera3Device::processCaptureResult(const camera3_capture_result *result) { 1191 ATRACE_CALL(); 1192 1193 status_t res; 1194 1195 uint32_t frameNumber = result->frame_number; 1196 if (result->result == NULL && result->num_output_buffers == 0) { 1197 SET_ERR("No result data provided by HAL for frame %d", 1198 frameNumber); 1199 return; 1200 } 1201 1202 // Get capture timestamp from list of in-flight requests, where it was added 1203 // by the shutter notification for this frame. Then update the in-flight 1204 // status and remove the in-flight entry if all result data has been 1205 // received. 1206 nsecs_t timestamp = 0; 1207 { 1208 Mutex::Autolock l(mInFlightLock); 1209 ssize_t idx = mInFlightMap.indexOfKey(frameNumber); 1210 if (idx == NAME_NOT_FOUND) { 1211 SET_ERR("Unknown frame number for capture result: %d", 1212 frameNumber); 1213 return; 1214 } 1215 InFlightRequest &request = mInFlightMap.editValueAt(idx); 1216 timestamp = request.captureTimestamp; 1217 if (timestamp == 0) { 1218 SET_ERR("Called before shutter notify for frame %d", 1219 frameNumber); 1220 return; 1221 } 1222 1223 if (result->result != NULL) { 1224 if (request.haveResultMetadata) { 1225 SET_ERR("Called multiple times with metadata for frame %d", 1226 frameNumber); 1227 return; 1228 } 1229 request.haveResultMetadata = true; 1230 } 1231 1232 request.numBuffersLeft -= result->num_output_buffers; 1233 1234 if (request.numBuffersLeft < 0) { 1235 SET_ERR("Too many buffers returned for frame %d", 1236 frameNumber); 1237 return; 1238 } 1239 1240 if (request.haveResultMetadata && request.numBuffersLeft == 0) { 1241 ATRACE_ASYNC_END("frame capture", frameNumber); 1242 mInFlightMap.removeItemsAt(idx, 1); 1243 } 1244 1245 // Sanity check - if we have too many in-flight frames, something has 1246 // likely gone wrong 1247 if (mInFlightMap.size() > kInFlightWarnLimit) { 1248 CLOGE("In-flight list too large: %d", mInFlightMap.size()); 1249 } 1250 1251 } 1252 1253 // Process the result metadata, if provided 1254 if (result->result != NULL) { 1255 Mutex::Autolock l(mOutputLock); 1256 1257 if (frameNumber != mNextResultFrameNumber) { 1258 SET_ERR("Out-of-order capture result metadata submitted! " 1259 "(got frame number %d, expecting %d)", 1260 frameNumber, mNextResultFrameNumber); 1261 return; 1262 } 1263 mNextResultFrameNumber++; 1264 1265 CameraMetadata &captureResult = 1266 *mResultQueue.insert(mResultQueue.end(), CameraMetadata()); 1267 1268 captureResult = result->result; 1269 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT, 1270 (int32_t*)&frameNumber, 1) != OK) { 1271 SET_ERR("Failed to set frame# in metadata (%d)", 1272 frameNumber); 1273 } else { 1274 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)", 1275 __FUNCTION__, mId, frameNumber); 1276 } 1277 1278 // Check that there's a timestamp in the result metadata 1279 1280 camera_metadata_entry entry = 1281 captureResult.find(ANDROID_SENSOR_TIMESTAMP); 1282 if (entry.count == 0) { 1283 SET_ERR("No timestamp provided by HAL for frame %d!", 1284 frameNumber); 1285 } else if (timestamp != entry.data.i64[0]) { 1286 SET_ERR("Timestamp mismatch between shutter notify and result" 1287 " metadata for frame %d (%lld vs %lld respectively)", 1288 frameNumber, timestamp, entry.data.i64[0]); 1289 } 1290 } // scope for mOutputLock 1291 1292 // Return completed buffers to their streams with the timestamp 1293 1294 for (size_t i = 0; i < result->num_output_buffers; i++) { 1295 Camera3Stream *stream = 1296 Camera3Stream::cast(result->output_buffers[i].stream); 1297 res = stream->returnBuffer(result->output_buffers[i], timestamp); 1298 // Note: stream may be deallocated at this point, if this buffer was the 1299 // last reference to it. 1300 if (res != OK) { 1301 SET_ERR("Can't return buffer %d for frame %d to its stream: " 1302 " %s (%d)", i, frameNumber, strerror(-res), res); 1303 } 1304 } 1305 1306 // Finally, signal any waiters for new frames 1307 1308 if (result->result != NULL) { 1309 mResultSignal.signal(); 1310 } 1311 1312} 1313 1314 1315 1316void Camera3Device::notify(const camera3_notify_msg *msg) { 1317 ATRACE_CALL(); 1318 NotificationListener *listener; 1319 { 1320 Mutex::Autolock l(mOutputLock); 1321 listener = mListener; 1322 } 1323 1324 if (msg == NULL) { 1325 SET_ERR("HAL sent NULL notify message!"); 1326 return; 1327 } 1328 1329 switch (msg->type) { 1330 case CAMERA3_MSG_ERROR: { 1331 int streamId = 0; 1332 if (msg->message.error.error_stream != NULL) { 1333 Camera3Stream *stream = 1334 Camera3Stream::cast( 1335 msg->message.error.error_stream); 1336 streamId = stream->getId(); 1337 } 1338 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d", 1339 mId, __FUNCTION__, msg->message.error.frame_number, 1340 streamId, msg->message.error.error_code); 1341 if (listener != NULL) { 1342 listener->notifyError(msg->message.error.error_code, 1343 msg->message.error.frame_number, streamId); 1344 } 1345 break; 1346 } 1347 case CAMERA3_MSG_SHUTTER: { 1348 ssize_t idx; 1349 uint32_t frameNumber = msg->message.shutter.frame_number; 1350 nsecs_t timestamp = msg->message.shutter.timestamp; 1351 // Verify ordering of shutter notifications 1352 { 1353 Mutex::Autolock l(mOutputLock); 1354 if (frameNumber != mNextShutterFrameNumber) { 1355 SET_ERR("Shutter notification out-of-order. Expected " 1356 "notification for frame %d, got frame %d", 1357 mNextShutterFrameNumber, frameNumber); 1358 break; 1359 } 1360 mNextShutterFrameNumber++; 1361 } 1362 1363 // Set timestamp for the request in the in-flight tracking 1364 { 1365 Mutex::Autolock l(mInFlightLock); 1366 idx = mInFlightMap.indexOfKey(frameNumber); 1367 if (idx >= 0) { 1368 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp; 1369 } 1370 } 1371 if (idx < 0) { 1372 SET_ERR("Shutter notification for non-existent frame number %d", 1373 frameNumber); 1374 break; 1375 } 1376 ALOGVV("Camera %d: %s: Shutter fired for frame %d at %lld", 1377 mId, __FUNCTION__, frameNumber, timestamp); 1378 // Call listener, if any 1379 if (listener != NULL) { 1380 listener->notifyShutter(frameNumber, timestamp); 1381 } 1382 break; 1383 } 1384 default: 1385 SET_ERR("Unknown notify message from HAL: %d", 1386 msg->type); 1387 } 1388} 1389 1390/** 1391 * RequestThread inner class methods 1392 */ 1393 1394Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, 1395 camera3_device_t *hal3Device) : 1396 Thread(false), 1397 mParent(parent), 1398 mHal3Device(hal3Device), 1399 mId(getId(parent)), 1400 mReconfigured(false), 1401 mDoPause(false), 1402 mPaused(true), 1403 mFrameNumber(0), 1404 mLatestRequestId(NAME_NOT_FOUND) { 1405} 1406 1407void Camera3Device::RequestThread::configurationComplete() { 1408 Mutex::Autolock l(mRequestLock); 1409 mReconfigured = true; 1410} 1411 1412status_t Camera3Device::RequestThread::queueRequest( 1413 sp<CaptureRequest> request) { 1414 Mutex::Autolock l(mRequestLock); 1415 mRequestQueue.push_back(request); 1416 1417 return OK; 1418} 1419 1420 1421status_t Camera3Device::RequestThread::queueTrigger( 1422 RequestTrigger trigger[], 1423 size_t count) { 1424 1425 Mutex::Autolock l(mTriggerMutex); 1426 status_t ret; 1427 1428 for (size_t i = 0; i < count; ++i) { 1429 ret = queueTriggerLocked(trigger[i]); 1430 1431 if (ret != OK) { 1432 return ret; 1433 } 1434 } 1435 1436 return OK; 1437} 1438 1439int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { 1440 sp<Camera3Device> d = device.promote(); 1441 if (d != NULL) return d->mId; 1442 return 0; 1443} 1444 1445status_t Camera3Device::RequestThread::queueTriggerLocked( 1446 RequestTrigger trigger) { 1447 1448 uint32_t tag = trigger.metadataTag; 1449 ssize_t index = mTriggerMap.indexOfKey(tag); 1450 1451 switch (trigger.getTagType()) { 1452 case TYPE_BYTE: 1453 // fall-through 1454 case TYPE_INT32: 1455 break; 1456 default: 1457 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, 1458 trigger.getTagType()); 1459 return INVALID_OPERATION; 1460 } 1461 1462 /** 1463 * Collect only the latest trigger, since we only have 1 field 1464 * in the request settings per trigger tag, and can't send more than 1 1465 * trigger per request. 1466 */ 1467 if (index != NAME_NOT_FOUND) { 1468 mTriggerMap.editValueAt(index) = trigger; 1469 } else { 1470 mTriggerMap.add(tag, trigger); 1471 } 1472 1473 return OK; 1474} 1475 1476status_t Camera3Device::RequestThread::setRepeatingRequests( 1477 const RequestList &requests) { 1478 Mutex::Autolock l(mRequestLock); 1479 mRepeatingRequests.clear(); 1480 mRepeatingRequests.insert(mRepeatingRequests.begin(), 1481 requests.begin(), requests.end()); 1482 return OK; 1483} 1484 1485status_t Camera3Device::RequestThread::clearRepeatingRequests() { 1486 Mutex::Autolock l(mRequestLock); 1487 mRepeatingRequests.clear(); 1488 return OK; 1489} 1490 1491void Camera3Device::RequestThread::setPaused(bool paused) { 1492 Mutex::Autolock l(mPauseLock); 1493 mDoPause = paused; 1494 mDoPauseSignal.signal(); 1495} 1496 1497status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) { 1498 ATRACE_CALL(); 1499 status_t res; 1500 Mutex::Autolock l(mPauseLock); 1501 while (!mPaused) { 1502 res = mPausedSignal.waitRelative(mPauseLock, timeout); 1503 if (res == TIMED_OUT) { 1504 return res; 1505 } 1506 } 1507 return OK; 1508} 1509 1510status_t Camera3Device::RequestThread::waitUntilRequestProcessed( 1511 int32_t requestId, nsecs_t timeout) { 1512 Mutex::Autolock l(mLatestRequestMutex); 1513 status_t res; 1514 while (mLatestRequestId != requestId) { 1515 nsecs_t startTime = systemTime(); 1516 1517 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); 1518 if (res != OK) return res; 1519 1520 timeout -= (systemTime() - startTime); 1521 } 1522 1523 return OK; 1524} 1525 1526 1527 1528bool Camera3Device::RequestThread::threadLoop() { 1529 1530 status_t res; 1531 1532 // Handle paused state. 1533 if (waitIfPaused()) { 1534 return true; 1535 } 1536 1537 // Get work to do 1538 1539 sp<CaptureRequest> nextRequest = waitForNextRequest(); 1540 if (nextRequest == NULL) { 1541 return true; 1542 } 1543 1544 // Create request to HAL 1545 camera3_capture_request_t request = camera3_capture_request_t(); 1546 Vector<camera3_stream_buffer_t> outputBuffers; 1547 1548 // Insert any queued triggers (before metadata is locked) 1549 int32_t triggerCount; 1550 res = insertTriggers(nextRequest); 1551 if (res < 0) { 1552 SET_ERR("RequestThread: Unable to insert triggers " 1553 "(capture request %d, HAL device: %s (%d)", 1554 (mFrameNumber+1), strerror(-res), res); 1555 cleanUpFailedRequest(request, nextRequest, outputBuffers); 1556 return false; 1557 } 1558 triggerCount = res; 1559 1560 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); 1561 1562 // If the request is the same as last, or we had triggers last time 1563 if (mPrevRequest != nextRequest || triggersMixedIn) { 1564 /** 1565 * The request should be presorted so accesses in HAL 1566 * are O(logn). Sidenote, sorting a sorted metadata is nop. 1567 */ 1568 nextRequest->mSettings.sort(); 1569 request.settings = nextRequest->mSettings.getAndLock(); 1570 mPrevRequest = nextRequest; 1571 ALOGVV("%s: Request settings are NEW", __FUNCTION__); 1572 1573 IF_ALOGV() { 1574 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); 1575 find_camera_metadata_ro_entry( 1576 request.settings, 1577 ANDROID_CONTROL_AF_TRIGGER, 1578 &e 1579 ); 1580 if (e.count > 0) { 1581 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", 1582 __FUNCTION__, 1583 mFrameNumber+1, 1584 e.data.u8[0]); 1585 } 1586 } 1587 } else { 1588 // leave request.settings NULL to indicate 'reuse latest given' 1589 ALOGVV("%s: Request settings are REUSED", 1590 __FUNCTION__); 1591 } 1592 1593 camera3_stream_buffer_t inputBuffer; 1594 1595 // Fill in buffers 1596 1597 if (nextRequest->mInputStream != NULL) { 1598 request.input_buffer = &inputBuffer; 1599 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer); 1600 if (res != OK) { 1601 SET_ERR("RequestThread: Can't get input buffer, skipping request:" 1602 " %s (%d)", strerror(-res), res); 1603 cleanUpFailedRequest(request, nextRequest, outputBuffers); 1604 return true; 1605 } 1606 } else { 1607 request.input_buffer = NULL; 1608 } 1609 1610 outputBuffers.insertAt(camera3_stream_buffer_t(), 0, 1611 nextRequest->mOutputStreams.size()); 1612 request.output_buffers = outputBuffers.array(); 1613 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) { 1614 res = nextRequest->mOutputStreams.editItemAt(i)-> 1615 getBuffer(&outputBuffers.editItemAt(i)); 1616 if (res != OK) { 1617 SET_ERR("RequestThread: Can't get output buffer, skipping request:" 1618 "%s (%d)", strerror(-res), res); 1619 cleanUpFailedRequest(request, nextRequest, outputBuffers); 1620 return true; 1621 } 1622 request.num_output_buffers++; 1623 } 1624 1625 request.frame_number = mFrameNumber++; 1626 1627 // Log request in the in-flight queue 1628 sp<Camera3Device> parent = mParent.promote(); 1629 if (parent == NULL) { 1630 CLOGE("RequestThread: Parent is gone"); 1631 cleanUpFailedRequest(request, nextRequest, outputBuffers); 1632 return false; 1633 } 1634 1635 res = parent->registerInFlight(request.frame_number, 1636 request.num_output_buffers); 1637 if (res != OK) { 1638 SET_ERR("RequestThread: Unable to register new in-flight request:" 1639 " %s (%d)", strerror(-res), res); 1640 cleanUpFailedRequest(request, nextRequest, outputBuffers); 1641 return false; 1642 } 1643 1644 // Submit request and block until ready for next one 1645 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number); 1646 ATRACE_BEGIN("camera3->process_capture_request"); 1647 res = mHal3Device->ops->process_capture_request(mHal3Device, &request); 1648 ATRACE_END(); 1649 1650 if (res != OK) { 1651 SET_ERR("RequestThread: Unable to submit capture request %d to HAL" 1652 " device: %s (%d)", request.frame_number, strerror(-res), res); 1653 cleanUpFailedRequest(request, nextRequest, outputBuffers); 1654 return false; 1655 } 1656 1657 if (request.settings != NULL) { 1658 nextRequest->mSettings.unlock(request.settings); 1659 } 1660 1661 // Remove any previously queued triggers (after unlock) 1662 res = removeTriggers(mPrevRequest); 1663 if (res != OK) { 1664 SET_ERR("RequestThread: Unable to remove triggers " 1665 "(capture request %d, HAL device: %s (%d)", 1666 request.frame_number, strerror(-res), res); 1667 return false; 1668 } 1669 mPrevTriggers = triggerCount; 1670 1671 // Read android.request.id from the request settings metadata 1672 // - inform waitUntilRequestProcessed thread of a new request ID 1673 { 1674 Mutex::Autolock al(mLatestRequestMutex); 1675 1676 camera_metadata_entry_t requestIdEntry = 1677 nextRequest->mSettings.find(ANDROID_REQUEST_ID); 1678 if (requestIdEntry.count > 0) { 1679 mLatestRequestId = requestIdEntry.data.i32[0]; 1680 } else { 1681 ALOGW("%s: Did not have android.request.id set in the request", 1682 __FUNCTION__); 1683 mLatestRequestId = NAME_NOT_FOUND; 1684 } 1685 1686 mLatestRequestSignal.signal(); 1687 } 1688 1689 // Return input buffer back to framework 1690 if (request.input_buffer != NULL) { 1691 Camera3Stream *stream = 1692 Camera3Stream::cast(request.input_buffer->stream); 1693 res = stream->returnInputBuffer(*(request.input_buffer)); 1694 // Note: stream may be deallocated at this point, if this buffer was the 1695 // last reference to it. 1696 if (res != OK) { 1697 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" 1698 " its stream:%s (%d)", __FUNCTION__, 1699 request.frame_number, strerror(-res), res); 1700 // TODO: Report error upstream 1701 } 1702 } 1703 1704 1705 1706 return true; 1707} 1708 1709void Camera3Device::RequestThread::cleanUpFailedRequest( 1710 camera3_capture_request_t &request, 1711 sp<CaptureRequest> &nextRequest, 1712 Vector<camera3_stream_buffer_t> &outputBuffers) { 1713 1714 if (request.settings != NULL) { 1715 nextRequest->mSettings.unlock(request.settings); 1716 } 1717 if (request.input_buffer != NULL) { 1718 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR; 1719 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer)); 1720 } 1721 for (size_t i = 0; i < request.num_output_buffers; i++) { 1722 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; 1723 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer( 1724 outputBuffers[i], 0); 1725 } 1726} 1727 1728sp<Camera3Device::CaptureRequest> 1729 Camera3Device::RequestThread::waitForNextRequest() { 1730 status_t res; 1731 sp<CaptureRequest> nextRequest; 1732 1733 // Optimized a bit for the simple steady-state case (single repeating 1734 // request), to avoid putting that request in the queue temporarily. 1735 Mutex::Autolock l(mRequestLock); 1736 1737 while (mRequestQueue.empty()) { 1738 if (!mRepeatingRequests.empty()) { 1739 // Always atomically enqueue all requests in a repeating request 1740 // list. Guarantees a complete in-sequence set of captures to 1741 // application. 1742 const RequestList &requests = mRepeatingRequests; 1743 RequestList::const_iterator firstRequest = 1744 requests.begin(); 1745 nextRequest = *firstRequest; 1746 mRequestQueue.insert(mRequestQueue.end(), 1747 ++firstRequest, 1748 requests.end()); 1749 // No need to wait any longer 1750 break; 1751 } 1752 1753 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); 1754 1755 if (res == TIMED_OUT) { 1756 // Signal that we're paused by starvation 1757 Mutex::Autolock pl(mPauseLock); 1758 if (mPaused == false) { 1759 mPaused = true; 1760 mPausedSignal.signal(); 1761 } 1762 // Stop waiting for now and let thread management happen 1763 return NULL; 1764 } 1765 } 1766 1767 if (nextRequest == NULL) { 1768 // Don't have a repeating request already in hand, so queue 1769 // must have an entry now. 1770 RequestList::iterator firstRequest = 1771 mRequestQueue.begin(); 1772 nextRequest = *firstRequest; 1773 mRequestQueue.erase(firstRequest); 1774 } 1775 1776 // Not paused 1777 Mutex::Autolock pl(mPauseLock); 1778 mPaused = false; 1779 1780 // Check if we've reconfigured since last time, and reset the preview 1781 // request if so. Can't use 'NULL request == repeat' across configure calls. 1782 if (mReconfigured) { 1783 mPrevRequest.clear(); 1784 mReconfigured = false; 1785 } 1786 1787 return nextRequest; 1788} 1789 1790bool Camera3Device::RequestThread::waitIfPaused() { 1791 status_t res; 1792 Mutex::Autolock l(mPauseLock); 1793 while (mDoPause) { 1794 // Signal that we're paused by request 1795 if (mPaused == false) { 1796 mPaused = true; 1797 mPausedSignal.signal(); 1798 } 1799 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); 1800 if (res == TIMED_OUT) { 1801 return true; 1802 } 1803 } 1804 // We don't set mPaused to false here, because waitForNextRequest needs 1805 // to further manage the paused state in case of starvation. 1806 return false; 1807} 1808 1809void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { 1810 sp<Camera3Device> parent = mParent.promote(); 1811 if (parent != NULL) { 1812 va_list args; 1813 va_start(args, fmt); 1814 1815 parent->setErrorStateV(fmt, args); 1816 1817 va_end(args); 1818 } 1819} 1820 1821status_t Camera3Device::RequestThread::insertTriggers( 1822 const sp<CaptureRequest> &request) { 1823 1824 Mutex::Autolock al(mTriggerMutex); 1825 1826 CameraMetadata &metadata = request->mSettings; 1827 size_t count = mTriggerMap.size(); 1828 1829 for (size_t i = 0; i < count; ++i) { 1830 RequestTrigger trigger = mTriggerMap.valueAt(i); 1831 1832 uint32_t tag = trigger.metadataTag; 1833 camera_metadata_entry entry = metadata.find(tag); 1834 1835 if (entry.count > 0) { 1836 /** 1837 * Already has an entry for this trigger in the request. 1838 * Rewrite it with our requested trigger value. 1839 */ 1840 RequestTrigger oldTrigger = trigger; 1841 1842 oldTrigger.entryValue = entry.data.u8[0]; 1843 1844 mTriggerReplacedMap.add(tag, oldTrigger); 1845 } else { 1846 /** 1847 * More typical, no trigger entry, so we just add it 1848 */ 1849 mTriggerRemovedMap.add(tag, trigger); 1850 } 1851 1852 status_t res; 1853 1854 switch (trigger.getTagType()) { 1855 case TYPE_BYTE: { 1856 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 1857 res = metadata.update(tag, 1858 &entryValue, 1859 /*count*/1); 1860 break; 1861 } 1862 case TYPE_INT32: 1863 res = metadata.update(tag, 1864 &trigger.entryValue, 1865 /*count*/1); 1866 break; 1867 default: 1868 ALOGE("%s: Type not supported: 0x%x", 1869 __FUNCTION__, 1870 trigger.getTagType()); 1871 return INVALID_OPERATION; 1872 } 1873 1874 if (res != OK) { 1875 ALOGE("%s: Failed to update request metadata with trigger tag %s" 1876 ", value %d", __FUNCTION__, trigger.getTagName(), 1877 trigger.entryValue); 1878 return res; 1879 } 1880 1881 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, 1882 trigger.getTagName(), 1883 trigger.entryValue); 1884 } 1885 1886 mTriggerMap.clear(); 1887 1888 return count; 1889} 1890 1891status_t Camera3Device::RequestThread::removeTriggers( 1892 const sp<CaptureRequest> &request) { 1893 Mutex::Autolock al(mTriggerMutex); 1894 1895 CameraMetadata &metadata = request->mSettings; 1896 1897 /** 1898 * Replace all old entries with their old values. 1899 */ 1900 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { 1901 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); 1902 1903 status_t res; 1904 1905 uint32_t tag = trigger.metadataTag; 1906 switch (trigger.getTagType()) { 1907 case TYPE_BYTE: { 1908 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); 1909 res = metadata.update(tag, 1910 &entryValue, 1911 /*count*/1); 1912 break; 1913 } 1914 case TYPE_INT32: 1915 res = metadata.update(tag, 1916 &trigger.entryValue, 1917 /*count*/1); 1918 break; 1919 default: 1920 ALOGE("%s: Type not supported: 0x%x", 1921 __FUNCTION__, 1922 trigger.getTagType()); 1923 return INVALID_OPERATION; 1924 } 1925 1926 if (res != OK) { 1927 ALOGE("%s: Failed to restore request metadata with trigger tag %s" 1928 ", trigger value %d", __FUNCTION__, 1929 trigger.getTagName(), trigger.entryValue); 1930 return res; 1931 } 1932 } 1933 mTriggerReplacedMap.clear(); 1934 1935 /** 1936 * Remove all new entries. 1937 */ 1938 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { 1939 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); 1940 status_t res = metadata.erase(trigger.metadataTag); 1941 1942 if (res != OK) { 1943 ALOGE("%s: Failed to erase metadata with trigger tag %s" 1944 ", trigger value %d", __FUNCTION__, 1945 trigger.getTagName(), trigger.entryValue); 1946 return res; 1947 } 1948 } 1949 mTriggerRemovedMap.clear(); 1950 1951 return OK; 1952} 1953 1954 1955 1956/** 1957 * Static callback forwarding methods from HAL to instance 1958 */ 1959 1960void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, 1961 const camera3_capture_result *result) { 1962 Camera3Device *d = 1963 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 1964 d->processCaptureResult(result); 1965} 1966 1967void Camera3Device::sNotify(const camera3_callback_ops *cb, 1968 const camera3_notify_msg *msg) { 1969 Camera3Device *d = 1970 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); 1971 d->notify(msg); 1972} 1973 1974}; // namespace android 1975