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 {BaseStruct, ns2x, Rect} from "./ProcedureWorkerCommon.js"; 17 18export function heap(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 19 if (use && res.length > 0) { 20 for (let i = 0; i < res.length; i++) { 21 let it = res[i]; 22 if ((it.startTime || 0) + (it.dur || 0) > (startNS || 0) && (it.startTime || 0) < (endNS || 0)) { 23 HeapStruct.setFrame(res[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 24 } else { 25 res[i].frame = null; 26 } 27 } 28 return; 29 } 30 res.length = 0; 31 if (list) { 32 for (let i = 0; i < list.length; i++) { 33 let it = list[i]; 34 if ((it.startTime || 0) + (it.dur || 0) > (startNS || 0) && (it.startTime || 0) < (endNS || 0)) { 35 HeapStruct.setFrame(list[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 36 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))) { 37 38 } else { 39 res.push(list[i]) 40 } 41 } 42 } 43 } 44} 45 46export class HeapStruct extends BaseStruct { 47 static hoverHeapStruct: HeapStruct | undefined; 48 startTime: number | undefined 49 endTime: number | undefined 50 dur: number | undefined 51 heapsize: number | undefined 52 maxHeapSize: number = 0 53 minHeapSize: number = 0 54 55 static setFrame(node: HeapStruct, padding: number, startNS: number, endNS: number, totalNS: number, frame: Rect) { 56 let x1: number, x2: number; 57 if ((node.startTime || 0) < startNS) { 58 x1 = 0; 59 } else { 60 x1 = ns2x((node.startTime || 0), startNS, endNS, totalNS, frame); 61 } 62 if ((node.startTime || 0) + (node.dur || 0) > endNS) { 63 x2 = frame.width; 64 } else { 65 // @ts-ignore 66 x2 = ns2x(node.startTime + node.dur, startNS, endNS, totalNS, frame); 67 } 68 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 69 let rectangle: Rect = new Rect(Math.floor(x1), Math.ceil(frame.y + padding), Math.ceil(getV), Math.floor(frame.height - padding * 2)); 70 node.frame = rectangle; 71 } 72 73 static draw(ctx: CanvasRenderingContext2D, data: HeapStruct) { 74 if (data.frame) { 75 let width = data.frame.width || 0; 76 ctx.fillStyle = "#2db3aa" 77 ctx.strokeStyle = "#2db3aa" 78 if (data.startTime === HeapStruct.hoverHeapStruct?.startTime) { 79 ctx.lineWidth = 1; 80 ctx.globalAlpha = 0.6; 81 let drawHeight: number = 0; 82 if (data.minHeapSize < 0) { 83 drawHeight = Math.ceil((((data.heapsize || 0) - data.minHeapSize) * (data.frame.height || 0)) / (data.maxHeapSize - data.minHeapSize)); 84 } else { 85 drawHeight = Math.ceil(((data.heapsize || 0) * (data.frame.height || 0)) / data.maxHeapSize); 86 } 87 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 88 ctx.beginPath() 89 ctx.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true) 90 ctx.fill() 91 ctx.globalAlpha = 1.0; 92 ctx.stroke(); 93 ctx.beginPath() 94 ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 95 ctx.lineWidth = 3; 96 ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight) 97 ctx.stroke(); 98 } else { 99 ctx.globalAlpha = 0.6; 100 ctx.lineWidth = 1; 101 let drawHeight: number = Math.ceil(((data.heapsize || 0) * (data.frame.height || 0)) / data.maxHeapSize); 102 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 103 } 104 } 105 ctx.globalAlpha = 1.0; 106 ctx.lineWidth = 1; 107 } 108}