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