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