Skip to content

Commit

Permalink
Hold on to dart-side refs for shader libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero committed Dec 11, 2023
1 parent 7f86292 commit 682ef43
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/gpu/lib/src/shader_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ base class ShaderLibrary extends NativeFieldWrapperClass1 {

ShaderLibrary._();

// Hold a Dart-side reference to shaders in the library as they're wrapped for
// the first time. This prevents the wrapper from getting prematurely
// destroyed.
Map<String, Shader> shaders_ = {};

Shader? operator [](String shaderName) {
// This `flutter_gpu` library isn't always registered as part of the builtin
// DartClassLibrary, and so we can't instantiate the Dart classes on the
// engine side.
// Providing a new wrapper to [_getShader] for wrapping the native
// counterpart (if it hasn't been wrapped already) is a hack to work around
// this.
return _getShader(shaderName, Shader._());
Shader? result = shaders_[shaderName];
if (result == null) {
result = _getShader(shaderName, Shader._());
if (result != null) {
shaders_[shaderName] = result;
}
}
return result;
}

@Native<Handle Function(Handle, Handle)>(
Expand Down

0 comments on commit 682ef43

Please sign in to comment.