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 { TraficEnum } from './utils/QueryEnum';
15fb726d48Sopenharmony_ciimport { lostFrameList } from './utils/AllMemoryCache';
16fb726d48Sopenharmony_ci
17fb726d48Sopenharmony_ciexport const queryPresentInfo = (args: any): string => {
18fb726d48Sopenharmony_ci  return `SELECT ts,dur,name FROM callstack WHERE callid in (SELECT id FROM "thread" WHERE name LIKE('${args.threadName}'))
19fb726d48Sopenharmony_ci    AND name LIKE('${args.funcName}')`;
20fb726d48Sopenharmony_ci};
21fb726d48Sopenharmony_ci
22fb726d48Sopenharmony_ciexport function lostFrameReceiver(data: any, proc: Function): void {
23fb726d48Sopenharmony_ci  if (data.params.trafic === TraficEnum.Memory) {
24fb726d48Sopenharmony_ci    if (!lostFrameList.has(data.params.pid)) {
25fb726d48Sopenharmony_ci      lostFrameList.set(data.params.pid, proc(queryPresentInfo(data.params)));
26fb726d48Sopenharmony_ci    }
27fb726d48Sopenharmony_ci    let res = lostFrameList.get(data.params.pid)!;
28fb726d48Sopenharmony_ci    arrayBufferHandler(data, res, true);
29fb726d48Sopenharmony_ci  } else {
30fb726d48Sopenharmony_ci    let res = proc(queryPresentInfo(data.params));
31fb726d48Sopenharmony_ci    arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
32fb726d48Sopenharmony_ci  }
33fb726d48Sopenharmony_ci}
34fb726d48Sopenharmony_ci
35fb726d48Sopenharmony_cifunction arrayBufferHandler(data: any, res: any[], transfer: boolean): void {
36fb726d48Sopenharmony_ci  let startTime = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startTime);
37fb726d48Sopenharmony_ci  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur);
38fb726d48Sopenharmony_ci  let argSetId = new Uint8Array(transfer ? res.length : data.params.sharedArrayBuffers.argSetId);
39fb726d48Sopenharmony_ci  let nofinish = new Uint8Array(transfer ? res.length : data.params.sharedArrayBuffers.nofinish);
40fb726d48Sopenharmony_ci  let presentId = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.presentId);
41fb726d48Sopenharmony_ci  res.forEach((it, i) => {
42fb726d48Sopenharmony_ci    let nameCutArr = it.name.split(' ');
43fb726d48Sopenharmony_ci    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.cpuData);
44fb726d48Sopenharmony_ci    startTime[i] = it.ts;
45fb726d48Sopenharmony_ci    dur[i] = it.dur;
46fb726d48Sopenharmony_ci    nofinish[i] = it.nofinish;
47fb726d48Sopenharmony_ci    argSetId[i] = it.argSetId;
48fb726d48Sopenharmony_ci    presentId[i] = Number(nameCutArr[nameCutArr.length - 1]);
49fb726d48Sopenharmony_ci  });
50fb726d48Sopenharmony_ci  (self as unknown as Worker).postMessage(
51fb726d48Sopenharmony_ci    {
52fb726d48Sopenharmony_ci      id: data.id,
53fb726d48Sopenharmony_ci      action: data.action,
54fb726d48Sopenharmony_ci      results: transfer
55fb726d48Sopenharmony_ci        ? {
56fb726d48Sopenharmony_ci            startTime: startTime.buffer,
57fb726d48Sopenharmony_ci            dur: dur.buffer,
58fb726d48Sopenharmony_ci            argSetID: argSetId.buffer,
59fb726d48Sopenharmony_ci            presentId: presentId.buffer,
60fb726d48Sopenharmony_ci          }
61fb726d48Sopenharmony_ci        : {},
62fb726d48Sopenharmony_ci      len: res.length,
63fb726d48Sopenharmony_ci      transfer: transfer,
64fb726d48Sopenharmony_ci    },
65fb726d48Sopenharmony_ci    transfer ? [startTime.buffer, dur.buffer, argSetId.buffer] : []
66fb726d48Sopenharmony_ci  );
67fb726d48Sopenharmony_ci}
68