Visitar URL original
[flutter_tools] Support flavors and transformers for shaders by Laurie-Lin · Pull Request #181889 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/flutter_tools/lib/src/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,8 @@ class ManifestAssetBundle implements AssetBundle {
return false;
});

for (final Uri shaderUri in flutterManifest.shaders) {
for (final AssetsEntry shaderEntry in flutterManifest.shaders) {
final Uri shaderUri = shaderEntry.uri;
for (final AssetsEntry assetEntry in flutterManifest.assets) {
final String assetPath = assetEntry.uri.path;
final String shaderPath = shaderUri.path;
Expand Down Expand Up @@ -1116,9 +1117,9 @@ class ManifestAssetBundle implements AssetBundle {
packageName: packageName,
attributedPackage: attributedPackage,
assetKind: AssetKind.shader,
flavors: <String>{},
platforms: <String>{},
transformers: <AssetTransformerEntry>[],
flavors: shaderEntry.flavors,
Comment thread
Laurie-Lin marked this conversation as resolved.
Resolved
platforms: shaderEntry.platforms,
transformers: shaderEntry.transformers,
);
}

Expand Down
22 changes: 21 additions & 1 deletion packages/flutter_tools/lib/src/build_system/targets/assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,28 @@ Future<Depfile> copyAssets(
relativePath: entry.key,
);
case AssetKind.shader:
File inputToCompiler = content.file as File;
if (entry.value.transformers.isNotEmpty) {
transformResource = await transformPool.request();
final String transformedShaderSourcePath = '${file.path}.transformed';
final AssetTransformationFailure? failure = await assetTransformer.transformAsset(
asset: inputToCompiler,
outputPath: transformedShaderSourcePath,
workingDirectory: environment.projectDir.path,
transformerEntries: entry.value.transformers,
logger: environment.logger,
);
if (failure != null) {
throwToolExit(
'User-defined transformation of shader "${entry.key}" failed.\n'
'${failure.message}',
);
}
inputToCompiler = environment.fileSystem.file(transformedShaderSourcePath);
}
Comment thread
Laurie-Lin marked this conversation as resolved.
Resolved

doCopy = !await shaderCompiler.compileShader(
input: content.file as File,
input: inputToCompiler,
outputPath: file.path,
targetPlatform: targetPlatform,
);
Expand Down
21 changes: 20 additions & 1 deletion packages/flutter_tools/lib/src/bundle_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,27 @@ Future<void> writeBundle(
case AssetKind.font:
break;
case AssetKind.shader:
File inputToCompiler = input;
if (entry.value.transformers.isNotEmpty) {
final String transformedShaderSourcePath = '${file.path}.transformed';
final AssetTransformationFailure? failure = await assetTransformer.transformAsset(
asset: inputToCompiler,
outputPath: transformedShaderSourcePath,
workingDirectory: projectDir.path,
transformerEntries: entry.value.transformers,
logger: logger,
);
if (failure != null) {
throwToolExit(
'User-defined transformation of shader "${entry.key}" failed.\n'
'${failure.message}',
);
}
inputToCompiler = fileSystem.file(transformedShaderSourcePath);
}
Comment thread
Laurie-Lin marked this conversation as resolved.
Resolved

doCopy = !await shaderCompiler.compileShader(
input: input,
input: inputToCompiler,
outputPath: file.path,
targetPlatform: targetPlatform,
);
Expand Down
17 changes: 16 additions & 1 deletion packages/flutter_tools/lib/src/devfs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,22 @@ class DevFS {
final AssetKind? kind = bundle.entries[archivePath]?.kind;
switch (kind) {
case AssetKind.shader:
final Future<DevFSContent?> pending = shaderCompiler.recompileShader(entry.content);
final Future<DevFSContent?> pending = (() async {
DevFSContent content = entry.content;
if (entry.transformers.isNotEmpty) {
final DevFSContent? transformed = await _assetTransformer.retransformAsset(
inputAssetKey: archivePath,
inputAssetContent: content,
transformerEntries: entry.transformers,
workingDirectory: rootDirectory.path,
);
if (transformed == null) {
return null;
}
content = transformed;
}
return shaderCompiler.recompileShader(content);
})();
pendingAssetBuilds.add(pending);
pending.then((DevFSContent? content) {
if (content == null) {
Expand Down
46 changes: 6 additions & 40 deletions packages/flutter_tools/lib/src/flutter_manifest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class FlutterManifest {
required Logger logger,
List<AssetsEntry>? assets,
List<Font>? fonts,
List<Uri>? shaders,
List<AssetsEntry>? shaders,
List<DeferredComponent>? deferredComponents,
bool removeDependencies = false,
}) {
Expand All @@ -107,9 +107,9 @@ class FlutterManifest {
}

if (shaders != null && shaders.isNotEmpty) {
copy._flutterDescriptor['shaders'] = YamlList.wrap(
shaders.map((Uri uri) => uri.toString()).toList(),
);
copy._flutterDescriptor['shaders'] = YamlList.wrap(<Object?>[
for (final AssetsEntry shader in shaders) shader.descriptor,
]);
}

if (deferredComponents != null && deferredComponents.isNotEmpty) {
Expand Down Expand Up @@ -426,31 +426,7 @@ class FlutterManifest {
return fonts;
}

late final List<Uri> shaders = _extractAssetUris('shaders', 'Shader');

List<Uri> _extractAssetUris(String key, String singularName) {
if (!_flutterDescriptor.containsKey(key)) {
return <Uri>[];
}

final items = _flutterDescriptor[key] as List<Object?>?;
if (items == null) {
return const <Uri>[];
}
final results = <Uri>[];
for (final Object? item in items) {
if (item is! String || item == '') {
_logger.printError('$singularName manifest contains a null or empty uri.');
continue;
}
try {
results.add(Uri.parse(item));
} on FormatException {
_logger.printError('$singularName manifest contains invalid uri: $item.');
}
}
return results;
}
late final List<AssetsEntry> shaders = _computeAssets(_flutterDescriptor['shaders']);

/// Whether localization Dart files should be generated.
late final generateLocalizations = _flutterDescriptor['generate'] == true;
Expand Down Expand Up @@ -567,17 +543,7 @@ void _validateFlutter(YamlMap? yaml, List<String> errors) {
case 'assets':
errors.addAll(_validateAssets(yamlValue));
case 'shaders':
if (yamlValue is! YamlList) {
errors.add(
'Expected "$yamlKey" to be a list, but got $yamlValue (${yamlValue.runtimeType}).',
);
} else if (yamlValue.isEmpty) {
break;
} else if (yamlValue[0] is! String) {
errors.add(
'Expected "$yamlKey" to be a list of strings, but the first element is $yamlValue (${yamlValue.runtimeType}).',
);
}
errors.addAll(_validateAssets(yamlValue));
Comment thread
Laurie-Lin marked this conversation as resolved.
Resolved
case 'fonts':
if (yamlValue is! YamlList) {
errors.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void main() {
rootProject.replacePubspec(
rootProject.manifest.copyWith(
logger: logger,
shaders: <Uri>[Uri(host: 'Random')],
shaders: <AssetsEntry>[AssetsEntry(uri: Uri.parse('Random'))],
),
);
expect(previewManifest.shouldRegeneratePubspec(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,134 @@ flutter:
},
);

testUsingContext(
'transforms shaders declared with transformers before compilation',
() async {
Cache.flutterRoot = Cache.defaultFlutterRoot(
platform: globals.platform,
fileSystem: fileSystem,
userMessages: UserMessages(),
);

final environment = Environment.test(
fileSystem.currentDirectory,
processManager: globals.processManager,
artifacts: Artifacts.test(),
fileSystem: fileSystem,
logger: logger,
platform: globals.platform,
defines: <String, String>{kBuildMode: BuildMode.debug.cliName},
);

final Artifacts artifacts = Artifacts.test();
final String impellercPath = artifacts.getHostArtifact(HostArtifact.impellerc).path;
fileSystem.file(impellercPath).createSync(recursive: true);

fileSystem.file('pubspec.yaml')
..createSync()
..writeAsStringSync('''
name: example
flutter:
shaders:
- path: shaders/test.frag
transformers:
- package: my_capitalizer_transformer
''');

writePackageConfigFiles(directory: globals.fs.currentDirectory, mainLibName: 'example');

fileSystem.file('shaders/test.frag')
..createSync(recursive: true)
..writeAsStringSync('abc');

await const CopyAssets().build(environment);

expect(logger.errorText, isEmpty);
expect(globals.processManager, hasNoRemainingExpectations);
expect(
fileSystem.file('${environment.buildDir.path}/flutter_assets/shaders/test.frag'),
exists,
);
},
overrides: <Type, Generator>{
Logger: () => logger,
FileSystem: () => fileSystem,
Platform: () => FakePlatform(),
ProcessManager: () {
final Artifacts artifacts = Artifacts.test();
final String impellercPath = artifacts.getHostArtifact(HostArtifact.impellerc).path;

return FakeProcessManager.list(<FakeCommand>[
FakeCommand(
command: <Pattern>[
artifacts.getArtifactPath(Artifact.engineDartBinary),
'run',
'my_capitalizer_transformer',
RegExp('--input=.*'),
RegExp('--output=.*'),
],
onRun: (List<String> args) {
final ArgResults parsedArgs =
(ArgParser()
..addOption('input')
..addOption('output'))
.parse(args);

final File input = fileSystem.file(parsedArgs['input'] as String);
expect(input, exists);
expect(input.readAsStringSync(), 'abc');

fileSystem.file(parsedArgs['output'])
..createSync(recursive: true)
..writeAsStringSync('ABC');
},
),
FakeCommand(
command: <Pattern>[
impellercPath,
'--sksl',
'--runtime-stage-gles',
'--runtime-stage-gles3',
'--runtime-stage-vulkan',
'--iplr',
RegExp(r'--sl=.*/flutter_assets/shaders/test\.frag$'),
RegExp(r'--spirv=.*/flutter_assets/shaders/test\.frag\.spirv$'),
RegExp(r'--input=.*/flutter_assets/shaders/test\.frag\.transformed$'),
'--input-type=frag',
RegExp(r'--include=.*/flutter_assets/shaders$'),
RegExp(r'--include=.*/shader_lib$'),
],
onRun: (List<String> args) {
String? inputPath;
String? outputPath;
String? outputSpirvPath;
for (final String arg in args) {
if (arg.startsWith('--input=')) {
inputPath = arg.substring('--input='.length);
} else if (arg.startsWith('--sl=')) {
outputPath = arg.substring('--sl='.length);
} else if (arg.startsWith('--spirv=')) {
outputSpirvPath = arg.substring('--spirv='.length);
}
}

expect(inputPath, isNotNull);
expect(outputPath, isNotNull);
expect(outputSpirvPath, isNotNull);

final File input = fileSystem.file(inputPath!);
expect(input, exists);
expect(input.readAsStringSync(), 'ABC');

fileSystem.file(outputPath!).createSync(recursive: true);
fileSystem.file(outputSpirvPath!).createSync(recursive: true);
},
),
]);
},
},
);

testUsingContext(
'exits tool if an asset transformation fails',
() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,30 @@ flutter:
expect(entry.platforms, isEmpty);
});

testWithoutContext('FlutterManifest parses shaders with transformers', () async {
const manifest = '''
name: test
flutter:
shaders:
- path: shaders/test.frag
transformers:
- package: my_transformer
args: ["--foo", "bar"]
''';

final FlutterManifest flutterManifest = FlutterManifest.createFromString(
manifest,
logger: logger,
)!;

expect(flutterManifest.shaders, hasLength(1));
final AssetsEntry shader = flutterManifest.shaders.single;
expect(shader.uri.path, 'shaders/test.frag');
expect(shader.transformers, hasLength(1));
expect(shader.transformers.single.package, 'my_transformer');
expect(shader.transformers.single.args, <String>['--foo', 'bar']);
});

testWithoutContext('FlutterManifest.copyWith generates a valid manifest', () async {
const manifest = '''
name: test
Expand Down Expand Up @@ -1673,7 +1697,7 @@ flutter:
FontAsset(Uri(path: 'assetUri'), weight: 100, style: 'normal'),
]),
],
shaders: <Uri>[Uri(path: 'shaderUri')],
shaders: <AssetsEntry>[AssetsEntry(uri: Uri(path: 'shaderUri'))],
deferredComponents: <DeferredComponent>[
DeferredComponent(
name: 'deferredComponent',
Expand Down