From 6be41a8e457ec2881f4ce351395ba84748a737b6 Mon Sep 17 00:00:00 2001
From: Sven Gothel
Date: Sat, 19 Oct 2013 06:47:48 +0200
Subject: Bug 773 - Device specific JOCL dynamic library look-up on Android -
Part 1/2
Use DynamicLibraryBundleInfo w/ alternative native library names,
drop manual coding of loading and binding, i.e. JOCLJNILibLoader.
After trying opencl native libs (and failing), try GL libs ..
We use a manual impl. to CL's 'clGetExtensionFunctionAddress' similar to JOAL, JOGL ...
---
resources/cl-impl.cfg | 11 +-
resources/clImplCustomCode.c | 24 ++++
resources/clImplCustomCode.java | 53 ++++++-
src/com/jogamp/opencl/CLPlatform.java | 81 +++--------
src/com/jogamp/opencl/JOCLJNILibLoader.java | 72 ----------
.../llb/impl/CLDynamicLibraryBundleInfo.java | 152 +++++++++++++++++++++
src/com/jogamp/opencl/llb/impl/CLImpl.java | 26 +---
7 files changed, 257 insertions(+), 162 deletions(-)
delete mode 100644 src/com/jogamp/opencl/JOCLJNILibLoader.java
create mode 100644 src/com/jogamp/opencl/llb/impl/CLDynamicLibraryBundleInfo.java
diff --git a/resources/cl-impl.cfg b/resources/cl-impl.cfg
index 32a3099d..c2aff892 100644
--- a/resources/cl-impl.cfg
+++ b/resources/cl-impl.cfg
@@ -5,6 +5,8 @@ Style ImplOnly
#imports for all generated java files
Import com.jogamp.opencl.llb.*
Import com.jogamp.opencl.llb.gl.CLGL
+Import java.security.AccessController
+Import java.security.PrivilegedAction
ClassJavadoc CLAbstractImpl /**
ClassJavadoc CLAbstractImpl * Java bindings to OpenCL, the Open Computing Language (generated).
@@ -45,10 +47,11 @@ ProcAddressNameExpr $UpperCase(arg)
#...or force all
ForceProcAddressGen __ALL__
-Unignore clGetExtensionFunctionAddress
-RenameJavaMethod clGetExtensionFunctionAddress clGetExtensionFunctionAddressImpl
-AccessControl clGetExtensionFunctionAddressImpl PROTECTED
-ArgumentIsString clGetExtensionFunctionAddressImpl 0
+#
+# extern CL_API_ENTRY void * CL_API_CALL clGetExtensionFunctionAddress(const char * /* func_name */) CL_API_SUFFIX__VERSION_1_0;
+#
+Ignore clGetExtensionFunctionAddress
+ForceProcAddressGen clGetExtensionFunctionAddress
#append to generated c files
CustomCCode #include
diff --git a/resources/clImplCustomCode.c b/resources/clImplCustomCode.c
index 4a43ae3b..751f819b 100644
--- a/resources/clImplCustomCode.c
+++ b/resources/clImplCustomCode.c
@@ -112,6 +112,30 @@ CL_CALLBACK void memObjDestructorCallback(cl_mem mem, void * object) {
(*jvm)->DetachCurrentThread(jvm);
}
+// extern CL_API_ENTRY void * CL_API_CALL clGetExtensionFunctionAddress(const char * /* func_name */) CL_API_SUFFIX__VERSION_1_0;
+
+JNIEXPORT jlong JNICALL
+Java_com_jogamp_opencl_llb_impl_CLAbstractImpl_dispatch_1clGetExtensionFunctionAddressStatic(JNIEnv *env, jclass _unused, jstring fname, jlong procAddress) {
+ typedef void* (CL_API_ENTRY*_local_LPCLGETPROCADDRESS)(const char * fname);
+ _local_LPCLGETPROCADDRESS ptr_clGetExtensionFunctionAddress;
+ const char* _strchars_fname = NULL;
+ void* _res;
+ if ( NULL != fname ) {
+ _strchars_fname = (*env)->GetStringUTFChars(env, fname, (jboolean*)NULL);
+ if ( NULL == _strchars_fname ) {
+ (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"),
+ "Failed to get UTF-8 chars for argument \"fname\" in native dispatcher for \"dispatch_clGetExtensionFunctionAddress\"");
+ return 0;
+ }
+ }
+ ptr_clGetExtensionFunctionAddress = (_local_LPCLGETPROCADDRESS) (intptr_t) procAddress;
+ assert(ptr_clGetExtensionFunctionAddress != NULL);
+ _res = (* ptr_clGetExtensionFunctionAddress) ((char *) _strchars_fname);
+ if ( NULL != fname ) {
+ (*env)->ReleaseStringUTFChars(env, fname, _strchars_fname);
+ }
+ return (jlong) (intptr_t) _res;
+}
/* Java->C glue code:
* Java package: com.jogamp.opencl.impl.CLImpl
diff --git a/resources/clImplCustomCode.java b/resources/clImplCustomCode.java
index 1826da86..bee53425 100644
--- a/resources/clImplCustomCode.java
+++ b/resources/clImplCustomCode.java
@@ -1,6 +1,53 @@
- protected final CLProcAddressTable addressTable;
+ static final DynamicLibraryBundle dynamicLookupHelper;
+ protected static final CLProcAddressTable addressTable;
- public CLAbstractImpl(CLProcAddressTable addressTable) {
- this.addressTable = addressTable;
+ static {
+ addressTable = new CLProcAddressTable();
+ if(null==addressTable) {
+ throw new RuntimeException("Couldn't instantiate ALProcAddressTable");
+ }
+
+ dynamicLookupHelper = AccessController.doPrivileged(new PrivilegedAction() {
+ public DynamicLibraryBundle run() {
+ final DynamicLibraryBundle bundle = new DynamicLibraryBundle(new CLDynamicLibraryBundleInfo());
+ if(null==bundle) {
+ throw new RuntimeException("Null CLDynamicLookupHelper");
+ }
+ if(!bundle.isToolLibLoaded()) {
+ throw new RuntimeException("Couln't load native CL library");
+ }
+ if(!bundle.isLibComplete()) {
+ throw new RuntimeException("Couln't load native CL/JNI glue library");
+ }
+ addressTable.reset(bundle);
+ return bundle;
+ } } );
}
+
+ public static CLProcAddressTable getCLProcAddressTable() { return addressTable; }
+
+ static long clGetExtensionFunctionAddress(long clGetExtensionFunctionAddressHandle, java.lang.String procname)
+ {
+ if (clGetExtensionFunctionAddressHandle == 0) {
+ throw new RuntimeException("Passed null pointer for method \"clGetExtensionFunctionAddress\"");
+ }
+ return dispatch_clGetExtensionFunctionAddressStatic(procname, clGetExtensionFunctionAddressHandle);
+ }
+
+ public CLAbstractImpl() {
+ }
+
+ /** Entry point (through function pointer) to C language function:
void* clGetExtensionFunctionAddress(const char * fname);
*/
+ long clGetExtensionFunctionAddress(String fname) {
+
+ final long __addr_ = addressTable._addressof_clGetExtensionFunctionAddress;
+ if (__addr_ == 0) {
+ throw new UnsupportedOperationException("Method \"clGetExtensionFunctionAddress\" not available");
+ }
+ return dispatch_clGetExtensionFunctionAddressStatic(fname, __addr_);
+ }
+
+ /** Entry point (through function pointer) to C language function:
void* clGetExtensionFunctionAddress(const char * fname);
*/
+ private static native long dispatch_clGetExtensionFunctionAddressStatic(String fname, long procAddress);
+
diff --git a/src/com/jogamp/opencl/CLPlatform.java b/src/com/jogamp/opencl/CLPlatform.java
index 7a4f6b43..99b2e16d 100644
--- a/src/com/jogamp/opencl/CLPlatform.java
+++ b/src/com/jogamp/opencl/CLPlatform.java
@@ -3,14 +3,14 @@
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
- *
+ *
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
- *
+ *
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
@@ -20,7 +20,7 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
+ *
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
@@ -36,11 +36,8 @@ import com.jogamp.opencl.llb.CLImageBinding;
import com.jogamp.opencl.llb.CL;
import com.jogamp.opencl.impl.CLTLAccessorFactory;
import com.jogamp.common.nio.Buffers;
-import com.jogamp.common.os.DynamicLookupHelper;
import com.jogamp.common.JogampRuntimeException;
-import com.jogamp.common.os.NativeLibrary;
import com.jogamp.common.nio.PointerBuffer;
-import com.jogamp.gluegen.runtime.FunctionAddressResolver;
import com.jogamp.opencl.llb.CLBufferBinding;
import com.jogamp.opencl.llb.CLCommandQueueBinding;
import com.jogamp.opencl.llb.CLContextBinding;
@@ -50,7 +47,6 @@ import com.jogamp.opencl.llb.CLMemObjBinding;
import com.jogamp.opencl.spi.CLPlatformInfoAccessor;
import com.jogamp.opencl.util.CLUtil;
import com.jogamp.opencl.llb.impl.CLImpl;
-import com.jogamp.opencl.llb.impl.CLProcAddressTable;
import com.jogamp.opencl.spi.CLAccessorFactory;
import com.jogamp.opencl.util.Filter;
import com.jogamp.opencl.util.JOCLVersion;
@@ -63,15 +59,13 @@ import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
-import java.security.PrivilegedAction;
-import static java.security.AccessController.*;
import static com.jogamp.opencl.CLException.*;
import static com.jogamp.opencl.llb.CL.*;
/**
* CLPlatfrorm representing a OpenCL implementation (e.g. graphics driver).
- *
+ *
* optional eager initialization:
*
* try{
@@ -80,15 +74,15 @@ import static com.jogamp.opencl.llb.CL.*;
* throw new RuntimeException("could not load Java OpenCL Binding");
* }
*
- *
+ *
* Example initialization:
*
* CLPlatform platform = CLPlatform.getDefault(type(GPU));
- *
+ *
* if(platform == null) {
* throw new RuntimeException("please update your graphics drivers");
* }
- *
+ *
* CLContext context = CLContext.create(platform.getMaxFlopsDevice());
* try {
* // use it
@@ -98,7 +92,7 @@ import static com.jogamp.opencl.llb.CL.*;
*
* concurrency:
* CLPlatform is threadsafe.
- *
+ *
* @author Michael Bien, et al.
* @see #initialize()
* @see #getDefault()
@@ -159,7 +153,7 @@ public class CLPlatform {
if(cl != null) {
return;
}
-
+
if(defaultFactory == null) {
if(factory == null) {
defaultFactory = new CLTLAccessorFactory();
@@ -169,49 +163,10 @@ public class CLPlatform {
}
try {
-
- final CLProcAddressTable table = new CLProcAddressTable(new FunctionAddressResolver() {
- @Override
- public long resolve(String name, DynamicLookupHelper lookup) {
-
- //FIXME workaround to fix a gluegen issue
- if(name.endsWith("Impl")) {
- name = name.substring(0, name.length() - "Impl".length());
- }
-
- if(name.endsWith("KHR") || name.endsWith("EXT")) {
- long address = ((CLImpl) cl).clGetExtensionFunctionAddress(name);
- if(address != 0) {
- return address;
- }
- }
-
- return lookup.dynamicLookupFunction(name);
- }
- });
-
- cl = new CLImpl(table);
-
- //load JOCL and init table
- doPrivileged(new PrivilegedAction