Commit d691cac6 authored by Ben Boeckel's avatar Ben Boeckel

vtkJava: make a module for java wrapping

parent f324194d
# TODO: Update FindJNI to use imported targets.
find_package(JNI REQUIRED)
set(classes
vtkJavaUtil)
set(headers
vtkJavaAwt.h)
vtk_module_add_module(VTK::Java
CLASSES ${classes}
HEADERS ${headers})
vtk_module_include(VTK::Java SYSTEM
PUBLIC
${JNI_INCLUDE_DIRS})
vtk_module_link(VTK::Java
PUBLIC
${JNI_LIBRARIES})
NAME
VTK::Java
LIBRARY_NAME
vtkJava
DEPENDS
VTK::CommonCore
TEST_DEPENDS
VTK::RenderingOpenGL2
VTK::TestingRendering
EXCLUDE_WRAP
/*=========================================================================
Program: Visualization Toolkit
Module: vtkJavaAwt.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef vtkJavaAwt_h
#define vtkJavaAwt_h
// for use with JAWT
#include "jawt_md.h"
#define VTK_JAVA_DEBUG
// On OS X, disable deprecation warnings since JAWT_GetAWT() is deprecated.
#if defined(__APPLE__) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#if defined(_WIN32)
#define WIN32_JAWT_LOCK_HACK
#endif
#if defined(WIN32_JAWT_LOCK_HACK)
#define WJLH_MAX_COUNT (32)
#define WJLH_HASH_FUNC(E,C,H) {\
jclass cls = E->GetObjectClass(C);\
jmethodID mid = E->GetMethodID(cls, "hashCode", "()I");\
H = E->CallIntMethod(C, mid); }
# include "vtkWindows.h"
int WJLH_init_check = 0;
# include <map> // STL Header
std::map<int,int> WJLH_lock_map;
#endif
extern "C" JNIEXPORT jint JNICALL
Java_vtk_vtkPanel_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
{
#if defined(WIN32_JAWT_LOCK_HACK)
int hash;
WJLH_HASH_FUNC(env, canvas, hash);
WJLH_lock_map[hash] = 0;
#endif
JAWT awt;
JAWT_DrawingSurface* ds;
JAWT_DrawingSurfaceInfo* dsi;
jint lock;
// get the render window pointer
vtkRenderWindow *temp0;
temp0 = (vtkRenderWindow *)(vtkJavaGetPointerFromObject(env,id0));
/* Get the AWT */
awt.version = JAWT_VERSION_1_3;
if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
{
#ifndef VTK_JAVA_DEBUG
printf("AWT Not found\n");
#endif
return 1;
}
/* Get the drawing surface */
ds = awt.GetDrawingSurface(env, canvas);
if (ds == NULL)
{
#ifndef VTK_JAVA_DEBUG
printf("NULL drawing surface\n");
#endif
return 1;
}
/* Lock the drawing surface */
lock = ds->Lock(ds);
if((lock & JAWT_LOCK_ERROR) != 0)
{
#ifndef VTK_JAVA_DEBUG
printf("Error locking surface\n");
#endif
awt.FreeDrawingSurface(ds);
return 1;
}
/* Get the drawing surface info */
dsi = ds->GetDrawingSurfaceInfo(ds);
if (dsi == NULL)
{
printf("Error getting surface info\n");
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);
return 1;
}
// Here is the win32 drawing code
#if defined(_WIN32)
temp0->Finalize();
JAWT_Win32DrawingSurfaceInfo* dsi_win;
dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
temp0->SetWindowId((void *)dsi_win->hwnd);
temp0->SetDisplayId((void *)dsi_win->hdc);
// also set parent id to avoid border sizes being added
temp0->SetParentId((void *)dsi_win->hdc);
// use mac code
#elif defined(__APPLE__)
JAWT_DrawingSurfaceInfo* dsi_mac;
dsi_mac = (JAWT_DrawingSurfaceInfo*)dsi->platformInfo;
// temp0->SetWindowId(dsi_mac->cocoaViewRef); // Wrong but allow compilation
// otherwise use X11 code
#else
JAWT_X11DrawingSurfaceInfo* dsi_x11;
dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
temp0->SetDisplayId((void *)dsi_x11->display);
temp0->SetWindowId((void *)dsi_x11->drawable);
temp0->SetParentId((void *)dsi_x11->display);
#endif
/* Free the drawing surface info */
ds->FreeDrawingSurfaceInfo(dsi);
/* Unlock the drawing surface */
ds->Unlock(ds);
/* Free the drawing surface */
awt.FreeDrawingSurface(ds);
#if defined(WIN32_JAWT_LOCK_HACK)
if (WJLH_init_check == 0)
{
WJLH_init_check = 1;
}
WJLH_lock_map[hash] = 1;
#endif
return 0;
}
extern "C" JNIEXPORT jint JNICALL
Java_vtk_rendering_awt_vtkInternalAwtComponent_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
{
#if defined(WIN32_JAWT_LOCK_HACK)
int hash;
WJLH_HASH_FUNC(env, canvas, hash);
WJLH_lock_map[hash] = 0;
#endif
JAWT awt;
JAWT_DrawingSurface* ds;
JAWT_DrawingSurfaceInfo* dsi;
jint lock;
// get the render window pointer
vtkRenderWindow *temp0;
temp0 = (vtkRenderWindow *)(vtkJavaGetPointerFromObject(env,id0));
/* Get the AWT */
awt.version = JAWT_VERSION_1_3;
if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
{
#ifndef VTK_JAVA_DEBUG
printf("AWT Not found\n");
#endif
return 1;
}
/* Get the drawing surface */
ds = awt.GetDrawingSurface(env, canvas);
if (ds == NULL)
{
#ifndef VTK_JAVA_DEBUG
printf("NULL drawing surface\n");
#endif
return 1;
}
/* Lock the drawing surface */
lock = ds->Lock(ds);
if((lock & JAWT_LOCK_ERROR) != 0)
{
#ifndef VTK_JAVA_DEBUG
printf("Error locking surface\n");
#endif
awt.FreeDrawingSurface(ds);
return 1;
}
/* Get the drawing surface info */
dsi = ds->GetDrawingSurfaceInfo(ds);
if (dsi == NULL)
{
printf("Error getting surface info\n");
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);
return 1;
}
// Here is the win32 drawing code
#if defined(_WIN32)
temp0->Finalize();
JAWT_Win32DrawingSurfaceInfo* dsi_win;
dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
temp0->SetWindowId((void *)dsi_win->hwnd);
temp0->SetDisplayId((void *)dsi_win->hdc);
// also set parent id to avoid border sizes being added
temp0->SetParentId((void *)dsi_win->hdc);
// use mac code
#elif defined(__APPLE__)
JAWT_DrawingSurfaceInfo* dsi_mac;
dsi_mac = (JAWT_DrawingSurfaceInfo*)dsi->platformInfo;
// temp0->SetWindowId(dsi_mac->cocoaViewRef); // Wrong but allow compilation
// otherwise use X11 code
#else
JAWT_X11DrawingSurfaceInfo* dsi_x11;
dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
temp0->SetDisplayId((void *)dsi_x11->display);
temp0->SetWindowId((void *)dsi_x11->drawable);
temp0->SetParentId((void *)dsi_x11->display);
#endif
/* Free the drawing surface info */
ds->FreeDrawingSurfaceInfo(dsi);
/* Unlock the drawing surface */
ds->Unlock(ds);
/* Free the drawing surface */
awt.FreeDrawingSurface(ds);
#if defined(WIN32_JAWT_LOCK_HACK)
if (WJLH_init_check == 0)
{
WJLH_init_check = 1;
}
WJLH_lock_map[hash] = 1;
#endif
return 0;
}
// Lock must be called prior to render or anything which might
// cause vtkRenderWindow to make an XLib call or to call Render().
// The Lock() and UnLock() functions are necessary for drawing in
// JAWT, but they also provide a form of mutex locking so that multiple
// java threads are prevented from accessing X at the same time. The only
// requirement JAWT has is that all operations on a JAWT_DrawingSurface
// MUST be performed from the same thread as the call to GetDrawingSurface.
extern "C" JNIEXPORT jint JNICALL
Java_vtk_vtkPanel_Lock(JNIEnv *env,
jobject canvas)
{
JAWT awt;
JAWT_DrawingSurface* ds;
jint lock;
/* Get the AWT */
awt.version = JAWT_VERSION_1_3;
if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
{
#ifndef VTK_JAVA_DEBUG
printf("AWT Not found\n");
#endif
return 1;
}
/* Get the drawing surface */
ds = awt.GetDrawingSurface(env, canvas);
if (ds == NULL)
{
#ifndef VTK_JAVA_DEBUG
printf("NULL drawing surface\n");
#endif
return 1;
}
#if defined(WIN32_JAWT_LOCK_HACK)
int hash;
WJLH_HASH_FUNC(env, canvas, hash);
if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
{
env->MonitorEnter(canvas);
}
else
{
#endif
/* Lock the drawing surface */
lock = ds->Lock(ds);
if((lock & JAWT_LOCK_ERROR) != 0)
{
#ifndef VTK_JAVA_DEBUG
printf("Error locking surface\n");
#endif
awt.FreeDrawingSurface(ds);
return 1;
}
#if defined(WIN32_JAWT_LOCK_HACK)
}
#endif
return 0;
}
// UnLock() must be called after a Lock() and execution of a
// function which might change the drawing surface. See Lock().
extern "C" JNIEXPORT jint JNICALL
Java_vtk_vtkPanel_UnLock(JNIEnv *env,
jobject canvas)
{
JAWT awt;
JAWT_DrawingSurface* ds;
/* Get the AWT */
awt.version = JAWT_VERSION_1_3;
if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
{
#ifndef VTK_JAVA_DEBUG
printf("AWT Not found\n");
#endif
return 1;
}
/* Get the drawing surface */
ds = awt.GetDrawingSurface(env, canvas);
if (ds == NULL)
{
#ifndef VTK_JAVA_DEBUG
printf("NULL drawing surface\n");
#endif
return 1;
}
#if defined(WIN32_JAWT_LOCK_HACK)
int hash;
WJLH_HASH_FUNC(env, canvas, hash);
if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
{
env->MonitorExit(canvas);
}
else
{
if (WJLH_init_check) WJLH_lock_map[hash]++;
#endif
/* Unlock the drawing surface */
ds->Unlock(ds);
#if defined(WIN32_JAWT_LOCK_HACK)
}
#endif
/* Free the drawing surface */
awt.FreeDrawingSurface(ds);
return 0;
}
// Undo disabling of deprecation warning.
#if defined(__APPLE__) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
#endif
// VTK-HeaderTest-Exclude: vtkJavaAwt.h
This diff is collapsed.
/*=========================================================================
Program: Visualization Toolkit
Module: vtkJavaUtil.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef vtkJavaUtil_h
#define vtkJavaUtil_h
#include "vtkSystemIncludes.h"
#include <jni.h>
#include "vtkCommand.h"
#include "vtkJavaModule.h"
#include <string>
extern VTKJAVA_EXPORT jlong q(JNIEnv *env,jobject obj);
extern VTKJAVA_EXPORT void *vtkJavaGetPointerFromObject(JNIEnv *env,jobject obj);
extern VTKJAVA_EXPORT char *vtkJavaUTFToChar(JNIEnv *env, jstring in);
extern VTKJAVA_EXPORT bool vtkJavaUTFToString(JNIEnv *env, jstring in, std::string &out);
extern VTKJAVA_EXPORT jstring vtkJavaMakeJavaString(JNIEnv *env, const char *in);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfFloatFromFloat(JNIEnv *env,
const float *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfDoubleFromFloat(JNIEnv *env,
const float *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfDoubleFromDouble(JNIEnv *env,
const double *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfShortFromShort(JNIEnv *env, const short *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfIntFromInt(JNIEnv *env, const int *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfIntFromIdType(JNIEnv *env, const vtkIdType *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfIntFromLongLong(JNIEnv *env, const long long *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfIntFromSignedChar(JNIEnv *env, const signed char *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfLongFromLong(JNIEnv *env, const long *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfByteFromUnsignedChar(JNIEnv *env, const unsigned char *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfByteFromChar(JNIEnv *env, const char *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfCharFromChar(JNIEnv *env, const char *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfUnsignedCharFromUnsignedChar(JNIEnv *env, const unsigned char *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfUnsignedIntFromUnsignedInt(JNIEnv *env, const unsigned int *arr, int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfUnsignedShortFromUnsignedShort(JNIEnv *env, const unsigned short *ptr,int size);
extern VTKJAVA_EXPORT jarray vtkJavaMakeJArrayOfUnsignedLongFromUnsignedLong(JNIEnv *env, const unsigned long *arr, int size);
// this is the void pointer parameter passed to the vtk callback routines on
// behalf of the Java interface for callbacks.
struct vtkJavaVoidFuncArg
{
JavaVM *vm;
jobject uobj;
jmethodID mid;
} ;
extern VTKJAVA_EXPORT void vtkJavaVoidFunc(void *);
extern VTKJAVA_EXPORT void vtkJavaVoidFuncArgDelete(void *);
class VTKJAVA_EXPORT vtkJavaCommand : public vtkCommand
{
public:
static vtkJavaCommand *New() { return new vtkJavaCommand; };
void SetGlobalRef(jobject obj) { this->uobj = obj; };
void SetMethodID(jmethodID id) { this->mid = id; };
void AssignJavaVM(JNIEnv *env) { env->GetJavaVM(&(this->vm)); };
void Execute(vtkObject *, unsigned long, void *);
JavaVM *vm;
jobject uobj;
jmethodID mid;
protected:
vtkJavaCommand();
~vtkJavaCommand();
};
#endif
// VTK-HeaderTest-Exclude: vtkJavaUtil.h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment