1// Copyright (c) 2021 Huawei Device Co., Ltd.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from '../utils/QueryEnum';
15import { threadPool } from '../../SqlLite';
16import { TraceRow } from '../../../component/trace/base/TraceRow';
17import { FuncStruct } from '../../ui-worker/ProcedureWorkerFunc';
18
19export function processTouchEventDispatchDataSender(tid: number, row: TraceRow<FuncStruct>): Promise<FuncStruct[]> {
20  let trafic: number = TraficEnum.Memory;
21  let width = row.clientWidth - CHART_OFFSET_LEFT;
22  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
23    row.sharedArrayBuffers = {
24      tid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
25      pid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
26      startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
27      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
28      id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
29      depth: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
30    };
31  }
32  return new Promise((resolve) => {
33    threadPool.submitProto(
34      QueryEnum.processTouchEventDispatchData,
35      {
36        startNS: TraceRow.range?.startNS || 0,
37        endNS: TraceRow.range?.endNS || 0,
38        recordStartNS: window.recordStartNS,
39        recordEndNS: window.recordEndNS,
40        t: Date.now(),
41        width: width,
42        trafic: trafic,
43        sharedArrayBuffers: row.sharedArrayBuffers,
44        tid: tid,
45      },
46      (res: unknown, len: number, transfer: boolean) => {
47        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
48      }
49    );
50
51
52  });
53}
54
55function arrayBufferHandler(buffers: unknown, len: number): FuncStruct[] {
56  let outArr: FuncStruct[] = [];
57  //@ts-ignore
58  let tid = new Int32Array(buffers.tid);
59  //@ts-ignore
60  let pid = new Int32Array(buffers.pid);
61  //@ts-ignore
62  let startTs = new Float64Array(buffers.startTs);
63  //@ts-ignore
64  let dur = new Float64Array(buffers.dur);
65  //@ts-ignore
66  let id = new Int32Array(buffers.id);
67  //@ts-ignore
68  let depth = new Int32Array(buffers.depth);
69  for (let i = 0; i < len; i++) {
70    outArr.push({
71      tid: tid[i],
72      pid: pid[i],
73      startTs: startTs[i],
74      dur: dur[i],
75      id: id[i],
76      depth: depth[i],
77    } as unknown as FuncStruct);
78  }
79  return outArr;
80}
81