问题描述
目前,似乎所有的 Vulkan 教程和示例都在 Android 平台上使用 NativeActivity.我想知道我们是否可以在 Android 上将 Vulkan 与 Java Activity 一起使用?
Currently, it seems all the Vulkan tutorials and samples use NativeActivity on Android platform. I would like to know whether we can use Vulkan with Java Activity on Android?
推荐答案
是的,您可以将 Vulkan 与您自己的 Activity 子类一起使用.由于 Android 没有针对 Vulkan 的 Java 语言绑定,因此您需要使用 JNI 或第三方 Java Vulkan 库(它只是为您执行 JNI).
Yes, you can use Vulkan with your own Activity subclass. Because Android doesn't have Java-language bindings for Vulkan, you'll need to use either JNI or a third-party Java Vulkan library (which is just doing the JNI for you).
您的视图层次结构将需要包含一个 SurfaceView,并且当您获得 Surfaceholder.Callback#surfaceChanged 回调时,您可以获得 Surface.如果你自己做 JNI,你可以调用 ANativeWindow_fromSurface从 Surface 获取 ANativeWindow,并使用它来创建您的 VkSurfaceKHR/VkSwapchainKHR.
Your View hierarchy will need to contain a SurfaceView, and when you get the Surfaceholder.Callback#surfaceChanged callback you can get the Surface. If you're doing the JNI yourself, you can call ANativeWindow_fromSurface to get the ANativeWindow from the Surface, and use that to create your VkSurfaceKHR/VkSwapchainKHR.
需要注意的一件事是在调用 VkAcquireNextImageKHR 时避免阻塞主 UI 线程.要么安排好你只在它不会长时间阻塞时调用它,要么把你的框架循环放在一个单独的线程上.
The one thing to be careful of is to avoid blocking the main UI thread when calling VkAcquireNextImageKHR. Either arrange so that you only call that when it won't block for long, or put your frame loop on a separate thread.
这篇关于我们可以在 Android 平台上将 Vulkan 与 Java Activity 一起使用吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!