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_RESULT_REQUEST_PROCESSOR_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_RESULT_REQUEST_PROCESSOR_H_
19 
20 #include <set>
21 
22 #include "request_processor.h"
23 #include "result_processor.h"
24 #include "vendor_tag_defs.h"
25 
26 namespace android {
27 namespace google_camera_hal {
28 
29 // RgbirdResultRequestProcessor implements a ResultProcessor handling realtime
30 // capture results for a logical camera consisting of one RGB and two IR camera
31 // sensors.
32 class RgbirdResultRequestProcessor : public ResultProcessor,
33                                      public RequestProcessor {
34  public:
35   struct RgbirdResultRequestProcessorCreateData {
36     // camera id of the color sensor
37     uint32_t rgb_camera_id = 0;
38     // camera id of the NIR sensor used as source
39     uint32_t ir1_camera_id = 0;
40     // camera id of the NIR sensor used as target
41     uint32_t ir2_camera_id = 0;
42     // stream id of the internal raw stream for hdr+
43     int32_t rgb_raw_stream_id = -1;
44     // whether hdr+ is supported
45     bool is_hdrplus_supported = false;
46     // stream id of the internal yuv stream in case depth is configured
47     int32_t rgb_internal_yuv_stream_id = -1;
48   };
49 
50   static std::unique_ptr<RgbirdResultRequestProcessor> Create(
51       const RgbirdResultRequestProcessorCreateData& create_data);
52 
53   virtual ~RgbirdResultRequestProcessor() = default;
54 
55   // Override functions of ResultProcessor start.
56   void SetResultCallback(
57       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
58       ProcessBatchCaptureResultFunc process_batch_capture_result) override;
59 
60   status_t AddPendingRequests(
61       const std::vector<ProcessBlockRequest>& process_block_requests,
62       const CaptureRequest& remaining_session_request) override;
63 
64   void ProcessResult(ProcessBlockResult block_result) override;
65 
66   void Notify(const ProcessBlockNotifyMessage& block_message) override;
67 
68   status_t FlushPendingRequests() override;
69   // Override functions of ResultProcessor end.
70 
71   // Override functions of RequestProcessor start.
72   status_t ConfigureStreams(
73       InternalStreamManager* internal_stream_manager,
74       const StreamConfiguration& stream_config,
75       StreamConfiguration* process_block_stream_config) override;
76 
77   status_t SetProcessBlock(std::unique_ptr<ProcessBlock> process_block) override;
78 
79   status_t ProcessRequest(const CaptureRequest& request) override;
80 
81   status_t Flush() override;
82   // Override functions of RequestProcessor end.
83 
84  protected:
85   RgbirdResultRequestProcessor(
86       const RgbirdResultRequestProcessorCreateData& create_data);
87 
88  private:
89   static constexpr int32_t kInvalidStreamId = -1;
90   static constexpr uint32_t kAutocalFrameNumber = 5;
91   static constexpr uint32_t kNumOfAutoCalInputBuffers = /*YUV+IR+IR*/ 3;
92   const uint32_t kRgbCameraId;
93   const uint32_t kIr1CameraId;
94   const uint32_t kIr2CameraId;
95   const int32_t kSyncWaitTime = 5000;  // milliseconds
96 
97   void ProcessResultForHdrplus(CaptureResult* result, bool* rgb_raw_output);
98   // Return the RGB internal YUV stream buffer if there is any and depth is
99   // configured
100   void TryReturnInternalBufferForDepth(CaptureResult* result,
101                                        bool* has_internal);
102 
103   // Save face detect mode for HDR+
104   void SaveFdForHdrplus(const CaptureRequest& request);
105   // Handle face detect metadata from result for HDR+
106   status_t HandleFdResultForHdrplus(uint32_t frameNumber,
107                                     HalCameraMetadata* metadata);
108   // Save lens shading map mode for HDR+
109   void SaveLsForHdrplus(const CaptureRequest& request);
110   // Handle Lens shading metadata from result for HDR+
111   status_t HandleLsResultForHdrplus(uint32_t frameNumber,
112                                     HalCameraMetadata* metadata);
113   // TODO(b/127322570): update the following function after FLL sync verified
114   // Remove internal streams for depth lock
115   status_t ReturnInternalStreams(CaptureResult* result);
116 
117   // Check fence status if need
118   status_t CheckFenceStatus(CaptureRequest* request);
119 
120   // Check all metadata exist for Autocal
121   // Protected by depth_requests_mutex_
122   bool IsAutocalMetadataReadyLocked(const HalCameraMetadata& metadata);
123 
124   // Prepare Depth Process Block request and try to submit that
125   status_t TrySubmitDepthProcessBlockRequest(
126       const ProcessBlockResult& block_result);
127 
128   // Whether the internal yuv stream buffer needs to be passed to the depth
129   // process block.
130   bool IsAutocalRequest(uint32_t frame_number) const;
131 
132   // Verify if all information is ready for a depth request for frame_number and
133   // submit the request to the process block if so.
134   status_t VerifyAndSubmitDepthRequest(uint32_t frame_number);
135 
136   std::mutex callback_lock_;
137 
138   // The following callbacks must be protected by callback_lock_.
139   ProcessCaptureResultFunc process_capture_result_;
140   NotifyFunc notify_;
141 
142   std::mutex depth_process_block_lock_;
143   // Protected by depth_process_block_lock_.
144   std::unique_ptr<ProcessBlock> depth_process_block_;
145 
146   // rgb_raw_stream_id_ is the stream ID of internal raw from RGB camera for HDR+
147   int32_t rgb_raw_stream_id_ = -1;
148   bool is_hdrplus_supported_ = false;
149 
150   // Current face detect mode set by framework.
151   uint8_t current_face_detect_mode_ = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
152 
153   std::mutex face_detect_lock_;
154   // Map from frame number to face detect mode requested for that frame by
155   // framework. And requested_face_detect_modes_ is protected by
156   // face_detect_lock_
157   std::unordered_map<uint32_t, uint8_t> requested_face_detect_modes_;
158 
159   // Current lens shading map mode set by framework.
160   uint8_t current_lens_shading_map_mode_ =
161       ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
162 
163   std::mutex lens_shading_lock_;
164   // Map from frame number to lens shading map mode requested for that frame
165   // by framework. And requested_lens_shading_map_modes_ is protected by
166   // lens_shading_lock_
167   std::unordered_map<uint32_t, uint8_t> requested_lens_shading_map_modes_;
168 
169   // Internal stream manager
170   InternalStreamManager* internal_stream_manager_ = nullptr;
171 
172   // TODO(b/128633958): remove this after FLL syncing is verified
173   bool force_internal_stream_ = false;
174 
175   // Set of framework stream id
176   std::set<int32_t> framework_stream_id_set_;
177 
178   std::mutex depth_requests_mutex_;
179 
180   // Map from framework number to capture request for depth process block. If a
181   // request does not contain any depth buffer, it is not recorded in the map.
182   // Protected by depth_requests_mutex_
183   std::unordered_map<uint32_t, std::unique_ptr<CaptureRequest>> depth_requests_;
184 
185   // Depth stream id if it is configured for the current session
186   int32_t depth_stream_id_ = -1;
187 
188   // If a depth stream is configured, always configure an extra internal YUV
189   // stream to cover the case when there is no request for any stream from the
190   // RGB sensor.
191   int32_t rgb_internal_yuv_stream_id_ = -1;
192 
193   // Whether RGB-IR auto-calibration is enabled. This affects how the internal
194   // YUV stream results are handled.
195   bool rgb_ir_auto_cal_enabled_ = false;
196 };
197 
198 }  // namespace google_camera_hal
199 }  // namespace android
200 
201 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_RESULT_REQUEST_PROCESSOR_H_
202