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, drawFlagLine, 18 drawLines, 19 drawLoading, 20 drawSelection, drawWakeUp, 21 ns2x, 22 Rect, Render, 23 RequestMessage 24} from "./ProcedureWorkerCommon.js"; 25import {ThreadStruct} from "./ProcedureWorkerThread.js"; 26export class NativeMemoryRender extends Render{ 27 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 28 if (req.canvas) { 29 req.context.clearRect(0, 0, req.frame.width, req.frame.height); 30 req.context.beginPath(); 31 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 32 drawSelection(req.context, req.params); 33 req.context.closePath(); 34 drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime); 35 } 36 // @ts-ignore 37 self.postMessage({ 38 id: req.id, 39 type: req.type, 40 results: req.canvas ? undefined : filter, 41 hover: ThreadStruct.hoverThreadStruct 42 }); 43 } 44} 45export class HeapRender { 46 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 47 if (req.lazyRefresh) { 48 heap(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh); 49 } else { 50 if (!req.useCache) { 51 heap(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, false); 52 } 53 } 54 if (req.canvas) { 55 req.context.clearRect(0, 0, req.canvas.width, req.canvas.height); 56 let arr = filter; 57 if (arr.length > 0 && !req.range.refresh && !req.useCache && req.lazyRefresh) { 58 drawLoading(req.context, req.startNS, req.endNS, req.totalNS, req.frame, arr[0].startTime, arr[arr.length - 1].startTime + arr[arr.length - 1].dur) 59 } 60 req.context.beginPath(); 61 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 62 HeapStruct.hoverHeapStruct = undefined; 63 if (req.isHover) { 64 for (let re of filter) { 65 if (re.frame && req.hoverX >= re.frame.x && req.hoverX <= re.frame.x + re.frame.width && req.hoverY >= re.frame.y && req.hoverY <= re.frame.y + re.frame.height) { 66 HeapStruct.hoverHeapStruct = re; 67 break; 68 } 69 } 70 } else { 71 HeapStruct.hoverHeapStruct = req.params.hoverHeapStruct; 72 } 73 for (let re of filter) { 74 HeapStruct.draw(req.context, re) 75 } 76 drawSelection(req.context, req.params); 77 drawWakeUp(req.context, req.wakeupBean, req.startNS, req.endNS, req.totalNS, req.frame); 78 req.context.closePath(); 79 drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime); 80 } 81 // @ts-ignore 82 self.postMessage({ 83 id: req.id, 84 type: req.type, 85 results: req.canvas ? undefined : filter, 86 hover: HeapStruct.hoverHeapStruct 87 }); 88 } 89} 90export function heap(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 91 if (use && res.length > 0) { 92 for (let i = 0; i < res.length; i++) { 93 let it = res[i]; 94 if ((it.startTime || 0) + (it.dur || 0) > (startNS || 0) && (it.startTime || 0) < (endNS || 0)) { 95 HeapStruct.setFrame(res[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 96 } else { 97 res[i].frame = null; 98 } 99 } 100 return; 101 } 102 res.length = 0; 103 if (list) { 104 for (let i = 0; i < list.length; i++) { 105 let it = list[i]; 106 if ((it.startTime || 0) + (it.dur || 0) > (startNS || 0) && (it.startTime || 0) < (endNS || 0)) { 107 HeapStruct.setFrame(list[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 108 if (i > 0 && ((list[i - 1].frame?.x || 0) == (list[i].frame?.x || 0) && (list[i - 1].frame?.width || 0) == (list[i].frame?.width || 0))) { 109 110 } else { 111 res.push(list[i]) 112 } 113 } 114 } 115 } 116} 117 118export class HeapStruct extends BaseStruct { 119 static hoverHeapStruct: HeapStruct | undefined; 120 startTime: number | undefined 121 endTime: number | undefined 122 dur: number | undefined 123 heapsize: number | undefined 124 maxHeapSize: number = 0 125 minHeapSize: number = 0 126 127 static setFrame(node: HeapStruct, padding: number, startNS: number, endNS: number, totalNS: number, frame: Rect) { 128 let x1: number, x2: number; 129 if ((node.startTime || 0) < startNS) { 130 x1 = 0; 131 } else { 132 x1 = ns2x((node.startTime || 0), startNS, endNS, totalNS, frame); 133 } 134 if ((node.startTime || 0) + (node.dur || 0) > endNS) { 135 x2 = frame.width; 136 } else { 137 // @ts-ignore 138 x2 = ns2x(node.startTime + node.dur, startNS, endNS, totalNS, frame); 139 } 140 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 141 let rectangle: Rect = new Rect(Math.floor(x1), Math.ceil(frame.y + padding), Math.ceil(getV), Math.floor(frame.height - padding * 2)); 142 node.frame = rectangle; 143 } 144 145 static draw(ctx: CanvasRenderingContext2D, data: HeapStruct) { 146 if (data.frame) { 147 let width = data.frame.width || 0; 148 ctx.fillStyle = "#2db3aa" 149 ctx.strokeStyle = "#2db3aa" 150 if (data.startTime === HeapStruct.hoverHeapStruct?.startTime) { 151 ctx.lineWidth = 1; 152 ctx.globalAlpha = 0.6; 153 let drawHeight: number = 0; 154 if (data.minHeapSize < 0) { 155 drawHeight = Math.ceil((((data.heapsize || 0) - data.minHeapSize) * (data.frame.height || 0)) / (data.maxHeapSize - data.minHeapSize)); 156 } else { 157 drawHeight = Math.ceil(((data.heapsize || 0) * (data.frame.height || 0)) / data.maxHeapSize); 158 } 159 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 160 ctx.beginPath() 161 ctx.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true) 162 ctx.fill() 163 ctx.globalAlpha = 1.0; 164 ctx.stroke(); 165 ctx.beginPath() 166 ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 167 ctx.lineWidth = 3; 168 ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight) 169 ctx.stroke(); 170 } else { 171 ctx.globalAlpha = 0.6; 172 ctx.lineWidth = 1; 173 let drawHeight: number = Math.ceil(((data.heapsize || 0) * (data.frame.height || 0)) / data.maxHeapSize); 174 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 175 } 176 } 177 ctx.globalAlpha = 1.0; 178 ctx.lineWidth = 1; 179 } 180}