1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "Resources.h"
15 
16 #include <log/log.h>
17 #include <stdlib.h>
18 
19 #define GOLDFISH_VK_OBJECT_DEBUG 0
20 
21 #if GOLDFISH_VK_OBJECT_DEBUG
22 #define D(fmt,...) ALOGD("%s: " fmt, __func__, ##__VA_ARGS__);
23 #else
24 #ifndef D
25 #define D(fmt,...)
26 #endif
27 #endif
28 
29 extern "C" {
30 
31 #define GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_IMPL(type) \
32     type new_from_host_##type(type underlying) { \
33         struct goldfish_##type* res = \
34             static_cast<goldfish_##type*>(malloc(sizeof(goldfish_##type))); \
35         if (!res) { \
36             ALOGE("FATAL: Failed to alloc " #type " handle"); \
37             abort(); \
38         } \
39         res->dispatch.magic = HWVULKAN_DISPATCH_MAGIC; \
40         res->underlying = (uint64_t)underlying; \
41         return reinterpret_cast<type>(res); \
42     } \
43 
44 #define GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_IMPL(type) \
45     type new_from_host_##type(type underlying) { \
46         struct goldfish_##type* res = \
47             static_cast<goldfish_##type*>(malloc(sizeof(goldfish_##type))); \
48         res->underlying = (uint64_t)underlying; \
49         return reinterpret_cast<type>(res); \
50     } \
51 
52 #define GOLDFISH_VK_AS_GOLDFISH_IMPL(type) \
53     struct goldfish_##type* as_goldfish_##type(type toCast) { \
54         return reinterpret_cast<goldfish_##type*>(toCast); \
55     } \
56 
57 #define GOLDFISH_VK_GET_HOST_IMPL(type) \
58     type get_host_##type(type toUnwrap) { \
59         if (!toUnwrap) return VK_NULL_HANDLE; \
60         auto as_goldfish = as_goldfish_##type(toUnwrap); \
61         return (type)(as_goldfish->underlying); \
62     } \
63 
64 #define GOLDFISH_VK_DELETE_GOLDFISH_IMPL(type) \
65     void delete_goldfish_##type(type toDelete) { \
66         D("guest %p", toDelete); \
67         free(as_goldfish_##type(toDelete)); \
68     } \
69 
70 #define GOLDFISH_VK_IDENTITY_IMPL(type) \
71     type vk_handle_identity_##type(type handle) { \
72         return handle; \
73     } \
74 
75 #define GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_U64_IMPL(type) \
76     type new_from_host_u64_##type(uint64_t underlying) { \
77         struct goldfish_##type* res = \
78             static_cast<goldfish_##type*>(malloc(sizeof(goldfish_##type))); \
79         if (!res) { \
80             ALOGE("FATAL: Failed to alloc " #type " handle"); \
81             abort(); \
82         } \
83         res->dispatch.magic = HWVULKAN_DISPATCH_MAGIC; \
84         res->underlying = underlying; \
85         return reinterpret_cast<type>(res); \
86     } \
87 
88 #define GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_U64_IMPL(type) \
89     type new_from_host_u64_##type(uint64_t underlying) { \
90         struct goldfish_##type* res = \
91             static_cast<goldfish_##type*>(malloc(sizeof(goldfish_##type))); \
92         res->underlying = underlying; \
93         D("guest %p: host u64: 0x%llx", res, (unsigned long long)res->underlying); \
94         return reinterpret_cast<type>(res); \
95     } \
96 
97 #define GOLDFISH_VK_GET_HOST_U64_IMPL(type) \
98     uint64_t get_host_u64_##type(type toUnwrap) { \
99         if (!toUnwrap) return 0; \
100         auto as_goldfish = as_goldfish_##type(toUnwrap); \
101         D("guest %p: host u64: 0x%llx", toUnwrap, (unsigned long long)as_goldfish->underlying); \
102         return as_goldfish->underlying; \
103     } \
104 
105 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_IMPL)
106 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_AS_GOLDFISH_IMPL)
107 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_IMPL)
108 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_IMPL)
109 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_IDENTITY_IMPL)
110 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_U64_IMPL)
111 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_U64_IMPL)
112 
113 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_AS_GOLDFISH_IMPL)
114 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_IMPL)
115 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_IDENTITY_IMPL)
116 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_U64_IMPL)
117 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_IMPL)
118 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_U64_IMPL)
119 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_IMPL)
120 
121 } // extern "C"
122