VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Wrapping/Java/vtkJavaAwt.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkJavaAwt.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 // for use with JAWT
00016 #include "jawt_md.h"
00017 
00018 #define VTK_JAVA_DEBUG
00019 
00020 // On OS X, disable deprecation warnings since JAWT_GetAWT() is deprecated.
00021 #if defined(__APPLE__)
00022   #if defined(__GNUC__) && ((__GNUC__ > 4) || \
00023                             ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)))
00024     #pragma GCC diagnostic push
00025   #endif
00026   #if defined(__GNUC__) && ((__GNUC__ > 4) || \
00027                             ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)))
00028     #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
00029   #endif
00030 #endif
00031 
00032 #if defined(_WIN32) || defined(WIN32)
00033 #define WIN32_JAWT_LOCK_HACK
00034 #endif
00035 
00036 #if defined(WIN32_JAWT_LOCK_HACK)
00037 #define WJLH_MAX_COUNT (32)
00038 #define WJLH_HASH_FUNC(E,C,H) {\
00039   jclass cls = E->GetObjectClass(C);\
00040   jmethodID mid = E->GetMethodID(cls, "hashCode", "()I");\
00041   H = E->CallIntMethod(C, mid); }
00042 # include "vtkWindows.h"
00043 int WJLH_init_check = 0;
00044 # include <map> // STL Header
00045 std::map<int,int> WJLH_lock_map;
00046 #endif
00047 
00048 extern "C" JNIEXPORT jint  JNICALL
00049 Java_vtk_vtkPanel_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
00050 {
00051 #if defined(WIN32_JAWT_LOCK_HACK)
00052   int hash;
00053   WJLH_HASH_FUNC(env, canvas, hash);
00054   WJLH_lock_map[hash] = 0;
00055 #endif
00056 
00057   JAWT awt;
00058   JAWT_DrawingSurface* ds;
00059   JAWT_DrawingSurfaceInfo* dsi;
00060   jint lock;
00061 
00062   // get the render window pointer
00063   vtkRenderWindow *temp0;
00064   temp0 = (vtkRenderWindow *)(vtkJavaGetPointerFromObject(env,id0));
00065 
00066   /* Get the AWT */
00067   awt.version = JAWT_VERSION_1_3;
00068   if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
00069     {
00070 #ifndef VTK_JAVA_DEBUG
00071     printf("AWT Not found\n");
00072 #endif
00073     return 1;
00074     }
00075 
00076   /* Get the drawing surface */
00077   ds = awt.GetDrawingSurface(env, canvas);
00078   if (ds == NULL)
00079     {
00080 #ifndef VTK_JAVA_DEBUG
00081     printf("NULL drawing surface\n");
00082 #endif
00083     return 1;
00084     }
00085 
00086   /* Lock the drawing surface */
00087   lock = ds->Lock(ds);
00088   if((lock & JAWT_LOCK_ERROR) != 0)
00089     {
00090 #ifndef VTK_JAVA_DEBUG
00091     printf("Error locking surface\n");
00092 #endif
00093     awt.FreeDrawingSurface(ds);
00094     return 1;
00095     }
00096 
00097   /* Get the drawing surface info */
00098   dsi = ds->GetDrawingSurfaceInfo(ds);
00099   if (dsi == NULL)
00100     {
00101     printf("Error getting surface info\n");
00102     ds->Unlock(ds);
00103     awt.FreeDrawingSurface(ds);
00104     return 1;
00105     }
00106 
00107 // Here is the win32 drawing code
00108 #if defined(_WIN32) || defined(WIN32)
00109   temp0->Finalize();
00110   JAWT_Win32DrawingSurfaceInfo* dsi_win;
00111   dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
00112   temp0->SetWindowId((void *)dsi_win->hwnd);
00113   temp0->SetDisplayId((void *)dsi_win->hdc);
00114   // also set parent id to avoid border sizes being added
00115   temp0->SetParentId((void *)dsi_win->hdc);
00116 // use mac code
00117 #elif defined(__APPLE__)
00118   JAWT_MacOSXDrawingSurfaceInfo* dsi_mac;
00119   dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
00120   temp0->SetWindowId(dsi_mac->cocoaViewRef);
00121 // otherwise use X11 code
00122 #else
00123   JAWT_X11DrawingSurfaceInfo* dsi_x11;
00124   dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
00125   temp0->SetDisplayId((void *)dsi_x11->display);
00126   temp0->SetWindowId((void *)dsi_x11->drawable);
00127   temp0->SetParentId((void *)dsi_x11->display);
00128 #endif
00129 
00130   /* Free the drawing surface info */
00131   ds->FreeDrawingSurfaceInfo(dsi);
00132 
00133   /* Unlock the drawing surface */
00134   ds->Unlock(ds);
00135 
00136   /* Free the drawing surface */
00137   awt.FreeDrawingSurface(ds);
00138 
00139 #if defined(WIN32_JAWT_LOCK_HACK)
00140   if (WJLH_init_check == 0)
00141     {
00142     WJLH_init_check = 1;
00143     }
00144   WJLH_lock_map[hash] = 1;
00145 #endif
00146   return 0;
00147 }
00148 
00149 extern "C" JNIEXPORT jint  JNICALL
00150 Java_vtk_rendering_awt_vtkInternalAwtComponent_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
00151 {
00152 #if defined(WIN32_JAWT_LOCK_HACK)
00153   int hash;
00154   WJLH_HASH_FUNC(env, canvas, hash);
00155   WJLH_lock_map[hash] = 0;
00156 #endif
00157 
00158   JAWT awt;
00159   JAWT_DrawingSurface* ds;
00160   JAWT_DrawingSurfaceInfo* dsi;
00161   jint lock;
00162 
00163   // get the render window pointer
00164   vtkRenderWindow *temp0;
00165   temp0 = (vtkRenderWindow *)(vtkJavaGetPointerFromObject(env,id0));
00166 
00167   /* Get the AWT */
00168   awt.version = JAWT_VERSION_1_3;
00169   if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
00170     {
00171 #ifndef VTK_JAVA_DEBUG
00172     printf("AWT Not found\n");
00173 #endif
00174     return 1;
00175     }
00176 
00177   /* Get the drawing surface */
00178   ds = awt.GetDrawingSurface(env, canvas);
00179   if (ds == NULL)
00180     {
00181 #ifndef VTK_JAVA_DEBUG
00182     printf("NULL drawing surface\n");
00183 #endif
00184     return 1;
00185     }
00186 
00187   /* Lock the drawing surface */
00188   lock = ds->Lock(ds);
00189   if((lock & JAWT_LOCK_ERROR) != 0)
00190     {
00191 #ifndef VTK_JAVA_DEBUG
00192     printf("Error locking surface\n");
00193 #endif
00194     awt.FreeDrawingSurface(ds);
00195     return 1;
00196     }
00197 
00198   /* Get the drawing surface info */
00199   dsi = ds->GetDrawingSurfaceInfo(ds);
00200   if (dsi == NULL)
00201     {
00202     printf("Error getting surface info\n");
00203     ds->Unlock(ds);
00204     awt.FreeDrawingSurface(ds);
00205     return 1;
00206     }
00207 
00208 // Here is the win32 drawing code
00209 #if defined(_WIN32) || defined(WIN32)
00210   temp0->Finalize();
00211   JAWT_Win32DrawingSurfaceInfo* dsi_win;
00212   dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
00213   temp0->SetWindowId((void *)dsi_win->hwnd);
00214   temp0->SetDisplayId((void *)dsi_win->hdc);
00215   // also set parent id to avoid border sizes being added
00216   temp0->SetParentId((void *)dsi_win->hdc);
00217 // use mac code
00218 #elif defined(__APPLE__)
00219   JAWT_MacOSXDrawingSurfaceInfo* dsi_mac;
00220   dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
00221   temp0->SetWindowId(dsi_mac->cocoaViewRef);
00222 // otherwise use X11 code
00223 #else
00224   JAWT_X11DrawingSurfaceInfo* dsi_x11;
00225   dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
00226   temp0->SetDisplayId((void *)dsi_x11->display);
00227   temp0->SetWindowId((void *)dsi_x11->drawable);
00228   temp0->SetParentId((void *)dsi_x11->display);
00229 #endif
00230 
00231   /* Free the drawing surface info */
00232   ds->FreeDrawingSurfaceInfo(dsi);
00233 
00234   /* Unlock the drawing surface */
00235   ds->Unlock(ds);
00236 
00237   /* Free the drawing surface */
00238   awt.FreeDrawingSurface(ds);
00239 
00240 #if defined(WIN32_JAWT_LOCK_HACK)
00241   if (WJLH_init_check == 0)
00242     {
00243     WJLH_init_check = 1;
00244     }
00245   WJLH_lock_map[hash] = 1;
00246 #endif
00247   return 0;
00248 }
00249 
00250 // Lock must be called prior to render or anything which might
00251 // cause vtkRenderWindow to make an XLib call or to call Render().
00252 // The Lock() and UnLock() functions are necessary for drawing in
00253 // JAWT, but they also provide a form of mutex locking so that multiple
00254 // java threads are prevented from accessing X at the same time.  The only
00255 // requirement JAWT has is that all operations on a JAWT_DrawingSurface
00256 // MUST be performed from the same thread as the call to GetDrawingSurface.
00257 extern "C" JNIEXPORT jint  JNICALL
00258 Java_vtk_vtkPanel_Lock(JNIEnv *env,
00259                        jobject canvas)
00260 {
00261   JAWT awt;
00262   JAWT_DrawingSurface* ds;
00263   jint lock;
00264 
00265   /* Get the AWT */
00266   awt.version = JAWT_VERSION_1_3;
00267   if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
00268     {
00269 #ifndef VTK_JAVA_DEBUG
00270     printf("AWT Not found\n");
00271 #endif
00272     return 1;
00273     }
00274 
00275   /* Get the drawing surface */
00276   ds = awt.GetDrawingSurface(env, canvas);
00277   if (ds == NULL)
00278     {
00279 #ifndef VTK_JAVA_DEBUG
00280     printf("NULL drawing surface\n");
00281 #endif
00282     return 1;
00283     }
00284 
00285 #if defined(WIN32_JAWT_LOCK_HACK)
00286   int hash;
00287   WJLH_HASH_FUNC(env, canvas, hash);
00288   if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
00289   {
00290     env->MonitorEnter(canvas);
00291   }
00292   else
00293   {
00294 #endif
00295   /* Lock the drawing surface */
00296   lock = ds->Lock(ds);
00297   if((lock & JAWT_LOCK_ERROR) != 0)
00298     {
00299 #ifndef VTK_JAVA_DEBUG
00300     printf("Error locking surface\n");
00301 #endif
00302     awt.FreeDrawingSurface(ds);
00303     return 1;
00304     }
00305 #if defined(WIN32_JAWT_LOCK_HACK)
00306   }
00307 #endif
00308 
00309   return 0;
00310 
00311 }
00312 
00313 // UnLock() must be called after a Lock() and execution of a
00314 // function which might change the drawing surface.  See Lock().
00315 extern "C" JNIEXPORT jint  JNICALL
00316 Java_vtk_vtkPanel_UnLock(JNIEnv *env,
00317                          jobject canvas)
00318 {
00319   JAWT awt;
00320   JAWT_DrawingSurface* ds;
00321 
00322   /* Get the AWT */
00323   awt.version = JAWT_VERSION_1_3;
00324   if (JAWT_GetAWT(env, &awt) == JNI_FALSE)
00325     {
00326 #ifndef VTK_JAVA_DEBUG
00327     printf("AWT Not found\n");
00328 #endif
00329     return 1;
00330     }
00331 
00332   /* Get the drawing surface */
00333   ds = awt.GetDrawingSurface(env, canvas);
00334   if (ds == NULL)
00335     {
00336 #ifndef VTK_JAVA_DEBUG
00337     printf("NULL drawing surface\n");
00338 #endif
00339     return 1;
00340     }
00341 
00342 #if defined(WIN32_JAWT_LOCK_HACK)
00343   int hash;
00344   WJLH_HASH_FUNC(env, canvas, hash);
00345   if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
00346   {
00347     env->MonitorExit(canvas);
00348   }
00349   else
00350   {
00351     if (WJLH_init_check) WJLH_lock_map[hash]++;
00352 #endif
00353   /* Unlock the drawing surface */
00354   ds->Unlock(ds);
00355 #if defined(WIN32_JAWT_LOCK_HACK)
00356   }
00357 #endif
00358 
00359   /* Free the drawing surface */
00360   awt.FreeDrawingSurface(ds);
00361 
00362   return 0;
00363 }
00364 
00365 // Undo disabling of deprecation warning.
00366 #if defined(__APPLE__)
00367   #if defined(__GNUC__) && ((__GNUC__ > 4) || \
00368                             ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)))
00369     #pragma GCC diagnostic pop
00370   #endif
00371 #endif
00372 
00373 // VTK-HeaderTest-Exclude: vtkJavaAwt.h