1 /*
2 * Copyright (c) 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 #include "idle_time.h"
17
18 #include "hilog_wrapper.h"
19 #include "transaction/rs_interfaces.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 constexpr int64_t PERIOD = 16666666; // ns
25 constexpr int64_t MS_PER_NS = 1000000;
26 constexpr int32_t TRY_COUNT_MAX = 6;
27 constexpr int32_t DEVIATION_MIN = 1000; // ns
28 std::shared_ptr<Rosen::VSyncReceiver> receiver_ = nullptr;
29 }
30
IdleTime(const std::shared_ptr<EventHandler> & eventHandler,const std::shared_ptr<NativeEngine> & nativeEngine)31 IdleTime::IdleTime(const std::shared_ptr<EventHandler> &eventHandler, const std::shared_ptr<NativeEngine> &nativeEngine)
32 {
33 eventHandler_ = eventHandler;
34 nativeEngine_ = nativeEngine;
35 }
36
GetSysTimeNs()37 int64_t IdleTime::GetSysTimeNs()
38 {
39 auto now = std::chrono::steady_clock::now().time_since_epoch();
40 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
41 }
42
OnVSync(int64_t timestamp,void * client)43 void IdleTime::OnVSync(int64_t timestamp, void* client)
44 {
45 // only use for 60HZ
46 if (continueFailCount_ > TRY_COUNT_MAX) {
47 HILOG_ERROR("Only support 60HZ.");
48 return;
49 }
50 int64_t period = timestamp - firstVSyncTime_;
51 int64_t lastPeriod = period_ == 0 ? PERIOD : period_;
52 int64_t deviation = (period - lastPeriod) > 0 ? period - lastPeriod : lastPeriod - period;
53 if (deviation > MS_PER_NS) { // deviation greater than 1ms
54 RequestVSync();
55 continueFailCount_++;
56 HILOG_DEBUG("fail count is %{public}d, timestamp is %{public}" PRId64 ", period is %{public}" PRId64,
57 continueFailCount_, timestamp, period);
58 } else {
59 if (eventHandler_ == nullptr) {
60 HILOG_ERROR("eventHandler_ is nullptr.");
61 return;
62 }
63 deviation = deviation < DEVIATION_MIN ? DEVIATION_MIN : deviation;
64 int64_t timeOut = lastPeriod / deviation; // up to 16666ms : MS_PER_NS / deviation * lastPeriod / MS_PER_NS
65 std::weak_ptr<IdleTime> weak(shared_from_this());
66 auto task = [weak]() {
67 auto idleTime = weak.lock();
68 if (idleTime == nullptr) {
69 HILOG_ERROR("idleTime is nullptr.");
70 return;
71 }
72 idleTime->RequestVSync();
73 };
74 eventHandler_->PostTask(task, timeOut);
75 if (successCount_ > TRY_COUNT_MAX) {
76 period_ = (period & lastPeriod) + ((period ^ lastPeriod) >> 1); // average
77 } else {
78 period_ = PERIOD;
79 successCount_++;
80 HILOG_DEBUG("fail count is %{public}d, timestamp is %{public}" PRId64 ", period is %{public}" PRId64,
81 continueFailCount_, timestamp, period);
82 }
83 continueFailCount_ = 0;
84 }
85
86 firstVSyncTime_ = timestamp;
87 }
88
RequestVSync()89 void IdleTime::RequestVSync()
90 {
91 if (receiver_ == nullptr) {
92 auto& rsClient = Rosen::RSInterfaces::GetInstance();
93 receiver_ = rsClient.CreateVSyncReceiver("ABILITY");
94 if (receiver_ == nullptr) {
95 HILOG_ERROR("Create VSync receiver failed.");
96 return;
97 }
98 receiver_->Init();
99 }
100 std::weak_ptr<IdleTime> weak(shared_from_this());
101 auto task = [weak](int64_t timestamp, void* data) {
102 auto idleTime = weak.lock();
103 if (idleTime == nullptr) {
104 HILOG_ERROR("idleTime is nullptr.");
105 return;
106 }
107 idleTime->OnVSync(timestamp, data);
108 };
109 Rosen::VSyncReceiver::FrameCallback frameCallback = {
110 .userData_ = this,
111 .callback_ = task,
112 };
113 receiver_->RequestNextVSync(frameCallback);
114 }
115
EventTask()116 void IdleTime::EventTask()
117 {
118 if (firstVSyncTime_ == 0 || period_ == 0) {
119 PostTask();
120 HILOG_ERROR("no VSync occur.");
121 return;
122 }
123 int64_t period = period_;
124
125 int64_t occurTimestamp = GetSysTimeNs();
126 int64_t numPeriod = (occurTimestamp - firstVSyncTime_) / period;
127 int64_t lastVSyncTime = numPeriod * period + firstVSyncTime_;
128 int64_t elapsedTime = occurTimestamp - lastVSyncTime;
129 int64_t idleTime = period - elapsedTime;
130 // set runtime
131 if (nativeEngine_ == nullptr) {
132 HILOG_ERROR("nativeEngine_ is nullptr.");
133 return;
134 }
135 nativeEngine_->NotifyIdleTime(idleTime / MS_PER_NS);
136 PostTask();
137 }
138
PostTask()139 void IdleTime::PostTask()
140 {
141 if (continueFailCount_ > TRY_COUNT_MAX) {
142 HILOG_ERROR("Only support 60HZ.");
143 return;
144 }
145
146 if (eventHandler_ == nullptr) {
147 HILOG_ERROR("eventHandler_ is nullptr.");
148 return;
149 }
150 std::weak_ptr<IdleTime> weak(shared_from_this());
151 auto task = [weak]() {
152 auto idleTime = weak.lock();
153 if (idleTime == nullptr) {
154 HILOG_ERROR("idleTime is nullptr.");
155 return;
156 }
157 idleTime->EventTask();
158 };
159 eventHandler_->PostTask(task, EventQueue::Priority::IDLE);
160 }
161
Start()162 void IdleTime::Start()
163 {
164 RequestVSync();
165 PostTask();
166 }
167 } // AppExecFwk
168 } // namespace OHOS