Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1 | /* |
| 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 Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 17 | #include "driver.h" |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 18 | |
| 19 | namespace vulkan { |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 20 | namespace driver { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 21 | |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 22 | DebugReportCallbackList::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 Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 31 | |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 32 | // initialize and prepend node to the list |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 33 | std::lock_guard<decltype(rwmutex_)> lock(rwmutex_); |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 34 | head_.next = new (mem) Node{head_.next, info.flags, info.pfnCallback, |
| 35 | info.pUserData, driver_handle}; |
| 36 | |
| 37 | return head_.next; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 40 | void 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 Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 50 | } |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 51 | |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 52 | allocator.pfnFree(allocator.pUserData, node); |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void 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 Wu | b3055f3 | 2016-05-03 12:04:47 +0800 | [diff] [blame^] | 61 | const char* message) const { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 62 | std::shared_lock<decltype(rwmutex_)> lock(rwmutex_); |
Chia-I Wu | b3055f3 | 2016-05-03 12:04:47 +0800 | [diff] [blame^] | 63 | const Node* node = &head_; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 64 | while ((node = node->next)) { |
| 65 | if ((node->flags & flags) != 0) { |
| 66 | node->callback(flags, object_type, object, location, message_code, |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 67 | layer_prefix, message, node->user_data); |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 72 | VkResult CreateDebugReportCallbackEXT( |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 73 | VkInstance instance, |
| 74 | const VkDebugReportCallbackCreateInfoEXT* create_info, |
| 75 | const VkAllocationCallbacks* allocator, |
| 76 | VkDebugReportCallbackEXT* callback) { |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 77 | const auto& driver = GetData(instance).driver; |
| 78 | VkDebugReportCallbackEXT driver_handle = VK_NULL_HANDLE; |
| 79 | if (driver.CreateDebugReportCallbackEXT) { |
| 80 | VkResult result = driver.CreateDebugReportCallbackEXT( |
| 81 | instance, create_info, allocator, &driver_handle); |
| 82 | if (result != VK_SUCCESS) |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | auto& callbacks = GetData(instance).debug_report_callbacks; |
| 87 | auto node = callbacks.AddCallback( |
| 88 | *create_info, driver_handle, |
| 89 | (allocator) ? *allocator : GetData(instance).allocator); |
| 90 | if (!node) { |
| 91 | if (driver_handle != VK_NULL_HANDLE) { |
| 92 | driver.DestroyDebugReportCallbackEXT(instance, driver_handle, |
| 93 | allocator); |
| 94 | } |
| 95 | |
| 96 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 97 | } |
| 98 | |
| 99 | *callback = callbacks.GetHandle(node); |
| 100 | |
| 101 | return VK_SUCCESS; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 104 | void DestroyDebugReportCallbackEXT(VkInstance instance, |
| 105 | VkDebugReportCallbackEXT callback, |
| 106 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | a0d40aa | 2016-05-05 12:43:03 +0800 | [diff] [blame] | 107 | if (callback == VK_NULL_HANDLE) |
| 108 | return; |
| 109 | |
| 110 | auto& callbacks = GetData(instance).debug_report_callbacks; |
| 111 | auto node = callbacks.FromHandle(callback); |
| 112 | auto driver_handle = callbacks.GetDriverHandle(node); |
| 113 | |
| 114 | callbacks.RemoveCallback( |
| 115 | node, (allocator) ? *allocator : GetData(instance).allocator); |
| 116 | |
| 117 | if (driver_handle != VK_NULL_HANDLE) { |
| 118 | GetData(instance).driver.DestroyDebugReportCallbackEXT( |
| 119 | instance, driver_handle, allocator); |
| 120 | } |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 123 | void DebugReportMessageEXT(VkInstance instance, |
| 124 | VkDebugReportFlagsEXT flags, |
| 125 | VkDebugReportObjectTypeEXT object_type, |
| 126 | uint64_t object, |
| 127 | size_t location, |
| 128 | int32_t message_code, |
| 129 | const char* layer_prefix, |
| 130 | const char* message) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 131 | if (GetData(instance).driver.DebugReportMessageEXT) { |
| 132 | GetData(instance).driver.DebugReportMessageEXT( |
| 133 | instance, flags, object_type, object, location, message_code, |
| 134 | layer_prefix, message); |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 135 | } |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 136 | GetData(instance).debug_report_callbacks.Message(flags, object_type, object, |
| 137 | location, message_code, |
| 138 | layer_prefix, message); |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 141 | } // namespace driver |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 142 | } // namespace vulkan |