huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "driver.h"
 
namespace vulkan {
namespace driver {
 
DebugReportCallbackList::Node* DebugReportCallbackList::AddCallback(
    const VkDebugReportCallbackCreateInfoEXT& info,
    VkDebugReportCallbackEXT driver_handle,
    const VkAllocationCallbacks& allocator) {
    void* mem = allocator.pfnAllocation(allocator.pUserData, sizeof(Node),
                                        alignof(Node),
                                        VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
    if (!mem)
        return nullptr;
 
    // initialize and prepend node to the list
    std::lock_guard<decltype(rwmutex_)> lock(rwmutex_);
    head_.next = new (mem) Node{head_.next, info.flags, info.pfnCallback,
                                info.pUserData, driver_handle};
 
    return head_.next;
}
 
void DebugReportCallbackList::RemoveCallback(
    Node* node,
    const VkAllocationCallbacks& allocator) {
    // remove node from the list
    {
        std::lock_guard<decltype(rwmutex_)> lock(rwmutex_);
        Node* prev = &head_;
        while (prev && prev->next != node)
            prev = prev->next;
        if (prev)
            prev->next = node->next;
    }
 
    allocator.pfnFree(allocator.pUserData, node);
}
 
void DebugReportCallbackList::Message(VkDebugReportFlagsEXT flags,
                                      VkDebugReportObjectTypeEXT object_type,
                                      uint64_t object,
                                      size_t location,
                                      int32_t message_code,
                                      const char* layer_prefix,
                                      const char* message) const {
    std::shared_lock<decltype(rwmutex_)> lock(rwmutex_);
    const Node* node = &head_;
    while ((node = node->next)) {
        if ((node->flags & flags) != 0) {
            node->callback(flags, object_type, object, location, message_code,
                           layer_prefix, message, node->user_data);
        }
    }
}
 
void DebugReportLogger::Message(VkDebugReportFlagsEXT flags,
                                VkDebugReportObjectTypeEXT object_type,
                                uint64_t object,
                                size_t location,
                                int32_t message_code,
                                const char* layer_prefix,
                                const char* message) const {
    const VkDebugReportCallbackCreateInfoEXT* info =
        reinterpret_cast<const VkDebugReportCallbackCreateInfoEXT*>(
            instance_pnext_);
    while (info) {
        if (info->sType ==
                VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT &&
            (info->flags & flags) != 0) {
            info->pfnCallback(flags, object_type, object, location,
                              message_code, layer_prefix, message,
                              info->pUserData);
        }
 
        info = reinterpret_cast<const VkDebugReportCallbackCreateInfoEXT*>(
            info->pNext);
    }
 
    if (callbacks_) {
        callbacks_->Message(flags, object_type, object, location, message_code,
                            layer_prefix, message);
    }
}
 
void DebugReportLogger::PrintV(VkDebugReportFlagsEXT flags,
                               VkDebugReportObjectTypeEXT object_type,
                               uint64_t object,
                               const char* format,
                               va_list ap) const {
    char buf[1024];
    int len = vsnprintf(buf, sizeof(buf), format, ap);
 
    // message truncated
    if (len >= static_cast<int>(sizeof(buf)))
        memcpy(buf + sizeof(buf) - 4, "...", 4);
 
    Message(flags, object_type, object, 0, 0, LOG_TAG, buf);
}
 
VkResult CreateDebugReportCallbackEXT(
    VkInstance instance,
    const VkDebugReportCallbackCreateInfoEXT* create_info,
    const VkAllocationCallbacks* allocator,
    VkDebugReportCallbackEXT* callback) {
    const auto& driver = GetData(instance).driver;
    VkDebugReportCallbackEXT driver_handle = VK_NULL_HANDLE;
    if (driver.CreateDebugReportCallbackEXT) {
        VkResult result = driver.CreateDebugReportCallbackEXT(
            instance, create_info, allocator, &driver_handle);
        if (result != VK_SUCCESS)
            return result;
    }
 
    auto& callbacks = GetData(instance).debug_report_callbacks;
    auto node = callbacks.AddCallback(
        *create_info, driver_handle,
        (allocator) ? *allocator : GetData(instance).allocator);
    if (!node) {
        if (driver_handle != VK_NULL_HANDLE) {
            driver.DestroyDebugReportCallbackEXT(instance, driver_handle,
                                                 allocator);
        }
 
        return VK_ERROR_OUT_OF_HOST_MEMORY;
    }
 
    *callback = callbacks.GetHandle(node);
 
    return VK_SUCCESS;
}
 
void DestroyDebugReportCallbackEXT(VkInstance instance,
                                   VkDebugReportCallbackEXT callback,
                                   const VkAllocationCallbacks* allocator) {
    if (callback == VK_NULL_HANDLE)
        return;
 
    auto& callbacks = GetData(instance).debug_report_callbacks;
    auto node = callbacks.FromHandle(callback);
    auto driver_handle = callbacks.GetDriverHandle(node);
 
    callbacks.RemoveCallback(
        node, (allocator) ? *allocator : GetData(instance).allocator);
 
    if (driver_handle != VK_NULL_HANDLE) {
        GetData(instance).driver.DestroyDebugReportCallbackEXT(
            instance, driver_handle, allocator);
    }
}
 
void DebugReportMessageEXT(VkInstance instance,
                           VkDebugReportFlagsEXT flags,
                           VkDebugReportObjectTypeEXT object_type,
                           uint64_t object,
                           size_t location,
                           int32_t message_code,
                           const char* layer_prefix,
                           const char* message) {
    if (GetData(instance).driver.DebugReportMessageEXT) {
        GetData(instance).driver.DebugReportMessageEXT(
            instance, flags, object_type, object, location, message_code,
            layer_prefix, message);
    } else {
        GetData(instance).debug_report_callbacks.Message(
            flags, object_type, object, location, message_code, layer_prefix,
            message);
    }
}
 
}  // namespace driver
}  // namespace vulkan