This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add a BroadcastStream to FrameTiming #11041
Merged
Merged
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,17 +10,18 @@ typedef VoidCallback = void Function(); | |
| /// Signature for [Window.onBeginFrame]. | ||
| typedef FrameCallback = void Function(Duration duration); | ||
|
|
||
| // ignore: deprecated_member_use_from_same_package | ||
| /// Signature for [Window.onReportTimings]. | ||
| /// | ||
| /// {@template dart.ui.TimingsCallback.list} | ||
| /// The callback takes a list of [FrameTiming] because it may not be | ||
| /// immediately triggered after each frame. Instead, Flutter tries to batch | ||
| /// frames together and send all their timings at once to decrease the | ||
| /// overhead (as this is available in the release mode). The list is sorted in | ||
| /// ascending order of time (earliest frame first). The timing of any frame | ||
| /// will be sent within about 1 second (100ms if in the profile/debug mode) | ||
| /// even if there are no later frames to batch. The timing of the first frame | ||
| /// will be sent immediately without batching. | ||
| /// The callback takes a list of [FrameTiming] because it may not be immediately | ||
| /// triggered after each frame. The list is sorted in ascending order of time | ||
| /// (earliest frame first). | ||
| /// {@template dart.ui.timings_batching} | ||
| /// Flutter tries to batch frames together and send all their timings at once to | ||
| /// decrease the overhead (as this is available in the release mode). The timing | ||
| /// of any frame will be sent within about 1 second (100ms if in the | ||
| /// profile/debug mode) even if there are no later frames to batch. The timing | ||
| /// of the first frame will be sent immediately without batching. | ||
| /// {@endtemplate} | ||
| typedef TimingsCallback = void Function(List<FrameTiming> timings); | ||
|
|
||
|
|
@@ -69,7 +70,7 @@ enum FramePhase { | |
|
|
||
| /// Time-related performance metrics of a frame. | ||
| /// | ||
| /// See [Window.onReportTimings] for how to get this. | ||
| /// See [Window.frameTimings] for how to get this. | ||
| /// | ||
| /// The metrics in debug mode (`flutter run` without any flags) may be very | ||
| /// different from those in profile and release modes due to the debug overhead. | ||
|
|
@@ -82,7 +83,7 @@ class FrameTiming { | |
| /// [FramePhase.values]. | ||
| /// | ||
| /// This constructor is usually only called by the Flutter engine, or a test. | ||
| /// To get the [FrameTiming] of your app, see [Window.onReportTimings]. | ||
| /// To get the [FrameTiming] of your app, see [Window.frameTimings]. | ||
| FrameTiming(List<int> timestamps) | ||
| : assert(timestamps.length == FramePhase.values.length), _timestamps = timestamps; | ||
|
|
||
|
|
@@ -914,31 +915,77 @@ class Window { | |
| /// A callback that is invoked to report the [FrameTiming] of recently | ||
| /// rasterized frames. | ||
| /// | ||
| /// This can be used to see if the application has missed frames (through | ||
| /// [FrameTiming.buildDuration] and [FrameTiming.rasterDuration]), or high | ||
| /// latencies (through [FrameTiming.totalSpan]). | ||
| /// | ||
| /// Unlike [Timeline], the timing information here is available in the release | ||
| /// mode (additional to the profile and the debug mode). Hence this can be | ||
| /// used to monitor the application's performance in the wild. | ||
| /// | ||
| /// {@macro dart.ui.TimingsCallback.list} | ||
| /// | ||
| /// If this is null, no additional work will be done. If this is not null, | ||
| /// Flutter spends less than 0.1ms every 1 second to report the timings | ||
| /// (measured on iPhone6S). The 0.1ms is about 0.6% of 16ms (frame budget for | ||
| /// 60fps), or 0.01% CPU usage per second. | ||
| /// This is deprecated, use [frameTimings] instead. | ||
| @Deprecated('Use frameTimings instead.') | ||
| TimingsCallback get onReportTimings => _onReportTimings; | ||
| TimingsCallback _onReportTimings; | ||
| Zone _onReportTimingsZone; | ||
| @Deprecated('Use frameTimings instead.') | ||
| set onReportTimings(TimingsCallback callback) { | ||
| _internalSetOnReportTimings(callback); | ||
| } | ||
|
|
||
| void _internalSetOnReportTimings(TimingsCallback callback) { | ||
| if ((callback == null) != (_onReportTimings == null)) { | ||
| _setNeedsReportTimings(callback != null); | ||
| } | ||
| _onReportTimings = callback; | ||
| _onReportTimingsZone = Zone.current; | ||
| } | ||
|
|
||
| // ignore: deprecated_member_use_from_same_package | ||
| /// Mock the calling of [onReportTimings] for unit tests. | ||
| void debugReportTimings(List<FrameTiming> timings) { | ||
| _onReportTimings(timings); | ||
| } | ||
|
|
||
| /// Check whether the engine has to report timings. | ||
| /// | ||
| /// This is for unit tests and debug purposes only. | ||
| bool get debugNeedsReportTimings => _onReportTimings != null; | ||
|
|
||
| StreamController<FrameTiming> _frameTimingBroadcastController; | ||
|
|
||
| void _onFrameTimingListen() { | ||
| _internalSetOnReportTimings((List<FrameTiming> timings) { | ||
| timings.forEach(_frameTimingBroadcastController.add); | ||
| }); | ||
| } | ||
|
|
||
| // If there's no one listening, set [onReportTimings] back to null so the | ||
| // engine won't send [FrameTiming] from engine to the framework. | ||
| void _onFrameTimingCancel() { | ||
| _internalSetOnReportTimings(null); | ||
| } | ||
|
|
||
| /// A broadcast stream of the frames' time-related performance metrics. | ||
| /// | ||
| /// This can be used to see if the application has missed frames (through | ||
| /// [FrameTiming.buildDuration] and [FrameTiming.rasterDuration]), or high | ||
| /// latencies (through [FrameTiming.totalSpan]). | ||
| /// | ||
| /// Unlike [Timeline], the timing information here is available in the release | ||
|
Member
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. drop the "the" before "release", "profile" and "debug"?
Contributor
Author
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. |
||
| /// mode (additional to profile and debug mode). Hence this can be used to | ||
| /// monitor the application's performance in the wild. | ||
| /// | ||
| /// {@macro dart.ui.timings_batching} | ||
| /// | ||
| /// If no one is listening to this stream, no additional work will be done. | ||
| /// Otherwise, Flutter spends less than 0.1ms every 1 second to report the | ||
| /// timings (measured on iPhone 6s). The 0.1ms is about 0.6% of 16ms (frame | ||
| /// budget for 60fps), or 0.01% CPU usage per second. | ||
| /// | ||
| /// See also: | ||
| /// | ||
| /// * [FrameTiming], the data event of this stream | ||
| Stream<FrameTiming> get frameTimings { | ||
| _frameTimingBroadcastController ??= StreamController<FrameTiming>.broadcast( | ||
| onListen: _onFrameTimingListen, | ||
| onCancel: _onFrameTimingCancel, | ||
| ); | ||
| return _frameTimingBroadcastController.stream; | ||
| } | ||
|
|
||
| _SetNeedsReportTimingsFunc _setNeedsReportTimings; | ||
| void _nativeSetNeedsReportTimings(bool value) native 'Window_setNeedsReportTimings'; | ||
|
|
||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
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.
Is it more efficient to call
timings.forEach(_frameTimingBroadcastController.add), or: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 believe forEach is ok here since we are passing in a method directly and do not allocate a closure.
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.
If I write a
for ( in )loop, the analyzer will complain... It forces me to writeforEach.