Skip to content

Commit

Permalink
Don't use dirty rect putImageData option on platforms where it's buggy.
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
mihaip committed Mar 4, 2023
1 parent a807196 commit 436eed6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/canUseCanvasDirtyRect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Chrome on Windows, Android and Chrome OS appears to be buggy when using the
* dirty rect option of putImageData. This function returns true if we can use
* it.
* https://github.com/mihaip/infinite-mac/issues/95
*/
export function canUseCanvasDirtyRect(): boolean {
if (cached !== undefined) {
return cached;
}

cached = check();
return cached;
}

function check(): boolean {
// Ideally we'd test for this bug via putImageData and getImageData, but
// the read back pixels are OK (at least in simple test cases).
const {userAgent} = navigator;
if (!userAgent.includes("Chrome")) {
return true;
}
return (
!userAgent.includes("Android") &&
!userAgent.includes("CrOS") &&
!userAgent.includes("Windows")
);
}

let cached: boolean | undefined;
3 changes: 2 additions & 1 deletion src/emulator/emulator-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
SharedMemoryEmulatorClipboard,
} from "./emulator-ui-clipboard";
import type {MachineDef} from "../machines";
import {canUseCanvasDirtyRect} from "../canUseCanvasDirtyRect";

export type EmulatorConfig = {
machine: MachineDef;
Expand Down Expand Up @@ -652,7 +653,7 @@ export class Emulator {
}
this.#screenImageData.data.set(imageData);
const dirtyRect = this.#video.consumeBlitRect();
if (dirtyRect) {
if (dirtyRect && canUseCanvasDirtyRect()) {
this.#screenCanvasContext.putImageData(
this.#screenImageData,
0,
Expand Down

0 comments on commit 436eed6

Please sign in to comment.