diff --git a/packages/core/fps-meter/fps-native.android.ts b/packages/core/fps-meter/fps-native.android.ts index d52d2789c..ca979e163 100644 --- a/packages/core/fps-meter/fps-native.android.ts +++ b/packages/core/fps-meter/fps-native.android.ts @@ -1,20 +1,33 @@ 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 (global).__postFrameCallback === 'function' && typeof (global).__removeFrameCallback === 'function'; } public start() { @@ -22,7 +35,12 @@ export class FPSCallback implements definition.FPSCallback { 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; } @@ -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; } @@ -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); + } } } diff --git a/packages/core/utils/mainthread-helper.android.ts b/packages/core/utils/mainthread-helper.android.ts index d3ee92147..1838a6a6c 100644 --- a/packages/core/utils/mainthread-helper.android.ts +++ b/packages/core/utils/mainthread-helper.android.ts @@ -1,9 +1,18 @@ +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 { @@ -11,9 +20,14 @@ export function isMainThread(): boolean { } 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(); + }, + }) + ); + } }