Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 4x 4x 4x 7x 6x 4x 10x 7x | import { setDisabled, setButtonTitle } from '../../pane-controls.ts';
// The Options dialog's render resolution auto-detect: the first-run benchmark of the select screen and the "Auto-detect
// resolution" button (`benchmarkRenderResolution`). They run with the dialog as `this` (`OptionsDialog`).
import { logger } from '@be-music/player-browser';
import {
benchmarkRenderResolution,
RenderResolutionBenchmarkCancelled,
readStoredRenderResolution,
renderResolutionLabel,
renderResolutionSteps,
storeRenderResolution,
} from '../../render-resolution.ts';
import type { OptionsDialog } from './index.ts';
const resolutionLog = logger('render-resolution');
export const AUTO_DETECT_RESOLUTION_LABEL = 'Use the best resolution';
/** How long the select screen settles (song images, fonts, first BGM) before the first-run resolution benchmark. */
const FIRST_RUN_RESOLUTION_DELAY_MS = 2000;
/** What the benchmark asks of the select screen it measures. */
export interface MeasuredSelectScreen {
/** Whether it is on the stage (between its enter and exit). */
readonly entered: boolean;
}
/**
* Whether this visit benchmarks the render resolution on its own: only before the player has picked one (by hand or by
* an earlier benchmark, `stored`), not when `?resolution=` in `search` pins it, and not where there's nothing to choose
* (a display at the lowest step).
*/
export function wantsFirstRunResolutionDetect(visit: {
readonly stored: number | undefined;
readonly search: string;
readonly devicePixelRatio: number;
}): boolean {
if (visit.stored !== undefined) return false;
if (new URLSearchParams(visit.search).has('resolution')) return false;
return renderResolutionSteps(visit.devicePixelRatio || 1).length >= 2;
}
/** Whether `measured` is still the select screen showing: the select screen now, and on the stage. */
export function isStillShowing(measured: unknown, select: MeasuredSelectScreen | undefined): boolean {
return select !== undefined && select === measured && select.entered;
}
/**
* Whether a benchmark run stops: when the page is hidden (a hidden page's frames are throttled), and, for a run that
* measures a select screen (`whileShowing`), once that screen is no longer the one showing.
*/
export function isResolutionDetectCancelled(state: {
readonly visible: boolean;
readonly whileShowing?: unknown;
readonly select: MeasuredSelectScreen | undefined;
}): boolean {
return !state.visible || (state.whileShowing !== undefined && !isStillShowing(state.whileShowing, state.select));
}
/**
* Before the player has picked a render resolution (by hand or by an earlier benchmark), benchmarks the select screen
* once, after it settles, to pick one for this device. Skipped where there's nothing to choose (a display at the lowest step) and
* when `?resolution=` pins it.
*/
export function scheduleFirstRunResolutionDetect(this: OptionsDialog): void {
if (this.firstRunResolutionScheduled) return;
this.firstRunResolutionScheduled = true;
const visit = {
stored: readStoredRenderResolution(),
search: window.location.search,
devicePixelRatio: window.devicePixelRatio,
};
if (!wantsFirstRunResolutionDetect(visit)) return;
const selectView = this.selectView;
window.setTimeout(() => {
// Gone on to a play already: measuring gameplay that just started would mislead, so wait for the next visit.
if (!isStillShowing(selectView, this.selectView) || readStoredRenderResolution() !== undefined) {
return;
}
void this.detectRenderResolution({ whileShowing: selectView });
}, FIRST_RUN_RESOLUTION_DELAY_MS);
}
/**
* Measures the frame rate at each resolution on the screen being shown and keeps (and stores) the highest that holds
* it. With `whileShowing`, the run is dropped — nothing stored, the previous resolution back — once that view is no
* longer the select screen; any run is dropped when the page is hidden, since a hidden page's frames are throttled.
*/
export async function detectRenderResolution(
this: OptionsDialog,
options: {
whileShowing?: unknown;
} = {},
): Promise<void> {
const steps = renderResolutionSteps(window.devicePixelRatio || 1);
if (this.detectingResolution || steps.length < 2) return;
this.detectingResolution = true;
const control = this.autoDetectResolutionControl;
setDisabled(control, true);
setDisabled(this.resolutionPicker, true);
try {
const result = await benchmarkRenderResolution({
steps,
apply: (value) => this.sceneHost.setResolution(value),
nextFrame: () => new Promise((resolve) => requestAnimationFrame(resolve)),
onStep: (value) => setButtonTitle(control, `Measuring ${renderResolutionLabel(value)}…`),
cancelled: () =>
isResolutionDetectCancelled({
visible: document.visibilityState === 'visible',
whileShowing: options.whileShowing,
select: this.selectView,
}),
});
this.guiState.renderResolution = result.chosen;
storeRenderResolution(result.chosen);
this.scheduleSettingsSave();
this.resolutionPicker?.refresh();
resolutionLog.info('picked', {
chosen: result.chosen,
baselineMs: result.baseline,
results: result.results.map(
({ resolution, times }) => `${resolution}×: p50 ${times.p50.toFixed(1)} ms, p90 ${times.p90.toFixed(1)} ms`,
),
});
setButtonTitle(control, `${AUTO_DETECT_RESOLUTION_LABEL} (picked ${renderResolutionLabel(result.chosen)})`);
} catch (error) {
if (!(error instanceof RenderResolutionBenchmarkCancelled)) resolutionLog.warn('auto-detect failed', error);
this.sceneHost.setResolution(this.guiState.renderResolution);
setButtonTitle(control, AUTO_DETECT_RESOLUTION_LABEL);
} finally {
this.detectingResolution = false;
setDisabled(control, false);
setDisabled(this.resolutionPicker, false);
}
}
|