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_DEPTH_PROCESS_BLOCK_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_DEPTH_PROCESS_BLOCK_H_
19 
20 #include <map>
21 
22 #include "depth_generator.h"
23 #include "hwl_types.h"
24 #include "process_block.h"
25 
26 using android::depth_generator::DepthGenerator;
27 using android::depth_generator::DepthRequestInfo;
28 using android::depth_generator::DepthResultStatus;
29 
30 namespace android {
31 namespace google_camera_hal {
32 
33 // DepthProcessBlock implements a ProcessBlock to generate a depth stream
34 // for a logical camera consisting of one RGB and two IR camera sensors.
35 class DepthProcessBlock : public ProcessBlock {
36  public:
37   struct DepthProcessBlockCreateData {
38     // stream id of the internal yuv stream from RGB sensor
39     int32_t rgb_internal_yuv_stream_id = -1;
40     // stream id of the internal raw stream from IR 1
41     int32_t ir1_internal_raw_stream_id = -1;
42     // stream id of the internal raw stream from IR 2
43     int32_t ir2_internal_raw_stream_id = -1;
44   };
45   // Create a DepthProcessBlock.
46   static std::unique_ptr<DepthProcessBlock> Create(
47       CameraDeviceSessionHwl* device_session_hwl,
48       HwlRequestBuffersFunc request_stream_buffers,
49       const DepthProcessBlockCreateData& create_data);
50 
51   virtual ~DepthProcessBlock();
52 
53   // Override functions of ProcessBlock start.
54   status_t ConfigureStreams(const StreamConfiguration& stream_config,
55                             const StreamConfiguration& overall_config) override;
56 
57   status_t SetResultProcessor(
58       std::unique_ptr<ResultProcessor> result_processor) override;
59 
60   status_t GetConfiguredHalStreams(
61       std::vector<HalStream>* hal_streams) const override;
62 
63   status_t ProcessRequests(
64       const std::vector<ProcessBlockRequest>& process_block_requests,
65       const CaptureRequest& remaining_session_request) override;
66 
67   status_t Flush() override;
68   // Override functions of ProcessBlock end.
69 
70  protected:
71   DepthProcessBlock(HwlRequestBuffersFunc request_stream_buffers_,
72                     const DepthProcessBlockCreateData& create_data);
73 
74  private:
75   struct PendingDepthRequestInfo {
76     CaptureRequest request;
77     DepthRequestInfo depth_request;
78   };
79 
80   static constexpr int32_t kInvalidStreamId = -1;
81   const uint32_t kDepthStreamMaxBuffers = 8;
82 
83   // Callback function to request stream buffer from camera device session
84   const HwlRequestBuffersFunc request_stream_buffers_;
85 
86   // Load the depth generator dynamically
87   status_t LoadDepthGenerator(std::unique_ptr<DepthGenerator>* depth_generator);
88 
89   // Map the input and output buffers from buffer_handle_t to UMD virtual addr
90   status_t MapBuffersForDepthGenerator(const StreamBuffer& stream_buffer,
91                                        depth_generator::Buffer* depth_buffer);
92 
93   // Get the gralloc buffer size of a stream
94   status_t GetStreamBufferSize(const Stream& stream, int32_t* buffer_size);
95 
96   // Ummap the input and output buffers
97   status_t UnmapBuffersForDepthGenerator(const StreamBuffer& stream_buffer,
98                                          uint8_t* addr);
99 
100   // Prepare a depth request info for the depth generator
101   status_t PrepareDepthRequestInfo(const CaptureRequest& request,
102                                    DepthRequestInfo* depth_request_info,
103                                    HalCameraMetadata* metadata,
104                                    const HalCameraMetadata* color_metadata);
105 
106   // Clean up a depth request info by unmapping the buffers
107   status_t UnmapDepthRequestBuffers(uint32_t frame_number);
108 
109   // Caclculate the ratio of logical camera active array size comparing to the
110   // IR camera active array size
111   status_t CalculateActiveArraySizeRatio(
112       CameraDeviceSessionHwl* device_session_hwl);
113 
114   // Calculate the crop region info from the RGB sensor framework to the IR
115   // sensor framework. Update the depth_request_info with the updated result.
116   status_t UpdateCropRegion(const CaptureRequest& request,
117                             DepthRequestInfo* depth_request_info,
118                             HalCameraMetadata* metadata);
119 
120   // Request the stream buffer for depth stream. incomplete_buffer is the
121   // StreamBuffer that does not have a valid buffer handle and needs to be
122   // replaced by the newly requested buffer.
123   status_t RequestDepthStreamBuffer(StreamBuffer* incomplete_buffer,
124                                     uint32_t frame_number);
125 
126   // Initialize the HAL Buffer Management status.
127   status_t InitializeBufferManagementStatus(
128       CameraDeviceSessionHwl* device_session_hwl);
129 
130   // Submit a depth request through the blocking depth generator API
131   status_t SubmitBlockingDepthRequest(const DepthRequestInfo& request_info);
132 
133   // Submit a detph request through the asynchronized depth generator API
134   status_t SubmitAsyncDepthRequest(const DepthRequestInfo& request_info);
135 
136   // Process the depth result of frame frame_number
137   status_t ProcessDepthResult(DepthResultStatus result_status,
138                               uint32_t frame_number);
139 
140   // Map all buffers needed by a depth request from request
141   status_t MapDepthRequestBuffers(const CaptureRequest& request,
142                                   DepthRequestInfo* depth_request_info);
143 
144   mutable std::mutex configure_lock_;
145 
146   // If streams are configured. Must be protected by configure_lock_.
147   bool is_configured_ = false;
148 
149   std::mutex result_processor_lock_;
150 
151   // Result processor. Must be protected by result_processor_lock_.
152   std::unique_ptr<ResultProcessor> result_processor_ = nullptr;
153 
154   // Depth stream configured in the depth process block
155   HalStream depth_stream_;
156 
157   // TODO(b/128633958): remove this after FLL syncing is verified
158   bool force_internal_stream_ = false;
159 
160   // Provider library handle.
161   void* depth_generator_lib_handle_ = nullptr;
162 
163   // Depth Generator
164   std::unique_ptr<DepthGenerator> depth_generator_ = nullptr;
165 
166   // Map from stream id to their buffer size
167   std::map<int32_t, uint32_t> stream_buffer_sizes_;
168 
169   // Map from stream id to the stream
170   std::map<int32_t, Stream> depth_io_streams_;
171 
172   // Ratio of logical camera active array size comparing to IR camera active
173   // array size.
174   float logical_to_ir_ratio_ = 1.0;
175 
176   // IR sensor active array sizes
177   int32_t ir_active_array_width_ = 640;
178   int32_t ir_active_array_height_ = 480;
179 
180   // Whether the HAL Buffer Management is supported
181   bool buffer_management_supported_ = false;
182 
183   // Whether the pipelined depth engine is enabled
184   bool pipelined_depth_engine_enabled_ = false;
185 
186   std::mutex pending_requests_mutex_;
187   // Pending depth request indexed by the frame_number
188   // Must be protected by pending_requests_mutex_
189   std::unordered_map<uint32_t, PendingDepthRequestInfo> pending_depth_requests_;
190 
191   // Whether RGB-IR auto-calibration is enabled. This affects how the internal
192   // YUV stream results are handled.
193   bool rgb_ir_auto_cal_enabled_ = false;
194 
195   // stream id of the internal yuv stream from RGB sensor
196   int32_t rgb_internal_yuv_stream_id_ = kInvalidStreamId;
197   // stream id of the internal raw stream from IR 1
198   int32_t ir1_internal_raw_stream_id_ = kInvalidStreamId;
199   // stream id of the internal raw stream from IR 2
200   int32_t ir2_internal_raw_stream_id_ = kInvalidStreamId;
201 
202   // Guarding async depth generator API calls and the result processing calls
203   std::mutex depth_generator_api_lock_;
204 };
205 
206 }  // namespace google_camera_hal
207 }  // namespace android
208 
209 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_DEPTH_PROCESS_BLOCK_H_
210