Main Page | Directories | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

vtkJavaAwt.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkJavaAwt.h,v $
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 
00021 #if defined(_WIN32) || defined(WIN32)
00022 #define WIN32_JAWT_LOCK_HACK
00023 #endif
00024 
00025 #if defined(WIN32_JAWT_LOCK_HACK)
00026 #define WJLH_MAX_COUNT (32)
00027 #define WJLH_HASH_FUNC(E,C,H) {\
00028   jclass cls = E->GetObjectClass(C);\
00029   jmethodID mid = E->GetMethodID(cls, "hashCode", "()I");\
00030   H = E->CallIntMethod(C, mid); }
00031 #include <windows.h>
00032 int WJLH_init_check = 0;
00033 #include "vtkstd/map"
00034 vtkstd::map<int,int> WJLH_lock_map;
00035 #endif
00036 
00037 extern "C" JNIEXPORT jint  JNICALL 
00038 Java_vtk_vtkPanel_RenderCreate(JNIEnv *env, jobject canvas, jobject id0)
00039 {
00040 #if defined(WIN32_JAWT_LOCK_HACK)
00041   int hash;
00042   WJLH_HASH_FUNC(env, canvas, hash);
00043   WJLH_lock_map[hash] = 0;
00044 #endif
00045 
00046   JAWT awt;
00047   JAWT_DrawingSurface* ds;
00048   JAWT_DrawingSurfaceInfo* dsi;
00049   jint lock;
00050 
00051   // get the render window pointer
00052   vtkRenderWindow *temp0;
00053   temp0 = (vtkRenderWindow *)(vtkJavaGetPointerFromObject(env,id0,(char *) "vtkRenderWindow"));
00054   
00055   /* Get the AWT */
00056   awt.version = JAWT_VERSION_1_3;
00057   if (JAWT_GetAWT(env, &awt) == JNI_FALSE) 
00058     {
00059 #ifndef VTK_JAVA_DEBUG
00060     printf("AWT Not found\n");
00061 #endif
00062     return 1;
00063     }
00064   
00065   /* Get the drawing surface */
00066   ds = awt.GetDrawingSurface(env, canvas);
00067   if (ds == NULL) 
00068     {
00069 #ifndef VTK_JAVA_DEBUG
00070     printf("NULL drawing surface\n");
00071 #endif
00072     return 1;
00073     }
00074   
00075   /* Lock the drawing surface */
00076   lock = ds->Lock(ds);
00077   if((lock & JAWT_LOCK_ERROR) != 0) 
00078     {
00079 #ifndef VTK_JAVA_DEBUG
00080     printf("Error locking surface\n");
00081 #endif
00082     awt.FreeDrawingSurface(ds);
00083     return 1;
00084     }
00085 
00086   /* Get the drawing surface info */
00087   dsi = ds->GetDrawingSurfaceInfo(ds);
00088   if (dsi == NULL) 
00089     {
00090     printf("Error getting surface info\n");
00091     ds->Unlock(ds);
00092     awt.FreeDrawingSurface(ds);
00093     return 1;
00094     }
00095   
00096 // Here is the win32 drawing code
00097 #if defined(_WIN32) || defined(WIN32)
00098   JAWT_Win32DrawingSurfaceInfo* dsi_win;
00099   dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
00100   temp0->SetWindowId((void *)dsi_win->hwnd);
00101   temp0->SetDisplayId((void *)dsi_win->hdc);
00102   // also set parent id to avoid border sizes being added
00103   temp0->SetParentId((void *)dsi_win->hdc);
00104 // use mac code
00105 #elif defined(VTK_USE_CARBON) || defined(VTK_USE_COCOA)
00106   JAWT_MacOSXDrawingSurfaceInfo* dsi_mac;
00107   dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
00108 #ifdef VTK_USE_CARBON
00109   temp0->SetWindowId((void *)dsi_mac->carbonWindowRef);
00110 #else
00111   //temp0->SetWindowId((void *)dsi_mac->cocoaWindowRef);
00112   temp0->SetWindowId((void *)dsi_mac->cocoaViewRef);
00113 #endif
00114 // otherwise use X11 code
00115 #else
00116   JAWT_X11DrawingSurfaceInfo* dsi_x11;
00117   dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
00118   temp0->SetDisplayId((void *)dsi_x11->display);
00119   temp0->SetWindowId((void *)dsi_x11->drawable);
00120   temp0->SetParentId((void *)dsi_x11->display);
00121 #endif
00122   
00123   /* Free the drawing surface info */
00124   ds->FreeDrawingSurfaceInfo(dsi);
00125   
00126   /* Unlock the drawing surface */
00127   ds->Unlock(ds);
00128   
00129   /* Free the drawing surface */
00130   awt.FreeDrawingSurface(ds);
00131 
00132 #if defined(WIN32_JAWT_LOCK_HACK)
00133 if (WJLH_init_check == 0)
00134 {
00135   WJLH_init_check = 1;
00136 }
00137   WJLH_lock_map[hash] = 1;
00138 #endif
00139   return 0;
00140 
00141 }
00142 
00143 
00144 // Lock must be called prior to render or anything which might
00145 // cause vtkRenderWindow to make an XLib call or to call Render(). 
00146 // The Lock() and UnLock() functions are necessary for drawing in
00147 // JAWT, but they also provide a form of mutex locking so that multiple
00148 // java threads are prevented from accessing X at the same time.  The only
00149 // requirement JAWT has is that all operations on a JAWT_DrawingSurface 
00150 // MUST be performed from the same thread as the call to GetDrawingSurface.
00151 extern "C" JNIEXPORT jint  JNICALL 
00152 Java_vtk_vtkPanel_Lock(JNIEnv *env, 
00153                        jobject canvas)
00154 {
00155   JAWT awt;
00156   JAWT_DrawingSurface* ds;
00157   jint lock;
00158 
00159   /* Get the AWT */
00160   awt.version = JAWT_VERSION_1_3;
00161   if (JAWT_GetAWT(env, &awt) == JNI_FALSE) 
00162     {
00163 #ifndef VTK_JAVA_DEBUG
00164     printf("AWT Not found\n");
00165 #endif
00166     return 1;
00167     }
00168   
00169   /* Get the drawing surface */
00170   ds = awt.GetDrawingSurface(env, canvas);
00171   if (ds == NULL) 
00172     {
00173 #ifndef VTK_JAVA_DEBUG
00174     printf("NULL drawing surface\n");
00175 #endif
00176     return 1;
00177     }
00178 
00179 #if defined(WIN32_JAWT_LOCK_HACK)
00180   int hash;
00181   WJLH_HASH_FUNC(env, canvas, hash);
00182   if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
00183   {
00184     env->MonitorEnter(canvas);      
00185   }
00186   else
00187   {
00188 #endif
00189   /* Lock the drawing surface */
00190   lock = ds->Lock(ds);
00191   if((lock & JAWT_LOCK_ERROR) != 0) 
00192     {
00193 #ifndef VTK_JAVA_DEBUG
00194     printf("Error locking surface\n");
00195 #endif
00196     awt.FreeDrawingSurface(ds);
00197     return 1;
00198     }
00199 #if defined(WIN32_JAWT_LOCK_HACK)
00200   }
00201 #endif
00202 
00203   return 0;
00204 
00205 }
00206 
00207 // UnLock() must be called after a Lock() and execution of a
00208 // function which might change the drawing surface.  See Lock().
00209 extern "C" JNIEXPORT jint  JNICALL 
00210 Java_vtk_vtkPanel_UnLock(JNIEnv *env, 
00211                          jobject canvas)
00212 {
00213   JAWT awt;
00214   JAWT_DrawingSurface* ds;
00215 
00216   /* Get the AWT */
00217   awt.version = JAWT_VERSION_1_3;
00218   if (JAWT_GetAWT(env, &awt) == JNI_FALSE) 
00219     {
00220 #ifndef VTK_JAVA_DEBUG
00221     printf("AWT Not found\n");
00222 #endif
00223     return 1;
00224     }
00225   
00226   /* Get the drawing surface */
00227   ds = awt.GetDrawingSurface(env, canvas);
00228   if (ds == NULL) 
00229     {
00230 #ifndef VTK_JAVA_DEBUG
00231     printf("NULL drawing surface\n");
00232 #endif
00233     return 1;
00234     }
00235 
00236 #if defined(WIN32_JAWT_LOCK_HACK)
00237   int hash;
00238   WJLH_HASH_FUNC(env, canvas, hash);
00239   if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
00240   {
00241     env->MonitorExit(canvas);
00242   }
00243   else
00244   {
00245     if (WJLH_init_check) WJLH_lock_map[hash]++;
00246 #endif
00247   /* Unlock the drawing surface */
00248   ds->Unlock(ds);
00249 #if defined(WIN32_JAWT_LOCK_HACK)
00250   }
00251 #endif
00252   
00253   /* Free the drawing surface */
00254   awt.FreeDrawingSurface(ds);
00255 
00256   return 0;
00257 }