• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "input_monitor.h"
17 #include "jank_frame_monitor.h"
18 #include "perf_trace.h"
19 #include "perf_utils.h"
20 #include "scene_monitor.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 
GetInstance()25 InputMonitor& InputMonitor::GetInstance()
26 {
27     static InputMonitor instance;
28     return instance;
29 }
30 
RecordInputEvent(PerfActionType type,PerfSourceType sourceType,int64_t time)31 void InputMonitor::RecordInputEvent(PerfActionType type, PerfSourceType sourceType, int64_t time)
32 {
33     std::lock_guard<std::mutex> Lock(mMutex);
34     mSourceType = sourceType;
35     if (time <= 0) {
36         time = GetCurrentRealTimeNs();
37     }
38     switch (type) {
39         case LAST_DOWN:
40             {
41                 XPERF_TRACE_SCOPED("RecordInputEvent: last_down=%lld(ns)", static_cast<long long>(time));
42                 mInputTime[LAST_DOWN] = time;
43                 break;
44             }
45         case LAST_UP:
46             {
47                 XPERF_TRACE_SCOPED("RecordInputEvent: last_up=%lld(ns)", static_cast<long long>(time));
48                 mInputTime[LAST_UP] = time;
49                 SceneMonitor::GetInstance().OnSceneChanged(SceneType::APP_RESPONSE, true);
50                 break;
51             }
52         case FIRST_MOVE:
53             {
54                 XPERF_TRACE_SCOPED("RecordInputEvent: first_move=%lld(ns)", static_cast<long long>(time));
55                 mInputTime[FIRST_MOVE] = time;
56                 break;
57             }
58         default:
59             break;
60     }
61 }
62 
GetInputTime(const std::string & sceneId,PerfActionType type,const std::string & note)63 int64_t InputMonitor::GetInputTime(const std::string& sceneId, PerfActionType type, const std::string& note)
64 {
65     std::lock_guard<std::mutex> Lock(mMutex);
66     int64_t inputTime = 0;
67     switch (type) {
68         case LAST_DOWN:
69             inputTime = mInputTime[LAST_DOWN];
70             break;
71         case LAST_UP:
72             inputTime = mInputTime[LAST_UP];
73             break;
74         case FIRST_MOVE:
75             inputTime = mInputTime[FIRST_MOVE];
76             break;
77         default:
78             break;
79     }
80     if (inputTime <= 0 || SceneMonitor::GetInstance().IsExceptResponseTime(inputTime, sceneId)) {
81         XPERF_TRACE_SCOPED("GetInputTime: now time");
82         inputTime = GetCurrentRealTimeNs();
83     }
84     return inputTime;
85 }
86 
SetVsyncTime(int64_t val)87 void InputMonitor::SetVsyncTime(int64_t val)
88 {
89     std::lock_guard<std::mutex> Lock(mMutex);
90     mVsyncTime = val;
91     return;
92 }
93 
GetSourceType()94 PerfSourceType InputMonitor::GetSourceType()
95 {
96     std::lock_guard<std::mutex> Lock(mMutex);
97     return mSourceType;
98 }
99 
GetVsyncTime()100 int64_t InputMonitor::GetVsyncTime()
101 {
102     std::lock_guard<std::mutex> Lock(mMutex);
103     return mVsyncTime;
104 }
105 
106 }
107 }