diff options
author | Michael Bien <[email protected]> | 2010-04-26 03:11:15 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-04-26 03:11:15 +0200 |
commit | 552a8d5d570a50829c7361dddcd35b2fe230c44e (patch) | |
tree | e9f4b17d68c1c7d00e3f75df7b7bce2311d9d7c8 | |
parent | 22cf865f702cac07a5059f45baace1d1caf5dfdb (diff) |
continued with callbacks.
- started with context error callback
- finished build program callback
-rw-r--r-- | resources/clImplCustomCode.c | 71 | ||||
-rw-r--r-- | resources/clImplCustomCode.java | 6 | ||||
-rw-r--r-- | src/com/jogamp/opencl/BuildProgramCallback.java | 2 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CreateContextCallback.java | 3 | ||||
-rw-r--r-- | test/com/jogamp/opencl/LowLevelBindingTest.java | 2 |
5 files changed, 60 insertions, 24 deletions
diff --git a/resources/clImplCustomCode.c b/resources/clImplCustomCode.c index b24f69ab..eb209157 100644 --- a/resources/clImplCustomCode.c +++ b/resources/clImplCustomCode.c @@ -8,7 +8,9 @@ void checkStatus(const char* msg, int status) { */ JavaVM * jvm; + jmethodID bcb_mid; +jmethodID cccb_mid; JNIEXPORT jint JNICALL @@ -22,14 +24,18 @@ JNI_OnLoad(JavaVM * _jvm, void *reserved) { } // throws ClassNotFoundException (or other reflection stuff) - jclass classID = (*env)->FindClass(env, "com/jogamp/opencl/BuildProgramCallback"); + jclass buildCBClassID = (*env)->FindClass(env, "com/jogamp/opencl/BuildProgramCallback"); + jclass errorHandlerClassID = (*env)->FindClass(env, "com/jogamp/opencl/CreateContextCallback"); - if (classID != NULL) { - // throws even more reflection Exceptions - // IDs are unique and do not change - bcb_mid = (*env)->GetMethodID(env, classID, "buildProgramCallback", "(J)V"); + // throws even more reflection Exceptions + // IDs are unique and do not change + if (buildCBClassID != NULL) { + bcb_mid = (*env)->GetMethodID(env, buildCBClassID, "buildFinished", "(J)V"); } - + if (errorHandlerClassID != NULL) { + cccb_mid = (*env)->GetMethodID(env, errorHandlerClassID, "onError", "(Ljava/lang/String;Ljava/nio/ByteBuffer;J)V"); + } + return JNI_VERSION_1_2; } @@ -47,7 +53,18 @@ void buildProgramCallback(cl_program id, void * object) { } void createContextCallback(const char * errinfo, const void * private_info, size_t cb, void * object) { - printf(" - - - - - - >error info:\n%s", errinfo); + + JNIEnv *env; + jobject obj = (jobject)object; + + (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL); + + jstring errorString = (*env)->NewStringUTF(env, errinfo); + + //TODO private_info + (*env)->CallVoidMethod(env, obj, cccb_mid, errorString, NULL, 0); + //(*env)->DeleteGlobalRef(env, obj); + } @@ -67,7 +84,9 @@ Java_com_jogamp_opencl_impl_CLImpl_clCreateContextFromType0(JNIEnv *env, jobject cl_context_properties* _props_ptr = NULL; int32_t * _errcode_ptr = NULL; - cl_context _ctx; + cl_context _ctx = NULL; + void (*pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data) = NULL; + jobject globalCB = NULL; if (props != NULL) { _props_ptr = (cl_context_properties*) (((char*) (*env)->GetDirectBufferAddress(env, props)) + props_byte_offset); @@ -77,8 +96,17 @@ Java_com_jogamp_opencl_impl_CLImpl_clCreateContextFromType0(JNIEnv *env, jobject _errcode_ptr = (void *) (((char*) (*env)->GetDirectBufferAddress(env, errcode)) + errcode_byte_offset); } - //TODO callback; payload - _ctx = clCreateContextFromType(_props_ptr, (uint64_t) device_type, NULL, NULL, (int32_t *) _errcode_ptr); + if (cb != NULL) { + pfn_notify = &createContextCallback; + globalCB = (*env)->NewGlobalRef(env, cb); + } + + _ctx = clCreateContextFromType(_props_ptr, (uint64_t) device_type, pfn_notify, globalCB, (int32_t *) _errcode_ptr); + + // if something went wrong + if (_ctx == NULL && globalCB != NULL) { + (*env)->DeleteGlobalRef(env, globalCB); + } return (jlong) (intptr_t)_ctx; } @@ -100,8 +128,9 @@ Java_com_jogamp_opencl_impl_CLImpl_clCreateContext0(JNIEnv *env, jobject _unused cl_context_properties* _props_ptr = NULL; int32_t * _errcode_ptr = NULL; size_t * _deviceListPtr = NULL; - - cl_context _ctx; + cl_context _ctx = NULL; + void (*pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data) = NULL; + jobject globalCB = NULL; if (props != NULL) { _props_ptr = (cl_context_properties*) (((char*) (*env)->GetDirectBufferAddress(env, props)) + props_byte_offset); @@ -113,8 +142,17 @@ Java_com_jogamp_opencl_impl_CLImpl_clCreateContext0(JNIEnv *env, jobject _unused _errcode_ptr = (void *) (((char*) (*env)->GetDirectBufferAddress(env, errcode)) + errcode_byte_offset); } - // TODO payload, callback... - _ctx = clCreateContext(_props_ptr, numDevices, (cl_device_id *)_deviceListPtr, NULL, NULL, (int32_t *) _errcode_ptr); + if (cb != NULL) { + pfn_notify = &createContextCallback; + globalCB = (*env)->NewGlobalRef(env, cb); + } + + _ctx = clCreateContext(_props_ptr, numDevices, (cl_device_id *)_deviceListPtr, pfn_notify, globalCB, (int32_t *) _errcode_ptr); + + // if something went wrong + if (_ctx == NULL && globalCB != NULL) { + (*env)->DeleteGlobalRef(env, globalCB); + } return (jlong) (intptr_t)_ctx; } @@ -159,6 +197,11 @@ Java_com_jogamp_opencl_impl_CLImpl_clBuildProgram0(JNIEnv *env, jobject _unused, _res = clBuildProgram((cl_program)program, (cl_uint)deviceCount, (cl_device_id *)_deviceListPtr, _strchars_options, pfn_notify, globalCB); + // if something went wrong + if(_res != CL_SUCCESS && globalCB != NULL) { + (*env)->DeleteGlobalRef(env, globalCB); + } + if (options != NULL) { (*env)->ReleaseStringUTFChars(env, options, _strchars_options); } diff --git a/resources/clImplCustomCode.java b/resources/clImplCustomCode.java index 5ebf12aa..b5b5ec61 100644 --- a/resources/clImplCustomCode.java +++ b/resources/clImplCustomCode.java @@ -10,9 +10,6 @@ if(properties!=null && !properties.isDirect()) throw new RuntimeException("Argument \"properties\" was not a direct buffer"); - if(pfn_notify != null) - throw new RuntimeException("asynchronous errorhandler are not supported yet, pass null through this method to block until complete."); - return this.clCreateContext0( properties!=null?properties.getBuffer():null, Buffers.getDirectBufferByteOffset(properties), devices!=null?devices.remaining():0, devices!=null?devices.getBuffer():null, Buffers.getDirectBufferByteOffset(devices), @@ -26,9 +23,6 @@ if(properties!=null && !properties.isDirect()) throw new RuntimeException("Argument \"properties\" was not a direct buffer"); - if(pfn_notify != null) - throw new RuntimeException("asynchronous errorhandler are not supported yet, pass null through this method to block until complete."); - return this.clCreateContextFromType0( properties!=null?properties.getBuffer():null, Buffers.getDirectBufferByteOffset(properties), device_type, pfn_notify, errcode_ret, Buffers.getDirectBufferByteOffset(errcode_ret) ); diff --git a/src/com/jogamp/opencl/BuildProgramCallback.java b/src/com/jogamp/opencl/BuildProgramCallback.java index 9969c918..c37891ec 100644 --- a/src/com/jogamp/opencl/BuildProgramCallback.java +++ b/src/com/jogamp/opencl/BuildProgramCallback.java @@ -14,6 +14,6 @@ public interface BuildProgramCallback { * Called when the program executable * has been built (successfully or unsuccessfully). */ - public void buildProgramCallback(long cl_program); + public void buildFinished(long cl_program); } diff --git a/src/com/jogamp/opencl/CreateContextCallback.java b/src/com/jogamp/opencl/CreateContextCallback.java index a73b6298..7a502134 100644 --- a/src/com/jogamp/opencl/CreateContextCallback.java +++ b/src/com/jogamp/opencl/CreateContextCallback.java @@ -6,9 +6,8 @@ import java.nio.ByteBuffer; * * @author Michael Bien */ -// TODO implement callbacks public interface CreateContextCallback { - public void createContextCallback(String errinfo, ByteBuffer private_info, long cb); + public void onError(String errinfo, ByteBuffer private_info, long cb); } diff --git a/test/com/jogamp/opencl/LowLevelBindingTest.java b/test/com/jogamp/opencl/LowLevelBindingTest.java index f5b5a27b..eb1d7bd4 100644 --- a/test/com/jogamp/opencl/LowLevelBindingTest.java +++ b/test/com/jogamp/opencl/LowLevelBindingTest.java @@ -252,7 +252,7 @@ public class LowLevelBindingTest { // tests if the callback is called final CountDownLatch latch = new CountDownLatch(1); BuildProgramCallback callback = new BuildProgramCallback() { - public void buildProgramCallback(long cl_program) { + public void buildFinished(long cl_program) { try{ assertEquals(program, cl_program); }finally{ |