1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef OHOS_CAMERA_UTIL_H
17 #define OHOS_CAMERA_UTIL_H
18
19 #include <limits.h>
20 #include <malloc.h>
21
22 #include "camera_metadata_info.h"
23 #include "display/graphic/common/v1_0/cm_color_space.h"
24 #include "safe_map.h"
25 #include "surface_type.h"
26 #include "v1_0/types.h"
27 #include "v1_1/types.h"
28 #include "v1_2/types.h"
29
30 namespace OHOS {
31 namespace CameraStandard {
32 class HStreamCommon;
33 static constexpr int32_t HDI_VERSION_1 = 1;
34 static constexpr int32_t HDI_VERSION_2 = 2;
35 static const std::string OHOS_PERMISSION_CAMERA = "ohos.permission.CAMERA";
36 static const std::string OHOS_PERMISSION_MANAGE_CAMERA_CONFIG = "ohos.permission.MANAGE_CAMERA_CONFIG";
37
38 enum CamServiceError {
39 CAMERA_OK = 0,
40 CAMERA_ALLOC_ERROR,
41 CAMERA_INVALID_ARG,
42 CAMERA_UNSUPPORTED,
43 CAMERA_DEVICE_BUSY,
44 CAMERA_DEVICE_CLOSED,
45 CAMERA_DEVICE_REQUEST_TIMEOUT,
46 CAMERA_STREAM_BUFFER_LOST,
47 CAMERA_INVALID_SESSION_CFG,
48 CAMERA_CAPTURE_LIMIT_EXCEED,
49 CAMERA_INVALID_STATE,
50 CAMERA_UNKNOWN_ERROR,
51 CAMERA_DEVICE_PREEMPTED,
52 CAMERA_OPERATION_NOT_ALLOWED,
53 CAMERA_DEVICE_ERROR,
54 CAMERA_NO_PERMISSION,
55 CAMERA_DEVICE_CONFLICT
56 };
57
58 enum ClientPriorityLevels {
59 PRIORITY_LEVEL_SAME = 0,
60 PRIORITY_LEVEL_LOWER,
61 PRIORITY_LEVEL_HIGHER
62 };
63
64 enum DeviceType {
65 FALLING_TYPE = 1,
66 FOLD_TYPE
67 };
68
69 enum FoldStatus {
70 UNKNOWN_FOLD = 0,
71 EXPAND,
72 FOLDED,
73 HALF_FOLD
74 };
75
76 enum FallingState {
77 FALLING_STATE = 1008,
78 };
79
80 extern std::unordered_map<int32_t, int32_t> g_cameraToPixelFormat;
81 extern std::map<int, std::string> g_cameraPos;
82 extern std::map<int, std::string> g_cameraType;
83 extern std::map<int, std::string> g_cameraConType;
84 extern std::map<int, std::string> g_cameraFormat;
85 extern std::map<int, std::string> g_cameraFocusMode;
86 extern std::map<int, std::string> g_cameraExposureMode;
87 extern std::map<int, std::string> g_cameraFlashMode;
88 extern std::map<int, std::string> g_cameraVideoStabilizationMode;
89 extern std::map<int, std::string> g_cameraPrelaunchAvailable;
90 extern std::map<int, std::string> g_cameraQuickThumbnailAvailable;
91 extern bool g_cameraDebugOn;
92
DisableJeMalloc()93 inline void DisableJeMalloc()
94 {
95 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
96 mallopt(M_SET_THREAD_CACHE, M_THREAD_CACHE_DISABLE);
97 mallopt(M_DELAYED_FREE, M_DELAYED_FREE_DISABLE);
98 mallopt(M_FLUSH_THREAD_CACHE, 0);
99 #endif
100 }
101
102 int32_t SetOpMode(int32_t opMode);
103
104 int32_t HdiToServiceError(OHOS::HDI::Camera::V1_0::CamRetCode ret);
105
106 int32_t HdiToServiceErrorV1_2(HDI::Camera::V1_2::CamRetCode ret);
107
108 std::string CreateMsg(const char* format, ...);
109
110 bool IsValidTokenId(uint32_t tokenId);
111
112 int32_t GetVersionId(uint32_t major, uint32_t minor);
113
114 bool IsValidMode(
115 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility);
116
117 bool IsValidSize(
118 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility, int32_t format, int32_t width, int32_t height);
119
120 void DumpMetadata(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraSettings);
121
122 std::string GetClientBundle(int uid);
123
124 int32_t JudgmentPriority(const pid_t& pid, const pid_t& pidCompared);
125
126 bool IsSameClient(const pid_t& pid, const pid_t& pidCompared);
127
128 bool IsInForeGround(const uint32_t callerToken);
129
130 bool IsCameraNeedClose(const uint32_t callerToken, const pid_t& pid, const pid_t& pidCompared);
131
132 int32_t CheckPermission(std::string permissionName, uint32_t callerToken);
133
134 void AddCameraPermissionUsedRecord(const uint32_t callingTokenId, const std::string permissionName);
135
IsCameraDebugOn()136 inline bool IsCameraDebugOn()
137 {
138 return g_cameraDebugOn;
139 }
140
SetCameraDebugValue(bool value)141 inline void SetCameraDebugValue(bool value)
142 {
143 g_cameraDebugOn = value;
144 }
145
146 template<typename T>
CastStream(sptr<HStreamCommon> streamCommon)147 sptr<T> CastStream(sptr<HStreamCommon> streamCommon)
148 {
149 if (streamCommon == nullptr) {
150 return nullptr;
151 }
152 return static_cast<T*>(streamCommon.GetRefPtr());
153 }
154 bool IsVerticalDevice();
155 } // namespace CameraStandard
156 } // namespace OHOS
157 #endif // OHOS_CAMERA_UTIL_H
158