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
16import { SpSystemTrace } from '../SpSystemTrace';
17import { TraceRow } from '../trace/base/TraceRow';
18import { info } from '../../../log/Log';
19import { renders } from '../../database/ui-worker/ProcedureWorker';
20import { FpsRender, FpsStruct } from '../../database/ui-worker/ProcedureWorkerFPS';
21import { getFps } from '../../database/sql/SqlLite.sql';
22
23export class SpFpsChart {
24  private trace: SpSystemTrace;
25
26  constructor(trace: SpSystemTrace) {
27    this.trace = trace;
28  }
29
30  async init(): Promise<void> {
31    let res = await getFps();
32    if (res.length === 0) {
33      return;
34    }
35    let startTime = new Date().getTime();
36    let fpsRow = TraceRow.skeleton<FpsStruct>();
37    fpsRow.rowId = 'fps';
38    fpsRow.rowType = TraceRow.ROW_TYPE_FPS;
39    fpsRow.rowParentId = '';
40    FpsStruct.maxFps = 0;
41    fpsRow.style.height = '40px';
42    fpsRow.name = 'FPS'; //@ts-ignore
43    fpsRow.supplier = (): Promise<Array<unknown>> => new Promise<Array<unknown>>((resolve, reject) => resolve(res));
44    fpsRow.favoriteChangeHandler = this.trace.favoriteChangeHandler;
45    fpsRow.selectChangeHandler = this.trace.selectChangeHandler;
46    fpsRow.focusHandler = (ev): void => {
47      let tip = '';
48      if (FpsStruct.hoverFpsStruct) {
49        tip = `<span>${FpsStruct.hoverFpsStruct.fps || 0}</span> `;
50      }
51      this.trace?.displayTip(fpsRow, FpsStruct.hoverFpsStruct, tip);
52    };
53    fpsRow.findHoverStruct = (): void => {
54      FpsStruct.hoverFpsStruct = fpsRow.getHoverStruct();
55    };
56    fpsRow.onThreadHandler = (useCache): void => {
57      let context: CanvasRenderingContext2D;
58      if (fpsRow.currentContext) {
59        context = fpsRow.currentContext;
60      } else {
61        context = fpsRow.collect ? this.trace.canvasFavoritePanelCtx! : this.trace.canvasPanelCtx!;
62      }
63      fpsRow.canvasSave(context);
64      (renders.fps as FpsRender).renderMainThread(
65        {
66          context: context,
67          useCache: useCache,
68          type: 'fps0',
69        },
70        fpsRow
71      );
72      fpsRow.canvasRestore(context, this.trace);
73    };
74    this.trace.rowsEL?.appendChild(fpsRow);
75    let durTime = new Date().getTime() - startTime;
76    info('The time to load the FPS data is: ', durTime);
77  }
78}
79