Optimize touch control's canvas, use low-power profile and disable antialiasing

This commit is contained in:
redphx 2024-05-07 17:07:15 +07:00
parent d1b99705e6
commit b9355d5c01
3 changed files with 28 additions and 2 deletions

View File

@ -23,7 +23,7 @@ import { RemotePlay } from "@modules/remote-play";
import { onHistoryChanged, patchHistoryMethod } from "@utils/history";
import { VibrationManager } from "@modules/vibration-manager";
import { PreloadedState } from "@utils/titles-info";
import { patchAudioContext, patchMeControl, patchRtcCodecs, patchRtcPeerConnection, patchVideoApi } from "@utils/monkey-patches";
import { patchAudioContext, patchCanvasContext, patchMeControl, patchRtcCodecs, patchRtcPeerConnection, patchVideoApi } from "@utils/monkey-patches";
import { STATES } from "@utils/global";
import { injectStreamMenuButtons } from "@modules/stream/stream-ui";
import { BxLogger } from "@utils/bx-logger";
@ -215,6 +215,7 @@ function main() {
patchRtcCodecs();
interceptHttpRequests();
patchVideoApi();
patchCanvasContext();
getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && patchAudioContext();
getPref(PrefKey.BLOCK_TRACKING) && patchMeControl();

View File

@ -9,7 +9,10 @@ export function takeScreenshot(callback: any) {
return;
}
const $canvasContext = $canvas.getContext('2d')!;
const $canvasContext = $canvas.getContext('2d', {
alpha: false,
willReadFrequently: false,
})!;
$canvasContext.drawImage($video, 0, 0, $canvas.width, $canvas.height);

View File

@ -180,3 +180,25 @@ export function patchMeControl() {
(window as any).MSA = new Proxy(MSA, MsaHandler);
(window as any).MeControl = new Proxy(MeControl, MeControlHandler);
}
/**
* Use power-saving flags for touch control
*/
export function patchCanvasContext() {
const nativeGetContext = HTMLCanvasElement.prototype.getContext;
// @ts-ignore
HTMLCanvasElement.prototype.getContext = function(contextType: string, contextAttributes?: any) {
if (contextType.includes('webgl')) {
contextAttributes = contextAttributes || {};
contextAttributes.antialias = false;
// Use low-power profile for touch controller
if (contextAttributes.powerPreference === 'high-performance') {
contextAttributes.powerPreference = 'low-power';
}
}
return nativeGetContext.apply(this, [contextType, contextAttributes]);
}
}