/* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_SERVERS_CAMERA3OFFLINESESSION_H #define ANDROID_SERVERS_CAMERA3OFFLINESESSION_H #include #include #include #include #include #include #include "common/CameraOfflineSessionBase.h" #include "device3/Camera3BufferManager.h" #include "device3/DistortionMapper.h" #include "device3/InFlightRequest.h" #include "device3/Camera3OutputUtils.h" #include "device3/RotateAndCropMapper.h" #include "device3/ZoomRatioMapper.h" #include "utils/TagMonitor.h" #include "utils/LatencyHistogram.h" #include namespace android { namespace camera3 { class Camera3Stream; class Camera3OutputStreamInterface; class Camera3StreamInterface; } // namespace camera3 // An immutable struct containing general states that will be copied from Camera3Device to // Camera3OfflineSession struct Camera3OfflineStates { Camera3OfflineStates( const TagMonitor& tagMonitor, const metadata_vendor_id_t vendorTagId, const bool useHalBufManager, const bool needFixupMonochromeTags, const bool usePartialResult, const uint32_t numPartialResults, const int64_t lastCompletedRegularFN, const int64_t lastCompletedReprocessFN, const int64_t lastCompletedZslFN, const uint32_t nextResultFN, const uint32_t nextReprocResultFN, const uint32_t nextZslResultFN, const uint32_t nextShutterFN, const uint32_t nextReprocShutterFN, const uint32_t nextZslShutterFN, const CameraMetadata& deviceInfo, const std::unordered_map& physicalDeviceInfoMap, const std::unordered_map& distortionMappers, const std::unordered_map& zoomRatioMappers, const std::unordered_map& rotateAndCropMappers) : mTagMonitor(tagMonitor), mVendorTagId(vendorTagId), mUseHalBufManager(useHalBufManager), mNeedFixupMonochromeTags(needFixupMonochromeTags), mUsePartialResult(usePartialResult), mNumPartialResults(numPartialResults), mLastCompletedRegularFrameNumber(lastCompletedRegularFN), mLastCompletedReprocessFrameNumber(lastCompletedReprocessFN), mLastCompletedZslFrameNumber(lastCompletedZslFN), mNextResultFrameNumber(nextResultFN), mNextReprocessResultFrameNumber(nextReprocResultFN), mNextZslStillResultFrameNumber(nextZslResultFN), mNextShutterFrameNumber(nextShutterFN), mNextReprocessShutterFrameNumber(nextReprocShutterFN), mNextZslStillShutterFrameNumber(nextZslShutterFN), mDeviceInfo(deviceInfo), mPhysicalDeviceInfoMap(physicalDeviceInfoMap), mDistortionMappers(distortionMappers), mZoomRatioMappers(zoomRatioMappers), mRotateAndCropMappers(rotateAndCropMappers) {} const TagMonitor& mTagMonitor; const metadata_vendor_id_t mVendorTagId; const bool mUseHalBufManager; const bool mNeedFixupMonochromeTags; const bool mUsePartialResult; const uint32_t mNumPartialResults; // The last completed (buffers, result metadata, and error notify) regular // request frame number const int64_t mLastCompletedRegularFrameNumber; // The last completed (buffers, result metadata, and error notify) reprocess // request frame number const int64_t mLastCompletedReprocessFrameNumber; // The last completed (buffers, result metadata, and error notify) zsl // request frame number const int64_t mLastCompletedZslFrameNumber; // the minimal frame number of the next non-reprocess result const uint32_t mNextResultFrameNumber; // the minimal frame number of the next reprocess result const uint32_t mNextReprocessResultFrameNumber; // the minimal frame number of the next ZSL still capture result const uint32_t mNextZslStillResultFrameNumber; // the minimal frame number of the next non-reprocess shutter const uint32_t mNextShutterFrameNumber; // the minimal frame number of the next reprocess shutter const uint32_t mNextReprocessShutterFrameNumber; // the minimal frame number of the next ZSL still capture shutter const uint32_t mNextZslStillShutterFrameNumber; const CameraMetadata& mDeviceInfo; const std::unordered_map& mPhysicalDeviceInfoMap; const std::unordered_map& mDistortionMappers; const std::unordered_map& mZoomRatioMappers; const std::unordered_map& mRotateAndCropMappers; }; /** * Camera3OfflineSession for offline session defined in HIDL ICameraOfflineSession@3.6 or higher */ class Camera3OfflineSession : public CameraOfflineSessionBase, virtual public hardware::camera::device::V3_5::ICameraDeviceCallback, public camera3::SetErrorInterface, public camera3::InflightRequestUpdateInterface, public camera3::RequestBufferInterface, public camera3::FlushBufferInterface { public: // initialize by Camera3Device. explicit Camera3OfflineSession(const String8& id, const sp& inputStream, const camera3::StreamSet& offlineStreamSet, camera3::BufferRecords&& bufferRecords, const camera3::InFlightRequestMap& offlineReqs, const Camera3OfflineStates& offlineStates, sp offlineSession); virtual ~Camera3OfflineSession(); virtual status_t initialize(wp listener) override; /** * CameraOfflineSessionBase interface */ status_t disconnect() override; status_t dump(int fd) override; /** * FrameProducer interface */ const String8& getId() const override; const CameraMetadata& info() const override; status_t waitForNextFrame(nsecs_t timeout) override; status_t getNextResult(CaptureResult *frame) override; // TODO: methods for notification (error/idle/finished etc) passing /** * End of CameraOfflineSessionBase interface */ /** * HIDL ICameraDeviceCallback interface */ /** * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback */ hardware::Return processCaptureResult_3_4( const hardware::hidl_vec< hardware::camera::device::V3_4::CaptureResult>& results) override; hardware::Return processCaptureResult( const hardware::hidl_vec< hardware::camera::device::V3_2::CaptureResult>& results) override; hardware::Return notify( const hardware::hidl_vec< hardware::camera::device::V3_2::NotifyMsg>& msgs) override; hardware::Return requestStreamBuffers( const hardware::hidl_vec< hardware::camera::device::V3_5::BufferRequest>& bufReqs, requestStreamBuffers_cb _hidl_cb) override; hardware::Return returnStreamBuffers( const hardware::hidl_vec< hardware::camera::device::V3_2::StreamBuffer>& buffers) override; /** * End of CameraOfflineSessionBase interface */ private: // Camera device ID const String8 mId; sp mInputStream; camera3::StreamSet mOutputStreams; camera3::BufferRecords mBufferRecords; std::mutex mOfflineReqsLock; camera3::InFlightRequestMap mOfflineReqs; sp mSession; TagMonitor mTagMonitor; const metadata_vendor_id_t mVendorTagId; const bool mUseHalBufManager; const bool mNeedFixupMonochromeTags; const bool mUsePartialResult; const uint32_t mNumPartialResults; std::mutex mOutputLock; std::list mResultQueue; std::condition_variable mResultSignal; // the last completed frame number of regular requests int64_t mLastCompletedRegularFrameNumber; // the last completed frame number of reprocess requests int64_t mLastCompletedReprocessFrameNumber; // the last completed frame number of ZSL still capture requests int64_t mLastCompletedZslFrameNumber; // the minimal frame number of the next non-reprocess result uint32_t mNextResultFrameNumber; // the minimal frame number of the next reprocess result uint32_t mNextReprocessResultFrameNumber; // the minimal frame number of the next ZSL still capture result uint32_t mNextZslStillResultFrameNumber; // the minimal frame number of the next non-reprocess shutter uint32_t mNextShutterFrameNumber; // the minimal frame number of the next reprocess shutter uint32_t mNextReprocessShutterFrameNumber; // the minimal frame number of the next ZSL still capture shutter uint32_t mNextZslStillShutterFrameNumber; // End of mOutputLock scope const CameraMetadata mDeviceInfo; std::unordered_map mPhysicalDeviceInfoMap; std::unordered_map mDistortionMappers; std::unordered_map mZoomRatioMappers; std::unordered_map mRotateAndCropMappers; mutable std::mutex mLock; enum Status { STATUS_UNINITIALIZED = 0, STATUS_ACTIVE, STATUS_ERROR, STATUS_CLOSED } mStatus; wp mListener; // End of mLock protect scope std::mutex mProcessCaptureResultLock; // FMQ to write result on. Must be guarded by mProcessCaptureResultLock. std::unique_ptr mResultMetadataQueue; // Tracking cause of fatal errors when in STATUS_ERROR String8 mErrorCause; // Lock to ensure requestStreamBuffers() callbacks are serialized std::mutex mRequestBufferInterfaceLock; // allow request buffer until all requests are processed or disconnectImpl is called bool mAllowRequestBuffer = true; // For client methods such as disconnect/dump std::mutex mInterfaceLock; // SetErrorInterface void setErrorState(const char *fmt, ...) override; void setErrorStateLocked(const char *fmt, ...) override; // InflightRequestUpdateInterface void onInflightEntryRemovedLocked(nsecs_t duration) override; void checkInflightMapLengthLocked() override; void onInflightMapFlushedLocked() override; // RequestBufferInterface bool startRequestBuffer() override; void endRequestBuffer() override; nsecs_t getWaitDuration() override; // FlushBufferInterface void getInflightBufferKeys(std::vector>* out) override; void getInflightRequestBufferKeys(std::vector* out) override; std::vector> getAllStreams() override; void setErrorStateLockedV(const char *fmt, va_list args); status_t disconnectImpl(); }; // class Camera3OfflineSession }; // namespace android #endif