1 /* 2 * Copyright (C) 2022 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 HARDWARE_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_DEVICE_SESSION_H_ 18 #define HARDWARE_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_DEVICE_SESSION_H_ 19 20 #include <aidl/android/hardware/camera/device/BnCameraDeviceSession.h> 21 #include <aidl/android/hardware/camera/device/ICameraDevice.h> 22 #include <aidl/android/hardware/camera/device/ICameraDeviceCallback.h> 23 #include <aidl/android/hardware/thermal/IThermal.h> 24 #include <android-base/thread_annotations.h> 25 #include <fmq/AidlMessageQueue.h> 26 #include <utils/StrongPointer.h> 27 28 #include <shared_mutex> 29 #include <vector> 30 31 #include "aidl_profiler.h" 32 #include "camera_device_session.h" 33 #include "hal_types.h" 34 35 namespace android { 36 namespace hardware { 37 namespace camera { 38 namespace device { 39 namespace implementation { 40 41 // AidlCameraDeviceSession implements the AIDL camera device session interface, 42 // ICameraDeviceSession, that contains the methods to configure and request 43 // captures from an active camera device. 44 class AidlCameraDeviceSession 45 : public aidl::android::hardware::camera::device::BnCameraDeviceSession { 46 public: 47 // Create a AidlCameraDeviceSession. 48 // device_session is a google camera device session that 49 // AidlCameraDeviceSession is going to manage. Creating a 50 // AidlCameraDeviceSession will fail if device_session is 51 // nullptr. 52 static std::shared_ptr<AidlCameraDeviceSession> Create( 53 const std::shared_ptr< 54 aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback, 55 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session, 56 std::shared_ptr<android::google_camera_hal::AidlProfiler> aidl_profiler); 57 58 virtual ~AidlCameraDeviceSession(); 59 60 // functions in ICameraDeviceSession 61 62 ndk::ScopedAStatus close() override; 63 64 ndk::ScopedAStatus configureStreams( 65 const aidl::android::hardware::camera::device::StreamConfiguration&, 66 std::vector<aidl::android::hardware::camera::device::HalStream>*) override; 67 68 ndk::ScopedAStatus constructDefaultRequestSettings( 69 aidl::android::hardware::camera::device::RequestTemplate in_type, 70 aidl::android::hardware::camera::device::CameraMetadata* aidl_return) 71 override; 72 73 ndk::ScopedAStatus flush() override; 74 75 ndk::ScopedAStatus getCaptureRequestMetadataQueue( 76 aidl::android::hardware::common::fmq::MQDescriptor< 77 int8_t, aidl::android::hardware::common::fmq::SynchronizedReadWrite>* 78 aidl_return) override; 79 80 ndk::ScopedAStatus getCaptureResultMetadataQueue( 81 aidl::android::hardware::common::fmq::MQDescriptor< 82 int8_t, aidl::android::hardware::common::fmq::SynchronizedReadWrite>* 83 aidl_return) override; 84 85 ndk::ScopedAStatus isReconfigurationRequired( 86 const aidl::android::hardware::camera::device::CameraMetadata& 87 in_oldSessionParams, 88 const aidl::android::hardware::camera::device::CameraMetadata& 89 in_newSessionParams, 90 bool* aidl_return) override; 91 92 ndk::ScopedAStatus processCaptureRequest( 93 const std::vector<aidl::android::hardware::camera::device::CaptureRequest>& 94 in_requests, 95 const std::vector<aidl::android::hardware::camera::device::BufferCache>& 96 in_cachesToRemove, 97 int32_t* aidl_return) override; 98 99 ndk::ScopedAStatus signalStreamFlush(const std::vector<int32_t>& in_streamIds, 100 int32_t in_streamConfigCounter) override; 101 102 ndk::ScopedAStatus switchToOffline( 103 const std::vector<int32_t>& in_streamsToKeep, 104 aidl::android::hardware::camera::device::CameraOfflineSessionInfo* 105 out_offlineSessionInfo, 106 std::shared_ptr< 107 aidl::android::hardware::camera::device::ICameraOfflineSession>* 108 aidl_return) override; 109 repeatingRequestEnd(int32_t,const std::vector<int32_t> &)110 ndk::ScopedAStatus repeatingRequestEnd( 111 int32_t /*in_frameNumber*/, 112 const std::vector<int32_t>& /*in_streamIds*/) override { 113 return ndk::ScopedAStatus::ok(); 114 }; 115 116 ndk::ScopedAStatus configureStreamsV2( 117 const aidl::android::hardware::camera::device::StreamConfiguration&, 118 aidl::android::hardware::camera::device::ConfigureStreamsRet*) override; 119 AidlCameraDeviceSession() = default; 120 121 protected: 122 ndk::SpAIBinder createBinder() override; 123 124 private: 125 using MetadataQueue = AidlMessageQueue< 126 int8_t, aidl::android::hardware::common::fmq::SynchronizedReadWrite>; 127 128 static constexpr uint32_t kRequestMetadataQueueSizeBytes = 1 << 20; // 1MB 129 static constexpr uint32_t kResultMetadataQueueSizeBytes = 1 << 20; // 1MB 130 131 // Initialize the latest available gralloc buffer mapper. 132 status_t InitializeBufferMapper(); 133 134 // Initialize AidlCameraDeviceSession with a CameraDeviceSession. 135 status_t Initialize( 136 const std::shared_ptr< 137 aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback, 138 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session, 139 std::shared_ptr<android::google_camera_hal::AidlProfiler> aidl_profiler); 140 141 // Create a metadata queue. 142 // If override_size_property contains a valid size, it will create a metadata 143 // queue of that size. If it override_size_property doesn't contain a valid 144 // size, it will create a metadata queue of the default size. 145 // default_size_bytes is the default size of the message queue in bytes. 146 // override_size_property is the name of the system property that contains 147 // the message queue size. 148 status_t CreateMetadataQueue(std::unique_ptr<MetadataQueue>* metadata_queue, 149 uint32_t default_size_bytes, 150 const char* override_size_property); 151 152 // Invoked when receiving a result from HAL. 153 void ProcessCaptureResult( 154 std::unique_ptr<google_camera_hal::CaptureResult> hal_result); 155 156 // Invoked when receiving a batched result from HAL. 157 void ProcessBatchCaptureResult( 158 std::vector<std::unique_ptr<google_camera_hal::CaptureResult>> hal_results); 159 160 // TODO b/311263114: Remove this method once the feature flag is enabled. 161 // This is needed since the framework has the feature support flagged. The HAL 162 // should not switch HAL buffer on / off is the framework doesn't support them 163 // (flag is off). Since aconfig flags are not shared between the framework and 164 // the HAL - the HAL can know about framework support through knowing whether 165 // configureStreamsV2 was called or not. 166 ndk::ScopedAStatus configureStreamsImpl( 167 const aidl::android::hardware::camera::device::StreamConfiguration&, 168 bool v2, aidl::android::hardware::camera::device::ConfigureStreamsRet*); 169 // Invoked when receiving a message from HAL. 170 void NotifyHalMessage(const google_camera_hal::NotifyMessage& hal_message); 171 172 // Invoked when requesting stream buffers from HAL. 173 google_camera_hal::BufferRequestStatus RequestStreamBuffers( 174 const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests, 175 std::vector<google_camera_hal::BufferReturn>* hal_buffer_returns); 176 177 // Invoked when returning stream buffers from HAL. 178 void ReturnStreamBuffers( 179 const std::vector<google_camera_hal::StreamBuffer>& return_hal_buffers); 180 181 // Set camera device session callbacks. 182 void SetSessionCallbacks(); 183 184 // Register a thermal changed callback. 185 // notify_throttling will be invoked when thermal status changes. 186 // If filter_type is false, type will be ignored and all types will be 187 // monitored. 188 // If filter_type is true, only type will be monitored. 189 status_t RegisterThermalChangedCallback( 190 google_camera_hal::NotifyThrottlingFunc notify_throttling, 191 bool filter_type, google_camera_hal::TemperatureType type); 192 193 // Unregister thermal changed callback. 194 void UnregisterThermalChangedCallback(); 195 196 // Log when the first frame buffers are all received. 197 void TryLogFirstFrameDone(const google_camera_hal::CaptureResult& result, 198 const char* caller_func_name); 199 200 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session_; 201 202 // Metadata queue to read the request metadata from. 203 std::unique_ptr<MetadataQueue> request_metadata_queue_; 204 205 // Metadata queue to write the result metadata to. 206 std::unique_ptr<MetadataQueue> result_metadata_queue_; 207 208 // Assuming callbacks to framework is thread-safe, the shared mutex is only 209 // used to protect member variable writing and reading. 210 std::shared_mutex aidl_device_callback_lock_; 211 // Protected by aidl_device_callback_lock_ 212 std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceCallback> 213 aidl_device_callback_; 214 215 std::mutex aidl_thermal_mutex_; 216 std::shared_ptr<aidl::android::hardware::thermal::IThermal> thermal_; 217 218 // Must be protected by hidl_thermal_mutex_. 219 std::shared_ptr<aidl::android::hardware::thermal::IThermalChangedCallback> 220 thermal_changed_callback_ GUARDED_BY(aidl_thermal_mutex_); 221 222 // Flag for profiling first frame processing time. 223 bool first_frame_requested_ = false; 224 225 // The frame number of first capture request after configure stream 226 uint32_t first_request_frame_number_ = 0; 227 228 std::mutex pending_first_frame_buffers_mutex_; 229 // Profiling first frame process time. Stop timer when it become 0. 230 // Must be protected by pending_first_frame_buffers_mutex_ 231 size_t num_pending_first_frame_buffers_ = 0; 232 233 std::shared_ptr<android::google_camera_hal::AidlProfiler> aidl_profiler_; 234 235 // The ID of preview stream. 236 int32_t preview_stream_id_ = -1; 237 238 // The timestamp of last preview image actually sent by HAL. 239 uint32_t preview_timestamp_last_ = 0; 240 }; 241 242 } // namespace implementation 243 } // namespace device 244 } // namespace camera 245 } // namespace hardware 246 } // namespace android 247 248 #endif // HARDWARE_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_DEVICE_SESSION_H_ 249