debug_report.h revision b3055f34650cd066a349e1e8cba294b05513ef2e
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#ifndef LIBVULKAN_DEBUG_REPORT_H
18#define LIBVULKAN_DEBUG_REPORT_H 1
19
20#include <vulkan/vulkan.h>
21#include <shared_mutex>
22
23namespace vulkan {
24namespace driver {
25
26// clang-format off
27VKAPI_ATTR VkResult CreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugReportCallbackEXT* pCallback);
28VKAPI_ATTR void DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback, const VkAllocationCallbacks* pAllocator);
29VKAPI_ATTR void DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object, size_t location, int32_t messageCode, const char* pLayerPrefix, const char* pMessage);
30// clang-format on
31
32class DebugReportCallbackList {
33   private:
34    // forward declaration
35    struct Node;
36
37   public:
38    DebugReportCallbackList()
39        : head_{nullptr, 0, nullptr, nullptr, VK_NULL_HANDLE} {}
40    DebugReportCallbackList(const DebugReportCallbackList&) = delete;
41    DebugReportCallbackList& operator=(const DebugReportCallbackList&) = delete;
42    ~DebugReportCallbackList() = default;
43
44    Node* AddCallback(const VkDebugReportCallbackCreateInfoEXT& info,
45                      VkDebugReportCallbackEXT driver_handle,
46                      const VkAllocationCallbacks& allocator);
47    void RemoveCallback(Node* node, const VkAllocationCallbacks& allocator);
48
49    void Message(VkDebugReportFlagsEXT flags,
50                 VkDebugReportObjectTypeEXT object_type,
51                 uint64_t object,
52                 size_t location,
53                 int32_t message_code,
54                 const char* layer_prefix,
55                 const char* message) const;
56
57    static Node* FromHandle(VkDebugReportCallbackEXT handle) {
58        return reinterpret_cast<Node*>(uintptr_t(handle));
59    }
60
61    static VkDebugReportCallbackEXT GetHandle(const Node* node) {
62        return VkDebugReportCallbackEXT(reinterpret_cast<uintptr_t>(node));
63    }
64
65    static VkDebugReportCallbackEXT GetDriverHandle(const Node* node) {
66        return node->driver_handle;
67    }
68
69   private:
70    struct Node {
71        Node* next;
72
73        VkDebugReportFlagsEXT flags;
74        PFN_vkDebugReportCallbackEXT callback;
75        void* user_data;
76
77        VkDebugReportCallbackEXT driver_handle;
78    };
79
80    // TODO(jessehall): replace with std::shared_mutex when available in libc++
81    mutable std::shared_timed_mutex rwmutex_;
82    Node head_;
83};
84
85}  // namespace driver
86}  // namespace vulkan
87
88#endif  // LIBVULKAN_DEBUG_REPORT_H
89