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 { TraceRow } from '../../component/trace/base/TraceRow.js'; 18import { 19 BaseStruct, 20 isFrameContainPoint, 21 ns2x, 22 Render, 23 RequestMessage, 24 drawString, 25} from './ProcedureWorkerCommon.js'; 26export class SoRender extends Render { 27 renderMainThread( 28 req: { 29 useCache: boolean; 30 context: CanvasRenderingContext2D; 31 type: string; 32 }, 33 row: TraceRow<SoStruct> 34 ) { 35 let soList = row.dataList; 36 let soFilter = row.dataListCache; 37 soDataFilter( 38 soList, 39 soFilter, 40 TraceRow.range!.startNS, 41 TraceRow.range!.endNS, 42 TraceRow.range!.totalNS, 43 row.frame, 44 req.useCache || !TraceRow.range!.refresh 45 ); 46 req.context.beginPath(); 47 let soFind = false; 48 for (let so of soFilter) { 49 SoStruct.draw(req.context, so); 50 if (row.isHover) { 51 if (so.dur === 0 || so.dur === null || so.dur === undefined) { 52 if ( 53 so.frame && 54 row.hoverX >= so.frame.x - 5 && 55 row.hoverX <= so.frame.x + 5 && 56 row.hoverY >= so.frame.y && 57 row.hoverY <= so.frame.y + so.frame.height 58 ) { 59 SoStruct.hoverSoStruct = so; 60 soFind = true; 61 } 62 } else { 63 if (so.frame && isFrameContainPoint(so.frame, row.hoverX, row.hoverY)) { 64 SoStruct.hoverSoStruct = so; 65 soFind = true; 66 } 67 } 68 } 69 } 70 if (!soFind && row.isHover) SoStruct.hoverSoStruct = undefined; 71 req.context.closePath(); 72 } 73 74 render(req: RequestMessage, list: Array<any>, filter: Array<any>) {} 75} 76 77export function soDataFilter( 78 soList: Array<SoStruct>, 79 soFilter: Array<SoStruct>, 80 startNS: number, 81 endNS: number, 82 totalNS: number, 83 frame: any, 84 use: boolean 85) { 86 if (use && soFilter.length > 0) { 87 for (let i = 0, len = soFilter.length; i < len; i++) { 88 if ((soFilter[i].startTs || 0) + (soFilter[i].dur || 0) >= startNS && (soFilter[i].startTs || 0) <= endNS) { 89 SoStruct.setSoFrame(soFilter[i], 0, startNS, endNS, totalNS, frame); 90 } else { 91 soFilter[i].frame = undefined; 92 } 93 } 94 return; 95 } 96 soFilter.length = 0; 97 if (soList) { 98 let groups = soList 99 .filter((it) => (it.startTs ?? 0) + (it.dur ?? 0) >= startNS && (it.startTs ?? 0) <= endNS) 100 .map((it) => { 101 SoStruct.setSoFrame(it, 0, startNS, endNS, totalNS, frame); 102 return it; 103 }) 104 .reduce((pre: any, current, index, arr) => { 105 if(current.frame) { 106 (pre[`${current.frame.x}-${current.depth}`] = pre[`${current.frame.x}-${current.depth}`] || []).push(current); 107 } 108 return pre; 109 }, {}); 110 Reflect.ownKeys(groups).map((kv) => { 111 let arr = groups[kv].sort((a: any, b: any) => b.dur - a.dur); 112 soFilter.push(arr[0]); 113 }); 114 } 115} 116 117export class SoStruct extends BaseStruct{ 118 static hoverSoStruct: SoStruct | undefined; 119 static selectSoStruct: SoStruct | undefined; 120 textMetricsWidth: number | undefined; 121 depth: number | undefined; 122 dur: number | undefined; 123 soName: string | undefined; 124 process: string | undefined; 125 startTs: number | undefined; 126 tid: number | undefined; 127 pid: number | undefined; 128 itid: number | undefined; 129 130 static setSoFrame(soNode: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 131 let x1: number, x2: number; 132 if ((soNode.startTs || 0) > startNS && (soNode.startTs || 0) < endNS) { 133 x1 = ns2x(soNode.startTs || 0, startNS, endNS, totalNS, frame); 134 } else { 135 x1 = 0; 136 } 137 if ( 138 (soNode.startTs || 0) + (soNode.dur || 0) > startNS && 139 (soNode.startTs || 0) + (soNode.dur || 0) < endNS 140 ) { 141 x2 = ns2x((soNode.startTs || 0) + (soNode.dur || 0), startNS, endNS, totalNS, frame); 142 } else { 143 x2 = frame.width; 144 } 145 if (!soNode.frame) { 146 soNode.frame = {}; 147 } 148 let getV: number = x2 - x1 < 1 ? 1 : x2 - x1; 149 soNode.frame.x = Math.floor(x1); 150 soNode.frame.y = soNode.depth * 20; 151 soNode.frame.width = Math.ceil(getV); 152 soNode.frame.height = 20; 153 } 154 155 static draw(ctx: CanvasRenderingContext2D, data: SoStruct) { 156 if (data.frame) { 157 if (data.dur === undefined || data.dur === null) { 158 } else { 159 ctx.globalAlpha = 1; 160 ctx.fillStyle = ColorUtils.FUNC_COLOR[ColorUtils.hashFunc(data.soName || '', 0, ColorUtils.FUNC_COLOR.length)]; 161 let textColor = ColorUtils.FUNC_COLOR[ColorUtils.hashFunc(data.soName || '', 0, ColorUtils.FUNC_COLOR.length)]; 162 let miniHeight = 20; 163 if (SoStruct.hoverSoStruct && data.soName == SoStruct.hoverSoStruct.soName) { 164 ctx.globalAlpha = 0.7; 165 } 166 ctx.fillRect(data.frame.x, data.frame.y, data.frame.width, miniHeight - padding * 2); 167 if (data.frame.width > 10) { 168 ctx.strokeStyle = '#fff'; 169 ctx.lineWidth = 1; 170 ctx.strokeRect(data.frame.x, data.frame.y, data.frame.width, miniHeight - padding * 2); 171 ctx.fillStyle = ColorUtils.funcTextColor(textColor); 172 drawString(ctx, `${data.soName || ''}`, 5, data.frame, data); 173 } 174 if (data === SoStruct.selectSoStruct) { 175 ctx.strokeStyle = '#000'; 176 ctx.lineWidth = 2; 177 ctx.strokeRect(data.frame.x, data.frame.y + 1, data.frame.width, miniHeight - padding * 2 - 2); 178 } 179 } 180 } 181 } 182 183} 184 185const padding = 1; 186