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 { 17 BaseStruct, 18 dataFilterHandler, 19 drawLoadingFrame, 20 drawString, 21 isFrameContainPoint, 22 Render, 23} from './ProcedureWorkerCommon'; 24import { TraceRow } from '../../component/trace/base/TraceRow'; 25import { ColorUtils } from '../../component/trace/base/ColorUtils'; 26import { SpSystemTrace } from '../../component/SpSystemTrace'; 27 28export class PerfToolRender extends Render { 29 renderMainThread( 30 perfReq: { 31 context: CanvasRenderingContext2D; 32 useCache: boolean; 33 type: string; 34 index: number; 35 }, 36 row: TraceRow<PerfToolStruct> 37 ): void { 38 PerfToolStruct.index = perfReq.index; 39 let perfToolList = row.dataList; 40 let perfToolFilter = row.dataListCache; 41 dataFilterHandler(perfToolList, perfToolFilter, { 42 startKey: 'startTs', 43 durKey: 'dur', 44 startNS: TraceRow.range?.startNS ?? 0, 45 endNS: TraceRow.range?.endNS ?? 0, 46 totalNS: TraceRow.range?.totalNS ?? 0, 47 frame: row.frame, 48 paddingTop: 3, 49 useCache: perfReq.useCache || !(TraceRow.range?.refresh ?? false), 50 }); 51 drawLoadingFrame(perfReq.context, perfToolFilter, row); 52 perfReq.context.beginPath(); 53 let find = false; 54 for (let re of perfToolFilter) { 55 PerfToolStruct.draw(perfReq.context, re); 56 if (row.isHover && re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) { 57 PerfToolStruct.hoverPerfToolStruct = re; 58 find = true; 59 } 60 } 61 if (!find && row.isHover) { 62 PerfToolStruct.hoverPerfToolStruct = undefined; 63 } 64 perfReq.context.closePath(); 65 } 66} 67export function PerfToolsStructOnClick( 68 clickRowType: string, 69 sp: SpSystemTrace, 70 entry?: PerfToolStruct, 71): Promise<unknown> { 72 73 return new Promise((resolve, reject) => { 74 if (clickRowType === TraceRow.ROW_TYPE_PERF_TOOL && (PerfToolStruct.hoverPerfToolStruct || entry)) { 75 PerfToolStruct.selectPerfToolStruct = entry || PerfToolStruct.hoverPerfToolStruct; 76 sp.traceSheetEL?.displayPerfToolsData(PerfToolStruct.selectPerfToolStruct!); 77 sp.timerShaftEL?.modifyFlagList(undefined); 78 reject(new Error()); 79 } else { 80 resolve(null); 81 } 82 }); 83} 84export class PerfToolStruct extends BaseStruct { 85 static hoverPerfToolStruct: PerfToolStruct | undefined; 86 static selectPerfToolStruct: PerfToolStruct | undefined; 87 static index = 0; 88 count: string | undefined; 89 startTs: number | undefined; 90 dur: number | undefined; 91 id: number | undefined; 92 name: string | undefined; 93 94 static draw(PerfContext: CanvasRenderingContext2D, data: PerfToolStruct): void { 95 if (data.frame) { 96 let width = data.frame.width || 0; 97 PerfContext.globalAlpha = 1; 98 PerfContext.fillStyle = ColorUtils.colorForTid(PerfToolStruct.index); 99 PerfContext.fillRect(data.frame.x, data.frame.y, width, data.frame.height); 100 if (data.frame.width > 7) { 101 PerfContext.textBaseline = 'middle'; 102 PerfContext.lineWidth = 1; 103 let countText: string | undefined = ''; 104 if (data.count) { 105 countText = `${data.count}`; 106 } 107 PerfContext.fillStyle = ColorUtils.funcTextColor('#000'); 108 drawString(PerfContext, countText, 2, data.frame, data); 109 } 110 if ( 111 data.id === PerfToolStruct.selectPerfToolStruct?.id && 112 data.name === PerfToolStruct.selectPerfToolStruct?.name 113 ) { 114 PerfContext.strokeStyle = '#000'; 115 PerfContext.lineWidth = 2; 116 PerfContext.strokeRect(data.frame.x, data.frame.y + 1, data.frame.width, data.frame.height - 2); 117 } 118 } 119 } 120 121 static isHover(clock: PerfToolStruct): boolean { 122 return clock === PerfToolStruct.hoverPerfToolStruct || clock === PerfToolStruct.selectPerfToolStruct; 123 } 124} 125