• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, isFrameContainPoint, Render, RequestMessage } from './ProcedureWorkerCommon';
18import { TraceRow } from '../../component/trace/base/TraceRow';
19
20export class FreqExtendRender extends Render {
21  renderMainThread(
22    freqReq: {
23      context: CanvasRenderingContext2D;
24      useCache: boolean;
25      type: string;
26    },
27    row: TraceRow<CpuFreqExtendStruct>
28  ) {
29    let freqExtendList = row.dataList;
30    let freqExtendFilter = row.dataListCache;
31    dataFilterHandler(freqExtendList, freqExtendFilter, {
32      startKey: 'startNS',
33      durKey: 'dur',
34      startNS: TraceRow.range?.startNS ?? 0,
35      endNS: TraceRow.range?.endNS ?? 0,
36      totalNS: TraceRow.range?.totalNS ?? 0,
37      frame: row.frame,
38      paddingTop: 5,
39      useCache: freqReq.useCache || !(TraceRow.range?.refresh ?? false),
40    });
41
42    if (row.isHover) {
43      CpuFreqExtendStruct.cycle = -1;
44      CpuFreqExtendStruct.isTabHover = false;
45    }
46    freqReq.context.beginPath();
47    for (let re of freqExtendFilter) {
48      if (row.isHover && re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) {
49        CpuFreqExtendStruct.hoverCpuFreqStruct = re;
50      }
51      if (!row.isHover && !CpuFreqExtendStruct.isTabHover) CpuFreqExtendStruct.hoverCpuFreqStruct = undefined;
52      CpuFreqExtendStruct.draw(freqReq.context, re);
53    }
54    freqReq.context.closePath();
55  }
56}
57
58export class CpuFreqExtendStruct extends BaseStruct {
59  static maxValue: number = 0;
60  static cycle: number = -1;
61  static isTabHover: boolean = false;
62  static hoverCpuFreqStruct: CpuFreqExtendStruct | undefined;
63  freq: number = 0;
64  static selectCpuFreqStruct: CpuFreqExtendStruct | undefined;
65  cpu: number | undefined;
66  value: number = 0;
67  startNS: number | undefined;
68  dur: number | undefined; //自补充,数据库没有返回
69  cycle: number | undefined;
70  type: string | undefined;
71  count: number = 0;
72
73  static draw(freqContext: CanvasRenderingContext2D, data: CpuFreqExtendStruct) {
74    if (data.frame) {
75      let width = data.frame.width || 0;
76      let index = data.cpu || 0;
77      index += 2;
78      let color = ColorUtils.colorForTid(index);
79      freqContext.fillStyle = color;
80      freqContext.strokeStyle = color;
81      if (
82        data === CpuFreqExtendStruct.hoverCpuFreqStruct ||
83        data === CpuFreqExtendStruct.selectCpuFreqStruct ||
84        data === CpuFreqExtendStruct.selectCpuFreqStruct ||
85        (data.cycle === CpuFreqExtendStruct.cycle && CpuFreqExtendStruct.cycle !== -1)
86      ) {
87        freqContext.fillStyle = '#ff0000';
88        freqContext.strokeStyle = '#ff0000';
89        freqContext.lineWidth = 3;
90        freqContext.globalAlpha = 0.6;
91        if (data.type === 'SCHED-SWITCH' || data.type === 'GPU-FREQ') {
92          freqContext.globalAlpha = 1;
93          freqContext.fillStyle = color;
94          freqContext.strokeStyle = color;
95        }
96        let drawHeight: number = Math.floor(
97          ((data.value || 0) * (data.frame.height || 0) * 1.0) / CpuFreqExtendStruct.maxValue
98        );
99        if (drawHeight < 1) {
100          drawHeight = 1;
101        }
102        freqContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight);
103        freqContext.globalAlpha = 0.8;
104        freqContext.strokeRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight);
105      } else {
106        freqContext.globalAlpha = 0.6;
107        freqContext.lineWidth = 1;
108        let drawHeight: number = Math.floor(
109          ((data.value || 0) * (data.frame.height || 0)) / CpuFreqExtendStruct.maxValue
110        );
111        if (drawHeight < 1) {
112          drawHeight = 1;
113        }
114        freqContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight);
115      }
116    }
117    freqContext.globalAlpha = 1.0;
118    freqContext.lineWidth = 1;
119  }
120}
121