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 { FrameAnimationStruct } from '../ui-worker/ProcedureWorkerFrameAnimation';
18fb726d48Sopenharmony_ciimport { FrameDynamicStruct } from '../ui-worker/ProcedureWorkerFrameDynamic';
19fb726d48Sopenharmony_ciimport { FrameSpacingStruct } from '../ui-worker/ProcedureWorkerFrameSpacing';
20fb726d48Sopenharmony_ci
21fb726d48Sopenharmony_ciexport function frameAnimationSender(row: TraceRow<FrameAnimationStruct>): Promise<FrameAnimationStruct[]> {
22fb726d48Sopenharmony_ci  let transferAnimationDataType: number = TraficEnum.ProtoBuffer;
23fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
24fb726d48Sopenharmony_ci  if (transferAnimationDataType === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
25fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
26fb726d48Sopenharmony_ci      animationId: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
27fb726d48Sopenharmony_ci      status: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
28fb726d48Sopenharmony_ci      startTs: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
29fb726d48Sopenharmony_ci      endTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
30fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
31fb726d48Sopenharmony_ci      depth: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
32fb726d48Sopenharmony_ci    };
33fb726d48Sopenharmony_ci  }
34fb726d48Sopenharmony_ci  return new Promise((resolve): void => {
35fb726d48Sopenharmony_ci    threadPool.submitProto(
36fb726d48Sopenharmony_ci      QueryEnum.FrameAnimationData,
37fb726d48Sopenharmony_ci      {
38fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
39fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
40fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
41fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
42fb726d48Sopenharmony_ci        width: width,
43fb726d48Sopenharmony_ci        t: new Date().getTime(),
44fb726d48Sopenharmony_ci        trafic: transferAnimationDataType,
45fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
46fb726d48Sopenharmony_ci      },
47fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
48fb726d48Sopenharmony_ci        // @ts-ignore
49fb726d48Sopenharmony_ci        resolve(animationBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
50fb726d48Sopenharmony_ci      }
51fb726d48Sopenharmony_ci    );
52fb726d48Sopenharmony_ci  });
53fb726d48Sopenharmony_ci}
54fb726d48Sopenharmony_ci
55fb726d48Sopenharmony_cifunction animationBufferHandler(res: unknown, len: number): unknown[] {
56fb726d48Sopenharmony_ci  // @ts-ignore
57fb726d48Sopenharmony_ci  let recordNs: number = (window as unknown).recordStartNS;
58fb726d48Sopenharmony_ci  let outArr = [];
59fb726d48Sopenharmony_ci  // @ts-ignore
60fb726d48Sopenharmony_ci  let animationId = new Uint16Array(res.animationId);
61fb726d48Sopenharmony_ci  // @ts-ignore
62fb726d48Sopenharmony_ci  let status = new Uint16Array(res.status);
63fb726d48Sopenharmony_ci  // @ts-ignore
64fb726d48Sopenharmony_ci  let startTs = new Float64Array(res.startTs);
65fb726d48Sopenharmony_ci  // @ts-ignore
66fb726d48Sopenharmony_ci  let endTs = new Float64Array(res.endTs);
67fb726d48Sopenharmony_ci  // @ts-ignore
68fb726d48Sopenharmony_ci  let dur = new Float64Array(res.dur);
69fb726d48Sopenharmony_ci  // @ts-ignore
70fb726d48Sopenharmony_ci  let depth = new Uint16Array(res.depth);
71fb726d48Sopenharmony_ci  for (let index = 0; index < len; index++) {
72fb726d48Sopenharmony_ci    outArr.push({
73fb726d48Sopenharmony_ci      animationId: animationId[index],
74fb726d48Sopenharmony_ci      status: status[index],
75fb726d48Sopenharmony_ci      startTs: startTs[index],
76fb726d48Sopenharmony_ci      endTs: endTs[index],
77fb726d48Sopenharmony_ci      dur: dur[index],
78fb726d48Sopenharmony_ci      depth: depth[index],
79fb726d48Sopenharmony_ci      inputTime: startTs[index] + recordNs,
80fb726d48Sopenharmony_ci      endTime: endTs[index] + recordNs,
81fb726d48Sopenharmony_ci    });
82fb726d48Sopenharmony_ci  }
83fb726d48Sopenharmony_ci  return outArr;
84fb726d48Sopenharmony_ci}
85fb726d48Sopenharmony_ci
86fb726d48Sopenharmony_ciexport function frameDynamicSender(row: TraceRow<FrameDynamicStruct>): Promise<FrameDynamicStruct[]> {
87fb726d48Sopenharmony_ci  let transferDynamicDataType: number = TraficEnum.Memory;
88fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
89fb726d48Sopenharmony_ci  if (transferDynamicDataType === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
90fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
91fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
92fb726d48Sopenharmony_ci      x: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
93fb726d48Sopenharmony_ci      y: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
94fb726d48Sopenharmony_ci      width: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
95fb726d48Sopenharmony_ci      height: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
96fb726d48Sopenharmony_ci      alpha: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
97fb726d48Sopenharmony_ci      ts: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
98fb726d48Sopenharmony_ci    };
99fb726d48Sopenharmony_ci  }
100fb726d48Sopenharmony_ci  return new Promise((resolve): void => {
101fb726d48Sopenharmony_ci    threadPool.submitProto(
102fb726d48Sopenharmony_ci      QueryEnum.FrameDynamicData,
103fb726d48Sopenharmony_ci      {
104fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
105fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
106fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
107fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
108fb726d48Sopenharmony_ci        width: width,
109fb726d48Sopenharmony_ci        t: new Date().getTime(),
110fb726d48Sopenharmony_ci        trafic: transferDynamicDataType,
111fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
112fb726d48Sopenharmony_ci      },
113fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
114fb726d48Sopenharmony_ci        // @ts-ignore
115fb726d48Sopenharmony_ci        resolve(dynamicBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
116fb726d48Sopenharmony_ci      }
117fb726d48Sopenharmony_ci    );
118fb726d48Sopenharmony_ci  });
119fb726d48Sopenharmony_ci}
120fb726d48Sopenharmony_ci
121fb726d48Sopenharmony_cifunction dynamicBufferHandler(res: unknown, len: number): unknown[] {
122fb726d48Sopenharmony_ci  let outArr = [];
123fb726d48Sopenharmony_ci  // @ts-ignore
124fb726d48Sopenharmony_ci  let id = new Uint16Array(res.id);
125fb726d48Sopenharmony_ci  // @ts-ignore
126fb726d48Sopenharmony_ci  let x = new Float32Array(res.x);
127fb726d48Sopenharmony_ci  // @ts-ignore
128fb726d48Sopenharmony_ci  let y = new Float32Array(res.y);
129fb726d48Sopenharmony_ci  // @ts-ignore
130fb726d48Sopenharmony_ci  let width = new Float32Array(res.width);
131fb726d48Sopenharmony_ci  // @ts-ignore
132fb726d48Sopenharmony_ci  let height = new Float32Array(res.height);
133fb726d48Sopenharmony_ci  // @ts-ignore
134fb726d48Sopenharmony_ci  let alpha = new Float32Array(res.alpha);
135fb726d48Sopenharmony_ci  // @ts-ignore
136fb726d48Sopenharmony_ci  let ts = new Float64Array(res.ts);
137fb726d48Sopenharmony_ci  for (let index = 0; index < len; index++) {
138fb726d48Sopenharmony_ci    outArr.push({
139fb726d48Sopenharmony_ci      id: id[index],
140fb726d48Sopenharmony_ci      x: x[index],
141fb726d48Sopenharmony_ci      y: y[index],
142fb726d48Sopenharmony_ci      width: width[index],
143fb726d48Sopenharmony_ci      height: height[index],
144fb726d48Sopenharmony_ci      alpha: alpha[index],
145fb726d48Sopenharmony_ci      ts: ts[index],
146fb726d48Sopenharmony_ci    });
147fb726d48Sopenharmony_ci  }
148fb726d48Sopenharmony_ci  return outArr;
149fb726d48Sopenharmony_ci}
150fb726d48Sopenharmony_ci
151fb726d48Sopenharmony_ciexport function frameSpacingSender(
152fb726d48Sopenharmony_ci  physicalWidth: number,
153fb726d48Sopenharmony_ci  physicalHeight: number,
154fb726d48Sopenharmony_ci  row: TraceRow<FrameSpacingStruct>
155fb726d48Sopenharmony_ci): Promise<FrameSpacingStruct[]> {
156fb726d48Sopenharmony_ci  let transferSpacingDataType: number = TraficEnum.Memory;
157fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
158fb726d48Sopenharmony_ci  if (transferSpacingDataType === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
159fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
160fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
161fb726d48Sopenharmony_ci      x: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
162fb726d48Sopenharmony_ci      y: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
163fb726d48Sopenharmony_ci      currentFrameWidth: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
164fb726d48Sopenharmony_ci      currentFrameHeight: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
165fb726d48Sopenharmony_ci      currentTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
166fb726d48Sopenharmony_ci      frameSpacingResult: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
167fb726d48Sopenharmony_ci      preTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
168fb726d48Sopenharmony_ci      preFrameWidth: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
169fb726d48Sopenharmony_ci      preFrameHeight: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
170fb726d48Sopenharmony_ci      preX: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
171fb726d48Sopenharmony_ci      preY: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
172fb726d48Sopenharmony_ci    };
173fb726d48Sopenharmony_ci  }
174fb726d48Sopenharmony_ci  return new Promise((resolve): void => {
175fb726d48Sopenharmony_ci    threadPool.submitProto(
176fb726d48Sopenharmony_ci      QueryEnum.FrameSpacingData,
177fb726d48Sopenharmony_ci      {
178fb726d48Sopenharmony_ci        physicalWidth: physicalWidth,
179fb726d48Sopenharmony_ci        physicalHeight: physicalHeight,
180fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
181fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
182fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
183fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
184fb726d48Sopenharmony_ci        width: width,
185fb726d48Sopenharmony_ci        t: new Date().getTime(),
186fb726d48Sopenharmony_ci        trafic: transferSpacingDataType,
187fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
188fb726d48Sopenharmony_ci      },
189fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
190fb726d48Sopenharmony_ci        // @ts-ignore
191fb726d48Sopenharmony_ci        resolve(spacingBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
192fb726d48Sopenharmony_ci      }
193fb726d48Sopenharmony_ci    );
194fb726d48Sopenharmony_ci  });
195fb726d48Sopenharmony_ci}
196fb726d48Sopenharmony_ci
197fb726d48Sopenharmony_cifunction spacingBufferHandler(res: unknown, len: number): unknown[] {
198fb726d48Sopenharmony_ci  let outArr = [];
199fb726d48Sopenharmony_ci  // @ts-ignore
200fb726d48Sopenharmony_ci  let id = new Uint16Array(res.id);
201fb726d48Sopenharmony_ci  // @ts-ignore
202fb726d48Sopenharmony_ci  let x = new Float32Array(res.x);
203fb726d48Sopenharmony_ci  // @ts-ignore
204fb726d48Sopenharmony_ci  let y = new Float32Array(res.y);
205fb726d48Sopenharmony_ci  // @ts-ignore
206fb726d48Sopenharmony_ci  let currentFrameWidth = new Float32Array(res.currentFrameWidth);
207fb726d48Sopenharmony_ci  // @ts-ignore
208fb726d48Sopenharmony_ci  let currentFrameHeight = new Float32Array(res.currentFrameHeight);
209fb726d48Sopenharmony_ci  // @ts-ignore
210fb726d48Sopenharmony_ci  let currentTs = new Float64Array(res.currentTs);
211fb726d48Sopenharmony_ci  // @ts-ignore
212fb726d48Sopenharmony_ci  let frameSpacingResult = new Float32Array(res.frameSpacingResult);
213fb726d48Sopenharmony_ci  // @ts-ignore
214fb726d48Sopenharmony_ci  let preTs = new Float64Array(res.preTs);
215fb726d48Sopenharmony_ci  // @ts-ignore
216fb726d48Sopenharmony_ci  let preFrameWidth = new Float32Array(res.preFrameWidth);
217fb726d48Sopenharmony_ci  // @ts-ignore
218fb726d48Sopenharmony_ci  let preFrameHeight = new Float32Array(res.preFrameHeight);
219fb726d48Sopenharmony_ci  // @ts-ignore
220fb726d48Sopenharmony_ci  let preX = new Float32Array(res.preX);
221fb726d48Sopenharmony_ci  // @ts-ignore
222fb726d48Sopenharmony_ci  let preY = new Float32Array(res.preY);
223fb726d48Sopenharmony_ci  for (let index = 0; index < len; index++) {
224fb726d48Sopenharmony_ci    outArr.push({
225fb726d48Sopenharmony_ci      id: id[index],
226fb726d48Sopenharmony_ci      x: x[index],
227fb726d48Sopenharmony_ci      y: y[index],
228fb726d48Sopenharmony_ci      currentFrameWidth: currentFrameWidth[index],
229fb726d48Sopenharmony_ci      currentFrameHeight: currentFrameHeight[index],
230fb726d48Sopenharmony_ci      currentTs: currentTs[index],
231fb726d48Sopenharmony_ci      frameSpacingResult: frameSpacingResult[index].toFixed(1),
232fb726d48Sopenharmony_ci      preTs: preTs[index],
233fb726d48Sopenharmony_ci      preFrameWidth: preFrameWidth[index],
234fb726d48Sopenharmony_ci      preFrameHeight: preFrameHeight[index],
235fb726d48Sopenharmony_ci      preX: preX[index],
236fb726d48Sopenharmony_ci      preY: preY[index],
237fb726d48Sopenharmony_ci    });
238fb726d48Sopenharmony_ci  }
239fb726d48Sopenharmony_ci  return outArr;
240fb726d48Sopenharmony_ci}
241