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

Commit 31dc3e7

Browse files
committed
Third part of soft transition to async FragmentProgram.fromAsset
1 parent a339093 commit 31dc3e7

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

lib/ui/painting.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,14 +4039,16 @@ class FragmentProgram extends NativeFieldWrapperClass1 {
40394039
/// The asset must be a file produced as the output of the `impellerc`
40404040
/// compiler. The constructed object should then be reused via the [shader]
40414041
/// method to create [Shader] objects that can be used by [Shader.paint].
4042-
static FragmentProgram fromAsset(String assetKey) {
4042+
static Future<FragmentProgram> fromAsset(String assetKey) {
40434043
FragmentProgram? program = _shaderRegistry[assetKey]?.target;
4044-
if (program == null) {
4045-
program = FragmentProgram._fromAsset(assetKey);
4046-
_shaderRegistry[assetKey] = WeakReference<FragmentProgram>(program);
4044+
if (program != null) {
4045+
return Future<FragmentProgram>.value(program);
40474046
}
4048-
4049-
return program;
4047+
return Future<FragmentProgram>.microtask(() {
4048+
final FragmentProgram program = FragmentProgram._fromAsset(assetKey);
4049+
_shaderRegistry[assetKey] = WeakReference<FragmentProgram>(program);
4050+
return program;
4051+
});
40504052
}
40514053

40524054
// TODO(zra): This is part of a soft transition of the framework to this

testing/dart/fragment_shader_test.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'shader_test_file_utils.dart';
1515

1616
void main() async {
1717
test('simple shader renders correctly', () async {
18-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
18+
final FragmentProgram program = await FragmentProgram.fromAsset(
1919
'functions.frag.iplr',
2020
);
2121
final Shader shader = program.shader(
@@ -25,7 +25,7 @@ void main() async {
2525
});
2626

2727
test('blue-green image renders green', () async {
28-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
28+
final FragmentProgram program = await FragmentProgram.fromAsset(
2929
'blue_green_sampler.frag.iplr',
3030
);
3131
final Image blueGreenImage = await _createBlueGreenImage();
@@ -39,7 +39,7 @@ void main() async {
3939
});
4040

4141
test('shader with uniforms renders correctly', () async {
42-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
42+
final FragmentProgram program = await FragmentProgram.fromAsset(
4343
'uniforms.frag.iplr',
4444
);
4545

@@ -65,7 +65,7 @@ void main() async {
6565
});
6666

6767
test('The ink_sparkle shader is accepted', () async {
68-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
68+
final FragmentProgram program = await FragmentProgram.fromAsset(
6969
'ink_sparkle.frag.iplr',
7070
);
7171
final Shader shader = program.shader(
@@ -79,7 +79,7 @@ void main() async {
7979
});
8080

8181
test('Uniforms are sorted correctly', () async {
82-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
82+
final FragmentProgram program = await FragmentProgram.fromAsset(
8383
'uniforms_sorted.frag.iplr',
8484
);
8585

@@ -97,7 +97,7 @@ void main() async {
9797
test('fromAsset throws an exception on invalid assetKey', () async {
9898
bool throws = false;
9999
try {
100-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
100+
final FragmentProgram program = await FragmentProgram.fromAsset(
101101
'<invalid>',
102102
);
103103
} catch (e) {
@@ -109,7 +109,7 @@ void main() async {
109109
test('fromAsset throws an exception on invalid data', () async {
110110
bool throws = false;
111111
try {
112-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
112+
final FragmentProgram program = await FragmentProgram.fromAsset(
113113
'DashInNooglerHat.jpg',
114114
);
115115
} catch (e) {
@@ -119,7 +119,7 @@ void main() async {
119119
});
120120

121121
test('fromAsset accepts a shader with no uniforms', () async {
122-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
122+
final FragmentProgram program = await FragmentProgram.fromAsset(
123123
'no_uniforms.frag.iplr',
124124
);
125125
final Shader shader = program.shader();
@@ -143,7 +143,7 @@ void main() async {
143143
_expectIplrShadersRenderGreen(iplrSupportedOpShaders);
144144

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

167167
test('Equality depends on data', () async {
168-
final FragmentProgram programA = await FragmentProgram.fromAssetAsync(
168+
final FragmentProgram programA = await FragmentProgram.fromAsset(
169169
'simple.frag.iplr',
170170
);
171-
final FragmentProgram programB = await FragmentProgram.fromAssetAsync(
171+
final FragmentProgram programB = await FragmentProgram.fromAsset(
172172
'uniforms.frag.iplr',
173173
);
174174
final Shader a = programA.shader();
@@ -242,7 +242,7 @@ Future<Map<String, FragmentProgram>> _loadShaderAssets(
242242
.where((FileSystemEntity entry) => path.extension(entry.path) == ext),
243243
(FileSystemEntity entry) async {
244244
final String key = path.basenameWithoutExtension(entry.path);
245-
out[key] = await FragmentProgram.fromAssetAsync(
245+
out[key] = await FragmentProgram.fromAsset(
246246
path.basename(entry.path),
247247
);
248248
},

testing/dart/observatory/shader_reload_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void main() {
1414
test('simple iplr shader can be re-initialized', () async {
1515
vms.VmService? vmService;
1616
try {
17-
final FragmentProgram program = await FragmentProgram.fromAssetAsync(
17+
final FragmentProgram program = await FragmentProgram.fromAsset(
1818
'functions.frag.iplr',
1919
);
2020
final Shader shader = program.shader(

0 commit comments

Comments
 (0)