1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { BaseStruct } from './BaseStruct';
17fb726d48Sopenharmony_ciimport { Utils } from '../component/trace/base/Utils';
18fb726d48Sopenharmony_ciimport { drawString } from '../database/ui-worker/ProcedureWorkerCommon';
19fb726d48Sopenharmony_ci
20fb726d48Sopenharmony_ciconst padding = 1;
21fb726d48Sopenharmony_ci
22fb726d48Sopenharmony_ciexport class ThreadStruct extends BaseStruct {
23fb726d48Sopenharmony_ci  static runningColor: string = '#467b3b';
24fb726d48Sopenharmony_ci  static rColor = '#a0b84d';
25fb726d48Sopenharmony_ci  static uninterruptibleSleepColor = '#f19d38';
26fb726d48Sopenharmony_ci  static sColor = '#FBFBFB';
27fb726d48Sopenharmony_ci  static hoverThreadStruct: ThreadStruct | undefined;
28fb726d48Sopenharmony_ci  static selectThreadStruct: ThreadStruct | undefined;
29fb726d48Sopenharmony_ci  static selectThreaStructList: Array<ThreadStruct> = [];
30fb726d48Sopenharmony_ci  hasSched: number | undefined;
31fb726d48Sopenharmony_ci  pid: number | undefined;
32fb726d48Sopenharmony_ci  processName: string | undefined;
33fb726d48Sopenharmony_ci  threadName: string | undefined;
34fb726d48Sopenharmony_ci  tid: number | undefined;
35fb726d48Sopenharmony_ci  upid: number | undefined;
36fb726d48Sopenharmony_ci  utid: number | undefined;
37fb726d48Sopenharmony_ci  switchCount: number | undefined;
38fb726d48Sopenharmony_ci  cpu: number | undefined;
39fb726d48Sopenharmony_ci  dur: number | undefined;
40fb726d48Sopenharmony_ci  end_ts: number | undefined;
41fb726d48Sopenharmony_ci  id: number | undefined;
42fb726d48Sopenharmony_ci  is_main_thread: number | undefined;
43fb726d48Sopenharmony_ci  name: string | undefined;
44fb726d48Sopenharmony_ci  startTime: number | undefined;
45fb726d48Sopenharmony_ci  start_ts: number | undefined;
46fb726d48Sopenharmony_ci  state: string | undefined;
47fb726d48Sopenharmony_ci  type: string | undefined;
48fb726d48Sopenharmony_ci  prio: number | undefined;
49fb726d48Sopenharmony_ci  curveFloatY: number | undefined;
50fb726d48Sopenharmony_ci
51fb726d48Sopenharmony_ci  static draw(threadBeanCanvasCtx: CanvasRenderingContext2D, threadBeanStructData: ThreadStruct): void {
52fb726d48Sopenharmony_ci    if (threadBeanStructData.frame) {
53fb726d48Sopenharmony_ci      threadBeanCanvasCtx.globalAlpha = 1;
54fb726d48Sopenharmony_ci      let stateText = threadBeanStructData.state || '';
55fb726d48Sopenharmony_ci      if ('S' === threadBeanStructData.state) {
56fb726d48Sopenharmony_ci        threadBeanCanvasCtx.fillStyle = ThreadStruct.sColor;
57fb726d48Sopenharmony_ci        threadBeanCanvasCtx.globalAlpha = 0.2; // transparency
58fb726d48Sopenharmony_ci        threadBeanCanvasCtx.fillRect(
59fb726d48Sopenharmony_ci          threadBeanStructData.frame.x,
60fb726d48Sopenharmony_ci          threadBeanStructData.frame.y + padding,
61fb726d48Sopenharmony_ci          threadBeanStructData.frame.width,
62fb726d48Sopenharmony_ci          threadBeanStructData.frame.height - padding * 2
63fb726d48Sopenharmony_ci        );
64fb726d48Sopenharmony_ci        threadBeanCanvasCtx.globalAlpha = 1; // transparency
65fb726d48Sopenharmony_ci      } else if ('R' === threadBeanStructData.state) {
66fb726d48Sopenharmony_ci        threadBeanCanvasCtx.fillStyle = ThreadStruct.rColor;
67fb726d48Sopenharmony_ci        this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData);
68fb726d48Sopenharmony_ci      } else if ('D' === threadBeanStructData.state) {
69fb726d48Sopenharmony_ci        threadBeanCanvasCtx.fillStyle = ThreadStruct.uninterruptibleSleepColor;
70fb726d48Sopenharmony_ci        this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData);
71fb726d48Sopenharmony_ci      } else if ('Running' === threadBeanStructData.state) {
72fb726d48Sopenharmony_ci        threadBeanCanvasCtx.fillStyle = ThreadStruct.runningColor;
73fb726d48Sopenharmony_ci        this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData);
74fb726d48Sopenharmony_ci      } else {
75fb726d48Sopenharmony_ci        threadBeanCanvasCtx.fillStyle = ThreadStruct.rColor;
76fb726d48Sopenharmony_ci        this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData);
77fb726d48Sopenharmony_ci      }
78fb726d48Sopenharmony_ci      if (
79fb726d48Sopenharmony_ci        ThreadStruct.selectThreadStruct &&
80fb726d48Sopenharmony_ci        ThreadStruct.equals(ThreadStruct.selectThreadStruct, threadBeanStructData) &&
81fb726d48Sopenharmony_ci        ThreadStruct.selectThreadStruct.state !== 'S'
82fb726d48Sopenharmony_ci      ) {
83fb726d48Sopenharmony_ci        threadBeanCanvasCtx.strokeStyle = '#232c5d';
84fb726d48Sopenharmony_ci        threadBeanCanvasCtx.lineWidth = 2;
85fb726d48Sopenharmony_ci        threadBeanCanvasCtx.strokeRect(
86fb726d48Sopenharmony_ci          threadBeanStructData.frame.x,
87fb726d48Sopenharmony_ci          threadBeanStructData.frame.y + padding,
88fb726d48Sopenharmony_ci          threadBeanStructData.frame.width - 2,
89fb726d48Sopenharmony_ci          threadBeanStructData.frame.height - padding * 2
90fb726d48Sopenharmony_ci        );
91fb726d48Sopenharmony_ci      }
92fb726d48Sopenharmony_ci    }
93fb726d48Sopenharmony_ci  }
94fb726d48Sopenharmony_ci
95fb726d48Sopenharmony_ci  private static drawRectAndString(
96fb726d48Sopenharmony_ci    threadBeanCanvasCtx: CanvasRenderingContext2D,
97fb726d48Sopenharmony_ci    threadBeanStructData: ThreadStruct
98fb726d48Sopenharmony_ci  ): void {
99fb726d48Sopenharmony_ci    // @ts-ignore
100fb726d48Sopenharmony_ci    threadBeanCanvasCtx.fillRect(
101fb726d48Sopenharmony_ci      threadBeanStructData.frame!.x,
102fb726d48Sopenharmony_ci      threadBeanStructData.frame!.y + padding,
103fb726d48Sopenharmony_ci      threadBeanStructData.frame!.width,
104fb726d48Sopenharmony_ci      threadBeanStructData.frame!.height - padding * 2
105fb726d48Sopenharmony_ci    );
106fb726d48Sopenharmony_ci    threadBeanCanvasCtx.fillStyle = '#fff';
107fb726d48Sopenharmony_ci    // @ts-ignore
108fb726d48Sopenharmony_ci    drawString(
109fb726d48Sopenharmony_ci      threadBeanCanvasCtx,
110fb726d48Sopenharmony_ci      ThreadStruct.getEndState(threadBeanStructData.state || ''),
111fb726d48Sopenharmony_ci      2,
112fb726d48Sopenharmony_ci      threadBeanStructData.frame!,
113fb726d48Sopenharmony_ci      threadBeanStructData
114fb726d48Sopenharmony_ci    );
115fb726d48Sopenharmony_ci  }
116fb726d48Sopenharmony_ci
117fb726d48Sopenharmony_ci  static getEndState(state: string): string {
118fb726d48Sopenharmony_ci    let statusMapElement = Utils.getEndState(state);
119fb726d48Sopenharmony_ci    if (statusMapElement) {
120fb726d48Sopenharmony_ci      return statusMapElement;
121fb726d48Sopenharmony_ci    } else {
122fb726d48Sopenharmony_ci      if ('' === statusMapElement || statusMapElement === null) {
123fb726d48Sopenharmony_ci        return '';
124fb726d48Sopenharmony_ci      }
125fb726d48Sopenharmony_ci      return 'Unknown State';
126fb726d48Sopenharmony_ci    }
127fb726d48Sopenharmony_ci  }
128fb726d48Sopenharmony_ci
129fb726d48Sopenharmony_ci  static equals(d1: ThreadStruct, d2: ThreadStruct): boolean {
130fb726d48Sopenharmony_ci    return (
131fb726d48Sopenharmony_ci      d1 &&
132fb726d48Sopenharmony_ci      d2 &&
133fb726d48Sopenharmony_ci      d1.cpu === d2.cpu &&
134fb726d48Sopenharmony_ci      d1.tid === d2.tid &&
135fb726d48Sopenharmony_ci      d1.state === d2.state &&
136fb726d48Sopenharmony_ci      d1.startTime === d2.startTime &&
137fb726d48Sopenharmony_ci      d1.dur === d2.dur
138fb726d48Sopenharmony_ci    );
139fb726d48Sopenharmony_ci  }
140fb726d48Sopenharmony_ci  static contrast(d1: ThreadStruct, d2: string | undefined | null, d3: string | undefined | null): boolean {
141fb726d48Sopenharmony_ci    return d1.pid === Number(d2) && d1.tid === Number(d3);
142fb726d48Sopenharmony_ci  }
143fb726d48Sopenharmony_ci}
144