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

Commit 3e17701d authored by Chia-I Wu's avatar Chia-I Wu Committed by android-build-merger
Browse files

vulkan: add a wrapper for hwvulkan_device_t

am: 31b2e4f0

* commit '31b2e4f0':
  vulkan: add a wrapper for hwvulkan_device_t

Change-Id: I4d64d13427218c437babb31d85982c438ed7f2af
parents b481e90b 31b2e4f0
Loading
Loading
Loading
Loading
+65 −49
Original line number Original line Diff line number Diff line
@@ -46,10 +46,26 @@ namespace driver {


namespace {
namespace {


class Hal {
   public:
    static bool Open();

    static const Hal& Get() { return hal_; }
    static const hwvulkan_device_t& Device() { return *Get().dev_; }

   private:
    Hal() : dev_(nullptr) {}
    Hal(const Hal&) = delete;
    Hal& operator=(const Hal&) = delete;

    static Hal hal_;

    const hwvulkan_device_t* dev_;
};

class CreateInfoWrapper {
class CreateInfoWrapper {
   public:
   public:
    CreateInfoWrapper(const hwvulkan_device_t* hw_dev,
    CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
                      const VkInstanceCreateInfo& create_info,
                      const VkAllocationCallbacks& allocator);
                      const VkAllocationCallbacks& allocator);
    CreateInfoWrapper(VkPhysicalDevice physical_dev,
    CreateInfoWrapper(VkPhysicalDevice physical_dev,
                      const VkDeviceCreateInfo& create_info,
                      const VkDeviceCreateInfo& create_info,
@@ -87,10 +103,7 @@ class CreateInfoWrapper {
    const bool is_instance_;
    const bool is_instance_;
    const VkAllocationCallbacks& allocator_;
    const VkAllocationCallbacks& allocator_;


    union {
        const hwvulkan_device_t* hw_dev_;
    VkPhysicalDevice physical_dev_;
    VkPhysicalDevice physical_dev_;
    };


    union {
    union {
        VkInstanceCreateInfo instance_info_;
        VkInstanceCreateInfo instance_info_;
@@ -103,12 +116,43 @@ class CreateInfoWrapper {
    std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
    std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
};
};


CreateInfoWrapper::CreateInfoWrapper(const hwvulkan_device_t* hw_dev,
Hal Hal::hal_;
                                     const VkInstanceCreateInfo& create_info,

bool Hal::Open() {
    ALOG_ASSERT(!dev_, "OpenHAL called more than once");

    // Use a stub device unless we successfully open a real HAL device.
    hal_.dev_ = &stubhal::kDevice;

    const hwvulkan_module_t* module;
    int result =
        hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
    if (result != 0) {
        ALOGI("no Vulkan HAL present, using stub HAL");
        return true;
    }

    hwvulkan_device_t* device;
    result =
        module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
                                     reinterpret_cast<hw_device_t**>(&device));
    if (result != 0) {
        // Any device with a Vulkan HAL should be able to open the device.
        ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
              result);
        return false;
    }

    hal_.dev_ = device;

    return true;
}

CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
                                     const VkAllocationCallbacks& allocator)
                                     const VkAllocationCallbacks& allocator)
    : is_instance_(true),
    : is_instance_(true),
      allocator_(allocator),
      allocator_(allocator),
      hw_dev_(hw_dev),
      physical_dev_(VK_NULL_HANDLE),
      instance_info_(create_info),
      instance_info_(create_info),
      extension_filter_() {
      extension_filter_() {
    hook_extensions_.set(ProcHook::EXTENSION_CORE);
    hook_extensions_.set(ProcHook::EXTENSION_CORE);
@@ -225,8 +269,8 @@ VkResult CreateInfoWrapper::SanitizeExtensions() {


VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
    if (is_instance_) {
    if (is_instance_) {
        return hw_dev_->EnumerateInstanceExtensionProperties(nullptr, &count,
        return Hal::Device().EnumerateInstanceExtensionProperties(
                                                             nullptr);
            nullptr, &count, nullptr);
    } else {
    } else {
        const auto& driver = GetData(physical_dev_).driver;
        const auto& driver = GetData(physical_dev_).driver;
        return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
        return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
@@ -238,8 +282,8 @@ VkResult CreateInfoWrapper::EnumerateExtensions(
    uint32_t& count,
    uint32_t& count,
    VkExtensionProperties* props) const {
    VkExtensionProperties* props) const {
    if (is_instance_) {
    if (is_instance_) {
        return hw_dev_->EnumerateInstanceExtensionProperties(nullptr, &count,
        return Hal::Device().EnumerateInstanceExtensionProperties(
                                                             props);
            nullptr, &count, props);
    } else {
    } else {
        const auto& driver = GetData(physical_dev_).driver;
        const auto& driver = GetData(physical_dev_).driver;
        return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
        return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
@@ -343,8 +387,6 @@ void CreateInfoWrapper::FilterExtension(const char* name) {
    }
    }
}
}


const hwvulkan_device_t* g_hwdevice = nullptr;

VKAPI_ATTR void* DefaultAllocate(void*,
VKAPI_ATTR void* DefaultAllocate(void*,
                                 size_t size,
                                 size_t size,
                                 size_t alignment,
                                 size_t alignment,
@@ -433,33 +475,7 @@ bool Debuggable() {
}
}


bool OpenHAL() {
bool OpenHAL() {
    ALOG_ASSERT(!g_hwdevice, "OpenHAL called more than once");
    return Hal::Open();

    // Use a stub device unless we successfully open a real HAL device.
    g_hwdevice = &stubhal::kDevice;

    const hwvulkan_module_t* module;
    int result =
        hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
    if (result != 0) {
        ALOGI("no Vulkan HAL present, using stub HAL");
        return true;
    }

    hwvulkan_device_t* device;
    result =
        module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
                                     reinterpret_cast<hw_device_t**>(&device));
    if (result != 0) {
        // Any device with a Vulkan HAL should be able to open the device.
        ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
              result);
        return false;
    }

    g_hwdevice = device;

    return true;
}
}


const VkAllocationCallbacks& GetDefaultAllocator() {
const VkAllocationCallbacks& GetDefaultAllocator() {
@@ -476,7 +492,7 @@ const VkAllocationCallbacks& GetDefaultAllocator() {
PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
    const ProcHook* hook = GetProcHook(pName);
    const ProcHook* hook = GetProcHook(pName);
    if (!hook)
    if (!hook)
        return g_hwdevice->GetInstanceProcAddr(instance, pName);
        return Hal::Device().GetInstanceProcAddr(instance, pName);


    if (!instance) {
    if (!instance) {
        if (hook->type == ProcHook::GLOBAL)
        if (hook->type == ProcHook::GLOBAL)
@@ -562,7 +578,7 @@ VkResult EnumerateInstanceExtensionProperties(
        *pPropertyCount -= count;
        *pPropertyCount -= count;
    }
    }


    VkResult result = g_hwdevice->EnumerateInstanceExtensionProperties(
    VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
        pLayerName, pPropertyCount, pProperties);
        pLayerName, pPropertyCount, pProperties);


    if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE))
    if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE))
@@ -608,7 +624,7 @@ VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
    const VkAllocationCallbacks& data_allocator =
    const VkAllocationCallbacks& data_allocator =
        (pAllocator) ? *pAllocator : GetDefaultAllocator();
        (pAllocator) ? *pAllocator : GetDefaultAllocator();


    CreateInfoWrapper wrapper(g_hwdevice, *pCreateInfo, data_allocator);
    CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
    VkResult result = wrapper.Validate();
    VkResult result = wrapper.Validate();
    if (result != VK_SUCCESS)
    if (result != VK_SUCCESS)
        return result;
        return result;
@@ -621,7 +637,7 @@ VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,


    // call into the driver
    // call into the driver
    VkInstance instance;
    VkInstance instance;
    result = g_hwdevice->CreateInstance(
    result = Hal::Device().CreateInstance(
        static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
        static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
        &instance);
        &instance);
    if (result != VK_SUCCESS) {
    if (result != VK_SUCCESS) {
@@ -631,10 +647,10 @@ VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,


    // initialize InstanceDriverTable
    // initialize InstanceDriverTable
    if (!SetData(instance, *data) ||
    if (!SetData(instance, *data) ||
        !InitDriverTable(instance, g_hwdevice->GetInstanceProcAddr,
        !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
                         wrapper.GetHalExtensions())) {
                         wrapper.GetHalExtensions())) {
        data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
        data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
            g_hwdevice->GetInstanceProcAddr(instance, "vkDestroyInstance"));
            Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
        if (data->driver.DestroyInstance)
        if (data->driver.DestroyInstance)
            data->driver.DestroyInstance(instance, pAllocator);
            data->driver.DestroyInstance(instance, pAllocator);


@@ -644,7 +660,7 @@ VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
    }
    }


    data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
    data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
        g_hwdevice->GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
        Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
    if (!data->get_device_proc_addr) {
    if (!data->get_device_proc_addr) {
        data->driver.DestroyInstance(instance, pAllocator);
        data->driver.DestroyInstance(instance, pAllocator);
        FreeInstanceData(data, data_allocator);
        FreeInstanceData(data, data_allocator);