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    dataFilterHandler,
18    isFrameContainPoint,
19    Rect,
20    Render,
21    RequestMessage,
22    drawString,
23    drawLoadingFrame,
24} from './ProcedureWorkerCommon';
25import { TraceRow } from '../../component/trace/base/TraceRow';
26import { Utils } from '../../component/trace/base/Utils';
27import { ThreadStruct as BaseThreadStruct } from '../../bean/ThreadStruct';
28import { SpSystemTrace } from '../../component/SpSystemTrace';
29import { SpSegmentationChart } from '../../component/chart/SpSegmentationChart';
30import { ns2x } from './ProcedureWorkerCommon';
31import { Flag } from '../../component/trace/timer-shaft/Flag';
32import { CpuFreqExtendStruct } from './ProcedureWorkerFreqExtend';
33import { BinderStruct } from './procedureWorkerBinder';
34import { TabPaneFreqStatesDataCut } from '../../component/trace/sheet/states/TabPaneFreqStatesDataCut';
35export class AllStatesRender extends Render {
36    renderMainThread(
37        threadReq: {
38            context: CanvasRenderingContext2D;
39            useCache: boolean;
40            type: string;
41            translateY: number;
42        },
43        row: TraceRow<AllstatesStruct>
44    ): void {
45        let threadList = row.dataList;
46        let threadFilter = row.dataListCache;
47        dataFilterHandler(threadList, threadFilter, {
48            startKey: 'startTime',
49            durKey: 'chartDur',
50            startNS: TraceRow.range?.startNS ?? 0,
51            endNS: TraceRow.range?.endNS ?? 0,
52            totalNS: TraceRow.range?.totalNS ?? 0,
53            frame: row.frame,
54            paddingTop: 3,
55            useCache: threadReq.useCache || !(TraceRow.range?.refresh ?? false),
56        });
57        drawLoadingFrame(threadReq.context, threadFilter, row);
58        threadReq.context.beginPath();
59        let find: boolean = false;
60        for (let re of threadFilter) {
61            re.translateY = threadReq.translateY;
62            AllstatesStruct.drawThread(threadReq.context, re);
63            if (row.isHover && re.frame && isFrameContainPoint(re.frame!, row.hoverX, row.hoverY)) {
64                SpSegmentationChart.tabHoverObj = { key: '', cycle: -1 };
65                AllstatesStruct.hoverThreadStruct = re;
66                find = true;
67            }
68        }
69        if (row.rowId === 'statesrow' &&
70            (!row.isHover || !find) &&
71            CpuFreqExtendStruct.hoverStruct === undefined &&
72            CpuFreqExtendStruct.selectCpuFreqStruct === undefined &&
73            !BinderStruct.selectCpuFreqStruct &&
74            !BinderStruct.hoverCpuFreqStruct &&
75            !TabPaneFreqStatesDataCut.isStateTabHover &&
76            (SpSegmentationChart.tabHoverObj && SpSegmentationChart.tabHoverObj.key === '')) {
77            AllstatesStruct.hoverThreadStruct = undefined;
78            SpSegmentationChart.trace.traceSheetEL!.systemLogFlag = undefined;
79            SpSegmentationChart.trace.tipEL!.style.display = 'none';
80            find = false;
81        }
82        threadReq.context.closePath();
83    }
84    render(threadReq: RequestMessage, threadList: Array<unknown>, threadFilter: Array<unknown>): void { }
85}
86
87export class AllstatesStruct extends BaseThreadStruct {
88    static hoverThreadStruct: AllstatesStruct | undefined;
89    static selectThreadStruct: AllstatesStruct | undefined;
90    static selectThreadStructList: Array<AllstatesStruct> = new Array<AllstatesStruct>();
91    static firstselectThreadStruct: AllstatesStruct | undefined;
92    argSetID: number | undefined;
93    translateY: number | undefined;
94    textMetricsWidth: number | undefined;
95    static startCycleTime: number = 0;
96    static endTime: number = 0;
97
98    static drawThread(threadContext: CanvasRenderingContext2D, data: AllstatesStruct): void {
99        if (data.frame) {
100            threadContext.globalAlpha = 1;
101            let stateText = AllstatesStruct.getEndState(data.state || '');
102            threadContext.fillStyle = Utils.getStateColor(data.state === 'S' ? 'Sleeping' : data.state || '');
103            threadContext.fillRect(data.frame.x, data.frame.y, data.frame.width, data.frame.height);
104            threadContext.fillStyle = '#fff';
105            threadContext.textBaseline = 'middle';
106            threadContext.font = '8px sans-serif';
107            data.frame.width > 7 && drawString(threadContext, stateText, 2, data.frame, data);
108            if (
109                AllstatesStruct.selectThreadStruct &&
110                AllstatesStruct.equals(AllstatesStruct.selectThreadStruct, data) &&
111                (AllstatesStruct.selectThreadStruct.state !== 'S' || data.name === 'all-state')
112            ) {
113                threadContext.strokeStyle = '#232c5d';
114                threadContext.lineWidth = 2;
115                threadContext.strokeRect(
116                    data.frame.x,
117                    data.frame.y,
118                    data.frame.width - 2,
119                    data.frame.height
120                );
121            }
122            if (AllstatesStruct.hoverThreadStruct === data && data.name === 'all-state') {
123                let pointX: number = ns2x(
124                    data.startTime || 0,
125                    TraceRow.range!.startNS,
126                    TraceRow.range!.endNS,
127                    TraceRow.range!.totalNS,
128                    new Rect(0, 0, TraceRow.FRAME_WIDTH, 0)
129                );
130                SpSegmentationChart.trace.traceSheetEL!.systemLogFlag = new Flag(
131                    Math.floor(pointX),
132                    0,
133                    0,
134                    0,
135                    data.startTime || 0,
136                    '#000000',
137                    '',
138                    true,
139                    ''
140                );
141            }
142        }
143    }
144}
145