interim Android fixes

This commit is contained in:
Nick Fisher
2023-10-01 20:44:23 +08:00
parent 762f9abde0
commit cf5ab5848d
6 changed files with 101 additions and 50 deletions

View File

@@ -3,6 +3,8 @@
extern "C" { extern "C" {
#include "PolyvoxFilamentFFIApi.h"
void* get_native_window_from_surface( void* get_native_window_from_surface(
jobject surface, jobject surface,
JNIEnv* env JNIEnv* env
@@ -12,8 +14,8 @@ extern "C" {
} }
// this does nothing, but we need it for JNA to return the correct pointer // this does nothing, but we need it for JNA to return the correct pointer
void* const make_render_callback_fn_pointer(void (*callback)(void*)) { FilamentRenderCallback make_render_callback_fn_pointer(FilamentRenderCallback callback) {
return (void* const)callback; return callback;
} }
} }

View File

@@ -31,10 +31,18 @@ interface RenderCallback : Callback {
interface FilamentInterop : Library { 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_render_callback_fn_pointer(renderCallback:RenderCallback) : Pointer
fun make_resource_loader(loadResourceFromOwner: LoadFilamentResourceFromOwner, freeResource: FreeFilamentResourceFromOwner, owner:Pointer?) : 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?)
} }

View File

@@ -17,8 +17,11 @@ class HotReloadPathHelper {
}.sortedBy { }.sortedBy {
it.lastModified() it.lastModified()
}.toList() }.toList()
if(files.size > 0) if(files.size > 0) {
Log.v("polyvox_filament", "Using hot reloaded asset at ${files.last().path}")
return files.last().path; return files.last().path;
}
Log.v("polyvox_filament", "No hot reloaded asset found.")
return null; return null;
} }
} }

View File

@@ -30,8 +30,29 @@ import java.util.concurrent.Executors
typealias EntityId = Int 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 */ /** PolyvoxFilamentPlugin */
class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, LoadFilamentResourceFromOwner, FreeFilamentResourceFromOwner, RenderCallback { class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, LoadFilamentResourceFromOwner, FreeFilamentResourceFromOwner {
companion object { companion object {
const val CHANNEL_NAME = "app.polyvox.filament/event" 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?) { override fun freeResourceFromOwner(rb: ResourceBuffer, owner: Pointer?) {
_resources.remove(rb.id) _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) _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 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 resultList = listOf(_surfaceTextureEntry!!.id(), Pointer.nativeValue(nativeWindow), null )
val resourceLoader = _lib.make_resource_loader(this, this, Pointer(0))
result.success(resultList) result.success(resultList)
} }
"getResourceLoaderWrapper" -> { "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)) result.success(Pointer.nativeValue(resourceLoader))
} }
"getRenderCallback" -> { "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)) result.success(listOf(Pointer.nativeValue(renderCallbackFnPointer), 0))
} }
"destroyTexture" -> { "destroyTexture" -> {

View File

@@ -264,9 +264,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
}, "${_frustumCulling ? "Disable" : "Enable"} frustum culling")); }, "${_frustumCulling ? "Disable" : "Enable"} frustum culling"));
return Stack(children: [ return Stack(children: [
Positioned( Positioned.fill(
bottom: 100,
height:768, width:1024,
child: FilamentGestureDetector( child: FilamentGestureDetector(
showControlOverlay: true, showControlOverlay: true,
controller: _filamentController, controller: _filamentController,
@@ -278,6 +276,8 @@ class _ExampleWidgetState extends State<ExampleWidget> {
left: 0, left: 0,
right: 0, right: 0,
height: 200, height: 200,
child: Container(
color: Colors.white,
child: SingleChildScrollView( child: SingleChildScrollView(
child: Wrap(children: children child: Wrap(children: children
// _item(24 () async { 'rotate by pi around Y axis'), // _item(24 () async { 'rotate by pi around Y axis'),
@@ -311,7 +311,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
// _item(30 () async { 'remove light'), // _item(30 () async { 'remove light'),
// _item(31 () async { 'clear all lights'), // _item(31 () async { 'clear all lights'),
// _item(32 () async { 'set camera model matrix'), // _item(32 () async { 'set camera model matrix'),
))), )))),
]); ]);
} }
} }

View File

@@ -9,12 +9,14 @@
#include "PolyvoxFilamentApi.h" #include "PolyvoxFilamentApi.h"
typedef int32_t EntityId; 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* 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_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 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 destroy_filament_viewer_ffi(void* const viewer);
FLUTTER_PLUGIN_EXPORT void render_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_rendering_ffi(void* const viewer, bool rendering);
FLUTTER_PLUGIN_EXPORT void set_frame_interval_ffi(float frameInterval); 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); FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection_ffi(void* const viewer, const uint32_t width, const uint32_t height, const float scaleFactor);