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 #include "AnimatorSceneDataProcessor.h"
17 #include "JankAnimatorMonitor.h"
18 #include "AppLaunchSceneDataProcessor.h"
19 #include "AppLaunchMonitor.h"
20 #include "AppLaunchSceneDbAdapter.h"
21 #include "SceneTimerOhImpl.h"
22 #include "AppTimerAdapter.h"
23 #include "AppStartReporter.h"
24 #include "JankAnimatorReporter.h"
25 #include "NormalContext.h"
26 #include "AppStartReporterAdapter.h"
27 #include "JankAnimatorReporterAdapter.h"
28 #include "JlogId.h"
29 #include "ActionId.h"
30 
31 namespace {
32     enum TimerUser {
33         APP_START,
34     };
35 }
36 
CreateContext()37 void NormalContext::CreateContext()
38 {
39     /* init monitor */
40     NormalContext::CommonParts common = MakeCommonParts();
41     /* hold on IEventObservable */
42     this->eventObservable = common.eventsPoster;
43 
44     InitAppStartMonitor(common);
45     InitJankAnimatorMonitor(common);
46 }
47 
MakeCommonParts()48 NormalContext::CommonParts NormalContext::MakeCommonParts()
49 {
50     ThrExecutor* thr = new ThrExecutor();
51     ISceneTimerInfrastructure* sceneTimerInfrastructure = new SceneTimerOhImpl();
52     EventsPoster* eventsPoster = new EventsPoster();
53     return NormalContext::CommonParts(thr, sceneTimerInfrastructure, eventsPoster);
54 }
55 
InitAppStartMonitor(const NormalContext::CommonParts & common)56 void NormalContext::InitAppStartMonitor(const NormalContext::CommonParts& common)
57 {
58     IMonitor* appStartMonitor = MakeAppStartMonitor(common);
59     RegisterMonitorByLogID(static_cast<int>(JLID_GRAPHIC_INTERACTION_RESPONSE_LATENCY), appStartMonitor);
60     RegisterMonitorByLogID(static_cast<int>(JLID_START_ABILITY), appStartMonitor);
61     RegisterMonitorByLogID(static_cast<int>(JLID_AAFWK_APP_STARTUP_TYPE), appStartMonitor);
62     RegisterMonitorByLogID(static_cast<int>(JLID_AAFWK_PROCESS_START), appStartMonitor);
63     RegisterMonitorByLogID(static_cast<int>(JLID_APP_ATTACH), appStartMonitor);
64     RegisterMonitorByLogID(static_cast<int>(JLID_APP_FOREGROUND), appStartMonitor);
65     RegisterMonitorByLogID(static_cast<int>(JLID_ABILITY_ONFOREGROUND), appStartMonitor);
66     RegisterMonitorByLogID(static_cast<int>(JLID_WINDOWMANAGER_START_WINDOW), appStartMonitor);
67     RegisterMonitorByLogID(static_cast<int>(JLID_GRAPHIC_FIRST_FRAME_DRAWN), appStartMonitor);
68     RegisterMonitorByLogID(static_cast<int>(JLID_AAFWK_DRAWN_COMPLETED), appStartMonitor);
69     RegisterMonitorByLogID(static_cast<int>(JLID_GRAPHIC_INTERACTION_COMPLETED_LATENCY), appStartMonitor);
70     RegisterMonitorByLogID(static_cast<int>(JLID_ACE_INTERACTION_COMPLETED_LATENCY), appStartMonitor);
71 }
72 
MakeAppStartMonitor(const NormalContext::CommonParts & common)73 IMonitor* NormalContext::MakeAppStartMonitor(const NormalContext::CommonParts& common)
74 {
75     IAppLaunchSceneDb* db = new AppLaunchSceneDbAdapter();
76     ITimeoutExecutor* exec = common.thr;
77     AppTimerAdapter* sceneTimer = new AppTimerAdapter(TimerUser::APP_START, common.timerInfra);
78     AppLaunchSceneDataProcessor* processor = new AppLaunchSceneDataProcessor(db, exec, nullptr, sceneTimer);
79     sceneTimer->SetCb(processor);
80     IAppStartReportInfrastructure* infrastructure = new AppStartReporter();
81     IAppStartReporter* reporter = new AppStartReporterAdapter(infrastructure, common.eventsPoster);
82     AppLaunchMonitor* appStartMonitor = new AppLaunchMonitor(this, common.thr, reporter, processor);
83 
84     processor->SetMetricReporter(appStartMonitor);
85     return appStartMonitor;
86 }
87 
InitJankAnimatorMonitor(const NormalContext::CommonParts & common)88 void NormalContext::InitJankAnimatorMonitor(const NormalContext::CommonParts& common)
89 {
90     IMonitor* animatorMonitor = MakeJankAnimatorMonitor(common);
91     RegisterMonitorByLogID(static_cast<int>(JLID_ACE_INTERACTION_APP_JANK), animatorMonitor);
92     RegisterMonitorByLogID(static_cast<int>(JLID_GRAPHIC_INTERACTION_RENDER_JANK), animatorMonitor);
93     RegisterMonitorByLogID(static_cast<int>(JLID_WINDOWMANAGER_FOCUS_WINDOW), animatorMonitor);
94 }
95 
MakeJankAnimatorMonitor(const NormalContext::CommonParts & common)96 IMonitor* NormalContext::MakeJankAnimatorMonitor(const NormalContext::CommonParts& common)
97 {
98     AnimatorSceneDataProcessor* dataProcessor = new AnimatorSceneDataProcessor();
99     IJankAnimatorReportInfrastructure* reporterImpl = new JankAnimatorReporter();
100     IJankAnimatorReporter* reporter = new JankAnimatorReporterAdapter(reporterImpl, common.eventsPoster);
101     JankAnimatorMonitor* animatorMonitor = new JankAnimatorMonitor(common.thr, dataProcessor, reporter);
102     dataProcessor->SetCb(animatorMonitor);
103     return animatorMonitor;
104 }