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

Common/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_MacDrawingSurfaceInfo* dsi_mac;
00107   dsi_mac = (JAWT_MacDrawingSurfaceInfo*)dsi->platformInfo;
00108   temp0->SetWindowId((void *)dsi_mac->fQDWindow);
00109 // otherwise use X11 code
00110 #else
00111   JAWT_X11DrawingSurfaceInfo* dsi_x11;
00112   dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
00113   temp0->SetDisplayId((void *)dsi_x11->display);
00114   temp0->SetWindowId((void *)dsi_x11->drawable);
00115   temp0->SetParentId((void *)dsi_x11->display);
00116 #endif
00117   
00118   /* Free the drawing surface info */
00119   ds->FreeDrawingSurfaceInfo(dsi);
00120   
00121   /* Unlock the drawing surface */
00122   ds->Unlock(ds);
00123   
00124   /* Free the drawing surface */
00125   awt.FreeDrawingSurface(ds);
00126 
00127 #if defined(WIN32_JAWT_LOCK_HACK)
00128 if (WJLH_init_check == 0)
00129 {
00130   WJLH_init_check = 1;
00131 }
00132   WJLH_lock_map[hash] = 1;
00133 #endif
00134   return 0;
00135 
00136 }
00137 
00138 
00139 // Lock must be called prior to render or anything which might
00140 // cause vtkRenderWindow to make an XLib call or to call Render(). 
00141 // The Lock() and UnLock() functions are necessary for drawing in
00142 // JAWT, but they also provide a form of mutex locking so that multiple
00143 // java threads are prevented from accessing X at the same time.  The only
00144 // requirement JAWT has is that all operations on a JAWT_DrawingSurface 
00145 // MUST be performed from the same thread as the call to GetDrawingSurface.
00146 extern "C" JNIEXPORT jint  JNICALL 
00147 Java_vtk_vtkPanel_Lock(JNIEnv *env, 
00148                        jobject canvas)
00149 {
00150   JAWT awt;
00151   JAWT_DrawingSurface* ds;
00152   jint lock;
00153 
00154   /* Get the AWT */
00155   awt.version = JAWT_VERSION_1_3;
00156   if (JAWT_GetAWT(env, &awt) == JNI_FALSE) 
00157     {
00158 #ifndef VTK_JAVA_DEBUG
00159     printf("AWT Not found\n");
00160 #endif
00161     return 1;
00162     }
00163   
00164   /* Get the drawing surface */
00165   ds = awt.GetDrawingSurface(env, canvas);
00166   if (ds == NULL) 
00167     {
00168 #ifndef VTK_JAVA_DEBUG
00169     printf("NULL drawing surface\n");
00170 #endif
00171     return 1;
00172     }
00173 
00174 #if defined(WIN32_JAWT_LOCK_HACK)
00175   int hash;
00176   WJLH_HASH_FUNC(env, canvas, hash);
00177   if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
00178   {
00179     env->MonitorEnter(canvas);      
00180   }
00181   else
00182   {
00183 #endif
00184   /* Lock the drawing surface */
00185   lock = ds->Lock(ds);
00186   if((lock & JAWT_LOCK_ERROR) != 0) 
00187     {
00188 #ifndef VTK_JAVA_DEBUG
00189     printf("Error locking surface\n");
00190 #endif
00191     awt.FreeDrawingSurface(ds);
00192     return 1;
00193     }
00194 #if defined(WIN32_JAWT_LOCK_HACK)
00195   }
00196 #endif
00197 
00198   return 0;
00199 
00200 }
00201 
00202 // UnLock() must be called after a Lock() and execution of a
00203 // function which might change the drawing surface.  See Lock().
00204 extern "C" JNIEXPORT jint  JNICALL 
00205 Java_vtk_vtkPanel_UnLock(JNIEnv *env, 
00206                          jobject canvas)
00207 {
00208   JAWT awt;
00209   JAWT_DrawingSurface* ds;
00210 
00211   /* Get the AWT */
00212   awt.version = JAWT_VERSION_1_3;
00213   if (JAWT_GetAWT(env, &awt) == JNI_FALSE) 
00214     {
00215 #ifndef VTK_JAVA_DEBUG
00216     printf("AWT Not found\n");
00217 #endif
00218     return 1;
00219     }
00220   
00221   /* Get the drawing surface */
00222   ds = awt.GetDrawingSurface(env, canvas);
00223   if (ds == NULL) 
00224     {
00225 #ifndef VTK_JAVA_DEBUG
00226     printf("NULL drawing surface\n");
00227 #endif
00228     return 1;
00229     }
00230 
00231 #if defined(WIN32_JAWT_LOCK_HACK)
00232   int hash;
00233   WJLH_HASH_FUNC(env, canvas, hash);
00234   if (WJLH_init_check && WJLH_lock_map[hash] > WJLH_MAX_COUNT)
00235   {
00236     env->MonitorExit(canvas);
00237   }
00238   else
00239   {
00240     if (WJLH_init_check) WJLH_lock_map[hash]++;
00241 #endif
00242   /* Unlock the drawing surface */
00243   ds->Unlock(ds);
00244 #if defined(WIN32_JAWT_LOCK_HACK)
00245   }
00246 #endif
00247   
00248   /* Free the drawing surface */
00249   awt.FreeDrawingSurface(ds);
00250 
00251   return 0;
00252 }