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