Skip to content

Commit

Permalink
Merge pull request #62 from JetBrains/refactor_jni
Browse files Browse the repository at this point in the history
Refactoring. Move getDrawingSurface to Java from JNI
  • Loading branch information
igordmn authored Feb 3, 2021
2 parents 7a24e47 + 37d8787 commit b6e0411
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 285 deletions.
4 changes: 4 additions & 0 deletions skiko/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,15 @@ tasks.withType(CppCompile::class.java).configureEach {
"-DSK_UNICODE_AVAILABLE",
*buildType.flags
))
val includeDir = "$projectDir/src/jvmMain/cpp/include"
when (targetOs) {
OS.MacOS -> {
compilerArgs.addAll(
listOf(
"-fvisibility=hidden",
"-fvisibility-inlines-hidden",
"-I$jdkHome/include/darwin",
"-I$includeDir",
"-DSK_SHAPER_CORETEXT_AVAILABLE",
"-DSK_BUILD_FOR_MAC",
"-DSK_METAL",
Expand All @@ -248,6 +250,7 @@ tasks.withType(CppCompile::class.java).configureEach {
"-fvisibility=hidden",
"-fvisibility-inlines-hidden",
"-I$jdkHome/include/linux",
"-I$includeDir",
"-DSK_BUILD_FOR_LINUX",
"-DSK_R32_SHIFT=16",
*buildType.clangFlags
Expand All @@ -258,6 +261,7 @@ tasks.withType(CppCompile::class.java).configureEach {
compilerArgs.addAll(
listOf(
"-I$jdkHome/include/win32",
"-I$includeDir",
"-DSK_BUILD_FOR_WIN",
"-D_CRT_SECURE_NO_WARNINGS",
"-D_HAS_EXCEPTIONS=0",
Expand Down
70 changes: 70 additions & 0 deletions skiko/src/jvmMain/cpp/common/awt_jni.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <cstdint>
#include <jawt_md.h>
#include "jni_helpers.h"

extern "C" jboolean Skiko_GetAWT(JNIEnv *env, JAWT *awt);

extern "C"
{
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_AWTKt_getAWT(JNIEnv *env, jobject obj)
{
JAWT *awt = new JAWT();
awt->version = (jint)JAWT_VERSION_9;
jboolean result = Skiko_GetAWT(env, awt);

if (result == JNI_FALSE)
{
return 0;
}
else
{
return toJavaPointer(awt);
}
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_AWTKt_getDrawingSurface(JNIEnv *env, jobject obj, jlong awtPtr, jobject layer)
{
JAWT *awt = fromJavaPointer<JAWT *>(awtPtr);
JAWT_DrawingSurface *ds = awt->GetDrawingSurface(env, layer);
return toJavaPointer(ds);
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_AWTKt_freeDrawingSurface(JNIEnv *env, jobject obj, jlong awtPtr, jlong drawingSurfacePtr)
{
JAWT *awt = fromJavaPointer<JAWT *>(awtPtr);
JAWT_DrawingSurface *ds = fromJavaPointer<JAWT_DrawingSurface *>(drawingSurfacePtr);
awt->FreeDrawingSurface(ds);
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_AWTKt_lockDrawingSurface(JNIEnv *env, jobject obj, jlong drawingSurfacePtr)
{
JAWT_DrawingSurface *ds = fromJavaPointer<JAWT_DrawingSurface *>(drawingSurfacePtr);
ds->Lock(ds);
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_AWTKt_unlockDrawingSurface(JNIEnv *env, jobject obj, jlong drawingSurfacePtr)
{
JAWT_DrawingSurface *ds = fromJavaPointer<JAWT_DrawingSurface *>(drawingSurfacePtr);
ds->Unlock(ds);
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_AWTKt_getDrawingSurfaceInfo(JNIEnv *env, jobject obj, jlong drawingSurfacePtr)
{
JAWT_DrawingSurface *ds = fromJavaPointer<JAWT_DrawingSurface *>(drawingSurfacePtr);
JAWT_DrawingSurfaceInfo *dsi = ds->GetDrawingSurfaceInfo(ds);
return toJavaPointer(dsi);
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_AWTKt_freeDrawingSurfaceInfo(JNIEnv *env, jobject obj, jlong drawingSurfacePtr, jlong drawingSurfaceInfoPtr)
{
JAWT_DrawingSurface *ds = fromJavaPointer<JAWT_DrawingSurface *>(drawingSurfacePtr);
JAWT_DrawingSurfaceInfo *dsi = fromJavaPointer<JAWT_DrawingSurfaceInfo *>(drawingSurfaceInfoPtr);
ds->FreeDrawingSurfaceInfo(dsi);
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_AWTKt_getPlatformInfo(JNIEnv *env, jobject obj, jlong drawingSurfaceInfoPtr)
{
JAWT_DrawingSurfaceInfo *dsi = fromJavaPointer<JAWT_DrawingSurfaceInfo *>(drawingSurfaceInfoPtr);
return toJavaPointer(dsi->platformInfo);
}
}
7 changes: 7 additions & 0 deletions skiko/src/jvmMain/cpp/include/jni_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

template<typename T>
T inline fromJavaPointer(jlong ptr) { return reinterpret_cast<T>(static_cast<uintptr_t>(ptr)); }

template<typename T>
jlong inline toJavaPointer(T ptr) { return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); }
73 changes: 8 additions & 65 deletions skiko/src/jvmMain/cpp/linux/drawlayer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,24 @@
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
#include "jni_helpers.h"

typedef GLXContext (*glXCreateContextAttribsARBProc)(Display *, GLXFBConfig, GLXContext, Bool, const int *);

extern "C" jboolean Skiko_GetAWT(JNIEnv *env, JAWT *awt);

extern "C"
{
JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_init(JNIEnv *env, jobject canvas)
JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_nativeInit(JNIEnv *env, jobject canvas, jlong platformInfoPtr)
{
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_dispose(JNIEnv *env, jobject canvas)
{
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_HardwareLayer_getWindowHandle(JNIEnv *env, jobject canvas)
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_HardwareLayer_getWindowHandle(JNIEnv *env, jobject canvas, jlong platformInfoPtr)
{
JAWT awt;
JAWT_DrawingSurface *ds = NULL;
JAWT_DrawingSurfaceInfo *dsi = NULL;

jboolean result = JNI_FALSE;
jint lock = 0;
JAWT_X11DrawingSurfaceInfo *dsi_x11;

awt.version = (jint)JAWT_VERSION_9;
result = Skiko_GetAWT(env, &awt);

if (result == JNI_FALSE)
{
fprintf(stderr, "JAWT_GetAWT failed! Result is JNI_FALSE\n");
return -1;
}

ds = awt.GetDrawingSurface(env, canvas);
lock = ds->Lock(ds);
dsi = ds->GetDrawingSurfaceInfo(ds);
dsi_x11 = (JAWT_X11DrawingSurfaceInfo *)dsi->platformInfo;

Window window = dsi_x11->drawable;

ds->FreeDrawingSurfaceInfo(dsi);
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);

return (jlong)window;
JAWT_X11DrawingSurfaceInfo *dsi_x11 = fromJavaPointer<JAWT_X11DrawingSurfaceInfo *>(platformInfoPtr);
return (jlong) dsi_x11->drawable;
}

double getDpiScaleByDisplay(Display *display)
Expand All @@ -77,38 +49,9 @@ extern "C"
return 1;
}

JNIEXPORT jfloat JNICALL Java_org_jetbrains_skiko_PlatformOperationsKt_linuxGetDpiScaleNative(JNIEnv *env, jobject properties, jobject component)
JNIEXPORT jfloat JNICALL Java_org_jetbrains_skiko_PlatformOperationsKt_linuxGetDpiScaleNative(JNIEnv *env, jobject properties, jlong platformInfoPtr)
{
JAWT awt;
JAWT_DrawingSurface *ds = NULL;
JAWT_DrawingSurfaceInfo *dsi = NULL;

jboolean result = JNI_FALSE;
jint lock = 0;
JAWT_X11DrawingSurfaceInfo *dsi_x11;

awt.version = (jint)JAWT_VERSION_9;
result = Skiko_GetAWT(env, &awt);

if (result == JNI_FALSE)
{
fprintf(stderr, "JAWT_GetAWT failed! Result is JNI_FALSE\n");
return -1;
}

ds = awt.GetDrawingSurface(env, component);
lock = ds->Lock(ds);
dsi = ds->GetDrawingSurfaceInfo(ds);
dsi_x11 = (JAWT_X11DrawingSurfaceInfo *)dsi->platformInfo;

Display *display = dsi_x11->display;

float dpi = (float)getDpiScaleByDisplay(display);

ds->FreeDrawingSurfaceInfo(dsi);
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);

return dpi;
JAWT_X11DrawingSurfaceInfo *dsi_x11 = fromJavaPointer<JAWT_X11DrawingSurfaceInfo *>(platformInfoPtr);
return (float) getDpiScaleByDisplay(dsi_x11->display);
}
} // extern "C"
80 changes: 18 additions & 62 deletions skiko/src/jvmMain/cpp/linux/redrawer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,30 @@
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
#include "jni_helpers.h"

typedef GLXContext (*glXCreateContextAttribsARBProc)(Display *, GLXFBConfig, GLXContext, Bool, const int *);

extern "C" jboolean Skiko_GetAWT(JNIEnv *env, JAWT *awt);

extern "C"
{
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_lockDrawingSurfaceNative(JNIEnv *env, jobject redrawer, jobject layer)
{
JAWT awt;
awt.version = (jint)JAWT_VERSION_9;
if (!Skiko_GetAWT(env, &awt))
{
fprintf(stderr, "JAWT_GetAWT failed! Result is JNI_FALSE\n");
return 0;
}

JAWT_DrawingSurface *ds = awt.GetDrawingSurface(env, layer);
ds->Lock(ds);

return static_cast<jlong>(reinterpret_cast<uintptr_t>(ds));
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_unlockDrawingSurfaceNative(JNIEnv *env, jobject redrawer, jlong drawingSurfacePtr)
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_getDisplay(JNIEnv *env, jobject redrawer, jlong platformInfoPtr)
{
JAWT_DrawingSurface *ds = reinterpret_cast<JAWT_DrawingSurface *>(static_cast<uintptr_t>(drawingSurfacePtr));
JAWT awt;
awt.version = (jint)JAWT_VERSION_9;
if (!Skiko_GetAWT(env, &awt))
{
fprintf(stderr, "JAWT_GetAWT failed! Result is JNI_FALSE\n");
return 0;
}

ds->Unlock(ds);
awt.FreeDrawingSurface(ds);

return static_cast<jlong>(reinterpret_cast<uintptr_t>(ds));
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_getDisplay(JNIEnv *env, jobject redrawer, jlong drawingSurfacePtr)
{
JAWT_DrawingSurface *ds = reinterpret_cast<JAWT_DrawingSurface *>(static_cast<uintptr_t>(drawingSurfacePtr));
JAWT_DrawingSurfaceInfo *dsi = ds->GetDrawingSurfaceInfo(ds);
JAWT_X11DrawingSurfaceInfo *dsi_x11 = (JAWT_X11DrawingSurfaceInfo *)dsi->platformInfo;

JAWT_X11DrawingSurfaceInfo *dsi_x11 = fromJavaPointer<JAWT_X11DrawingSurfaceInfo *>(platformInfoPtr);
Display *display = dsi_x11->display;

ds->FreeDrawingSurfaceInfo(dsi);
return static_cast<jlong>(reinterpret_cast<uintptr_t>(display));
return toJavaPointer(display);
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_getWindow(JNIEnv *env, jobject redrawer, jlong drawingSurfacePtr)
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_getWindow(JNIEnv *env, jobject redrawer, jlong platformInfoPtr)
{
JAWT_DrawingSurface *ds = reinterpret_cast<JAWT_DrawingSurface *>(static_cast<uintptr_t>(drawingSurfacePtr));
JAWT_DrawingSurfaceInfo *dsi = ds->GetDrawingSurfaceInfo(ds);
JAWT_X11DrawingSurfaceInfo *dsi_x11 = (JAWT_X11DrawingSurfaceInfo *)dsi->platformInfo;

JAWT_X11DrawingSurfaceInfo *dsi_x11 = fromJavaPointer<JAWT_X11DrawingSurfaceInfo *>(platformInfoPtr);
Window window = dsi_x11->drawable;

ds->FreeDrawingSurfaceInfo(dsi);
return static_cast<jlong>(reinterpret_cast<uintptr_t>(window));
return toJavaPointer(window);
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_setSwapInterval(JNIEnv *env, jobject redrawer, jlong displayPtr, jlong windowPtr, jint interval)
{
Display *display = reinterpret_cast<Display *>(static_cast<uintptr_t>(displayPtr));
Window window = reinterpret_cast<Window>(static_cast<uintptr_t>(windowPtr));
Display *display = fromJavaPointer<Display *>(displayPtr);
Window window = fromJavaPointer<Window>(windowPtr);

// according to:
// https://opengl.gpuinfo.org/listreports.php?extension=GLX_EXT_swap_control
Expand Down Expand Up @@ -106,35 +62,35 @@ extern "C"

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_swapBuffers(JNIEnv *env, jobject redrawer, jlong displayPtr, jlong windowPtr)
{
Display *display = reinterpret_cast<Display *>(static_cast<uintptr_t>(displayPtr));
Window window = reinterpret_cast<Window>(static_cast<uintptr_t>(windowPtr));
Display *display = fromJavaPointer<Display *>(displayPtr);
Window window = fromJavaPointer<Window>(windowPtr);

glXSwapBuffers(display, window);
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_makeCurrent(JNIEnv *env, jobject redrawer, jlong displayPtr, jlong windowPtr, jlong contextPtr)
{
Display *display = reinterpret_cast<Display *>(static_cast<uintptr_t>(displayPtr));
Window window = reinterpret_cast<Window>(static_cast<uintptr_t>(windowPtr));
GLXContext *context = reinterpret_cast<GLXContext *>(static_cast<uintptr_t>(contextPtr));
Display *display = fromJavaPointer<Display *>(displayPtr);
Window window = fromJavaPointer<Window>(windowPtr);
GLXContext *context = fromJavaPointer<GLXContext *>(contextPtr);

glXMakeCurrent(display, window, *context);
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_createContext(JNIEnv *env, jobject redrawer, jlong displayPtr)
{
Display *display = reinterpret_cast<Display *>(static_cast<uintptr_t>(displayPtr));
Display *display = fromJavaPointer<Display *>(displayPtr);
GLint att[] = {GLX_RGBA, GLX_DOUBLEBUFFER, True, None};
XVisualInfo *vi = glXChooseVisual(display, 0, att);

GLXContext *context = new GLXContext(glXCreateContext(display, vi, NULL, GL_TRUE));
return static_cast<jlong>(reinterpret_cast<uintptr_t>(context));
return toJavaPointer(context);
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_destroyContext(JNIEnv *env, jobject redrawer, jlong displayPtr, jlong contextPtr)
{
Display *display = reinterpret_cast<Display *>(static_cast<uintptr_t>(displayPtr));
GLXContext *context = reinterpret_cast<GLXContext *>(static_cast<uintptr_t>(contextPtr));
Display *display = fromJavaPointer<Display *>(displayPtr);
GLXContext *context = fromJavaPointer<GLXContext *>(contextPtr);

glXDestroyContext(display, *context);
delete context;
Expand Down
38 changes: 5 additions & 33 deletions skiko/src/jvmMain/cpp/windows/drawlayer.cc
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
#define WIN32_LEAN_AND_MEAN
#include <jawt_md.h>

extern "C" jboolean Skiko_GetAWT(JNIEnv *env, JAWT *awt);
#include "jni_helpers.h"

extern "C"
{
JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_init(JNIEnv *env, jobject canvas)
JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_nativeInit(JNIEnv *env, jobject canvas, jlong platformInfoPtr)
{
}

JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_dispose(JNIEnv *env, jobject canvas)
{
}

JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_HardwareLayer_getWindowHandle(JNIEnv *env, jobject canvas)
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_HardwareLayer_getWindowHandle(JNIEnv *env, jobject canvas, jlong platformInfoPtr)
{
JAWT awt;
JAWT_DrawingSurface *ds = NULL;
JAWT_DrawingSurfaceInfo *dsi = NULL;

jboolean result = JNI_FALSE;
jint lock = 0;
JAWT_Win32DrawingSurfaceInfo *dsi_win;

awt.version = (jint)JAWT_VERSION_9;
result = Skiko_GetAWT(env, &awt);

if (result == JNI_FALSE)
{
fprintf(stderr, "JAWT_GetAWT failed! Result is JNI_FALSE\n");
return -1;
}

ds = awt.GetDrawingSurface(env, canvas);
lock = ds->Lock(ds);
dsi = ds->GetDrawingSurfaceInfo(ds);
dsi_win = (JAWT_Win32DrawingSurfaceInfo *)dsi->platformInfo;

HWND hwnd = dsi_win->hwnd;

ds->FreeDrawingSurfaceInfo(dsi);
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);

return (jlong)hwnd;
JAWT_Win32DrawingSurfaceInfo* dsi_win = fromJavaPointer<JAWT_Win32DrawingSurfaceInfo *>(platformInfoPtr);
return (jlong) dsi_win->hwnd;
}
} // extern "C"
Loading

0 comments on commit b6e0411

Please sign in to comment.