Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Third part of soft transition to async FragmentProgram.fromAsset #34932

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions lib/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4039,14 +4039,16 @@ class FragmentProgram extends NativeFieldWrapperClass1 {
/// The asset must be a file produced as the output of the `impellerc`
/// compiler. The constructed object should then be reused via the [shader]
/// method to create [Shader] objects that can be used by [Shader.paint].
static FragmentProgram fromAsset(String assetKey) {
static Future<FragmentProgram> fromAsset(String assetKey) {
FragmentProgram? program = _shaderRegistry[assetKey]?.target;
if (program == null) {
program = FragmentProgram._fromAsset(assetKey);
_shaderRegistry[assetKey] = WeakReference<FragmentProgram>(program);
if (program != null) {
return Future<FragmentProgram>.value(program);
}

return program;
return Future<FragmentProgram>.microtask(() {
final FragmentProgram program = FragmentProgram._fromAsset(assetKey);
_shaderRegistry[assetKey] = WeakReference<FragmentProgram>(program);
return program;
});
}

// TODO(zra): This is part of a soft transition of the framework to this
Expand Down
2 changes: 1 addition & 1 deletion lib/web_ui/lib/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ class ImageDescriptor {
}

class FragmentProgram {
static FragmentProgram fromAsset(String assetKey) {
static Future<FragmentProgram> fromAsset(String assetKey) {
throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.');
}

Expand Down
24 changes: 12 additions & 12 deletions testing/dart/fragment_shader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'shader_test_file_utils.dart';

void main() async {
test('simple shader renders correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'functions.frag.iplr',
);
final Shader shader = program.shader(
Expand All @@ -25,7 +25,7 @@ void main() async {
});

test('blue-green image renders green', () async {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'blue_green_sampler.frag.iplr',
);
final Image blueGreenImage = await _createBlueGreenImage();
Expand All @@ -39,7 +39,7 @@ void main() async {
});

test('shader with uniforms renders correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'uniforms.frag.iplr',
);

Expand All @@ -65,7 +65,7 @@ void main() async {
});

test('The ink_sparkle shader is accepted', () async {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'ink_sparkle.frag.iplr',
);
final Shader shader = program.shader(
Expand All @@ -79,7 +79,7 @@ void main() async {
});

test('Uniforms are sorted correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'uniforms_sorted.frag.iplr',
);

Expand All @@ -97,7 +97,7 @@ void main() async {
test('fromAsset throws an exception on invalid assetKey', () async {
bool throws = false;
try {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
await FragmentProgram.fromAsset(
'<invalid>',
);
} catch (e) {
Expand All @@ -109,7 +109,7 @@ void main() async {
test('fromAsset throws an exception on invalid data', () async {
bool throws = false;
try {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
await FragmentProgram.fromAsset(
'DashInNooglerHat.jpg',
);
} catch (e) {
Expand All @@ -119,7 +119,7 @@ void main() async {
});

test('fromAsset accepts a shader with no uniforms', () async {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'no_uniforms.frag.iplr',
);
final Shader shader = program.shader();
Expand All @@ -143,7 +143,7 @@ void main() async {
_expectIplrShadersRenderGreen(iplrSupportedOpShaders);

test('Equality depends on floatUniforms', () async {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'simple.frag.iplr',
);
final Float32List ones = Float32List.fromList(<double>[1]);
Expand All @@ -165,10 +165,10 @@ void main() async {
});

test('Equality depends on data', () async {
final FragmentProgram programA = await FragmentProgram.fromAssetAsync(
final FragmentProgram programA = await FragmentProgram.fromAsset(
'simple.frag.iplr',
);
final FragmentProgram programB = await FragmentProgram.fromAssetAsync(
final FragmentProgram programB = await FragmentProgram.fromAsset(
'uniforms.frag.iplr',
);
final Shader a = programA.shader();
Expand Down Expand Up @@ -242,7 +242,7 @@ Future<Map<String, FragmentProgram>> _loadShaderAssets(
.where((FileSystemEntity entry) => path.extension(entry.path) == ext),
(FileSystemEntity entry) async {
final String key = path.basenameWithoutExtension(entry.path);
out[key] = await FragmentProgram.fromAssetAsync(
out[key] = await FragmentProgram.fromAsset(
path.basename(entry.path),
);
},
Expand Down
7 changes: 6 additions & 1 deletion testing/dart/observatory/shader_reload_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ void main() {
test('simple iplr shader can be re-initialized', () async {
vms.VmService? vmService;
try {
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
final FragmentProgram program = await FragmentProgram.fromAsset(
'functions.frag.iplr',
);
final Shader shader = program.shader(
floatUniforms: Float32List.fromList(<double>[1]),
);
_use(shader);

final developer.ServiceProtocolInfo info = await developer.Service.getInfo();

Expand Down Expand Up @@ -48,3 +49,7 @@ void main() {
}
});
}

void _use(Shader shader) {

}