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 #ifndef ANDROID_SERVERS_CAMERA3DEVICE_H 18 #define ANDROID_SERVERS_CAMERA3DEVICE_H 19 20 #include <utils/Condition.h> 21 #include <utils/Errors.h> 22 #include <utils/List.h> 23 #include <utils/Mutex.h> 24 #include <utils/Thread.h> 25 #include <utils/KeyedVector.h> 26 #include <utils/Timers.h> 27 #include <hardware/camera3.h> 28 #include <camera/CaptureResult.h> 29 30 #include "common/CameraDeviceBase.h" 31 #include "device3/StatusTracker.h" 32 #include "device3/Camera3BufferManager.h" 33 34 /** 35 * Function pointer types with C calling convention to 36 * use for HAL callback functions. 37 */ 38 extern "C" { 39 typedef void (callbacks_process_capture_result_t)( 40 const struct camera3_callback_ops *, 41 const camera3_capture_result_t *); 42 43 typedef void (callbacks_notify_t)( 44 const struct camera3_callback_ops *, 45 const camera3_notify_msg_t *); 46 } 47 48 namespace android { 49 50 namespace camera3 { 51 52 class Camera3Stream; 53 class Camera3ZslStream; 54 class Camera3OutputStreamInterface; 55 class Camera3StreamInterface; 56 57 } 58 59 /** 60 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 or higher. 61 */ 62 class Camera3Device : 63 public CameraDeviceBase, 64 private camera3_callback_ops { 65 public: 66 67 Camera3Device(int id); 68 69 virtual ~Camera3Device(); 70 71 /** 72 * CameraDeviceBase interface 73 */ 74 75 virtual int getId() const; 76 77 // Transitions to idle state on success. 78 virtual status_t initialize(CameraModule *module); 79 virtual status_t disconnect(); 80 virtual status_t dump(int fd, const Vector<String16> &args); 81 virtual const CameraMetadata& info() const; 82 83 // Capture and setStreamingRequest will configure streams if currently in 84 // idle state 85 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL); 86 virtual status_t captureList(const List<const CameraMetadata> &requests, 87 int64_t *lastFrameNumber = NULL); 88 virtual status_t setStreamingRequest(const CameraMetadata &request, 89 int64_t *lastFrameNumber = NULL); 90 virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests, 91 int64_t *lastFrameNumber = NULL); 92 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL); 93 94 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout); 95 96 // Actual stream creation/deletion is delayed until first request is submitted 97 // If adding streams while actively capturing, will pause device before adding 98 // stream, reconfiguring device, and unpausing. 99 virtual status_t createStream(sp<Surface> consumer, 100 uint32_t width, uint32_t height, int format, 101 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id, 102 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID); 103 virtual status_t createInputStream( 104 uint32_t width, uint32_t height, int format, 105 int *id); 106 virtual status_t createZslStream( 107 uint32_t width, uint32_t height, 108 int depth, 109 /*out*/ 110 int *id, 111 sp<camera3::Camera3ZslStream>* zslStream); 112 virtual status_t createReprocessStreamFromStream(int outputId, int *id); 113 114 virtual status_t getStreamInfo(int id, 115 uint32_t *width, uint32_t *height, 116 uint32_t *format, android_dataspace *dataSpace); 117 virtual status_t setStreamTransform(int id, int transform); 118 119 virtual status_t deleteStream(int id); 120 virtual status_t deleteReprocessStream(int id); 121 122 virtual status_t configureStreams(bool isConstraiedHighSpeed = false); 123 virtual status_t getInputBufferProducer( 124 sp<IGraphicBufferProducer> *producer); 125 126 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request); 127 128 // Transitions to the idle state on success 129 virtual status_t waitUntilDrained(); 130 131 virtual status_t setNotifyCallback(NotificationListener *listener); 132 virtual bool willNotify3A(); 133 virtual status_t waitForNextFrame(nsecs_t timeout); 134 virtual status_t getNextResult(CaptureResult *frame); 135 136 virtual status_t triggerAutofocus(uint32_t id); 137 virtual status_t triggerCancelAutofocus(uint32_t id); 138 virtual status_t triggerPrecaptureMetering(uint32_t id); 139 140 virtual status_t pushReprocessBuffer(int reprocessStreamId, 141 buffer_handle_t *buffer, wp<BufferReleasedListener> listener); 142 143 virtual status_t flush(int64_t *lastFrameNumber = NULL); 144 145 virtual status_t prepare(int streamId); 146 147 virtual status_t tearDown(int streamId); 148 149 virtual status_t addBufferListenerForStream(int streamId, 150 wp<camera3::Camera3StreamBufferListener> listener); 151 152 virtual status_t prepare(int maxCount, int streamId); 153 154 virtual uint32_t getDeviceVersion(); 155 156 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const; 157 ssize_t getPointCloudBufferSize() const; 158 ssize_t getRawOpaqueBufferSize(int32_t width, int32_t height) const; 159 160 // Methods called by subclasses 161 void notifyStatus(bool idle); // updates from StatusTracker 162 163 private: 164 static const size_t kDumpLockAttempts = 10; 165 static const size_t kDumpSleepDuration = 100000; // 0.10 sec 166 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec 167 static const nsecs_t kActiveTimeout = 500000000; // 500 ms 168 static const size_t kInFlightWarnLimit = 20; 169 static const size_t kInFlightWarnLimitHighSpeed = 256; // batch size 32 * pipe depth 8 170 // SCHED_FIFO priority for request submission thread in HFR mode 171 static const int kConstrainedHighSpeedThreadPriority = 1; 172 173 struct RequestTrigger; 174 // minimal jpeg buffer size: 256KB + blob header 175 static const ssize_t kMinJpegBufferSize = 256 * 1024 + sizeof(camera3_jpeg_blob); 176 // Constant to use for stream ID when one doesn't exist 177 static const int NO_STREAM = -1; 178 179 // A lock to enforce serialization on the input/configure side 180 // of the public interface. 181 // Only locked by public methods inherited from CameraDeviceBase. 182 // Not locked by methods guarded by mOutputLock, since they may act 183 // concurrently to the input/configure side of the interface. 184 // Must be locked before mLock if both will be locked by a method 185 Mutex mInterfaceLock; 186 187 // The main lock on internal state 188 Mutex mLock; 189 190 // Camera device ID 191 const int mId; 192 193 // Flag indicating is the current active stream configuration is constrained high speed. 194 bool mIsConstrainedHighSpeedConfiguration; 195 196 /**** Scope for mLock ****/ 197 198 camera3_device_t *mHal3Device; 199 200 CameraMetadata mDeviceInfo; 201 202 CameraMetadata mRequestTemplateCache[CAMERA3_TEMPLATE_COUNT]; 203 204 uint32_t mDeviceVersion; 205 206 // whether Camera3Device should derive ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST for 207 // backward compatibility. Should not be changed after initialization. 208 bool mDerivePostRawSensKey = false; 209 210 struct Size { 211 uint32_t width; 212 uint32_t height; widthSize213 Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){} 214 }; 215 // Map from format to size. 216 Vector<Size> mSupportedOpaqueInputSizes; 217 218 enum Status { 219 STATUS_ERROR, 220 STATUS_UNINITIALIZED, 221 STATUS_UNCONFIGURED, 222 STATUS_CONFIGURED, 223 STATUS_ACTIVE 224 } mStatus; 225 226 // Only clear mRecentStatusUpdates, mStatusWaiters from waitUntilStateThenRelock 227 Vector<Status> mRecentStatusUpdates; 228 int mStatusWaiters; 229 230 Condition mStatusChanged; 231 232 // Tracking cause of fatal errors when in STATUS_ERROR 233 String8 mErrorCause; 234 235 // Mapping of stream IDs to stream instances 236 typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> > 237 StreamSet; 238 239 StreamSet mOutputStreams; 240 sp<camera3::Camera3Stream> mInputStream; 241 int mNextStreamId; 242 bool mNeedConfig; 243 244 int mDummyStreamId; 245 246 // Whether to send state updates upstream 247 // Pause when doing transparent reconfiguration 248 bool mPauseStateNotify; 249 250 // Need to hold on to stream references until configure completes. 251 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams; 252 253 // Whether the HAL will send partial result 254 bool mUsePartialResult; 255 256 // Number of partial results that will be delivered by the HAL. 257 uint32_t mNumPartialResults; 258 259 /**** End scope for mLock ****/ 260 261 // The offset converting from clock domain of other subsystem 262 // (video/hardware composer) to that of camera. Assumption is that this 263 // offset won't change during the life cycle of the camera device. In other 264 // words, camera device shouldn't be open during CPU suspend. 265 nsecs_t mTimestampOffset; 266 267 typedef struct AeTriggerCancelOverride { 268 bool applyAeLock; 269 uint8_t aeLock; 270 bool applyAePrecaptureTrigger; 271 uint8_t aePrecaptureTrigger; 272 } AeTriggerCancelOverride_t; 273 274 class CaptureRequest : public LightRefBase<CaptureRequest> { 275 public: 276 CameraMetadata mSettings; 277 sp<camera3::Camera3Stream> mInputStream; 278 camera3_stream_buffer_t mInputBuffer; 279 Vector<sp<camera3::Camera3OutputStreamInterface> > 280 mOutputStreams; 281 CaptureResultExtras mResultExtras; 282 // Used to cancel AE precapture trigger for devices doesn't support 283 // CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL 284 AeTriggerCancelOverride_t mAeTriggerCancelOverride; 285 // The number of requests that should be submitted to HAL at a time. 286 // For example, if batch size is 8, this request and the following 7 287 // requests will be submitted to HAL at a time. The batch size for 288 // the following 7 requests will be ignored by the request thread. 289 int mBatchSize; 290 }; 291 typedef List<sp<CaptureRequest> > RequestList; 292 293 status_t checkStatusOkToCaptureLocked(); 294 295 status_t convertMetadataListToRequestListLocked( 296 const List<const CameraMetadata> &metadataList, 297 /*out*/ 298 RequestList *requestList); 299 300 status_t submitRequestsHelper(const List<const CameraMetadata> &requests, bool repeating, 301 int64_t *lastFrameNumber = NULL); 302 303 /** 304 * Get the last request submitted to the hal by the request thread. 305 * 306 * Takes mLock. 307 */ 308 virtual CameraMetadata getLatestRequestLocked(); 309 310 /** 311 * Update the current device status and wake all waiting threads. 312 * 313 * Must be called with mLock held. 314 */ 315 void internalUpdateStatusLocked(Status status); 316 317 /** 318 * Pause processing and flush everything, but don't tell the clients. 319 * This is for reconfiguring outputs transparently when according to the 320 * CameraDeviceBase interface we shouldn't need to. 321 * Must be called with mLock and mInterfaceLock both held. 322 */ 323 status_t internalPauseAndWaitLocked(); 324 325 /** 326 * Resume work after internalPauseAndWaitLocked() 327 * Must be called with mLock and mInterfaceLock both held. 328 */ 329 status_t internalResumeLocked(); 330 331 /** 332 * Wait until status tracker tells us we've transitioned to the target state 333 * set, which is either ACTIVE when active==true or IDLE (which is any 334 * non-ACTIVE state) when active==false. 335 * 336 * Needs to be called with mLock and mInterfaceLock held. This means there 337 * can ever only be one waiter at most. 338 * 339 * During the wait mLock is released. 340 * 341 */ 342 status_t waitUntilStateThenRelock(bool active, nsecs_t timeout); 343 344 /** 345 * Implementation of waitUntilDrained. On success, will transition to IDLE state. 346 * 347 * Need to be called with mLock and mInterfaceLock held. 348 */ 349 status_t waitUntilDrainedLocked(); 350 351 /** 352 * Do common work for setting up a streaming or single capture request. 353 * On success, will transition to ACTIVE if in IDLE. 354 */ 355 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request); 356 357 /** 358 * Build a CaptureRequest request from the CameraDeviceBase request 359 * settings. 360 */ 361 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request); 362 363 /** 364 * Take the currently-defined set of streams and configure the HAL to use 365 * them. This is a long-running operation (may be several hundered ms). 366 */ 367 status_t configureStreamsLocked(); 368 369 /** 370 * Cancel stream configuration that did not finish successfully. 371 */ 372 void cancelStreamsConfigurationLocked(); 373 374 /** 375 * Add a dummy stream to the current stream set as a workaround for 376 * not allowing 0 streams in the camera HAL spec. 377 */ 378 status_t addDummyStreamLocked(); 379 380 /** 381 * Remove a dummy stream if the current config includes real streams. 382 */ 383 status_t tryRemoveDummyStreamLocked(); 384 385 /** 386 * Set device into an error state due to some fatal failure, and set an 387 * error message to indicate why. Only the first call's message will be 388 * used. The message is also sent to the log. 389 */ 390 void setErrorState(const char *fmt, ...); 391 void setErrorStateV(const char *fmt, va_list args); 392 void setErrorStateLocked(const char *fmt, ...); 393 void setErrorStateLockedV(const char *fmt, va_list args); 394 395 /** 396 * Debugging trylock/spin method 397 * Try to acquire a lock a few times with sleeps between before giving up. 398 */ 399 bool tryLockSpinRightRound(Mutex& lock); 400 401 /** 402 * Helper function to determine if an input size for implementation defined 403 * format is supported. 404 */ 405 bool isOpaqueInputSizeSupported(uint32_t width, uint32_t height); 406 407 /** 408 * Helper function to get the largest Jpeg resolution (in area) 409 * Return Size(0, 0) if static metatdata is invalid 410 */ 411 Size getMaxJpegResolution() const; 412 413 /** 414 * Helper function to get the offset between MONOTONIC and BOOTTIME 415 * timestamp. 416 */ 417 static nsecs_t getMonoToBoottimeOffset(); 418 419 /** 420 * Helper function to map between legacy and new dataspace enums 421 */ 422 static android_dataspace mapToLegacyDataspace(android_dataspace dataSpace); 423 424 struct RequestTrigger { 425 // Metadata tag number, e.g. android.control.aePrecaptureTrigger 426 uint32_t metadataTag; 427 // Metadata value, e.g. 'START' or the trigger ID 428 int32_t entryValue; 429 430 // The last part of the fully qualified path, e.g. afTrigger getTagNameRequestTrigger431 const char *getTagName() const { 432 return get_camera_metadata_tag_name(metadataTag) ?: "NULL"; 433 } 434 435 // e.g. TYPE_BYTE, TYPE_INT32, etc. getTagTypeRequestTrigger436 int getTagType() const { 437 return get_camera_metadata_tag_type(metadataTag); 438 } 439 }; 440 441 /** 442 * Thread for managing capture request submission to HAL device. 443 */ 444 class RequestThread : public Thread { 445 446 public: 447 448 RequestThread(wp<Camera3Device> parent, 449 sp<camera3::StatusTracker> statusTracker, 450 camera3_device_t *hal3Device, 451 bool aeLockAvailable); 452 453 void setNotificationListener(NotificationListener *listener); 454 455 /** 456 * Call after stream (re)-configuration is completed. 457 */ 458 void configurationComplete(bool isConstrainedHighSpeed); 459 460 /** 461 * Set or clear the list of repeating requests. Does not block 462 * on either. Use waitUntilPaused to wait until request queue 463 * has emptied out. 464 */ 465 status_t setRepeatingRequests(const RequestList& requests, 466 /*out*/ 467 int64_t *lastFrameNumber = NULL); 468 status_t clearRepeatingRequests(/*out*/ 469 int64_t *lastFrameNumber = NULL); 470 471 status_t queueRequestList(List<sp<CaptureRequest> > &requests, 472 /*out*/ 473 int64_t *lastFrameNumber = NULL); 474 475 /** 476 * Remove all queued and repeating requests, and pending triggers 477 */ 478 status_t clear(NotificationListener *listener, 479 /*out*/ 480 int64_t *lastFrameNumber = NULL); 481 482 /** 483 * Flush all pending requests in HAL. 484 */ 485 status_t flush(); 486 487 /** 488 * Queue a trigger to be dispatched with the next outgoing 489 * process_capture_request. The settings for that request only 490 * will be temporarily rewritten to add the trigger tag/value. 491 * Subsequent requests will not be rewritten (for this tag). 492 */ 493 status_t queueTrigger(RequestTrigger trigger[], size_t count); 494 495 /** 496 * Pause/unpause the capture thread. Doesn't block, so use 497 * waitUntilPaused to wait until the thread is paused. 498 */ 499 void setPaused(bool paused); 500 501 /** 502 * Wait until thread processes the capture request with settings' 503 * android.request.id == requestId. 504 * 505 * Returns TIMED_OUT in case the thread does not process the request 506 * within the timeout. 507 */ 508 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout); 509 510 /** 511 * Shut down the thread. Shutdown is asynchronous, so thread may 512 * still be running once this method returns. 513 */ 514 virtual void requestExit(); 515 516 /** 517 * Get the latest request that was sent to the HAL 518 * with process_capture_request. 519 */ 520 CameraMetadata getLatestRequest() const; 521 522 /** 523 * Returns true if the stream is a target of any queued or repeating 524 * capture request 525 */ 526 bool isStreamPending(sp<camera3::Camera3StreamInterface>& stream); 527 528 protected: 529 530 virtual bool threadLoop(); 531 532 private: 533 static int getId(const wp<Camera3Device> &device); 534 535 status_t queueTriggerLocked(RequestTrigger trigger); 536 // Mix-in queued triggers into this request 537 int32_t insertTriggers(const sp<CaptureRequest> &request); 538 // Purge the queued triggers from this request, 539 // restoring the old field values for those tags. 540 status_t removeTriggers(const sp<CaptureRequest> &request); 541 542 // HAL workaround: Make sure a trigger ID always exists if 543 // a trigger does 544 status_t addDummyTriggerIds(const sp<CaptureRequest> &request); 545 546 static const nsecs_t kRequestTimeout = 50e6; // 50 ms 547 548 // Used to prepare a batch of requests. 549 struct NextRequest { 550 sp<CaptureRequest> captureRequest; 551 camera3_capture_request_t halRequest; 552 Vector<camera3_stream_buffer_t> outputBuffers; 553 bool submitted; 554 }; 555 556 // Wait for the next batch of requests and put them in mNextRequests. mNextRequests will 557 // be empty if it times out. 558 void waitForNextRequestBatch(); 559 560 // Waits for a request, or returns NULL if times out. Must be called with mRequestLock hold. 561 sp<CaptureRequest> waitForNextRequestLocked(); 562 563 // Prepare HAL requests and output buffers in mNextRequests. Return TIMED_OUT if getting any 564 // output buffer timed out. If an error is returned, the caller should clean up the pending 565 // request batch. 566 status_t prepareHalRequests(); 567 568 // Return buffers, etc, for requests in mNextRequests that couldn't be fully constructed and 569 // send request errors if sendRequestError is true. The buffers will be returned in the 570 // ERROR state to mark them as not having valid data. mNextRequests will be cleared. 571 void cleanUpFailedRequests(bool sendRequestError); 572 573 // Stop the repeating request if any of its output streams is abandoned. 574 void checkAndStopRepeatingRequest(); 575 576 // Pause handling 577 bool waitIfPaused(); 578 void unpauseForNewRequests(); 579 580 // Relay error to parent device object setErrorState 581 void setErrorState(const char *fmt, ...); 582 583 // If the input request is in mRepeatingRequests. Must be called with mRequestLock hold 584 bool isRepeatingRequestLocked(const sp<CaptureRequest>); 585 586 // Handle AE precapture trigger cancel for devices <= CAMERA_DEVICE_API_VERSION_3_2. 587 void handleAePrecaptureCancelRequest(sp<CaptureRequest> request); 588 589 // Clear repeating requests. Must be called with mRequestLock held. 590 status_t clearRepeatingRequestsLocked(/*out*/ int64_t *lastFrameNumber = NULL); 591 592 wp<Camera3Device> mParent; 593 wp<camera3::StatusTracker> mStatusTracker; 594 camera3_device_t *mHal3Device; 595 596 NotificationListener *mListener; 597 598 const int mId; // The camera ID 599 int mStatusId; // The RequestThread's component ID for 600 // status tracking 601 602 Mutex mRequestLock; 603 Condition mRequestSignal; 604 RequestList mRequestQueue; 605 RequestList mRepeatingRequests; 606 // The next batch of requests being prepped for submission to the HAL, no longer 607 // on the request queue. Read-only even with mRequestLock held, outside 608 // of threadLoop 609 Vector<NextRequest> mNextRequests; 610 611 // To protect flush() and sending a request batch to HAL. 612 Mutex mFlushLock; 613 614 bool mReconfigured; 615 616 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused 617 Mutex mPauseLock; 618 bool mDoPause; 619 Condition mDoPauseSignal; 620 bool mPaused; 621 Condition mPausedSignal; 622 623 sp<CaptureRequest> mPrevRequest; 624 int32_t mPrevTriggers; 625 626 uint32_t mFrameNumber; 627 628 mutable Mutex mLatestRequestMutex; 629 Condition mLatestRequestSignal; 630 // android.request.id for latest process_capture_request 631 int32_t mLatestRequestId; 632 CameraMetadata mLatestRequest; 633 634 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap; 635 Mutex mTriggerMutex; 636 TriggerMap mTriggerMap; 637 TriggerMap mTriggerRemovedMap; 638 TriggerMap mTriggerReplacedMap; 639 uint32_t mCurrentAfTriggerId; 640 uint32_t mCurrentPreCaptureTriggerId; 641 642 int64_t mRepeatingLastFrameNumber; 643 644 // Whether the device supports AE lock 645 bool mAeLockAvailable; 646 647 // Flag indicating if we should prepare video stream for video requests. 648 bool mPrepareVideoStream; 649 }; 650 sp<RequestThread> mRequestThread; 651 652 /** 653 * In-flight queue for tracking completion of capture requests. 654 */ 655 656 struct InFlightRequest { 657 // Set by notify() SHUTTER call. 658 nsecs_t shutterTimestamp; 659 // Set by process_capture_result(). 660 nsecs_t sensorTimestamp; 661 int requestStatus; 662 // Set by process_capture_result call with valid metadata 663 bool haveResultMetadata; 664 // Decremented by calls to process_capture_result with valid output 665 // and input buffers 666 int numBuffersLeft; 667 CaptureResultExtras resultExtras; 668 // If this request has any input buffer 669 bool hasInputBuffer; 670 671 // The last metadata that framework receives from HAL and 672 // not yet send out because the shutter event hasn't arrived. 673 // It's added by process_capture_result and sent when framework 674 // receives the shutter event. 675 CameraMetadata pendingMetadata; 676 677 // The metadata of the partial results that framework receives from HAL so far 678 // and has sent out. 679 CameraMetadata collectedPartialResult; 680 681 // Buffers are added by process_capture_result when output buffers 682 // return from HAL but framework has not yet received the shutter 683 // event. They will be returned to the streams when framework receives 684 // the shutter event. 685 Vector<camera3_stream_buffer_t> pendingOutputBuffers; 686 687 // Used to cancel AE precapture trigger for devices doesn't support 688 // CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL 689 AeTriggerCancelOverride_t aeTriggerCancelOverride; 690 691 // Default constructor needed by KeyedVector InFlightRequestInFlightRequest692 InFlightRequest() : 693 shutterTimestamp(0), 694 sensorTimestamp(0), 695 requestStatus(OK), 696 haveResultMetadata(false), 697 numBuffersLeft(0), 698 hasInputBuffer(false), 699 aeTriggerCancelOverride({false, 0, false, 0}){ 700 } 701 InFlightRequestInFlightRequest702 InFlightRequest(int numBuffers, CaptureResultExtras extras, bool hasInput, 703 AeTriggerCancelOverride aeTriggerCancelOverride) : 704 shutterTimestamp(0), 705 sensorTimestamp(0), 706 requestStatus(OK), 707 haveResultMetadata(false), 708 numBuffersLeft(numBuffers), 709 resultExtras(extras), 710 hasInputBuffer(hasInput), 711 aeTriggerCancelOverride(aeTriggerCancelOverride){ 712 } 713 }; 714 715 // Map from frame number to the in-flight request state 716 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap; 717 718 Mutex mInFlightLock; // Protects mInFlightMap 719 InFlightMap mInFlightMap; 720 721 status_t registerInFlight(uint32_t frameNumber, 722 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput, 723 const AeTriggerCancelOverride_t &aeTriggerCancelOverride); 724 725 /** 726 * Override result metadata for cancelling AE precapture trigger applied in 727 * handleAePrecaptureCancelRequest(). 728 */ 729 void overrideResultForPrecaptureCancel(CameraMetadata* result, 730 const AeTriggerCancelOverride_t &aeTriggerCancelOverride); 731 732 /** 733 * Tracking for idle detection 734 */ 735 sp<camera3::StatusTracker> mStatusTracker; 736 737 /** 738 * Graphic buffer manager for output streams. Each device has a buffer manager, which is used 739 * by the output streams to get and return buffers if these streams are registered to this 740 * buffer manager. 741 */ 742 sp<camera3::Camera3BufferManager> mBufferManager; 743 744 /** 745 * Thread for preparing streams 746 */ 747 class PreparerThread : private Thread, public virtual RefBase { 748 public: 749 PreparerThread(); 750 ~PreparerThread(); 751 752 void setNotificationListener(NotificationListener *listener); 753 754 /** 755 * Queue up a stream to be prepared. Streams are processed by a background thread in FIFO 756 * order. Pre-allocate up to maxCount buffers for the stream, or the maximum number needed 757 * for the pipeline if maxCount is ALLOCATE_PIPELINE_MAX. 758 */ 759 status_t prepare(int maxCount, sp<camera3::Camera3StreamInterface>& stream); 760 761 /** 762 * Cancel all current and pending stream preparation 763 */ 764 status_t clear(); 765 766 private: 767 Mutex mLock; 768 769 virtual bool threadLoop(); 770 771 // Guarded by mLock 772 773 NotificationListener *mListener; 774 List<sp<camera3::Camera3StreamInterface> > mPendingStreams; 775 bool mActive; 776 bool mCancelNow; 777 778 // Only accessed by threadLoop and the destructor 779 780 sp<camera3::Camera3StreamInterface> mCurrentStream; 781 }; 782 sp<PreparerThread> mPreparerThread; 783 784 /** 785 * Output result queue and current HAL device 3A state 786 */ 787 788 // Lock for output side of device 789 Mutex mOutputLock; 790 791 /**** Scope for mOutputLock ****/ 792 // the minimal frame number of the next non-reprocess result 793 uint32_t mNextResultFrameNumber; 794 // the minimal frame number of the next reprocess result 795 uint32_t mNextReprocessResultFrameNumber; 796 // the minimal frame number of the next non-reprocess shutter 797 uint32_t mNextShutterFrameNumber; 798 // the minimal frame number of the next reprocess shutter 799 uint32_t mNextReprocessShutterFrameNumber; 800 List<CaptureResult> mResultQueue; 801 Condition mResultSignal; 802 NotificationListener *mListener; 803 804 /**** End scope for mOutputLock ****/ 805 806 /** 807 * Callback functions from HAL device 808 */ 809 void processCaptureResult(const camera3_capture_result *result); 810 811 void notify(const camera3_notify_msg *msg); 812 813 // Specific notify handlers 814 void notifyError(const camera3_error_msg_t &msg, 815 NotificationListener *listener); 816 void notifyShutter(const camera3_shutter_msg_t &msg, 817 NotificationListener *listener); 818 819 // helper function to return the output buffers to the streams. 820 void returnOutputBuffers(const camera3_stream_buffer_t *outputBuffers, 821 size_t numBuffers, nsecs_t timestamp); 822 823 // Send a partial capture result. 824 void sendPartialCaptureResult(const camera_metadata_t * partialResult, 825 const CaptureResultExtras &resultExtras, uint32_t frameNumber, 826 const AeTriggerCancelOverride_t &aeTriggerCancelOverride); 827 828 // Send a total capture result given the pending metadata and result extras, 829 // partial results, and the frame number to the result queue. 830 void sendCaptureResult(CameraMetadata &pendingMetadata, 831 CaptureResultExtras &resultExtras, 832 CameraMetadata &collectedPartialResult, uint32_t frameNumber, 833 bool reprocess, const AeTriggerCancelOverride_t &aeTriggerCancelOverride); 834 835 // Insert the result to the result queue after updating frame number and overriding AE 836 // trigger cancel. 837 // mOutputLock must be held when calling this function. 838 void insertResultLocked(CaptureResult *result, uint32_t frameNumber, 839 const AeTriggerCancelOverride_t &aeTriggerCancelOverride); 840 841 /**** Scope for mInFlightLock ****/ 842 843 // Remove the in-flight request of the given index from mInFlightMap 844 // if it's no longer needed. It must only be called with mInFlightLock held. 845 void removeInFlightRequestIfReadyLocked(int idx); 846 847 /**** End scope for mInFlightLock ****/ 848 849 /** 850 * Static callback forwarding methods from HAL to instance 851 */ 852 static callbacks_process_capture_result_t sProcessCaptureResult; 853 854 static callbacks_notify_t sNotify; 855 856 }; // class Camera3Device 857 858 }; // namespace android 859 860 #endif 861