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