1 // Copyright (C) 2018 The Android Open Source Project
2 // Copyright (C) 2018 Google Inc.
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 #pragma once
16 
17 #include <vulkan/vulkan.h>
18 
19 #include <functional>
20 
21 #include "VulkanDispatch.h"
22 #include "VulkanHandles.h"
23 
24 namespace gfxstream {
25 namespace vk {
26 
27 class VkDecoderGlobalState;
28 
29 class VulkanHandleMapping {
30    public:
VulkanHandleMapping(VkDecoderGlobalState * state)31     VulkanHandleMapping(VkDecoderGlobalState* state) : m_state(state) {}
~VulkanHandleMapping()32     virtual ~VulkanHandleMapping() {}
33 
34 #define DECLARE_HANDLE_MAP_PURE_VIRTUAL_METHOD(type)                                 \
35     virtual void mapHandles_##type(type* handles, size_t count = 1) = 0;             \
36     virtual void mapHandles_##type##_u64(const type* handles, uint64_t* handle_u64s, \
37                                          size_t count = 1) = 0;                      \
38     virtual void mapHandles_u64_##type(const uint64_t* handle_u64s, type* handles,   \
39                                        size_t count = 1) = 0;
40 
41     GOLDFISH_VK_LIST_HANDLE_TYPES(DECLARE_HANDLE_MAP_PURE_VIRTUAL_METHOD)
42    protected:
43     VkDecoderGlobalState* m_state;
44 };
45 
46 class DefaultHandleMapping : public VulkanHandleMapping {
47    public:
48     DefaultHandleMapping() : VulkanHandleMapping(nullptr) {}
49     virtual ~DefaultHandleMapping() {}
50 
51 #define DECLARE_HANDLE_MAP_OVERRIDE(type)                                                  \
52     void mapHandles_##type(type* handles, size_t count) override;                          \
53     void mapHandles_##type##_u64(const type* handles, uint64_t* handle_u64s, size_t count) \
54         override;                                                                          \
55     void mapHandles_u64_##type(const uint64_t* handle_u64s, type* handles, size_t count) override;
56 
57     GOLDFISH_VK_LIST_HANDLE_TYPES(DECLARE_HANDLE_MAP_OVERRIDE)
58 };
59 
60 #define DEFINE_BOXED_DISPATCHABLE_HANDLE_GLOBAL_API_DECL(type) \
61     type unbox_##type(type boxed);                             \
62     type unboxed_to_boxed_##type(type boxed);                  \
63     void delete_##type(type boxed);                            \
64     VulkanDispatch* dispatch_##type(type boxed);
65 
66 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_DISPATCHABLE_HANDLE_GLOBAL_API_DECL)
67 
68 #define DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_GLOBAL_API_DECL(type)                           \
69     type new_boxed_non_dispatchable_##type(type underlying);                                 \
70     void delete_##type(type boxed);                                                          \
71     void delayed_delete_##type(type boxed, VkDevice device, std::function<void()> callback); \
72     void set_boxed_non_dispatchable_##type(type boxed, type underlying);                     \
73     type unbox_##type(type boxed);                                                           \
74     type unboxed_to_boxed_non_dispatchable_##type(type boxed);
75 
76 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_GLOBAL_API_DECL)
77 
78 }  // namespace vk
79 }  // namespace gfxstream
80