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 #include "DebugUtilsHelper.h"
16
17 #include <inttypes.h>
18
19 #include <cstdarg>
20 #include <cstdio>
21
22 #include "host-common/logging.h"
23
24 namespace gfxstream {
25 namespace vk {
26
withUtilsDisabled()27 /*static*/ DebugUtilsHelper DebugUtilsHelper::withUtilsDisabled() {
28 return DebugUtilsHelper(false, VK_NULL_HANDLE, nullptr);
29 }
30
withUtilsEnabled(VkDevice device,const VulkanDispatch * dispatch)31 /*static*/ DebugUtilsHelper DebugUtilsHelper::withUtilsEnabled(VkDevice device,
32 const VulkanDispatch* dispatch) {
33 return DebugUtilsHelper(true, device, dispatch);
34 }
35
DebugUtilsHelper(bool enabled,VkDevice device,const VulkanDispatch * dispatch)36 DebugUtilsHelper::DebugUtilsHelper(bool enabled, VkDevice device, const VulkanDispatch* dispatch)
37 : m_debugUtilsEnabled(enabled), m_vkDevice(device), m_vk(dispatch) {}
38
addDebugLabelToHandle(uint64_t object,VkObjectType objectType,const char * format,...) const39 void DebugUtilsHelper::addDebugLabelToHandle(uint64_t object, VkObjectType objectType,
40 const char* format, ...) const {
41 if (!m_debugUtilsEnabled) {
42 return;
43 }
44
45 char objectLabelBuffer[256];
46
47 va_list vararg;
48 va_start(vararg, format);
49 vsnprintf(objectLabelBuffer, sizeof(objectLabelBuffer), format, vararg);
50 va_end(vararg);
51
52 const VkDebugUtilsObjectNameInfoEXT objectNameInfo = {
53 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
54 .pNext = nullptr,
55 .objectType = objectType,
56 .objectHandle = object,
57 .pObjectName = objectLabelBuffer,
58 };
59
60 VkResult result = m_vk->vkSetDebugUtilsObjectNameEXT(m_vkDevice, &objectNameInfo);
61 if (result != VK_SUCCESS) {
62 ERR("Failed to add debug label to %" PRIu64, object);
63 }
64 }
65
cmdBeginDebugLabel(VkCommandBuffer commandBuffer,const char * format,...) const66 void DebugUtilsHelper::cmdBeginDebugLabel(VkCommandBuffer commandBuffer, const char* format,
67 ...) const {
68 if (!m_debugUtilsEnabled) {
69 return;
70 }
71
72 va_list vararg;
73 va_start(vararg, format);
74 char labelBuffer[256];
75 vsnprintf(labelBuffer, sizeof(labelBuffer), format, vararg);
76 va_end(vararg);
77
78 const VkDebugUtilsLabelEXT labelInfo = {
79 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
80 .pNext = nullptr,
81 .pLabelName = labelBuffer,
82 .color = { 0.0f, 0.0f, 0.0f, 1.0f },
83 };
84 m_vk->vkCmdBeginDebugUtilsLabelEXT(commandBuffer, &labelInfo);
85 }
86
cmdEndDebugLabel(VkCommandBuffer commandBuffer) const87 void DebugUtilsHelper::cmdEndDebugLabel(VkCommandBuffer commandBuffer) const {
88 if (!m_debugUtilsEnabled) {
89 return;
90 }
91
92 m_vk->vkCmdEndDebugUtilsLabelEXT(commandBuffer);
93 }
94
95 } // namespace vk
96 } // namespace gfxstream
97