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 { ColorUtils } from '../../component/trace/base/ColorUtils'; 17import { 18 BaseStruct, 19 dataFilterHandler, 20 drawLoadingFrame, 21 isFrameContainPoint, 22 Render, 23 RequestMessage, 24} from './ProcedureWorkerCommon'; 25import { TraceRow } from '../../component/trace/base/TraceRow'; 26import {SpSystemTrace} from "../../component/SpSystemTrace"; 27 28export class FreqRender extends Render { 29 renderMainThread( 30 freqReq: { 31 context: CanvasRenderingContext2D; 32 useCache: boolean; 33 type: string; 34 }, 35 row: TraceRow<CpuFreqStruct> 36 ) { 37 let freqList = row.dataList; 38 let freqFilter = row.dataListCache; 39 dataFilterHandler(freqList, freqFilter, { 40 startKey: 'startNS', 41 durKey: 'dur', 42 startNS: TraceRow.range?.startNS ?? 0, 43 endNS: TraceRow.range?.endNS ?? 0, 44 totalNS: TraceRow.range?.totalNS ?? 0, 45 frame: row.frame, 46 paddingTop: 5, 47 useCache: freqReq.useCache || !(TraceRow.range?.refresh ?? false), 48 }); 49 drawLoadingFrame(freqReq.context, freqFilter, row); 50 freqReq.context.beginPath(); 51 let find = false; 52 for (let re of freqFilter) { 53 if (row.isHover && re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) { 54 CpuFreqStruct.hoverCpuFreqStruct = re; 55 find = true; 56 } 57 CpuFreqStruct.draw(freqReq.context, re); 58 } 59 if (!find && row.isHover) CpuFreqStruct.hoverCpuFreqStruct = undefined; 60 freqReq.context.closePath(); 61 let s = CpuFreqStruct.maxFreqName; 62 let textMetrics = freqReq.context.measureText(s); 63 freqReq.context.globalAlpha = 0.8; 64 freqReq.context.fillStyle = '#f0f0f0'; 65 freqReq.context.fillRect(0, 5, textMetrics.width + 8, 18); 66 freqReq.context.globalAlpha = 1; 67 freqReq.context.fillStyle = '#333'; 68 freqReq.context.textBaseline = 'middle'; 69 freqReq.context.fillText(s, 4, 5 + 9); 70 } 71} 72export function CpuFreqStructOnClick(clickRowType: string, sp: SpSystemTrace) { 73 return new Promise((resolve, reject) => { 74 if (clickRowType === TraceRow.ROW_TYPE_CPU_FREQ && CpuFreqStruct.hoverCpuFreqStruct) { 75 CpuFreqStruct.selectCpuFreqStruct = CpuFreqStruct.hoverCpuFreqStruct; 76 sp.traceSheetEL?.displayFreqData(); 77 sp.timerShaftEL?.modifyFlagList(undefined); 78 reject(); 79 }else{ 80 resolve(null); 81 } 82 }); 83} 84export class CpuFreqStruct extends BaseStruct { 85 static maxFreq: number = 0; 86 static maxFreqName: string = '0 GHz'; 87 static hoverCpuFreqStruct: CpuFreqStruct | undefined; 88 static selectCpuFreqStruct: CpuFreqStruct | undefined; 89 cpu: number | undefined; 90 value: number | undefined; 91 startNS: number | undefined; 92 dur: number | undefined; //自补充,数据库没有返回 93 94 static draw(freqContext: CanvasRenderingContext2D, data: CpuFreqStruct) { 95 if (data.frame) { 96 let width = data.frame.width || 0; 97 let index = data.cpu || 0; 98 index += 2; 99 freqContext.fillStyle = ColorUtils.colorForTid(index); 100 freqContext.strokeStyle = ColorUtils.colorForTid(index); 101 if (data === CpuFreqStruct.hoverCpuFreqStruct || data === CpuFreqStruct.selectCpuFreqStruct) { 102 freqContext.lineWidth = 1; 103 freqContext.globalAlpha = 0.6; 104 let drawHeight: number = Math.floor( 105 ((data.value || 0) * (data.frame.height || 0) * 1.0) / CpuFreqStruct.maxFreq 106 ); 107 freqContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight); 108 freqContext.beginPath(); 109 freqContext.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true); 110 freqContext.fill(); 111 freqContext.globalAlpha = 1.0; 112 freqContext.stroke(); 113 freqContext.beginPath(); 114 freqContext.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 115 freqContext.lineWidth = 3; 116 freqContext.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight); 117 freqContext.stroke(); 118 } else { 119 freqContext.globalAlpha = 0.6; 120 freqContext.lineWidth = 1; 121 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / CpuFreqStruct.maxFreq); 122 freqContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight); 123 } 124 } 125 freqContext.globalAlpha = 1.0; 126 freqContext.lineWidth = 1; 127 } 128} 129 130const textPadding = 2; 131