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