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_DUAL_IR_RESULT_PROCESSOR_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_DUAL_IR_RESULT_PROCESSOR_H_
19 
20 #include <map>
21 
22 #include "request_processor.h"
23 #include "result_processor.h"
24 
25 namespace android {
26 namespace google_camera_hal {
27 
28 // DualIrResultRequestProcessor implements a ResultProcessor for a logical
29 // camera that consists of two IR cameras. It also implements a RequestProcessor
30 // for the logical camera to generate depth.
31 class DualIrResultRequestProcessor : public ResultProcessor,
32                                      public RequestProcessor {
33  public:
34   // Create a DualIrResultRequestProcessor.
35   // device_session_hwl is owned by the client and must be valid during the life
36   // cycle of this DualIrResultRequestProcessor.
37   // stream_config is the stream configuration set by the framework. It's not
38   // the process block's stream configuration.
39   // lead_camera_id is the ID of the lead IR camera.
40   static std::unique_ptr<DualIrResultRequestProcessor> Create(
41       CameraDeviceSessionHwl* device_session_hwl,
42       const StreamConfiguration& stream_config, uint32_t lead_camera_id);
43 
44   virtual ~DualIrResultRequestProcessor() = default;
45 
46   // Override functions of ResultProcessor start.
47   void SetResultCallback(ProcessCaptureResultFunc process_capture_result,
48                          NotifyFunc notify) override;
49 
50   status_t AddPendingRequests(
51       const std::vector<ProcessBlockRequest>& process_block_requests,
52       const CaptureRequest& remaining_session_request) override;
53 
54   void ProcessResult(ProcessBlockResult block_result) override;
55 
56   void Notify(const ProcessBlockNotifyMessage& block_message) override;
57 
58   status_t FlushPendingRequests() override;
59   // Override functions of ResultProcessor end.
60 
61   // Override functions of RequestProcessor start.
62   status_t ConfigureStreams(
63       InternalStreamManager* internal_stream_manager,
64       const StreamConfiguration& stream_config,
65       StreamConfiguration* process_block_stream_config) override;
66 
67   status_t SetProcessBlock(std::unique_ptr<ProcessBlock> process_block) override;
68 
69   status_t ProcessRequest(const CaptureRequest& request) override;
70 
71   status_t Flush() override;
72   // Override functions of RequestProcessor end.
73 
74  protected:
75   DualIrResultRequestProcessor(const StreamConfiguration& stream_config,
76                                uint32_t logical_camera_id,
77                                uint32_t lead_camera_id);
78 
79  private:
80   const uint32_t kLogicalCameraId;
81   const uint32_t kLeadCameraId;
82 
83   // Define a pending result metadata
84   struct PendingResultMetadata {
85     // Result metadata for the logical camera.
86     std::unique_ptr<HalCameraMetadata> metadata;
87     // Map from a physical camera ID to the physical camera's result metadata.
88     std::map<uint32_t, std::unique_ptr<HalCameraMetadata>> physical_metadata;
89   };
90 
91   // If a stream is a physical stream configured by the framework.
92   // stream_id is the ID of the stream.
93   // physical_camera_id will be filled with the physical camera ID if this
94   // method return true.
95   bool IsFrameworkPhyiscalStream(int32_t stream_id,
96                                  uint32_t* physical_camera_id) const;
97 
98   // Add pending physical camera's result metadata to the map.
99   // block_request is a block request used figure out pending results.
100   // physical_metadata is the map to add the pending physical camera's result
101   // metadata to.
102   status_t AddPendingPhysicalCameraMetadata(
103       const ProcessBlockRequest& block_request,
104       std::map<uint32_t, std::unique_ptr<HalCameraMetadata>>* physical_metadata);
105 
106   // Try to send result metadata for a frame number if all of it's result
107   // metadata are ready. Must have pending_result_metadata_mutex_ locked.
108   void TrySendingResultMetadataLocked(uint32_t frame_number);
109 
110   // Process a result metadata and update the pending result metadata map.
111   status_t ProcessResultMetadata(
112       uint32_t frame_number, uint32_t physical_camera_id,
113       std::unique_ptr<HalCameraMetadata> result_metadata);
114 
115   // Map from a stream ID to a camera ID based on framework stream configuration.
116   std::map<int32_t, uint32_t> stream_camera_ids_;
117 
118   std::mutex pending_result_metadata_mutex_;
119 
120   // Map from a frame number to the pending result metadata. Must be protected
121   // by pending_result_metadata_mutex_.
122   std::map<uint32_t, PendingResultMetadata> pending_result_metadata_;
123 
124   std::mutex callback_lock_;
125 
126   // The following callbacks must be protected by callback_lock_.
127   ProcessCaptureResultFunc process_capture_result_;
128   NotifyFunc notify_;
129 };
130 
131 }  // namespace google_camera_hal
132 }  // namespace android
133 
134 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_DUAL_IR_RESULT_PROCESSOR_H_
135