From eb6b0ebe4b2acda361927003f0fd72504f08512a Mon Sep 17 00:00:00 2001 From: retgal Date: Thu, 19 Dec 2024 00:25:03 +0100 Subject: [PATCH] Optimize tile creation --- .../dayon/assisted/capture/CaptureEngine.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/mpo/dayon/assisted/capture/CaptureEngine.java b/src/main/java/mpo/dayon/assisted/capture/CaptureEngine.java index f3a63368..0b5dd08f 100644 --- a/src/main/java/mpo/dayon/assisted/capture/CaptureEngine.java +++ b/src/main/java/mpo/dayon/assisted/capture/CaptureEngine.java @@ -165,10 +165,10 @@ private static int syncOnTick(final long start, final int captureCount, final in while (true) { final long captureMaxEnd = start + (captureCount + delayedCaptureCount) * tick; final long capturePause = captureMaxEnd - System.currentTimeMillis(); - if (capturePause < 0) { + if (capturePause < 0L) { ++delayedCaptureCount; - Log.warn(format("Skipping capture (%d) %s", captureId + delayedCaptureCount, UnitUtilities.toElapsedTime(-capturePause))); - } else if (capturePause > 0) { + Log.warn(format("Skipping capture (%s) %s", captureId + delayedCaptureCount, UnitUtilities.toElapsedTime(-capturePause))); + } else if (capturePause > 0L) { try { TimeUnit.MILLISECONDS.sleep(capturePause); } catch (InterruptedException e) { @@ -210,7 +210,7 @@ private CaptureTile[] computeDirtyTiles(byte[] capture) { for (int ty = 0; ty < captureDimension.height; ty += TILE_DIMENSION.height) { final int th = min(captureDimension.height - ty, TILE_DIMENSION.height); for (int tx = 0; tx < captureDimension.width; tx += TILE_DIMENSION.width) { - final int tw = Math.min(captureDimension.width - tx, TILE_DIMENSION.width); + final int tw = min(captureDimension.width - tx, TILE_DIMENSION.width); tileData = createTile(capture, captureDimension.width, tw, th, tx, ty, pixelSize); final long cs = CaptureTile.computeChecksum(tileData, 0, tileData.length); if (cs != previousCapture[tileId]) { @@ -230,13 +230,14 @@ private static byte[] createTile(byte[] capture, int width, int tw, int th, int final int capacity = tw * th * pixelSize; final byte[] tile = new byte[capacity]; final int maxSrcPos = capture.length; - final int maxDestPos = capacity - tw * pixelSize + 1; int srcPos = ty * width * pixelSize + tx * pixelSize; int destPos = 0; - while (destPos < maxDestPos && srcPos < maxSrcPos) { - System.arraycopy(capture, srcPos, tile, destPos, tw * pixelSize); - srcPos += width * pixelSize; - destPos += tw * pixelSize; + final int screenRowIncrement = width * pixelSize; + final int tileRowIncrement = tw * pixelSize; + while (destPos < capacity && srcPos < maxSrcPos) { + System.arraycopy(capture, srcPos, tile, destPos, tileRowIncrement); + srcPos += screenRowIncrement; + destPos += tileRowIncrement; } return tile; }