blob: c4a1174191d62ef85931b9e04609300a35d09a0d [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
22VkResult DebugReportCallbackList::CreateCallback(
23 VkInstance instance,
24 const VkDebugReportCallbackCreateInfoEXT* create_info,
25 const VkAllocationCallbacks* allocator,
26 VkDebugReportCallbackEXT* callback) {
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -070027 VkDebugReportCallbackEXT driver_callback = VK_NULL_HANDLE;
28
Chia-I Wu4a6a9162016-03-26 07:17:34 +080029 if (GetData(instance).driver.CreateDebugReportCallbackEXT) {
30 VkResult result = GetData(instance).driver.CreateDebugReportCallbackEXT(
31 instance, create_info, allocator, &driver_callback);
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -070032 if (result != VK_SUCCESS)
33 return result;
34 }
Jesse Hall715b86a2016-01-16 16:34:29 -080035
36 const VkAllocationCallbacks* alloc =
Chia-I Wu4a6a9162016-03-26 07:17:34 +080037 allocator ? allocator : &GetData(instance).allocator;
Jesse Hall715b86a2016-01-16 16:34:29 -080038 void* mem =
39 alloc->pfnAllocation(alloc->pUserData, sizeof(Node), alignof(Node),
40 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
41 if (!mem) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +080042 if (GetData(instance).driver.DestroyDebugReportCallbackEXT) {
43 GetData(instance).driver.DestroyDebugReportCallbackEXT(
44 instance, driver_callback, allocator);
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -070045 }
Jesse Hall715b86a2016-01-16 16:34:29 -080046 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
58void 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 Wu4a6a9162016-03-26 07:17:34 +080070 if (GetData(instance).driver.DestroyDebugReportCallbackEXT) {
71 GetData(instance).driver.DestroyDebugReportCallbackEXT(
72 instance, node->driver_callback, allocator);
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -070073 }
Jesse Hall715b86a2016-01-16 16:34:29 -080074
75 const VkAllocationCallbacks* alloc =
Chia-I Wu4a6a9162016-03-26 07:17:34 +080076 allocator ? allocator : &GetData(instance).allocator;
Jesse Hall715b86a2016-01-16 16:34:29 -080077 alloc->pfnFree(alloc->pUserData, node);
78}
79
80void 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 Wu62262232016-03-26 07:06:44 +080097VkResult CreateDebugReportCallbackEXT(
Jesse Hall715b86a2016-01-16 16:34:29 -080098 VkInstance instance,
99 const VkDebugReportCallbackCreateInfoEXT* create_info,
100 const VkAllocationCallbacks* allocator,
101 VkDebugReportCallbackEXT* callback) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800102 return GetData(instance).debug_report_callbacks.CreateCallback(
Jesse Hall715b86a2016-01-16 16:34:29 -0800103 instance, create_info, allocator, callback);
104}
105
Chia-I Wu62262232016-03-26 07:06:44 +0800106void DestroyDebugReportCallbackEXT(VkInstance instance,
107 VkDebugReportCallbackEXT callback,
108 const VkAllocationCallbacks* allocator) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800109 if (callback)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800110 GetData(instance).debug_report_callbacks.DestroyCallback(
111 instance, callback, allocator);
Jesse Hall715b86a2016-01-16 16:34:29 -0800112}
113
Chia-I Wu62262232016-03-26 07:06:44 +0800114void 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 Wu4a6a9162016-03-26 07:17:34 +0800122 if (GetData(instance).driver.DebugReportMessageEXT) {
123 GetData(instance).driver.DebugReportMessageEXT(
124 instance, flags, object_type, object, location, message_code,
125 layer_prefix, message);
Courtney Goeltzenleuchter6fecdd52016-02-03 15:14:46 -0700126 }
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800127 GetData(instance).debug_report_callbacks.Message(flags, object_type, object,
128 location, message_code,
129 layer_prefix, message);
Jesse Hall715b86a2016-01-16 16:34:29 -0800130}
131
Chia-I Wu62262232016-03-26 07:06:44 +0800132} // namespace driver
Jesse Hall715b86a2016-01-16 16:34:29 -0800133} // namespace vulkan