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_STREAM_CONFIGURATION_MAP_H_
18 #define EMULATOR_STREAM_CONFIGURATION_MAP_H_
19 
20 #include <memory>
21 #include <set>
22 #include <unordered_map>
23 
24 #include "hwl_types.h"
25 #include "system/camera_metadata.h"
26 #include "utils/Timers.h"
27 
28 namespace android {
29 
30 using google_camera_hal::HalCameraMetadata;
31 
32 typedef std::pair<uint32_t, uint32_t> StreamSize;
33 typedef std::pair<android_pixel_format_t, StreamSize> StreamConfig;
34 
35 inline bool operator==(const StreamConfig& lhs, const StreamConfig& rhs) {
36   return (std::get<0>(lhs) == std::get<0>(rhs)) &&
37          (std::get<1>(lhs).first == std::get<1>(rhs).first) &&
38          (std::get<1>(lhs).second == std::get<1>(rhs).second);
39 }
40 
41 struct StreamConfigurationHash {
operatorStreamConfigurationHash42   inline std::size_t operator()(const StreamConfig& entry) const {
43     size_t result = 1;
44     size_t hashValue = 31;
45     result = hashValue * result +
46              std::hash<android_pixel_format_t>{}(std::get<0>(entry));
47     result =
48         hashValue * result + std::hash<uint32_t>{}(std::get<1>(entry).first);
49     result =
50         hashValue * result + std::hash<uint32_t>{}(std::get<1>(entry).second);
51     return result;
52   }
53 };
54 
55 class StreamConfigurationMap {
56  public:
57   StreamConfigurationMap(const HalCameraMetadata& chars,
58                          bool maxResolution = false);
59 
GetOutputFormats()60   const std::set<android_pixel_format_t>& GetOutputFormats() const {
61     return stream_output_formats_;
62   }
63 
GetOutputSizes(android_pixel_format_t format)64   const std::set<StreamSize>& GetOutputSizes(android_pixel_format_t format) {
65     return stream_output_size_map_[format];
66   }
67 
GetDynamicPhysicalStreamOutputFormats()68   const std::set<android_pixel_format_t>& GetDynamicPhysicalStreamOutputFormats()
69       const {
70     return dynamic_physical_stream_output_formats_;
71   }
72 
GetDynamicPhysicalStreamOutputSizes(android_pixel_format_t format)73   const std::set<StreamSize>& GetDynamicPhysicalStreamOutputSizes(
74       android_pixel_format_t format) {
75     return dynamic_physical_stream_output_size_map_[format];
76   }
77 
GetOutputMinFrameDuration(StreamConfig configuration)78   nsecs_t GetOutputMinFrameDuration(StreamConfig configuration) const {
79     auto ret = stream_min_duration_map_.find(configuration);
80     return (ret == stream_min_duration_map_.end()) ? 0 : ret->second;
81   }
82 
GetOutputStallDuration(StreamConfig configuration)83   nsecs_t GetOutputStallDuration(StreamConfig configuration) const {
84     auto ret = stream_stall_map_.find(configuration);
85     return (ret == stream_stall_map_.end()) ? 0 : ret->second;
86   }
87 
SupportsReprocessing()88   bool SupportsReprocessing() const {
89     return !stream_input_output_map_.empty();
90   }
91 
GetValidOutputFormatsForInput(android_pixel_format_t format)92   const std::set<android_pixel_format_t>& GetValidOutputFormatsForInput(
93       android_pixel_format_t format) {
94     return stream_input_output_map_[format];
95   }
96 
GetInputFormats()97   const std::set<android_pixel_format_t>& GetInputFormats() {
98     return stream_input_formats_;
99   }
100 
101  private:
102   void AppendAvailableStreamConfigurations(const camera_metadata_ro_entry& entry);
103   void AppendAvailableDynamicPhysicalStreamConfigurations(
104       const camera_metadata_ro_entry& entry);
105   void AppendAvailableStreamMinDurations(const camera_metadata_ro_entry_t& entry);
106   void AppendAvailableStreamStallDurations(const camera_metadata_ro_entry& entry);
107 
108   const size_t kStreamFormatOffset = 0;
109   const size_t kStreamWidthOffset = 1;
110   const size_t kStreamHeightOffset = 2;
111   const size_t kStreamIsInputOffset = 3;
112   const size_t kStreamMinDurationOffset = 3;
113   const size_t kStreamStallDurationOffset = 3;
114   const size_t kStreamConfigurationSize = 4;
115 
116   std::set<android_pixel_format_t> stream_output_formats_;
117   std::unordered_map<android_pixel_format_t, std::set<StreamSize>>
118       stream_output_size_map_;
119   std::unordered_map<StreamConfig, nsecs_t, StreamConfigurationHash>
120       stream_stall_map_;
121   std::unordered_map<StreamConfig, nsecs_t, StreamConfigurationHash>
122       stream_min_duration_map_;
123   std::set<android_pixel_format_t> stream_input_formats_;
124   std::unordered_map<android_pixel_format_t, std::set<android_pixel_format_t>>
125       stream_input_output_map_;
126 
127   std::set<android_pixel_format_t> dynamic_physical_stream_output_formats_;
128   std::unordered_map<android_pixel_format_t, std::set<StreamSize>>
129       dynamic_physical_stream_output_size_map_;
130 };
131 
132 typedef std::unordered_map<uint32_t, std::unique_ptr<StreamConfigurationMap>>
133     PhysicalStreamConfigurationMap;
134 
135 }  // namespace android
136 
137 #endif  // EMULATOR_STREAM_CONFIGURATION_MAP_H_
138