Use std::function in Layer callbacks
authorCharles Giessen <charles@lunarg.com>
Fri, 10 Dec 2021 03:17:57 +0000 (20:17 -0700)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Sat, 11 Dec 2021 00:08:23 +0000 (17:08 -0700)
Also fixed the compilation issues in the linux build

tests/framework/layer/test_layer.cpp
tests/framework/layer/test_layer.h
tests/framework/test_util.h
tests/loader_layer_tests.cpp
tests/loader_regression_tests.cpp

index c96880d9be36d5c0a9d71bba5dada727b9f5c571..04905991b825b6e6232b0a82c20007d1d56ddf82 100644 (file)
@@ -161,8 +161,7 @@ VKAPI_ATTR VkResult VKAPI_CALL test_vkCreateInstance(const VkInstanceCreateInfo*
     // next layer in the chain.
     layer_init_instance_dispatch_table(layer.instance_handle, &layer.instance_dispatch_table, fpGetInstanceProcAddr);
 
-    if (layer.create_instance_callback.callback)
-        result = layer.create_instance_callback.callback(layer, layer.create_instance_callback.data);
+    if (layer.create_instance_callback) result = layer.create_instance_callback(layer);
 
     return result;
 }
@@ -195,8 +194,7 @@ VKAPI_ATTR VkResult VKAPI_CALL test_vkCreateDevice(VkPhysicalDevice physicalDevi
     // initialize layer's dispatch table
     layer_init_device_dispatch_table(device.device_handle, &device.dispatch_table, fpGetDeviceProcAddr);
 
-    if (layer.create_device_callback.callback)
-        result = layer.create_device_callback.callback(layer, layer.create_device_callback.data);
+    if (layer.create_device_callback) result = layer.create_device_callback(layer);
 
     return result;
 }
index 919538da7c4e1db4e8575ecdd4e4230d7f3dcc09..9ea27298c98d763c7d039407f94eb7f7594b3c7d 100644 (file)
@@ -112,14 +112,10 @@ struct TestLayer {
 
     BUILDER_VALUE(TestLayer, bool, intercept_vkEnumerateInstanceExtensionProperties, false)
     BUILDER_VALUE(TestLayer, bool, intercept_vkEnumerateInstanceLayerProperties, false)
-    struct LayerCallback {
-        FP_layer_callback callback = nullptr;
-        void* data = nullptr;
-    };
     // Called in vkCreateInstance after calling down the chain & returning
-    BUILDER_VALUE(TestLayer, LayerCallback, create_instance_callback, {})
+    BUILDER_VALUE(TestLayer, std::function<VkResult(TestLayer& layer)>, create_instance_callback, {})
     // Called in vkCreateDevice after calling down the chain & returning
-    BUILDER_VALUE(TestLayer, LayerCallback, create_device_callback, {})
+    BUILDER_VALUE(TestLayer, std::function<VkResult(TestLayer& layer)>, create_device_callback, {})
 
     PFN_vkGetInstanceProcAddr next_vkGetInstanceProcAddr = VK_NULL_HANDLE;
     PFN_vkGetDeviceProcAddr next_vkGetDeviceProcAddr = VK_NULL_HANDLE;
index 02fa73ba05b6c9fe6e0cb1195b8072daef0cabe7..fd509f235950d0e6e9a75493d7975ef5aeb1e0dc 100644 (file)
@@ -57,6 +57,7 @@
 #include <unordered_map>
 #include <utility>
 #include <memory>
+#include <functional>
 
 #include <cassert>
 #include <cstring>
index d4342556be664ae29b3d0908f70a9dfcc930e882..8c682d78482b9c13fd8904b9f24864f2c799b91a 100644 (file)
@@ -241,28 +241,26 @@ TEST_F(LayerCreateInstance, GetPhysicalDeviceProperties2) {
         "regular_test_layer.json");
 
     auto& layer = env->get_test_layer(0);
-    layer.set_create_instance_callback(TestLayer::LayerCallback{
-        [](TestLayer& layer, void* data) -> VkResult {
-            uint32_t phys_dev_count = 0;
-            VkResult res = layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count, nullptr);
-            if (res != VK_SUCCESS || phys_dev_count > 1) {
-                return VK_ERROR_INITIALIZATION_FAILED;  // expecting only a single physical device.
-            }
-            VkPhysicalDevice phys_dev{};
-            res = layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count, &phys_dev);
-            if (res != VK_SUCCESS) {
-                return VK_ERROR_INITIALIZATION_FAILED;
-            }
-            VkPhysicalDeviceProperties2 props2{};
-            props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
-            layer.instance_dispatch_table.GetPhysicalDeviceProperties2(phys_dev, &props2);
-
-            VkPhysicalDeviceFeatures2 features2{};
-            features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
-            layer.instance_dispatch_table.GetPhysicalDeviceFeatures2(phys_dev, &features2);
-            return VK_SUCCESS;
-        },
-        nullptr});
+    layer.set_create_instance_callback([](TestLayer& layer) -> VkResult {
+        uint32_t phys_dev_count = 0;
+        VkResult res = layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count, nullptr);
+        if (res != VK_SUCCESS || phys_dev_count > 1) {
+            return VK_ERROR_INITIALIZATION_FAILED;  // expecting only a single physical device.
+        }
+        VkPhysicalDevice phys_dev{};
+        res = layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count, &phys_dev);
+        if (res != VK_SUCCESS) {
+            return VK_ERROR_INITIALIZATION_FAILED;
+        }
+        VkPhysicalDeviceProperties2 props2{};
+        props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
+        layer.instance_dispatch_table.GetPhysicalDeviceProperties2(phys_dev, &props2);
+
+        VkPhysicalDeviceFeatures2 features2{};
+        features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
+        layer.instance_dispatch_table.GetPhysicalDeviceFeatures2(phys_dev, &features2);
+        return VK_SUCCESS;
+    });
 
     InstWrapper inst{env->vulkan_functions};
     inst.create_info.add_layer(regular_layer_name).set_api_version(1, 1, 0);
@@ -280,22 +278,20 @@ TEST_F(LayerCreateInstance, GetPhysicalDeviceProperties2KHR) {
         "regular_test_layer.json");
 
     auto& layer = env->get_test_layer(0);
-    layer.set_create_instance_callback(TestLayer::LayerCallback{
-        [](TestLayer& layer, void* data) -> VkResult {
-            uint32_t phys_dev_count = 1;
-            VkPhysicalDevice phys_dev{};
-            layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count, &phys_dev);
-
-            VkPhysicalDeviceProperties2KHR props2{};
-            props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
-            layer.instance_dispatch_table.GetPhysicalDeviceProperties2KHR(phys_dev, &props2);
-
-            VkPhysicalDeviceFeatures2KHR features2{};
-            features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR;
-            layer.instance_dispatch_table.GetPhysicalDeviceFeatures2KHR(phys_dev, &features2);
-            return VK_SUCCESS;
-        },
-        nullptr});
+    layer.set_create_instance_callback([](TestLayer& layer) -> VkResult {
+        uint32_t phys_dev_count = 1;
+        VkPhysicalDevice phys_dev{};
+        layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count, &phys_dev);
+
+        VkPhysicalDeviceProperties2KHR props2{};
+        props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
+        layer.instance_dispatch_table.GetPhysicalDeviceProperties2KHR(phys_dev, &props2);
+
+        VkPhysicalDeviceFeatures2KHR features2{};
+        features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR;
+        layer.instance_dispatch_table.GetPhysicalDeviceFeatures2KHR(phys_dev, &features2);
+        return VK_SUCCESS;
+    });
 
     InstWrapper inst{env->vulkan_functions};
     inst.create_info.add_layer(regular_layer_name).add_extension("VK_KHR_get_physical_device_properties2");
index ce740cbf1621e0eca3104fdc1e65c0a2fe945bc3..e872bbec630529f33c9853325652f1fb2d096a4e 100644 (file)
@@ -772,7 +772,8 @@ TEST_F(EnumeratePhysicalDeviceGroups, TwoCallIncomplete) {
 #if defined(__linux__) || defined(__FreeBSD__)
 // Make sure the loader reports the correct message based on if USE_UNSAFE_FILE_SEARCH is set or not
 TEST(EnvironmentVariables, NonSecureEnvVarLookup) {
-    SingleICDShim env(TestICDDetails(TEST_ICD_PATH_VERSION_6));
+    FrameworkEnvironment env{};
+    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_6));
     env.get_test_icd().physical_devices.emplace_back("physical_device_0");
 
     DebugUtilsLogger log{VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT};
@@ -799,7 +800,8 @@ TEST(EnvironmentVariables, XDG) {
     set_env_var("XDG_DATA_DIRS", "::::/tmp/goober3:/tmp/goober4/with spaces:::");
     set_env_var("XDG_DATA_HOME", "::::/tmp/goober3:/tmp/goober4/with spaces:::");
 
-    SingleICDShim env{TestICDDetails{TEST_ICD_PATH_VERSION_6}};
+    FrameworkEnvironment env{};
+    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_6));
     env.get_test_icd().physical_devices.push_back({});
 
     InstWrapper inst{env.vulkan_functions};
@@ -828,12 +830,13 @@ TEST(EnvironmentVariables, VK_LAYER_PATH) {
     vk_layer_path += (HOME / "/ with spaces/:::::/tandy:").str();
     set_env_var("VK_LAYER_PATH", vk_layer_path);
 
-    SingleICDShim env{TestICDDetails{TEST_ICD_PATH_VERSION_6}};
+    FrameworkEnvironment env{};
+    env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_6));
     env.get_test_icd().physical_devices.push_back({});
     env.platform_shim->redirect_path("/tmp/carol", env.explicit_layer_folder.location());
 
     const char* layer_name = "TestLayer";
-    env->add_explicit_layer(
+    env.add_explicit_layer(
         ManifestLayer{}.add_layer(
             ManifestLayer::LayerDescription{}.set_name(layer_name).set_lib_path(TEST_LAYER_PATH_EXPORT_VERSION_2)),
         "test_layer.json");