• 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_frame_rate_tool.h"
17 
18 namespace OHOS::Rosen {
19 std::once_flag g_createFlag;
20 std::shared_ptr<HgmFrameRateTool> instance_ = nullptr;
21 
GetInstance()22 std::shared_ptr<HgmFrameRateTool> HgmFrameRateTool::GetInstance()
23 {
24     std::call_once(g_createFlag, [&] {
25         instance_ = std::shared_ptr<HgmFrameRateTool>(new HgmFrameRateTool());
26     });
27     return instance_;
28 }
29 
HgmFrameRateTool()30 HgmFrameRateTool::HgmFrameRateTool()
31 {
32 }
33 
~HgmFrameRateTool()34 HgmFrameRateTool::~HgmFrameRateTool() noexcept
35 {
36     std::lock_guard<std::mutex> lock(listMutex_);
37     auto screenProfile = screenProfileList_.begin();
38     while (screenProfile != screenProfileList_.end()) {
39         screenProfileList_.erase(screenProfile++);
40     }
41 }
42 
AddScreenProfile(ScreenId id,int32_t width,int32_t height,int32_t phyWidth,int32_t phyHeight)43 int32_t HgmFrameRateTool::AddScreenProfile(
44     ScreenId id, int32_t width, int32_t height, int32_t phyWidth, int32_t phyHeight)
45 {
46     auto screenProfile = std::make_shared<ScreenProfile>(id, width, height, phyWidth, phyHeight);
47     std::lock_guard<std::mutex> lock(listMutex_);
48     screenProfileList_.push_back(screenProfile);
49     return EXEC_SUCCESS;
50 }
51 
RemoveScreenProfile(ScreenId id)52 int32_t HgmFrameRateTool::RemoveScreenProfile(ScreenId id)
53 {
54     std::lock_guard<std::mutex> lock(listMutex_);
55     for (auto screenProfile = screenProfileList_.begin(); screenProfile != screenProfileList_.end(); ++screenProfile) {
56         if ((*screenProfile)->GetId() == id) {
57             screenProfileList_.erase(screenProfile);
58             break;
59         }
60     }
61     return EXEC_SUCCESS;
62 }
63 
GetScreenProfile(ScreenId id) const64 std::shared_ptr<HgmFrameRateTool::ScreenProfile> HgmFrameRateTool::GetScreenProfile(ScreenId id) const
65 {
66     for (auto screenProfile = screenProfileList_.begin(); screenProfile != screenProfileList_.end(); ++screenProfile) {
67         if ((*screenProfile)->GetId() == id) {
68             return *screenProfile;
69         }
70     }
71     return nullptr;
72 }
73 
CalModifierPreferred(ScreenId id,HgmModifierProfile & hgmModifierProfile,std::shared_ptr<ParsedConfigData> parsedConfigData) const74 int32_t HgmFrameRateTool::CalModifierPreferred(
75     ScreenId id, HgmModifierProfile &hgmModifierProfile, std::shared_ptr<ParsedConfigData> parsedConfigData) const
76 {
77     auto screenProfile = GetScreenProfile(id);
78     if (!screenProfile) {
79         return HGM_ERROR;
80     }
81     auto [xSpeed, ySpeed] = applyDimension(
82         SpeedTransType::TRANS_PIXEL_TO_MM, hgmModifierProfile.xSpeed, hgmModifierProfile.ySpeed, screenProfile);
83     auto mixSpeed = sqrt(xSpeed * xSpeed + ySpeed * ySpeed);
84 
85     auto dynamicSettingMap = parsedConfigData->GetAnimationDynamicSettingMap(hgmModifierProfile.hgmModifierType);
86     for (const auto &iter: dynamicSettingMap) {
87         if (mixSpeed >= iter.second.min && (mixSpeed < iter.second.max || iter.second.max == -1)) {
88             return iter.second.preferred_fps;
89         }
90     }
91     return HGM_ERROR;
92 }
93 
applyDimension(SpeedTransType speedTransType,float xSpeed,float ySpeed,std::shared_ptr<ScreenProfile> screenProfile)94 std::pair<float, float> HgmFrameRateTool::applyDimension(
95     SpeedTransType speedTransType, float xSpeed, float ySpeed, std::shared_ptr<ScreenProfile> screenProfile)
96 {
97     switch (speedTransType) {
98         case SpeedTransType::TRANS_MM_TO_PIXEL:
99             return std::pair<float, float>(
100                 xSpeed * screenProfile->GetXDpi() / INCH_2_MM, ySpeed * screenProfile->GetYDpi() / INCH_2_MM);
101         case SpeedTransType::TRANS_PIXEL_TO_MM:
102             return std::pair<float, float>(
103                 xSpeed / screenProfile->GetXDpi() * INCH_2_MM, ySpeed / screenProfile->GetYDpi() * INCH_2_MM);
104         default:
105             return std::pair<float, float>(0, 0);
106     }
107 }
108 
109 } // namespace OHOS::Rosen