-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recover when browser throws on ImageElement.decode due to too many images #15160
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
// found in the LICENSE file. | ||
|
||
part of engine; | ||
|
||
final bool _supportsDecode = js_util.getProperty( | ||
js_util.getProperty( | ||
js_util.getProperty(html.window, 'Image'), 'prototype'), | ||
|
@@ -23,46 +22,54 @@ class HtmlCodec implements ui.Codec { | |
|
||
@override | ||
Future<ui.FrameInfo> getNextFrame() async { | ||
StreamSubscription<html.Event> loadSubscription; | ||
StreamSubscription<html.Event> errorSubscription; | ||
final Completer<ui.FrameInfo> completer = Completer<ui.FrameInfo>(); | ||
final html.ImageElement imgElement = html.ImageElement(); | ||
// If the browser doesn't support asynchronous decoding of an image, | ||
// then use the `onload` event to decide when it's ready to paint to the | ||
// DOM. Unfortunately, this will case the image to be decoded synchronously | ||
// on the main thread, and may cause dropped framed. | ||
if (!_supportsDecode) { | ||
loadSubscription = imgElement.onLoad.listen((html.Event event) { | ||
loadSubscription.cancel(); | ||
errorSubscription.cancel(); | ||
if (_supportsDecode) { | ||
final html.ImageElement imgElement = html.ImageElement(); | ||
imgElement.src = src; | ||
js_util.setProperty(imgElement, 'decoding', 'async'); | ||
imgElement.decode().then((dynamic _) { | ||
final HtmlImage image = HtmlImage( | ||
imgElement, | ||
imgElement.naturalWidth, | ||
imgElement.naturalHeight, | ||
); | ||
completer.complete(SingleFrameInfo(image)); | ||
}).catchError((e) { | ||
// This code path is hit on Chrome 80.0.3987.16 when too many | ||
// images are on the page (~1000). | ||
// Fallback here is to load using onLoad instead. | ||
_decodeUsingOnLoad(completer); | ||
}); | ||
} else { | ||
_decodeUsingOnLoad(completer); | ||
} | ||
return completer.future; | ||
} | ||
|
||
void _decodeUsingOnLoad(Completer completer) { | ||
StreamSubscription<html.Event> loadSubscription; | ||
StreamSubscription<html.Event> errorSubscription; | ||
final html.ImageElement imgElement = html.ImageElement(); | ||
// If the browser doesn't support asynchronous decoding of an image, | ||
// then use the `onload` event to decide when it's ready to paint to the | ||
// DOM. Unfortunately, this will case the image to be decoded synchronously | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: case => cause There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
// on the main thread, and may cause dropped framed. | ||
errorSubscription = imgElement.onError.listen((html.Event event) { | ||
loadSubscription?.cancel(); | ||
errorSubscription.cancel(); | ||
completer.completeError(event); | ||
}); | ||
loadSubscription = imgElement.onLoad.listen((html.Event event) { | ||
loadSubscription.cancel(); | ||
errorSubscription.cancel(); | ||
final HtmlImage image = HtmlImage( | ||
imgElement, | ||
imgElement.naturalWidth, | ||
imgElement.naturalHeight, | ||
); | ||
completer.complete(SingleFrameInfo(image)); | ||
}); | ||
imgElement.src = src; | ||
// If the browser supports asynchronous image decoding, use that instead | ||
// of `onload`. | ||
if (_supportsDecode) { | ||
imgElement.decode().then((dynamic _) { | ||
errorSubscription.cancel(); | ||
final HtmlImage image = HtmlImage( | ||
imgElement, | ||
imgElement.naturalWidth, | ||
imgElement.naturalHeight, | ||
); | ||
completer.complete(SingleFrameInfo(image)); | ||
}); | ||
} | ||
return completer.future; | ||
} | ||
|
||
@override | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better strategy might be to have a configurable limit on decoding concurrency. We'd then just pick a limit per browser. That may not obviate the need for this fallback, but it would be good to not rely on the fallback during normal app execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if we set a limit for Chrome and monitored rate of decoding it could change over time with no way to detect. Since it is edge case and likely to be fixed (other js image libraries reverted usage, its a known issue), fallback seemed best option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM