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

Commit 8fc28ae

Browse files
author
Jonah Williams
authored
[ui] hold a strong reference to fragment program objects. (#49868)
The native engine needs to hold a strong reference to fragment programs. So the dart side holding a weak ref is more or less pointless
1 parent bd9acf3 commit 8fc28ae

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

lib/ui/painting.dart

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4465,35 +4465,31 @@ base class FragmentProgram extends NativeFieldWrapperClass1 {
44654465
// the same encoding here so that users can load assets with the same
44664466
// key they have written in the pubspec.
44674467
final String encodedKey = Uri(path: Uri.encodeFull(assetKey)).path;
4468-
final FragmentProgram? program = _shaderRegistry[encodedKey]?.target;
4468+
final FragmentProgram? program = _shaderRegistry[encodedKey];
44694469
if (program != null) {
44704470
return Future<FragmentProgram>.value(program);
44714471
}
44724472
return Future<FragmentProgram>.microtask(() {
44734473
final FragmentProgram program = FragmentProgram._fromAsset(encodedKey);
4474-
_shaderRegistry[encodedKey] = WeakReference<FragmentProgram>(program);
4474+
_shaderRegistry[encodedKey] = program;
44754475
return program;
44764476
});
44774477
}
44784478

44794479
// This is a cache of shaders that have been loaded by
4480-
// FragmentProgram.fromAsset. It holds weak references to the FragmentPrograms
4481-
// so that the case where an in-use program is requested again can be fast,
4482-
// but programs that are no longer referenced are not retained because of the
4483-
// cache.
4484-
static final Map<String, WeakReference<FragmentProgram>> _shaderRegistry =
4485-
<String, WeakReference<FragmentProgram>>{};
4480+
// FragmentProgram.fromAsset. It holds a strong reference to theFragmentPrograms
4481+
// The native engine will retain the resources associated with this shader
4482+
// program (PSO variants) until shutdown, so maintaining a strong reference
4483+
// here ensures we do not perform extra work if the dart object is continually
4484+
// re-initialized.
4485+
static final Map<String, FragmentProgram> _shaderRegistry =
4486+
<String, FragmentProgram>{};
44864487

44874488
static void _reinitializeShader(String assetKey) {
44884489
// If a shader for the asset isn't already registered, then there's no
44894490
// need to reinitialize it. The new shader will be loaded and initialized
44904491
// the next time the program access it.
4491-
final WeakReference<FragmentProgram>? programRef = _shaderRegistry[assetKey];
4492-
if (programRef == null) {
4493-
return;
4494-
}
4495-
4496-
final FragmentProgram? program = programRef.target;
4492+
final FragmentProgram? program = _shaderRegistry[assetKey];
44974493
if (program == null) {
44984494
return;
44994495
}

testing/dart/fragment_shader_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ void main() async {
5050
return;
5151
}
5252

53+
test('FragmentProgram objects are cached.', () async {
54+
final FragmentProgram programA = await FragmentProgram.fromAsset(
55+
'blue_green_sampler.frag.iplr',
56+
);
57+
final FragmentProgram programB = await FragmentProgram.fromAsset(
58+
'blue_green_sampler.frag.iplr',
59+
);
60+
61+
expect(identical(programA, programB), true);
62+
});
63+
5364
test('FragmentShader setSampler throws with out-of-bounds index', () async {
5465
final FragmentProgram program = await FragmentProgram.fromAsset(
5566
'blue_green_sampler.frag.iplr',

0 commit comments

Comments
 (0)