1 /* 2 * Copyright (C) 2013-2018 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 <utility> 21 #include <unordered_map> 22 #include <set> 23 #include <tuple> 24 25 #include <utils/Condition.h> 26 #include <utils/Errors.h> 27 #include <utils/List.h> 28 #include <utils/Mutex.h> 29 #include <utils/Thread.h> 30 #include <utils/KeyedVector.h> 31 #include <utils/Timers.h> 32 33 #include <camera/CaptureResult.h> 34 35 #include "CameraServiceWatchdog.h" 36 #include <aidl/android/hardware/camera/device/CameraBlob.h> 37 38 #include "common/CameraDeviceBase.h" 39 #include "device3/BufferUtils.h" 40 #include "device3/StatusTracker.h" 41 #include "device3/Camera3BufferManager.h" 42 #include "device3/DistortionMapper.h" 43 #include "device3/ZoomRatioMapper.h" 44 #include "device3/RotateAndCropMapper.h" 45 #include "device3/UHRCropAndMeteringRegionMapper.h" 46 #include "device3/InFlightRequest.h" 47 #include "device3/Camera3OutputInterface.h" 48 #include "device3/Camera3OfflineSession.h" 49 #include "device3/Camera3StreamInterface.h" 50 #include "utils/TagMonitor.h" 51 #include "utils/IPCTransport.h" 52 #include "utils/LatencyHistogram.h" 53 #include "utils/CameraServiceProxyWrapper.h" 54 #include <camera_metadata_hidden.h> 55 56 using android::camera3::camera_capture_request_t; 57 using android::camera3::camera_request_template; 58 using android::camera3::camera_stream_buffer_t; 59 using android::camera3::camera_stream_configuration_t; 60 using android::camera3::camera_stream_configuration_mode_t; 61 using android::camera3::CAMERA_TEMPLATE_COUNT; 62 using android::camera3::OutputStreamInfo; 63 64 namespace android { 65 66 namespace camera3 { 67 68 class Camera3Stream; 69 class Camera3ZslStream; 70 class Camera3StreamInterface; 71 72 } // namespace camera3 73 74 /** 75 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 or higher. 76 */ 77 class Camera3Device : 78 public CameraDeviceBase, 79 public camera3::SetErrorInterface, 80 public camera3::InflightRequestUpdateInterface, 81 public camera3::RequestBufferInterface, 82 public camera3::FlushBufferInterface { 83 friend class HidlCamera3Device; 84 friend class AidlCamera3Device; 85 public: 86 87 explicit Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper, 88 const String8& id, bool overrideForPerfClass, bool overrideToPortrait, 89 bool legacyClient = false); 90 91 virtual ~Camera3Device(); 92 // Delete and optionally close native handles and clear the input vector afterward 93 static void cleanupNativeHandles( 94 std::vector<native_handle_t*> *handles, bool closeFd = false); 95 getTransportType()96 virtual IPCTransport getTransportType() const override { 97 return mInterface->getTransportType(); 98 } 99 100 /** 101 * CameraDeviceBase interface 102 */ 103 104 const String8& getId() const override; 105 getVendorTagId()106 metadata_vendor_id_t getVendorTagId() const override { return mVendorTagId; } 107 108 // Watchdog thread 109 sp<CameraServiceWatchdog> mCameraServiceWatchdog; 110 111 // Transitions to idle state on success. 112 virtual status_t initialize(sp<CameraProviderManager> /*manager*/, 113 const String8& /*monitorTags*/) = 0; 114 115 status_t disconnect() override; 116 status_t dump(int fd, const Vector<String16> &args) override; 117 status_t startWatchingTags(const String8 &tags) override; 118 status_t stopWatchingTags() override; 119 status_t dumpWatchedEventsToVector(std::vector<std::string> &out) override; 120 const CameraMetadata& info() const override; 121 const CameraMetadata& infoPhysical(const String8& physicalId) const override; isCompositeJpegRDisabled()122 bool isCompositeJpegRDisabled() const override { return mIsCompositeJpegRDisabled; }; 123 124 // Capture and setStreamingRequest will configure streams if currently in 125 // idle state 126 status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) override; 127 status_t captureList(const List<const PhysicalCameraSettingsList> &requestsList, 128 const std::list<const SurfaceMap> &surfaceMaps, 129 int64_t *lastFrameNumber = NULL) override; 130 status_t setStreamingRequest(const CameraMetadata &request, 131 int64_t *lastFrameNumber = NULL) override; 132 status_t setStreamingRequestList(const List<const PhysicalCameraSettingsList> &requestsList, 133 const std::list<const SurfaceMap> &surfaceMaps, 134 int64_t *lastFrameNumber = NULL) override; 135 status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) override; 136 137 status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) override; 138 139 // Actual stream creation/deletion is delayed until first request is submitted 140 // If adding streams while actively capturing, will pause device before adding 141 // stream, reconfiguring device, and unpausing. If the client create a stream 142 // with nullptr consumer surface, the client must then call setConsumers() 143 // and finish the stream configuration before starting output streaming. 144 status_t createStream(sp<Surface> consumer, 145 uint32_t width, uint32_t height, int format, 146 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id, 147 const String8& physicalCameraId, 148 const std::unordered_set<int32_t> &sensorPixelModesUsed, 149 std::vector<int> *surfaceIds = nullptr, 150 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID, 151 bool isShared = false, bool isMultiResolution = false, 152 uint64_t consumerUsage = 0, 153 int64_t dynamicRangeProfile = 154 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD, 155 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT, 156 int timestampBase = OutputConfiguration::TIMESTAMP_BASE_DEFAULT, 157 int mirrorMode = OutputConfiguration::MIRROR_MODE_AUTO, 158 int32_t colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED, 159 bool useReadoutTimestamp = false) 160 override; 161 162 status_t createStream(const std::vector<sp<Surface>>& consumers, 163 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format, 164 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id, 165 const String8& physicalCameraId, 166 const std::unordered_set<int32_t> &sensorPixelModesUsed, 167 std::vector<int> *surfaceIds = nullptr, 168 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID, 169 bool isShared = false, bool isMultiResolution = false, 170 uint64_t consumerUsage = 0, 171 int64_t dynamicRangeProfile = 172 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD, 173 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT, 174 int timestampBase = OutputConfiguration::TIMESTAMP_BASE_DEFAULT, 175 int mirrorMode = OutputConfiguration::MIRROR_MODE_AUTO, 176 int32_t colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED, 177 bool useReadoutTimestamp = false) 178 override; 179 180 status_t createInputStream( 181 uint32_t width, uint32_t height, int format, bool isMultiResolution, 182 int *id) override; 183 184 status_t getStreamInfo(int id, StreamInfo *streamInfo) override; 185 status_t setStreamTransform(int id, int transform) override; 186 187 status_t deleteStream(int id) override; 188 189 status_t configureStreams(const CameraMetadata& sessionParams, 190 int operatingMode = 191 camera_stream_configuration_mode_t::CAMERA_STREAM_CONFIGURATION_NORMAL_MODE) override; 192 status_t getInputBufferProducer( 193 sp<IGraphicBufferProducer> *producer) override; 194 195 void getOfflineStreamIds(std::vector<int> *offlineStreamIds) override; 196 197 status_t createDefaultRequest(camera_request_template_t templateId, 198 CameraMetadata *request) override; 199 200 // Transitions to the idle state on success 201 status_t waitUntilDrained() override; 202 203 status_t setNotifyCallback(wp<NotificationListener> listener) override; 204 bool willNotify3A() override; 205 status_t waitForNextFrame(nsecs_t timeout) override; 206 status_t getNextResult(CaptureResult *frame) override; 207 208 status_t triggerAutofocus(uint32_t id) override; 209 status_t triggerCancelAutofocus(uint32_t id) override; 210 status_t triggerPrecaptureMetering(uint32_t id) override; 211 212 status_t flush(int64_t *lastFrameNumber = NULL) override; 213 214 status_t prepare(int streamId) override; 215 216 status_t tearDown(int streamId) override; 217 218 status_t addBufferListenerForStream(int streamId, 219 wp<camera3::Camera3StreamBufferListener> listener) override; 220 221 status_t prepare(int maxCount, int streamId) override; 222 223 ssize_t getJpegBufferSize(const CameraMetadata &info, uint32_t width, 224 uint32_t height) const override; 225 ssize_t getPointCloudBufferSize(const CameraMetadata &info) const; 226 ssize_t getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width, int32_t height, 227 bool maxResolution) const; 228 229 // Methods called by subclasses 230 void notifyStatus(bool idle); // updates from StatusTracker 231 232 /** 233 * Set the deferred consumer surfaces to the output stream and finish the deferred 234 * consumer configuration. 235 */ 236 status_t setConsumerSurfaces( 237 int streamId, const std::vector<sp<Surface>>& consumers, 238 std::vector<int> *surfaceIds /*out*/) override; 239 240 /** 241 * Update a given stream. 242 */ 243 status_t updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces, 244 const std::vector<OutputStreamInfo> &outputInfo, 245 const std::vector<size_t> &removedSurfaceIds, 246 KeyedVector<sp<Surface>, size_t> *outputMap/*out*/); 247 248 /** 249 * Drop buffers for stream of streamId if dropping is true. If dropping is false, do not 250 * drop buffers for stream of streamId. 251 */ 252 status_t dropStreamBuffers(bool dropping, int streamId) override; 253 254 nsecs_t getExpectedInFlightDuration() override; 255 switchToOffline(const std::vector<int32_t> &,sp<CameraOfflineSessionBase> *)256 virtual status_t switchToOffline(const std::vector<int32_t>& , 257 /*out*/ sp<CameraOfflineSessionBase>* ) override { 258 return INVALID_OPERATION; 259 }; 260 261 // RequestBufferInterface 262 bool startRequestBuffer() override; 263 void endRequestBuffer() override; 264 nsecs_t getWaitDuration() override; 265 266 // FlushBufferInterface 267 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override; 268 void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override; 269 std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override; 270 271 /** 272 * Set the current behavior for the ROTATE_AND_CROP control when in AUTO. 273 * 274 * The value must be one of the ROTATE_AND_CROP_* values besides AUTO, 275 * and defaults to NONE. 276 */ 277 status_t setRotateAndCropAutoBehavior( 278 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue, bool fromHal); 279 280 /** 281 * Set the current behavior for the AUTOFRAMING control when in AUTO. 282 * 283 * The value must be one of the AUTOFRAMING_* values besides AUTO. 284 */ 285 status_t setAutoframingAutoBehavior( 286 camera_metadata_enum_android_control_autoframing_t autoframingValue); 287 288 /** 289 * Whether camera muting (producing black-only output) is supported. 290 * 291 * Calling setCameraMute(true) when this returns false will return an 292 * INVALID_OPERATION error. 293 */ 294 bool supportsCameraMute(); 295 296 /** 297 * Mute the camera. 298 * 299 * When muted, black image data is output on all output streams. 300 */ 301 status_t setCameraMute(bool enabled); 302 303 /** 304 * Enables/disables camera service watchdog 305 */ 306 status_t setCameraServiceWatchdog(bool enabled); 307 308 // Set stream use case overrides 309 void setStreamUseCaseOverrides( 310 const std::vector<int64_t>& useCaseOverrides); 311 312 // Clear stream use case overrides 313 void clearStreamUseCaseOverrides(); 314 315 /** 316 * Whether the camera device supports zoom override. 317 */ 318 bool supportsZoomOverride(); 319 320 // Set/reset zoom override 321 status_t setZoomOverride(int32_t zoomOverride); 322 323 // Get the status trackeer for the camera device getStatusTracker()324 wp<camera3::StatusTracker> getStatusTracker() { return mStatusTracker; } 325 326 // Whether the device is in error state 327 bool hasDeviceError(); 328 329 /** 330 * The injection camera session to replace the internal camera 331 * session. 332 */ 333 status_t injectCamera(const String8& injectedCamId, 334 sp<CameraProviderManager> manager); 335 336 /** 337 * Stop the injection camera and restore to internal camera session. 338 */ 339 status_t stopInjection(); 340 341 protected: 342 status_t disconnectImpl(); 343 static status_t removeFwkOnlyRegionKeys(CameraMetadata *request); 344 345 float getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream); 346 347 static const size_t kDumpLockAttempts = 10; 348 static const size_t kDumpSleepDuration = 100000; // 0.10 sec 349 static const nsecs_t kActiveTimeout = 500000000; // 500 ms 350 static const nsecs_t kMinWarnInflightDuration = 5000000000; // 5 s 351 static const size_t kInFlightWarnLimit = 30; 352 static const size_t kInFlightWarnLimitHighSpeed = 256; // batch size 32 * pipe depth 8 353 static const nsecs_t kMinInflightDuration = 5000000000; // 5 s 354 static const nsecs_t kBaseGetBufferWait = 3000000000; // 3 sec. 355 // SCHED_FIFO priority for request submission thread in HFR mode 356 static const int kRequestThreadPriority = 1; 357 358 struct RequestTrigger; 359 // minimal jpeg buffer size: 256KB + blob header 360 static const ssize_t kMinJpegBufferSize = 361 256 * 1024 + sizeof(aidl::android::hardware::camera::device::CameraBlob); 362 // Constant to use for stream ID when one doesn't exist 363 static const int NO_STREAM = -1; 364 365 std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper; 366 367 // A lock to enforce serialization on the input/configure side 368 // of the public interface. 369 // Only locked by public methods inherited from CameraDeviceBase. 370 // Not locked by methods guarded by mOutputLock, since they may act 371 // concurrently to the input/configure side of the interface. 372 // Must be locked before mLock if both will be locked by a method 373 Mutex mInterfaceLock; 374 375 // The main lock on internal state 376 Mutex mLock; 377 378 // Camera device ID 379 const String8 mId; 380 381 // Legacy camera client flag 382 bool mLegacyClient; 383 384 // Current stream configuration mode; 385 int mOperatingMode; 386 // Current session wide parameters 387 hardware::camera2::impl::CameraMetadataNative mSessionParams; 388 389 // Constant to use for no set operating mode 390 static const int NO_MODE = -1; 391 392 // Flag indicating is the current active stream configuration is constrained high speed. 393 bool mIsConstrainedHighSpeedConfiguration; 394 395 /**** Scope for mLock ****/ 396 397 class HalInterface : public camera3::Camera3StreamBufferFreedListener, 398 public camera3::BufferRecordsInterface { 399 public: HalInterface(bool useHalBufManager,bool supportOfflineProcessing)400 HalInterface(bool useHalBufManager, bool supportOfflineProcessing) : 401 mUseHalBufManager(useHalBufManager), 402 mIsReconfigurationQuerySupported(true), 403 mSupportOfflineProcessing(supportOfflineProcessing) 404 {}; 405 HalInterface(const HalInterface &other); 406 HalInterface(); 407 408 virtual IPCTransport getTransportType() const = 0; 409 410 // Returns true if constructed with a valid device or session, and not yet cleared 411 virtual bool valid() = 0; 412 413 // Reset this HalInterface object (does not call close()) 414 virtual void clear() = 0; 415 416 // Calls into the HAL interface 417 418 // Caller takes ownership of requestTemplate 419 virtual status_t constructDefaultRequestSettings(camera_request_template templateId, 420 /*out*/ camera_metadata_t **requestTemplate) = 0; 421 422 virtual status_t configureStreams(const camera_metadata_t * sessionParams, 423 /*inout*/ camera_stream_configuration_t * config, 424 const std::vector<uint32_t>& bufferSizes, int64_t logId) = 0; 425 426 // The injection camera configures the streams to hal. 427 virtual status_t configureInjectedStreams( 428 const camera_metadata_t* sessionParams, 429 /*inout*/ camera_stream_configuration_t* config, 430 const std::vector<uint32_t>& bufferSizes, 431 const CameraMetadata& cameraCharacteristics) = 0; 432 433 // When the call succeeds, the ownership of acquire fences in requests is transferred to 434 // HalInterface. More specifically, the current implementation will send the fence to 435 // HAL process and close the FD in cameraserver process. When the call fails, the ownership 436 // of the acquire fence still belongs to the caller. 437 virtual status_t processBatchCaptureRequests( 438 std::vector<camera_capture_request_t*>& requests, 439 /*out*/uint32_t* numRequestProcessed) = 0; 440 441 virtual status_t flush() = 0; 442 443 virtual status_t dump(int fd) = 0; 444 445 virtual status_t close() = 0; 446 447 virtual void signalPipelineDrain(const std::vector<int>& streamIds) = 0; 448 449 virtual bool isReconfigurationRequired(CameraMetadata& oldSessionParams, 450 CameraMetadata& newSessionParams) = 0; 451 452 virtual status_t repeatingRequestEnd(uint32_t frameNumber, 453 const std::vector<int32_t> &streamIds) = 0; 454 455 ///////////////////////////////////////////////////////////////////// 456 // Implements BufferRecordsInterface 457 458 std::pair<bool, uint64_t> getBufferId( 459 const buffer_handle_t& buf, int streamId) override; 460 461 uint64_t removeOneBufferCache(int streamId, const native_handle_t* handle) override; 462 463 status_t popInflightBuffer(int32_t frameNumber, int32_t streamId, 464 /*out*/ buffer_handle_t **buffer) override; 465 466 status_t pushInflightRequestBuffer( 467 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) override; 468 469 status_t popInflightRequestBuffer(uint64_t bufferId, 470 /*out*/ buffer_handle_t** buffer, 471 /*optional out*/ int32_t* streamId = nullptr) override; 472 473 ///////////////////////////////////////////////////////////////////// 474 475 // Get a vector of (frameNumber, streamId) pair of currently inflight 476 // buffers 477 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out); 478 479 // Get a vector of bufferId of currently inflight buffers 480 void getInflightRequestBufferKeys(std::vector<uint64_t>* out); 481 482 void onStreamReConfigured(int streamId); 483 484 protected: 485 486 // Return true if the input caches match what we have; otherwise false 487 bool verifyBufferIds(int32_t streamId, std::vector<uint64_t>& inBufIds); 488 489 template <typename OfflineSessionInfoT> verifyBufferCaches(const OfflineSessionInfoT * offlineSessionInfo,camera3::BufferRecords * bufferRecords)490 status_t verifyBufferCaches( 491 const OfflineSessionInfoT *offlineSessionInfo, camera3::BufferRecords *bufferRecords) { 492 // Validate buffer caches 493 std::vector<int32_t> streams; 494 streams.reserve(offlineSessionInfo->offlineStreams.size()); 495 for (auto offlineStream : offlineSessionInfo->offlineStreams) { 496 int32_t id = offlineStream.id; 497 streams.push_back(id); 498 // Verify buffer caches 499 std::vector<uint64_t> bufIds(offlineStream.circulatingBufferIds.begin(), 500 offlineStream.circulatingBufferIds.end()); 501 { 502 // Due to timing it is possible that we may not have any remaining pending 503 // capture requests that can update the caches on Hal side. This can result in 504 // buffer cache mismatch between the service and the Hal and must be accounted 505 // for. 506 std::lock_guard<std::mutex> l(mFreedBuffersLock); 507 for (const auto& it : mFreedBuffers) { 508 if (it.first == id) { 509 ALOGV("%s: stream ID %d buffer id %" PRIu64 " cache removal still " 510 "pending", __FUNCTION__, id, it.second); 511 const auto& cachedEntry = std::find(bufIds.begin(), bufIds.end(), 512 it.second); 513 if (cachedEntry != bufIds.end()) { 514 bufIds.erase(cachedEntry); 515 } else { 516 ALOGE("%s: stream ID %d buffer id %" PRIu64 " cache removal still " 517 "pending however buffer is no longer in the offline stream " 518 "info!", __FUNCTION__, id, it.second); 519 } 520 } 521 } 522 } 523 if (!verifyBufferIds(id, bufIds)) { 524 ALOGE("%s: stream ID %d buffer cache records mismatch!", __FUNCTION__, id); 525 return UNKNOWN_ERROR; 526 } 527 } 528 529 // Move buffer records 530 bufferRecords->takeBufferCaches(mBufferRecords, streams); 531 bufferRecords->takeInflightBufferMap(mBufferRecords); 532 bufferRecords->takeRequestedBufferMap(mBufferRecords); 533 return OK; 534 } 535 536 virtual void onBufferFreed(int streamId, const native_handle_t* handle) override; 537 538 std::mutex mFreedBuffersLock; 539 std::vector<std::pair<int, uint64_t>> mFreedBuffers; 540 541 // Keep track of buffer cache and inflight buffer records 542 camera3::BufferRecords mBufferRecords; 543 544 uint32_t mNextStreamConfigCounter = 1; 545 546 const bool mUseHalBufManager; 547 bool mIsReconfigurationQuerySupported; 548 549 const bool mSupportOfflineProcessing; 550 }; // class HalInterface 551 552 sp<HalInterface> mInterface; 553 554 CameraMetadata mDeviceInfo; 555 bool mSupportNativeZoomRatio; 556 bool mIsCompositeJpegRDisabled; 557 std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap; 558 559 CameraMetadata mRequestTemplateCache[CAMERA_TEMPLATE_COUNT]; 560 561 struct Size { 562 uint32_t width; 563 uint32_t height; widthSize564 explicit Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){} 565 }; 566 567 enum Status { 568 STATUS_ERROR, 569 STATUS_UNINITIALIZED, 570 STATUS_UNCONFIGURED, 571 STATUS_CONFIGURED, 572 STATUS_ACTIVE 573 } mStatus; 574 575 struct StatusInfo { 576 Status status; 577 bool isInternal; // status triggered by internal reconfigureCamera. 578 }; 579 580 bool mStatusIsInternal; 581 582 // Only clear mRecentStatusUpdates, mStatusWaiters from waitUntilStateThenRelock 583 Vector<StatusInfo> mRecentStatusUpdates; 584 int mStatusWaiters; 585 586 Condition mStatusChanged; 587 588 // Tracking cause of fatal errors when in STATUS_ERROR 589 String8 mErrorCause; 590 591 camera3::StreamSet mOutputStreams; 592 sp<camera3::Camera3Stream> mInputStream; 593 bool mIsInputStreamMultiResolution; 594 SessionStatsBuilder mSessionStatsBuilder; 595 // Map from stream group ID to physical cameras backing the stream group 596 std::map<int32_t, std::set<String8>> mGroupIdPhysicalCameraMap; 597 598 int mNextStreamId; 599 bool mNeedConfig; 600 601 int mFakeStreamId; 602 603 // Whether to send state updates upstream 604 // Pause when doing transparent reconfiguration 605 bool mPauseStateNotify; 606 607 // Need to hold on to stream references until configure completes. 608 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams; 609 610 // Whether the HAL will send partial result 611 bool mUsePartialResult; 612 613 // Number of partial results that will be delivered by the HAL. 614 uint32_t mNumPartialResults; 615 616 /**** End scope for mLock ****/ 617 618 bool mDeviceTimeBaseIsRealtime; 619 // The offset converting from clock domain of other subsystem 620 // (video/hardware composer) to that of camera. Assumption is that this 621 // offset won't change during the life cycle of the camera device. In other 622 // words, camera device shouldn't be open during CPU suspend. 623 nsecs_t mTimestampOffset; 624 625 class CaptureRequest : public LightRefBase<CaptureRequest> { 626 public: 627 PhysicalCameraSettingsList mSettingsList; 628 sp<camera3::Camera3Stream> mInputStream; 629 camera_stream_buffer_t mInputBuffer; 630 camera3::Size mInputBufferSize; 631 Vector<sp<camera3::Camera3OutputStreamInterface> > 632 mOutputStreams; 633 SurfaceMap mOutputSurfaces; 634 CaptureResultExtras mResultExtras; 635 // The number of requests that should be submitted to HAL at a time. 636 // For example, if batch size is 8, this request and the following 7 637 // requests will be submitted to HAL at a time. The batch size for 638 // the following 7 requests will be ignored by the request thread. 639 int mBatchSize; 640 // Whether this request is from a repeating or repeating burst. 641 bool mRepeating; 642 // Whether this request has ROTATE_AND_CROP_AUTO set, so needs both 643 // overriding of ROTATE_AND_CROP value and adjustment of coordinates 644 // in several other controls in both the request and the result 645 bool mRotateAndCropAuto; 646 // Indicates that the ROTATE_AND_CROP value within 'mSettingsList' was modified 647 // irrespective of the original value. 648 bool mRotateAndCropChanged = false; 649 // Whether this request has AUTOFRAMING_AUTO set, so need to override the AUTOFRAMING value 650 // in the capture request. 651 bool mAutoframingAuto; 652 // Indicates that the auto framing value within 'mSettingsList' was modified 653 bool mAutoframingChanged = false; 654 655 // Whether this capture request has its zoom ratio set to 1.0x before 656 // the framework overrides it for camera HAL consumption. 657 bool mZoomRatioIs1x; 658 // The systemTime timestamp when the request is created. 659 nsecs_t mRequestTimeNs; 660 661 // Whether this capture request's distortion correction update has 662 // been done. 663 bool mDistortionCorrectionUpdated = false; 664 // Whether this capture request's rotation and crop update has been 665 // done. 666 bool mRotationAndCropUpdated = false; 667 // Whether this capture request's autoframing has been done. 668 bool mAutoframingUpdated = false; 669 // Whether this capture request's zoom ratio update has been done. 670 bool mZoomRatioUpdated = false; 671 // Whether this max resolution capture request's crop / metering region update has been 672 // done. 673 bool mUHRCropAndMeteringRegionsUpdated = false; 674 }; 675 typedef List<sp<CaptureRequest> > RequestList; 676 677 status_t checkStatusOkToCaptureLocked(); 678 679 status_t convertMetadataListToRequestListLocked( 680 const List<const PhysicalCameraSettingsList> &metadataList, 681 const std::list<const SurfaceMap> &surfaceMaps, 682 bool repeating, nsecs_t requestTimeNs, 683 /*out*/ 684 RequestList *requestList); 685 686 void convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList, 687 std::list<const SurfaceMap>& surfaceMaps, 688 const CameraMetadata& request); 689 690 status_t submitRequestsHelper(const List<const PhysicalCameraSettingsList> &requestsList, 691 const std::list<const SurfaceMap> &surfaceMaps, 692 bool repeating, 693 int64_t *lastFrameNumber = NULL); 694 695 // lock to ensure only one processCaptureResult is called at a time. 696 Mutex mProcessCaptureResultLock; 697 698 /** 699 * Common initialization code shared by both HAL paths 700 * 701 * Must be called with mLock and mInterfaceLock held. 702 */ 703 status_t initializeCommonLocked(); 704 705 /** 706 * Get the last request submitted to the hal by the request thread. 707 * 708 * Must be called with mLock held. 709 */ 710 virtual CameraMetadata getLatestRequestLocked(); 711 712 virtual status_t injectionCameraInitialize(const String8 &injectCamId, 713 sp<CameraProviderManager> manager) = 0; 714 715 /** 716 * Update the current device status and wake all waiting threads. 717 * 718 * Must be called with mLock held. 719 */ 720 void internalUpdateStatusLocked(Status status); 721 722 /** 723 * Pause processing and flush everything, but don't tell the clients. 724 * This is for reconfiguring outputs transparently when according to the 725 * CameraDeviceBase interface we shouldn't need to. 726 * Must be called with mLock and mInterfaceLock both held. 727 */ 728 status_t internalPauseAndWaitLocked(nsecs_t maxExpectedDuration, 729 bool requestThreadInvocation); 730 731 /** 732 * Resume work after internalPauseAndWaitLocked() 733 * Must be called with mLock and mInterfaceLock both held. 734 */ 735 status_t internalResumeLocked(); 736 737 /** 738 * Wait until status tracker tells us we've transitioned to the target state 739 * set, which is either ACTIVE when active==true or IDLE (which is any 740 * non-ACTIVE state) when active==false. 741 * 742 * Needs to be called with mLock and mInterfaceLock held. This means there 743 * can ever only be one waiter at most. 744 * 745 * During the wait mLock is released. 746 * 747 */ 748 status_t waitUntilStateThenRelock(bool active, nsecs_t timeout, 749 bool requestThreadInvocation); 750 751 /** 752 * Implementation of waitUntilDrained. On success, will transition to IDLE state. 753 * 754 * Need to be called with mLock and mInterfaceLock held. 755 */ 756 status_t waitUntilDrainedLocked(nsecs_t maxExpectedDuration); 757 758 /** 759 * Do common work for setting up a streaming or single capture request. 760 * On success, will transition to ACTIVE if in IDLE. 761 */ 762 sp<CaptureRequest> setUpRequestLocked(const PhysicalCameraSettingsList &request, 763 const SurfaceMap &surfaceMap); 764 765 /** 766 * Build a CaptureRequest request from the CameraDeviceBase request 767 * settings. 768 */ 769 sp<CaptureRequest> createCaptureRequest(const PhysicalCameraSettingsList &request, 770 const SurfaceMap &surfaceMap); 771 772 /** 773 * Internally re-configure camera device using new session parameters. 774 * This will get triggered by the request thread. 775 */ 776 bool reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId); 777 778 /** 779 * Return true in case of any output or input abandoned streams, 780 * otherwise return false. 781 */ 782 bool checkAbandonedStreamsLocked(); 783 784 /** 785 * Filter stream session parameters and configure camera HAL. 786 */ 787 status_t filterParamsAndConfigureLocked(const CameraMetadata& sessionParams, 788 int operatingMode); 789 790 /** 791 * Take the currently-defined set of streams and configure the HAL to use 792 * them. This is a long-running operation (may be several hundered ms). 793 */ 794 status_t configureStreamsLocked(int operatingMode, 795 const CameraMetadata& sessionParams, bool notifyRequestThread = true); 796 797 /** 798 * Cancel stream configuration that did not finish successfully. 799 */ 800 void cancelStreamsConfigurationLocked(); 801 802 /** 803 * Add a fake stream to the current stream set as a workaround for 804 * not allowing 0 streams in the camera HAL spec. 805 */ 806 status_t addFakeStreamLocked(); 807 808 /** 809 * Remove a fake stream if the current config includes real streams. 810 */ 811 status_t tryRemoveFakeStreamLocked(); 812 813 /** 814 * Set device into an error state due to some fatal failure, and set an 815 * error message to indicate why. Only the first call's message will be 816 * used. The message is also sent to the log. 817 */ 818 void setErrorState(const char *fmt, ...) override; 819 void setErrorStateLocked(const char *fmt, ...) override; 820 void setErrorStateV(const char *fmt, va_list args); 821 void setErrorStateLockedV(const char *fmt, va_list args); 822 823 ///////////////////////////////////////////////////////////////////// 824 // Implements InflightRequestUpdateInterface 825 826 void onInflightEntryRemovedLocked(nsecs_t duration) override; 827 void checkInflightMapLengthLocked() override; 828 void onInflightMapFlushedLocked() override; 829 830 ///////////////////////////////////////////////////////////////////// 831 832 /** 833 * Debugging trylock/spin method 834 * Try to acquire a lock a few times with sleeps between before giving up. 835 */ 836 bool tryLockSpinRightRound(Mutex& lock); 837 838 /** 839 * Helper function to get the offset between MONOTONIC and BOOTTIME 840 * timestamp. 841 */ 842 static nsecs_t getMonoToBoottimeOffset(); 843 844 // Override rotate_and_crop control if needed 845 static bool overrideAutoRotateAndCrop(const sp<CaptureRequest> &request /*out*/, 846 bool overrideToPortrait, 847 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride); 848 849 // Override auto framing control if needed 850 static bool overrideAutoframing(const sp<CaptureRequest> &request /*out*/, 851 camera_metadata_enum_android_control_autoframing_t autoframingOverride); 852 853 struct RequestTrigger { 854 // Metadata tag number, e.g. android.control.aePrecaptureTrigger 855 uint32_t metadataTag; 856 // Metadata value, e.g. 'START' or the trigger ID 857 int32_t entryValue; 858 859 // The last part of the fully qualified path, e.g. afTrigger getTagNameRequestTrigger860 const char *getTagName() const { 861 return get_camera_metadata_tag_name(metadataTag) ?: "NULL"; 862 } 863 864 // e.g. TYPE_BYTE, TYPE_INT32, etc. getTagTypeRequestTrigger865 int getTagType() const { 866 return get_camera_metadata_tag_type(metadataTag); 867 } 868 }; 869 870 /** 871 * Thread for managing capture request submission to HAL device. 872 */ 873 class RequestThread : public Thread { 874 875 public: 876 877 RequestThread(wp<Camera3Device> parent, 878 sp<camera3::StatusTracker> statusTracker, 879 sp<HalInterface> interface, 880 const Vector<int32_t>& sessionParamKeys, 881 bool useHalBufManager, 882 bool supportCameraMute, 883 bool overrideToPortrait, 884 bool supportSettingsOverride); 885 ~RequestThread(); 886 887 void setNotificationListener(wp<NotificationListener> listener); 888 889 /** 890 * Call after stream (re)-configuration is completed. 891 */ 892 void configurationComplete(bool isConstrainedHighSpeed, 893 const CameraMetadata& sessionParams, 894 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap); 895 896 /** 897 * Set or clear the list of repeating requests. Does not block 898 * on either. Use waitUntilPaused to wait until request queue 899 * has emptied out. 900 */ 901 status_t setRepeatingRequests(const RequestList& requests, 902 /*out*/ 903 int64_t *lastFrameNumber = NULL); 904 status_t clearRepeatingRequests(/*out*/ 905 int64_t *lastFrameNumber = NULL); 906 907 status_t queueRequestList(List<sp<CaptureRequest> > &requests, 908 /*out*/ 909 int64_t *lastFrameNumber = NULL); 910 911 /** 912 * Remove all queued and repeating requests, and pending triggers 913 */ 914 status_t clear(/*out*/int64_t *lastFrameNumber = NULL); 915 916 /** 917 * Flush all pending requests in HAL. 918 */ 919 status_t flush(); 920 921 /** 922 * Queue a trigger to be dispatched with the next outgoing 923 * process_capture_request. The settings for that request only 924 * will be temporarily rewritten to add the trigger tag/value. 925 * Subsequent requests will not be rewritten (for this tag). 926 */ 927 status_t queueTrigger(RequestTrigger trigger[], size_t count); 928 929 /** 930 * Pause/unpause the capture thread. Doesn't block, so use 931 * waitUntilPaused to wait until the thread is paused. 932 */ 933 void setPaused(bool paused); 934 935 // set mRequestClearing - no new requests are expected to be queued to RequestThread 936 void setRequestClearing(); 937 938 /** 939 * Wait until thread processes the capture request with settings' 940 * android.request.id == requestId. 941 * 942 * Returns TIMED_OUT in case the thread does not process the request 943 * within the timeout. 944 */ 945 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout); 946 947 /** 948 * Shut down the thread. Shutdown is asynchronous, so thread may 949 * still be running once this method returns. 950 */ 951 virtual void requestExit(); 952 953 /** 954 * Get the latest request that was sent to the HAL 955 * with process_capture_request. 956 */ 957 CameraMetadata getLatestRequest() const; 958 959 /** 960 * Returns true if the stream is a target of any queued or repeating 961 * capture request 962 */ 963 bool isStreamPending(sp<camera3::Camera3StreamInterface>& stream); 964 965 /** 966 * Returns true if the surface is a target of any queued or repeating 967 * capture request 968 */ 969 bool isOutputSurfacePending(int streamId, size_t surfaceId); 970 971 // dump processCaptureRequest latency dumpCaptureRequestLatency(int fd,const char * name)972 void dumpCaptureRequestLatency(int fd, const char* name) { 973 mRequestLatency.dump(fd, name); 974 } 975 976 void signalPipelineDrain(const std::vector<int>& streamIds); 977 void resetPipelineDrain(); 978 979 void clearPreviousRequest(); 980 981 status_t setRotateAndCropAutoBehavior( 982 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue); 983 984 status_t setAutoframingAutoBehaviour( 985 camera_metadata_enum_android_control_autoframing_t autoframingValue); 986 987 status_t setComposerSurface(bool composerSurfacePresent); 988 989 status_t setCameraMute(int32_t muteMode); 990 991 status_t setZoomOverride(int32_t zoomOverride); 992 993 status_t setHalInterface(sp<HalInterface> newHalInterface); 994 995 protected: 996 997 virtual bool threadLoop(); 998 999 static const String8& getId(const wp<Camera3Device> &device); 1000 1001 status_t queueTriggerLocked(RequestTrigger trigger); 1002 // Mix-in queued triggers into this request 1003 int32_t insertTriggers(const sp<CaptureRequest> &request); 1004 // Purge the queued triggers from this request, 1005 // restoring the old field values for those tags. 1006 status_t removeTriggers(const sp<CaptureRequest> &request); 1007 1008 // HAL workaround: Make sure a trigger ID always exists if 1009 // a trigger does 1010 status_t addFakeTriggerIds(const sp<CaptureRequest> &request); 1011 1012 // Override rotate_and_crop control if needed; returns true if the current value was changed 1013 bool overrideAutoRotateAndCrop(const sp<CaptureRequest> &request /*out*/); 1014 1015 // Override autoframing control if needed; returns true if the current value was changed 1016 bool overrideAutoframing(const sp<CaptureRequest> &request); 1017 1018 // Override test_pattern control if needed for camera mute; returns true 1019 // if the current value was changed 1020 bool overrideTestPattern(const sp<CaptureRequest> &request); 1021 1022 // Override settings override if needed for lower zoom latency; return 1023 // true if the current value was changed 1024 bool overrideSettingsOverride(const sp<CaptureRequest> &request); 1025 1026 static const nsecs_t kRequestTimeout = 50e6; // 50 ms 1027 1028 // TODO: does this need to be adjusted for long exposure requests? 1029 static const nsecs_t kRequestSubmitTimeout = 200e6; // 200 ms 1030 1031 // Used to prepare a batch of requests. 1032 struct NextRequest { 1033 sp<CaptureRequest> captureRequest; 1034 camera_capture_request_t halRequest; 1035 Vector<camera_stream_buffer_t> outputBuffers; 1036 bool submitted; 1037 }; 1038 1039 // Wait for the next batch of requests and put them in mNextRequests. mNextRequests will 1040 // be empty if it times out. 1041 void waitForNextRequestBatch(); 1042 1043 // Waits for a request, or returns NULL if times out. Must be called with mRequestLock hold. 1044 sp<CaptureRequest> waitForNextRequestLocked(); 1045 1046 // Prepare HAL requests and output buffers in mNextRequests. Return TIMED_OUT if getting any 1047 // output buffer timed out. If an error is returned, the caller should clean up the pending 1048 // request batch. 1049 status_t prepareHalRequests(); 1050 1051 // Return buffers, etc, for requests in mNextRequests that couldn't be fully constructed and 1052 // send request errors if sendRequestError is true. The buffers will be returned in the 1053 // ERROR state to mark them as not having valid data. mNextRequests will be cleared. 1054 void cleanUpFailedRequests(bool sendRequestError); 1055 1056 // Stop the repeating request if any of its output streams is abandoned. 1057 void checkAndStopRepeatingRequest(); 1058 1059 // Release physical camera settings and camera id resources. 1060 void cleanupPhysicalSettings(sp<CaptureRequest> request, 1061 /*out*/camera_capture_request_t *halRequest); 1062 1063 // Pause handling 1064 bool waitIfPaused(); 1065 void unpauseForNewRequests(); 1066 1067 // Relay error to parent device object setErrorState 1068 void setErrorState(const char *fmt, ...); 1069 1070 // If the input request is in mRepeatingRequests. Must be called with mRequestLock hold 1071 bool isRepeatingRequestLocked(const sp<CaptureRequest>&); 1072 1073 // Clear repeating requests. Must be called with mRequestLock held. 1074 status_t clearRepeatingRequestsLocked(/*out*/ int64_t *lastFrameNumber = NULL); 1075 1076 // send request in mNextRequests to HAL in a batch. Return true = sucssess 1077 bool sendRequestsBatch(); 1078 1079 // Calculate the expected (minimum, maximum, isFixedFps) duration info for a request 1080 struct ExpectedDurationInfo { 1081 nsecs_t minDuration; 1082 nsecs_t maxDuration; 1083 bool isFixedFps; 1084 }; 1085 ExpectedDurationInfo calculateExpectedDurationRange( 1086 const camera_metadata_t *request); 1087 1088 // Check and update latest session parameters based on the current request settings. 1089 bool updateSessionParameters(const CameraMetadata& settings); 1090 1091 // Check whether FPS range session parameter re-configuration is needed in constrained 1092 // high speed recording camera sessions. 1093 bool skipHFRTargetFPSUpdate(int32_t tag, const camera_metadata_ro_entry_t& newEntry, 1094 const camera_metadata_entry_t& currentEntry); 1095 1096 // Update next request sent to HAL 1097 void updateNextRequest(NextRequest& nextRequest); 1098 1099 wp<Camera3Device> mParent; 1100 wp<camera3::StatusTracker> mStatusTracker; 1101 sp<HalInterface> mInterface; 1102 1103 wp<NotificationListener> mListener; 1104 1105 const String8 mId; // The camera ID 1106 int mStatusId; // The RequestThread's component ID for 1107 // status tracking 1108 1109 Mutex mRequestLock; 1110 Condition mRequestSignal; 1111 bool mRequestClearing; 1112 1113 Condition mRequestSubmittedSignal; 1114 RequestList mRequestQueue; 1115 RequestList mRepeatingRequests; 1116 bool mFirstRepeating; 1117 // The next batch of requests being prepped for submission to the HAL, no longer 1118 // on the request queue. Read-only even with mRequestLock held, outside 1119 // of threadLoop 1120 Vector<NextRequest> mNextRequests; 1121 1122 // To protect flush() and sending a request batch to HAL. 1123 Mutex mFlushLock; 1124 1125 bool mReconfigured; 1126 1127 // Used by waitIfPaused, waitForNextRequest, waitUntilPaused, and signalPipelineDrain 1128 Mutex mPauseLock; 1129 bool mDoPause; 1130 Condition mDoPauseSignal; 1131 bool mPaused; 1132 bool mNotifyPipelineDrain; 1133 std::vector<int> mStreamIdsToBeDrained; 1134 1135 sp<CaptureRequest> mPrevRequest; 1136 int32_t mPrevTriggers; 1137 std::set<std::string> mPrevCameraIdsWithZoom; 1138 1139 uint32_t mFrameNumber; 1140 1141 mutable Mutex mLatestRequestMutex; 1142 Condition mLatestRequestSignal; 1143 // android.request.id for latest process_capture_request 1144 int32_t mLatestRequestId; 1145 int32_t mLatestFailedRequestId; 1146 CameraMetadata mLatestRequest; 1147 std::unordered_map<std::string, CameraMetadata> mLatestPhysicalRequest; 1148 1149 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap; 1150 Mutex mTriggerMutex; 1151 TriggerMap mTriggerMap; 1152 TriggerMap mTriggerRemovedMap; 1153 TriggerMap mTriggerReplacedMap; 1154 uint32_t mCurrentAfTriggerId; 1155 uint32_t mCurrentPreCaptureTriggerId; 1156 camera_metadata_enum_android_scaler_rotate_and_crop_t mRotateAndCropOverride; 1157 camera_metadata_enum_android_control_autoframing_t mAutoframingOverride; 1158 bool mComposerOutput; 1159 int32_t mCameraMute; // 0 = no mute, otherwise the TEST_PATTERN_MODE to use 1160 bool mCameraMuteChanged; 1161 int32_t mSettingsOverride; // -1 = use original, otherwise 1162 // the settings override to use. 1163 1164 int64_t mRepeatingLastFrameNumber; 1165 1166 // Flag indicating if we should prepare video stream for video requests. 1167 bool mPrepareVideoStream; 1168 1169 bool mConstrainedMode; 1170 1171 static const int32_t kRequestLatencyBinSize = 40; // in ms 1172 CameraLatencyHistogram mRequestLatency; 1173 1174 Vector<int32_t> mSessionParamKeys; 1175 CameraMetadata mLatestSessionParams; 1176 1177 std::map<int32_t, std::set<String8>> mGroupIdPhysicalCameraMap; 1178 1179 const bool mUseHalBufManager; 1180 const bool mSupportCameraMute; 1181 const bool mOverrideToPortrait; 1182 const bool mSupportSettingsOverride; 1183 int32_t mVndkVersion = -1; 1184 }; 1185 1186 virtual sp<RequestThread> createNewRequestThread(wp<Camera3Device> /*parent*/, 1187 sp<camera3::StatusTracker> /*statusTracker*/, 1188 sp<HalInterface> /*interface*/, 1189 const Vector<int32_t>& /*sessionParamKeys*/, 1190 bool /*useHalBufManager*/, 1191 bool /*supportCameraMute*/, 1192 bool /*overrideToPortrait*/, 1193 bool /*supportSettingsOverride*/) = 0; 1194 1195 sp<RequestThread> mRequestThread; 1196 1197 /** 1198 * In-flight queue for tracking completion of capture requests. 1199 */ 1200 std::mutex mInFlightLock; 1201 camera3::InFlightRequestMap mInFlightMap; 1202 nsecs_t mExpectedInflightDuration = 0; 1203 int64_t mLastCompletedRegularFrameNumber = -1; 1204 int64_t mLastCompletedReprocessFrameNumber = -1; 1205 int64_t mLastCompletedZslFrameNumber = -1; 1206 // End of mInFlightLock protection scope 1207 1208 int mInFlightStatusId; // const after initialize 1209 1210 status_t registerInFlight(uint32_t frameNumber, 1211 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput, 1212 bool callback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration, 1213 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds, 1214 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto, 1215 const std::set<std::string>& cameraIdsWithZoom, const SurfaceMap& outputSurfaces, 1216 nsecs_t requestTimeNs); 1217 1218 /** 1219 * Tracking for idle detection 1220 */ 1221 sp<camera3::StatusTracker> mStatusTracker; 1222 1223 /** 1224 * Graphic buffer manager for output streams. Each device has a buffer manager, which is used 1225 * by the output streams to get and return buffers if these streams are registered to this 1226 * buffer manager. 1227 */ 1228 sp<camera3::Camera3BufferManager> mBufferManager; 1229 1230 /** 1231 * Thread for preparing streams 1232 */ 1233 class PreparerThread : private Thread, public virtual RefBase { 1234 public: 1235 PreparerThread(); 1236 ~PreparerThread(); 1237 1238 void setNotificationListener(wp<NotificationListener> listener); 1239 1240 /** 1241 * Queue up a stream to be prepared. Streams are processed by a background thread in FIFO 1242 * order. Pre-allocate up to maxCount buffers for the stream, or the maximum number needed 1243 * for the pipeline if maxCount is ALLOCATE_PIPELINE_MAX. 1244 */ 1245 status_t prepare(int maxCount, sp<camera3::Camera3StreamInterface>& stream); 1246 1247 /** 1248 * Cancel all current and pending stream preparation 1249 */ 1250 status_t clear(); 1251 1252 /** 1253 * Pause all preparation activities 1254 */ 1255 void pause(); 1256 1257 /** 1258 * Resume preparation activities 1259 */ 1260 status_t resume(); 1261 1262 private: 1263 Mutex mLock; 1264 Condition mThreadActiveSignal; 1265 1266 virtual bool threadLoop(); 1267 1268 // Guarded by mLock 1269 1270 wp<NotificationListener> mListener; 1271 std::list<std::tuple<int, sp<camera3::Camera3StreamInterface>>> mPendingStreams; 1272 bool mActive; 1273 bool mCancelNow; 1274 1275 // Only accessed by threadLoop and the destructor 1276 1277 sp<camera3::Camera3StreamInterface> mCurrentStream; 1278 int mCurrentMaxCount; 1279 bool mCurrentPrepareComplete; 1280 }; 1281 sp<PreparerThread> mPreparerThread; 1282 1283 /** 1284 * Output result queue and current HAL device 3A state 1285 */ 1286 1287 // Lock for output side of device 1288 std::mutex mOutputLock; 1289 1290 /**** Scope for mOutputLock ****/ 1291 // the minimal frame number of the next non-reprocess result 1292 uint32_t mNextResultFrameNumber; 1293 // the minimal frame number of the next reprocess result 1294 uint32_t mNextReprocessResultFrameNumber; 1295 // the minimal frame number of the next ZSL still capture result 1296 uint32_t mNextZslStillResultFrameNumber; 1297 // the minimal frame number of the next non-reprocess shutter 1298 uint32_t mNextShutterFrameNumber; 1299 // the minimal frame number of the next reprocess shutter 1300 uint32_t mNextReprocessShutterFrameNumber; 1301 // the minimal frame number of the next ZSL still capture shutter 1302 uint32_t mNextZslStillShutterFrameNumber; 1303 std::list<CaptureResult> mResultQueue; 1304 std::condition_variable mResultSignal; 1305 wp<NotificationListener> mListener; 1306 1307 /**** End scope for mOutputLock ****/ 1308 1309 /**** Scope for mInFlightLock ****/ 1310 1311 // Remove the in-flight map entry of the given index from mInFlightMap. 1312 // It must only be called with mInFlightLock held. 1313 void removeInFlightMapEntryLocked(int idx); 1314 1315 // Remove all in-flight requests and return all buffers. 1316 // This is used after HAL interface is closed to cleanup any request/buffers 1317 // not returned by HAL. 1318 void flushInflightRequests(); 1319 1320 /**** End scope for mInFlightLock ****/ 1321 1322 /** 1323 * Distortion correction support 1324 */ 1325 // Map from camera IDs to its corresponding distortion mapper. Only contains 1326 // 1 ID if the device isn't a logical multi-camera. Otherwise contains both 1327 // logical camera and its physical subcameras. 1328 std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers; 1329 1330 /** 1331 * Zoom ratio mapper support 1332 */ 1333 std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers; 1334 1335 /** 1336 * UHR request crop / metering region mapper support 1337 */ 1338 std::unordered_map<std::string, camera3::UHRCropAndMeteringRegionMapper> 1339 mUHRCropAndMeteringRegionMappers; 1340 1341 /** 1342 * RotateAndCrop mapper support 1343 */ 1344 std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers; 1345 1346 // Debug tracker for metadata tag value changes 1347 // - Enabled with the -m <taglist> option to dumpsys, such as 1348 // dumpsys -m android.control.aeState,android.control.aeMode 1349 // - Disabled with -m off 1350 // - dumpsys -m 3a is a shortcut for ae/af/awbMode, State, and Triggers 1351 TagMonitor mTagMonitor; 1352 1353 void monitorMetadata(TagMonitor::eventSource source, int64_t frameNumber, 1354 nsecs_t timestamp, const CameraMetadata& metadata, 1355 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata, 1356 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers, 1357 int32_t inputStreamId); 1358 1359 metadata_vendor_id_t mVendorTagId; 1360 1361 // Cached last requested template id 1362 int mLastTemplateId; 1363 1364 // Synchronizes access to status tracker between inflight updates and disconnect. 1365 // b/79972865 1366 Mutex mTrackerLock; 1367 1368 // Whether HAL request buffers through requestStreamBuffers API 1369 bool mUseHalBufManager = false; 1370 1371 // Lock to ensure requestStreamBuffers() callbacks are serialized 1372 std::mutex mRequestBufferInterfaceLock; 1373 1374 // The state machine to control when requestStreamBuffers should allow 1375 // HAL to request buffers. 1376 enum RequestBufferState { 1377 /** 1378 * This is the initial state. 1379 * requestStreamBuffers call will return FAILED_CONFIGURING in this state. 1380 * Will switch to RB_STATUS_READY after a successful configureStreams or 1381 * processCaptureRequest call. 1382 */ 1383 RB_STATUS_STOPPED, 1384 1385 /** 1386 * requestStreamBuffers call will proceed in this state. 1387 * When device is asked to stay idle via waitUntilStateThenRelock() call: 1388 * - Switch to RB_STATUS_STOPPED if there is no inflight requests and 1389 * request thread is paused. 1390 * - Switch to RB_STATUS_PENDING_STOP otherwise 1391 */ 1392 RB_STATUS_READY, 1393 1394 /** 1395 * requestStreamBuffers call will proceed in this state. 1396 * Switch to RB_STATUS_STOPPED when all inflight requests are fulfilled 1397 * and request thread is paused 1398 */ 1399 RB_STATUS_PENDING_STOP, 1400 }; 1401 1402 class RequestBufferStateMachine { 1403 public: 1404 status_t initialize(sp<camera3::StatusTracker> statusTracker); 1405 1406 // Return if the state machine currently allows for requestBuffers 1407 // If the state allows for it, mRequestBufferOngoing will be set to true 1408 // and caller must call endRequestBuffer() later to unset the flag 1409 bool startRequestBuffer(); 1410 void endRequestBuffer(); 1411 1412 // Events triggered by application API call 1413 void onStreamsConfigured(); 1414 void onWaitUntilIdle(); 1415 1416 // Events usually triggered by hwBinder processCaptureResult callback thread 1417 // But can also be triggered on request thread for failed request, or on 1418 // hwbinder notify callback thread for shutter/error callbacks 1419 void onInflightMapEmpty(); 1420 1421 // Events triggered by RequestThread 1422 void onSubmittingRequest(); 1423 void onRequestThreadPaused(); 1424 1425 // Events triggered by successful switchToOffline call 1426 // Return true is there is no ongoing requestBuffer call. 1427 bool onSwitchToOfflineSuccess(); 1428 1429 private: 1430 void notifyTrackerLocked(bool active); 1431 1432 // Switch to STOPPED state and return true if all conditions allows for it. 1433 // Otherwise do nothing and return false. 1434 bool checkSwitchToStopLocked(); 1435 1436 std::mutex mLock; 1437 RequestBufferState mStatus = RB_STATUS_STOPPED; 1438 1439 bool mRequestThreadPaused = true; 1440 bool mInflightMapEmpty = true; 1441 bool mRequestBufferOngoing = false; 1442 bool mSwitchedToOffline = false; 1443 1444 wp<camera3::StatusTracker> mStatusTracker; 1445 int mRequestBufferStatusId; 1446 } mRequestBufferSM; 1447 1448 // Fix up result metadata for monochrome camera. 1449 bool mNeedFixupMonochromeTags; 1450 1451 // Whether HAL supports offline processing capability. 1452 bool mSupportOfflineProcessing = false; 1453 1454 // Whether the HAL supports camera muting via test pattern 1455 bool mSupportCameraMute = false; 1456 // Whether the HAL supports SOLID_COLOR or BLACK if mSupportCameraMute is true 1457 bool mSupportTestPatternSolidColor = false; 1458 // Whether the HAL supports zoom settings override 1459 bool mSupportZoomOverride = false; 1460 1461 // Whether the camera framework overrides the device characteristics for 1462 // performance class. 1463 bool mOverrideForPerfClass; 1464 1465 // Whether the camera framework overrides the device characteristics for 1466 // app compatibility reasons. 1467 bool mOverrideToPortrait; 1468 camera_metadata_enum_android_scaler_rotate_and_crop_t mRotateAndCropOverride; 1469 bool mComposerOutput; 1470 1471 // Auto framing override value 1472 camera_metadata_enum_android_control_autoframing mAutoframingOverride; 1473 1474 // Settings override value 1475 int32_t mSettingsOverride; // -1 = use original, otherwise 1476 // the settings override to use. 1477 1478 // Current active physical id of the logical multi-camera, if any 1479 std::string mActivePhysicalId; 1480 1481 // The current minimum expected frame duration based on AE_TARGET_FPS_RANGE 1482 nsecs_t mMinExpectedDuration = 0; 1483 // Whether the camera device runs at fixed frame rate based on AE_MODE and 1484 // AE_TARGET_FPS_RANGE 1485 bool mIsFixedFps = false; 1486 1487 // Injection camera related methods. 1488 class Camera3DeviceInjectionMethods : public virtual RefBase { 1489 public: 1490 Camera3DeviceInjectionMethods(wp<Camera3Device> parent); 1491 1492 ~Camera3DeviceInjectionMethods(); 1493 1494 // Injection camera will replace the internal camera and configure streams 1495 // when device is IDLE and request thread is paused. 1496 status_t injectCamera( 1497 camera3::camera_stream_configuration& injectionConfig, 1498 const std::vector<uint32_t>& injectionBufferSizes); 1499 1500 // Stop the injection camera and switch back to backup hal interface. 1501 status_t stopInjection(); 1502 1503 bool isInjecting(); 1504 1505 bool isStreamConfigCompleteButNotInjected(); 1506 1507 const String8& getInjectedCamId() const; 1508 1509 void getInjectionConfig(/*out*/ camera3::camera_stream_configuration* injectionConfig, 1510 /*out*/ std::vector<uint32_t>* injectionBufferSizes); 1511 1512 // When the streaming configuration is completed and the camera device is active, but the 1513 // injection camera has not yet been injected, the streaming configuration of the internal 1514 // camera will be stored first. 1515 void storeInjectionConfig( 1516 const camera3::camera_stream_configuration& injectionConfig, 1517 const std::vector<uint32_t>& injectionBufferSizes); 1518 1519 protected: 1520 // Configure the streams of injection camera, it need wait until the 1521 // output streams are created and configured to the original camera before 1522 // proceeding. 1523 status_t injectionConfigureStreams( 1524 camera3::camera_stream_configuration& injectionConfig, 1525 const std::vector<uint32_t>& injectionBufferSizes); 1526 1527 // Disconnect the injection camera and delete the hal interface. 1528 void injectionDisconnectImpl(); 1529 1530 // Use injection camera hal interface to replace and backup original 1531 // camera hal interface. 1532 virtual status_t replaceHalInterface(sp<HalInterface> /*newHalInterface*/, 1533 bool /*keepBackup*/) = 0; 1534 1535 wp<Camera3Device> mParent; 1536 1537 // Backup of the original camera hal interface. 1538 sp<HalInterface> mBackupHalInterface; 1539 1540 // Generated injection camera hal interface. 1541 sp<HalInterface> mInjectedCamHalInterface; 1542 1543 // The flag indicates that the stream configuration is complete, the camera device is 1544 // active, but the injection camera has not yet been injected. 1545 bool mIsStreamConfigCompleteButNotInjected = false; 1546 1547 // Copy the configuration of the internal camera. 1548 camera3::camera_stream_configuration mInjectionConfig; 1549 1550 // Copy the streams of the internal camera. 1551 Vector<camera3::camera_stream_t*> mInjectionStreams; 1552 1553 // Copy the bufferSizes of the output streams of the internal camera. 1554 std::vector<uint32_t> mInjectionBufferSizes; 1555 1556 // Synchronizes access to injection camera between initialize and 1557 // disconnect. 1558 Mutex mInjectionLock; 1559 1560 // The injection camera ID. 1561 String8 mInjectedCamId; 1562 }; 1563 1564 virtual sp<Camera3DeviceInjectionMethods> 1565 createCamera3DeviceInjectionMethods(wp<Camera3Device>) = 0; 1566 1567 sp<Camera3DeviceInjectionMethods> mInjectionMethods; 1568 1569 void overrideStreamUseCaseLocked(); 1570 1571 }; // class Camera3Device 1572 1573 }; // namespace android 1574 1575 #endif 1576