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