From 423f56335023a5d1538ef0348b57099f1c296dbf Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Mon, 2 Oct 2023 13:51:35 +0800 Subject: [PATCH] working on Android (Flutter 3.15.0-15.2.pre only) --- README.md | 1 + .../app/polyvox/filament/FilamentInterop.kt | 4 +- .../polyvox/filament/PolyvoxFilamentPlugin.kt | 48 +++++++------------ example/android/app/build.gradle | 2 +- 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 92fe1686..095e9162 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ All: (even though headers are under the iOS directory these are used across all platforms). Android: +- IMPORTANT - current version only works on Flutter 3.15.0-15.2.pre - build from Filament repository on Linux (build native, then build Android). Reminder that NDK >= 24 required. - need to specifically move imageio/png/tinyexr - if release build, then need to comment out -fno-exceptions diff --git a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt index 40450839..5d5e9693 100644 --- a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt +++ b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt @@ -31,7 +31,7 @@ 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; @@ -39,7 +39,7 @@ fun create_swap_chain_ffi(vieer:Pointer?, surface:Pointer?, width:Int, height:In 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_filament_viewer(context:Pointer?, platform:Pointer?, loader: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) diff --git a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt index fa394eb0..38082027 100644 --- a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt +++ b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt @@ -25,10 +25,6 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.view.TextureRegistry.SurfaceTextureEntry import java.io.File import java.util.* -import java.util.concurrent.Executors - - -typealias EntityId = Int class LoadFilamentResourceFromOwnerImpl(plugin:PolyvoxFilamentPlugin) : LoadFilamentResourceFromOwner { var plugin = plugin @@ -48,6 +44,10 @@ class RenderCallbackImpl(plugin:PolyvoxFilamentPlugin) : RenderCallback { var plugin = plugin override fun renderCallback(owner:Pointer?) { plugin.renderCallback(); + + if(!plugin._surface!!.isValid) { + Log.e("ERR", "ERR", null) + } } } @@ -67,14 +67,12 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo private lateinit var _lib : FilamentInterop - private var _surfaceTexture: SurfaceTexture? = null + var _surfaceTexture: SurfaceTexture? = null private var _surfaceTextureEntry: SurfaceTextureEntry? = null - private var _surface: Surface? = null + var _surface: Surface? = null private lateinit var activity:Activity - private val executor = Executors.newFixedThreadPool(1); - override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { this.flutterPluginBinding = flutterPluginBinding channel = MethodChannel(flutterPluginBinding.binaryMessenger, CHANNEL_NAME) @@ -103,9 +101,12 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo if (hotReloadPath != null) { data = File(hotReloadPath).readBytes() } else { + Log.i("polyvox_filament", "Loading resource from main asset bundle") + val assetManager: AssetManager = activity.assets try { data = assetManager.open(key).readBytes() + Log.i("polyvox_filament", "Loaded ${data.size} bytes") } catch (e:Exception) { Log.e("polyvox_filament", "Failed to open asset at ${path}", null) } @@ -147,10 +148,9 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { Log.e("polyvox_filament", call.method, null) when (call.method) { - "getSharedContext" -> { - val nativeWindow = _lib.get_native_window_from_surface(_surface!! as Object, JNIEnv.CURRENT) - result.success(Pointer.nativeValue(nativeWindow)) - } + "getSharedContext" -> { + result.success(null) + } "createTexture" -> { if(_surfaceTextureEntry != null) { result.error("TEXTURE_EXISTS", "Texture already exist. Make sure you call destroyTexture first", null) @@ -160,10 +160,10 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo val width = args[0] as Double val height = args[1] as Double if(width <1 || height < 1) { - result.error("DIMENSION_MISMATCH","Both dimensions must be greater than zero", null); + result.error("DIMENSION_MISMATCH","Both dimensions must be greater than zero (you provided $width x $height)", null); return; } - Log.i("polyvox_filament", "Creating texture of size ${width}x${height}"); + Log.i("polyvox_filament", "Creating Surface Texture of size ${width}x${height}"); _surfaceTextureEntry = flutterPluginBinding.textureRegistry.createSurfaceTexture() _surfaceTexture = _surfaceTextureEntry!!.surfaceTexture(); @@ -171,24 +171,12 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo _surface = Surface(_surfaceTexture) - if(!_surface!!.isValid) { - Log.e("ERR", "ERR", null) - } + 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 ) result.success(resultList) diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 443353fe..7dec6266 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -28,7 +28,7 @@ apply plugin: 'kotlin-android-extensions' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 31 + compileSdkVersion 33 ndkVersion "25.2.9519653" compileOptions {