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 "rs_qos_thread.h"
17
18 #include <vector>
19
20 #include "vsync_distributor.h"
21 #include "platform/common/rs_innovation.h"
22
23 namespace OHOS::Rosen {
24 std::once_flag RSQosThread::flag_;
25 RSQosThread* RSQosThread::instance_ = nullptr;
26 sptr<VSyncDistributor> RSQosThread::appVSyncDistributor_ = nullptr;
27
GetInstance()28 RSQosThread* RSQosThread::GetInstance()
29 {
30 std::call_once(flag_, &RSQosThread::Init);
31 return instance_;
32 }
33
Init()34 void RSQosThread::Init()
35 {
36 instance_ = new RSQosThread();
37 if (!RSInnovation::_s_qosVsyncFuncLoaded) {
38 return;
39 }
40
41 using CreateQosServiceFunc = void* (*)();
42 using QosSetBoundaryRateFunc = void* (*)(int, int);
43 using QosSetRegisteFuncCBFunc =
44 void* (*)(std::function<void(uint32_t, int)>,
45 std::function<void(std::vector<std::pair<uint32_t, int> >&)>);
46
47 auto CreateQosService = (CreateQosServiceFunc)RSInnovation::_s_createRSQosService;
48 auto QosSetBoundaryRate = (QosSetBoundaryRateFunc)RSInnovation::_s_qosSetBoundaryRate;
49 auto QosRegisteFuncCB = (QosSetRegisteFuncCBFunc)RSInnovation::_s_qosRegisteFuncCB;
50
51 QosSetBoundaryRate(MIN_RATE, MAX_RATE);
52 QosRegisteFuncCB(SetQosVSyncRate, GetQosVSyncRateInfos);
53 CreateQosService();
54 }
55
ThreadStart()56 void RSQosThread::ThreadStart()
57 {
58 if (!RSInnovation::_s_qosVsyncFuncLoaded) {
59 return;
60 }
61
62 using QosTreadStartFunc = void* (*)();
63 auto QosTreadStart = (QosTreadStartFunc)RSInnovation::_s_qosThreadStart;
64 QosTreadStart();
65 }
66
ThreadStop()67 void RSQosThread::ThreadStop()
68 {
69 if (!RSInnovation::_s_qosVsyncFuncLoaded) {
70 return;
71 }
72
73 using QosTreadStopFunc = void* (*)();
74 auto QosTreadStop = (QosTreadStopFunc)RSInnovation::_s_qosThreadStop;
75 QosTreadStop();
76 }
77
GetQosVSyncRateInfos(std::vector<std::pair<uint32_t,int32_t>> & appsRateVec)78 void RSQosThread::GetQosVSyncRateInfos(std::vector<std::pair<uint32_t, int32_t>>& appsRateVec)
79 {
80 if (appVSyncDistributor_ == nullptr) {
81 return;
82 }
83
84 appVSyncDistributor_->GetQosVSyncRateInfos(appsRateVec);
85 }
86
SetQosVSyncRate(uint32_t pid,int32_t rate)87 void RSQosThread::SetQosVSyncRate(uint32_t pid, int32_t rate)
88 {
89 if (appVSyncDistributor_ == nullptr) {
90 return;
91 }
92
93 appVSyncDistributor_->SetQosVSyncRate(pid, rate);
94 }
95
ResetQosPid()96 void RSQosThread::ResetQosPid()
97 {
98 using QosOnRSResetPidFunc = void* (*)();
99
100 auto QosOnRSResetPid = (QosOnRSResetPidFunc)RSInnovation::_s_qosOnRSResetPid;
101 QosOnRSResetPid();
102 }
103
OnRSVisibilityChangeCB(std::map<uint32_t,bool> & pidVisMap)104 void RSQosThread::OnRSVisibilityChangeCB(std::map<uint32_t, bool>& pidVisMap)
105 {
106 if (!qosCal_) {
107 return;
108 }
109
110 using QosOnRSVisibilityChangeCBFunc = void (*)(std::map<uint32_t, bool>&);
111
112 auto QosOnRSVisibilityChangeCB = (QosOnRSVisibilityChangeCBFunc)RSInnovation::_s_qosOnRSVisibilityChangeCB;
113 QosOnRSVisibilityChangeCB(pidVisMap);
114 }
115 } // amespace OHOS::Rosen