我在Internet上发现的任何资源,从创建本机活动并提供android_app->窗口来创建vkandroidsurfacekhr。因此,我只想知道我们可以有一个窗口管理器,该窗口管理器可以为表面创建提供此窗口。
从一个简单的java应用程序创建一个vkandroidsurfacekhr,您可以获得Android.view.view.view的实例,并执行本机呼叫到AnativeWindow_FromSurface(env,win)。注意视图及其子类能够从GPU中绘制3D内容,例如OpenGL和Vulkan。
我在第9100行,
的API中这样做 /**
* Get display handles for Android and AWT Canvas
* @param win - a java.awt.Canvas instance or a android.view.Surface
* @param displayHandles - return native surface handle
*
* @return true if all goes Ok.
*/
protected static native boolean getDisplayHandles0(Object win, long[] displayHandles);/*
#ifdef VK_USE_PLATFORM_ANDROID_KHR
ANativeWindow* window;
// Return the ANativeWindow associated with a Java Surface object,
// for interacting with it through native code. This acquires a reference
// on the ANativeWindow that is returned; be sure to use ANativeWindow_release()
// when done with it so that it doesn't leak.
window = ANativeWindow_fromSurface(env, win);
displayHandles[0] = reinterpret_cast<jlong>(window);
return JNI_TRUE;
#else
...
#endif
*/
}
我还以另一种方式(第10370行)从上述来源实现了这一点:
/**
*
* @see http://www.javaworld.com/article/2075263/core-java/embed-java-code-into-your-native-apps.html
*
* @param instance - Vulkan instance
* @param nativeWindow - instance of android.view.Surface or java.awt.Canvas
* @param pAllocatorHandle - native handle to a VkAllocationCallbacks
* @param pSurface
* @return
*/
protected static native int vkCreateWindowSurface0(long instance,
Object nativeWindow,
long pAllocatorHandle,
long[] pSurface,
long[] awtDrawingSurface);/*
VkAllocationCallbacks* pAllocator = reinterpret_cast<VkAllocationCallbacks*>(pAllocatorHandle);
VkInstance vkInstance = reinterpret_cast<VkInstance>(instance);
VkSurfaceKHR* _pSurface = new VkSurfaceKHR[1];
VkResult res = VkResult::VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
#ifdef VK_USE_PLATFORM_ANDROID_KHR
ANativeWindow* window = NULL;
window = ANativeWindow_fromSurface(env, nativeWindow);
if (window == NULL)
return VkResult::VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
VkAndroidSurfaceCreateInfoKHR info;
info.sType = VkStructureType::VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
info.pNext = NULL;
info.flags = 0;
info.window = window;
res = vkCreateAndroidSurfaceKHR(vkInstance, &info, pAllocator, _pSurface);
#else
...
#endif
if(res >= 0){
pSurface[0] = reinterpret_cast<jlong>(_pSurface[0]);
}else{
pSurface[0] = (jlong)0;
fprintf(stderr,"Failed to create Vulkan SurfaceKHR.");
}
delete[] _pSurface;
return res;
}