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 | |
| 22 | VkResult DebugReportCallbackList::CreateCallback( |
| 23 | VkInstance instance, |
| 24 | const VkDebugReportCallbackCreateInfoEXT* create_info, |
| 25 | const VkAllocationCallbacks* allocator, |
| 26 | VkDebugReportCallbackEXT* callback) { |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 27 | VkDebugReportCallbackEXT driver_callback = VK_NULL_HANDLE; |
| 28 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 29 | if (GetData(instance).driver.CreateDebugReportCallbackEXT) { |
| 30 | VkResult result = GetData(instance).driver.CreateDebugReportCallbackEXT( |
| 31 | instance, create_info, allocator, &driver_callback); |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 32 | if (result != VK_SUCCESS) |
| 33 | return result; |
| 34 | } |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 35 | |
| 36 | const VkAllocationCallbacks* alloc = |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 37 | allocator ? allocator : &GetData(instance).allocator; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 38 | void* mem = |
| 39 | alloc->pfnAllocation(alloc->pUserData, sizeof(Node), alignof(Node), |
| 40 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
| 41 | if (!mem) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 42 | if (GetData(instance).driver.DestroyDebugReportCallbackEXT) { |
| 43 | GetData(instance).driver.DestroyDebugReportCallbackEXT( |
| 44 | instance, driver_callback, allocator); |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 45 | } |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 46 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 47 | } |
| 48 | |
| 49 | std::lock_guard<decltype(rwmutex_)> lock(rwmutex_); |
| 50 | head_.next = |
| 51 | new (mem) Node{head_.next, create_info->flags, create_info->pfnCallback, |
| 52 | create_info->pUserData, driver_callback}; |
| 53 | *callback = |
| 54 | VkDebugReportCallbackEXT(reinterpret_cast<uintptr_t>(head_.next)); |
| 55 | return VK_SUCCESS; |
| 56 | } |
| 57 | |
| 58 | void DebugReportCallbackList::DestroyCallback( |
| 59 | VkInstance instance, |
| 60 | VkDebugReportCallbackEXT callback, |
| 61 | const VkAllocationCallbacks* allocator) { |
| 62 | Node* node = reinterpret_cast<Node*>(uintptr_t(callback)); |
| 63 | std::unique_lock<decltype(rwmutex_)> lock(rwmutex_); |
| 64 | Node* prev = &head_; |
| 65 | while (prev && prev->next != node) |
| 66 | prev = prev->next; |
| 67 | prev->next = node->next; |
| 68 | lock.unlock(); |
| 69 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 70 | if (GetData(instance).driver.DestroyDebugReportCallbackEXT) { |
| 71 | GetData(instance).driver.DestroyDebugReportCallbackEXT( |
| 72 | instance, node->driver_callback, allocator); |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 73 | } |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 74 | |
| 75 | const VkAllocationCallbacks* alloc = |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 76 | allocator ? allocator : &GetData(instance).allocator; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 77 | alloc->pfnFree(alloc->pUserData, node); |
| 78 | } |
| 79 | |
| 80 | void DebugReportCallbackList::Message(VkDebugReportFlagsEXT flags, |
| 81 | VkDebugReportObjectTypeEXT object_type, |
| 82 | uint64_t object, |
| 83 | size_t location, |
| 84 | int32_t message_code, |
| 85 | const char* layer_prefix, |
| 86 | const char* message) { |
| 87 | std::shared_lock<decltype(rwmutex_)> lock(rwmutex_); |
| 88 | Node* node = &head_; |
| 89 | while ((node = node->next)) { |
| 90 | if ((node->flags & flags) != 0) { |
| 91 | node->callback(flags, object_type, object, location, message_code, |
| 92 | layer_prefix, message, node->data); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 97 | VkResult CreateDebugReportCallbackEXT( |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 98 | VkInstance instance, |
| 99 | const VkDebugReportCallbackCreateInfoEXT* create_info, |
| 100 | const VkAllocationCallbacks* allocator, |
| 101 | VkDebugReportCallbackEXT* callback) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 102 | return GetData(instance).debug_report_callbacks.CreateCallback( |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 103 | instance, create_info, allocator, callback); |
| 104 | } |
| 105 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 106 | void DestroyDebugReportCallbackEXT(VkInstance instance, |
| 107 | VkDebugReportCallbackEXT callback, |
| 108 | const VkAllocationCallbacks* allocator) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 109 | if (callback) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 110 | GetData(instance).debug_report_callbacks.DestroyCallback( |
| 111 | instance, callback, allocator); |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 114 | void DebugReportMessageEXT(VkInstance instance, |
| 115 | VkDebugReportFlagsEXT flags, |
| 116 | VkDebugReportObjectTypeEXT object_type, |
| 117 | uint64_t object, |
| 118 | size_t location, |
| 119 | int32_t message_code, |
| 120 | const char* layer_prefix, |
| 121 | const char* message) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 122 | if (GetData(instance).driver.DebugReportMessageEXT) { |
| 123 | GetData(instance).driver.DebugReportMessageEXT( |
| 124 | instance, flags, object_type, object, location, message_code, |
| 125 | layer_prefix, message); |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 126 | } |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 127 | GetData(instance).debug_report_callbacks.Message(flags, object_type, object, |
| 128 | location, message_code, |
| 129 | layer_prefix, message); |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 132 | } // namespace driver |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 133 | } // namespace vulkan |