Harshad Untwale
Harshad Untwale

Reputation: 89

Do I have to create a native activity to get a window or I can provide one from window manager on android devices for vulkan applications?

whatever resources that I have found on internet, initialize from creating a native activity and providing android_app->window for creating vkAndroidSurfaceKHR. So, I just want to know can we have a window manager which supplies this window for surface creation.

Upvotes: 2

Views: 1439

Answers (1)

Alex Byrth
Alex Byrth

Reputation: 1482

To create a vkAndroidSurfaceKHR from a simple Java app, you get your instance of android.view.View and perform a native call to ANativeWindow_fromSurface(env, win). Note View and its subclasses are able to draw 3D content from GPU, as OpenGL and Vulkan.

I did this way in my api around line 9100,

 /**
 * 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
*/
}

I also implemented this in another way, around line 10370, from same above source:

/**
 * 
 * @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;
 }

Upvotes: 1

Related Questions