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(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 = 39 ((fpsBeanStructData.fps || 0) * (fpsBeanStructData.frame.height || 0) * 1.0) / FpsStruct.maxFps; 40 fpsBeanStructCanvasCtx.fillRect( 41 fpsBeanStructData.frame.x, 42 fpsBeanStructData.frame.y + fpsBeanStructData.frame.height - drawHeight, 43 fpsBeanWidth, 44 drawHeight 45 ); 46 fpsBeanStructCanvasCtx.beginPath(); 47 fpsBeanStructCanvasCtx.arc( 48 fpsBeanStructData.frame.x, 49 fpsBeanStructData.frame.y + fpsBeanStructData.frame.height - drawHeight, 50 3, 51 0, 52 2 * Math.PI, 53 true 54 ); 55 fpsBeanStructCanvasCtx.fill(); 56 fpsBeanStructCanvasCtx.globalAlpha = 1.0; 57 fpsBeanStructCanvasCtx.stroke(); 58 fpsBeanStructCanvasCtx.beginPath(); 59 fpsBeanStructCanvasCtx.moveTo( 60 fpsBeanStructData.frame.x + 3, 61 fpsBeanStructData.frame.y + fpsBeanStructData.frame.height - drawHeight 62 ); 63 fpsBeanStructCanvasCtx.lineWidth = 3; 64 fpsBeanStructCanvasCtx.lineTo( 65 fpsBeanStructData.frame.x + fpsBeanWidth, 66 fpsBeanStructData.frame.y + fpsBeanStructData.frame.height - drawHeight 67 ); 68 fpsBeanStructCanvasCtx.stroke(); 69 } else { 70 fpsBeanStructCanvasCtx.globalAlpha = 0.6; 71 fpsBeanStructCanvasCtx.lineWidth = 1; 72 let drawHeight: number = 73 ((fpsBeanStructData.fps || 0) * (fpsBeanStructData.frame.height || 0) * 1.0) / FpsStruct.maxFps; 74 fpsBeanStructCanvasCtx.fillRect( 75 fpsBeanStructData.frame.x, 76 fpsBeanStructData.frame.y + fpsBeanStructData.frame.height - drawHeight, 77 fpsBeanWidth, 78 drawHeight 79 ); 80 } 81 } 82 fpsBeanStructCanvasCtx.globalAlpha = 1.0; 83 fpsBeanStructCanvasCtx.lineWidth = 1; 84 } 85 86 static setFrame( 87 fpsBeanStructNode: FpsStruct, 88 fpsBeanFramePadding: number, 89 startNS: number, 90 endNS: number, 91 totalNS: number, 92 frame: Rect 93 ) { 94 let fpsBeanStructX1: number, fpsBeanStructX2: number; 95 if ((fpsBeanStructNode.startNS || 0) < startNS) { 96 fpsBeanStructX1 = 0; 97 } else { 98 fpsBeanStructX1 = ns2x(fpsBeanStructNode.startNS || 0, startNS, endNS, totalNS, frame); 99 } 100 if ((fpsBeanStructNode.startNS || 0) + (fpsBeanStructNode.dur || 0) > endNS) { 101 fpsBeanStructX2 = frame.width; 102 } else { 103 fpsBeanStructX2 = ns2x( 104 (fpsBeanStructNode.startNS || 0) + (fpsBeanStructNode.dur || 0), 105 startNS, 106 endNS, 107 totalNS, 108 frame 109 ); 110 } 111 let getV: number = fpsBeanStructX2 - fpsBeanStructX1 <= 1 ? 1 : fpsBeanStructX2 - fpsBeanStructX1; 112 let rectangle: Rect = new Rect( 113 Math.floor(fpsBeanStructX1), 114 Math.ceil(frame.y + fpsBeanFramePadding), 115 Math.ceil(getV), 116 Math.floor(frame.height - fpsBeanFramePadding * 2) 117 ); 118 fpsBeanStructNode.frame = rectangle; 119 } 120} 121 122const textPadding = 2; 123