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 {
17  BaseStruct,
18  dataFilterHandler,
19  isFrameContainPoint,
20  Render,
21  drawString,
22  drawLoadingFrame,
23} from './ProcedureWorkerCommon';
24import { TraceRow } from '../../component/trace/base/TraceRow';
25import { ColorUtils } from '../../component/trace/base/ColorUtils';
26import { SpSystemTrace } from '../../component/SpSystemTrace';
27
28export class IrqRender extends Render {
29  renderMainThread(
30    irqReq: {
31      context: CanvasRenderingContext2D;
32      useCache: boolean;
33      type: string;
34      index: number;
35    },
36    row: TraceRow<IrqStruct>
37  ): void {
38    IrqStruct.index = irqReq.index;
39    let irqList = row.dataList;
40    let irqFilter = row.dataListCache;
41    dataFilterHandler(irqList, irqFilter, {
42      startKey: 'startNS',
43      durKey: 'dur',
44      startNS: TraceRow.range?.startNS ?? 0,
45      endNS: TraceRow.range?.endNS ?? 0,
46      totalNS: TraceRow.range?.totalNS ?? 0,
47      frame: row.frame,
48      paddingTop: 5,
49      useCache: irqReq.useCache || !(TraceRow.range?.refresh ?? false),
50    });
51    drawLoadingFrame(irqReq.context, irqFilter, row);
52    irqReq.context.beginPath();
53    let find = false;
54    for (let re of irqFilter) {
55      IrqStruct.draw(irqReq.context, re, row.isHover);
56      if (row.isHover && re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) {
57        IrqStruct.hoverIrqStruct = re;
58        find = true;
59      }
60    }
61    if (!find && row.isHover) {
62      IrqStruct.hoverIrqStruct = undefined;
63    }
64    irqReq.context.closePath();
65    irqReq.context.globalAlpha = 0.8;
66    irqReq.context.fillStyle = '#f0f0f0';
67    irqReq.context.globalAlpha = 1;
68    irqReq.context.fillStyle = '#333';
69    irqReq.context.textBaseline = 'middle';
70  }
71}
72
73const padding = 3;
74export function IrqStructOnClick(
75  clickRowType: string,
76  sp: SpSystemTrace,
77  entry?: IrqStruct,
78): Promise<unknown> {
79  return new Promise((resolve, reject) => {
80    if (clickRowType === TraceRow.ROW_TYPE_IRQ && (IrqStruct.hoverIrqStruct || entry)) {
81      IrqStruct.selectIrqStruct = entry || IrqStruct.hoverIrqStruct;
82      sp.traceSheetEL?.displayIrqData(IrqStruct.selectIrqStruct!);
83      sp.timerShaftEL?.modifyFlagList(undefined);
84      reject(new Error());
85    } else {
86      resolve(null);
87    }
88  });
89}
90export class IrqStruct extends BaseStruct {
91  static maxValue: number = 0;
92  static maxName: string = '';
93  static hoverIrqStruct: IrqStruct | undefined;
94  static selectIrqStruct: IrqStruct | undefined;
95  static index = 0;
96  id: number | undefined;
97  startNS: number | undefined;
98  name: string | undefined;
99  dur: number | undefined; //自补充,数据库没有返回
100  textMetricsWidth: number | undefined; //自补充
101  argSetId: number | undefined;
102
103  static draw(ctx: CanvasRenderingContext2D, data: IrqStruct, isHover: boolean): void {
104    if (data.frame) {
105      ctx.fillStyle = ColorUtils.colorForName(data.name || '');
106      ctx.strokeStyle = '#232c5d';
107      if ((data === IrqStruct.hoverIrqStruct && isHover) || data === IrqStruct.selectIrqStruct) {
108        ctx.lineWidth = 1;
109        ctx.globalAlpha = 0.6;
110        ctx.fillRect(data.frame.x, data.frame.y + padding, data.frame.width, data.frame.height - padding * 2);
111        ctx.lineWidth = 2;
112        ctx.strokeRect(data.frame.x, data.frame.y + padding, data.frame.width - 2, data.frame.height - padding * 2);
113      } else {
114        ctx.globalAlpha = 0.6;
115        ctx.lineWidth = 1;
116        ctx.fillRect(data.frame.x, data.frame.y + padding, data.frame.width, data.frame.height - padding * 2);
117      }
118      ctx.globalAlpha = 1.0;
119      ctx.lineWidth = 1;
120      ctx.fillStyle = '#fff';
121      ctx.textBaseline = 'middle';
122      ctx.font = '8px sans-serif';
123      data.frame.width > 7 && drawString(ctx, data.name || '', 2, data.frame, data);
124    }
125  }
126}
127