VTK
vtkJavaAwt.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkJavaAwt.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // for use with JAWT
16 #include "jawt_md.h"
17 
18 #define VTK_JAVA_DEBUG
19 
20 // On OS X, disable deprecation warnings since JAWT_GetAWT() is deprecated.
21 #if defined(__APPLE__)
22  #if defined(__GNUC__) && ((__GNUC__ > 4) || \
23  ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)))
24  #pragma GCC diagnostic push
25  #endif
26  #if defined(__GNUC__) && ((__GNUC__ > 4) || \
27  ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)))
28  #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
29  #endif
30 #endif
31 
32 #if defined(_WIN32) || defined(WIN32)
33 #define WIN32_JAWT_LOCK_HACK
34 #endif
35 
36 #if defined(WIN32_JAWT_LOCK_HACK)
37 #define WJLH_MAX_COUNT (32)
38 #define WJLH_HASH_FUNC(E,C,H) {\
39  jclass cls = E->GetObjectClass(C);\
40  jmethodID mid = E->GetMethodID(cls, "hashCode", "()I");\
41  H = E->CallIntMethod(C, mid); }
42 # include "vtkWindows.h"
43 int WJLH_init_check = 0;
44 # include <map> // STL Header
45 std::map<int,int> WJLH_lock_map;
46 #endif
47 
48 extern "C" JNIEXPORT jint JNICALL
49 Java_vtk_vtkPanel_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
50 {
51 #if defined(WIN32_JAWT_LOCK_HACK)
52  int hash;
53  WJLH_HASH_FUNC(env, canvas, hash);
54  WJLH_lock_map[hash] = 0;
55 #endif
56 
57  JAWT awt;
58  JAWT_DrawingSurface* ds;
59  JAWT_DrawingSurfaceInfo* dsi;
60  jint lock;
61 
62  // get the render window pointer
63  vtkRenderWindow *temp0;
64  temp0 = (vtkRenderWindow *)(vtkJavaGetPointerFromObject(env,id0));
65 
66  /* Get the AWT */
67  awt.version = JAWT_VERSION_1_3;
68  if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
69  {
70 #ifndef VTK_JAVA_DEBUG
71  printf("AWT Not found\n");
72 #endif
73  return 1;
74  }
75 
76  /* Get the drawing surface */
77  ds = awt.GetDrawingSurface(env, canvas);
78  if (ds == NULL)
79  {
80 #ifndef VTK_JAVA_DEBUG
81  printf("NULL drawing surface\n");
82 #endif
83  return 1;
84  }
85 
86  /* Lock the drawing surface */
87  lock = ds->Lock(ds);
88  if((lock & JAWT_LOCK_ERROR) != 0)
89  {
90 #ifndef VTK_JAVA_DEBUG
91  printf("Error locking surface\n");
92 #endif
93  awt.FreeDrawingSurface(ds);
94  return 1;
95  }
96 
97  /* Get the drawing surface info */
98  dsi = ds->GetDrawingSurfaceInfo(ds);
99  if (dsi == NULL)
100  {
101  printf("Error getting surface info\n");
102  ds->Unlock(ds);
103  awt.FreeDrawingSurface(ds);
104  return 1;
105  }
106 
107 // Here is the win32 drawing code
108 #if defined(_WIN32) || defined(WIN32)
109  temp0->Finalize();
110  JAWT_Win32DrawingSurfaceInfo* dsi_win;
111  dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
112  temp0->SetWindowId((void *)dsi_win->hwnd);
113  temp0->SetDisplayId((void *)dsi_win->hdc);
114  // also set parent id to avoid border sizes being added
115  temp0->SetParentId((void *)dsi_win->hdc);
116 // use mac code
117 #elif defined(__APPLE__)
118  JAWT_MacOSXDrawingSurfaceInfo* dsi_mac;
119  dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
120  temp0->SetWindowId(dsi_mac->cocoaViewRef);
121 // otherwise use X11 code
122 #else
123  JAWT_X11DrawingSurfaceInfo* dsi_x11;
124  dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
125  temp0->SetDisplayId((void *)dsi_x11->display);
126  temp0->SetWindowId((void *)dsi_x11->drawable);
127  temp0->SetParentId((void *)dsi_x11->display);
128 #endif
129 
130  /* Free the drawing surface info */
131  ds->FreeDrawingSurfaceInfo(dsi);
132 
133  /* Unlock the drawing surface */
134  ds->Unlock(ds);
135 
136  /* Free the drawing surface */
137  awt.FreeDrawingSurface(ds);
138 
139 #if defined(WIN32_JAWT_LOCK_HACK)
140  if (WJLH_init_check == 0)
141  {
142  WJLH_init_check = 1;
143  }
144  WJLH_lock_map[hash] = 1;
145 #endif
146  return 0;
147 }
148 
149 extern "C" JNIEXPORT jint JNICALL
150 Java_vtk_rendering_awt_vtkInternalAwtComponent_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
151 {
152 #if defined(WIN32_JAWT_LOCK_HACK)
153  int hash;
154  WJLH_HASH_FUNC(env, canvas, hash);
155  WJLH_lock_map[hash] = 0;
156 #endif
157 
158  JAWT awt;
159  JAWT_DrawingSurface* ds;
160  JAWT_DrawingSurfaceInfo* dsi;
161  jint lock;
162 
163  // get the render window pointer
164  vtkRenderWindow *temp0;
165  temp0 = (vtkRenderWindow *)(vtkJavaGetPointerFromObject(env,id0));
166 
167  /* Get the AWT */
168  awt.version = JAWT_VERSION_1_3;
169  if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
170  {
171 #ifndef VTK_JAVA_DEBUG
172  printf("AWT Not found\n");
173 #endif
174  return 1;
175  }
176 
177  /* Get the drawing surface */
178  ds = awt.GetDrawingSurface(env, canvas);
179  if (ds == NULL)
180  {
181 #ifndef VTK_JAVA_DEBUG
182  printf("NULL drawing surface\n");
183 #endif
184  return 1;
185  }
186 
187  /* Lock the drawing surface */
188  lock = ds->Lock(ds);
189  if((lock & JAWT_LOCK_ERROR) != 0)
190  {
191 #ifndef VTK_JAVA_DEBUG
192  printf("Error locking surface\n");
193 #endif
194  awt.FreeDrawingSurface(ds);
195  return 1;
196  }
197 
198  /* Get the drawing surface info */
199  dsi = ds->GetDrawingSurfaceInfo(ds);
200  if (dsi == NULL)
201  {
202  printf("Error getting surface info\n");
203  ds->Unlock(ds);
204  awt.FreeDrawingSurface(ds);
205  return 1;
206  }
207 
208 // Here is the win32 drawing code
209 #if defined(_WIN32) || defined(WIN32)
210  temp0->Finalize();
211  JAWT_Win32DrawingSurfaceInfo* dsi_win;
212  dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
213  temp0->SetWindowId((void *)dsi_win->hwnd);
214  temp0->SetDisplayId((void *)dsi_win->hdc);
215  // also set parent id to avoid border sizes being added
216  temp0->SetParentId((void *)dsi_win->hdc);
217 // use mac code
218 #elif defined(__APPLE__)
219  JAWT_MacOSXDrawingSurfaceInfo* dsi_mac;
220  dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
221  temp0->SetWindowId(dsi_mac->cocoaViewRef);
222 // otherwise use X11 code
223 #else
224  JAWT_X11DrawingSurfaceInfo* dsi_x11;
225  dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
226  temp0->SetDisplayId((void *)dsi_x11->display);
227  temp0->SetWindowId((void *)dsi_x11->drawable);
228  temp0->SetParentId((void *)dsi_x11->display);
229 #endif
230 
231  /* Free the drawing surface info */
232  ds->FreeDrawingSurfaceInfo(dsi);
233 
234  /* Unlock the drawing surface */
235  ds->Unlock(ds);
236 
237  /* Free the drawing surface */
238  awt.FreeDrawingSurface(ds);
239 
240 #if defined(WIN32_JAWT_LOCK_HACK)
241  if (WJLH_init_check == 0)
242  {
243  WJLH_init_check = 1;
244  }
245  WJLH_lock_map[hash] = 1;
246 #endif
247  return 0;
248 }
249 
250 // Lock must be called prior to render or anything which might
251 // cause vtkRenderWindow to make an XLib call or to call Render().
252 // The Lock() and UnLock() functions are necessary for drawing in
253 // JAWT, but they also provide a form of mutex locking so that multiple
254 // java threads are prevented from accessing X at the same time. The only
255 // requirement JAWT has is that all operations on a JAWT_DrawingSurface
256 // MUST be performed from the same thread as the call to GetDrawingSurface.
257 extern "C" JNIEXPORT jint JNICALL
259  jobject canvas)
260 {
261  JAWT awt;
262  JAWT_DrawingSurface* ds;
263  jint lock;
264 
265  /* Get the AWT */
266  awt.version = JAWT_VERSION_1_3;
267  if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
268  {
269 #ifndef VTK_JAVA_DEBUG
270  printf("AWT Not found\n");
271 #endif
272  return 1;
273  }
274 
275  /* Get the drawing surface */
276  ds = awt.GetDrawingSurface(env, canvas);
277  if (ds == NULL)
278  {
279 #ifndef VTK_JAVA_DEBUG
280  printf("NULL drawing surface\n");
281 #endif
282  return 1;
283  }
284 
285 #if defined(WIN32_JAWT_LOCK_HACK)
286  int hash;
287  WJLH_HASH_FUNC(env, canvas, hash);
288  if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
289  {
290  env->MonitorEnter(canvas);
291  }
292  else
293  {
294 #endif
295  /* Lock the drawing surface */
296  lock = ds->Lock(ds);
297  if((lock & JAWT_LOCK_ERROR) != 0)
298  {
299 #ifndef VTK_JAVA_DEBUG
300  printf("Error locking surface\n");
301 #endif
302  awt.FreeDrawingSurface(ds);
303  return 1;
304  }
305 #if defined(WIN32_JAWT_LOCK_HACK)
306  }
307 #endif
308 
309  return 0;
310 
311 }
312 
313 // UnLock() must be called after a Lock() and execution of a
314 // function which might change the drawing surface. See Lock().
315 extern "C" JNIEXPORT jint JNICALL
317  jobject canvas)
318 {
319  JAWT awt;
320  JAWT_DrawingSurface* ds;
321 
322  /* Get the AWT */
323  awt.version = JAWT_VERSION_1_3;
324  if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
325  {
326 #ifndef VTK_JAVA_DEBUG
327  printf("AWT Not found\n");
328 #endif
329  return 1;
330  }
331 
332  /* Get the drawing surface */
333  ds = awt.GetDrawingSurface(env, canvas);
334  if (ds == NULL)
335  {
336 #ifndef VTK_JAVA_DEBUG
337  printf("NULL drawing surface\n");
338 #endif
339  return 1;
340  }
341 
342 #if defined(WIN32_JAWT_LOCK_HACK)
343  int hash;
344  WJLH_HASH_FUNC(env, canvas, hash);
345  if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
346  {
347  env->MonitorExit(canvas);
348  }
349  else
350  {
351  if (WJLH_init_check) WJLH_lock_map[hash]++;
352 #endif
353  /* Unlock the drawing surface */
354  ds->Unlock(ds);
355 #if defined(WIN32_JAWT_LOCK_HACK)
356  }
357 #endif
358 
359  /* Free the drawing surface */
360  awt.FreeDrawingSurface(ds);
361 
362  return 0;
363 }
364 
365 // Undo disabling of deprecation warning.
366 #if defined(__APPLE__)
367  #if defined(__GNUC__) && ((__GNUC__ > 4) || \
368  ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)))
369  #pragma GCC diagnostic pop
370  #endif
371 #endif
372 
373 // VTK-HeaderTest-Exclude: vtkJavaAwt.h
JNIEXPORT jint JNICALL Java_vtk_vtkPanel_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
Definition: vtkJavaAwt.h:49
virtual void Finalize()=0
JNIEXPORT jint JNICALL Java_vtk_vtkPanel_Lock(JNIEnv *env, jobject canvas)
Definition: vtkJavaAwt.h:258
virtual void SetWindowId(void *)=0
virtual void SetParentId(void *)=0
virtual void SetDisplayId(void *)=0
JNIEXPORT jint JNICALL Java_vtk_rendering_awt_vtkInternalAwtComponent_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
Definition: vtkJavaAwt.h:150
create a window for renderers to draw into
VTKWRAPPINGJAVA_EXPORT void * vtkJavaGetPointerFromObject(JNIEnv *env, jobject obj)
JNIEXPORT jint JNICALL Java_vtk_vtkPanel_UnLock(JNIEnv *env, jobject canvas)
Definition: vtkJavaAwt.h:316