Blame view

天文台pc/tianwentai-ui/node_modules/motion-dom/dist/es/stats/index.mjs 3.73 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  import { activeAnimations } from './animation-count.mjs';
  import { statsBuffer } from './buffer.mjs';
  import { frame, cancelFrame, frameData } from '../frameloop/frame.mjs';
  
  function record() {
      const { value } = statsBuffer;
      if (value === null) {
          cancelFrame(record);
          return;
      }
      value.frameloop.rate.push(frameData.delta);
      value.animations.mainThread.push(activeAnimations.mainThread);
      value.animations.waapi.push(activeAnimations.waapi);
      value.animations.layout.push(activeAnimations.layout);
  }
  function mean(values) {
      return values.reduce((acc, value) => acc + value, 0) / values.length;
  }
  function summarise(values, calcAverage = mean) {
      if (values.length === 0) {
          return {
              min: 0,
              max: 0,
              avg: 0,
          };
      }
      return {
          min: Math.min(...values),
          max: Math.max(...values),
          avg: calcAverage(values),
      };
  }
  const msToFps = (ms) => Math.round(1000 / ms);
  function clearStatsBuffer() {
      statsBuffer.value = null;
      statsBuffer.addProjectionMetrics = null;
  }
  function reportStats() {
      const { value } = statsBuffer;
      if (!value) {
          throw new Error("Stats are not being measured");
      }
      clearStatsBuffer();
      cancelFrame(record);
      const summary = {
          frameloop: {
              setup: summarise(value.frameloop.setup),
              rate: summarise(value.frameloop.rate),
              read: summarise(value.frameloop.read),
              resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
              preUpdate: summarise(value.frameloop.preUpdate),
              update: summarise(value.frameloop.update),
              preRender: summarise(value.frameloop.preRender),
              render: summarise(value.frameloop.render),
              postRender: summarise(value.frameloop.postRender),
          },
          animations: {
              mainThread: summarise(value.animations.mainThread),
              waapi: summarise(value.animations.waapi),
              layout: summarise(value.animations.layout),
          },
          layoutProjection: {
              nodes: summarise(value.layoutProjection.nodes),
              calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
              calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
          },
      };
      /**
       * Convert the rate to FPS
       */
      const { rate } = summary.frameloop;
      rate.min = msToFps(rate.min);
      rate.max = msToFps(rate.max);
      rate.avg = msToFps(rate.avg);
      [rate.min, rate.max] = [rate.max, rate.min];
      return summary;
  }
  function recordStats() {
      if (statsBuffer.value) {
          clearStatsBuffer();
          throw new Error("Stats are already being measured");
      }
      const newStatsBuffer = statsBuffer;
      newStatsBuffer.value = {
          frameloop: {
              setup: [],
              rate: [],
              read: [],
              resolveKeyframes: [],
              preUpdate: [],
              update: [],
              preRender: [],
              render: [],
              postRender: [],
          },
          animations: {
              mainThread: [],
              waapi: [],
              layout: [],
          },
          layoutProjection: {
              nodes: [],
              calculatedTargetDeltas: [],
              calculatedProjections: [],
          },
      };
      newStatsBuffer.addProjectionMetrics = (metrics) => {
          const { layoutProjection } = newStatsBuffer.value;
          layoutProjection.nodes.push(metrics.nodes);
          layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
          layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
      };
      frame.postRender(record, true);
      return reportStats;
  }
  
  export { recordStats };
  //# sourceMappingURL=index.mjs.map