1 /*
2 * Copyright (c) 2023-2023 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_DPS_UTILS_H
17 #define OHOS_CAMERA_DPS_UTILS_H
18
19 #include <unistd.h>
20
21 #include "watch_dog.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
26 using SteadyTimePoint = std::chrono::steady_clock::time_point;
27 using Nano = std::chrono::nanoseconds;
28 using Micro = std::chrono::microseconds;
29 using Milli = std::chrono::milliseconds;
30 using Seconds = std::chrono::seconds;
31
GetSteadyNow()32 inline SteadyTimePoint GetSteadyNow()
33 {
34 return SteadyTimePoint::clock::now();
35 }
36
37 template <typename Duration, typename TimePoint>
GetDiffTime(TimePoint begin,TimePoint end)38 inline auto GetDiffTime(TimePoint begin, TimePoint end)
39 {
40 if (begin > end) {
41 return static_cast<typename Duration::rep>(0);
42 }
43 return std::chrono::duration_cast<Duration>(end - begin).count();
44 }
45
46 template <typename Duration, typename TimePoint>
GetDiffTime(TimePoint begin)47 inline auto GetDiffTime(TimePoint begin)
48 {
49 return GetDiffTime<Duration>(begin, TimePoint::clock::now());
50 }
51
IsFileEmpty(int fd)52 inline bool IsFileEmpty(int fd)
53 {
54 off_t fileSize = lseek(fd, 0, SEEK_END);
55 if (fileSize == (off_t)-1) {
56 return false;
57 }
58 return fileSize == 0;
59 }
60
ClearFileContent(int fd)61 inline bool ClearFileContent(int fd)
62 {
63 if (ftruncate(fd, 0) != 0) {
64 return false;
65 }
66 if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
67 return false;
68 }
69 return true;
70 }
71
72 template <typename U>
AlignUp(U num,U alignment)73 constexpr U AlignUp(U num, U alignment)
74 {
75 return alignment ? ((num + alignment - 1) & (~(alignment - 1))) : num;
76 }
77
78 template <typename T, typename... Args>
79 struct MakeSharedHelper : public T {
MakeSharedHelperMakeSharedHelper80 explicit MakeSharedHelper(Args&&... args) : T(std::forward<Args>(args)...)
81 {
82 }
83 };
84
85 template <typename T, typename... Args>
CreateShared(Args &&...args)86 std::shared_ptr<T> CreateShared(Args&&... args)
87 {
88 return std::make_shared<MakeSharedHelper<T, Args &&...>>(std::forward<Args>(args)...);
89 }
90
91 template <typename T, typename... Args>
92 struct MakeUniqueHelper : public T {
MakeUniqueHelperMakeUniqueHelper93 explicit MakeUniqueHelper(Args&&... args) : T(std::forward<Args>(args)...)
94 {
95 }
96 };
97
98 template <typename T, typename... Args>
CreateUnique(Args &&...args)99 std::unique_ptr<T> CreateUnique(Args&&... args)
100 {
101 return std::make_unique<MakeUniqueHelper<T, Args &&...>>(std::forward<Args>(args)...);
102 }
103
GetVersionId(uint32_t major,uint32_t minor)104 inline int32_t GetVersionId(uint32_t major, uint32_t minor)
105 {
106 const uint32_t offset = 8;
107 return static_cast<int32_t>((major << offset) | minor);
108 }
109
110 Watchdog& GetGlobalWatchdog();
111 float TransExifOrientationToDegree(const std::string& orientation);
112
113 struct DpsCallerInfo {
114 int32_t pid;
115 int32_t uid;
116 uint32_t tokenID;
117 std::string bundleName;
118 std::string version;
119 };
120
121 DpsCallerInfo GetDpsCallerInfo();
122 } // namespace DeferredProcessing
123 } // namespace CameraStandard
124 } // namespace OHOS
125 #endif // OHOS_CAMERA_DPS_UTILS_H