44
55import 'dart:async' ;
66import 'dart:collection' ;
7- import 'dart:developer' ;
8- import 'dart:ui' show AppLifecycleState;
7+ import 'dart:developer' show Flow, Timeline ;
8+ import 'dart:ui' show AppLifecycleState, FramePhase, FrameTiming ;
99
1010import 'package:collection/collection.dart' show PriorityQueue, HeapPriorityQueue;
1111import 'package:flutter/foundation.dart' ;
@@ -198,6 +198,17 @@ mixin SchedulerBinding on BindingBase, ServicesBinding {
198198 window.onDrawFrame = _handleDrawFrame;
199199 SystemChannels .lifecycle.setMessageHandler (_handleLifecycleMessage);
200200 readInitialLifecycleStateFromNativeWindow ();
201+
202+ if (! kReleaseMode) {
203+ int frameNumber = 0 ;
204+
205+ window.onReportTimings = (List <FrameTiming > timings) {
206+ for (FrameTiming frameTiming in timings) {
207+ frameNumber += 1 ;
208+ _profileFramePostEvent (frameNumber, frameTiming);
209+ }
210+ };
211+ }
201212 }
202213
203214 /// The current [SchedulerBinding] , if one has been created.
@@ -842,8 +853,7 @@ mixin SchedulerBinding on BindingBase, ServicesBinding {
842853 }
843854 Duration _currentFrameTimeStamp;
844855
845- int _profileFrameNumber = 0 ;
846- final Stopwatch _profileFrameStopwatch = Stopwatch ();
856+ int _debugFrameNumber = 0 ;
847857 String _debugBanner;
848858 bool _ignoreNextEngineDrawFrame = false ;
849859
@@ -894,21 +904,17 @@ mixin SchedulerBinding on BindingBase, ServicesBinding {
894904 if (rawTimeStamp != null )
895905 _lastRawTimeStamp = rawTimeStamp;
896906
897- if (! kReleaseMode) {
898- _profileFrameNumber += 1 ;
899- _profileFrameStopwatch.reset ();
900- _profileFrameStopwatch.start ();
901- }
902-
903907 assert (() {
908+ _debugFrameNumber += 1 ;
909+
904910 if (debugPrintBeginFrameBanner || debugPrintEndFrameBanner) {
905911 final StringBuffer frameTimeStampDescription = StringBuffer ();
906912 if (rawTimeStamp != null ) {
907913 _debugDescribeTimeStamp (_currentFrameTimeStamp, frameTimeStampDescription);
908914 } else {
909915 frameTimeStampDescription.write ('(warm-up frame)' );
910916 }
911- _debugBanner = '▄▄▄▄▄▄▄▄ Frame ${_profileFrameNumber .toString ().padRight (7 )} ${frameTimeStampDescription .toString ().padLeft (18 )} ▄▄▄▄▄▄▄▄' ;
917+ _debugBanner = '▄▄▄▄▄▄▄▄ Frame ${_debugFrameNumber .toString ().padRight (7 )} ${frameTimeStampDescription .toString ().padLeft (18 )} ▄▄▄▄▄▄▄▄' ;
912918 if (debugPrintBeginFrameBanner)
913919 debugPrint (_debugBanner);
914920 }
@@ -961,10 +967,6 @@ mixin SchedulerBinding on BindingBase, ServicesBinding {
961967 } finally {
962968 _schedulerPhase = SchedulerPhase .idle;
963969 Timeline .finishSync (); // end the Frame
964- if (! kReleaseMode) {
965- _profileFrameStopwatch.stop ();
966- _profileFramePostEvent ();
967- }
968970 assert (() {
969971 if (debugPrintEndFrameBanner)
970972 debugPrint ('▀' * _debugBanner.length);
@@ -975,11 +977,13 @@ mixin SchedulerBinding on BindingBase, ServicesBinding {
975977 }
976978 }
977979
978- void _profileFramePostEvent () {
980+ void _profileFramePostEvent (int frameNumber, FrameTiming frameTiming ) {
979981 postEvent ('Flutter.Frame' , < String , dynamic > {
980- 'number' : _profileFrameNumber,
981- 'startTime' : _currentFrameTimeStamp.inMicroseconds,
982- 'elapsed' : _profileFrameStopwatch.elapsedMicroseconds,
982+ 'number' : frameNumber,
983+ 'startTime' : frameTiming.timestampInMicroseconds (FramePhase .buildStart),
984+ 'elapsed' : frameTiming.totalSpan.inMicroseconds,
985+ 'build' : frameTiming.buildDuration.inMicroseconds,
986+ 'raster' : frameTiming.rasterDuration.inMicroseconds,
983987 });
984988 }
985989
0 commit comments