flutter/packages/flutter_tools/lib/src/cmake_project.dart
Loïc Sharma ce61eda70c
[Windows] Ensure window is shown (#127046)
## Background

The Windows runner has a race at startup:

1. **Platform thread**: creates a hidden window
2. **Platform thread**: launches the Flutter engine
3. **UI/Raster threads**: renders the first frame
4. **Platform thread**: Registers a callback to show the window once the next frame has been rendered.

Steps 3 and 4 happen in parallel and it is possible for step 3 to complete before step 4 starts. In this scenario, the next frame callback is never called and the window is never shown.

As a result the `windows_startup_test`'s test, which [verifies that the "show window" callback is called](1f09a8662d/dev/integration_tests/windows_startup_test/windows/runner/flutter_window.cpp (L60-L64)), can flake if the first frame is rendered before the show window callback has been registered.

## Solution

This change makes the runner schedule a frame after it registers the next frame callback. If step 3 hasn't completed yet, this no-ops as a frame is already scheduled. If step 3 has already completed, a new frame will be rendered, which will call the next frame callback and show the window.

Part of https://github.com/flutter/flutter/issues/119415

See this thread for alternatives that were considered: https://github.com/flutter/engine/pull/42061#issuecomment-1550080722
2023-05-19 22:25:55 +00:00

145 lines
5.0 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'base/file_system.dart';
import 'base/utils.dart';
import 'platform_plugins.dart';
import 'project.dart';
/// Represents a CMake-based sub-project.
///
/// This defines interfaces common to Windows and Linux projects.
abstract class CmakeBasedProject {
/// The parent of this project.
FlutterProject get parent;
/// Whether the subproject (either Windows or Linux) exists in the Flutter project.
bool existsSync();
/// The native project CMake specification.
File get cmakeFile;
/// Contains definitions for the Flutter library and the tool.
File get managedCmakeFile;
/// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
/// the build.
File get generatedCmakeConfigFile;
/// Included CMake with rules and variables for plugin builds.
File get generatedPluginCmakeFile;
/// The directory to write plugin symlinks.
Directory get pluginSymlinkDirectory;
}
/// The Windows sub project.
class WindowsProject extends FlutterProjectPlatform implements CmakeBasedProject {
WindowsProject.fromFlutter(this.parent);
@override
final FlutterProject parent;
@override
String get pluginConfigKey => WindowsPlugin.kConfigKey;
String get _childDirectory => 'windows';
@override
bool existsSync() => _editableDirectory.existsSync() && cmakeFile.existsSync();
@override
File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');
@override
File get managedCmakeFile => managedDirectory.childFile('CMakeLists.txt');
@override
File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');
@override
File get generatedPluginCmakeFile => managedDirectory.childFile('generated_plugins.cmake');
/// The native entrypoint's CMake specification.
File get runnerCmakeFile => runnerDirectory.childFile('CMakeLists.txt');
/// The native entrypoint's file that adds Flutter to the window.
File get runnerFlutterWindowFile => runnerDirectory.childFile('flutter_window.cpp');
/// The native entrypoint's resource file. Used to configure things
/// like the application icon, name, and version.
File get runnerResourceFile => runnerDirectory.childFile('Runner.rc');
@override
Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');
Directory get _editableDirectory => parent.directory.childDirectory(_childDirectory);
/// The directory in the project that is managed by Flutter. As much as
/// possible, files that are edited by Flutter tooling after initial project
/// creation should live here.
Directory get managedDirectory => _editableDirectory.childDirectory('flutter');
/// The subdirectory of [managedDirectory] that contains files that are
/// generated on the fly. All generated files that are not intended to be
/// checked in should live here.
Directory get ephemeralDirectory => managedDirectory.childDirectory('ephemeral');
/// The directory in the project that is owned by the app. As much as
/// possible, Flutter tooling should not edit files in this directory after
/// initial project creation.
Directory get runnerDirectory => _editableDirectory.childDirectory('runner');
Future<void> ensureReadyForPlatformSpecificTooling() async {}
}
/// The Linux sub project.
class LinuxProject extends FlutterProjectPlatform implements CmakeBasedProject {
LinuxProject.fromFlutter(this.parent);
@override
final FlutterProject parent;
@override
String get pluginConfigKey => LinuxPlugin.kConfigKey;
static final RegExp _applicationIdPattern = RegExp(r'''^\s*set\s*\(\s*APPLICATION_ID\s*"(.*)"\s*\)\s*$''');
Directory get _editableDirectory => parent.directory.childDirectory('linux');
/// The directory in the project that is managed by Flutter. As much as
/// possible, files that are edited by Flutter tooling after initial project
/// creation should live here.
Directory get managedDirectory => _editableDirectory.childDirectory('flutter');
/// The subdirectory of [managedDirectory] that contains files that are
/// generated on the fly. All generated files that are not intended to be
/// checked in should live here.
Directory get ephemeralDirectory => managedDirectory.childDirectory('ephemeral');
@override
bool existsSync() => _editableDirectory.existsSync();
@override
File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');
@override
File get managedCmakeFile => managedDirectory.childFile('CMakeLists.txt');
@override
File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');
@override
File get generatedPluginCmakeFile => managedDirectory.childFile('generated_plugins.cmake');
@override
Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');
Future<void> ensureReadyForPlatformSpecificTooling() async {}
String? get applicationId {
return firstMatchInFile(cmakeFile, _applicationIdPattern)?.group(1);
}
}