From 921c80302c1ea1342d35d3b31a90196772f3e0dd Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 8 Aug 2022 13:42:06 -0700 Subject: [PATCH] [flutter_tools] support hot reload of font assets (#109091) --- packages/flutter_tools/lib/src/devfs.dart | 11 ++++ .../lib/src/isolated/devfs_web.dart | 3 ++ packages/flutter_tools/lib/src/run_hot.dart | 12 ++++- packages/flutter_tools/lib/src/vmservice.dart | 15 ++++++ .../test/general.shard/devfs_test.dart | 51 +++++++++++++++++++ .../general.shard/resident_runner_test.dart | 3 ++ 6 files changed, 93 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index 50d49f6765..2f4cac0fd6 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -20,6 +20,8 @@ import 'compile.dart'; import 'convert.dart' show base64, utf8; import 'vmservice.dart'; +const String _kFontManifest = 'FontManifest.json'; + class DevFSConfig { /// Should DevFS assume that symlink targets are stable? bool cacheSymlinks = false; @@ -485,6 +487,9 @@ class DevFS { // A flag to indicate whether we have called `setAssetDirectory` on the target device. bool hasSetAssetDirectory = false; + /// Whether the font manifest was uploaded during [update]. + bool didUpdateFontManifest = false; + List sources = []; DateTime? lastCompiled; DateTime? _previousCompiled; @@ -589,6 +594,7 @@ class DevFS { assert(trackWidgetCreation != null); assert(generator != null); final DateTime candidateCompileTime = DateTime.now(); + didUpdateFontManifest = false; lastPackageConfig = packageConfig; _widgetCacheOutputFile = _fileSystem.file('$dillOutputPath.incremental.dill.widget_cache'); @@ -644,6 +650,11 @@ class DevFS { if (deviceUri.path.startsWith(assetBuildDirPrefix)) { archivePath = deviceUri.path.substring(assetBuildDirPrefix.length); } + // If the font manifest is updated, mark this as true so the hot runner + // can invoke a service extension to force the engine to reload fonts. + if (archivePath == _kFontManifest) { + didUpdateFontManifest = true; + } if (bundle.entryKinds[archivePath] == AssetKind.shader) { final Future pending = shaderCompiler.recompileShader(content); diff --git a/packages/flutter_tools/lib/src/isolated/devfs_web.dart b/packages/flutter_tools/lib/src/isolated/devfs_web.dart index 301678b32a..99a4bd1026 100644 --- a/packages/flutter_tools/lib/src/isolated/devfs_web.dart +++ b/packages/flutter_tools/lib/src/isolated/devfs_web.dart @@ -677,6 +677,9 @@ class WebDevFS implements DevFS { @override bool hasSetAssetDirectory = false; + @override + bool didUpdateFontManifest = false; + Future? _cachedExtensionFuture; StreamSubscription? _connectedApps; diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index 9ee2225f97..5ced6432c0 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -1032,7 +1032,7 @@ class HotRunner extends ResidentRunner { @visibleForTesting Future evictDirtyAssets() async { - final List?>> futures = >>[]; + final List> futures = >[]; for (final FlutterDevice? device in flutterDevices) { if (device!.devFS!.assetPathsToEvict.isEmpty && device.devFS!.shaderPathsToEvict.isEmpty) { continue; @@ -1060,6 +1060,14 @@ class HotRunner extends ResidentRunner { globals.printError('Application isolate not found for $device'); continue; } + + if (device.devFS!.didUpdateFontManifest) { + futures.add(device.vmService!.reloadAssetFonts( + isolateId: views.first.uiIsolate!.id!, + viewId: views.first.id, + )); + } + for (final String assetPath in device.devFS!.assetPathsToEvict) { futures.add( device.vmService! @@ -1081,7 +1089,7 @@ class HotRunner extends ResidentRunner { device.devFS!.assetPathsToEvict.clear(); device.devFS!.shaderPathsToEvict.clear(); } - await Future.wait?>(futures); + await Future.wait(futures); } @override diff --git a/packages/flutter_tools/lib/src/vmservice.dart b/packages/flutter_tools/lib/src/vmservice.dart index 09f24fb171..dc5f0519b3 100644 --- a/packages/flutter_tools/lib/src/vmservice.dart +++ b/packages/flutter_tools/lib/src/vmservice.dart @@ -24,6 +24,7 @@ const String kListViewsMethod = '_flutter.listViews'; const String kScreenshotSkpMethod = '_flutter.screenshotSkp'; const String kScreenshotMethod = '_flutter.screenshot'; const String kRenderFrameWithRasterStatsMethod = '_flutter.renderFrameWithRasterStats'; +const String kReloadAssetFonts = '_flutter.reloadAssetFonts'; /// The error response code from an unrecoverable compilation failure. const int kIsolateReloadBarred = 1005; @@ -880,6 +881,20 @@ class FlutterVmService { } } + /// Tell the provided flutter view that the font manifest has been updated + /// and asset fonts should be reloaded. + Future reloadAssetFonts({ + required String isolateId, + required String viewId, + }) async { + await callMethodWrapper( + kReloadAssetFonts, + isolateId: isolateId, args: { + 'viewId': viewId, + }, + ); + } + /// Waits for a signal from the VM service that [extensionName] is registered. /// /// Looks at the list of loaded extensions for first Flutter view, as well as diff --git a/packages/flutter_tools/test/general.shard/devfs_test.dart b/packages/flutter_tools/test/general.shard/devfs_test.dart index fce8a66e0d..3d3caf9399 100644 --- a/packages/flutter_tools/test/general.shard/devfs_test.dart +++ b/packages/flutter_tools/test/general.shard/devfs_test.dart @@ -643,6 +643,57 @@ void main() { FileSystem: () => fileSystem, ProcessManager: () => processManager, }); + + testUsingContext('DevFS tracks when FontManifest is updated', () async { + final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost( + requests: [createDevFSRequest], + httpAddress: Uri.parse('http://localhost'), + ); + final BufferLogger logger = BufferLogger.test(); + final DevFS devFS = DevFS( + fakeVmServiceHost.vmService, + 'test', + fileSystem.currentDirectory, + fileSystem: fileSystem, + logger: logger, + osUtils: FakeOperatingSystemUtils(), + httpClient: FakeHttpClient.any(), + ); + + await devFS.create(); + + expect(devFS.didUpdateFontManifest, false); + + final FakeResidentCompiler residentCompiler = FakeResidentCompiler() + ..onRecompile = (Uri mainUri, List? invalidatedFiles) async { + fileSystem.file('lib/foo.dill') + ..createSync(recursive: true) + ..writeAsBytesSync([1, 2, 3, 4, 5]); + return const CompilerOutput('lib/foo.dill', 0, []); + }; + final FakeBundle bundle = FakeBundle() + ..entries['FontManifest.json'] = DevFSByteContent([1, 2, 3, 4]); + + final UpdateFSReport report = await devFS.update( + mainUri: Uri.parse('lib/main.dart'), + generator: residentCompiler, + dillOutputPath: 'lib/foo.dill', + pathToReload: 'lib/foo.txt.dill', + trackWidgetCreation: false, + invalidatedFiles: [], + packageConfig: PackageConfig.empty, + shaderCompiler: const FakeShaderCompiler(), + bundle: bundle, + ); + + expect(report.success, true); + expect(devFS.shaderPathsToEvict, {}); + expect(devFS.assetPathsToEvict, {'FontManifest.json'}); + expect(devFS.didUpdateFontManifest, true); + }, overrides: { + FileSystem: () => fileSystem, + ProcessManager: () => processManager, + }); }); } diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 1e39119fd9..e7513b5b42 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -2633,6 +2633,9 @@ class FakeDevFS extends Fake implements DevFS { @override Set shaderPathsToEvict = {}; + @override + bool didUpdateFontManifest = false; + UpdateFSReport nextUpdateReport = UpdateFSReport(success: true); @override