• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {BaseStruct, ns2x, Rect} from "./ProcedureWorkerCommon.js";
17
18export function fps(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any,use:boolean) {
19    if(use && res.length > 0){
20        res.forEach(it => FpsStruct.setFrame(it, 5, startNS, endNS, totalNS, frame));
21        return;
22    }
23    FpsStruct.maxFps = 0
24    res.length = 0 ;
25    if (list) {
26        for (let i = 0, len = list.length; i < len; i++) {
27            let it = list[i];
28            if ((it.fps || 0) > FpsStruct.maxFps) {
29                FpsStruct.maxFps = it.fps || 0
30            }
31            if (i === list.length - 1) {
32                it.dur = (endNS || 0) - (it.startNS || 0)
33            } else {
34                it.dur = (list[i + 1].startNS || 0) - (it.startNS || 0)
35            }
36            if ((it.startNS || 0) + (it.dur || 0) > (startNS) && (it.startNS || 0) < (endNS)) {
37                FpsStruct.setFrame(list[i], 5, startNS, endNS, totalNS, frame)
38                if (i > 0 && ((list[i - 1].frame?.x || 0) == (list[i].frame?.x || 0) && (list[i - 1].frame?.width || 0) == (list[i].frame?.width || 0))) {
39
40                } else {
41                    res.push(list[i])
42                }
43            }
44        }
45    }
46}
47
48export class FpsStruct extends BaseStruct {
49    static maxFps: number = 0
50    static maxFpsName: string = "0 FPS"
51    static hoverFpsStruct: FpsStruct | undefined;
52    static selectFpsStruct: FpsStruct | undefined;
53    fps: number | undefined
54    startNS: number | undefined = 0
55    dur: number | undefined //自补充,数据库没有返回
56
57    static draw(ctx: CanvasRenderingContext2D, data: FpsStruct) {
58        if (data.frame) {
59            let width = data.frame.width || 0;
60            ctx.fillStyle = '#535da6'
61            ctx.strokeStyle = '#535da6'
62            if (data.startNS === FpsStruct.hoverFpsStruct?.startNS) {
63                ctx.lineWidth = 1;
64                ctx.globalAlpha = 0.6;
65                let drawHeight: number = ((data.fps || 0) * (data.frame.height || 0) * 1.0) / FpsStruct.maxFps;
66                ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight)
67                ctx.beginPath()
68                ctx.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true)
69                ctx.fill()
70                ctx.globalAlpha = 1.0;
71                ctx.stroke();
72                ctx.beginPath()
73                ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight);
74                ctx.lineWidth = 3;
75                ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight)
76                ctx.stroke();
77            } else {
78                ctx.globalAlpha = 0.6;
79                ctx.lineWidth = 1;
80                let drawHeight: number = ((data.fps || 0) * (data.frame.height || 0) * 1.0) / FpsStruct.maxFps;
81                ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight)
82            }
83        }
84        ctx.globalAlpha = 1.0;
85        ctx.lineWidth = 1;
86    }
87
88    static setFrame(node: FpsStruct, padding: number, startNS: number, endNS: number, totalNS: number, frame: Rect) {
89        let x1: number, x2: number;
90        if ((node.startNS || 0) < startNS) {
91            x1 = 0;
92        } else {
93            x1 = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame);
94        }
95        if ((node.startNS || 0) + (node.dur || 0) > endNS) {
96            x2 = frame.width;
97        } else {
98            x2 = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame);
99        }
100        let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1;
101        let rectangle: Rect = new Rect(Math.floor(x1), Math.ceil(frame.y + padding), Math.ceil(getV), Math.floor(frame.height - padding * 2));
102        node.frame = rectangle;
103    }
104}
105
106const textPadding = 2;