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_RT_REQUEST_PROCESSOR_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_RT_REQUEST_PROCESSOR_H_
19 
20 #include <limits>
21 
22 #include "process_block.h"
23 #include "request_processor.h"
24 
25 namespace android {
26 namespace google_camera_hal {
27 
28 // RgbirdRtRequestProcessor implements a RequestProcessor handling realtime
29 // requests for a logical camera consisting of one RGB camera sensor and two IR
30 // camera sensors.
31 class RgbirdRtRequestProcessor : public RequestProcessor {
32  public:
33   // device_session_hwl is owned by the caller and must be valid during the
34   // lifetime of this RgbirdRtRequestProcessor.
35   static std::unique_ptr<RgbirdRtRequestProcessor> Create(
36       CameraDeviceSessionHwl* device_session_hwl, bool is_hdrplus_supported);
37 
38   virtual ~RgbirdRtRequestProcessor() = default;
39 
40   // Override functions of RequestProcessor start.
41   status_t ConfigureStreams(
42       InternalStreamManager* internal_stream_manager,
43       const StreamConfiguration& stream_config,
44       StreamConfiguration* process_block_stream_config) override;
45 
46   status_t SetProcessBlock(std::unique_ptr<ProcessBlock> process_block) override;
47 
48   status_t ProcessRequest(const CaptureRequest& request) override;
49 
50   status_t Flush() override;
51   // Override functions of RequestProcessor end.
52 
53   // Whether the current session is a session in which auto cal should happen.
54   bool IsAutocalSession() const;
55 
56  protected:
57   RgbirdRtRequestProcessor(uint32_t rgb_camera_id, uint32_t ir1_camera_id,
58                            uint32_t ir2_camera_id, uint32_t active_array_width,
59                            uint32_t active_array_height,
60                            bool is_hdrplus_supported,
61                            CameraDeviceSessionHwl* device_session_hwl);
62 
63  private:
64   static const int32_t kStreamIdInvalid = -1;
65   static constexpr uint32_t kAutocalFrameNumber = 5;
66   const uint32_t kDefaultYuvStreamWidth = 640;
67   const uint32_t kDefaultYuvStreamHeight = 480;
68   const uint32_t kRgbCameraId;
69   const uint32_t kIr1CameraId;
70   const uint32_t kIr2CameraId;
71 
72   status_t CreateDepthInternalStreams(
73       InternalStreamManager* internal_stream_manager,
74       StreamConfiguration* process_block_stream_config);
75 
76   status_t RegisterHdrplusInternalRaw(
77       StreamConfiguration* process_block_stream_config);
78   status_t TryAddHdrplusRawOutputLocked(CaptureRequest* block_request,
79                                         const CaptureRequest& request);
80   // Try to add RGB internal YUV buffer if there is no request on any stream
81   // from the RGB sensor.
82   // Must lock process_block_lock_ before calling this function.
83   status_t TryAddDepthInternalYuvOutputLocked(CaptureRequest* block_request);
84   status_t AddIrRawProcessBlockRequestLocked(
85       std::vector<ProcessBlockRequest>* block_requests,
86       const CaptureRequest& request, uint32_t camera_id);
87 
88   status_t TryAddRgbProcessBlockRequestLocked(
89       std::vector<ProcessBlockRequest>* block_requests,
90       const CaptureRequest& request);
91 
92   // Find a resolution from the available stream configuration that has the same
93   // aspect ratio with one of the non-raw and non-depth stream in the framework
94   // stream config.
95   // If there is no non-raw and non-depth stream from framework, use the
96   // resolution with the smallest area in the available stream config.
97   status_t FindSmallestResolutionForInternalYuvStream(
98       const StreamConfiguration& process_block_stream_config,
99       uint32_t* yuv_w_adjusted, uint32_t* yuv_h_adjusted);
100 
101   /// Find smallest non-warped YUV stream resolution supported by HWL
102   status_t FindSmallestNonWarpedYuvStreamResolution(uint32_t* yuv_w_adjusted,
103                                                     uint32_t* yuv_h_adjusted);
104 
105   // Set the stream id of the yuv stream that does not need warping in
106   // the session parameter of the process block stream configuration.
107   status_t SetNonWarpedYuvStreamId(
108       int32_t non_warped_yuv_stream_id,
109       StreamConfiguration* process_block_stream_config);
110 
111   // Whether the internal YUV stream result should be used for auto cal.
112   bool IsAutocalRequest(uint32_t frame_number);
113 
114   std::mutex process_block_lock_;
115 
116   // Protected by process_block_lock_.
117   std::unique_ptr<ProcessBlock> process_block_;
118   // [0]: IR1 stream; [1]: IR2 stream
119   int32_t ir_raw_stream_id_[2] = {kStreamIdInvalid, kStreamIdInvalid};
120   int32_t rgb_yuv_stream_id_ = kStreamIdInvalid;
121 
122   bool preview_intent_seen_ = false;
123   // rgb_raw_stream_id_ is the stream ID of internal raw from RGB camera for HDR+
124   int32_t rgb_raw_stream_id_ = -1;
125   uint32_t rgb_active_array_width_ = 0;
126   uint32_t rgb_active_array_height_ = 0;
127   bool is_hdrplus_supported_ = false;
128   bool is_hdrplus_zsl_enabled_ = false;
129 
130   // TODO(b/128633958): remove this after FLL syncing is verified
131   bool force_internal_stream_ = false;
132   int32_t depth_stream_id_ = kStreamIdInvalid;
133   InternalStreamManager* internal_stream_manager_ = nullptr;
134   CameraDeviceSessionHwl* device_session_hwl_ = nullptr;
135 
136   // Whether RGB-IR auto cal is needed
137   bool rgb_ir_auto_cal_enabled_ = false;
138   // Indicates whether a session needs auto cal(not every session needs even if
139   // rgb_ir_auto_cal_enabled_ is true).
140   bool is_auto_cal_session_ = false;
141   bool auto_cal_triggered_ = false;
142 };
143 
144 }  // namespace google_camera_hal
145 }  // namespace android
146 
147 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_RT_REQUEST_PROCESSOR_H_
148