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