• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hgm_vsync_generator_controller.h"
17 #include <cstdint>
18 
19 #include "hgm_core.h"
20 #include "hgm_log.h"
21 #include "rs_trace.h"
22 
23 namespace OHOS {
24 namespace Rosen {
HgmVSyncGeneratorController(sptr<VSyncController> rsController,sptr<VSyncController> appController,sptr<VSyncGenerator> vsyncGenerator)25 HgmVSyncGeneratorController::HgmVSyncGeneratorController(sptr<VSyncController> rsController,
26     sptr<VSyncController> appController, sptr<VSyncGenerator> vsyncGenerator)
27     : rsController_(rsController),
28     appController_(appController),
29     vsyncGenerator_(vsyncGenerator)
30 {
31 }
32 
~HgmVSyncGeneratorController()33 HgmVSyncGeneratorController::~HgmVSyncGeneratorController() {}
34 
GetAppOffset(const uint32_t controllerRate)35 int32_t HgmVSyncGeneratorController::GetAppOffset(const uint32_t controllerRate)
36 {
37     auto alignRate = HgmCore::Instance().GetAlignRate();
38     auto supportedMaxTE = HgmCore::Instance().GetSupportedMaxTE();
39     if (alignRate == 0 || controllerRate == 0) {
40         HGM_LOGE("HgmVSyncGeneratorController::GetAppOffset illegal alignRate or controllerRate");
41         return 0;
42     }
43     if (alignRate < controllerRate) {
44         return 0;
45     }
46     return static_cast<int32_t>(supportedMaxTE / controllerRate - supportedMaxTE / alignRate);
47 }
48 
ChangeGeneratorRate(const uint32_t controllerRate,const std::vector<std::pair<FrameRateLinkerId,uint32_t>> & appData)49 void HgmVSyncGeneratorController::ChangeGeneratorRate(const uint32_t controllerRate,
50     const std::vector<std::pair<FrameRateLinkerId, uint32_t>>& appData)
51 {
52     int32_t pulseNum = GetAppOffset(controllerRate);
53 
54     VSyncGenerator::ListenerRefreshRateData listenerRate = {.cb = appController_, .refreshRates = appData};
55     VSyncGenerator::ListenerPhaseOffsetData listenerPhase;
56 
57     if (currentRate_ != controllerRate) {
58         HGM_LOGI("HgmVSyncGeneratorController::ChangeGeneratorRate controllerRate = %{public}d, appOffset = %{public}d",
59             controllerRate, pulseNum);
60         RS_TRACE_NAME("HgmVSyncGeneratorController::ChangeGeneratorRate controllerRate: " +
61             std::to_string(controllerRate) + ", appOffset: " + std::to_string(pulseNum));
62         listenerPhase.cb = appController_;
63         listenerPhase.phaseByPulseNum = pulseNum;
64         vsyncGenerator_->ChangeGeneratorRefreshRateModel(listenerRate, listenerPhase, controllerRate);
65         currentOffset_ = vsyncGenerator_->GetVSyncPulse() * pulseNum;
66         currentRate_ = controllerRate;
67     } else {
68         vsyncGenerator_->ChangeGeneratorRefreshRateModel(listenerRate, listenerPhase, controllerRate);
69     }
70 }
71 
GetCurrentOffset() const72 int64_t HgmVSyncGeneratorController::GetCurrentOffset() const
73 {
74     return currentOffset_;
75 }
76 
GetCurrentRate() const77 uint32_t HgmVSyncGeneratorController::GetCurrentRate() const
78 {
79     return currentRate_;
80 }
81 } // namespace Rosen
82 } // namespace OHOS