1

I'm porting a program that uses JNI to use Panama instead. This program uses OpenGL to draw onto a Canvas object. I can port the Windows and OpenGL calls to use Panama, but the problem is I need to get the HWND of the Canvas I want to draw to. The C code looks like this:

JNIEXPORT void JNICALL Java_somepackage_SomeCanvas_paint(JNIEnv* env, jobject canvas, jobject graphics)
{
    JAWT awt;
    awt.version = JAWT_VERSION_1_4;
    JAWT_GetAWT(env, &awt);
    JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas);
    ds->Lock(ds);
    JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
    JAWT_Win32DrawingSurfaceInfo* dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
    HWND hwnd = dsi_win->hwnd;
    ...
}

I can't call JAWT_GetAWT from Panama though, because it needs a JNIEnv argument, which is only available in JNI calls. How do I get the HWND (or JAWT_DrawingSurface) of the Canvas instead?

I've tried looking online for any sort of API I could use to do this, but I haven't found anything. I'm hoping someone here might know.

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
James
  • 13
  • 3

2 Answers2

1

You could theoretically get a JNIEnv* using the Panama API by loading the jvm library, and using the invocation API's JNI_GetCreatedJavaVMs and then GetEnv on the retrieved JavaVM* to get a JNIEnv*.

I think after that you're still stuck when needing to call GetDrawingSurface though, because it takes an object handle of the canvas object as an argument, and there is currently no way to turn a Java object into a handle in pure Java code (at least, not that I'm aware of).

The Java AWT Native Interface that you're using has a dependency on JNI, so I don't think you can get rid of JNI entirely (and the Panama API currently doesn't really have specific JNI interop support)

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • While you can call JNI functions, the resulting references are useless, as they are invalidated once the native call is done. (Except `NewGlobalRef`, but that requires a valid reference). `RegisterNatives` requires a valid class reference (which you don't have), but the passed object references would be valid during the upcall. (The only JNI call that doesn't require a valid handle is `DefineClass` with a `NULL` classloader.) – Johannes Kuhn Mar 04 '23 at 14:16
0

Not really answering your question, but if you are willing to use OpenGL from Panama, you may look at this https://gitlab.com/jzy3d/panama-gl. For now we avoid working with JAWT by rendering images offscreen and then display this image into AWT or Swing canvas (other windowing toolkits such as SWT and JavaFX will come soon).

Martin Pernollet
  • 2,285
  • 1
  • 28
  • 39