1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { SpSystemTrace } from '../SpSystemTrace';
17fb726d48Sopenharmony_ciimport { TraceRow } from '../trace/base/TraceRow';
18fb726d48Sopenharmony_ciimport { info } from '../../../log/Log';
19fb726d48Sopenharmony_ciimport { renders } from '../../database/ui-worker/ProcedureWorker';
20fb726d48Sopenharmony_ciimport { FpsRender, FpsStruct } from '../../database/ui-worker/ProcedureWorkerFPS';
21fb726d48Sopenharmony_ciimport { getFps } from '../../database/sql/SqlLite.sql';
22fb726d48Sopenharmony_ci
23fb726d48Sopenharmony_ciexport class SpFpsChart {
24fb726d48Sopenharmony_ci  private trace: SpSystemTrace;
25fb726d48Sopenharmony_ci
26fb726d48Sopenharmony_ci  constructor(trace: SpSystemTrace) {
27fb726d48Sopenharmony_ci    this.trace = trace;
28fb726d48Sopenharmony_ci  }
29fb726d48Sopenharmony_ci
30fb726d48Sopenharmony_ci  async init(): Promise<void> {
31fb726d48Sopenharmony_ci    let res = await getFps();
32fb726d48Sopenharmony_ci    if (res.length === 0) {
33fb726d48Sopenharmony_ci      return;
34fb726d48Sopenharmony_ci    }
35fb726d48Sopenharmony_ci    let startTime = new Date().getTime();
36fb726d48Sopenharmony_ci    let fpsRow = TraceRow.skeleton<FpsStruct>();
37fb726d48Sopenharmony_ci    fpsRow.rowId = 'fps';
38fb726d48Sopenharmony_ci    fpsRow.rowType = TraceRow.ROW_TYPE_FPS;
39fb726d48Sopenharmony_ci    fpsRow.rowParentId = '';
40fb726d48Sopenharmony_ci    FpsStruct.maxFps = 0;
41fb726d48Sopenharmony_ci    fpsRow.style.height = '40px';
42fb726d48Sopenharmony_ci    fpsRow.name = 'FPS'; //@ts-ignore
43fb726d48Sopenharmony_ci    fpsRow.supplier = (): Promise<Array<unknown>> => new Promise<Array<unknown>>((resolve, reject) => resolve(res));
44fb726d48Sopenharmony_ci    fpsRow.favoriteChangeHandler = this.trace.favoriteChangeHandler;
45fb726d48Sopenharmony_ci    fpsRow.selectChangeHandler = this.trace.selectChangeHandler;
46fb726d48Sopenharmony_ci    fpsRow.focusHandler = (ev): void => {
47fb726d48Sopenharmony_ci      let tip = '';
48fb726d48Sopenharmony_ci      if (FpsStruct.hoverFpsStruct) {
49fb726d48Sopenharmony_ci        tip = `<span>${FpsStruct.hoverFpsStruct.fps || 0}</span> `;
50fb726d48Sopenharmony_ci      }
51fb726d48Sopenharmony_ci      this.trace?.displayTip(fpsRow, FpsStruct.hoverFpsStruct, tip);
52fb726d48Sopenharmony_ci    };
53fb726d48Sopenharmony_ci    fpsRow.findHoverStruct = (): void => {
54fb726d48Sopenharmony_ci      FpsStruct.hoverFpsStruct = fpsRow.getHoverStruct();
55fb726d48Sopenharmony_ci    };
56fb726d48Sopenharmony_ci    fpsRow.onThreadHandler = (useCache): void => {
57fb726d48Sopenharmony_ci      let context: CanvasRenderingContext2D;
58fb726d48Sopenharmony_ci      if (fpsRow.currentContext) {
59fb726d48Sopenharmony_ci        context = fpsRow.currentContext;
60fb726d48Sopenharmony_ci      } else {
61fb726d48Sopenharmony_ci        context = fpsRow.collect ? this.trace.canvasFavoritePanelCtx! : this.trace.canvasPanelCtx!;
62fb726d48Sopenharmony_ci      }
63fb726d48Sopenharmony_ci      fpsRow.canvasSave(context);
64fb726d48Sopenharmony_ci      (renders.fps as FpsRender).renderMainThread(
65fb726d48Sopenharmony_ci        {
66fb726d48Sopenharmony_ci          context: context,
67fb726d48Sopenharmony_ci          useCache: useCache,
68fb726d48Sopenharmony_ci          type: 'fps0',
69fb726d48Sopenharmony_ci        },
70fb726d48Sopenharmony_ci        fpsRow
71fb726d48Sopenharmony_ci      );
72fb726d48Sopenharmony_ci      fpsRow.canvasRestore(context, this.trace);
73fb726d48Sopenharmony_ci    };
74fb726d48Sopenharmony_ci    this.trace.rowsEL?.appendChild(fpsRow);
75fb726d48Sopenharmony_ci    let durTime = new Date().getTime() - startTime;
76fb726d48Sopenharmony_ci    info('The time to load the FPS data is: ', durTime);
77fb726d48Sopenharmony_ci  }
78fb726d48Sopenharmony_ci}
79