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