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 #ifndef RS_REALTIME_REFRESH_RATE_MANAGER_H 17 #define RS_REALTIME_REFRESH_RATE_MANAGER_H 18 19 #include <atomic> 20 #include <chrono> 21 #include <condition_variable> 22 #include <mutex> 23 #include <thread> 24 #include <unordered_map> 25 26 #include "hgm_frame_rate_manager.h" 27 #include "screen_manager/screen_types.h" 28 29 namespace OHOS::Rosen { 30 enum class RealtimeRefreshRateType : int32_t { 31 START = -1, 32 COLLECT = 0, 33 SHOW = 1, 34 END = 2, 35 }; 36 37 class RSRealtimeRefreshRateManager { 38 public: 39 static RSRealtimeRefreshRateManager& Instance(); 40 GetShowRefreshRateEnabled()41 bool GetShowRefreshRateEnabled() const 42 { 43 return showEnabled_; 44 } 45 void SetShowRefreshRateEnabled(bool enabled, int32_t type); 46 uint32_t GetRealtimeRefreshRate(ScreenId screenId); 47 private: 48 friend class RSHardwareThread; 49 RSRealtimeRefreshRateManager() = default; 50 ~RSRealtimeRefreshRateManager() = default; 51 CountRealtimeFrame(const ScreenId screenId)52 inline void CountRealtimeFrame(const ScreenId screenId) 53 { 54 if (showEnabled_ || collectEnabled_) { 55 std::unique_lock<std::mutex> lock(showRealtimeFrameMutex_); 56 realtimeFrameCountMap_[screenId]++; 57 } 58 } 59 void StatisticsRefreshRateDataLocked(std::shared_ptr<HgmFrameRateManager> frameRateMgr); 60 61 std::atomic<bool> showEnabled_ = false; 62 std::atomic<bool> collectEnabled_ = false; 63 std::atomic<bool> isCollectRefreshRateTaskRunning_ = false; 64 std::unordered_map<ScreenId, uint32_t> currRealtimeRefreshRateMap_; 65 std::unordered_map<ScreenId, uint32_t> realtimeFrameCountMap_; 66 std::mutex showRealtimeFrameMutex_; 67 std::mutex threadMutex_; 68 std::chrono::steady_clock::time_point startTime_; 69 uint32_t lastRefreshRate_ = 0; 70 bool isRealtimeRefreshRateChange_ = false; 71 std::function<void()> showRefreshRateTask_ = nullptr; 72 const std::string EVENT_ID = "SHOW_REFRESH_RATE"; 73 74 static constexpr uint8_t IDLE_FPS_THRESHOLD = 8; 75 static constexpr auto NS_PER_S = 76 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(1)).count(); 77 static constexpr uint8_t DEFAULT_REALTIME_REFRESH_RATE = 1; 78 static constexpr uint32_t EVENT_INTERVAL = 250; // 250ms 79 }; 80 } // namespace OHOS::Rosen 81 #endif // RS_REALTIME_REFRESH_RATE_MANAGER_H 82