• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import { createGPData, extractNumber } from '../base/BaseProfilerUtils';
16import { BaseProfiler } from '../base/BaseProfiler';
17import { CollectorType } from '../base/ProfilerConstant';
18import { SocketProfiler } from '../base/SocketProfiler';
19import SPLogger from '../../../common/utils/SPLogger';
20
21import worker from '@ohos.worker';
22import WorkerHandler from '../WorkerHandler';
23let mainWorker = globalThis.MainWorker;
24
25mainWorker.onmessage = function (result) {
26  WorkerHandler.socketHandler(result);
27};
28
29export class FPS extends BaseProfiler implements SocketProfiler {
30  private fpsMap: Map<String, String> = new Map;
31
32  public static instance: FPS = null;
33  public static getInstance() {
34    if (this.instance == null) {
35      this.instance = new FPS();
36    }
37    return this.instance;
38  }
39
40  init() {
41    //初始化FPS
42    return CollectorType.TYPE_FPS;
43  }
44
45  isSupport() {
46    if (globalThis.useDaemon) {
47      return true;
48    }
49    return false;
50  }
51
52  readData() {
53    if (globalThis.useDaemon) {
54      this.readMessageQueue();
55    }
56    return createGPData('FPS', this.fpsMap);
57  }
58
59  readMessageQueue() {
60    mainWorker.postMessage({ fps: true, pkg: globalThis.collectPkg });
61    if (globalThis.fpsArr.length > 0) {
62      let fpsQueue: String[] = globalThis.fpsArr;
63      let fpsJitterQueue: String[] = globalThis.fpsJitterArr;
64      let curFPS = fpsQueue.pop();
65      globalThis.timerFps = curFPS;
66      let curFPSJitter = fpsJitterQueue.pop();
67      let fpsJitters = '"' + curFPSJitter.split('==').join(',') + '"';
68      this.fpsMap.set('fpsJitters', fpsJitters);
69      this.fpsMap.set('fps', extractNumber(curFPS));
70    }
71  }
72}
73