1 /* 2 * Copyright (C) 2019 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_GOOGLE_CAMERA_HAL_RGBIRD_CAPTURE_SESSION_H_ 18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_CAPTURE_SESSION_H_ 19 20 #include "camera_buffer_allocator_hwl.h" 21 #include "camera_device_session_hwl.h" 22 #include "capture_session.h" 23 #include "depth_process_block.h" 24 #include "hwl_types.h" 25 #include "result_dispatcher.h" 26 #include "result_processor.h" 27 #include "rgbird_depth_result_processor.h" 28 #include "rgbird_result_request_processor.h" 29 #include "rgbird_rt_request_processor.h" 30 31 namespace android { 32 namespace google_camera_hal { 33 34 // RgbirdCaptureSession implements a CaptureSession that contains a single 35 // process chain that consists of 36 // 37 // RgbirdRtRequestProcessor -> MultiCameraRtProcessBlock -> 38 // RgbirdResultRequestProcessor -> DepthProcessBlock -> 39 // BasicResultProcessor 40 // 41 // It only supports a camera device session that consists of one RGB and two 42 // IR cameras. 43 class RgbirdCaptureSession : public CaptureSession { 44 public: 45 // Return if the device session HWL and stream configuration are supported. 46 static bool IsStreamConfigurationSupported( 47 CameraDeviceSessionHwl* device_session_hwl, 48 const StreamConfiguration& stream_config); 49 50 // Create a RgbirdCaptureSession. 51 // 52 // device_session_hwl is owned by the caller and must be valid during the 53 // lifetime of RgbirdCaptureSession. 54 // stream_config is the stream configuration. 55 // process_capture_result is the callback function to notify results. 56 // notify is the callback function to notify messages. 57 // process_batch_capture_result is the callback function to notify batched 58 // results. 59 // notify is the callback function to notify messages. 60 // hal_configured_streams will be filled with HAL configured streams. 61 // camera_allocator_hwl is owned by the caller and must be valid during the 62 // lifetime of RgbirdCaptureSession 63 static std::unique_ptr<CaptureSession> Create( 64 CameraDeviceSessionHwl* device_session_hwl, 65 const StreamConfiguration& stream_config, 66 ProcessCaptureResultFunc process_capture_result, 67 ProcessBatchCaptureResultFunc process_batch_capture_result, 68 NotifyFunc notify, HwlSessionCallback session_callback, 69 std::vector<HalStream>* hal_configured_streams, 70 CameraBufferAllocatorHwl* camera_allocator_hwl = nullptr); 71 72 virtual ~RgbirdCaptureSession(); 73 74 // Override functions in CaptureSession start. 75 status_t ProcessRequest(const CaptureRequest& request) override; 76 77 status_t Flush() override; 78 // Override functions in CaptureSession end. 79 80 protected: 81 RgbirdCaptureSession() = default; 82 83 private: 84 static constexpr int32_t kInvalidStreamId = -1; 85 static const uint32_t kRgbRawBufferCount = 16; 86 // Min required buffer count of internal raw stream. 87 static const uint32_t kRgbMinRawBufferCount = 12; 88 static constexpr uint32_t kPartialResult = 1; 89 static const android_pixel_format_t kHdrplusRawFormat = HAL_PIXEL_FORMAT_RAW10; 90 static const uint32_t kDefaultInternalBufferCount = 8; 91 92 status_t Initialize(CameraDeviceSessionHwl* device_session_hwl, 93 const StreamConfiguration& stream_config, 94 ProcessCaptureResultFunc process_capture_result, 95 NotifyFunc notify, 96 HwlRequestBuffersFunc request_stream_buffers, 97 std::vector<HalStream>* hal_configured_streams); 98 99 // Create a process chain that contains a realtime process block and a 100 // depth process block. 101 status_t CreateProcessChain(const StreamConfiguration& stream_config, 102 ProcessCaptureResultFunc process_capture_result, 103 NotifyFunc notify, 104 std::vector<HalStream>* hal_configured_streams); 105 106 // Check if all streams in stream_config are also in 107 // process_block_stream_config. 108 bool AreAllStreamsConfigured( 109 const StreamConfiguration& stream_config, 110 const StreamConfiguration& process_block_stream_config) const; 111 112 // Setup realtime process chain 113 status_t SetupRealtimeProcessChain( 114 const StreamConfiguration& stream_config, 115 ProcessCaptureResultFunc process_capture_result, NotifyFunc notify, 116 std::unique_ptr<ProcessBlock>* realtime_process_block, 117 std::unique_ptr<RgbirdResultRequestProcessor>* realtime_result_processor, 118 std::unique_ptr<ProcessBlock>* depth_process_block, 119 std::unique_ptr<ResultProcessor>* depth_result_processor); 120 121 // Setup hdrplus process chain 122 status_t SetupHdrplusProcessChain( 123 const StreamConfiguration& stream_config, 124 ProcessCaptureResultFunc process_capture_result, NotifyFunc notify, 125 std::unique_ptr<ProcessBlock>* hdrplus_process_block, 126 std::unique_ptr<ResultProcessor>* hdrplus_result_processor); 127 128 // Configure streams for the process chain. 129 status_t ConfigureStreams(const StreamConfiguration& stream_config, 130 RequestProcessor* request_processor, 131 ProcessBlock* process_block, 132 StreamConfiguration* process_block_stream_config); 133 134 // Build pipelines and return HAL configured streams. 135 // Allocate internal raw buffer 136 status_t BuildPipelines(const StreamConfiguration& stream_config, 137 ProcessBlock* realtime_process_block, 138 ProcessBlock* depth_process_block, 139 ProcessBlock* hdrplus_process_block, 140 std::vector<HalStream>* hal_configured_streams); 141 142 // Connect the process chain. 143 status_t ConnectProcessChain(RequestProcessor* request_processor, 144 std::unique_ptr<ProcessBlock> process_block, 145 std::unique_ptr<ResultProcessor> result_processor); 146 147 // Invoked when receiving a result from result processor. 148 void ProcessCaptureResult(std::unique_ptr<CaptureResult> result); 149 150 // Invoked when reciving a message from result processor. 151 void NotifyHalMessage(const NotifyMessage& message); 152 153 // Get internal rgb raw stream id from request processor. 154 status_t ConfigureHdrplusRawStreamId( 155 const StreamConfiguration& process_block_stream_config); 156 157 // Get internal RGB YUV stream id and IR RAW streams from request processor in 158 // case depth is configured. 159 status_t SetDepthInternalStreamId( 160 const StreamConfiguration& process_block_stream_config, 161 const StreamConfiguration& stream_config); 162 163 // Combine usage of realtime and HDR+ hal stream 164 // And allocate internal rgb raw stream buffers 165 status_t ConfigureHdrplusUsageAndBuffers( 166 std::vector<HalStream>* hal_configured_streams, 167 std::vector<HalStream>* hdrplus_hal_configured_streams); 168 169 // Allocate buffers for internal stream buffer managers 170 status_t AllocateInternalBuffers( 171 const StreamConfiguration& framework_stream_config, 172 std::vector<HalStream>* hal_configured_streams, 173 ProcessBlock* hdrplus_process_block); 174 175 // Initialize physical camera ids from the camera characteristics 176 status_t InitializeCameraIds(CameraDeviceSessionHwl* device_session_hwl); 177 178 // Remove internal streams from the hal configured stream list 179 status_t PurgeHalConfiguredStream( 180 const StreamConfiguration& stream_config, 181 std::vector<HalStream>* hal_configured_streams); 182 183 // Determine if a depth process block is needed the capture session 184 bool NeedDepthProcessBlock() const; 185 186 // Create stream config for the Depth process chain segment 187 // Keep all output stream from stream_config, change rt internal streams added 188 // for depth processing as input streams. 189 status_t MakeDepthStreamConfig( 190 const StreamConfiguration& rt_process_block_stream_config, 191 const StreamConfiguration& stream_config, 192 StreamConfiguration* depth_stream_config); 193 194 // Create the segment of chain that contains a depth process block 195 status_t CreateDepthChainSegment( 196 std::unique_ptr<DepthProcessBlock>* depth_process_block, 197 std::unique_ptr<RgbirdDepthResultProcessor>* depth_result_processor, 198 RgbirdResultRequestProcessor* rt_result_processor, 199 const StreamConfiguration& overall_config, 200 const StreamConfiguration& stream_config, 201 StreamConfiguration* depth_block_stream_config); 202 203 // Setup the offline segment connecting to the realtime process chain 204 status_t SetupDepthChainSegment( 205 const StreamConfiguration& stream_config, 206 RgbirdResultRequestProcessor* realtime_result_processor, 207 std::unique_ptr<ProcessBlock>* depth_process_block, 208 std::unique_ptr<ResultProcessor>* depth_result_processor, 209 StreamConfiguration* rt_process_block_stream_config); 210 211 // device_session_hwl_ is owned by the client. 212 CameraDeviceSessionHwl* device_session_hwl_ = nullptr; 213 std::unique_ptr<InternalStreamManager> internal_stream_manager_; 214 215 std::unique_ptr<RgbirdRtRequestProcessor> rt_request_processor_; 216 217 std::unique_ptr<RequestProcessor> hdrplus_request_processor_; 218 219 std::unique_ptr<ResultDispatcher> result_dispatcher_; 220 221 std::mutex callback_lock_; 222 // The following callbacks must be protected by callback_lock_. 223 ProcessCaptureResultFunc process_capture_result_; 224 NotifyFunc notify_; 225 HwlRequestBuffersFunc request_stream_buffers_; 226 227 // For error notify to framework directly 228 NotifyFunc device_session_notify_; 229 int32_t rgb_raw_stream_id_ = kInvalidStreamId; 230 bool is_hdrplus_supported_ = false; 231 232 // Whether the stream configuration has depth stream 233 bool has_depth_stream_ = false; 234 // Internal YUV stream id if there is a depth stream configured 235 int32_t rgb_internal_yuv_stream_id_ = kInvalidStreamId; 236 // Internal IR source stream id 237 int32_t ir1_internal_raw_stream_id_ = kInvalidStreamId; 238 // Internal IR target stream id 239 int32_t ir2_internal_raw_stream_id_ = kInvalidStreamId; 240 241 // Camera ids parsed from the characteristics 242 uint32_t rgb_camera_id_ = 0; 243 // Ir1 generates the src buffer for depth 244 uint32_t ir1_camera_id_ = 0; 245 // Ir2 generates the tar buffer for depth 246 uint32_t ir2_camera_id_ = 0; 247 248 // TODO(b/128633958): remove this after FLL syncing is verified 249 bool force_internal_stream_ = false; 250 // Use this stream id to check the request is HDR+ compatible 251 int32_t hal_preview_stream_id_ = -1; 252 }; 253 254 } // namespace google_camera_hal 255 } // namespace android 256 257 #endif // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_CAPTURE_SESSION_H_ 258