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_core.h"
17
18 #include <algorithm>
19 #include <cstddef>
20 #include <cstdint>
21 #include <string>
22 #include <unistd.h>
23
24 #include "hgm_frame_rate_manager.h"
25 #include "hgm_config_callback_manager.h"
26 #include "hgm_log.h"
27 #include "vsync_generator.h"
28 #include "platform/common/rs_system_properties.h"
29 #include "parameters.h"
30 #include "frame_rate_report.h"
31 #include "sandbox_utils.h"
32 #include "hgm_screen_info.h"
33 #include "syspara/parameter.h"
34
35 namespace OHOS::Rosen {
36 namespace {
37 std::map<uint32_t, int64_t> IDEAL_PERIOD = {
38 { 144, 6944444 },
39 { 120, 8333333 },
40 { 90, 11111111 },
41 { 80, 12500000 },
42 { 72, 13888888 },
43 { 60, 16666666 },
44 { 48, 20833333 },
45 { 45, 22222222 },
46 { 40, 25000000 },
47 { 36, 27777777 },
48 { 30, 33333333 },
49 { 24, 41666666 },
50 { 20, 50000000 },
51 { 15, 66666666 },
52 { 10, 100000000 },
53 };
54 } // namespace
55
SysModeChangeProcess(const char * key,const char * value,void * context)56 void HgmCore::SysModeChangeProcess(const char* key, const char* value, void* context)
57 {
58 std::string mode(value);
59 HgmTaskHandleThread::Instance().PostTask([mode] () {
60 auto curMode = HgmCore::Instance().GetCurrentRefreshRateMode();
61 HgmCore::Instance().GetPolicyConfigData()->UpdateRefreshRateForSettings(mode);
62 HgmCore::Instance().SetRefreshRateMode(curMode);
63 RSSystemProperties::SetHgmRefreshRateModesEnabled(std::to_string(curMode));
64 HGM_LOGI("System mode changed to %{public}s, cur refresh mode is %{public}d", mode.c_str(), curMode);
65 });
66 }
67
Instance()68 HgmCore& HgmCore::Instance()
69 {
70 static HgmCore instance;
71 instance.Init();
72 return instance;
73 }
74
HgmCore()75 HgmCore::HgmCore()
76 {
77 HGM_LOGI("Construction of Hgmcore");
78 }
79
Init()80 void HgmCore::Init()
81 {
82 static std::once_flag onceFlag;
83 std::call_once(onceFlag, [this]() {
84 if (InitXmlConfig() != EXEC_SUCCESS) {
85 HGM_LOGE("HgmCore falied to parse");
86 return;
87 }
88
89 // ensure that frameRateManager init after XML parsed.
90 hgmFrameRateMgr_ = std::make_unique<HgmFrameRateManager>();
91
92 auto newRateMode = static_cast<int32_t>(RSSystemProperties::GetHgmRefreshRateModesEnabled());
93 if (newRateMode == 0) {
94 HGM_LOGI("HgmCore No customer refreshrate mode found, set to xml default");
95 if (mPolicyConfigData_ == nullptr || !XMLParser::IsNumber(mPolicyConfigData_->defaultRefreshRateMode_)) {
96 HGM_LOGE("HgmCore failed to get parsed data");
97 } else {
98 customFrameRateMode_ = std::stoi(mPolicyConfigData_->defaultRefreshRateMode_);
99 }
100 } else {
101 HGM_LOGI("HgmCore No customer refreshrate mode found: %{public}d", newRateMode);
102 customFrameRateMode_ = newRateMode;
103 if (customFrameRateMode_ != HGM_REFRESHRATE_MODE_AUTO &&
104 mPolicyConfigData_ != nullptr && mPolicyConfigData_->xmlCompatibleMode_) {
105 customFrameRateMode_ = mPolicyConfigData_->SettingModeId2XmlModeId(customFrameRateMode_);
106 }
107 CheckCustomFrameRateModeValid();
108 }
109 if (mPolicyConfigVisitor_ != nullptr) {
110 mPolicyConfigVisitor_->SetXmlModeId(std::to_string(customFrameRateMode_));
111 }
112 (void)AddParamWatcher();
113 SetLtpoConfig();
114 HGM_LOGI("HgmCore initialization success!!!");
115 });
116 }
117
AddParamWatcher() const118 int HgmCore::AddParamWatcher() const
119 {
120 // SysModeChangeProcess will be called when first WatchParameter
121 int ret = WatchParameter("persist.sys.mode", HgmCore::SysModeChangeProcess, nullptr);
122 if (ret != SUCCESS) {
123 HGM_LOGE("WatchParameter fail: %{public}d", ret);
124 }
125 return ret;
126 }
127
CheckCustomFrameRateModeValid()128 void HgmCore::CheckCustomFrameRateModeValid()
129 {
130 if (hgmFrameRateMgr_ == nullptr || mPolicyConfigData_ == nullptr) {
131 return;
132 }
133
134 auto curScreenStrategyId = hgmFrameRateMgr_->GetCurScreenStrategyId();
135 auto& screenConfigs = mPolicyConfigData_->screenConfigs_;
136 if (screenConfigs.find(curScreenStrategyId) == screenConfigs.end()) {
137 return;
138 }
139
140 auto& screenConfig = screenConfigs[curScreenStrategyId];
141 auto modeStr = std::to_string(customFrameRateMode_);
142 if (screenConfig.find(modeStr) != screenConfig.end() || screenConfig.empty()) {
143 return;
144 }
145
146 int32_t maxMode = HGM_REFRESHRATE_MODE_AUTO;
147 for (auto& [modeStr, _] : screenConfig) {
148 if (!XMLParser::IsNumber(modeStr)) {
149 continue;
150 }
151 auto mode = std::stoi(modeStr);
152 if (maxMode < mode) {
153 maxMode = mode;
154 }
155 }
156 HGM_LOGE("auto repair mode: %{public}d -> %{public}d", customFrameRateMode_, maxMode);
157 customFrameRateMode_ = maxMode;
158 }
159
InitXmlConfig()160 int32_t HgmCore::InitXmlConfig()
161 {
162 HGM_LOGD("HgmCore is parsing xml configuration");
163 if (!mParser_) {
164 mParser_ = std::make_unique<XMLParser>();
165 }
166
167 if (mParser_->LoadConfiguration(CONFIG_FILE_PRODUCT) != EXEC_SUCCESS) {
168 HGM_LOGW("HgmCore failed to load prod xml configuration file");
169 }
170 if (mParser_->Parse() != EXEC_SUCCESS) {
171 HGM_LOGW("HgmCore failed to parse prod xml configuration");
172 }
173
174 if (!mPolicyConfigData_) {
175 mPolicyConfigData_ = mParser_->GetParsedData();
176 }
177 if (mPolicyConfigData_ != nullptr) {
178 mPolicyConfigVisitor_ = std::make_shared<PolicyConfigVisitorImpl>(*mPolicyConfigData_);
179 }
180
181 return EXEC_SUCCESS;
182 }
183
SetASConfig(const PolicyConfigData::ScreenSetting & curScreenSetting)184 void HgmCore::SetASConfig(const PolicyConfigData::ScreenSetting& curScreenSetting)
185 {
186 if (curScreenSetting.ltpoConfig.find("adaptiveSync") != curScreenSetting.ltpoConfig.end()) {
187 std::string asConfig = curScreenSetting.ltpoConfig.at("adaptiveSync");
188 if (asConfig == "1" || asConfig == "0") {
189 adaptiveSync_ = std::stoi(asConfig);
190 } else {
191 adaptiveSync_ = 0;
192 }
193 } else {
194 adaptiveSync_ = 0;
195 HGM_LOGW("HgmCore failed to find adaptiveSync strategy for LTPO, then set to 0");
196 }
197 }
198
SetLtpoConfig()199 void HgmCore::SetLtpoConfig()
200 {
201 if ((hgmFrameRateMgr_ == nullptr) || (mPolicyConfigData_ == nullptr)) {
202 return;
203 }
204 auto curScreenStrategyId = hgmFrameRateMgr_->GetCurScreenStrategyId();
205 if (mPolicyConfigData_->screenConfigs_.count(curScreenStrategyId) == 0 ||
206 mPolicyConfigData_->screenConfigs_[curScreenStrategyId].count(std::to_string(customFrameRateMode_)) == 0) {
207 return;
208 }
209 auto curScreenSetting =
210 mPolicyConfigData_->screenConfigs_[curScreenStrategyId][std::to_string(customFrameRateMode_)];
211 const auto& ltpoConfig = curScreenSetting.ltpoConfig;
212 if (ltpoConfig.find("switch") != ltpoConfig.end() && XMLParser::IsNumber(ltpoConfig.at("switch"))) {
213 ltpoEnabled_ = std::stoi(ltpoConfig.at("switch"));
214 } else {
215 ltpoEnabled_ = 0;
216 HGM_LOGW("HgmCore failed to find switch strategy for LTPO");
217 }
218
219 if (ltpoConfig.find("maxTE") != ltpoConfig.end() && XMLParser::IsNumber(ltpoConfig.at("maxTE"))) {
220 maxTE_ = std::stoul(ltpoConfig.at("maxTE"));
221 CreateVSyncGenerator()->SetVSyncMaxRefreshRate(maxTE_);
222 } else {
223 maxTE_ = 0;
224 HGM_LOGW("HgmCore failed to find TE strategy for LTPO");
225 }
226
227 if (ltpoConfig.find("alignRate") != ltpoConfig.end() && XMLParser::IsNumber(ltpoConfig.at("alignRate"))) {
228 alignRate_ = std::stoul(ltpoConfig.at("alignRate"));
229 } else {
230 alignRate_ = 0;
231 HGM_LOGW("HgmCore failed to find alignRate strategy for LTPO");
232 }
233
234 if (ltpoConfig.count("pipelineOffsetPulseNum") != 0 &&
235 XMLParser::IsNumber(ltpoConfig.at("pipelineOffsetPulseNum"))) {
236 pipelineOffsetPulseNum_ = std::stoi(ltpoConfig.at("pipelineOffsetPulseNum"));
237 CreateVSyncGenerator()->SetVSyncPhaseByPulseNum(pipelineOffsetPulseNum_);
238 } else {
239 pipelineOffsetPulseNum_ = 0;
240 HGM_LOGW("HgmCore failed to find pipelineOffset strategy for LTPO");
241 }
242 SetIdealPipelineOffset(pipelineOffsetPulseNum_);
243 SetASConfig(curScreenSetting);
244
245 SetScreenConstraintConfig();
246 SetPerformanceConfig();
247 HGM_LOGI("HgmCore LTPO strategy ltpoEnabled: %{public}d, maxTE: %{public}d, alignRate: %{public}d, "
248 "pipelineOffsetPulseNum: %{public}d, vBlankIdleCorrectSwitch: %{public}d, "
249 "lowRateToHighQuickSwitch: %{public}d, pluseNum_: %{public}d, isDelayMode_: %{public}d",
250 ltpoEnabled_, maxTE_, alignRate_, pipelineOffsetPulseNum_, vBlankIdleCorrectSwitch_.load(),
251 lowRateToHighQuickSwitch_.load(), pluseNum_, isDelayMode_);
252 }
253
SetScreenConstraintConfig()254 void HgmCore::SetScreenConstraintConfig()
255 {
256 auto curScreenStrategyId = hgmFrameRateMgr_->GetCurScreenStrategyId();
257 if (mPolicyConfigData_->screenConfigs_.count(curScreenStrategyId) == 0 ||
258 mPolicyConfigData_->screenConfigs_[curScreenStrategyId].count(std::to_string(customFrameRateMode_)) == 0) {
259 return;
260 }
261 const auto& curScreenSetting =
262 mPolicyConfigData_->screenConfigs_[curScreenStrategyId][std::to_string(customFrameRateMode_)];
263 if (curScreenSetting.ltpoConfig.find("vBlankIdleCorrectSwitch") != curScreenSetting.ltpoConfig.end() &&
264 XMLParser::IsNumber(curScreenSetting.ltpoConfig.at("vBlankIdleCorrectSwitch"))) {
265 vBlankIdleCorrectSwitch_.store(std::stoi(curScreenSetting.ltpoConfig.at("vBlankIdleCorrectSwitch")));
266 } else {
267 vBlankIdleCorrectSwitch_.store(false);
268 HGM_LOGW("HgmCore failed to find vBlankIdleCorrectSwitch strategy for LTPO");
269 }
270
271 if (curScreenSetting.ltpoConfig.find("lowRateToHighQuickSwitch") != curScreenSetting.ltpoConfig.end() &&
272 XMLParser::IsNumber(curScreenSetting.ltpoConfig.at("lowRateToHighQuickSwitch"))) {
273 lowRateToHighQuickSwitch_.store(std::stoi(curScreenSetting.ltpoConfig.at("lowRateToHighQuickSwitch")));
274 } else {
275 lowRateToHighQuickSwitch_.store(false);
276 HGM_LOGW("HgmCore failed to find lowRateToHighQuickSwitch strategy for LTPO");
277 }
278 }
279
SetPerformanceConfig()280 void HgmCore::SetPerformanceConfig()
281 {
282 auto curScreenStrategyId = hgmFrameRateMgr_->GetCurScreenStrategyId();
283 if (mPolicyConfigData_->screenConfigs_.count(curScreenStrategyId) == 0 ||
284 mPolicyConfigData_->screenConfigs_[curScreenStrategyId].count(std::to_string(customFrameRateMode_)) == 0) {
285 return;
286 }
287 const auto& curScreenSetting =
288 mPolicyConfigData_->screenConfigs_[curScreenStrategyId][std::to_string(customFrameRateMode_)];
289 if (curScreenSetting.performanceConfig.count("pluseNum") != 0 &&
290 XMLParser::IsNumber(curScreenSetting.performanceConfig.at("pluseNum"))) {
291 pluseNum_ = std::stoi(curScreenSetting.performanceConfig.at("pluseNum"));
292 } else {
293 pluseNum_ = -1;
294 HGM_LOGW("HgmCore failed to find pluseNum_ strategy for LTPO");
295 }
296 if (curScreenSetting.performanceConfig.count("piplineDelayModeEnable") != 0) {
297 isDelayMode_ = curScreenSetting.performanceConfig.at("piplineDelayModeEnable") != "0";
298 } else {
299 isDelayMode_ = true;
300 HGM_LOGW("HgmCore failed to find piplineDelayModeEnable_ strategy for LTPO");
301 }
302 }
303
RegisterRefreshRateModeChangeCallback(const RefreshRateModeChangeCallback & callback)304 void HgmCore::RegisterRefreshRateModeChangeCallback(const RefreshRateModeChangeCallback& callback)
305 {
306 refreshRateModeChangeCallback_ = callback;
307 if (refreshRateModeChangeCallback_ != nullptr) {
308 auto refreshRateModeName = GetCurrentRefreshRateModeName();
309 refreshRateModeChangeCallback_(refreshRateModeName);
310 }
311 }
312
SetCustomRateMode(int32_t mode)313 int32_t HgmCore::SetCustomRateMode(int32_t mode)
314 {
315 customFrameRateMode_ = mode;
316 return EXEC_SUCCESS;
317 }
318
RegisterRefreshRateUpdateCallback(const RefreshRateUpdateCallback & callback)319 void HgmCore::RegisterRefreshRateUpdateCallback(const RefreshRateUpdateCallback& callback)
320 {
321 ScreenId screenId = HgmCore::Instance().GetActiveScreenId();
322 uint32_t refreshRate = HgmCore::Instance().GetScreenCurrentRefreshRate(screenId);
323 refreshRateUpdateCallback_ = callback;
324 if (refreshRateUpdateCallback_ != nullptr) {
325 refreshRateUpdateCallback_(refreshRate);
326 }
327 }
328
SetScreenRefreshRate(ScreenId id,int32_t sceneId,int32_t rate,bool shouldSendCallback)329 int32_t HgmCore::SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate, bool shouldSendCallback)
330 {
331 if (!IsEnabled()) {
332 HGM_LOGD("HgmCore is not enabled");
333 return HGM_ERROR;
334 }
335 // set the screen to the desired refreshrate
336 HGM_LOGD("HgmCore setting screen " PUBU64 " to the rate of %{public}d", id, rate);
337 auto screen = GetScreen(id);
338 if (!screen) {
339 HGM_LOGW("HgmCore failed to get screen of : " PUBU64 "", id);
340 return HGM_ERROR;
341 }
342
343 if (rate <= 0) {
344 HGM_LOGW("HgmCore refuse an illegal framerate: %{public}d", rate);
345 return HGM_ERROR;
346 }
347 int32_t modeToSwitch = screen->SetActiveRefreshRate(sceneId, static_cast<uint32_t>(rate));
348 if (modeToSwitch < 0) {
349 return modeToSwitch;
350 }
351
352 std::lock_guard<std::mutex> lock(modeListMutex_);
353
354 // the rate is accepted and passed to a list, will be applied by hardwarethread before sending the composition
355 HGM_LOGD("HgmCore the rate of %{public}d is accepted, the target mode is %{public}d", rate, modeToSwitch);
356 if (modeListToApply_ == nullptr) {
357 HGM_LOGD("HgmCore modeListToApply_ is invalid, buiding a new mode list");
358 modeListToApply_ = std::make_unique<std::unordered_map<ScreenId, int32_t>>();
359 }
360 auto modeList = modeListToApply_.get();
361 (*modeList)[id] = modeToSwitch;
362
363 if (refreshRateUpdateCallback_ && shouldSendCallback) {
364 refreshRateUpdateCallback_(rate);
365 HGM_LOGD("refresh rate changed, notify to app");
366 }
367 return modeToSwitch;
368 }
369
SetRateAndResolution(ScreenId id,int32_t sceneId,int32_t rate,int32_t width,int32_t height)370 int32_t HgmCore::SetRateAndResolution(ScreenId id, int32_t sceneId, int32_t rate, int32_t width, int32_t height)
371 {
372 // reserved
373 return HGM_ERROR;
374 }
375
SetRefreshRateMode(int32_t refreshRateMode)376 int32_t HgmCore::SetRefreshRateMode(int32_t refreshRateMode)
377 {
378 if (mPolicyConfigVisitor_ != nullptr) {
379 mPolicyConfigVisitor_->SetSettingModeId(refreshRateMode);
380 }
381 // setting mode to xml modeid
382 if (mPolicyConfigData_ != nullptr && mPolicyConfigData_->xmlCompatibleMode_) {
383 refreshRateMode = mPolicyConfigData_->SettingModeId2XmlModeId(refreshRateMode);
384 }
385 HGM_LOGD("HgmCore set refreshrate mode to : %{public}d", refreshRateMode);
386 // change refreshrate mode by setting application
387 if (SetCustomRateMode(refreshRateMode) != EXEC_SUCCESS) {
388 return HGM_ERROR;
389 }
390
391 hgmFrameRateMgr_->HandleRefreshRateMode(refreshRateMode);
392 // sync vsync mode after refreshRate mode switching
393 auto refreshRateModeName = GetCurrentRefreshRateModeName();
394 if (refreshRateModeChangeCallback_ != nullptr) {
395 refreshRateModeChangeCallback_(refreshRateModeName);
396 }
397 HgmConfigCallbackManager::GetInstance()->SyncRefreshRateModeChangeCallback(refreshRateModeName);
398 return EXEC_SUCCESS;
399 }
400
NotifyScreenPowerStatus(ScreenId id,ScreenPowerStatus status)401 void HgmCore::NotifyScreenPowerStatus(ScreenId id, ScreenPowerStatus status)
402 {
403 if (hgmFrameRateMgr_ != nullptr) {
404 hgmFrameRateMgr_->HandleScreenPowerStatus(id, status);
405 }
406
407 if (refreshRateModeChangeCallback_ != nullptr) {
408 auto refreshRateModeName = GetCurrentRefreshRateModeName();
409 refreshRateModeChangeCallback_(refreshRateModeName);
410 }
411 }
412
NotifyScreenRectFrameRateChange(ScreenId id,const GraphicIRect & activeRect)413 void HgmCore::NotifyScreenRectFrameRateChange(ScreenId id, const GraphicIRect& activeRect)
414 {
415 if (hgmFrameRateMgr_ != nullptr) {
416 hgmFrameRateMgr_->HandleScreenRectFrameRate(id, activeRect);
417 }
418
419 if (refreshRateModeChangeCallback_ != nullptr) {
420 auto refreshRateModeName = GetCurrentRefreshRateModeName();
421 refreshRateModeChangeCallback_(refreshRateModeName);
422 }
423 }
424
AddScreen(ScreenId id,int32_t defaultMode,ScreenSize & screenSize)425 int32_t HgmCore::AddScreen(ScreenId id, int32_t defaultMode, ScreenSize& screenSize)
426 {
427 // add a physical screen to hgm during hotplug event
428 HGM_LOGI("HgmCore adding screen : " PUBI64 "", id);
429 bool removeId = std::any_of(screenIds_.begin(), screenIds_.end(),
430 [id](const ScreenId screen) { return screen == id; });
431 if (removeId) {
432 if (RemoveScreen(id) != EXEC_SUCCESS) {
433 HGM_LOGW("HgmCore failed to remove the existing screen, not adding : " PUBI64 "", id);
434 return HGM_BASE_REMOVE_FAILED;
435 }
436 }
437
438 sptr<HgmScreen> newScreen = new HgmScreen(id, defaultMode, screenSize);
439 auto configData = HgmCore::Instance().GetPolicyConfigData();
440 if (configData != nullptr) {
441 auto& hgmScreenInfo = HgmScreenInfo::GetInstance();
442 auto isLtpo = hgmScreenInfo.IsLtpoType(hgmScreenInfo.GetScreenType(id));
443 std::string curScreenName = "screen" + std::to_string(id) + "_" + (isLtpo ? "LTPO" : "LTPS");
444 for (auto strategyConfig : configData->screenStrategyConfigs_) {
445 if (strategyConfig.first.find(curScreenName) == 0) {
446 newScreen->SetSelfOwnedScreenFlag(true);
447 break;
448 }
449 }
450 }
451
452 std::lock_guard<std::mutex> lock(listMutex_);
453 screenList_.push_back(newScreen);
454 screenIds_.push_back(id);
455
456 int32_t screenNum = GetScreenListSize();
457 HGM_LOGI("HgmCore num of screen is %{public}d", screenNum);
458 return EXEC_SUCCESS;
459 }
460
RemoveScreen(ScreenId id)461 int32_t HgmCore::RemoveScreen(ScreenId id)
462 {
463 std::lock_guard<std::mutex> lock(listMutex_);
464 // delete a screen during a hotplug event
465 HGM_LOGD("HgmCore deleting the screen : " PUBU64 "", id);
466 for (auto screen = screenIds_.begin(); screen != screenIds_.end(); ++screen) {
467 if (*screen == id) {
468 screenIds_.erase(screen);
469 break;
470 }
471 }
472 for (auto screen = screenList_.begin(); screen != screenList_.end(); ++screen) {
473 if ((*screen)->GetId() == id) {
474 screenList_.erase(screen);
475 break;
476 }
477 }
478 return EXEC_SUCCESS;
479 }
480
AddScreenInfo(ScreenId id,int32_t width,int32_t height,uint32_t rate,int32_t mode)481 int32_t HgmCore::AddScreenInfo(ScreenId id, int32_t width, int32_t height, uint32_t rate, int32_t mode)
482 {
483 // add a supported screen mode to the screen
484 auto screen = GetScreen(id);
485 if (!screen) {
486 HGM_LOGW("HgmCore failed to get screen of : " PUBU64 "", id);
487 return HGM_NO_SCREEN;
488 }
489
490 if (screen->AddScreenModeInfo(width, height, rate, mode) == EXEC_SUCCESS) {
491 return EXEC_SUCCESS;
492 }
493
494 HGM_LOGW("HgmCore failed to add screen mode info of screen : " PUBU64 "", id);
495 return HGM_SCREEN_PARAM_ERROR;
496 }
497
GetScreenCurrentRefreshRate(ScreenId id) const498 uint32_t HgmCore::GetScreenCurrentRefreshRate(ScreenId id) const
499 {
500 auto screen = GetScreen(id);
501 if (!screen) {
502 HGM_LOGD("HgmCore failed to find screen " PUBU64 "", id);
503 return static_cast<uint32_t>(EXEC_SUCCESS);
504 }
505
506 return screen->GetActiveRefreshRate();
507 }
508
GetCurrentRefreshRateMode() const509 int32_t HgmCore::GetCurrentRefreshRateMode() const
510 {
511 if (mPolicyConfigData_ != nullptr && mPolicyConfigData_->xmlCompatibleMode_) {
512 auto ret = mPolicyConfigData_->XmlModeId2SettingModeId(std::to_string(customFrameRateMode_));
513 HGM_LOGI("In GetCurrentRefreshRateMode, xmlid: %{public}d, setid: %{public}d", customFrameRateMode_, ret);
514 return ret;
515 }
516 return customFrameRateMode_;
517 }
518
GetCurrentRefreshRateModeName() const519 int32_t HgmCore::GetCurrentRefreshRateModeName() const
520 {
521 if (mPolicyConfigData_ != nullptr && mPolicyConfigData_->xmlCompatibleMode_) {
522 return mPolicyConfigData_->GetRefreshRateModeName(customFrameRateMode_);
523 }
524 std::map<int32_t, int32_t> modeIdRateMap = {{-1, -1}, {1, 60}, {2, 90}, {3, 120}};
525 if (modeIdRateMap.find(customFrameRateMode_) != modeIdRateMap.end()) {
526 return modeIdRateMap[customFrameRateMode_];
527 }
528 return customFrameRateMode_;
529 }
530
GetScreen(ScreenId id) const531 sptr<HgmScreen> HgmCore::GetScreen(ScreenId id) const
532 {
533 std::lock_guard<std::mutex> lock(listMutex_);
534 for (auto screen = screenList_.begin(); screen != screenList_.end(); ++screen) {
535 if ((*screen)->GetId() == id) {
536 return *screen;
537 }
538 }
539
540 return nullptr;
541 }
542
GetScreenSupportedRefreshRates(ScreenId id)543 std::vector<uint32_t> HgmCore::GetScreenSupportedRefreshRates(ScreenId id)
544 {
545 HgmTaskHandleThread::Instance().DetectMultiThreadingCalls();
546 auto screen = GetScreen(id);
547 if (!screen) {
548 HGM_LOGW("HgmCore failed to find screen " PUBU64 "", id);
549 return std::vector<uint32_t>(static_cast<uint32_t>(EXEC_SUCCESS));
550 }
551
552 auto supportedRates = screen->GetSupportedRates();
553 std::vector<uint32_t> retVec;
554 retVec.assign(supportedRates.begin(), supportedRates.end());
555 return retVec;
556 }
557
GetScreenComponentRefreshRates(ScreenId id)558 std::vector<int32_t> HgmCore::GetScreenComponentRefreshRates(ScreenId id)
559 {
560 if (!mPolicyConfigData_) {
561 HGM_LOGW("HgmCore no parsed component data, returning default value");
562 return std::vector<int32_t>(static_cast<uint32_t>(EXEC_SUCCESS));
563 }
564
565 std::vector<int32_t> retVec;
566 for (const auto& [rate, _] : mPolicyConfigData_->refreshRateForSettings_) {
567 retVec.emplace_back(rate);
568 HGM_LOGE("HgmCore Adding component rate: %{public}d", rate);
569 }
570 return retVec;
571 }
572
GetModesToApply()573 std::unique_ptr<std::unordered_map<ScreenId, int32_t>> HgmCore::GetModesToApply()
574 {
575 std::lock_guard<std::mutex> lock(modeListMutex_);
576 return std::move(modeListToApply_);
577 }
578
SetActiveScreenId(ScreenId id)579 void HgmCore::SetActiveScreenId(ScreenId id)
580 {
581 activeScreenId_.store(id);
582 }
583
GetActiveScreen() const584 sptr<HgmScreen> HgmCore::GetActiveScreen() const
585 {
586 auto activeScreenId = GetActiveScreenId();
587 if (activeScreenId == INVALID_SCREEN_ID) {
588 HGM_LOGE("HgmScreen activeScreenId_ noset");
589 return nullptr;
590 }
591 return GetScreen(activeScreenId);
592 }
593
GetIdealPeriod(uint32_t rate)594 int64_t HgmCore::GetIdealPeriod(uint32_t rate)
595 {
596 if (IDEAL_PERIOD.count(rate)) {
597 return IDEAL_PERIOD[rate];
598 }
599 return 0;
600 }
601 } // namespace OHOS::Rosen
602