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