Updated for openjdk-19-internal
[panamaz] / test-vulkan / src / zvk / PFN_Test.java
1
2 package zvk;
3
4 import java.lang.invoke.*;
5 import jdk.incubator.foreign.*;
6
7 /*
8 typedef void (*PFN_vkDeviceMemoryReportCallbackEXT)(const VkDeviceMemoryReportCallbackDataEXT* pCallbackData, void* pUserData);
9 */
10
11 // or just a record?  does it need to do anything fancy?
12 /*
13 interface FunctionPointer<T> {
14         NativeSymbol symbol();
15         T function();
16 }
17 */
18 record FunctionPointer<T>(NativeSymbol symbol, T function) { }
19
20 @FunctionalInterface
21 public interface PFN_Test extends Memory.Addressable {
22
23         void call(VkDeviceMemoryReportCallbackDataEXT info);
24
25         default MemoryAddress address() {
26                 throw new UnsupportedOperationException("Non-native method");
27         }
28
29         @FunctionalInterface
30         interface Trampoline {
31                 void call(MemoryAddress info, MemoryAddress userData);
32         };
33
34         static FunctionDescriptor DESCRIPTOR() {
35                 return FunctionDescriptor.ofVoid(
36                         Memory.POINTER,
37                         Memory.POINTER);
38         }
39
40         public static FunctionPointer<PFN_Test> upcall(PFN_Test target, ResourceScope scope) {
41                 return new FunctionPointer<> (
42                         Memory.upcall((Trampoline)(info, userData)->{
43                                         try {
44                                                 target.call(
45                                                         new VkDeviceMemoryReportCallbackDataEXT(
46                                                                 MemorySegment.ofAddress(info,
47                                                                         VkDeviceMemoryReportCallbackDataEXT.LAYOUT.byteSize(),
48                                                                         ResourceScope.globalScope())));
49                                         } catch (Exception x) {
50                                         }
51                                 },
52                                 "call",
53                                 "(Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;)I",
54                                 DESCRIPTOR(),
55                                 scope),
56                         target);
57         }
58
59         public static FunctionPointer<PFN_Test> downcall(String name, MemoryAddress addr, ResourceScope scope) {
60                 NativeSymbol symbol = NativeSymbol.ofAddress(name, addr, scope);
61                 MethodHandle fp = Memory.downcall(symbol, DESCRIPTOR());
62
63                 return new FunctionPointer<>(
64                         symbol,
65                         (info) -> {
66                                 try {
67                                         fp.invokeExact((Addressable)Memory.address(info));
68                                 } catch (Throwable t) {
69                                         throw new RuntimeException(t);
70                                 }
71                         });
72         }
73
74         public static NativeSymbol ofUpcall(PFN_Test target, ResourceScope scope) {
75                 Trampoline trampline = (info, userData) -> {
76                         try {
77                                 target.call(
78                                         new VkDeviceMemoryReportCallbackDataEXT(
79                                                 MemorySegment.ofAddress(info,
80                                                         VkDeviceMemoryReportCallbackDataEXT.LAYOUT.byteSize(),
81                                                         ResourceScope.globalScope())));
82                         } catch (Exception x) {
83                         }
84                 };
85                 return Memory.upcall(trampline, "call",
86                         "(Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;)I",
87                         DESCRIPTOR(),
88                         scope);
89         }
90
91         public static PFN_Test ofDowncall(String name, MemoryAddress addr, ResourceScope scope) {
92                 NativeSymbol symbol = NativeSymbol.ofAddress(name, addr, scope);
93                 MethodHandle fp = Memory.downcall(symbol, DESCRIPTOR());
94
95                 return new PFN_Test() {
96                         public MemoryAddress address() { return symbol.address(); };
97
98                         public void call(VkDeviceMemoryReportCallbackDataEXT info) {
99                                 try {
100                                         fp.invokeExact((Addressable)Memory.address(info));
101                                 } catch (Throwable t) {
102                                         throw new RuntimeException(t);
103                                 }
104                         }
105                 };
106         }
107 }