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