From c486fc4a966dece3ea9401634979214f4c845b0a Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 22 Apr 2016 11:05:52 -0700 Subject: [PATCH] Revert "A blinking cursor should push only one frame (#3445)" This reverts commit 161f945e84bd3c801f32a030245f37755d5f677a. This patch caused a number of regressions. Fixes #3497 --- packages/flutter/lib/src/material/drawer.dart | 2 + .../flutter/lib/src/scheduler/binding.dart | 48 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/flutter/lib/src/material/drawer.dart b/packages/flutter/lib/src/material/drawer.dart index 6d7990762a..d17572b6e6 100644 --- a/packages/flutter/lib/src/material/drawer.dart +++ b/packages/flutter/lib/src/material/drawer.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/scheduler.dart'; import 'package:flutter/widgets.dart'; import 'colors.dart'; @@ -170,6 +171,7 @@ class DrawerControllerState extends State { } double get _width { + assert(!Scheduler.debugInFrame); // we should never try to read the tree state while building or laying out RenderBox drawerBox = _drawerKey.currentContext?.findRenderObject(); if (drawerBox != null) return drawerBox.size.width; diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index 1f47b420ab..f7e56e37eb 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -86,7 +86,7 @@ abstract class SchedulerBinding extends BindingBase { void initServiceExtensions() { super.initServiceExtensions(); registerNumericServiceExtension( - name: 'timeDilation', + name: 'timeDilation', getter: () => timeDilation, setter: (double value) { timeDilation = value; @@ -94,8 +94,9 @@ abstract class SchedulerBinding extends BindingBase { ); } + /// The strategy to use when deciding whether to run a task or not. - /// + /// /// Defaults to [defaultSchedulingStrategy]. SchedulingStrategy schedulingStrategy = defaultSchedulingStrategy; @@ -118,6 +119,7 @@ abstract class SchedulerBinding extends BindingBase { _ensureEventLoopCallback(); } + // Whether this scheduler already requested to be called from the event loop. bool _hasRequestedAnEventLoopCallback = false; @@ -153,10 +155,11 @@ abstract class SchedulerBinding extends BindingBase { } else { // TODO(floitsch): we shouldn't need to request a frame. Just schedule // an event-loop callback. - _scheduleFrame(); + ensureVisualUpdate(); } } + int _nextFrameCallbackId = 0; // positive Map _transientCallbacks = {}; final Set _removedIds = new HashSet(); @@ -184,7 +187,7 @@ abstract class SchedulerBinding extends BindingBase { /// Callbacks registered with this method can be canceled using /// [cancelFrameCallbackWithId]. int scheduleFrameCallback(FrameCallback callback, { bool rescheduling: false }) { - _scheduleFrame(); + ensureVisualUpdate(); return addFrameCallback(callback, rescheduling: rescheduling); } @@ -267,6 +270,7 @@ abstract class SchedulerBinding extends BindingBase { return true; } + final List _persistentCallbacks = new List(); /// Adds a persistent frame callback. @@ -282,6 +286,7 @@ abstract class SchedulerBinding extends BindingBase { _persistentCallbacks.add(callback); } + final List _postFrameCallbacks = new List(); /// Schedule a callback for the end of this frame. @@ -301,11 +306,10 @@ abstract class SchedulerBinding extends BindingBase { _postFrameCallbacks.add(callback); } - // Whether this scheduler as requested that handleBeginFrame be called soon. - bool _hasScheduledFrame = false; - // Whether this scheduler is currently producing a frame in handleBeginFrame. - bool _isProducingFrame = false; + // Whether this scheduler already requested to be called at the beginning of + // the next frame. + bool _hasRequestedABeginFrameCallback = false; /// If necessary, schedules a new frame by calling /// [ui.window.scheduleFrame]. @@ -315,18 +319,20 @@ abstract class SchedulerBinding extends BindingBase { /// device's screen is turned off it will typically be delayed until /// the screen is on and the application is visible.) void ensureVisualUpdate() { - if (_hasScheduledFrame || _isProducingFrame) - return; - _scheduleFrame(); - } - - void _scheduleFrame() { - if (_hasScheduledFrame) + if (_hasRequestedABeginFrameCallback) return; ui.window.scheduleFrame(); - _hasScheduledFrame = true; + _hasRequestedABeginFrameCallback = true; } + /// Whether the scheduler is currently handling a "begin frame" + /// callback. + /// + /// True while [handleBeginFrame] is running in checked mode. False + /// otherwise. + static bool get debugInFrame => _debugInFrame; + static bool _debugInFrame = false; + /// Called by the engine to produce a new frame. /// /// This function first calls all the callbacks registered by @@ -336,11 +342,11 @@ abstract class SchedulerBinding extends BindingBase { /// callbacks registered by [addPostFrameCallback]. void handleBeginFrame(Duration rawTimeStamp) { Timeline.startSync('Begin frame'); - assert(!_isProducingFrame); - _isProducingFrame = true; - _hasScheduledFrame = false; + assert(!_debugInFrame); + assert(() { _debugInFrame = true; return true; }); Duration timeStamp = new Duration( microseconds: (rawTimeStamp.inMicroseconds / timeDilation).round()); + _hasRequestedABeginFrameCallback = false; _invokeTransientFrameCallbacks(timeStamp); for (FrameCallback callback in _persistentCallbacks) @@ -352,7 +358,7 @@ abstract class SchedulerBinding extends BindingBase { for (FrameCallback callback in localPostFrameCallbacks) _invokeFrameCallback(callback, timeStamp); - _isProducingFrame = false; + assert(() { _debugInFrame = false; return true; }); Timeline.finishSync(); // All frame-related callbacks have been executed. Run lower-priority tasks. @@ -361,7 +367,7 @@ abstract class SchedulerBinding extends BindingBase { void _invokeTransientFrameCallbacks(Duration timeStamp) { Timeline.startSync('Animate'); - assert(_isProducingFrame); + assert(_debugInFrame); Map callbacks = _transientCallbacks; _transientCallbacks = new Map(); callbacks.forEach((int id, _FrameCallbackEntry callbackEntry) {