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