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 { 18 BaseStruct, 19 drawFlagLine, 20 drawLines, 21 drawLoading, 22 drawSelection, 23 hiPerf, 24 HiPerfStruct, 25 PerfRender, 26 RequestMessage, 27} from './ProcedureWorkerCommon.js'; 28import { TraceRow } from '../../component/trace/base/TraceRow.js'; 29 30export class HiperfProcessRender extends PerfRender { 31 renderMainThread(hiPerfProcessReq: any, row: TraceRow<HiPerfProcessStruct>) { 32 let list = row.dataList; 33 let filter = row.dataListCache; 34 let groupBy10MS = hiPerfProcessReq.scale > 30_000_000; 35 if (list && row.dataList2.length == 0) { 36 row.dataList2 = HiPerfProcessStruct.groupBy10MS(list, hiPerfProcessReq.intervalPerf); 37 } 38 hiPerf( 39 list, 40 row.dataList2, 41 filter, 42 TraceRow.range?.startNS ?? 0, 43 TraceRow.range?.endNS ?? 0, 44 row.frame, 45 groupBy10MS, 46 hiPerfProcessReq.useCache || (TraceRow.range?.refresh ?? false) 47 ); 48 hiPerfProcessReq.context.beginPath(); 49 hiPerfProcessReq.context.fillStyle = ColorUtils.FUNC_COLOR[0]; 50 hiPerfProcessReq.context.strokeStyle = ColorUtils.FUNC_COLOR[0]; 51 let path = new Path2D(); 52 let offset = groupBy10MS ? 0 : 3; 53 let find = false; 54 for (let re of filter) { 55 HiPerfProcessStruct.draw(hiPerfProcessReq.context, path, re, groupBy10MS); 56 if (row.isHover) { 57 if (re.frame && row.hoverX >= re.frame.x - offset && row.hoverX <= re.frame.x + re.frame.width + offset) { 58 HiPerfProcessStruct.hoverStruct = re; 59 find = true; 60 } 61 } 62 } 63 if (!find && row.isHover) HiPerfProcessStruct.hoverStruct = undefined; 64 groupBy10MS ? hiPerfProcessReq.context.fill(path) : hiPerfProcessReq.context.stroke(path); 65 hiPerfProcessReq.context.closePath(); 66 } 67 68 render(hiPerfProcessRequest: RequestMessage, list: Array<any>, filter: Array<any>, dataList2: Array<any>) { 69 let groupBy10MS = hiPerfProcessRequest.scale > 100_000_000; 70 if (hiPerfProcessRequest.lazyRefresh) { 71 hiPerf( 72 list, 73 dataList2, 74 filter, 75 hiPerfProcessRequest.startNS, 76 hiPerfProcessRequest.endNS, 77 hiPerfProcessRequest.frame, 78 groupBy10MS, 79 hiPerfProcessRequest.useCache || !hiPerfProcessRequest.range.refresh 80 ); 81 } else { 82 if (!hiPerfProcessRequest.useCache) { 83 hiPerf( 84 list, 85 dataList2, 86 filter, 87 hiPerfProcessRequest.startNS, 88 hiPerfProcessRequest.endNS, 89 hiPerfProcessRequest.frame, 90 groupBy10MS, 91 false 92 ); 93 } 94 } 95 if (hiPerfProcessRequest.canvas) { 96 hiPerfProcessRequest.context.clearRect(0, 0, hiPerfProcessRequest.frame.width, hiPerfProcessRequest.frame.height); 97 let arr = filter; 98 if ( 99 arr.length > 0 && 100 !hiPerfProcessRequest.range.refresh && 101 !hiPerfProcessRequest.useCache && 102 hiPerfProcessRequest.lazyRefresh 103 ) { 104 drawLoading( 105 hiPerfProcessRequest.context, 106 hiPerfProcessRequest.startNS, 107 hiPerfProcessRequest.endNS, 108 hiPerfProcessRequest.totalNS, 109 hiPerfProcessRequest.frame, 110 arr[0].startNS, 111 arr[arr.length - 1].startNS + arr[arr.length - 1].dur 112 ); 113 } 114 drawLines( 115 hiPerfProcessRequest.context, 116 hiPerfProcessRequest.xs, 117 hiPerfProcessRequest.frame.height, 118 hiPerfProcessRequest.lineColor 119 ); 120 hiPerfProcessRequest.context.stroke(); 121 hiPerfProcessRequest.context.beginPath(); 122 HiPerfProcessStruct.hoverStruct = undefined; 123 hiPerfProcessRequest.context.fillStyle = ColorUtils.FUNC_COLOR[0]; 124 hiPerfProcessRequest.context.strokeStyle = ColorUtils.FUNC_COLOR[0]; 125 if (hiPerfProcessRequest.isHover) { 126 let offset = groupBy10MS ? 0 : 3; 127 for (let re of filter) { 128 if ( 129 re.frame && 130 hiPerfProcessRequest.hoverX >= re.frame.x - offset && 131 hiPerfProcessRequest.hoverX <= re.frame.x + re.frame.width + offset 132 ) { 133 HiPerfProcessStruct.hoverStruct = re; 134 break; 135 } 136 } 137 } else { 138 HiPerfProcessStruct.hoverStruct = hiPerfProcessRequest.params.hoverStruct; 139 } 140 HiPerfProcessStruct.selectStruct = hiPerfProcessRequest.params.selectStruct; 141 let path = new Path2D(); 142 for (let re of filter) { 143 HiPerfProcessStruct.draw(hiPerfProcessRequest.context, path, re, groupBy10MS); 144 } 145 groupBy10MS ? hiPerfProcessRequest.context.fill(path) : hiPerfProcessRequest.context.stroke(path); 146 hiPerfProcessRequest.context.closePath(); 147 drawSelection(hiPerfProcessRequest.context, hiPerfProcessRequest.params); 148 drawFlagLine( 149 hiPerfProcessRequest.context, 150 hiPerfProcessRequest.flagMoveInfo, 151 hiPerfProcessRequest.flagSelectedInfo, 152 hiPerfProcessRequest.startNS, 153 hiPerfProcessRequest.endNS, 154 hiPerfProcessRequest.totalNS, 155 hiPerfProcessRequest.frame, 156 hiPerfProcessRequest.slicesTime 157 ); 158 } 159 // @ts-ignore 160 self.postMessage({ 161 id: hiPerfProcessRequest.id, 162 type: hiPerfProcessRequest.type, 163 results: hiPerfProcessRequest.canvas ? undefined : filter, 164 hover: HiPerfProcessStruct.hoverStruct, 165 }); 166 } 167} 168 169export class HiPerfProcessStruct extends HiPerfStruct { 170 static hoverStruct: HiPerfProcessStruct | undefined; 171 static selectStruct: HiPerfProcessStruct | undefined; 172} 173