From 18e1981a2ae47b267cea66e4de6cb00d6330885a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 10 Apr 2019 17:59:36 +0200 Subject: [PATCH] Only sending changed map data --- .../java/sznp/virtualcomputer/Computer.java | 3 ++- .../virtualcomputer/renderer/GPURenderer.java | 25 ++++++++++++++++--- .../renderer/GPURendererInternal.java | 2 +- .../renderer/MCFrameBuffer.java | 4 +-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java index 40cc76e..c29c184 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java @@ -8,6 +8,7 @@ import org.bukkit.command.CommandSender; import org.virtualbox_6_0.*; import sznp.virtualcomputer.events.MachineEventHandler; import sznp.virtualcomputer.events.VBoxEventHandler; +import sznp.virtualcomputer.renderer.GPURenderer; import sznp.virtualcomputer.renderer.GPURendererInternal; import sznp.virtualcomputer.renderer.MCFrameBuffer; import sznp.virtualcomputer.util.Scancode; @@ -207,7 +208,7 @@ public final class Computer { sendMessage(sender, "§eComputer powered off."); //This block runs later } }); - GPURendererInternal.setPixels(new byte[1], 0, 0); //Black screen + GPURenderer.update(new byte[1], 0, 0, 0, 0, 640, 480); //Black screen stopEvents(); MouseLockerPlayerListener.computerStop(); } diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java index e67a101..0551f3e 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java @@ -16,8 +16,10 @@ public class GPURenderer extends MapRenderer implements IRenderer { private byte[] buffer; private GPURendererInternal kernel; private WorldMap wmap; + private int mapx, mapy; //Store at central location after conversion private static int[] colors_; + private static int changedX, changedY, changedWidth, changedHeight; public GPURenderer(short id, World world, int mapx, int mapy) throws Exception { MapView map = IRenderer.prepare(id, world); @@ -32,6 +34,8 @@ public class GPURenderer extends MapRenderer implements IRenderer { colors_[i] = cs[i].getRGB(); } } + this.mapx = mapx; + this.mapy = mapy; Field field = map.getClass().getDeclaredField("worldMap"); field.setAccessible(true); wmap = (WorldMap) field.get(map); @@ -46,19 +50,34 @@ public class GPURenderer extends MapRenderer implements IRenderer { public void render(MapView map, MapCanvas canvas, Player player) { Timing t = new Timing(); try { - if (kernel.isRendered()) return; //TODO: Stop rendering after computer is stopped + if (kernel.isRendered()) return; if (buffer == null) { //The buffer remains the same, as the canvas remains the same Field field = canvas.getClass().getDeclaredField("buffer"); field.setAccessible(true); buffer = (byte[]) field.get(canvas); } + if (changedX >= (mapx + 1) * 128 || changedY >= (mapy + 1) * 128 + || changedX + changedWidth < mapx * 128 || changedY + changedHeight < mapy * 128) + return; //No change for this map - TODO: Test + int x = changedX % 128; + int y = changedY % 128; + int w = x + changedWidth >= 128 ? 128 - x - 1 : changedWidth; + int h = y + changedHeight >= 128 ? 128 - y - 1 : changedHeight; kernel.render(buffer); - wmap.flagDirty(0, 0); - wmap.flagDirty(127, 127); // Send the whole image - TODO: Only send changes + wmap.flagDirty(x, y); + wmap.flagDirty(x + w, y + h); // Send the changes only } catch (Exception e) { e.printStackTrace(); } if (t.elapsedMS() > 60) System.out.println("Map rendering took " + t.elapsedMS() + "ms"); } + + public static void update(byte[] pixels, int width, int height, int changedX, int changedY, int changedWidth, int changedHeight) { + GPURenderer.changedX = changedX; + GPURenderer.changedY = changedY; + GPURenderer.changedWidth = changedWidth; + GPURenderer.changedHeight = changedHeight; + GPURendererInternal.setPixels(pixels, width, height); + } } diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java index 636bdf3..e1a723f 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java @@ -95,7 +95,7 @@ public class GPURendererInternal extends Kernel { private static final int BLUE = 0; @SuppressWarnings("Convert2Lambda") //Aparapi fails with lambdas - public static void setPixels(byte[] pixels, int width, int height) { + static void setPixels(byte[] pixels, int width, int height) { renderers.forEach(new Consumer() { @Override //IT'S THE LAMBDAS (exception) public void accept(GPURendererInternal r) { diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/MCFrameBuffer.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/MCFrameBuffer.java index c8a5d6b..a4c49da 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/MCFrameBuffer.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/renderer/MCFrameBuffer.java @@ -106,7 +106,7 @@ public class MCFrameBuffer implements IFramebuffer { pointer = new Pointer(ptr[0]); this.width = (int) w[0]; this.height = (int) h[0]; - GPURendererInternal.setPixels(pointer.getByteArray(0L, (int) (w[0] * h[0] * 4)), (int) w[0], (int) h[0]); + GPURenderer.update(pointer.getByteArray(0L, (int) (w[0] * h[0] * 4)), (int) w[0], (int) h[0], 0, 0, this.width, this.height); } else { PluginMain.allpixels = new Pointer(ptr[0]).getByteBuffer(0L, width * height * 4); if (width * height > 640 * 480) @@ -129,7 +129,7 @@ public class MCFrameBuffer implements IFramebuffer { @Override public void notifyUpdate(long x, long y, long width, long height) { Timing t = new Timing(); - GPURendererInternal.setPixels(pointer.getByteArray(0L, (this.width * this.height * 4)), this.width, this.height); //TODO: Only copy changed part + GPURenderer.update(pointer.getByteArray(0L, this.width * this.height * 4), this.width, this.height, (int) x, (int) y, (int) width, (int) height); if (t.elapsedMS() > 60) //Typically 1ms max System.out.println("Update took " + t.elapsedMS() + "ms"); }