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 | |
| 17 | #include "loader.h" |
| 18 | |
| 19 | namespace vulkan { |
| 20 | |
| 21 | VkResult DebugReportCallbackList::CreateCallback( |
| 22 | VkInstance instance, |
| 23 | const VkDebugReportCallbackCreateInfoEXT* create_info, |
| 24 | const VkAllocationCallbacks* allocator, |
| 25 | VkDebugReportCallbackEXT* callback) { |
| 26 | VkDebugReportCallbackEXT driver_callback; |
| 27 | VkResult result = GetDriverDispatch(instance).CreateDebugReportCallbackEXT( |
| 28 | GetDriverInstance(instance), create_info, allocator, &driver_callback); |
| 29 | if (result != VK_SUCCESS) |
| 30 | return result; |
| 31 | |
| 32 | const VkAllocationCallbacks* alloc = |
| 33 | allocator ? allocator : GetAllocator(instance); |
| 34 | void* mem = |
| 35 | alloc->pfnAllocation(alloc->pUserData, sizeof(Node), alignof(Node), |
| 36 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
| 37 | if (!mem) { |
| 38 | GetDriverDispatch(instance).DestroyDebugReportCallbackEXT( |
| 39 | GetDriverInstance(instance), driver_callback, allocator); |
| 40 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 41 | } |
| 42 | |
| 43 | std::lock_guard<decltype(rwmutex_)> lock(rwmutex_); |
| 44 | head_.next = |
| 45 | new (mem) Node{head_.next, create_info->flags, create_info->pfnCallback, |
| 46 | create_info->pUserData, driver_callback}; |
| 47 | *callback = |
| 48 | VkDebugReportCallbackEXT(reinterpret_cast<uintptr_t>(head_.next)); |
| 49 | return VK_SUCCESS; |
| 50 | } |
| 51 | |
| 52 | void DebugReportCallbackList::DestroyCallback( |
| 53 | VkInstance instance, |
| 54 | VkDebugReportCallbackEXT callback, |
| 55 | const VkAllocationCallbacks* allocator) { |
| 56 | Node* node = reinterpret_cast<Node*>(uintptr_t(callback)); |
| 57 | std::unique_lock<decltype(rwmutex_)> lock(rwmutex_); |
| 58 | Node* prev = &head_; |
| 59 | while (prev && prev->next != node) |
| 60 | prev = prev->next; |
| 61 | prev->next = node->next; |
| 62 | lock.unlock(); |
| 63 | |
| 64 | GetDriverDispatch(instance).DestroyDebugReportCallbackEXT( |
| 65 | GetDriverInstance(instance), node->driver_callback, allocator); |
| 66 | |
| 67 | const VkAllocationCallbacks* alloc = |
| 68 | allocator ? allocator : GetAllocator(instance); |
| 69 | alloc->pfnFree(alloc->pUserData, node); |
| 70 | } |
| 71 | |
| 72 | void DebugReportCallbackList::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) { |
| 79 | std::shared_lock<decltype(rwmutex_)> lock(rwmutex_); |
| 80 | Node* node = &head_; |
| 81 | while ((node = node->next)) { |
| 82 | if ((node->flags & flags) != 0) { |
| 83 | node->callback(flags, object_type, object, location, message_code, |
| 84 | layer_prefix, message, node->data); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | VkResult CreateDebugReportCallbackEXT_Bottom( |
| 90 | VkInstance instance, |
| 91 | const VkDebugReportCallbackCreateInfoEXT* create_info, |
| 92 | const VkAllocationCallbacks* allocator, |
| 93 | VkDebugReportCallbackEXT* callback) { |
| 94 | return GetDebugReportCallbacks(instance).CreateCallback( |
| 95 | instance, create_info, allocator, callback); |
| 96 | } |
| 97 | |
| 98 | void DestroyDebugReportCallbackEXT_Bottom( |
| 99 | VkInstance instance, |
| 100 | VkDebugReportCallbackEXT callback, |
| 101 | const VkAllocationCallbacks* allocator) { |
| 102 | if (callback) |
| 103 | GetDebugReportCallbacks(instance).DestroyCallback(instance, callback, |
| 104 | allocator); |
| 105 | } |
| 106 | |
| 107 | void DebugReportMessageEXT_Bottom(VkInstance instance, |
| 108 | VkDebugReportFlagsEXT flags, |
| 109 | VkDebugReportObjectTypeEXT object_type, |
| 110 | uint64_t object, |
| 111 | size_t location, |
| 112 | int32_t message_code, |
| 113 | const char* layer_prefix, |
| 114 | const char* message) { |
| 115 | GetDriverDispatch(instance).DebugReportMessageEXT( |
| 116 | GetDriverInstance(instance), flags, object_type, object, location, |
| 117 | message_code, layer_prefix, message); |
| 118 | GetDebugReportCallbacks(instance).Message(flags, object_type, object, |
| 119 | location, message_code, |
| 120 | layer_prefix, message); |
| 121 | } |
| 122 | |
| 123 | } // namespace vulkan |