1 /*
2 * Copyright (c) 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 "feature/hyper_graphic_manager/rs_frame_rate_linker.h"
17
18 #include "command/rs_frame_rate_linker_command.h"
19 #include "platform/common/rs_log.h"
20 #include "sandbox_utils.h"
21 #include "transaction/rs_transaction_proxy.h"
22 #include "transaction/rs_interfaces.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 static bool g_isUniRenderEnabled = false;
GenerateId()27 FrameRateLinkerId RSFrameRateLinker::GenerateId()
28 {
29 static pid_t pid_ = GetRealPid();
30 static std::atomic<uint32_t> currentId_ = 0;
31
32 auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
33 if (currentId == UINT32_MAX) {
34 ROSEN_LOGE("RSFrameRateLinker Id overflow");
35 }
36
37 // concat two 32-bit numbers to one 64-bit number
38 return ((FrameRateLinkerId)pid_ << 32) | (currentId);
39 }
40
Create()41 std::shared_ptr<RSFrameRateLinker> RSFrameRateLinker::Create()
42 {
43 auto linker = std::make_shared<RSFrameRateLinker>();
44 ROSEN_LOGI("RSFrameRateLinker::Create id: %{public}" PRIu64, linker->GetId());
45 return linker;
46 }
47
RSFrameRateLinker()48 RSFrameRateLinker::RSFrameRateLinker() : id_(GenerateId())
49 {
50 InitUniRenderEnabled();
51 }
52
~RSFrameRateLinker()53 RSFrameRateLinker::~RSFrameRateLinker()
54 {
55 // tell RT/RS to destroy related frameRateLinker
56 std::unique_ptr<RSCommand> command = std::make_unique<RSFrameRateLinkerDestroy>(id_);
57 AddCommand(command, IsUniRenderEnabled());
58
59 auto renderServiceClient =
60 std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
61 if (renderServiceClient == nullptr) {
62 ROSEN_LOGE("RSFrameRateLinker renderServiceClient is nullptr!");
63 return;
64 }
65 renderServiceClient->UnregisterFrameRateLinker(id_);
66 ROSEN_LOGI("RSFrameRateLinker::Destroy id: %{public}" PRIu64, id_);
67 }
68
GetId() const69 FrameRateLinkerId RSFrameRateLinker::GetId() const
70 {
71 return id_;
72 }
73
IsUniRenderEnabled() const74 bool RSFrameRateLinker::IsUniRenderEnabled() const
75 {
76 return g_isUniRenderEnabled;
77 }
78
UpdateFrameRateRange(const FrameRateRange & range,int32_t animatorExpectedFrameRate,std::shared_ptr<RSUIContext> rsUIContext)79 void RSFrameRateLinker::UpdateFrameRateRange(
80 const FrameRateRange& range, int32_t animatorExpectedFrameRate, std::shared_ptr<RSUIContext> rsUIContext)
81 {
82 rsUIContext_ = rsUIContext;
83 if (currentRange_ != range || currAnimatorExpectedFrameRate_ != animatorExpectedFrameRate) {
84 currentRange_ = range;
85 currAnimatorExpectedFrameRate_ = animatorExpectedFrameRate;
86 std::unique_ptr<RSCommand> command = std::make_unique<RSFrameRateLinkerUpdateRange>(GetId(),
87 range, animatorExpectedFrameRate);
88 AddCommand(command, IsUniRenderEnabled());
89 }
90 }
91
UpdateFrameRateRangeImme(const FrameRateRange & range,int32_t animatorExpectedFrameRate)92 void RSFrameRateLinker::UpdateFrameRateRangeImme(const FrameRateRange& range, int32_t animatorExpectedFrameRate)
93 {
94 if (currentRange_ != range || currAnimatorExpectedFrameRate_ != animatorExpectedFrameRate) {
95 currentRange_ = range;
96 currAnimatorExpectedFrameRate_ = animatorExpectedFrameRate;
97 RSInterfaces::GetInstance().SyncFrameRateRange(GetId(), range, animatorExpectedFrameRate);
98 }
99 }
100
SetEnable(bool enabled)101 void RSFrameRateLinker::SetEnable(bool enabled)
102 {
103 isEnabled_ = enabled;
104 }
105
AddCommand(std::unique_ptr<RSCommand> & command,bool isRenderServiceCommand)106 void RSFrameRateLinker::AddCommand(std::unique_ptr<RSCommand>& command, bool isRenderServiceCommand)
107 {
108 auto rsUIContext = rsUIContext_.lock();
109 if (rsUIContext != nullptr) {
110 auto transaction = rsUIContext->GetRSTransaction();
111 transaction->AddCommand(command, IsUniRenderEnabled());
112 } else {
113 auto transactionProxy = RSTransactionProxy::GetInstance();
114 if (transactionProxy != nullptr) {
115 transactionProxy->AddCommand(command, IsUniRenderEnabled());
116 }
117 }
118 }
119
IsEnable()120 bool RSFrameRateLinker::IsEnable()
121 {
122 return isEnabled_;
123 }
124
InitUniRenderEnabled()125 void RSFrameRateLinker::InitUniRenderEnabled()
126 {
127 static bool inited = false;
128 if (!inited) {
129 inited = true;
130 g_isUniRenderEnabled = RSSystemProperties::GetUniRenderEnabled();
131 ROSEN_LOGD("RSFrameRateLinker::InitUniRenderEnabled:%{public}d", g_isUniRenderEnabled);
132 }
133 }
134 } // namespace Rosen
135 } // namespace OHOS
136