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 EMULATOR_CAMERA_HAL_LOGICAL_REQUEST_STATE_H
18 #define EMULATOR_CAMERA_HAL_LOGICAL_REQUEST_STATE_H
19 
20 #include "EmulatedRequestState.h"
21 #include "hwl_types.h"
22 #include "utils/HWLUtils.h"
23 
24 namespace android {
25 
26 using google_camera_hal::HalCameraMetadata;
27 using google_camera_hal::HalStream;
28 using google_camera_hal::HwlPipelineCallback;
29 
30 struct EmulatedStream : public HalStream {
31   uint32_t width, height;
32   size_t buffer_size;
33   bool is_input;
34   int32_t group_id;
35 };
36 
37 struct EmulatedPipeline {
38   HwlPipelineCallback cb;
39   // stream id -> stream map
40   std::unordered_map<uint32_t, EmulatedStream> streams;
41   uint32_t physical_camera_id, pipeline_id;
42 };
43 
44 // [physical_camera_id -> [group_id -> stream_id]]
45 typedef std::map<uint32_t, std::map<uint32_t, int32_t>> DynamicStreamIdMapType;
46 
47 // Info keeping track of mapping between zoom ratio range, focal length, and
48 // physical camera Id.
49 struct ZoomRatioPhysicalCameraInfo {
50   float focal_length;
51   float min_zoom_ratio;
52   float max_zoom_ratio;
53   uint32_t physical_camera_id;
54 };
55 
56 class EmulatedLogicalRequestState {
57  public:
58   EmulatedLogicalRequestState(uint32_t camera_id);
59   virtual ~EmulatedLogicalRequestState();
60 
61   status_t Initialize(std::unique_ptr<HalCameraMetadata> static_meta,
62                       PhysicalDeviceMapPtr physical_device_map);
63 
64   status_t GetDefaultRequest(
65       RequestTemplate type,
66       std::unique_ptr<HalCameraMetadata>* default_settings /*out*/);
67 
68   std::unique_ptr<HwlPipelineResult> InitializeLogicalResult(
69       uint32_t pipeline_id, uint32_t frame_number);
70 
71   status_t InitializeLogicalSettings(
72       std::unique_ptr<HalCameraMetadata> request_settings,
73       std::unique_ptr<std::set<uint32_t>> physical_camera_output_ids,
74       EmulatedSensor::LogicalCameraSettings* logical_settings /*out*/);
75 
76   static std::unique_ptr<HalCameraMetadata> AdaptLogicalCharacteristics(
77       std::unique_ptr<HalCameraMetadata> logical_chars,
78       PhysicalDeviceMapPtr physical_devices);
79 
80   status_t UpdateRequestForDynamicStreams(
81       HwlPipelineRequest* request,
82       const std::vector<EmulatedPipeline>& pipelines,
83       const DynamicStreamIdMapType& dynamic_stream_id_map_type,
84       bool use_default_physical_camera);
85 
86  private:
87   uint32_t logical_camera_id_ = 0;
88   std::unique_ptr<EmulatedRequestState> logical_request_state_;
89   bool is_logical_device_ = false;
90   std::unique_ptr<std::set<uint32_t>> physical_camera_output_ids_;
91   PhysicalDeviceMapPtr physical_device_map_;
92   // Maps a physical device id to its respective request state
93   std::unordered_map<uint32_t, std::unique_ptr<EmulatedRequestState>>
94       physical_request_states_;
95 
96   // Describes the mapping between particular zoom ratio boundary value and
97   // physical device id. The vector is sorted by ascending zoom ratios.
98   std::vector<ZoomRatioPhysicalCameraInfo> zoom_ratio_physical_camera_info_;
99   uint32_t current_physical_camera_ = 0;
100 
101   static std::vector<ZoomRatioPhysicalCameraInfo> GetZoomRatioPhysicalCameraInfo(
102       const HalCameraMetadata* logical_chars,
103       const PhysicalDeviceMap* physical_devices);
104   static void UpdateActivePhysicalId(HalCameraMetadata* result_metadata,
105                                      uint32_t device_id);
106 
107   EmulatedLogicalRequestState(const EmulatedLogicalRequestState&) = delete;
108   EmulatedLogicalRequestState& operator=(const EmulatedLogicalRequestState&) =
109       delete;
110 };
111 
112 }  // namespace android
113 
114 #endif  // EMULATOR_CAMERA_HAL_LOGICAL_REQUEST_STATE_H
115