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 #include "VulkanHandleMapping.h" 16 17 #include <vulkan/vulkan.h> 18 19 #include "VkDecoderGlobalState.h" 20 #include "VulkanBoxedHandles.h" 21 22 namespace gfxstream { 23 namespace vk { 24 25 #define DEFAULT_HANDLE_MAP_DEFINE(type) \ 26 void DefaultHandleMapping::mapHandles_##type(type*, size_t) { return; } \ 27 void DefaultHandleMapping::mapHandles_##type##_u64(const type* handles, uint64_t* handle_u64s, \ 28 size_t count) { \ 29 for (size_t i = 0; i < count; ++i) { \ 30 handle_u64s[i] = (uint64_t)(uintptr_t)handles[i]; \ 31 } \ 32 } \ 33 void DefaultHandleMapping::mapHandles_u64_##type(const uint64_t* handle_u64s, type* handles, \ 34 size_t count) { \ 35 for (size_t i = 0; i < count; ++i) { \ 36 handles[i] = (type)(uintptr_t)handle_u64s[i]; \ 37 } \ 38 } 39 40 GOLDFISH_VK_LIST_HANDLE_TYPES(DEFAULT_HANDLE_MAP_DEFINE) 41 42 43 #define MAKE_HANDLE_MAPPING_FOREACH(class_name, type_name, map_impl, map_to_u64_impl, map_from_u64_impl) \ 44 void class_name::mapHandles_##type_name(type_name* handles, size_t count) { \ 45 for (size_t i = 0; i < count; ++i) { \ 46 map_impl \ 47 } \ 48 } \ 49 void class_name::mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s, size_t count) { \ 50 for (size_t i = 0; i < count; ++i) { \ 51 map_to_u64_impl \ 52 } \ 53 } \ 54 void class_name::mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) { \ 55 for (size_t i = 0; i < count; ++i) { \ 56 map_from_u64_impl \ 57 } \ 58 } 59 60 #define BOXED_DISPATCHABLE_UNWRAP_IMPL(type_name) \ 61 MAKE_HANDLE_MAPPING_FOREACH( \ 62 BoxedHandleUnwrapMapping, \ 63 type_name, \ 64 if (handles[i]) { \ 65 handles[i] = unbox_##type_name(handles[i]); \ 66 } else { \ 67 handles[i] = (type_name) nullptr; \ 68 } \ 69 , \ 70 if (handles[i]) { \ 71 handle_u64s[i] = (uint64_t)unbox_##type_name(handles[i]); \ 72 } else { \ 73 handle_u64s[i] = 0; \ 74 }, \ 75 if (handle_u64s[i]) { \ 76 handles[i] = unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]); \ 77 } else { \ 78 handles[i] = (type_name) nullptr; \ 79 }) 80 81 #define BOXED_NON_DISPATCHABLE_UNWRAP_IMPL(type_name) \ 82 MAKE_HANDLE_MAPPING_FOREACH( \ 83 BoxedHandleUnwrapMapping, \ 84 type_name, \ 85 if (handles[i]) { \ 86 handles[i] = unbox_##type_name(handles[i]); \ 87 } else { \ 88 handles[i] = (type_name) nullptr; \ 89 } \ 90 , \ 91 if (handles[i]) { \ 92 handle_u64s[i] = (uint64_t)unbox_##type_name(handles[i]); \ 93 } else { \ 94 handle_u64s[i] = 0; \ 95 }, \ 96 if (handle_u64s[i]) { \ 97 handles[i] = unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]); \ 98 } else { \ 99 handles[i] = (type_name) nullptr; \ 100 }) 101 102 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_UNWRAP_IMPL) 103 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_UNWRAP_IMPL) 104 105 // Not used, so we do not define. 106 #define BOXED_DISPATCHABLE_CREATE_IMPL(type_name) \ 107 MAKE_HANDLE_MAPPING_FOREACH( \ 108 BoxedHandleCreateMapping, \ 109 type_name, \ 110 (void)handles[i]; , \ 111 (void)handle_u64s[i]; , \ 112 (void)handles[i]; \ 113 ) 114 115 // We only use the create/destroy mappings for non dispatchable handles. 116 #define BOXED_NON_DISPATCHABLE_CREATE_IMPL(type_name) \ 117 MAKE_HANDLE_MAPPING_FOREACH( \ 118 BoxedHandleCreateMapping, \ 119 type_name, \ 120 handles[i] = new_boxed_non_dispatchable_##type_name(handles[i]); , \ 121 handle_u64s[i] = (uint64_t)new_boxed_non_dispatchable_##type_name(handles[i]); , \ 122 handles[i] = (type_name)new_boxed_non_dispatchable_##type_name( \ 123 (type_name)(uintptr_t)handle_u64s[i]); \ 124 ) 125 126 #define BOXED_NON_DISPATCHABLE_UNWRAP_AND_DELETE_IMPL(type_name) \ 127 MAKE_HANDLE_MAPPING_FOREACH( \ 128 BoxedHandleCreateMapping, \ 129 type_name, \ 130 if (handles[i]) { \ 131 auto boxed = handles[i]; \ 132 handles[i] = unbox_##type_name(handles[i]); \ 133 delete_##type_name(boxed); \ 134 } else { \ 135 handles[i] = (type_name) nullptr; \ 136 } \ 137 , \ 138 if (handles[i]) { \ 139 auto boxed = handles[i]; \ 140 handle_u64s[i] = (uint64_t)unbox_##type_name(handles[i]); \ 141 delete_##type_name(boxed); \ 142 } else { \ 143 handle_u64s[i] = 0; \ 144 }, \ 145 if (handle_u64s[i]) { \ 146 auto boxed = (type_name)(uintptr_t)handle_u64s[i]; \ 147 handles[i] = unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]); \ 148 delete_##type_name(boxed); \ 149 } else { \ 150 handles[i] = (type_name) nullptr; \ 151 }) 152 153 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_CREATE_IMPL) 154 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_CREATE_IMPL) 155 156 } // namespace vk 157 } // namespace gfxstream 158