1 /*
2 * Copyright (c) 2021-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 "pipeline/rs_render_frame_rate_linker.h"
17
18 #include "ipc_callbacks/rs_iframe_rate_linker_expected_fps_update_callback.h"
19 #include "platform/common/rs_log.h"
20 #include "sandbox_utils.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 const std::chrono::steady_clock::duration NATIVE_VSYNC_FALLBACK_INTERVAL =
25 std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::milliseconds(200));
GenerateId()26 FrameRateLinkerId RSRenderFrameRateLinker::GenerateId()
27 {
28 static pid_t pid_ = GetRealPid();
29 static std::atomic<uint32_t> currentId_ = 0;
30
31 auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
32 if (currentId == UINT32_MAX) {
33 ROSEN_LOGE("RSRenderFrameRateLinker Id overflow");
34 }
35
36 // concat two 32-bit numbers to one 64-bit number
37 return ((FrameRateLinkerId)pid_ << 32) | (currentId);
38 }
39
RSRenderFrameRateLinker(FrameRateLinkerId id,ObserverType observer)40 RSRenderFrameRateLinker::RSRenderFrameRateLinker(FrameRateLinkerId id, ObserverType observer)
41 : id_(id), observer_(observer)
42 {
43 Notify();
44 }
45
RSRenderFrameRateLinker(ObserverType observer)46 RSRenderFrameRateLinker::RSRenderFrameRateLinker(ObserverType observer)
47 : RSRenderFrameRateLinker(GenerateId(), observer) {}
48
RSRenderFrameRateLinker(FrameRateLinkerId id)49 RSRenderFrameRateLinker::RSRenderFrameRateLinker(FrameRateLinkerId id) : RSRenderFrameRateLinker(id, nullptr) {}
50
RSRenderFrameRateLinker()51 RSRenderFrameRateLinker::RSRenderFrameRateLinker() : RSRenderFrameRateLinker(GenerateId(), nullptr) {}
52
RSRenderFrameRateLinker(const RSRenderFrameRateLinker & other)53 RSRenderFrameRateLinker::RSRenderFrameRateLinker(const RSRenderFrameRateLinker& other)
54 {
55 Copy(std::move(other));
56 }
57
RSRenderFrameRateLinker(const RSRenderFrameRateLinker && other)58 RSRenderFrameRateLinker::RSRenderFrameRateLinker(const RSRenderFrameRateLinker&& other)
59 {
60 Copy(std::move(other));
61 }
62
operator =(const RSRenderFrameRateLinker & other)63 RSRenderFrameRateLinker& RSRenderFrameRateLinker::operator=(const RSRenderFrameRateLinker& other)
64 {
65 Copy(std::move(other));
66 return *this;
67 }
68
operator =(const RSRenderFrameRateLinker && other)69 RSRenderFrameRateLinker& RSRenderFrameRateLinker::operator=(const RSRenderFrameRateLinker&& other)
70 {
71 Copy(std::move(other));
72 return *this;
73 }
74
~RSRenderFrameRateLinker()75 RSRenderFrameRateLinker::~RSRenderFrameRateLinker()
76 {
77 Notify();
78 }
79
SetExpectedRange(const FrameRateRange & range)80 void RSRenderFrameRateLinker::SetExpectedRange(const FrameRateRange& range)
81 {
82 std::lock_guard<std::mutex> lock(mutex_);
83 if (expectedRange_.preferred_ != range.preferred_) {
84 for (auto& [_, cb] : expectedFpsChangeCallbacks_) {
85 if (cb) {
86 cb->OnFrameRateLinkerExpectedFpsUpdate(ExtractPid(id_), xcomponentId_, range.preferred_);
87 }
88 }
89 }
90
91 if (expectedRange_ != range) {
92 expectedRange_ = range;
93 Notify();
94 }
95 }
96
GetExpectedRange() const97 const FrameRateRange& RSRenderFrameRateLinker::GetExpectedRange() const
98 {
99 std::lock_guard<std::mutex> lock(mutex_);
100 return expectedRange_;
101 }
102
SetFrameRate(uint32_t rate)103 void RSRenderFrameRateLinker::SetFrameRate(uint32_t rate)
104 {
105 std::lock_guard<std::mutex> lock(mutex_);
106 if (frameRate_ != rate) {
107 frameRate_ = rate;
108 Notify();
109 }
110 }
111
GetFrameRate() const112 uint32_t RSRenderFrameRateLinker::GetFrameRate() const
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 return frameRate_;
116 }
117
SetAnimatorExpectedFrameRate(int32_t animatorExpectedFrameRate)118 void RSRenderFrameRateLinker::SetAnimatorExpectedFrameRate(int32_t animatorExpectedFrameRate)
119 {
120 if (animatorExpectedFrameRate_ != animatorExpectedFrameRate) {
121 animatorExpectedFrameRate_ = animatorExpectedFrameRate;
122 Notify();
123 }
124 }
125
SetVsyncName(const std::string & vsyncName)126 void RSRenderFrameRateLinker::SetVsyncName(const std::string& vsyncName)
127 {
128 vsyncName_ = vsyncName;
129 }
130
SetWindowNodeId(uint64_t windowNodeId)131 void RSRenderFrameRateLinker::SetWindowNodeId(uint64_t windowNodeId)
132 {
133 windowNodeId_ = windowNodeId;
134 }
135
Copy(const RSRenderFrameRateLinker && other)136 void RSRenderFrameRateLinker::Copy(const RSRenderFrameRateLinker&& other)
137 {
138 id_ = other.id_;
139 expectedRange_ = other.expectedRange_;
140 frameRate_ = other.frameRate_;
141 animatorExpectedFrameRate_ = other.animatorExpectedFrameRate_;
142 }
143
Notify()144 void RSRenderFrameRateLinker::Notify()
145 {
146 if (observer_ != nullptr) {
147 observer_(*this);
148 }
149 }
150
RegisterExpectedFpsUpdateCallback(pid_t listener,sptr<RSIFrameRateLinkerExpectedFpsUpdateCallback> callback)151 void RSRenderFrameRateLinker::RegisterExpectedFpsUpdateCallback(pid_t listener,
152 sptr<RSIFrameRateLinkerExpectedFpsUpdateCallback> callback)
153 {
154 if (callback == nullptr) {
155 expectedFpsChangeCallbacks_.erase(listener);
156 return;
157 }
158
159 // if this listener has registered a callback before, replace it.
160 expectedFpsChangeCallbacks_[listener] = callback;
161 callback->OnFrameRateLinkerExpectedFpsUpdate(ExtractPid(id_), xcomponentId_, expectedRange_.preferred_);
162 }
163
UpdateNativeVSyncTimePoint()164 void RSRenderFrameRateLinker::UpdateNativeVSyncTimePoint()
165 {
166 nativeVSyncTimePoint_.store(std::chrono::steady_clock::now());
167 }
168
NativeVSyncIsTimeOut() const169 bool RSRenderFrameRateLinker::NativeVSyncIsTimeOut() const
170 {
171 return std::chrono::steady_clock::now() - nativeVSyncTimePoint_.load() > NATIVE_VSYNC_FALLBACK_INTERVAL;
172 }
173 } // namespace Rosen
174 } // namespace OHOS
175