Updated JNI tests

This commit is contained in:
Andrea Cavalli 2017-12-13 22:34:15 +01:00
parent d5f0514208
commit 85d1e7d713
13 changed files with 232 additions and 53 deletions

View File

@ -56,7 +56,6 @@
<buildTargets>
<target name="TestJNI.h" path="src/main/jni" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>TestJNI.h</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -64,7 +63,6 @@
</target>
<target name="all" path="src/main/jni" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>all</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -72,12 +70,19 @@
</target>
<target name="clean" path="src/main/jni" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>clean</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="windows" path="src/main/jni" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>picalculatornative.dll</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
</buildTargets>
</storageModule>
</cproject>

33
MmapByteBuffer.java Normal file
View File

@ -0,0 +1,33 @@
package org.warp.picalculator;
import java.nio.ByteBuffer;
public class MmapByteBuffer {
private int fd;
private int address;
private int length;
private ByteBuffer buffer;
public MmapByteBuffer(int fd, int address, int length, ByteBuffer buffer) {
this.fd = fd;
this.address = address;
this.length = length;
this.buffer = buffer;
}
public int getFd() {
return fd;
}
public int getAddress() {
return address;
}
public int getLength() {
return length;
}
public ByteBuffer getBuffer() {
return buffer;
}
}

View File

@ -0,0 +1,34 @@
package org.warp.picalculator;
import java.nio.ByteBuffer;
public class MmapByteBuffer {
private int fd;
private int address;
private int length;
private ByteBuffer buffer;
public MmapByteBuffer(int fd, int address, int length, ByteBuffer buffer) {
this.fd = fd;
this.address = address;
this.length = length;
this.buffer = buffer;
}
public int getFd() {
return fd;
}
public int getAddress() {
return address;
}
public int getLength() {
return length;
}
public ByteBuffer getBuffer() {
return buffer;
}
}

View File

@ -1,21 +1,31 @@
package org.warp.picalculator;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.warp.picalculator.MmapByteBuffer;
import cz.adamh.utils.NativeUtils;
public class TestJNI {
static {
try {
NativeUtils.loadLibraryFromJar("/picalculatornative.dll");
} catch (IOException e) {
e.printStackTrace(); // This is probably not the best way to handle exception :-)
}
}
private native void sayHello();
public TestJNI() {
public static void main(String[] args) {
// invoke the native method
new TestJNI().sayHello();
}
}
static {
System.load("/boot/libpicalc.so");
}
private native MmapByteBuffer getDisplayBuffer();
private native void disposeDisplayBuffer();
public MmapByteBuffer retrieveBuffer() {
return getDisplayBuffer();
}
public void deleteBuffer() {
disposeDisplayBuffer();
}
}

View File

@ -8,6 +8,8 @@ import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.Semaphore;
import org.warp.picalculator.MmapByteBuffer;
import org.warp.picalculator.TestJNI;
import org.warp.picalculator.Utils;
import org.warp.picalculator.gui.graphicengine.BinaryFont;
import org.warp.picalculator.gui.graphicengine.GraphicEngine;
@ -27,13 +29,16 @@ public class FBEngine implements GraphicEngine {
private static final int WIDTH = 480;
private static final int HEIGHT = 320;
private static final int[] SIZE = new int[] {WIDTH, HEIGHT};
private TestJNI jni = new TestJNI();
public FBRenderer r;
private MappedByteBuffer fb, realFb;
private MappedByteBuffer fb;
MmapByteBuffer realFb;
private RandomAccessFile fbFileRW;
public volatile boolean initialized = false;
public Semaphore exitSemaphore = new Semaphore(0);
private boolean resizedTrigger = false;
@Override
public int[] getSize() {
return SIZE;
@ -59,22 +64,15 @@ public class FBEngine implements GraphicEngine {
@Override
public void create(Runnable onInitialized) {
resizedTrigger = true;
File fbFile = new File("/dev/fb1");
try {
fbFileRW = new RandomAccessFile(fbFile, "rw");
final long fbLen = FB_DISPLAY_HEIGHT*FB_DISPLAY_WIDTH*(FB_DISPLAY_BPP/8);
fb = (MappedByteBuffer) MappedByteBuffer.allocateDirect((int) fbLen);
realFb = fbFileRW.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, fbLen);
realFb = jni.retrieveBuffer();
final long fbLen = realFb.getLength();
fb = (MappedByteBuffer) MappedByteBuffer.allocateDirect((int) fbLen);
r = new FBRenderer(this, fb);
r = new FBRenderer(this, fb);
initialized = true;
if (onInitialized != null)
onInitialized.run();
} catch (IOException ex) {
System.err.println("Cannot read framebuffer fb1");
ex.printStackTrace();
}
initialized = true;
if (onInitialized != null)
onInitialized.run();
}
@Override
@ -144,15 +142,14 @@ public class FBEngine implements GraphicEngine {
_________________TMP = 0;
}
_________________TMP++;
realFb.clear();
realFb.put(fb);
realFb.getBuffer().clear();
realFb.getBuffer().put(fb);
for (int i = 0; i < fb.capacity()/2; i++) {
realFb.put(i, (byte) (0xFF));
realFb.getBuffer().put(i, (byte) (0xFF));
}
for (int i = fb.capacity()/2; i < fb.capacity(); i++) {
realFb.put(i, (byte) (0x18));
realFb.getBuffer().put(i, (byte) (0x18));
}
realFb.force();
}
@Override

1
src/main/jni/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/buffermanager.c

View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 2.8)
project(picalculatornative)
SET(COMPILE_DEFINITIONS -Werror)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
include_directories(/opt/vc/include)
include_directories(/opt/vc/include/interface/vcos/pthreads)
include_directories(/opt/vc/include/interface/vmcs_host)
include_directories(/opt/vc/include/interface/vmcs_host/linux)
link_directories(/opt/vc/lib)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES TestJNI.cpp)
add_library(picalculatornative SHARED ${SOURCE_FILES})

View File

@ -1,9 +0,0 @@
#include <jni.h>
#include <stdio.h>
#include "TestJNI.h"
JNIEXPORT void JNICALL Java_org_warp_picalculator_TestJNI_sayHello(JNIEnv *env, jobject thisObj)
{
printf("Hello World!\n");
return;
}

75
src/main/jni/TestJNI.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <jni.h>
#include <stdio.h>
#include <stdio.h>
#include <syslog.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "TestJNI.h"
int fbfd = -1;
char *fbp = nullptr;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
JNIEXPORT void JNICALL Java_org_warp_picalculator_TestJNI_disposeDisplayBuffer(JNIEnv *env, jobject thisObj)
{
if (fbp != nullptr) {
munmap(fbp, finfo.smem_len);
fbp = nullptr;
}
if (fbfd != -1) {
close(fbfd);
fbfd = -1;
}
return;
}
JNIEXPORT jobject JNICALL Java_org_warp_picalculator_TestJNI_getDisplayBuffer(JNIEnv *env, jobject thisObj)
{
syslog(LOG_INFO, "[JNI NATIVE] INIT");
fbfd = open("/dev/fb1", O_RDWR);
if (fbfd == -1) {
syslog(LOG_ERR, "Unable to open secondary display");
return NULL;
}
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
syslog(LOG_ERR, "Unable to get secondary display information");
return NULL;
}
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
syslog(LOG_ERR, "Unable to get secondary display information");
return NULL;
}
syslog(LOG_INFO, "Second display is %d x %d %dbps\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
fbp = (char*) mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if (fbp <= 0) {
syslog(LOG_ERR, "Unable to create mamory mapping");
close(fbfd);
return NULL;
}
char* class_name = "org/warp/picalculator/MmapByteBuffer";
jclass clz = env->FindClass(class_name);
if (clz == NULL) {
printf("Error, could not find class '%s'\n", class_name);
return NULL;
}
char* signature = "(IIILjava/nio/ByteBuffer;)V";
jmethodID constructor = env->GetMethodID( clz, "<init>", signature);
if (constructor == NULL) {
printf("Error, could not find constructor %s %s\n", class_name, signature);
return NULL;
}
syslog(LOG_INFO, "[JNI NATIVE] DONE");
return env->NewObject(clz, constructor, fbfd, fbp, finfo.smem_len,
env->NewDirectByteBuffer(fbp, finfo.smem_len));
}

View File

@ -9,10 +9,18 @@ extern "C" {
#endif
/*
* Class: org_warp_picalculator_TestJNI
* Method: sayHello
* Method: getDisplayBuffer
* Signature: ()Lorg/warp/picalculator/MmapByteBuffer;
*/
JNIEXPORT jobject JNICALL Java_org_warp_picalculator_TestJNI_getDisplayBuffer
(JNIEnv *, jobject);
/*
* Class: org_warp_picalculator_TestJNI
* Method: disposeDisplayBuffer
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_warp_picalculator_TestJNI_sayHello
JNIEXPORT void JNICALL Java_org_warp_picalculator_TestJNI_disposeDisplayBuffer
(JNIEnv *, jobject);
#ifdef __cplusplus

Binary file not shown.

View File

@ -6,16 +6,24 @@ PATH1B = org.warp.picalculator
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)/$(PATH1A)
all : picalculatornative.dll
all : picalculatornative.so
# $@ matches the target, $< matches the first dependancy
picalculatornative.dll : TestJNI.o
picalculatornative.so : TestJNI.o
gcc -m64 -mtune=generic -Wl,--add-stdcall-alias -shared -o ../resources/$@ $<
# $@ matches the target, $< matches the first dependancy
picalculatornative.dll : windows_TestJNI.o
x86_64-w64-mingw32-gcc.exe -m64 -mtune=generic -Wl,--add-stdcall-alias -shared -o ../resources/$@ $<
# $@ matches the target, $< matches the first dependancy
TestJNI.o : TestJNI.c TestJNI.h
windows_TestJNI.o : TestJNI.c TestJNI.h
x86_64-w64-mingw32-gcc.exe -m64 -mtune=generic -I"G:\Program Files\Java\jdk1.8.0_144\include" -I"G:\Program Files\Java\jdk1.8.0_144\include\win32" -c $< -o $@
# $@ matches the target, $< matches the first dependancy
TestJNI.o : TestJNI.c TestJNI.h
gcc -m64 -mtune=generic -I"G:\Program Files\Java\jdk1.8.0_144\include" -I"G:\Program Files\Java\jdk1.8.0_144\include\win32" -c $< -o $@
# $* matches the target filename without the extension
TestJNI.h : TestJNI.class
javah -o "$*.h" -classpath "$(CLASS_PATH)" $(PATH1B).$*