|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:ui/src/engine.dart'; |
| 6 | +import 'package:ui/ui.dart' as ui; |
| 7 | + |
| 8 | +class FrameTimingRecorder { |
| 9 | + final int _vsyncStartMicros = _currentFrameVsyncStart; |
| 10 | + final int _buildStartMicros = _currentFrameBuildStart; |
| 11 | + |
| 12 | + int? _buildFinishMicros; |
| 13 | + int? _rasterStartMicros; |
| 14 | + int? _rasterFinishMicros; |
| 15 | + |
| 16 | + /// Collects frame timings from frames. |
| 17 | + /// |
| 18 | + /// This list is periodically reported to the framework (see [_kFrameTimingsSubmitInterval]). |
| 19 | + static List<ui.FrameTiming> _frameTimings = <ui.FrameTiming>[]; |
| 20 | + |
| 21 | + /// These two metrics are collected early in the process, before the respective |
| 22 | + /// scene builders are created. These are instead treated as global state, which |
| 23 | + /// are used to initialize any recorders that are created by the scene builders. |
| 24 | + static int _currentFrameVsyncStart = 0; |
| 25 | + static int _currentFrameBuildStart = 0; |
| 26 | + |
| 27 | + static void recordCurrentFrameVsync() { |
| 28 | + if (frameTimingsEnabled) { |
| 29 | + _currentFrameVsyncStart = _nowMicros(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + static void recordCurrentFrameBuildStart() { |
| 34 | + if (frameTimingsEnabled) { |
| 35 | + _currentFrameBuildStart = _nowMicros(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// The last time (in microseconds) we submitted frame timings. |
| 40 | + static int _frameTimingsLastSubmitTime = _nowMicros(); |
| 41 | + /// The amount of time in microseconds we wait between submitting |
| 42 | + /// frame timings. |
| 43 | + static const int _kFrameTimingsSubmitInterval = 100000; // 100 milliseconds |
| 44 | + |
| 45 | + /// Whether we are collecting [ui.FrameTiming]s. |
| 46 | + static bool get frameTimingsEnabled { |
| 47 | + return EnginePlatformDispatcher.instance.onReportTimings != null; |
| 48 | + } |
| 49 | + |
| 50 | + /// Current timestamp in microseconds taken from the high-precision |
| 51 | + /// monotonically increasing timer. |
| 52 | + /// |
| 53 | + /// See also: |
| 54 | + /// |
| 55 | + /// * https://developer.mozilla.org/en-US/docs/Web/API/Performance/now, |
| 56 | + /// particularly notes about Firefox rounding to 1ms for security reasons, |
| 57 | + /// which can be bypassed in tests by setting certain browser options. |
| 58 | + static int _nowMicros() { |
| 59 | + return (domWindow.performance.now() * 1000).toInt(); |
| 60 | + } |
| 61 | + |
| 62 | + void recordBuildFinish([int? buildFinish]) { |
| 63 | + assert(_buildFinishMicros == null, "can't record build finish more than once"); |
| 64 | + _buildFinishMicros = buildFinish ?? _nowMicros(); |
| 65 | + } |
| 66 | + |
| 67 | + void recordRasterStart([int? rasterStart]) { |
| 68 | + assert(_rasterStartMicros == null, "can't record raster start more than once"); |
| 69 | + _rasterStartMicros = rasterStart ?? _nowMicros(); |
| 70 | + } |
| 71 | + |
| 72 | + void recordRasterFinish([int? rasterFinish]) { |
| 73 | + assert(_rasterFinishMicros == null, "can't record raster finish more than once"); |
| 74 | + _rasterFinishMicros = rasterFinish ?? _nowMicros(); |
| 75 | + } |
| 76 | + |
| 77 | + void submitTimings() { |
| 78 | + assert( |
| 79 | + _buildFinishMicros != null && |
| 80 | + _rasterStartMicros != null && |
| 81 | + _rasterFinishMicros != null, |
| 82 | + 'Attempted to submit an incomplete timings.' |
| 83 | + ); |
| 84 | + final ui.FrameTiming timing = ui.FrameTiming( |
| 85 | + vsyncStart: _vsyncStartMicros, |
| 86 | + buildStart: _buildStartMicros, |
| 87 | + buildFinish: _buildFinishMicros!, |
| 88 | + rasterStart: _rasterStartMicros!, |
| 89 | + rasterFinish: _rasterFinishMicros!, |
| 90 | + rasterFinishWallTime: _rasterFinishMicros!, |
| 91 | + ); |
| 92 | + _frameTimings.add(timing); |
| 93 | + final int now = _nowMicros(); |
| 94 | + if (now - _frameTimingsLastSubmitTime > _kFrameTimingsSubmitInterval) { |
| 95 | + _frameTimingsLastSubmitTime = now; |
| 96 | + EnginePlatformDispatcher.instance.invokeOnReportTimings(_frameTimings); |
| 97 | + _frameTimings = <ui.FrameTiming>[]; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments