Skip to content

Commit

Permalink
osgi compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
redrezo committed Jul 29, 2020
1 parent fecfab7 commit 153ff59
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 27 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ plugins {
id 'java'
id 'eclipse'
id 'maven-publish'
id 'visual-studio'
}

apply plugin: 'org.standardout.bnd-platform'
Expand Down
7 changes: 7 additions & 0 deletions native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def getJNIPlatformDir() {
}
}

def getJNIIncludeDir() {
Jvm.current().javaHome.toPath().resolve('include')
}
def getJNIPlatformIncludeDir() {
getJNIIncludeDir().resolve(getJNIPlatformDir())
}

def addJNIIncludes(compileTask) {
def includeDir = Jvm.current().javaHome.toPath().resolve('include')
def platformIncludeDir = includeDir.resolve(getJNIPlatformDir())
Expand Down
7 changes: 7 additions & 0 deletions native/driftcpp/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'cpp-library'
id 'maven-publish'
id 'visual-studio'
}


Expand All @@ -13,6 +14,12 @@ library {
machines.macOS.x86_64
]

privateHeaders {
from file('src/main/headers')
from file(getJNIPlatformIncludeDir())
from file(getJNIIncludeDir())
}

binaries.configureEach {
def compileTask = compileTask.get()
//def linkTask = linkTask.get()
Expand Down
78 changes: 52 additions & 26 deletions native/driftcpp/src/main/cpp/driftcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

#include "driftcppint.h"

jobject driftfx::JNI::classLoader;

// Class
jclass driftfx::JNI::cClass;
jmethodID driftfx::JNI::mClassForName;

// Vec2i
jclass driftfx::JNI::cVec2i;
jmethodID driftfx::JNI::mVec2iConstructor;
Expand Down Expand Up @@ -37,38 +43,68 @@ jclass driftfx::JNI::cGLRenderer;
jmethodID driftfx::JNI::mGLRendererGetGLTextureId;


void driftfx::JNI::init(JNIEnv* env) {
jclass driftfx::JNI::getClass(JNIEnv* env, const char* name) {
jstring className = env->NewStringUTF(name);
jclass result = (jclass) env->CallStaticObjectMethod(cClass, mClassForName, className, JNI_FALSE, classLoader);
env->DeleteLocalRef(className);
return result;
}

void driftfx::JNI::init(JNIEnv* env, jobject _classLoader) {
classLoader = env->NewGlobalRef(_classLoader);
// Class
cClass = env->FindClass("java/lang/Class");
cClass = (jclass)env->NewGlobalRef(cClass);
mClassForName = env->GetStaticMethodID(cClass, "forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;");
// Vec2i
cVec2i = env->FindClass("org/eclipse/fx/drift/Vec2i");
cVec2i = getClass(env, "org.eclipse.fx.drift.Vec2i"); //env->FindClass("org/eclipse/fx/drift/Vec2i");
cVec2i = (jclass)env->NewGlobalRef(cVec2i);
mVec2iConstructor = env->GetMethodID(cVec2i, "<init>", "(II)V");
fVec2iX = env->GetFieldID(cVec2i, "x", "I");
fVec2iY = env->GetFieldID(cVec2i, "y", "I");
// SwapchainConfig
cSwapchainConfig = env->FindClass("org/eclipse/fx/drift/SwapchainConfig");
cSwapchainConfig = getClass(env, "org.eclipse.fx.drift.SwapchainConfig"); //env->FindClass("org/eclipse/fx/drift/SwapchainConfig");
cSwapchainConfig = (jclass)env->NewGlobalRef(cSwapchainConfig);
mSwapchainConfigConstructor = env->GetMethodID(cSwapchainConfig, "<init>", "(Lorg/eclipse/fx/drift/Vec2i;ILorg/eclipse/fx/drift/PresentationMode;Lorg/eclipse/fx/drift/TransferType;)V");
fSwapchainConfigSize = env->GetFieldID(cSwapchainConfig, "size", "Lorg/eclipse/fx/drift/Vec2i;");
fSwapchainConfigImageCount = env->GetFieldID(cSwapchainConfig, "imageCount", "I");
fSwapchainConfigPresentationMode = env->GetFieldID(cSwapchainConfig, "presentationMode", "Lorg/eclipse/fx/drift/PresentationMode;");
fSwapchainConfigTransferType = env->GetFieldID(cSwapchainConfig, "transferType", "Lorg/eclipse/fx/drift/TransferType;");
// Swapchain
jclass cSwapchain = env->FindClass("org/eclipse/fx/drift/Swapchain");
jclass cSwapchain = getClass(env, "org.eclipse.fx.drift.Swapchain"); //env->FindClass("org/eclipse/fx/drift/Swapchain");
mSwapchainAcquire = env->GetMethodID(cSwapchain, "acquire", "()Lorg/eclipse/fx/drift/RenderTarget;");
// mSwapchainTryAcquire // TODO generics!?
mSwapchainPresent = env->GetMethodID(cSwapchain, "present", "(Lorg/eclipse/fx/drift/RenderTarget;)V");
mSwapchainDispose = env->GetMethodID(cSwapchain, "dispose", "()V");
mSwapchainGetConfig = env->GetMethodID(cSwapchain, "getConfig", "()Lorg/eclipse/fx/drift/SwapchainConfig;");
// Renderer
jclass cRenderer = env->FindClass("org/eclipse/fx/drift/Renderer");
jclass cRenderer = getClass(env, "org.eclipse.fx.drift.Renderer"); //env->FindClass("org/eclipse/fx/drift/Renderer");
mRendererGetSize = env->GetMethodID(cRenderer, "getSize", "()Lorg/eclipse/fx/drift/Vec2i;");
mRendererCreateSwapchain = env->GetMethodID(cRenderer, "createSwapchain", "(Lorg/eclipse/fx/drift/SwapchainConfig;)Lorg/eclipse/fx/drift/Swapchain;");
// GLRenderer
cGLRenderer = env->FindClass("org/eclipse/fx/drift/GLRenderer");
cGLRenderer = getClass(env, "org.eclipse.fx.drift.GLRenderer"); //env->FindClass("org/eclipse/fx/drift/GLRenderer");
cGLRenderer = (jclass)env->NewGlobalRef(cGLRenderer);
mGLRendererGetGLTextureId = env->GetStaticMethodID(cGLRenderer, "getGLTextureId", "(Lorg/eclipse/fx/drift/RenderTarget;)I");

jclass cStandardTransferTypes = getClass(env, "org.eclipse.fx.drift.StandardTransferTypes"); //env->FindClass("org/eclipse/fx/drift/StandardTransferTypes");
jfieldID fStandardTransferTypesMainMemory = env->GetStaticFieldID(cStandardTransferTypes, "MainMemory", "Lorg/eclipse/fx/drift/TransferType;");
jfieldID fStandardTransferTypesIOSurface = env->GetStaticFieldID(cStandardTransferTypes, "IOSurface", "Lorg/eclipse/fx/drift/TransferType;");
jfieldID fStandardTransferTypesNVDXInterop = env->GetStaticFieldID(cStandardTransferTypes, "NVDXInterop", "Lorg/eclipse/fx/drift/TransferType;");

jobject mainMemory = env->GetStaticObjectField(cStandardTransferTypes, fStandardTransferTypesMainMemory);
jobject ioSurface = env->GetStaticObjectField(cStandardTransferTypes, fStandardTransferTypesIOSurface);
jobject nvdxInterop = env->GetStaticObjectField(cStandardTransferTypes, fStandardTransferTypesNVDXInterop);

driftfx::StandardTransferTypes::MainMemory = new TransferTypeImpl(env, mainMemory);
driftfx::StandardTransferTypes::IOSurface = new TransferTypeImpl(env, ioSurface);
driftfx::StandardTransferTypes::NVDXInterop = new TransferTypeImpl(env, nvdxInterop);
}
void driftfx::JNI::dispose(JNIEnv* env) {
env->DeleteGlobalRef(classLoader);

// Class
env->DeleteGlobalRef(cClass);

// Vec2i
env->DeleteGlobalRef(cVec2i);
// SwapchainConfig
Expand Down Expand Up @@ -244,33 +280,23 @@ GLuint driftfx::GLRenderer::getGLTextureId(driftfx::RenderTarget* renderTarget)


driftfx::Renderer* driftfx::initializeRenderer(JNIEnv* env, jobject javaRenderer) {
return new driftfx::RendererImpl(env, javaRenderer);
}

// TODO - we want this only once!
driftfx::JNI::init(env);

// -- init transfer types -> TODO once!

jclass cStandardTransferTypes = env->FindClass("org/eclipse/fx/drift/StandardTransferTypes");
jfieldID fStandardTransferTypesMainMemory = env->GetStaticFieldID(cStandardTransferTypes, "MainMemory", "Lorg/eclipse/fx/drift/TransferType;");
jfieldID fStandardTransferTypesIOSurface = env->GetStaticFieldID(cStandardTransferTypes, "IOSurface", "Lorg/eclipse/fx/drift/TransferType;");
jfieldID fStandardTransferTypesNVDXInterop = env->GetStaticFieldID(cStandardTransferTypes, "NVDXInterop", "Lorg/eclipse/fx/drift/TransferType;");

jobject mainMemory = env->GetStaticObjectField(cStandardTransferTypes, fStandardTransferTypesMainMemory);
jobject ioSurface = env->GetStaticObjectField(cStandardTransferTypes, fStandardTransferTypesIOSurface);
jobject nvdxInterop = env->GetStaticObjectField(cStandardTransferTypes, fStandardTransferTypesNVDXInterop);

driftfx::StandardTransferTypes::MainMemory = new TransferTypeImpl(env, mainMemory);
driftfx::StandardTransferTypes::IOSurface = new TransferTypeImpl(env, ioSurface);
driftfx::StandardTransferTypes::NVDXInterop = new TransferTypeImpl(env, nvdxInterop);
driftfx::TransferType* driftfx::StandardTransferTypes::MainMemory = nullptr;
driftfx::TransferType* driftfx::StandardTransferTypes::IOSurface = nullptr;
driftfx::TransferType* driftfx::StandardTransferTypes::NVDXInterop = nullptr;

// --

return new driftfx::RendererImpl(env, javaRenderer);
extern "C" JNIEXPORT void JNICALL Java_org_eclipse_fx_drift_internal_DriftCPP_init(JNIEnv *env, jclass cls, jobject classLoader) {
driftfx::JNI::init(env, classLoader);
}

extern "C" JNIEXPORT void JNICALL Java_org_eclipse_fx_drift_internal_DriftCPP_dispose(JNIEnv *env, jclass cls) {
driftfx::JNI::dispose(env);
}



driftfx::TransferType* driftfx::StandardTransferTypes::MainMemory = nullptr;
driftfx::TransferType* driftfx::StandardTransferTypes::IOSurface = nullptr;
driftfx::TransferType* driftfx::StandardTransferTypes::NVDXInterop = nullptr;
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ namespace driftfx {

class JNI {
public:
static void init(JNIEnv* env);
static void init(JNIEnv* env, jobject classLoader);
static void dispose(JNIEnv* env);

private:
static jobject classLoader;

// Class
private:
static jclass cClass;
static jmethodID mClassForName;
public:
static jclass getClass(JNIEnv* env, const char* name);

// Vec2i
private:
Expand Down
1 change: 1 addition & 0 deletions native/driftfx/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'cpp-library'
id 'maven-publish'
id 'visual-studio'
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.eclipse.fx.drift.internal;

import org.eclipse.fx.drift.util.NativeUtil;

public class DriftCPP {

static {
NativeUtil.loadLibrary(DriftCPP.class, "driftcpp");
}

static native void init(ClassLoader classLoader);
static native void dispose();

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public static boolean isMacOs() {
public static void loadLibrary(Class<?> context, String libname) {
if (USE_JAVA_LIBRARY_PATH || osgi) {
// osgi will take care of it
Log.info("loading " + libname + " via system call");
System.loadLibrary(libname);
}
else {

// we need to make it happen

String filename = getFilename(libname);
Expand All @@ -56,12 +58,14 @@ public static void loadLibrary(Class<?> context, String libname) {
URL url = context.getResource(resourceName);
Log.debug("Resource Lookup: name: " + resourceName + ", context: " + context + " => " + url);


try (InputStream in = context.getResourceAsStream("/native/" + filename)) {
extract(in, extractPath);
} catch (IOException e) {
e.printStackTrace();
}

Log.info("loading " + libname + " from extracted location (" + extractPath + ")");
System.load(extractPath.toString());
}
}
Expand Down

0 comments on commit 153ff59

Please sign in to comment.