1 /* 2 * Copyright (c) 2024 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 #ifndef HGM_ENERGY_CONSUMPTION_POLICY_H 17 #define HGM_ENERGY_CONSUMPTION_POLICY_H 18 19 #include <mutex> 20 #include <string> 21 #include <unordered_map> 22 23 #include "hgm_command.h" 24 #include "hgm_touch_manager.h" 25 #include "variable_frame_rate/rs_variable_frame_rate.h" 26 27 #include "animation/rs_frame_rate_range.h" 28 #include "common/rs_common_def.h" 29 30 namespace OHOS::Rosen { 31 class HgmEnergyConsumptionPolicy { 32 public: 33 static HgmEnergyConsumptionPolicy& Instance(); 34 void SetEnergyConsumptionConfig(std::unordered_map<std::string, std::string> animationPowerConfig); 35 void SetUiEnergyConsumptionConfig(std::unordered_map<std::string, std::string> uiPowerConfig); 36 void SetAnimationEnergyConsumptionAssuranceMode(bool isEnergyConsumptionAssuranceMode); 37 // called by RSMainThread 38 void StatisticAnimationTime(uint64_t timestamp); 39 void StartNewAnimation(const std::string& componentName); 40 // called by RSMainThread 41 void GetAnimationIdleFps(FrameRateRange& rsRange); 42 void SetTouchState(TouchState touchState); 43 44 bool GetUiIdleFps(FrameRateRange& rsRange, pid_t pid = 0); 45 void SetRefreshRateMode(int32_t currentRefreshMode, std::string curScreenStrategyId); 46 void PrintEnergyConsumptionLog(const FrameRateRange& rsRange); 47 void SetVideoCallSceneInfo(const EventInfo& eventInfo); 48 // called by RSMainThread 49 void StatisticsVideoCallBufferCount(pid_t pid, const std::string& surfaceName); 50 // called by RSMainThread 51 void CheckOnlyVideoCallExist(); 52 // called by RSMainThread 53 bool GetVideoCallVsyncChange(); 54 bool GetVideoCallFrameRate(pid_t pid, const std::string& vsyncName, FrameRateRange &finalRange); 55 void SetCurrentPkgName(const std::vector<std::string>& pkgs); 56 void SetEnergyConsumptionAssuranceSceneInfo(const EventInfo& eventInfo); 57 58 private: 59 // <rateType, <isEnable, idleFps>> 60 std::unordered_map<uint32_t, std::pair<bool, int>> uiEnergyAssuranceMap_; 61 std::atomic<bool> isAnimationEnergyAssuranceEnable_ = false; 62 std::atomic<bool> isAnimationEnergyConsumptionAssuranceMode_ = false; 63 bool isTouchIdle_ = false; 64 int64_t rsAnimationTouchIdleTime_ = 1000; 65 std::atomic<uint64_t> firstAnimationTimestamp_ = 0; 66 std::atomic<uint64_t> lastAnimationTimestamp_ = 0; 67 // Unit: ms 68 int animationIdleDuration_ = 2000; 69 int animationIdleFps_ = 60; 70 std::string lastAssuranceLog_ = ""; 71 int32_t currentRefreshMode_ = -1; 72 std::string curScreenStrategyId_ = "LTPO-DEFAULT"; 73 std::atomic<pid_t> videoCallPid_ = { DEFAULT_PID }; 74 std::string videoCallVsyncName_ = ""; 75 int videoCallMaxFrameRate_ = 0; 76 std::atomic<bool> isEnableVideoCall_ = { false }; 77 std::atomic<int32_t> videoBufferCount_ = { 0 }; 78 std::atomic<bool> isSubmitDecisionTask_ = { false }; 79 std::atomic<bool> isOnlyVideoCallExist_ = { false }; 80 std::atomic<bool> isVideoCallVsyncChange_ = { false }; 81 // concurrency protection >>> 82 mutable std::mutex videoCallLock_; 83 std::string videoCallLayerName_ = ""; 84 // concurrency protection <<< 85 std::atomic<bool> dragSceneEnable_ = { true }; 86 std::atomic<pid_t> dragSceneDisablePid_ = { 0 }; 87 88 HgmEnergyConsumptionPolicy(); 89 ~HgmEnergyConsumptionPolicy() = default; 90 HgmEnergyConsumptionPolicy(const HgmEnergyConsumptionPolicy&) = delete; 91 HgmEnergyConsumptionPolicy(const HgmEnergyConsumptionPolicy&&) = delete; 92 HgmEnergyConsumptionPolicy& operator=(const HgmEnergyConsumptionPolicy&) = delete; 93 HgmEnergyConsumptionPolicy& operator=(const HgmEnergyConsumptionPolicy&&) = delete; 94 static void ConverStrToInt(int& targetNum, std::string sourceStr, int defaultValue); 95 void SetEnergyConsumptionRateRange(FrameRateRange& rsRange, int idleFps); 96 int32_t GetComponentEnergyConsumptionConfig(const std::string& componentName); 97 // Invoked by the render_service thread 98 void GetComponentFps(FrameRateRange& rsRange); 99 }; 100 } // namespace OHOS::Rosen 101 102 #endif