1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <vulkan/vulkan.h>
18 
19 #include "goldfish_vk_dispatch.h"
20 
21 namespace gfxstream {
22 namespace vk {
23 
24 class DebugUtilsHelper {
25    public:
26     static DebugUtilsHelper withUtilsEnabled(VkDevice device, const VulkanDispatch* dispatch);
27 
28     static DebugUtilsHelper withUtilsDisabled();
29 
30     ~DebugUtilsHelper() = default;
31 
32     template <typename VkObjectT, typename... T>
addDebugLabel(VkObjectT object,const char * format,T &&...formatArgs)33     void addDebugLabel(VkObjectT object, const char* format, T&&... formatArgs) const {
34         if (!m_debugUtilsEnabled) {
35             return;
36         }
37 
38         VkObjectType objectType = VK_OBJECT_TYPE_UNKNOWN;
39         if constexpr (std::is_same_v<VkObjectT, VkBuffer>) {
40             objectType = VK_OBJECT_TYPE_BUFFER;
41         } else if constexpr (std::is_same_v<VkObjectT, VkCommandBuffer>) {
42             objectType = VK_OBJECT_TYPE_COMMAND_BUFFER;
43         } else if constexpr (std::is_same_v<VkObjectT, VkCommandPool>) {
44             objectType = VK_OBJECT_TYPE_COMMAND_POOL;
45         } else if constexpr (std::is_same_v<VkObjectT, VkDescriptorSet>) {
46             objectType = VK_OBJECT_TYPE_DESCRIPTOR_SET;
47         } else if constexpr (std::is_same_v<VkObjectT, VkDevice>) {
48             objectType = VK_OBJECT_TYPE_DEVICE;
49         } else if constexpr (std::is_same_v<VkObjectT, VkDeviceMemory>) {
50             objectType = VK_OBJECT_TYPE_DEVICE_MEMORY;
51         } else if constexpr (std::is_same_v<VkObjectT, VkFence>) {
52             objectType = VK_OBJECT_TYPE_FENCE;
53         } else if constexpr (std::is_same_v<VkObjectT, VkFramebuffer>) {
54             objectType = VK_OBJECT_TYPE_FRAMEBUFFER;
55         } else if constexpr (std::is_same_v<VkObjectT, VkImage>) {
56             objectType = VK_OBJECT_TYPE_IMAGE;
57         } else if constexpr (std::is_same_v<VkObjectT, VkImageView>) {
58             objectType = VK_OBJECT_TYPE_IMAGE_VIEW;
59         } else if constexpr (std::is_same_v<VkObjectT, VkInstance>) {
60             objectType = VK_OBJECT_TYPE_INSTANCE;
61         } else if constexpr (std::is_same_v<VkObjectT, VkPipeline>) {
62             objectType = VK_OBJECT_TYPE_PIPELINE;
63         } else if constexpr (std::is_same_v<VkObjectT, VkSampler>) {
64             objectType = VK_OBJECT_TYPE_SAMPLER;
65         } else {
66             static_assert(sizeof(VkObjectT) == 0,
67                           "Unhandled VkObjectT. Please update DebugUtilsHelper.h.");
68         }
69 
70         addDebugLabelToHandle((uint64_t)object, objectType, format, std::forward<T>(formatArgs)...);
71     }
72 
73     void cmdBeginDebugLabel(VkCommandBuffer commandBuffer, const char* format, ...) const;
74     void cmdEndDebugLabel(VkCommandBuffer commandBuffer) const;
75 
isEnabled()76     bool isEnabled() const { return m_debugUtilsEnabled; }
77 
78    private:
79     DebugUtilsHelper(bool enabled, VkDevice device, const VulkanDispatch* dispatch);
80 
81     void addDebugLabelToHandle(uint64_t object, VkObjectType objectType, const char* format,
82                                ...) const;
83 
84     bool m_debugUtilsEnabled = false;
85     VkDevice m_vkDevice = VK_NULL_HANDLE;
86     const VulkanDispatch* m_vk = nullptr;
87 };
88 
89 }  // namespace vk
90 }  // namespace gfxstream
91