Skip to content

Commit d5ea50c

Browse files
committed
add FFM implementation of OpenGL interface
1 parent e959ed4 commit d5ea50c

File tree

11 files changed

+927
-76
lines changed

11 files changed

+927
-76
lines changed

native/common/gl.cpp

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,84 @@
55
jboolean glPlatformInit();
66
jboolean glGetFunction(void **funcPtr, const char *name); //platform impl
77

8-
void (*_glActiveTexture)(int);
9-
void (*_glAlphaFunc)(int,int);
10-
void (*_glAttachShader)(int,int);
11-
void (*_glBindBuffer)(int,int);
12-
void (*_glBindFramebuffer)(int,int);
13-
void (*_glBindRenderbuffer)(int,int);
14-
void (*_glBindTexture)(int,int);
15-
void (*_glBlendFunc)(int,int);
16-
void (*_glBufferData)(int,void*,void *,int);
17-
void (*_glClear)(int);
18-
void (*_glClearColor)(float,float,float,float);
19-
void (*_glClearStencil)(int);
20-
void (*_glColorMask)(jboolean,jboolean,jboolean,jboolean);
21-
void (*_glCompileShader)(int);
22-
int (*_glCreateProgram)();
23-
int (*_glCreateShader)(int);
24-
void (*_glCullFace)(int);
25-
void (*_glDeleteBuffers)(int,int *);
26-
void (*_glDeleteFramebuffers)(int,int *);
27-
void (*_glDeleteRenderbuffers)(int,int *);
28-
void (*_glDeleteTextures)(int,int *);
29-
void (*_glDrawElements)(int,int,int,void*);
30-
void (*_glDepthFunc)(int);
31-
void (*_glDisable)(int);
32-
void (*_glDisableVertexAttribArray)(int);
33-
void (*_glDepthMask)();
34-
void (*_glEnable)();
35-
void (*_glEnableVertexAttribArray)();
36-
void (*_glFlush)();
37-
void (*_glFramebufferTexture2D)(int,int,int,int,int);
38-
void (*_glFramebufferRenderbuffer)(int,int,int,int);
39-
void (*_glFrontFace)(int);
40-
int (*_glGetAttribLocation)(int,const char *);
41-
int (*_glGetError)();
42-
const char* (*_glGetProgramInfoLog)(int,int,int*,char*);
43-
const char* (*_glGetShaderInfoLog)(int,int,int*,char*);
44-
const char* (*_glGetString)(int);
45-
void (*_glGetIntegerv)(int,int *);
46-
void (*_glGenBuffers)(int,int *);
47-
void (*_glGenFramebuffers)(int,int *);
48-
void (*_glGenRenderbuffers)(int,int *);
49-
void (*_glGenTextures)(int,int *);
50-
int (*_glGetUniformLocation)(int,const char *);
51-
void (*_glLinkProgram)(int);
52-
void (*_glPixelStorei)(int,int);
53-
void (*_glReadPixels)(int,int,int,int,int,int,int *);
54-
void (*_glRenderbufferStorage)(int,int,int,int);
55-
int (*_glShaderSource)(int,int,const char**,int *);
56-
int (*_glStencilFunc)(int,int,int);
57-
int (*_glStencilMask)(int);
58-
int (*_glStencilOp)(int,int,int);
59-
void (*_glTexImage2D)(int,int,int,int,int,int,int,int,int *);
60-
void (*_glTexSubImage2D)(int,int,int,int,int,int,int,int,int *);
61-
void (*_glTexParameteri)(int,int,int);
62-
void (*_glUseProgram)(int);
63-
void (*_glUniformMatrix4fv)(int,int,int,float *);
64-
void (*_glUniform4fv)(int,int,float *);
65-
void (*_glUniform3fv)(int,int,float *);
66-
void (*_glUniform2fv)(int,int,float *);
67-
void (*_glUniform1f)(int, float);
68-
void (*_glUniform4iv)(int,int,int *);
69-
void (*_glUniform3iv)(int,int,int *);
70-
void (*_glUniform2iv)(int,int,int *);
71-
void (*_glUniform1i)(int,int);
72-
void (*_glVertexAttribPointer)(int, int, int, int, int, void*);
73-
void (*_glViewport)(int, int, int, int);
74-
75-
// _jni_GLJNI_ -> _jni_GLJNI_
76-
77-
JNIEXPORT jboolean JNICALL Java_javaforce_jni_GLJNI_init
78-
(JNIEnv *e, jobject c)
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
JNIEXPORT jboolean JNICALL GLinit();
13+
14+
JNIEXPORT void (*_glActiveTexture)(int);
15+
JNIEXPORT void (*_glAlphaFunc)(int,int);
16+
JNIEXPORT void (*_glAttachShader)(int,int);
17+
JNIEXPORT void (*_glBindBuffer)(int,int);
18+
JNIEXPORT void (*_glBindFramebuffer)(int,int);
19+
JNIEXPORT void (*_glBindRenderbuffer)(int,int);
20+
JNIEXPORT void (*_glBindTexture)(int,int);
21+
JNIEXPORT void (*_glBlendFunc)(int,int);
22+
JNIEXPORT void (*_glBufferData)(int,void*,void*,int);
23+
JNIEXPORT void (*_glClear)(int);
24+
JNIEXPORT void (*_glClearColor)(float,float,float,float);
25+
JNIEXPORT void (*_glClearStencil)(int);
26+
JNIEXPORT void (*_glColorMask)(jboolean,jboolean,jboolean,jboolean);
27+
JNIEXPORT void (*_glCompileShader)(int);
28+
JNIEXPORT int (*_glCreateProgram)();
29+
JNIEXPORT int (*_glCreateShader)(int);
30+
JNIEXPORT void (*_glCullFace)(int);
31+
JNIEXPORT void (*_glDeleteBuffers)(int,int *);
32+
JNIEXPORT void (*_glDeleteFramebuffers)(int,int *);
33+
JNIEXPORT void (*_glDeleteRenderbuffers)(int,int *);
34+
JNIEXPORT void (*_glDeleteTextures)(int,int *);
35+
JNIEXPORT void (*_glDrawElements)(int,int,int,void*);
36+
JNIEXPORT void (*_glDepthFunc)(int);
37+
JNIEXPORT void (*_glDisable)(int);
38+
JNIEXPORT void (*_glDisableVertexAttribArray)(int);
39+
JNIEXPORT void (*_glDepthMask)();
40+
JNIEXPORT void (*_glEnable)();
41+
JNIEXPORT void (*_glEnableVertexAttribArray)();
42+
JNIEXPORT void (*_glFlush)();
43+
JNIEXPORT void (*_glFramebufferTexture2D)(int,int,int,int,int);
44+
JNIEXPORT void (*_glFramebufferRenderbuffer)(int,int,int,int);
45+
JNIEXPORT void (*_glFrontFace)(int);
46+
JNIEXPORT int (*_glGetAttribLocation)(int,const char *);
47+
JNIEXPORT int (*_glGetError)();
48+
JNIEXPORT const char* (*_glGetProgramInfoLog)(int,int,int*,char*);
49+
JNIEXPORT const char* (*_glGetShaderInfoLog)(int,int,int*,char*);
50+
JNIEXPORT const char* (*_glGetString)(int);
51+
JNIEXPORT void (*_glGetIntegerv)(int,int *);
52+
JNIEXPORT void (*_glGenBuffers)(int,int *);
53+
JNIEXPORT void (*_glGenFramebuffers)(int,int *);
54+
JNIEXPORT void (*_glGenRenderbuffers)(int,int *);
55+
JNIEXPORT void (*_glGenTextures)(int,int *);
56+
JNIEXPORT int (*_glGetUniformLocation)(int,const char *);
57+
JNIEXPORT void (*_glLinkProgram)(int);
58+
JNIEXPORT void (*_glPixelStorei)(int,int);
59+
JNIEXPORT void (*_glReadPixels)(int,int,int,int,int,int,int *);
60+
JNIEXPORT void (*_glRenderbufferStorage)(int,int,int,int);
61+
JNIEXPORT int (*_glShaderSource)(int,int,const char**,int *);
62+
JNIEXPORT int (*_glStencilFunc)(int,int,int);
63+
JNIEXPORT int (*_glStencilMask)(int);
64+
JNIEXPORT int (*_glStencilOp)(int,int,int);
65+
JNIEXPORT void (*_glTexImage2D)(int,int,int,int,int,int,int,int,int *);
66+
JNIEXPORT void (*_glTexSubImage2D)(int,int,int,int,int,int,int,int,int *);
67+
JNIEXPORT void (*_glTexParameteri)(int,int,int);
68+
JNIEXPORT void (*_glUseProgram)(int);
69+
JNIEXPORT void (*_glUniformMatrix4fv)(int,int,int,float *);
70+
JNIEXPORT void (*_glUniform4fv)(int,int,float *);
71+
JNIEXPORT void (*_glUniform3fv)(int,int,float *);
72+
JNIEXPORT void (*_glUniform2fv)(int,int,float *);
73+
JNIEXPORT void (*_glUniform1f)(int, float);
74+
JNIEXPORT void (*_glUniform4iv)(int,int,int *);
75+
JNIEXPORT void (*_glUniform3iv)(int,int,int *);
76+
JNIEXPORT void (*_glUniform2iv)(int,int,int *);
77+
JNIEXPORT void (*_glUniform1i)(int,int);
78+
JNIEXPORT void (*_glVertexAttribPointer)(int, int, int, int, int, void*);
79+
JNIEXPORT void (*_glViewport)(int, int, int, int);
80+
81+
#ifdef __cplusplus
82+
}
83+
#endif
84+
85+
JNIEXPORT jboolean JNICALL GLinit()
7986
{
8087
if (!glPlatformInit()) return JNI_FALSE;
8188
glGetFunction((void**)&_glActiveTexture,"glActiveTexture");
@@ -147,6 +154,12 @@ JNIEXPORT jboolean JNICALL Java_javaforce_jni_GLJNI_init
147154
return JNI_TRUE;
148155
}
149156

157+
JNIEXPORT jboolean JNICALL Java_javaforce_jni_GLJNI_init
158+
(JNIEnv *e, jobject c)
159+
{
160+
return GLinit();
161+
}
162+
150163
JNIEXPORT void JNICALL Java_javaforce_jni_GLJNI_glActiveTexture
151164
(JNIEnv *e, jobject c, jint i1)
152165
{

native/linux/build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<project name="native" default="linux" basedir=".">
22
<description>native</description>
33

4-
<property name="opt" value="-O3"/> <!-- change to -g to debug -->
4+
<property name="opt" value="-O3 -Wl,--export-dynamic"/> <!-- change to -g to debug -->
55

66
<macrodef name="build" description="create linux natives">
77
<attribute name="libs"/>

native/linux/loader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ JavaVMInitArgs *BuildArgs() {
262262
opts[nOpts++] = "-Dcom.sun.management.jmxremote.ssl=false";
263263
}
264264

265+
opts[nOpts++] = (char*)"--enable-native-access=ALL-UNNAMED";
266+
opts[nOpts++] = MakeString("-Djava.app.exec=%s", g_argv[0]);
265267
opts[nOpts++] = (char*)"-Djava.app.home=/usr/bin";
266268
opts[nOpts++] = MakeString("-Djava.app.name=%s", mainclass);
267269
opts[nOpts++] = MakeString("-Dvisualvm.display.name=%s", mainclass);

native/windows/loader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ JavaVMInitArgs *BuildArgs() {
274274
opts[nOpts++] = "-Dcom.sun.management.jmxremote.authenticate=false";
275275
opts[nOpts++] = "-Dcom.sun.management.jmxremote.ssl=false";
276276
}
277+
opts[nOpts++] = "--enable-native-access=ALL-UNNAMED";
278+
opts[nOpts++] = MakeString("-Djava.app.exec=%s", module);
277279
opts[nOpts++] = MakeString("-Djava.app.home=%s", exepath);
278280
opts[nOpts++] = MakeString("-Djava.app.name=%s", mainclass);
279281
opts[nOpts++] = MakeString("-Dvisualvm.display.name=%s", mainclass);
@@ -734,7 +736,6 @@ int main(int argc, char **argv)
734736
g_argc = argc;
735737

736738
GetModuleFileName(NULL, module, MAX_PATH);
737-
trim(module, '.');
738739
strcpy(exepath, module);
739740
trim(exepath, '\\');
740741

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ This should be removed for any public release.
155155

156156
Requirements
157157
------------
158-
- JDK 17+
158+
- JDK 25+
159159
- Apache Ant 1.10+
160160
- Linux : gcc
161161
- Windows : VC++, wixtoolset 4+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package javaforce.ffm;
2+
3+
/** Executable Symbol Lookup
4+
*
5+
* Find symbols in the main executable.
6+
*
7+
* @author pquiring
8+
*/
9+
10+
import java.util.*;
11+
12+
import java.lang.foreign.*;
13+
import java.lang.invoke.*;
14+
import static java.lang.foreign.ValueLayout.*;
15+
16+
import javaforce.*;
17+
18+
public class ExecSymbolLookup implements SymbolLookup {
19+
20+
private MemorySegment handle; //executable handle
21+
private MethodHandle getsymbol; //GetProcAddress() or dlsym()
22+
private Arena arena;
23+
24+
private static final int RTLD_LAZY = 1;
25+
private static final int RTLD_NOW = 2;
26+
private static final int RTLD_GLOBAL = 0x100;
27+
28+
private static boolean debug = false;
29+
30+
public boolean init(FFM ffm) {
31+
JFLog.log("ExecSymbolLookup init");
32+
try {
33+
arena = Arena.global();
34+
if (JF.isWindows()) {
35+
SymbolLookup kernel32 = SymbolLookup.libraryLookup("kernel32", arena);
36+
ffm.setSymbolLookup(kernel32);
37+
MethodHandle GetModuleHandle = ffm.getFunction("GetModuleHandleA", ffm.getFunctionDesciptor(ADDRESS, ADDRESS));
38+
getsymbol = ffm.getFunction("GetProcAddress", ffm.getFunctionDesciptor(ADDRESS, ADDRESS, ADDRESS));
39+
try {
40+
handle = (MemorySegment)GetModuleHandle.invokeExact(MemorySegment.NULL);
41+
} catch (Throwable t) {
42+
JFLog.log(t);
43+
}
44+
} else {
45+
MethodHandle dlopen = ffm.getFunction("dlopen", ffm.getFunctionDesciptor(ADDRESS, ADDRESS, JAVA_INT));
46+
if (debug) JFLog.log("dlopen=" + dlopen);
47+
getsymbol = ffm.getFunction("dlsym", ffm.getFunctionDesciptor(ADDRESS, ADDRESS, ADDRESS));
48+
if (debug) JFLog.log("getsymbol=" + getsymbol);
49+
try {
50+
handle = (MemorySegment)dlopen.invokeExact(MemorySegment.NULL, RTLD_NOW | RTLD_GLOBAL);
51+
if (debug) JFLog.log("handle=" + handle);
52+
} catch (Throwable t) {
53+
JFLog.log(t);
54+
}
55+
}
56+
ffm.setSymbolLookup(this);
57+
return true;
58+
} catch (Throwable t) {
59+
JFLog.log(t);
60+
return false;
61+
}
62+
}
63+
64+
public Optional<MemorySegment> find(String name) {
65+
try {
66+
if (debug) JFLog.log("lookup:name=" + name);
67+
MemorySegment sym = (MemorySegment)getsymbol.invokeExact(handle, arena.allocateFrom(name));
68+
if (debug) JFLog.log("lookup:sym=" + sym);
69+
return Optional.of(sym);
70+
} catch (Throwable t) {
71+
JFLog.log(t);
72+
return null;
73+
}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)