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.js"; 17import {BaseStruct} from "./BaseStruct.js"; 18 19import {ns2x} from "../component/trace/TimerShaftElement.js"; 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(ctx: CanvasRenderingContext2D, data: FpsStruct) { 31 if (data.frame) { 32 let width = data.frame.width || 0; 33 ctx.fillStyle = '#535da6' 34 ctx.strokeStyle = '#535da6' 35 if (data.startNS === FpsStruct.hoverFpsStruct?.startNS) { 36 ctx.lineWidth = 1; 37 ctx.globalAlpha = 0.6; 38 let drawHeight: number = ((data.fps || 0) * (data.frame.height || 0) * 1.0) / FpsStruct.maxFps; 39 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 40 ctx.beginPath() 41 ctx.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true) 42 ctx.fill() 43 ctx.globalAlpha = 1.0; 44 ctx.stroke(); 45 ctx.beginPath() 46 ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 47 ctx.lineWidth = 3; 48 ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight) 49 ctx.stroke(); 50 } else { 51 ctx.globalAlpha = 0.6; 52 ctx.lineWidth = 1; 53 let drawHeight: number = ((data.fps || 0) * (data.frame.height || 0) * 1.0) / FpsStruct.maxFps; 54 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 55 } 56 } 57 ctx.globalAlpha = 1.0; 58 ctx.lineWidth = 1; 59 } 60 61 static setFrame(node: FpsStruct, padding: number, startNS: number, endNS: number, totalNS: number, frame: Rect) { 62 let x1: number, x2: number; 63 if ((node.startNS || 0) < startNS) { 64 x1 = 0; 65 } else { 66 x1 = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame); 67 } 68 if ((node.startNS || 0) + (node.dur || 0) > endNS) { 69 x2 = frame.width; 70 } else { 71 x2 = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 72 } 73 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 74 let rectangle: Rect = new Rect(Math.floor(x1), Math.ceil(frame.y + padding), Math.ceil(getV), Math.floor(frame.height - padding * 2)); 75 node.frame = rectangle; 76 } 77} 78 79const textPadding = 2; 80 81