Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 06193802 authored by Jesse Hall's avatar Jesse Hall
Browse files

libvulkan: Implement new VkFence parameter in vkAcquireNextImageKHR

This parameter was added recently but wasn't hooked up. This adds a
new parameter to the vkAcquireImageANDROID extension function, and
plumbs the fence through from vkAcquireNextImageKHR to it.

This change also fixes some function signatures for API functions that
are implemented in the loader bottom rather than the driver. These
functions are only ever called through function pointers returned by
vkGet*ProcAddr, and therefore pass through a cast to
PFN_vkVoidFunction. So the compiler had no way to know they were
supposed to match a particular prototype, and couldn't issue an error
when they didn't. This change adds explicit static casts to the
expected function pointer type before reinterpret casting to the
generic function pointer type to enable compile errors.

Change-Id: I8a7e065502f783d5f2381b43c880644868234f8f
(cherry picked from commit f62f5de0c60212796b6d910cbd194c7002226264)
parent 40f7bf6b
Loading
Loading
Loading
Loading
+24 −5
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@
= Vulkan on Android Implementor's Guide =
:toc: right
:numbered:
:revnumber: 3
:revnumber: 4

This document is intended for GPU IHVs writing Vulkan drivers for Android, and OEMs integrating them for specific devices. It describes how a Vulkan driver interacts with the system, how GPU-specific tools should be installed, and Android-specific requirements.

@@ -99,7 +99,9 @@ When creating a gralloc-backed image, the +VkImageCreateInfo+ will have:
  .pQueueFamilyIndices = VkSwapChainCreateInfoWSI::pQueueFamilyIndices
----

+vkAcquireImageANDROID+ acquires ownership of a swapchain image and imports an externally-signalled native fence into an existing +VkSemaphore+ object:
+vkAcquireImageANDROID+ acquires ownership of a swapchain image and imports an
externally-signalled native fence into both an existing VkSemaphore object
and an existing VkFence object:

[source,c]
----
@@ -107,12 +109,26 @@ VkResult VKAPI vkAcquireImageANDROID(
    VkDevice            device,
    VkImage             image,
    int                 nativeFenceFd,
    VkSemaphore         semaphore
);
    VkSemaphore         semaphore,
    VkFence             fence
);
----

This function is called during +vkAcquireNextImageWSI+ to import a native fence into the +VkSemaphore+ object provided by the application. The driver may also use this opportunity to recognize and handle any external changes to the gralloc buffer state; many drivers won't need to do anything here. This call puts the +VkSemaphore+ into the same "pending" state as +vkQueueSignalSemaphore+, so queues can wait on the semaphore. The +VkSemaphore+ signals when the underlying native fence signals; if the fence has already signalled, then the semaphore will be in the signalled state when this function returns. The driver takes ownership of the fence fd and is responsible for closing it when the +VkSemaphore+ is destroyed, when a different native fence is imported, or any other condition that replaces the +VkSemaphore+'s underlying synchronization object. If +fenceFd+ is -1, the +VkSemaphore+ will be considered signalled immediately, but it can still be passed to +vkQueueWaitSemaphore+.
This function is called during +vkAcquireNextImageWSI+ to import a native
fence into the +VkSemaphore+ and +VkFence+ objects provided by the
application. Both semaphore and fence objects are optional in this call. The
driver may also use this opportunity to recognize and handle any external
changes to the gralloc buffer state; many drivers won't need to do anything
here. This call puts the +VkSemaphore+ and +VkFence+ into the same "pending"
state as +vkQueueSignalSemaphore+ and +vkQueueSubmit+ respectively, so queues
can wait on the semaphore and the application can wait on the fence. Both
objects become signalled when the underlying native fence signals; if the
native fence has already signalled, then the semaphore will be in the signalled
state when this function returns. The driver takes ownership of the fence fd
and is responsible for closing it when no longer needed. It must do so even if
neither a semaphore or fence object is provided, or even if
+vkAcquireImageANDROID+ fails and returns an error. If +fenceFd+ is -1, it
is as if the native fence was already signalled.

+vkQueueSignalReleaseImageANDROID+ prepares a swapchain image for external use, and creates a native fence and schedules it to be signalled when prior work on the queue has completed.

@@ -140,3 +156,6 @@ This will be called during +vkQueuePresentWSI+ on the provided queue. Effects ar
   * Replaced vkImportNativeFenceANDROID and vkQueueSignalNativeFenceANDROID
     with vkAcquireImageANDROID and vkQueueSignalReleaseImageANDROID, to allow
     drivers to known the ownership state of swapchain images.
. *2015-12-03*
   * Added a VkFence parameter to vkAcquireImageANDROID corresponding to the
     parameter added to vkAcquireNextImageKHR.
+34 −5
Original line number Diff line number Diff line
@@ -843,7 +843,9 @@ http://www.gnu.org/software/src-highlite -->
  .queueFamilyCount    = VkSwapChainCreateInfoWSI::queueFamilyCount
  .pQueueFamilyIndices = VkSwapChainCreateInfoWSI::pQueueFamilyIndices</pre>
</div></div>
<div class="paragraph"><p><span class="monospaced">vkAcquireImageANDROID</span> acquires ownership of a swapchain image and imports an externally-signalled native fence into an existing <span class="monospaced">VkSemaphore</span> object:</p></div>
<div class="paragraph"><p><span class="monospaced">vkAcquireImageANDROID</span> acquires ownership of a swapchain image and imports an
externally-signalled native fence into both an existing VkSemaphore object
and an existing VkFence object:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
@@ -853,10 +855,24 @@ http://www.gnu.org/software/src-highlite -->
    <span style="color: #008080">VkDevice</span>            device<span style="color: #990000">,</span>
    <span style="color: #008080">VkImage</span>             image<span style="color: #990000">,</span>
    <span style="color: #009900">int</span>                 nativeFenceFd<span style="color: #990000">,</span>
    VkSemaphore         semaphore
<span style="color: #990000">);</span>
    <span style="color: #008080">VkSemaphore</span>         semaphore<span style="color: #990000">,</span>
    VkFence             fence
<span style="color: #990000">);</span></tt></pre></div></div>
<div class="paragraph"><p>This function is called during <span class="monospaced">vkAcquireNextImageWSI</span> to import a native fence into the <span class="monospaced">VkSemaphore</span> object provided by the application. The driver may also use this opportunity to recognize and handle any external changes to the gralloc buffer state; many drivers won&#8217;t need to do anything here. This call puts the <span class="monospaced">VkSemaphore</span> into the same "pending" state as <span class="monospaced">vkQueueSignalSemaphore</span>, so queues can wait on the semaphore. The <span class="monospaced">VkSemaphore</span> signals when the underlying native fence signals; if the fence has already signalled, then the semaphore will be in the signalled state when this function returns. The driver takes ownership of the fence fd and is responsible for closing it when the <span class="monospaced">VkSemaphore</span> is destroyed, when a different native fence is imported, or any other condition that replaces the <span class="monospaced">VkSemaphore</span>'s underlying synchronization object. If <span class="monospaced">fenceFd</span> is -1, the <span class="monospaced">VkSemaphore</span> will be considered signalled immediately, but it can still be passed to <span class="monospaced">vkQueueWaitSemaphore</span>.</p></div>
<div class="paragraph"><p>This function is called during <span class="monospaced">vkAcquireNextImageWSI</span> to import a native
fence into the <span class="monospaced">VkSemaphore</span> and <span class="monospaced">VkFence</span> objects provided by the
application. Both semaphore and fence objects are optional in this call. The
driver may also use this opportunity to recognize and handle any external
changes to the gralloc buffer state; many drivers won&#8217;t need to do anything
here. This call puts the <span class="monospaced">VkSemaphore</span> and <span class="monospaced">VkFence</span> into the same "pending"
state as <span class="monospaced">vkQueueSignalSemaphore</span> and <span class="monospaced">vkQueueSubmit</span> respectively, so queues
can wait on the semaphore and the application can wait on the fence. Both
objects become signalled when the underlying native fence signals; if the
native fence has already signalled, then the semaphore will be in the signalled
state when this function returns. The driver takes ownership of the fence fd
and is responsible for closing it when no longer needed. It must do so even if
neither a semaphore or fence object is provided, or even if
<span class="monospaced">vkAcquireImageANDROID</span> fails and returns an error. If <span class="monospaced">fenceFd</span> is -1, it
is as if the native fence was already signalled.</p></div>
<div class="paragraph"><p><span class="monospaced">vkQueueSignalReleaseImageANDROID</span> prepares a swapchain image for external use, and creates a native fence and schedules it to be signalled when prior work on the queue has completed.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.6
@@ -926,6 +942,19 @@ Replaced vkImportNativeFenceANDROID and vkQueueSignalNativeFenceANDROID
</li>
</ul></div>
</li>
<li>
<p>
<strong>2015-12-03</strong>
</p>
<div class="ulist"><ul>
<li>
<p>
Added a VkFence parameter to vkAcquireImageANDROID corresponding to the
     parameter added to vkAcquireNextImageKHR.
</p>
</li>
</ul></div>
</li>
</ol></div>
</div>
</div>
@@ -934,7 +963,7 @@ Replaced vkImportNativeFenceANDROID and vkQueueSignalNativeFenceANDROID
<div id="footer">
<div id="footer-text">
Version 3<br>
Last updated 2015-11-04 10:58:22 PST
Last updated 2015-12-03 15:47:36 PST
</div>
</div>
</body>
+11 −11
Original line number Diff line number Diff line
@@ -17,21 +17,20 @@
#ifndef __VK_ANDROID_NATIVE_BUFFER_H__
#define __VK_ANDROID_NATIVE_BUFFER_H__

#include <vulkan/vulkan.h>
#include <system/window.h>

// TODO(jessehall): Get a real extension number officially assigned.
#define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER 1024
#define VK_ANDROID_NATIVE_BUFFER_REVISION         1
#define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME   "VK_ANDROID_native_buffer"
#include <vulkan/vulkan.h>

#ifdef __cplusplus
extern "C" {
#endif

// See https://gitlab.khronos.org/vulkan/vulkan/blob/master/doc/proposals/proposed/NVIDIA/VulkanRegistryProposal.txt
// and Khronos bug 14154 for explanation of these magic numbers.
#define VK_ANDROID_NATIVE_BUFFER_ENUM(type,id)    ((type)((int)0xc0000000 - VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER * -1024 + (id)))
#define VK_ANDROID_native_buffer 1

#define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER 11
#define VK_ANDROID_NATIVE_BUFFER_REVISION         4
#define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME   "VK_ANDROID_native_buffer"

#define VK_ANDROID_NATIVE_BUFFER_ENUM(type,id)    ((type)(1000000000 + (1000 * (VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER - 1)) + (id)))
#define VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID   VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 0)

typedef struct {
@@ -48,7 +47,7 @@ typedef struct {
} VkNativeBufferANDROID;

typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)(VkDevice device, VkFormat format, VkImageUsageFlags imageUsage, int* grallocUsage);
typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)(VkDevice device, VkImage image, int nativeFenceFd, VkSemaphore semaphore);
typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)(VkDevice device, VkImage image, int nativeFenceFd, VkSemaphore semaphore, VkFence fence);
typedef VkResult (VKAPI_PTR *PFN_vkQueueSignalReleaseImageANDROID)(VkQueue queue, VkImage image, int* pNativeFenceFd);
// -- DEPRECATED --
typedef VkResult (VKAPI_PTR *PFN_vkImportNativeFenceANDROID)(VkDevice device, VkSemaphore semaphore, int nativeFenceFd);
@@ -66,7 +65,8 @@ VKAPI_ATTR VkResult VKAPI_CALL vkAcquireImageANDROID(
    VkDevice            device,
    VkImage             image,
    int                 nativeFenceFd,
    VkSemaphore         semaphore
    VkSemaphore         semaphore,
    VkFence             fence
);
VKAPI_ATTR VkResult VKAPI_CALL vkQueueSignalReleaseImageANDROID(
    VkQueue             queue,
+30 −15
Original line number Diff line number Diff line
@@ -539,33 +539,44 @@ VkBool32 LogDebugMessageCallback(VkFlags message_flags,
    return false;
}

VkResult Noop(...) {
VkResult Noop() {
    return VK_SUCCESS;
}

VKAPI_ATTR PFN_vkVoidFunction
GetLayerDeviceProcAddr(VkDevice device, const char* name) {
    // The static_casts are used to ensure that our function actually
    // matches the API function prototype. Otherwise, if the API function
    // prototype changes (only a problem during API development), the compiler
    // has no way of knowing that the function is supposed to match the
    // prototype, so won't warn us if they don't.
    if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkGetDeviceProcAddr>(GetLayerDeviceProcAddr));
    }
    if (strcmp(name, "vkCreateDevice") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(Noop);
    }
    // WSI extensions are not in the driver so return the loader functions
    if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkCreateSwapchainKHR>(CreateSwapchainKHR));
    }
    if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkDestroySwapchainKHR>(DestroySwapchainKHR));
    }
    if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkGetSwapchainImagesKHR>(GetSwapchainImagesKHR));
    }
    if (strcmp(name, "vkAcquireNextImageKHR") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkAcquireNextImageKHR>(AcquireNextImageKHR));
    }
    if (strcmp(name, "vkQueuePresentKHR") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkQueuePresentKHR>(QueuePresentKHR));
    }
    if (!device)
        return GetGlobalDeviceProcAddr(name);
@@ -927,7 +938,8 @@ PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) {
        return reinterpret_cast<PFN_vkVoidFunction>(Noop);
    }
    if (strcmp(name, "vkCreateInstance") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkCreateInstance>(CreateInstanceBottom));
    }
    return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name);
}
@@ -1116,16 +1128,20 @@ PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) {
    if (!device)
        return GetGlobalDeviceProcAddr(name);
    if (strcmp(name, "vkGetDeviceProcAddr") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkGetDeviceProcAddr>(GetDeviceProcAddr));
    }
    if (strcmp(name, "vkGetDeviceQueue") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkGetDeviceQueue>(GetDeviceQueue));
    }
    if (strcmp(name, "vkAllocateCommandBuffers") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(AllocateCommandBuffers);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkAllocateCommandBuffers>(AllocateCommandBuffers));
    }
    if (strcmp(name, "vkDestroyDevice") == 0) {
        return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice);
        return reinterpret_cast<PFN_vkVoidFunction>(
            static_cast<PFN_vkDestroyDevice>(DestroyDevice));
    }
    return GetSpecificDeviceProcAddr(GetVtbl(device), name);
}
@@ -1164,7 +1180,7 @@ VkResult AllocateCommandBuffers(VkDevice device,
    return VK_SUCCESS;
}

VkResult DestroyDevice(VkDevice drv_device,
void DestroyDevice(VkDevice drv_device,
                   const VkAllocationCallbacks* /*allocator*/) {
    const DeviceVtbl* vtbl = GetVtbl(drv_device);
    Device* device = static_cast<Device*>(vtbl->device);
@@ -1174,7 +1190,6 @@ VkResult DestroyDevice(VkDevice drv_device,
    }
    vtbl->DestroyDevice(drv_device, device->instance->alloc);
    DestroyDevice(device);
    return VK_SUCCESS;
}

void* AllocMem(VkInstance instance,
+16 −13
Original line number Diff line number Diff line
@@ -215,15 +215,17 @@ VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
                        const VkAllocationCallbacks* allocator,
                        VkInstance* instance);
PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name);
PFN_vkVoidFunction GetDeviceProcAddr(VkDevice drv_device, const char* name);
void GetDeviceQueue(VkDevice drv_device,
VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice drv_device,
                                                const char* name);
VKAPI_ATTR void GetDeviceQueue(VkDevice drv_device,
                               uint32_t family,
                               uint32_t index,
                               VkQueue* out_queue);
VkResult AllocateCommandBuffers(VkDevice device,
VKAPI_ATTR VkResult
AllocateCommandBuffers(VkDevice device,
                       const VkCommandBufferAllocateInfo* alloc_info,
                       VkCommandBuffer* cmdbufs);
VkResult DestroyDevice(VkDevice drv_device,
VKAPI_ATTR void DestroyDevice(VkDevice drv_device,
                              const VkAllocationCallbacks* allocator);

void* AllocMem(VkInstance instance,
@@ -292,7 +294,7 @@ CreateSwapchainKHR(VkDevice device,
                   const VkSwapchainCreateInfoKHR* create_info,
                   const VkAllocationCallbacks* allocator,
                   VkSwapchainKHR* swapchain_handle);
VKAPI_ATTR VkResult DestroySwapchainKHR(VkDevice device,
VKAPI_ATTR void DestroySwapchainKHR(VkDevice device,
                                    VkSwapchainKHR swapchain_handle,
                                    const VkAllocationCallbacks* allocator);
VKAPI_ATTR VkResult GetSwapchainImagesKHR(VkDevice device,
@@ -303,6 +305,7 @@ VKAPI_ATTR VkResult AcquireNextImageKHR(VkDevice device,
                                        VkSwapchainKHR swapchain_handle,
                                        uint64_t timeout,
                                        VkSemaphore semaphore,
                                        VkFence fence,
                                        uint32_t* image_index);
VKAPI_ATTR VkResult
QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info);
Loading