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 { Rect } from '../component/trace/timer-shaft/Rect';
17import { BaseStruct } from './BaseStruct';
18
19import { ns2x } from '../component/trace/TimerShaftElement';
20
21export class FpsStruct extends BaseStruct {
22  static maxFps: number = 0;
23  static maxFpsName: string = '0 FPS';
24  static hoverFpsStruct: FpsStruct | undefined;
25  static selectFpsStruct: FpsStruct | undefined;
26  fps: number | undefined;
27  startNS: number | undefined = 0;
28  dur: number | undefined; //自补充,数据库没有返回
29
30  static draw(fpsCtx: CanvasRenderingContext2D, fpsData: FpsStruct): void {
31    if (fpsData.frame) {
32      let fpsBeanWidth = fpsData.frame.width || 0;
33      fpsCtx.fillStyle = '#535da6';
34      fpsCtx.strokeStyle = '#535da6';
35      if (fpsData.startNS === FpsStruct.hoverFpsStruct?.startNS) {
36        fpsCtx.lineWidth = 1;
37        fpsCtx.globalAlpha = 0.6;
38        let drawHeight: number = ((fpsData.fps || 0) * (fpsData.frame.height || 0) * 1.0) / FpsStruct.maxFps;
39        fpsCtx.fillRect(fpsData.frame.x, fpsData.frame.y + fpsData.frame.height - drawHeight, fpsBeanWidth, drawHeight);
40        fpsCtx.beginPath();
41        fpsCtx.arc(fpsData.frame.x, fpsData.frame.y + fpsData.frame.height - drawHeight, 3, 0, 2 * Math.PI, true);
42        fpsCtx.fill();
43        fpsCtx.globalAlpha = 1.0;
44        fpsCtx.stroke();
45        fpsCtx.beginPath();
46        fpsCtx.moveTo(fpsData.frame.x + 3, fpsData.frame.y + fpsData.frame.height - drawHeight);
47        fpsCtx.lineWidth = 3;
48        fpsCtx.lineTo(fpsData.frame.x + fpsBeanWidth, fpsData.frame.y + fpsData.frame.height - drawHeight);
49        fpsCtx.stroke();
50      } else {
51        fpsCtx.globalAlpha = 0.6;
52        fpsCtx.lineWidth = 1;
53        let drawHeight: number = ((fpsData.fps || 0) * (fpsData.frame.height || 0) * 1.0) / FpsStruct.maxFps;
54        fpsCtx.fillRect(fpsData.frame.x, fpsData.frame.y + fpsData.frame.height - drawHeight, fpsBeanWidth, drawHeight);
55      }
56    }
57    fpsCtx.globalAlpha = 1.0;
58    fpsCtx.lineWidth = 1;
59  }
60
61  static setFrame(
62    fpsStruct: FpsStruct,
63    padding: number,
64    startNS: number,
65    endNS: number,
66    totalNS: number,
67    frame: Rect
68  ): void {
69    let fpsBeanStructX1: number;
70    let fpsBeanStructX2: number;
71    if ((fpsStruct.startNS || 0) < startNS) {
72      fpsBeanStructX1 = 0;
73    } else {
74      fpsBeanStructX1 = ns2x(fpsStruct.startNS || 0, startNS, endNS, totalNS, frame);
75    }
76    if ((fpsStruct.startNS || 0) + (fpsStruct.dur || 0) > endNS) {
77      fpsBeanStructX2 = frame.width;
78    } else {
79      fpsBeanStructX2 = ns2x((fpsStruct.startNS || 0) + (fpsStruct.dur || 0), startNS, endNS, totalNS, frame);
80    }
81    let getV: number = fpsBeanStructX2 - fpsBeanStructX1 <= 1 ? 1 : fpsBeanStructX2 - fpsBeanStructX1;
82    let rectangle: Rect = new Rect(
83      Math.floor(fpsBeanStructX1),
84      Math.ceil(frame.y + padding),
85      Math.ceil(getV),
86      Math.floor(frame.height - padding * 2)
87    );
88    fpsStruct.frame = rectangle;
89  }
90}
91