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