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 int pulseNum;
88 if (auto configPluseNum = HgmCore::Instance().GetPluseNum(); configPluseNum != -1) {
89 pulseNum = configPluseNum;
90 } else {
91 if (isNeedUpdateAppOffset && controllerRate == OLED_60_HZ) {
92 pulseNum = 0;
93 } else {
94 pulseNum = GetAppOffset(controllerRate);
95 }
96 }
97
98 VSyncGenerator::ListenerRefreshRateData listenerRate = {.cb = appController_, .refreshRates = appData};
99 VSyncGenerator::ListenerPhaseOffsetData listenerPhase;
100
101 if (currentRate_ != controllerRate) {
102 HGM_LOGD("HgmVSyncGeneratorController::ChangeGeneratorRate controllerRate = %{public}d, appOffset = %{public}d",
103 controllerRate, pulseNum);
104 RS_TRACE_NAME("HgmVSyncGeneratorController::ChangeGeneratorRate controllerRate: " +
105 std::to_string(controllerRate) + ", appOffset: " + std::to_string(pulseNum) +
106 ", nextVSyncTargetTime =" + std::to_string(targetTime));
107 listenerPhase.cb = appController_;
108 listenerPhase.phaseByPulseNum = pulseNum;
109 vsyncGenerator_->ChangeGeneratorRefreshRateModel(
110 listenerRate, listenerPhase, controllerRate, vsyncCount, targetTime);
111 currentOffset_ = vsyncGenerator_->GetVSyncPulse() * pulseNum;
112 currentRate_ = controllerRate;
113 } else {
114 if (isNeedUpdateAppOffset) {
115 listenerPhase.phaseByPulseNum = pulseNum;
116 }
117 vsyncGenerator_->ChangeGeneratorRefreshRateModel(listenerRate, listenerPhase, controllerRate, vsyncCount);
118 }
119 return vsyncCount;
120 }
121 } // namespace Rosen
122 } // namespace OHOS
123