Directly using VirtualBox from Java #5

Merged
NorbiPeti merged 60 commits from directvb into master 2019-04-18 23:29:21 +00:00
3 changed files with 73 additions and 6 deletions
Showing only changes of commit f029fc22ec - Show all commits

View file

@ -5,10 +5,16 @@ import com.aparapi.Range;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import jnr.ffi.LibraryLoader;
import org.bukkit.Color;
import org.bukkit.map.MapPalette;
import java.io.File;
import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class Test {
public static void main(String[] args) {
@ -20,18 +26,47 @@ public class Test {
System.out.println(bb.get(0)); //19 AYY //TO!DO: Use setSource, we don't want to wrap the native array*/
//final int[] a={5,6,8,2,3,10,5,26,5,416,41,85,416,41};
//final int[] b={10,80,10,3,32,20,56,85,51,968,156,5894,10,60,52};
final int[] a={5,6};
final int[] b={10,80};
//final int[] a={5,6};
//final int[] b={10,80};
long t=System.nanoTime();
int[] a= IntStream.range(0, 640*480).toArray();
final int[] res=new int[a.length];
Kernel kernel=new Kernel() {
@Override
public void run() {
int i=getGlobalId();
//System.out.println(i);
res[i]=a[i]+b[i];
//res[i]=a[i]+b[i];
Color c=Color.fromBGR((int)a[i]);
res[i]= MapPalette.matchColor(c.getRed(), c.getGreen(), c.getBlue());
}
};
kernel.execute(Range.create(res.length));
System.out.println(Arrays.toString(res));
//System.out.println(Arrays.toString(res));
System.out.println(a[10]);
System.out.println("OpenCL time: "+(System.nanoTime()-t)/1000000f+"ms");
t=System.nanoTime();
for (int i = 0; i < res.length; i++) {
Color c=Color.fromBGR((int)a[i]);
res[i]= MapPalette.matchColor(c.getRed(), c.getGreen(), c.getBlue());
}
System.out.println("For loop time: "+(System.nanoTime()-t)/1000000f+"ms");
t=System.nanoTime();
System.arraycopy(a, 0, res, 0, res.length);
System.out.println("Sys time: "+(System.nanoTime()-t)/1000000f+"ms");
PXCLib pxc = LibraryLoader.create(PXCLib.class).search(new File("").getAbsolutePath()).load("pxc");
ByteBuffer bb=ByteBuffer.allocateDirect(640*480);
try {
Field f=Buffer.class.getDeclaredField("address");
f.setAccessible(true);
long addr= (long) f.get(bb);
pxc.setSource(addr, 640, 480, 5, 4);
pxc.updateAndGetMap(0, 0, 640, 480, null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}

View file

@ -38,6 +38,18 @@ void setSource(addr address, short w, short h, short mcx, short mcy) {
image=(void*)address;
maps=malloc(MAPSIZE*MAPSIZE*mcx*mcy); //1 byte per pixel
width=w, height=h, mapcx=mcx, mapcy=mcy;
printf("PXC: w=%d h=%d mcx=%d mcy=%d\n", w, h, mcx, mcy);
}
NativeColor* getNativeColor(int x, int y) {
if(x<0||x>width||y<0||y>height) return NULL;
return ((NativeColor*)image)+x+y*width;
}
char* getMapColor(int x, int y) {
if(x<0||x>width||y<0||y>height) return NULL;
int mc=x/MAPSIZE+y/MAPSIZE*mapcx;
return maps+mc*MAPSIZE*MAPSIZE; //TODO: Use
}
//May return 0
@ -45,16 +57,25 @@ void* updateAndGetMap(int x, int y, int w, int h, int** out_changed) { //TODO: S
if(image==NULL || maps==NULL) return 0;
char* mapp = maps;
NativeColor* imgp = image;
printf("PXC: mapp=%p imgp=%p\n", mapp, imgp);
for(int k=0; k<mapcx; k++) {
printf("PXC: k=%d\n", k);
for(int l=0; l<mapcy; l++) {
printf("PXC: l=%d\n", l);
mapp = maps + MAPSIZE*k + MAPSIZE*mapcx*MAPSIZE*l; //Go to the start of the map
printf("PXC: mapp=%p imgp=%p - %d\n", mapp, imgp, *mapp);
for(short i=0; i<MAPSIZE; i++) { //We need to jump
for(short j=0; j<MAPSIZE; j++) {
*mapp=matchColor(*imgp);
//*mapp=matchColor(*imgp); - TODO
NativeColor x={.red=0, .green=0, .blue=255};
*mapp=matchColor(x);
printf("PXC: imgp: %ld - %x\n", (void*)imgp-image, *imgp); //TODO: The bottom of the image
printf("PXC: mapcx: %d mapcy: %d\n", k, l); //TODO: Has nothing
imgp++; //Increment by the size of NativeColor so 4
mapp++;
}
imgp+=width-MAPSIZE; //Go back to the first column
//printf("Row done, imgp=%p\n", imgp);
}
imgp+=MAPSIZE; //Go to the next map, kind of - TODO: Use X and Y coords!
if((void*)mapp - maps > MAPSIZE * MAPSIZE * mapcx * mapcy ||
@ -64,6 +85,7 @@ void* updateAndGetMap(int x, int y, int w, int h, int** out_changed) { //TODO: S
}
}
}
printf("PXC: ret maps=%p\n", maps);
return maps; //TODO: Return changed only
}

View file

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
/* https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
gcc -Wall -c -fpic pxc.c
@ -8,8 +9,17 @@ gcc -Wall pxct.c -L. -lpxc -Wl,-rpath=.
int convert(int px[], int *out[]);
typedef long long int addr;
void setSource(addr address, short w, short h, short mcx, short mcy);
void* updateAndGetMap(int x, int y, int w, int h, int** out_changed);
int main() {
convert(NULL, NULL);
printf("Setting source...");
void* p = malloc(640*480*4);
setSource((addr)p, 640, 480, 5, 4);
printf("Updating map...");
void* x=updateAndGetMap(0, 0, 640, 480, NULL);
free(p);
return 0;
}