@@ -1005,6 +1005,11 @@ class IsolateNameServer {
10051005///
10061006/// [FrameTiming] records a timestamp of each phase for performance analysis.
10071007enum FramePhase {
1008+ /// The timestamp of the vsync signal given by the operating system.
1009+ ///
1010+ /// See also [FrameTiming.vsyncOverhead] .
1011+ vsyncStart,
1012+
10081013 /// When the UI thread starts building a frame.
10091014 ///
10101015 /// See also [FrameTiming.buildDuration] .
@@ -1035,16 +1040,35 @@ enum FramePhase {
10351040/// Therefore it's recommended to only monitor and analyze performance metrics
10361041/// in profile and release modes.
10371042class FrameTiming {
1043+ /// Construct [FrameTiming] with raw timestamps in microseconds.
1044+ ///
1045+ /// This constructor is used for unit test only. Real [FrameTiming] s should
1046+ /// be retrieved from [Window.onReportTimings] .
1047+ factory FrameTiming ({
1048+ required int vsyncStart,
1049+ required int buildStart,
1050+ required int buildFinish,
1051+ required int rasterStart,
1052+ required int rasterFinish,
1053+ }) {
1054+ return FrameTiming ._(< int > [
1055+ vsyncStart,
1056+ buildStart,
1057+ buildFinish,
1058+ rasterStart,
1059+ rasterFinish
1060+ ]);
1061+ }
1062+
10381063 /// Construct [FrameTiming] with raw timestamps in microseconds.
10391064 ///
10401065 /// List [timestamps] must have the same number of elements as
10411066 /// [FramePhase.values] .
10421067 ///
10431068 /// This constructor is usually only called by the Flutter engine, or a test.
10441069 /// To get the [FrameTiming] of your app, see [Window.onReportTimings] .
1045- FrameTiming (List <int > timestamps)
1046- : assert (timestamps.length == FramePhase .values.length),
1047- _timestamps = timestamps;
1070+ FrameTiming ._(List <int > timestamps)
1071+ : assert (timestamps.length == FramePhase .values.length), _timestamps = timestamps;
10481072
10491073 /// Construct [FrameTiming] with given timestamp in micrseconds.
10501074 ///
@@ -1053,13 +1077,22 @@ class FrameTiming {
10531077 ///
10541078 /// TODO(CareF): This is part of #20229. Remove back to default constructor
10551079 /// after #20229 lands and corresponding framwork PRs land.
1056- FrameTiming .fromTimeStamps ({
1080+ factory FrameTiming .fromTimeStamps ({
10571081 int ? vsyncStart,
10581082 required int buildStart,
10591083 required int buildFinish,
10601084 required int rasterStart,
10611085 required int rasterFinish
1062- }) : _timestamps = < int > [buildStart, buildFinish, rasterStart, rasterFinish];
1086+ }) {
1087+ return FrameTiming ._(< int > [
1088+ // This is for temporarily backward compatiblilty.
1089+ vsyncStart ?? buildStart,
1090+ buildStart,
1091+ buildFinish,
1092+ rasterStart,
1093+ rasterFinish
1094+ ]);
1095+ }
10631096
10641097 /// This is a raw timestamp in microseconds from some epoch. The epoch in all
10651098 /// [FrameTiming] is the same, but it may not match [DateTime] 's epoch.
@@ -1095,24 +1128,28 @@ class FrameTiming {
10951128 _rawDuration (FramePhase .rasterFinish) -
10961129 _rawDuration (FramePhase .rasterStart);
10971130
1098- /// The timespan between build start and raster finish.
1131+ /// The duration between receiving the vsync signal and starting building the
1132+ /// frame.
1133+ Duration get vsyncOverhead => _rawDuration (FramePhase .buildStart) - _rawDuration (FramePhase .vsyncStart);
1134+
1135+ /// The timespan between vsync start and raster finish.
10991136 ///
11001137 /// To achieve the lowest latency on an X fps display, this should not exceed
11011138 /// 1000/X milliseconds.
11021139 /// {@macro dart.ui.FrameTiming.fps_milliseconds}
11031140 ///
1104- /// See also [buildDuration] and [rasterDuration] .
1141+ /// See also [vsyncOverhead] , [ buildDuration] and [rasterDuration] .
11051142 Duration get totalSpan =>
11061143 _rawDuration (FramePhase .rasterFinish) -
1107- _rawDuration (FramePhase .buildStart );
1144+ _rawDuration (FramePhase .vsyncStart );
11081145
11091146 final List <int > _timestamps; // in microseconds
11101147
11111148 String _formatMS (Duration duration) => '${duration .inMicroseconds * 0.001 }ms' ;
11121149
11131150 @override
11141151 String toString () {
1115- return '$runtimeType (buildDuration: ${_formatMS (buildDuration )}, rasterDuration: ${_formatMS (rasterDuration )}, totalSpan: ${_formatMS (totalSpan )})' ;
1152+ return '$runtimeType (buildDuration: ${_formatMS (buildDuration )}, rasterDuration: ${_formatMS (rasterDuration )}, vsyncOverhead: ${ _formatMS ( vsyncOverhead )}, totalSpan: ${_formatMS (totalSpan )})' ;
11161153 }
11171154}
11181155
0 commit comments