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

Commit 87085d4f authored by Chia-I Wu's avatar Chia-I Wu Committed by Android (Google) Code Review
Browse files

Merge changes I05c45303,I25b71570,I56ebc0ee into nyc-dev

* changes:
  vulkan: fix vkGet*ProcAddr for un-enabled extensions
  vulkan: do not query non-enabled WSI functions
  vulkan: pass hal_extensions to InitDriverTable
parents d4797c2f 36cc00a1
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -465,6 +465,7 @@ class LayerChain {

    VkExtensionProperties* driver_extensions_;
    uint32_t driver_extension_count_;
    std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_;
};

LayerChain::LayerChain(bool is_instance, const VkAllocationCallbacks& allocator)
@@ -477,7 +478,9 @@ LayerChain::LayerChain(bool is_instance, const VkAllocationCallbacks& allocator)
      get_instance_proc_addr_(nullptr),
      get_device_proc_addr_(nullptr),
      driver_extensions_(nullptr),
      driver_extension_count_(0) {}
      driver_extension_count_(0) {
    enabled_extensions_.set(driver::ProcHook::EXTENSION_CORE);
}

LayerChain::~LayerChain() {
    allocator_.pfnFree(allocator_.pUserData, driver_extensions_);
@@ -694,11 +697,11 @@ VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info,

    // initialize InstanceData
    InstanceData& data = GetData(instance);
    memset(&data, 0, sizeof(data));

    data.instance = instance;

    if (!InitDispatchTable(instance, get_instance_proc_addr_)) {
    if (!InitDispatchTable(instance, get_instance_proc_addr_,
                           enabled_extensions_)) {
        if (data.dispatch.DestroyInstance)
            data.dispatch.DestroyInstance(instance, allocator);

@@ -774,9 +777,8 @@ VkResult LayerChain::Create(VkPhysicalDevice physical_dev,

    // initialize DeviceData
    DeviceData& data = GetData(dev);
    memset(&data, 0, sizeof(data));

    if (!InitDispatchTable(dev, get_device_proc_addr_)) {
    if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) {
        if (data.dispatch.DestroyDevice)
            data.dispatch.DestroyDevice(dev, allocator);

@@ -816,6 +818,10 @@ VkResult LayerChain::ValidateExtensions(const char* const* extension_names,
            ALOGE("Failed to enable missing instance extension %s", name);
            return VK_ERROR_EXTENSION_NOT_PRESENT;
        }

        auto ext_bit = driver::GetProcHookExtension(name);
        if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN)
            enabled_extensions_.set(ext_bit);
    }

    return VK_SUCCESS;
@@ -849,6 +855,10 @@ VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev,
            ALOGE("Failed to enable missing device extension %s", name);
            return VK_ERROR_EXTENSION_NOT_PRESENT;
        }

        auto ext_bit = driver::GetProcHookExtension(name);
        if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN)
            enabled_extensions_.set(ext_bit);
    }

    return VK_SUCCESS;
+77 −7
Original line number Diff line number Diff line
@@ -37,14 +37,81 @@ namespace api {
        }                                                              \
    } while (0)

// TODO do we want to point to a stub or nullptr when ext is not enabled?
// Exported extension functions may be invoked even when their extensions
// are disabled.  Dispatch to stubs when that happens.
#define INIT_PROC_EXT(ext, obj, proc)            \
    do {                                         \
        if (extensions[driver::ProcHook::ext])   \
            INIT_PROC(obj, proc);                \
        else                                     \
            data.dispatch.proc = disabled##proc; \
    } while (0)

bool InitDispatchTable(VkInstance instance,
                       PFN_vkGetInstanceProcAddr get_proc) {
namespace {

// clang-format off

VKAPI_ATTR void disabledDestroySurfaceKHR(VkInstance, VkSurfaceKHR, const VkAllocationCallbacks*) {
    ALOGE("VK_KHR_surface not enabled. vkDestroySurfaceKHR not executed.");
}

VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice, uint32_t, VkSurfaceKHR, VkBool32*) {
    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceSupportKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice, VkSurfaceKHR, VkSurfaceCapabilitiesKHR*) {
    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceCapabilitiesKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice, VkSurfaceKHR, uint32_t*, VkSurfaceFormatKHR*) {
    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceFormatsKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice, VkSurfaceKHR, uint32_t*, VkPresentModeKHR*) {
    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfacePresentModesKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR VkResult disabledCreateSwapchainKHR(VkDevice, const VkSwapchainCreateInfoKHR*, const VkAllocationCallbacks*, VkSwapchainKHR*) {
    ALOGE("VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR void disabledDestroySwapchainKHR(VkDevice, VkSwapchainKHR, const VkAllocationCallbacks*) {
    ALOGE("VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
}

VKAPI_ATTR VkResult disabledGetSwapchainImagesKHR(VkDevice, VkSwapchainKHR, uint32_t*, VkImage*) {
    ALOGE("VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR VkResult disabledAcquireNextImageKHR(VkDevice, VkSwapchainKHR, uint64_t, VkSemaphore, VkFence, uint32_t*) {
    ALOGE("VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR VkResult disabledQueuePresentKHR(VkQueue, const VkPresentInfoKHR*) {
    ALOGE("VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
    return VK_SUCCESS;
}

VKAPI_ATTR VkResult disabledCreateAndroidSurfaceKHR(VkInstance, const VkAndroidSurfaceCreateInfoKHR*, const VkAllocationCallbacks*, VkSurfaceKHR*) {
    ALOGE("VK_KHR_android_surface not enabled. vkCreateAndroidSurfaceKHR not executed.");
    return VK_SUCCESS;
}

// clang-format on

}  // anonymous

bool InitDispatchTable(
    VkInstance instance,
    PFN_vkGetInstanceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT>& extensions) {
    auto& data = GetData(instance);
    bool success = true;

@@ -73,7 +140,10 @@ bool InitDispatchTable(VkInstance instance,
    return success;
}

bool InitDispatchTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc) {
bool InitDispatchTable(
    VkDevice dev,
    PFN_vkGetDeviceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT>& extensions) {
    auto& data = GetData(dev);
    bool success = true;

+10 −2
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@
#ifndef LIBVULKAN_API_GEN_H
#define LIBVULKAN_API_GEN_H

#include <bitset>
#include <vulkan/vulkan.h>
#include "driver_gen.h"

namespace vulkan {
namespace api {
@@ -179,8 +181,14 @@ struct DeviceDispatchTable {
    // clang-format on
};

bool InitDispatchTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc);
bool InitDispatchTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc);
bool InitDispatchTable(
    VkInstance instance,
    PFN_vkGetInstanceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT>& extensions);
bool InitDispatchTable(
    VkDevice dev,
    PFN_vkGetDeviceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT>& extensions);

}  // namespace api
}  // namespace vulkan
+87 −41
Original line number Diff line number Diff line
@@ -37,7 +37,9 @@
#ifndef LIBVULKAN_API_GEN_H
#define LIBVULKAN_API_GEN_H

#include <bitset>
#include <vulkan/vulkan.h>
#include "driver_gen.h"

namespace vulkan {«
namespace api {«
@@ -62,8 +64,14 @@ struct DeviceDispatchTable {
  // clang-format on
};

bool InitDispatchTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc);
bool InitDispatchTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc);
bool InitDispatchTable(
    VkInstance instance,
    PFN_vkGetInstanceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions);
bool InitDispatchTable(
    VkDevice dev,
    PFN_vkGetDeviceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions);

»} // namespace api
»} // namespace vulkan
@@ -95,7 +103,21 @@ namespace api {«

{{Macro "api.C++.DefineInitProcExtMacro"}}

bool InitDispatchTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc) {
namespace {«

// clang-format off

{{range $f := AllCommands $}}
  {{Macro "api.C++.DefineExtensionStub" $f}}
{{end}}
// clang-format on

»} // anonymous

bool InitDispatchTable(
    VkInstance instance,
    PFN_vkGetInstanceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions) {
    auto& data = GetData(instance);
    bool success = true;

@@ -110,7 +132,10 @@ bool InitDispatchTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc)
    return success;
}

bool InitDispatchTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc) {
bool InitDispatchTable(
    VkDevice dev,
    PFN_vkGetDeviceProcAddr get_proc,
    const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions) {
    auto& data = GetData(dev);
    bool success = true;

@@ -163,6 +188,7 @@ bool InitDispatchTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc) {
#ifndef LIBVULKAN_DRIVER_GEN_H
#define LIBVULKAN_DRIVER_GEN_H

#include <bitset>
#include <vulkan/vulkan.h>
#include <vulkan/vk_android_native_buffer.h>

@@ -194,8 +220,10 @@ struct DeviceDriverTable {
const ProcHook* GetProcHook(const char* name);
ProcHook::Extension GetProcHookExtension(const char* name);

bool InitDriverTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc);
bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc);
bool InitDriverTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc,
                     const std::bitset<ProcHook::EXTENSION_COUNT> &extensions);
bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
                     const std::bitset<ProcHook::EXTENSION_COUNT> &extensions);

»} // namespace driver
»} // namespace vulkan
@@ -228,7 +256,7 @@ namespace {«
// clang-format off

{{range $f := AllCommands $}}
  {{Macro "driver.C++.DefineProcHookStubs" $f}}
  {{Macro "driver.C++.DefineProcHookStub" $f}}
{{end}}
// clang-format on

@@ -273,7 +301,8 @@ ProcHook::Extension GetProcHookExtension(const char* name) {

{{Macro "driver.C++.DefineInitProcExtMacro"}}

bool InitDriverTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc)
bool InitDriverTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc,
                     const std::bitset<ProcHook::EXTENSION_COUNT> &extensions)
{
    auto& data = GetData(instance);
    bool success = true;
@@ -289,7 +318,8 @@ bool InitDriverTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc)
    return success;
}

bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc)
bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
                     const std::bitset<ProcHook::EXTENSION_COUNT> &extensions)
{
    auto& data = GetData(dev);
    bool success = true;
@@ -428,13 +458,41 @@ bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc)
-------------------------------------------------------------------------------
*/}}
{{define "api.C++.DefineInitProcExtMacro"}}
  // TODO do we want to point to a stub or nullptr when ext is not enabled?
  // Exported extension functions may be invoked even when their extensions
  // are disabled.  Dispatch to stubs when that happens.
  #define INIT_PROC_EXT(ext, obj, proc) do {                    \
      if (extensions[driver::ProcHook::ext])                    \
        INIT_PROC(obj, proc);                                   \
      else                                                      \
        data.dispatch.proc = disabled ## proc;                  \
  } while(0)
{{end}}


{{/*
-------------------------------------------------------------------------------
  Emits a stub for an exported extension function.
-------------------------------------------------------------------------------
*/}}
{{define "api.C++.DefineExtensionStub"}}
  {{AssertType $ "Function"}}

  {{$ext := GetAnnotation $ "extension"}}
  {{if and $ext (Macro "IsFunctionExported" $)}}
    {{$ext_name := index $ext.Arguments 0}}

    {{$base := (Macro "BaseName" $)}}
    {{$unnamed_params := (ForEach $.CallParameters "ParameterType" | JoinWith ", ")}}

    VKAPI_ATTR {{Node "Type" $.Return}} disabled{{$base}}({{$unnamed_params}}) {
      ALOGE("{{$ext_name}} not enabled. {{$.Name}} not executed.");
      {{if not (IsVoid $.Return.Type)}}return VK_SUCCESS;{{end}}
    }

  {{end}}
{{end}}


{{/*
------------------------------------------------------------------------------
  Emits code for vkGetInstanceProcAddr for function interception.
@@ -642,13 +700,13 @@ VK_KHR_swapchain

{{/*
------------------------------------------------------------------------------
  Emits true if a function needs ProcHook stubs.
  Emits true if a function needs a ProcHook stub.
------------------------------------------------------------------------------
*/}}
{{define "driver.NeedProcHookStubs"}}
{{define "driver.NeedProcHookStub"}}
  {{AssertType $ "Function"}}

  {{if (Macro "driver.IsIntercepted" $)}}
  {{if and (Macro "driver.IsIntercepted" $) (Macro "IsDeviceDispatched" $)}}
    {{$ext := GetAnnotation $ "extension"}}
    {{if $ext}}
      {{if not (Macro "IsExtensionInternal" $ext)}}true{{end}}
@@ -686,8 +744,7 @@ VK_KHR_swapchain
      Extension extension;

      PFN_vkVoidFunction proc;
      PFN_vkVoidFunction disabled_proc; // nullptr for global hooks
      PFN_vkVoidFunction checked_proc;  // nullptr for global/instance hooks
      PFN_vkVoidFunction checked_proc;  // always nullptr for non-device hooks
  };
{{end}}

@@ -699,7 +756,7 @@ VK_KHR_swapchain
*/}}
{{define "driver.C++.DefineInitProcExtMacro"}}
  #define INIT_PROC_EXT(ext, obj, proc) do {                    \
      if (data.hal_extensions[ProcHook::ext])           \
      if (extensions[ProcHook::ext])                            \
        INIT_PROC(obj, proc);                                   \
  } while(0)
{{end}}
@@ -707,35 +764,31 @@ VK_KHR_swapchain

{{/*
-------------------------------------------------------------------------------
  Emits definitions of stub functions for ProcHook.
  Emits a stub for ProcHook::checked_proc.
-------------------------------------------------------------------------------
*/}}
{{define "driver.C++.DefineProcHookStubs"}}
{{define "driver.C++.DefineProcHookStub"}}
  {{AssertType $ "Function"}}

  {{if (Macro "driver.NeedProcHookStubs" $)}}
  {{if (Macro "driver.NeedProcHookStub" $)}}
    {{$ext := GetAnnotation $ "extension"}}
    {{$ext_name := index $ext.Arguments 0}}

    {{$base := (Macro "BaseName" $)}}
    {{$unnamed_params := (ForEach $.CallParameters "ParameterType" | JoinWith ", ")}}

    VKAPI_ATTR {{Node "Type" $.Return}} disabled{{$base}}({{$unnamed_params}}) {
      ALOGE("{{$ext_name}} not enabled. {{$.Name}} not executed.");
      {{if not (IsVoid $.Return.Type)}}return VK_SUCCESS;{{end}}
    }
    {{if (Macro "IsDeviceDispatched" $)}}

    VKAPI_ATTR {{Node "Type" $.Return}} checked{{$base}}({{Macro "Parameters" $}}) {
        {{if not (IsVoid $.Return.Type)}}return §{{end}}

      {{$p0 := index $.CallParameters 0}}
      {{$ext_hook := Strings ("ProcHook::") (Macro "BaseName" $ext)}}
        (GetData({{$p0.Name}}).hook_extensions[{{$ext_hook}}]) ? §
          {{$base}}({{Macro "Arguments" $}}) : §
          disabled{{$base}}({{Macro "Arguments" $}});

      if (GetData({{$p0.Name}}).hook_extensions[{{$ext_hook}}]) {
        {{if not (IsVoid $.Return.Type)}}return §{{end}}
        {{$base}}({{Macro "Arguments" $}});
      } else {
        ALOGE("{{$ext_name}} not enabled. {{$.Name}} not executed.");
        {{if not (IsVoid $.Return.Type)}}return VK_SUCCESS;{{end}}
      }
    }
    {{end}}

  {{end}}
{{end}}
@@ -762,7 +815,6 @@ VK_KHR_swapchain
    ProcHook::EXTENSION_CORE,
    reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
    nullptr,
    nullptr,
  },
{{end}}

@@ -788,17 +840,14 @@ VK_KHR_swapchain
      {{if (Macro "IsExtensionInternal" $ext)}}
        nullptr,
        nullptr,
        nullptr,
      {{else}}
        reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
        reinterpret_cast<PFN_vkVoidFunction>(disabled{{$base}}),
        nullptr,
      {{end}}
    {{else}}
      ProcHook::EXTENSION_CORE,
      reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
      nullptr,
      nullptr,
    {{end}}
  },
{{end}}
@@ -825,17 +874,14 @@ VK_KHR_swapchain
      {{if (Macro "IsExtensionInternal" $ext)}}
        nullptr,
        nullptr,
        nullptr,
      {{else}}
        reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
        reinterpret_cast<PFN_vkVoidFunction>(disabled{{$base}}),
        reinterpret_cast<PFN_vkVoidFunction>(checked{{$base}}),
      {{end}}
    {{else}}
      ProcHook::EXTENSION_CORE,
      reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
      nullptr,
      nullptr,
    {{end}}
  },
{{end}}
+7 −8
Original line number Diff line number Diff line
@@ -499,7 +499,7 @@ PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
        case ProcHook::INSTANCE:
            proc = (GetData(instance).hook_extensions[hook->extension])
                       ? hook->proc
                       : hook->disabled_proc;
                       : nullptr;
            break;
        case ProcHook::DEVICE:
            proc = (hook->extension == ProcHook::EXTENSION_CORE)
@@ -528,9 +528,8 @@ PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
        return nullptr;
    }

    return (GetData(device).hook_extensions[hook->extension])
               ? hook->proc
               : hook->disabled_proc;
    return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
                                                              : nullptr;
}

VkResult EnumerateInstanceExtensionProperties(
@@ -616,7 +615,6 @@ VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
        return VK_ERROR_OUT_OF_HOST_MEMORY;

    data->hook_extensions |= wrapper.GetHookExtensions();
    data->hal_extensions |= wrapper.GetHalExtensions();

    // call into the driver
    VkInstance instance;
@@ -630,7 +628,8 @@ VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,

    // initialize InstanceDriverTable
    if (!SetData(instance, *data) ||
        !InitDriverTable(instance, g_hwdevice->GetInstanceProcAddr)) {
        !InitDriverTable(instance, g_hwdevice->GetInstanceProcAddr,
                         wrapper.GetHalExtensions())) {
        data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
            g_hwdevice->GetInstanceProcAddr(instance, "vkDestroyInstance"));
        if (data->driver.DestroyInstance)
@@ -687,7 +686,6 @@ VkResult CreateDevice(VkPhysicalDevice physicalDevice,
        return VK_ERROR_OUT_OF_HOST_MEMORY;

    data->hook_extensions |= wrapper.GetHookExtensions();
    data->hal_extensions |= wrapper.GetHalExtensions();

    // call into the driver
    VkDevice dev;
@@ -701,7 +699,8 @@ VkResult CreateDevice(VkPhysicalDevice physicalDevice,

    // initialize DeviceDriverTable
    if (!SetData(dev, *data) ||
        !InitDriverTable(dev, instance_data.get_device_proc_addr)) {
        !InitDriverTable(dev, instance_data.get_device_proc_addr,
                         wrapper.GetHalExtensions())) {
        data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
            instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
        if (data->driver.DestroyDevice)
Loading