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, TraficEnum } from './utils/QueryEnum';
15fb726d48Sopenharmony_ciimport { threadPool } from '../SqlLite';
16fb726d48Sopenharmony_ciimport { TraceRow } from '../../component/trace/base/TraceRow';
17fb726d48Sopenharmony_ciimport { JanksStruct } from '../../bean/JanksStruct';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ciexport function frameJanksSender(queryEnum: number, row: TraceRow<JanksStruct>): Promise<JanksStruct[]> {
20fb726d48Sopenharmony_ci  let transferJankDataType: number = TraficEnum.Memory;
21fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
22fb726d48Sopenharmony_ci  if (transferJankDataType === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
23fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
24fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
25fb726d48Sopenharmony_ci      ipid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
26fb726d48Sopenharmony_ci      name: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
27fb726d48Sopenharmony_ci      app_dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
28fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
29fb726d48Sopenharmony_ci      ts: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
30fb726d48Sopenharmony_ci      jank_tag: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
31fb726d48Sopenharmony_ci      pid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
32fb726d48Sopenharmony_ci      rs_ts: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
33fb726d48Sopenharmony_ci      rs_vsync: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
34fb726d48Sopenharmony_ci      rs_dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
35fb726d48Sopenharmony_ci      rs_ipid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
36fb726d48Sopenharmony_ci      rs_pid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
37fb726d48Sopenharmony_ci      rs_name: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
38fb726d48Sopenharmony_ci      depth: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
39fb726d48Sopenharmony_ci    };
40fb726d48Sopenharmony_ci  }
41fb726d48Sopenharmony_ci  return new Promise((resolve): void => {
42fb726d48Sopenharmony_ci    threadPool.submitProto(
43fb726d48Sopenharmony_ci      queryEnum,
44fb726d48Sopenharmony_ci      {
45fb726d48Sopenharmony_ci        queryEnum: queryEnum,
46fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
47fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
48fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
49fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
50fb726d48Sopenharmony_ci        width: width,
51fb726d48Sopenharmony_ci        t: new Date().getTime(),
52fb726d48Sopenharmony_ci        trafic: transferJankDataType,
53fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
54fb726d48Sopenharmony_ci      },
55fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
56fb726d48Sopenharmony_ci        // @ts-ignore
57fb726d48Sopenharmony_ci        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
58fb726d48Sopenharmony_ci      }
59fb726d48Sopenharmony_ci    );
60fb726d48Sopenharmony_ci  });
61fb726d48Sopenharmony_ci}
62fb726d48Sopenharmony_ci
63fb726d48Sopenharmony_cifunction arrayBufferHandler(res: unknown, len: number): unknown[] {
64fb726d48Sopenharmony_ci  let outArr = [];
65fb726d48Sopenharmony_ci  // @ts-ignore
66fb726d48Sopenharmony_ci  let id = new Uint16Array(res.id);
67fb726d48Sopenharmony_ci  // @ts-ignore
68fb726d48Sopenharmony_ci  let ipId = new Uint16Array(res.ipid);
69fb726d48Sopenharmony_ci  // @ts-ignore
70fb726d48Sopenharmony_ci  let nameId = new Int32Array(res.name);
71fb726d48Sopenharmony_ci  // @ts-ignore
72fb726d48Sopenharmony_ci  let app_dur = new Float64Array(res.app_dur);
73fb726d48Sopenharmony_ci  // @ts-ignore
74fb726d48Sopenharmony_ci  let dur = new Float64Array(res.dur);
75fb726d48Sopenharmony_ci  // @ts-ignore
76fb726d48Sopenharmony_ci  let ts = new Float64Array(res.ts);
77fb726d48Sopenharmony_ci  // @ts-ignore
78fb726d48Sopenharmony_ci  let jank_tag = new Uint16Array(res.jank_tag);
79fb726d48Sopenharmony_ci  // @ts-ignore
80fb726d48Sopenharmony_ci  let pid = new Uint16Array(res.pid);
81fb726d48Sopenharmony_ci  // @ts-ignore
82fb726d48Sopenharmony_ci  let rsTs = new Float64Array(res.rs_ts);
83fb726d48Sopenharmony_ci  // @ts-ignore
84fb726d48Sopenharmony_ci  let rs_vsync = new Int32Array(res.rs_vsync);
85fb726d48Sopenharmony_ci  // @ts-ignore
86fb726d48Sopenharmony_ci  let rs_dur = new Float64Array(res.rs_dur);
87fb726d48Sopenharmony_ci  // @ts-ignore
88fb726d48Sopenharmony_ci  let rs_ipId = new Uint16Array(res.rs_ipid);
89fb726d48Sopenharmony_ci  // @ts-ignore
90fb726d48Sopenharmony_ci  let rs_pid = new Uint16Array(res.rs_pid);
91fb726d48Sopenharmony_ci  // @ts-ignore
92fb726d48Sopenharmony_ci  let rs_name = new Int32Array(res.rs_name);
93fb726d48Sopenharmony_ci  // @ts-ignore
94fb726d48Sopenharmony_ci  let depth = new Uint16Array(res.depth);
95fb726d48Sopenharmony_ci  for (let index = 0; index < len; index++) {
96fb726d48Sopenharmony_ci    outArr.push({
97fb726d48Sopenharmony_ci      id: id[index],
98fb726d48Sopenharmony_ci      ipid: ipId[index],
99fb726d48Sopenharmony_ci      name: nameId[index],
100fb726d48Sopenharmony_ci      app_dur: app_dur[index],
101fb726d48Sopenharmony_ci      dur: dur[index],
102fb726d48Sopenharmony_ci      ts: ts[index],
103fb726d48Sopenharmony_ci      jank_tag: jank_tag[index],
104fb726d48Sopenharmony_ci      pid: pid[index],
105fb726d48Sopenharmony_ci      rs_ts: rsTs[index],
106fb726d48Sopenharmony_ci      rs_vsync: rs_vsync[index],
107fb726d48Sopenharmony_ci      rs_dur: rs_dur[index],
108fb726d48Sopenharmony_ci      rs_ipid: rs_ipId[index],
109fb726d48Sopenharmony_ci      rs_pid: rs_pid[index],
110fb726d48Sopenharmony_ci      rs_name: rs_name[index],
111fb726d48Sopenharmony_ci      depth: depth[index],
112fb726d48Sopenharmony_ci    });
113fb726d48Sopenharmony_ci  }
114fb726d48Sopenharmony_ci  return outArr;
115fb726d48Sopenharmony_ci}
116