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