From 662047e6fa656793f14bebc51fb82d11db1069bb Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:21:55 +0000 Subject: [PATCH] perf(mobile): keep three visited terminals mounted and release hidden WebGL --- .../session/MobileSessionActiveContent.tsx | 16 ++++++--- mobile/src/terminal/TerminalWebView.tsx | 2 +- mobile/src/terminal/TerminalWebView.web.tsx | 2 +- .../src/terminal/document/document-scope.ts | 3 ++ .../terminal/document/host-message-router.ts | 6 ++++ mobile/src/terminal/document/terminal-init.ts | 4 ++- .../src/terminal/document/webgl-recovery.ts | 22 ++++++++++++ .../src/terminal/session/TerminalPaneView.tsx | 16 ++++----- .../session/mounted-terminal-lru.test.ts | 22 ++++++++++++ .../terminal/session/mounted-terminal-lru.ts | 36 +++++++++++++++++++ .../use-mobile-session-terminal-webview.ts | 14 ++++++++ .../terminal-webview-cell-box.test.ts | 2 +- .../src/terminal/terminal-webview-contract.ts | 4 +-- .../terminal/terminal-webview-engine.test.ts | 17 ++++++++- .../src/terminal/terminal-webview-messages.ts | 1 + .../use-terminal-webview-controller.ts | 8 ++++- 16 files changed, 153 insertions(+), 22 deletions(-) create mode 100644 mobile/src/terminal/session/mounted-terminal-lru.test.ts create mode 100644 mobile/src/terminal/session/mounted-terminal-lru.ts diff --git a/mobile/src/session/MobileSessionActiveContent.tsx b/mobile/src/session/MobileSessionActiveContent.tsx index 33865e3ed3f..fe8e8038017 100644 --- a/mobile/src/session/MobileSessionActiveContent.tsx +++ b/mobile/src/session/MobileSessionActiveContent.tsx @@ -8,6 +8,7 @@ import { Animated, View, Text, Pressable, ActivityIndicator } from 'react-native import { saveTerminalTextScale } from '../storage/preferences' import { MobileBrowserPane } from '../browser/MobileBrowserPane' import { TerminalPaneView } from '../terminal/session/TerminalPaneView' +import { useMountedTerminalHandles } from '../terminal/session/mounted-terminal-lru' import { MobileNativeChatOverlay } from './native-chat/MobileNativeChatOverlay' import { mobileSessionNativeChatHostOutage } from './native-chat/mobile-native-chat-host-outage' import { MobileAgentMessageProvider } from './native-chat/transcript/MobileAgentMessageProvider' @@ -87,6 +88,7 @@ export function MobileSessionActiveContent({ discardMarkdownLocalContent, saveMarkdownTab, setTerminalWebViewRef, + releaseTerminalDocument, handleTerminalWebReady, notifyTerminalFrame, notifyTerminalCellBoxChange, @@ -114,6 +116,13 @@ export function MobileSessionActiveContent({ showEmptyState, createTabBusy } = controller + const mountedTerminalHandles = useMountedTerminalHandles( + visibleTerminalHandles, + terminals.map((terminal) => terminal.handle) + ) + const mountedTerminals = terminals.filter((terminal) => + mountedTerminalHandles.includes(terminal.handle) + ) const { keyboardLift, activeTerminalKeyboardLift, toastAnimatedStyle } = useMobileSessionContentKeyboardLift(controller) const openUrlInOrca = useOpenUrlInOrca(controller) @@ -258,14 +267,12 @@ export function MobileSessionActiveContent({ ) : ( - {terminals.map((terminal) => ( + {mountedTerminals.map((terminal) => ( ( useImperativeHandle(ref, () => handle, [handle]) // Seed scale in page source before document startup; Android injected scripts may run too late. const [source] = useState(() => - xtermWebViewSource({ textScale: props.textScale ?? 1, shown: props.shownAtMount ?? true }) + xtermWebViewSource({ textScale: props.textScale ?? 1, shown: props.shown ?? true }) ) const handleMessage = useCallback( diff --git a/mobile/src/terminal/TerminalWebView.web.tsx b/mobile/src/terminal/TerminalWebView.web.tsx index 3c6db3d6f7c..4e7e8f91f3a 100644 --- a/mobile/src/terminal/TerminalWebView.web.tsx +++ b/mobile/src/terminal/TerminalWebView.web.tsx @@ -54,7 +54,7 @@ export const TerminalWebView = forwardRef( // Seed shown/scale before document startup, matching native pre-content state. const [atMount] = useState(() => ({ textScale: props.textScale ?? 1, - shown: props.shownAtMount ?? true + shown: props.shown ?? true })) useImperativeHandle(ref, () => handle, [handle]) diff --git a/mobile/src/terminal/document/document-scope.ts b/mobile/src/terminal/document/document-scope.ts index 84fe2b78ec0..ded6e943d85 100644 --- a/mobile/src/terminal/document/document-scope.ts +++ b/mobile/src/terminal/document/document-scope.ts @@ -41,6 +41,8 @@ export type TerminalDocumentState = { terminalGeneration: number termObserverDisposables: TerminalDocumentDisposable[] initRows: number + // Hidden documents hold no WebGL context: WebKit caps live contexts per page and drops the oldest. + shown: boolean webglAddon: TerminalDocumentWebglAddon | null webglRecoveryTimer: ReturnType | null terminalThemeInput: TerminalDocumentThemeMessage @@ -162,6 +164,7 @@ function createTerminalDocumentState(): TerminalDocumentState { terminalGeneration: 0, termObserverDisposables: [], initRows: 24, + shown: true, webglAddon: null, webglRecoveryTimer: null, terminalThemeInput: null, diff --git a/mobile/src/terminal/document/host-message-router.ts b/mobile/src/terminal/document/host-message-router.ts index 380b2f5f427..8606d6cb5d0 100644 --- a/mobile/src/terminal/document/host-message-router.ts +++ b/mobile/src/terminal/document/host-message-router.ts @@ -12,10 +12,12 @@ import { resetEvictionCounter } from './selection-state-and-eviction' import { applyTerminalTheme } from './terminal-theme' import { init, resize, write } from './terminal-init' import { applyTextScale } from './text-scaling' +import { setWebglShown } from './webgl-recovery' import { afterQueuedWrites, resetWriteQueue } from './write-queue' export type TerminalHostMessage = { active?: boolean + shown?: boolean id?: number type?: string cols?: number @@ -134,6 +136,10 @@ export function handleMsg(scope: TerminalDocumentScope, msg: TerminalHostMessage suspendSurfaceTouchGestures(scope) } } + } else if (msg.type === 'set-shown') { + if (typeof msg.shown === 'boolean') { + setWebglShown(scope, msg.shown) + } } else if (msg.type === 'set-theme') { applyTerminalTheme(scope, msg.terminalTheme) } else if (msg.type === 'cancel-select') { diff --git a/mobile/src/terminal/document/terminal-init.ts b/mobile/src/terminal/document/terminal-init.ts index 8a0e5f0b691..25c30c70adf 100644 --- a/mobile/src/terminal/document/terminal-init.ts +++ b/mobile/src/terminal/document/terminal-init.ts @@ -57,7 +57,9 @@ function openTerminal(scope: TerminalDocumentScope, cols: number, rows: number) attachOsc52ClipboardBridge(scope, term) attachProgramStatusQueryReplies(scope, term) term.open(scope.surface!) - attachWebglAddon(scope, true) + if (scope.shown) { + attachWebglAddon(scope, true) + } try { const unicodeAddon = scope.createUnicode11Addon() if (unicodeAddon) { diff --git a/mobile/src/terminal/document/webgl-recovery.ts b/mobile/src/terminal/document/webgl-recovery.ts index c5b73a08bac..e893a67071d 100644 --- a/mobile/src/terminal/document/webgl-recovery.ts +++ b/mobile/src/terminal/document/webgl-recovery.ts @@ -97,7 +97,29 @@ function onDocumentVisibilityChange(scope: TerminalDocumentScope) { refreshTerminalSurface(scope) } +// Release the context while the host hides this document and take a fresh one on show; xterm draws with its DOM renderer in between. +export function setWebglShown(scope: TerminalDocumentScope, shown: boolean) { + if (scope.shown === shown) { + return + } + scope.shown = shown + if (shown) { + if (!scope.webglAddon) { + attachWebglAddon(scope, true) + } + return + } + cancelWebglContextRecovery(scope) + const addon = scope.webglAddon + // Clear first so the context loss this dispose raises is not taken for a GPU failure. + scope.webglAddon = null + try { + addon?.dispose() + } catch {} +} + export function startWebglRecovery(scope: TerminalDocumentScope) { + scope.shown = scope.start().shown const onVisibilityChange = () => onDocumentVisibilityChange(scope) document.addEventListener('visibilitychange', onVisibilityChange) scope.removeWebglRecovery = () => { diff --git a/mobile/src/terminal/session/TerminalPaneView.tsx b/mobile/src/terminal/session/TerminalPaneView.tsx index a063818cc14..f7b7d5a1c78 100644 --- a/mobile/src/terminal/session/TerminalPaneView.tsx +++ b/mobile/src/terminal/session/TerminalPaneView.tsx @@ -31,6 +31,8 @@ type TerminalPaneViewProps = { terminalTheme?: MobileTerminalTheme textScale: number onRef: (handle: string, ref: TerminalWebViewHandle | null) => void + // Must keep one identity: the pane reports its document gone when it unmounts. + onDocumentReleased: (handle: string) => void onWebReady: (handle: string) => void onSelectionMode: (handle: string, active: boolean) => void onSelectionCopy: (handle: string, text: string) => void @@ -63,6 +65,7 @@ export function TerminalPaneView({ terminalTheme, textScale, onRef, + onDocumentReleased, onWebReady, onSelectionMode, onSelectionCopy, @@ -82,10 +85,7 @@ export function TerminalPaneView({ }: TerminalPaneViewProps) { useMobileColorScheme() const t = useMobileTranslation() - const [visited, setVisited] = useState(visible) - if (visible && !visited) { - setVisited(true) - } + useEffect(() => () => onDocumentReleased(handle), [handle, onDocumentReleased]) const [routeFocused, setRouteFocused] = useState(false) useFocusEffect( useCallback(() => { @@ -113,13 +113,9 @@ export function TerminalPaneView({ ? terminalPaneKeyboardLayout(splitFrame, splitDirection, splitIndex, keyboardInset) : null - if (!visible && !visited) { - return null - } - return (