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