1 /* 2 * Copyright (c) 2022 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 OHOS_ROSEN_RESSCHED_REPORT_H 17 #define OHOS_ROSEN_RESSCHED_REPORT_H 18 19 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 20 #include "event_handler.h" 21 #include "res_sched_client.h" 22 #endif 23 #include "window_helper.h" 24 #include "wm_common.h" 25 #include "wm_single_instance.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 30 namespace { 31 constexpr int64_t PERF_TIME_OUT = 200; 32 constexpr int64_t SLIDE_PERF_TIME_OUT = 1000; 33 constexpr int32_t PERF_CLICK_NORMAL_CODE = 9; 34 constexpr int32_t PERF_DRAG_CODE = 31; 35 constexpr int32_t PERF_MOVE_CODE = 32; 36 constexpr int32_t PERF_ANIMATION_BOOST_CODE = 33; 37 constexpr int32_t PERF_SLIDE_CODE = 11; 38 constexpr int32_t PERF_STATUS_BAR_DRAG_CODE = 37; 39 const std::string TASK_NAME = "SlideOff"; 40 } 41 42 /** 43 * @brief Slide event status 44 */ 45 enum SlideEventStatus : int64_t { 46 SLIDE_EVENT_OFF = 0, 47 SLIDE_EVENT_ON = 1, 48 SLIDE_NORMAL_BEGIN = 3, 49 SLIDE_NORMAL_END = 4, 50 }; 51 #endif 52 53 class ResSchedReport { 54 WM_DECLARE_SINGLE_INSTANCE(ResSchedReport); 55 public: StopPerfIfNeed()56 void StopPerfIfNeed() 57 { 58 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 59 if (windowDragBoost_) { 60 ClosePerf(PERF_DRAG_CODE); 61 windowDragBoost_ = false; 62 } 63 if (windowMovingBoost_) { 64 ClosePerf(PERF_MOVE_CODE); 65 windowMovingBoost_ = false; 66 } 67 #endif 68 } 69 TrigClick()70 void TrigClick() 71 { 72 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 73 std::unordered_map<std::string, std::string> mapPayload; 74 // 2 means click event. 75 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_CLICK_NORMAL_CODE, 2, mapPayload); 76 #endif 77 } 78 AnimationBoost()79 void AnimationBoost() 80 { 81 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 82 std::unordered_map<std::string, std::string> mapPayload; 83 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_ANIMATION_BOOST_CODE, 0, mapPayload); 84 #endif 85 } 86 RequestPerfIfNeed(WindowSizeChangeReason reason, WindowType type, WindowMode mode)87 void RequestPerfIfNeed(WindowSizeChangeReason reason, WindowType type, WindowMode mode) 88 { 89 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 90 if (WindowHelper::IsMainFloatingWindow(type, mode) || WindowHelper::IsSplitWindowMode(mode)) { 91 switch (reason) { 92 case WindowSizeChangeReason::DRAG_END: { 93 if (windowDragBoost_) { 94 ClosePerf(PERF_DRAG_CODE); 95 windowDragBoost_ = false; 96 } 97 break; 98 } 99 case WindowSizeChangeReason::DRAG_START: 100 [[fallthrough]]; 101 case WindowSizeChangeReason::DRAG: { 102 RequestPerf(PERF_DRAG_CODE, PERF_TIME_OUT); 103 windowDragBoost_ = true; 104 break; 105 } 106 case WindowSizeChangeReason::MOVE: { 107 RequestPerf(PERF_MOVE_CODE, PERF_TIME_OUT); 108 windowMovingBoost_ = true; 109 break; 110 } 111 default: { 112 // doing nothing 113 } 114 } 115 } 116 #endif 117 } 118 TrigSlide(WindowType type, bool isOn)119 void TrigSlide(WindowType type, bool isOn) 120 { 121 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 122 if (type == WindowType::WINDOW_TYPE_STATUS_BAR || type == WindowType::WINDOW_TYPE_PANEL) { 123 StatusBarDrag(isOn); 124 } 125 if (type == WindowType::WINDOW_TYPE_DESKTOP || type == WindowType::WINDOW_TYPE_KEYGUARD) { 126 Slide(isOn); 127 } 128 #endif 129 } 130 private: 131 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE StatusBarDrag(bool isOn)132 void StatusBarDrag(bool isOn) 133 { 134 static auto lastRequestPerfTime = std::chrono::steady_clock::now(); 135 static auto eventRunner = AppExecFwk::EventRunner::GetMainEventRunner(); 136 static auto handler = std::make_shared<AppExecFwk::EventHandler>(eventRunner); 137 static auto task = []() { 138 std::unordered_map<std::string, std::string> mapPayload; 139 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_STATUS_BAR_DRAG_CODE, 140 1, mapPayload); 141 }; 142 auto current = std::chrono::steady_clock::now(); 143 bool isTimeOut = std::chrono::duration_cast<std::chrono::milliseconds>(current - lastRequestPerfTime). 144 count() > SLIDE_PERF_TIME_OUT; 145 if (isTimeOut && isOn) { 146 handler->RemoveTask(TASK_NAME); 147 lastRequestPerfTime = current; 148 std::unordered_map<std::string, std::string> mapPayload; 149 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_STATUS_BAR_DRAG_CODE, 150 0, mapPayload); 151 } 152 if (!isOn) { 153 std::unordered_map<std::string, std::string> mapPayload; 154 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_STATUS_BAR_DRAG_CODE, 155 0, mapPayload); 156 // 990 is the animation duration. 157 handler->PostTask(task, TASK_NAME, 990, AppExecFwk::EventQueue::Priority::HIGH); 158 } 159 } 160 Slide(bool isOn)161 void Slide(bool isOn) 162 { 163 static auto lastRequestPerfTime = std::chrono::steady_clock::now(); 164 auto current = std::chrono::steady_clock::now(); 165 bool isTimeOut = std::chrono::duration_cast<std::chrono::milliseconds>(current - lastRequestPerfTime). 166 count() > SLIDE_PERF_TIME_OUT; 167 if (isTimeOut && isOn) { 168 lastRequestPerfTime = current; 169 std::unordered_map<std::string, std::string> mapPayload; 170 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_SLIDE_CODE, 171 SlideEventStatus::SLIDE_EVENT_ON, mapPayload); 172 } 173 if (!isOn) { 174 std::unordered_map<std::string, std::string> mapPayload; 175 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_SLIDE_CODE, 176 SlideEventStatus::SLIDE_EVENT_OFF, mapPayload); 177 } 178 } 179 RequestPerf(int32_t code, int64_t timeOut)180 void RequestPerf(int32_t code, int64_t timeOut) 181 { 182 auto currentTime = std::chrono::steady_clock::now(); 183 bool isTimeOut = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastRequestPerfTime_). 184 count() > timeOut; 185 if (isTimeOut) { 186 std::unordered_map<std::string, std::string> mapPayload; 187 // 0 means doing action. 188 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(code, 0, mapPayload); 189 lastRequestPerfTime_ = currentTime; 190 } 191 } 192 ClosePerf(int32_t code)193 void ClosePerf(int32_t code) 194 { 195 std::unordered_map<std::string, std::string> mapPayload; 196 // 1 means stop action. 197 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(code, 1, mapPayload); 198 } 199 200 std::chrono::steady_clock::time_point lastRequestPerfTime_ = std::chrono::steady_clock::now(); 201 bool windowMovingBoost_ = false; 202 bool windowDragBoost_ = false; 203 #endif 204 }; 205 } // namespace Rosen 206 } // namespace OHOS 207 #endif // OHOS_ROSEN_RESSCHED_REPORT_H 208