Skip to content

Commit

Permalink
feat(android): runOnMain, postFrameCallback & removeFrameCallback (#9943
Browse files Browse the repository at this point in the history
)
  • Loading branch information
triniwiz committed Jul 13, 2022
1 parent 6f48b4b commit 49343cb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
46 changes: 37 additions & 9 deletions packages/core/fps-meter/fps-native.android.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
import * as definition from './fps-native';
import { Device } from '../platform';

export class FPSCallback implements definition.FPSCallback {
private impl: android.view.Choreographer.FrameCallback;
private impl: android.view.Choreographer.FrameCallback | ((nanos: number) => void);
private onFrame: (currentTimeMillis: number) => void;

public running: boolean;

sdkVersion: number;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.running = false;
this.onFrame = onFrame;

this.impl = new android.view.Choreographer.FrameCallback({
doFrame: (nanos: number) => {
this.sdkVersion = parseInt(Device.sdkVersion);

if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
this.impl = (nanos: number) => {
this.handleFrame(nanos);
},
});
};
} else {
this.impl = new android.view.Choreographer.FrameCallback({
doFrame: (nanos: number) => {
this.handleFrame(nanos);
},
});
}
}

private _isNativeFramesSupported() {
return typeof (<any>global).__postFrameCallback === 'function' && typeof (<any>global).__removeFrameCallback === 'function';
}

public start() {
if (this.running) {
return;
}

android.view.Choreographer.getInstance().postFrameCallback(this.impl);
if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
(global as any).__postFrameCallback(this.impl);
} else {
android.view.Choreographer.getInstance().postFrameCallback(this.impl as any);
}

this.running = true;
}

Expand All @@ -31,7 +49,12 @@ export class FPSCallback implements definition.FPSCallback {
return;
}

android.view.Choreographer.getInstance().removeFrameCallback(this.impl);
if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
(global as any).__removeFrameCallback(this.impl);
} else {
android.view.Choreographer.getInstance().removeFrameCallback(this.impl as any);
}

this.running = false;
}

Expand All @@ -43,6 +66,11 @@ export class FPSCallback implements definition.FPSCallback {
// divide by 1 000 000 since the parameter is in nanoseconds
this.onFrame(nanos / 1000000);
// add the FrameCallback instance again since it is automatically removed from the Choreographer
android.view.Choreographer.getInstance().postFrameCallback(this.impl);

if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
(global as any).__postFrameCallback(this.impl);
} else {
android.view.Choreographer.getInstance().postFrameCallback(this.impl as any);
}
}
}
34 changes: 24 additions & 10 deletions packages/core/utils/mainthread-helper.android.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import { android as ad } from '../application';

export function dispatchToMainThread(func: () => void) {
new android.os.Handler(android.os.Looper.getMainLooper()).post(
new java.lang.Runnable({
run: func,
})
);
const runOnMainThread = (global as any).__runOnMainThread;
if (runOnMainThread) {
runOnMainThread(() => {
func();
});
} else {
new android.os.Handler(android.os.Looper.getMainLooper()).post(
new java.lang.Runnable({
run: func,
})
);
}
}

export function isMainThread(): boolean {
return android.os.Looper.myLooper() === android.os.Looper.getMainLooper();
}

export function dispatchToUIThread(func: () => void) {
return function (func) {
if (func) {
func();
}
};
const activity: androidx.appcompat.app.AppCompatActivity = ad.foregroundActivity || ad.startActivity;
if (activity && func) {
activity.runOnUiThread(
new java.lang.Runnable({
run() {
func();
},
})
);
}
}

0 comments on commit 49343cb

Please sign in to comment.