From 4ca7bfa0c2f891236142e421862c0c13e63c7ed6 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Wed, 3 Jul 2019 09:13:09 -0700 Subject: [PATCH] Re-apply 'Add currentSystemFrameTimeStamp to SchedulerBinding' (#35492) --- .../flutter/lib/src/scheduler/binding.dart | 16 +++++++++ .../test/scheduler/scheduler_test.dart | 33 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index 50cd635f90..d0eb09f024 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -853,6 +853,22 @@ mixin SchedulerBinding on BindingBase, ServicesBinding { } Duration _currentFrameTimeStamp; + /// The raw time stamp as provided by the engine to [Window.onBeginFrame] + /// for the frame currently being processed. + /// + /// Unlike [currentFrameTimeStamp], this time stamp is neither adjusted to + /// offset when the epoch started nor scaled to reflect the [timeDilation] in + /// the current epoch. + /// + /// On most platforms, this is a more or less arbitrary value, and should + /// generally be ignored. On Fuchsia, this corresponds to the system-provided + /// presentation time, and can be used to ensure that animations running in + /// different processes are synchronized. + Duration get currentSystemFrameTimeStamp { + assert(_lastRawTimeStamp != null); + return _lastRawTimeStamp; + } + int _debugFrameNumber = 0; String _debugBanner; bool _ignoreNextEngineDrawFrame = false; diff --git a/packages/flutter/test/scheduler/scheduler_test.dart b/packages/flutter/test/scheduler/scheduler_test.dart index 5eaa0fc6eb..bbb7dd6b18 100644 --- a/packages/flutter/test/scheduler/scheduler_test.dart +++ b/packages/flutter/test/scheduler/scheduler_test.dart @@ -10,6 +10,7 @@ import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import '../flutter_test_alternative.dart'; +import 'scheduler_tester.dart'; class TestSchedulerBinding extends BindingBase with ServicesBinding, SchedulerBinding { final Map>> eventsDispatched = >>{}; @@ -148,4 +149,36 @@ void main() { expect(event['build'], 5000); expect(event['raster'], 4000); }); + + test('currentSystemFrameTimeStamp is the raw timestamp', () { + Duration lastTimeStamp; + Duration lastSystemTimeStamp; + + void frameCallback(Duration timeStamp) { + expect(timeStamp, scheduler.currentFrameTimeStamp); + lastTimeStamp = scheduler.currentFrameTimeStamp; + lastSystemTimeStamp = scheduler.currentSystemFrameTimeStamp; + } + + scheduler.scheduleFrameCallback(frameCallback); + tick(const Duration(seconds: 2)); + expect(lastTimeStamp, Duration.zero); + expect(lastSystemTimeStamp, const Duration(seconds: 2)); + + scheduler.scheduleFrameCallback(frameCallback); + tick(const Duration(seconds: 4)); + expect(lastTimeStamp, const Duration(seconds: 2)); + expect(lastSystemTimeStamp, const Duration(seconds: 4)); + + timeDilation = 2; + scheduler.scheduleFrameCallback(frameCallback); + tick(const Duration(seconds: 6)); + expect(lastTimeStamp, const Duration(seconds: 2)); // timeDilation calls SchedulerBinding.resetEpoch + expect(lastSystemTimeStamp, const Duration(seconds: 6)); + + scheduler.scheduleFrameCallback(frameCallback); + tick(const Duration(seconds: 8)); + expect(lastTimeStamp, const Duration(seconds: 3)); // 2s + (8 - 6)s / 2 + expect(lastSystemTimeStamp, const Duration(seconds: 8)); + }); }