Skip to content

Conversation

jiahaog
Copy link
Member

@jiahaog jiahaog commented Apr 30, 2020

Description

The completer backing WidgetsBinding.firstFrameRasterized never completes when deferFirstFrame is used.

Please see jiahaog/repro_flutter_first_frame@ed49ba4 for a small reproduction.

This is because firstFrameCallback is removed here when the current frame is a deferred frame

SchedulerBinding.instance.removeTimingsCallback(firstFrameCallback);

but _needToReportFirstFrame is reset back to true. As a result, the next iteration of drawFrame for the frame that should be sent to the engine will never trigger the conditional block that reports the frames.

if (_needToReportFirstFrame) {
assert(!_firstFrameCompleter.isCompleted);
firstFrameCallback = (List<FrameTiming> timings) {
assert(sendFramesToEngine);
if (!kReleaseMode) {
developer.Timeline.instantSync('Rasterized first useful frame');
developer.postEvent('Flutter.FirstFrame', <String, dynamic>{});
}
SchedulerBinding.instance.removeTimingsCallback(firstFrameCallback);
firstFrameCallback = null;
_firstFrameCompleter.complete();
};
// Callback is only invoked when [Window.render] is called. When
// [sendFramesToEngine] is set to false during the frame, it will not
// be called and we need to remove the callback (see below).
SchedulerBinding.instance.addTimingsCallback(firstFrameCallback);
}

This affects driver tests as well when the FlutterDriver.waitUntilFirstFrameRasterized is used. (Internal bug b/155033896)

Related Issues

#45135

Tests

I added the following tests:

  • Add a test where deferred frames will trigger the first frame rasterized flag.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

  • No, no existing tests failed, so this is not a breaking change.
  • Yes, this is a breaking change. If not, delete the remainder of this section.
    • I wrote a design doc: https://flutter.dev/go/template Replace this with a link to your design doc's short link
    • I got input from the developer relations team, specifically from: Replace with the names of who gave advice
    • I wrote a migration guide: Replace with a link to your migration guide

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! The fix looks right. In order to accept the PR we will need a test, though. Can you please add one? Thanks.

This Future does not complete when `deferFirstFrame` is used.
@jiahaog jiahaog force-pushed the fix-first-frame branch from 5956f1d to 8d07d47 Compare May 1, 2020 05:09
@jiahaog jiahaog force-pushed the fix-first-frame branch from 8d07d47 to a539e6d Compare May 1, 2020 06:26

// Simulates the engine completing a frame render to trigger the
// appropriate callback setting [WidgetBinding.firstFrameRasterized].
binding.window.onReportTimings(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if there's a better way to do this, as there doesn't seem to be an engine in the tests that actually calls the callbacks.

@jiahaog
Copy link
Member Author

jiahaog commented May 1, 2020

@goderbauer added tests, please take a look!

@jiahaog jiahaog requested a review from goderbauer May 1, 2020 11:45
Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after comments are resolved.

jiahaog and others added 2 commits May 2, 2020 07:18
…est.dart

Co-authored-by: Michael Goderbauer <goderbauer@google.com>
@jiahaog jiahaog requested a review from goderbauer May 1, 2020 23:23
Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fluttergithubbot fluttergithubbot merged commit d6f6fc4 into flutter:master May 5, 2020
@jiahaog jiahaog deleted the fix-first-frame branch May 5, 2020 02:51
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants