| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef SRC_UI_LIB_ESCHER_TEST_COMMON_VK_VK_DEBUG_UTILS_MESSAGE_COLLECTOR_H_ |
| #define SRC_UI_LIB_ESCHER_TEST_COMMON_VK_VK_DEBUG_UTILS_MESSAGE_COLLECTOR_H_ |
| |
| #include <functional> |
| #include <string> |
| #include <vector> |
| |
| #include <vulkan/vulkan.hpp> |
| |
| namespace escher::test::impl { |
| |
| // Collects Vulkan debug reports and provides functions used by validation debug report checking |
| // macros. |
| // |
| // A test fixture which has an instance of |VkDebugUtilsMessengerCallbackRegistry| and |
| // |VkDebugUtilsMessageCollector| will be able to store all validation debug reports by setting |
| // |HandleDebugUtilsMessage| with |pUserData| pointing to this instance, as its callback function. |
| // |
| // NOTE: Users may want to use the test fixture class |TestWithVkValidationLayer| directly if they |
| // only need the basic unit tests with Vulkan validation layer enabled. |
| // |
| class VkDebugUtilsMessageCollector { |
| public: |
| // Debug report struct generated by Vulkan validation layer. |
| struct VkDebugUtilsMessage { |
| vk::DebugUtilsMessageSeverityFlagsEXT severity; |
| vk::DebugUtilsMessageTypeFlagsEXT types; |
| vk::ObjectType object_type; |
| uint64_t object; |
| std::string layer_prefix; |
| int32_t message_code; |
| std::string message; |
| |
| std::string ErrorMessage() const { |
| return "[" + vk::to_string(severity) + "][" + vk::to_string(types) + "] " + |
| " [Object type] " + vk::to_string(object_type) + " [Object id] " + |
| std::to_string(object) + " [Layer] " + layer_prefix + " [Message] # " + |
| std::to_string(message_code) + " : " + message; |
| } |
| }; |
| |
| // Default constructor. |
| VkDebugUtilsMessageCollector() = default; |
| |
| // Default Validation layer debug handler which stores all debug reports to the |
| // std::vector<VkDebugUtilsMessage> object which |pUserData|points to. |
| // |
| // The callback function is specified in |
| // https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/ |
| // PFN_vkDebugUtilsMessengerEXT.html |
| static uint32_t HandleDebugUtilsMessage(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, |
| VkDebugUtilsMessageTypeFlagsEXT message_types, |
| const VkDebugUtilsMessengerCallbackDataEXT* callback_data, |
| void* user_data); |
| |
| // Print all debug reports with given flags. If there is no debug reports with that given flags |
| // it will do nothing and return false; otherwise it returns true. |
| // |file| and |line| are passed by macros using __FILE__ and __LINE__ to print error position. |
| bool PrintDebugUtilsMessagesWithFlags(const vk::DebugUtilsMessageSeverityFlagsEXT& severity, |
| const vk::DebugUtilsMessageTypeFlagsEXT& types, |
| const char* file, size_t line); |
| |
| // Helper function which prints out the debug reports if |
| // |predicate|(number of debug reports with |flags|, |num_threshold|) == false. |
| // |file| and |line| are passed by macros using __FILE__ and __LINE__ to print error position. |
| // |
| // Used only by |EXPECT/ASSERT_VULKAN_VALIDATION_LAYER_ERRORS_[PREDICATE]()| macros and should |
| // only be called within test scope. |
| bool PrintDebugUtilsMessagesOnFalsePredicate( |
| const vk::DebugUtilsMessageSeverityFlagsEXT& severity, |
| const vk::DebugUtilsMessageTypeFlagsEXT& types, size_t num_threshold, |
| const std::function<bool(size_t, size_t)>& predicate, const char* file, size_t line) const; |
| |
| // Removes debug reports with certain debug report flag from |debug_utils_messages_| list to |
| // suppress test failure. |
| // Used by REMOVE_VK_VALIDATION_[WARNINGS/ERRORS/PERFORMANCE_WARNINGS]() macros and should |
| // only be called within test scope.. |
| void RemoveDebugUtilsMessagesWithFlag(const vk::DebugUtilsMessageSeverityFlagsEXT& severity, |
| const vk::DebugUtilsMessageTypeFlagsEXT& types); |
| |
| // Removes all debug reports from |debug_utils_messages_| list to suppress test failure. |
| // Used by REMOVE_VK_VALIDATION_DEBUG_UTILS_MESSAGES() macro and should only be called within |
| // test scope. |
| void RemoveAllDebugUtilsMessages() { |
| RemoveDebugUtilsMessagesWithFlag(~vk::DebugUtilsMessageSeverityFlagsEXT(), |
| ~vk::DebugUtilsMessageTypeFlagsEXT()); |
| } |
| |
| private: |
| // Helper function which outputs all debug reports with specific flag. |
| std::vector<VkDebugUtilsMessage> DebugUtilsMessagesWithFlag( |
| const vk::DebugUtilsMessageSeverityFlagsEXT& severity, |
| const vk::DebugUtilsMessageTypeFlagsEXT& types) const; |
| |
| std::vector<VkDebugUtilsMessage> debug_utils_messages_ = {}; |
| }; |
| |
| } // namespace escher::test::impl |
| |
| #endif // SRC_UI_LIB_ESCHER_TEST_COMMON_VK_VK_DEBUG_UTILS_MESSAGE_COLLECTOR_H_ |