1

Currently, android Vulkan only supports NativeActivity, but is there any way we can use Java Activity and SurfaceView or any other view and pass Native through JNI to get NativeWindow handler.

I tried looking around and link my surface view but it didn't work for me, any sample code or example will be appreciated.

Hemant
  • 61
  • 5

1 Answers1

2

I don't know of any sample code off-hand, but if you have a SurfaceView you want to get the Surface from it, and from that you can get (in C) the ANativeWindow for creating the VkSurfaceKHR/VkSwapchainKHR. The sequence is something like:

Java: surface = surfaceView->getHolder()->getSurface();

Pass surface to a JNI call into C as a jobject.

C: window = ANativeWindow_fromSurface(env, jsurface);

That function is declared in the NDK android/native_window_jni.h header.

You'll want to register callbacks with the SurfaceView's SurfaceHolder and manage the window lifecycle (which is tied to the Activity lifecycle) correctly.

Jesse Hall
  • 6,441
  • 23
  • 29
  • On top of this, it's probably also worth looking at what glSurfaceView does (it's open source) to manage rendering on a thread other than the main UI thread and to handle any resizing, etc. In my projects I've created a vkSurfaceView which is like a simplified glSurfaceView. Really, google ought to supply a workable vkSurfaceView implementation. – Columbo Oct 31 '18 at 19:56
  • Hi, This SO answer shows basic setup needed to implement VulkanSurfaceView: https://stackoverflow.com/a/60706911/1418981 – Vlad Mar 16 '20 at 13:35