This repository was archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathUltralightGPUDriverNativeUtil.java
More file actions
230 lines (199 loc) · 9.1 KB
/
UltralightGPUDriverNativeUtil.java
File metadata and controls
230 lines (199 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Ultralight Java - Java wrapper for the Ultralight web engine
* Copyright (C) 2020 - 2021 LabyMedia and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.labymedia.ultralight.gpu;
import com.labymedia.ultralight.UltralightLoadException;
import com.labymedia.ultralight.os.Architecture;
import com.labymedia.ultralight.os.OperatingSystem;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class UltralightGPUDriverNativeUtil {
private static UltralightGPUDriverNativeUtil instance;
private UltralightGPUDriverNativeUtil() {
}
/**
* Extracts all native libraries for ultralight-java-gpu to a given directory.
*
* @param nativesDir the path to extract the native libraries to
* // * @throws IOException if native libraries could not be extracted or read
*/
public static void extractNativeLibrary(Path nativesDir) throws UltralightLoadException {
OperatingSystem operatingSystem = OperatingSystem.get();
Architecture architecture = Architecture.get();
String nameWithArch = operatingSystem.mapLibraryName("ultralight-java-gpu-" + architecture.getBits());
try {
if (extractResource(nameWithArch, nativesDir.resolve(nameWithArch))) {
return;
}
String nameWithoutArch = operatingSystem.mapLibraryName("ultralight-java-gpu");
if (extractResource(nameWithoutArch, nativesDir.resolve(nameWithoutArch))) {
return;
}
} catch (IOException e) {
throw new UltralightLoadException("Failed to extract native library", e);
}
throw new UltralightLoadException("Failed to extract native library.");
}
/**
* Extracts the given resource path to the given file. Parent directories are created as required.
*
* @param resourcePath The resource path to extract
* @param targetFile The path to the file to extract the resource to
* @return {@code true} if the resource has been extracted, {@code false} otherwise
* @throws IOException If an I/O error occurs during extraction
*/
private static boolean extractResource(String resourcePath, Path targetFile) throws IOException {
try (InputStream stream = UltralightGPUDriverNativeUtil.class.getClassLoader().getResourceAsStream("native-binaries/" + resourcePath)) {
if (stream == null) {
return false;
}
// Retrieve the target directory
Path targetDir = targetFile.getParent();
if (!Files.isDirectory(targetDir)) {
// Create the target directory
Files.createDirectories(targetDir);
}
// Copy the resource stream
Files.copy(stream, targetFile, StandardCopyOption.REPLACE_EXISTING);
}
return true;
}
/**
* Tries to find the given library in the given directory.
*
* @param nativesDir The directory to search the library in
* @param libraryName The name of the library to find
* @param operatingSystem The operating system to find the library for
* @param architecture The architecture to find the library for
* @return A path to the found library
*/
private static Path determineLibraryPath(
Path nativesDir,
String libraryName,
OperatingSystem operatingSystem,
Architecture architecture
) {
// First try to find the library from ${nativesDir}/${prefix}${name}-${bits}${suffix}
Path pathWithArchitecture = nativesDir.resolve(
operatingSystem.mapLibraryName(libraryName + "-" + architecture.getBits()));
if (Files.isRegularFile(pathWithArchitecture)) {
// Found it
return pathWithArchitecture;
}
// Then try to find the library from ${nativesDir}/${prefix}${name}${suffix}
Path pathWithoutArchitecture = nativesDir.resolve(operatingSystem.mapLibraryName(libraryName));
if (Files.isRegularFile(pathWithoutArchitecture)) {
return pathWithoutArchitecture;
}
throw new RuntimeException("Failed to find library " + libraryName);
}
/**
* Load all native libraries for ultralight-java-gpu from a given directory.
*
* @param nativesDir the directory to load native libraries from
*/
public static void load(Path nativesDir) {
OperatingSystem operatingSystem = OperatingSystem.get();
Architecture architecture = Architecture.get();
Path ultralightGpuLibrary =
determineLibraryPath(nativesDir, "ultralight-java-gpu", operatingSystem, architecture);
try {
System.load(ultralightGpuLibrary.toAbsolutePath().toString());
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("Failed to load ultralight-java-gpu native library.", e);
}
}
/**
* Extracts all native libraries for ultralight-java-gpu to a given directory and loads them through JNI.
*
* @param nativesDir the native directory to save the libraries to
* @throws UltralightLoadException if native libraries could not be extracted or read
*/
public static void extractAndLoadNativeLibraries(Path nativesDir) throws UltralightLoadException {
extractNativeLibrary(nativesDir);
load(nativesDir);
}
/**
* Get the global singleton of {@link UltralightGPUDriverNativeUtil}.
* Will be initialized lazy.
*
* @return the global singleton of {@link UltralightGPUDriverNativeUtil}
*/
public static UltralightGPUDriverNativeUtil getInstance() {
if (UltralightGPUDriverNativeUtil.instance == null) {
UltralightGPUDriverNativeUtil.instance = new UltralightGPUDriverNativeUtil();
}
return UltralightGPUDriverNativeUtil.instance;
}
/**
* @see <a href="https://docs.ultralig.ht/docs/using-a-custom-gpudriver">Ultralight GPU driver implementation guide</a>
*/
public native long createOpenGLContext(long window, boolean msaa, long loaderFunction);
/**
* @param context GPUDriver context handle
* @see <a href="https://docs.ultralig.ht/docs/using-a-custom-gpudriver">Ultralight GPU driver implementation guide</a>
*/
public native long getDriverFromContext(long context);
/**
* @param handle GPUDriver handle
* @see <a href="https://docs.ultralig.ht/docs/using-a-custom-gpudriver">Ultralight GPU driver implementation guide</a>
*/
public native void beginSynchronize(long handle);
/**
* @param handle GPUDriver handle
* @see <a href="https://docs.ultralig.ht/docs/using-a-custom-gpudriver">Ultralight GPU driver implementation guide</a>
*/
public native void endSynchronize(long handle);
/**
* @param handle GPUDriver handle
* @see <a href="https://docs.ultralig.ht/docs/using-a-custom-gpudriver">Ultralight GPU driver implementation guide</a>
*/
public native boolean hasCommandsPending(long handle);
/**
* @param handle GPUDriver handle
* @see <a href="https://docs.ultralig.ht/docs/using-a-custom-gpudriver">Ultralight GPU driver implementation guide</a>
*/
public native void drawCommandList(long handle);
/**
* Bind OpenGL texture for a given OpenGL Context handle to a specified texture index.
*
* @param handle OpenGL context handle
* @param textureId GL_ACTIVE_TEXTURE id
* @param texture Ultralight renderTarget texture id
*/
public native void bindTexture(long handle, long textureId, long texture);
/**
* Get the OpenGL texture id for a given driver-specific texture index.
*
* @param handle OpenGL context handle
* @param texture Ultralight renderTarget texture id
*
* @return OpenGL texture id
*/
public native int getGlTextureId(long handle, long texture);
/**
* Set which GLFW context should be active.
*
* @param handle OpenGL context handle
* @param window GLFW window handle
*/
public native void setActiveWindow(long handle, long window);
}