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 {
25 namespace {
26     constexpr uint32_t TARGET_TIME_TRIGGER_PULSE_NUM = 6;
27 }
HgmVSyncGeneratorController(sptr<VSyncController> rsController,sptr<VSyncController> appController,sptr<VSyncGenerator> vsyncGenerator)28 HgmVSyncGeneratorController::HgmVSyncGeneratorController(sptr<VSyncController> rsController,
29     sptr<VSyncController> appController, sptr<VSyncGenerator> vsyncGenerator)
30     : rsController_(rsController),
31     appController_(appController),
32     vsyncGenerator_(vsyncGenerator)
33 {
34 }
35 
~HgmVSyncGeneratorController()36 HgmVSyncGeneratorController::~HgmVSyncGeneratorController() {}
37 
GetAppOffset(const uint32_t controllerRate)38 int32_t HgmVSyncGeneratorController::GetAppOffset(const uint32_t controllerRate)
39 {
40     auto alignRate = HgmCore::Instance().GetAlignRate();
41     auto supportedMaxTE = HgmCore::Instance().GetSupportedMaxTE();
42     if (alignRate == 0 || controllerRate == 0) {
43         HGM_LOGE("HgmVSyncGeneratorController::GetAppOffset illegal alignRate or controllerRate");
44         return 0;
45     }
46     if (alignRate < controllerRate) {
47         return 0;
48     }
49     return static_cast<int32_t>(supportedMaxTE / controllerRate - supportedMaxTE / alignRate);
50 }
51 
CalcVSyncQuickTriggerTime(uint64_t lastVSyncTime,uint32_t lastRate)52 uint64_t HgmVSyncGeneratorController::CalcVSyncQuickTriggerTime(uint64_t lastVSyncTime, uint32_t lastRate)
53 {
54     if (lastRate == 0) {
55         HGM_LOGE("HgmVSyncGeneratorController::CalcVSyncQuickTriggerTime illegal param");
56         return 0;
57     }
58     if (vsyncGenerator_ == nullptr) {
59         HGM_LOGE("HgmVSyncGeneratorController::vsyncGenerator is nullptr");
60         return 0;
61     }
62     uint32_t maxPluseNum = HgmCore::Instance().GetSupportedMaxTE() / lastRate;
63 
64     uint64_t pulse = vsyncGenerator_->GetVSyncPulse() >= 0 ?
65                      static_cast<uint64_t>(vsyncGenerator_->GetVSyncPulse()) : 0;
66     uint64_t targetTime = lastVSyncTime + pulse * TARGET_TIME_TRIGGER_PULSE_NUM;
67     uint64_t currTime = static_cast<uint64_t>(
68         std::chrono::duration_cast<std::chrono::nanoseconds>(
69             std::chrono::steady_clock::now().time_since_epoch()).count()) + pulse;
70     targetTime = targetTime > currTime ? targetTime : currTime;
71     uint64_t threshodTime = lastVSyncTime + pulse * (maxPluseNum - 1); // threshod value
72     if (targetTime > threshodTime) {
73         return 0;
74     }
75     return targetTime;
76 }
77 
ChangeGeneratorRate(const uint32_t controllerRate,const std::vector<std::pair<FrameRateLinkerId,uint32_t>> & appData,uint64_t targetTime,bool isNeedUpdateAppOffset)78 void HgmVSyncGeneratorController::ChangeGeneratorRate(const uint32_t controllerRate,
79     const std::vector<std::pair<FrameRateLinkerId, uint32_t>>& appData, uint64_t targetTime, bool isNeedUpdateAppOffset)
80 {
81     if (vsyncGenerator_ == nullptr) {
82         HGM_LOGE("HgmVSyncGeneratorController::vsyncGenerator is nullptr");
83         return;
84     }
85     int pulseNum;
86     if (isNeedUpdateAppOffset && controllerRate == OLED_60_HZ) {
87         pulseNum = 0;
88     } else {
89         pulseNum = GetAppOffset(controllerRate);
90     }
91 
92     VSyncGenerator::ListenerRefreshRateData listenerRate = {.cb = appController_, .refreshRates = appData};
93     VSyncGenerator::ListenerPhaseOffsetData listenerPhase;
94 
95     if (currentRate_ != controllerRate) {
96         HGM_LOGI("HgmVSyncGeneratorController::ChangeGeneratorRate controllerRate = %{public}d, appOffset = %{public}d",
97             controllerRate, pulseNum);
98         RS_TRACE_NAME("HgmVSyncGeneratorController::ChangeGeneratorRate controllerRate: " +
99             std::to_string(controllerRate) + ", appOffset: " + std::to_string(pulseNum) +
100             ", nextVSyncTargetTime =" + std::to_string(targetTime));
101         listenerPhase.cb = appController_;
102         listenerPhase.phaseByPulseNum = pulseNum;
103         vsyncGenerator_->ChangeGeneratorRefreshRateModel(listenerRate, listenerPhase, controllerRate, targetTime);
104         currentOffset_ = vsyncGenerator_->GetVSyncPulse() * pulseNum;
105         currentRate_ = controllerRate;
106     } else {
107         if (isNeedUpdateAppOffset) {
108             listenerPhase.phaseByPulseNum = pulseNum;
109         }
110         vsyncGenerator_->ChangeGeneratorRefreshRateModel(listenerRate, listenerPhase, controllerRate);
111     }
112 }
113 
GetCurrentOffset() const114 int64_t HgmVSyncGeneratorController::GetCurrentOffset() const
115 {
116     return currentOffset_;
117 }
118 
GetCurrentRate() const119 uint32_t HgmVSyncGeneratorController::GetCurrentRate() const
120 {
121     return currentRate_;
122 }
123 } // namespace Rosen
124 } // namespace OHOS