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, dataFilterHandler, drawLoadingFrame, isFrameContainPoint, Render } from './ProcedureWorkerCommon';
17import { TraceRow } from '../../component/trace/base/TraceRow';
18import { ColorUtils } from '../../component/trace/base/ColorUtils';
19import { SpSystemTrace } from '../../component/SpSystemTrace';
20
21export class ClockRender extends Render {
22  renderMainThread(
23    clockReq: {
24      context: CanvasRenderingContext2D;
25      useCache: boolean;
26      type: string;
27      maxValue: number;
28      index: number;
29      maxName: string;
30    },
31    row: TraceRow<ClockStruct>
32  ): void {
33    ClockStruct.index = clockReq.index;
34    let clockList = row.dataList;
35    let clockFilter = row.dataListCache;
36    dataFilterHandler(clockList, clockFilter, {
37      startKey: 'startNS',
38      durKey: 'dur',
39      startNS: TraceRow.range?.startNS ?? 0,
40      endNS: TraceRow.range?.endNS ?? 0,
41      totalNS: TraceRow.range?.totalNS ?? 0,
42      frame: row.frame,
43      paddingTop: 5,
44      useCache: clockReq.useCache || !(TraceRow.range?.refresh ?? false),
45    });
46    drawLoadingFrame(clockReq.context, clockFilter, row);
47    clockReq.context.beginPath();
48    let find = false;
49    for (let re of clockFilter) {
50      ClockStruct.draw(clockReq.context, re, clockReq.maxValue);
51      if (row.isHover && re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) {
52        ClockStruct.hoverClockStruct = re;
53        find = true;
54      }
55    }
56    if (!find && row.isHover) {
57      ClockStruct.hoverClockStruct = undefined;
58    }
59    clockReq.context.closePath();
60    let s = clockReq.maxName;
61    let textMetrics = clockReq.context.measureText(s);
62    clockReq.context.globalAlpha = 0.8;
63    clockReq.context.fillStyle = '#f0f0f0';
64    clockReq.context.fillRect(0, 5, textMetrics.width + 8, 18);
65    clockReq.context.globalAlpha = 1;
66    clockReq.context.fillStyle = '#333';
67    clockReq.context.textBaseline = 'middle';
68    clockReq.context.fillText(s, 4, 5 + 9);
69  }
70}
71export function ClockStructOnClick(
72  clickRowType: string,
73  sp: SpSystemTrace,
74  entry?: ClockStruct,
75): Promise<unknown> {
76  return new Promise((resolve, reject) => {
77    if (clickRowType === TraceRow.ROW_TYPE_CLOCK && (ClockStruct.hoverClockStruct || entry)) {
78      ClockStruct.selectClockStruct = entry || ClockStruct.hoverClockStruct;
79      sp.traceSheetEL?.displayClockData(ClockStruct.selectClockStruct!);
80      sp.timerShaftEL?.modifyFlagList(undefined);
81      reject(new Error());
82    } else {
83      resolve(null);
84    }
85  });
86}
87export class ClockStruct extends BaseStruct {
88  static maxValue: number = 0;
89  static maxName: string = '';
90  static hoverClockStruct: ClockStruct | undefined;
91  static selectClockStruct: ClockStruct | undefined;
92  static index = 0;
93  filterId: number | undefined;
94  value: number | undefined;
95  startNS: number | undefined;
96  dur: number | undefined; //自补充,数据库没有返回
97  delta: number | undefined; //自补充,数据库没有返回
98
99  static draw(clockContext: CanvasRenderingContext2D, data: ClockStruct, maxValue: number): void {
100    if (data.frame) {
101      let width = data.frame.width || 0;
102      clockContext.fillStyle = ColorUtils.colorForTid(ClockStruct.index);
103      clockContext.strokeStyle = ColorUtils.colorForTid(ClockStruct.index);
104      let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / maxValue);
105      if (drawHeight === 0) {
106        drawHeight = 1;
107      }
108      if (ClockStruct.isHover(data)) {
109        clockContext.lineWidth = 1;
110        clockContext.globalAlpha = 0.6;
111        clockContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight);
112        clockContext.beginPath();
113        clockContext.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true);
114        clockContext.fill();
115        clockContext.globalAlpha = 1.0;
116        clockContext.stroke();
117        clockContext.beginPath();
118        clockContext.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight);
119        clockContext.lineWidth = 3;
120        clockContext.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight);
121        clockContext.stroke();
122      } else {
123        clockContext.lineWidth = 1;
124        clockContext.globalAlpha = 1.0;
125        clockContext.strokeRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight);
126        clockContext.globalAlpha = 0.6;
127        clockContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight);
128      }
129    }
130    clockContext.globalAlpha = 1.0;
131    clockContext.lineWidth = 1;
132  }
133
134  static isHover(clock: ClockStruct): boolean {
135    return clock === ClockStruct.hoverClockStruct || clock === ClockStruct.selectClockStruct;
136  }
137}
138