iOS porting: Image-sending performance: Pipelining, bounding-box updates

This commit is contained in:
James Babcock committed 2026-09-08 13:18:50 -07:00
1 parent 16764359bd
commit 79b6c276f8
15 files changed
+592 -57

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
import type { BleTrafficSample } from '../native/ble-traffic'
/** Shared Android/iOS five-second traffic readout. Poll once per second. */
export class BleBandwidthMeter {
private history: Array<{ atMs: number; bytes: number; frames: number }> = []
reset(): void { this.history = [] }
sample(sample: BleTrafficSample, atMs: number): string {
const previous = this.history[this.history.length - 1]
if (previous && (atMs <= previous.atMs || sample.bytes < previous.bytes || sample.frames < previous.frames)) this.reset()
this.history.push({ atMs, bytes: sample.bytes, frames: sample.frames })
while (this.history.length && this.history[0]!.atMs < atMs - 5000) this.history.shift()
let label = `BLE sent: ${formatCount(sample.messages)} messages, ${formatCount(sample.bytes)} bytes`
const oldest = this.history[0]!, elapsedSec = (atMs - oldest.atMs) / 1000
if (elapsedSec > 0) {
const byteDelta = sample.bytes - oldest.bytes, frameDelta = sample.frames - oldest.frames
label += ` · ${formatByteRate(byteDelta / elapsedSec)}, ${(frameDelta / elapsedSec).toFixed(1)} fps`
if (frameDelta > 0) label += `, ${formatCount(byteDelta / frameDelta)} B/frame`
}
return label
}
}
function formatCount(value: number): string { return String(Math.round(value)).replace(/\B(?=(\d{3})+(?!\d))/g, ',') }
function formatByteRate(bytesPerSec: number): string {
if (bytesPerSec >= 1e6) return (bytesPerSec / 1e6).toFixed(2) + ' MB/s'
if (bytesPerSec >= 1e3) return (bytesPerSec / 1e3).toFixed(1) + ' kB/s'
return Math.round(bytesPerSec) + ' B/s'
}
+35 -8
View File
@@ -3,7 +3,9 @@ import { createConfigureDevicesPage } from './config-page.ios'
import { ALL_APPS } from '../apps/all-apps'
import { IosPreviewController } from '../g2/ios-preview-controller'
import { PhoneGestureRecognizer } from './phone-gestures'
import { onAnySettingChanged, previewColorSetting } from '../ui/dashboard-settings'
import { BleBandwidthMeter } from './ble-bandwidth-meter'
import { sampleBleTraffic } from '../native/ble-traffic'
import { onAnySettingChanged, previewColorSetting, showBleBandwidthSetting } from '../ui/dashboard-settings'
import { loadDeviceAddresses } from '../g2/device-addresses'
import { deviceAddressError } from '../g2/ios-peripheral-identity'
@@ -11,7 +13,7 @@ export function createMainPage(): Page {
const page = new Page()
page.actionBarHidden = true
const root = new GridLayout()
root.rows = 'auto,*'
root.rows = 'auto,*,auto'
page.content = root
const header = new GridLayout()
header.columns = '*,auto'; header.padding = '12 20'
@@ -127,20 +129,44 @@ export function createMainPage(): Page {
view.padding = '16 8'; view.fontSize = 14
GridLayout.setColumn(view, index); footer.addChild(view)
}
const bandwidth = new Label()
bandwidth.accessibilityIdentifier = 'ble-bandwidth-indicator'
bandwidth.fontSize = 10; bandwidth.textWrap = true; bandwidth.padding = '2 8 4'
bandwidth.color = new Color('#68756c'); bandwidth.isUserInteractionEnabled = false
bandwidth.visibility = 'collapse'
GridLayout.setRow(bandwidth, 2); root.addChild(bandwidth)
const bandwidthMeter = new BleBandwidthMeter()
let bandwidthTimer: ReturnType<typeof setInterval> | null = null
let foreground = false
function stopBandwidth(): void {
if (bandwidthTimer !== null) clearInterval(bandwidthTimer)
bandwidthTimer = null; bandwidthMeter.reset()
}
function syncBandwidth(): void {
if (!foreground) { stopBandwidth(); return }
const enabled = showBleBandwidthSetting.get()
bandwidth.visibility = enabled ? 'visible' : 'collapse'
if (!enabled) { stopBandwidth(); return }
if (bandwidthTimer !== null) return
const refresh = () => { bandwidth.text = bandwidthMeter.sample(sampleBleTraffic(), Date.now()) }
refresh(); bandwidthTimer = setInterval(refresh, 1000)
}
let lastLayout = ''
function layout(): void {
const size = root.getActualSize()
if (size.width <= 0 || size.height <= 0) return
const key = `${size.width}:${size.height}:${selectedTab}`
const metricsHeight = bandwidth.visibility === 'visible' ? bandwidth.getActualSize().height : 0
const availableHeight = size.height - metricsHeight
const key = `${size.width}:${availableHeight}:${selectedTab}`
if (key === lastLayout) return
lastLayout = key; padInput.cancel(); mirrorInput.cancel()
const landscape = size.width > size.height
body.rows = landscape ? '*' : 'auto,*'; body.columns = landscape ? '*,320' : '*'
GridLayout.setRow(controls, landscape ? 0 : 1); GridLayout.setColumn(controls, landscape ? 1 : 0)
const width = landscape ? Math.min(size.width - 320, (size.height - 64) * 4 / 3) : size.width
const width = landscape ? Math.min(size.width - 320, (availableHeight - 64) * 4 / 3) : size.width
mirror.width = width; mirror.height = width * 3 / 4
pad.width = selectedTab === 'ring' ? 150 : Math.min(360, (landscape ? 320 : size.width) - 32)
pad.height = Math.max(120, Math.min(250, size.height - (landscape ? 180 : width * 3 / 4 + 200)))
pad.height = Math.max(120, Math.min(250, availableHeight - (landscape ? 180 : width * 3 / 4 + 200)))
}
// iOS emits layoutChanged inside its layout pass. Defer mutations so
// NativeScript does not clear our new layout request at the end of that pass.
@@ -150,17 +176,18 @@ export function createMainPage(): Page {
layoutTimer = setTimeout(() => { layoutTimer = null; layout() }, 0)
})
const pause = () => {
foreground = false; stopBandwidth()
if (layoutTimer !== null) clearTimeout(layoutTimer)
layoutTimer = null
padInput.cancel(); mirrorInput.cancel(); controller.pause()
}
const resume = () => controller.resume()
const resume = () => { foreground = true; controller.resume(); syncBandwidth() }
let offSettings: (() => void) | null = null
let firstLoad = true
page.on('loaded', () => {
Application.on(Application.suspendEvent, pause); Application.on(Application.resumeEvent, resume)
offSettings?.(); offSettings = onAnySettingChanged(() => { sizeButton.text = controller.displayModeLabel })
selectTab(selectedTab); controller.resume()
offSettings?.(); offSettings = onAnySettingChanged(() => { sizeButton.text = controller.displayModeLabel; syncBandwidth() })
selectTab(selectedTab); resume()
if (firstLoad) {
firstLoad = false
// Match Android's reconnect-on-launch behavior once addresses are saved.
+4 -33
View File
@@ -1,3 +1,4 @@
import { BleBandwidthMeter } from "./ble-bandwidth-meter";
import {
Application,
Dialogs,
@@ -1268,7 +1269,7 @@ export class MainViewModel extends Observable {
private _bleBandwidthLabel = "";
private bleBandwidthTimer: ReturnType<typeof setInterval> | null = null;
// Recent counter samples, one per poll tick, for the windowed rates.
private bleRateHistory: Array<{ atMs: number; bytes: number; frames: number }> = [];
private readonly bleBandwidthMeter = new BleBandwidthMeter();
get bleBandwidthVisibility(): "visible" | "collapse" {
return showBleBandwidthSetting.get() ? "visible" : "collapse";
@@ -1297,29 +1298,13 @@ export class MainViewModel extends Observable {
this.bleBandwidthTimer = null;
}
// Don't let a later re-enable compute a rate across the disabled gap.
this.bleRateHistory = [];
this.bleBandwidthMeter.reset();
}
private refreshBleBandwidth(): void {
let label: string;
try {
const sample = sampleBleTraffic();
const atMs = Date.now();
this.bleRateHistory.push({ atMs, bytes: sample.bytes, frames: sample.frames });
while (this.bleRateHistory.length > 0 && this.bleRateHistory[0]!.atMs < atMs - BLE_RATE_WINDOW_MS) {
this.bleRateHistory.shift();
}
label = `BLE sent: ${formatCount(sample.messages)} messages, ${formatCount(sample.bytes)} bytes`;
const oldest = this.bleRateHistory[0]!;
const elapsedSec = (atMs - oldest.atMs) / 1000;
if (elapsedSec > 0) {
const byteDelta = sample.bytes - oldest.bytes;
const frameDelta = sample.frames - oldest.frames;
label += ` · ${formatByteRate(byteDelta / elapsedSec)}, ${(frameDelta / elapsedSec).toFixed(1)} fps`;
if (frameDelta > 0) {
label += `, ${formatCount(byteDelta / frameDelta)} B/frame`;
}
}
label = this.bleBandwidthMeter.sample(sampleBleTraffic(), Date.now());
} catch (error) {
label = `BLE sent: ${this.formatError(error)}`;
}
@@ -1342,20 +1327,6 @@ export class MainViewModel extends Observable {
}
}
/** 1234567 -> "1,234,567"; kept exact rather than rounded so growth is visible at a glance. */
function formatCount(value: number): string {
return String(Math.round(value)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/** How far back the BLE bandwidth indicator's rates look. */
const BLE_RATE_WINDOW_MS = 5000;
function formatByteRate(bytesPerSec: number): string {
if (bytesPerSec >= 1e6) return (bytesPerSec / 1e6).toFixed(2) + " MB/s";
if (bytesPerSec >= 1e3) return (bytesPerSec / 1e3).toFixed(1) + " kB/s";
return Math.round(bytesPerSec) + " B/s";
}
/** NativeScript swipe direction -> the watch-scheme directional gesture. */
function swipeKind(direction: SwipeDirection): "swipe-up" | "swipe-down" | "swipe-left" | "swipe-right" {
switch (direction) {