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 { type LtpoStruct } from '../../database/ui-worker/ProcedureWorkerLTPO';
15import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
16import { threadPool } from '../SqlLite';
17import { TraceRow } from '../../component/trace/base/TraceRow';
18
19export function lostFrameSender(tName: String, fName: String, row: TraceRow<LtpoStruct>): Promise<LtpoStruct[]> {
20  let trafic: number = TraficEnum.Memory;
21  let width = row.clientWidth - CHART_OFFSET_LEFT;
22  if ((trafic === TraficEnum.SharedArrayBuffer || trafic === TraficEnum.Memory) && !row.sharedArrayBuffers) {
23    row.sharedArrayBuffers = {
24      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
25      startTime: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
26      presentId: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
27    };
28  }
29  return new Promise((resolve): void => {
30    threadPool.submitProto(
31      QueryEnum.LostFrameData,
32      {
33        threadName: tName,
34        funcName: fName,
35        startNS: TraceRow.range?.startNS || 0,
36        endNS: TraceRow.range?.endNS || 0,
37        recordStartNS: window.recordStartNS,
38        recordEndNS: window.recordEndNS,
39        width: width,
40        trafic: trafic,
41        sharedArrayBuffers: row.sharedArrayBuffers,
42      },
43      (res: unknown, len: number, transfer: boolean): void => {
44        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
45      }
46    );
47  });
48}
49
50function arrayBufferHandler(res: any, len: number): LtpoStruct[] {
51  let outArr: LtpoStruct[] = [];
52  let startTime = new Float64Array(res.startTime);
53  let dur = new Float64Array(res.dur);
54  let id = new Uint16Array(res.id);
55  let presentId = new Float64Array(res.presentId);
56  for (let i = 0; i < len; i++) {
57    outArr.push({
58      id: id[i],
59      dur: dur[i],
60      startTime: startTime[i],
61      presentId: presentId[i],
62    } as unknown as LtpoStruct);
63  }
64  return outArr;
65}
66