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_UTILS_UTILS_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_UTILS_H_
19 
20 #include <log/log.h>
21 
22 #include <map>
23 #include <set>
24 #include <utility>
25 
26 #include "hal_types.h"
27 #include "physical_camera_info_hwl.h"
28 
29 namespace android {
30 namespace google_camera_hal {
31 namespace utils {
32 
33 bool IsPreviewStream(const Stream& stream);
34 bool IsJPEGSnapshotStream(const Stream& stream);
35 bool IsVideoStream(const Stream& stream);
36 bool IsRawStream(const Stream& stream);
37 bool IsInputRawStream(const Stream& stream);
38 bool IsArbitraryDataSpaceRawStream(const Stream& stream);
39 bool IsYUVSnapshotStream(const Stream& stream);
40 bool IsDepthStream(const Stream& stream);
41 bool IsOutputZslStream(const Stream& stream);
42 bool IsSoftwareDenoiseEligibleSnapshotStream(const Stream& stream);
43 bool IsSecuredStream(const Stream& stream);
44 bool IsStreamUseCasesVideoCall(const Stream& stream);
45 bool IsHdrStream(const Stream& stream);
46 
47 bool HasCapability(const HalCameraMetadata* metadata, uint8_t capability);
48 
49 status_t GetSensorPhysicalSize(const HalCameraMetadata* characteristics,
50                                float* width, float* height);
51 
52 status_t GetSensorActiveArraySize(const HalCameraMetadata* characteristics,
53                                   Rect* active_array,
54                                   bool maximum_resolution = false);
55 
56 status_t GetSensorPixelArraySize(const HalCameraMetadata* characteristics,
57                                  Dimension* pixel_array);
58 
59 status_t GetFocalLength(const HalCameraMetadata* characteristics,
60                         float* focal_length);
61 
62 status_t GetZoomRatioRange(const HalCameraMetadata* characteristics,
63                            ZoomRatioRange* zoom_ratio_range);
64 
65 // Return if LiveSnapshot is configured
66 bool IsLiveSnapshotConfigured(const StreamConfiguration& stream_config);
67 
68 // Return true if max fps is the same at high speed mode
69 bool IsHighSpeedModeFpsCompatible(StreamConfigurationMode mode,
70                                   const HalCameraMetadata* old_session,
71                                   const HalCameraMetadata* new_session);
72 
73 // This method is for IsReconfigurationRequired purpose
74 // Return true if meet any condition below
75 // 1. Any session entry count is zero
76 // 2. All metadata are the same between old and new session.
77 //    For ANDROID_CONTROL_AE_TARGET_FPS_RANGE, only compare max fps.
78 bool IsSessionParameterCompatible(const HalCameraMetadata* old_session,
79                                   const HalCameraMetadata* new_session);
80 
81 bool SupportRealtimeThread();
82 status_t SetRealtimeThread(pthread_t thread);
83 status_t UpdateThreadSched(pthread_t thread, int32_t policy,
84                            struct sched_param* param);
85 
86 status_t GetStreamUseCases(const HalCameraMetadata* static_metadata,
87                            std::set<int64_t>* stream_use_cases);
88 
89 status_t GetPhysicalCameraStreamUseCases(
90     const PhysicalCameraInfoHwl* physical_camera_info,
91     std::map<uint32_t, std::set<int64_t>>* camera_id_to_stream_use_cases);
92 
93 bool IsStreamUseCaseSupported(
94     const StreamConfiguration& stream_config, uint32_t logical_camera_id,
95     const std::map<uint32_t, std::set<int64_t>>& camera_id_to_stream_use_cases,
96     bool log_if_not_supported = true);
97 
98 // Map the rectangle to the coordination of HAL.
99 void ConvertZoomRatio(float zoom_ratio, const Dimension& active_array_dimension,
100                       int32_t* left, int32_t* top, int32_t* width,
101                       int32_t* height);
102 
103 // Clamp the coordinate boundary within dimension.
104 template <typename T>
105 void ClampBoundary(const Dimension& active_array_dimension, T* x, T* y,
106                    T* width = nullptr, T* height = nullptr) {
107   if (x == nullptr || y == nullptr) {
108     ALOGE("%s, invalid params", __FUNCTION__);
109     return;
110   }
111   *x = std::clamp(*x, static_cast<T>(0),
112                   static_cast<T>(active_array_dimension.width - 1));
113   *y = std::clamp(*y, static_cast<T>(0),
114                   static_cast<T>(active_array_dimension.height - 1));
115 
116   if (width) {
117     *width = std::clamp(*width, static_cast<T>(1),
118                         static_cast<T>(active_array_dimension.width - *x));
119   }
120   if (height) {
121     *height = std::clamp(*height, static_cast<T>(1),
122                          static_cast<T>(active_array_dimension.height - *y));
123   }
124 }
125 
126 // Map the position to the coordinate of framework.
127 template <typename T>
128 void RevertZoomRatio(const float zoom_ratio,
129                      const Dimension& active_array_dimension,
130                      const bool round_to_int, T* x, T* y, T* width = nullptr,
131                      T* height = nullptr) {
132   if (x == nullptr || y == nullptr) {
133     ALOGE("%s, invalid params", __FUNCTION__);
134     return;
135   }
136   float tmp_x = *x * zoom_ratio -
137                 0.5f * active_array_dimension.width * (zoom_ratio - 1.0f);
138   float tmp_y = *y * zoom_ratio -
139                 0.5f * active_array_dimension.height * (zoom_ratio - 1.0f);
140 
141   if (round_to_int) {
142     *x = std::round(tmp_x);
143     *y = std::round(tmp_y);
144   } else {
145     *x = tmp_x;
146     *y = tmp_y;
147   }
148 
149   if (width) {
150     *width = std::round(*width * zoom_ratio);
151   }
152   if (height) {
153     *height = std::round(*height * zoom_ratio);
154   }
155   ClampBoundary(active_array_dimension, x, y, width, height);
156 }
157 
158 std::vector<std::string> FindLibraryPaths(const char* dir_path);
159 
160 }  // namespace utils
161 }  // namespace google_camera_hal
162 }  // namespace android
163 
164 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_UTILS_H_
165