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 {
GenerateId()24 FrameRateLinkerId RSRenderFrameRateLinker::GenerateId()
25 {
26 static pid_t pid_ = GetRealPid();
27 static std::atomic<uint32_t> currentId_ = 0;
28
29 auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
30 if (currentId == UINT32_MAX) {
31 ROSEN_LOGE("RSRenderFrameRateLinker Id overflow");
32 }
33
34 // concat two 32-bit numbers to one 64-bit number
35 return ((FrameRateLinkerId)pid_ << 32) | (currentId);
36 }
37
RSRenderFrameRateLinker(FrameRateLinkerId id,ObserverType observer)38 RSRenderFrameRateLinker::RSRenderFrameRateLinker(FrameRateLinkerId id, ObserverType observer)
39 : id_(id), observer_(observer)
40 {
41 Notify();
42 }
43
RSRenderFrameRateLinker(ObserverType observer)44 RSRenderFrameRateLinker::RSRenderFrameRateLinker(ObserverType observer)
45 : RSRenderFrameRateLinker(GenerateId(), observer) {}
46
RSRenderFrameRateLinker(FrameRateLinkerId id)47 RSRenderFrameRateLinker::RSRenderFrameRateLinker(FrameRateLinkerId id) : RSRenderFrameRateLinker(id, nullptr) {}
48
RSRenderFrameRateLinker()49 RSRenderFrameRateLinker::RSRenderFrameRateLinker() : RSRenderFrameRateLinker(GenerateId(), nullptr) {}
50
RSRenderFrameRateLinker(const RSRenderFrameRateLinker & other)51 RSRenderFrameRateLinker::RSRenderFrameRateLinker(const RSRenderFrameRateLinker& other)
52 {
53 Copy(std::move(other));
54 }
55
RSRenderFrameRateLinker(const RSRenderFrameRateLinker && other)56 RSRenderFrameRateLinker::RSRenderFrameRateLinker(const RSRenderFrameRateLinker&& other)
57 {
58 Copy(std::move(other));
59 }
60
operator =(const RSRenderFrameRateLinker & other)61 RSRenderFrameRateLinker& RSRenderFrameRateLinker::operator=(const RSRenderFrameRateLinker& other)
62 {
63 Copy(std::move(other));
64 return *this;
65 }
66
operator =(const RSRenderFrameRateLinker && other)67 RSRenderFrameRateLinker& RSRenderFrameRateLinker::operator=(const RSRenderFrameRateLinker&& other)
68 {
69 Copy(std::move(other));
70 return *this;
71 }
72
~RSRenderFrameRateLinker()73 RSRenderFrameRateLinker::~RSRenderFrameRateLinker()
74 {
75 Notify();
76 }
77
SetExpectedRange(const FrameRateRange & range)78 void RSRenderFrameRateLinker::SetExpectedRange(const FrameRateRange& range)
79 {
80 std::lock_guard<std::mutex> lock(mutex_);
81 if (expectedRange_.preferred_ != range.preferred_) {
82 for (auto& [_, cb] : expectedFpsChangeCallbacks_) {
83 if (cb) {
84 cb->OnFrameRateLinkerExpectedFpsUpdate(ExtractPid(id_), range.preferred_);
85 }
86 }
87 }
88
89 if (expectedRange_ != range) {
90 expectedRange_ = range;
91 Notify();
92 }
93 }
94
GetExpectedRange() const95 const FrameRateRange& RSRenderFrameRateLinker::GetExpectedRange() const
96 {
97 std::lock_guard<std::mutex> lock(mutex_);
98 return expectedRange_;
99 }
100
SetFrameRate(uint32_t rate)101 void RSRenderFrameRateLinker::SetFrameRate(uint32_t rate)
102 {
103 std::lock_guard<std::mutex> lock(mutex_);
104 if (frameRate_ != rate) {
105 frameRate_ = rate;
106 Notify();
107 }
108 }
109
GetFrameRate() const110 uint32_t RSRenderFrameRateLinker::GetFrameRate() const
111 {
112 std::lock_guard<std::mutex> lock(mutex_);
113 return frameRate_;
114 }
115
SetAnimatorExpectedFrameRate(int32_t animatorExpectedFrameRate)116 void RSRenderFrameRateLinker::SetAnimatorExpectedFrameRate(int32_t animatorExpectedFrameRate)
117 {
118 if (animatorExpectedFrameRate_ != animatorExpectedFrameRate) {
119 animatorExpectedFrameRate_ = animatorExpectedFrameRate;
120 Notify();
121 }
122 }
123
SetVsyncName(const std::string & vsyncName)124 void RSRenderFrameRateLinker::SetVsyncName(const std::string& vsyncName)
125 {
126 vsyncName_ = vsyncName;
127 }
128
Copy(const RSRenderFrameRateLinker && other)129 void RSRenderFrameRateLinker::Copy(const RSRenderFrameRateLinker&& other)
130 {
131 id_ = other.id_;
132 expectedRange_ = other.expectedRange_;
133 frameRate_ = other.frameRate_;
134 animatorExpectedFrameRate_ = other.animatorExpectedFrameRate_;
135 }
136
Notify()137 void RSRenderFrameRateLinker::Notify()
138 {
139 if (observer_ != nullptr) {
140 observer_(*this);
141 }
142 }
143
RegisterExpectedFpsUpdateCallback(pid_t listener,sptr<RSIFrameRateLinkerExpectedFpsUpdateCallback> callback)144 void RSRenderFrameRateLinker::RegisterExpectedFpsUpdateCallback(pid_t listener,
145 sptr<RSIFrameRateLinkerExpectedFpsUpdateCallback> callback)
146 {
147 if (callback == nullptr) {
148 expectedFpsChangeCallbacks_.erase(listener);
149 return;
150 }
151
152 // if this listener has registered a callback before, replace it.
153 expectedFpsChangeCallbacks_[listener] = callback;
154 callback->OnFrameRateLinkerExpectedFpsUpdate(ExtractPid(id_), expectedRange_.preferred_);
155 }
156
157 } // namespace Rosen
158 } // namespace OHOS
159