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 } from './BaseStruct';
18
19export class CpuFreqStruct extends BaseStruct {
20  static maxFreq: number = 0;
21  static maxFreqName: string = '0 GHz';
22  static hoverCpuFreqStruct: CpuFreqStruct | undefined;
23  static selectCpuFreqStruct: CpuFreqStruct | undefined;
24  cpu: number | undefined;
25  value: number | undefined;
26  startNS: number | undefined;
27  dur: number | undefined; // Self-supplementing, the database is not returned
28
29  static draw(freqCtx: unknown, freqData: CpuFreqStruct): void {
30    if (freqData.frame) {
31      let freqWidth = freqData.frame.width || 0;
32      let freqIndex = freqData.cpu || 0;
33      freqIndex += 2;
34      // @ts-ignore
35      freqCtx.fillStyle = ColorUtils.colorForTid(freqIndex);
36      // @ts-ignore
37      freqCtx.strokeStyle = ColorUtils.colorForTid(freqIndex);
38      if (freqData.startNS === CpuFreqStruct.hoverCpuFreqStruct?.startNS) {
39        // @ts-ignore
40        freqCtx.lineWidth = 1;
41        // @ts-ignore
42        freqCtx.globalAlpha = 0.6;
43        let drawHeight: number = Math.floor(
44          ((freqData.value || 0) * (freqData.frame.height || 0) * 1.0) / CpuFreqStruct.maxFreq
45        );
46        // @ts-ignore
47        freqCtx.fillRect(
48          freqData.frame.x,
49          freqData.frame.y + freqData.frame.height - drawHeight,
50          freqWidth,
51          drawHeight
52        );
53        // @ts-ignore
54        freqCtx.beginPath();
55        // @ts-ignore
56        freqCtx.arc(freqData.frame.x, freqData.frame.y + freqData.frame.height - drawHeight, 3, 0, 2 * Math.PI, true);
57        // @ts-ignore
58        freqCtx.fill();
59        // @ts-ignore
60        freqCtx.globalAlpha = 1.0;
61        // @ts-ignore
62        freqCtx.stroke();
63        // @ts-ignore
64        freqCtx.beginPath();
65        // @ts-ignore
66        freqCtx.moveTo(freqData.frame.x + 3, freqData.frame.y + freqData.frame.height - drawHeight);
67        // @ts-ignore
68        freqCtx.lineWidth = 3;
69        // @ts-ignore
70        freqCtx.lineTo(freqData.frame.x + freqWidth, freqData.frame.y + freqData.frame.height - drawHeight);
71        // @ts-ignore
72        freqCtx.stroke();
73      } else {
74        // @ts-ignore
75        freqCtx.globalAlpha = 0.6;
76        // @ts-ignore
77        freqCtx.lineWidth = 1;
78        let drawHeight: number = Math.floor(
79          ((freqData.value || 0) * (freqData.frame.height || 0)) / CpuFreqStruct.maxFreq
80        );
81        // @ts-ignore
82        freqCtx.fillRect(
83          freqData.frame.x,
84          freqData.frame.y + freqData.frame.height - drawHeight,
85          freqWidth,
86          drawHeight
87        );
88      }
89    }
90    // @ts-ignore
91    freqCtx.globalAlpha = 1.0;
92    // @ts-ignore
93    freqCtx.lineWidth = 1;
94  }
95}
96