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