blob: 0c2f138d84ee6bfd0f6fee3ddd848255d21765f9 [file] [log] [blame]
Jesse Hall715b86a2016-01-16 16:34:29 -08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu4a6a9162016-03-26 07:17:34 +080017#include "driver.h"
Jesse Hall715b86a2016-01-16 16:34:29 -080018
19namespace vulkan {
Chia-I Wu62262232016-03-26 07:06:44 +080020namespace driver {
Jesse Hall715b86a2016-01-16 16:34:29 -080021
Chia-I Wua0d40aa2016-05-05 12:43:03 +080022DebugReportCallbackList::Node* DebugReportCallbackList::AddCallback(
23 const VkDebugReportCallbackCreateInfoEXT& info,
24 VkDebugReportCallbackEXT driver_handle,
25 const VkAllocationCallbacks& allocator) {
26 void* mem = allocator.pfnAllocation(allocator.pUserData, sizeof(Node),
27 alignof(Node),
28 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
29 if (!mem)
30 return nullptr;
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -070031
Chia-I Wua0d40aa2016-05-05 12:43:03 +080032 // initialize and prepend node to the list
Jesse Hall715b86a2016-01-16 16:34:29 -080033 std::lock_guard<decltype(rwmutex_)> lock(rwmutex_);
Chia-I Wua0d40aa2016-05-05 12:43:03 +080034 head_.next = new (mem) Node{head_.next, info.flags, info.pfnCallback,
35 info.pUserData, driver_handle};
36
37 return head_.next;
Jesse Hall715b86a2016-01-16 16:34:29 -080038}
39
Chia-I Wua0d40aa2016-05-05 12:43:03 +080040void DebugReportCallbackList::RemoveCallback(
41 Node* node,
42 const VkAllocationCallbacks& allocator) {
43 // remove node from the list
44 {
45 std::lock_guard<decltype(rwmutex_)> lock(rwmutex_);
46 Node* prev = &head_;
47 while (prev && prev->next != node)
48 prev = prev->next;
49 prev->next = node->next;
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -070050 }
Jesse Hall715b86a2016-01-16 16:34:29 -080051
Chia-I Wua0d40aa2016-05-05 12:43:03 +080052 allocator.pfnFree(allocator.pUserData, node);
Jesse Hall715b86a2016-01-16 16:34:29 -080053}
54
55void DebugReportCallbackList::Message(VkDebugReportFlagsEXT flags,
56 VkDebugReportObjectTypeEXT object_type,
57 uint64_t object,
58 size_t location,
59 int32_t message_code,
60 const char* layer_prefix,
Chia-I Wub3055f32016-05-03 12:04:47 +080061 const char* message) const {
Jesse Hall715b86a2016-01-16 16:34:29 -080062 std::shared_lock<decltype(rwmutex_)> lock(rwmutex_);
Chia-I Wub3055f32016-05-03 12:04:47 +080063 const Node* node = &head_;
Jesse Hall715b86a2016-01-16 16:34:29 -080064 while ((node = node->next)) {
65 if ((node->flags & flags) != 0) {
66 node->callback(flags, object_type, object, location, message_code,
Chia-I Wua0d40aa2016-05-05 12:43:03 +080067 layer_prefix, message, node->user_data);
Jesse Hall715b86a2016-01-16 16:34:29 -080068 }
69 }
70}
71
Chia-I Wubc011fc2016-05-03 12:19:55 +080072void DebugReportLogger::Message(VkDebugReportFlagsEXT flags,
73 VkDebugReportObjectTypeEXT object_type,
74 uint64_t object,
75 size_t location,
76 int32_t message_code,
77 const char* layer_prefix,
78 const char* message) const {
79 const VkDebugReportCallbackCreateInfoEXT* info =
80 reinterpret_cast<const VkDebugReportCallbackCreateInfoEXT*>(
81 instance_pnext_);
82 while (info) {
Jesse Hall26763382016-05-20 07:13:52 -070083 if (info->sType ==
84 VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT &&
Chia-I Wuede02ac2016-05-13 10:45:59 +080085 (info->flags & flags) != 0) {
Chia-I Wubc011fc2016-05-03 12:19:55 +080086 info->pfnCallback(flags, object_type, object, location,
87 message_code, layer_prefix, message,
88 info->pUserData);
89 }
90
91 info = reinterpret_cast<const VkDebugReportCallbackCreateInfoEXT*>(
92 info->pNext);
93 }
94
95 if (callbacks_) {
96 callbacks_->Message(flags, object_type, object, location, message_code,
97 layer_prefix, message);
98 }
99}
100
101void DebugReportLogger::PrintV(VkDebugReportFlagsEXT flags,
102 VkDebugReportObjectTypeEXT object_type,
103 uint64_t object,
104 const char* format,
105 va_list ap) const {
106 char buf[1024];
107 int len = vsnprintf(buf, sizeof(buf), format, ap);
108
109 // message truncated
110 if (len >= static_cast<int>(sizeof(buf)))
111 memcpy(buf + sizeof(buf) - 4, "...", 4);
112
113 Message(flags, object_type, object, 0, 0, LOG_TAG, buf);
114}
115
Chia-I Wu62262232016-03-26 07:06:44 +0800116VkResult CreateDebugReportCallbackEXT(
Jesse Hall715b86a2016-01-16 16:34:29 -0800117 VkInstance instance,
118 const VkDebugReportCallbackCreateInfoEXT* create_info,
119 const VkAllocationCallbacks* allocator,
120 VkDebugReportCallbackEXT* callback) {
Chia-I Wua0d40aa2016-05-05 12:43:03 +0800121 const auto& driver = GetData(instance).driver;
122 VkDebugReportCallbackEXT driver_handle = VK_NULL_HANDLE;
123 if (driver.CreateDebugReportCallbackEXT) {
124 VkResult result = driver.CreateDebugReportCallbackEXT(
125 instance, create_info, allocator, &driver_handle);
126 if (result != VK_SUCCESS)
127 return result;
128 }
129
130 auto& callbacks = GetData(instance).debug_report_callbacks;
131 auto node = callbacks.AddCallback(
132 *create_info, driver_handle,
133 (allocator) ? *allocator : GetData(instance).allocator);
134 if (!node) {
135 if (driver_handle != VK_NULL_HANDLE) {
136 driver.DestroyDebugReportCallbackEXT(instance, driver_handle,
137 allocator);
138 }
139
140 return VK_ERROR_OUT_OF_HOST_MEMORY;
141 }
142
143 *callback = callbacks.GetHandle(node);
144
145 return VK_SUCCESS;
Jesse Hall715b86a2016-01-16 16:34:29 -0800146}
147
Chia-I Wu62262232016-03-26 07:06:44 +0800148void DestroyDebugReportCallbackEXT(VkInstance instance,
149 VkDebugReportCallbackEXT callback,
150 const VkAllocationCallbacks* allocator) {
Chia-I Wua0d40aa2016-05-05 12:43:03 +0800151 if (callback == VK_NULL_HANDLE)
152 return;
153
154 auto& callbacks = GetData(instance).debug_report_callbacks;
155 auto node = callbacks.FromHandle(callback);
156 auto driver_handle = callbacks.GetDriverHandle(node);
157
158 callbacks.RemoveCallback(
159 node, (allocator) ? *allocator : GetData(instance).allocator);
160
161 if (driver_handle != VK_NULL_HANDLE) {
162 GetData(instance).driver.DestroyDebugReportCallbackEXT(
163 instance, driver_handle, allocator);
164 }
Jesse Hall715b86a2016-01-16 16:34:29 -0800165}
166
Chia-I Wu62262232016-03-26 07:06:44 +0800167void DebugReportMessageEXT(VkInstance instance,
168 VkDebugReportFlagsEXT flags,
169 VkDebugReportObjectTypeEXT object_type,
170 uint64_t object,
171 size_t location,
172 int32_t message_code,
173 const char* layer_prefix,
174 const char* message) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800175 if (GetData(instance).driver.DebugReportMessageEXT) {
176 GetData(instance).driver.DebugReportMessageEXT(
177 instance, flags, object_type, object, location, message_code,
178 layer_prefix, message);
Chia-I Wu2e673662016-05-03 12:06:21 +0800179 } else {
180 GetData(instance).debug_report_callbacks.Message(
181 flags, object_type, object, location, message_code, layer_prefix,
182 message);
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700183 }
Jesse Hall715b86a2016-01-16 16:34:29 -0800184}
185
Chia-I Wu62262232016-03-26 07:06:44 +0800186} // namespace driver
Jesse Hall715b86a2016-01-16 16:34:29 -0800187} // namespace vulkan