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