forked from Blendman974/faceclaw
Phone CPU usage optimization
This commit is contained in:
1 parent
44eddedc01
commit
9560f3ac8d
26 files changed
+606
-167
No files matched your search
@@ -761,17 +761,19 @@ function renderAndSubmit(window: FlappyWindow, inputFrameId: number): void {
|
||||
}
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
||||
frameTimings.span(frameId, "submitSurfaceFrame", () =>
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
||||
)
|
||||
);
|
||||
window.lastSubmittedFingerprint = fingerprint;
|
||||
} catch (error) {
|
||||
|
||||
@@ -268,7 +268,6 @@ class DashboardController {
|
||||
private lockSurfaceConfigured = false;
|
||||
private lastLockScreenEnabled = lockScreenEnabledSetting.get();
|
||||
private offState: (() => void) | null = null;
|
||||
private offLog: (() => void) | null = null;
|
||||
private offRing: (() => void) | null = null;
|
||||
private offBattery: (() => void) | null = null;
|
||||
private offSilentMode: (() => void) | null = null;
|
||||
@@ -1327,9 +1326,6 @@ class DashboardController {
|
||||
ring: ringAddress,
|
||||
});
|
||||
this.communicator = communicator;
|
||||
this.offLog = communicator.onLog((line) => {
|
||||
this.appendLog(line);
|
||||
});
|
||||
this.offState = communicator.onStateChange((state) => {
|
||||
if (state.phase !== "connected") resetRingInputFilter();
|
||||
if (state.phase === "unpaired") {
|
||||
@@ -1542,8 +1538,6 @@ class DashboardController {
|
||||
const message = this.formatError(error);
|
||||
this.offState?.();
|
||||
this.offState = null;
|
||||
this.offLog?.();
|
||||
this.offLog = null;
|
||||
this.offRing?.();
|
||||
this.offRing = null;
|
||||
this.offBattery?.();
|
||||
@@ -1682,8 +1676,6 @@ class DashboardController {
|
||||
this.clearDashboardTimer();
|
||||
this.offState?.();
|
||||
this.offState = null;
|
||||
this.offLog?.();
|
||||
this.offLog = null;
|
||||
this.offRing?.();
|
||||
this.offRing = null;
|
||||
this.offBattery?.();
|
||||
@@ -2593,7 +2585,7 @@ class DashboardController {
|
||||
}
|
||||
|
||||
private appendLog(line: string): void {
|
||||
console.log(`[${formatTimestamp(new Date())}] ${line}`);
|
||||
//console.log(`[${formatTimestamp(new Date())}] ${line}`);
|
||||
}
|
||||
|
||||
private setDisplayPreview(preview: ImageSource | null): void {
|
||||
@@ -2610,6 +2602,22 @@ class DashboardController {
|
||||
*/
|
||||
private previewTrailingTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private lastRecordCaptureAtMs = 0;
|
||||
private phonePreviewVisible: (() => boolean) | null = null;
|
||||
|
||||
/** The main page owns the mirror; other phone pages must not keep it rendering. */
|
||||
attachPhonePreview(isVisible: () => boolean): () => void {
|
||||
this.phonePreviewVisible = isVisible;
|
||||
this.lastConnectedPreviewUpdateAtMs = 0;
|
||||
this.updateCompositePreview();
|
||||
return () => {
|
||||
if (this.phonePreviewVisible !== isVisible) return;
|
||||
this.phonePreviewVisible = null;
|
||||
if (this.previewTrailingTimer) {
|
||||
clearTimeout(this.previewTrailingTimer);
|
||||
this.previewTrailingTimer = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the preview now if the floor allows, otherwise once the floor
|
||||
@@ -2617,6 +2625,7 @@ class DashboardController {
|
||||
* of waiting for the safety-net poll.
|
||||
*/
|
||||
private schedulePreviewUpdate(): void {
|
||||
if (!this.phonePreviewVisible && !this.screenRecordingActive) return;
|
||||
if (this.previewTrailingTimer) return;
|
||||
const wait = this.lastConnectedPreviewUpdateAtMs + CONNECTED_PREVIEW_MIN_UPDATE_MS - Date.now();
|
||||
if (wait <= 0) {
|
||||
@@ -2644,6 +2653,13 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
this.lastConnectedPreviewUpdateAtMs = now;
|
||||
// A user-requested GIF is an independent consumer, including while the
|
||||
// phone is asleep or another page/app is foregrounded.
|
||||
if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) {
|
||||
this.lastRecordCaptureAtMs = now;
|
||||
display.recordScreenFrame();
|
||||
}
|
||||
if (!this.phonePreviewVisible?.()) return;
|
||||
// The connected foreground service intentionally keeps this controller
|
||||
// alive after the phone UI is backgrounded. Do not keep constructing
|
||||
// 640x480 Android Bitmaps for a window that cannot display them: besides
|
||||
@@ -2652,10 +2668,8 @@ class DashboardController {
|
||||
if (global.isAndroid) {
|
||||
const activity = Application.android.foregroundActivity;
|
||||
if (!activity || !activity.hasWindowFocus()) return;
|
||||
}
|
||||
if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) {
|
||||
this.lastRecordCaptureAtMs = now;
|
||||
display.recordScreenFrame();
|
||||
const power = activity.getSystemService(android.content.Context.POWER_SERVICE) as android.os.PowerManager;
|
||||
if (!power?.isInteractive()) return;
|
||||
}
|
||||
const preview = display.getCompositePreview(previewColorSetting.get() === "green");
|
||||
if (preview) {
|
||||
|
||||
@@ -149,7 +149,9 @@ export class GrayImage {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.pixels = new Uint8Array(width * height);
|
||||
this.clear(fill);
|
||||
if (fill !== 0) {
|
||||
this.clear(fill);
|
||||
}
|
||||
}
|
||||
|
||||
get draws(): readonly DeferredDraw[] {
|
||||
|
||||
@@ -137,7 +137,6 @@ export class FaceclawCommunicatorBridge {
|
||||
// does not race against finishes that land before the wait starts.
|
||||
private readonly finishedFrameOutcomes = new Map<number, string>();
|
||||
private readonly frameFinishedWaiters = new Map<number, Set<(outcome: string) => void>>();
|
||||
private readonly logListeners = new Set<(line: string) => void>();
|
||||
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
|
||||
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
|
||||
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
|
||||
@@ -161,9 +160,6 @@ export class FaceclawCommunicatorBridge {
|
||||
addresses.ring ?? "",
|
||||
);
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawBleCommunicatorListener({
|
||||
onLog: (line: string) => {
|
||||
this.emitAsync(this.logListeners, String(line));
|
||||
},
|
||||
onStateChange: (phase: string, status: string) => {
|
||||
const state = {
|
||||
phase: String(phase) as CommunicatorPhase,
|
||||
@@ -293,11 +289,6 @@ export class FaceclawCommunicatorBridge {
|
||||
return result;
|
||||
}
|
||||
|
||||
onLog(listener: (line: string) => void): () => void {
|
||||
this.logListeners.add(listener);
|
||||
return () => this.logListeners.delete(listener);
|
||||
}
|
||||
|
||||
onStateChange(listener: (state: CommunicatorState) => void): () => void {
|
||||
this.stateListeners.add(listener);
|
||||
return () => this.stateListeners.delete(listener);
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { createListenerPool, type InterfaceAddress } from '../remote/listeners';
|
||||
declare const FaceclawRemoteInput: any;
|
||||
export function remoteNative() {
|
||||
export function remoteNative(onRequestReady: () => void) {
|
||||
return createListenerPool(() => {
|
||||
const native = FaceclawRemoteInput.alloc().init();
|
||||
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => native.stop(),
|
||||
native.setRequestListener(onRequestReady);
|
||||
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); },
|
||||
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeResponse(id, response) };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { createListenerPool, type InterfaceAddress } from '../remote/listeners';
|
||||
import { Utils } from '@nativescript/core';
|
||||
declare const com: any;
|
||||
export function remoteNative() {
|
||||
export function remoteNative(onRequestReady: () => void) {
|
||||
return createListenerPool(() => {
|
||||
const native = new com.faceclaw.app.FaceclawRemoteInput();
|
||||
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => native.stop(),
|
||||
const ready = new java.lang.Runnable({ run: onRequestReady });
|
||||
native.setRequestListener(ready);
|
||||
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); },
|
||||
nextRequest: () => native.nextRequest(), complete: (id, response) => native.complete(id, response) };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ type MainPageState = {
|
||||
model: MainViewModel
|
||||
propertyChangeHandler: (args: EventData & { propertyName?: string }) => void
|
||||
orientationHandler: () => void
|
||||
detachPreview: () => void
|
||||
}
|
||||
|
||||
function getPageState(page: Page): MainPageState | undefined {
|
||||
@@ -32,6 +33,7 @@ function cleanupPage(page: Page): void {
|
||||
return
|
||||
}
|
||||
state.model.off(Observable.propertyChangeEvent, state.propertyChangeHandler)
|
||||
state.detachPreview()
|
||||
Application.off(Application.orientationChangedEvent, state.orientationHandler)
|
||||
// navigatingTo builds a fresh model each visit; drop the old one's
|
||||
// controller/settings subscriptions or every navigation leaks a listener.
|
||||
@@ -130,6 +132,8 @@ export function loaded(args: EventData) {
|
||||
|
||||
const state: MainPageState = {
|
||||
model,
|
||||
detachPreview: dashboardController.attachPhonePreview(() =>
|
||||
page.isLoaded && !model.displayPreviewMessage && (!isAndroid || !!page.android?.isShown())),
|
||||
orientationHandler: () => {
|
||||
setTimeout(() => {
|
||||
model.refreshLayoutMetrics()
|
||||
|
||||
+22
-19
@@ -10,17 +10,33 @@ export function remoteInputStatus(): string { return status; }
|
||||
export function remoteInterfaceSelection(): string { return getStringSetting(INTERFACE_STORAGE_KEY, 'tailscale'); }
|
||||
export function selectRemoteInterface(value: string): void { setStringSetting(INTERFACE_STORAGE_KEY, value); }
|
||||
|
||||
/** Owned by the main controller. Native socket work never enters a JS isolate. */
|
||||
/** Owned by the main controller. Native sockets notify JS only for complete requests. */
|
||||
export function startRemoteInput(host: RemoteHost): () => void {
|
||||
const native = remoteNative();
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
const native = remoteNative(() => { void drain(); });
|
||||
let busy = false;
|
||||
let stopped = false;
|
||||
let networkTimer: ReturnType<typeof setInterval> | null = null;
|
||||
// A notification while handling another request is covered by the next drain
|
||||
// iteration. No timer or native bridge calls are needed while sockets are idle.
|
||||
const drain = async () => {
|
||||
if (busy || stopped) return;
|
||||
busy = true;
|
||||
try {
|
||||
while (!stopped) {
|
||||
const raw = native.nextRequest();
|
||||
if (!raw) break;
|
||||
const request = JSON.parse(String(raw));
|
||||
if (Date.now() >= request.expiresAt) continue;
|
||||
let reply;
|
||||
try { reply = await handleRequest(request.body, remoteTokens, host); }
|
||||
catch { reply = { ok: false, error: 'failed', message: 'Request failed.' }; }
|
||||
if (!stopped) native.complete(request.id, JSON.stringify(reply));
|
||||
}
|
||||
} finally { busy = false; }
|
||||
};
|
||||
const sync = () => {
|
||||
if (!remoteTokens.list().length) {
|
||||
native.stop();
|
||||
if (timer !== null) clearInterval(timer);
|
||||
timer = null;
|
||||
if (networkTimer !== null) clearInterval(networkTimer);
|
||||
networkTimer = null;
|
||||
status = 'No tokens; listener stopped.';
|
||||
@@ -34,21 +50,8 @@ export function startRemoteInput(host: RemoteHost): () => void {
|
||||
if (failure) status += `\n${failure}`;
|
||||
// Reconcile after VPN reconnects/address changes; unchanged sockets stay open.
|
||||
if (networkTimer === null) networkTimer = setInterval(sync, 3000);
|
||||
if (timer !== null) return;
|
||||
timer = setInterval(() => {
|
||||
if (busy) return;
|
||||
const raw = native.nextRequest();
|
||||
if (!raw) return;
|
||||
const request = JSON.parse(String(raw));
|
||||
if (Date.now() >= request.expiresAt) return;
|
||||
busy = true;
|
||||
void handleRequest(request.body, remoteTokens, host)
|
||||
.then(reply => native.complete(request.id, JSON.stringify(reply)))
|
||||
.catch(() => native.complete(request.id, JSON.stringify({ ok: false, error: 'failed', message: 'Request failed.' })))
|
||||
.finally(() => { busy = false; });
|
||||
}, 15);
|
||||
};
|
||||
sync();
|
||||
const off = onSettingsStoreChanged(key => { if (key === TOKEN_STORAGE_KEY || key === INTERFACE_STORAGE_KEY) sync(); });
|
||||
return () => { off(); if (timer !== null) clearInterval(timer); if (networkTimer !== null) clearInterval(networkTimer); native.stop(); };
|
||||
return () => { stopped = true; off(); if (networkTimer !== null) clearInterval(networkTimer); native.stop(); };
|
||||
}
|
||||
Reference in new issue
Block a user