/* * 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 HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_CAMERA_DEVICE_SESSION_H_ #define HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_CAMERA_DEVICE_SESSION_H_ #include #include #include #include #include #include #include "camera_device_session.h" #include "hidl_profiler.h" #include "hidl_thermal_utils.h" namespace android { namespace hardware { namespace camera { namespace device { namespace V3_7 { namespace implementation { using ::android::hardware::camera::common::V1_0::Status; using ::android::hardware::camera::device::V3_2::BufferCache; using ::android::hardware::camera::device::V3_2::RequestTemplate; using ::android::hardware::camera::device::V3_5::ICameraDeviceCallback; using ::android::hardware::camera::device::V3_7::CaptureRequest; using ::android::hardware::camera::device::V3_7::ICameraDeviceSession; using ::android::hardware::camera::device::V3_7::StreamConfiguration; using ::android::hardware::camera::implementation::HidlProfiler; using MetadataQueue = ::android::hardware::MessageQueue; // HidlCameraDeviceSession implements the HIDL camera device session interface, // ICameraDeviceSession, that contains the methods to configure and request // captures from an active camera device. class HidlCameraDeviceSession : public ICameraDeviceSession { public: // Create a HidlCameraDeviceSession. // device_session is a google camera device session that // HidlCameraDeviceSession is going to manage. Creating a // HidlCameraDeviceSession will fail if device_session is // nullptr. static std::unique_ptr Create( const sp& callback, std::unique_ptr device_session, std::shared_ptr hidl_profiler); virtual ~HidlCameraDeviceSession(); // Override functions in ICameraDeviceSession Return configureStreams_3_7( const StreamConfiguration& requestedConfiguration, configureStreams_3_7_cb _hidl_cb) override; Return processCaptureRequest_3_7( const hidl_vec& requests, const hidl_vec& cachesToRemove, processCaptureRequest_3_7_cb _hidl_cb) override; Return configureStreams_3_6( const V3_5::StreamConfiguration& requestedConfiguration, ICameraDeviceSession::configureStreams_3_6_cb _hidl_cb) override; Return switchToOffline(const hidl_vec& streamsToKeep, switchToOffline_cb _hidl_cb) override; Return constructDefaultRequestSettings( RequestTemplate type, ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) override; Return configureStreams_3_5( const V3_5::StreamConfiguration& requestedConfiguration, ICameraDeviceSession::configureStreams_3_5_cb _hidl_cb) override; Return getCaptureRequestMetadataQueue( ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) override; Return getCaptureResultMetadataQueue( ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) override; Return processCaptureRequest_3_4( const hidl_vec& requests, const hidl_vec& cachesToRemove, processCaptureRequest_3_4_cb _hidl_cb) override; Return signalStreamFlush(const hidl_vec& streamIds, uint32_t streamConfigCounter) override; Return isReconfigurationRequired(const V3_2::CameraMetadata& oldSessionParams, const V3_2::CameraMetadata& newSessionParams, ICameraDeviceSession::isReconfigurationRequired_cb _hidl_cb) override; Return flush() override; Return close() override; // Legacy methods Return configureStreams(const V3_2::StreamConfiguration&, configureStreams_cb _hidl_cb) override; Return configureStreams_3_3(const V3_2::StreamConfiguration&, configureStreams_3_3_cb _hidl_cb) override; Return configureStreams_3_4(const V3_4::StreamConfiguration&, configureStreams_3_4_cb _hidl_cb) override; Return processCaptureRequest( const hidl_vec& requests, const hidl_vec& cachesToRemove, processCaptureRequest_cb _hidl_cb) override; // End of override functions in ICameraDeviceSession protected: HidlCameraDeviceSession() = default; private: static constexpr uint32_t kRequestMetadataQueueSizeBytes = 1 << 20; // 1MB static constexpr uint32_t kResultMetadataQueueSizeBytes = 1 << 20; // 1MB // Initialize the latest available gralloc buffer mapper. status_t InitializeBufferMapper(); // Initialize HidlCameraDeviceSession with a CameraDeviceSession. status_t Initialize( const sp& callback, std::unique_ptr device_session, std::shared_ptr hidl_profiler); // Create a metadata queue. // If override_size_property contains a valid size, it will create a metadata // queue of that size. If it override_size_property doesn't contain a valid // size, it will create a metadata queue of the default size. // default_size_bytes is the default size of the message queue in bytes. // override_size_property is the name of the system property that contains // the message queue size. status_t CreateMetadataQueue(std::unique_ptr* metadata_queue, uint32_t default_size_bytes, const char* override_size_property); // Invoked when receiving a result from HAL. void ProcessCaptureResult( std::unique_ptr hal_result); // Invoked when reciving a message from HAL. void NotifyHalMessage(const google_camera_hal::NotifyMessage& hal_message); // Invoked when requesting stream buffers from HAL. google_camera_hal::BufferRequestStatus RequestStreamBuffers( const std::vector& hal_buffer_requests, std::vector* hal_buffer_returns); // Invoked when returning stream buffers from HAL. void ReturnStreamBuffers( const std::vector& return_hal_buffers); // Import a buffer handle. template buffer_handle_t ImportBufferHandle(const sp buffer_mapper_, const hidl_handle& buffer_hidl_handle); // Set camera device session callbacks. void SetSessionCallbacks(); // Register a thermal changed callback. // notify_throttling will be invoked when thermal status changes. // If filter_type is false, type will be ignored and all types will be // monitored. // If filter_type is true, only type will be monitored. status_t RegisterThermalChangedCallback( google_camera_hal::NotifyThrottlingFunc notify_throttling, bool filter_type, google_camera_hal::TemperatureType type); // Unregister thermal changed callback. void UnregisterThermalChangedCallback(); std::unique_ptr device_session_; // Metadata queue to read the request metadata from. std::unique_ptr request_metadata_queue_; // Metadata queue to write the result metadata to. std::unique_ptr result_metadata_queue_; // Assuming callbacks to framework is thread-safe, the shared mutex is only // used to protect member variable writing and reading. std::shared_mutex hidl_device_callback_lock_; // Protected by hidl_device_callback_lock_ sp hidl_device_callback_; sp buffer_mapper_v2_; sp buffer_mapper_v3_; sp buffer_mapper_v4_; std::mutex hidl_thermal_mutex_; sp thermal_; // Must be protected by hidl_thermal_mutex_. sp thermal_changed_callback_; // Flag for profiling first frame processing time. bool first_frame_requested_ = false; // The frame number of first capture request after configure stream uint32_t first_request_frame_number_ = 0; std::mutex pending_first_frame_buffers_mutex_; // Profiling first frame process time. Stop timer when it become 0. // Must be protected by pending_first_frame_buffers_mutex_ size_t num_pending_first_frame_buffers_ = 0; std::shared_ptr hidl_profiler_; }; } // namespace implementation } // namespace V3_7 } // namespace device } // namespace camera } // namespace hardware } // namespace android #endif // HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_CAMERA_DEVICE_SESSION_H_