From cf5ab5848de287f2aac7010a8019b45dd4dd4fc6 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sun, 1 Oct 2023 20:44:23 +0800 Subject: [PATCH] interim Android fixes --- android/src/main/cpp/FilamentAndroid.cpp | 6 +- .../app/polyvox/filament/FilamentInterop.kt | 14 +++- .../polyvox/filament/HotReloadPathHelper.kt | 7 +- .../polyvox/filament/PolyvoxFilamentPlugin.kt | 50 +++++++++++-- example/lib/main.dart | 72 +++++++++---------- ios/include/PolyvoxFilamentFFIApi.h | 2 + 6 files changed, 101 insertions(+), 50 deletions(-) diff --git a/android/src/main/cpp/FilamentAndroid.cpp b/android/src/main/cpp/FilamentAndroid.cpp index 82cebc06..abc96ac1 100644 --- a/android/src/main/cpp/FilamentAndroid.cpp +++ b/android/src/main/cpp/FilamentAndroid.cpp @@ -3,6 +3,8 @@ extern "C" { + #include "PolyvoxFilamentFFIApi.h" + void* get_native_window_from_surface( jobject surface, JNIEnv* env @@ -12,8 +14,8 @@ extern "C" { } // this does nothing, but we need it for JNA to return the correct pointer - void* const make_render_callback_fn_pointer(void (*callback)(void*)) { - return (void* const)callback; + FilamentRenderCallback make_render_callback_fn_pointer(FilamentRenderCallback callback) { + return callback; } } diff --git a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt index a1324e1e..40450839 100644 --- a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt +++ b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt @@ -31,10 +31,18 @@ interface RenderCallback : Callback { interface FilamentInterop : Library { -fun get_native_window_from_surface(surface:Object, env:JNIEnv) : Pointer?; +fun get_native_window_from_surface(surface:Object, env:JNIEnv) : Pointer; fun make_render_callback_fn_pointer(renderCallback:RenderCallback) : Pointer fun make_resource_loader(loadResourceFromOwner: LoadFilamentResourceFromOwner, freeResource: FreeFilamentResourceFromOwner, owner:Pointer?) : Pointer; -fun create_filament_viewer_ffi(context:Pointer?, platform:Pointer?, loader:Pointer?, rc:Pointer?, rco:Pointer?) : Pointer; - +fun create_filament_viewer_ffi(context:Pointer, platform:Pointer, loader:Pointer, rc:Pointer, rco:Pointer) : Pointer; +fun create_swap_chain_ffi(vieer:Pointer?, surface:Pointer?, width:Int, height:Int) +fun set_background_color_ffi(viewer: Pointer?, r: Float, g: Float, b: Float, a: Float) +fun update_viewport_and_camera_projection_ffi(viewer: Pointer?, width: Int, height: Int, scale_factor: Float) +fun render_ffi(viewer: Pointer?) +fun create_filament_viewer(context:Pointer?, platform:Pointer?, loader:Pointer?, rc:Pointer?, rco:Pointer?) : Pointer; +fun create_swap_chain(vieer:Pointer?, surface:Pointer?, width:Int, height:Int) +fun set_background_color(viewer: Pointer?, r: Float, g: Float, b: Float, a: Float) +fun update_viewport_and_camera_projection(viewer: Pointer?, width: Int, height: Int, scale_factor: Float) +fun render(viewer: Pointer?, u:Long, a:Pointer?, b:Pointer?, c:Pointer?) } diff --git a/android/src/main/kotlin/app/polyvox/filament/HotReloadPathHelper.kt b/android/src/main/kotlin/app/polyvox/filament/HotReloadPathHelper.kt index 282de021..feea71d9 100644 --- a/android/src/main/kotlin/app/polyvox/filament/HotReloadPathHelper.kt +++ b/android/src/main/kotlin/app/polyvox/filament/HotReloadPathHelper.kt @@ -17,8 +17,11 @@ class HotReloadPathHelper { }.sortedBy { it.lastModified() }.toList() - if(files.size > 0) - return files.last().path; + if(files.size > 0) { + Log.v("polyvox_filament", "Using hot reloaded asset at ${files.last().path}") + return files.last().path; + } + Log.v("polyvox_filament", "No hot reloaded asset found.") return null; } } diff --git a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt index 00cd9d4b..fa394eb0 100644 --- a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt +++ b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt @@ -30,8 +30,29 @@ import java.util.concurrent.Executors typealias EntityId = Int +class LoadFilamentResourceFromOwnerImpl(plugin:PolyvoxFilamentPlugin) : LoadFilamentResourceFromOwner { + var plugin = plugin + override fun loadResourceFromOwner(path: String?, owner: Pointer?): ResourceBuffer { + return plugin.loadResourceFromOwner(path, owner) + } +} + +class FreeFilamentResourceFromOwnerImpl(plugin:PolyvoxFilamentPlugin) : FreeFilamentResourceFromOwner { + var plugin = plugin + override fun freeResourceFromOwner(rb: ResourceBuffer, owner: Pointer?) { + plugin.freeResourceFromOwner(rb, owner) + } +} + +class RenderCallbackImpl(plugin:PolyvoxFilamentPlugin) : RenderCallback { + var plugin = plugin + override fun renderCallback(owner:Pointer?) { + plugin.renderCallback(); + } +} + /** PolyvoxFilamentPlugin */ -class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, LoadFilamentResourceFromOwner, FreeFilamentResourceFromOwner, RenderCallback { +class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, LoadFilamentResourceFromOwner, FreeFilamentResourceFromOwner { companion object { const val CHANNEL_NAME = "app.polyvox.filament/event" @@ -113,13 +134,12 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo } - override fun freeResourceFromOwner(rb: ResourceBuffer, owner: Pointer?) { _resources.remove(rb.id) } - override fun renderCallback(owner:Pointer?) { - + fun renderCallback() { + Log.e("polyvox_filament", "Rdner callacbk", null) } @@ -151,18 +171,34 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo _surface = Surface(_surfaceTexture) + if(!_surface!!.isValid) { + Log.e("ERR", "ERR", null) + } + val nativeWindow = _lib.get_native_window_from_surface(_surface!! as Object, JNIEnv.CURRENT) + val resourceLoader = _lib.make_resource_loader(LoadFilamentResourceFromOwnerImpl(this), FreeFilamentResourceFromOwnerImpl(this), Pointer(0)) + val renderCallbackFnPointer = _lib.make_render_callback_fn_pointer(RenderCallbackImpl(this)) + + val viewer = _lib.create_filament_viewer(nativeWindow, resourceLoader,Pointer(0),renderCallbackFnPointer,Pointer(0)) + _lib.create_swap_chain(viewer, nativeWindow, width.toInt(),height.toInt()) + _lib.update_viewport_and_camera_projection(viewer, width.toInt(), height.toInt(), 1.0f) + _lib.set_background_color(viewer, 1.0f, 1.0f, 0.0f, 1.0f) + _lib.render(viewer, 0, Pointer(0),Pointer(0),Pointer(0)) + _lib.render(viewer, 0, Pointer(0),Pointer(0),Pointer(0)) + _lib.render(viewer, 0, Pointer(0),Pointer(0),Pointer(0)) + _lib.render(viewer, 0, Pointer(0),Pointer(0),Pointer(0)) + val resultList = listOf(_surfaceTextureEntry!!.id(), Pointer.nativeValue(nativeWindow), null ) - val resourceLoader = _lib.make_resource_loader(this, this, Pointer(0)) + result.success(resultList) } "getResourceLoaderWrapper" -> { - val resourceLoader = _lib.make_resource_loader(this, this, Pointer(0)) + val resourceLoader = _lib.make_resource_loader(LoadFilamentResourceFromOwnerImpl(this), FreeFilamentResourceFromOwnerImpl(this), Pointer(0)) result.success(Pointer.nativeValue(resourceLoader)) } "getRenderCallback" -> { - val renderCallbackFnPointer = _lib.make_render_callback_fn_pointer(this) + val renderCallbackFnPointer = _lib.make_render_callback_fn_pointer(RenderCallbackImpl(this)) result.success(listOf(Pointer.nativeValue(renderCallbackFnPointer), 0)) } "destroyTexture" -> { diff --git a/example/lib/main.dart b/example/lib/main.dart index a55bc474..b345225b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -65,7 +65,7 @@ class _ExampleWidgetState extends State { }); super.initState(); } - + Widget _item(void Function() onTap, String text) { return GestureDetector( onTap: onTap, @@ -264,9 +264,7 @@ class _ExampleWidgetState extends State { }, "${_frustumCulling ? "Disable" : "Enable"} frustum culling")); return Stack(children: [ - Positioned( - bottom: 100, - height:768, width:1024, + Positioned.fill( child: FilamentGestureDetector( showControlOverlay: true, controller: _filamentController, @@ -278,40 +276,42 @@ class _ExampleWidgetState extends State { left: 0, right: 0, height: 200, - child: SingleChildScrollView( - child: Wrap(children: children - // _item(24 () async { 'rotate by pi around Y axis'), - // _item(5 () async { 'load flight helmet'), + child: Container( + color: Colors.white, + child: SingleChildScrollView( + child: Wrap(children: children + // _item(24 () async { 'rotate by pi around Y axis'), + // _item(5 () async { 'load flight helmet'), - // _item(7 () async { 'set all weights to 1'), - // _item(8 () async { 'set all weights to 0'), - // _item(9 () async { 'play all animations'), - // _item(34 () async { 'play animation 0'), - // _item(34 () async { 'play animation 0 (noreplace)'), - // _item(35 () async { 'play animation 1'), - // _item(34 () async { 'play animation 0 (noreplace)'), - // _item(36 () async { 'play animation 2'), - // _item(34 () async { 'play animation 0 (noreplace)'), - // _item(36 () async { 'play animation 3'), - // _item(34 () async { 'play animation 3 (noreplace)'), - // _item(37 () async { 'stop animation 0'), + // _item(7 () async { 'set all weights to 1'), + // _item(8 () async { 'set all weights to 0'), + // _item(9 () async { 'play all animations'), + // _item(34 () async { 'play animation 0'), + // _item(34 () async { 'play animation 0 (noreplace)'), + // _item(35 () async { 'play animation 1'), + // _item(34 () async { 'play animation 0 (noreplace)'), + // _item(36 () async { 'play animation 2'), + // _item(34 () async { 'play animation 0 (noreplace)'), + // _item(36 () async { 'play animation 3'), + // _item(34 () async { 'play animation 3 (noreplace)'), + // _item(37 () async { 'stop animation 0'), - // _item(14 () async { 'set camera'), - // _item(15 () async { 'animate weights'), - // _item(16 () async { 'get target names'), - // _item(17 () async { 'get animation names'), - // _item(18 () async { 'pan left'), - // _item(19 () async { 'pan right'), - // _item(25 () async { - // Text(_vertical ? 'set horizontal' : 'set vertical')), - // _item(26 () async { 'set camera pos to 0,0,3'), - // _item(27 () async { 'toggle framerate'), - // _item(28 () async { 'set bg image pos'), - // _item(29 () async { 'add light'), - // _item(30 () async { 'remove light'), - // _item(31 () async { 'clear all lights'), - // _item(32 () async { 'set camera model matrix'), - ))), + // _item(14 () async { 'set camera'), + // _item(15 () async { 'animate weights'), + // _item(16 () async { 'get target names'), + // _item(17 () async { 'get animation names'), + // _item(18 () async { 'pan left'), + // _item(19 () async { 'pan right'), + // _item(25 () async { + // Text(_vertical ? 'set horizontal' : 'set vertical')), + // _item(26 () async { 'set camera pos to 0,0,3'), + // _item(27 () async { 'toggle framerate'), + // _item(28 () async { 'set bg image pos'), + // _item(29 () async { 'add light'), + // _item(30 () async { 'remove light'), + // _item(31 () async { 'clear all lights'), + // _item(32 () async { 'set camera model matrix'), + )))), ]); } } diff --git a/ios/include/PolyvoxFilamentFFIApi.h b/ios/include/PolyvoxFilamentFFIApi.h index 2319969c..a93a83d3 100644 --- a/ios/include/PolyvoxFilamentFFIApi.h +++ b/ios/include/PolyvoxFilamentFFIApi.h @@ -9,12 +9,14 @@ #include "PolyvoxFilamentApi.h" typedef int32_t EntityId; +typedef void (*FilamentRenderCallback)(void* const owner); FLUTTER_PLUGIN_EXPORT void* const create_filament_viewer_ffi(void* const context, void* const platform, const ResourceLoaderWrapper* const loader, void (*renderCallback)(void* const renderCallbackOwner), void* const renderCallbackOwner); FLUTTER_PLUGIN_EXPORT void create_swap_chain_ffi(void* const viewer, void* const surface, uint32_t width, uint32_t height); FLUTTER_PLUGIN_EXPORT void create_render_target_ffi(void* const viewer, uint32_t nativeTextureId, uint32_t width, uint32_t height); FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer_ffi(void* const viewer); FLUTTER_PLUGIN_EXPORT void render_ffi(void* const viewer); +FLUTTER_PLUGIN_EXPORT FilamentRenderCallback make_render_callback_fn_pointer(FilamentRenderCallback); FLUTTER_PLUGIN_EXPORT void set_rendering_ffi(void* const viewer, bool rendering); FLUTTER_PLUGIN_EXPORT void set_frame_interval_ffi(float frameInterval); FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection_ffi(void* const viewer, const uint32_t width, const uint32_t height, const float scaleFactor);