1 | import { SDK_VERSION } from '../utils/constants';
|
2 | export class FPSCallback {
|
3 | constructor(onFrame) {
|
4 | this.running = false;
|
5 | this.onFrame = onFrame;
|
6 | this.nativeFramesSupported = SDK_VERSION >= 24 && this._isNativeFramesSupported();
|
7 | if (this.nativeFramesSupported) {
|
8 | this.impl = (nanos) => {
|
9 | this.handleFrame(nanos);
|
10 | };
|
11 | }
|
12 | else {
|
13 | this.impl = new android.view.Choreographer.FrameCallback({
|
14 | doFrame: (nanos) => {
|
15 | this.handleFrame(nanos);
|
16 | },
|
17 | });
|
18 | }
|
19 | }
|
20 | _isNativeFramesSupported() {
|
21 | return typeof global.__postFrameCallback === 'function' && typeof global.__removeFrameCallback === 'function';
|
22 | }
|
23 | start() {
|
24 | if (this.running) {
|
25 | return;
|
26 | }
|
27 | if (this.nativeFramesSupported) {
|
28 | global.__postFrameCallback(this.impl);
|
29 | }
|
30 | else {
|
31 | android.view.Choreographer.getInstance().postFrameCallback(this.impl);
|
32 | }
|
33 | this.running = true;
|
34 | }
|
35 | stop() {
|
36 | if (!this.running) {
|
37 | return;
|
38 | }
|
39 | if (this.nativeFramesSupported) {
|
40 | global.__removeFrameCallback(this.impl);
|
41 | }
|
42 | else {
|
43 | android.view.Choreographer.getInstance().removeFrameCallback(this.impl);
|
44 | }
|
45 | this.running = false;
|
46 | }
|
47 | handleFrame(nanos) {
|
48 | if (!this.running) {
|
49 | return;
|
50 | }
|
51 |
|
52 | this.onFrame(nanos / 1000000);
|
53 |
|
54 | if (this.nativeFramesSupported) {
|
55 | global.__postFrameCallback(this.impl);
|
56 | }
|
57 | else {
|
58 | android.view.Choreographer.getInstance().postFrameCallback(this.impl);
|
59 | }
|
60 | }
|
61 | }
|
62 |
|
\ | No newline at end of file |