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'; 17import { TraceRow } from '../../component/trace/base/TraceRow'; 18import { 19 BaseStruct, 20 drawFlagLine, 21 drawLines, 22 drawLoading, 23 drawSelection, 24 drawWakeUp, 25 isFrameContainPoint, 26 ns2x, 27 PerfRender, 28 Render, 29 RequestMessage, 30 mem, 31 drawLoadingFrame, 32} from './ProcedureWorkerCommon'; 33import { CpuStruct } from './cpu/ProcedureWorkerCPU'; 34import { ProcessMemStruct as BaseProcessMemStruct } from '../../bean/ProcessMemStruct'; 35export class MemRender extends Render { 36 renderMainThread( 37 req: { 38 useCache: boolean; 39 context: CanvasRenderingContext2D; 40 type: string; 41 }, 42 row: TraceRow<ProcessMemStruct> 43 ): void { 44 let memList = row.dataList; 45 let memFilter = row.dataListCache; 46 mem( 47 memList, 48 memFilter, 49 TraceRow.range!.startNS, 50 TraceRow.range!.endNS, 51 TraceRow.range!.totalNS, 52 row.frame, 53 req.useCache || !TraceRow.range!.refresh 54 ); 55 drawLoadingFrame(req.context, memFilter, row); 56 req.context.beginPath(); 57 let memFind = false; 58 for (let re of memFilter) { 59 ProcessMemStruct.draw(req.context, re); 60 if (row.isHover) { 61 if (re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) { 62 ProcessMemStruct.hoverProcessMemStruct = re; 63 memFind = true; 64 } 65 } 66 } 67 if (!memFind && row.isHover) { 68 ProcessMemStruct.hoverProcessMemStruct = undefined; 69 } 70 req.context.closePath(); 71 } 72} 73 74export class ProcessMemStruct extends BaseProcessMemStruct { 75 static draw(memContext: CanvasRenderingContext2D, data: ProcessMemStruct) { 76 if (data.frame) { 77 let width = data.frame.width || 0; 78 memContext.fillStyle = ColorUtils.colorForTid(data.maxValue || 0); 79 memContext.strokeStyle = ColorUtils.colorForTid(data.maxValue || 0); 80 if (data === ProcessMemStruct.hoverProcessMemStruct) { 81 memContext.lineWidth = 1; 82 memContext.globalAlpha = 0.6; 83 let memDrawHeight: number = Math.floor( 84 ((data.value || 0) * (data.frame.height || 0) * 1.0) / (data.maxValue || 1) 85 ); 86 memDrawHeight = memDrawHeight > 0 ? memDrawHeight : 1; 87 memContext.fillRect(data.frame.x, data.frame.y + data.frame.height - memDrawHeight, width, memDrawHeight); 88 memContext.beginPath(); 89 memContext.arc(data.frame.x, data.frame.y + data.frame.height - memDrawHeight, 3, 0, 2 * Math.PI, true); 90 memContext.fill(); 91 memContext.globalAlpha = 1.0; 92 memContext.stroke(); 93 memContext.closePath(); 94 memContext.beginPath(); 95 memContext.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - memDrawHeight); 96 memContext.lineWidth = 3; 97 memContext.lineTo(data.frame.x + width, data.frame.y + data.frame.height - memDrawHeight); 98 memContext.stroke(); 99 memContext.closePath(); 100 } else { 101 memContext.globalAlpha = 0.6; 102 memContext.lineWidth = 1; 103 let drawHeight: number = ((data.value || 0) * (data.frame.height || 0) * 1.0) / (data.maxValue || 1); 104 drawHeight = drawHeight > 0 ? drawHeight : 1; 105 memContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight); 106 if (width > 2) { 107 memContext.lineWidth = 1; 108 memContext.globalAlpha = 1.0; 109 memContext.strokeRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight); 110 } 111 } 112 } 113 memContext.globalAlpha = 1.0; 114 memContext.lineWidth = 1; 115 } 116} 117