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 } from './BaseStruct';
17import { Rect } from '../component/trace/timer-shaft/Rect';
18import { ColorUtils } from '../component/trace/base/ColorUtils';
19import { drawString } from '../database/ui-worker/ProcedureWorkerCommon';
20
21export class FuncStruct extends BaseStruct {
22  static hoverFuncStruct: FuncStruct | undefined;
23  static selectFuncStruct: FuncStruct | undefined;
24  argsetid: number | undefined;
25  depth: number | undefined;
26  dur: number | undefined;
27  funName: string | undefined;
28  id: number | undefined;
29  callid: number | undefined;
30  is_main_thread: number | undefined;
31  parent_id: number | undefined;
32  startTs: number | undefined;
33  threadName: string | undefined;
34  tid: number | undefined;
35  itid: number | undefined;
36  ipid: number | undefined;
37  identify: number | undefined;
38  track_id: number | undefined;
39  nofinish: boolean = false;
40  // distributed relation chain
41  ts: number | undefined;
42  pid: number | undefined;
43  traceId: string | undefined;
44  chainId: string | undefined;
45  chainName: string | undefined;
46  spanId: string | undefined;
47  parentSpanId: string | undefined;
48  chainFlag: string | undefined;
49
50  static draw(funcBeanStructCanvasCtx: CanvasRenderingContext2D, funcBeanStruct: FuncStruct): void {
51    if (funcBeanStruct.frame) {
52      if (
53        funcBeanStruct.dur === undefined ||
54        funcBeanStruct.dur === null ||
55        funcBeanStruct.dur === 0 ||
56        FuncStruct.isBinder(funcBeanStruct)
57      ) {
58      } else {
59        funcBeanStructCanvasCtx.fillStyle =
60          ColorUtils.FUNC_COLOR[funcBeanStruct.depth || 0 % ColorUtils.FUNC_COLOR.length];
61        let miniHeight = 20;
62        funcBeanStructCanvasCtx.fillRect(
63          funcBeanStruct.frame.x,
64          funcBeanStruct.frame.y,
65          funcBeanStruct.frame.width,
66          miniHeight - padding * 2
67        );
68        funcBeanStructCanvasCtx.fillStyle = '#fff';
69        drawString(funcBeanStructCanvasCtx, funcBeanStruct.funName || '', 5, funcBeanStruct.frame, funcBeanStruct);
70        if (FuncStruct.isSelected(funcBeanStruct)) {
71          funcBeanStructCanvasCtx.strokeStyle = '#000';
72          funcBeanStructCanvasCtx.lineWidth = 1;
73          funcBeanStructCanvasCtx.strokeRect(
74            funcBeanStruct.frame.x,
75            funcBeanStruct.frame.y,
76            funcBeanStruct.frame.width,
77            miniHeight - padding * 2
78          );
79        }
80      }
81    }
82  }
83
84  static isSelected(data: FuncStruct): boolean {
85    return (
86      FuncStruct.selectFuncStruct !== undefined &&
87      FuncStruct.selectFuncStruct.startTs === data.startTs &&
88      FuncStruct.selectFuncStruct.dur === data.dur &&
89      FuncStruct.selectFuncStruct.funName === data.funName
90    );
91  }
92
93  static isBinder(data: FuncStruct): boolean {
94    if (
95      data.funName &&
96      (data.funName.toLowerCase().startsWith('binder transaction') ||
97        data.funName.toLowerCase().startsWith('binder async') ||
98        data.funName.toLowerCase().startsWith('binder reply'))
99    ) {
100      return true;
101    } else {
102      return false;
103    }
104  }
105
106  static isBinderAsync(data: FuncStruct): boolean {
107    if (data.funName && data.funName.toLowerCase().includes('async')) {
108      return true;
109    } else {
110      return false;
111    }
112  }
113}
114
115const padding = 1;
116