Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[canvaskit] Disable createImageBitmap support on Chrome 110 or older on Windows. #48475

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions lib/web_ui/lib/src/engine/browser_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ BrowserEngine detectBrowserEngineByVendorAgent(String vendor, String agent) {
}

// Assume Blink otherwise, but issue a warning.
print('WARNING: failed to detect current browser engine. Assuming this is a Chromium-compatible browser.');
print(
'WARNING: failed to detect current browser engine. Assuming this is a Chromium-compatible browser.');
return BrowserEngine.blink;
}

Expand Down Expand Up @@ -141,8 +142,9 @@ OperatingSystem detectOperatingSystem({
if (platform.startsWith('Mac')) {
// iDevices requesting a "desktop site" spoof their UA so it looks like a Mac.
// This checks if we're in a touch device, or on a real mac.
final int maxTouchPoints =
overrideMaxTouchPoints ?? domWindow.navigator.maxTouchPoints?.toInt() ?? 0;
final int maxTouchPoints = overrideMaxTouchPoints ??
domWindow.navigator.maxTouchPoints?.toInt() ??
0;
if (maxTouchPoints > 2) {
return OperatingSystem.iOs;
}
Expand Down Expand Up @@ -204,6 +206,36 @@ bool get isIOS15 {
domWindow.navigator.userAgent.contains('OS 15_');
}

/// Detect if running on Chrome version 110 or older on Windows.
///
/// These versions of Chrome have a bug on Windows which causes
/// rendering to be flipped upside down.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe leave a TODO pointing to a Github issue to clean this up later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// TODO(harryterkelsen): Remove this check once we stop supporting Chrome 110
// and earlier, https://github.com/flutter/flutter/issues/139186.
bool get isChrome110OrOlderOnWindows {
if (debugIsChrome110OrOlderOnWindows != null) {
return debugIsChrome110OrOlderOnWindows!;
}
if (_cachedIsChrome110OrOlderOnWindows != null) {
return _cachedIsChrome110OrOlderOnWindows!;
}
if (operatingSystem != OperatingSystem.windows) {
return _cachedIsChrome110OrOlderOnWindows = false;
}
final RegExp chromeRegexp = RegExp(r'Chrom(e|ium)\/([0-9]+)\.');
final RegExpMatch? match =
chromeRegexp.firstMatch(domWindow.navigator.userAgent);
if (match != null) {
final int chromeVersion = int.parse(match.group(2)!);
return _cachedIsChrome110OrOlderOnWindows = chromeVersion <= 110;
}
return _cachedIsChrome110OrOlderOnWindows = false;
}

// Cache the result of checking if the app is running on Chrome 110 on Windows
// since we check this on every frame.
bool? _cachedIsChrome110OrOlderOnWindows;

/// If set to true pretends that the current browser is iOS Safari.
///
/// Useful for tests. Do not use in production code.
Expand Down Expand Up @@ -234,6 +266,9 @@ bool get isWasm => const bool.fromEnvironment('dart.library.ffi');
/// Use in tests to simulate the detection of iOS 15.
bool? debugIsIOS15;

/// Use in tests to simulated the detection of Chrome 110 or older on Windows.
bool? debugIsChrome110OrOlderOnWindows;

int? _cachedWebGLVersion;

/// The highest WebGL version supported by the current browser, or -1 if WebGL
Expand Down
6 changes: 4 additions & 2 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3672,8 +3672,10 @@ external JSAny? get _createImageBitmapFunction;
/// Set to `true` to disable `createImageBitmap` support. Used in tests.
bool debugDisableCreateImageBitmapSupport = false;

bool browserSupportsCreateImageBitmap =
!debugDisableCreateImageBitmapSupport || _createImageBitmapFunction != null;
bool get browserSupportsCreateImageBitmap =>
_createImageBitmapFunction != null &&
!isChrome110OrOlderOnWindows &&
!debugDisableCreateImageBitmapSupport;

@JS()
@staticInterop
Expand Down
15 changes: 14 additions & 1 deletion lib/web_ui/test/canvaskit/no_create_image_bitmap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ void testMain() {
setUpCanvasKitTest();
setUp(() async {
EngineFlutterDisplay.instance.debugOverrideDevicePixelRatio(1.0);
debugDisableCreateImageBitmapSupport = true;
});

tearDown(() {
debugDisableCreateImageBitmapSupport = false;
debugIsChrome110OrOlderOnWindows = null;
});

test('can render without createImageBitmap', () async {
debugDisableCreateImageBitmapSupport = true;

expect(browserSupportsCreateImageBitmap, isFalse);

final CkPictureRecorder recorder = CkPictureRecorder();
final CkCanvas canvas = recorder.beginRecording(region);

Expand Down Expand Up @@ -61,5 +65,14 @@ void testMain() {
region: region,
);
});

test(
'createImageBitmap support is disabled on '
'Windows on Chrome version 110 or older', () async {
debugIsChrome110OrOlderOnWindows = true;
debugDisableCreateImageBitmapSupport = false;

expect(browserSupportsCreateImageBitmap, isFalse);
});
});
}