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