• 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 { 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 XpowerThreadCountRender extends Render {
22  renderMainThread(
23    xpowerReq: {
24      context: CanvasRenderingContext2D;
25      useCache: boolean;
26    },
27    row: TraceRow<XpowerThreadCountStruct>
28  ): void {
29    let xpowerThreadCountList = row.dataList;
30    let xpowerThreadCountFilter = row.dataListCache;
31    let maxValue = 0;
32    if (xpowerThreadCountFilter.length > 0) {// @ts-ignore
33      maxValue = xpowerThreadCountFilter.map((item) => item.value).reduce((a: unknown, b: unknown) => Math.max(a, b));
34    }
35    dataFilterHandler(xpowerThreadCountList, xpowerThreadCountFilter, {
36      startKey: 'startNS',
37      durKey: 'dur',
38      startNS: TraceRow.range?.startNS ?? 0,
39      endNS: TraceRow.range?.endNS ?? 0,
40      totalNS: TraceRow.range?.totalNS ?? 0,
41      frame: row.frame,
42      paddingTop: 5,
43      useCache: xpowerReq.useCache || !(TraceRow.range?.refresh ?? false),
44    });
45    drawLoadingFrame(xpowerReq.context, xpowerThreadCountFilter, row);
46    xpowerReq.context.beginPath();
47    let find = false;
48    for (let re of xpowerThreadCountFilter) {
49      XpowerThreadCountStruct.draw(xpowerReq.context, re, maxValue);
50      if (row.isHover && re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) {
51        XpowerThreadCountStruct.hoverXpowerStruct = re;
52        find = true;
53      }
54    }
55    if (!find) {
56      XpowerThreadCountStruct.hoverXpowerStruct = undefined;
57    }
58    xpowerReq.context.closePath();
59    let maxValueStr = String(maxValue);
60    let textMetrics = xpowerReq.context.measureText(maxValueStr);
61    xpowerReq.context.globalAlpha = 0.8;
62    xpowerReq.context.fillStyle = '#f0f0f0';
63    xpowerReq.context.fillRect(0, 5, textMetrics.width + 8, 18);
64    xpowerReq.context.globalAlpha = 1;
65    xpowerReq.context.fillStyle = '#333';
66    xpowerReq.context.textBaseline = 'middle';
67    xpowerReq.context.fillText(maxValueStr, 4, 5 + 9);
68  }
69}
70
71export function XpowerThreadCountStructOnClick(clickRowType: string, sp: SpSystemTrace, entry?: XpowerThreadCountStruct): Promise<unknown> {
72  return new Promise((resolve, reject) => {
73    if (clickRowType === TraceRow.ROW_TYPE_XPOWER_THREAD_COUNT && (XpowerThreadCountStruct.hoverXpowerStruct || entry)) {
74      XpowerThreadCountStruct.selectXpowerStruct = entry || XpowerThreadCountStruct.hoverXpowerStruct;
75      sp.traceSheetEL?.displayXpowerTreadCountData(XpowerThreadCountStruct.selectXpowerStruct!);
76      sp.timerShaftEL?.modifyFlagList(undefined);
77      reject(new Error());
78    } else {
79      resolve(null);
80    }
81  });
82}
83
84export class XpowerThreadCountStruct extends BaseStruct {
85  static maxValue: number = 0;
86  static hoverXpowerStruct: XpowerThreadCountStruct | undefined;
87  static selectXpowerStruct: XpowerThreadCountStruct | undefined;
88  static index = 0;
89  value: number = 0;
90  startNS: number = 0;
91  dur: number = 0; //自补充,数据库没有返回
92
93  static draw(xpowerContext: CanvasRenderingContext2D, data: XpowerThreadCountStruct, maxValue: number): void {
94    if (data.frame) {
95      const width = data.frame.width || 0;
96      const drawHeight = this.calculateDrawHeight(data, maxValue);
97      const cutHeight = 0;
98
99      if (XpowerThreadCountStruct.isHover(data)) {
100        this.drawHoverState(xpowerContext, data, width, drawHeight, cutHeight);
101      } else {
102        this.drawNormalState(xpowerContext, data, width, drawHeight, cutHeight);
103      }
104    }
105    xpowerContext.globalAlpha = 1.0;
106    xpowerContext.lineWidth = 1;
107  }
108
109  private static calculateDrawHeight(data: XpowerThreadCountStruct, maxValue: number): number {
110    let drawHeight = Math.floor(((data.value || 0) * (data.frame!.height || 0) * 1.0) / maxValue);
111    return drawHeight === 0 ? 1 : drawHeight;
112  }
113
114  private static drawHoverState(
115    xpowerContext: CanvasRenderingContext2D,
116    data: XpowerThreadCountStruct,
117    width: number,
118    drawHeight: number,
119    cutHeight: number
120  ): void {
121    xpowerContext.fillStyle = ColorUtils.colorForTid(XpowerThreadCountStruct.index);
122    xpowerContext.strokeStyle = ColorUtils.colorForTid(XpowerThreadCountStruct.index);
123    xpowerContext.lineWidth = 1;
124    xpowerContext.globalAlpha = 0.6;
125    xpowerContext.fillRect(
126      data.frame!.x,
127      data.frame!.y + data.frame!.height - drawHeight - cutHeight,
128      width,
129      drawHeight
130    );
131    xpowerContext.beginPath();
132    xpowerContext.arc(
133      data.frame!.x,
134      data.frame!.y + data.frame!.height - drawHeight - cutHeight,
135      3,
136      0,
137      2 * Math.PI,
138      true
139    );
140    xpowerContext.fill();
141    xpowerContext.globalAlpha = 1.0;
142    xpowerContext.stroke();
143    xpowerContext.beginPath();
144    xpowerContext.moveTo(data.frame!.x + 3, data.frame!.y + data.frame!.height - drawHeight - cutHeight);
145    xpowerContext.lineWidth = 3;
146    xpowerContext.lineTo(data.frame!.x + width, data.frame!.y + data.frame!.height - drawHeight - cutHeight);
147    xpowerContext.stroke();
148  }
149
150  private static drawNormalState(
151    xpowerContext: CanvasRenderingContext2D,
152    data: XpowerThreadCountStruct,
153    width: number,
154    drawHeight: number,
155    cutHeight: number
156  ): void {
157    xpowerContext.fillStyle = ColorUtils.colorForTid(XpowerThreadCountStruct.index);
158    xpowerContext.strokeStyle = ColorUtils.colorForTid(XpowerThreadCountStruct.index);
159    xpowerContext.lineWidth = 1;
160    xpowerContext.globalAlpha = 1.0;
161    xpowerContext.strokeRect(
162      data.frame!.x,
163      data.frame!.y + data.frame!.height - drawHeight - cutHeight,
164      width,
165      drawHeight
166    );
167    xpowerContext.globalAlpha = 0.6;
168    xpowerContext.fillRect(
169      data.frame!.x,
170      data.frame!.y + data.frame!.height - drawHeight - cutHeight,
171      width,
172      drawHeight
173    );
174  }
175
176  static isHover(xpower: XpowerThreadCountStruct): boolean {
177    return (
178      xpower === XpowerThreadCountStruct.hoverXpowerStruct || xpower === XpowerThreadCountStruct.selectXpowerStruct
179    );
180  }
181}
182