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