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