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