• 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 {
17  BaseStruct,
18  dataFilterHandler,
19  drawLoadingFrame,
20  drawString,
21  isFrameContainPoint,
22  Render,
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 PerfToolRender extends Render {
29  renderMainThread(
30    perfReq: {
31      context: CanvasRenderingContext2D;
32      useCache: boolean;
33      type: string;
34      index: number;
35    },
36    row: TraceRow<PerfToolStruct>
37  ): void {
38    PerfToolStruct.index = perfReq.index;
39    let perfToolList = row.dataList;
40    let perfToolFilter = row.dataListCache;
41    dataFilterHandler(perfToolList, perfToolFilter, {
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: perfReq.useCache || !(TraceRow.range?.refresh ?? false),
50    });
51    drawLoadingFrame(perfReq.context, perfToolFilter, row);
52    perfReq.context.beginPath();
53    let find = false;
54    for (let re of perfToolFilter) {
55      PerfToolStruct.draw(perfReq.context, re);
56      if (row.isHover && re.frame && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) {
57        PerfToolStruct.hoverPerfToolStruct = re;
58        find = true;
59      }
60    }
61    if (!find && row.isHover) {
62      PerfToolStruct.hoverPerfToolStruct = undefined;
63    }
64    perfReq.context.closePath();
65  }
66}
67export function PerfToolsStructOnClick(clickRowType: string, sp: SpSystemTrace): Promise<unknown> {
68  return new Promise((resolve, reject) => {
69    if (clickRowType === TraceRow.ROW_TYPE_PERF_TOOL && PerfToolStruct.hoverPerfToolStruct) {
70      PerfToolStruct.selectPerfToolStruct = PerfToolStruct.hoverPerfToolStruct;
71      sp.traceSheetEL?.displayPerfToolsData(PerfToolStruct.selectPerfToolStruct);
72      sp.timerShaftEL?.modifyFlagList(undefined);
73      reject(new Error());
74    } else {
75      resolve(null);
76    }
77  });
78}
79export class PerfToolStruct extends BaseStruct {
80  static hoverPerfToolStruct: PerfToolStruct | undefined;
81  static selectPerfToolStruct: PerfToolStruct | undefined;
82  static index = 0;
83  count: string | undefined;
84  startNS: number | undefined;
85  dur: number | undefined;
86  id: number | undefined;
87  name: string | undefined;
88
89  static draw(PerfContext: CanvasRenderingContext2D, data: PerfToolStruct): void {
90    if (data.frame) {
91      let width = data.frame.width || 0;
92      PerfContext.globalAlpha = 1;
93      PerfContext.fillStyle = ColorUtils.colorForTid(PerfToolStruct.index);
94      PerfContext.fillRect(data.frame.x, data.frame.y, width, data.frame.height);
95      if (data.frame.width > 7) {
96        PerfContext.textBaseline = 'middle';
97        PerfContext.lineWidth = 1;
98        let countText: string | undefined = '';
99        if (data.count) {
100          countText = `${data.count}`;
101        }
102        PerfContext.fillStyle = ColorUtils.funcTextColor('#000');
103        drawString(PerfContext, countText, 2, data.frame, data);
104      }
105      if (
106        data.id === PerfToolStruct.selectPerfToolStruct?.id &&
107        data.name === PerfToolStruct.selectPerfToolStruct?.name
108      ) {
109        PerfContext.strokeStyle = '#000';
110        PerfContext.lineWidth = 2;
111        PerfContext.strokeRect(data.frame.x, data.frame.y + 1, data.frame.width, data.frame.height - 2);
112      }
113    }
114  }
115
116  static isHover(clock: PerfToolStruct): boolean {
117    return clock === PerfToolStruct.hoverPerfToolStruct || clock === PerfToolStruct.selectPerfToolStruct;
118  }
119}
120