use void callback for setBackgroundImage to correctly free pointer after dispatch

This commit is contained in:
Nick Fisher
2024-04-20 13:46:10 +08:00
parent 49229f8c88
commit 15882891e2
4 changed files with 12 additions and 6 deletions

View File

@@ -36,7 +36,7 @@ extern "C"
FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection_ffi(void *const viewer, const uint32_t width, const uint32_t height, const float scaleFactor, void (*onComplete)()); FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection_ffi(void *const viewer, const uint32_t width, const uint32_t height, const float scaleFactor, void (*onComplete)());
FLUTTER_PLUGIN_EXPORT void set_background_color_ffi(void *const viewer, const float r, const float g, const float b, const float a); FLUTTER_PLUGIN_EXPORT void set_background_color_ffi(void *const viewer, const float r, const float g, const float b, const float a);
FLUTTER_PLUGIN_EXPORT void clear_background_image_ffi(void *const viewer); FLUTTER_PLUGIN_EXPORT void clear_background_image_ffi(void *const viewer);
FLUTTER_PLUGIN_EXPORT void set_background_image_ffi(void *const viewer, const char *path, bool fillHeight); FLUTTER_PLUGIN_EXPORT void set_background_image_ffi(void *const viewer, const char *path, bool fillHeight, void (*onComplete)());
FLUTTER_PLUGIN_EXPORT void set_background_image_position_ffi(void *const viewer, float x, float y, bool clamp); FLUTTER_PLUGIN_EXPORT void set_background_image_position_ffi(void *const viewer, float x, float y, bool clamp);
FLUTTER_PLUGIN_EXPORT void set_tone_mapping_ffi(void *const viewer, int toneMapping); FLUTTER_PLUGIN_EXPORT void set_tone_mapping_ffi(void *const viewer, int toneMapping);
FLUTTER_PLUGIN_EXPORT void set_bloom_ffi(void *const viewer, float strength); FLUTTER_PLUGIN_EXPORT void set_bloom_ffi(void *const viewer, float strength);

View File

@@ -307,9 +307,12 @@ FLUTTER_PLUGIN_EXPORT void clear_background_image_ffi(void *const viewer) {
FLUTTER_PLUGIN_EXPORT void set_background_image_ffi(void *const viewer, FLUTTER_PLUGIN_EXPORT void set_background_image_ffi(void *const viewer,
const char *path, const char *path,
bool fillHeight) { bool fillHeight, void (*callback)()) {
std::packaged_task<void()> lambda( std::packaged_task<void()> lambda(
[=] { set_background_image(viewer, path, fillHeight); }); [=] {
set_background_image(viewer, path, fillHeight);
callback();
});
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
} }
FLUTTER_PLUGIN_EXPORT void set_background_image_position_ffi(void *const viewer, FLUTTER_PLUGIN_EXPORT void set_background_image_position_ffi(void *const viewer,

View File

@@ -531,8 +531,10 @@ class FilamentControllerFFI extends FilamentController {
throw Exception("No viewer available, ignoring"); throw Exception("No viewer available, ignoring");
} }
final pathPtr = path.toNativeUtf8().cast<Char>(); final pathPtr = path.toNativeUtf8().cast<Char>();
await _withVoidCallback((cb) {
set_background_image_ffi(_viewer!, pathPtr, fillHeight, cb);
});
set_background_image_ffi(_viewer!, pathPtr, fillHeight);
allocator.free(pathPtr); allocator.free(pathPtr);
} }

View File

@@ -1147,13 +1147,14 @@ external void clear_background_image_ffi(
); );
@ffi.Native< @ffi.Native<
ffi.Void Function( ffi.Void Function(ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Char>, ffi.Bool)>( ffi.Bool, ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>>)>(
symbol: 'set_background_image_ffi', assetId: 'flutter_filament_plugin') symbol: 'set_background_image_ffi', assetId: 'flutter_filament_plugin')
external void set_background_image_ffi( external void set_background_image_ffi(
ffi.Pointer<ffi.Void> viewer, ffi.Pointer<ffi.Void> viewer,
ffi.Pointer<ffi.Char> path, ffi.Pointer<ffi.Char> path,
bool fillHeight, bool fillHeight,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> onComplete,
); );
@ffi.Native< @ffi.Native<