• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SOC_PERF_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 SOC_PERF_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_SLIDE_CODE = 34;
37     const std::string TASK_NAME = "SlideOff";
38 }
39 #endif
40 
41 class ResSchedReport {
42     WM_DECLARE_SINGLE_INSTANCE(ResSchedReport);
43     public:
StopPerfIfNeed()44     void StopPerfIfNeed()
45     {
46 #ifdef SOC_PERF_ENABLE
47         if (windowDragBoost_) {
48             ClosePerf(PERF_DRAG_CODE);
49             windowDragBoost_ = false;
50         }
51         if (windowMovingBoost_) {
52             ClosePerf(PERF_MOVE_CODE);
53             windowMovingBoost_ = false;
54         }
55 #endif
56     }
57 
TrigClick()58     void TrigClick()
59     {
60 #ifdef SOC_PERF_ENABLE
61         std::unordered_map<std::string, std::string> mapPayload;
62         // 2 means click event.
63         OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_CLICK_NORMAL_CODE, 2, mapPayload);
64 #endif
65     }
66 
RequestPerfIfNeed(WindowSizeChangeReason reason,WindowType type,WindowMode mode)67     void RequestPerfIfNeed(WindowSizeChangeReason reason, WindowType type, WindowMode mode)
68     {
69 #ifdef SOC_PERF_ENABLE
70         if (WindowHelper::IsMainFloatingWindow(type, mode) || WindowHelper::IsSplitWindowMode(mode)) {
71             switch (reason) {
72                 case WindowSizeChangeReason::DRAG_END: {
73                     if (windowDragBoost_) {
74                         ClosePerf(PERF_DRAG_CODE);
75                         windowDragBoost_ = false;
76                     }
77                     break;
78                 }
79                 case WindowSizeChangeReason::DRAG_START:
80                     [[fallthrough]];
81                 case WindowSizeChangeReason::DRAG: {
82                     RequestPerf(PERF_DRAG_CODE, PERF_TIME_OUT);
83                     windowDragBoost_ = true;
84                     break;
85                 }
86                 case WindowSizeChangeReason::MOVE: {
87                     RequestPerf(PERF_MOVE_CODE, PERF_TIME_OUT);
88                     windowMovingBoost_ = true;
89                     break;
90                 }
91                 default: {
92                     // doing nothing
93                 }
94             }
95         }
96 #endif
97     }
98 
TrigSlide(WindowType type,bool isOn)99     void TrigSlide(WindowType type, bool isOn)
100     {
101 #ifdef SOC_PERF_ENABLE
102         if (type != WindowType::WINDOW_TYPE_STATUS_BAR) {
103             return;
104         }
105         static auto lastRequestPerfTime = std::chrono::steady_clock::now();
106         static auto eventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
107         static auto handler = std::make_shared<AppExecFwk::EventHandler>(eventRunner);
108         static auto task = []() {
109             std::unordered_map<std::string, std::string> mapPayload;
110             OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_SLIDE_CODE, 1, mapPayload);
111         };
112         auto current = std::chrono::steady_clock::now();
113         bool isTimeOut = std::chrono::duration_cast<std::chrono::milliseconds>(current - lastRequestPerfTime).
114             count() > SLIDE_PERF_TIME_OUT;
115         if (isTimeOut && isOn) {
116             handler->RemoveTask(TASK_NAME);
117             lastRequestPerfTime = current;
118             std::unordered_map<std::string, std::string> mapPayload;
119             OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(PERF_SLIDE_CODE, 0, mapPayload);
120         }
121         if (!isTimeOut && !isOn) {
122             // 500 is the animation duration.
123             handler->PostTask(task, TASK_NAME, 500, AppExecFwk::EventQueue::Priority::HIGH);
124         }
125 #endif
126     }
127 private:
128 #ifdef SOC_PERF_ENABLE
RequestPerf(int32_t code,int64_t timeOut)129     void RequestPerf(int32_t code, int64_t timeOut)
130     {
131         auto currentTime = std::chrono::steady_clock::now();
132         bool isTimeOut = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastRequestPerfTime_).
133             count() > timeOut;
134         if (isTimeOut) {
135             std::unordered_map<std::string, std::string> mapPayload;
136             // 0 means doing action.
137             OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(code, 0, mapPayload);
138             lastRequestPerfTime_ = currentTime;
139         }
140     }
141 
ClosePerf(int32_t code)142     void ClosePerf(int32_t code)
143     {
144         std::unordered_map<std::string, std::string> mapPayload;
145         // 1 means stop action.
146         OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(code, 1, mapPayload);
147     }
148 
149     std::chrono::steady_clock::time_point lastRequestPerfTime_ = std::chrono::steady_clock::now();
150     bool windowMovingBoost_ = false;
151     bool windowDragBoost_ = false;
152 #endif
153 };
154 } // namespace Rosen
155 } // namespace OHOS
156 #endif // OHOS_ROSEN_RESSCHED_REPORT_H
157