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 #include "steady_clock.h"
17 #include "dp_log.h"
18 
19 namespace OHOS {
20 namespace CameraStandard {
21 namespace DeferredProcessing {
22 
GetTimestampMilli()23 uint64_t SteadyClock::GetTimestampMilli()
24 {
25     auto timePoint = std::chrono::steady_clock::now();
26     return std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch()).count();
27 }
28 
GetTimestampMicro()29 uint64_t SteadyClock::GetTimestampMicro()
30 {
31     auto timePoint = std::chrono::steady_clock::now();
32     return std::chrono::duration_cast<std::chrono::microseconds>(timePoint.time_since_epoch()).count();
33 }
34 
GetElapsedTimeMs(uint64_t startMs)35 uint64_t SteadyClock::GetElapsedTimeMs(uint64_t startMs)
36 {
37     auto currTime = SteadyClock::GetTimestampMilli();
38     uint64_t diff = 0;
39     if (currTime > startMs) {
40         diff = currTime - startMs;
41     } else {
42         DP_ERR_LOG("invalid parameter, startMs: %{public}d, currTime: %{public}d", static_cast<int>(startMs),
43             static_cast<int>(currTime));
44     }
45     return diff;
46 }
47 
GetRemainingTimeMs(uint64_t expirationTimeMs)48 std::chrono::milliseconds SteadyClock::GetRemainingTimeMs(uint64_t expirationTimeMs)
49 {
50     auto currTime = SteadyClock::GetTimestampMilli();
51     uint64_t remainingTimeMs = (expirationTimeMs > currTime) ? (expirationTimeMs - currTime) : 0;
52     DP_DEBUG_LOG("expirationTimeMs: %{public}d, currTime: %{public}d, remainingTime: %{public}d",
53         static_cast<int>(expirationTimeMs), static_cast<int>(currTime), static_cast<int>(remainingTimeMs));
54     return std::chrono::milliseconds(remainingTimeMs);
55 }
56 
SteadyClock()57 SteadyClock::SteadyClock() : start_(std::chrono::steady_clock::now())
58 {
59     DP_DEBUG_LOG("entered.");
60 }
61 
Reset()62 void SteadyClock::Reset()
63 {
64     DP_DEBUG_LOG("entered.");
65     start_ = std::chrono::steady_clock::now();
66     return;
67 }
68 
GetElapsedTimeMs()69 uint64_t SteadyClock::GetElapsedTimeMs()
70 {
71     DP_DEBUG_LOG("entered.");
72     constexpr int timescale = 1000;
73     auto end = std::chrono::steady_clock::now();
74     return static_cast<uint64_t>(std::chrono::duration<double>(end - start_).count() * timescale);
75 }
76 } //namespace DeferredProcessing
77 } // namespace CameraStandard
78 } // namespace OHOS
79