-
I'm trying to create a My Kotlin code: class MainActivity : AppCompatActivity(), SurfaceHolder.Callback {
private var surfaceView: SurfaceView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
surfaceView = findViewById(R.id.surfaceView)
val holder = surfaceView!!.holder
holder.addCallback(this)
}
override fun surfaceCreated(holder: SurfaceHolder) {
val jni = JNILib()
println(str)
jni.initialize(holder.surface)
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
// TODO("Not yet implemented")
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
// TODO("Not yet implemented")
}
} rust code: extern "C" {
pub fn ANativeWindow_fromSurface(env: JNIEnv, surface: JObject) -> usize;
}
pub fn get_raw_window_handle(env: JNIEnv, surface: JObject) -> RawWindowHandle {
let a_native_window = unsafe {
ANativeWindow_fromSurface(env, surface)
};
let mut handle = AndroidHandle::empty();
handle.a_native_window = a_native_window as *mut c_void;
return RawWindowHandle::Android(handle)
}
static mut state: Option<State> = None;
#[no_mangle]
pub extern "system" fn Java_dwuggh_Scratchpad_JNILib_initialize(env: JNIEnv,
class: JClass,
surface: JObject) {
android_logger::init_once(
Config::default()
.with_min_level(log::Level::Trace) // limit log level
.with_tag("mytag") // logs will show under mytag tag
.with_filter( // configure messages for specific crate
FilterBuilder::new()
.parse("trace,hello::crate=error")
.build())
);
log::debug!("running!");
let handle = get_raw_window_handle(env, surface);
log::debug!("get handle!");
// AWindow is a wrapper for the handle, which implements HasRawWindowHandle(by directy return the handle)
let awindow = AWindow {
handle
};
unsafe {
state = Some(futures::executor::block_on(State::new_native(&awindow, [800, 600])));
log::debug!("get state!");
}
}
pub async fn new_native<W>(window: &W, window_size: [u32; 2]) -> State
where
W: raw_window_handle::HasRawWindowHandle {
let size = winit::dpi::PhysicalSize::new(window_size[0], window_size[1]);
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
log::debug!("initializing surface");
let surface: wgpu::Surface = unsafe { instance.create_surface(window) };
log::debug!("successfully initialize surface: {:?}", surface);
// ...
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm not doing any Android dev, so I'm not informed on the topic. In general, the |
Beta Was this translation helpful? Give feedback.
I'm not doing any Android dev, so I'm not informed on the topic. In general, the
wgpu::Surface
is an API primitive that controls whatever your system has to present things on screen. Your code doesn't look wrong to me :)